[clang] [clang] Fix preprocessor output from #embed (PR #126742)

Mariya Podchishchaeva via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 11 07:23:10 PST 2025


https://github.com/Fznamznon created https://github.com/llvm/llvm-project/pull/126742

When bytes with negative signed char values appear in the data, make sure to use raw bytes from the data string when preprocessing, not char values.

Fixes https://github.com/llvm/llvm-project/issues/102798

>From 76e3de6b6205b7f9b6e3aef82febb94e62920214 Mon Sep 17 00:00:00 2001
From: "Podchishchaeva, Mariya" <mariya.podchishchaeva at intel.com>
Date: Tue, 10 Dec 2024 07:09:57 -0800
Subject: [PATCH] [clang] Fix preprocessor output from #embed

When bytes with negative signed char values appear in the data, make
sure to use raw bytes from the data string when preprocessing, not char
values.

Fixes https://github.com/llvm/llvm-project/issues/102798
---
 clang/docs/ReleaseNotes.rst                        | 3 +++
 clang/lib/Frontend/PrintPreprocessedOutput.cpp     | 5 ++---
 clang/test/Preprocessor/embed_preprocess_to_file.c | 8 ++++++++
 3 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 369d9e9de7d1639..5da929c2a214331 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -141,6 +141,9 @@ Improvements to Coverage Mapping
 Bug Fixes in This Version
 -------------------------
 
+- Clang now outputs correct values when #embed data contains bytes with negative
+  signed char values (#GH102798).
+
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
diff --git a/clang/lib/Frontend/PrintPreprocessedOutput.cpp b/clang/lib/Frontend/PrintPreprocessedOutput.cpp
index 1005825441b3e66..2ae355fb338851d 100644
--- a/clang/lib/Frontend/PrintPreprocessedOutput.cpp
+++ b/clang/lib/Frontend/PrintPreprocessedOutput.cpp
@@ -974,11 +974,10 @@ static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
       // Loop over the contents and print them as a comma-delimited list of
       // values.
       bool PrintComma = false;
-      for (auto Iter = Data->BinaryData.begin(), End = Data->BinaryData.end();
-           Iter != End; ++Iter) {
+      for (unsigned char Byte : Data->BinaryData.bytes()) {
         if (PrintComma)
           *Callbacks->OS << ", ";
-        *Callbacks->OS << static_cast<unsigned>(*Iter);
+        *Callbacks->OS << static_cast<int>(Byte);
         PrintComma = true;
       }
     } else if (Tok.isAnnotation()) {
diff --git a/clang/test/Preprocessor/embed_preprocess_to_file.c b/clang/test/Preprocessor/embed_preprocess_to_file.c
index 9895d958cf96d6d..b3c99d36f784a2b 100644
--- a/clang/test/Preprocessor/embed_preprocess_to_file.c
+++ b/clang/test/Preprocessor/embed_preprocess_to_file.c
@@ -37,3 +37,11 @@ const char even_more[] = {
 // DIRECTIVE-NEXT: #embed <jk.txt> prefix(4, 5,) suffix(, 6, 7) /* clang -E -dE */
 // DIRECTIVE-NEXT:  , 8, 9, 10
 // DIRECTIVE-NEXT: };
+
+constexpr char big_one[] = {
+#embed <big_char.txt>
+};
+
+// EXPANDED: constexpr char big_one[] = {255
+// DIRECTIVE: constexpr char big_one[] = {
+// DIRECTIVE-NEXT: #embed <big_char.txt>



More information about the cfe-commits mailing list