summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorElliott Baron <ebaron@fedoraproject.org>2009-11-17 16:33:56 -0500
committerElliott Baron <ebaron@fedoraproject.org>2009-11-17 16:33:56 -0500
commit8e5b06bf942549bbec94becfa0936e5c2787423a (patch)
treeff757a197924b8059a26c5bb9c293017a234e4bd
parentb7e5efd36bbd548d948a2e2fa8a31648091a8dda (diff)
downloadcodan-8e5b06bf942549bbec94becfa0936e5c2787423a.tar.gz
codan-8e5b06bf942549bbec94becfa0936e5c2787423a.tar.xz
codan-8e5b06bf942549bbec94becfa0936e5c2787423a.zip
Refactored conditional parsing to an ASTVisitor.
* org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/ConditionalVisitor.java: New file. * org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/PropertySimulator.java: Extracted class.
-rw-r--r--org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/ConditionalVisitor.java100
-rw-r--r--org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/PropertySimulator.java74
2 files changed, 102 insertions, 72 deletions
diff --git a/org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/ConditionalVisitor.java b/org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/ConditionalVisitor.java
new file mode 100644
index 0000000..0cbc11a
--- /dev/null
+++ b/org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/ConditionalVisitor.java
@@ -0,0 +1,100 @@
+package org.eclipse.cdt.codan.extension;
+
+import org.eclipse.cdt.core.dom.ast.ASTVisitor;
+import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
+import org.eclipse.cdt.core.dom.ast.IASTExpression;
+import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
+import org.eclipse.cdt.core.dom.ast.IASTName;
+import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
+import org.eclipse.cdt.core.dom.ast.IBinding;
+import org.eclipse.cdt.core.dom.ast.IVariable;
+
+public class ConditionalVisitor extends ASTVisitor {
+ private ExecutionState es;
+ private boolean value;
+
+ public ConditionalVisitor(ExecutionState es, boolean value) {
+ this.es = es;
+ this.value = value;
+ shouldVisitExpressions = true;
+ }
+
+ @Override
+ public int visit(IASTExpression node) {
+ int ret = PROCESS_CONTINUE;
+ // Modify execution state according to branch condition
+ ExecutionStateClause clause = null;
+ // *N.B.* content for condition IBlock is condition expression itself
+ if (node instanceof IASTBinaryExpression) { // TODO use visitor, nested BinaryExpressions
+ // FIXME compound conditionals
+ IASTBinaryExpression binExpr = (IASTBinaryExpression) node;
+ int op = binExpr.getOperator();
+
+ // FIXME other ops
+ // Check operator is an equality operator
+ if (op == IASTBinaryExpression.op_equals) { // if (x == 0)
+ IASTExpression o1 = binExpr.getOperand1();
+ if (o1 instanceof IASTIdExpression) {
+ IASTName name = ((IASTIdExpression) o1).getName();
+ clause = parseConditional(clause, name, binExpr.getOperand2(), value);
+ }
+ }
+ else if (op == IASTBinaryExpression.op_notequals) { // if (x != 0)
+ IASTExpression o1 = binExpr.getOperand1();
+ if (o1 instanceof IASTIdExpression) {
+ IASTName name = ((IASTIdExpression) o1).getName();
+ clause = parseConditional(clause, name, binExpr.getOperand2(), !value); // Negation
+ }
+ }
+ }
+ else if (node instanceof IASTUnaryExpression) { // if (!x)
+ IASTUnaryExpression uExpr = (IASTUnaryExpression) node;
+ int op = uExpr.getOperator();
+
+ // Check operator is a negation operator
+ if (op == IASTUnaryExpression.op_not) {
+ IASTExpression operand = uExpr.getOperand();
+ if (operand instanceof IASTIdExpression) {
+ IASTName name = ((IASTIdExpression) operand).getName();
+ clause = parseConditional(clause, name, !value); // Negation
+ }
+ }
+ }
+ else if (node instanceof IASTIdExpression) { // if (x)
+ IASTName name = ((IASTIdExpression) node).getName();
+ clause = parseConditional(clause, name, value);
+ }
+
+ if (clause != null) {
+ es.addClause(clause);
+ // TODO Theorem Prover goes here! / Determine if branch is feasible
+ es.bindTruthAssignments();
+ ret = PROCESS_SKIP;
+ }
+ else {
+ // FIXME Handle unresolvable case
+ }
+ return ret;
+ }
+
+ private ExecutionStateClause parseConditional(ExecutionStateClause clause, IASTName name, IASTExpression valueExpr, boolean branchTruth) {
+ IBinding binding = name.resolveBinding();
+ if (binding instanceof IVariable) {
+ IVariable var = (IVariable) binding;
+ Boolean truth = ASTParserUtil.getTruthValue(valueExpr);
+ if (truth != null) {
+ clause = new ExecutionStateClause(var, truth == branchTruth);
+ }
+ }
+ return clause;
+ }
+
+ private ExecutionStateClause parseConditional(ExecutionStateClause clause, IASTName name, boolean branchTruth) {
+ IBinding binding = name.resolveBinding();
+ if (binding instanceof IVariable) {
+ IVariable var = (IVariable) binding;
+ clause = new ExecutionStateClause(var, branchTruth);
+ }
+ return clause;
+ }
+}
diff --git a/org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/PropertySimulator.java b/org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/PropertySimulator.java
index 655dca2..1cd6911 100644
--- a/org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/PropertySimulator.java
+++ b/org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/PropertySimulator.java
@@ -309,57 +309,8 @@ public class PropertySimulator {
SymbolicState ret = s.copy();
if (node != null) {
- // Modify execution state according to branch condition
- ExecutionStateClause clause = null;
- // *N.B.* content for condition IBlock is condition expression itself
- if (node instanceof IASTBinaryExpression) {
- // FIXME compound conditionals
- IASTBinaryExpression binExpr = (IASTBinaryExpression) node;
- int op = binExpr.getOperator();
-
- // FIXME other ops
- // Check operator is an equality operator
- if (op == IASTBinaryExpression.op_equals) { // if (x == 0)
- IASTExpression o1 = binExpr.getOperand1();
- if (o1 instanceof IASTIdExpression) {
- IASTName name = ((IASTIdExpression) o1).getName();
- clause = parseConditional(clause, name, binExpr.getOperand2(), value);
- }
- }
- else if (op == IASTBinaryExpression.op_notequals) { // if (x != 0)
- IASTExpression o1 = binExpr.getOperand1();
- if (o1 instanceof IASTIdExpression) {
- IASTName name = ((IASTIdExpression) o1).getName();
- clause = parseConditional(clause, name, binExpr.getOperand2(), !value); // Negation
- }
- }
- }
- else if (node instanceof IASTUnaryExpression) { // if (!x)
- IASTUnaryExpression uExpr = (IASTUnaryExpression) node;
- int op = uExpr.getOperator();
-
- // Check operator is a negation operator
- if (op == IASTUnaryExpression.op_not) {
- IASTExpression operand = uExpr.getOperand();
- if (operand instanceof IASTIdExpression) {
- IASTName name = ((IASTIdExpression) operand).getName();
- clause = parseConditional(clause, name, !value); // Negation
- }
- }
- }
- else if (node instanceof IASTIdExpression) { // if (x)
- IASTName name = ((IASTIdExpression) node).getName();
- clause = parseConditional(clause, name, value);
- }
-
- if (clause != null) {
- ret.getExecutionState().addClause(clause);
- // TODO Theorem Prover goes here! / Determine if branch is feasible
- ret.getExecutionState().bindTruthAssignments();
- }
- else {
- // FIXME Handle unresolvable case
- }
+ ConditionalVisitor visitor = new ConditionalVisitor(ret.getExecutionState(), value);
+ node.accept(visitor);
}
return ret;
@@ -396,25 +347,4 @@ public class PropertySimulator {
return blk.getInEdges().length > 1;
}
- // FIXME REFACTOR
- private ExecutionStateClause parseConditional(ExecutionStateClause clause, IASTName name, IASTExpression valueExpr, boolean branchTruth) {
- IBinding binding = name.resolveBinding();
- if (binding instanceof IVariable) {
- IVariable var = (IVariable) binding;
- Boolean truth = ASTParserUtil.getTruthValue(valueExpr);
- if (truth != null) {
- clause = new ExecutionStateClause(var, truth == branchTruth);
- }
- }
- return clause;
- }
-
- private ExecutionStateClause parseConditional(ExecutionStateClause clause, IASTName name, boolean branchTruth) {
- IBinding binding = name.resolveBinding();
- if (binding instanceof IVariable) {
- IVariable var = (IVariable) binding;
- clause = new ExecutionStateClause(var, branchTruth);
- }
- return clause;
- }
}