[clang] be2147d - Remove reference type when checking const structs

via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 28 13:22:07 PST 2022


Author: Weverything
Date: 2022-01-28T13:08:58-08:00
New Revision: be2147db054ec096199d1251bfab9065c7e0f29b

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

LOG: Remove reference type when checking const structs

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

Added: 
    clang/test/CodeGenCXX/merge-all-constants-references.cpp

Modified: 
    clang/lib/CodeGen/CGExprConstant.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp
index cf1f2e0eab92d..ac4b4d1308ab6 100644
--- a/clang/lib/CodeGen/CGExprConstant.cpp
+++ b/clang/lib/CodeGen/CGExprConstant.cpp
@@ -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
index 0000000000000..76c6f292ef421
--- /dev/null
+++ b/clang/test/CodeGenCXX/merge-all-constants-references.cpp
@@ -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({});
+}


        


More information about the cfe-commits mailing list