Coverage Report

Created: 2020-09-19 12:23

/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
98.5k
          NL(NL), Context(Context) {}
81
82
35.0k
    void PrintStmt(Stmt *S) { PrintStmt(S, Policy.Indentation); }
83
84
35.0k
    void PrintStmt(Stmt *S, int SubIndent) {
85
35.0k
      IndentLevel += SubIndent;
86
35.0k
      if (S && isa<Expr>(S)) {
87
        // If this is an expr used in a stmt context, indent and newline it.
88
6.96k
        Indent();
89
6.96k
        Visit(S);
90
6.96k
        OS << ";" << NL;
91
28.1k
      } else if (S) {
92
28.1k
        Visit(S);
93
0
      } else {
94
0
        Indent() << "<<<NULL STATEMENT>>>" << NL;
95
0
      }
96
35.0k
      IndentLevel -= SubIndent;
97
35.0k
    }
98
99
6.72k
    void PrintInitStmt(Stmt *S, unsigned PrefixWidth) {
100
      // FIXME: Cope better with odd prefix widths.
101
6.72k
      IndentLevel += (PrefixWidth + 1) / 2;
102
6.72k
      if (auto *DS = dyn_cast<DeclStmt>(S))
103
6.63k
        PrintRawDeclStmt(DS);
104
84
      else
105
84
        PrintExpr(cast<Expr>(S));
106
6.72k
      OS << "; ";
107
6.72k
      IndentLevel -= (PrefixWidth + 1) / 2;
108
6.72k
    }
109
110
6.76k
    void PrintControlledStmt(Stmt *S) {
111
6.76k
      if (auto *CS = dyn_cast<CompoundStmt>(S)) {
112
1.21k
        OS << " ";
113
1.21k
        PrintRawCompoundStmt(CS);
114
1.21k
        OS << NL;
115
5.55k
      } else {
116
5.55k
        OS << NL;
117
5.55k
        PrintStmt(S);
118
5.55k
      }
119
6.76k
    }
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
47.9k
    raw_ostream &Indent(int Delta = 0) {
140
232k
      for (int i = 0, e = IndentLevel+Delta; i < e; 
++i184k
)
141
184k
        OS << "  ";
142
47.9k
      return OS;
143
47.9k
    }
144
145
301k
    void Visit(Stmt* S) {
146
301k
      if (Helper && 
Helper->handledStmt(S,OS)105k
)
147
54.5k
          return;
148
246k
      else StmtVisitor<StmtPrinter>::Visit(S);
149
301k
    }
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
6.93k
void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
176
6.93k
  OS << "{" << NL;
177
6.93k
  for (auto *I : Node->body())
178
19.9k
    PrintStmt(I);
179
6.93k
180
6.93k
  Indent() << "}";
181
6.93k
}
182
183
29
void StmtPrinter::PrintRawDecl(Decl *D) {
184
29
  D->print(OS, Policy, IndentLevel);
185
29
}
186
187
13.6k
void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
188
13.6k
  SmallVector<Decl *, 2> Decls(S->decls());
189
13.6k
  Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
190
13.6k
}
191
192
580
void StmtPrinter::VisitNullStmt(NullStmt *Node) {
193
580
  Indent() << ";" << NL;
194
580
}
195
196
6.99k
void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
197
6.99k
  Indent();
198
6.99k
  PrintRawDeclStmt(Node);
199
6.99k
  OS << ";" << NL;
200
6.99k
}
201
202
5.53k
void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
203
5.53k
  Indent();
204
5.53k
  PrintRawCompoundStmt(Node);
205
5.53k
  OS << "" << NL;
206
5.53k
}
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
32
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
20
void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
231
49
  for (const auto *Attr : Node->getAttrs()) {
232
49
    Attr->printPretty(OS, Policy);
233
49
  }
234
20
235
20
  PrintStmt(Node->getSubStmt(), 0);
236
20
}
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
28
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
28
258
28
  if (Stmt *Else = If->getElse()) {
259
4
    OS << "else";
260
4
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
16
void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
293
16
  Indent() << "while (";
294
16
  if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
295
1
    PrintRawDeclStmt(DS);
296
15
  else
297
15
    PrintExpr(Node->getCond());
298
16
  OS << ")" << NL;
299
16
  PrintStmt(Node->getBody());
300
16
}
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
2
313
2
  OS << "while (";
314
2
  PrintExpr(Node->getCond());
315
2
  OS << ");" << NL;
316
2
}
317
318
6.72k
void StmtPrinter::VisitForStmt(ForStmt *Node) {
319
6.72k
  Indent() << "for (";
320
6.72k
  if (Node->getInit())
321
6.72k
    PrintInitStmt(Node->getInit(), 5);
322
2
  else
323
2
    OS << (Node->getCond() ? 
"; "0
: ";");
324
6.72k
  if (Node->getCond())
325
6.72k
    PrintExpr(Node->getCond());
326
6.72k
  OS << ";";
327
6.72k
  if (Node->getInc()) {
328
6.72k
    OS << " ";
329
6.72k
    PrintExpr(Node->getInc());
330
6.72k
  }
331
6.72k
  OS << ")";
332
6.72k
  PrintControlledStmt(Node->getBody());
333
6.72k
}
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
18
void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
348
18
  Indent() << "for (";
349
18
  if (Node->getInit())
350
1
    PrintInitStmt(Node->getInit(), 5);
351
18
  PrintingPolicy SubPolicy(Policy);
352
18
  SubPolicy.SuppressInitializers = true;
353
18
  Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
354
18
  OS << " : ";
355
18
  PrintExpr(Node->getRangeInit());
356
18
  OS << ")";
357
18
  PrintControlledStmt(Node->getBody());
358
18
}
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
0
367
0
  if (NestedNameSpecifier *Qualifier
368
0
        = Node->getQualifierLoc().getNestedNameSpecifier())
369
0
    Qualifier->print(OS, Policy);
370
0
371
0
  OS << Node->getNameInfo() << ") ";
372
0
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.20k
void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
399
2.20k
  Indent() << "return";
400
2.20k
  if (Node->getRetValue()) {
401
2.12k
    OS << " ";
402
2.12k
    PrintExpr(Node->getRetValue());
403
2.12k
  }
404
2.20k
  OS << ";";
405
2.20k
  if (Policy.IncludeNewlines) OS << NL;
406
2.20k
}
407
408
13
void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
409
13
  Indent() << "asm ";
410
13
411
13
  if (Node->isVolatile())
412
1
    OS << "volatile ";
413
13
414
13
  if (Node->isAsmGoto())
415
8
    OS << "goto ";
416
13
417
13
  OS << "(";
418
13
  VisitStringLiteral(Node->getAsmString());
419
13
420
  // Outputs
421
13
  if (Node->getNumOutputs() != 0 || 
Node->getNumInputs() != 010
||
422
4
      Node->getNumClobbers() != 0 || Node->getNumLabels() != 0)
423
11
    OS << " : ";
424
13
425
16
  for (unsigned i = 0, e = Node->getNumOutputs(); i != e; 
++i3
) {
426
3
    if (i != 0)
427
0
      OS << ", ";
428
3
429
3
    if (!Node->getOutputName(i).empty()) {
430
0
      OS << '[';
431
0
      OS << Node->getOutputName(i);
432
0
      OS << "] ";
433
0
    }
434
3
435
3
    VisitStringLiteral(Node->getOutputConstraintLiteral(i));
436
3
    OS << " (";
437
3
    Visit(Node->getOutputExpr(i));
438
3
    OS << ")";
439
3
  }
440
13
441
  // Inputs
442
13
  if (Node->getNumInputs() != 0 || 
Node->getNumClobbers() != 06
||
443
6
      Node->getNumLabels() != 0)
444
9
    OS << " : ";
445
13
446
21
  for (unsigned i = 0, e = Node->getNumInputs(); i != e; 
++i8
) {
447
8
    if (i != 0)
448
1
      OS << ", ";
449
8
450
8
    if (!Node->getInputName(i).empty()) {
451
0
      OS << '[';
452
0
      OS << Node->getInputName(i);
453
0
      OS << "] ";
454
0
    }
455
8
456
8
    VisitStringLiteral(Node->getInputConstraintLiteral(i));
457
8
    OS << " (";
458
8
    Visit(Node->getInputExpr(i));
459
8
    OS << ")";
460
8
  }
461
13
462
  // Clobbers
463
13
  if (Node->getNumClobbers() != 0 || Node->getNumLabels())
464
8
    OS << " : ";
465
13
466
13
  for (unsigned i = 0, e = Node->getNumClobbers(); i != e; 
++i0
) {
467
0
    if (i != 0)
468
0
      OS << ", ";
469
0
470
0
    VisitStringLiteral(Node->getClobberStringLiteral(i));
471
0
  }
472
13
473
  // Labels
474
13
  if (Node->getNumLabels() != 0)
475
8
    OS << " : ";
476
13
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
13
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
3
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
3
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
11.6k
                                              bool ForceNoStmt) {
641
11.6k
  OMPClausePrinter Printer(OS, Policy);
642
11.6k
  ArrayRef<OMPClause *> Clauses = S->clauses();
643
11.6k
  for (auto *Clause : Clauses)
644
25.1k
    if (Clause && !Clause->isImplicit()) {
645
24.3k
      OS << ' ';
646
24.3k
      Printer.Visit(Clause);
647
24.3k
    }
648
11.6k
  OS << NL;
649
11.6k
  if (!ForceNoStmt && 
S->hasAssociatedStmt()9.82k
)
650
9.42k
    PrintStmt(S->getRawStmt());
651
11.6k
}
652
653
405
void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
654
405
  Indent() << "#pragma omp parallel";
