[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 Nov 20 11:48:03 PST 2026


https://github.com/abhina-sree updated https://github.com/llvm/llvm-project/pull/196569

>From 39fde2d9af35364b6288bcffb4462e11c6481403 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/test/CodeGen/systemz-charset.cpp |  4 +++-
 4 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h
index 7c5c35a5cfb7c5..2dd3390897605b 100644
--- a/clang/include/clang/Basic/TargetInfo.h
+++ b/clang/include/clang/Basic/TargetInfo.h
@@ -330,6 +330,8 @@ class TargetInfo : public TransferrableTargetInfo,
 
   virtual ~TargetInfo();
 
+  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 7d5fd98b348941..92cd40225c61fb 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -13963,6 +13963,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 6e390226d2e4b8..fb08d71ec8595f 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) {
   MaxOpenCLWorkGroupSize = 1024;
 
   MaxBitIntWidth.reset();
+
+  ExecStrConverter = new llvm::TextEncodingConverter(
+      std::move(*llvm::TextEncodingConverter::createNoopConverter()));
 }
 
 // Out of line virtual dtor for TargetInfo.
diff --git a/clang/test/CodeGen/systemz-charset.cpp b/clang/test/CodeGen/systemz-charset.cpp
index 9647b2e88af53e..ae65e102e10a11 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 {
     const char* D;
     int S;
@@ -102,3 +101,6 @@ void function()
   // CHECK-UTF8: asm{{.*}}\09nop\0A
 }
 
+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 d749e67f5f1e6fe4b1445db8e6bac4cec558eca8 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 |  2 ++
 2 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 92cd40225c61fb..6fb02c8e1f696e 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -13957,6 +13957,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(
@@ -13964,15 +13973,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 7252a68585617c..2136d5009943f8 100644
--- a/clang/lib/Lex/TextEncoding.cpp
+++ b/clang/lib/Lex/TextEncoding.cpp
@@ -56,6 +56,8 @@ TextEncoding::setConvertersFromOptions(TextEncoding &TE,
       return ErrorOrLiteralConverter.getError();
   }
 
+   TInfo.ExecStrConverter = TE.ToLiteralEncodingConverter.get();
+
   // The IBM-1047 converters are only needed on z/OS, where the system
   // (assembler/linker) default encoding is IBM-1047 rather than UTF-8.
   if (TInfo.getTriple().getOS() != llvm::Triple::ZOS)



More information about the llvm-branch-commits mailing list