Coverage Report

Created: 2020-02-25 14:32

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