george.karpenkov created this revision.
george.karpenkov added reviewers: NoQ, Eugene.Zelenko.

Some environments need a different end-of-line character, namely, dot graphs.
There, "\l" must be used to left-adjust the printed text, as "\n" centers 
everything printed.


https://reviews.llvm.org/D51824

Files:
  clang/include/clang/AST/Stmt.h
  clang/lib/AST/DeclPrinter.cpp
  clang/lib/AST/StmtPrinter.cpp

Index: clang/lib/AST/StmtPrinter.cpp
===================================================================
--- clang/lib/AST/StmtPrinter.cpp
+++ clang/lib/AST/StmtPrinter.cpp
@@ -69,14 +69,16 @@
     unsigned IndentLevel;
     PrinterHelper* Helper;
     PrintingPolicy Policy;
+    std::string NewlineSymbol;
     const ASTContext *Context;
 
   public:
     StmtPrinter(raw_ostream &os, PrinterHelper *helper,
                 const PrintingPolicy &Policy, unsigned Indentation = 0,
+                StringRef NewlineSymbol = "\n",
                 const ASTContext *Context = nullptr)
         : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy),
-          Context(Context) {}
+          NewlineSymbol(NewlineSymbol), Context(Context) {}
 
     void PrintStmt(Stmt *S) {
       PrintStmt(S, Policy.Indentation);
@@ -88,11 +90,11 @@
         // If this is an expr used in a stmt context, indent and newline it.
         Indent();
         Visit(S);
-        OS << ";\n";
+        OS << ";" << NewlineSymbol;
       } else if (S) {
         Visit(S);
       } else {
-        Indent() << "<<<NULL STATEMENT>>>\n";
+        Indent() << "<<<NULL STATEMENT>>>" << NewlineSymbol;
       }
       IndentLevel -= SubIndent;
     }
@@ -128,7 +130,7 @@
     }
 
     void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
-      Indent() << "<<unknown stmt type>>\n";
+      Indent() << "<<unknown stmt type>>" << NewlineSymbol;
     }
 
     void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
@@ -152,7 +154,7 @@
 /// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
 /// with no newline after the }.
 void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
-  OS << "{\n";
+  OS << "{" << NewlineSymbol;
   for (auto *I : Node->body())
     PrintStmt(I);
 
@@ -169,19 +171,19 @@
 }
 
 void StmtPrinter::VisitNullStmt(NullStmt *Node) {
-  Indent() << ";\n";
+  Indent() << ";" << NewlineSymbol;
 }
 
 void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
   Indent();
   PrintRawDeclStmt(Node);
-  OS << ";\n";
+  OS << ";" << NewlineSymbol;
 }
 
 void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
   Indent();
   PrintRawCompoundStmt(Node);
-  OS << "\n";
+  OS << "" << NewlineSymbol;
 }
 
 void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
@@ -191,18 +193,18 @@
     OS << " ... ";
     PrintExpr(Node->getRHS());
   }
-  OS << ":\n";
+  OS << ":" << NewlineSymbol;
 
   PrintStmt(Node->getSubStmt(), 0);
 }
 
 void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
-  Indent(-1) << "default:\n";
+  Indent(-1) << "default:" << NewlineSymbol;
   PrintStmt(Node->getSubStmt(), 0);
 }
 
 void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
-  Indent(-1) << Node->getName() << ":\n";
+  Indent(-1) << Node->getName() << ":" << NewlineSymbol;
   PrintStmt(Node->getSubStmt(), 0);
 }
 
@@ -225,9 +227,9 @@
   if (auto *CS = dyn_cast<CompoundStmt>(If->getThen())) {
     OS << ' ';
     PrintRawCompoundStmt(CS);
-    OS << (If->getElse() ? ' ' : '\n');
+    OS << (If->getElse() ? " " : NewlineSymbol);
   } else {
-    OS << '\n';
+    OS << NewlineSymbol;
     PrintStmt(If->getThen());
     if (If->getElse()) Indent();
   }
@@ -238,12 +240,12 @@
     if (auto *CS = dyn_cast<CompoundStmt>(Else)) {
       OS << ' ';
       PrintRawCompoundStmt(CS);
-      OS << '\n';
+      OS << NewlineSymbol;
     } else if (auto *ElseIf = dyn_cast<IfStmt>(Else)) {
       OS << ' ';
       PrintRawIfStmt(ElseIf);
     } else {
-      OS << '\n';
+      OS << NewlineSymbol;
       PrintStmt(If->getElse());
     }
   }