655
405
  PrintOMPExecutableDirective(Node);
656
405
}
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
69
void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
704
69
  Indent() << "#pragma omp parallel for";
705
69
  PrintOMPExecutableDirective(Node);
706
69
}
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.30k
void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
777
2.30k
  Indent() << "#pragma omp target";
778
2.30k
  PrintOMPExecutableDirective(Node);
779
2.30k
}
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
225
    OMPTargetUpdateDirective *Node) {
870
225
  Indent() << "#pragma omp target update";
871
225
  PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
872
225
}
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
4.28k
void StmtPrinter::VisitConstantExpr(ConstantExpr *Node) {
965
4.28k
  PrintExpr(Node->getSubExpr());
966
4.28k
}
967
968
50.2k
void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
969
50.2k
  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
47.2k
  if (NestedNameSpecifier *Qualifier = Node->getQualifier())
974
374
    Qualifier->print(OS, Policy);
975
47.2k
  if (Node->hasTemplateKeyword())
976
2
    OS << "template ";
977
47.2k
  OS << Node->getNameInfo();
978
47.2k
  if (Node->hasExplicitTemplateArgs())
979
684
    printTemplateArgumentList(OS, Node->template_arguments(), Policy);
980
47.2k
}
981
982
void StmtPrinter::VisitDependentScopeDeclRefExpr(
983
152
                                           DependentScopeDeclRefExpr *Node) {
984
152
  if (NestedNameSpecifier *Qualifier = Node->getQualifier())
985
152
    Qualifier->print(OS, Policy);
986
152
  if (Node->hasTemplateKeyword())
987
8
    OS << "template ";
988
152
  OS << Node->getNameInfo();
989
152
  if (Node->hasExplicitTemplateArgs())
990
6
    printTemplateArgumentList(OS, Node->template_arguments(), Policy);
991
152
}
992
993
37
void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
994
37
  if (Node->getQualifier())
995
10
    Node->getQualifier()->print(OS, Policy);
996
37
  if (Node->hasTemplateKeyword())
997
5
    OS << "template ";
998
37
  OS << Node->getNameInfo();
999
37
  if (Node->hasExplicitTemplateArgs())
1000
24
    printTemplateArgumentList(OS, Node->template_arguments(), Policy);
1001
37
}
1002
1003
1
static bool isImplicitSelf(const Expr *E) {
1004
1
  if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
1005
1
    if (const auto *PD = dyn_cast<ImplicitParamDecl>(DRE->getDecl())) {
1006
1
      if (PD->getParameterKind() == ImplicitParamDecl::ObjCSelf &&
1007
1
          DRE->getBeginLoc().isInvalid())
1008
1
        return true;
1009
0
    }
1010
1
  }
1011
0
  return false;
1012
0
}
1013
1014
28
void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
1015
28
  if (Node->getBase()) {
1016
28
    if (!Policy.SuppressImplicitBase ||
1017
27
        
!isImplicitSelf(Node->getBase()->IgnoreImpCasts())1
) {
1018
27
      PrintExpr(Node->getBase());
1019
25
      OS << (Node->isArrow() ? "->" : 
"."2
);
1020
27
    }
1021
28
  }
1022
28
  OS << *Node->getDecl();
1023
28
}
1024
1025
5
void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
1026
5
  if (Node->isSuperReceiver())
1027
1
    OS << "super.";
1028
4
  else if (Node->isObjectReceiver() && 
Node->getBase()1
) {
1029
1
    PrintExpr(Node->getBase());
1030
1
    OS << ".";
1031
3
  } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
1032
3
    OS << Node->getClassReceiver()->getName() << ".";
1033
3
  }
1034
5
1035
5
  if (Node->isImplicitProperty()) {
1036
4
    if (const auto *Getter = Node->getImplicitPropertyGetter())
1037
3
      Getter->getSelector().print(OS);
1038
1
    else
1039
1
      OS << SelectorTable::getPropertyNameFromSetterSelector(
1040
1
          Node->getImplicitPropertySetter()->getSelector());
1041
4
  } else
1042
1
    OS << Node->getExplicitProperty()->getName();
1043
5
}
1044
1045
4
void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
1046
4
  PrintExpr(Node->getBaseExpr());
1047
4
  OS << "[";
1048
4
  PrintExpr(Node->getKeyExpr());
1049
4
  OS << "]";
1050
4
}
1051
1052
15
void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
1053
15
  OS << PredefinedExpr::getIdentKindName(Node->getIdentKind());
1054
15
}
1055
1056
274
void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
1057
274
  unsigned value = Node->getValue();
1058
274
1059
274
  switch (Node->getKind()) {
1060
167
  case CharacterLiteral::Ascii: break; // no prefix.
1061
43
  case CharacterLiteral::Wide:  OS << 'L'; break;
1062
4
  case CharacterLiteral::UTF8:  OS << "u8"; break;
1063
32
  case CharacterLiteral::UTF16: OS << 'u'; break;
1064
28
  case CharacterLiteral::UTF32: OS << 'U'; break;
1065
274
  }
1066
274
1067
274
  switch (value) {
1068
15
  case '\\':
1069
15
    OS << "'\\\\'";
1070
15
    break;
1071
15
  case '\'':
1072
15
    OS << "'\\''";
1073
15
    break;
1074
10
  case '\a':
1075
    // TODO: K&R: the meaning of '\\a' is different in traditional C
1076
10
    OS << "'\\a'";
1077
10
    break;
1078
10
  case '\b':
1079
10
    OS << "'\\b'";
1080
10
    break;
1081
  // Nonstandard escape sequence.
1082
  /*case '\e':
1083
    OS << "'\\e'";
1084
    break;*/
1085
10
  case '\f':
1086
10
    OS << "'\\f'";
1087
10
    break;
1088
10
  case '\n':
1089
10
    OS << "'\\n'";
1090
10
    break;
1091
10
  case '\r':
1092
10
    OS << "'\\r'";
1093
10
    break;
1094
10
  case '\t':
1095
10
    OS << "'\\t'";
1096
10
    break;
1097
10
  case '\v':
1098
10
    OS << "'\\v'";
1099
10
    break;
1100
174
  default:
1101
    // A character literal might be sign-extended, which
1102
    // would result in an invalid \U escape sequence.
1103
    // FIXME: multicharacter literals such as '\xFF\xFF\xFF\xFF'
1104
    // are not correctly handled.
1105
174
    if ((value & ~0xFFu) == ~0xFFu && 
Node->getKind() == CharacterLiteral::Ascii1
)
1106
1
      value &= 0xFFu;
1107
174
    if (value < 256 && 
isPrintable((unsigned char)value)167
)
1108
105
      OS << "'" << (char)value << "'";
1109
69
    else if (value < 256)
1110
62
      OS << "'\\x" << llvm::format("%02x", value) << "'";
1111
7
    else if (value <= 0xFFFF)
1112
7
      OS << "'\\u" << llvm::format("%04x", value) << "'";
1113
0
    else
1114
0
      OS << "'\\U" << llvm::format("%08x", value) << "'";
1115
274
  }
1116
274
}
1117
1118
/// Prints the given expression using the original source text. Returns true on
1119
/// success, false otherwise.
1120
static bool printExprAsWritten(raw_ostream &OS, Expr *E,
1121
13
                               const ASTContext *Context) {
1122
13
  if (!Context)
1123
6
    return false;
1124
7
  bool Invalid = false;
1125
7
  StringRef Source = Lexer::getSourceText(
1126
7
      CharSourceRange::getTokenRange(E->getSourceRange()),
1127
7
      Context->getSourceManager(), Context->getLangOpts(), &Invalid);
1128
7
  if (!Invalid) {
1129
6
    OS << Source;
1130
6
    return true;
1131
6
  }
1132
1
  return false;
1133
1
}
1134
1135
24.8k
void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
1136
24.8k
  if (Policy.ConstantsAsWritten && 
printExprAsWritten(OS, Node, Context)12
)
1137
5
    return;
1138
24.8k
  bool isSigned = Node->getType()->isSignedIntegerType();
1139
24.8k
  OS << Node->getValue().toString(10, isSigned);
1140
24.8k
1141
  // Emit suffixes.  Integer literals are always a builtin integer type.
1142
24.8k
  switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1143
0
  default: llvm_unreachable("Unexpected type for integer literal!");
1144
2
  case BuiltinType::Char_S:
1145
2
  case BuiltinType::Char_U:    OS << "i8"; break;
1146
1
  case BuiltinType::UChar:     OS << "Ui8"; break;
1147
2
  case BuiltinType::Short:     OS << "i16"; break;
1148
1
  case BuiltinType::UShort:    OS << "Ui16"; break;
1149
24.6k
  case BuiltinType::Int:       break; // no suffix.
1150
101
  case BuiltinType::UInt:      OS << 'U'; break;
1151
14
  case BuiltinType::Long:      OS << 'L'; break;
1152
29
  case BuiltinType::ULong:     OS << "UL"; break;
1153
8
  case BuiltinType::LongLong:  OS << "LL"; break;
