[clang] 1ac700c - [CodeGen] Fix clang crash on aggregate initialization of array of labels

Johannes Altmanninger via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 27 16:09:38 PST 2019


Author: Johannes Altmanninger
Date: 2019-11-28T00:59:25+01:00
New Revision: 1ac700cdef787383ad49a0e37d9894491ef19480

URL: https://github.com/llvm/llvm-project/commit/1ac700cdef787383ad49a0e37d9894491ef19480
DIFF: https://github.com/llvm/llvm-project/commit/1ac700cdef787383ad49a0e37d9894491ef19480.diff

LOG: [CodeGen] Fix clang crash on aggregate initialization of array of labels

Summary: Fix PR43700

The ConstantEmitter in AggExprEmitter::EmitArrayInit was initialized
with the CodeGenFunction set to null, which caused the crash.
Also simplify another call, and make the CGF member a const pointer
since it is public but only assigned in the constructor.

Subscribers: cfe-commits

Tags: #clang

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

Added: 
    clang/test/CodeGen/label-array-aggregate-init.c

Modified: 
    clang/lib/CodeGen/CGExprAgg.cpp
    clang/lib/CodeGen/CGExprScalar.cpp
    clang/lib/CodeGen/ConstantEmitter.h

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index 7e69f63fe135..ecb5253c07ec 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -493,7 +493,7 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType,
   if (NumInitElements * elementSize.getQuantity() > 16 &&
       elementType.isTriviallyCopyableType(CGF.getContext())) {
     CodeGen::CodeGenModule &CGM = CGF.CGM;
-    ConstantEmitter Emitter(CGM);
+    ConstantEmitter Emitter(CGF);
     LangAS AS = ArrayQTy.getAddressSpace();
     if (llvm::Constant *C = Emitter.tryEmitForInitializer(E, AS, ArrayQTy)) {
       auto GV = new llvm::GlobalVariable(

diff  --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index d727e326a27a..750b5503c08f 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -644,8 +644,8 @@ class ScalarExprEmitter
     auto &Ctx = CGF.getContext();
     APValue Evaluated =
         SLE->EvaluateInContext(Ctx, CGF.CurSourceLocExprScope.getDefaultExpr());
-    return ConstantEmitter(CGF.CGM, &CGF)
-        .emitAbstract(SLE->getLocation(), Evaluated, SLE->getType());
+    return ConstantEmitter(CGF).emitAbstract(SLE->getLocation(), Evaluated,
+                                             SLE->getType());
   }
 
   Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {

diff  --git a/clang/lib/CodeGen/ConstantEmitter.h b/clang/lib/CodeGen/ConstantEmitter.h
index 59a19730f4eb..121acbac4fa9 100644
--- a/clang/lib/CodeGen/ConstantEmitter.h
+++ b/clang/lib/CodeGen/ConstantEmitter.h
@@ -23,7 +23,7 @@ namespace CodeGen {
 class ConstantEmitter {
 public:
   CodeGenModule &CGM;
-  CodeGenFunction *CGF;
+  CodeGenFunction *const CGF;
 
 private:
   bool Abstract = false;

diff  --git a/clang/test/CodeGen/label-array-aggregate-init.c b/clang/test/CodeGen/label-array-aggregate-init.c
new file mode 100644
index 000000000000..6821fd355ec1
--- /dev/null
+++ b/clang/test/CodeGen/label-array-aggregate-init.c
@@ -0,0 +1,6 @@
+// RUN: %clang -cc1 -emit-llvm %s -o /dev/null
+
+int main() {
+L:
+  (void)(void *[]){ &&L, 0, 0 };
+}


        


More information about the cfe-commits mailing list