1 //===--- StmtPrinter.cpp - Printing implementation for Stmt ASTs ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Stmt::dumpPretty/Stmt::printPretty methods, which
11 // pretty print the AST back out to C code.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Attr.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/DeclObjC.h"
19 #include "clang/AST/DeclTemplate.h"
20 #include "clang/AST/Expr.h"
21 #include "clang/AST/ExprCXX.h"
22 #include "clang/AST/PrettyPrinter.h"
23 #include "clang/AST/StmtVisitor.h"
24 #include "clang/Basic/CharInfo.h"
25 #include "llvm/ADT/SmallString.h"
26 #include "llvm/Support/Format.h"
27 using namespace clang;
28 
29 //===----------------------------------------------------------------------===//
30 // StmtPrinter Visitor
31 //===----------------------------------------------------------------------===//
32 
33 namespace  {
34   class StmtPrinter : public StmtVisitor<StmtPrinter> {
35     raw_ostream &OS;
36     unsigned IndentLevel;
37     clang::PrinterHelper* Helper;
38     PrintingPolicy Policy;
39 
40   public:
StmtPrinter(raw_ostream & os,PrinterHelper * helper,const PrintingPolicy & Policy,unsigned Indentation=0)41     StmtPrinter(raw_ostream &os, PrinterHelper* helper,
42                 const PrintingPolicy &Policy,
43                 unsigned Indentation = 0)
44       : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy) {}
45 
PrintStmt(Stmt * S)46     void PrintStmt(Stmt *S) {
47       PrintStmt(S, Policy.Indentation);
48     }
49 
PrintStmt(Stmt * S,int SubIndent)50     void PrintStmt(Stmt *S, int SubIndent) {
51       IndentLevel += SubIndent;
52       if (S && isa<Expr>(S)) {
53         // If this is an expr used in a stmt context, indent and newline it.
54         Indent();
55         Visit(S);
56         OS << ";\n";
57       } else if (S) {
58         Visit(S);
59       } else {
60         Indent() << "<<<NULL STATEMENT>>>\n";
61       }
62       IndentLevel -= SubIndent;
63     }
64 
65     void PrintRawCompoundStmt(CompoundStmt *S);
66     void PrintRawDecl(Decl *D);
67     void PrintRawDeclStmt(const DeclStmt *S);
68     void PrintRawIfStmt(IfStmt *If);
69     void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
70     void PrintCallArgs(CallExpr *E);
71     void PrintRawSEHExceptHandler(SEHExceptStmt *S);
72     void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
73     void PrintOMPExecutableDirective(OMPExecutableDirective *S);
74 
PrintExpr(Expr * E)75     void PrintExpr(Expr *E) {
76       if (E)
77         Visit(E);
78       else
79         OS << "<null expr>";
80     }
81 
Indent(int Delta=0)82     raw_ostream &Indent(int Delta = 0) {
83       for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
84         OS << "  ";
85       return OS;
86     }
87 
Visit(Stmt * S)88     void Visit(Stmt* S) {
89       if (Helper && Helper->handledStmt(S,OS))
90           return;
91       else StmtVisitor<StmtPrinter>::Visit(S);
92     }
93 
VisitStmt(Stmt * Node)94     void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
95       Indent() << "<<unknown stmt type>>\n";
96     }
VisitExpr(Expr * Node)97     void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
98       OS << "<<unknown expr type>>";
99     }
100     void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
101 
102 #define ABSTRACT_STMT(CLASS)
103 #define STMT(CLASS, PARENT) \
104     void Visit##CLASS(CLASS *Node);
105 #include "clang/AST/StmtNodes.inc"
106   };
107 }
108 
109 //===----------------------------------------------------------------------===//
110 //  Stmt printing methods.
111 //===----------------------------------------------------------------------===//
112 
113 /// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
114 /// with no newline after the }.
PrintRawCompoundStmt(CompoundStmt * Node)115 void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
116   OS << "{\n";
117   for (auto *I : Node->body())
118     PrintStmt(I);
119 
120   Indent() << "}";
121 }
122 
PrintRawDecl(Decl * D)123 void StmtPrinter::PrintRawDecl(Decl *D) {
124   D->print(OS, Policy, IndentLevel);
125 }
126 
PrintRawDeclStmt(const DeclStmt * S)127 void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
128   SmallVector<Decl*, 2> Decls(S->decls());
129   Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
130 }
131 
VisitNullStmt(NullStmt * Node)132 void StmtPrinter::VisitNullStmt(NullStmt *Node) {
133   Indent() << ";\n";
134 }
135 
VisitDeclStmt(DeclStmt * Node)136 void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
137   Indent();
138   PrintRawDeclStmt(Node);
139   OS << ";\n";
140 }
141 
VisitCompoundStmt(CompoundStmt * Node)142 void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
143   Indent();
144   PrintRawCompoundStmt(Node);
145   OS << "\n";
146 }
147 
VisitCaseStmt(CaseStmt * Node)148 void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
149   Indent(-1) << "case ";
150   PrintExpr(Node->getLHS());
151   if (Node->getRHS()) {
152     OS << " ... ";
153     PrintExpr(Node->getRHS());
154   }
155   OS << ":\n";
156 
157   PrintStmt(Node->getSubStmt(), 0);
158 }
159 
VisitDefaultStmt(DefaultStmt * Node)160 void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
161   Indent(-1) << "default:\n";
162   PrintStmt(Node->getSubStmt(), 0);
163 }
164 
VisitLabelStmt(LabelStmt * Node)165 void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
166   Indent(-1) << Node->getName() << ":\n";
167   PrintStmt(Node->getSubStmt(), 0);
168 }
169 
VisitAttributedStmt(AttributedStmt * Node)170 void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
171   for (const auto *Attr : Node->getAttrs()) {
172     Attr->printPretty(OS, Policy);
173   }
174 
175   PrintStmt(Node->getSubStmt(), 0);
176 }
177 
PrintRawIfStmt(IfStmt * If)178 void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
179   OS << "if (";
180   if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
181     PrintRawDeclStmt(DS);
182   else
183     PrintExpr(If->getCond());
184   OS << ')';
185 
186   if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
187     OS << ' ';
188     PrintRawCompoundStmt(CS);
189     OS << (If->getElse() ? ' ' : '\n');
190   } else {
191     OS << '\n';
192     PrintStmt(If->getThen());
193     if (If->getElse()) Indent();
194   }
195 
196   if (Stmt *Else = If->getElse()) {
197     OS << "else";
198 
199     if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
200       OS << ' ';
201       PrintRawCompoundStmt(CS);
202       OS << '\n';
203     } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
204       OS << ' ';
205       PrintRawIfStmt(ElseIf);
206     } else {
207       OS << '\n';
208       PrintStmt(If->getElse());
209     }
210   }
211 }
212 
VisitIfStmt(IfStmt * If)213 void StmtPrinter::VisitIfStmt(IfStmt *If) {
214   Indent();
215   PrintRawIfStmt(If);
216 }
217 
VisitSwitchStmt(SwitchStmt * Node)218 void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
219   Indent() << "switch (";
220   if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
221     PrintRawDeclStmt(DS);
222   else
223     PrintExpr(Node->getCond());
224   OS << ")";
225 
226   // Pretty print compoundstmt bodies (very common).
227   if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
228     OS << " ";
229     PrintRawCompoundStmt(CS);
230     OS << "\n";
231   } else {
232     OS << "\n";
233     PrintStmt(Node->getBody());
234   }
235 }
236 
VisitWhileStmt(WhileStmt * Node)237 void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
238   Indent() << "while (";
239   if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
240     PrintRawDeclStmt(DS);
241   else
242     PrintExpr(Node->getCond());
243   OS << ")\n";
244   PrintStmt(Node->getBody());
245 }
246 
VisitDoStmt(DoStmt * Node)247 void StmtPrinter::VisitDoStmt(DoStmt *Node) {
248   Indent() << "do ";
249   if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
250     PrintRawCompoundStmt(CS);
251     OS << " ";
252   } else {
253     OS << "\n";
254     PrintStmt(Node->getBody());
255     Indent();
256   }
257 
258   OS << "while (";
259   PrintExpr(Node->getCond());
260   OS << ");\n";
261 }
262 
VisitForStmt(ForStmt * Node)263 void StmtPrinter::VisitForStmt(ForStmt *Node) {
264   Indent() << "for (";
265   if (Node->getInit()) {
266     if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
267       PrintRawDeclStmt(DS);
268     else
269       PrintExpr(cast<Expr>(Node->getInit()));
270   }
271   OS << ";";
272   if (Node->getCond()) {
273     OS << " ";
274     PrintExpr(Node->getCond());
275   }
276   OS << ";";
277   if (Node->getInc()) {
278     OS << " ";
279     PrintExpr(Node->getInc());
280   }
281   OS << ") ";
282 
283   if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
284     PrintRawCompoundStmt(CS);
285     OS << "\n";
286   } else {
287     OS << "\n";
288     PrintStmt(Node->getBody());
289   }
290 }
291 
VisitObjCForCollectionStmt(ObjCForCollectionStmt * Node)292 void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
293   Indent() << "for (";
294   if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
295     PrintRawDeclStmt(DS);
296   else
297     PrintExpr(cast<Expr>(Node->getElement()));
298   OS << " in ";
299   PrintExpr(Node->getCollection());
300   OS << ") ";
301 
302   if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
303     PrintRawCompoundStmt(CS);
304     OS << "\n";
305   } else {
306     OS << "\n";
307     PrintStmt(Node->getBody());
308   }
309 }
310 
VisitCXXForRangeStmt(CXXForRangeStmt * Node)311 void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
312   Indent() << "for (";
313   PrintingPolicy SubPolicy(Policy);
314   SubPolicy.SuppressInitializers = true;
315   Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
316   OS << " : ";
317   PrintExpr(Node->getRangeInit());
318   OS << ") {\n";
319   PrintStmt(Node->getBody());
320   Indent() << "}";
321   if (Policy.IncludeNewlines) OS << "\n";
322 }
323 
VisitMSDependentExistsStmt(MSDependentExistsStmt * Node)324 void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
325   Indent();
326   if (Node->isIfExists())
327     OS << "__if_exists (";
328   else
329     OS << "__if_not_exists (";
330 
331   if (NestedNameSpecifier *Qualifier
332         = Node->getQualifierLoc().getNestedNameSpecifier())
333     Qualifier->print(OS, Policy);
334 
335   OS << Node->getNameInfo() << ") ";
336 
337   PrintRawCompoundStmt(Node->getSubStmt());
338 }
339 
VisitGotoStmt(GotoStmt * Node)340 void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
341   Indent() << "goto " << Node->getLabel()->getName() << ";";
342   if (Policy.IncludeNewlines) OS << "\n";
343 }
344 
VisitIndirectGotoStmt(IndirectGotoStmt * Node)345 void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
346   Indent() << "goto *";
347   PrintExpr(Node->getTarget());
348   OS << ";";
349   if (Policy.IncludeNewlines) OS << "\n";
350 }
351 
VisitContinueStmt(ContinueStmt * Node)352 void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
353   Indent() << "continue;";
354   if (Policy.IncludeNewlines) OS << "\n";
355 }
356 
VisitBreakStmt(BreakStmt * Node)357 void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
358   Indent() << "break;";
359   if (Policy.IncludeNewlines) OS << "\n";
360 }
361 
362 
VisitReturnStmt(ReturnStmt * Node)363 void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
364   Indent() << "return";
365   if (Node->getRetValue()) {
366     OS << " ";
367     PrintExpr(Node->getRetValue());
368   }
369   OS << ";";
370   if (Policy.IncludeNewlines) OS << "\n";
371 }
372 
373 
VisitGCCAsmStmt(GCCAsmStmt * Node)374 void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
375   Indent() << "asm ";
376 
377   if (Node->isVolatile())
378     OS << "volatile ";
379 
380   OS << "(";
381   VisitStringLiteral(Node->getAsmString());
382 
383   // Outputs
384   if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
385       Node->getNumClobbers() != 0)
386     OS << " : ";
387 
388   for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
389     if (i != 0)
390       OS << ", ";
391 
392     if (!Node->getOutputName(i).empty()) {
393       OS << '[';
394       OS << Node->getOutputName(i);
395       OS << "] ";
396     }
397 
398     VisitStringLiteral(Node->getOutputConstraintLiteral(i));
399     OS << " ";
400     Visit(Node->getOutputExpr(i));
401   }
402 
403   // Inputs
404   if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
405     OS << " : ";
406 
407   for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
408     if (i != 0)
409       OS << ", ";
410 
411     if (!Node->getInputName(i).empty()) {
412       OS << '[';
413       OS << Node->getInputName(i);
414       OS << "] ";
415     }
416 
417     VisitStringLiteral(Node->getInputConstraintLiteral(i));
418     OS << " ";
419     Visit(Node->getInputExpr(i));
420   }
421 
422   // Clobbers
423   if (Node->getNumClobbers() != 0)
424     OS << " : ";
425 
426   for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
427     if (i != 0)
428       OS << ", ";
429 
430     VisitStringLiteral(Node->getClobberStringLiteral(i));
431   }
432 
433   OS << ");";
434   if (Policy.IncludeNewlines) OS << "\n";
435 }
436 
VisitMSAsmStmt(MSAsmStmt * Node)437 void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
438   // FIXME: Implement MS style inline asm statement printer.
439   Indent() << "__asm ";
440   if (Node->hasBraces())
441     OS << "{\n";
442   OS << Node->getAsmString() << "\n";
443   if (Node->hasBraces())
444     Indent() << "}\n";
445 }
446 
VisitCapturedStmt(CapturedStmt * Node)447 void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
448   PrintStmt(Node->getCapturedDecl()->getBody());
449 }
450 
VisitObjCAtTryStmt(ObjCAtTryStmt * Node)451 void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
452   Indent() << "@try";
453   if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
454     PrintRawCompoundStmt(TS);
455     OS << "\n";
456   }
457 
458   for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
459     ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
460     Indent() << "@catch(";
461     if (catchStmt->getCatchParamDecl()) {
462       if (Decl *DS = catchStmt->getCatchParamDecl())
463         PrintRawDecl(DS);
464     }
465     OS << ")";
466     if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
467       PrintRawCompoundStmt(CS);
468       OS << "\n";
469     }
470   }
471 
472   if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
473         Node->getFinallyStmt())) {
474     Indent() << "@finally";
475     PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
476     OS << "\n";
477   }
478 }
479 
VisitObjCAtFinallyStmt(ObjCAtFinallyStmt * Node)480 void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
481 }
482 
VisitObjCAtCatchStmt(ObjCAtCatchStmt * Node)483 void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
484   Indent() << "@catch (...) { /* todo */ } \n";
485 }
486 
VisitObjCAtThrowStmt(ObjCAtThrowStmt * Node)487 void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
488   Indent() << "@throw";
489   if (Node->getThrowExpr()) {
490     OS << " ";
491     PrintExpr(Node->getThrowExpr());
492   }
493   OS << ";\n";
494 }
495 
VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt * Node)496 void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
497   Indent() << "@synchronized (";
498   PrintExpr(Node->getSynchExpr());
499   OS << ")";
500   PrintRawCompoundStmt(Node->getSynchBody());
501   OS << "\n";
502 }
503 
VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt * Node)504 void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
505   Indent() << "@autoreleasepool";
506   PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
507   OS << "\n";
508 }
509 
PrintRawCXXCatchStmt(CXXCatchStmt * Node)510 void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
511   OS << "catch (";
512   if (Decl *ExDecl = Node->getExceptionDecl())
513     PrintRawDecl(ExDecl);
514   else
515     OS << "...";
516   OS << ") ";
517   PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
518 }
519 
VisitCXXCatchStmt(CXXCatchStmt * Node)520 void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
521   Indent();
522   PrintRawCXXCatchStmt(Node);
523   OS << "\n";
524 }
525 
VisitCXXTryStmt(CXXTryStmt * Node)526 void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
527   Indent() << "try ";
528   PrintRawCompoundStmt(Node->getTryBlock());
529   for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
530     OS << " ";
531     PrintRawCXXCatchStmt(Node->getHandler(i));
532   }
533   OS << "\n";
534 }
535 
VisitSEHTryStmt(SEHTryStmt * Node)536 void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
537   Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
538   PrintRawCompoundStmt(Node->getTryBlock());
539   SEHExceptStmt *E = Node->getExceptHandler();
540   SEHFinallyStmt *F = Node->getFinallyHandler();
541   if(E)
542     PrintRawSEHExceptHandler(E);
543   else {
544     assert(F && "Must have a finally block...");
545     PrintRawSEHFinallyStmt(F);
546   }
547   OS << "\n";
548 }
549 
PrintRawSEHFinallyStmt(SEHFinallyStmt * Node)550 void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
551   OS << "__finally ";
552   PrintRawCompoundStmt(Node->getBlock());
553   OS << "\n";
554 }
555 
PrintRawSEHExceptHandler(SEHExceptStmt * Node)556 void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
557   OS << "__except (";
558   VisitExpr(Node->getFilterExpr());
559   OS << ")\n";
560   PrintRawCompoundStmt(Node->getBlock());
561   OS << "\n";
562 }
563 
VisitSEHExceptStmt(SEHExceptStmt * Node)564 void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
565   Indent();
566   PrintRawSEHExceptHandler(Node);
567   OS << "\n";
568 }
569 
VisitSEHFinallyStmt(SEHFinallyStmt * Node)570 void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
571   Indent();
572   PrintRawSEHFinallyStmt(Node);
573   OS << "\n";
574 }
575 
VisitSEHLeaveStmt(SEHLeaveStmt * Node)576 void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) {
577   Indent() << "__leave;";
578   if (Policy.IncludeNewlines) OS << "\n";
579 }
580 
581 //===----------------------------------------------------------------------===//
582 //  OpenMP clauses printing methods
583 //===----------------------------------------------------------------------===//
584 
585 namespace {
586 class OMPClausePrinter : public OMPClauseVisitor<OMPClausePrinter> {
587   raw_ostream &OS;
588   const PrintingPolicy &Policy;
589   /// \brief Process clauses with list of variables.
590   template <typename T>
591   void VisitOMPClauseList(T *Node, char StartSym);
592 public:
OMPClausePrinter(raw_ostream & OS,const PrintingPolicy & Policy)593   OMPClausePrinter(raw_ostream &OS, const PrintingPolicy &Policy)
594     : OS(OS), Policy(Policy) { }
595 #define OPENMP_CLAUSE(Name, Class)                              \
596   void Visit##Class(Class *S);
597 #include "clang/Basic/OpenMPKinds.def"
598 };
599 
VisitOMPIfClause(OMPIfClause * Node)600 void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) {
601   OS << "if(";
602   Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
603   OS << ")";
604 }
605 
VisitOMPFinalClause(OMPFinalClause * Node)606 void OMPClausePrinter::VisitOMPFinalClause(OMPFinalClause *Node) {
607   OS << "final(";
608   Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
609   OS << ")";
610 }
611 
VisitOMPNumThreadsClause(OMPNumThreadsClause * Node)612 void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) {
613   OS << "num_threads(";
614   Node->getNumThreads()->printPretty(OS, nullptr, Policy, 0);
615   OS << ")";
616 }
617 
VisitOMPSafelenClause(OMPSafelenClause * Node)618 void OMPClausePrinter::VisitOMPSafelenClause(OMPSafelenClause *Node) {
619   OS << "safelen(";
620   Node->getSafelen()->printPretty(OS, nullptr, Policy, 0);
621   OS << ")";
622 }
623 
VisitOMPCollapseClause(OMPCollapseClause * Node)624 void OMPClausePrinter::VisitOMPCollapseClause(OMPCollapseClause *Node) {
625   OS << "collapse(";
626   Node->getNumForLoops()->printPretty(OS, nullptr, Policy, 0);
627   OS << ")";
628 }
629 
VisitOMPDefaultClause(OMPDefaultClause * Node)630 void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
631   OS << "default("
632      << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind())
633      << ")";
634 }
635 
VisitOMPProcBindClause(OMPProcBindClause * Node)636 void OMPClausePrinter::VisitOMPProcBindClause(OMPProcBindClause *Node) {
637   OS << "proc_bind("
638      << getOpenMPSimpleClauseTypeName(OMPC_proc_bind, Node->getProcBindKind())
639      << ")";
640 }
641 
VisitOMPScheduleClause(OMPScheduleClause * Node)642 void OMPClausePrinter::VisitOMPScheduleClause(OMPScheduleClause *Node) {
643   OS << "schedule("
644      << getOpenMPSimpleClauseTypeName(OMPC_schedule, Node->getScheduleKind());
645   if (Node->getChunkSize()) {
646     OS << ", ";
647     Node->getChunkSize()->printPretty(OS, nullptr, Policy);
648   }
649   OS << ")";
650 }
651 
VisitOMPOrderedClause(OMPOrderedClause *)652 void OMPClausePrinter::VisitOMPOrderedClause(OMPOrderedClause *) {
653   OS << "ordered";
654 }
655 
VisitOMPNowaitClause(OMPNowaitClause *)656 void OMPClausePrinter::VisitOMPNowaitClause(OMPNowaitClause *) {
657   OS << "nowait";
658 }
659 
VisitOMPUntiedClause(OMPUntiedClause *)660 void OMPClausePrinter::VisitOMPUntiedClause(OMPUntiedClause *) {
661   OS << "untied";
662 }
663 
VisitOMPMergeableClause(OMPMergeableClause *)664 void OMPClausePrinter::VisitOMPMergeableClause(OMPMergeableClause *) {
665   OS << "mergeable";
666 }
667 
VisitOMPReadClause(OMPReadClause *)668 void OMPClausePrinter::VisitOMPReadClause(OMPReadClause *) { OS << "read"; }
669 
VisitOMPWriteClause(OMPWriteClause *)670 void OMPClausePrinter::VisitOMPWriteClause(OMPWriteClause *) { OS << "write"; }
671 
VisitOMPUpdateClause(OMPUpdateClause *)672 void OMPClausePrinter::VisitOMPUpdateClause(OMPUpdateClause *) {
673   OS << "update";
674 }
675 
VisitOMPCaptureClause(OMPCaptureClause *)676 void OMPClausePrinter::VisitOMPCaptureClause(OMPCaptureClause *) {
677   OS << "capture";
678 }
679 
VisitOMPSeqCstClause(OMPSeqCstClause *)680 void OMPClausePrinter::VisitOMPSeqCstClause(OMPSeqCstClause *) {
681   OS << "seq_cst";
682 }
683 
684 template<typename T>
VisitOMPClauseList(T * Node,char StartSym)685 void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
686   for (typename T::varlist_iterator I = Node->varlist_begin(),
687                                     E = Node->varlist_end();
688          I != E; ++I) {
689     assert(*I && "Expected non-null Stmt");
690     if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(*I)) {
691       OS << (I == Node->varlist_begin() ? StartSym : ',');
692       cast<NamedDecl>(DRE->getDecl())->printQualifiedName(OS);
693     } else {
694       OS << (I == Node->varlist_begin() ? StartSym : ',');
695       (*I)->printPretty(OS, nullptr, Policy, 0);
696     }
697   }
698 }
699 
VisitOMPPrivateClause(OMPPrivateClause * Node)700 void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
701   if (!Node->varlist_empty()) {
702     OS << "private";
703     VisitOMPClauseList(Node, '(');
704     OS << ")";
705   }
706 }
707 
VisitOMPFirstprivateClause(OMPFirstprivateClause * Node)708 void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
709   if (!Node->varlist_empty()) {
710     OS << "firstprivate";
711     VisitOMPClauseList(Node, '(');
712     OS << ")";
713   }
714 }
715 
VisitOMPLastprivateClause(OMPLastprivateClause * Node)716 void OMPClausePrinter::VisitOMPLastprivateClause(OMPLastprivateClause *Node) {
717   if (!Node->varlist_empty()) {
718     OS << "lastprivate";
719     VisitOMPClauseList(Node, '(');
720     OS << ")";
721   }
722 }
723 
VisitOMPSharedClause(OMPSharedClause * Node)724 void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
725   if (!Node->varlist_empty()) {
726     OS << "shared";
727     VisitOMPClauseList(Node, '(');
728     OS << ")";
729   }
730 }
731 
VisitOMPReductionClause(OMPReductionClause * Node)732 void OMPClausePrinter::VisitOMPReductionClause(OMPReductionClause *Node) {
733   if (!Node->varlist_empty()) {
734     OS << "reduction(";
735     NestedNameSpecifier *QualifierLoc =
736         Node->getQualifierLoc().getNestedNameSpecifier();
737     OverloadedOperatorKind OOK =
738         Node->getNameInfo().getName().getCXXOverloadedOperator();
739     if (QualifierLoc == nullptr && OOK != OO_None) {
740       // Print reduction identifier in C format
741       OS << getOperatorSpelling(OOK);
742     } else {
743       // Use C++ format
744       if (QualifierLoc != nullptr)
745         QualifierLoc->print(OS, Policy);
746       OS << Node->getNameInfo();
747     }
748     OS << ":";
749     VisitOMPClauseList(Node, ' ');
750     OS << ")";
751   }
752 }
753 
VisitOMPLinearClause(OMPLinearClause * Node)754 void OMPClausePrinter::VisitOMPLinearClause(OMPLinearClause *Node) {
755   if (!Node->varlist_empty()) {
756     OS << "linear";
757     VisitOMPClauseList(Node, '(');
758     if (Node->getStep() != nullptr) {
759       OS << ": ";
760       Node->getStep()->printPretty(OS, nullptr, Policy, 0);
761     }
762     OS << ")";
763   }
764 }
765 
VisitOMPAlignedClause(OMPAlignedClause * Node)766 void OMPClausePrinter::VisitOMPAlignedClause(OMPAlignedClause *Node) {
767   if (!Node->varlist_empty()) {
768     OS << "aligned";
769     VisitOMPClauseList(Node, '(');
770     if (Node->getAlignment() != nullptr) {
771       OS << ": ";
772       Node->getAlignment()->printPretty(OS, nullptr, Policy, 0);
773     }
774     OS << ")";
775   }
776 }
777 
VisitOMPCopyinClause(OMPCopyinClause * Node)778 void OMPClausePrinter::VisitOMPCopyinClause(OMPCopyinClause *Node) {
779   if (!Node->varlist_empty()) {
780     OS << "copyin";
781     VisitOMPClauseList(Node, '(');
782     OS << ")";
783   }
784 }
785 
VisitOMPCopyprivateClause(OMPCopyprivateClause * Node)786 void OMPClausePrinter::VisitOMPCopyprivateClause(OMPCopyprivateClause *Node) {
787   if (!Node->varlist_empty()) {
788     OS << "copyprivate";
789     VisitOMPClauseList(Node, '(');
790     OS << ")";
791   }
792 }
793 
VisitOMPFlushClause(OMPFlushClause * Node)794 void OMPClausePrinter::VisitOMPFlushClause(OMPFlushClause *Node) {
795   if (!Node->varlist_empty()) {
796     VisitOMPClauseList(Node, '(');
797     OS << ")";
798   }
799 }
800 }
801 
802 //===----------------------------------------------------------------------===//
803 //  OpenMP directives printing methods
804 //===----------------------------------------------------------------------===//
805 
PrintOMPExecutableDirective(OMPExecutableDirective * S)806 void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S) {
807   OMPClausePrinter Printer(OS, Policy);
808   ArrayRef<OMPClause *> Clauses = S->clauses();
809   for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
810        I != E; ++I)
811     if (*I && !(*I)->isImplicit()) {
812       Printer.Visit(*I);
813       OS << ' ';
814     }
815   OS << "\n";
816   if (S->hasAssociatedStmt() && S->getAssociatedStmt()) {
817     assert(isa<CapturedStmt>(S->getAssociatedStmt()) &&
818            "Expected captured statement!");
819     Stmt *CS = cast<CapturedStmt>(S->getAssociatedStmt())->getCapturedStmt();
820     PrintStmt(CS);
821   }
822 }
823 
VisitOMPParallelDirective(OMPParallelDirective * Node)824 void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
825   Indent() << "#pragma omp parallel ";
826   PrintOMPExecutableDirective(Node);
827 }
828 
VisitOMPSimdDirective(OMPSimdDirective * Node)829 void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
830   Indent() << "#pragma omp simd ";
831   PrintOMPExecutableDirective(Node);
832 }
833 
VisitOMPForDirective(OMPForDirective * Node)834 void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
835   Indent() << "#pragma omp for ";
836   PrintOMPExecutableDirective(Node);
837 }
838 
VisitOMPForSimdDirective(OMPForSimdDirective * Node)839 void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) {
840   Indent() << "#pragma omp for simd ";
841   PrintOMPExecutableDirective(Node);
842 }
843 
VisitOMPSectionsDirective(OMPSectionsDirective * Node)844 void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
845   Indent() << "#pragma omp sections ";
846   PrintOMPExecutableDirective(Node);
847 }
848 
VisitOMPSectionDirective(OMPSectionDirective * Node)849 void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
850   Indent() << "#pragma omp section";
851   PrintOMPExecutableDirective(Node);
852 }
853 
VisitOMPSingleDirective(OMPSingleDirective * Node)854 void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
855   Indent() << "#pragma omp single ";
856   PrintOMPExecutableDirective(Node);
857 }
858 
VisitOMPMasterDirective(OMPMasterDirective * Node)859 void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
860   Indent() << "#pragma omp master";
861   PrintOMPExecutableDirective(Node);
862 }
863 
VisitOMPCriticalDirective(OMPCriticalDirective * Node)864 void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
865   Indent() << "#pragma omp critical";
866   if (Node->getDirectiveName().getName()) {
867     OS << " (";
868     Node->getDirectiveName().printName(OS);
869     OS << ")";
870   }
871   PrintOMPExecutableDirective(Node);
872 }
873 
VisitOMPParallelForDirective(OMPParallelForDirective * Node)874 void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
875   Indent() << "#pragma omp parallel for ";
876   PrintOMPExecutableDirective(Node);
877 }
878 
VisitOMPParallelForSimdDirective(OMPParallelForSimdDirective * Node)879 void StmtPrinter::VisitOMPParallelForSimdDirective(
880     OMPParallelForSimdDirective *Node) {
881   Indent() << "#pragma omp parallel for simd ";
882   PrintOMPExecutableDirective(Node);
883 }
884 
VisitOMPParallelSectionsDirective(OMPParallelSectionsDirective * Node)885 void StmtPrinter::VisitOMPParallelSectionsDirective(
886     OMPParallelSectionsDirective *Node) {
887   Indent() << "#pragma omp parallel sections ";
888   PrintOMPExecutableDirective(Node);
889 }
890 
VisitOMPTaskDirective(OMPTaskDirective * Node)891 void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
892   Indent() << "#pragma omp task ";
893   PrintOMPExecutableDirective(Node);
894 }
895 
VisitOMPTaskyieldDirective(OMPTaskyieldDirective * Node)896 void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
897   Indent() << "#pragma omp taskyield";
898   PrintOMPExecutableDirective(Node);
899 }
900 
VisitOMPBarrierDirective(OMPBarrierDirective * Node)901 void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
902   Indent() << "#pragma omp barrier";
903   PrintOMPExecutableDirective(Node);
904 }
905 
VisitOMPTaskwaitDirective(OMPTaskwaitDirective * Node)906 void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
907   Indent() << "#pragma omp taskwait";
908   PrintOMPExecutableDirective(Node);
909 }
910 
VisitOMPFlushDirective(OMPFlushDirective * Node)911 void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
912   Indent() << "#pragma omp flush ";
913   PrintOMPExecutableDirective(Node);
914 }
915 
VisitOMPOrderedDirective(OMPOrderedDirective * Node)916 void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
917   Indent() << "#pragma omp ordered";
918   PrintOMPExecutableDirective(Node);
919 }
920 
VisitOMPAtomicDirective(OMPAtomicDirective * Node)921 void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
922   Indent() << "#pragma omp atomic ";
923   PrintOMPExecutableDirective(Node);
924 }
925 
VisitOMPTargetDirective(OMPTargetDirective * Node)926 void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
927   Indent() << "#pragma omp target ";
928   PrintOMPExecutableDirective(Node);
929 }
930 
VisitOMPTeamsDirective(OMPTeamsDirective * Node)931 void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) {
932   Indent() << "#pragma omp teams ";
933   PrintOMPExecutableDirective(Node);
934 }
935 
936 //===----------------------------------------------------------------------===//
937 //  Expr printing methods.
938 //===----------------------------------------------------------------------===//
939 
VisitDeclRefExpr(DeclRefExpr * Node)940 void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
941   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
942     Qualifier->print(OS, Policy);
943   if (Node->hasTemplateKeyword())
944     OS << "template ";
945   OS << Node->getNameInfo();
946   if (Node->hasExplicitTemplateArgs())
947     TemplateSpecializationType::PrintTemplateArgumentList(
948         OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
949 }
950 
VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr * Node)951 void StmtPrinter::VisitDependentScopeDeclRefExpr(
952                                            DependentScopeDeclRefExpr *Node) {
953   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
954     Qualifier->print(OS, Policy);
955   if (Node->hasTemplateKeyword())
956     OS << "template ";
957   OS << Node->getNameInfo();
958   if (Node->hasExplicitTemplateArgs())
959     TemplateSpecializationType::PrintTemplateArgumentList(
960         OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
961 }
962 
VisitUnresolvedLookupExpr(UnresolvedLookupExpr * Node)963 void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
964   if (Node->getQualifier())
965     Node->getQualifier()->print(OS, Policy);
966   if (Node->hasTemplateKeyword())
967     OS << "template ";
968   OS << Node->getNameInfo();
969   if (Node->hasExplicitTemplateArgs())
970     TemplateSpecializationType::PrintTemplateArgumentList(
971         OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
972 }
973 
VisitObjCIvarRefExpr(ObjCIvarRefExpr * Node)974 void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
975   if (Node->getBase()) {
976     PrintExpr(Node->getBase());
977     OS << (Node->isArrow() ? "->" : ".");
978   }
979   OS << *Node->getDecl();
980 }
981 
VisitObjCPropertyRefExpr(ObjCPropertyRefExpr * Node)982 void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
983   if (Node->isSuperReceiver())
984     OS << "super.";
985   else if (Node->isObjectReceiver() && Node->getBase()) {
986     PrintExpr(Node->getBase());
987     OS << ".";
988   } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
989     OS << Node->getClassReceiver()->getName() << ".";
990   }
991 
992   if (Node->isImplicitProperty())
993     Node->getImplicitPropertyGetter()->getSelector().print(OS);
994   else
995     OS << Node->getExplicitProperty()->getName();
996 }
997 
VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr * Node)998 void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
999 
1000   PrintExpr(Node->getBaseExpr());
1001   OS << "[";
1002   PrintExpr(Node->getKeyExpr());
1003   OS << "]";
1004 }
1005 
VisitPredefinedExpr(PredefinedExpr * Node)1006 void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
1007   OS << PredefinedExpr::getIdentTypeName(Node->getIdentType());
1008 }
1009 
VisitCharacterLiteral(CharacterLiteral * Node)1010 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
1011   unsigned value = Node->getValue();
1012 
1013   switch (Node->getKind()) {
1014   case CharacterLiteral::Ascii: break; // no prefix.
1015   case CharacterLiteral::Wide:  OS << 'L'; break;
1016   case CharacterLiteral::UTF16: OS << 'u'; break;
1017   case CharacterLiteral::UTF32: OS << 'U'; break;
1018   }
1019 
1020   switch (value) {
1021   case '\\':
1022     OS << "'\\\\'";
1023     break;
1024   case '\'':
1025     OS << "'\\''";
1026     break;
1027   case '\a':
1028     // TODO: K&R: the meaning of '\\a' is different in traditional C
1029     OS << "'\\a'";
1030     break;
1031   case '\b':
1032     OS << "'\\b'";
1033     break;
1034   // Nonstandard escape sequence.
1035   /*case '\e':
1036     OS << "'\\e'";
1037     break;*/
1038   case '\f':
1039     OS << "'\\f'";
1040     break;
1041   case '\n':
1042     OS << "'\\n'";
1043     break;
1044   case '\r':
1045     OS << "'\\r'";
1046     break;
1047   case '\t':
1048     OS << "'\\t'";
1049     break;
1050   case '\v':
1051     OS << "'\\v'";
1052     break;
1053   default:
1054     if (value < 256 && isPrintable((unsigned char)value))
1055       OS << "'" << (char)value << "'";
1056     else if (value < 256)
1057       OS << "'\\x" << llvm::format("%02x", value) << "'";
1058     else if (value <= 0xFFFF)
1059       OS << "'\\u" << llvm::format("%04x", value) << "'";
1060     else
1061       OS << "'\\U" << llvm::format("%08x", value) << "'";
1062   }
1063 }
1064 
VisitIntegerLiteral(IntegerLiteral * Node)1065 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
1066   bool isSigned = Node->getType()->isSignedIntegerType();
1067   OS << Node->getValue().toString(10, isSigned);
1068 
1069   // Emit suffixes.  Integer literals are always a builtin integer type.
1070   switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1071   default: llvm_unreachable("Unexpected type for integer literal!");
1072   case BuiltinType::Char_S:
1073   case BuiltinType::Char_U:    OS << "i8"; break;
1074   case BuiltinType::UChar:     OS << "Ui8"; break;
1075   case BuiltinType::Short:     OS << "i16"; break;
1076   case BuiltinType::UShort:    OS << "Ui16"; break;
1077   case BuiltinType::Int:       break; // no suffix.
1078   case BuiltinType::UInt:      OS << 'U'; break;
1079   case BuiltinType::Long:      OS << 'L'; break;
1080   case BuiltinType::ULong:     OS << "UL"; break;
1081   case BuiltinType::LongLong:  OS << "LL"; break;
1082   case BuiltinType::ULongLong: OS << "ULL"; break;
1083   case BuiltinType::Int128:    OS << "i128"; break;
1084   case BuiltinType::UInt128:   OS << "Ui128"; break;
1085   }
1086 }
1087 
PrintFloatingLiteral(raw_ostream & OS,FloatingLiteral * Node,bool PrintSuffix)1088 static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1089                                  bool PrintSuffix) {
1090   SmallString<16> Str;
1091   Node->getValue().toString(Str);
1092   OS << Str;
1093   if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1094     OS << '.'; // Trailing dot in order to separate from ints.
1095 
1096   if (!PrintSuffix)
1097     return;
1098 
1099   // Emit suffixes.  Float literals are always a builtin float type.
1100   switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1101   default: llvm_unreachable("Unexpected type for float literal!");
1102   case BuiltinType::Half:       break; // FIXME: suffix?
1103   case BuiltinType::Double:     break; // no suffix.
1104   case BuiltinType::Float:      OS << 'F'; break;
1105   case BuiltinType::LongDouble: OS << 'L'; break;
1106   }
1107 }
1108 
VisitFloatingLiteral(FloatingLiteral * Node)1109 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
1110   PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
1111 }
1112 
VisitImaginaryLiteral(ImaginaryLiteral * Node)1113 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1114   PrintExpr(Node->getSubExpr());
1115   OS << "i";
1116 }
1117 
VisitStringLiteral(StringLiteral * Str)1118 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
1119   Str->outputString(OS);
1120 }
VisitParenExpr(ParenExpr * Node)1121 void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1122   OS << "(";
1123   PrintExpr(Node->getSubExpr());
1124   OS << ")";
1125 }
VisitUnaryOperator(UnaryOperator * Node)1126 void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
1127   if (!Node->isPostfix()) {
1128     OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1129 
1130     // Print a space if this is an "identifier operator" like __real, or if
1131     // it might be concatenated incorrectly like '+'.
1132     switch (Node->getOpcode()) {
1133     default: break;
1134     case UO_Real:
1135     case UO_Imag:
1136     case UO_Extension:
1137       OS << ' ';
1138       break;
1139     case UO_Plus:
1140     case UO_Minus:
1141       if (isa<UnaryOperator>(Node->getSubExpr()))
1142         OS << ' ';
1143       break;
1144     }
1145   }
1146   PrintExpr(Node->getSubExpr());
1147 
1148   if (Node->isPostfix())
1149     OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1150 }
1151 
VisitOffsetOfExpr(OffsetOfExpr * Node)1152 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1153   OS << "__builtin_offsetof(";
1154   Node->getTypeSourceInfo()->getType().print(OS, Policy);
1155   OS << ", ";
1156   bool PrintedSomething = false;
1157   for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
1158     OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
1159     if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
1160       // Array node
1161       OS << "[";
1162       PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1163       OS << "]";
1164       PrintedSomething = true;
1165       continue;
1166     }
1167 
1168     // Skip implicit base indirections.
1169     if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
1170       continue;
1171 
1172     // Field or identifier node.
1173     IdentifierInfo *Id = ON.getFieldName();
1174     if (!Id)
1175       continue;
1176 
1177     if (PrintedSomething)
1178       OS << ".";
1179     else
1180       PrintedSomething = true;
1181     OS << Id->getName();
1182   }
1183   OS << ")";
1184 }
1185 
VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr * Node)1186 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
1187   switch(Node->getKind()) {
1188   case UETT_SizeOf:
1189     OS << "sizeof";
1190     break;
1191   case UETT_AlignOf:
1192     if (Policy.LangOpts.CPlusPlus)
1193       OS << "alignof";
1194     else if (Policy.LangOpts.C11)
1195       OS << "_Alignof";
1196     else
1197       OS << "__alignof";
1198     break;
1199   case UETT_VecStep:
1200     OS << "vec_step";
1201     break;
1202   }
1203   if (Node->isArgumentType()) {
1204     OS << '(';
1205     Node->getArgumentType().print(OS, Policy);
1206     OS << ')';
1207   } else {
1208     OS << " ";
1209     PrintExpr(Node->getArgumentExpr());
1210   }
1211 }
1212 
VisitGenericSelectionExpr(GenericSelectionExpr * Node)1213 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1214   OS << "_Generic(";
1215   PrintExpr(Node->getControllingExpr());
1216   for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
1217     OS << ", ";
1218     QualType T = Node->getAssocType(i);
1219     if (T.isNull())
1220       OS << "default";
1221     else
1222       T.print(OS, Policy);
1223     OS << ": ";
1224     PrintExpr(Node->getAssocExpr(i));
1225   }
1226   OS << ")";
1227 }
1228 
VisitArraySubscriptExpr(ArraySubscriptExpr * Node)1229 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
1230   PrintExpr(Node->getLHS());
1231   OS << "[";
1232   PrintExpr(Node->getRHS());
1233   OS << "]";
1234 }
1235 
PrintCallArgs(CallExpr * Call)1236 void StmtPrinter::PrintCallArgs(CallExpr *Call) {
1237   for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
1238     if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1239       // Don't print any defaulted arguments
1240       break;
1241     }
1242 
1243     if (i) OS << ", ";
1244     PrintExpr(Call->getArg(i));
1245   }
1246 }
1247 
VisitCallExpr(CallExpr * Call)1248 void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1249   PrintExpr(Call->getCallee());
1250   OS << "(";
1251   PrintCallArgs(Call);
1252   OS << ")";
1253 }
VisitMemberExpr(MemberExpr * Node)1254 void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
1255   // FIXME: Suppress printing implicit bases (like "this")
1256   PrintExpr(Node->getBase());
1257 
1258   MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
1259   FieldDecl  *ParentDecl   = ParentMember
1260     ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : nullptr;
1261 
1262   if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
1263     OS << (Node->isArrow() ? "->" : ".");
1264 
1265   if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1266     if (FD->isAnonymousStructOrUnion())
1267       return;
1268 
1269   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1270     Qualifier->print(OS, Policy);
1271   if (Node->hasTemplateKeyword())
1272     OS << "template ";
1273   OS << Node->getMemberNameInfo();
1274   if (Node->hasExplicitTemplateArgs())
1275     TemplateSpecializationType::PrintTemplateArgumentList(
1276         OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1277 }
VisitObjCIsaExpr(ObjCIsaExpr * Node)1278 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1279   PrintExpr(Node->getBase());
1280   OS << (Node->isArrow() ? "->isa" : ".isa");
1281 }
1282 
VisitExtVectorElementExpr(ExtVectorElementExpr * Node)1283 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
1284   PrintExpr(Node->getBase());
1285   OS << ".";
1286   OS << Node->getAccessor().getName();
1287 }
VisitCStyleCastExpr(CStyleCastExpr * Node)1288 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
1289   OS << '(';
1290   Node->getTypeAsWritten().print(OS, Policy);
1291   OS << ')';
1292   PrintExpr(Node->getSubExpr());
1293 }
VisitCompoundLiteralExpr(CompoundLiteralExpr * Node)1294 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
1295   OS << '(';
1296   Node->getType().print(OS, Policy);
1297   OS << ')';
1298   PrintExpr(Node->getInitializer());
1299 }
VisitImplicitCastExpr(ImplicitCastExpr * Node)1300 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
1301   // No need to print anything, simply forward to the subexpression.
1302   PrintExpr(Node->getSubExpr());
1303 }
VisitBinaryOperator(BinaryOperator * Node)1304 void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1305   PrintExpr(Node->getLHS());
1306   OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1307   PrintExpr(Node->getRHS());
1308 }
VisitCompoundAssignOperator(CompoundAssignOperator * Node)1309 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1310   PrintExpr(Node->getLHS());
1311   OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1312   PrintExpr(Node->getRHS());
1313 }
VisitConditionalOperator(ConditionalOperator * Node)1314 void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1315   PrintExpr(Node->getCond());
1316   OS << " ? ";
1317   PrintExpr(Node->getLHS());
1318   OS << " : ";
1319   PrintExpr(Node->getRHS());
1320 }
1321 
1322 // GNU extensions.
1323 
1324 void
VisitBinaryConditionalOperator(BinaryConditionalOperator * Node)1325 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1326   PrintExpr(Node->getCommon());
1327   OS << " ?: ";
1328   PrintExpr(Node->getFalseExpr());
1329 }
VisitAddrLabelExpr(AddrLabelExpr * Node)1330 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
1331   OS << "&&" << Node->getLabel()->getName();
1332 }
1333 
VisitStmtExpr(StmtExpr * E)1334 void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1335   OS << "(";
1336   PrintRawCompoundStmt(E->getSubStmt());
1337   OS << ")";
1338 }
1339 
VisitChooseExpr(ChooseExpr * Node)1340 void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1341   OS << "__builtin_choose_expr(";
1342   PrintExpr(Node->getCond());
1343   OS << ", ";
1344   PrintExpr(Node->getLHS());
1345   OS << ", ";
1346   PrintExpr(Node->getRHS());
1347   OS << ")";
1348 }
1349 
VisitGNUNullExpr(GNUNullExpr *)1350 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1351   OS << "__null";
1352 }
1353 
VisitShuffleVectorExpr(ShuffleVectorExpr * Node)1354 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1355   OS << "__builtin_shufflevector(";
1356   for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1357     if (i) OS << ", ";
1358     PrintExpr(Node->getExpr(i));
1359   }
1360   OS << ")";
1361 }
1362 
VisitConvertVectorExpr(ConvertVectorExpr * Node)1363 void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1364   OS << "__builtin_convertvector(";
1365   PrintExpr(Node->getSrcExpr());
1366   OS << ", ";
1367   Node->getType().print(OS, Policy);
1368   OS << ")";
1369 }
1370 
VisitInitListExpr(InitListExpr * Node)1371 void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
1372   if (Node->getSyntacticForm()) {
1373     Visit(Node->getSyntacticForm());
1374     return;
1375   }
1376 
1377   OS << "{";
1378   for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1379     if (i) OS << ", ";
1380     if (Node->getInit(i))
1381       PrintExpr(Node->getInit(i));
1382     else
1383       OS << "{}";
1384   }
1385   OS << "}";
1386 }
1387 
VisitParenListExpr(ParenListExpr * Node)1388 void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1389   OS << "(";
1390   for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1391     if (i) OS << ", ";
1392     PrintExpr(Node->getExpr(i));
1393   }
1394   OS << ")";
1395 }
1396 
VisitDesignatedInitExpr(DesignatedInitExpr * Node)1397 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
1398   for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1399                       DEnd = Node->designators_end();
1400        D != DEnd; ++D) {
1401     if (D->isFieldDesignator()) {
1402       if (D->getDotLoc().isInvalid()) {
1403         if (IdentifierInfo *II = D->getFieldName())
1404           OS << II->getName() << ":";
1405       } else {
1406         OS << "." << D->getFieldName()->getName();
1407       }
1408     } else {
1409       OS << "[";
1410       if (D->isArrayDesignator()) {
1411         PrintExpr(Node->getArrayIndex(*D));
1412       } else {
1413         PrintExpr(Node->getArrayRangeStart(*D));
1414         OS << " ... ";
1415         PrintExpr(Node->getArrayRangeEnd(*D));
1416       }
1417       OS << "]";
1418     }
1419   }
1420 
1421   OS << " = ";
1422   PrintExpr(Node->getInit());
1423 }
1424 
VisitImplicitValueInitExpr(ImplicitValueInitExpr * Node)1425 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
1426   if (Policy.LangOpts.CPlusPlus) {
1427     OS << "/*implicit*/";
1428     Node->getType().print(OS, Policy);
1429     OS << "()";
1430   } else {
1431     OS << "/*implicit*/(";
1432     Node->getType().print(OS, Policy);
1433     OS << ')';
1434     if (Node->getType()->isRecordType())
1435       OS << "{}";
1436     else
1437       OS << 0;
1438   }
1439 }
1440 
VisitVAArgExpr(VAArgExpr * Node)1441 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
1442   OS << "__builtin_va_arg(";
1443   PrintExpr(Node->getSubExpr());
1444   OS << ", ";
1445   Node->getType().print(OS, Policy);
1446   OS << ")";
1447 }
1448 
VisitPseudoObjectExpr(PseudoObjectExpr * Node)1449 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1450   PrintExpr(Node->getSyntacticForm());
1451 }
1452 
VisitAtomicExpr(AtomicExpr * Node)1453 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
1454   const char *Name = nullptr;
1455   switch (Node->getOp()) {
1456 #define BUILTIN(ID, TYPE, ATTRS)
1457 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1458   case AtomicExpr::AO ## ID: \
1459     Name = #ID "("; \
1460     break;
1461 #include "clang/Basic/Builtins.def"
1462   }
1463   OS << Name;
1464 
1465   // AtomicExpr stores its subexpressions in a permuted order.
1466   PrintExpr(Node->getPtr());
1467   if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1468       Node->getOp() != AtomicExpr::AO__atomic_load_n) {
1469     OS << ", ";
1470     PrintExpr(Node->getVal1());
1471   }
1472   if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1473       Node->isCmpXChg()) {
1474     OS << ", ";
1475     PrintExpr(Node->getVal2());
1476   }
1477   if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1478       Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1479     OS << ", ";
1480     PrintExpr(Node->getWeak());
1481   }
1482   if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
1483     OS << ", ";
1484     PrintExpr(Node->getOrder());
1485   }
1486   if (Node->isCmpXChg()) {
1487     OS << ", ";
1488     PrintExpr(Node->getOrderFail());
1489   }
1490   OS << ")";
1491 }
1492 
1493 // C++
VisitCXXOperatorCallExpr(CXXOperatorCallExpr * Node)1494 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1495   const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1496     "",
1497 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1498     Spelling,
1499 #include "clang/Basic/OperatorKinds.def"
1500   };
1501 
1502   OverloadedOperatorKind Kind = Node->getOperator();
1503   if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1504     if (Node->getNumArgs() == 1) {
1505       OS << OpStrings[Kind] << ' ';
1506       PrintExpr(Node->getArg(0));
1507     } else {
1508       PrintExpr(Node->getArg(0));
1509       OS << ' ' << OpStrings[Kind];
1510     }
1511   } else if (Kind == OO_Arrow) {
1512     PrintExpr(Node->getArg(0));
1513   } else if (Kind == OO_Call) {
1514     PrintExpr(Node->getArg(0));
1515     OS << '(';
1516     for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1517       if (ArgIdx > 1)
1518         OS << ", ";
1519       if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1520         PrintExpr(Node->getArg(ArgIdx));
1521     }
1522     OS << ')';
1523   } else if (Kind == OO_Subscript) {
1524     PrintExpr(Node->getArg(0));
1525     OS << '[';
1526     PrintExpr(Node->getArg(1));
1527     OS << ']';
1528   } else if (Node->getNumArgs() == 1) {
1529     OS << OpStrings[Kind] << ' ';
1530     PrintExpr(Node->getArg(0));
1531   } else if (Node->getNumArgs() == 2) {
1532     PrintExpr(Node->getArg(0));
1533     OS << ' ' << OpStrings[Kind] << ' ';
1534     PrintExpr(Node->getArg(1));
1535   } else {
1536     llvm_unreachable("unknown overloaded operator");
1537   }
1538 }
1539 
VisitCXXMemberCallExpr(CXXMemberCallExpr * Node)1540 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1541   // If we have a conversion operator call only print the argument.
1542   CXXMethodDecl *MD = Node->getMethodDecl();
1543   if (MD && isa<CXXConversionDecl>(MD)) {
1544     PrintExpr(Node->getImplicitObjectArgument());
1545     return;
1546   }
1547   VisitCallExpr(cast<CallExpr>(Node));
1548 }
1549 
VisitCUDAKernelCallExpr(CUDAKernelCallExpr * Node)1550 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1551   PrintExpr(Node->getCallee());
1552   OS << "<<<";
1553   PrintCallArgs(Node->getConfig());
1554   OS << ">>>(";
1555   PrintCallArgs(Node);
1556   OS << ")";
1557 }
1558 
VisitCXXNamedCastExpr(CXXNamedCastExpr * Node)1559 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1560   OS << Node->getCastName() << '<';
1561   Node->getTypeAsWritten().print(OS, Policy);
1562   OS << ">(";
1563   PrintExpr(Node->getSubExpr());
1564   OS << ")";
1565 }
1566 
VisitCXXStaticCastExpr(CXXStaticCastExpr * Node)1567 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1568   VisitCXXNamedCastExpr(Node);
1569 }
1570 
VisitCXXDynamicCastExpr(CXXDynamicCastExpr * Node)1571 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1572   VisitCXXNamedCastExpr(Node);
1573 }
1574 
VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr * Node)1575 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1576   VisitCXXNamedCastExpr(Node);
1577 }
1578 
VisitCXXConstCastExpr(CXXConstCastExpr * Node)1579 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1580   VisitCXXNamedCastExpr(Node);
1581 }
1582 
VisitCXXTypeidExpr(CXXTypeidExpr * Node)1583 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1584   OS << "typeid(";
1585   if (Node->isTypeOperand()) {
1586     Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1587   } else {
1588     PrintExpr(Node->getExprOperand());
1589   }
1590   OS << ")";
1591 }
1592 
VisitCXXUuidofExpr(CXXUuidofExpr * Node)1593 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1594   OS << "__uuidof(";
1595   if (Node->isTypeOperand()) {
1596     Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1597   } else {
1598     PrintExpr(Node->getExprOperand());
1599   }
1600   OS << ")";
1601 }
1602 
VisitMSPropertyRefExpr(MSPropertyRefExpr * Node)1603 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1604   PrintExpr(Node->getBaseExpr());
1605   if (Node->isArrow())
1606     OS << "->";
1607   else
1608     OS << ".";
1609   if (NestedNameSpecifier *Qualifier =
1610       Node->getQualifierLoc().getNestedNameSpecifier())
1611     Qualifier->print(OS, Policy);
1612   OS << Node->getPropertyDecl()->getDeclName();
1613 }
1614 
VisitUserDefinedLiteral(UserDefinedLiteral * Node)1615 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1616   switch (Node->getLiteralOperatorKind()) {
1617   case UserDefinedLiteral::LOK_Raw:
1618     OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
1619     break;
1620   case UserDefinedLiteral::LOK_Template: {
1621     DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1622     const TemplateArgumentList *Args =
1623       cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1624     assert(Args);
1625 
1626     if (Args->size() != 1) {
1627       OS << "operator \"\" " << Node->getUDSuffix()->getName();
1628       TemplateSpecializationType::PrintTemplateArgumentList(
1629           OS, Args->data(), Args->size(), Policy);
1630       OS << "()";
1631       return;
1632     }
1633 
1634     const TemplateArgument &Pack = Args->get(0);
1635     for (const auto &P : Pack.pack_elements()) {
1636       char C = (char)P.getAsIntegral().getZExtValue();
1637       OS << C;
1638     }
1639     break;
1640   }
1641   case UserDefinedLiteral::LOK_Integer: {
1642     // Print integer literal without suffix.
1643     IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1644     OS << Int->getValue().toString(10, /*isSigned*/false);
1645     break;
1646   }
1647   case UserDefinedLiteral::LOK_Floating: {
1648     // Print floating literal without suffix.
1649     FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1650     PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1651     break;
1652   }
1653   case UserDefinedLiteral::LOK_String:
1654   case UserDefinedLiteral::LOK_Character:
1655     PrintExpr(Node->getCookedLiteral());
1656     break;
1657   }
1658   OS << Node->getUDSuffix()->getName();
1659 }
1660 
VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr * Node)1661 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1662   OS << (Node->getValue() ? "true" : "false");
1663 }
1664 
VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr * Node)1665 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1666   OS << "nullptr";
1667 }
1668 
VisitCXXThisExpr(CXXThisExpr * Node)1669 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1670   OS << "this";
1671 }
1672 
VisitCXXThrowExpr(CXXThrowExpr * Node)1673 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1674   if (!Node->getSubExpr())
1675     OS << "throw";
1676   else {
1677     OS << "throw ";
1678     PrintExpr(Node->getSubExpr());
1679   }
1680 }
1681 
VisitCXXDefaultArgExpr(CXXDefaultArgExpr * Node)1682 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1683   // Nothing to print: we picked up the default argument.
1684 }
1685 
VisitCXXDefaultInitExpr(CXXDefaultInitExpr * Node)1686 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1687   // Nothing to print: we picked up the default initializer.
1688 }
1689 
VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr * Node)1690 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1691   Node->getType().print(OS, Policy);
1692   // If there are no parens, this is list-initialization, and the braces are
1693   // part of the syntax of the inner construct.
1694   if (Node->getLParenLoc().isValid())
1695     OS << "(";
1696   PrintExpr(Node->getSubExpr());
1697   if (Node->getLParenLoc().isValid())
1698     OS << ")";
1699 }
1700 
VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr * Node)1701 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1702   PrintExpr(Node->getSubExpr());
1703 }
1704 
VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr * Node)1705 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1706   Node->getType().print(OS, Policy);
1707   if (Node->isStdInitListInitialization())
1708     /* Nothing to do; braces are part of creating the std::initializer_list. */;
1709   else if (Node->isListInitialization())
1710     OS << "{";
1711   else
1712     OS << "(";
1713   for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1714                                          ArgEnd = Node->arg_end();
1715        Arg != ArgEnd; ++Arg) {
1716     if (Arg->isDefaultArgument())
1717       break;
1718     if (Arg != Node->arg_begin())
1719       OS << ", ";
1720     PrintExpr(*Arg);
1721   }
1722   if (Node->isStdInitListInitialization())
1723     /* See above. */;
1724   else if (Node->isListInitialization())
1725     OS << "}";
1726   else
1727     OS << ")";
1728 }
1729 
VisitLambdaExpr(LambdaExpr * Node)1730 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1731   OS << '[';
1732   bool NeedComma = false;
1733   switch (Node->getCaptureDefault()) {
1734   case LCD_None:
1735     break;
1736 
1737   case LCD_ByCopy:
1738     OS << '=';
1739     NeedComma = true;
1740     break;
1741 
1742   case LCD_ByRef:
1743     OS << '&';
1744     NeedComma = true;
1745     break;
1746   }
1747   for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1748                                  CEnd = Node->explicit_capture_end();
1749        C != CEnd;
1750        ++C) {
1751     if (NeedComma)
1752       OS << ", ";
1753     NeedComma = true;
1754 
1755     switch (C->getCaptureKind()) {
1756     case LCK_This:
1757       OS << "this";
1758       break;
1759 
1760     case LCK_ByRef:
1761       if (Node->getCaptureDefault() != LCD_ByRef || C->isInitCapture())
1762         OS << '&';
1763       OS << C->getCapturedVar()->getName();
1764       break;
1765 
1766     case LCK_ByCopy:
1767       OS << C->getCapturedVar()->getName();
1768       break;
1769     case LCK_VLAType:
1770       llvm_unreachable("VLA type in explicit captures.");
1771     }
1772 
1773     if (C->isInitCapture())
1774       PrintExpr(C->getCapturedVar()->getInit());
1775   }
1776   OS << ']';
1777 
1778   if (Node->hasExplicitParameters()) {
1779     OS << " (";
1780     CXXMethodDecl *Method = Node->getCallOperator();
1781     NeedComma = false;
1782     for (auto P : Method->params()) {
1783       if (NeedComma) {
1784         OS << ", ";
1785       } else {
1786         NeedComma = true;
1787       }
1788       std::string ParamStr = P->getNameAsString();
1789       P->getOriginalType().print(OS, Policy, ParamStr);
1790     }
1791     if (Method->isVariadic()) {
1792       if (NeedComma)
1793         OS << ", ";
1794       OS << "...";
1795     }
1796     OS << ')';
1797 
1798     if (Node->isMutable())
1799       OS << " mutable";
1800 
1801     const FunctionProtoType *Proto
1802       = Method->getType()->getAs<FunctionProtoType>();
1803     Proto->printExceptionSpecification(OS, Policy);
1804 
1805     // FIXME: Attributes
1806 
1807     // Print the trailing return type if it was specified in the source.
1808     if (Node->hasExplicitResultType()) {
1809       OS << " -> ";
1810       Proto->getReturnType().print(OS, Policy);
1811     }
1812   }
1813 
1814   // Print the body.
1815   CompoundStmt *Body = Node->getBody();
1816   OS << ' ';
1817   PrintStmt(Body);
1818 }
1819 
VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr * Node)1820 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
1821   if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
1822     TSInfo->getType().print(OS, Policy);
1823   else
1824     Node->getType().print(OS, Policy);
1825   OS << "()";
1826 }
1827 
VisitCXXNewExpr(CXXNewExpr * E)1828 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1829   if (E->isGlobalNew())
1830     OS << "::";
1831   OS << "new ";
1832   unsigned NumPlace = E->getNumPlacementArgs();
1833   if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
1834     OS << "(";
1835     PrintExpr(E->getPlacementArg(0));
1836     for (unsigned i = 1; i < NumPlace; ++i) {
1837       if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
1838         break;
1839       OS << ", ";
1840       PrintExpr(E->getPlacementArg(i));
1841     }
1842     OS << ") ";
1843   }
1844   if (E->isParenTypeId())
1845     OS << "(";
1846   std::string TypeS;
1847   if (Expr *Size = E->getArraySize()) {
1848     llvm::raw_string_ostream s(TypeS);
1849     s << '[';
1850     Size->printPretty(s, Helper, Policy);
1851     s << ']';
1852   }
1853   E->getAllocatedType().print(OS, Policy, TypeS);
1854   if (E->isParenTypeId())
1855     OS << ")";
1856 
1857   CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
1858   if (InitStyle) {
1859     if (InitStyle == CXXNewExpr::CallInit)
1860       OS << "(";
1861     PrintExpr(E->getInitializer());
1862     if (InitStyle == CXXNewExpr::CallInit)
1863       OS << ")";
1864   }
1865 }
1866 
VisitCXXDeleteExpr(CXXDeleteExpr * E)1867 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1868   if (E->isGlobalDelete())
1869     OS << "::";
1870   OS << "delete ";
1871   if (E->isArrayForm())
1872     OS << "[] ";
1873   PrintExpr(E->getArgument());
1874 }
1875 
VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr * E)1876 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1877   PrintExpr(E->getBase());
1878   if (E->isArrow())
1879     OS << "->";
1880   else
1881     OS << '.';
1882   if (E->getQualifier())
1883     E->getQualifier()->print(OS, Policy);
1884   OS << "~";
1885 
1886   if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1887     OS << II->getName();
1888   else
1889     E->getDestroyedType().print(OS, Policy);
1890 }
1891 
VisitCXXConstructExpr(CXXConstructExpr * E)1892 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
1893   if (E->isListInitialization() && !E->isStdInitListInitialization())
1894     OS << "{";
1895 
1896   for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1897     if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1898       // Don't print any defaulted arguments
1899       break;
1900     }
1901 
1902     if (i) OS << ", ";
1903     PrintExpr(E->getArg(i));
1904   }
1905 
1906   if (E->isListInitialization() && !E->isStdInitListInitialization())
1907     OS << "}";
1908 }
1909 
VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr * E)1910 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
1911   PrintExpr(E->getSubExpr());
1912 }
1913 
VisitExprWithCleanups(ExprWithCleanups * E)1914 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
1915   // Just forward to the subexpression.
1916   PrintExpr(E->getSubExpr());
1917 }
1918 
1919 void
VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr * Node)1920 StmtPrinter::VisitCXXUnresolvedConstructExpr(
1921                                            CXXUnresolvedConstructExpr *Node) {
1922   Node->getTypeAsWritten().print(OS, Policy);
1923   OS << "(";
1924   for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
1925                                              ArgEnd = Node->arg_end();
1926        Arg != ArgEnd; ++Arg) {
1927     if (Arg != Node->arg_begin())
1928       OS << ", ";
1929     PrintExpr(*Arg);
1930   }
1931   OS << ")";
1932 }
1933 
VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr * Node)1934 void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1935                                          CXXDependentScopeMemberExpr *Node) {
1936   if (!Node->isImplicitAccess()) {
1937     PrintExpr(Node->getBase());
1938     OS << (Node->isArrow() ? "->" : ".");
1939   }
1940   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1941     Qualifier->print(OS, Policy);
1942   if (Node->hasTemplateKeyword())
1943     OS << "template ";
1944   OS << Node->getMemberNameInfo();
1945   if (Node->hasExplicitTemplateArgs())
1946     TemplateSpecializationType::PrintTemplateArgumentList(
1947         OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1948 }
1949 
VisitUnresolvedMemberExpr(UnresolvedMemberExpr * Node)1950 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
1951   if (!Node->isImplicitAccess()) {
1952     PrintExpr(Node->getBase());
1953     OS << (Node->isArrow() ? "->" : ".");
1954   }
1955   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1956     Qualifier->print(OS, Policy);
1957   if (Node->hasTemplateKeyword())
1958     OS << "template ";
1959   OS << Node->getMemberNameInfo();
1960   if (Node->hasExplicitTemplateArgs())
1961     TemplateSpecializationType::PrintTemplateArgumentList(
1962         OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1963 }
1964 
getTypeTraitName(TypeTrait TT)1965 static const char *getTypeTraitName(TypeTrait TT) {
1966   switch (TT) {
1967 #define TYPE_TRAIT_1(Spelling, Name, Key) \
1968 case clang::UTT_##Name: return #Spelling;
1969 #define TYPE_TRAIT_2(Spelling, Name, Key) \
1970 case clang::BTT_##Name: return #Spelling;
1971 #define TYPE_TRAIT_N(Spelling, Name, Key) \
1972   case clang::TT_##Name: return #Spelling;
1973 #include "clang/Basic/TokenKinds.def"
1974   }
1975   llvm_unreachable("Type trait not covered by switch");
1976 }
1977 
getTypeTraitName(ArrayTypeTrait ATT)1978 static const char *getTypeTraitName(ArrayTypeTrait ATT) {
1979   switch (ATT) {
1980   case ATT_ArrayRank:        return "__array_rank";
1981   case ATT_ArrayExtent:      return "__array_extent";
1982   }
1983   llvm_unreachable("Array type trait not covered by switch");
1984 }
1985 
getExpressionTraitName(ExpressionTrait ET)1986 static const char *getExpressionTraitName(ExpressionTrait ET) {
1987   switch (ET) {
1988   case ET_IsLValueExpr:      return "__is_lvalue_expr";
1989   case ET_IsRValueExpr:      return "__is_rvalue_expr";
1990   }
1991   llvm_unreachable("Expression type trait not covered by switch");
1992 }
1993 
VisitTypeTraitExpr(TypeTraitExpr * E)1994 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
1995   OS << getTypeTraitName(E->getTrait()) << "(";
1996   for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
1997     if (I > 0)
1998       OS << ", ";
1999     E->getArg(I)->getType().print(OS, Policy);
2000   }
2001   OS << ")";
2002 }
2003 
VisitArrayTypeTraitExpr(ArrayTypeTraitExpr * E)2004 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
2005   OS << getTypeTraitName(E->getTrait()) << '(';
2006   E->getQueriedType().print(OS, Policy);
2007   OS << ')';
2008 }
2009 
VisitExpressionTraitExpr(ExpressionTraitExpr * E)2010 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
2011   OS << getExpressionTraitName(E->getTrait()) << '(';
2012   PrintExpr(E->getQueriedExpression());
2013   OS << ')';
2014 }
2015 
VisitCXXNoexceptExpr(CXXNoexceptExpr * E)2016 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
2017   OS << "noexcept(";
2018   PrintExpr(E->getOperand());
2019   OS << ")";
2020 }
2021 
VisitPackExpansionExpr(PackExpansionExpr * E)2022 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
2023   PrintExpr(E->getPattern());
2024   OS << "...";
2025 }
2026 
VisitSizeOfPackExpr(SizeOfPackExpr * E)2027 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
2028   OS << "sizeof...(" << *E->getPack() << ")";
2029 }
2030 
VisitSubstNonTypeTemplateParmPackExpr(SubstNonTypeTemplateParmPackExpr * Node)2031 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2032                                        SubstNonTypeTemplateParmPackExpr *Node) {
2033   OS << *Node->getParameterPack();
2034 }
2035 
VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr * Node)2036 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2037                                        SubstNonTypeTemplateParmExpr *Node) {
2038   Visit(Node->getReplacement());
2039 }
2040 
VisitFunctionParmPackExpr(FunctionParmPackExpr * E)2041 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2042   OS << *E->getParameterPack();
2043 }
2044 
VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr * Node)2045 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
2046   PrintExpr(Node->GetTemporaryExpr());
2047 }
2048 
VisitCXXFoldExpr(CXXFoldExpr * E)2049 void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
2050   OS << "(";
2051   if (E->getLHS()) {
2052     PrintExpr(E->getLHS());
2053     OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2054   }
2055   OS << "...";
2056   if (E->getRHS()) {
2057     OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2058     PrintExpr(E->getRHS());
2059   }
2060   OS << ")";
2061 }
2062 
2063 // Obj-C
2064 
VisitObjCStringLiteral(ObjCStringLiteral * Node)2065 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2066   OS << "@";
2067   VisitStringLiteral(Node->getString());
2068 }
2069 
VisitObjCBoxedExpr(ObjCBoxedExpr * E)2070 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
2071   OS << "@";
2072   Visit(E->getSubExpr());
2073 }
2074 
VisitObjCArrayLiteral(ObjCArrayLiteral * E)2075 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2076   OS << "@[ ";
2077   StmtRange ch = E->children();
2078   if (ch.first != ch.second) {
2079     while (1) {
2080       Visit(*ch.first);
2081       ++ch.first;
2082       if (ch.first == ch.second) break;
2083       OS << ", ";
2084     }
2085   }
2086   OS << " ]";
2087 }
2088 
VisitObjCDictionaryLiteral(ObjCDictionaryLiteral * E)2089 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2090   OS << "@{ ";
2091   for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
2092     if (I > 0)
2093       OS << ", ";
2094 
2095     ObjCDictionaryElement Element = E->getKeyValueElement(I);
2096     Visit(Element.Key);
2097     OS << " : ";
2098     Visit(Element.Value);
2099     if (Element.isPackExpansion())
2100       OS << "...";
2101   }
2102   OS << " }";
2103 }
2104 
VisitObjCEncodeExpr(ObjCEncodeExpr * Node)2105 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
2106   OS << "@encode(";
2107   Node->getEncodedType().print(OS, Policy);
2108   OS << ')';
2109 }
2110 
VisitObjCSelectorExpr(ObjCSelectorExpr * Node)2111 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
2112   OS << "@selector(";
2113   Node->getSelector().print(OS);
2114   OS << ')';
2115 }
2116 
VisitObjCProtocolExpr(ObjCProtocolExpr * Node)2117 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
2118   OS << "@protocol(" << *Node->getProtocol() << ')';
2119 }
2120 
VisitObjCMessageExpr(ObjCMessageExpr * Mess)2121 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2122   OS << "[";
2123   switch (Mess->getReceiverKind()) {
2124   case ObjCMessageExpr::Instance:
2125     PrintExpr(Mess->getInstanceReceiver());
2126     break;
2127 
2128   case ObjCMessageExpr::Class:
2129     Mess->getClassReceiver().print(OS, Policy);
2130     break;
2131 
2132   case ObjCMessageExpr::SuperInstance:
2133   case ObjCMessageExpr::SuperClass:
2134     OS << "Super";
2135     break;
2136   }
2137 
2138   OS << ' ';
2139   Selector selector = Mess->getSelector();
2140   if (selector.isUnarySelector()) {
2141     OS << selector.getNameForSlot(0);
2142   } else {
2143     for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
2144       if (i < selector.getNumArgs()) {
2145         if (i > 0) OS << ' ';
2146         if (selector.getIdentifierInfoForSlot(i))
2147           OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
2148         else
2149            OS << ":";
2150       }
2151       else OS << ", "; // Handle variadic methods.
2152 
2153       PrintExpr(Mess->getArg(i));
2154     }
2155   }
2156   OS << "]";
2157 }
2158 
VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr * Node)2159 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2160   OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2161 }
2162 
2163 void
VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr * E)2164 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2165   PrintExpr(E->getSubExpr());
2166 }
2167 
2168 void
VisitObjCBridgedCastExpr(ObjCBridgedCastExpr * E)2169 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
2170   OS << '(' << E->getBridgeKindName();
2171   E->getType().print(OS, Policy);
2172   OS << ')';
2173   PrintExpr(E->getSubExpr());
2174 }
2175 
VisitBlockExpr(BlockExpr * Node)2176 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
2177   BlockDecl *BD = Node->getBlockDecl();
2178   OS << "^";
2179 
2180   const FunctionType *AFT = Node->getFunctionType();
2181 
2182   if (isa<FunctionNoProtoType>(AFT)) {
2183     OS << "()";
2184   } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
2185     OS << '(';
2186     for (BlockDecl::param_iterator AI = BD->param_begin(),
2187          E = BD->param_end(); AI != E; ++AI) {
2188       if (AI != BD->param_begin()) OS << ", ";
2189       std::string ParamStr = (*AI)->getNameAsString();
2190       (*AI)->getType().print(OS, Policy, ParamStr);
2191     }
2192 
2193     const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
2194     if (FT->isVariadic()) {
2195       if (!BD->param_empty()) OS << ", ";
2196       OS << "...";
2197     }
2198     OS << ')';
2199   }
2200   OS << "{ }";
2201 }
2202 
VisitOpaqueValueExpr(OpaqueValueExpr * Node)2203 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
2204   PrintExpr(Node->getSourceExpr());
2205 }
2206 
VisitTypoExpr(TypoExpr * Node)2207 void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
2208   // TODO: Print something reasonable for a TypoExpr, if necessary.
2209   assert(false && "Cannot print TypoExpr nodes");
2210 }
2211 
VisitAsTypeExpr(AsTypeExpr * Node)2212 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2213   OS << "__builtin_astype(";
2214   PrintExpr(Node->getSrcExpr());
2215   OS << ", ";
2216   Node->getType().print(OS, Policy);
2217   OS << ")";
2218 }
2219 
2220 //===----------------------------------------------------------------------===//
2221 // Stmt method implementations
2222 //===----------------------------------------------------------------------===//
2223 
dumpPretty(const ASTContext & Context) const2224 void Stmt::dumpPretty(const ASTContext &Context) const {
2225   printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
2226 }
2227 
printPretty(raw_ostream & OS,PrinterHelper * Helper,const PrintingPolicy & Policy,unsigned Indentation) const2228 void Stmt::printPretty(raw_ostream &OS,
2229                        PrinterHelper *Helper,
2230                        const PrintingPolicy &Policy,
2231                        unsigned Indentation) const {
2232   StmtPrinter P(OS, Helper, Policy, Indentation);
2233   P.Visit(const_cast<Stmt*>(this));
2234 }
2235 
2236 //===----------------------------------------------------------------------===//
2237 // PrinterHelper
2238 //===----------------------------------------------------------------------===//
2239 
2240 // Implement virtual destructor.
~PrinterHelper()2241 PrinterHelper::~PrinterHelper() {}
2242