StmtPrinter: allow customizing the end-of-line character

Differential Revision: https://reviews.llvm.org/D51824

llvm-svn: 342311
This commit is contained in:
George Karpenkov 2018-09-15 02:02:31 +00:00
parent 439652363a
commit 64885ae9c0
3 changed files with 58 additions and 54 deletions

View File

@ -424,6 +424,7 @@ public:
void dumpPretty(const ASTContext &Context) const; void dumpPretty(const ASTContext &Context) const;
void printPretty(raw_ostream &OS, PrinterHelper *Helper, void printPretty(raw_ostream &OS, PrinterHelper *Helper,
const PrintingPolicy &Policy, unsigned Indentation = 0, const PrintingPolicy &Policy, unsigned Indentation = 0,
StringRef NewlineSymbol = "\n",
const ASTContext *Context = nullptr) const; const ASTContext *Context = nullptr) const;
/// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only

View File

@ -544,7 +544,7 @@ void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
prettyPrintAttributes(D); prettyPrintAttributes(D);
if (Expr *Init = D->getInitExpr()) { if (Expr *Init = D->getInitExpr()) {
Out << " = "; Out << " = ";
Init->printPretty(Out, nullptr, Policy, Indentation, &Context); Init->printPretty(Out, nullptr, Policy, Indentation, "\n", &Context);
} }
} }

View File

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