1154
2
  case BuiltinType::ULongLong: OS << "ULL"; break;
1155
24.8k
  }
1156
24.8k
}
1157
1158
14
void StmtPrinter::VisitFixedPointLiteral(FixedPointLiteral *Node) {
1159
14
  if (Policy.ConstantsAsWritten && 
printExprAsWritten(OS, Node, Context)0
)
1160
0
    return;
1161
14
  OS << Node->getValueAsString(/*Radix=*/10);
1162
14
1163
14
  switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1164
0
    default: llvm_unreachable("Unexpected type for fixed point literal!");
1165
0
    case BuiltinType::ShortFract:   OS << "hr"; break;
1166
0
    case BuiltinType::ShortAccum:   OS << "hk"; break;
1167
0
    case BuiltinType::UShortFract:  OS << "uhr"; break;
1168
0
    case BuiltinType::UShortAccum:  OS << "uhk"; break;
1169
8
    case BuiltinType::Fract:        OS << "r"; break;
1170
4
    case BuiltinType::Accum:        OS << "k"; break;
1171
0
    case BuiltinType::UFract:       OS << "ur"; break;
1172
0
    case BuiltinType::UAccum:       OS << "uk"; break;
1173
0
    case BuiltinType::LongFract:    OS << "lr"; break;
1174
2
    case BuiltinType::LongAccum:    OS << "lk"; break;
1175
0
    case BuiltinType::ULongFract:   OS << "ulr"; break;
1176
0
    case BuiltinType::ULongAccum:   OS << "ulk"; break;
1177
14
  }
1178
14
}
1179
1180
static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1181
153
                                 bool PrintSuffix) {
1182
153
  SmallString<16> Str;
1183
153
  Node->getValue().toString(Str);
1184
153
  OS << Str;
1185
153
  if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1186
112
    OS << '.'; // Trailing dot in order to separate from ints.
1187
153
1188
153
  if (!PrintSuffix)
1189
2
    return;
1190
151
1191
  // Emit suffixes.  Float literals are always a builtin float type.
1192
151
  switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1193
0
  default: llvm_unreachable("Unexpected type for float literal!");
1194
0
  case BuiltinType::Half:       break; // FIXME: suffix?
1195
118
  case BuiltinType::Double:     break; // no suffix.
1196
0
  case BuiltinType::Float16:    OS << "F16"; break;
1197
21
  case BuiltinType::Float:      OS << 'F'; break;
1198
12
  case BuiltinType::LongDouble: OS << 'L'; break;
1199
0
  case BuiltinType::Float128:   OS << 'Q'; break;
1200
151
  }
1201
151
}
1202
1203
152
void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
1204
152
  if (Policy.ConstantsAsWritten && 
printExprAsWritten(OS, Node, Context)1
)
1205
1
    return;
1206
151
  PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
1207
151
}
1208
1209
22
void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1210
22
  PrintExpr(Node->getSubExpr());
1211
22
  OS << "i";
1212
22
}
1213
1214
747
void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
1215
747
  Str->outputString(OS);
1216
747
}
1217
1218
1.97k
void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1219
1.97k
  OS << "(";
1220
1.97k
  PrintExpr(Node->getSubExpr());
1221
1.97k
  OS << ")";
1222
1.97k
}
1223
1224
23.6k
void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
1225
23.6k
  if (!Node->isPostfix()) {
1226
22.0k
    OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1227
22.0k
1228
    // Print a space if this is an "identifier operator" like __real, or if
1229
    // it might be concatenated incorrectly like '+'.
1230
22.0k
    switch (Node->getOpcode()) {
1231
21.9k
    default: break;
1232
8
    case UO_Real:
1233
8
    case UO_Imag:
1234
8
    case UO_Extension:
1235
8
      OS << ' ';
1236
8
      break;
1237
128
    case UO_Plus:
1238
128
    case UO_Minus:
1239
128
      if (isa<UnaryOperator>(Node->getSubExpr()))
1240
0
        OS << ' ';
1241
128
      break;
1242
23.6k
    }
1243
23.6k
  }
1244
23.6k
  PrintExpr(Node->getSubExpr());
1245
23.6k
1246
23.6k
  if (Node->isPostfix())
1247
1.57k
    OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1248
23.6k
}
1249
1250
2
void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1251
2
  OS << "__builtin_offsetof(";
1252
2
  Node->getTypeSourceInfo()->getType().print(OS, Policy);
1253
2
  OS << ", ";
1254
2
  bool PrintedSomething = false;
1255
4
  for (unsigned i = 0, n = Node->getNumComponents(); i < n; 
++i2
) {
1256
2
    OffsetOfNode ON = Node->getComponent(i);
1257
2
    if (ON.getKind() == OffsetOfNode::Array) {
1258
      // Array node
1259
0
      OS << "[";
1260
0
      PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1261
0
      OS << "]";
1262
0
      PrintedSomething = true;
1263
0
      continue;
1264
0
    }
1265
2
1266
    // Skip implicit base indirections.
1267
2
    if (ON.getKind() == OffsetOfNode::Base)
1268
0
      continue;
1269
2
1270
    // Field or identifier node.
1271
2
    IdentifierInfo *Id = ON.getFieldName();
1272
2
    if (!Id)
1273
0
      continue;
1274
2
1275
2
    if (PrintedSomething)
1276
0
      OS << ".";
1277
2
    else
1278
2
      PrintedSomething = true;
1279
2
    OS << Id->getName();
1280
2
  }
1281
2
  OS << ")";
1282
2
}
1283
1284
void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(
1285
528
    UnaryExprOrTypeTraitExpr *Node) {
1286
528
  const char *Spelling = getTraitSpelling(Node->getKind());
1287
528
  if (Node->getKind() == UETT_AlignOf) {
1288
15
    if (Policy.Alignof)
1289
15
      Spelling = "alignof";
1290
0
    else if (Policy.UnderscoreAlignof)
1291
0
      Spelling = "_Alignof";
1292
0
    else
1293
0
      Spelling = "__alignof";
1294
15
  }
1295
528
1296
528
  OS << Spelling;
1297
528
1298
528
  if (Node->isArgumentType()) {
1299
381
    OS << '(';
1300
381
    Node->getArgumentType().print(OS, Policy);
1301
381
    OS << ')';
1302
147
  } else {
1303
147
    OS << " ";
1304
147
    PrintExpr(Node->getArgumentExpr());
1305
147
  }
1306
528
}
1307
1308
0
void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1309
0
  OS << "_Generic(";
1310
0
  PrintExpr(Node->getControllingExpr());
1311
0
  for (const GenericSelectionExpr::Association Assoc : Node->associations()) {
1312
0
    OS << ", ";
1313
0
    QualType T = Assoc.getType();
1314
0
    if (T.isNull())
1315
0
      OS << "default";
1316
0
    else
1317
0
      T.print(OS, Policy);
1318
0
    OS << ": ";
1319
0
    PrintExpr(Assoc.getAssociationExpr());
1320
0
  }
1321
0
  OS << ")";
1322
0
}
1323
1324
1.39k
void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
1325
1.39k
  PrintExpr(Node->getLHS());
1326
1.39k
  OS << "[";
1327
1.39k
  PrintExpr(Node->getRHS());
1328
1.39k
  OS << "]";
1329
1.39k
}
1330
1331
0
void StmtPrinter::VisitMatrixSubscriptExpr(MatrixSubscriptExpr *Node) {
1332
0
  PrintExpr(Node->getBase());
1333
0
  OS << "[";
1334
0
  PrintExpr(Node->getRowIdx());
1335
0
  OS << "]";
1336
0
  OS << "[";
1337
0
  PrintExpr(Node->getColumnIdx());
1338
0
  OS << "]";
1339
0
}
1340
1341
2.36k
void StmtPrinter::VisitOMPArraySectionExpr(OMPArraySectionExpr *Node) {
1342
2.36k
  PrintExpr(Node->getBase());
1343
2.36k
  OS << "[";
1344
2.36k
  if (Node->getLowerBound())
1345
1.42k
    PrintExpr(Node->getLowerBound());
1346
2.36k
  if (Node->getColonLocFirst().isValid()) {
1347
2.35k
    OS << ":";
1348
2.35k
    if (Node->getLength())
1349
1.66k
      PrintExpr(Node->getLength());
1350
2.35k
  }
1351
2.36k
  if (Node->getColonLocSecond().isValid()) {
1352
96
    OS << ":";
1353
96
    if (Node->getStride())
1354
96
      PrintExpr(Node->getStride());
1355
96
  }
1356
2.36k
  OS << "]";
1357
2.36k
}
1358
1359
96
void StmtPrinter::VisitOMPArrayShapingExpr(OMPArrayShapingExpr *Node) {
1360
96
  OS << "(";
1361
208
  for (Expr *E : Node->getDimensions()) {
1362
208
    OS << "[";
1363
208
    PrintExpr(E);
1364
208
    OS << "]";
1365
208
  }
1366
96
  OS << ")";
1367
96
  PrintExpr(Node->getBase());
1368
96
}
1369
1370
28
void StmtPrinter::VisitOMPIteratorExpr(OMPIteratorExpr *Node) {
1371
28
  OS << "iterator(";
1372
76
  for (unsigned I = 0, E = Node->numOfIterators(); I < E; 
++I48
) {
1373
48
    auto *VD = cast<ValueDecl>(Node->getIteratorDecl(I));
1374
48
    VD->getType().print(OS, Policy);
1375
48
    const OMPIteratorExpr::IteratorRange Range = Node->getIteratorRange(I);
1376
48
    OS << " " << VD->getName() << " = ";
1377
48
    PrintExpr(Range.Begin);
1378
48
    OS << ":";
1379
48
    PrintExpr(Range.End);
1380
48
    if (Range.Step) {
1381
20
      OS << ":";
1382
20
      PrintExpr(Range.Step);
1383
20
    }
1384
48
    if (I < E - 1)
1385
20
      OS << ", ";
1386
48
  }
1387
28
  OS << ")";
1388
28
}
1389
1390
10.0k
void StmtPrinter::PrintCallArgs(CallExpr *Call) {
1391
13.0k
  for (unsigned i = 0, e = Call->getNumArgs(); i != e; 
++i2.99k
) {
1392
2.99k
    if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1393
      // Don't print any defaulted arguments
1394
2
      break;
1395
2
    }
1396
2.99k
1397
2.99k
    if (i) 
OS << ", "1.27k
;
1398
2.99k
    PrintExpr(Call->getArg(i));
1399
2.99k
  }
