From 2aa868251003835897c617c0cf1eb8ffcfa733e0 Mon Sep 17 00:00:00 2001
From: Johannes Mey <johannes.mey@tu-dresden.de>
Date: Tue, 17 Dec 2019 15:25:23 +0100
Subject: [PATCH] separate analysis and cfg a bit more

---
 reusablecfg/src/main/jastadd/Helpers.jrag     |  8 +++++
 .../jastadd/VariableDeclarationScope.jrag     | 32 +++++++++----------
 2 files changed, 23 insertions(+), 17 deletions(-)
 create mode 100644 reusablecfg/src/main/jastadd/Helpers.jrag

diff --git a/reusablecfg/src/main/jastadd/Helpers.jrag b/reusablecfg/src/main/jastadd/Helpers.jrag
new file mode 100644
index 0000000..0663dc4
--- /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 ac41195..501017f 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;
+  }
 
 }
-- 
GitLab