Commit 6d56f5f2 authored by Daniel Honsel's avatar Daniel Honsel
Browse files

completed code style validation

parent 89318d0b
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -1081,8 +1081,8 @@ RefValue:

	// IDENTIFIER omitted
PredefinedValue:
	value=BSTRING | bValue=BooleanValue | value=NUMBER | value=HSTRING | value=OSTRING | vValue=VerdictTypeValue |
	value=FloatValue | value=ADDRESSVALUE | value=OMITKEYWORD | value=CharStringValue;
	bstring=BSTRING | boolean=BooleanValue | integer=NUMBER | hstring=HSTRING | ostring=OSTRING | verdictType=VerdictTypeValue |
	float=FloatValue | address=ADDRESSVALUE | omit=OMITKEYWORD | charString=CharStringValue;

BooleanExpression:
	SingleExpression;
+193 −0
Original line number Diff line number Diff line
@@ -102,6 +102,8 @@ import de.ugoe.cs.swe.tTCN3.InLineTemplate
import de.ugoe.cs.swe.tTCN3.RunsOnSpec
import de.ugoe.cs.swe.tTCN3.FunctionInstance
import de.ugoe.cs.swe.tTCN3.AltstepInstance
import de.ugoe.cs.swe.tTCN3.ModuleControlBody
import de.ugoe.cs.swe.tTCN3.PredefinedValue

class CheckDefinitionComeFirstParameter {
	public boolean hasOtherDefinitions
@@ -1338,6 +1340,197 @@ class TTCN3Validator extends AbstractTTCN3Validator {
		false
	}

	private def ArrayList<EObject> findAllParameters(EObject parent) {
		val ArrayList<EObject> paramList = newArrayList
		paramList.addAll(parent.getAllContentsOfType(FormalValuePar))
		paramList.addAll(parent.getAllContentsOfType(FormalTimerPar))
		paramList.addAll(parent.getAllContentsOfType(FormalTemplatePar))
		paramList.addAll(parent.getAllContentsOfType(FormalPortPar))
		return paramList
	}

	@Check
	def checkNoUnusedFormalParameters(AltstepDef altstep) {
		if (activeProfile.checkNoUnusedFormalParameters) {
			if (altstep.params != null) {
				val ArrayList<EObject> paramList = altstep.params.findAllParameters
				altstep.checkNoUnusedFormalParameters(paramList)
			}
		}
	}

	@Check
	def checkNoUnusedFormalParameters(TestcaseDef testcase) {
		if (activeProfile.checkNoUnusedFormalParameters) {
			if (testcase.parList != null) {
				val ArrayList<EObject> paramList = testcase.parList.findAllParameters
				testcase.checkNoUnusedFormalParameters(paramList)
			}
		}
	}

	@Check
	def checkNoUnusedFormalParameters(FunctionDef function) {
		if (activeProfile.checkNoUnusedFormalParameters) {
			if (function.parameterList != null) {
				val ArrayList<EObject> paramList = function.parameterList.findAllParameters
				function.checkNoUnusedFormalParameters(paramList)
			}
		}
	}

	@Check
	def checkNoUnusedFormalParameters(BaseTemplate template) {
		if (!activeProfile.checkNoUnusedFormalParameters)
			return

		var current = template
		val ArrayList<EObject> paramList = newArrayList
		val ArrayList<BaseTemplate> consideredTemplates = newArrayList
		consideredTemplates.add(current)

		do {
			if (current.parList != null) {
				for (p : current.parList.findAllParameters) {
					if (paramList.filter[(it as TTCN3Reference).name == (p as TTCN3Reference).name].size == 0) {
						paramList.add(p)
					}
				}
			}
			if ((current.eContainer as TemplateDef).derived != null) {
				if (consideredTemplates.contains((current.eContainer as TemplateDef).derived.template)) {
					val message = "" + "Possible cyclic modifies sequence: \"" + template.name + "\". Skipping..."
					val INode node = NodeModelUtils.getNode(template)
					info(
						message,
						null,
						MessageClass.STYLE.toString,
						node.startLine.toString,
						node.endLine.toString,
						"6.13, " + MiscTools.getMethodName(),
						LogLevel.INFORMATION.toString
					);
					current = null
				} else {
					current = (current.eContainer as TemplateDef).derived.template
				}
				current = (current.eContainer as TemplateDef).derived.template
			} else {
				current = null
			}
		} while (current != null)

		template.checkNoUnusedFormalParameters(paramList)
	}

