Skip to content

Commit 030df66

Browse files
mjgartonrinon
andauthored
Fix evaluateConstantInt (immunant#339)
* Fix evaluateConstantInt Ensure we set the value when there is one, instead of when there isn't. * Use getIntegerConstantExpr in evaluateConstantInt isIntegerConstantExpr no longer retrieves the constant from an expr in LLVM 12, so we need to call our helper to do that to preserve the old behavior. Co-authored-by: Stephen Crane <[email protected]>
1 parent ed1af8e commit 030df66

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

c2rust-ast-exporter/src/AstExporter.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -484,22 +484,25 @@ class TranslateASTVisitor final
484484
}
485485

486486
bool evaluateConstantInt(Expr *E, APSInt &constant) {
487-
bool hasValue = E->isIntegerConstantExpr(*Context);
487+
auto value = getIntegerConstantExpr(*E, *Context);
488488

489-
if (!hasValue) {
489+
if (value) {
490+
constant = *value;
491+
return true;
492+
} else {
490493
#if CLANG_VERSION_MAJOR < 8
491494
APSInt eval_result;
492495
#else
493496
Expr::EvalResult eval_result;
494497
#endif // CLANG_VERSION_MAJOR
495-
hasValue = E->EvaluateAsInt(eval_result, *Context);
498+
bool hasValue = E->EvaluateAsInt(eval_result, *Context);
496499
#if CLANG_VERSION_MAJOR < 8
497500
constant = eval_result;
498501
#else
499502
constant = eval_result.Val.getInt();
500503
#endif // CLANG_VERSION_MAJOR
504+
return hasValue;
501505
}
502-
return hasValue;
503506
}
504507

505508
// Template required because Decl and Stmt don't share a common base class

0 commit comments

Comments
 (0)