Coverage Report

Created: 2022-01-25 06:29

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