[clang] [llvm] [clang] Fix encoding handling of asm strings (PR #169803)

Hubert Tong via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 10 22:02:32 PST 2026


================
@@ -1391,8 +1391,22 @@ StringLiteral::getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
     unsigned TokNumBytes = SLP.GetStringLength();
 
     // If the byte is in this token, return the location of the byte.
-    if (ByteNo < TokNumBytes ||
-        (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
+    //
+    // The string data may have been converted to a different encoding (e.g.
+    // -fexec-charset) than the one obtained by re-lexing the source token here,
+    // so ByteNo can lie beyond the end of the last token. In that case, clamp
+    // to the end of the last token rather than failing.
+    //
+    // FIXME: When the conversion changes the number of bytes (e.g. a
+    // multi-byte UTF-8 sequence that maps to a single byte), ByteNo no longer
+    // corresponds to the byte offset in the source spelling, so the returned
----------------
hubert-reinterpretcast wrote:

A non-asm case that generates the assertion failure without the clamping change is
```c
int printf(const char *, ...);
int main(void) {
  printf("\N{MULTIPLICATION SIGN}%s");
}
```
when compiled using
```
clang -cc1 -fexec-charset ISO-2022-JP printfdiag.c
```

I believe this is different enough that the clamping change should be in a separate PR alongside this test and the non-z/OS-asm-specific FIXME. In particular, this shows that the clamping change actually falls outside the PR's stated scope, under which it was reviewed.

The z/OS asm diagnostic-location test should remain in this PR as it relates to the other FIXME.

https://github.com/llvm/llvm-project/pull/169803


More information about the cfe-commits mailing list