Skip to content

Commit

Permalink
[CIR] [Lowering] care trailing zero for lowering constant array
Browse files Browse the repository at this point in the history
  • Loading branch information
ChuanqiXu9 committed Oct 15, 2024
1 parent b130f71 commit 21b6d4f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2229,7 +2229,10 @@ class CIRGlobalOpLowering
if (auto constArr =
mlir::dyn_cast<mlir::cir::ConstArrayAttr>(init.value())) {
if (auto attr = mlir::dyn_cast<mlir::StringAttr>(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<mlir::ArrayAttr>(constArr.getElts())) {
// Failed to use a compact attribute as an initializer:
Expand Down
9 changes: 9 additions & 0 deletions clang/test/CIR/Lowering/str.c
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 21b6d4f

Please sign in to comment.