[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 May 22 06:06:56 PDT 2026
https://github.com/abhina-sree updated https://github.com/llvm/llvm-project/pull/196569
>From 343a4568d6dcf29d27d3388af55efb0cd48460df 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/TextEncodingConfig.cpp | 5 +++--
clang/test/CodeGen/systemz-charset.cpp | 4 ++++
5 files changed, 22 insertions(+), 2 deletions(-)
diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h
index ec7d4fcd4d8e3..6c0e65a85ee13 100644
--- a/clang/include/clang/Basic/TargetInfo.h
+++ b/clang/include/clang/Basic/TargetInfo.h
@@ -326,6 +326,8 @@ class TargetInfo : public TransferrableTargetInfo,
llvm::TextEncodingConverter *FormatStrConverter;
+ 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 a0894318dbd53..80e073385ce82 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -13752,6 +13752,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 43efca42886cc..0c553033ad069 100644
--- a/clang/lib/Basic/TargetInfo.cpp
+++ b/clang/lib/Basic/TargetInfo.cpp
@@ -197,6 +197,9 @@ TargetInfo::TargetInfo(const llvm::Triple &T) : Triple(T) {
FormatStrConverter = new 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/TextEncodingConfig.cpp b/clang/lib/Lex/TextEncodingConfig.cpp
index bc4a6b220412b..6df88e258ffde 100644
--- a/clang/lib/Lex/TextEncodingConfig.cpp
+++ b/clang/lib/Lex/TextEncodingConfig.cpp
@@ -37,10 +37,11 @@ TextEncodingConfig::setConvertersFromOptions(TextEncodingConfig &TEC,
return std::error_code();
ErrorOr<TextEncodingConverter> ErrorOrConverter =
llvm::TextEncodingConverter::create(UTF8, TEC.ExecEncoding);
- if (ErrorOrConverter)
+ if (ErrorOrConverter) {
TEC.ToExecEncodingConverter =
new TextEncodingConverter(std::move(*ErrorOrConverter));
- else
+ TInfo.ExecStrConverter = TEC.ToExecEncodingConverter;
+ } 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 396b82909fc9b..ffab1c50ed3d5 100644
--- a/clang/test/CodeGen/systemz-charset.cpp
+++ b/clang/test/CodeGen/systemz-charset.cpp
@@ -71,3 +71,7 @@ const char16_t *UnicodeUCNString16 = u"\u00E2\u00AC\U000000DF";
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]
+
+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 83b06f8faedaed38551be7223429e8b3d18f2dab 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 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 80e073385ce82..0da35a63253f1 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -13746,6 +13746,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(
@@ -13753,15 +13762,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;
}
More information about the llvm-branch-commits
mailing list