1400
10.0k
}
1401
1402
10.0k
void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1403
10.0k
  PrintExpr(Call->getCallee());
1404
10.0k
  OS << "(";
1405
10.0k
  PrintCallArgs(Call);
1406
10.0k
  OS << ")";
1407
10.0k
}
1408
1409
1
static bool isImplicitThis(const Expr *E) {
1410
1
  if (const auto *TE = dyn_cast<CXXThisExpr>(E))
1411
1
    return TE->isImplicit();
1412
0
  return false;
1413
0
}
1414
1415
6.16k
void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
1416
6.16k
  if (!Policy.SuppressImplicitBase || 
!isImplicitThis(Node->getBase())1
) {
1417
6.16k
    PrintExpr(Node->getBase());
1418
6.16k
1419
6.16k
    auto *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
1420
6.16k
    FieldDecl *ParentDecl =
1421
643
        ParentMember ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl())
1422
5.52k
                     : nullptr;
1423
6.16k
1424
6.16k
    if (!ParentDecl || 
!ParentDecl->isAnonymousStructOrUnion()643
)
1425
6.16k
      OS << (Node->isArrow() ? 
"->"4.01k
:
"."2.15k
);
1426
6.16k
  }
1427
6.16k
1428
6.16k
  if (auto *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1429
5.85k
    if (FD->isAnonymousStructOrUnion())
1430
4
      return;
1431
6.16k
1432
6.16k
  if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1433
658
    Qualifier->print(OS, Policy);
1434
6.16k
  if (Node->hasTemplateKeyword())
1435
3
    OS << "template ";
1436
6.16k
  OS << Node->getMemberNameInfo();
1437
6.16k
  if (Node->hasExplicitTemplateArgs())
1438
9
    printTemplateArgumentList(OS, Node->template_arguments(), Policy);
1439
6.16k
}
1440
1441
0
void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1442
0
  PrintExpr(Node->getBase());
1443
0
  OS << (Node->isArrow() ? "->isa" : ".isa");
1444
0
}
1445
1446
8
void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
1447
8
  PrintExpr(Node->getBase());
1448
8
  OS << ".";
1449
8
  OS << Node->getAccessor().getName();
1450
8
}
1451
1452
2.30k
void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
1453
2.30k
  OS << '(';
1454
2.30k
  Node->getTypeAsWritten().print(OS, Policy);
1455
2.30k
  OS << ')';
1456
2.30k
  PrintExpr(Node->getSubExpr());
1457
2.30k
}
1458
1459
22
void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
1460
22
  OS << '(';
1461
22
  Node->getType().print(OS, Policy);
1462
22
  OS << ')';
1463
22
  PrintExpr(Node->getInitializer());
1464
22
}
1465
1466
48.9k
void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
1467
  // No need to print anything, simply forward to the subexpression.
1468
48.9k
  PrintExpr(Node->getSubExpr());
1469
48.9k
}
1470
1471
15.2k
void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1472
15.2k
  PrintExpr(Node->getLHS());
1473
15.2k
  OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1474
15.2k
  PrintExpr(Node->getRHS());
1475
15.2k
}
1476
1477
447
void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1478
447
  PrintExpr(Node->getLHS());
1479
447
  OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1480
447
  PrintExpr(Node->getRHS());
1481
447
}
1482
1483
93
void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1484
93
  PrintExpr(Node->getCond());
1485
93
  OS << " ? ";
1486
93
  PrintExpr(Node->getLHS());
1487
93
  OS << " : ";
1488
93
  PrintExpr(Node->getRHS());
1489
93
}
1490
1491
// GNU extensions.
1492
1493
void
1494
28
StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1495
28
  PrintExpr(Node->getCommon());
1496
28
  OS << " ?: ";
1497
28
  PrintExpr(Node->getFalseExpr());
1498
28
}
1499
1500
46
void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
1501
46
  OS << "&&" << Node->getLabel()->getName();
1502
46
}
1503
1504
6
void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1505
6
  OS << "(";
1506
6
  PrintRawCompoundStmt(E->getSubStmt());
1507
6
  OS << ")";
1508
6
}
1509
1510
4
void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1511
4
  OS << "__builtin_choose_expr(";
1512
4
  PrintExpr(Node->getCond());
1513
4
  OS << ", ";
1514
4
  PrintExpr(Node->getLHS());
1515
4
  OS << ", ";
1516
4
  PrintExpr(Node->getRHS());
1517
4
  OS << ")";
1518
4
}
1519
1520
4
void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1521
4
  OS << "__null";
1522
4
}
1523
1524
0
void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1525
0
  OS << "__builtin_shufflevector(";
1526
0
  for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1527
0
    if (i) OS << ", ";
1528
0
    PrintExpr(Node->getExpr(i));
1529
0
  }
1530
0
  OS << ")";
1531
0
}
1532
1533
0
void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1534
0
  OS << "__builtin_convertvector(";
1535
0
  PrintExpr(Node->getSrcExpr());
1536
0
  OS << ", ";
1537
0
  Node->getType().print(OS, Policy);
1538
0
  OS << ")";
1539
0
}
1540
1541
1.14k
void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
1542
1.14k
  if (Node->getSyntacticForm()) {
1543
517
    Visit(Node->getSyntacticForm());
1544
517
    return;
1545
517
  }
1546
632
1547
632
  OS << "{";
1548
1.60k
  for (unsigned i = 0, e = Node->getNumInits(); i != e; 
++i972
) {
1549
972
    if (i) 
OS << ", "373
;
1550
972
    if (Node->getInit(i))
1551
972
      PrintExpr(Node->getInit(i));
1552
0
    else
1553
0
      OS << "{}";
1554
972
  }
1555
632
  OS << "}";
1556
632
}
1557
1558
0
void StmtPrinter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *Node) {
1559
  // There's no way to express this expression in any of our supported
1560
  // languages, so just emit something terse and (hopefully) clear.
1561
0
  OS << "{";
1562
0
  PrintExpr(Node->getSubExpr());
1563
0
  OS << "}";
1564
0
}
1565
1566
0
void StmtPrinter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *Node) {
1567
0
  OS << "*";
1568
0
}
1569
1570
8
void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1571
8
  OS << "(";
1572
17
  for (unsigned i = 0, e = Node->getNumExprs(); i != e; 
++i9
) {
1573
9
    if (i) 
OS << ", "1
;
1574
9
    PrintExpr(Node->getExpr(i));
1575
9
  }
1576
8
  OS << ")";
1577
8
}
1578
1579
38
void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
1580
38
  bool NeedsEquals = true;
1581
70
  for (const DesignatedInitExpr::Designator &D : Node->designators()) {
1582
70
    if (D.isFieldDesignator()) {
1583
42
      if (D.getDotLoc().isInvalid()) {
1584
3
        if (IdentifierInfo *II = D.getFieldName()) {
1585
1
          OS << II->getName() << ":";
1586
1
          NeedsEquals = false;
1587
1
        }
1588
39
      } else {
1589
39
        OS << "." << D.getFieldName()->getName();
1590
39
      }
1591
28
    } else {
1592
28
      OS << "[";
1593
28
      if (D.isArrayDesignator()) {
1594
28
        PrintExpr(Node->getArrayIndex(D));
1595
0
      } else {
1596
0
        PrintExpr(Node->getArrayRangeStart(D));
1597
0
        OS << " ... ";
1598
0
        PrintExpr(Node->getArrayRangeEnd(D));
1599
0
      }
1600
28
      OS << "]";
1601
28
    }
1602
70
  }
1603
38
1604
38
  if (NeedsEquals)
1605
37
    OS << " = ";
1606
1
  else
1607
1
    OS << " ";
1608
38
  PrintExpr(Node->getInit());
1609
38
}
1610
1611
void StmtPrinter::VisitDesignatedInitUpdateExpr(
1612
2
    DesignatedInitUpdateExpr *Node) {
1613
2
  OS << "{";
1614
2
  OS << "/*base*/";
1615
2
  PrintExpr(Node->getBase());
1616
2
  OS << ", ";
1617
2
1618
2
  OS << "/*updater*/";
1619
2
  PrintExpr(Node->getUpdater());
1620
2
  OS << "}";
1621
2
}
1622
1623
4
void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
1624
4
  OS << "/*no init*/";
