[clang] Convert user-defined literals to fexec-charset (PR #222037)

via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 8 08:48:01 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Abhina Sree (abhina-sree)

<details>
<summary>Changes</summary>

This patch converts user-defined literals to the fexec-charset

---
Full diff: https://github.com/llvm/llvm-project/pull/222037.diff


2 Files Affected:

- (modified) clang/lib/Sema/SemaExpr.cpp (+24-4) 
- (modified) clang/test/CodeGenCXX/cxx11-user-defined-literal.cpp (+12-6) 


``````````diff
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index d06079a43e23a..e26bd94edfb6c 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -3846,6 +3846,17 @@ bool Sema::CheckLoopHintExpr(Expr *E, SourceLocation Loc, bool AllowZero) {
   return false;
 }
 
+static Token createToken(StringRef SpellString, SourceLocation TokLoc) {
+  Token StringTok;
+  StringTok.startToken();
+  StringTok.setKind(tok::string_literal);
+  StringTok.setLocation(TokLoc);
+  StringTok.setLiteralData(SpellString.data());
+  StringTok.setLength(SpellString.size());
+
+  return StringTok;
+}
+
 ExprResult Sema::ActOnNumericConstant(const Token &Tok, Scope *UDLScope) {
   // Fast path for a single digit (which is quite common).  A single digit
   // cannot have a trigraph, escaped newline, radix prefix, or suffix.
@@ -3939,10 +3950,19 @@ ExprResult Sema::ActOnNumericConstant(const Token &Tok, Scope *UDLScope) {
       QualType StrTy = Context.getConstantArrayType(
           Context.adjustStringLiteralBaseType(Context.CharTy.withConst()),
           llvm::APInt(32, Length + 1), nullptr, ArraySizeModifier::Normal, 0);
-      Expr *Lit =
-          StringLiteral::Create(Context, StringRef(TokSpelling.data(), Length),
-                                StringLiteralKind::Ordinary,
-                                /*Pascal*/ false, StrTy, TokLoc);
+      SmallString<256> SpellString;
+      SpellString += "\"";
+      SpellString += StringRef(TokSpelling.data(), Length);
+      SpellString += "\"";
+      Token StringTok = createToken(SpellString, TokLoc);
+      StringLiteralParser TempLiteral(StringTok, PP,
+                                      StringLiteralEvalMethod::Evaluated,
+                                      CA_ToLiteralEncoding);
+      if (TempLiteral.hadError)
+        return ExprError();
+      Expr *Lit = StringLiteral::Create(Context, TempLiteral.GetString(),
+                                        StringLiteralKind::Ordinary,
+                                        /*Pascal*/ false, StrTy, TokLoc);
       return BuildLiteralOperatorCall(R, OpNameInfo, Lit, TokLoc);
     }
 
diff --git a/clang/test/CodeGenCXX/cxx11-user-defined-literal.cpp b/clang/test/CodeGenCXX/cxx11-user-defined-literal.cpp
index 3b18a9ad811c3..2528d82ae573c 100644
--- a/clang/test/CodeGenCXX/cxx11-user-defined-literal.cpp
+++ b/clang/test/CodeGenCXX/cxx11-user-defined-literal.cpp
@@ -1,4 +1,5 @@
-// RUN: %clang_cc1 -std=c++11 -triple x86_64-linux-gnu -emit-llvm %s -o - | FileCheck %s
+// RUN: %clang_cc1 -std=c++11 -triple x86_64-linux-gnu -emit-llvm %s -o - | FileCheck -check-prefix=CHECK-ASCII -check-prefix=CHECK %s
+// RUN: %clang_cc1 -std=c++11 -triple x86_64-linux-gnu -fexec-charset IBM-1047 -emit-llvm %s -o - | FileCheck -check-prefix=CHECK-EBCDIC -check-prefix=CHECK %s
 
 struct S { S(); ~S(); S(const S &); void operator()(int); };
 using size_t = decltype(sizeof(int));
@@ -9,11 +10,16 @@ S operator"" _f(long double);
 S operator"" _r(const char *);
 template<char...Cs> S operator"" _t() { return S(); }
 
-// CHECK: @[[s_foo:.*]] = {{.*}} constant [4 x i8] c"foo\00"
-// CHECK: @[[s_bar:.*]] = {{.*}} constant [4 x i8] c"bar\00"
-// CHECK: @[[s_123:.*]] = {{.*}} constant [4 x i8] c"123\00"
-// CHECK: @[[s_4_9:.*]] = {{.*}} constant [4 x i8] c"4.9\00"
-// CHECK: @[[s_0xffffeeee:.*]] = {{.*}} constant [11 x i8] c"0xffffeeee\00"
+// CHECK-ASCII:  @[[s_foo:.*]] = {{.*}} constant [4 x i8] c"foo\00"
+// CHECK-EBCDIC: @[[s_foo:.*]] = {{.*}} constant [4 x i8] c"\86\96\96\00"
+// CHECK-ASCII:  @[[s_bar:.*]] = {{.*}} constant [4 x i8] c"bar\00"
+// CHECK-EBCDIC: @[[s_bar:.*]] = {{.*}} constant [4 x i8] c"\82\81\99\00"
+// CHECK-ASCII:  @[[s_123:.*]] = {{.*}} constant [4 x i8] c"123\00"
+// CHECK-EBCDIC: @[[s_123:.*]] = {{.*}} constant [4 x i8] c"\F1\F2\F3\00"
+// CHECK-ASCII:  @[[s_4_9:.*]] = {{.*}} constant [4 x i8] c"4.9\00"
+// CHECK-EBCDIC: @[[s_4_9:.*]] = {{.*}} constant [4 x i8] c"\F4K\F9\00"
+// CHECK-ASCII:  @[[s_0xffffeeee:.*]] = {{.*}} constant [11 x i8] c"0xffffeeee\00"
+// CHECK-EBCDIC: @[[s_0xffffeeee:.*]] = {{.*}} constant [11 x i8] c"\F0\A7\86\86\86\86\85\85\85\85\00"
 
 void f() {
   // CHECK: call void @_Zli2_xPKcm({{.*}}, ptr noundef @[[s_foo]], i64 noundef 3)

``````````

</details>


https://github.com/llvm/llvm-project/pull/222037


More information about the cfe-commits mailing list