@@ -266,9 +268,9 @@
   if (auto *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
     OS << " ";
     PrintRawCompoundStmt(CS);
-    OS << "\n";
+    OS << NewlineSymbol;
   } else {
-    OS << "\n";
+    OS << NewlineSymbol;
     PrintStmt(Node->getBody());
   }
 }
@@ -279,7 +281,7 @@
     PrintRawDeclStmt(DS);
   else
     PrintExpr(Node->getCond());
-  OS << ")\n";
+  OS << ")" << NewlineSymbol;
   PrintStmt(Node->getBody());
 }
 
@@ -289,14 +291,14 @@
     PrintRawCompoundStmt(CS);
     OS << " ";
   } else {
-    OS << "\n";
+    OS << NewlineSymbol;
     PrintStmt(Node->getBody());
     Indent();
   }
 
   OS << "while (";
   PrintExpr(Node->getCond());
-  OS << ");\n";
+  OS << ");" << NewlineSymbol;
 }
 
 void StmtPrinter::VisitForStmt(ForStmt *Node) {
@@ -321,9 +323,9 @@
 
   if (auto *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
     PrintRawCompoundStmt(CS);
-    OS << "\n";
+    OS << NewlineSymbol;
   } else {
-    OS << "\n";
+    OS << NewlineSymbol;
     PrintStmt(Node->getBody());
   }
 }
@@ -340,9 +342,9 @@
 
   if (auto *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
     PrintRawCompoundStmt(CS);
-    OS << "\n";
+    OS << NewlineSymbol;
   } else {
-    OS << "\n";
+    OS << NewlineSymbol;
     PrintStmt(Node->getBody());
   }
 }
@@ -354,10 +356,10 @@
   Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
   OS << " : ";
   PrintExpr(Node->getRangeInit());
-  OS << ") {\n";
+  OS << ") {" << NewlineSymbol;
   PrintStmt(Node->getBody());
   Indent() << "}";
-  if (Policy.IncludeNewlines) OS << "\n";
+  if (Policy.IncludeNewlines) OS << NewlineSymbol;
 }
 
 void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
@@ -378,24 +380,24 @@
 
 void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
   Indent() << "goto " << Node->getLabel()->getName() << ";";
-  if (Policy.IncludeNewlines) OS << "\n";
+  if (Policy.IncludeNewlines) OS << NewlineSymbol;
 }
 
 void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
   Indent() << "goto *";
   PrintExpr(Node->getTarget());
   OS << ";";
-  if (Policy.IncludeNewlines) OS << "\n";
+  if (Policy.IncludeNewlines) OS << NewlineSymbol;
 }
 
 void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
   Indent() << "continue;";
-  if (Policy.IncludeNewlines) OS << "\n";
+  if (Policy.IncludeNewlines) OS << NewlineSymbol;
 }
 
 void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
   Indent() << "break;";
-  if (Policy.IncludeNewlines) OS << "\n";
+  if (Policy.IncludeNewlines) OS << NewlineSymbol;
 }
 
 void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
@@ -405,7 +407,7 @@
     PrintExpr(Node->getRetValue());
   }
   OS << ";";
-  if (Policy.IncludeNewlines) OS << "\n";
+  if (Policy.IncludeNewlines) OS << NewlineSymbol;
 }
 
 void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
@@ -470,17 +472,17 @@
   }
 
   OS << ");";
-  if (Policy.IncludeNewlines) OS << "\n";
+  if (Policy.IncludeNewlines) OS << NewlineSymbol;
 }
 
 void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
   // FIXME: Implement MS style inline asm statement printer.
   Indent() << "__asm ";
   if (Node->hasBraces())
-    OS << "{\n";
-  OS << Node->getAsmString() << "\n";
+    OS << "{" << NewlineSymbol;
+  OS << Node->getAsmString() << NewlineSymbol;
   if (Node->hasBraces())
-    Indent() << "}\n";
+    Indent() << "}" << NewlineSymbol;
 }
 
 void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
@@ -491,7 +493,7 @@
   Indent() << "@try";
   if (auto *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
     PrintRawCompoundStmt(TS);
-    OS << "\n";
+    OS << NewlineSymbol;
   }
 
   for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
@@ -504,31 +506,31 @@
     OS << ")";
     if (auto *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
       PrintRawCompoundStmt(CS);
-      OS << "\n";
+      OS << NewlineSymbol;
     }
   }
 
   if (auto *FS = static_cast<ObjCAtFinallyStmt *>(Node->getFinallyStmt())) {
     Indent() << "@finally";
     PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
-    OS << "\n";
+    OS << NewlineSymbol;
   }
 }
 
 void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
 }
 
 void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
