[PATCH] D117376: Remove reference type when checking const structs

Richard Trieu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 14 17:34:28 PST 2022


rtrieu created this revision.
rtrieu added reviewers: rsmith, cfe-commits.
rtrieu requested review of this revision.
Herald added a project: clang.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D117376

Files:
  clang/lib/CodeGen/CGExprConstant.cpp
  clang/test/CodeGenCXX/merge-all-constants-references.cpp


Index: clang/test/CodeGenCXX/merge-all-constants-references.cpp
===================================================================
--- /dev/null
+++ 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({});
+}
Index: clang/lib/CodeGen/CGExprConstant.cpp
===================================================================
--- clang/lib/CodeGen/CGExprConstant.cpp
+++ clang/lib/CodeGen/CGExprConstant.cpp
@@ -851,6 +851,7 @@
 }
 
 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());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D117376.400180.patch
Type: text/x-patch
Size: 942 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220115/950f3abe/attachment.bin>


More information about the cfe-commits mailing list