Skip to content

Commit 52e5c10

Browse files
[CGExprConstant] stop evaluating StringLiterals for non-ConstantArrayTypes
Fixes a bug introduced by commit b54294e ("[clang][ConstantEmitter] have tryEmitPrivate[ForVarInit] try ConstExprEmitter fast-path first") In the added test case, the QualType is a LValueReferenceType. LValueReferenceType 0x558412998d90 'const char (&)[41]' `-ParenType 0x558412998d30 'const char[41]' sugar `-ConstantArrayType 0x558412998cf0 'const char[41]' 41 `-QualType 0x55841294c271 'const char' const `-BuiltinType 0x55841294c270 'char'
1 parent bbab5e4 commit 52e5c10

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

clang/lib/CodeGen/CGExprConstant.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "clang/AST/Attr.h"
2323
#include "clang/AST/RecordLayout.h"
2424
#include "clang/AST/StmtVisitor.h"
25+
#include "clang/AST/Type.h"
2526
#include "clang/Basic/Builtins.h"
2627
#include "llvm/ADT/STLExtras.h"
2728
#include "llvm/ADT/Sequence.h"
@@ -1358,6 +1359,8 @@ class ConstExprEmitter :
13581359
}
13591360

13601361
llvm::Constant *VisitStringLiteral(StringLiteral *E, QualType T) {
1362+
if (!isa<ConstantArrayType>(T.getDesugaredType(CGM.getContext())))
1363+
return nullptr;
13611364
// This is a string literal initializing an array in an initializer.
13621365
return CGM.GetConstantArrayFromStringLiteral(E);
13631366
}

clang/test/CodeGenCXX/const-init-cxx11.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,8 @@ namespace DR2126 {
424424
// CHECK: @_ZN33ClassTemplateWithStaticDataMember3useE ={{.*}} constant ptr @_ZGRN33ClassTemplateWithStaticDataMember1SIvE1aE_
425425
// CHECK: @_ZGRN39ClassTemplateWithHiddenStaticDataMember1SIvE1aE_ = linkonce_odr hidden constant i32 5, comdat
426426
// CHECK: @_ZN39ClassTemplateWithHiddenStaticDataMember3useE ={{.*}} constant ptr @_ZGRN39ClassTemplateWithHiddenStaticDataMember1SIvE1aE_
427+
// CHECK: @.str.[[STR:[0-9]+]] ={{.*}} constant [9 x i8] c"12345678\00"
428+
// CHECK-NEXT: @e = global %struct.PR69979 { ptr @.str.[[STR]] }
427429
// CHECK: @_ZGRZN20InlineStaticConstRef3funEvE1i_ = linkonce_odr constant i32 10, comdat
428430
// CHECK20: @_ZZN12LocalVarInit4dtorEvE1a = internal constant {{.*}} i32 103
429431

@@ -632,6 +634,10 @@ struct X {
632634
const char *f() { return &X::p; }
633635
}
634636

637+
struct PR69979 {
638+
const char (&d)[9];
639+
} e {"12345678"};
640+
635641
// VirtualMembers::TemplateClass::templateMethod() must be defined in this TU,
636642
// not just declared.
637643
// CHECK: define linkonce_odr void @_ZN14VirtualMembers13TemplateClassIiE14templateMethodEv(ptr {{[^,]*}} %this)

0 commit comments

Comments
 (0)