Skip to content

Commit

Permalink
Use values cached in ConstantExprs for expression evaluation where
Browse files Browse the repository at this point in the history
present.

No functionality change intended.

Differential Revision: https://reviews.llvm.org/D76438
  • Loading branch information
DarkArc authored and zygoloid committed Mar 21, 2020
1 parent fc7233d commit be10b7e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
3 changes: 3 additions & 0 deletions clang/include/clang/AST/Expr.h
Expand Up @@ -1056,6 +1056,9 @@ class ConstantExpr final
bool isImmediateInvocation() const {
return ConstantExprBits.IsImmediateInvocation;
}
bool hasAPValueResult() const {
return ConstantExprBits.APValueKind != APValue::None;
}
APValue getAPValueResult() const;
APValue &getResultAsAPValue() const { return APValueResult(); }
llvm::APSInt getResultAsAPSInt() const;
Expand Down
5 changes: 2 additions & 3 deletions clang/lib/AST/Expr.cpp
Expand Up @@ -357,6 +357,8 @@ llvm::APSInt ConstantExpr::getResultAsAPSInt() const {
}

APValue ConstantExpr::getAPValueResult() const {
assert(hasAPValueResult());

switch (ConstantExprBits.ResultKind) {
case ConstantExpr::RSK_APValue:
return APValueResult();
Expand Down Expand Up @@ -2722,9 +2724,6 @@ static Expr *IgnoreParensSingleStep(Expr *E) {
return CE->getChosenSubExpr();
}

else if (auto *CE = dyn_cast<ConstantExpr>(E))
return CE->getSubExpr();

return E;
}

Expand Down
9 changes: 7 additions & 2 deletions clang/lib/AST/ExprConstant.cpp
Expand Up @@ -6776,8 +6776,13 @@ class ExprEvaluatorBase
return Error(E);
}

bool VisitConstantExpr(const ConstantExpr *E)
{ return StmtVisitorTy::Visit(E->getSubExpr()); }
bool VisitConstantExpr(const ConstantExpr *E) {
if (E->hasAPValueResult())
return DerivedSuccess(E->getAPValueResult(), E);

return StmtVisitorTy::Visit(E->getSubExpr());
}

bool VisitParenExpr(const ParenExpr *E)
{ return StmtVisitorTy::Visit(E->getSubExpr()); }
bool VisitUnaryExtension(const UnaryOperator *E)
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/Sema/SemaExpr.cpp
Expand Up @@ -15166,6 +15166,12 @@ Sema::VerifyIntegerConstantExpression(Expr *E, llvm::APSInt *Result,
return ExprError();
}

ExprResult RValueExpr = DefaultLvalueConversion(E);
if (RValueExpr.isInvalid())
return ExprError();

E = RValueExpr.get();

// Circumvent ICE checking in C++11 to avoid evaluating the expression twice
// in the non-ICE case.
if (!getLangOpts().CPlusPlus11 && E->isIntegerConstantExpr(Context)) {
Expand Down

0 comments on commit be10b7e

Please sign in to comment.