[cfe-commits] r166073 - in /cfe/trunk: lib/AST/StmtPrinter.cpp test/CXX/ast-print.cpp

Eli Friedman eli.friedman at gmail.com
Tue Oct 16 16:45:15 PDT 2012


Author: efriedma
Date: Tue Oct 16 18:45:15 2012
New Revision: 166073

URL: http://llvm.org/viewvc/llvm-project?rev=166073&view=rev
Log:
Fix pretty-printing for variables declared in a condition.  Patch by Grzegorz Jablonski.


Modified:
    cfe/trunk/lib/AST/StmtPrinter.cpp
    cfe/trunk/test/CXX/ast-print.cpp

Modified: cfe/trunk/lib/AST/StmtPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/StmtPrinter.cpp?rev=166073&r1=166072&r2=166073&view=diff
==============================================================================
--- cfe/trunk/lib/AST/StmtPrinter.cpp (original)
+++ cfe/trunk/lib/AST/StmtPrinter.cpp Tue Oct 16 18:45:15 2012
@@ -61,7 +61,7 @@
 
     void PrintRawCompoundStmt(CompoundStmt *S);
     void PrintRawDecl(Decl *D);
-    void PrintRawDeclStmt(DeclStmt *S);
+    void PrintRawDeclStmt(const DeclStmt *S);
     void PrintRawIfStmt(IfStmt *If);
     void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
     void PrintCallArgs(CallExpr *E);
@@ -121,8 +121,8 @@
   D->print(OS, Policy, IndentLevel);
 }
 
-void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
-  DeclStmt::decl_iterator Begin = S->decl_begin(), End = S->decl_end();
+void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
+  DeclStmt::const_decl_iterator Begin = S->decl_begin(), End = S->decl_end();
   SmallVector<Decl*, 2> Decls;
   for ( ; Begin != End; ++Begin)
     Decls.push_back(*Begin);
@@ -187,7 +187,10 @@
 
 void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
   OS << "if (";
-  PrintExpr(If->getCond());
+  if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
+    PrintRawDeclStmt(DS);
+  else
+    PrintExpr(If->getCond());
   OS << ')';
 
   if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
@@ -224,7 +227,10 @@
 
 void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
   Indent() << "switch (";
-  PrintExpr(Node->getCond());
+  if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
+    PrintRawDeclStmt(DS);
+  else
+    PrintExpr(Node->getCond());
   OS << ")";
 
   // Pretty print compoundstmt bodies (very common).
@@ -240,7 +246,10 @@
 
 void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
   Indent() << "while (";
-  PrintExpr(Node->getCond());
+  if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
+    PrintRawDeclStmt(DS);
+  else
+    PrintExpr(Node->getCond());
   OS << ")\n";
   PrintStmt(Node->getBody());
 }

Modified: cfe/trunk/test/CXX/ast-print.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/ast-print.cpp?rev=166073&r1=166072&r2=166073&view=diff
==============================================================================
--- cfe/trunk/test/CXX/ast-print.cpp (original)
+++ cfe/trunk/test/CXX/ast-print.cpp Tue Oct 16 18:45:15 2012
@@ -19,3 +19,14 @@
     (r->method());
 }
 
+// CHECK: if (int a = 1)
+// CHECK:  while (int a = 1)
+// CHECK:  switch (int a = 1)
+
+void f()
+{
+    if (int a = 1) { }
+    while (int a = 1) { }
+    switch (int a = 1) { }
+}
+





More information about the cfe-commits mailing list