1//===- StmtPrinter.cpp - Printing implementation for Stmt ASTs ------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Stmt::dumpPretty/Stmt::printPretty methods, which
10// pretty print the AST back out to C code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Attr.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclBase.h"
18#include "clang/AST/DeclCXX.h"
19#include "clang/AST/DeclObjC.h"
20#include "clang/AST/DeclOpenMP.h"
21#include "clang/AST/DeclTemplate.h"
22#include "clang/AST/Expr.h"
23#include "clang/AST/ExprCXX.h"
24#include "clang/AST/ExprObjC.h"
25#include "clang/AST/ExprOpenMP.h"
26#include "clang/AST/NestedNameSpecifier.h"
27#include "clang/AST/OpenMPClause.h"
28#include "clang/AST/PrettyPrinter.h"
29#include "clang/AST/Stmt.h"
30#include "clang/AST/StmtCXX.h"
31#include "clang/AST/StmtObjC.h"
32#include "clang/AST/StmtOpenMP.h"
33#include "clang/AST/StmtVisitor.h"
34#include "clang/AST/TemplateBase.h"
35#include "clang/AST/Type.h"
36#include "clang/Basic/CharInfo.h"
37#include "clang/Basic/ExpressionTraits.h"
38#include "clang/Basic/IdentifierTable.h"
39#include "clang/Basic/JsonSupport.h"
40#include "clang/Basic/LLVM.h"
41#include "clang/Basic/Lambda.h"
42#include "clang/Basic/OpenMPKinds.h"
43#include "clang/Basic/OperatorKinds.h"
44#include "clang/Basic/SourceLocation.h"
45#include "clang/Basic/TypeTraits.h"
46#include "clang/Lex/Lexer.h"
47#include "llvm/ADT/ArrayRef.h"
48#include "llvm/ADT/SmallString.h"
49#include "llvm/ADT/SmallVector.h"
50#include "llvm/ADT/StringRef.h"
51#include "llvm/Support/Casting.h"
52#include "llvm/Support/Compiler.h"
53#include "llvm/Support/ErrorHandling.h"
54#include "llvm/Support/Format.h"
55#include "llvm/Support/raw_ostream.h"
56#include <cassert>
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, Policy.Indentation); }
83
84 void PrintStmt(Stmt *S, int SubIndent) {
85 IndentLevel += SubIndent;
86 if (S && isa<Expr>(S)) {
87 // If this is an expr used in a stmt context, indent and newline it.
88 Indent();
89 Visit(S);
90 OS << ";" << NL;
91 } else if (S) {
92 Visit(S);
93 } else {
94 Indent() << "<<<NULL STATEMENT>>>" << NL;
95 }
96 IndentLevel -= SubIndent;
97 }
98
99 void PrintInitStmt(Stmt *S, unsigned PrefixWidth) {
100 // FIXME: Cope better with odd prefix widths.
101 IndentLevel += (PrefixWidth + 1) / 2;
102 if (auto *DS = dyn_cast<DeclStmt>(S))
103 PrintRawDeclStmt(DS);
104 else
105 PrintExpr(cast<Expr>(S));
106 OS << "; ";
107 IndentLevel -= (PrefixWidth + 1) / 2;
108 }
109
110 void PrintControlledStmt(Stmt *S) {
111 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
112 OS << " ";
113 PrintRawCompoundStmt(CS);
114 OS << NL;
115 } else {
116 OS << NL;
117 PrintStmt(S);
118 }
119 }
120
121 void PrintRawCompoundStmt(CompoundStmt *S);
122 void PrintRawDecl(Decl *D);
123 void PrintRawDeclStmt(const DeclStmt *S);
124 void PrintRawIfStmt(IfStmt *If);
125 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
126 void PrintCallArgs(CallExpr *E);
127 void PrintRawSEHExceptHandler(SEHExceptStmt *S);
128 void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
129 void PrintOMPExecutableDirective(OMPExecutableDirective *S,
130 bool ForceNoStmt = false);
131
132 void PrintExpr(Expr *E) {
133 if (E)
134 Visit(E);
135 else
136 OS << "<null expr>";
137 }
138
139 raw_ostream &Indent(int Delta = 0) {
140 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
141 OS << " ";
142 return OS;
143 }
144
145 void Visit(Stmt* S) {
146 if (Helper && Helper->handledStmt(S,OS))
147 return;
148 else StmtVisitor<StmtPrinter>::Visit(S);
149 }
150
151 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
152 Indent() << "<<unknown stmt type>>" << NL;
153 }
154
155 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
156 OS << "<<unknown expr type>>";
157 }
158
159 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
160
161#define ABSTRACT_STMT(CLASS)
162#define STMT(CLASS, PARENT) \
163 void Visit##CLASS(CLASS *Node);
164#include "clang/AST/StmtNodes.inc"
165 };
166
167} // namespace
168
169//===----------------------------------------------------------------------===//
170// Stmt printing methods.
171//===----------------------------------------------------------------------===//
172
173/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
174/// with no newline after the }.
175void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
176 OS << "{" << NL;
177 for (auto *I : Node->body())
178 PrintStmt(I);
179
180 Indent() << "}";
181}
182
183void StmtPrinter::PrintRawDecl(Decl *D) {
184 D->print(OS, Policy, IndentLevel);
185}
186
187void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
188 SmallVector<Decl *, 2> Decls(S->decls());
189 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
190}
191
192void StmtPrinter::VisitNullStmt(NullStmt *Node) {
193 Indent() << ";" << NL;
194}
195
196void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
197 Indent();
198 PrintRawDeclStmt(Node);
199 OS << ";" << NL;
200}
201
202void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
203 Indent();
204 PrintRawCompoundStmt(Node);
205 OS << "" << NL;
206}
207
208void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
209 Indent(-1) << "case ";
210 PrintExpr(Node->getLHS());
211 if (Node->getRHS()) {
212 OS << " ... ";
213 PrintExpr(Node->getRHS());
214 }
215 OS << ":" << NL;
216
217 PrintStmt(Node->getSubStmt(), 0);
218}
219
220void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
221 Indent(-1) << "default:" << NL;
222 PrintStmt(Node->getSubStmt(), 0);
223}
224
225void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
226 Indent(-1) << Node->getName() << ":" << NL;
227 PrintStmt(Node->getSubStmt(), 0);
228}
229
230void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
231 for (const auto *Attr : Node->getAttrs()) {
232 Attr->printPretty(OS, Policy);
233 }
234
235 PrintStmt(Node->getSubStmt(), 0);
236}
237
238void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
239 OS << "if (";
240 if (If->getInit())
241 PrintInitStmt(If->getInit(), 4);
242 if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
243 PrintRawDeclStmt(DS);
244 else
245 PrintExpr(If->getCond());
246 OS << ')';
247
248 if (auto *CS = dyn_cast<CompoundStmt>(If->getThen())) {
249 OS << ' ';
250 PrintRawCompoundStmt(CS);
251 OS << (If->getElse() ? " " : NL);
252 } else {
253 OS << NL;
254 PrintStmt(If->getThen());
255 if (If->getElse()) Indent();
256 }
257
258 if (Stmt *Else = If->getElse()) {
259 OS << "else";
260
261 if (auto *CS = dyn_cast<CompoundStmt>(Else)) {
262 OS << ' ';
263 PrintRawCompoundStmt(CS);
264 OS << NL;
265 } else if (auto *ElseIf = dyn_cast<IfStmt>(Else)) {
266 OS << ' ';
267 PrintRawIfStmt(ElseIf);
268 } else {
269 OS << NL;
270 PrintStmt(If->getElse());
271 }
272 }
273}
274
275void StmtPrinter::VisitIfStmt(IfStmt *If) {
276 Indent();
277 PrintRawIfStmt(If);
278}
279
280void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
281 Indent() << "switch (";
282 if (Node->getInit())
283 PrintInitStmt(Node->getInit(), 8);
284 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
285 PrintRawDeclStmt(DS);
286 else
287 PrintExpr(Node->getCond());
288 OS << ")";
289 PrintControlledStmt(Node->getBody());
290}
291
292void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
293 Indent() << "while (";
294 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
295 PrintRawDeclStmt(DS);
296 else
297 PrintExpr(Node->getCond());
298 OS << ")" << NL;
299 PrintStmt(Node->getBody());
300}
301
302void StmtPrinter::VisitDoStmt(DoStmt *Node) {
303 Indent() << "do ";
304 if (auto *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
305 PrintRawCompoundStmt(CS);
306 OS << " ";
307 } else {
308 OS << NL;
309 PrintStmt(Node->getBody());
310 Indent();
311 }
312
313 OS << "while (";
314 PrintExpr(Node->getCond());
315 OS << ");" << NL;
316}
317
318void StmtPrinter::VisitForStmt(ForStmt *Node) {
319 Indent() << "for (";
320 if (Node->getInit())
321 PrintInitStmt(Node->getInit(), 5);
322 else
323 OS << (Node->getCond() ? "; " : ";");
324 if (Node->getCond())
325 PrintExpr(Node->getCond());
326 OS << ";";
327 if (Node->getInc()) {
328 OS << " ";
329 PrintExpr(Node->getInc());
330 }
331 OS << ")";
332 PrintControlledStmt(Node->getBody());
333}
334
335void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
336 Indent() << "for (";
337 if (auto *DS = dyn_cast<DeclStmt>(Node->getElement()))
338 PrintRawDeclStmt(DS);
339 else
340 PrintExpr(cast<Expr>(Node->getElement()));
341 OS << " in ";
342 PrintExpr(Node->getCollection());
343 OS << ")";
344 PrintControlledStmt(Node->getBody());
345}
346
347void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
348 Indent() << "for (";
349 if (Node->getInit())
350 PrintInitStmt(Node->getInit(), 5);
351 PrintingPolicy SubPolicy(Policy);
352 SubPolicy.SuppressInitializers = true;
353 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
354 OS << " : ";
355 PrintExpr(Node->getRangeInit());
356 OS << ")";
357 PrintControlledStmt(Node->getBody());
358}
359
360void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
361 Indent();
362 if (Node->isIfExists())
363 OS << "__if_exists (";
364 else
365 OS << "__if_not_exists (";
366
367 if (NestedNameSpecifier *Qualifier
368 = Node->getQualifierLoc().getNestedNameSpecifier())
369 Qualifier->print(OS, Policy);
370
371 OS << Node->getNameInfo() << ") ";
372
373 PrintRawCompoundStmt(Node->getSubStmt());
374}
375
376void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
377 Indent() << "goto " << Node->getLabel()->getName() << ";";
378 if (Policy.IncludeNewlines) OS << NL;
379}
380
381void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
382 Indent() << "goto *";
383 PrintExpr(Node->getTarget());
384 OS << ";";
385 if (Policy.IncludeNewlines) OS << NL;
386}
387
388void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
389 Indent() << "continue;";
390 if (Policy.IncludeNewlines) OS << NL;
391}
392
393void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
394 Indent() << "break;";
395 if (Policy.IncludeNewlines) OS << NL;
396}
397
398void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
399 Indent() << "return";
400 if (Node->getRetValue()) {
401 OS << " ";
402 PrintExpr(Node->getRetValue());
403 }
404 OS << ";";
405 if (Policy.IncludeNewlines) OS << NL;
406}
407
408void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
409 Indent() << "asm ";
410
411 if (Node->isVolatile())
412 OS << "volatile ";
413
414 if (Node->isAsmGoto())
415 OS << "goto ";
416
417 OS << "(";
418 VisitStringLiteral(Node->getAsmString());
419
420 // Outputs
421 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
422 Node->getNumClobbers() != 0 || Node->getNumLabels() != 0)
423 OS << " : ";
424
425 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
426 if (i != 0)
427 OS << ", ";
428
429 if (!Node->getOutputName(i).empty()) {
430 OS << '[';
431 OS << Node->getOutputName(i);
432 OS << "] ";
433 }
434
435 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
436 OS << " (";
437 Visit(Node->getOutputExpr(i));
438 OS << ")";
439 }
440
441 // Inputs
442 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0 ||
443 Node->getNumLabels() != 0)
444 OS << " : ";
445
446 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
447 if (i != 0)
448 OS << ", ";
449
450 if (!Node->getInputName(i).empty()) {
451 OS << '[';
452 OS << Node->getInputName(i);
453 OS << "] ";
454 }
455
456 VisitStringLiteral(Node->getInputConstraintLiteral(i));
457 OS << " (";
458 Visit(Node->getInputExpr(i));
459 OS << ")";
460 }
461
462 // Clobbers
463 if (Node->getNumClobbers() != 0 || Node->getNumLabels())
464 OS << " : ";
465
466 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
467 if (i != 0)
468 OS << ", ";
469
470 VisitStringLiteral(Node->getClobberStringLiteral(i));
471 }
472
473 // Labels
474 if (Node->getNumLabels() != 0)
475 OS << " : ";
476
477 for (unsigned i = 0, e = Node->getNumLabels(); i != e; ++i) {
478 if (i != 0)
479 OS << ", ";
480 OS << Node->getLabelName(i);
481 }
482
483 OS << ");";
484 if (Policy.IncludeNewlines) OS << NL;
485}
486
487void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
488 // FIXME: Implement MS style inline asm statement printer.
489 Indent() << "__asm ";
490 if (Node->hasBraces())
491 OS << "{" << NL;
492 OS << Node->getAsmString() << NL;
493 if (Node->hasBraces())
494 Indent() << "}" << NL;
495}
496
497void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
498 PrintStmt(Node->getCapturedDecl()->getBody());
499}
500
501void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
502 Indent() << "@try";
503 if (auto *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
504 PrintRawCompoundStmt(TS);
505 OS << NL;
506 }
507
508 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
509 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
510 Indent() << "@catch(";
511 if (catchStmt->getCatchParamDecl()) {
512 if (Decl *DS = catchStmt->getCatchParamDecl())
513 PrintRawDecl(DS);
514 }
515 OS << ")";
516 if (auto *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
517 PrintRawCompoundStmt(CS);
518 OS << NL;
519 }
520 }
521
522 if (auto *FS = static_cast<ObjCAtFinallyStmt *>(Node->getFinallyStmt())) {
523 Indent() << "@finally";
524 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
525 OS << NL;
526 }
527}
528
529void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
530}
531
532void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
533 Indent() << "@catch (...) { /* todo */ } " << NL;
534}
535
536void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
537 Indent() << "@throw";
538 if (Node->getThrowExpr()) {
539 OS << " ";
540 PrintExpr(Node->getThrowExpr());
541 }
542 OS << ";" << NL;
543}
544
545void StmtPrinter::VisitObjCAvailabilityCheckExpr(
546 ObjCAvailabilityCheckExpr *Node) {
547 OS << "@available(...)";
548}
549
550void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
551 Indent() << "@synchronized (";
552 PrintExpr(Node->getSynchExpr());
553 OS << ")";
554 PrintRawCompoundStmt(Node->getSynchBody());
555 OS << NL;
556}
557
558void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
559 Indent() << "@autoreleasepool";
560 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
561 OS << NL;
562}
563
564void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
565 OS << "catch (";
566 if (Decl *ExDecl = Node->getExceptionDecl())
567 PrintRawDecl(ExDecl);
568 else
569 OS << "...";
570 OS << ") ";
571 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
572}
573
574void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
575 Indent();
576 PrintRawCXXCatchStmt(Node);
577 OS << NL;
578}
579
580void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
581 Indent() << "try ";
582 PrintRawCompoundStmt(Node->getTryBlock());
583 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
584 OS << " ";
585 PrintRawCXXCatchStmt(Node->getHandler(i));
586 }
587 OS << NL;
588}
589
590void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
591 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
592 PrintRawCompoundStmt(Node->getTryBlock());
593 SEHExceptStmt *E = Node->getExceptHandler();
594 SEHFinallyStmt *F = Node->getFinallyHandler();
595 if(E)
596 PrintRawSEHExceptHandler(E);
597 else {
598 assert(F && "Must have a finally block...");
599 PrintRawSEHFinallyStmt(F);
600 }
601 OS << NL;
602}
603
604void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
605 OS << "__finally ";
606 PrintRawCompoundStmt(Node->getBlock());
607 OS << NL;
608}
609
610void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
611 OS << "__except (";
612 VisitExpr(Node->getFilterExpr());
613 OS << ")" << NL;
614 PrintRawCompoundStmt(Node->getBlock());
615 OS << NL;
616}
617
618void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
619 Indent();
620 PrintRawSEHExceptHandler(Node);
621 OS << NL;
622}
623
624void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
625 Indent();
626 PrintRawSEHFinallyStmt(Node);
627 OS << NL;
628}
629
630void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) {
631 Indent() << "__leave;";
632 if (Policy.IncludeNewlines) OS << NL;
633}
634
635//===----------------------------------------------------------------------===//
636// OpenMP directives printing methods
637//===----------------------------------------------------------------------===//
638
639void StmtPrinter::VisitOMPCanonicalLoop(OMPCanonicalLoop *Node) {
640 PrintStmt(Node->getLoopStmt());
641}
642
643void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S,
644 bool ForceNoStmt) {
645 OMPClausePrinter Printer(OS, Policy);
646 ArrayRef<OMPClause *> Clauses = S->clauses();
647 for (auto *Clause : Clauses)
648 if (Clause && !Clause->isImplicit()) {
649 OS << ' ';
650 Printer.Visit(Clause);
651 }
652 OS << NL;
653 if (!ForceNoStmt && S->hasAssociatedStmt())
654 PrintStmt(S->getRawStmt());
655}
656
657void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
658 Indent() << "#pragma omp parallel";
659 PrintOMPExecutableDirective(Node);
660}
661
662void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
663 Indent() << "#pragma omp simd";
664 PrintOMPExecutableDirective(Node);
665}
666
667void StmtPrinter::VisitOMPTileDirective(OMPTileDirective *Node) {
668 Indent() << "#pragma omp tile";
669 PrintOMPExecutableDirective(Node);
670}
671
672void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
673 Indent() << "#pragma omp for";
674 PrintOMPExecutableDirective(Node);
675}
676
677void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) {
678 Indent() << "#pragma omp for simd";
679 PrintOMPExecutableDirective(Node);
680}
681
682void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
683 Indent() << "#pragma omp sections";
684 PrintOMPExecutableDirective(Node);
685}
686
687void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
688 Indent() << "#pragma omp section";
689 PrintOMPExecutableDirective(Node);
690}
691
692void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
693 Indent() << "#pragma omp single";
694 PrintOMPExecutableDirective(Node);
695}
696
697void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
698 Indent() << "#pragma omp master";
699 PrintOMPExecutableDirective(Node);
700}
701
702void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
703 Indent() << "#pragma omp critical";
704 if (Node->getDirectiveName().getName()) {
705 OS << " (";
706 Node->getDirectiveName().printName(OS, Policy);
707 OS << ")";
708 }
709 PrintOMPExecutableDirective(Node);
710}
711
712void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
713 Indent() << "#pragma omp parallel for";
714 PrintOMPExecutableDirective(Node);
715}
716
717void StmtPrinter::VisitOMPParallelForSimdDirective(
718 OMPParallelForSimdDirective *Node) {
719 Indent() << "#pragma omp parallel for simd";
720 PrintOMPExecutableDirective(Node);
721}
722
723void StmtPrinter::VisitOMPParallelMasterDirective(
724 OMPParallelMasterDirective *Node) {
725 Indent() << "#pragma omp parallel master";
726 PrintOMPExecutableDirective(Node);
727}
728
729void StmtPrinter::VisitOMPParallelSectionsDirective(
730 OMPParallelSectionsDirective *Node) {
731 Indent() << "#pragma omp parallel sections";
732 PrintOMPExecutableDirective(Node);
733}
734
735void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
736 Indent() << "#pragma omp task";
737 PrintOMPExecutableDirective(Node);
738}
739
740void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
741 Indent() << "#pragma omp taskyield";
742 PrintOMPExecutableDirective(Node);
743}
744
745void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
746 Indent() << "#pragma omp barrier";
747 PrintOMPExecutableDirective(Node);
748}
749
750void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
751 Indent() << "#pragma omp taskwait";
752 PrintOMPExecutableDirective(Node);
753}
754
755void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) {
756 Indent() << "#pragma omp taskgroup";
757 PrintOMPExecutableDirective(Node);
758}
759
760void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
761 Indent() << "#pragma omp flush";
762 PrintOMPExecutableDirective(Node);
763}
764
765void StmtPrinter::VisitOMPDepobjDirective(OMPDepobjDirective *Node) {
766 Indent() << "#pragma omp depobj";
767 PrintOMPExecutableDirective(Node);
768}
769
770void StmtPrinter::VisitOMPScanDirective(OMPScanDirective *Node) {
771 Indent() << "#pragma omp scan";
772 PrintOMPExecutableDirective(Node);
773}
774
775void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
776 Indent() << "#pragma omp ordered";
777 PrintOMPExecutableDirective(Node, Node->hasClausesOfKind<OMPDependClause>());
778}
779
780void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
781 Indent() << "#pragma omp atomic";
782 PrintOMPExecutableDirective(Node);
783}
784
785void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
786 Indent() << "#pragma omp target";
787 PrintOMPExecutableDirective(Node);
788}
789
790void StmtPrinter::VisitOMPTargetDataDirective(OMPTargetDataDirective *Node) {
791 Indent() << "#pragma omp target data";
792 PrintOMPExecutableDirective(Node);
793}
794
795void StmtPrinter::VisitOMPTargetEnterDataDirective(
796 OMPTargetEnterDataDirective *Node) {
797 Indent() << "#pragma omp target enter data";
798 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
799}
800
801void StmtPrinter::VisitOMPTargetExitDataDirective(
802 OMPTargetExitDataDirective *Node) {
803 Indent() << "#pragma omp target exit data";
804 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
805}
806
807void StmtPrinter::VisitOMPTargetParallelDirective(
808 OMPTargetParallelDirective *Node) {
809 Indent() << "#pragma omp target parallel";
810 PrintOMPExecutableDirective(Node);
811}
812
813void StmtPrinter::VisitOMPTargetParallelForDirective(
814 OMPTargetParallelForDirective *Node) {
815 Indent() << "#pragma omp target parallel for";
816 PrintOMPExecutableDirective(Node);
817}
818
819void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) {
820 Indent() << "#pragma omp teams";
821 PrintOMPExecutableDirective(Node);
822}
823
824void StmtPrinter::VisitOMPCancellationPointDirective(
825 OMPCancellationPointDirective *Node) {
826 Indent() << "#pragma omp cancellation point "
827 << getOpenMPDirectiveName(Node->getCancelRegion());
828 PrintOMPExecutableDirective(Node);
829}
830
831void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) {
832 Indent() << "#pragma omp cancel "
833 << getOpenMPDirectiveName(Node->getCancelRegion());
834 PrintOMPExecutableDirective(Node);
835}
836
837void StmtPrinter::VisitOMPTaskLoopDirective(OMPTaskLoopDirective *Node) {
838 Indent() << "#pragma omp taskloop";
839 PrintOMPExecutableDirective(Node);
840}
841
842void StmtPrinter::VisitOMPTaskLoopSimdDirective(
843 OMPTaskLoopSimdDirective *Node) {
844 Indent() << "#pragma omp taskloop simd";
845 PrintOMPExecutableDirective(Node);
846}
847
848void StmtPrinter::VisitOMPMasterTaskLoopDirective(
849 OMPMasterTaskLoopDirective *Node) {
850 Indent() << "#pragma omp master taskloop";
851 PrintOMPExecutableDirective(Node);
852}
853
854void StmtPrinter::VisitOMPMasterTaskLoopSimdDirective(
855 OMPMasterTaskLoopSimdDirective *Node) {
856 Indent() << "#pragma omp master taskloop simd";
857 PrintOMPExecutableDirective(Node);
858}
859
860void StmtPrinter::VisitOMPParallelMasterTaskLoopDirective(
861 OMPParallelMasterTaskLoopDirective *Node) {
862 Indent() << "#pragma omp parallel master taskloop";
863 PrintOMPExecutableDirective(Node);
864}
865
866void StmtPrinter::VisitOMPParallelMasterTaskLoopSimdDirective(
867 OMPParallelMasterTaskLoopSimdDirective *Node) {
868 Indent() << "#pragma omp parallel master taskloop simd";
869 PrintOMPExecutableDirective(Node);
870}
871
872void StmtPrinter::VisitOMPDistributeDirective(OMPDistributeDirective *Node) {
873 Indent() << "#pragma omp distribute";
874 PrintOMPExecutableDirective(Node);
875}
876
877void StmtPrinter::VisitOMPTargetUpdateDirective(
878 OMPTargetUpdateDirective *Node) {
879 Indent() << "#pragma omp target update";
880 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
881}
882
883void StmtPrinter::VisitOMPDistributeParallelForDirective(
884 OMPDistributeParallelForDirective *Node) {
885 Indent() << "#pragma omp distribute parallel for";
886 PrintOMPExecutableDirective(Node);
887}
888
889void StmtPrinter::VisitOMPDistributeParallelForSimdDirective(
890 OMPDistributeParallelForSimdDirective *Node) {
891 Indent() << "#pragma omp distribute parallel for simd";
892 PrintOMPExecutableDirective(Node);
893}
894
895void StmtPrinter::VisitOMPDistributeSimdDirective(
896 OMPDistributeSimdDirective *Node) {
897 Indent() << "#pragma omp distribute simd";
898 PrintOMPExecutableDirective(Node);
899}
900
901void StmtPrinter::VisitOMPTargetParallelForSimdDirective(
902 OMPTargetParallelForSimdDirective *Node) {
903 Indent() << "#pragma omp target parallel for simd";
904 PrintOMPExecutableDirective(Node);
905}
906
907void StmtPrinter::VisitOMPTargetSimdDirective(OMPTargetSimdDirective *Node) {
908 Indent() << "#pragma omp target simd";
909 PrintOMPExecutableDirective(Node);
910}
911
912void StmtPrinter::VisitOMPTeamsDistributeDirective(
913 OMPTeamsDistributeDirective *Node) {
914 Indent() << "#pragma omp teams distribute";
915 PrintOMPExecutableDirective(Node);
916}
917
918void StmtPrinter::VisitOMPTeamsDistributeSimdDirective(
919 OMPTeamsDistributeSimdDirective *Node) {
920 Indent() << "#pragma omp teams distribute simd";
921 PrintOMPExecutableDirective(Node);
922}
923
924void StmtPrinter::VisitOMPTeamsDistributeParallelForSimdDirective(
925 OMPTeamsDistributeParallelForSimdDirective *Node) {
926 Indent() << "#pragma omp teams distribute parallel for simd";
927 PrintOMPExecutableDirective(Node);
928}
929
930void StmtPrinter::VisitOMPTeamsDistributeParallelForDirective(
931 OMPTeamsDistributeParallelForDirective *Node) {
932 Indent() << "#pragma omp teams distribute parallel for";
933 PrintOMPExecutableDirective(Node);
934}
935
936void StmtPrinter::VisitOMPTargetTeamsDirective(OMPTargetTeamsDirective *Node) {
937 Indent() << "#pragma omp target teams";
938 PrintOMPExecutableDirective(Node);
939}
940
941void StmtPrinter::VisitOMPTargetTeamsDistributeDirective(
942 OMPTargetTeamsDistributeDirective *Node) {
943 Indent() << "#pragma omp target teams distribute";
944 PrintOMPExecutableDirective(Node);
945}
946
947void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForDirective(
948 OMPTargetTeamsDistributeParallelForDirective *Node) {
949 Indent() << "#pragma omp target teams distribute parallel for";
950 PrintOMPExecutableDirective(Node);
951}
952
953void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForSimdDirective(
954 OMPTargetTeamsDistributeParallelForSimdDirective *Node) {
955 Indent() << "#pragma omp target teams distribute parallel for simd";
956 PrintOMPExecutableDirective(Node);
957}
958
959void StmtPrinter::VisitOMPTargetTeamsDistributeSimdDirective(
960 OMPTargetTeamsDistributeSimdDirective *Node) {
961 Indent() << "#pragma omp target teams distribute simd";
962 PrintOMPExecutableDirective(Node);
963}
964
965void StmtPrinter::VisitOMPInteropDirective(OMPInteropDirective *Node) {
966 Indent() << "#pragma omp interop";
967 PrintOMPExecutableDirective(Node);
968}
969
970void StmtPrinter::VisitOMPDispatchDirective(OMPDispatchDirective *Node) {
971 Indent() << "#pragma omp dispatch";
972 PrintOMPExecutableDirective(Node);
973}
974
975void StmtPrinter::VisitOMPMaskedDirective(OMPMaskedDirective *Node) {
976 Indent() << "#pragma omp masked";
977 PrintOMPExecutableDirective(Node);
978}
979
980//===----------------------------------------------------------------------===//
981// Expr printing methods.
982//===----------------------------------------------------------------------===//
983
984void StmtPrinter::VisitSourceLocExpr(SourceLocExpr *Node) {
985 OS << Node->getBuiltinStr() << "()";
986}
987
988void StmtPrinter::VisitConstantExpr(ConstantExpr *Node) {
989 PrintExpr(Node->getSubExpr());
990}
991
992void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
993 if (const auto *OCED = dyn_cast<OMPCapturedExprDecl>(Node->getDecl())) {
994 OCED->getInit()->IgnoreImpCasts()->printPretty(OS, nullptr, Policy);
995 return;
996 }
997 if (const auto *TPOD = dyn_cast<TemplateParamObjectDecl>(Node->getDecl())) {
998 TPOD->printAsExpr(OS);
999 return;
1000 }
1001 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1002 Qualifier->print(OS, Policy);
1003 if (Node->hasTemplateKeyword())
1004 OS << "template ";
1005 OS << Node->getNameInfo();
1006 if (Node->hasExplicitTemplateArgs())
1007 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
1008}
1009
1010void StmtPrinter::VisitDependentScopeDeclRefExpr(
1011 DependentScopeDeclRefExpr *Node) {
1012 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1013 Qualifier->print(OS, Policy);
1014 if (Node->hasTemplateKeyword())
1015 OS << "template ";
1016 OS << Node->getNameInfo();
1017 if (Node->hasExplicitTemplateArgs())
1018 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
1019}
1020
1021void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
1022 if (Node->getQualifier())
1023 Node->getQualifier()->print(OS, Policy);
1024 if (Node->hasTemplateKeyword())
1025 OS << "template ";
1026 OS << Node->getNameInfo();
1027 if (Node->hasExplicitTemplateArgs())
1028 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
1029}
1030
1031static bool isImplicitSelf(const Expr *E) {
1032 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
1033 if (const auto *PD = dyn_cast<ImplicitParamDecl>(DRE->getDecl())) {
1034 if (PD->getParameterKind() == ImplicitParamDecl::ObjCSelf &&
1035 DRE->getBeginLoc().isInvalid())
1036 return true;
1037 }
1038 }
1039 return false;
1040}
1041
1042void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
1043 if (Node->getBase()) {
1044 if (!Policy.SuppressImplicitBase ||
1045 !isImplicitSelf(Node->getBase()->IgnoreImpCasts())) {
1046 PrintExpr(Node->getBase());
1047 OS << (Node->isArrow() ? "->" : ".");
1048 }
1049 }
1050 OS << *Node->getDecl();
1051}
1052
1053void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
1054 if (Node->isSuperReceiver())
1055 OS << "super.";
1056 else if (Node->isObjectReceiver() && Node->getBase()) {
1057 PrintExpr(Node->getBase());
1058 OS << ".";
1059 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
1060 OS << Node->getClassReceiver()->getName() << ".";
1061 }
1062
1063 if (Node->isImplicitProperty()) {
1064 if (const auto *Getter = Node->getImplicitPropertyGetter())
1065 Getter->getSelector().print(OS);
1066 else
1067 OS << SelectorTable::getPropertyNameFromSetterSelector(
1068 Node->getImplicitPropertySetter()->getSelector());
1069 } else
1070 OS << Node->getExplicitProperty()->getName();
1071}
1072
1073void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
1074 PrintExpr(Node->getBaseExpr());
1075 OS << "[";
1076 PrintExpr(Node->getKeyExpr());
1077 OS << "]";
1078}
1079
1080void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
1081 OS << PredefinedExpr::getIdentKindName(Node->getIdentKind());
1082}
1083
1084void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
1085 unsigned value = Node->getValue();
1086
1087 switch (Node->getKind()) {
1088 case CharacterLiteral::Ascii: break; // no prefix.
1089 case CharacterLiteral::Wide: OS << 'L'; break;
1090 case CharacterLiteral::UTF8: OS << "u8"; break;
1091 case CharacterLiteral::UTF16: OS << 'u'; break;
1092 case CharacterLiteral::UTF32: OS << 'U'; break;
1093 }
1094
1095 switch (value) {
1096 case '\\':
1097 OS << "'\\\\'";
1098 break;
1099 case '\'':
1100 OS << "'\\''";
1101 break;
1102 case '\a':
1103 // TODO: K&R: the meaning of '\\a' is different in traditional C
1104 OS << "'\\a'";
1105 break;
1106 case '\b':
1107 OS << "'\\b'";
1108 break;
1109 // Nonstandard escape sequence.
1110 /*case '\e':
1111 OS << "'\\e'";
1112 break;*/
1113 case '\f':
1114 OS << "'\\f'";
1115 break;
1116 case '\n':
1117 OS << "'\\n'";
1118 break;
1119 case '\r':
1120 OS << "'\\r'";
1121 break;
1122 case '\t':
1123 OS << "'\\t'";
1124 break;
1125 case '\v':
1126 OS << "'\\v'";
1127 break;
1128 default:
1129 // A character literal might be sign-extended, which
1130 // would result in an invalid \U escape sequence.
1131 // FIXME: multicharacter literals such as '\xFF\xFF\xFF\xFF'
1132 // are not correctly handled.
1133 if ((value & ~0xFFu) == ~0xFFu && Node->getKind() == CharacterLiteral::Ascii)
1134 value &= 0xFFu;
1135 if (value < 256 && isPrintable((unsigned char)value))
1136 OS << "'" << (char)value << "'";
1137 else if (value < 256)
1138 OS << "'\\x" << llvm::format("%02x", value) << "'";
1139 else if (value <= 0xFFFF)
1140 OS << "'\\u" << llvm::format("%04x", value) << "'";
1141 else
1142 OS << "'\\U" << llvm::format("%08x", value) << "'";
1143 }
1144}
1145
1146/// Prints the given expression using the original source text. Returns true on
1147/// success, false otherwise.
1148static bool printExprAsWritten(raw_ostream &OS, Expr *E,
1149 const ASTContext *Context) {
1150 if (!Context)
1151 return false;
1152 bool Invalid = false;
1153 StringRef Source = Lexer::getSourceText(
1154 CharSourceRange::getTokenRange(E->getSourceRange()),
1155 Context->getSourceManager(), Context->getLangOpts(), &Invalid);
1156 if (!Invalid) {
1157 OS << Source;
1158 return true;
1159 }
1160 return false;
1161}
1162
1163void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
1164 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1165 return;
1166 bool isSigned = Node->getType()->isSignedIntegerType();
1167 OS << Node->getValue().toString(10, isSigned);
1168
1169 // Emit suffixes. Integer literals are always a builtin integer type.
1170 switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1171 default: llvm_unreachable("Unexpected type for integer literal!");
1172 case BuiltinType::Char_S:
1173 case BuiltinType::Char_U: OS << "i8"; break;
1174 case BuiltinType::UChar: OS << "Ui8"; break;
1175 case BuiltinType::Short: OS << "i16"; break;
1176 case BuiltinType::UShort: OS << "Ui16"; break;
1177 case BuiltinType::Int: break; // no suffix.
1178 case BuiltinType::UInt: OS << 'U'; break;
1179 case BuiltinType::Long: OS << 'L'; break;
1180 case BuiltinType::ULong: OS << "UL"; break;
1181 case BuiltinType::LongLong: OS << "LL"; break;
1182 case BuiltinType::ULongLong: OS << "ULL"; break;
1183 case BuiltinType::Int128:
1184 break; // no suffix.
1185 case BuiltinType::UInt128:
1186 break; // no suffix.
1187 }
1188}
1189
1190void StmtPrinter::VisitFixedPointLiteral(FixedPointLiteral *Node) {
1191 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1192 return;
1193 OS << Node->getValueAsString(/*Radix=*/10);
1194
1195 switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1196 default: llvm_unreachable("Unexpected type for fixed point literal!");
1197 case BuiltinType::ShortFract: OS << "hr"; break;
1198 case BuiltinType::ShortAccum: OS << "hk"; break;
1199 case BuiltinType::UShortFract: OS << "uhr"; break;
1200 case BuiltinType::UShortAccum: OS << "uhk"; break;
1201 case BuiltinType::Fract: OS << "r"; break;
1202 case BuiltinType::Accum: OS << "k"; break;
1203 case BuiltinType::UFract: OS << "ur"; break;
1204 case BuiltinType::UAccum: OS << "uk"; break;
1205 case BuiltinType::LongFract: OS << "lr"; break;
1206 case BuiltinType::LongAccum: OS << "lk"; break;
1207 case BuiltinType::ULongFract: OS << "ulr"; break;
1208 case BuiltinType::ULongAccum: OS << "ulk"; break;
1209 }
1210}
1211
1212static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1213 bool PrintSuffix) {
1214 SmallString<16> Str;
1215 Node->getValue().toString(Str);
1216 OS << Str;
1217 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1218 OS << '.'; // Trailing dot in order to separate from ints.
1219
1220 if (!PrintSuffix)
1221 return;
1222
1223 // Emit suffixes. Float literals are always a builtin float type.
1224 switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1225 default: llvm_unreachable("Unexpected type for float literal!");
1226 case BuiltinType::Half: break; // FIXME: suffix?
1227 case BuiltinType::Double: break; // no suffix.
1228 case BuiltinType::Float16: OS << "F16"; break;
1229 case BuiltinType::Float: OS << 'F'; break;
1230 case BuiltinType::LongDouble: OS << 'L'; break;
1231 case BuiltinType::Float128: OS << 'Q'; break;
1232 }
1233}
1234
1235void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
1236 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1237 return;
1238 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
1239}
1240
1241void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1242 PrintExpr(Node->getSubExpr());
1243 OS << "i";
1244}
1245
1246void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
1247 Str->outputString(OS);
1248}
1249
1250void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1251 OS << "(";
1252 PrintExpr(Node->getSubExpr());
1253 OS << ")";
1254}
1255
1256void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
1257 if (!Node->isPostfix()) {
1258 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1259
1260 // Print a space if this is an "identifier operator" like __real, or if
1261 // it might be concatenated incorrectly like '+'.
1262 switch (Node->getOpcode()) {
1263 default: break;
1264 case UO_Real:
1265 case UO_Imag:
1266 case UO_Extension:
1267 OS << ' ';
1268 break;
1269 case UO_Plus:
1270 case UO_Minus:
1271 if (isa<UnaryOperator>(Node->getSubExpr()))
1272 OS << ' ';
1273 break;
1274 }
1275 }
1276 PrintExpr(Node->getSubExpr());
1277
1278 if (Node->isPostfix())
1279 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1280}
1281
1282void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1283 OS << "__builtin_offsetof(";
1284 Node->getTypeSourceInfo()->getType().print(OS, Policy);
1285 OS << ", ";
1286 bool PrintedSomething = false;
1287 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
1288 OffsetOfNode ON = Node->getComponent(i);
1289 if (ON.getKind() == OffsetOfNode::Array) {
1290 // Array node
1291 OS << "[";
1292 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1293 OS << "]";
1294 PrintedSomething = true;
1295 continue;
1296 }
1297
1298 // Skip implicit base indirections.
1299 if (ON.getKind() == OffsetOfNode::Base)
1300 continue;
1301
1302 // Field or identifier node.
1303 IdentifierInfo *Id = ON.getFieldName();
1304 if (!Id)
1305 continue;
1306
1307 if (PrintedSomething)
1308 OS << ".";
1309 else
1310 PrintedSomething = true;
1311 OS << Id->getName();
1312 }
1313 OS << ")";
1314}
1315
1316void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(
1317 UnaryExprOrTypeTraitExpr *Node) {
1318 const char *Spelling = getTraitSpelling(Node->getKind());
1319 if (Node->getKind() == UETT_AlignOf) {
1320 if (Policy.Alignof)
1321 Spelling = "alignof";
1322 else if (Policy.UnderscoreAlignof)
1323 Spelling = "_Alignof";
1324 else
1325 Spelling = "__alignof";
1326 }
1327
1328 OS << Spelling;
1329
1330 if (Node->isArgumentType()) {
1331 OS << '(';
1332 Node->getArgumentType().print(OS, Policy);
1333 OS << ')';
1334 } else {
1335 OS << " ";
1336 PrintExpr(Node->getArgumentExpr());
1337 }
1338}
1339
1340void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1341 OS << "_Generic(";
1342 PrintExpr(Node->getControllingExpr());
1343 for (const GenericSelectionExpr::Association Assoc : Node->associations()) {
1344 OS << ", ";
1345 QualType T = Assoc.getType();
1346 if (T.isNull())
1347 OS << "default";
1348 else
1349 T.print(OS, Policy);
1350 OS << ": ";
1351 PrintExpr(Assoc.getAssociationExpr());
1352 }
1353 OS << ")";
1354}
1355
1356void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
1357 PrintExpr(Node->getLHS());
1358 OS << "[";
1359 PrintExpr(Node->getRHS());
1360 OS << "]";
1361}
1362
1363void StmtPrinter::VisitMatrixSubscriptExpr(MatrixSubscriptExpr *Node) {
1364 PrintExpr(Node->getBase());
1365 OS << "[";
1366 PrintExpr(Node->getRowIdx());
1367 OS << "]";
1368 OS << "[";
1369 PrintExpr(Node->getColumnIdx());
1370 OS << "]";
1371}
1372
1373void StmtPrinter::VisitOMPArraySectionExpr(OMPArraySectionExpr *Node) {
1374 PrintExpr(Node->getBase());
1375 OS << "[";
1376 if (Node->getLowerBound())
1377 PrintExpr(Node->getLowerBound());
1378 if (Node->getColonLocFirst().isValid()) {
1379 OS << ":";
1380 if (Node->getLength())
1381 PrintExpr(Node->getLength());
1382 }
1383 if (Node->getColonLocSecond().isValid()) {
1384 OS << ":";
1385 if (Node->getStride())
1386 PrintExpr(Node->getStride());
1387 }
1388 OS << "]";
1389}
1390
1391void StmtPrinter::VisitOMPArrayShapingExpr(OMPArrayShapingExpr *Node) {
1392 OS << "(";
1393 for (Expr *E : Node->getDimensions()) {
1394 OS << "[";
1395 PrintExpr(E);
1396 OS << "]";
1397 }
1398 OS << ")";
1399 PrintExpr(Node->getBase());
1400}
1401
1402void StmtPrinter::VisitOMPIteratorExpr(OMPIteratorExpr *Node) {
1403 OS << "iterator(";
1404 for (unsigned I = 0, E = Node->numOfIterators(); I < E; ++I) {
1405 auto *VD = cast<ValueDecl>(Node->getIteratorDecl(I));
1406 VD->getType().print(OS, Policy);
1407 const OMPIteratorExpr::IteratorRange Range = Node->getIteratorRange(I);
1408 OS << " " << VD->getName() << " = ";
1409 PrintExpr(Range.Begin);
1410 OS << ":";
1411 PrintExpr(Range.End);
1412 if (Range.Step) {
1413 OS << ":";
1414 PrintExpr(Range.Step);
1415 }
1416 if (I < E - 1)
1417 OS << ", ";
1418 }
1419 OS << ")";
1420}
1421
1422void StmtPrinter::PrintCallArgs(CallExpr *Call) {
1423 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
1424 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1425 // Don't print any defaulted arguments
1426 break;
1427 }
1428
1429 if (i) OS << ", ";
1430 PrintExpr(Call->getArg(i));
1431 }
1432}
1433
1434void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1435 PrintExpr(Call->getCallee());
1436 OS << "(";
1437 PrintCallArgs(Call);
1438 OS << ")";
1439}
1440
1441static bool isImplicitThis(const Expr *E) {
1442 if (const auto *TE = dyn_cast<CXXThisExpr>(E))
1443 return TE->isImplicit();
1444 return false;
1445}
1446
1447void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
1448 if (!Policy.SuppressImplicitBase || !isImplicitThis(Node->getBase())) {
1449 PrintExpr(Node->getBase());
1450
1451 auto *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
1452 FieldDecl *ParentDecl =
1453 ParentMember ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl())
1454 : nullptr;
1455
1456 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
1457 OS << (Node->isArrow() ? "->" : ".");
1458 }
1459
1460 if (auto *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1461 if (FD->isAnonymousStructOrUnion())
1462 return;
1463
1464 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1465 Qualifier->print(OS, Policy);
1466 if (Node->hasTemplateKeyword())
1467 OS << "template ";
1468 OS << Node->getMemberNameInfo();
1469 if (Node->hasExplicitTemplateArgs())
1470 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
1471}
1472
1473void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1474 PrintExpr(Node->getBase());
1475 OS << (Node->isArrow() ? "->isa" : ".isa");
1476}
1477
1478void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
1479 PrintExpr(Node->getBase());
1480 OS << ".";
1481 OS << Node->getAccessor().getName();
1482}
1483
1484void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
1485 OS << '(';
1486 Node->getTypeAsWritten().print(OS, Policy);
1487 OS << ')';
1488 PrintExpr(Node->getSubExpr());
1489}
1490
1491void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
1492 OS << '(';
1493 Node->getType().print(OS, Policy);
1494 OS << ')';
1495 PrintExpr(Node->getInitializer());
1496}
1497
1498void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
1499 // No need to print anything, simply forward to the subexpression.
1500 PrintExpr(Node->getSubExpr());
1501}
1502
1503void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1504 PrintExpr(Node->getLHS());
1505 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1506 PrintExpr(Node->getRHS());
1507}
1508
1509void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1510 PrintExpr(Node->getLHS());
1511 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1512 PrintExpr(Node->getRHS());
1513}
1514
1515void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1516 PrintExpr(Node->getCond());
1517 OS << " ? ";
1518 PrintExpr(Node->getLHS());
1519 OS << " : ";
1520 PrintExpr(Node->getRHS());
1521}
1522
1523// GNU extensions.
1524
1525void
1526StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1527 PrintExpr(Node->getCommon());
1528 OS << " ?: ";
1529 PrintExpr(Node->getFalseExpr());
1530}
1531
1532void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
1533 OS << "&&" << Node->getLabel()->getName();
1534}
1535
1536void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1537 OS << "(";
1538 PrintRawCompoundStmt(E->getSubStmt());
1539 OS << ")";
1540}
1541
1542void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1543 OS << "__builtin_choose_expr(";
1544 PrintExpr(Node->getCond());
1545 OS << ", ";
1546 PrintExpr(Node->getLHS());
1547 OS << ", ";
1548 PrintExpr(Node->getRHS());
1549 OS << ")";
1550}
1551
1552void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1553 OS << "__null";
1554}
1555
1556void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1557 OS << "__builtin_shufflevector(";
1558 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1559 if (i) OS << ", ";
1560 PrintExpr(Node->getExpr(i));
1561 }
1562 OS << ")";
1563}
1564
1565void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1566 OS << "__builtin_convertvector(";
1567 PrintExpr(Node->getSrcExpr());
1568 OS << ", ";
1569 Node->getType().print(OS, Policy);
1570 OS << ")";
1571}
1572
1573void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
1574 if (Node->getSyntacticForm()) {
1575 Visit(Node->getSyntacticForm());
1576 return;
1577 }
1578
1579 OS << "{";
1580 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1581 if (i) OS << ", ";
1582 if (Node->getInit(i))
1583 PrintExpr(Node->getInit(i));
1584 else
1585 OS << "{}";
1586 }
1587 OS << "}";
1588}
1589
1590void StmtPrinter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *Node) {
1591 // There's no way to express this expression in any of our supported
1592 // languages, so just emit something terse and (hopefully) clear.
1593 OS << "{";
1594 PrintExpr(Node->getSubExpr());
1595 OS << "}";
1596}
1597
1598void StmtPrinter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *Node) {
1599 OS << "*";
1600}
1601
1602void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1603 OS << "(";
1604 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1605 if (i) OS << ", ";
1606 PrintExpr(Node->getExpr(i));
1607 }
1608 OS << ")";
1609}
1610
1611void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
1612 bool NeedsEquals = true;
1613 for (const DesignatedInitExpr::Designator &D : Node->designators()) {
1614 if (D.isFieldDesignator()) {
1615 if (D.getDotLoc().isInvalid()) {
1616 if (IdentifierInfo *II = D.getFieldName()) {
1617 OS << II->getName() << ":";
1618 NeedsEquals = false;
1619 }
1620 } else {
1621 OS << "." << D.getFieldName()->getName();
1622 }
1623 } else {
1624 OS << "[";
1625 if (D.isArrayDesignator()) {
1626 PrintExpr(Node->getArrayIndex(D));
1627 } else {
1628 PrintExpr(Node->getArrayRangeStart(D));
1629 OS << " ... ";
1630 PrintExpr(Node->getArrayRangeEnd(D));
1631 }
1632 OS << "]";
1633 }
1634 }
1635
1636 if (NeedsEquals)
1637 OS << " = ";
1638 else
1639 OS << " ";
1640 PrintExpr(Node->getInit());
1641}
1642
1643void StmtPrinter::VisitDesignatedInitUpdateExpr(
1644 DesignatedInitUpdateExpr *Node) {
1645 OS << "{";
1646 OS << "/*base*/";
1647 PrintExpr(Node->getBase());
1648 OS << ", ";
1649
1650 OS << "/*updater*/";
1651 PrintExpr(Node->getUpdater());
1652 OS << "}";
1653}
1654
1655void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
1656 OS << "/*no init*/";
1657}
1658
1659void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
1660 if (Node->getType()->getAsCXXRecordDecl()) {
1661 OS << "/*implicit*/";
1662 Node->getType().print(OS, Policy);
1663 OS << "()";
1664 } else {
1665 OS << "/*implicit*/(";
1666 Node->getType().print(OS, Policy);
1667 OS << ')';
1668 if (Node->getType()->isRecordType())
1669 OS << "{}";
1670 else
1671 OS << 0;
1672 }
1673}
1674
1675void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
1676 OS << "__builtin_va_arg(";
1677 PrintExpr(Node->getSubExpr());
1678 OS << ", ";
1679 Node->getType().print(OS, Policy);
1680 OS << ")";
1681}
1682
1683void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1684 PrintExpr(Node->getSyntacticForm());
1685}
1686
1687void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
1688 const char *Name = nullptr;
1689 switch (Node->getOp()) {
1690#define BUILTIN(ID, TYPE, ATTRS)
1691#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1692 case AtomicExpr::AO ## ID: \
1693 Name = #ID "("; \
1694 break;
1695#include "clang/Basic/Builtins.def"
1696 }
1697 OS << Name;
1698
1699 // AtomicExpr stores its subexpressions in a permuted order.
1700 PrintExpr(Node->getPtr());
1701 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1702 Node->getOp() != AtomicExpr::AO__atomic_load_n &&
1703 Node->getOp() != AtomicExpr::AO__opencl_atomic_load) {
1704 OS << ", ";
1705 PrintExpr(Node->getVal1());
1706 }
1707 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1708 Node->isCmpXChg()) {
1709 OS << ", ";
1710 PrintExpr(Node->getVal2());
1711 }
1712 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1713 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1714 OS << ", ";
1715 PrintExpr(Node->getWeak());
1716 }
1717 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init &&
1718 Node->getOp() != AtomicExpr::AO__opencl_atomic_init) {
1719 OS << ", ";
1720 PrintExpr(Node->getOrder());
1721 }
1722 if (Node->isCmpXChg()) {
1723 OS << ", ";
1724 PrintExpr(Node->getOrderFail());
1725 }
1726 OS << ")";
1727}
1728
1729// C++
1730void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1731 OverloadedOperatorKind Kind = Node->getOperator();
1732 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1733 if (Node->getNumArgs() == 1) {
1734 OS << getOperatorSpelling(Kind) << ' ';
1735 PrintExpr(Node->getArg(0));
1736 } else {
1737 PrintExpr(Node->getArg(0));
1738 OS << ' ' << getOperatorSpelling(Kind);
1739 }
1740 } else if (Kind == OO_Arrow) {
1741 PrintExpr(Node->getArg(0));
1742 } else if (Kind == OO_Call) {
1743 PrintExpr(Node->getArg(0));
1744 OS << '(';
1745 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1746 if (ArgIdx > 1)
1747 OS << ", ";
1748 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1749 PrintExpr(Node->getArg(ArgIdx));
1750 }
1751 OS << ')';
1752 } else if (Kind == OO_Subscript) {
1753 PrintExpr(Node->getArg(0));
1754 OS << '[';
1755 PrintExpr(Node->getArg(1));
1756 OS << ']';
1757 } else if (Node->getNumArgs() == 1) {
1758 OS << getOperatorSpelling(Kind) << ' ';
1759 PrintExpr(Node->getArg(0));
1760 } else if (Node->getNumArgs() == 2) {
1761 PrintExpr(Node->getArg(0));
1762 OS << ' ' << getOperatorSpelling(Kind) << ' ';
1763 PrintExpr(Node->getArg(1));
1764 } else {
1765 llvm_unreachable("unknown overloaded operator");
1766 }
1767}
1768
1769void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1770 // If we have a conversion operator call only print the argument.
1771 CXXMethodDecl *MD = Node->getMethodDecl();
1772 if (MD && isa<CXXConversionDecl>(MD)) {
1773 PrintExpr(Node->getImplicitObjectArgument());
1774 return;
1775 }
1776 VisitCallExpr(cast<CallExpr>(Node));
1777}
1778
1779void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1780 PrintExpr(Node->getCallee());
1781 OS << "<<<";
1782 PrintCallArgs(Node->getConfig());
1783 OS << ">>>(";
1784 PrintCallArgs(Node);
1785 OS << ")";
1786}
1787
1788void StmtPrinter::VisitCXXRewrittenBinaryOperator(
1789 CXXRewrittenBinaryOperator *Node) {
1790 CXXRewrittenBinaryOperator::DecomposedForm Decomposed =
1791 Node->getDecomposedForm();
1792 PrintExpr(const_cast<Expr*>(Decomposed.LHS));
1793 OS << ' ' << BinaryOperator::getOpcodeStr(Decomposed.Opcode) << ' ';
1794 PrintExpr(const_cast<Expr*>(Decomposed.RHS));
1795}
1796
1797void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1798 OS << Node->getCastName() << '<';
1799 Node->getTypeAsWritten().print(OS, Policy);
1800 OS << ">(";
1801 PrintExpr(Node->getSubExpr());
1802 OS << ")";
1803}
1804
1805void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1806 VisitCXXNamedCastExpr(Node);
1807}
1808
1809void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1810 VisitCXXNamedCastExpr(Node);
1811}
1812
1813void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1814 VisitCXXNamedCastExpr(Node);
1815}
1816
1817void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1818 VisitCXXNamedCastExpr(Node);
1819}
1820
1821void StmtPrinter::VisitBuiltinBitCastExpr(BuiltinBitCastExpr *Node) {
1822 OS << "__builtin_bit_cast(";
1823 Node->getTypeInfoAsWritten()->getType().print(OS, Policy);
1824 OS << ", ";
1825 PrintExpr(Node->getSubExpr());
1826 OS << ")";
1827}
1828
1829void StmtPrinter::VisitCXXAddrspaceCastExpr(CXXAddrspaceCastExpr *Node) {
1830 VisitCXXNamedCastExpr(Node);
1831}
1832
1833void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1834 OS << "typeid(";
1835 if (Node->isTypeOperand()) {
1836 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1837 } else {
1838 PrintExpr(Node->getExprOperand());
1839 }
1840 OS << ")";
1841}
1842
1843void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1844 OS << "__uuidof(";
1845 if (Node->isTypeOperand()) {
1846 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1847 } else {
1848 PrintExpr(Node->getExprOperand());
1849 }
1850 OS << ")";
1851}
1852
1853void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1854 PrintExpr(Node->getBaseExpr());
1855 if (Node->isArrow())
1856 OS << "->";
1857 else
1858 OS << ".";
1859 if (NestedNameSpecifier *Qualifier =
1860 Node->getQualifierLoc().getNestedNameSpecifier())
1861 Qualifier->print(OS, Policy);
1862 OS << Node->getPropertyDecl()->getDeclName();
1863}
1864
1865void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) {
1866 PrintExpr(Node->getBase());
1867 OS << "[";
1868 PrintExpr(Node->getIdx());
1869 OS << "]";
1870}
1871
1872void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1873 switch (Node->getLiteralOperatorKind()) {
1874 case UserDefinedLiteral::LOK_Raw:
1875 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
1876 break;
1877 case UserDefinedLiteral::LOK_Template: {
1878 const auto *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1879 const TemplateArgumentList *Args =
1880 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1881 assert(Args);
1882
1883 if (Args->size() != 1) {
1884 OS << "operator\"\"" << Node->getUDSuffix()->getName();
1885 printTemplateArgumentList(OS, Args->asArray(), Policy);
1886 OS << "()";
1887 return;
1888 }
1889
1890 const TemplateArgument &Pack = Args->get(0);
1891 for (const auto &P : Pack.pack_elements()) {
1892 char C = (char)P.getAsIntegral().getZExtValue();
1893 OS << C;
1894 }
1895 break;
1896 }
1897 case UserDefinedLiteral::LOK_Integer: {
1898 // Print integer literal without suffix.
1899 const auto *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1900 OS << Int->getValue().toString(10, /*isSigned*/false);
1901 break;
1902 }
1903 case UserDefinedLiteral::LOK_Floating: {
1904 // Print floating literal without suffix.
1905 auto *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1906 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1907 break;
1908 }
1909 case UserDefinedLiteral::LOK_String:
1910 case UserDefinedLiteral::LOK_Character:
1911 PrintExpr(Node->getCookedLiteral());
1912 break;
1913 }
1914 OS << Node->getUDSuffix()->getName();
1915}
1916
1917void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1918 OS << (Node->getValue() ? "true" : "false");
1919}
1920
1921void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1922 OS << "nullptr";
1923}
1924
1925void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1926 OS << "this";
1927}
1928
1929void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1930 if (!Node->getSubExpr())
1931 OS << "throw";
1932 else {
1933 OS << "throw ";
1934 PrintExpr(Node->getSubExpr());
1935 }
1936}
1937
1938void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1939 // Nothing to print: we picked up the default argument.
1940}
1941
1942void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1943 // Nothing to print: we picked up the default initializer.
1944}
1945
1946void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1947 Node->getType().print(OS, Policy);
1948 // If there are no parens, this is list-initialization, and the braces are
1949 // part of the syntax of the inner construct.
1950 if (Node->getLParenLoc().isValid())
1951 OS << "(";
1952 PrintExpr(Node->getSubExpr());
1953 if (Node->getLParenLoc().isValid())
1954 OS << ")";
1955}
1956
1957void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1958 PrintExpr(Node->getSubExpr());
1959}
1960
1961void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1962 Node->getType().print(OS, Policy);
1963 if (Node->isStdInitListInitialization())
1964 /* Nothing to do; braces are part of creating the std::initializer_list. */;
1965 else if (Node->isListInitialization())
1966 OS << "{";
1967 else
1968 OS << "(";
1969 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1970 ArgEnd = Node->arg_end();
1971 Arg != ArgEnd; ++Arg) {
1972 if ((*Arg)->isDefaultArgument())
1973 break;
1974 if (Arg != Node->arg_begin())
1975 OS << ", ";
1976 PrintExpr(*Arg);
1977 }
1978 if (Node->isStdInitListInitialization())
1979 /* See above. */;
1980 else if (Node->isListInitialization())
1981 OS << "}";
1982 else
1983 OS << ")";
1984}
1985
1986void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1987 OS << '[';
1988 bool NeedComma = false;
1989 switch (Node->getCaptureDefault()) {
1990 case LCD_None:
1991 break;
1992
1993 case LCD_ByCopy:
1994 OS << '=';
1995 NeedComma = true;
1996 break;
1997
1998 case LCD_ByRef:
1999 OS << '&';
2000 NeedComma = true;
2001 break;
2002 }
2003 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
2004 CEnd = Node->explicit_capture_end();
2005 C != CEnd;
2006 ++C) {
2007 if (C->capturesVLAType())
2008 continue;
2009
2010 if (NeedComma)
2011 OS << ", ";
2012 NeedComma = true;
2013
2014 switch (C->getCaptureKind()) {
2015 case LCK_This:
2016 OS << "this";
2017 break;
2018
2019 case LCK_StarThis:
2020 OS << "*this";
2021 break;
2022
2023 case LCK_ByRef:
2024 if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C))
2025 OS << '&';
2026 OS << C->getCapturedVar()->getName();
2027 break;
2028
2029 case LCK_ByCopy:
2030 OS << C->getCapturedVar()->getName();
2031 break;
2032
2033 case LCK_VLAType:
2034 llvm_unreachable("VLA type in explicit captures.");
2035 }
2036
2037 if (C->isPackExpansion())
2038 OS << "...";
2039
2040 if (Node->isInitCapture(C)) {
2041 VarDecl *D = C->getCapturedVar();
2042
2043 llvm::StringRef Pre;
2044 llvm::StringRef Post;
2045 if (D->getInitStyle() == VarDecl::CallInit &&
2046 !isa<ParenListExpr>(D->getInit())) {
2047 Pre = "(";
2048 Post = ")";
2049 } else if (D->getInitStyle() == VarDecl::CInit) {
2050 Pre = " = ";
2051 }
2052
2053 OS << Pre;
2054 PrintExpr(D->getInit());
2055 OS << Post;
2056 }
2057 }
2058 OS << ']';
2059
2060 if (!Node->getExplicitTemplateParameters().empty()) {
2061 Node->getTemplateParameterList()->print(
2062 OS, Node->getLambdaClass()->getASTContext(),
2063 /*OmitTemplateKW*/true);
2064 }
2065
2066 if (Node->hasExplicitParameters()) {
2067 OS << '(';
2068 CXXMethodDecl *Method = Node->getCallOperator();
2069 NeedComma = false;
2070 for (const auto *P : Method->parameters()) {
2071 if (NeedComma) {
2072 OS << ", ";
2073 } else {
2074 NeedComma = true;
2075 }
2076 std::string ParamStr = P->getNameAsString();
2077