From 8e5b06bf942549bbec94becfa0936e5c2787423a Mon Sep 17 00:00:00 2001 From: Elliott Baron Date: Tue, 17 Nov 2009 16:33:56 -0500 Subject: 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. --- .../cdt/codan/extension/ConditionalVisitor.java | 100 +++++++++++++++++++++ .../cdt/codan/extension/PropertySimulator.java | 74 +-------------- 2 files changed, 102 insertions(+), 72 deletions(-) create mode 100644 org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/ConditionalVisitor.java (limited to 'org.eclipse.cdt.codan.extension/src/org/eclipse/cdt') 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; - } } -- cgit