xref: /freebsd-src/contrib/llvm-project/clang/lib/AST/StmtPrinter.cpp (revision 5e801ac66d24704442eba426ed13c3effb8a34e7)
1 //===- StmtPrinter.cpp - Printing implementation for Stmt ASTs ------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the Stmt::dumpPretty/Stmt::printPretty methods, which
10 // pretty print the AST back out to C code.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/Attr.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclBase.h"
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/DeclObjC.h"
20 #include "clang/AST/DeclOpenMP.h"
21 #include "clang/AST/DeclTemplate.h"
22 #include "clang/AST/Expr.h"
23 #include "clang/AST/ExprCXX.h"
24 #include "clang/AST/ExprObjC.h"
25 #include "clang/AST/ExprOpenMP.h"
26 #include "clang/AST/NestedNameSpecifier.h"
27 #include "clang/AST/OpenMPClause.h"
28 #include "clang/AST/PrettyPrinter.h"
29 #include "clang/AST/Stmt.h"
30 #include "clang/AST/StmtCXX.h"
31 #include "clang/AST/StmtObjC.h"
32 #include "clang/AST/StmtOpenMP.h"
33 #include "clang/AST/StmtVisitor.h"
34 #include "clang/AST/TemplateBase.h"
35 #include "clang/AST/Type.h"
36 #include "clang/Basic/CharInfo.h"
37 #include "clang/Basic/ExpressionTraits.h"
38 #include "clang/Basic/IdentifierTable.h"
39 #include "clang/Basic/JsonSupport.h"
40 #include "clang/Basic/LLVM.h"
41 #include "clang/Basic/Lambda.h"
42 #include "clang/Basic/OpenMPKinds.h"
43 #include "clang/Basic/OperatorKinds.h"
44 #include "clang/Basic/SourceLocation.h"
45 #include "clang/Basic/TypeTraits.h"
46 #include "clang/Lex/Lexer.h"
47 #include "llvm/ADT/ArrayRef.h"
48 #include "llvm/ADT/SmallString.h"
49 #include "llvm/ADT/SmallVector.h"
50 #include "llvm/ADT/StringExtras.h"
51 #include "llvm/ADT/StringRef.h"
52 #include "llvm/Support/Casting.h"
53 #include "llvm/Support/Compiler.h"
54 #include "llvm/Support/ErrorHandling.h"
55 #include "llvm/Support/raw_ostream.h"
56 #include <cassert>
57 #include <string>
58 
59 using namespace clang;
60 
61 //===----------------------------------------------------------------------===//
62 // StmtPrinter Visitor
63 //===----------------------------------------------------------------------===//
64 
65 namespace {
66 
67   class StmtPrinter : public StmtVisitor<StmtPrinter> {
68     raw_ostream &OS;
69     unsigned IndentLevel;
70     PrinterHelper* Helper;
71     PrintingPolicy Policy;
72     std::string NL;
73     const ASTContext *Context;
74 
75   public:
76     StmtPrinter(raw_ostream &os, PrinterHelper *helper,
77                 const PrintingPolicy &Policy, unsigned Indentation = 0,
78                 StringRef NL = "\n", const ASTContext *Context = nullptr)
79         : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy),
80           NL(NL), Context(Context) {}
81 
82     void PrintStmt(Stmt *S) { PrintStmt(S, Policy.Indentation); }
83 
84     void PrintStmt(Stmt *S, int SubIndent) {
85       IndentLevel += SubIndent;
86       if (S && isa<Expr>(S)) {
87         // If this is an expr used in a stmt context, indent and newline it.
88         Indent();
89         Visit(S);
90         OS << ";" << NL;
91       } else if (S) {
92         Visit(S);
93       } else {
94         Indent() << "<<<NULL STATEMENT>>>" << NL;
95       }
96       IndentLevel -= SubIndent;
97     }
98 
99     void PrintInitStmt(Stmt *S, unsigned PrefixWidth) {
100       // FIXME: Cope better with odd prefix widths.
101       IndentLevel += (PrefixWidth + 1) / 2;
102       if (auto *DS = dyn_cast<DeclStmt>(S))
103         PrintRawDeclStmt(DS);
104       else
105         PrintExpr(cast<Expr>(S));
106       OS << "; ";
107       IndentLevel -= (PrefixWidth + 1) / 2;
108     }
109 
110     void PrintControlledStmt(Stmt *S) {
111       if (auto *CS = dyn_cast<CompoundStmt>(S)) {
112         OS << " ";
113         PrintRawCompoundStmt(CS);
114         OS << NL;
115       } else {
116         OS << NL;
117         PrintStmt(S);
118       }
119     }
120 
121     void PrintRawCompoundStmt(CompoundStmt *S);
122     void PrintRawDecl(Decl *D);
123     void PrintRawDeclStmt(const DeclStmt *S);
124     void PrintRawIfStmt(IfStmt *If);
125     void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
126     void PrintCallArgs(CallExpr *E);
127     void PrintRawSEHExceptHandler(SEHExceptStmt *S);
128     void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
129     void PrintOMPExecutableDirective(OMPExecutableDirective *S,
130                                      bool ForceNoStmt = false);
131 
132     void PrintExpr(Expr *E) {
133       if (E)
134         Visit(E);
135       else
136         OS << "<null expr>";
137     }
138 
139     raw_ostream &Indent(int Delta = 0) {
140       for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
141         OS << "  ";
142       return OS;
143     }
144 
145     void Visit(Stmt* S) {
146       if (Helper && Helper->handledStmt(S,OS))
147           return;
148       else StmtVisitor<StmtPrinter>::Visit(S);
149     }
150 
151     void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
152       Indent() << "<<unknown stmt type>>" << NL;
153     }
154 
155     void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
156       OS << "<<unknown expr type>>";
157     }
158 
159     void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
160 
161 #define ABSTRACT_STMT(CLASS)
162 #define STMT(CLASS, PARENT) \
163     void Visit##CLASS(CLASS *Node);
164 #include "clang/AST/StmtNodes.inc"
165   };
166 
167 } // namespace
168 
169 //===----------------------------------------------------------------------===//
170 //  Stmt printing methods.
171 //===----------------------------------------------------------------------===//
172 
173 /// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
174 /// with no newline after the }.
175 void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
176   OS << "{" << NL;
177   for (auto *I : Node->body())
178     PrintStmt(I);
179 
180   Indent() << "}";
181 }
182 
183 void StmtPrinter::PrintRawDecl(Decl *D) {
184   D->print(OS, Policy, IndentLevel);
185 }
186 
187 void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
188   SmallVector<Decl *, 2> Decls(S->decls());
189   Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
190 }
191 
192 void StmtPrinter::VisitNullStmt(NullStmt *Node) {
193   Indent() << ";" << NL;
194 }
195 
196 void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
197   Indent();
198   PrintRawDeclStmt(Node);
199   OS << ";" << NL;
200 }
201 
202 void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
203   Indent();
204   PrintRawCompoundStmt(Node);
205   OS << "" << NL;
206 }
207 
208 void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
209   Indent(-1) << "case ";
210   PrintExpr(Node->getLHS());
211   if (Node->getRHS()) {
212     OS << " ... ";
213     PrintExpr(Node->getRHS());
214   }
215   OS << ":" << NL;
216 
217   PrintStmt(Node->getSubStmt(), 0);
218 }
219 
220 void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
221   Indent(-1) << "default:" << NL;
222   PrintStmt(Node->getSubStmt(), 0);
223 }
224 
225 void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
226   Indent(-1) << Node->getName() << ":" << NL;
227   PrintStmt(Node->getSubStmt(), 0);
228 }
229 
230 void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
231   for (const auto *Attr : Node->getAttrs()) {
232     Attr->printPretty(OS, Policy);
233   }
234 
235   PrintStmt(Node->getSubStmt(), 0);
236 }
237 
238 void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
239   if (If->isConsteval()) {
240     OS << "if ";
241     if (If->isNegatedConsteval())
242       OS << "!";
243     OS << "consteval";
244     OS << NL;
245     PrintStmt(If->getThen());
246     if (Stmt *Else = If->getElse()) {
247       Indent();
248       OS << "else";
249       PrintStmt(Else);
250       OS << NL;
251     }
252     return;
253   }
254 
255   OS << "if (";
256   if (If->getInit())
257     PrintInitStmt(If->getInit(), 4);
258   if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
259     PrintRawDeclStmt(DS);
260   else
261     PrintExpr(If->getCond());
262   OS << ')';
263 
264   if (auto *CS = dyn_cast<CompoundStmt>(If->getThen())) {
265     OS << ' ';
266     PrintRawCompoundStmt(CS);
267     OS << (If->getElse() ? " " : NL);
268   } else {
269     OS << NL;
270     PrintStmt(If->getThen());
271     if (If->getElse()) Indent();
272   }
273 
274   if (Stmt *Else = If->getElse()) {
275     OS << "else";
276 
277     if (auto *CS = dyn_cast<CompoundStmt>(Else)) {
278       OS << ' ';
279       PrintRawCompoundStmt(CS);
280       OS << NL;
281     } else if (auto *ElseIf = dyn_cast<IfStmt>(Else)) {
282       OS << ' ';
283       PrintRawIfStmt(ElseIf);
284     } else {
285       OS << NL;
286       PrintStmt(If->getElse());
287     }
288   }
289 }
290 
291 void StmtPrinter::VisitIfStmt(IfStmt *If) {
292   Indent();
293   PrintRawIfStmt(If);
294 }
295 
296 void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
297   Indent() << "switch (";
298   if (Node->getInit())
299     PrintInitStmt(Node->getInit(), 8);
300   if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
301     PrintRawDeclStmt(DS);
302   else
303     PrintExpr(Node->getCond());
304   OS << ")";
305   PrintControlledStmt(Node->getBody());
306 }
307 
308 void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
309   Indent() << "while (";
310   if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
311     PrintRawDeclStmt(DS);
312   else
313     PrintExpr(Node->getCond());
314   OS << ")" << NL;
315   PrintStmt(Node->getBody());
316 }
317 
318 void StmtPrinter::VisitDoStmt(DoStmt *Node) {
319   Indent() << "do ";
320   if (auto *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
321     PrintRawCompoundStmt(CS);
322     OS << " ";
323   } else {
324     OS << NL;
325     PrintStmt(Node->getBody());
326     Indent();
327   }
328 
329   OS << "while (";
330   PrintExpr(Node->getCond());
331   OS << ");" << NL;
332 }
333 
334 void StmtPrinter::VisitForStmt(ForStmt *Node) {
335   Indent() << "for (";
336   if (Node->getInit())
337     PrintInitStmt(Node->getInit(), 5);
338   else
339     OS << (Node->getCond() ? "; " : ";");
340   if (Node->getCond())
341     PrintExpr(Node->getCond());
342   OS << ";";
343   if (Node->getInc()) {
344     OS << " ";
345     PrintExpr(Node->getInc());
346   }
347   OS << ")";
348   PrintControlledStmt(Node->getBody());
349 }
350 
351 void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
352   Indent() << "for (";
353   if (auto *DS = dyn_cast<DeclStmt>(Node->getElement()))
354     PrintRawDeclStmt(DS);
355   else
356     PrintExpr(cast<Expr>(Node->getElement()));
357   OS << " in ";
358   PrintExpr(Node->getCollection());
359   OS << ")";
360   PrintControlledStmt(Node->getBody());
361 }
362 
363 void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
364   Indent() << "for (";
365   if (Node->getInit())
366     PrintInitStmt(Node->getInit(), 5);
367   PrintingPolicy SubPolicy(Policy);
368   SubPolicy.SuppressInitializers = true;
369   Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
370   OS << " : ";
371   PrintExpr(Node->getRangeInit());
372   OS << ")";
373   PrintControlledStmt(Node->getBody());
374 }
375 
376 void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
377   Indent();
378   if (Node->isIfExists())
379     OS << "__if_exists (";
380   else
381     OS << "__if_not_exists (";
382 
383   if (NestedNameSpecifier *Qualifier
384         = Node->getQualifierLoc().getNestedNameSpecifier())
385     Qualifier->print(OS, Policy);
386 
387   OS << Node->getNameInfo() << ") ";
388 
389   PrintRawCompoundStmt(Node->getSubStmt());
390 }
391 
392 void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
393   Indent() << "goto " << Node->getLabel()->getName() << ";";
394   if (Policy.IncludeNewlines) OS << NL;
395 }
396 
397 void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
398   Indent() << "goto *";
399   PrintExpr(Node->getTarget());
400   OS << ";";
401   if (Policy.IncludeNewlines) OS << NL;
402 }
403 
404 void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
405   Indent() << "continue;";
406   if (Policy.IncludeNewlines) OS << NL;
407 }
408 
409 void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
410   Indent() << "break;";
411   if (Policy.IncludeNewlines) OS << NL;
412 }
413 
414 void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
415   Indent() << "return";
416   if (Node->getRetValue()) {
417     OS << " ";
418     PrintExpr(Node->getRetValue());
419   }
420   OS << ";";
421   if (Policy.IncludeNewlines) OS << NL;
422 }
423 
424 void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
425   Indent() << "asm ";
426 
427   if (Node->isVolatile())
428     OS << "volatile ";
429 
430   if (Node->isAsmGoto())
431     OS << "goto ";
432 
433   OS << "(";
434   VisitStringLiteral(Node->getAsmString());
435 
436   // Outputs
437   if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
438       Node->getNumClobbers() != 0 || Node->getNumLabels() != 0)
439     OS << " : ";
440 
441   for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
442     if (i != 0)
443       OS << ", ";
444 
445     if (!Node->getOutputName(i).empty()) {
446       OS << '[';
447       OS << Node->getOutputName(i);
448       OS << "] ";
449     }
450 
451     VisitStringLiteral(Node->getOutputConstraintLiteral(i));
452     OS << " (";
453     Visit(Node->getOutputExpr(i));
454     OS << ")";
455   }
456 
457   // Inputs
458   if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0 ||
459       Node->getNumLabels() != 0)
460     OS << " : ";
461 
462   for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
463     if (i != 0)
464       OS << ", ";
465 
466     if (!Node->getInputName(i).empty()) {
467       OS << '[';
468       OS << Node->getInputName(i);
469       OS << "] ";
470     }
471 
472     VisitStringLiteral(Node->getInputConstraintLiteral(i));
473     OS << " (";
474     Visit(Node->getInputExpr(i));
475     OS << ")";
476   }
477 
478   // Clobbers
479   if (Node->getNumClobbers() != 0 || Node->getNumLabels())
480     OS << " : ";
481 
482   for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
483     if (i != 0)
484       OS << ", ";
485 
486     VisitStringLiteral(Node->getClobberStringLiteral(i));
487   }
488 
489   // Labels
490   if (Node->getNumLabels() != 0)
491     OS << " : ";
492 
493   for (unsigned i = 0, e = Node->getNumLabels(); i != e; ++i) {
494     if (i != 0)
495       OS << ", ";
496     OS << Node->getLabelName(i);
497   }
498 
499   OS << ");";
500   if (Policy.IncludeNewlines) OS << NL;
501 }
502 
503 void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
504   // FIXME: Implement MS style inline asm statement printer.
505   Indent() << "__asm ";
506   if (Node->hasBraces())
507     OS << "{" << NL;
508   OS << Node->getAsmString() << NL;
509   if (Node->hasBraces())
510     Indent() << "}" << NL;
511 }
512 
513 void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
514   PrintStmt(Node->getCapturedDecl()->getBody());
515 }
516 
517 void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
518   Indent() << "@try";
519   if (auto *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
520     PrintRawCompoundStmt(TS);
521     OS << NL;
522   }
523 
524   for (ObjCAtCatchStmt *catchStmt : Node->catch_stmts()) {
525     Indent() << "@catch(";
526     if (Decl *DS = catchStmt->getCatchParamDecl())
527       PrintRawDecl(DS);
528     OS << ")";
529     if (auto *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
530       PrintRawCompoundStmt(CS);
531       OS << NL;
532     }
533   }
534 
535   if (auto *FS = static_cast<ObjCAtFinallyStmt *>(Node->getFinallyStmt())) {
536     Indent() << "@finally";
537     PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
538     OS << NL;
539   }
540 }
541 
542 void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
543 }
544 
545 void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
546   Indent() << "@catch (...) { /* todo */ } " << NL;
547 }
548 
549 void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
550   Indent() << "@throw";
551   if (Node->getThrowExpr()) {
552     OS << " ";
553     PrintExpr(Node->getThrowExpr());
554   }
555   OS << ";" << NL;
556 }
557 
558 void StmtPrinter::VisitObjCAvailabilityCheckExpr(
559     ObjCAvailabilityCheckExpr *Node) {
560   OS << "@available(...)";
561 }
562 
563 void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
564   Indent() << "@synchronized (";
565   PrintExpr(Node->getSynchExpr());
566   OS << ")";
567   PrintRawCompoundStmt(Node->getSynchBody());
568   OS << NL;
569 }
570 
571 void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
572   Indent() << "@autoreleasepool";
573   PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
574   OS << NL;
575 }
576 
577 void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
578   OS << "catch (";
579   if (Decl *ExDecl = Node->getExceptionDecl())
580     PrintRawDecl(ExDecl);
581   else
582     OS << "...";
583   OS << ") ";
584   PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
585 }
586 
587 void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
588   Indent();
589   PrintRawCXXCatchStmt(Node);
590   OS << NL;
591 }
592 
593 void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
594   Indent() << "try ";
595   PrintRawCompoundStmt(Node->getTryBlock());
596   for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
597     OS << " ";
598     PrintRawCXXCatchStmt(Node->getHandler(i));
599   }
600   OS << NL;
601 }
602 
603 void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
604   Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
605   PrintRawCompoundStmt(Node->getTryBlock());
606   SEHExceptStmt *E = Node->getExceptHandler();
607   SEHFinallyStmt *F = Node->getFinallyHandler();
608   if(E)
609     PrintRawSEHExceptHandler(E);
610   else {
611     assert(F && "Must have a finally block...");
612     PrintRawSEHFinallyStmt(F);
613   }
614   OS << NL;
615 }
616 
617 void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
618   OS << "__finally ";
619   PrintRawCompoundStmt(Node->getBlock());
620   OS << NL;
621 }
622 
623 void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
624   OS << "__except (";
625   VisitExpr(Node->getFilterExpr());
626   OS << ")" << NL;
627   PrintRawCompoundStmt(Node->getBlock());
628   OS << NL;
629 }
630 
631 void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
632   Indent();
633   PrintRawSEHExceptHandler(Node);
634   OS << NL;
635 }
636 
637 void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
638   Indent();
639   PrintRawSEHFinallyStmt(Node);
640   OS << NL;
641 }
642 
643 void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) {
644   Indent() << "__leave;";
645   if (Policy.IncludeNewlines) OS << NL;
646 }
647 
648 //===----------------------------------------------------------------------===//
649 //  OpenMP directives printing methods
650 //===----------------------------------------------------------------------===//
651 
652 void StmtPrinter::VisitOMPCanonicalLoop(OMPCanonicalLoop *Node) {
653   PrintStmt(Node->getLoopStmt());
654 }
655 
656 void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S,
657                                               bool ForceNoStmt) {
658   OMPClausePrinter Printer(OS, Policy);
659   ArrayRef<OMPClause *> Clauses = S->clauses();
660   for (auto *Clause : Clauses)
661     if (Clause && !Clause->isImplicit()) {
662       OS << ' ';
663       Printer.Visit(Clause);
664     }
665   OS << NL;
666   if (!ForceNoStmt && S->hasAssociatedStmt())
667     PrintStmt(S->getRawStmt());
668 }
669 
670 void StmtPrinter::VisitOMPMetaDirective(OMPMetaDirective *Node) {
671   Indent() << "#pragma omp metadirective";
672   PrintOMPExecutableDirective(Node);
673 }
674 
675 void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
676   Indent() << "#pragma omp parallel";
677   PrintOMPExecutableDirective(Node);
678 }
679 
680 void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
681   Indent() << "#pragma omp simd";
682   PrintOMPExecutableDirective(Node);
683 }
684 
685 void StmtPrinter::VisitOMPTileDirective(OMPTileDirective *Node) {
686   Indent() << "#pragma omp tile";
687   PrintOMPExecutableDirective(Node);
688 }
689 
690 void StmtPrinter::VisitOMPUnrollDirective(OMPUnrollDirective *Node) {
691   Indent() << "#pragma omp unroll";
692   PrintOMPExecutableDirective(Node);
693 }
694 
695 void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
696   Indent() << "#pragma omp for";
697   PrintOMPExecutableDirective(Node);
698 }
699 
700 void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) {
701   Indent() << "#pragma omp for simd";
702   PrintOMPExecutableDirective(Node);
703 }
704 
705 void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
706   Indent() << "#pragma omp sections";
707   PrintOMPExecutableDirective(Node);
708 }
709 
710 void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
711   Indent() << "#pragma omp section";
712   PrintOMPExecutableDirective(Node);
713 }
714 
715 void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
716   Indent() << "#pragma omp single";
717   PrintOMPExecutableDirective(Node);
718 }
719 
720 void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
721   Indent() << "#pragma omp master";
722   PrintOMPExecutableDirective(Node);
723 }
724 
725 void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
726   Indent() << "#pragma omp critical";
727   if (Node->getDirectiveName().getName()) {
728     OS << " (";
729     Node->getDirectiveName().printName(OS, Policy);
730     OS << ")";
731   }
732   PrintOMPExecutableDirective(Node);
733 }
734 
735 void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
736   Indent() << "#pragma omp parallel for";
737   PrintOMPExecutableDirective(Node);
738 }
739 
740 void StmtPrinter::VisitOMPParallelForSimdDirective(
741     OMPParallelForSimdDirective *Node) {
742   Indent() << "#pragma omp parallel for simd";
743   PrintOMPExecutableDirective(Node);
744 }
745 
746 void StmtPrinter::VisitOMPParallelMasterDirective(
747     OMPParallelMasterDirective *Node) {
748   Indent() << "#pragma omp parallel master";
749   PrintOMPExecutableDirective(Node);
750 }
751 
752 void StmtPrinter::VisitOMPParallelSectionsDirective(
753     OMPParallelSectionsDirective *Node) {
754   Indent() << "#pragma omp parallel sections";
755   PrintOMPExecutableDirective(Node);
756 }
757 
758 void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
759   Indent() << "#pragma omp task";
760   PrintOMPExecutableDirective(Node);
761 }
762 
763 void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
764   Indent() << "#pragma omp taskyield";
765   PrintOMPExecutableDirective(Node);
766 }
767 
768 void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
769   Indent() << "#pragma omp barrier";
770   PrintOMPExecutableDirective(Node);
771 }
772 
773 void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
774   Indent() << "#pragma omp taskwait";
775   PrintOMPExecutableDirective(Node);
776 }
777 
778 void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) {
779   Indent() << "#pragma omp taskgroup";
780   PrintOMPExecutableDirective(Node);
781 }
782 
783 void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
784   Indent() << "#pragma omp flush";
785   PrintOMPExecutableDirective(Node);
786 }
787 
788 void StmtPrinter::VisitOMPDepobjDirective(OMPDepobjDirective *Node) {
789   Indent() << "#pragma omp depobj";
790   PrintOMPExecutableDirective(Node);
791 }
792 
793 void StmtPrinter::VisitOMPScanDirective(OMPScanDirective *Node) {
794   Indent() << "#pragma omp scan";
795   PrintOMPExecutableDirective(Node);
796 }
797 
798 void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
799   Indent() << "#pragma omp ordered";
800   PrintOMPExecutableDirective(Node, Node->hasClausesOfKind<OMPDependClause>());
801 }
802 
803 void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
804   Indent() << "#pragma omp atomic";
805   PrintOMPExecutableDirective(Node);
806 }
807 
808 void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
809   Indent() << "#pragma omp target";
810   PrintOMPExecutableDirective(Node);
811 }
812 
813 void StmtPrinter::VisitOMPTargetDataDirective(OMPTargetDataDirective *Node) {
814   Indent() << "#pragma omp target data";
815   PrintOMPExecutableDirective(Node);
816 }
817 
818 void StmtPrinter::VisitOMPTargetEnterDataDirective(
819     OMPTargetEnterDataDirective *Node) {
820   Indent() << "#pragma omp target enter data";
821   PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
822 }
823 
824 void StmtPrinter::VisitOMPTargetExitDataDirective(
825     OMPTargetExitDataDirective *Node) {
826   Indent() << "#pragma omp target exit data";
827   PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
828 }
829 
830 void StmtPrinter::VisitOMPTargetParallelDirective(
831     OMPTargetParallelDirective *Node) {
832   Indent() << "#pragma omp target parallel";
833   PrintOMPExecutableDirective(Node);
834 }
835 
836 void StmtPrinter::VisitOMPTargetParallelForDirective(
837     OMPTargetParallelForDirective *Node) {
838   Indent() << "#pragma omp target parallel for";
839   PrintOMPExecutableDirective(Node);
840 }
841 
842 void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) {
843   Indent() << "#pragma omp teams";
844   PrintOMPExecutableDirective(Node);
845 }
846 
847 void StmtPrinter::VisitOMPCancellationPointDirective(
848     OMPCancellationPointDirective *Node) {
849   Indent() << "#pragma omp cancellation point "
850            << getOpenMPDirectiveName(Node->getCancelRegion());
851   PrintOMPExecutableDirective(Node);
852 }
853 
854 void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) {
855   Indent() << "#pragma omp cancel "
856            << getOpenMPDirectiveName(Node->getCancelRegion());
857   PrintOMPExecutableDirective(Node);
858 }
859 
860 void StmtPrinter::VisitOMPTaskLoopDirective(OMPTaskLoopDirective *Node) {
861   Indent() << "#pragma omp taskloop";
862   PrintOMPExecutableDirective(Node);
863 }
864 
865 void StmtPrinter::VisitOMPTaskLoopSimdDirective(
866     OMPTaskLoopSimdDirective *Node) {
867   Indent() << "#pragma omp taskloop simd";
868   PrintOMPExecutableDirective(Node);
869 }
870 
871 void StmtPrinter::VisitOMPMasterTaskLoopDirective(
872     OMPMasterTaskLoopDirective *Node) {
873   Indent() << "#pragma omp master taskloop";
874   PrintOMPExecutableDirective(Node);
875 }
876 
877 void StmtPrinter::VisitOMPMasterTaskLoopSimdDirective(
878     OMPMasterTaskLoopSimdDirective *Node) {
879   Indent() << "#pragma omp master taskloop simd";
880   PrintOMPExecutableDirective(Node);
881 }
882 
883 void StmtPrinter::VisitOMPParallelMasterTaskLoopDirective(
884     OMPParallelMasterTaskLoopDirective *Node) {
885   Indent() << "#pragma omp parallel master taskloop";
886   PrintOMPExecutableDirective(Node);
887 }
888 
889 void StmtPrinter::VisitOMPParallelMasterTaskLoopSimdDirective(
890     OMPParallelMasterTaskLoopSimdDirective *Node) {
891   Indent() << "#pragma omp parallel master taskloop simd";
892   PrintOMPExecutableDirective(Node);
893 }
894 
895 void StmtPrinter::VisitOMPDistributeDirective(OMPDistributeDirective *Node) {
896   Indent() << "#pragma omp distribute";
897   PrintOMPExecutableDirective(Node);
898 }
899 
900 void StmtPrinter::VisitOMPTargetUpdateDirective(
901     OMPTargetUpdateDirective *Node) {
902   Indent() << "#pragma omp target update";
903   PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
904 }
905 
906 void StmtPrinter::VisitOMPDistributeParallelForDirective(
907     OMPDistributeParallelForDirective *Node) {
908   Indent() << "#pragma omp distribute parallel for";
909   PrintOMPExecutableDirective(Node);
910 }
911 
912 void StmtPrinter::VisitOMPDistributeParallelForSimdDirective(
913     OMPDistributeParallelForSimdDirective *Node) {
914   Indent() << "#pragma omp distribute parallel for simd";
915   PrintOMPExecutableDirective(Node);
916 }
917 
918 void StmtPrinter::VisitOMPDistributeSimdDirective(
919     OMPDistributeSimdDirective *Node) {
920   Indent() << "#pragma omp distribute simd";
921   PrintOMPExecutableDirective(Node);
922 }
923 
924 void StmtPrinter::VisitOMPTargetParallelForSimdDirective(
925     OMPTargetParallelForSimdDirective *Node) {
926   Indent() << "#pragma omp target parallel for simd";
927   PrintOMPExecutableDirective(Node);
928 }
929 
930 void StmtPrinter::VisitOMPTargetSimdDirective(OMPTargetSimdDirective *Node) {
931   Indent() << "#pragma omp target simd";
932   PrintOMPExecutableDirective(Node);
933 }
934 
935 void StmtPrinter::VisitOMPTeamsDistributeDirective(
936     OMPTeamsDistributeDirective *Node) {
937   Indent() << "#pragma omp teams distribute";
938   PrintOMPExecutableDirective(Node);
939 }
940 
941 void StmtPrinter::VisitOMPTeamsDistributeSimdDirective(
942     OMPTeamsDistributeSimdDirective *Node) {
943   Indent() << "#pragma omp teams distribute simd";
944   PrintOMPExecutableDirective(Node);
945 }
946 
947 void StmtPrinter::VisitOMPTeamsDistributeParallelForSimdDirective(
948     OMPTeamsDistributeParallelForSimdDirective *Node) {
949   Indent() << "#pragma omp teams distribute parallel for simd";
950   PrintOMPExecutableDirective(Node);
951 }
952 
953 void StmtPrinter::VisitOMPTeamsDistributeParallelForDirective(
954     OMPTeamsDistributeParallelForDirective *Node) {
955   Indent() << "#pragma omp teams distribute parallel for";
956   PrintOMPExecutableDirective(Node);
957 }
958 
959 void StmtPrinter::VisitOMPTargetTeamsDirective(OMPTargetTeamsDirective *Node) {
960   Indent() << "#pragma omp target teams";
961   PrintOMPExecutableDirective(Node);
962 }
963 
964 void StmtPrinter::VisitOMPTargetTeamsDistributeDirective(
965     OMPTargetTeamsDistributeDirective *Node) {
966   Indent() << "#pragma omp target teams distribute";
967   PrintOMPExecutableDirective(Node);
968 }
969 
970 void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForDirective(
971     OMPTargetTeamsDistributeParallelForDirective *Node) {
972   Indent() << "#pragma omp target teams distribute parallel for";
973   PrintOMPExecutableDirective(Node);
974 }
975 
976 void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForSimdDirective(
977     OMPTargetTeamsDistributeParallelForSimdDirective *Node) {
978   Indent() << "#pragma omp target teams distribute parallel for simd";
979   PrintOMPExecutableDirective(Node);
980 }
981 
982 void StmtPrinter::VisitOMPTargetTeamsDistributeSimdDirective(
983     OMPTargetTeamsDistributeSimdDirective *Node) {
984   Indent() << "#pragma omp target teams distribute simd";
985   PrintOMPExecutableDirective(Node);
986 }
987 
988 void StmtPrinter::VisitOMPInteropDirective(OMPInteropDirective *Node) {
989   Indent() << "#pragma omp interop";
990   PrintOMPExecutableDirective(Node);
991 }
992 
993 void StmtPrinter::VisitOMPDispatchDirective(OMPDispatchDirective *Node) {
994   Indent() << "#pragma omp dispatch";
995   PrintOMPExecutableDirective(Node);
996 }
997 
998 void StmtPrinter::VisitOMPMaskedDirective(OMPMaskedDirective *Node) {
999   Indent() << "#pragma omp masked";
1000   PrintOMPExecutableDirective(Node);
1001 }
1002 
1003 void StmtPrinter::VisitOMPGenericLoopDirective(OMPGenericLoopDirective *Node) {
1004   Indent() << "#pragma omp loop";
1005   PrintOMPExecutableDirective(Node);
1006 }
1007 
1008 //===----------------------------------------------------------------------===//
1009 //  Expr printing methods.
1010 //===----------------------------------------------------------------------===//
1011 
1012 void StmtPrinter::VisitSourceLocExpr(SourceLocExpr *Node) {
1013   OS << Node->getBuiltinStr() << "()";
1014 }
1015 
1016 void StmtPrinter::VisitConstantExpr(ConstantExpr *Node) {
1017   PrintExpr(Node->getSubExpr());
1018 }
1019 
1020 void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
1021   if (const auto *OCED = dyn_cast<OMPCapturedExprDecl>(Node->getDecl())) {
1022     OCED->getInit()->IgnoreImpCasts()->printPretty(OS, nullptr, Policy);
1023     return;
1024   }
1025   if (const auto *TPOD = dyn_cast<TemplateParamObjectDecl>(Node->getDecl())) {
1026     TPOD->printAsExpr(OS);
1027     return;
1028   }
1029   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1030     Qualifier->print(OS, Policy);
1031   if (Node->hasTemplateKeyword())
1032     OS << "template ";
1033   OS << Node->getNameInfo();
1034   if (Node->hasExplicitTemplateArgs()) {
1035     const TemplateParameterList *TPL = nullptr;
1036     if (!Node->hadMultipleCandidates())
1037       if (auto *TD = dyn_cast<TemplateDecl>(Node->getDecl()))
1038         TPL = TD->getTemplateParameters();
1039     printTemplateArgumentList(OS, Node->template_arguments(), Policy, TPL);
1040   }
1041 }
1042 
1043 void StmtPrinter::VisitDependentScopeDeclRefExpr(
1044                                            DependentScopeDeclRefExpr *Node) {
1045   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1046     Qualifier->print(OS, Policy);
1047   if (Node->hasTemplateKeyword())
1048     OS << "template ";
1049   OS << Node->getNameInfo();
1050   if (Node->hasExplicitTemplateArgs())
1051     printTemplateArgumentList(OS, Node->template_arguments(), Policy);
1052 }
1053 
1054 void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
1055   if (Node->getQualifier())
1056     Node->getQualifier()->print(OS, Policy);
1057   if (Node->hasTemplateKeyword())
1058     OS << "template ";
1059   OS << Node->getNameInfo();
1060   if (Node->hasExplicitTemplateArgs())
1061     printTemplateArgumentList(OS, Node->template_arguments(), Policy);
1062 }
1063 
1064 static bool isImplicitSelf(const Expr *E) {
1065   if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
1066     if (const auto *PD = dyn_cast<ImplicitParamDecl>(DRE->getDecl())) {
1067       if (PD->getParameterKind() == ImplicitParamDecl::ObjCSelf &&
1068           DRE->getBeginLoc().isInvalid())
1069         return true;
1070     }
1071   }
1072   return false;
1073 }
1074 
1075 void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
1076   if (Node->getBase()) {
1077     if (!Policy.SuppressImplicitBase ||
1078         !isImplicitSelf(Node->getBase()->IgnoreImpCasts())) {
1079       PrintExpr(Node->getBase());
1080       OS << (Node->isArrow() ? "->" : ".");
1081     }
1082   }
1083   OS << *Node->getDecl();
1084 }
1085 
1086 void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
1087   if (Node->isSuperReceiver())
1088     OS << "super.";
1089   else if (Node->isObjectReceiver() && Node->getBase()) {
1090     PrintExpr(Node->getBase());
1091     OS << ".";
1092   } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
1093     OS << Node->getClassReceiver()->getName() << ".";
1094   }
1095 
1096   if (Node->isImplicitProperty()) {
1097     if (const auto *Getter = Node->getImplicitPropertyGetter())
1098       Getter->getSelector().print(OS);
1099     else
1100       OS << SelectorTable::getPropertyNameFromSetterSelector(
1101           Node->getImplicitPropertySetter()->getSelector());
1102   } else
1103     OS << Node->getExplicitProperty()->getName();
1104 }
1105 
1106 void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
1107   PrintExpr(Node->getBaseExpr());
1108   OS << "[";
1109   PrintExpr(Node->getKeyExpr());
1110   OS << "]";
1111 }
1112 
1113 void StmtPrinter::VisitSYCLUniqueStableNameExpr(
1114     SYCLUniqueStableNameExpr *Node) {
1115   OS << "__builtin_sycl_unique_stable_name(";
1116   Node->getTypeSourceInfo()->getType().print(OS, Policy);
1117   OS << ")";
1118 }
1119 
1120 void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
1121   OS << PredefinedExpr::getIdentKindName(Node->getIdentKind());
1122 }
1123 
1124 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
1125   CharacterLiteral::print(Node->getValue(), Node->getKind(), OS);
1126 }
1127 
1128 /// Prints the given expression using the original source text. Returns true on
1129 /// success, false otherwise.
1130 static bool printExprAsWritten(raw_ostream &OS, Expr *E,
1131                                const ASTContext *Context) {
1132   if (!Context)
1133     return false;
1134   bool Invalid = false;
1135   StringRef Source = Lexer::getSourceText(
1136       CharSourceRange::getTokenRange(E->getSourceRange()),
1137       Context->getSourceManager(), Context->getLangOpts(), &Invalid);
1138   if (!Invalid) {
1139     OS << Source;
1140     return true;
1141   }
1142   return false;
1143 }
1144 
1145 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
1146   if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1147     return;
1148   bool isSigned = Node->getType()->isSignedIntegerType();
1149   OS << toString(Node->getValue(), 10, isSigned);
1150 
1151   // Emit suffixes.  Integer literals are always a builtin integer type.
1152   switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1153   default: llvm_unreachable("Unexpected type for integer literal!");
1154   case BuiltinType::Char_S:
1155   case BuiltinType::Char_U:    OS << "i8"; break;
1156   case BuiltinType::UChar:     OS << "Ui8"; break;
1157   case BuiltinType::Short:     OS << "i16"; break;
1158   case BuiltinType::UShort:    OS << "Ui16"; break;
1159   case BuiltinType::Int:       break; // no suffix.
1160   case BuiltinType::UInt:      OS << 'U'; break;
1161   case BuiltinType::Long:      OS << 'L'; break;
1162   case BuiltinType::ULong:     OS << "UL"; break;
1163   case BuiltinType::LongLong:  OS << "LL"; break;
1164   case BuiltinType::ULongLong: OS << "ULL"; break;
1165   case BuiltinType::Int128:
1166     break; // no suffix.
1167   case BuiltinType::UInt128:
1168     break; // no suffix.
1169   }
1170 }
1171 
1172 void StmtPrinter::VisitFixedPointLiteral(FixedPointLiteral *Node) {
1173   if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1174     return;
1175   OS << Node->getValueAsString(/*Radix=*/10);
1176 
1177   switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1178     default: llvm_unreachable("Unexpected type for fixed point literal!");
1179     case BuiltinType::ShortFract:   OS << "hr"; break;
1180     case BuiltinType::ShortAccum:   OS << "hk"; break;
1181     case BuiltinType::UShortFract:  OS << "uhr"; break;
1182     case BuiltinType::UShortAccum:  OS << "uhk"; break;
1183     case BuiltinType::Fract:        OS << "r"; break;
1184     case BuiltinType::Accum:        OS << "k"; break;
1185     case BuiltinType::UFract:       OS << "ur"; break;
1186     case BuiltinType::UAccum:       OS << "uk"; break;
1187     case BuiltinType::LongFract:    OS << "lr"; break;
1188     case BuiltinType::LongAccum:    OS << "lk"; break;
1189     case BuiltinType::ULongFract:   OS << "ulr"; break;
1190     case BuiltinType::ULongAccum:   OS << "ulk"; break;
1191   }
1192 }
1193 
1194 static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1195                                  bool PrintSuffix) {
1196   SmallString<16> Str;
1197   Node->getValue().toString(Str);
1198   OS << Str;
1199   if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1200     OS << '.'; // Trailing dot in order to separate from ints.
1201 
1202   if (!PrintSuffix)
1203     return;
1204 
1205   // Emit suffixes.  Float literals are always a builtin float type.
1206   switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1207   default: llvm_unreachable("Unexpected type for float literal!");
1208   case BuiltinType::Half:       break; // FIXME: suffix?
1209   case BuiltinType::Ibm128:     break; // FIXME: No suffix for ibm128 literal
1210   case BuiltinType::Double:     break; // no suffix.
1211   case BuiltinType::Float16:    OS << "F16"; break;
1212   case BuiltinType::Float:      OS << 'F'; break;
1213   case BuiltinType::LongDouble: OS << 'L'; break;
1214   case BuiltinType::Float128:   OS << 'Q'; break;
1215   }
1216 }
1217 
1218 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
1219   if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1220     return;
1221   PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
1222 }
1223 
1224 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1225   PrintExpr(Node->getSubExpr());
1226   OS << "i";
1227 }
1228 
1229 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
1230   Str->outputString(OS);
1231 }
1232 
1233 void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1234   OS << "(";
1235   PrintExpr(Node->getSubExpr());
1236   OS << ")";
1237 }
1238 
1239 void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
1240   if (!Node->isPostfix()) {
1241     OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1242 
1243     // Print a space if this is an "identifier operator" like __real, or if
1244     // it might be concatenated incorrectly like '+'.
1245     switch (Node->getOpcode()) {
1246     default: break;
1247     case UO_Real:
1248     case UO_Imag:
1249     case UO_Extension:
1250       OS << ' ';
1251       break;
1252     case UO_Plus:
1253     case UO_Minus:
1254       if (isa<UnaryOperator>(Node->getSubExpr()))
1255         OS << ' ';
1256       break;
1257     }
1258   }
1259   PrintExpr(Node->getSubExpr());
1260 
1261   if (Node->isPostfix())
1262     OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1263 }
1264 
1265 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1266   OS << "__builtin_offsetof(";
1267   Node->getTypeSourceInfo()->getType().print(OS, Policy);
1268   OS << ", ";
1269   bool PrintedSomething = false;
1270   for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
1271     OffsetOfNode ON = Node->getComponent(i);
1272     if (ON.getKind() == OffsetOfNode::Array) {
1273       // Array node
1274       OS << "[";
1275       PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1276       OS << "]";
1277       PrintedSomething = true;
1278       continue;
1279     }
1280 
1281     // Skip implicit base indirections.
1282     if (ON.getKind() == OffsetOfNode::Base)
1283       continue;
1284 
1285     // Field or identifier node.
1286     IdentifierInfo *Id = ON.getFieldName();
1287     if (!Id)
1288       continue;
1289 
1290     if (PrintedSomething)
1291       OS << ".";
1292     else
1293       PrintedSomething = true;
1294     OS << Id->getName();
1295   }
1296   OS << ")";
1297 }
1298 
1299 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(
1300     UnaryExprOrTypeTraitExpr *Node) {
1301   const char *Spelling = getTraitSpelling(Node->getKind());
1302   if (Node->getKind() == UETT_AlignOf) {
1303     if (Policy.Alignof)
1304       Spelling = "alignof";
1305     else if (Policy.UnderscoreAlignof)
1306       Spelling = "_Alignof";
1307     else
1308       Spelling = "__alignof";
1309   }
1310 
1311   OS << Spelling;
1312 
1313   if (Node->isArgumentType()) {
1314     OS << '(';
1315     Node->getArgumentType().print(OS, Policy);
1316     OS << ')';
1317   } else {
1318     OS << " ";
1319     PrintExpr(Node->getArgumentExpr());
1320   }
1321 }
1322 
1323 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1324   OS << "_Generic(";
1325   PrintExpr(Node->getControllingExpr());
1326   for (const GenericSelectionExpr::Association Assoc : Node->associations()) {
1327     OS << ", ";
1328     QualType T = Assoc.getType();
1329     if (T.isNull())
1330       OS << "default";
1331     else
1332       T.print(OS, Policy);
1333     OS << ": ";
1334     PrintExpr(Assoc.getAssociationExpr());
1335   }
1336   OS << ")";
1337 }
1338 
1339 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
1340   PrintExpr(Node->getLHS());
1341   OS << "[";
1342   PrintExpr(Node->getRHS());
1343   OS << "]";
1344 }
1345 
1346 void StmtPrinter::VisitMatrixSubscriptExpr(MatrixSubscriptExpr *Node) {
1347   PrintExpr(Node->getBase());
1348   OS << "[";
1349   PrintExpr(Node->getRowIdx());
1350   OS << "]";
1351   OS << "[";
1352   PrintExpr(Node->getColumnIdx());
1353   OS << "]";
1354 }
1355 
1356 void StmtPrinter::VisitOMPArraySectionExpr(OMPArraySectionExpr *Node) {
1357   PrintExpr(Node->getBase());
1358   OS << "[";
1359   if (Node->getLowerBound())
1360     PrintExpr(Node->getLowerBound());
1361   if (Node->getColonLocFirst().isValid()) {
1362     OS << ":";
1363     if (Node->getLength())
1364       PrintExpr(Node->getLength());
1365   }
1366   if (Node->getColonLocSecond().isValid()) {
1367     OS << ":";
1368     if (Node->getStride())
1369       PrintExpr(Node->getStride());
1370   }
1371   OS << "]";
1372 }
1373 
1374 void StmtPrinter::VisitOMPArrayShapingExpr(OMPArrayShapingExpr *Node) {
1375   OS << "(";
1376   for (Expr *E : Node->getDimensions()) {
1377     OS << "[";
1378     PrintExpr(E);
1379     OS << "]";
1380   }
1381   OS << ")";
1382   PrintExpr(Node->getBase());
1383 }
1384 
1385 void StmtPrinter::VisitOMPIteratorExpr(OMPIteratorExpr *Node) {
1386   OS << "iterator(";
1387   for (unsigned I = 0, E = Node->numOfIterators(); I < E; ++I) {
1388     auto *VD = cast<ValueDecl>(Node->getIteratorDecl(I));
1389     VD->getType().print(OS, Policy);
1390     const OMPIteratorExpr::IteratorRange Range = Node->getIteratorRange(I);
1391     OS << " " << VD->getName() << " = ";
1392     PrintExpr(Range.Begin);
1393     OS << ":";
1394     PrintExpr(Range.End);
1395     if (Range.Step) {
1396       OS << ":";
1397       PrintExpr(Range.Step);
1398     }
1399     if (I < E - 1)
1400       OS << ", ";
1401   }
1402   OS << ")";
1403 }
1404 
1405 void StmtPrinter::PrintCallArgs(CallExpr *Call) {
1406   for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
1407     if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1408       // Don't print any defaulted arguments
1409       break;
1410     }
1411 
1412     if (i) OS << ", ";
1413     PrintExpr(Call->getArg(i));
1414   }
1415 }
1416 
1417 void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1418   PrintExpr(Call->getCallee());
1419   OS << "(";
1420   PrintCallArgs(Call);
1421   OS << ")";
1422 }
1423 
1424 static bool isImplicitThis(const Expr *E) {
1425   if (const auto *TE = dyn_cast<CXXThisExpr>(E))
1426     return TE->isImplicit();
1427   return false;
1428 }
1429 
1430 void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
1431   if (!Policy.SuppressImplicitBase || !isImplicitThis(Node->getBase())) {
1432     PrintExpr(Node->getBase());
1433 
1434     auto *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
1435     FieldDecl *ParentDecl =
1436         ParentMember ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl())
1437                      : nullptr;
1438 
1439     if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
1440       OS << (Node->isArrow() ? "->" : ".");
1441   }
1442 
1443   if (auto *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1444     if (FD->isAnonymousStructOrUnion())
1445       return;
1446 
1447   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1448     Qualifier->print(OS, Policy);
1449   if (Node->hasTemplateKeyword())
1450     OS << "template ";
1451   OS << Node->getMemberNameInfo();
1452   const TemplateParameterList *TPL = nullptr;
1453   if (auto *FD = dyn_cast<FunctionDecl>(Node->getMemberDecl())) {
1454     if (!Node->hadMultipleCandidates())
1455       if (auto *FTD = FD->getPrimaryTemplate())
1456         TPL = FTD->getTemplateParameters();
1457   } else if (auto *VTSD =
1458                  dyn_cast<VarTemplateSpecializationDecl>(Node->getMemberDecl()))
1459     TPL = VTSD->getSpecializedTemplate()->getTemplateParameters();
1460   if (Node->hasExplicitTemplateArgs())
1461     printTemplateArgumentList(OS, Node->template_arguments(), Policy, TPL);
1462 }
1463 
1464 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1465   PrintExpr(Node->getBase());
1466   OS << (Node->isArrow() ? "->isa" : ".isa");
1467 }
1468 
1469 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
1470   PrintExpr(Node->getBase());
1471   OS << ".";
1472   OS << Node->getAccessor().getName();
1473 }
1474 
1475 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
1476   OS << '(';
1477   Node->getTypeAsWritten().print(OS, Policy);
1478   OS << ')';
1479   PrintExpr(Node->getSubExpr());
1480 }
1481 
1482 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
1483   OS << '(';
1484   Node->getType().print(OS, Policy);
1485   OS << ')';
1486   PrintExpr(Node->getInitializer());
1487 }
1488 
1489 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
1490   // No need to print anything, simply forward to the subexpression.
1491   PrintExpr(Node->getSubExpr());
1492 }
1493 
1494 void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1495   PrintExpr(Node->getLHS());
1496   OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1497   PrintExpr(Node->getRHS());
1498 }
1499 
1500 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1501   PrintExpr(Node->getLHS());
1502   OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1503   PrintExpr(Node->getRHS());
1504 }
1505 
1506 void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1507   PrintExpr(Node->getCond());
1508   OS << " ? ";
1509   PrintExpr(Node->getLHS());
1510   OS << " : ";
1511   PrintExpr(Node->getRHS());
1512 }
1513 
1514 // GNU extensions.
1515 
1516 void
1517 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1518   PrintExpr(Node->getCommon());
1519   OS << " ?: ";
1520   PrintExpr(Node->getFalseExpr());
1521 }
1522 
1523 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
1524   OS << "&&" << Node->getLabel()->getName();
1525 }
1526 
1527 void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1528   OS << "(";
1529   PrintRawCompoundStmt(E->getSubStmt());
1530   OS << ")";
1531 }
1532 
1533 void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1534   OS << "__builtin_choose_expr(";
1535   PrintExpr(Node->getCond());
1536   OS << ", ";
1537   PrintExpr(Node->getLHS());
1538   OS << ", ";
1539   PrintExpr(Node->getRHS());
1540   OS << ")";
1541 }
1542 
1543 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1544   OS << "__null";
1545 }
1546 
1547 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1548   OS << "__builtin_shufflevector(";
1549   for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1550     if (i) OS << ", ";
1551     PrintExpr(Node->getExpr(i));
1552   }
1553   OS << ")";
1554 }
1555 
1556 void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1557   OS << "__builtin_convertvector(";
1558   PrintExpr(Node->getSrcExpr());
1559   OS << ", ";
1560   Node->getType().print(OS, Policy);
1561   OS << ")";
1562 }
1563 
1564 void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
1565   if (Node->getSyntacticForm()) {
1566     Visit(Node->getSyntacticForm());
1567     return;
1568   }
1569 
1570   OS << "{";
1571   for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1572     if (i) OS << ", ";
1573     if (Node->getInit(i))
1574       PrintExpr(Node->getInit(i));
1575     else
1576       OS << "{}";
1577   }
1578   OS << "}";
1579 }
1580 
1581 void StmtPrinter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *Node) {
1582   // There's no way to express this expression in any of our supported
1583   // languages, so just emit something terse and (hopefully) clear.
1584   OS << "{";
1585   PrintExpr(Node->getSubExpr());
1586   OS << "}";
1587 }
1588 
1589 void StmtPrinter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *Node) {
1590   OS << "*";
1591 }
1592 
1593 void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1594   OS << "(";
1595   for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1596     if (i) OS << ", ";
1597     PrintExpr(Node->getExpr(i));
1598   }
1599   OS << ")";
1600 }
1601 
1602 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
1603   bool NeedsEquals = true;
1604   for (const DesignatedInitExpr::Designator &D : Node->designators()) {
1605     if (D.isFieldDesignator()) {
1606       if (D.getDotLoc().isInvalid()) {
1607         if (IdentifierInfo *II = D.getFieldName()) {
1608           OS << II->getName() << ":";
1609           NeedsEquals = false;
1610         }
1611       } else {
1612         OS << "." << D.getFieldName()->getName();
1613       }
1614     } else {
1615       OS << "[";
1616       if (D.isArrayDesignator()) {
1617         PrintExpr(Node->getArrayIndex(D));
1618       } else {
1619         PrintExpr(Node->getArrayRangeStart(D));
1620         OS << " ... ";
1621         PrintExpr(Node->getArrayRangeEnd(D));
1622       }
1623       OS << "]";
1624     }
1625   }
1626 
1627   if (NeedsEquals)
1628     OS << " = ";
1629   else
1630     OS << " ";
1631   PrintExpr(Node->getInit());
1632 }
1633 
1634 void StmtPrinter::VisitDesignatedInitUpdateExpr(
1635     DesignatedInitUpdateExpr *Node) {
1636   OS << "{";
1637   OS << "/*base*/";
1638   PrintExpr(Node->getBase());
1639   OS << ", ";
1640 
1641   OS << "/*updater*/";
1642   PrintExpr(Node->getUpdater());
1643   OS << "}";
1644 }
1645 
1646 void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
1647   OS << "/*no init*/";
1648 }
1649 
1650 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
1651   if (Node->getType()->getAsCXXRecordDecl()) {
1652     OS << "/*implicit*/";
1653     Node->getType().print(OS, Policy);
1654     OS << "()";
1655   } else {
1656     OS << "/*implicit*/(";
1657     Node->getType().print(OS, Policy);
1658     OS << ')';
1659     if (Node->getType()->isRecordType())
1660       OS << "{}";
1661     else
1662       OS << 0;
1663   }
1664 }
1665 
1666 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
1667   OS << "__builtin_va_arg(";
1668   PrintExpr(Node->getSubExpr());
1669   OS << ", ";
1670   Node->getType().print(OS, Policy);
1671   OS << ")";
1672 }
1673 
1674 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1675   PrintExpr(Node->getSyntacticForm());
1676 }
1677 
1678 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
1679   const char *Name = nullptr;
1680   switch (Node->getOp()) {
1681 #define BUILTIN(ID, TYPE, ATTRS)
1682 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1683   case AtomicExpr::AO ## ID: \
1684     Name = #ID "("; \
1685     break;
1686 #include "clang/Basic/Builtins.def"
1687   }
1688   OS << Name;
1689 
1690   // AtomicExpr stores its subexpressions in a permuted order.
1691   PrintExpr(Node->getPtr());
1692   if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1693       Node->getOp() != AtomicExpr::AO__atomic_load_n &&
1694       Node->getOp() != AtomicExpr::AO__opencl_atomic_load) {
1695     OS << ", ";
1696     PrintExpr(Node->getVal1());
1697   }
1698   if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1699       Node->isCmpXChg()) {
1700     OS << ", ";
1701     PrintExpr(Node->getVal2());
1702   }
1703   if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1704       Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1705     OS << ", ";
1706     PrintExpr(Node->getWeak());
1707   }
1708   if (Node->getOp() != AtomicExpr::AO__c11_atomic_init &&
1709       Node->getOp() != AtomicExpr::AO__opencl_atomic_init) {
1710     OS << ", ";
1711     PrintExpr(Node->getOrder());
1712   }
1713   if (Node->isCmpXChg()) {
1714     OS << ", ";
1715     PrintExpr(Node->getOrderFail());
1716   }
1717   OS << ")";
1718 }
1719 
1720 // C++
1721 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1722   OverloadedOperatorKind Kind = Node->getOperator();
1723   if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1724     if (Node->getNumArgs() == 1) {
1725       OS << getOperatorSpelling(Kind) << ' ';
1726       PrintExpr(Node->getArg(0));
1727     } else {
1728       PrintExpr(Node->getArg(0));
1729       OS << ' ' << getOperatorSpelling(Kind);
1730     }
1731   } else if (Kind == OO_Arrow) {
1732     PrintExpr(Node->getArg(0));
1733   } else if (Kind == OO_Call) {
1734     PrintExpr(Node->getArg(0));
1735     OS << '(';
1736     for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1737       if (ArgIdx > 1)
1738         OS << ", ";
1739       if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1740         PrintExpr(Node->getArg(ArgIdx));
1741     }
1742     OS << ')';
1743   } else if (Kind == OO_Subscript) {
1744     PrintExpr(Node->getArg(0));
1745     OS << '[';
1746     PrintExpr(Node->getArg(1));
1747     OS << ']';
1748   } else if (Node->getNumArgs() == 1) {
1749     OS << getOperatorSpelling(Kind) << ' ';
1750     PrintExpr(Node->getArg(0));
1751   } else if (Node->getNumArgs() == 2) {
1752     PrintExpr(Node->getArg(0));
1753     OS << ' ' << getOperatorSpelling(Kind) << ' ';
1754     PrintExpr(Node->getArg(1));
1755   } else {
1756     llvm_unreachable("unknown overloaded operator");
1757   }
1758 }
1759 
1760 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1761   // If we have a conversion operator call only print the argument.
1762   CXXMethodDecl *MD = Node->getMethodDecl();
1763   if (MD && isa<CXXConversionDecl>(MD)) {
1764     PrintExpr(Node->getImplicitObjectArgument());
1765     return;
1766   }
1767   VisitCallExpr(cast<CallExpr>(Node));
1768 }
1769 
1770 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1771   PrintExpr(Node->getCallee());
1772   OS << "<<<";
1773   PrintCallArgs(Node->getConfig());
1774   OS << ">>>(";
1775   PrintCallArgs(Node);
1776   OS << ")";
1777 }
1778 
1779 void StmtPrinter::VisitCXXRewrittenBinaryOperator(
1780     CXXRewrittenBinaryOperator *Node) {
1781   CXXRewrittenBinaryOperator::DecomposedForm Decomposed =
1782       Node->getDecomposedForm();
1783   PrintExpr(const_cast<Expr*>(Decomposed.LHS));
1784   OS << ' ' << BinaryOperator::getOpcodeStr(Decomposed.Opcode) << ' ';
1785   PrintExpr(const_cast<Expr*>(Decomposed.RHS));
1786 }
1787 
1788 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1789   OS << Node->getCastName() << '<';
1790   Node->getTypeAsWritten().print(OS, Policy);
1791   OS << ">(";
1792   PrintExpr(Node->getSubExpr());
1793   OS << ")";
1794 }
1795 
1796 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1797   VisitCXXNamedCastExpr(Node);
1798 }
1799 
1800 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1801   VisitCXXNamedCastExpr(Node);
1802 }
1803 
1804 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1805   VisitCXXNamedCastExpr(Node);
1806 }
1807 
1808 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1809   VisitCXXNamedCastExpr(Node);
1810 }
1811 
1812 void StmtPrinter::VisitBuiltinBitCastExpr(BuiltinBitCastExpr *Node) {
1813   OS << "__builtin_bit_cast(";
1814   Node->getTypeInfoAsWritten()->getType().print(OS, Policy);
1815   OS << ", ";
1816   PrintExpr(Node->getSubExpr());
1817   OS << ")";
1818 }
1819 
1820 void StmtPrinter::VisitCXXAddrspaceCastExpr(CXXAddrspaceCastExpr *Node) {
1821   VisitCXXNamedCastExpr(Node);
1822 }
1823 
1824 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1825   OS << "typeid(";
1826   if (Node->isTypeOperand()) {
1827     Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1828   } else {
1829     PrintExpr(Node->getExprOperand());
1830   }
1831   OS << ")";
1832 }
1833 
1834 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1835   OS << "__uuidof(";
1836   if (Node->isTypeOperand()) {
1837     Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1838   } else {
1839     PrintExpr(Node->getExprOperand());
1840   }
1841   OS << ")";
1842 }
1843 
1844 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1845   PrintExpr(Node->getBaseExpr());
1846   if (Node->isArrow())
1847     OS << "->";
1848   else
1849     OS << ".";
1850   if (NestedNameSpecifier *Qualifier =
1851       Node->getQualifierLoc().getNestedNameSpecifier())
1852     Qualifier->print(OS, Policy);
1853   OS << Node->getPropertyDecl()->getDeclName();
1854 }
1855 
1856 void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) {
1857   PrintExpr(Node->getBase());
1858   OS << "[";
1859   PrintExpr(Node->getIdx());
1860   OS << "]";
1861 }
1862 
1863 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1864   switch (Node->getLiteralOperatorKind()) {
1865   case UserDefinedLiteral::LOK_Raw:
1866     OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
1867     break;
1868   case UserDefinedLiteral::LOK_Template: {
1869     const auto *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1870     const TemplateArgumentList *Args =
1871       cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1872     assert(Args);
1873 
1874     if (Args->size() != 1) {
1875       const TemplateParameterList *TPL = nullptr;
1876       if (!DRE->hadMultipleCandidates())
1877         if (const auto *TD = dyn_cast<TemplateDecl>(DRE->getDecl()))
1878           TPL = TD->getTemplateParameters();
1879       OS << "operator\"\"" << Node->getUDSuffix()->getName();
1880       printTemplateArgumentList(OS, Args->asArray(), Policy, TPL);
1881       OS << "()";
1882       return;
1883     }
1884 
1885     const TemplateArgument &Pack = Args->get(0);
1886     for (const auto &P : Pack.pack_elements()) {
1887       char C = (char)P.getAsIntegral().getZExtValue();
1888       OS << C;
1889     }
1890     break;
1891   }
1892   case UserDefinedLiteral::LOK_Integer: {
1893     // Print integer literal without suffix.
1894     const auto *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1895     OS << toString(Int->getValue(), 10, /*isSigned*/false);
1896     break;
1897   }
1898   case UserDefinedLiteral::LOK_Floating: {
1899     // Print floating literal without suffix.
1900     auto *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1901     PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1902     break;
1903   }
1904   case UserDefinedLiteral::LOK_String:
1905   case UserDefinedLiteral::LOK_Character:
1906     PrintExpr(Node->getCookedLiteral());
1907     break;
1908   }
1909   OS << Node->getUDSuffix()->getName();
1910 }
1911 
1912 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1913   OS << (Node->getValue() ? "true" : "false");
1914 }
1915 
1916 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1917   OS << "nullptr";
1918 }
1919 
1920 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1921   OS << "this";
1922 }
1923 
1924 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1925   if (!Node->getSubExpr())
1926     OS << "throw";
1927   else {
1928     OS << "throw ";
1929     PrintExpr(Node->getSubExpr());
1930   }
1931 }
1932 
1933 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1934   // Nothing to print: we picked up the default argument.
1935 }
1936 
1937 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1938   // Nothing to print: we picked up the default initializer.
1939 }
1940 
1941 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1942   Node->getType().print(OS, Policy);
1943   // If there are no parens, this is list-initialization, and the braces are
1944   // part of the syntax of the inner construct.
1945   if (Node->getLParenLoc().isValid())
1946     OS << "(";
1947   PrintExpr(Node->getSubExpr());
1948   if (Node->getLParenLoc().isValid())
1949     OS << ")";
1950 }
1951 
1952 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1953   PrintExpr(Node->getSubExpr());
1954 }
1955 
1956 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1957   Node->getType().print(OS, Policy);
1958   if (Node->isStdInitListInitialization())
1959     /* Nothing to do; braces are part of creating the std::initializer_list. */;
1960   else if (Node->isListInitialization())
1961     OS << "{";
1962   else
1963     OS << "(";
1964   for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1965                                          ArgEnd = Node->arg_end();
1966        Arg != ArgEnd; ++Arg) {
1967     if ((*Arg)->isDefaultArgument())
1968       break;
1969     if (Arg != Node->arg_begin())
1970       OS << ", ";
1971     PrintExpr(*Arg);
1972   }
1973   if (Node->isStdInitListInitialization())
1974     /* See above. */;
1975   else if (Node->isListInitialization())
1976     OS << "}";
1977   else
1978     OS << ")";
1979 }
1980 
1981 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1982   OS << '[';
1983   bool NeedComma = false;
1984   switch (Node->getCaptureDefault()) {
1985   case LCD_None:
1986     break;
1987 
1988   case LCD_ByCopy:
1989     OS << '=';
1990     NeedComma = true;
1991     break;
1992 
1993   case LCD_ByRef:
1994     OS << '&';
1995     NeedComma = true;
1996     break;
1997   }
1998   for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1999                                  CEnd = Node->explicit_capture_end();
2000        C != CEnd;
2001        ++C) {
2002     if (C->capturesVLAType())
2003       continue;
2004 
2005     if (NeedComma)
2006       OS << ", ";
2007     NeedComma = true;
2008 
2009     switch (C->getCaptureKind()) {
2010     case LCK_This:
2011       OS << "this";
2012       break;
2013 
2014     case LCK_StarThis:
2015       OS << "*this";
2016       break;
2017 
2018     case LCK_ByRef:
2019       if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C))
2020         OS << '&';
2021       OS << C->getCapturedVar()->getName();
2022       break;
2023 
2024     case LCK_ByCopy:
2025       OS << C->getCapturedVar()->getName();
2026       break;
2027 
2028     case LCK_VLAType:
2029       llvm_unreachable("VLA type in explicit captures.");
2030     }
2031 
2032     if (C->isPackExpansion())
2033       OS << "...";
2034 
2035     if (Node->isInitCapture(C)) {
2036       VarDecl *D = C->getCapturedVar();
2037 
2038       llvm::StringRef Pre;
2039       llvm::StringRef Post;
2040       if (D->getInitStyle() == VarDecl::CallInit &&
2041           !isa<ParenListExpr>(D->getInit())) {
2042         Pre = "(";
2043         Post = ")";
2044       } else if (D->getInitStyle() == VarDecl::CInit) {
2045         Pre = " = ";
2046       }
2047 
2048       OS << Pre;
2049       PrintExpr(D->getInit());
2050       OS << Post;
2051     }
2052   }
2053   OS << ']';
2054 
2055   if (!Node->getExplicitTemplateParameters().empty()) {
2056     Node->getTemplateParameterList()->print(
2057         OS, Node->getLambdaClass()->getASTContext(),
2058         /*OmitTemplateKW*/true);
2059   }
2060 
2061   if (Node->hasExplicitParameters()) {
2062     OS << '(';
2063     CXXMethodDecl *Method = Node->getCallOperator();
2064     NeedComma = false;
2065     for (const auto *P : Method->parameters()) {
2066       if (NeedComma) {
2067         OS << ", ";
2068       } else {
2069         NeedComma = true;
2070       }
2071       std::string ParamStr = P->getNameAsString();
2072       P->getOriginalType().print(OS, Policy, ParamStr);
2073     }
2074     if (Method->isVariadic()) {
2075       if (NeedComma)
2076         OS << ", ";
2077       OS << "...";
2078     }
2079     OS << ')';
2080 
2081     if (Node->isMutable())
2082       OS << " mutable";
2083 
2084     auto *Proto = Method->getType()->castAs<FunctionProtoType>();
2085     Proto->printExceptionSpecification(OS, Policy);
2086 
2087     // FIXME: Attributes
2088 
2089     // Print the trailing return type if it was specified in the source.
2090     if (Node->hasExplicitResultType()) {
2091       OS << " -> ";
2092       Proto->getReturnType().print(OS, Policy);
2093     }
2094   }
2095 
2096   // Print the body.
2097   OS << ' ';
2098   if (Policy.TerseOutput)
2099     OS << "{}";
2100   else
2101     PrintRawCompoundStmt(Node->getCompoundStmtBody());
2102 }
2103 
2104 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
2105   if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
2106     TSInfo->getType().print(OS, Policy);
2107   else
2108     Node->getType().print(OS, Policy);
2109   OS << "()";
2110 }
2111 
2112 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
2113   if (E->isGlobalNew())
2114     OS << "::";
2115   OS << "new ";
2116   unsigned NumPlace = E->getNumPlacementArgs();
2117   if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
2118     OS << "(";
2119     PrintExpr(E->getPlacementArg(0));
2120     for (unsigned i = 1; i < NumPlace; ++i) {
2121       if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
2122         break;
2123       OS << ", ";
2124       PrintExpr(E->getPlacementArg(i));
2125     }
2126     OS << ") ";
2127   }
2128   if (E->isParenTypeId())
2129     OS << "(";
2130   std::string TypeS;
2131   if (Optional<Expr *> Size = E->getArraySize()) {
2132     llvm::raw_string_ostream s(TypeS);
2133     s << '[';
2134     if (*Size)
2135       (*Size)->printPretty(s, Helper, Policy);
2136     s << ']';
2137   }
2138   E->getAllocatedType().print(OS, Policy, TypeS);
2139   if (E->isParenTypeId())
2140     OS << ")";
2141 
2142   CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
2143   if (InitStyle) {
2144     if (InitStyle == CXXNewExpr::CallInit)
2145       OS << "(";
2146     PrintExpr(E->getInitializer());
2147     if (InitStyle == CXXNewExpr::CallInit)
2148       OS << ")";
2149   }
2150 }
2151 
2152 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
2153   if (E->isGlobalDelete())
2154     OS << "::";
2155   OS << "delete ";
2156   if (E->isArrayForm())
2157     OS << "[] ";
2158   PrintExpr(E->getArgument());
2159 }
2160 
2161 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
2162   PrintExpr(E->getBase());
2163   if (E->isArrow())
2164     OS << "->";
2165   else
2166     OS << '.';
2167   if (E->getQualifier())
2168     E->getQualifier()->print(OS, Policy);
2169   OS << "~";
2170 
2171   if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
2172     OS << II->getName();
2173   else
2174     E->getDestroyedType().print(OS, Policy);
2175 }
2176 
2177 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
2178   if (E->isListInitialization() && !E->isStdInitListInitialization())
2179     OS << "{";
2180 
2181   for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
2182     if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
2183       // Don't print any defaulted arguments
2184       break;
2185     }
2186 
2187     if (i) OS << ", ";
2188     PrintExpr(E->getArg(i));
2189   }
2190 
2191   if (E->isListInitialization() && !E->isStdInitListInitialization())
2192     OS << "}";
2193 }
2194 
2195 void StmtPrinter::VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E) {
2196   // Parens are printed by the surrounding context.
2197   OS << "<forwarded>";
2198 }
2199 
2200 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
2201   PrintExpr(E->getSubExpr());
2202 }
2203 
2204 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
2205   // Just forward to the subexpression.
2206   PrintExpr(E->getSubExpr());
2207 }
2208 
2209 void
2210 StmtPrinter::VisitCXXUnresolvedConstructExpr(
2211                                            CXXUnresolvedConstructExpr *Node) {
2212   Node->getTypeAsWritten().print(OS, Policy);
2213   OS << "(";
2214   for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
2215                                              ArgEnd = Node->arg_end();
2216        Arg != ArgEnd; ++Arg) {
2217     if (Arg != Node->arg_begin())
2218       OS << ", ";
2219     PrintExpr(*Arg);
2220   }
2221   OS << ")";
2222 }
2223 
2224 void StmtPrinter::VisitCXXDependentScopeMemberExpr(
2225                                          CXXDependentScopeMemberExpr *Node) {
2226   if (!Node->isImplicitAccess()) {
2227     PrintExpr(Node->getBase());
2228     OS << (Node->isArrow() ? "->" : ".");
2229   }
2230   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2231     Qualifier->print(OS, Policy);
2232   if (Node->hasTemplateKeyword())
2233     OS << "template ";
2234   OS << Node->getMemberNameInfo();
2235   if (Node->hasExplicitTemplateArgs())
2236     printTemplateArgumentList(OS, Node->template_arguments(), Policy);
2237 }
2238 
2239 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
2240   if (!Node->isImplicitAccess()) {
2241     PrintExpr(Node->getBase());
2242     OS << (Node->isArrow() ? "->" : ".");
2243   }
2244   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2245     Qualifier->print(OS, Policy);
2246   if (Node->hasTemplateKeyword())
2247     OS << "template ";
2248   OS << Node->getMemberNameInfo();
2249   if (Node->hasExplicitTemplateArgs())
2250     printTemplateArgumentList(OS, Node->template_arguments(), Policy);
2251 }
2252 
2253 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
2254   OS << getTraitSpelling(E->getTrait()) << "(";
2255   for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
2256     if (I > 0)
2257       OS << ", ";
2258     E->getArg(I)->getType().print(OS, Policy);
2259   }
2260   OS << ")";
2261 }
2262 
2263 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
2264   OS << getTraitSpelling(E->getTrait()) << '(';
2265   E->getQueriedType().print(OS, Policy);
2266   OS << ')';
2267 }
2268 
2269 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
2270   OS << getTraitSpelling(E->getTrait()) << '(';
2271   PrintExpr(E->getQueriedExpression());
2272   OS << ')';
2273 }
2274 
2275 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
2276   OS << "noexcept(";
2277   PrintExpr(E->getOperand());
2278   OS << ")";
2279 }
2280 
2281 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
2282   PrintExpr(E->getPattern());
2283   OS << "...";
2284 }
2285 
2286 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
2287   OS << "sizeof...(" << *E->getPack() << ")";
2288 }
2289 
2290 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2291                                        SubstNonTypeTemplateParmPackExpr *Node) {
2292   OS << *Node->getParameterPack();
2293 }
2294 
2295 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2296                                        SubstNonTypeTemplateParmExpr *Node) {
2297   Visit(Node->getReplacement());
2298 }
2299 
2300 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2301   OS << *E->getParameterPack();
2302 }
2303 
2304 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
2305   PrintExpr(Node->getSubExpr());
2306 }
2307 
2308 void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
2309   OS << "(";
2310   if (E->getLHS()) {
2311     PrintExpr(E->getLHS());
2312     OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2313   }
2314   OS << "...";
2315   if (E->getRHS()) {
2316     OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2317     PrintExpr(E->getRHS());
2318   }
2319   OS << ")";
2320 }
2321 
2322 void StmtPrinter::VisitConceptSpecializationExpr(ConceptSpecializationExpr *E) {
2323   NestedNameSpecifierLoc NNS = E->getNestedNameSpecifierLoc();
2324   if (NNS)
2325     NNS.getNestedNameSpecifier()->print(OS, Policy);
2326   if (E->getTemplateKWLoc().isValid())
2327     OS << "template ";
2328   OS << E->getFoundDecl()->getName();
2329   printTemplateArgumentList(OS, E->getTemplateArgsAsWritten()->arguments(),
2330                             Policy,
2331                             E->getNamedConcept()->getTemplateParameters());
2332 }
2333 
2334 void StmtPrinter::VisitRequiresExpr(RequiresExpr *E) {
2335   OS << "requires ";
2336   auto LocalParameters = E->getLocalParameters();
2337   if (!LocalParameters.empty()) {
2338     OS << "(";
2339     for (ParmVarDecl *LocalParam : LocalParameters) {
2340       PrintRawDecl(LocalParam);
2341       if (LocalParam != LocalParameters.back())
2342         OS << ", ";
2343     }
2344 
2345     OS << ") ";
2346   }
2347   OS << "{ ";
2348   auto Requirements = E->getRequirements();
2349   for (concepts::Requirement *Req : Requirements) {
2350     if (auto *TypeReq = dyn_cast<concepts::TypeRequirement>(Req)) {
2351       if (TypeReq->isSubstitutionFailure())
2352         OS << "<<error-type>>";
2353       else
2354         TypeReq->getType()->getType().print(OS, Policy);
2355     } else if (auto *ExprReq = dyn_cast<concepts::ExprRequirement>(Req)) {
2356       if (ExprReq->isCompound())
2357         OS << "{ ";
2358       if (ExprReq->isExprSubstitutionFailure())
2359         OS << "<<error-expression>>";
2360       else
2361         PrintExpr(ExprReq->getExpr());
2362       if (ExprReq->isCompound()) {
2363         OS << " }";
2364         if (ExprReq->getNoexceptLoc().isValid())
2365           OS << " noexcept";
2366         const auto &RetReq = ExprReq->getReturnTypeRequirement();
2367         if (!RetReq.isEmpty()) {
2368           OS << " -> ";
2369           if (RetReq.isSubstitutionFailure())
2370             OS << "<<error-type>>";
2371           else if (RetReq.isTypeConstraint())
2372             RetReq.getTypeConstraint()->print(OS, Policy);
2373         }
2374       }
2375     } else {
2376       auto *NestedReq = cast<concepts::NestedRequirement>(Req);
2377       OS << "requires ";
2378       if (NestedReq->isSubstitutionFailure())
2379         OS << "<<error-expression>>";
2380       else
2381         PrintExpr(NestedReq->getConstraintExpr());
2382     }
2383     OS << "; ";
2384   }
2385   OS << "}";
2386 }
2387 
2388 // C++ Coroutines TS
2389 
2390 void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) {
2391   Visit(S->getBody());
2392 }
2393 
2394 void StmtPrinter::VisitCoreturnStmt(CoreturnStmt *S) {
2395   OS << "co_return";
2396   if (S->getOperand()) {
2397     OS << " ";
2398     Visit(S->getOperand());
2399   }
2400   OS << ";";
2401 }
2402 
2403 void StmtPrinter::VisitCoawaitExpr(CoawaitExpr *S) {
2404   OS << "co_await ";
2405   PrintExpr(S->getOperand());
2406 }
2407 
2408 void StmtPrinter::VisitDependentCoawaitExpr(DependentCoawaitExpr *S) {
2409   OS << "co_await ";
2410   PrintExpr(S->getOperand());
2411 }
2412 
2413 void StmtPrinter::VisitCoyieldExpr(CoyieldExpr *S) {
2414   OS << "co_yield ";
2415   PrintExpr(S->getOperand());
2416 }
2417 
2418 // Obj-C
2419 
2420 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2421   OS << "@";
2422   VisitStringLiteral(Node->getString());
2423 }
2424 
2425 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
2426   OS << "@";
2427   Visit(E->getSubExpr());
2428 }
2429 
2430 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2431   OS << "@[ ";
2432   ObjCArrayLiteral::child_range Ch = E->children();
2433   for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) {
2434     if (I != Ch.begin())
2435       OS << ", ";
2436     Visit(*I);
2437   }
2438   OS << " ]";
2439 }
2440 
2441 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2442   OS << "@{ ";
2443   for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
2444     if (I > 0)
2445       OS << ", ";
2446 
2447     ObjCDictionaryElement Element = E->getKeyValueElement(I);
2448     Visit(Element.Key);
2449     OS << " : ";
2450     Visit(Element.Value);
2451     if (Element.isPackExpansion())
2452       OS << "...";
2453   }
2454   OS << " }";
2455 }
2456 
2457 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
2458   OS << "@encode(";
2459   Node->getEncodedType().print(OS, Policy);
2460   OS << ')';
2461 }
2462 
2463 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
2464   OS << "@selector(";
2465   Node->getSelector().print(OS);
2466   OS << ')';
2467 }
2468 
2469 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
2470   OS << "@protocol(" << *Node->getProtocol() << ')';
2471 }
2472 
2473 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2474   OS << "[";
2475   switch (Mess->getReceiverKind()) {
2476   case ObjCMessageExpr::Instance:
2477     PrintExpr(Mess->getInstanceReceiver());
2478     break;
2479 
2480   case ObjCMessageExpr::Class:
2481     Mess->getClassReceiver().print(OS, Policy);
2482     break;
2483 
2484   case ObjCMessageExpr::SuperInstance:
2485   case ObjCMessageExpr::SuperClass:
2486     OS << "Super";
2487     break;
2488   }
2489 
2490   OS << ' ';
2491   Selector selector = Mess->getSelector();
2492   if (selector.isUnarySelector()) {
2493     OS << selector.getNameForSlot(0);
2494   } else {
2495     for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
2496       if (i < selector.getNumArgs()) {
2497         if (i > 0) OS << ' ';
2498         if (selector.getIdentifierInfoForSlot(i))
2499           OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
2500         else
2501            OS << ":";
2502       }
2503       else OS << ", "; // Handle variadic methods.
2504 
2505       PrintExpr(Mess->getArg(i));
2506     }
2507   }
2508   OS << "]";
2509 }
2510 
2511 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2512   OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2513 }
2514 
2515 void
2516 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2517   PrintExpr(E->getSubExpr());
2518 }
2519 
2520 void
2521 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
2522   OS << '(' << E->getBridgeKindName();
2523   E->getType().print(OS, Policy);
2524   OS << ')';
2525   PrintExpr(E->getSubExpr());
2526 }
2527 
2528 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
2529   BlockDecl *BD = Node->getBlockDecl();
2530   OS << "^";
2531 
2532   const FunctionType *AFT = Node->getFunctionType();
2533 
2534   if (isa<FunctionNoProtoType>(AFT)) {
2535     OS << "()";
2536   } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
2537     OS << '(';
2538     for (BlockDecl::param_iterator AI = BD->param_begin(),
2539          E = BD->param_end(); AI != E; ++AI) {
2540       if (AI != BD->param_begin()) OS << ", ";
2541       std::string ParamStr = (*AI)->getNameAsString();
2542       (*AI)->getType().print(OS, Policy, ParamStr);
2543     }
2544 
2545     const auto *FT = cast<FunctionProtoType>(AFT);
2546     if (FT->isVariadic()) {
2547       if (!BD->param_empty()) OS << ", ";
2548       OS << "...";
2549     }
2550     OS << ')';
2551   }
2552   OS << "{ }";
2553 }
2554 
2555 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
2556   PrintExpr(Node->getSourceExpr());
2557 }
2558 
2559 void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
2560   // TODO: Print something reasonable for a TypoExpr, if necessary.
2561   llvm_unreachable("Cannot print TypoExpr nodes");
2562 }
2563 
2564 void StmtPrinter::VisitRecoveryExpr(RecoveryExpr *Node) {
2565   OS << "<recovery-expr>(";
2566   const char *Sep = "";
2567   for (Expr *E : Node->subExpressions()) {
2568     OS << Sep;
2569     PrintExpr(E);
2570     Sep = ", ";
2571   }
2572   OS << ')';
2573 }
2574 
2575 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2576   OS << "__builtin_astype(";
2577   PrintExpr(Node->getSrcExpr());
2578   OS << ", ";
2579   Node->getType().print(OS, Policy);
2580   OS << ")";
2581 }
2582 
2583 //===----------------------------------------------------------------------===//
2584 // Stmt method implementations
2585 //===----------------------------------------------------------------------===//
2586 
2587 void Stmt::dumpPretty(const ASTContext &Context) const {
2588   printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
2589 }
2590 
2591 void Stmt::printPretty(raw_ostream &Out, PrinterHelper *Helper,
2592                        const PrintingPolicy &Policy, unsigned Indentation,
2593                        StringRef NL, const ASTContext *Context) const {
2594   StmtPrinter P(Out, Helper, Policy, Indentation, NL, Context);
2595   P.Visit(const_cast<Stmt *>(this));
2596 }
2597 
2598 void Stmt::printPrettyControlled(raw_ostream &Out, PrinterHelper *Helper,
2599                                  const PrintingPolicy &Policy,
2600                                  unsigned Indentation, StringRef NL,
2601                                  const ASTContext *Context) const {
2602   StmtPrinter P(Out, Helper, Policy, Indentation, NL, Context);
2603   P.PrintControlledStmt(const_cast<Stmt *>(this));
2604 }
2605 
2606 void Stmt::printJson(raw_ostream &Out, PrinterHelper *Helper,
2607                      const PrintingPolicy &Policy, bool AddQuotes) const {
2608   std::string Buf;
2609   llvm::raw_string_ostream TempOut(Buf);
2610 
2611   printPretty(TempOut, Helper, Policy);
2612 
2613   Out << JsonFormat(TempOut.str(), AddQuotes);
2614 }
2615 
2616 //===----------------------------------------------------------------------===//
2617 // PrinterHelper
2618 //===----------------------------------------------------------------------===//
2619 
2620 // Implement virtual destructor.
2621 PrinterHelper::~PrinterHelper() = default;
2622