diff options
author | Mariya Podchishchaeva <mariya.podchishchaeva@intel.com> | 2023-04-05 05:01:42 -0400 |
---|---|---|
committer | Tom Stellard <tstellar@redhat.com> | 2023-05-31 20:07:29 -0700 |
commit | cf85b3e5662d9ac436b4e1a0361513dfbe547782 (patch) | |
tree | 5efe315399555626c5443cc654d2dc600e04c7fb | |
parent | e32cbe930f5b36cc3e09c1e86a1c03cb6065f22b (diff) | |
download | llvm-cf85b3e5662d9ac436b4e1a0361513dfbe547782.tar.gz |
[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 https://github.com/llvm/llvm-project/issues/58207
Reviewed By: aaron.ballman, shafik
Differential Revision: https://reviews.llvm.org/D146234
(cherry picked from commit a5e1a93ea10ffc06a32df6d9f410e9b297ed136d)
-rw-r--r-- | clang/docs/ReleaseNotes.rst | 3 | ||||
-rw-r--r-- | clang/include/clang/Sema/Sema.h | 3 | ||||
-rw-r--r-- | clang/lib/Sema/SemaExpr.cpp | 26 | ||||
-rw-r--r-- | clang/test/SemaCXX/cxx2a-consteval.cpp | 38 |
4 files changed, 62 insertions, 8 deletions
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index bc770c895af8..ca73b26eacf7 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -718,6 +718,9 @@ Bug Fixes in This Version - Fix crash when attempting to perform parenthesized initialization of an aggregate with a base class with only non-public constructors. (`#62296 <https://github.com/llvm/llvm-project/issues/62296>`_) +- Fix crash when handling nested immediate invocations in initializers of global + variables. + (`#58207 <https://github.com/llvm/llvm-project/issues/58207>`_) Bug Fixes to Compiler Builtins ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index d40dacb5aa0f..e57955f16bdd 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -1394,6 +1394,9 @@ public: /// A stack of expression evaluation contexts. SmallVector<ExpressionEvaluationContextRecord, 8> ExprEvalContexts; + // Set of failed immediate invocations to avoid double diagnosing. + llvm::SmallPtrSet<ConstantExpr *, 4> FailedImmediateInvocations; + /// Emit a warning for all pending noderef expressions that we recorded. void WarnOnPendingNoDerefs(ExpressionEvaluationContextRecord &Rec); diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 2842add2cc4a..9d865f487098 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -17805,6 +17805,7 @@ static void EvaluateAndDiagnoseImmediateInvocation( bool Result = CE->EvaluateAsConstantExpr( Eval, SemaRef.getASTContext(), ConstantExprKind::ImmediateInvocation); if (!Result || !Notes.empty()) { + SemaRef.FailedImmediateInvocations.insert(CE); Expr *InnerExpr = CE->getSubExpr()->IgnoreImplicit(); if (auto *FunctionalCast = dyn_cast<CXXFunctionalCastExpr>(InnerExpr)) InnerExpr = FunctionalCast->getSubExpr(); @@ -17849,10 +17850,16 @@ static void RemoveNestedImmediateInvocation( [E](Sema::ImmediateInvocationCandidate Elem) { return Elem.getPointer() == E; }); - assert(It != IISet.rend() && - "ConstantExpr marked IsImmediateInvocation should " - "be present"); - It->setInt(1); // Mark as deleted + // It is possible that some subexpression of the current immediate + // invocation was handled from another expression evaluation context. Do + // not handle the current immediate invocation if some of its + // subexpressions failed before. + if (It == IISet.rend()) { + if (SemaRef.FailedImmediateInvocations.contains(E)) + CurrentII->setInt(1); + } else { + It->setInt(1); // Mark as deleted + } } ExprResult TransformConstantExpr(ConstantExpr *E) { if (!E->isImmediateInvocation()) @@ -17925,10 +17932,13 @@ HandleImmediateInvocations(Sema &SemaRef, SemaRef.RebuildingImmediateInvocation) return; - /// When we have more then 1 ImmediateInvocationCandidates we need to check - /// for nested ImmediateInvocationCandidates. when we have only 1 we only - /// need to remove ReferenceToConsteval in the immediate invocation. - if (Rec.ImmediateInvocationCandidates.size() > 1) { + /// When we have more than 1 ImmediateInvocationCandidates or previously + /// failed immediate invocations, we need to check for nested + /// ImmediateInvocationCandidates in order to avoid duplicate diagnostics. + /// Otherwise we only need to remove ReferenceToConsteval in the immediate + /// invocation. + if (Rec.ImmediateInvocationCandidates.size() > 1 || + !SemaRef.FailedImmediateInvocations.empty()) { /// Prevent sema calls during the tree transform from adding pointers that /// are already in the sets. diff --git a/clang/test/SemaCXX/cxx2a-consteval.cpp b/clang/test/SemaCXX/cxx2a-consteval.cpp index 6ae2664046d7..b3c722dce53a 100644 --- a/clang/test/SemaCXX/cxx2a-consteval.cpp +++ b/clang/test/SemaCXX/cxx2a-consteval.cpp @@ -1029,3 +1029,41 @@ int f() { int x = A{}; } } + +namespace GH58207 { +struct tester { + consteval tester(const char* name) noexcept { } +}; +consteval const char* make_name(const char* name) { return name;} +consteval const char* pad(int P) { return "thestring"; } + +int bad = 10; // expected-note 6{{declared here}} + +tester glob1(make_name("glob1")); +tester glob2(make_name("glob2")); +constexpr tester cglob(make_name("cglob")); +tester paddedglob(make_name(pad(bad))); // expected-error {{call to consteval function 'GH58207::make_name' is not a constant expression}} \ + // expected-note {{read of non-const variable 'bad' is not allowed in a constant expression}} + +constexpr tester glob3 = { make_name("glob3") }; +constexpr tester glob4 = { make_name(pad(bad)) }; // expected-error {{call to consteval function 'GH58207::make_name' is not a constant expression}} \ + // expected-error {{constexpr variable 'glob4' must be initialized by a constant expression}} \ + // expected-note 2{{read of non-const variable 'bad' is not allowed in a constant expression}} + +auto V = make_name(pad(3)); +auto V1 = make_name(pad(bad)); // expected-error {{call to consteval function 'GH58207::make_name' is not a constant expression}} \ + // expected-note {{read of non-const variable 'bad' is not allowed in a constant expression}} + + +void foo() { + static tester loc1(make_name("loc1")); + static constexpr tester loc2(make_name("loc2")); + static tester paddedloc(make_name(pad(bad))); // expected-error {{call to consteval function 'GH58207::make_name' is not a constant expression}} \ + // expected-note {{read of non-const variable 'bad' is not allowed in a constant expression}} +} + +void bar() { + static tester paddedloc(make_name(pad(bad))); // expected-error {{call to consteval function 'GH58207::make_name' is not a constant expression}} \ + // expected-note {{read of non-const variable 'bad' is not allowed in a constant expression}} +} +} |