Improve char literal pretty printing, patch by Keith Bauer!


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@39846 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/AST/StmtPrinter.cpp b/AST/StmtPrinter.cpp
index 7832c74..a883996 100644
--- a/AST/StmtPrinter.cpp
+++ b/AST/StmtPrinter.cpp
@@ -292,13 +292,50 @@
 }
 
 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
+  // FIXME should print an L for wchar_t constants
   unsigned value = Node->getValue();
-  if (isprint(value)) {
-    OS << "'" << (char)value << "'";
-  } else {
-    // FIXME something to indicate this is a character literal?
-    OS << std::hex << std::setiosflags(std::ios_base::showbase) << value
-       << std::dec << std::resetiosflags(std::ios_base::showbase);
+  switch (value) {
+  case '\\':
+    OS << "'\\\\'";
+    break;
+  case '\'':
+    OS << "'\\''";
+    break;
+  case '\a':
+    // TODO: K&R: the meaning of '\\a' is different in traditional C
+    OS << "'\\a'";
+    break;
+  case '\b':
+    OS << "'\\b'";
+    break;
+  // Nonstandard escape sequence.
+  /*case '\e':
+    OS << "'\\e'";
+    break;*/
+  case '\f':
+    OS << "'\\f'";
+    break;
+  case '\n':
+    OS << "'\\n'";
+    break;
+  case '\r':
+    OS << "'\\r'";
+    break;
+  case '\t':
+    OS << "'\\t'";
+    break;
+  case '\v':
+    OS << "'\\v'";
+    break;
+  default:
+    if (isprint(value) && value < 256) {
+      OS << "'" << (char)value << "'";
+    } else if (value < 256) {
+      OS << "'\\x" << std::hex << value << std::dec << "'";
+    } else {
+      // FIXME what to really do here?
+      OS << value;
+    }
   }
 }