Commit 2f854a90 authored by Daniel Honsel's avatar Daniel Honsel
Browse files

fixed some naming convention checks

parent a3e69430
Loading
Loading
Loading
Loading
+111 −47
Original line number Diff line number Diff line
@@ -4,12 +4,16 @@ import com.google.common.base.Strings
import de.ugoe.cs.swe.TTCN3Configuration.QualityCheckProfile
import de.ugoe.cs.swe.common.ConfigTools
import de.ugoe.cs.swe.common.MiscTools
import de.ugoe.cs.swe.common.logging.LoggingInterface.LogLevel
import de.ugoe.cs.swe.common.logging.LoggingInterface.MessageClass
import de.ugoe.cs.swe.tTCN3.AltstepDef
import de.ugoe.cs.swe.tTCN3.BaseTemplate
import de.ugoe.cs.swe.tTCN3.ComponentDef
import de.ugoe.cs.swe.tTCN3.ComponentElementDef
import de.ugoe.cs.swe.tTCN3.ConstDef
import de.ugoe.cs.swe.tTCN3.EnumDefNamed
import de.ugoe.cs.swe.tTCN3.Enumeration
import de.ugoe.cs.swe.tTCN3.ExtConstDef
import de.ugoe.cs.swe.tTCN3.ExtFunctionDef
import de.ugoe.cs.swe.tTCN3.FormalPortPar
import de.ugoe.cs.swe.tTCN3.FormalTemplatePar
@@ -20,10 +24,15 @@ import de.ugoe.cs.swe.tTCN3.GroupDef
import de.ugoe.cs.swe.tTCN3.MatchingSymbol
import de.ugoe.cs.swe.tTCN3.ModuleDefinition
import de.ugoe.cs.swe.tTCN3.ModuleParameter
import de.ugoe.cs.swe.tTCN3.PortDef
import de.ugoe.cs.swe.tTCN3.PortElement
import de.ugoe.cs.swe.tTCN3.RecordDefNamed
import de.ugoe.cs.swe.tTCN3.RecordOfDefNamed
import de.ugoe.cs.swe.tTCN3.SetDefNamed
import de.ugoe.cs.swe.tTCN3.SetOfDefNamed
import de.ugoe.cs.swe.tTCN3.SignatureDef
import de.ugoe.cs.swe.tTCN3.SingleConstDef
import de.ugoe.cs.swe.tTCN3.SingleTempVarInstance
import de.ugoe.cs.swe.tTCN3.SingleVarInstance
import de.ugoe.cs.swe.tTCN3.SubTypeDefNamed
import de.ugoe.cs.swe.tTCN3.TTCN3Module
@@ -32,6 +41,7 @@ import de.ugoe.cs.swe.tTCN3.TTCN3Reference
import de.ugoe.cs.swe.tTCN3.TemplateDef
import de.ugoe.cs.swe.tTCN3.TestcaseDef
import de.ugoe.cs.swe.tTCN3.TimerInstance
import de.ugoe.cs.swe.tTCN3.UnionDefNamed
import java.util.regex.Matcher
import java.util.regex.Pattern
import org.eclipse.xtext.nodemodel.INode
@@ -42,12 +52,6 @@ import org.eclipse.xtext.validation.EValidatorRegistrar

import static extension de.ugoe.cs.swe.common.TTCN3ScopeHelper.*
import static extension org.eclipse.xtext.EcoreUtil2.*
import de.ugoe.cs.swe.tTCN3.SingleTempVarInstance
import de.ugoe.cs.swe.tTCN3.ComponentDef
import de.ugoe.cs.swe.tTCN3.SetDefNamed
import de.ugoe.cs.swe.tTCN3.RecordDefNamed
import de.ugoe.cs.swe.tTCN3.UnionDefNamed
import de.ugoe.cs.swe.tTCN3.PortDef

public class NamingConventionsValidator extends AbstractDeclarativeValidator {
	val ConfigTools configTools = ConfigTools.getInstance;
@@ -86,12 +90,15 @@ public class NamingConventionsValidator extends AbstractDeclarativeValidator {
		const.checkIdentifierForNamingConventionCompliance(regExp, message)
	}

	// TODO: fix external constants
	// @Check
	// def checkExtConstantnName(ExtConstDef const) {
	// val regExp = activeProfile.namingConventionsConfig.extFunctionRegExp;
	// const.checkIdentifierForNamingConventionCompliance(regExp, "External Constant")
	// }	
	 @Check
	 def checkExtConstantnName(ExtConstDef const) {
	 	val regExp = activeProfile.namingConventionsConfig.extConstantRegExp;
	 	val INode node = NodeModelUtils.getNode(const)
	 	for (n : const.id.ids) {
	 		n.toString.checkIdentifierForNamingConventionCompliance(node, regExp, "External Constant")
	 	}	 	
	 }
		