1625
4
}
1626
1627
6
void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
1628
6
  if (Node->getType()->getAsCXXRecordDecl()) {
1629
0
    OS << "/*implicit*/";
1630
0
    Node->getType().print(OS, Policy);
1631
0
    OS << "()";
1632
6
  } else {
1633
6
    OS << "/*implicit*/(";
1634
6
    Node->getType().print(OS, Policy);
1635
6
    OS << ')';
1636
6
    if (Node->getType()->isRecordType())
1637
0
      OS << "{}";
1638
6
    else
1639
6
      OS << 0;
1640
6
  }
1641
6
}
1642
1643
6
void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
1644
6
  OS << "__builtin_va_arg(";
1645
6
  PrintExpr(Node->getSubExpr());
1646
6
  OS << ", ";
1647
6
  Node->getType().print(OS, Policy);
1648
6
  OS << ")";
1649
6
}
1650
1651
33
void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1652
33
  PrintExpr(Node->getSyntacticForm());
1653
33
}
1654
1655
6
void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
1656
6
  const char *Name = nullptr;
1657
6
  switch (Node->getOp()) {
1658
0
#define BUILTIN(ID, TYPE, ATTRS)
1659
0
#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1660
6
  case AtomicExpr::AO ## ID: \
1661
6
    Name = #ID "("; \
1662
6
    break;
1663
0
#include "clang/Basic/Builtins.def"
1664
6
  }
1665
6
  OS << Name;
1666
6
1667
  // AtomicExpr stores its subexpressions in a permuted order.
1668
6
  PrintExpr(Node->getPtr());
1669
6
  if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1670
5
      Node->getOp() != AtomicExpr::AO__atomic_load_n &&
1671
1
      Node->getOp() != AtomicExpr::AO__opencl_atomic_load) {
1672
1
    OS << ", ";
1673
1
    PrintExpr(Node->getVal1());
1674
1
  }
1675
6
  if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1676
6
      Node->isCmpXChg()) {
1677
0
    OS << ", ";
1678
0
    PrintExpr(Node->getVal2());
1679
0
  }
1680
6
  if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1681
6
      Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1682
0
    OS << ", ";
1683
0
    PrintExpr(Node->getWeak());
1684
0
  }
1685
6
  if (Node->getOp() != AtomicExpr::AO__c11_atomic_init &&
1686
5
      Node->getOp() != AtomicExpr::AO__opencl_atomic_init) {
1687
5
    OS << ", ";
1688
5
    PrintExpr(Node->getOrder());
1689
5
  }
1690
6
  if (Node->isCmpXChg()) {
1691
0
    OS << ", ";
1692
0
    PrintExpr(Node->getOrderFail());
1693
0
  }
1694
6
  OS << ")";
1695
6
}
1696
1697
// C++
1698
133
void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1699
133
  OverloadedOperatorKind Kind = Node->getOperator();
1700
133
  if (Kind == OO_PlusPlus || 
Kind == OO_MinusMinus132
) {
1701
1
    if (Node->getNumArgs() == 1) {
1702
0
      OS << getOperatorSpelling(Kind) << ' ';
1703
0
      PrintExpr(Node->getArg(0));
1704
1
    } else {
1705
1
      PrintExpr(Node->getArg(0));
1706
1
      OS << ' ' << getOperatorSpelling(Kind);
1707
1
    }
1708
132
  } else if (Kind == OO_Arrow) {
1709
3
    PrintExpr(Node->getArg(0));
1710
129
  } else if (Kind == OO_Call) {
1711
82
    PrintExpr(Node->getArg(0));
1712
82
    OS << '(';
1713
96
    for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); 
++ArgIdx14
) {
1714
14
      if (ArgIdx > 1)
1715
3
        OS << ", ";
1716
14
      if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1717
13
        PrintExpr(Node->getArg(ArgIdx));
1718
14
    }
1719
82
    OS << ')';
1720
47
  } else if (Kind == OO_Subscript) {
1721
0
    PrintExpr(Node->getArg(0));
1722
0
    OS << '[';
1723
0
    PrintExpr(Node->getArg(1));
1724
0
    OS << ']';
1725
47
  } else if (Node->getNumArgs() == 1) {
1726
1
    OS << getOperatorSpelling(Kind) << ' ';
1727
1
    PrintExpr(Node->getArg(0));
1728
46
  } else if (Node->getNumArgs() == 2) {
1729
46
    PrintExpr(Node->getArg(0));
1730
46
    OS << ' ' << getOperatorSpelling(Kind) << ' ';
1731
46
    PrintExpr(Node->getArg(1));
1732
0
  } else {
1733
0
    llvm_unreachable("unknown overloaded operator");
1734
0
  }
1735
133
}
1736
1737
358
void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1738
  // If we have a conversion operator call only print the argument.
1739
358
  CXXMethodDecl *MD = Node->getMethodDecl();
1740
358
  if (MD && isa<CXXConversionDecl>(MD)) {
1741
278
    PrintExpr(Node->getImplicitObjectArgument());
1742
278
    return;
1743
278
  }
1744
80
  VisitCallExpr(cast<CallExpr>(Node));
1745
80
}
1746
1747
0
void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1748
0
  PrintExpr(Node->getCallee());
1749
0
  OS << "<<<";
1750
0
  PrintCallArgs(Node->getConfig());
1751
0
  OS << ">>>(";
1752
0
  PrintCallArgs(Node);
1753
0
  OS << ")";
1754
0
}
1755
1756
void StmtPrinter::VisitCXXRewrittenBinaryOperator(
1757
21
    CXXRewrittenBinaryOperator *Node) {
1758
21
  CXXRewrittenBinaryOperator::DecomposedForm Decomposed =
1759
21
      Node->getDecomposedForm();
1760
21
  PrintExpr(const_cast<Expr*>(Decomposed.LHS));
1761
21
  OS << ' ' << BinaryOperator::getOpcodeStr(Decomposed.Opcode) << ' ';
1762
21
  PrintExpr(const_cast<Expr*>(Decomposed.RHS));
1763
21
}
1764
1765
25
void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1766
25
  OS << Node->getCastName() << '<';
1767
25
  Node->getTypeAsWritten().print(OS, Policy);
1768
25
  OS << ">(";
1769
25
  PrintExpr(Node->getSubExpr());
1770
25
  OS << ")";
1771
25
}
1772
1773
13
void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1774
13
  VisitCXXNamedCastExpr(Node);
1775
13
}
1776
1777
4
void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1778
4
  VisitCXXNamedCastExpr(Node);
1779
4
}
1780
1781
4
void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1782
4
  VisitCXXNamedCastExpr(Node);
1783
4
}
1784
1785
4
void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1786
4
  VisitCXXNamedCastExpr(Node);
1787
4
}
1788
1789
0
void StmtPrinter::VisitBuiltinBitCastExpr(BuiltinBitCastExpr *Node) {
1790
0
  OS << "__builtin_bit_cast(";
1791
0
  Node->getTypeInfoAsWritten()->getType().print(OS, Policy);
1792
0
  OS << ", ";
1793
0
  PrintExpr(Node->getSubExpr());
1794
0
  OS << ")";
1795
0
}
1796
1797
0
void StmtPrinter::VisitCXXAddrspaceCastExpr(CXXAddrspaceCastExpr *Node) {
1798
0
  VisitCXXNamedCastExpr(Node);
1799
0
}
1800
1801
28
void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1802
28
  OS << "typeid(";
1803
28
  if (Node->isTypeOperand()) {
1804
14
    Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1805
14
  } else {
1806
14
    PrintExpr(Node->getExprOperand());
1807
14
  }
1808
28
  OS << ")";
1809
28
}
1810
1811
0
void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1812
0
  OS << "__uuidof(";
1813
0
  if (Node->isTypeOperand()) {
1814
0
    Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1815
0
  } else {
1816
0
    PrintExpr(Node->getExprOperand());
1817
0
  }
1818
0
  OS << ")";
1819
0
}
1820
1821
22
void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1822
22
  PrintExpr(Node->getBaseExpr());
1823
22
  if (Node->isArrow())
1824
22
    OS << "->";
1825
0
  else
1826
0
    OS << ".";
1827
22
  if (NestedNameSpecifier *Qualifier =
1828
0
      Node->getQualifierLoc().getNestedNameSpecifier())
1829
0
    Qualifier->print(OS, Policy);
1830
22
  OS << Node->getPropertyDecl()->getDeclName();
