From 21b6d4fa519aa1505c8e102071d06830ac4d7ff4 Mon Sep 17 00:00:00 2001 From: Chuanqi Xu Date: Tue, 15 Oct 2024 14:50:53 +0800 Subject: [PATCH] [CIR] [Lowering] care trailing zero for lowering constant array --- clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 5 ++++- clang/test/CIR/Lowering/str.c | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 clang/test/CIR/Lowering/str.c diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 94d889d535d0..93221b409df2 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -2229,7 +2229,10 @@ class CIRGlobalOpLowering if (auto constArr = mlir::dyn_cast(init.value())) { if (auto attr = mlir::dyn_cast(constArr.getElts())) { - init = rewriter.getStringAttr(attr.getValue()); + llvm::SmallString<256> literal(attr.getValue()); + if (constArr.getTrailingZerosNum()) + literal.append(constArr.getTrailingZerosNum(), '\0'); + init = rewriter.getStringAttr(literal); } else if (auto attr = mlir::dyn_cast(constArr.getElts())) { // Failed to use a compact attribute as an initializer: diff --git a/clang/test/CIR/Lowering/str.c b/clang/test/CIR/Lowering/str.c new file mode 100644 index 000000000000..cc393daee884 --- /dev/null +++ b/clang/test/CIR/Lowering/str.c @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll +// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM + +void f(char *fmt, ...); +void test() { + f("test\0"); +} + +// LLVM: @.str = {{.*}}[6 x i8] c"test\00\00"