-  Indent() << "@catch (...) { /* todo */ } \n";
+  Indent() << "@catch (...) { /* todo */ } " << NewlineSymbol;
 }
 
 void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
   Indent() << "@throw";
   if (Node->getThrowExpr()) {
     OS << " ";
     PrintExpr(Node->getThrowExpr());
   }
-  OS << ";\n";
+  OS << ";" << NewlineSymbol;
 }
 
 void StmtPrinter::VisitObjCAvailabilityCheckExpr(
@@ -541,13 +543,13 @@
   PrintExpr(Node->getSynchExpr());
   OS << ")";
   PrintRawCompoundStmt(Node->getSynchBody());
-  OS << "\n";
+  OS << NewlineSymbol;
 }
 
 void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
   Indent() << "@autoreleasepool";
   PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
-  OS << "\n";
+  OS << NewlineSymbol;
 }
 
 void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
@@ -563,7 +565,7 @@
 void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
   Indent();
   PrintRawCXXCatchStmt(Node);
-  OS << "\n";
+  OS << NewlineSymbol;
 }
 
 void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
@@ -573,7 +575,7 @@
     OS << " ";
     PrintRawCXXCatchStmt(Node->getHandler(i));
   }
-  OS << "\n";
+  OS << NewlineSymbol;
 }
 
 void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
@@ -587,38 +589,38 @@
     assert(F && "Must have a finally block...");
     PrintRawSEHFinallyStmt(F);
   }
-  OS << "\n";
+  OS << NewlineSymbol;
 }
 
 void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
   OS << "__finally ";
   PrintRawCompoundStmt(Node->getBlock());
-  OS << "\n";
+  OS << NewlineSymbol;
 }
 
 void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
   OS << "__except (";
   VisitExpr(Node->getFilterExpr());
-  OS << ")\n";
+  OS << ")" << NewlineSymbol;
   PrintRawCompoundStmt(Node->getBlock());
-  OS << "\n";
+  OS << NewlineSymbol;
 }
 
 void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
   Indent();
   PrintRawSEHExceptHandler(Node);
-  OS << "\n";
+  OS << NewlineSymbol;
 }
 
 void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
   Indent();
   PrintRawSEHFinallyStmt(Node);
-  OS << "\n";
+  OS << NewlineSymbol;
 }
 
 void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) {
   Indent() << "__leave;";
-  if (Policy.IncludeNewlines) OS << "\n";
+  if (Policy.IncludeNewlines) OS << NewlineSymbol;
 }
 
 //===----------------------------------------------------------------------===//
@@ -1067,7 +1069,7 @@
       OS << ' ';
       Printer.Visit(Clause);
     }
-  OS << "\n";
+  OS << NewlineSymbol;
   if (!ForceNoStmt && S->hasAssociatedStmt())
     PrintStmt(S->getInnermostCapturedStmt()->getCapturedStmt());
 }
@@ -2808,8 +2810,9 @@
 
 void Stmt::printPretty(raw_ostream &OS, PrinterHelper *Helper,
                        const PrintingPolicy &Policy, unsigned Indentation,
+                       StringRef NewlineSymbol,
                        const ASTContext *Context) const {
-  StmtPrinter P(OS, Helper, Policy, Indentation, Context);
+  StmtPrinter P(OS, Helper, Policy, Indentation, NewlineSymbol, Context);
   P.Visit(const_cast<Stmt*>(this));
 }
 
Index: clang/lib/AST/DeclPrinter.cpp
===================================================================
--- clang/lib/AST/DeclPrinter.cpp
+++ clang/lib/AST/DeclPrinter.cpp
@@ -544,7 +544,7 @@
   prettyPrintAttributes(D);
   if (Expr *Init = D->getInitExpr()) {
     Out << " = ";
-    Init->printPretty(Out, nullptr, Policy, Indentation, &Context);
+    Init->printPretty(Out, nullptr, Policy, Indentation, "\n", &Context);
   }
 }
 
Index: clang/include/clang/AST/Stmt.h
===================================================================
--- clang/include/clang/AST/Stmt.h
+++ clang/include/clang/AST/Stmt.h
@@ -424,6 +424,7 @@
   void dumpPretty(const ASTContext &Context) const;
   void printPretty(raw_ostream &OS, PrinterHelper *Helper,
                    const PrintingPolicy &Policy, unsigned Indentation = 0,
+                   StringRef NewlineSymbol = "\n",
                    const ASTContext *Context = nullptr) const;
 
   /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz.  Only
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to