1831
22
}
1832
1833
40
void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) {
1834
40
  PrintExpr(Node->getBase());
1835
40
  OS << "[";
1836
40
  PrintExpr(Node->getIdx());
1837
40
  OS << "]";
1838
40
}
1839
1840
14
void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1841
14
  switch (Node->getLiteralOperatorKind()) {
1842
2
  case UserDefinedLiteral::LOK_Raw:
1843
2
    OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
1844
2
    break;
1845
3
  case UserDefinedLiteral::LOK_Template: {
1846
3
    const auto *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1847
3
    const TemplateArgumentList *Args =
1848
3
      cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1849
3
    assert(Args);
1850
3
1851
3
    if (Args->size() != 1) {
1852
1
      OS << "operator\"\"" << Node->getUDSuffix()->getName();
1853
1
      printTemplateArgumentList(OS, Args->asArray(), Policy);
1854
1
      OS << "()";
1855
1
      return;
1856
1
    }
1857
2
1858
2
    const TemplateArgument &Pack = Args->get(0);
1859
16
    for (const auto &P : Pack.pack_elements()) {
1860
16
      char C = (char)P.getAsIntegral().getZExtValue();
1861
16
      OS << C;
1862
16
    }
1863
2
    break;
1864
2
  }
1865
2
  case UserDefinedLiteral::LOK_Integer: {
1866
    // Print integer literal without suffix.
1867
2
    const auto *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1868
2
    OS << Int->getValue().toString(10, /*isSigned*/false);
1869
2
    break;
1870
2
  }
1871
2
  case UserDefinedLiteral::LOK_Floating: {
1872
    // Print floating literal without suffix.
1873
2
    auto *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1874
2
    PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1875
2
    break;
1876
2
  }
1877
5
  case UserDefinedLiteral::LOK_String:
1878
5
  case UserDefinedLiteral::LOK_Character:
1879
5
    PrintExpr(Node->getCookedLiteral());
1880
5
    break;
1881
13
  }
1882
13
  OS << Node->getUDSuffix()->getName();
1883
13
}
1884
1885
1.27k
void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1886
1.06k
  OS << (Node->getValue() ? "true" : 
"false"207
);
1887
1.27k
}
1888
1889
89
void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1890
89
  OS << "nullptr";
1891
89
}
1892
1893
4.57k
void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1894
4.57k
  OS << "this";
1895
4.57k
}
1896
1897
2
void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1898
2
  if (!Node->getSubExpr())
1899
2
    OS << "throw";
1900
0
  else {
1901
0
    OS << "throw ";
1902
0
    PrintExpr(Node->getSubExpr());
1903
0
  }
1904
2
}
1905
1906
8
void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1907
  // Nothing to print: we picked up the default argument.
1908
8
}
1909
1910
1
void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1911
  // Nothing to print: we picked up the default initializer.
1912
1
}
1913
1914
416
void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1915
416
  Node->getType().print(OS, Policy);
1916
  // If there are no parens, this is list-initialization, and the braces are
1917
  // part of the syntax of the inner construct.
1918
416
  if (Node->getLParenLoc().isValid())
1919
187
    OS << "(";
1920
416
  PrintExpr(Node->getSubExpr());
1921
416
  if (Node->getLParenLoc().isValid())
1922
187
    OS << ")";
1923
416
}
1924
1925
869
void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1926
869
  PrintExpr(Node->getSubExpr());
1927
869
}
1928
1929
701
void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1930
701
  Node->getType().print(OS, Policy);
1931
701
  if (Node->isStdInitListInitialization())
1932
0
    /* Nothing to do; braces are part of creating the std::initializer_list. */;
1933
701
  else if (Node->isListInitialization())
1934
53
    OS << "{";
1935
648
  else
1936
648
    OS << "(";
1937
701
  for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1938
701
                                         ArgEnd = Node->arg_end();
1939
714
       Arg != ArgEnd; 
++Arg13
) {
1940
14
    if ((*Arg)->isDefaultArgument())
1941
1
      break;
1942
13
    if (Arg != Node->arg_begin())
1943
7
      OS << ", ";
1944
13
    PrintExpr(*Arg);
1945
13
  }
1946
701
  if (Node->isStdInitListInitialization())
1947
0
    /* See above. */;
1948
701
  else if (Node->isListInitialization())
1949
53
    OS << "}";
1950
648
  else
1951
648
    OS << ")";
1952
701
}
1953
1954
139
void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1955
139
  OS << '[';
1956
139
  bool NeedComma = false;
1957
139
  switch (Node->getCaptureDefault()) {
1958
77
  case LCD_None:
1959
77
    break;
1960
0
1961
10
  case LCD_ByCopy:
1962
10
    OS << '=';
1963
10
    NeedComma = true;
1964
10
    break;
1965
0
1966
52
  case LCD_ByRef:
1967
52
    OS << '&';
1968
52
    NeedComma = true;
1969
52
    break;
1970
139
  }
1971
139
  for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1972
139
                                 CEnd = Node->explicit_capture_end();
1973
209
       C != CEnd;
1974
70
       ++C) {
1975
70
    if (C->capturesVLAType())
1976
1
      continue;
1977
69
1978
69
    if (NeedComma)
1979
15
      OS << ", ";
1980
69
    NeedComma = true;
1981
69
1982
69
    switch (C->getCaptureKind()) {
1983
5
    case LCK_This:
1984
5
      OS << "this";
1985
5
      break;
1986
0
1987
0
    case LCK_StarThis:
1988
0
      OS << "*this";
1989
0
      break;
1990
0
1991
25
    case LCK_ByRef:
1992
25
      if (Node->getCaptureDefault() != LCD_ByRef || 
Node->isInitCapture(C)0
)
1993
25
        OS << '&';
1994
25
      OS << C->getCapturedVar()->getName();
1995
25
      break;
1996
0
1997
39
    case LCK_ByCopy:
1998
39
      OS << C->getCapturedVar()->getName();
1999
39
      break;
2000
0
2001
0
    case LCK_VLAType:
2002
0
      llvm_unreachable("VLA type in explicit captures.");
2003
69
    }
2004
69
2005
69
    if (C->isPackExpansion())
2006
4
      OS << "...";
2007
69
2008
69
    if (Node->isInitCapture(C)) {
2009
17
      VarDecl *D = C->getCapturedVar();
2010
17
2011
17
      llvm::StringRef Pre;
2012
17
      llvm::StringRef Post;
2013
17
      if (D->getInitStyle() == VarDecl::CallInit &&
2014
8
          !isa<ParenListExpr>(D->getInit())) {
2015
5
        Pre = "(";
2016
5
        Post = ")";
2017
12
      } else if (D->getInitStyle() == VarDecl::CInit) {
2018
7
        Pre = " = ";
2019
7
      }
2020
17
2021
17
      OS << Pre;
2022
17
      PrintExpr(D->getInit());
2023
17
      OS << Post;
2024
17
    }
2025
69
  }
2026
139
  OS << ']';
2027
139
2028
139
  if (!Node->getExplicitTemplateParameters().empty()) {
2029
1
    Node->getTemplateParameterList()->print(
2030
1
        OS, Node->getLambdaClass()->getASTContext(),
2031
1
        /*OmitTemplateKW*/true);
2032
1
  }
2033
139
2034
139
  if (Node->hasExplicitParameters()) {
2035
92
    OS << '(';
2036
92
    CXXMethodDecl *Method = Node->getCallOperator();
2037
92
    NeedComma = false;
2038
33
    for (const auto *P : Method->parameters()) {
2039
33
      if (NeedComma) {
2040
14
        OS << ", ";
2041
19
      } else {
2042
19
        NeedComma = true;
2043
19
      }
2044
33
      std::string ParamStr = P->getNameAsString();
2045
33
      P->getOriginalType().print(OS, Policy, ParamStr);
2046
33
    }
2047
92
    if (Method->isVariadic()) {
2048
0
      if (NeedComma)
2049
0
        OS << ", ";
2050
0
      OS << "...";
2051
0
    }
2052
92
    OS << ')';
2053
92
2054
92
    if (Node->isMutable())
2055
11
      OS << " mutable";
2056
92
2057
92
    auto *Proto = Method->getType()->castAs<FunctionProtoType>();
2058
92
    Proto->printExceptionSpecification(OS, Policy);
2059
92
2060
    // FIXME: Attributes
2061
92
2062
    // Print the trailing return type if it was specified in the source.
2063
92
    if (Node->hasExplicitResultType()) {
2064
2
      OS << " -> ";
2065
2
      Proto->getReturnType().print(OS, Policy);
2066
2
    }
2067
92
  }
2068
139
2069
  // Print the body.
2070
139
  OS << ' ';
2071
139
  if (Policy.TerseOutput)
2072
1
    OS << "{}";
2073
138
  else
2074
138
    PrintRawCompoundStmt(Node->getCompoundStmtBody());
2075
139
}
2076
2077
258
void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
2078
258
  if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
2079
258
    TSInfo->getType().print(OS, Policy);
2080
0
  else
2081
0
    Node->getType().print(OS, Policy);
2082
258
  OS << "()";
2083
258
}
2084
2085
109
void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
2086
109
  if (E->isGlobalNew())
2087
0
    OS << "::";
2088
109
  OS << "new ";
2089
109
  unsigned NumPlace = E->getNumPlacementArgs();
2090
109
  if (NumPlace > 0 && 
!isa<CXXDefaultArgExpr>(E->getPlacementArg(0))16
) {
2091
15
    OS << "(";
2092
15
    PrintExpr(E->getPlacementArg(0));
2093
15
    for (unsigned i = 1; i < NumPlace; 
++i0
) {
2094
1
      if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
2095
1
        break;
2096
0
      OS << ", ";
2097
0
      PrintExpr(E->getPlacementArg(i));
2098
0
    }
2099
15
    OS << ") ";
2100
15
  }
2101
109
  if (E->isParenTypeId())
2102
0
    OS << "(";
2103
109
  std::string TypeS;
2104
109
  if (Optional<Expr *> Size = E->getArraySize()) {
2105
11
    llvm::raw_string_ostream s(TypeS);
2106
11
    s << '[';
2107
11
    if (*Size)
2108
11
      (*Size)->printPretty(s, Helper, Policy);
2109
11
    s << ']';
2110
11
  }
