[PATCH] D16433: Fix printing signed character literals

Nick Sumner via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 21 15:28:53 PST 2016


nick.sumner created this revision.
nick.sumner added reviewers: bkramer, rsmith.
nick.sumner added a subscriber: cfe-commits.

Allow 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.

http://reviews.llvm.org/D16433

Files:
  lib/AST/StmtPrinter.cpp
  test/Sema/ast-print.c

Index: test/Sema/ast-print.c
===================================================================
--- test/Sema/ast-print.c
+++ test/Sema/ast-print.c
@@ -66,3 +66,6 @@
     c;
   });
 }
+
+// CHECK: char c = '\x80';
+char c = '\200';
Index: lib/AST/StmtPrinter.cpp
===================================================================
--- lib/AST/StmtPrinter.cpp
+++ lib/AST/StmtPrinter.cpp
@@ -1230,8 +1230,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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D16433.45607.patch
Type: text/x-patch
Size: 854 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160121/5a9fdc10/attachment.bin>


More information about the cfe-commits mailing list