Coverage Report

Created: 2021-01-26 06:56

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