Skip to content

Commit e6d2597

Browse files
ChuanqiXu9lanza
authored andcommitted
[CIR] [Lowering] care trailing zero for lowering constant array (#976)
Close #975 See the attached test case for example
1 parent 02f6c9d commit e6d2597

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2283,7 +2283,10 @@ class CIRGlobalOpLowering
22832283
if (auto constArr =
22842284
mlir::dyn_cast<mlir::cir::ConstArrayAttr>(init.value())) {
22852285
if (auto attr = mlir::dyn_cast<mlir::StringAttr>(constArr.getElts())) {
2286-
init = rewriter.getStringAttr(attr.getValue());
2286+
llvm::SmallString<256> literal(attr.getValue());
2287+
if (constArr.getTrailingZerosNum())
2288+
literal.append(constArr.getTrailingZerosNum(), '\0');
2289+
init = rewriter.getStringAttr(literal);
22872290
} else if (auto attr =
22882291
mlir::dyn_cast<mlir::ArrayAttr>(constArr.getElts())) {
22892292
// Failed to use a compact attribute as an initializer:

clang/test/CIR/Lowering/str.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll
2+
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM
3+
4+
void f(char *fmt, ...);
5+
void test() {
6+
f("test\0");
7+
}
8+
9+
// LLVM: @.str = {{.*}}[6 x i8] c"test\00\00"

0 commit comments

Comments
 (0)