Remove reference type when checking const structs
authorWeverything <rtrieu@google.com>
Fri, 14 Jan 2022 23:42:36 +0000 (15:42 -0800)
committerWeverything <rtrieu@google.com>
Fri, 28 Jan 2022 21:08:58 +0000 (13:08 -0800)
ConstStructBuilder::Finalize in CGExprConstant.ccp assumes that the
passed in QualType is a RecordType.  In some instances, the type is a
reference to a RecordType and the reference needs to be removed first.

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

clang/lib/CodeGen/CGExprConstant.cpp
clang/test/CodeGenCXX/merge-all-constants-references.cpp [new file with mode: 0644]

index cf1f2e0..ac4b4d1 100644 (file)
@@ -851,6 +851,7 @@ bool ConstStructBuilder::Build(const APValue &Val, const RecordDecl *RD,
 }
 
 llvm::Constant *ConstStructBuilder::Finalize(QualType Type) {
+  Type = Type.getNonReferenceType();
   RecordDecl *RD = Type->castAs<RecordType>()->getDecl();
   llvm::Type *ValTy = CGM.getTypes().ConvertType(Type);
   return Builder.build(ValTy, RD->hasFlexibleArrayMember());
diff --git a/clang/test/CodeGenCXX/merge-all-constants-references.cpp b/clang/test/CodeGenCXX/merge-all-constants-references.cpp
new file mode 100644 (file)
index 0000000..76c6f29
--- /dev/null
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -fmerge-all-constants %s -o /dev/null
+
+struct A {
+};
+
+struct B {
+  const struct A& a = {};
+};
+
+void Test(const struct B&);
+
+void Run() {
+  Test({});
+}