[cfe-commits] r39469 - /cfe/cfe/trunk/AST/StmtPrinter.cpp

clattner at cs.uiuc.edu clattner at cs.uiuc.edu
Wed Jul 11 09:44:44 PDT 2007


Author: clattner
Date: Wed Jul 11 11:44:44 2007
New Revision: 39469

URL: http://llvm.org/viewvc/llvm-project?rev=39469&view=rev
Log:
pretty print switch/if stmts whose bodies are compound stmts.

Modified:
    cfe/cfe/trunk/AST/StmtPrinter.cpp

Modified: cfe/cfe/trunk/AST/StmtPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/StmtPrinter.cpp?rev=39469&r1=39468&r2=39469&view=diff

==============================================================================
--- cfe/cfe/trunk/AST/StmtPrinter.cpp (original)
+++ cfe/cfe/trunk/AST/StmtPrinter.cpp Wed Jul 11 11:44:44 2007
@@ -63,6 +63,8 @@
       }
       IndentLevel -= SubIndent;
     }
+    
+    void PrintRawCompoundStmt(CompoundStmt *S);
 
     void PrintExpr(Expr *E) {
       if (E)
@@ -92,14 +94,20 @@
   Indent() << "<<unknown stmt type>>\n";
 }
 
-void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
-  Indent() << "{\n";
-  
+/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
+/// with no newline after the }.
+void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
+  OS << "{\n";
   for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
        I != E; ++I)
     PrintStmt(*I);
   
-  Indent() << "}\n";
+  Indent() << "}";
+}
+
+void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
+  Indent();
+  PrintRawCompoundStmt(Node);
 }
 
 void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
@@ -127,20 +135,46 @@
 void StmtPrinter::VisitIfStmt(IfStmt *If) {
   Indent() << "if (";
   PrintExpr(If->getCond());
+  OS << ')';
+  
+  if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
+    OS << ' ';
+    PrintRawCompoundStmt(CS);
+    OS << (If->getElse() ? ' ' : '\n');
+  } else {
+    OS << '\n';
+    PrintStmt(If->getThen());
+    if (If->getElse()) Indent();
+  }
 
-  OS << ")\n";
-  PrintStmt(If->getThen());
-  if (If->getElse()) {
-    Indent() << "else\n";
-    PrintStmt(If->getElse());
+  if (Stmt *Else = If->getElse()) {
+    OS << "else";
+    
+    if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
+      OS << ' ';
+      PrintRawCompoundStmt(CS);
+      OS << '\n';
+    } else {
+      OS << '\n';
+      PrintStmt(If->getElse());
+    }
   }
 }
 
 void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
   Indent() << "switch (";
   PrintExpr(Node->getCond());
-  OS << ")\n";
-  PrintStmt(Node->getBody());
+  OS << ")";
+  
+  // Pretty print compoundstmt bodies (very common).
+  if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
+    OS << " ";
+    PrintRawCompoundStmt(CS);
+    OS << "\n";
+  } else {
+    OS << "\n";
+    PrintStmt(Node->getBody());
+  }
 }
 
 void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {





More information about the cfe-commits mailing list