-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[clang][ExprConst] Call FastEvaluateAsRValue in isCXX11ConstantExpr
#151466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@llvm/pr-subscribers-clang Author: Timm Baeder (tbaederr) ChangesThis was one case where we didn't call Full diff: https://github.com/llvm/llvm-project/pull/151466.diff 1 Files Affected:
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 9808298a1b1d0..fb98bf0dc74d2 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -16849,31 +16849,31 @@ static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) {
CheckMemoryLeaks(Info);
}
-static bool FastEvaluateAsRValue(const Expr *Exp, Expr::EvalResult &Result,
+static bool FastEvaluateAsRValue(const Expr *Exp, APValue &Result,
const ASTContext &Ctx, bool &IsConst) {
// Fast-path evaluations of integer literals, since we sometimes see files
// containing vast quantities of these.
if (const auto *L = dyn_cast<IntegerLiteral>(Exp)) {
- Result.Val = APValue(APSInt(L->getValue(),
- L->getType()->isUnsignedIntegerType()));
+ Result =
+ APValue(APSInt(L->getValue(), L->getType()->isUnsignedIntegerType()));
IsConst = true;
return true;
}
if (const auto *L = dyn_cast<CXXBoolLiteralExpr>(Exp)) {
- Result.Val = APValue(APSInt(APInt(1, L->getValue())));
+ Result = APValue(APSInt(APInt(1, L->getValue())));
IsConst = true;
return true;
}
if (const auto *FL = dyn_cast<FloatingLiteral>(Exp)) {
- Result.Val = APValue(FL->getValue());
+ Result = APValue(FL->getValue());
IsConst = true;
return true;
}
if (const auto *L = dyn_cast<CharacterLiteral>(Exp)) {
- Result.Val = APValue(Ctx.MakeIntValue(L->getValue(), L->getType()));
+ Result = APValue(Ctx.MakeIntValue(L->getValue(), L->getType()));
IsConst = true;
return true;
}
@@ -16882,7 +16882,7 @@ static bool FastEvaluateAsRValue(const Expr *Exp, Expr::EvalResult &Result,
if (CE->hasAPValueResult()) {
APValue APV = CE->getAPValueResult();
if (!APV.isLValue()) {
- Result.Val = std::move(APV);
+ Result = std::move(APV);
IsConst = true;
return true;
}
@@ -16912,7 +16912,7 @@ static bool EvaluateAsRValue(const Expr *E, Expr::EvalResult &Result,
const ASTContext &Ctx, EvalInfo &Info) {
assert(!E->isValueDependent());
bool IsConst;
- if (FastEvaluateAsRValue(E, Result, Ctx, IsConst))
+ if (FastEvaluateAsRValue(E, Result.Val, Ctx, IsConst))
return IsConst;
return EvaluateAsRValue(Info, E, Result.Val);
@@ -17069,7 +17069,8 @@ bool Expr::EvaluateAsConstantExpr(EvalResult &Result, const ASTContext &Ctx,
assert(!isValueDependent() &&
"Expression evaluator can't be called on a dependent expression.");
bool IsConst;
- if (FastEvaluateAsRValue(this, Result, Ctx, IsConst) && Result.Val.hasValue())
+ if (FastEvaluateAsRValue(this, Result.Val, Ctx, IsConst) &&
+ Result.Val.hasValue())
return true;
ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsConstantExpr");
@@ -17284,7 +17285,7 @@ void Expr::EvaluateForOverflow(const ASTContext &Ctx) const {
ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateForOverflow");
bool IsConst;
EvalResult EVResult;
- if (!FastEvaluateAsRValue(this, EVResult, Ctx, IsConst)) {
+ if (!FastEvaluateAsRValue(this, EVResult.Val, Ctx, IsConst)) {
EvalInfo Info(Ctx, EVResult, EvalInfo::EM_IgnoreSideEffects);
Info.CheckingForUndefinedBehavior = true;
(void)::EvaluateAsRValue(Info, this, EVResult.Val);
@@ -17813,9 +17814,8 @@ Expr::getIntegerConstantExpr(const ASTContext &Ctx, SourceLocation *Loc) const {
return std::nullopt;
}
- APSInt Value;
-
if (Ctx.getLangOpts().CPlusPlus11) {
+ APSInt Value;
if (EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc))
return Value;
return std::nullopt;
@@ -17855,13 +17855,20 @@ bool Expr::isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result,
// issues.
assert(Ctx.getLangOpts().CPlusPlus);
+ bool IsConst;
+ APValue Scratch;
+ if (FastEvaluateAsRValue(this, Scratch, Ctx, IsConst) && Scratch.hasValue()) {
+ if (Result)
+ *Result = Scratch;
+ return true;
+ }
+
// Build evaluation settings.
Expr::EvalStatus Status;
SmallVector<PartialDiagnosticAt, 8> Diags;
Status.Diag = &Diags;
EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpression);
- APValue Scratch;
bool IsConstExpr =
::EvaluateAsRValue(Info, this, Result ? *Result : Scratch) &&
// FIXME: We don't produce a diagnostic for this, but the callers that
|
AaronBallman
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
This was one case where we didn't call
FastEvaluateAsRValuebefore going throughEvaluateAsRValue.Also replace the
EvalResultparameter with anAPValueone, sinceFastEvaluateAsRVauleonly needs that.Small gains: https://llvm-compile-time-tracker.com/compare.php?from=112af8e62e734938547d50eeb7b416c8dd666f45&to=b2ea804b9e22b7f37eb1b07b01c0a8057275fe4a&stat=instructions:u