Author: Aaron Ballman
Date: 2021-10-14T15:48:10-04:00
New Revision: 68157fe15b238428d0fdbeb38c14afd5bda574da

URL: 
https://github.com/llvm/llvm-project/commit/68157fe15b238428d0fdbeb38c14afd5bda574da
DIFF: 
https://github.com/llvm/llvm-project/commit/68157fe15b238428d0fdbeb38c14afd5bda574da.diff

LOG: Fix a crash on valid consteval code.

Not all constants are emitted within the context of a function, so use
the module's ASTContext instead because 1) that's the same as the
current function ASTContext, and 2) the module can never be null.

Fixes PR50787.

Added: 
    clang/test/CodeGenCXX/cxx20-consteval-crash.cpp

Modified: 
    clang/lib/CodeGen/CGExprConstant.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGExprConstant.cpp 
b/clang/lib/CodeGen/CGExprConstant.cpp
index 734024149bbbe..d9ecf57a9ab52 100644
--- a/clang/lib/CodeGen/CGExprConstant.cpp
+++ b/clang/lib/CodeGen/CGExprConstant.cpp
@@ -1369,7 +1369,7 @@ llvm::Constant 
*ConstantEmitter::tryEmitConstantExpr(const ConstantExpr *CE) {
   const Expr *Inner = CE->getSubExpr()->IgnoreImplicit();
   QualType RetType;
   if (auto *Call = dyn_cast<CallExpr>(Inner))
-    RetType = Call->getCallReturnType(CGF->getContext());
+    RetType = Call->getCallReturnType(CGM.getContext());
   else if (auto *Ctor = dyn_cast<CXXConstructExpr>(Inner))
     RetType = Ctor->getType();
   llvm::Constant *Res =

diff  --git a/clang/test/CodeGenCXX/cxx20-consteval-crash.cpp 
b/clang/test/CodeGenCXX/cxx20-consteval-crash.cpp
new file mode 100644
index 0000000000000..ef868fa85749f
--- /dev/null
+++ b/clang/test/CodeGenCXX/cxx20-consteval-crash.cpp
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 %s -emit-llvm 
-o - | FileCheck %s
+
+namespace PR50787 {
+// This code would previously cause a crash.
+extern int x_;
+consteval auto& X() { return x_; }
+constexpr auto& x1 = X();
+auto x2 = X();
+
+// CHECK: @_ZN7PR507872x_E = external global i32, align 4
+// CHECK-NEXT: @_ZN7PR507872x1E = constant i32* @_ZN7PR507872x_E, align 8
+// CHECK-NEXT: @_ZN7PR507872x2E = global i32* @_ZN7PR507872x_E, align 4
+}
+


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to