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

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


Author: clattner
Date: Wed Jul 11 11:46:38 2007
New Revision: 39648

URL: http://llvm.org/viewvc/llvm-project?rev=39648&view=rev
Log:
Pretty print if/else/elseif chains nicer, like this:

void printutf8(unsigned int X) {
  if (X <= 127)
    printf("%c", (char)X);
  else if (X <= 2047)
    printf("%d %d ", 128 + 64 + (X >> 6), 128 + (X & ((1 << 6) - 1)));
  else if (X <= 65535)
    printf("%c%c%c", 128 + 64 + 32 + (X >> 12), 128 + ((X >> 6) & 63), 128 + (X & 63));
  else
    printf("UNKNOWN %d\n", X);

instead of:

  if (X <= 127)
    printf("%c", (char)X);
  else
    if (X <= 2047)
      printf("%d %d ", 128 + 64 + (X >> 6), 128 + (X & ((1 << 6) - 1)));
    else
      if (X <= 65535)
        printf("%c%c%c", 128 + 64 + 32 + (X >> 12), 128 + ((X >> 6) & 63), 128 + (X & 63));
      else
        printf("UNKNOWN %d\n", X);

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=39648&r1=39647&r2=39648&view=diff

==============================================================================
--- cfe/cfe/trunk/AST/StmtPrinter.cpp (original)
+++ cfe/cfe/trunk/AST/StmtPrinter.cpp Wed Jul 11 11:46:38 2007
@@ -48,7 +48,8 @@
     
     void PrintRawCompoundStmt(CompoundStmt *S);
     void PrintRawDecl(Decl *D);
-
+    void PrintRawIfStmt(IfStmt *If);
+    
     void PrintExpr(Expr *E) {
       if (E)
         E->visit(*this);
@@ -159,8 +160,8 @@
   PrintStmt(Node->getSubStmt(), 0);
 }
 
-void StmtPrinter::VisitIfStmt(IfStmt *If) {
-  Indent() << "if ";
+void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
+  OS << "if ";
   PrintExpr(If->getCond());
   
   if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
@@ -172,7 +173,7 @@
     PrintStmt(If->getThen());
     if (If->getElse()) Indent();
   }
-
+  
   if (Stmt *Else = If->getElse()) {
     OS << "else";
     
@@ -180,6 +181,9 @@
       OS << ' ';
       PrintRawCompoundStmt(CS);
       OS << '\n';
+    } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
+      OS << ' ';
+      PrintRawIfStmt(ElseIf);
     } else {
       OS << '\n';
       PrintStmt(If->getElse());
@@ -187,6 +191,11 @@
   }
 }
 
+void StmtPrinter::VisitIfStmt(IfStmt *If) {
+  Indent();
+  PrintRawIfStmt(If);
+}
+
 void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
   Indent() << "switch (";
   PrintExpr(Node->getCond());





More information about the cfe-commits mailing list