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

Hubert Tong via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 12 16:57:42 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:

> I created the following two issues. Please let me know if I described the issue correctly [#241514](https://github.com/llvm/llvm-project/issues/241514) [#241524](https://github.com/llvm/llvm-project/issues/241524)

Insofar as https://github.com/llvm/llvm-project/issues/241514 is focused on the ICE as a symptom and the clamping as a solution, it is acceptable as a tracking issue for the ICE.

Of the two incorrect diagnostic-cursor problems (string literal under `-fexec-charset` and z/OS asm string literal), https://github.com/llvm/llvm-project/issues/241524 should only be used to track one. As such, a separate GH issue should be opened to track the other. Of these two problems, I believe more effort should be invested for `-fexec-charset` string literals to determine the correct corresponding source location.

For the z/OS asm string problem, I suggest resolving it by first improving the diagnostic for computed asm strings and then applying said solution. In particular, a diagnostic for the computed asm case currently points to the whole constant expression, giving no specific indicator into the result string (https://github.com/llvm/llvm-project/pull/169803#discussion_r4519508676). We could attach a separate diagnostic with an artificial source location populated with some representation of the converted string. This cannot be applied to the `-fexec-charset` string literal case because, unlike the cases discussed here, we do not require successful conversion of the string value to UTF-8.

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


More information about the cfe-commits mailing list