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
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
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits