Skip to content

Commit a5e1a93

Browse files
committed
[clang] Fix crash when handling nested immediate invocations
Before this patch it was expected that if there was several immediate invocations they all belong to the same expression evaluation context. During parsing of non local variable initializer a new evaluation context is pushed, so code like this ``` namespace scope { struct channel { consteval channel(const char* name) noexcept { } }; consteval const char* make_channel_name(const char* name) { return name;} channel rsx_log(make_channel_name("rsx_log")); } ``` produced a nested immediate invocation whose subexpressions are attached to different expression evaluation contexts. The constructor call belongs to TU context and `make_channel_name` call to context of variable initializer. This patch removes this assumption and adds tracking of previously failed immediate invocations, so it is possible when handling an immediate invocation th check that its subexpressions from possibly another evaluation context contains errors and not produce duplicate diagnostics. Fixes llvm#58207 Reviewed By: aaron.ballman, shafik Differential Revision: https://reviews.llvm.org/D146234
1 parent 7553bad commit a5e1a93

File tree

4 files changed

+62
-8
lines changed

4 files changed

+62
-8
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ Bug Fixes in This Version
271271
(`#60887 <https://github.com/llvm/llvm-project/issues/60887>`_)
272272
- Fix incorrect merging of lambdas across modules.
273273
(`#60985 <https://github.com/llvm/llvm-project/issues/60985>`_)
274+
- Fix crash when handling nested immediate invocations in initializers of global
275+
variables.
276+
(`#58207 <https://github.com/llvm/llvm-project/issues/58207>`_)
274277

275278

276279
Bug Fixes to Compiler Builtins

clang/include/clang/Sema/Sema.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,9 @@ class Sema final {
13921392
/// A stack of expression evaluation contexts.
13931393
SmallVector<ExpressionEvaluationContextRecord, 8> ExprEvalContexts;
13941394

1395+
// Set of failed immediate invocations to avoid double diagnosing.
1396+
llvm::SmallPtrSet<ConstantExpr *, 4> FailedImmediateInvocations;
1397+
13951398
/// Emit a warning for all pending noderef expressions that we recorded.
13961399
void WarnOnPendingNoDerefs(ExpressionEvaluationContextRecord &Rec);
13971400

clang/lib/Sema/SemaExpr.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17860,6 +17860,7 @@ static void EvaluateAndDiagnoseImmediateInvocation(
1786017860
bool Result = CE->EvaluateAsConstantExpr(
1786117861
Eval, SemaRef.getASTContext(), ConstantExprKind::ImmediateInvocation);
1786217862
if (!Result || !Notes.empty()) {
17863+
SemaRef.FailedImmediateInvocations.insert(CE);
1786317864
Expr *InnerExpr = CE->getSubExpr()->IgnoreImplicit();
1786417865
if (auto *FunctionalCast = dyn_cast<CXXFunctionalCastExpr>(InnerExpr))
1786517866
InnerExpr = FunctionalCast->getSubExpr();
@@ -17904,10 +17905,16 @@ static void RemoveNestedImmediateInvocation(
1790417905
[E](Sema::ImmediateInvocationCandidate Elem) {
1790517906
return Elem.getPointer() == E;
1790617907
});
17907-
assert(It != IISet.rend() &&
17908-
"ConstantExpr marked IsImmediateInvocation should "
17909-
"be present");
17910-
It->setInt(1); // Mark as deleted
17908+
// It is possible that some subexpression of the current immediate
17909+
// invocation was handled from another expression evaluation context. Do
17910+
// not handle the current immediate invocation if some of its
17911+
// subexpressions failed before.
17912+
if (It == IISet.rend()) {
17913+
if (SemaRef.FailedImmediateInvocations.contains(E))
17914+
CurrentII->setInt(1);
17915+
} else {
17916+
It->setInt(1); // Mark as deleted
17917+
}
1791117918
}
1791217919
ExprResult TransformConstantExpr(ConstantExpr *E) {
1791317920
if (!E->isImmediateInvocation())
@@ -17980,10 +17987,13 @@ HandleImmediateInvocations(Sema &SemaRef,
1798017987
SemaRef.RebuildingImmediateInvocation)
1798117988
return;
1798217989

17983-
/// When we have more then 1 ImmediateInvocationCandidates we need to check
17984-
/// for nested ImmediateInvocationCandidates. when we have only 1 we only
17985-
/// need to remove ReferenceToConsteval in the immediate invocation.
17986-
if (Rec.ImmediateInvocationCandidates.size() > 1) {
17990+
/// When we have more than 1 ImmediateInvocationCandidates or previously
17991+
/// failed immediate invocations, we need to check for nested
17992+
/// ImmediateInvocationCandidates in order to avoid duplicate diagnostics.
17993+
/// Otherwise we only need to remove ReferenceToConsteval in the immediate
17994+
/// invocation.
17995+
if (Rec.ImmediateInvocationCandidates.size() > 1 ||
17996+
!SemaRef.FailedImmediateInvocations.empty()) {
1798717997

1798817998
/// Prevent sema calls during the tree transform from adding pointers that
1798917999
/// are already in the sets.

clang/test/SemaCXX/cxx2a-consteval.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,3 +1065,41 @@ constexpr auto B = A{A{}}.f();
10651065
static_assert(B == 0);
10661066

10671067
}
1068+
1069+
namespace GH58207 {
1070+
struct tester {
1071+
consteval tester(const char* name) noexcept { }
1072+
};
1073+
consteval const char* make_name(const char* name) { return name;}
1074+
consteval const char* pad(int P) { return "thestring"; }
1075+
1076+
int bad = 10; // expected-note 6{{declared here}}
1077+
1078+
tester glob1(make_name("glob1"));
1079+
tester glob2(make_name("glob2"));
1080+
constexpr tester cglob(make_name("cglob"));
1081+
tester paddedglob(make_name(pad(bad))); // expected-error {{call to consteval function 'GH58207::make_name' is not a constant expression}} \
1082+
// expected-note {{read of non-const variable 'bad' is not allowed in a constant expression}}
1083+
1084+
constexpr tester glob3 = { make_name("glob3") };
1085+
constexpr tester glob4 = { make_name(pad(bad)) }; // expected-error {{call to consteval function 'GH58207::make_name' is not a constant expression}} \
1086+
// expected-error {{constexpr variable 'glob4' must be initialized by a constant expression}} \
1087+
// expected-note 2{{read of non-const variable 'bad' is not allowed in a constant expression}}
1088+
1089+
auto V = make_name(pad(3));
1090+
auto V1 = make_name(pad(bad)); // expected-error {{call to consteval function 'GH58207::make_name' is not a constant expression}} \
1091+
// expected-note {{read of non-const variable 'bad' is not allowed in a constant expression}}
1092+
1093+
1094+
void foo() {
1095+
static tester loc1(make_name("loc1"));
1096+
static constexpr tester loc2(make_name("loc2"));
1097+
static tester paddedloc(make_name(pad(bad))); // expected-error {{call to consteval function 'GH58207::make_name' is not a constant expression}} \
1098+
// expected-note {{read of non-const variable 'bad' is not allowed in a constant expression}}
1099+
}
1100+
1101+
void bar() {
1102+
static tester paddedloc(make_name(pad(bad))); // expected-error {{call to consteval function 'GH58207::make_name' is not a constant expression}} \
1103+
// expected-note {{read of non-const variable 'bad' is not allowed in a constant expression}}
1104+
}
1105+
}

0 commit comments

Comments
 (0)