Coverage Report

Created: 2019-07-24 05:18

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