Commit b82f8c22 authored by Daniel Honsel's avatar Daniel Honsel
Browse files

improved checkLocalDefinitionsComeFirst

parent f5a8707b
Loading
Loading
Loading
Loading
+37 −37
Original line number Diff line number Diff line
@@ -233,8 +233,10 @@ class CodeStyleValidator extends AbstractDeclarativeValidator {
		val definition = moduleDefinition.def

		if (definition instanceof FunctionDef) {
			definition.statement.checkLocalDefinitionsComeFirst
			definition.statement.checkLocalDefinitionsComeFirstStatemanet
		} else if (definition instanceof TestcaseDef) {
			definition.statement.checkLocalDefinitionsComeFirst
			definition.statement.checkLocalDefinitionsComeFirstStatemanet
		} else if (definition instanceof AltstepDef) {
			definition.checkLocalDefinitionsComeFirstAltstepDef
@@ -243,7 +245,38 @@ class CodeStyleValidator extends AbstractDeclarativeValidator {
				definition.body.structured.component.checkLocalDefinitionsComeFirstComponent
			}
		}
	}
	
	private def checkLocalDefinitionsComeFirst(StatementBlock statement) {
		var int firstStatement = 0;
		if (statement.stat.size > 0) {
			firstStatement = NodeModelUtils.getNode(statement.stat.get(0)).startLine
		}		
		for (d : statement.def) {
			if (d.locDef != null && d.locDef.constDef != null) {
				d.locDef.constDef.checkDefinitionBeforeFirstOtherStatement(firstStatement)
			} else if (d.locInst != null && d.locInst.timer != null) {
				d.locInst.timer.checkDefinitionBeforeFirstOtherStatement(firstStatement)
			} else if (d.locInst != null && d.locInst.variable != null) {
				d.locInst.variable.checkDefinitionBeforeFirstOtherStatement(firstStatement)
			}
		}		
	}
	
	private def checkDefinitionBeforeFirstOtherStatement(EObject object, int first) {
				var int line = NodeModelUtils.getNode(object).startLine
				if (line > first) {
					val message = "Local definition is not at the beginning!"
					statistics.incrementCountStyle
					warning(
						message,
						null, //TODO: struct feature
						MessageClass.STYLE.toString,
						line.toString,
						line.toString,
						"6.8, " + "checkLocalDefinitionsComeFirst"
					);					
				}						
	}
	
	private def checkLocalDefinitionsComeFirstStatemanet(StatementBlock statement) {
@@ -257,9 +290,6 @@ class CodeStyleValidator extends AbstractDeclarativeValidator {
				d.locInst.variable.checkLocalDefinitionsComeFirstOrder(params)
			}
		}
		for (s : statement.stat) {
			s.checkLocalDefinitionsComeFirstOther
		}
	}

	private def checkLocalDefinitionsComeFirstAltstepDef(AltstepDef altstep) {
@@ -273,7 +303,6 @@ class CodeStyleValidator extends AbstractDeclarativeValidator {
				d.const.checkLocalDefinitionsComeFirstOrder(params)
			}
		}
		altstep.guard.checkLocalDefinitionsComeFirstOther
	}

	private def checkLocalDefinitionsComeFirstComponent(ComponentDef component) {
@@ -287,37 +316,8 @@ class CodeStyleValidator extends AbstractDeclarativeValidator {
				d.element.timer.checkLocalDefinitionsComeFirstOrder(params)
			else if (d.element.const != null)
				d.element.const.checkLocalDefinitionsComeFirstOrder(params)
		}
	}

	private def checkLocalDefinitionsComeFirstOther(EObject parent) {
		val configuredOrder = localDefOrderClasses
		val List<EObject> foundChilds = newArrayList
		for (t : configuredOrder) {

			// TODO: find a way to get the Class<EObject> to fix the hack
			if (t.instanceClassName.contains("VarInstance")) {
				foundChilds.addAll(parent.getAllContentsOfType(VarInstance))
			} else if (t.instanceClassName.contains("ConstDef")) {
				foundChilds.addAll(parent.getAllContentsOfType(ConstDef))
			} else if (t.instanceClassName.contains("TimerInstance")) {
				foundChilds.addAll(parent.getAllContentsOfType(TimerInstance))
			} else if (t.instanceClassName.contains("PortInstance")) {
				foundChilds.addAll(parent.getAllContentsOfType(PortInstance))
			}
		}
		for (c : foundChilds) {
			val message = "Local definition is not at the beginning!"
			val INode node = NodeModelUtils.getNode(c)
			statistics.incrementCountStyle
			warning(
				message,
				null, //TODO: struct feature
				MessageClass.STYLE.toString,
				node.startLine.toString,
				node.endLine.toString,
				"6.8, " + "checkLocalDefinitionsComeFirst"
			);
			else if (d.element.template != null)
				d.element.template.checkLocalDefinitionsComeFirstOrder(params)				
		}
	}