	private def checkNoUnusedFormalParameters(EObject parent, ArrayList<EObject> params) {
		val module = parent.findDesiredParent(TTCN3Module)
		for (p : params) {
			if (p.isUnreferenced(module)) { // TODO: test this call and consider alternatives
				val message = "Formal parameter \"" + (p as TTCN3Reference).name + "\" in definition for \"" +
					(parent as TTCN3Reference).name + "\" is never used!"
				val INode node = NodeModelUtils.getNode(p)
				warning(
					message,
					null, //TODO: EStructuralFeature
					MessageClass.STYLE.toString,
					node.startLine.toString,
					node.endLine.toString,
					"6.11, " + MiscTools.getMethodName()
				);
			}
		}
	}

	@Check
	def checkNoUnusedLocalDefinitions(SingleVarInstance variable) { // this considers timers, too
		if (!activeProfile.checkNoUnusedLocalDefinitions) {
			return
		}
		variable.checkNoUnusedLocalDefinitionsAll
	}

	@Check
	def checkNoUnusedLocalDefinitions(SingleConstDef variable) {
		if (!activeProfile.checkNoUnusedLocalDefinitions) {
			return
		}
		variable.checkNoUnusedLocalDefinitionsAll
	}

	@Check
	def checkNoUnusedLocalDefinitions(SingleTempVarInstance variable) {
		if (!activeProfile.checkNoUnusedLocalDefinitions) {
			return
		}
		variable.checkNoUnusedLocalDefinitionsAll
	}

	@Check
	def checkNoUnusedLocalDefinitions(BaseTemplate variable) {
		if (!activeProfile.checkNoUnusedLocalDefinitions) {
			return
		}
		variable.checkNoUnusedLocalDefinitionsAll
	}

	private def checkNoUnusedLocalDefinitionsAll(TTCN3Reference variable) {
		if (variable.variableLocal) {
			val module = variable.findDesiredParent(TTCN3Module)
			if (variable.isUnreferenced(module)) {
				val message = "Local definition for \"" + variable.name + "\" in definition of \"" + module.name +
					"\" is never used!"
				val INode node = NodeModelUtils.getNode(variable)
				warning(
					message,
					null, //TODO: EStructuralFeature
					MessageClass.STYLE.toString,
					node.startLine.toString,
					node.endLine.toString,
					"6.11, " + MiscTools.getMethodName()
				);
			}
		}
	}

	private def variableLocal(EObject variable) {
		val parentFunction = variable.findDesiredParent(FunctionDef)
		val parentAltStep = variable.findDesiredParent(AltstepDef)
		val parentComponent = variable.findDesiredParent(ComponentDef)
		val parentTestcase = variable.findDesiredParent(TestcaseDef)
		val parentControl = variable.findDesiredParent(ModuleControlBody)

		return (parentFunction != null || parentAltStep != null || parentComponent != null || parentTestcase != null ||
			parentControl != null)
	}

	@Check
	def checkNoLiterals(PredefinedValue value) {
		if (!activeProfile.checkNoLiterals) {
			return
		}

		val parentModulePar = value.findDesiredParent(ModuleParDef)
		val parentConstDef = value.findDesiredParent(ConstDef)
		val parentTemplate = value.findDesiredParent(TemplateDef)

		if (parentModulePar == null && parentConstDef == null && parentTemplate == null) {
			if (value.bstring != null || value.integer != null || value.float != null || value.hstring != null ||
				value.ostring != null || value.charString != null) {
				val message = "Literal value is used!"
				val INode node = NodeModelUtils.getNode(value)
				warning(
					message,
					null, //TODO: EStructuralFeature
					MessageClass.STYLE.toString,
					node.startLine.toString,
					node.endLine.toString,
					"6.12, " + MiscTools.getMethodName()
				);
			}
		}
	}

/**
	  * END: Code Style ------------------------------------------------------------------------------
	  */