https://github.com/abhina-sree updated https://github.com/llvm/llvm-project/pull/196569
>From 8b99097451e212505cc469b46703af3d94d891aa Mon Sep 17 00:00:00 2001 From: Abhina Sreeskantharajan <[email protected]> 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 | 4 +++- 5 files changed, 20 insertions(+), 2 deletions(-) diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h index 6311b6b567a5e..81b1a056a4c4a 100644 --- a/clang/include/clang/Basic/TargetInfo.h +++ b/clang/include/clang/Basic/TargetInfo.h @@ -329,6 +329,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 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 1bb0026d8422e..9cc2a737fafbf 100644 --- a/clang/lib/Basic/TargetInfo.cpp +++ b/clang/lib/Basic/TargetInfo.cpp @@ -198,6 +198,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/lib/Lex/TextEncoding.cpp b/clang/lib/Lex/TextEncoding.cpp index c988a0123e4ef..90def3a1c4e31 100644 --- a/clang/lib/Lex/TextEncoding.cpp +++ b/clang/lib/Lex/TextEncoding.cpp @@ -69,7 +69,8 @@ TextEncoding::setConvertersFromOptions(TextEncoding &TE, if (ErrorOrConverter) TE.FromIBM1047Converter = std::make_unique<TextEncodingConverter>(std::move(*ErrorOrConverter)); - else + TInfo.ExecStrConverter = TEC.ToLiteralEncodingConverter.get(); + } else return ErrorOrConverter.getError(); return std::error_code(); diff --git a/clang/test/CodeGen/systemz-charset.cpp b/clang/test/CodeGen/systemz-charset.cpp index b6b79842468c1..7cb100f05206b 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; @@ -100,3 +99,6 @@ void function() // 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 d16d9b80f6d49ae66c4c28adaf92ebc053ca653f Mon Sep 17 00:00:00 2001 From: Abhina Sreeskantharajan <[email protected]> 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 90def3a1c4e31..86b7cb1fbca58 100644 --- a/clang/lib/Lex/TextEncoding.cpp +++ b/clang/lib/Lex/TextEncoding.cpp @@ -47,6 +47,8 @@ TextEncoding::setConvertersFromOptions(TextEncoding &TE, return ErrorOrLiteralConverter.getError(); } + TInfo.ExecStrConverter = TE.ToLiteralEncodingConverter.get(); + if (TInfo.getDefaultOrdinaryLiteralEncoding() == UTF8) return std::error_code(); @@ -69,8 +71,7 @@ TextEncoding::setConvertersFromOptions(TextEncoding &TE, if (ErrorOrConverter) TE.FromIBM1047Converter = std::make_unique<TextEncodingConverter>(std::move(*ErrorOrConverter)); - TInfo.ExecStrConverter = TEC.ToLiteralEncodingConverter.get(); - } else + else return ErrorOrConverter.getError(); return std::error_code(); _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
