diff --git a/reusablecfg/src/main/jastadd/Helpers.jrag b/reusablecfg/src/main/jastadd/Helpers.jrag new file mode 100644 index 0000000000000000000000000000000000000000..0663dc4cbe7ae487b1d22c05903cb44053c2b17d --- /dev/null +++ b/reusablecfg/src/main/jastadd/Helpers.jrag @@ -0,0 +1,8 @@ +aspect Helpers { + syn boolean List.contains(ASTNode other) { + for (ASTNode node : this) { + if (node.equals(other)) return true; + } + return false; + } +} diff --git a/reusablecfg/src/main/jastadd/VariableDeclarationScope.jrag b/reusablecfg/src/main/jastadd/VariableDeclarationScope.jrag index ac411950e7394e69ac6efcc2052d632558091879..501017f3ff48ef57a66527d5951dbee2b85423e1 100644 --- a/reusablecfg/src/main/jastadd/VariableDeclarationScope.jrag +++ b/reusablecfg/src/main/jastadd/VariableDeclarationScope.jrag @@ -28,16 +28,15 @@ aspect VariableDeclarationScope { syn boolean CfgNode.isDeclarationOf(Variable var) = false; eq CfgBranch.isDeclarationOf(Variable var) = - branchDeclaresVariable(var) - || variableDeclaredInsideStatement(var); + getHostStatement().declaresVariable(var) + || getHostStatement().variableDeclaredInside(var); - syn boolean CfgBranch.variableDeclaredInsideStatement(Variable var) { - Stmt stmt = getHostStatement(); + syn boolean Stmt.variableDeclaredInside(Variable var) { ASTNode node = (ASTNode) var; - while (node != stmt && node != null) { + while (node != this && node != null) { node = node.getParent(); } - return node == stmt; + return node == this; } /** The statement this expression is part of. */ @@ -45,16 +44,15 @@ aspect VariableDeclarationScope { eq Stmt.getChild().hostStatement() = this; eq Program.getChild().hostStatement() = null; - /** Test if the CFG node is tied to a statement that declares the variable. */ - inh boolean CfgBranch.branchDeclaresVariable(Variable var); - - eq EnhancedForStmt.branch().branchDeclaresVariable(Variable var) = - getVariableDecl() == var; - eq IfStmt.branch().branchDeclaresVariable(Variable var) = false; - eq ConditionalExpr.branch().branchDeclaresVariable(Variable var) = false; - eq ForStmt.branch().branchDeclaresVariable(Variable var) = false; - eq WhileStmt.branch().branchDeclaresVariable(Variable var) = false; - eq DoStmt.branch().branchDeclaresVariable(Variable var) = false; - eq SwitchStmt.branch().branchDeclaresVariable(Variable var) = false; + /** Test if a statement declares a variable */ + syn boolean Stmt.declaresVariable(Variable var) = false; + eq VarDeclStmt.declaresVariable(Variable var) = getDeclaratorList().contains((ASTNode)var); + eq EnhancedForStmt.declaresVariable(Variable var) = getVariableDecl() == var; + eq Block.declaresVariable(Variable var) { + for (Stmt stmt : getStmtList()) { + if (stmt.declaresVariable(var)) return true; + } + return false; + } }