	@Check
	def checkAltstepName(AltstepDef altstep) {
		val regExp = activeProfile.namingConventionsConfig.altstepRegExp;
@@ -200,24 +207,39 @@ public class NamingConventionsValidator extends AbstractDeclarativeValidator {
		dataType.checkIdentifierForNamingConventionCompliance(regExp, "DataType")
	}
	
	@Check
	def checkSetOfTypeName(EnumDefNamed dataType) {
		val regExp = activeProfile.namingConventionsConfig.dataTypeRegExp;
		dataType.checkIdentifierForNamingConventionCompliance(regExp, "DataType")
	}	
	
	@Check
	def checkSetOfTypeName(PortElement port) {
		val regExp = activeProfile.namingConventionsConfig.portInstanceRegExp;
		port.checkIdentifierForNamingConventionCompliance(regExp, "PortInstance")
	}	
	
	// TODO: check if something more is required (see implementation of old t3tools)
	// Timer instances are included here
	@Check
	def checkVariableName(SingleVarInstance variable) {
		var regExp = ""
		var message = ""
		val TimerInstance timer = variable.findDesiredParent(TimerInstance)
		val ComponentElementDef componend = variable.findDesiredParent(ComponentElementDef)
		val type = variable.findVariableType

		if (timer != null && componend != null) {
			regExp = activeProfile.namingConventionsConfig.componentTimerRegExp
			message = "ComponentTimer"
		} else if (timer != null && componend == null) {
			regExp = activeProfile.namingConventionsConfig.timerRegExp
			message = "Timer"
		} else if (timer == null && componend != null) {
		if (componend != null) {
			regExp = activeProfile.namingConventionsConfig.componentVariableRegExp
			message = "ComponentVariable"
		} else if (type != null) {
			val refType = type.getReferencedType
			if (refType instanceof ComponentDef) {
				regExp = activeProfile.namingConventionsConfig.componentInstanceRegExp
				message = "ComponentInstance"
			} else {
				// TODO: information about not resolved type...
				regExp = activeProfile.namingConventionsConfig.variableRegExp
				message = "Variable"
			}		
		} else {
			regExp = activeProfile.namingConventionsConfig.variableRegExp
			message = "Variable"
@@ -225,6 +247,24 @@ public class NamingConventionsValidator extends AbstractDeclarativeValidator {
		variable.checkIdentifierForNamingConventionCompliance(regExp, message)
	}
	
	@Check
	def checkTimerInstances(TimerInstance timer) {
		val componend = timer.findDesiredParent(ComponentDef)
		var regExp = ""
		var message = ""
				
		for (v : timer.list.variables) {
			if (componend != null) {
				regExp = activeProfile.namingConventionsConfig.componentTimerRegExp
				message = "ComponentTimer"
			} else {
				regExp = activeProfile.namingConventionsConfig.timerRegExp
				message = "Timer"
			}	
			v.checkIdentifierForNamingConventionCompliance(regExp, message)
		}
	}
	
	@Check
	def checkVariableName(SingleTempVarInstance variable) {
		var regExp = activeProfile.namingConventionsConfig.variableRegExp
@@ -238,7 +278,7 @@ public class NamingConventionsValidator extends AbstractDeclarativeValidator {
		var message = ""
		val templateDef = template.findDesiredParent(TemplateDef)
		val isDerived = templateDef.derived != null
		val matchingSymbols = template.eAllOfType(MatchingSymbol)
		val matchingSymbols = templateDef.body.eAllOfType(MatchingSymbol)

		if (!isDerived) {
			if (matchingSymbols.empty) {
@@ -258,7 +298,7 @@ public class NamingConventionsValidator extends AbstractDeclarativeValidator {
			}
		}

		// check meaage template conventions
		// check message template conventions
		template.checkIdentifierForNamingConventionCompliance(regExp, message)
	}

@@ -276,6 +316,7 @@ public class NamingConventionsValidator extends AbstractDeclarativeValidator {

		// check if it is not a present restriction, but a restriction
		if (templateDef.restriction != null && templateDef.restriction.present == null) {
			if (template.parList != null && template.parList.params != null) {
				for (p : template.parList.params) {
					if (p.template != null) {
						if (p.template.restriction == null || p.template.restriction.restriction == null ||
@@ -285,21 +326,22 @@ public class NamingConventionsValidator extends AbstractDeclarativeValidator {
					}
					if (isAmbiguous) {
						TTCN3StatisticsProvider.getInstance.increaseCountNaming
					val INode node = NodeModelUtils.getNode(template)
						val INode node = NodeModelUtils.getNode(templateDef)
						val infoMessage = "The template definition for \"" + template.name +
							"\" is ambiguous. It cannot be determined whether it is a send or a receive template according to STF160's conventions. It will not be analyzed further for naming conventions compliance."
					warning(
						info(
							infoMessage,
							TTCN3Package.eINSTANCE.TTCN3Reference_Name,
							MessageClass.NAMING.toString,
							node.startLine.toString,
							node.endLine.toString,
						"2.1, " + MiscTools.getMethodName()
							"2.1, " + MiscTools.getMethodName(),
							LogLevel.INFORMATION.toString
						);
						return;
					}
				}

			}
			// if no problems occurred this far check name for send template
			isSendTemplate = true;
		} // (else) check name for receive template
@@ -331,6 +373,28 @@ public class NamingConventionsValidator extends AbstractDeclarativeValidator {
		return matcher.matches
	}
	
	def private checkIdentifierForNamingConventionCompliance(String name, INode node, String regExp, String type) {
		if (Strings.isNullOrEmpty(regExp))
			return;

		if (!activeProfile.checkNamingConventions)
			return;

		if (!regExp.regExpMatch(name)) {
			TTCN3StatisticsProvider.getInstance.increaseCountNaming
			val message = "\"" + name + "\" does not comply to the naming conventions for \"" + type + "\"!"
			warning(
				message,
				null,
				MessageClass.NAMING.toString,
				node.startLine.toString,
				node.endLine.toString,
				"2.1, " + regExp
			);
			return;
		}
	}	
	
	def private checkIdentifierForNamingConventionCompliance(TTCN3Reference object, String regExp, String type) {
		if (object == null || Strings.isNullOrEmpty(regExp))
			return;
@@ -345,7 +409,7 @@ public class NamingConventionsValidator extends AbstractDeclarativeValidator {
			val message = "\"" + object.name + "\" does not comply to the naming conventions for \"" + type + "\"!"
			warning(
				message,
				TTCN3Package.eINSTANCE.TTCN3Reference_Name,
				null,
				MessageClass.NAMING.toString,
				node.startLine.toString,
				node.endLine.toString,