2111
109
  E->getAllocatedType().print(OS, Policy, TypeS);
2112
109
  if (E->isParenTypeId())
2113
0
    OS << ")";
2114
109
2115
109
  CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
2116
109
  if (InitStyle) {
2117
60
    if (InitStyle == CXXNewExpr::CallInit)
2118
60
      OS << "(";
2119
60
    PrintExpr(E->getInitializer());
2120
60
    if (InitStyle == CXXNewExpr::CallInit)
2121
60
      OS << ")";
2122
60
  }
2123
109
}
2124
2125
4
void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
2126
4
  if (E->isGlobalDelete())
2127
0
    OS << "::";
2128
4
  OS << "delete ";
2129
4
  if (E->isArrayForm())
2130
2
    OS << "[] ";
2131
4
  PrintExpr(E->getArgument());
2132
4
}
2133
2134
5
void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
2135
5
  PrintExpr(E->getBase());
2136
5
  if (E->isArrow())
2137
4
    OS << "->";
2138
1
  else
2139
1
    OS << '.';
2140
5
  if (E->getQualifier())
2141
0
    E->getQualifier()->print(OS, Policy);
2142
5
  OS << "~";
2143
5
2144
5
  if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
2145
0
    OS << II->getName();
2146
5
  else
2147
5
    E->getDestroyedType().print(OS, Policy);
2148
5
}
2149
2150
1.31k
void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
2151
1.31k
  if (E->isListInitialization() && 
!E->isStdInitListInitialization()46
)
2152
46
    OS << "{";
2153
1.31k
2154
2.19k
  for (unsigned i = 0, e = E->getNumArgs(); i != e; 
++i881
) {
2155
889
    if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
2156
      // Don't print any defaulted arguments
2157
8
      break;
2158
8
    }
2159
881
2160
881
    if (i) 
OS << ", "7
;
2161
881
    PrintExpr(E->getArg(i));
2162
881
  }
2163
1.31k
2164
1.31k
  if (E->isListInitialization() && 
!E->isStdInitListInitialization()46
)
2165
46
    OS << "}";
2166
1.31k
}
2167
2168
0
void StmtPrinter::VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E) {
2169
  // Parens are printed by the surrounding context.
2170
0
  OS << "<forwarded>";
2171
0
}
2172
2173
0
void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
2174
0
  PrintExpr(E->getSubExpr());
2175
0
}
2176
2177
377
void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
2178
  // Just forward to the subexpression.
2179
377
  PrintExpr(E->getSubExpr());
2180
377
}
2181
2182
void
2183
StmtPrinter::VisitCXXUnresolvedConstructExpr(
2184
239
                                           CXXUnresolvedConstructExpr *Node) {
2185
239
  Node->getTypeAsWritten().print(OS, Policy);
2186
239
  OS << "(";
2187
239
  for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
2188
239
                                             ArgEnd = Node->arg_end();
2189
249
       Arg != ArgEnd; 
++Arg10
) {
2190
10
    if (Arg != Node->arg_begin())
2191
0
      OS << ", ";
2192
10
    PrintExpr(*Arg);
2193
10
  }
2194
239
  OS << ")";
2195
239
}
2196
2197
void StmtPrinter::VisitCXXDependentScopeMemberExpr(
2198
1.95k
                                         CXXDependentScopeMemberExpr *Node) {
2199
1.95k
  if (!Node->isImplicitAccess()) {
2200
1.79k
    PrintExpr(Node->getBase());
2201
1.25k
    OS << (Node->isArrow() ? 
"->"546
: ".");
2202
1.79k
  }
2203
1.95k
  if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2204
156
    Qualifier->print(OS, Policy);
2205
1.95k
  if (Node->hasTemplateKeyword())
2206
3
    OS << "template ";
2207
1.95k
  OS << Node->getMemberNameInfo();
2208
1.95k
  if (Node->hasExplicitTemplateArgs())
2209
2
    printTemplateArgumentList(OS, Node->template_arguments(), Policy);
2210
1.95k
}
2211
2212
8
void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
2213
8
  if (!Node->isImplicitAccess()) {
2214
8
    PrintExpr(Node->getBase());
2215
8
    OS << (Node->isArrow() ? 
"->"0
: ".");
2216
8
  }
2217
8
  if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2218
0
    Qualifier->print(OS, Policy);
2219
8
  if (Node->hasTemplateKeyword())
2220
1
    OS << "template ";
2221
8
  OS << Node->getMemberNameInfo();
2222
8
  if (Node->hasExplicitTemplateArgs())
2223
8
    printTemplateArgumentList(OS, Node->template_arguments(), Policy);
2224
8
}
2225
2226
202
void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
2227
202
  OS << getTraitSpelling(E->getTrait()) << "(";
2228
411
  for (unsigned I = 0, N = E->getNumArgs(); I != N; 
++I209
) {
2229
209
    if (I > 0)
2230
7
      OS << ", ";
2231
209
    E->getArg(I)->getType().print(OS, Policy);
2232
209
  }
2233
202
  OS << ")";
2234
202
}
2235
2236
0
void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
2237
0
  OS << getTraitSpelling(E->getTrait()) << '(';
2238
0
  E->getQueriedType().print(OS, Policy);
2239
0
  OS << ')';
2240
0
}
2241
2242
0
void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
2243
0
  OS << getTraitSpelling(E->getTrait()) << '(';
2244
0
  PrintExpr(E->getQueriedExpression());
2245
0
  OS << ')';
2246
0
}
2247
2248
0
void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
2249
0
  OS << "noexcept(";
2250
0
  PrintExpr(E->getOperand());
2251
0
  OS << ")";
2252
0
}
2253
2254
379
void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
2255
379
  PrintExpr(E->getPattern());
2256
379
  OS << "...";
2257
379
}
2258
2259
20
void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
2260
20
  OS << "sizeof...(" << *E->getPack() << ")";
2261
20
}
2262
2263
void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2264
5
                                       SubstNonTypeTemplateParmPackExpr *Node) {
2265
5
  OS << *Node->getParameterPack();
2266
5
}
2267
2268
void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2269
1.98k
                                       SubstNonTypeTemplateParmExpr *Node) {
2270
1.98k
  Visit(Node->getReplacement());
2271
1.98k
}
2272
2273
0
void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2274
0
  OS << *E->getParameterPack();
2275
0
}
2276
2277
1.15k
void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
2278
1.15k
  PrintExpr(Node->getSubExpr());
2279
1.15k
}
2280
2281
0
void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
2282
0
  OS << "(";
2283
0
  if (E->getLHS()) {
2284
0
    PrintExpr(E->getLHS());
2285
0
    OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2286
0
  }
2287
0
  OS << "...";
2288
0
  if (E->getRHS()) {
2289
0
    OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2290
0
    PrintExpr(E->getRHS());
2291
0
  }
2292
0
  OS << ")";
2293
0
}
2294
2295
42
void StmtPrinter::VisitConceptSpecializationExpr(ConceptSpecializationExpr *E) {
2296
42
  NestedNameSpecifierLoc NNS = E->getNestedNameSpecifierLoc();
2297
42
  if (NNS)
2298
0
    NNS.getNestedNameSpecifier()->print(OS, Policy);
2299
42
  if (E->getTemplateKWLoc().isValid())
2300
0
    OS << "template ";
2301
42
  OS << E->getFoundDecl()->getName();
2302
42
  printTemplateArgumentList(OS, E->getTemplateArgsAsWritten()->arguments(),
2303
42
                            Policy);
2304
42
}
2305
2306
37
void StmtPrinter::VisitRequiresExpr(RequiresExpr *E) {
2307
37
  OS << "requires ";
2308
37
  auto LocalParameters = E->getLocalParameters();
2309
37
  if (!LocalParameters.empty()) {
2310
14
    OS << "(";
2311
15
    for (ParmVarDecl *LocalParam : LocalParameters) {
2312
15
      PrintRawDecl(LocalParam);
2313
15
      if (LocalParam != LocalParameters.back())
2314
1
        OS << ", ";
2315
15
    }
2316
14
2317
14
    OS << ") ";
2318
14
  }
2319
37
  OS << "{ ";
2320
37
  auto Requirements = E->getRequirements();
2321
51
  for (concepts::Requirement *Req : Requirements) {
2322
51
    if (auto *TypeReq = dyn_cast<concepts::TypeRequirement>(Req)) {
2323
13
      if (TypeReq->isSubstitutionFailure())
2324
4
        OS << "<<error-type>>";
2325
9
      else
2326
9
        TypeReq->getType()->getType().print(OS, Policy);
2327
38
    } else if (auto *ExprReq = dyn_cast<concepts::ExprRequirement>(Req)) {
2328
21
      if (ExprReq->isCompound())
2329
11
        OS << "{ ";
2330
21
      if (ExprReq->isExprSubstitutionFailure())
2331
2
        OS << "<<error-expression>>";
2332
19
      else
2333
19
        PrintExpr(ExprReq->getExpr());
2334
21
      if (ExprReq->isCompound()) {
2335
11
        OS << " }";
2336
11
        if (ExprReq->getNoexceptLoc().isValid())
2337
4
          OS << " noexcept";
2338
11
        const auto &RetReq = ExprReq->getReturnTypeRequirement();
2339
11
        if (!RetReq.isEmpty()) {
2340
9
          OS << " -> ";
2341
9
          if (RetReq.isSubstitutionFailure())
2342
1
            OS << "<<error-type>>";
2343
8
          else if (RetReq.isTypeConstraint())
2344
8
            RetReq.getTypeConstraint()->print(OS, Policy);
2345
9
        }
2346
11
      }
2347
17
    } else {
2348
17
      auto *NestedReq = cast<concepts::NestedRequirement>(Req);
2349
17
      OS << "requires ";
2350
17
      if (NestedReq->isSubstitutionFailure())
2351
1
        OS << "<<error-expression>>";
2352
16
      else
2353
16
        PrintExpr(NestedReq->getConstraintExpr());
2354
17
    }
2355
51
    OS << "; ";
2356
51
  }
2357
37
  OS << "}";
2358
37
}
2359
2360
// C++ Coroutines TS
2361
2362
0
void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) {
2363
0
  Visit(S->getBody());
2364
0
}
2365
2366
1
void StmtPrinter::VisitCoreturnStmt(CoreturnStmt *S) {
2367
1
  OS << "co_return";
2368
1
  if (S->getOperand()) {
2369
1
    OS << " ";
2370
1
    Visit(S->getOperand());
2371
1
  }
2372
1
  OS << ";";
2373
1
}
2374
2375
0
void StmtPrinter::VisitCoawaitExpr(CoawaitExpr *S) {
2376
0
  OS << "co_await ";
2377
0
  PrintExpr(S->getOperand());
2378
0
}
2379
2380
0
void StmtPrinter::VisitDependentCoawaitExpr(DependentCoawaitExpr *S) {
2381
0
  OS << "co_await ";
2382
0
  PrintExpr(S->getOperand());
2383
0
}
2384
2385
0
void StmtPrinter::VisitCoyieldExpr(CoyieldExpr *S) {
2386
0
  OS << "co_yield ";
2387
0
  PrintExpr(S->getOperand());
2388
0
}
2389
2390
// Obj-C
2391
2392
4
void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2393
4
  OS << "@";
