git: a7e6ae2dc64b - stable/13 - Apply clang fix for assertion building emulators/rpcs3

From: Dimitry Andric <dim_at_FreeBSD.org>
Date: Wed, 03 May 2023 10:41:10 UTC
The branch stable/13 has been updated by dim:

URL: https://cgit.FreeBSD.org/src/commit/?id=a7e6ae2dc64b313520adc6141e4cffb1fa962b66

commit a7e6ae2dc64b313520adc6141e4cffb1fa962b66
Author:     Dimitry Andric <dim@FreeBSD.org>
AuthorDate: 2023-04-28 14:08:42 +0000
Commit:     Dimitry Andric <dim@FreeBSD.org>
CommitDate: 2023-05-03 08:18:46 +0000

    Apply clang fix for assertion building emulators/rpcs3
    
    Merge commit a5e1a93ea10f from llvm-project (by Mariya Podchishchaeva):
    
      [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
    
    PR:             269489
    MFC after:      3 days
    
    (cherry picked from commit 56f2446575c78d962b6dda5e3310bec078622f3d)
---
 .../llvm-project/clang/include/clang/Sema/Sema.h   |  3 +++
 contrib/llvm-project/clang/lib/Sema/SemaExpr.cpp   | 26 +++++++++++++++-------
 2 files changed, 21 insertions(+), 8 deletions(-)

diff --git a/contrib/llvm-project/clang/include/clang/Sema/Sema.h b/contrib/llvm-project/clang/include/clang/Sema/Sema.h
index 681a76dfa56a..53257a1bb028 100644
--- a/contrib/llvm-project/clang/include/clang/Sema/Sema.h
+++ b/contrib/llvm-project/clang/include/clang/Sema/Sema.h
@@ -1365,6 +1365,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/contrib/llvm-project/clang/lib/Sema/SemaExpr.cpp b/contrib/llvm-project/clang/lib/Sema/SemaExpr.cpp
index 83081bbf0aa0..a8fe9a68c8cb 100644
--- a/contrib/llvm-project/clang/lib/Sema/SemaExpr.cpp
+++ b/contrib/llvm-project/clang/lib/Sema/SemaExpr.cpp
@@ -17529,6 +17529,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();
@@ -17567,10 +17568,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())
@@ -17639,10 +17646,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.