Hi all,

The attached patch allows StmtPrinter to print signed character
literals. Given the code:
char c = '\200';

The character literal is presently printed as:
char c = '\Uffffff80';

The original literal has the signed value -128 (or unsigned value 128)
and only has any meaning when clamped to the extended ASCII range.
'\Uffffff80' isn't a valid character.

With the patch, the literal is printed as:
char c = '\x80';

As a side note, this doesn't handle multicharacter literals, but I
don't think there is enough information in the AST to make that
possible. They are implementation defined, anyway.

Best
Nick
Index: lib/AST/StmtPrinter.cpp
===================================================================
--- lib/AST/StmtPrinter.cpp	(revision 238537)
+++ lib/AST/StmtPrinter.cpp	(working copy)
@@ -1053,8 +1053,8 @@
   default:
     if (value < 256 && isPrintable((unsigned char)value))
       OS << "'" << (char)value << "'";
-    else if (value < 256)
-      OS << "'\\x" << llvm::format("%02x", value) << "'";
+    else if (value < 256 || CharacterLiteral::Ascii == Node->getKind())
+      OS << "'\\x" << llvm::format("%02x", (unsigned char)value) << "'";
     else if (value <= 0xFFFF)
       OS << "'\\u" << llvm::format("%04x", value) << "'";
     else
Index: test/Sema/ast-print.c
===================================================================
--- test/Sema/ast-print.c	(revision 238537)
+++ test/Sema/ast-print.c	(working copy)
@@ -53,3 +53,6 @@
 
 // CHECK: struct pair_t p = {a: 3, .b = 4};
 struct pair_t p = {a: 3, .b = 4};
+
+// CHECK: char c = '\x80';
+char c = '\200';
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to