[clang] 55bbcbf - [clang] Reset track of immediate function context when entering new function

Mariya Podchishchaeva via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 6 03:39:27 PDT 2023


Author: Mariya Podchishchaeva
Date: 2023-04-06T06:35:26-04:00
New Revision: 55bbcbf511b4df0dfe524b2d06fc561a939c6b16

URL: https://github.com/llvm/llvm-project/commit/55bbcbf511b4df0dfe524b2d06fc561a939c6b16
DIFF: https://github.com/llvm/llvm-project/commit/55bbcbf511b4df0dfe524b2d06fc561a939c6b16.diff

LOG: [clang] Reset track of immediate function context when entering new function

Due to not resetting that, clang still thinks that it is in immediate
function context even if it already entered non-consteval function.
This caused consteval functions reaching codegen in some cases.

Fixes https://github.com/llvm/llvm-project/issues/61142

Reviewed By: cor3ntin, aaron.ballman

Differential Revision: https://reviews.llvm.org/D147531

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaDecl.cpp
    clang/test/CodeGenCXX/cxx20-consteval-crash.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index c07f23af4a629..03bbebaf1249c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -279,6 +279,9 @@ Bug Fixes in This Version
 - Fix crash when generating code coverage information for `PseudoObjectExpr` in 
   Clang AST.
   (`#45481 <https://github.com/llvm/llvm-project/issues/45481>`_)
+- Fix the assertion hit when a template consteval function appears in a nested
+  consteval/constexpr call chain.
+  (`#61142 <https://github.com/llvm/llvm-project/issues/61142>`_)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

diff  --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 0feb0ab48f3a8..6c6bf78ffc9c8 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -15180,6 +15180,15 @@ Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D,
         FD->isConsteval() ? ExpressionEvaluationContext::ImmediateFunctionContext
                           : ExprEvalContexts.back().Context);
 
+  // Each ExpressionEvaluationContextRecord also keeps track of whether the
+  // context is nested in an immediate function context, so smaller contexts
+  // that appear inside immediate functions (like variable initializers) are
+  // considered to be inside an immediate function context even though by
+  // themselves they are not immediate function contexts. But when a new
+  // function is entered, we need to reset this tracking, since the entered
+  // function might be not an immediate function.
+  ExprEvalContexts.back().InImmediateFunctionContext = FD->isConsteval();
+
   // Check for defining attributes before the check for redefinition.
   if (const auto *Attr = FD->getAttr<AliasAttr>()) {
     Diag(Attr->getLocation(), diag::err_alias_is_definition) << FD << 0;

diff  --git a/clang/test/CodeGenCXX/cxx20-consteval-crash.cpp b/clang/test/CodeGenCXX/cxx20-consteval-crash.cpp
index ebd7eafaf7156..1639f0ef82d03 100644
--- a/clang/test/CodeGenCXX/cxx20-consteval-crash.cpp
+++ b/clang/test/CodeGenCXX/cxx20-consteval-crash.cpp
@@ -116,3 +116,27 @@ void method() {
 }
 
 } // namespace GH60166
+
+namespace GH61142 {
+
+template <typename T>
+struct Test {
+  constexpr static void bar() {
+    foo();
+  }
+  consteval static void foo() {};
+};
+
+consteval void a() {
+  Test<int>::bar();
+}
+
+void b() {
+  Test<int>::bar();
+}
+
+// Make sure consteval function is not emitted.
+// CHECK-NOT: call {{.*}}foo{{.*}}()
+// CHECK-NOT: define {{.*}}foo{{.*}}()
+
+} // namespace GH61142


        


More information about the cfe-commits mailing list