[llvm] 3bba538 - [AsmWriter] Use unsigned char more consistently

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 8 07:28:31 PST 2023


Author: Nikita Popov
Date: 2023-12-08T16:28:23+01:00
New Revision: 3bba53854a21177b5423f9212343ecec6316e4bf

URL: https://github.com/llvm/llvm-project/commit/3bba53854a21177b5423f9212343ecec6316e4bf
DIFF: https://github.com/llvm/llvm-project/commit/3bba53854a21177b5423f9212343ecec6316e4bf.diff

LOG: [AsmWriter] Use unsigned char more consistently

On platforms where char is signed, the ">> 4" shift will produce
incorrect results. We were already working on unsigned char for
most characters, but not for the first one.

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

Added: 
    

Modified: 
    llvm/lib/IR/AsmWriter.cpp
    llvm/test/Assembler/named-metadata.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp
index bff64e3a15a24..95cdec722062e 100644
--- a/llvm/lib/IR/AsmWriter.cpp
+++ b/llvm/lib/IR/AsmWriter.cpp
@@ -3520,15 +3520,15 @@ static void printMetadataIdentifier(StringRef Name,
   if (Name.empty()) {
     Out << "<empty name> ";
   } else {
-    if (isalpha(static_cast<unsigned char>(Name[0])) || Name[0] == '-' ||
-        Name[0] == '$' || Name[0] == '.' || Name[0] == '_')
-      Out << Name[0];
+    unsigned char FirstC = static_cast<unsigned char>(Name[0]);
+    if (isalpha(FirstC) || FirstC == '-' || FirstC == '$' || FirstC == '.' ||
+        FirstC == '_')
+      Out << FirstC;
     else
-      Out << '\\' << hexdigit(Name[0] >> 4) << hexdigit(Name[0] & 0x0F);
+      Out << '\\' << hexdigit(FirstC >> 4) << hexdigit(FirstC & 0x0F);
     for (unsigned i = 1, e = Name.size(); i != e; ++i) {
       unsigned char C = Name[i];
-      if (isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
-          C == '.' || C == '_')
+      if (isalnum(C) || C == '-' || C == '$' || C == '.' || C == '_')
         Out << C;
       else
         Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);

diff  --git a/llvm/test/Assembler/named-metadata.ll b/llvm/test/Assembler/named-metadata.ll
index 9fa37a7989d4f..32ebf2bfd26f3 100644
--- a/llvm/test/Assembler/named-metadata.ll
+++ b/llvm/test/Assembler/named-metadata.ll
@@ -23,3 +23,7 @@
 ; when emitting it, followed by xfoo.
 !\xfoo = !{!0, !1, !2}
 ; CHECK: !\5Cxfoo = !{!0, !1, !2}
+
+; Make sure we handle escapes with the high bit set correctly.
+!\FFfoo = !{!0, !1, !2}
+; CHECK: !\FFfoo = !{!0, !1, !2}


        


More information about the llvm-commits mailing list