1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 22:52:11 +02:00

More robust implementation of ControlFlowGraphBuilder.isConstant method

Change-Id: I43fd84e8c374a638834be3c2aeb216b96fc6b461
This commit is contained in:
Sergey Prigogin 2016-10-05 17:03:05 -07:00
parent 27f99d2a86
commit 64aec369e1
2 changed files with 25 additions and 6 deletions

View file

@ -56,7 +56,6 @@ import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTSwitchStatement;
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTWhileStatement;
import org.eclipse.cdt.core.dom.ast.IValue;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCatchHandler;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTRangeBasedForStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTryBlockStatement;
@ -581,11 +580,9 @@ public class ControlFlowGraphBuilder {
if (node instanceof ICfgData) {
IASTNode ast = (IASTNode) ((ICfgData) node).getData();
if (ast instanceof IASTExpression) {
IValue dvalue = ValueFactory.create((IASTExpression) ast);
Number numericalValue = dvalue.numberValue();
if (numericalValue == null)
return false;
return numericalValue.longValue() == testvalue;
Number numericalValue = ValueFactory.getConstantNumericalValue((IASTExpression) ast);
if (numericalValue != null)
return numericalValue.longValue() == testvalue;
}
}
return false;

View file

@ -583,4 +583,26 @@ public class ValueFactory {
private static boolean isDeferredValue(IValue value) {
return value instanceof IntegralValue && ((IntegralValue) value).numberValue() == null;
}
/**
* Returns the numerical value of the given expression if the expression can be evaluated
* at compile time.
*
* @param expr the expression to evaluate
* @return the numerical value of the expression, or {@code null} if the expression cannot be evaluated
* to a constant
*/
public static Number getConstantNumericalValue(IASTExpression expr) {
IValue val = evaluate(expr);
if (val != null) {
return val.numberValue();
}
if (expr instanceof ICPPEvaluationOwner) {
ICPPEvaluation eval = ((ICPPEvaluationOwner) expr).getEvaluation();
if (eval.isConstantExpression(expr) && !eval.isValueDependent())
return eval.getValue(expr).numberValue();
}
return null;
}
}