diff --git a/clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp b/clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp index c41ab54be09ca..74d2448accc3e 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp @@ -254,8 +254,8 @@ class ConstExprEmitter } mlir::Attribute VisitStringLiteral(StringLiteral *e, QualType t) { - cgm.errorNYI(e->getBeginLoc(), "ConstExprEmitter::VisitStringLiteral"); - return {}; + // This is a string literal initializing an array in an initializer. + return cgm.getConstantArrayFromStringLiteral(e); } mlir::Attribute VisitObjCEncodeExpr(ObjCEncodeExpr *e, QualType t) { diff --git a/clang/test/CIR/CodeGen/string-literals.c b/clang/test/CIR/CodeGen/string-literals.c index 00f59b09400c8..90ea21906f363 100644 --- a/clang/test/CIR/CodeGen/string-literals.c +++ b/clang/test/CIR/CodeGen/string-literals.c @@ -5,6 +5,18 @@ // RUN: %clang_cc1 -triple aarch64-none-linux-android21 -emit-llvm %s -o %t.ll // RUN: FileCheck --check-prefix=OGCG --input-file=%t.ll %s +char g_str[] = "1234"; + +// CIR: cir.global external @g_str = #cir.const_array<"1234\00" : !cir.array> : !cir.array + +char g_oversized[100] = "123"; + +// CIR: cir.global external @g_oversized = #cir.const_array<"123" : !cir.array, trailing_zeros> : !cir.array + +char g_exact[4] = "123"; + +// CIR: cir.global external @g_exact = #cir.const_array<"123\00" : !cir.array> : !cir.array + // CIR: cir.global "private" cir_private dsolocal @[[STR1_GLOBAL:.*]] = #cir.const_array<"1\00" : !cir.array> : !cir.array // CIR: cir.global "private" cir_private dsolocal @[[STR2_GLOBAL:.*]] = #cir.zero : !cir.array // CIR: cir.global "private" cir_private dsolocal @[[STR3_GLOBAL:.*]] = #cir.zero : !cir.array diff --git a/clang/test/CIR/CodeGen/string-literals.cpp b/clang/test/CIR/CodeGen/string-literals.cpp new file mode 100644 index 0000000000000..c56eb74387329 --- /dev/null +++ b/clang/test/CIR/CodeGen/string-literals.cpp @@ -0,0 +1,23 @@ +// RUN: %clang_cc1 -triple aarch64-none-linux-android21 -fclangir -emit-cir %s -o %t.cir +// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s +// RUN: %clang_cc1 -triple aarch64-none-linux-android21 -fclangir -emit-llvm %s -o %t-cir.ll +// RUN: FileCheck --check-prefix=LLVM --input-file=%t-cir.ll %s +// RUN: %clang_cc1 -triple aarch64-none-linux-android21 -emit-llvm %s -o %t.ll +// RUN: FileCheck --check-prefix=OGCG --input-file=%t.ll %s + +// CIR: cir.global "private" cir_private dsolocal @[[STR1_GLOBAL:.*]] = #cir.const_array<"abcd\00" : !cir.array> : !cir.array + +// LLVM: @[[STR1_GLOBAL:.*]] = private global [5 x i8] c"abcd\00" + +// OGCG: @[[STR1_GLOBAL:.*]] = private unnamed_addr constant [5 x i8] c"abcd\00" + +decltype(auto) returns_literal() { + return "abcd"; +} + +// CIR: cir.func{{.*}} @_Z15returns_literalv() -> !cir.ptr> +// CIR: %[[RET_ADDR:.*]] = cir.alloca !cir.ptr>, !cir.ptr>>, ["__retval"] +// CIR: %[[STR_ADDR:.*]] = cir.get_global @[[STR1_GLOBAL]] : !cir.ptr> +// CIR: cir.store{{.*}} %[[STR_ADDR]], %[[RET_ADDR]] +// CIR: %[[RET:.*]] = cir.load %[[RET_ADDR]] +// CIR: cir.return %[[RET]]