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