[clang] [Clang] Fix char width assumption in __annotation (PR #193731)

via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 23 05:56:59 PDT 2026


https://github.com/kjedruczyk created https://github.com/llvm/llvm-project/pull/193731

The __annotation builtin in `Clang` hardcoded an assumption that wide string literal arguments are always `UTF-16`, which holds on Windows but fails on platforms where `wchar_t` is 4 bytes. The fix widens the assertion to also accept 4-byte characters.

Fixes #184318

>From 9f4a8007ac96ee96e15da962e0bc51c9429c5761 Mon Sep 17 00:00:00 2001
From: Krzysztof Jedruczyk <kjedruczyk at bloomberg.net>
Date: Thu, 23 Apr 2026 13:22:02 +0100
Subject: [PATCH] [Clang] Fix char width assumption in __annotation

---
 clang/lib/CodeGen/CGBuiltin.cpp | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 7a745b6e36138..67de2a34f44ea 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -5597,12 +5597,17 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
     SmallVector<Metadata *, 1> Strings;
     for (const Expr *Arg : E->arguments()) {
       const auto *Str = cast<StringLiteral>(Arg->IgnoreParenCasts());
-      assert(Str->getCharByteWidth() == 2);
+      assert(Str->getCharByteWidth() == 2 || Str->getCharByteWidth() == 4);
       StringRef WideBytes = Str->getBytes();
       std::string StrUtf8;
-      if (!convertUTF16ToUTF8String(
-              ArrayRef(WideBytes.data(), WideBytes.size()), StrUtf8)) {
-        CGM.ErrorUnsupported(E, "non-UTF16 __annotation argument");
+      bool Converted =
+          (Str->getCharByteWidth() == 2)
+              ? convertUTF16ToUTF8String(
+                    ArrayRef(WideBytes.data(), WideBytes.size()), StrUtf8)
+              : convertUTF32ToUTF8String(
+                    ArrayRef(WideBytes.data(), WideBytes.size()), StrUtf8);
+      if (!Converted) {
+        CGM.ErrorUnsupported(E, "non-Unicode __annotation argument");
         continue;
       }
       Strings.push_back(llvm::MDString::get(getLLVMContext(), StrUtf8));



More information about the cfe-commits mailing list