[llvm-branch-commits] [clang] Convert to exec-charset inside getPredefinedStringLiteralFromCache (PR #196569)
Abhina Sree via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Fri Sep 4 06:56:47 PDT 2026
https://github.com/abhina-sree updated https://github.com/llvm/llvm-project/pull/196569
>From 0d6d0149fd108bb0be5f312086aa1aab696796e2 Mon Sep 17 00:00:00 2001
From: Abhina Sreeskantharajan <Abhina.Sreeskantharajan at ibm.com>
Date: Fri, 8 May 2026 12:20:45 -0400
Subject: [PATCH 1/2] convert to exec-charset inside
getPredefinedStringLiteralFromCache, test __builtin_FILE()
---
clang/include/clang/Basic/TargetInfo.h | 2 ++
clang/lib/AST/ASTContext.cpp | 10 ++++++++++
clang/lib/Basic/TargetInfo.cpp | 3 +++
clang/lib/Lex/TextEncoding.cpp | 3 ++-
clang/test/CodeGen/systemz-charset.cpp | 5 ++++-
5 files changed, 21 insertions(+), 2 deletions(-)
diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h
index 0518e8beb5a68..94c6acfb9f244 100644
--- a/clang/include/clang/Basic/TargetInfo.h
+++ b/clang/include/clang/Basic/TargetInfo.h
@@ -332,6 +332,8 @@ class TargetInfo : public TransferrableTargetInfo,
std::unique_ptr<llvm::TextEncodingConverter> FromSystemEncodingConverter;
+ llvm::TextEncodingConverter *ExecStrConverter;
+
/// Retrieve the target options.
TargetOptions &getTargetOpts() const {
assert(TargetOpts && "Missing target options");
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 35a2a0b131a3f..067a16b46d0ea 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -13897,6 +13897,16 @@ ASTContext::getPredefinedStringLiteralFromCache(StringRef Key) const {
*this, Key, StringLiteralKind::Ordinary,
/*Pascal*/ false, getStringLiteralArrayType(CharTy, Key.size()),
SourceLocation());
+
+ llvm::TextEncodingConverter *Converter = getTargetInfo().ExecStrConverter;
+ if (Converter) {
+ SmallString<128> Converted;
+ Converter->convert(Result->getString(), Converted);
+ Result = StringLiteral::Create(
+ *this, Converted, StringLiteralKind::Ordinary, /*Pascal*/ false,
+ getStringLiteralArrayType(CharTy, Converted.size()), SourceLocation());
+ }
+
return Result;
}
diff --git a/clang/lib/Basic/TargetInfo.cpp b/clang/lib/Basic/TargetInfo.cpp
index 3e08c2a1f4c06..2d4972225c0ba 100644
--- a/clang/lib/Basic/TargetInfo.cpp
+++ b/clang/lib/Basic/TargetInfo.cpp
@@ -201,6 +201,9 @@ TargetInfo::TargetInfo(const llvm::Triple &T) : Triple(T) {
FromSystemEncodingConverter = std::make_unique<llvm::TextEncodingConverter>(
std::move(*llvm::TextEncodingConverter::createNoopConverter()));
+
+ ExecStrConverter = new llvm::TextEncodingConverter(
+ std::move(*llvm::TextEncodingConverter::createNoopConverter()));
}
// Out of line virtual dtor for TargetInfo.
diff --git a/clang/lib/Lex/TextEncoding.cpp b/clang/lib/Lex/TextEncoding.cpp
index fd59f3df59bdf..3250a0c50a21a 100644
--- a/clang/lib/Lex/TextEncoding.cpp
+++ b/clang/lib/Lex/TextEncoding.cpp
@@ -55,7 +55,8 @@ TextEncoding::setConvertersFromOptions(TextEncoding &TE,
if (ErrorOrConverter)
TE.ToSystemEncodingConverter =
std::make_unique<TextEncodingConverter>(std::move(*ErrorOrConverter));
- else
+ TInfo.ExecStrConverter = TEC.ToLiteralEncodingConverter.get();
+ } else
return ErrorOrConverter.getError();
ErrorOrConverter = llvm::TextEncodingConverter::create(
diff --git a/clang/test/CodeGen/systemz-charset.cpp b/clang/test/CodeGen/systemz-charset.cpp
index 6348fb72dac9a..473e17b489285 100644
--- a/clang/test/CodeGen/systemz-charset.cpp
+++ b/clang/test/CodeGen/systemz-charset.cpp
@@ -73,7 +73,6 @@ const char32_t *UnicodeUCNString32 = U"\u00E2\u00AC\U000000DF";
//CHECK: [4 x i32] [i32 226, i32 172, i32 223, i32 0]
//CHECK-UTF8: [4 x i32] [i32 226, i32 172, i32 223, i32 0]
-
struct string_view {
int S;
const char* D;
@@ -96,3 +95,7 @@ void function()
}
// CHECK: asm{{.*}}|\86\96\96
// CHECK-UTF8: asm{{.*}}|foo
+
+const char *file = __builtin_FILE();
+//CHECK: {{.*}}\A2\A8\A2\A3\85\94\A9`\83\88\81\99\A2\85\A3K\83\97\97\00"
+//CHECK-UTF8: {{.*}}systemz-charset.cpp\00"
>From 088e1abdde07f528e197e9d2e6dcbbdfdf9c9b0f Mon Sep 17 00:00:00 2001
From: Abhina Sreeskantharajan <Abhina.Sreeskantharajan at ibm.com>
Date: Fri, 22 May 2026 08:51:37 -0400
Subject: [PATCH 2/2] Convert the key before cache lookup to prevent encoding
differences
---
clang/lib/AST/ASTContext.cpp | 18 +++++++++---------
clang/lib/Lex/TextEncoding.cpp | 5 +++--
2 files changed, 12 insertions(+), 11 deletions(-)
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 067a16b46d0ea..ba902065a8ba6 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -13891,6 +13891,15 @@ QualType ASTContext::getStringLiteralArrayType(QualType EltTy,
StringLiteral *
ASTContext::getPredefinedStringLiteralFromCache(StringRef Key) const {
+ // Apply encoding conversion to the key before cache lookup to ensure
+ // proper deduplication when the same source location is used multiple times
+ SmallString<128> ConvertedKey;
+ llvm::TextEncodingConverter *Converter = getTargetInfo().ExecStrConverter;
+ if (Converter) {
+ Converter->convert(Key, ConvertedKey);
+ Key = ConvertedKey;
+ }
+
StringLiteral *&Result = StringLiteralCache[Key];
if (!Result)
Result = StringLiteral::Create(
@@ -13898,15 +13907,6 @@ ASTContext::getPredefinedStringLiteralFromCache(StringRef Key) const {
/*Pascal*/ false, getStringLiteralArrayType(CharTy, Key.size()),
SourceLocation());
- llvm::TextEncodingConverter *Converter = getTargetInfo().ExecStrConverter;
- if (Converter) {
- SmallString<128> Converted;
- Converter->convert(Result->getString(), Converted);
- Result = StringLiteral::Create(
- *this, Converted, StringLiteralKind::Ordinary, /*Pascal*/ false,
- getStringLiteralArrayType(CharTy, Converted.size()), SourceLocation());
- }
-
return Result;
}
diff --git a/clang/lib/Lex/TextEncoding.cpp b/clang/lib/Lex/TextEncoding.cpp
index 3250a0c50a21a..58664761d1c4a 100644
--- a/clang/lib/Lex/TextEncoding.cpp
+++ b/clang/lib/Lex/TextEncoding.cpp
@@ -44,6 +44,8 @@ TextEncoding::setConvertersFromOptions(TextEncoding &TE,
return ErrorOrLiteralConverter.getError();
}
+ TInfo.ExecStrConverter = TE.ToLiteralEncodingConverter.get();
+
if (TInfo.getDefaultOrdinaryLiteralEncoding() == UTF8)
return std::error_code();
@@ -55,8 +57,7 @@ TextEncoding::setConvertersFromOptions(TextEncoding &TE,
if (ErrorOrConverter)
TE.ToSystemEncodingConverter =
std::make_unique<TextEncodingConverter>(std::move(*ErrorOrConverter));
- TInfo.ExecStrConverter = TEC.ToLiteralEncodingConverter.get();
- } else
+ else
return ErrorOrConverter.getError();
ErrorOrConverter = llvm::TextEncodingConverter::create(
More information about the llvm-branch-commits
mailing list