2394
4
  VisitStringLiteral(Node->getString());
2395
4
}
2396
2397
8
void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
2398
8
  OS << "@";
2399
8
  Visit(E->getSubExpr());
2400
8
}
2401
2402
1
void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2403
1
  OS << "@[ ";
2404
1
  ObjCArrayLiteral::child_range Ch = E->children();
2405
3
  for (auto I = Ch.begin(), E = Ch.end(); I != E; 
++I2
) {
2406
2
    if (I != Ch.begin())
2407
1
      OS << ", ";
2408
2
    Visit(*I);
2409
2
  }
2410
1
  OS << " ]";
2411
1
}
2412
2413
3
void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2414
3
  OS << "@{ ";
2415
8
  for (unsigned I = 0, N = E->getNumElements(); I != N; 
++I5
) {
2416
5
    if (I > 0)
2417
2
      OS << ", ";
2418
5
2419
5
    ObjCDictionaryElement Element = E->getKeyValueElement(I);
2420
5
    Visit(Element.Key);
2421
5
    OS << " : ";
2422
5
    Visit(Element.Value);
2423
5
    if (Element.isPackExpansion())
2424
1
      OS << "...";
2425
5
  }
2426
3
  OS << " }";
2427
3
}
2428
2429
6
void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
2430
6
  OS << "@encode(";
2431
6
  Node->getEncodedType().print(OS, Policy);
2432
6
  OS << ')';
2433
6
}
2434
2435
0
void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
2436
0
  OS << "@selector(";
2437
0
  Node->getSelector().print(OS);
2438
0
  OS << ')';
2439
0
}
2440
2441
0
void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
2442
0
  OS << "@protocol(" << *Node->getProtocol() << ')';
2443
0
}
2444
2445
20
void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2446
20
  OS << "[";
2447
20
  switch (Mess->getReceiverKind()) {
2448
16
  case ObjCMessageExpr::Instance:
2449
16
    PrintExpr(Mess->getInstanceReceiver());
2450
16
    break;
2451
0
2452
2
  case ObjCMessageExpr::Class:
2453
2
    Mess->getClassReceiver().print(OS, Policy);
2454
2
    break;
2455
0
2456
2
  case ObjCMessageExpr::SuperInstance:
2457
2
  case ObjCMessageExpr::SuperClass:
2458
2
    OS << "Super";
2459
2
    break;
2460
20
  }
2461
20
2462
20
  OS << ' ';
2463
20
  Selector selector = Mess->getSelector();
2464
20
  if (selector.isUnarySelector()) {
2465
16
    OS << selector.getNameForSlot(0);
2466
4
  } else {
2467
8
    for (unsigned i = 0, e = Mess->getNumArgs(); i != e; 
++i4
) {
2468
4
      if (i < selector.getNumArgs()) {
2469
4
        if (i > 0) 
OS << ' '0
;
2470
4
        if (selector.getIdentifierInfoForSlot(i))
2471
4
          OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
2472
0
        else
2473
0
           OS << ":";
2474
4
      }
2475
0
      else OS << ", "; // Handle variadic methods.
2476
4
2477
4
      PrintExpr(Mess->getArg(i));
2478
4
    }
2479
4
  }
2480
20
  OS << "]";
2481
20
}
2482
2483
0
void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2484
0
  OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2485
0
}
2486
2487
void
2488
0
StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2489
0
  PrintExpr(E->getSubExpr());
2490
0
}
2491
2492
void
2493
0
StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
2494
0
  OS << '(' << E->getBridgeKindName();
2495
0
  E->getType().print(OS, Policy);
2496
0
  OS << ')';
2497
0
  PrintExpr(E->getSubExpr());
2498
0
}
2499
2500
10
void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
2501
10
  BlockDecl *BD = Node->getBlockDecl();
2502
10
  OS << "^";
2503
10
2504
10
  const FunctionType *AFT = Node->getFunctionType();
2505
10
2506
10
  if (isa<FunctionNoProtoType>(AFT)) {
2507
0
    OS << "()";
2508
10
  } else if (!BD->param_empty() || 
cast<FunctionProtoType>(AFT)->isVariadic()7
) {
2509
3
    OS << '(';
2510
3
    for (BlockDecl::param_iterator AI = BD->param_begin(),
2511
9
         E = BD->param_end(); AI != E; 
++AI6
) {
2512
6
      if (AI != BD->param_begin()) 
OS << ", "3
;
2513
6
      std::string ParamStr = (*AI)->getNameAsString();
2514
6
      (*AI)->getType().print(OS, Policy, ParamStr);
2515
6
    }
2516
3
2517
3
    const auto *FT = cast<FunctionProtoType>(AFT);
2518
3
    if (FT->isVariadic()) {
2519
0
      if (!BD->param_empty()) OS << ", ";
2520
0
      OS << "...";
2521
0
    }
2522
3
    OS << ')';
2523
3
  }
2524
10
  OS << "{ }";
2525
10
}
2526
2527
114
void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
2528
114
  PrintExpr(Node->getSourceExpr());
2529
114
}
2530
2531
0
void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
2532
  // TODO: Print something reasonable for a TypoExpr, if necessary.
2533
0
  llvm_unreachable("Cannot print TypoExpr nodes");
2534
0
}
2535
2536
18
void StmtPrinter::VisitRecoveryExpr(RecoveryExpr *Node) {
2537
18
  OS << "<recovery-expr>(";
2538
18
  const char *Sep = "";
2539
16
  for (Expr *E : Node->subExpressions()) {
2540
16
    OS << Sep;
2541
16
    PrintExpr(E);
2542
16
    Sep = ", ";
2543
16
  }
2544
18
  OS << ')';
2545
18
}
2546
2547
0
void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2548
0
  OS << "__builtin_astype(";
2549
0
  PrintExpr(Node->getSrcExpr());
2550
0
  OS << ", ";
2551
0
  Node->getType().print(OS, Policy);
2552
0
  OS << ")";
2553
0
}
2554
2555
//===----------------------------------------------------------------------===//
2556
// Stmt method implementations
2557
//===----------------------------------------------------------------------===//
2558
2559
0
void Stmt::dumpPretty(const ASTContext &Context) const {
2560
0
  printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
2561
0
}
2562
2563
void Stmt::printPretty(raw_ostream &Out, PrinterHelper *Helper,
2564
                       const PrintingPolicy &Policy, unsigned Indentation,
2565
98.5k
                       StringRef NL, const ASTContext *Context) const {
2566
98.5k
  StmtPrinter P(Out, Helper, Policy, Indentation, NL, Context);
2567
98.5k
  P.Visit(const_cast<Stmt *>(this));
2568
98.5k
}
2569
2570
void Stmt::printJson(raw_ostream &Out, PrinterHelper *Helper,
2571
293
                     const PrintingPolicy &Policy, bool AddQuotes) const {
2572
293
  std::string Buf;
2573
293
  llvm::raw_string_ostream TempOut(Buf);
2574
293
2575
293
  printPretty(TempOut, Helper, Policy);
2576
293
2577
293
  Out << JsonFormat(TempOut.str(), AddQuotes);
2578
293
}
2579
2580
//===----------------------------------------------------------------------===//
2581
// PrinterHelper
2582
//===----------------------------------------------------------------------===//
2583
2584
// Implement virtual destructor.
2585
47.7k
PrinterHelper::~PrinterHelper() = default;