Skip to content

Commit

Permalink
[clang][VarDecl] Reset un-evaluated constant for all C++ modes (#65818)
Browse files Browse the repository at this point in the history
After commit 610ec95 ("[clang] allow const structs/unions/arrays to
be constant expressions for C"), attempts to evaluate
structs/unions/arrays as constants are also performed for C++98 and
C++03.

An assertion was getting tripped up since the potentially-partially
evaluated value was not being reset for those 2 language modes.  Make
sure to reset it now for all C++ modes.

Fixes: #65784
  • Loading branch information
nickdesaulniers committed Sep 11, 2023
1 parent 46f3ea5 commit 2618154
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 2 additions & 2 deletions clang/lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2555,10 +2555,10 @@ APValue *VarDecl::evaluateValueImpl(SmallVectorImpl<PartialDiagnosticAt> &Notes,
bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, Ctx, this, Notes,
IsConstantInitialization);

// In C++11, this isn't a constant initializer if we produced notes. In that
// In C++, this isn't a constant initializer if we produced notes. In that
// case, we can't keep the result, because it may only be correct under the
// assumption that the initializer is a constant context.
if (IsConstantInitialization && Ctx.getLangOpts().CPlusPlus11 &&
if (IsConstantInitialization && Ctx.getLangOpts().CPlusPlus &&
!Notes.empty())
Result = false;

Expand Down
5 changes: 5 additions & 0 deletions clang/test/SemaCXX/constant-expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,8 @@ namespace PR31701 {
const C c = C::n<i>;
}
}

struct PR65784s{
int *ptr;
} const PR65784[] = {(int *)""};
PR65784s PR65784f() { return *PR65784; }

0 comments on commit 2618154

Please sign in to comment.