steven_watanabe created this revision.
steven_watanabe added a reviewer: rsmith.
steven_watanabe added a subscriber: cfe-commits.

Currently literals like '\xff' may be sign extended, resulting in output like 
'\Uffffffff', which is not valid.

http://reviews.llvm.org/D17206

Files:
  lib/AST/StmtPrinter.cpp
  test/Misc/ast-print-char-literal.cpp

Index: test/Misc/ast-print-char-literal.cpp
===================================================================
--- test/Misc/ast-print-char-literal.cpp
+++ test/Misc/ast-print-char-literal.cpp
@@ -13,6 +13,8 @@
   h<u8'2'>();
 }
 
+char j = '\xFF';
+
 // CHECK: char c = u8'1';
 // CHECK-NEXT: char d = '1';
 // CHECK-NEXT: char e = U'1';
@@ -22,3 +24,4 @@
 // CHECK: template <char c = u8'1'>
 
 // CHECK: h<u8'2'>();
+// CHECK: char j = '\xff';
Index: lib/AST/StmtPrinter.cpp
===================================================================
--- lib/AST/StmtPrinter.cpp
+++ lib/AST/StmtPrinter.cpp
@@ -1250,6 +1250,10 @@
     OS << "'\\v'";
     break;
   default:
+    // A character literal might be sign-extended, which
+    // would result in an invalid \U escape sequence.
+    if ((value & ~0xFFu) == ~0xFFu && Node->getKind() == 
CharacterLiteral::Ascii)
+      value &= 0xFFu;
     if (value < 256 && isPrintable((unsigned char)value))
       OS << "'" << (char)value << "'";
     else if (value < 256)


Index: test/Misc/ast-print-char-literal.cpp
===================================================================
--- test/Misc/ast-print-char-literal.cpp
+++ test/Misc/ast-print-char-literal.cpp
@@ -13,6 +13,8 @@
   h<u8'2'>();
 }
 
+char j = '\xFF';
+
 // CHECK: char c = u8'1';
 // CHECK-NEXT: char d = '1';
 // CHECK-NEXT: char e = U'1';
@@ -22,3 +24,4 @@
 // CHECK: template <char c = u8'1'>
 
 // CHECK: h<u8'2'>();
+// CHECK: char j = '\xff';
Index: lib/AST/StmtPrinter.cpp
===================================================================
--- lib/AST/StmtPrinter.cpp
+++ lib/AST/StmtPrinter.cpp
@@ -1250,6 +1250,10 @@
     OS << "'\\v'";
     break;
   default:
+    // A character literal might be sign-extended, which
+    // would result in an invalid \U escape sequence.
+    if ((value & ~0xFFu) == ~0xFFu && Node->getKind() == CharacterLiteral::Ascii)
+      value &= 0xFFu;
     if (value < 256 && isPrintable((unsigned char)value))
       OS << "'" << (char)value << "'";
     else if (value < 256)
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to