Coverage Report

Created: 2021-08-24 07:12

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/JSONNodeDumper.cpp
Line
Count
Source (jump to first uncovered line)
1
#include "clang/AST/JSONNodeDumper.h"
2
#include "clang/Basic/SourceManager.h"
3
#include "clang/Basic/Specifiers.h"
4
#include "clang/Lex/Lexer.h"
5
#include "llvm/ADT/StringSwitch.h"
6
7
using namespace clang;
8
9
1.88k
void JSONNodeDumper::addPreviousDeclaration(const Decl *D) {
10
1.88k
  switch (D->getKind()) {
11
0
#define DECL(DERIVED, BASE)                                                    \
12
1.88k
  case Decl::DERIVED:                                                          \
13
1.88k
    return writePreviousDeclImpl(cast<DERIVED##Decl>(D));
14
0
#define ABSTRACT_DECL(DECL)
15
1.88k
#include 
"clang/AST/DeclNodes.inc"0
16
1.88k
#undef ABSTRACT_DECL
17
1.88k
#undef DECL
18
1.88k
  }
19
0
  llvm_unreachable("Decl that isn't part of DeclNodes.inc!");
20
0
}
21
22
34
void JSONNodeDumper::Visit(const Attr *A) {
23
34
  const char *AttrName = nullptr;
24
34
  switch (A->getKind()) {
25
0
#define ATTR(X)                                                                \
26
34
  case attr::X:                                                                \
27
34
    AttrName = #X"Attr";                                                       \
28
34
    break;
29
34
#include 
"clang/Basic/AttrList.inc"0
30
34
#undef ATTR
31
34
  }
32
34
  JOS.attribute("id", createPointerRepresentation(A));
33
34
  JOS.attribute("kind", AttrName);
34
34
  JOS.attributeObject("range", [A, this] { writeSourceRange(A->getRange()); });
35
34
  attributeOnlyIfTrue("inherited", A->isInherited());
36
34
  attributeOnlyIfTrue("implicit", A->isImplicit());
37
38
  // FIXME: it would be useful for us to output the spelling kind as well as
39
  // the actual spelling. This would allow us to distinguish between the
40
  // various attribute syntaxes, but we don't currently track that information
41
  // within the AST.
42
  //JOS.attribute("spelling", A->getSpelling());
43
44
34
  InnerAttrVisitor::Visit(A);
45
34
}
46
47
2.50k
void JSONNodeDumper::Visit(const Stmt *S) {
48
2.50k
  if (!S)
49
37
    return;
50
51
2.47k
  JOS.attribute("id", createPointerRepresentation(S));
52
2.47k
  JOS.attribute("kind", S->getStmtClassName());
53
2.47k
  JOS.attributeObject("range",
54
2.47k
                      [S, this] { writeSourceRange(S->getSourceRange()); });
55
56
2.47k
  if (const auto *E = dyn_cast<Expr>(S)) {
57
1.83k
    JOS.attribute("type", createQualType(E->getType()));
58
1.83k
    const char *Category = nullptr;
59
1.83k
    switch (E->getValueKind()) {
60
590
    case VK_LValue: Category = "lvalue"; break;
61
4
    case VK_XValue: Category = "xvalue"; break;
62
1.24k
    case VK_PRValue:
63
1.24k
      Category = "prvalue";
64
1.24k
      break;
65
1.83k
    }
66
1.83k
    JOS.attribute("valueCategory", Category);
67
1.83k
  }
68
2.47k
  InnerStmtVisitor::Visit(S);
69
2.47k
}
70
71
474
void JSONNodeDumper::Visit(const Type *T) {
72
474
  JOS.attribute("id", createPointerRepresentation(T));
73
74
474
  if (!T)
75
1
    return;
76
77
473
  JOS.attribute("kind", (llvm::Twine(T->getTypeClassName()) + "Type").str());
78
473
  JOS.attribute("type", createQualType(QualType(T, 0), /*Desugar*/ false));
79
473
  attributeOnlyIfTrue("containsErrors", T->containsErrors());
80
473
  attributeOnlyIfTrue("isDependent", T->isDependentType());
81
473
  attributeOnlyIfTrue("isInstantiationDependent",
82
473
                      T->isInstantiationDependentType());
83
473
  attributeOnlyIfTrue("isVariablyModified", T->isVariablyModifiedType());
84
473
  attributeOnlyIfTrue("containsUnexpandedPack",
85
473
                      T->containsUnexpandedParameterPack());
86
473
  attributeOnlyIfTrue("isImported", T->isFromAST());
87
473
  InnerTypeVisitor::Visit(T);
88
473
}
89
90
1
void JSONNodeDumper::Visit(QualType T) {
91
1
  JOS.attribute("id", createPointerRepresentation(T.getAsOpaquePtr()));
92
1
  JOS.attribute("kind", "QualType");
93
1
  JOS.attribute("type", createQualType(T));
94
1
  JOS.attribute("qualifiers", T.split().Quals.getAsString());
95
1
}
96
97
1.88k
void JSONNodeDumper::Visit(const Decl *D) {
98
1.88k
  JOS.attribute("id", createPointerRepresentation(D));
99
100
1.88k
  if (!D)
101
1
    return;
102
103
1.88k
  JOS.attribute("kind", (llvm::Twine(D->getDeclKindName()) + "Decl").str());
104
1.88k
  JOS.attributeObject("loc",
105
1.88k
                      [D, this] { writeSourceLocation(D->getLocation()); });
106
1.88k
  JOS.attributeObject("range",
107
1.88k
                      [D, this] { writeSourceRange(D->getSourceRange()); });
108
1.88k
  attributeOnlyIfTrue("isImplicit", D->isImplicit());
109
1.88k
  attributeOnlyIfTrue("isInvalid", D->isInvalidDecl());
110
111
1.88k
  if (D->isUsed())
112
264
    JOS.attribute("isUsed", true);
113
1.61k
  else if (D->isThisDeclarationReferenced())
114
119
    JOS.attribute("isReferenced", true);
115
116
1.88k
  if (const auto *ND = dyn_cast<NamedDecl>(D))
117
1.75k
    attributeOnlyIfTrue("isHidden", !ND->isUnconditionallyVisible());
118
119
1.88k
  if (D->getLexicalDeclContext() != D->getDeclContext()) {
120
    // Because of multiple inheritance, a DeclContext pointer does not produce
121
    // the same pointer representation as a Decl pointer that references the
122
    // same AST Node.
123
16
    const auto *ParentDeclContextDecl = dyn_cast<Decl>(D->getDeclContext());
124
16
    JOS.attribute("parentDeclContextId",
125
16
                  createPointerRepresentation(ParentDeclContextDecl));
126
16
  }
127
128
1.88k
  addPreviousDeclaration(D);
129
1.88k
  InnerDeclVisitor::Visit(D);
130
1.88k
}
131
132
void JSONNodeDumper::Visit(const comments::Comment *C,
133
64
                           const comments::FullComment *FC) {
134
64
  if (!C)
135
0
    return;
136
137
64
  JOS.attribute("id", createPointerRepresentation(C));
138
64
  JOS.attribute("kind", C->getCommentKindName());
139
64
  JOS.attributeObject("loc",
140
64
                      [C, this] { writeSourceLocation(C->getLocation()); });
141
64
  JOS.attributeObject("range",
142
64
                      [C, this] { writeSourceRange(C->getSourceRange()); });
143
144
64
  InnerCommentVisitor::visit(C, FC);
145
64
}
146
147
void JSONNodeDumper::Visit(const TemplateArgument &TA, SourceRange R,
148
33
                           const Decl *From, StringRef Label) {
149
33
  JOS.attribute("kind", "TemplateArgument");
150
33
  if (R.isValid())
151
5
    JOS.attributeObject("range", [R, this] { writeSourceRange(R); });
152
153
33
  if (From)
154
0
    JOS.attribute(Label.empty() ? "fromDecl" : Label, createBareDeclRef(From));
155
156
33
  InnerTemplateArgVisitor::Visit(TA);
157
33
}
158
159
21
void JSONNodeDumper::Visit(const CXXCtorInitializer *Init) {
160
21
  JOS.attribute("kind", "CXXCtorInitializer");
161
21
  if (Init->isAnyMemberInitializer())
162
17
    JOS.attribute("anyInit", createBareDeclRef(Init->getAnyMember()));
163
4
  else if (Init->isBaseInitializer())
164
4
    JOS.attribute("baseInit",
165
4
                  createQualType(QualType(Init->getBaseClass(), 0)));
166
0
  else if (Init->isDelegatingInitializer())
167
0
    JOS.attribute("delegatingInit",
168
0
                  createQualType(Init->getTypeSourceInfo()->getType()));
169
0
  else
170
0
    llvm_unreachable("Unknown initializer type");
171
21
}
172
173
2
void JSONNodeDumper::Visit(const OMPClause *C) {}
174
175
3
void JSONNodeDumper::Visit(const BlockDecl::Capture &C) {
176
3
  JOS.attribute("kind", "Capture");
177
3
  attributeOnlyIfTrue("byref", C.isByRef());
178
3
  attributeOnlyIfTrue("nested", C.isNested());
179
3
  if (C.getVariable())
180
3
    JOS.attribute("var", createBareDeclRef(C.getVariable()));
181
3
}
182
183
12
void JSONNodeDumper::Visit(const GenericSelectionExpr::ConstAssociation &A) {
184
12
  JOS.attribute("associationKind", A.getTypeSourceInfo() ? 
"case"7
:
"default"5
);
185
12
  attributeOnlyIfTrue("selected", A.isSelected());
186
12
}
187
188
4
void JSONNodeDumper::Visit(const concepts::Requirement *R) {
189
4
  if (!R)
190
0
    return;
191
192
4
  switch (R->getKind()) {
193
1
  case concepts::Requirement::RK_Type:
194
1
    JOS.attribute("kind", "TypeRequirement");
195
1
    break;
196
1
  case concepts::Requirement::RK_Simple:
197
1
    JOS.attribute("kind", "SimpleRequirement");
198
1
    break;
199
1
  case concepts::Requirement::RK_Compound:
200
1
    JOS.attribute("kind", "CompoundRequirement");
201
1
    break;
202
1
  case concepts::Requirement::RK_Nested:
203
1
    JOS.attribute("kind", "NestedRequirement");
204
1
    break;
205
4
  }
206
207
4
  if (auto *ER = dyn_cast<concepts::ExprRequirement>(R))
208
2
    attributeOnlyIfTrue("noexcept", ER->hasNoexceptRequirement());
209
210
4
  attributeOnlyIfTrue("isDependent", R->isDependent());
211
4
  if (!R->isDependent())
212
1
    JOS.attribute("satisfied", R->isSatisfied());
213
4
  attributeOnlyIfTrue("containsUnexpandedPack",
214
4
                      R->containsUnexpandedParameterPack());
215
4
}
216
217
11
void JSONNodeDumper::Visit(const APValue &Value, QualType Ty) {
218
11
  std::string Str;
219
11
  llvm::raw_string_ostream OS(Str);
220
11
  Value.printPretty(OS, Ctx, Ty);
221
11
  JOS.attribute("value", OS.str());
222
11
}
223
224
9.62k
void JSONNodeDumper::writeIncludeStack(PresumedLoc Loc, bool JustFirst) {
225
9.62k
  if (Loc.isInvalid())
226
9.27k
    return;
227
228
346
  JOS.attributeBegin("includedFrom");
229
346
  JOS.objectBegin();
230
231
346
  if (!JustFirst) {
232
    // Walk the stack recursively, then print out the presumed location.
233
0
    writeIncludeStack(SM.getPresumedLoc(Loc.getIncludeLoc()));
234
0
  }
235
236
346
  JOS.attribute("file", Loc.getFilename());
237
346
  JOS.objectEnd();
238
346
  JOS.attributeEnd();
239
346
}
240
241
void JSONNodeDumper::writeBareSourceLocation(SourceLocation Loc,
242
10.8k
                                             bool IsSpelling) {
243
10.8k
  PresumedLoc Presumed = SM.getPresumedLoc(Loc);
244
10.8k
  unsigned ActualLine = IsSpelling ? 
SM.getSpellingLineNumber(Loc)10.8k
245
10.8k
                                   : 
SM.getExpansionLineNumber(Loc)7
;
246
10.8k
  StringRef ActualFile = SM.getBufferName(Loc);
247
248
10.8k
  if (Presumed.isValid()) {
249
9.62k
    JOS.attribute("offset", SM.getDecomposedLoc(Loc).second);
250
9.62k
    if (LastLocFilename != ActualFile) {
251
164
      JOS.attribute("file", ActualFile);
252
164
      JOS.attribute("line", ActualLine);
253
9.45k
    } else if (LastLocLine != ActualLine)
254
2.10k
      JOS.attribute("line", ActualLine);
255
256
9.62k
    StringRef PresumedFile = Presumed.getFilename();
257
9.62k
    if (PresumedFile != ActualFile && 
LastLocPresumedFilename != PresumedFile15
)
258
3
      JOS.attribute("presumedFile", PresumedFile);
259
260
9.62k
    unsigned PresumedLine = Presumed.getLine();
261
9.62k
    if (ActualLine != PresumedLine && 
LastLocPresumedLine != PresumedLine17
)
262
6
      JOS.attribute("presumedLine", PresumedLine);
263
264
9.62k
    JOS.attribute("col", Presumed.getColumn());
265
9.62k
    JOS.attribute("tokLen",
266
9.62k
                  Lexer::MeasureTokenLength(Loc, SM, Ctx.getLangOpts()));
267
9.62k
    LastLocFilename = ActualFile;
268
9.62k
    LastLocPresumedFilename = PresumedFile;
269
9.62k
    LastLocPresumedLine = PresumedLine;
270
9.62k
    LastLocLine = ActualLine;
271
272
    // Orthogonal to the file, line, and column de-duplication is whether the
273
    // given location was a result of an include. If so, print where the
274
    // include location came from.
275
9.62k
    writeIncludeStack(SM.getPresumedLoc(Presumed.getIncludeLoc()),
276
9.62k
                      /*JustFirst*/ true);
277
9.62k
  }
278
10.8k
}
279
280
10.8k
void JSONNodeDumper::writeSourceLocation(SourceLocation Loc) {
281
10.8k
  SourceLocation Spelling = SM.getSpellingLoc(Loc);
282
10.8k
  SourceLocation Expansion = SM.getExpansionLoc(Loc);
283
284
10.8k
  if (Expansion != Spelling) {
285
    // If the expansion and the spelling are different, output subobjects
286
    // describing both locations.
287
7
    JOS.attributeObject("spellingLoc", [Spelling, this] {
288
7
      writeBareSourceLocation(Spelling, /*IsSpelling*/ true);
289
7
    });
290
7
    JOS.attributeObject("expansionLoc", [Expansion, Loc, this] {
291
7
      writeBareSourceLocation(Expansion, /*IsSpelling*/ false);
292
      // If there is a macro expansion, add extra information if the interesting
293
      // bit is the macro arg expansion.
294
7
      if (SM.isMacroArgExpansion(Loc))
295
3
        JOS.attribute("isMacroArgExpansion", true);
296
7
    });
297
7
  } else
298
10.8k
    writeBareSourceLocation(Spelling, /*IsSpelling*/ true);
299
10.8k
}
300
301
4.45k
void JSONNodeDumper::writeSourceRange(SourceRange R) {
302
4.45k
  JOS.attributeObject("begin",
303
4.45k
                      [R, this] { writeSourceLocation(R.getBegin()); });
304
4.45k
  JOS.attributeObject("end", [R, this] { writeSourceLocation(R.getEnd()); });
305
4.45k
}
306
307
5.81k
std::string JSONNodeDumper::createPointerRepresentation(const void *Ptr) {
308
  // Because JSON stores integer values as signed 64-bit integers, trying to
309
  // represent them as such makes for very ugly pointer values in the resulting
310
  // output. Instead, we convert the value to hex and treat it as a string.
311
5.81k
  return "0x" + llvm::utohexstr(reinterpret_cast<uint64_t>(Ptr), true);
312
5.81k
}
313
314
4.25k
llvm::json::Object JSONNodeDumper::createQualType(QualType QT, bool Desugar) {
315
4.25k
  SplitQualType SQT = QT.split();
316
4.25k
  llvm::json::Object Ret{{"qualType", QualType::getAsString(SQT, PrintPolicy)}};
317
318
4.25k
  if (Desugar && 
!QT.isNull()3.78k
) {
319
3.77k
    SplitQualType DSQT = QT.getSplitDesugaredType();
320
3.77k
    if (DSQT != SQT)
321
312
      Ret["desugaredQualType"] = QualType::getAsString(DSQT, PrintPolicy);
322
3.77k
    if (const auto *TT = QT->getAs<TypedefType>())
323
68
      Ret["typeAliasDeclId"] = createPointerRepresentation(TT->getDecl());
324
3.77k
  }
325
4.25k
  return Ret;
326
4.25k
}
327
328
11
void JSONNodeDumper::writeBareDeclRef(const Decl *D) {
329
11
  JOS.attribute("id", createPointerRepresentation(D));
330
11
  if (!D)
331
0
    return;
332
333
11
  JOS.attribute("kind", (llvm::Twine(D->getDeclKindName()) + "Decl").str());
334
11
  if (const auto *ND = dyn_cast<NamedDecl>(D))
335
11
    JOS.attribute("name", ND->getDeclName().getAsString());
336
11
  if (const auto *VD = dyn_cast<ValueDecl>(D))
337
1
    JOS.attribute("type", createQualType(VD->getType()));
338
11
}
339
340
678
llvm::json::Object JSONNodeDumper::createBareDeclRef(const Decl *D) {
341
678
  llvm::json::Object Ret{{"id", createPointerRepresentation(D)}};
342
678
  if (!D)
343
5
    return Ret;
344
345
673
  Ret["kind"] = (llvm::Twine(D->getDeclKindName()) + "Decl").str();
346
673
  if (const auto *ND = dyn_cast<NamedDecl>(D))
347
670
    Ret["name"] = ND->getDeclName().getAsString();
348
673
  if (const auto *VD = dyn_cast<ValueDecl>(D))
349
511
    Ret["type"] = createQualType(VD->getType());
350
673
  return Ret;
351
678
}
352
353
492
llvm::json::Array JSONNodeDumper::createCastPath(const CastExpr *C) {
354
492
  llvm::json::Array Ret;
355
492
  if (C->path_empty())
356
491
    return Ret;
357
358
2
  
for (auto I = C->path_begin(), E = C->path_end(); 1
I != E;
++I1
) {
359
1
    const CXXBaseSpecifier *Base = *I;
360
1
    const auto *RD =
361
1
        cast<CXXRecordDecl>(Base->getType()->castAs<RecordType>()->getDecl());
362
363
1
    llvm::json::Object Val{{"name", RD->getName()}};
364
1
    if (Base->isVirtual())
365
0
      Val["isVirtual"] = true;
366
1
    Ret.push_back(std::move(Val));
367
1
  }
368
1
  return Ret;
369
492
}
370
371
7.60k
#define FIELD2(Name, Flag)  if (RD->Flag()) 
Ret[Name] = true3.32k
372
1.90k
#define FIELD1(Flag)        FIELD2(#Flag, Flag)
373
374
static llvm::json::Object
375
119
createDefaultConstructorDefinitionData(const CXXRecordDecl *RD) {
376
119
  llvm::json::Object Ret;
377
378
119
  FIELD2("exists", hasDefaultConstructor);
379
119
  FIELD2("trivial", hasTrivialDefaultConstructor);
380
119
  FIELD2("nonTrivial", hasNonTrivialDefaultConstructor);
381
119
  FIELD2("userProvided", hasUserProvidedDefaultConstructor);
382
119
  FIELD2("isConstexpr", hasConstexprDefaultConstructor);
383
119
  FIELD2("needsImplicit", needsImplicitDefaultConstructor);
384
119
  FIELD2("defaultedIsConstexpr", defaultedDefaultConstructorIsConstexpr);
385
386
119
  return Ret;
387
119
}
388
389
static llvm::json::Object
390
119
createCopyConstructorDefinitionData(const CXXRecordDecl *RD) {
391
119
  llvm::json::Object Ret;
392
393
119
  FIELD2("simple", hasSimpleCopyConstructor);
394
119
  FIELD2("trivial", hasTrivialCopyConstructor);
395
119
  FIELD2("nonTrivial", hasNonTrivialCopyConstructor);
396
119
  FIELD2("userDeclared", hasUserDeclaredCopyConstructor);
397
119
  FIELD2("hasConstParam", hasCopyConstructorWithConstParam);
398
119
  FIELD2("implicitHasConstParam", implicitCopyConstructorHasConstParam);
399
119
  FIELD2("needsImplicit", needsImplicitCopyConstructor);
400
119
  FIELD2("needsOverloadResolution", needsOverloadResolutionForCopyConstructor);
401
119
  if (!RD->needsOverloadResolutionForCopyConstructor())
402
115
    FIELD2("defaultedIsDeleted", defaultedCopyConstructorIsDeleted);
403
404
119
  return Ret;
405
119
}
406
407
static llvm::json::Object
408
119
createMoveConstructorDefinitionData(const CXXRecordDecl *RD) {
409
119
  llvm::json::Object Ret;
410
411
119
  FIELD2("exists", hasMoveConstructor);
412
119
  FIELD2("simple", hasSimpleMoveConstructor);
413
119
  FIELD2("trivial", hasTrivialMoveConstructor);
414
119
  FIELD2("nonTrivial", hasNonTrivialMoveConstructor);
415
119
  FIELD2("userDeclared", hasUserDeclaredMoveConstructor);
416
119
  FIELD2("needsImplicit", needsImplicitMoveConstructor);
417
119
  FIELD2("needsOverloadResolution", needsOverloadResolutionForMoveConstructor);
418
119
  if (!RD->needsOverloadResolutionForMoveConstructor())
419
115
    FIELD2("defaultedIsDeleted", defaultedMoveConstructorIsDeleted);
420
421
119
  return Ret;
422
119
}
423
424
static llvm::json::Object
425
119
createCopyAssignmentDefinitionData(const CXXRecordDecl *RD) {
426
119
  llvm::json::Object Ret;
427
428
119
  FIELD2("simple", hasSimpleCopyAssignment);
429
119
  FIELD2("trivial", hasTrivialCopyAssignment);
430
119
  FIELD2("nonTrivial", hasNonTrivialCopyAssignment);
431
119
  FIELD2("hasConstParam", hasCopyAssignmentWithConstParam);
432
119
  FIELD2("implicitHasConstParam", implicitCopyAssignmentHasConstParam);
433
119
  FIELD2("userDeclared", hasUserDeclaredCopyAssignment);
434
119
  FIELD2("needsImplicit", needsImplicitCopyAssignment);
435
119
  FIELD2("needsOverloadResolution", needsOverloadResolutionForCopyAssignment);
436
437
119
  return Ret;
438
119
}
439
440
static llvm::json::Object
441
119
createMoveAssignmentDefinitionData(const CXXRecordDecl *RD) {
442
119
  llvm::json::Object Ret;
443
444
119
  FIELD2("exists", hasMoveAssignment);
445
119
  FIELD2("simple", hasSimpleMoveAssignment);
446
119
  FIELD2("trivial", hasTrivialMoveAssignment);
447
119
  FIELD2("nonTrivial", hasNonTrivialMoveAssignment);
448
119
  FIELD2("userDeclared", hasUserDeclaredMoveAssignment);
449
119
  FIELD2("needsImplicit", needsImplicitMoveAssignment);
450
119
  FIELD2("needsOverloadResolution", needsOverloadResolutionForMoveAssignment);
451
452
119
  return Ret;
453
119
}
454
455
static llvm::json::Object
456
119
createDestructorDefinitionData(const CXXRecordDecl *RD) {
457
119
  llvm::json::Object Ret;
458
459
119
  FIELD2("simple", hasSimpleDestructor);
460
119
  FIELD2("irrelevant", hasIrrelevantDestructor);
461
119
  FIELD2("trivial", hasTrivialDestructor);
462
119
  FIELD2("nonTrivial", hasNonTrivialDestructor);
463
119
  FIELD2("userDeclared", hasUserDeclaredDestructor);
464
119
  FIELD2("needsImplicit", needsImplicitDestructor);
465
119
  FIELD2("needsOverloadResolution", needsOverloadResolutionForDestructor);
466
119
  if (!RD->needsOverloadResolutionForDestructor())
467
115
    FIELD2("defaultedIsDeleted", defaultedDestructorIsDeleted);
468
469
119
  return Ret;
470
119
}
471
472
llvm::json::Object
473
119
JSONNodeDumper::createCXXRecordDefinitionData(const CXXRecordDecl *RD) {
474
119
  llvm::json::Object Ret;
475
476
  // This data is common to all C++ classes.
477
119
  FIELD1(isGenericLambda);
478
119
  FIELD1(isLambda);
479
119
  FIELD1(isEmpty);
480
119
  FIELD1(isAggregate);
481
119
  FIELD1(isStandardLayout);
482
119
  FIELD1(isTriviallyCopyable);
483
119
  FIELD1(isPOD);
484
119
  FIELD1(isTrivial);
485
119
  FIELD1(isPolymorphic);
486
119
  FIELD1(isAbstract);
487
119
  FIELD1(isLiteral);
488
119
  FIELD1(canPassInRegisters);
489
119
  FIELD1(hasUserDeclaredConstructor);
490
119
  FIELD1(hasConstexprNonCopyMoveConstructor);
491
119
  FIELD1(hasMutableFields);
492
119
  FIELD1(hasVariantMembers);
493
119
  FIELD2("canConstDefaultInit", allowConstDefaultInit);
494
495
119
  Ret["defaultCtor"] = createDefaultConstructorDefinitionData(RD);
496
119
  Ret["copyCtor"] = createCopyConstructorDefinitionData(RD);
497
119
  Ret["moveCtor"] = createMoveConstructorDefinitionData(RD);
498
119
  Ret["copyAssign"] = createCopyAssignmentDefinitionData(RD);
499
119
  Ret["moveAssign"] = createMoveAssignmentDefinitionData(RD);
500
119
  Ret["dtor"] = createDestructorDefinitionData(RD);
501
502
119
  return Ret;
503
119
}
504
505
#undef FIELD1
506
#undef FIELD2
507
508
65
std::string JSONNodeDumper::createAccessSpecifier(AccessSpecifier AS) {
509
65
  const auto AccessSpelling = getAccessSpelling(AS);
510
65
  if (AccessSpelling.empty())
511
9
    return "none";
512
56
  return AccessSpelling.str();
513
65
}
514
515
llvm::json::Object
516
14
JSONNodeDumper::createCXXBaseSpecifier(const CXXBaseSpecifier &BS) {
517
14
  llvm::json::Object Ret;
518
519
14
  Ret["type"] = createQualType(BS.getType());
520
14
  Ret["access"] = createAccessSpecifier(BS.getAccessSpecifier());
521
14
  Ret["writtenAccess"] =
522
14
      createAccessSpecifier(BS.getAccessSpecifierAsWritten());
523
14
  if (BS.isVirtual())
524
5
    Ret["isVirtual"] = true;
525
14
  if (BS.isPackExpansion())
526
1
    Ret["isPackExpansion"] = true;
527
528
14
  return Ret;
529
14
}
530
531
0
void JSONNodeDumper::VisitTypedefType(const TypedefType *TT) {
532
0
  JOS.attribute("decl", createBareDeclRef(TT->getDecl()));
533
0
}
534
535
4
void JSONNodeDumper::VisitFunctionType(const FunctionType *T) {
536
4
  FunctionType::ExtInfo E = T->getExtInfo();
537
4
  attributeOnlyIfTrue("noreturn", E.getNoReturn());
538
4
  attributeOnlyIfTrue("producesResult", E.getProducesResult());
539
4
  if (E.getHasRegParm())
540
0
    JOS.attribute("regParm", E.getRegParm());
541
4
  JOS.attribute("cc", FunctionType::getNameForCallConv(E.getCC()));
542
4
}
543
544
4
void JSONNodeDumper::VisitFunctionProtoType(const FunctionProtoType *T) {
545
4
  FunctionProtoType::ExtProtoInfo E = T->getExtProtoInfo();
546
4
  attributeOnlyIfTrue("trailingReturn", E.HasTrailingReturn);
547
4
  attributeOnlyIfTrue("const", T->isConst());
548
4
  attributeOnlyIfTrue("volatile", T->isVolatile());
549
4
  attributeOnlyIfTrue("restrict", T->isRestrict());
550
4
  attributeOnlyIfTrue("variadic", E.Variadic);
551
4
  switch (E.RefQualifier) {
552
0
  case RQ_LValue: JOS.attribute("refQualifier", "&"); break;
553
0
  case RQ_RValue: JOS.attribute("refQualifier", "&&"); break;
554
4
  case RQ_None: break;
555
4
  }
556
4
  switch (E.ExceptionSpec.Type) {
557
0
  case EST_DynamicNone:
558
0
  case EST_Dynamic: {
559
0
    JOS.attribute("exceptionSpec", "throw");
560
0
    llvm::json::Array Types;
561
0
    for (QualType QT : E.ExceptionSpec.Exceptions)
562
0
      Types.push_back(createQualType(QT));
563
0
    JOS.attribute("exceptionTypes", std::move(Types));
564
0
  } break;
565
0
  case EST_MSAny:
566
0
    JOS.attribute("exceptionSpec", "throw");
567
0
    JOS.attribute("throwsAny", true);
568
0
    break;
569
0
  case EST_BasicNoexcept:
570
0
    JOS.attribute("exceptionSpec", "noexcept");
571
0
    break;
572
0
  case EST_NoexceptTrue:
573
0
  case EST_NoexceptFalse:
574
0
    JOS.attribute("exceptionSpec", "noexcept");
575
0
    JOS.attribute("conditionEvaluatesTo",
576
0
                E.ExceptionSpec.Type == EST_NoexceptTrue);
577
    //JOS.attributeWithCall("exceptionSpecExpr",
578
    //                    [this, E]() { Visit(E.ExceptionSpec.NoexceptExpr); });
579
0
    break;
580
0
  case EST_NoThrow:
581
0
    JOS.attribute("exceptionSpec", "nothrow");
582
0
    break;
583
  // FIXME: I cannot find a way to trigger these cases while dumping the AST. I
584
  // suspect you can only run into them when executing an AST dump from within
585
  // the debugger, which is not a use case we worry about for the JSON dumping
586
  // feature.
587
0
  case EST_DependentNoexcept:
588
0
  case EST_Unevaluated:
589
0
  case EST_Uninstantiated:
590
0
  case EST_Unparsed:
591
4
  case EST_None: break;
592
4
  }
593
4
  VisitFunctionType(T);
594
4
}
595
596
0
void JSONNodeDumper::VisitRValueReferenceType(const ReferenceType *RT) {
597
0
  attributeOnlyIfTrue("spelledAsLValue", RT->isSpelledAsLValue());
598
0
}
599
600
58
void JSONNodeDumper::VisitArrayType(const ArrayType *AT) {
601
58
  switch (AT->getSizeModifier()) {
602
0
  case ArrayType::Star:
603
0
    JOS.attribute("sizeModifier", "*");
604
0
    break;
605
0
  case ArrayType::Static:
606
0
    JOS.attribute("sizeModifier", "static");
607
0
    break;
608
58
  case ArrayType::Normal:
609
58
    break;
610
58
  }
611
612
58
  std::string Str = AT->getIndexTypeQualifiers().getAsString();
613
58
  if (!Str.empty())
614
0
    JOS.attribute("indexTypeQualifiers", Str);
615
58
}
616
617
57
void JSONNodeDumper::VisitConstantArrayType(const ConstantArrayType *CAT) {
618
  // FIXME: this should use ZExt instead of SExt, but JSON doesn't allow a
619
  // narrowing conversion to int64_t so it cannot be expressed.
620
57
  JOS.attribute("size", CAT->getSize().getSExtValue());
621
57
  VisitArrayType(CAT);
622
57
}
623
624
void JSONNodeDumper::VisitDependentSizedExtVectorType(
625
0
    const DependentSizedExtVectorType *VT) {
626
0
  JOS.attributeObject(
627
0
      "attrLoc", [VT, this] { writeSourceLocation(VT->getAttributeLoc()); });
628
0
}
629
630
0
void JSONNodeDumper::VisitVectorType(const VectorType *VT) {
631
0
  JOS.attribute("numElements", VT->getNumElements());
632
0
  switch (VT->getVectorKind()) {
633
0
  case VectorType::GenericVector:
634
0
    break;
635
0
  case VectorType::AltiVecVector:
636
0
    JOS.attribute("vectorKind", "altivec");
637
0
    break;
638
0
  case VectorType::AltiVecPixel:
639
0
    JOS.attribute("vectorKind", "altivec pixel");
640
0
    break;
641
0
  case VectorType::AltiVecBool:
642
0
    JOS.attribute("vectorKind", "altivec bool");
643
0
    break;
644
0
  case VectorType::NeonVector:
645
0
    JOS.attribute("vectorKind", "neon");
646
0
    break;
647
0
  case VectorType::NeonPolyVector:
648
0
    JOS.attribute("vectorKind", "neon poly");
649
0
    break;
650
0
  case VectorType::SveFixedLengthDataVector:
651
0
    JOS.attribute("vectorKind", "fixed-length sve data vector");
652
0
    break;
653
0
  case VectorType::SveFixedLengthPredicateVector:
654
0
    JOS.attribute("vectorKind", "fixed-length sve predicate vector");
655
0
    break;
656
0
  }
657
0
}
658
659
0
void JSONNodeDumper::VisitUnresolvedUsingType(const UnresolvedUsingType *UUT) {
660
0
  JOS.attribute("decl", createBareDeclRef(UUT->getDecl()));
661
0
}
662
663
0
void JSONNodeDumper::VisitUnaryTransformType(const UnaryTransformType *UTT) {
664
0
  switch (UTT->getUTTKind()) {
665
0
  case UnaryTransformType::EnumUnderlyingType:
666
0
    JOS.attribute("transformKind", "underlying_type");
667
0
    break;
668
0
  }
669
0
}
670
671
118
void JSONNodeDumper::VisitTagType(const TagType *TT) {
672
118
  JOS.attribute("decl", createBareDeclRef(TT->getDecl()));
673
118
}
674
675
void JSONNodeDumper::VisitTemplateTypeParmType(
676
8
    const TemplateTypeParmType *TTPT) {
677
8
  JOS.attribute("depth", TTPT->getDepth());
678
8
  JOS.attribute("index", TTPT->getIndex());
679
8
  attributeOnlyIfTrue("isPack", TTPT->isParameterPack());
680
8
  JOS.attribute("decl", createBareDeclRef(TTPT->getDecl()));
681
8
}
682
683
0
void JSONNodeDumper::VisitAutoType(const AutoType *AT) {
684
0
  JOS.attribute("undeduced", !AT->isDeduced());
685
0
  switch (AT->getKeyword()) {
686
0
  case AutoTypeKeyword::Auto:
687
0
    JOS.attribute("typeKeyword", "auto");
688
0
    break;
689
0
  case AutoTypeKeyword::DecltypeAuto:
690
0
    JOS.attribute("typeKeyword", "decltype(auto)");
691
0
    break;
692
0
  case AutoTypeKeyword::GNUAutoType:
693
0
    JOS.attribute("typeKeyword", "__auto_type");
694
0
    break;
695
0
  }
696
0
}
697
698
void JSONNodeDumper::VisitTemplateSpecializationType(
699
0
    const TemplateSpecializationType *TST) {
700
0
  attributeOnlyIfTrue("isAlias", TST->isTypeAlias());
701
702
0
  std::string Str;
703
0
  llvm::raw_string_ostream OS(Str);
704
0
  TST->getTemplateName().print(OS, PrintPolicy);
705
0
  JOS.attribute("templateName", OS.str());
706
0
}
707
708
void JSONNodeDumper::VisitInjectedClassNameType(
709
0
    const InjectedClassNameType *ICNT) {
710
0
  JOS.attribute("decl", createBareDeclRef(ICNT->getDecl()));
711
0
}
712
713
0
void JSONNodeDumper::VisitObjCInterfaceType(const ObjCInterfaceType *OIT) {
714
0
  JOS.attribute("decl", createBareDeclRef(OIT->getDecl()));
715
0
}
716
717
0
void JSONNodeDumper::VisitPackExpansionType(const PackExpansionType *PET) {
718
0
  if (llvm::Optional<unsigned> N = PET->getNumExpansions())
719
0
    JOS.attribute("numExpansions", *N);
720
0
}
721
722
2
void JSONNodeDumper::VisitElaboratedType(const ElaboratedType *ET) {
723
2
  if (const NestedNameSpecifier *NNS = ET->getQualifier()) {
724
1
    std::string Str;
725
1
    llvm::raw_string_ostream OS(Str);
726
1
    NNS->print(OS, PrintPolicy, /*ResolveTemplateArgs*/ true);
727
1
    JOS.attribute("qualifier", OS.str());
728
1
  }
729
2
  if (const TagDecl *TD = ET->getOwnedTagDecl())
730
0
    JOS.attribute("ownedTagDecl", createBareDeclRef(TD));
731
2
}
732
733
1
void JSONNodeDumper::VisitMacroQualifiedType(const MacroQualifiedType *MQT) {
734
1
  JOS.attribute("macroName", MQT->getMacroIdentifier()->getName());
735
1
}
736
737
2
void JSONNodeDumper::VisitMemberPointerType(const MemberPointerType *MPT) {
738
2
  attributeOnlyIfTrue("isData", MPT->isMemberDataPointer());
739
2
  attributeOnlyIfTrue("isFunction", MPT->isMemberFunctionPointer());
740
2
}
741
742
1.74k
void JSONNodeDumper::VisitNamedDecl(const NamedDecl *ND) {
743
1.74k
  if (ND && ND->getDeclName()) {
744
1.60k
    JOS.attribute("name", ND->getNameAsString());
745
    // FIXME: There are likely other contexts in which it makes no sense to ask
746
    // for a mangled name.
747
1.60k
    if (!isa<RequiresExprBodyDecl>(ND->getDeclContext())) {
748
1.60k
      std::string MangledName = ASTNameGen.getName(ND);
749
1.60k
      if (!MangledName.empty())
750
764
        JOS.attribute("mangledName", MangledName);
751
1.60k
    }
752
1.60k
  }
753
1.74k
}
754
755
294
void JSONNodeDumper::VisitTypedefDecl(const TypedefDecl *TD) {
756
294
  VisitNamedDecl(TD);
757
294
  JOS.attribute("type", createQualType(TD->getUnderlyingType()));
758
294
}
759
760
5
void JSONNodeDumper::VisitTypeAliasDecl(const TypeAliasDecl *TAD) {
761
5
  VisitNamedDecl(TAD);
762
5
  JOS.attribute("type", createQualType(TAD->getUnderlyingType()));
763
5
}
764
765
16
void JSONNodeDumper::VisitNamespaceDecl(const NamespaceDecl *ND) {
766
16
  VisitNamedDecl(ND);
767
16
  attributeOnlyIfTrue("isInline", ND->isInline());
768
16
  if (!ND->isOriginalNamespace())
769
0
    JOS.attribute("originalNamespace",
770
0
                  createBareDeclRef(ND->getOriginalNamespace()));
771
16
}
772
773
3
void JSONNodeDumper::VisitUsingDirectiveDecl(const UsingDirectiveDecl *UDD) {
774
3
  JOS.attribute("nominatedNamespace",
775
3
                createBareDeclRef(UDD->getNominatedNamespace()));
776
3
}
777
778
1
void JSONNodeDumper::VisitNamespaceAliasDecl(const NamespaceAliasDecl *NAD) {
779
1
  VisitNamedDecl(NAD);
780
1
  JOS.attribute("aliasedNamespace",
781
1
                createBareDeclRef(NAD->getAliasedNamespace()));
782
1
}
783
784
5
void JSONNodeDumper::VisitUsingDecl(const UsingDecl *UD) {
785
5
  std::string Name;
786
5
  if (const NestedNameSpecifier *NNS = UD->getQualifier()) {
787
5
    llvm::raw_string_ostream SOS(Name);
788
5
    NNS->print(SOS, UD->getASTContext().getPrintingPolicy());
789
5
  }
790
5
  Name += UD->getNameAsString();
791
5
  JOS.attribute("name", Name);
792
5
}
793
794
0
void JSONNodeDumper::VisitUsingEnumDecl(const UsingEnumDecl *UED) {
795
0
  JOS.attribute("target", createBareDeclRef(UED->getEnumDecl()));
796
0
}
797
798
4
void JSONNodeDumper::VisitUsingShadowDecl(const UsingShadowDecl *USD) {
799
4
  JOS.attribute("target", createBareDeclRef(USD->getTargetDecl()));
800
4
}
801
802
464
void JSONNodeDumper::VisitVarDecl(const VarDecl *VD) {
803
464
  VisitNamedDecl(VD);
804
464
  JOS.attribute("type", createQualType(VD->getType()));
805
806
464
  StorageClass SC = VD->getStorageClass();
807
464
  if (SC != SC_None)
808
26
    JOS.attribute("storageClass", VarDecl::getStorageClassSpecifierString(SC));
809
464
  switch (VD->getTLSKind()) {
810
0
  case VarDecl::TLS_Dynamic: JOS.attribute("tls", "dynamic"); break;
811
1
  case VarDecl::TLS_Static: JOS.attribute("tls", "static"); break;
812
463
  case VarDecl::TLS_None: break;
813
464
  }
814
464
  attributeOnlyIfTrue("nrvo", VD->isNRVOVariable());
815
464
  attributeOnlyIfTrue("inline", VD->isInline());
816
464
  attributeOnlyIfTrue("constexpr", VD->isConstexpr());
817
464
  attributeOnlyIfTrue("modulePrivate", VD->isModulePrivate());
818
464
  if (VD->hasInit()) {
819
84
    switch (VD->getInitStyle()) {
820
76
    case VarDecl::CInit: JOS.attribute("init", "c");  break;
821
8
    case VarDecl::CallInit: JOS.attribute("init", "call"); break;
822
0
    case VarDecl::ListInit: JOS.attribute("init", "list"); break;
823
84
    }
824
84
  }
825
464
  attributeOnlyIfTrue("isParameterPack", VD->isParameterPack());
826
464
}
827
828
103
void JSONNodeDumper::VisitFieldDecl(const FieldDecl *FD) {
829
103
  VisitNamedDecl(FD);
830
103
  JOS.attribute("type", createQualType(FD->getType()));
831
103
  attributeOnlyIfTrue("mutable", FD->isMutable());
832
103
  attributeOnlyIfTrue("modulePrivate", FD->isModulePrivate());
833
103
  attributeOnlyIfTrue("isBitfield", FD->isBitField());
834
103
  attributeOnlyIfTrue("hasInClassInitializer", FD->hasInClassInitializer());
835
103
}
836
837
439
void JSONNodeDumper::VisitFunctionDecl(const FunctionDecl *FD) {
838
439
  VisitNamedDecl(FD);
839
439
  JOS.attribute("type", createQualType(FD->getType()));
840
439
  StorageClass SC = FD->getStorageClass();
841
439
  if (SC != SC_None)
842
11
    JOS.attribute("storageClass", VarDecl::getStorageClassSpecifierString(SC));
843
439
  attributeOnlyIfTrue("inline", FD->isInlineSpecified());
844
439
  attributeOnlyIfTrue("virtual", FD->isVirtualAsWritten());
845
439
  attributeOnlyIfTrue("pure", FD->isPure());
846
439
  attributeOnlyIfTrue("explicitlyDeleted", FD->isDeletedAsWritten());
847
439
  attributeOnlyIfTrue("constexpr", FD->isConstexpr());
848
439
  attributeOnlyIfTrue("variadic", FD->isVariadic());
849
850
439
  if (FD->isDefaulted())
851
67
    JOS.attribute("explicitlyDefaulted",
852
67
                  FD->isDeleted() ? 
"deleted"0
: "default");
853
439
}
854
855
11
void JSONNodeDumper::VisitEnumDecl(const EnumDecl *ED) {
856
11
  VisitNamedDecl(ED);
857
11
  if (ED->isFixed())
858
4
    JOS.attribute("fixedUnderlyingType", createQualType(ED->getIntegerType()));
859
11
  if (ED->isScoped())
860
3
    JOS.attribute("scopedEnumTag",
861
3
                  ED->isScopedUsingClassTag() ? 
"class"2
:
"struct"1
);
862
11
}
863
18
void JSONNodeDumper::VisitEnumConstantDecl(const EnumConstantDecl *ECD) {
864
18
  VisitNamedDecl(ECD);
865
18
  JOS.attribute("type", createQualType(ECD->getType()));
866
18
}
867
868
239
void JSONNodeDumper::VisitRecordDecl(const RecordDecl *RD) {
869
239
  VisitNamedDecl(RD);
870
239
  JOS.attribute("tagUsed", RD->getKindName());
871
239
  attributeOnlyIfTrue("completeDefinition", RD->isCompleteDefinition());
872
239
}
873
221
void JSONNodeDumper::VisitCXXRecordDecl(const CXXRecordDecl *RD) {
874
221
  VisitRecordDecl(RD);
875
876
  // All other information requires a complete definition.
877
221
  if (!RD->isCompleteDefinition())
878
102
    return;
879
880
119
  JOS.attribute("definitionData", createCXXRecordDefinitionData(RD));
881
119
  if (RD->getNumBases()) {
882
11
    JOS.attributeArray("bases", [this, RD] {
883
11
      for (const auto &Spec : RD->bases())
884
14
        JOS.value(createCXXBaseSpecifier(Spec));
885
11
    });
886
11
  }
887
119
}
888
889
48
void JSONNodeDumper::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
890
48
  VisitNamedDecl(D);
891
48
  JOS.attribute("tagUsed", D->wasDeclaredWithTypename() ? 
"typename"40
:
"class"8
);
892
48
  JOS.attribute("depth", D->getDepth());
893
48
  JOS.attribute("index", D->getIndex());
894
48
  attributeOnlyIfTrue("isParameterPack", D->isParameterPack());
895
896
48
  if (D->hasDefaultArgument())
897
2
    JOS.attributeObject("defaultArg", [=] {
898
2
      Visit(D->getDefaultArgument(), SourceRange(),
899
2
            D->getDefaultArgStorage().getInheritedFrom(),
900
2
            D->defaultArgumentWasInherited() ? 
"inherited from"0
: "previous");
901
2
    });
902
48
}
903
904
void JSONNodeDumper::VisitNonTypeTemplateParmDecl(
905
6
    const NonTypeTemplateParmDecl *D) {
906
6
  VisitNamedDecl(D);
907
6
  JOS.attribute("type", createQualType(D->getType()));
908
6
  JOS.attribute("depth", D->getDepth());
909
6
  JOS.attribute("index", D->getIndex());
910
6
  attributeOnlyIfTrue("isParameterPack", D->isParameterPack());
911
912
6
  if (D->hasDefaultArgument())
913
0
    JOS.attributeObject("defaultArg", [=] {
914
0
      Visit(D->getDefaultArgument(), SourceRange(),
915
0
            D->getDefaultArgStorage().getInheritedFrom(),
916
0
            D->defaultArgumentWasInherited() ? "inherited from" : "previous");
917
0
    });
918
6
}
919
920
void JSONNodeDumper::VisitTemplateTemplateParmDecl(
921
2
    const TemplateTemplateParmDecl *D) {
922
2
  VisitNamedDecl(D);
923
2
  JOS.attribute("depth", D->getDepth());
924
2
  JOS.attribute("index", D->getIndex());
925
2
  attributeOnlyIfTrue("isParameterPack", D->isParameterPack());
926
927
2
  if (D->hasDefaultArgument())
928
1
    JOS.attributeObject("defaultArg", [=] {
929
1
      const auto *InheritedFrom = D->getDefaultArgStorage().getInheritedFrom();
930
1
      Visit(D->getDefaultArgument().getArgument(),
931
1
            InheritedFrom ? 
InheritedFrom->getSourceRange()0
: SourceLocation{},
932
1
            InheritedFrom,
933
1
            D->defaultArgumentWasInherited() ? 
"inherited from"0
: "previous");
934
1
    });
935
2
}
936
937
0
void JSONNodeDumper::VisitLinkageSpecDecl(const LinkageSpecDecl *LSD) {
938
0
  StringRef Lang;
939
0
  switch (LSD->getLanguage()) {
940
0
  case LinkageSpecDecl::lang_c: Lang = "C"; break;
941
0
  case LinkageSpecDecl::lang_cxx: Lang = "C++"; break;
942
0
  }
943
0
  JOS.attribute("language", Lang);
944
0
  attributeOnlyIfTrue("hasBraces", LSD->hasBraces());
945
0
}
946
947
37
void JSONNodeDumper::VisitAccessSpecDecl(const AccessSpecDecl *ASD) {
948
37
  JOS.attribute("access", createAccessSpecifier(ASD->getAccess()));
949
37
}
950
951
2
void JSONNodeDumper::VisitFriendDecl(const FriendDecl *FD) {
952
2
  if (const TypeSourceInfo *T = FD->getFriendType())
953
1
    JOS.attribute("type", createQualType(T->getType()));
954
2
}
955
956
8
void JSONNodeDumper::VisitObjCIvarDecl(const ObjCIvarDecl *D) {
957
8
  VisitNamedDecl(D);
958
8
  JOS.attribute("type", createQualType(D->getType()));
959
8
  attributeOnlyIfTrue("synthesized", D->getSynthesize());
960
8
  switch (D->getAccessControl()) {
961
0
  case ObjCIvarDecl::None: JOS.attribute("access", "none"); break;
962
5
  case ObjCIvarDecl::Private: JOS.attribute("access", "private"); break;
963
1
  case ObjCIvarDecl::Protected: JOS.attribute("access", "protected"); break;
964
1
  case ObjCIvarDecl::Public: JOS.attribute("access", "public"); break;
965
1
  case ObjCIvarDecl::Package: JOS.attribute("access", "package"); break;
966
8
  }
967
8
}
968
969
15
void JSONNodeDumper::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
970
15
  VisitNamedDecl(D);
971
15
  JOS.attribute("returnType", createQualType(D->getReturnType()));
972
15
  JOS.attribute("instance", D->isInstanceMethod());
973
15
  attributeOnlyIfTrue("variadic", D->isVariadic());
974
15
}
975
976
1
void JSONNodeDumper::VisitObjCTypeParamDecl(const ObjCTypeParamDecl *D) {
977
1
  VisitNamedDecl(D);
978
1
  JOS.attribute("type", createQualType(D->getUnderlyingType()));
979
1
  attributeOnlyIfTrue("bounded", D->hasExplicitBound());
980
1
  switch (D->getVariance()) {
981
1
  case ObjCTypeParamVariance::Invariant:
982
1
    break;
983
0
  case ObjCTypeParamVariance::Covariant:
984
0
    JOS.attribute("variance", "covariant");
985
0
    break;
986
0
  case ObjCTypeParamVariance::Contravariant:
987
0
    JOS.attribute("variance", "contravariant");
988
0
    break;
989
1
  }
990
1
}
991
992
1
void JSONNodeDumper::VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
993
1
  VisitNamedDecl(D);
994
1
  JOS.attribute("interface", createBareDeclRef(D->getClassInterface()));
995
1
  JOS.attribute("implementation", createBareDeclRef(D->getImplementation()));
996
997
1
  llvm::json::Array Protocols;
998
1
  for (const auto* P : D->protocols())
999
1
    Protocols.push_back(createBareDeclRef(P));
1000
1
  if (!Protocols.empty())
1001
1
    JOS.attribute("protocols", std::move(Protocols));
1002
1
}
1003
1004
1
void JSONNodeDumper::VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) {
1005
1
  VisitNamedDecl(D);
1006
1
  JOS.attribute("interface", createBareDeclRef(D->getClassInterface()));
1007
1
  JOS.attribute("categoryDecl", createBareDeclRef(D->getCategoryDecl()));
1008
1
}
1009
1010
1
void JSONNodeDumper::VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
1011
1
  VisitNamedDecl(D);
1012
1013
1
  llvm::json::Array Protocols;
1014
1
  for (const auto *P : D->protocols())
1015
0
    Protocols.push_back(createBareDeclRef(P));
1016
1
  if (!Protocols.empty())
1017
0
    JOS.attribute("protocols", std::move(Protocols));
1018
1
}
1019
1020
4
void JSONNodeDumper::VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
1021
4
  VisitNamedDecl(D);
1022
4
  JOS.attribute("super", createBareDeclRef(D->getSuperClass()));
1023
4
  JOS.attribute("implementation", createBareDeclRef(D->getImplementation()));
1024
1025
4
  llvm::json::Array Protocols;
1026
4
  for (const auto* P : D->protocols())
1027
2
    Protocols.push_back(createBareDeclRef(P));
1028
4
  if (!Protocols.empty())
1029
2
    JOS.attribute("protocols", std::move(Protocols));
1030
4
}
1031
1032
void JSONNodeDumper::VisitObjCImplementationDecl(
1033
3
    const ObjCImplementationDecl *D) {
1034
3
  VisitNamedDecl(D);
1035
3
  JOS.attribute("super", createBareDeclRef(D->getSuperClass()));
1036
3
  JOS.attribute("interface", createBareDeclRef(D->getClassInterface()));
1037
3
}
1038
1039
void JSONNodeDumper::VisitObjCCompatibleAliasDecl(
1040
1
    const ObjCCompatibleAliasDecl *D) {
1041
1
  VisitNamedDecl(D);
1042
1
  JOS.attribute("interface", createBareDeclRef(D->getClassInterface()));
1043
1
}
1044
1045
2
void JSONNodeDumper::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
1046
2
  VisitNamedDecl(D);
1047
2
  JOS.attribute("type", createQualType(D->getType()));
1048
1049
2
  switch (D->getPropertyImplementation()) {
1050
2
  case ObjCPropertyDecl::None: break;
1051
0
  case ObjCPropertyDecl::Required: JOS.attribute("control", "required"); break;
1052
0
  case ObjCPropertyDecl::Optional: JOS.attribute("control", "optional"); break;
1053
2
  }
1054
1055
2
  ObjCPropertyAttribute::Kind Attrs = D->getPropertyAttributes();
1056
2
  if (Attrs != ObjCPropertyAttribute::kind_noattr) {
1057
2
    if (Attrs & ObjCPropertyAttribute::kind_getter)
1058
1
      JOS.attribute("getter", createBareDeclRef(D->getGetterMethodDecl()));
1059
2
    if (Attrs & ObjCPropertyAttribute::kind_setter)
1060
1
      JOS.attribute("setter", createBareDeclRef(D->getSetterMethodDecl()));
1061
2
    attributeOnlyIfTrue("readonly",
1062
2
                        Attrs & ObjCPropertyAttribute::kind_readonly);
1063
2
    attributeOnlyIfTrue("assign", Attrs & ObjCPropertyAttribute::kind_assign);
1064
2
    attributeOnlyIfTrue("readwrite",
1065
2
                        Attrs & ObjCPropertyAttribute::kind_readwrite);
1066
2
    attributeOnlyIfTrue("retain", Attrs & ObjCPropertyAttribute::kind_retain);
1067
2
    attributeOnlyIfTrue("copy", Attrs & ObjCPropertyAttribute::kind_copy);
1068
2
    attributeOnlyIfTrue("nonatomic",
1069
2
                        Attrs & ObjCPropertyAttribute::kind_nonatomic);
1070
2
    attributeOnlyIfTrue("atomic", Attrs & ObjCPropertyAttribute::kind_atomic);
1071
2
    attributeOnlyIfTrue("weak", Attrs & ObjCPropertyAttribute::kind_weak);
1072
2
    attributeOnlyIfTrue("strong", Attrs & ObjCPropertyAttribute::kind_strong);
1073
2
    attributeOnlyIfTrue("unsafe_unretained",
1074
2
                        Attrs & ObjCPropertyAttribute::kind_unsafe_unretained);
1075
2
    attributeOnlyIfTrue("class", Attrs & ObjCPropertyAttribute::kind_class);
1076
2
    attributeOnlyIfTrue("direct", Attrs & ObjCPropertyAttribute::kind_direct);
1077
2
    attributeOnlyIfTrue("nullability",
1078
2
                        Attrs & ObjCPropertyAttribute::kind_nullability);
1079
2
    attributeOnlyIfTrue("null_resettable",
1080
2
                        Attrs & ObjCPropertyAttribute::kind_null_resettable);
1081
2
  }
1082
2
}
1083
1084
2
void JSONNodeDumper::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
1085
2
  VisitNamedDecl(D->getPropertyDecl());
1086
2
  JOS.attribute("implKind", D->getPropertyImplementation() ==
1087
2
                                    ObjCPropertyImplDecl::Synthesize
1088
2
                                ? "synthesize"
1089
2
                                : 
"dynamic"0
);
1090
2
  JOS.attribute("propertyDecl", createBareDeclRef(D->getPropertyDecl()));
1091
2
  JOS.attribute("ivarDecl", createBareDeclRef(D->getPropertyIvarDecl()));
1092
2
}
1093
1094
3
void JSONNodeDumper::VisitBlockDecl(const BlockDecl *D) {
1095
3
  attributeOnlyIfTrue("variadic", D->isVariadic());
1096
3
  attributeOnlyIfTrue("capturesThis", D->capturesCXXThis());
1097
3
}
1098
1099
2
void JSONNodeDumper::VisitObjCEncodeExpr(const ObjCEncodeExpr *OEE) {
1100
2
  JOS.attribute("encodedType", createQualType(OEE->getEncodedType()));
1101
2
}
1102
1103
9
void JSONNodeDumper::VisitObjCMessageExpr(const ObjCMessageExpr *OME) {
1104
9
  std::string Str;
1105
9
  llvm::raw_string_ostream OS(Str);
1106
1107
9
  OME->getSelector().print(OS);
1108
9
  JOS.attribute("selector", OS.str());
1109
1110
9
  switch (OME->getReceiverKind()) {
1111
8
  case ObjCMessageExpr::Instance:
1112
8
    JOS.attribute("receiverKind", "instance");
1113
8
    break;
1114
1
  case ObjCMessageExpr::Class:
1115
1
    JOS.attribute("receiverKind", "class");
1116
1
    JOS.attribute("classType", createQualType(OME->getClassReceiver()));
1117
1
    break;
1118
0
  case ObjCMessageExpr::SuperInstance:
1119
0
    JOS.attribute("receiverKind", "super (instance)");
1120
0
    JOS.attribute("superType", createQualType(OME->getSuperType()));
1121
0
    break;
1122
0
  case ObjCMessageExpr::SuperClass:
1123
0
    JOS.attribute("receiverKind", "super (class)");
1124
0
    JOS.attribute("superType", createQualType(OME->getSuperType()));
1125
0
    break;
1126
9
  }
1127
1128
9
  QualType CallReturnTy = OME->getCallReturnType(Ctx);
1129
9
  if (OME->getType() != CallReturnTy)
1130
0
    JOS.attribute("callReturnType", createQualType(CallReturnTy));
1131
9
}
1132
1133
1
void JSONNodeDumper::VisitObjCBoxedExpr(const ObjCBoxedExpr *OBE) {
1134
1
  if (const ObjCMethodDecl *MD = OBE->getBoxingMethod()) {
1135
1
    std::string Str;
1136
1
    llvm::raw_string_ostream OS(Str);
1137
1138
1
    MD->getSelector().print(OS);
1139
1
    JOS.attribute("selector", OS.str());
1140
1
  }
1141
1
}
1142
1143
1
void JSONNodeDumper::VisitObjCSelectorExpr(const ObjCSelectorExpr *OSE) {
1144
1
  std::string Str;
1145
1
  llvm::raw_string_ostream OS(Str);
1146
1147
1
  OSE->getSelector().print(OS);
1148
1
  JOS.attribute("selector", OS.str());
1149
1
}
1150
1151
1
void JSONNodeDumper::VisitObjCProtocolExpr(const ObjCProtocolExpr *OPE) {
1152
1
  JOS.attribute("protocol", createBareDeclRef(OPE->getProtocol()));
1153
1
}
1154
1155
2
void JSONNodeDumper::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *OPRE) {
1156
2
  if (OPRE->isImplicitProperty()) {
1157
0
    JOS.attribute("propertyKind", "implicit");
1158
0
    if (const ObjCMethodDecl *MD = OPRE->getImplicitPropertyGetter())
1159
0
      JOS.attribute("getter", createBareDeclRef(MD));
1160
0
    if (const ObjCMethodDecl *MD = OPRE->getImplicitPropertySetter())
1161
0
      JOS.attribute("setter", createBareDeclRef(MD));
1162
2
  } else {
1163
2
    JOS.attribute("propertyKind", "explicit");
1164
2
    JOS.attribute("property", createBareDeclRef(OPRE->getExplicitProperty()));
1165
2
  }
1166
1167
2
  attributeOnlyIfTrue("isSuperReceiver", OPRE->isSuperReceiver());
1168
2
  attributeOnlyIfTrue("isMessagingGetter", OPRE->isMessagingGetter());
1169
2
  attributeOnlyIfTrue("isMessagingSetter", OPRE->isMessagingSetter());
1170
2
}
1171
1172
void JSONNodeDumper::VisitObjCSubscriptRefExpr(
1173
4
    const ObjCSubscriptRefExpr *OSRE) {
1174
4
  JOS.attribute("subscriptKind",
1175
4
                OSRE->isArraySubscriptRefExpr() ? 
"array"2
:
"dictionary"2
);
1176
1177
4
  if (const ObjCMethodDecl *MD = OSRE->getAtIndexMethodDecl())
1178
0
    JOS.attribute("getter", createBareDeclRef(MD));
1179
4
  if (const ObjCMethodDecl *MD = OSRE->setAtIndexMethodDecl())
1180
0
    JOS.attribute("setter", createBareDeclRef(MD));
1181
4
}
1182
1183
1
void JSONNodeDumper::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *OIRE) {
1184
1
  JOS.attribute("decl", createBareDeclRef(OIRE->getDecl()));
1185
1
  attributeOnlyIfTrue("isFreeIvar", OIRE->isFreeIvar());
1186
1
  JOS.attribute("isArrow", OIRE->isArrow());
1187
1
}
1188
1189
2
void JSONNodeDumper::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *OBLE) {
1190
2
  JOS.attribute("value", OBLE->getValue() ? 
"__objc_yes"1
:
"__objc_no"1
);
1191
2
}
1192
1193
463
void JSONNodeDumper::VisitDeclRefExpr(const DeclRefExpr *DRE) {
1194
463
  JOS.attribute("referencedDecl", createBareDeclRef(DRE->getDecl()));
1195
463
  if (DRE->getDecl() != DRE->getFoundDecl())
1196
4
    JOS.attribute("foundReferencedDecl",
1197
4
                  createBareDeclRef(DRE->getFoundDecl()));
1198
463
  switch (DRE->isNonOdrUse()) {
1199
448
  case NOUR_None: break;
1200
14
  case NOUR_Unevaluated: JOS.attribute("nonOdrUseReason", "unevaluated"); break;
1201
1
  case NOUR_Constant: JOS.attribute("nonOdrUseReason", "constant"); break;
1202
0
  case NOUR_Discarded: JOS.attribute("nonOdrUseReason", "discarded"); break;
1203
463
  }
1204
463
}
1205
1206
void JSONNodeDumper::VisitSYCLUniqueStableNameExpr(
1207
0
    const SYCLUniqueStableNameExpr *E) {
1208
0
  JOS.attribute("typeSourceInfo",
1209
0
                createQualType(E->getTypeSourceInfo()->getType()));
1210
0
}
1211
1212
0
void JSONNodeDumper::VisitPredefinedExpr(const PredefinedExpr *PE) {
1213
0
  JOS.attribute("name", PredefinedExpr::getIdentKindName(PE->getIdentKind()));
1214
0
}
1215
1216
46
void JSONNodeDumper::VisitUnaryOperator(const UnaryOperator *UO) {
1217
46
  JOS.attribute("isPostfix", UO->isPostfix());
1218
46
  JOS.attribute("opcode", UnaryOperator::getOpcodeStr(UO->getOpcode()));
1219
46
  if (!UO->canOverflow())
1220
20
    JOS.attribute("canOverflow", false);
1221
46
}
1222
1223
151
void JSONNodeDumper::VisitBinaryOperator(const BinaryOperator *BO) {
1224
151
  JOS.attribute("opcode", BinaryOperator::getOpcodeStr(BO->getOpcode()));
1225
151
}
1226
1227
void JSONNodeDumper::VisitCompoundAssignOperator(
1228
1
    const CompoundAssignOperator *CAO) {
1229
1
  VisitBinaryOperator(CAO);
1230
1
  JOS.attribute("computeLHSType", createQualType(CAO->getComputationLHSType()));
1231
1
  JOS.attribute("computeResultType",
1232
1
                createQualType(CAO->getComputationResultType()));
1233
1
}
1234
1235
60
void JSONNodeDumper::VisitMemberExpr(const MemberExpr *ME) {
1236
  // Note, we always write this Boolean field because the information it conveys
1237
  // is critical to understanding the AST node.
1238
60
  ValueDecl *VD = ME->getMemberDecl();
1239
60
  JOS.attribute("name", VD && VD->getDeclName() ? VD->getNameAsString() : 
""0
);
1240
60
  JOS.attribute("isArrow", ME->isArrow());
1241
60
  JOS.attribute("referencedMemberDecl", createPointerRepresentation(VD));
1242
60
  switch (ME->isNonOdrUse()) {
1243
60
  case NOUR_None: break;
1244
0
  case NOUR_Unevaluated: JOS.attribute("nonOdrUseReason", "unevaluated"); break;
1245
0
  case NOUR_Constant: JOS.attribute("nonOdrUseReason", "constant"); break;
1246
0
  case NOUR_Discarded: JOS.attribute("nonOdrUseReason", "discarded"); break;
1247
60
  }
1248
60
}
1249
1250
11
void JSONNodeDumper::VisitCXXNewExpr(const CXXNewExpr *NE) {
1251
11
  attributeOnlyIfTrue("isGlobal", NE->isGlobalNew());
1252
11
  attributeOnlyIfTrue("isArray", NE->isArray());
1253
11
  attributeOnlyIfTrue("isPlacement", NE->getNumPlacementArgs() != 0);
1254
11
  switch (NE->getInitializationStyle()) {
1255
8
  case CXXNewExpr::NoInit: break;
1256
0
  case CXXNewExpr::CallInit: JOS.attribute("initStyle", "call"); break;
1257
3
  case CXXNewExpr::ListInit: JOS.attribute("initStyle", "list"); break;
1258
11
  }
1259
11
  if (const FunctionDecl *FD = NE->getOperatorNew())
1260
10
    JOS.attribute("operatorNewDecl", createBareDeclRef(FD));
1261
11
  if (const FunctionDecl *FD = NE->getOperatorDelete())
1262
0
    JOS.attribute("operatorDeleteDecl", createBareDeclRef(FD));
1263
11
}
1264
6
void JSONNodeDumper::VisitCXXDeleteExpr(const CXXDeleteExpr *DE) {
1265
6
  attributeOnlyIfTrue("isGlobal", DE->isGlobalDelete());
1266
6
  attributeOnlyIfTrue("isArray", DE->isArrayForm());
1267
6
  attributeOnlyIfTrue("isArrayAsWritten", DE->isArrayFormAsWritten());
1268
6
  if (const FunctionDecl *FD = DE->getOperatorDelete())
1269
5
    JOS.attribute("operatorDeleteDecl", createBareDeclRef(FD));
1270
6
}
1271
1272
33
void JSONNodeDumper::VisitCXXThisExpr(const CXXThisExpr *TE) {
1273
33
  attributeOnlyIfTrue("implicit", TE->isImplicit());
1274
33
}
1275
1276
492
void JSONNodeDumper::VisitCastExpr(const CastExpr *CE) {
1277
492
  JOS.attribute("castKind", CE->getCastKindName());
1278
492
  llvm::json::Array Path = createCastPath(CE);
1279
492
  if (!Path.empty())
1280
1
    JOS.attribute("path", std::move(Path));
1281
  // FIXME: This may not be useful information as it can be obtusely gleaned
1282
  // from the inner[] array.
1283
492
  if (const NamedDecl *ND = CE->getConversionFunction())
1284
0
    JOS.attribute("conversionFunc", createBareDeclRef(ND));
1285
492
}
1286
1287
463
void JSONNodeDumper::VisitImplicitCastExpr(const ImplicitCastExpr *ICE) {
1288
463
  VisitCastExpr(ICE);
1289
463
  attributeOnlyIfTrue("isPartOfExplicitCast", ICE->isPartOfExplicitCast());
1290
463
}
1291
1292
56
void JSONNodeDumper::VisitCallExpr(const CallExpr *CE) {
1293
56
  attributeOnlyIfTrue("adl", CE->usesADL());
1294
56
}
1295
1296
void JSONNodeDumper::VisitUnaryExprOrTypeTraitExpr(
1297
6
    const UnaryExprOrTypeTraitExpr *TTE) {
1298
6
  JOS.attribute("name", getTraitSpelling(TTE->getKind()));
1299
6
  if (TTE->isArgumentType())
1300
2
    JOS.attribute("argType", createQualType(TTE->getArgumentType()));
1301
6
}
1302
1303
1
void JSONNodeDumper::VisitSizeOfPackExpr(const SizeOfPackExpr *SOPE) {
1304
1
  VisitNamedDecl(SOPE->getPack());
1305
1
}
1306
1307
void JSONNodeDumper::VisitUnresolvedLookupExpr(
1308
1
    const UnresolvedLookupExpr *ULE) {
1309
1
  JOS.attribute("usesADL", ULE->requiresADL());
1310
1
  JOS.attribute("name", ULE->getName().getAsString());
1311
1312
1
  JOS.attributeArray("lookups", [this, ULE] {
1313
1
    for (const NamedDecl *D : ULE->decls())
1314
0
      JOS.value(createBareDeclRef(D));
1315
1
  });
1316
1
}
1317
1318
1
void JSONNodeDumper::VisitAddrLabelExpr(const AddrLabelExpr *ALE) {
1319
1
  JOS.attribute("name", ALE->getLabel()->getName());
1320
1
  JOS.attribute("labelDeclId", createPointerRepresentation(ALE->getLabel()));
1321
1
}
1322
1323
3
void JSONNodeDumper::VisitCXXTypeidExpr(const CXXTypeidExpr *CTE) {
1324
3
  if (CTE->isTypeOperand()) {
1325
2
    QualType Adjusted = CTE->getTypeOperand(Ctx);
1326
2
    QualType Unadjusted = CTE->getTypeOperandSourceInfo()->getType();
1327
2
    JOS.attribute("typeArg", createQualType(Unadjusted));
1328
2
    if (Adjusted != Unadjusted)
1329
1
      JOS.attribute("adjustedTypeArg", createQualType(Adjusted));
1330
2
  }
1331
3
}
1332
1333
16
void JSONNodeDumper::VisitConstantExpr(const ConstantExpr *CE) {
1334
16
  if (CE->getResultAPValueKind() != APValue::None)
1335
11
    Visit(CE->getAPValueResult(), CE->getType());
1336
16
}
1337
1338
10
void JSONNodeDumper::VisitInitListExpr(const InitListExpr *ILE) {
1339
10
  if (const FieldDecl *FD = ILE->getInitializedFieldInUnion())
1340
2
    JOS.attribute("field", createBareDeclRef(FD));
1341
10
}
1342
1343
void JSONNodeDumper::VisitGenericSelectionExpr(
1344
7
    const GenericSelectionExpr *GSE) {
1345
7
  attributeOnlyIfTrue("resultDependent", GSE->isResultDependent());
1346
7
}
1347
1348
void JSONNodeDumper::VisitCXXUnresolvedConstructExpr(
1349
4
    const CXXUnresolvedConstructExpr *UCE) {
1350
4
  if (UCE->getType() != UCE->getTypeAsWritten())
1351
0
    JOS.attribute("typeAsWritten", createQualType(UCE->getTypeAsWritten()));
1352
4
  attributeOnlyIfTrue("list", UCE->isListInitialization());
1353
4
}
1354
1355
20
void JSONNodeDumper::VisitCXXConstructExpr(const CXXConstructExpr *CE) {
1356
20
  CXXConstructorDecl *Ctor = CE->getConstructor();
1357
20
  JOS.attribute("ctorType", createQualType(Ctor->getType()));
1358
20
  attributeOnlyIfTrue("elidable", CE->isElidable());
1359
20
  attributeOnlyIfTrue("list", CE->isListInitialization());
1360
20
  attributeOnlyIfTrue("initializer_list", CE->isStdInitListInitialization());
1361
20
  attributeOnlyIfTrue("zeroing", CE->requiresZeroInitialization());
1362
20
  attributeOnlyIfTrue("hadMultipleCandidates", CE->hadMultipleCandidates());
1363
1364
20
  switch (CE->getConstructionKind()) {
1365
18
  case CXXConstructExpr::CK_Complete:
1366
18
    JOS.attribute("constructionKind", "complete");
1367
18
    break;
1368
0
  case CXXConstructExpr::CK_Delegating:
1369
0
    JOS.attribute("constructionKind", "delegating");
1370
0
    break;
1371
2
  case CXXConstructExpr::CK_NonVirtualBase:
1372
2
    JOS.attribute("constructionKind", "non-virtual base");
1373
2
    break;
1374
0
  case CXXConstructExpr::CK_VirtualBase:
1375
0
    JOS.attribute("constructionKind", "virtual base");
1376
0
    break;
1377
20
  }
1378
20
}
1379
1380
9
void JSONNodeDumper::VisitExprWithCleanups(const ExprWithCleanups *EWC) {
1381
9
  attributeOnlyIfTrue("cleanupsHaveSideEffects",
1382
9
                      EWC->cleanupsHaveSideEffects());
1383
9
  if (EWC->getNumObjects()) {
1384
4
    JOS.attributeArray("cleanups", [this, EWC] {
1385
4
      for (const ExprWithCleanups::CleanupObject &CO : EWC->getObjects())
1386
4
        if (auto *BD = CO.dyn_cast<BlockDecl *>()) {
1387
3
          JOS.value(createBareDeclRef(BD));
1388
3
        } else 
if (auto *1
CLE1
= CO.dyn_cast<CompoundLiteralExpr *>()) {
1389
1
          llvm::json::Object Obj;
1390
1
          Obj["id"] = createPointerRepresentation(CLE);
1391
1
          Obj["kind"] = CLE->getStmtClassName();
1392
1
          JOS.value(std::move(Obj));
1393
1
        } else {
1394
0
          llvm_unreachable("unexpected cleanup object type");
1395
0
        }
1396
4
    });
1397
4
  }
1398
9
}
1399
1400
void JSONNodeDumper::VisitCXXBindTemporaryExpr(
1401
1
    const CXXBindTemporaryExpr *BTE) {
1402
1
  const CXXTemporary *Temp = BTE->getTemporary();
1403
1
  JOS.attribute("temp", createPointerRepresentation(Temp));
1404
1
  if (const CXXDestructorDecl *Dtor = Temp->getDestructor())
1405
1
    JOS.attribute("dtor", createBareDeclRef(Dtor));
1406
1
}
1407
1408
void JSONNodeDumper::VisitMaterializeTemporaryExpr(
1409
5
    const MaterializeTemporaryExpr *MTE) {
1410
5
  if (const ValueDecl *VD = MTE->getExtendingDecl())
1411
4
    JOS.attribute("extendingDecl", createBareDeclRef(VD));
1412
1413
5
  switch (MTE->getStorageDuration()) {
1414
4
  case SD_Automatic:
1415
4
    JOS.attribute("storageDuration", "automatic");
1416
4
    break;
1417
0
  case SD_Dynamic:
1418
0
    JOS.attribute("storageDuration", "dynamic");
1419
0
    break;
1420
1
  case SD_FullExpression:
1421
1
    JOS.attribute("storageDuration", "full expression");
1422
1
    break;
1423
0
  case SD_Static:
1424
0
    JOS.attribute("storageDuration", "static");
1425
0
    break;
1426
0
  case SD_Thread:
1427
0
    JOS.attribute("storageDuration", "thread");
1428
0
    break;
1429
5
  }
1430
1431
5
  attributeOnlyIfTrue("boundToLValueRef", MTE->isBoundToLvalueReference());
1432
5
}
1433
1434
void JSONNodeDumper::VisitCXXDependentScopeMemberExpr(
1435
8
    const CXXDependentScopeMemberExpr *DSME) {
1436
8
  JOS.attribute("isArrow", DSME->isArrow());
1437
8
  JOS.attribute("member", DSME->getMember().getAsString());
1438
8
  attributeOnlyIfTrue("hasTemplateKeyword", DSME->hasTemplateKeyword());
1439
8
  attributeOnlyIfTrue("hasExplicitTemplateArgs",
1440
8
                      DSME->hasExplicitTemplateArgs());
1441
1442
8
  if (DSME->getNumTemplateArgs()) {
1443
1
    JOS.attributeArray("explicitTemplateArgs", [DSME, this] {
1444
1
      for (const TemplateArgumentLoc &TAL : DSME->template_arguments())
1445
1
        JOS.object(
1446
1
            [&TAL, this] { Visit(TAL.getArgument(), TAL.getSourceRange()); });
1447
1
    });
1448
1
  }
1449
8
}
1450
1451
1
void JSONNodeDumper::VisitRequiresExpr(const RequiresExpr *RE) {
1452
1
  if (!RE->isValueDependent())
1453
0
    JOS.attribute("satisfied", RE->isSatisfied());
1454
1
}
1455
1456
211
void JSONNodeDumper::VisitIntegerLiteral(const IntegerLiteral *IL) {
1457
211
  llvm::SmallString<16> Buffer;
1458
211
  IL->getValue().toString(Buffer,
1459
211
                          /*Radix=*/10, IL->getType()->isSignedIntegerType());
1460
211
  JOS.attribute("value", Buffer);
1461
211
}
1462
2
void JSONNodeDumper::VisitCharacterLiteral(const CharacterLiteral *CL) {
1463
  // FIXME: This should probably print the character literal as a string,
1464
  // rather than as a numerical value. It would be nice if the behavior matched
1465
  // what we do to print a string literal; right now, it is impossible to tell
1466
  // the difference between 'a' and L'a' in C from the JSON output.
1467
2
  JOS.attribute("value", CL->getValue());
1468
2
}
1469
0
void JSONNodeDumper::VisitFixedPointLiteral(const FixedPointLiteral *FPL) {
1470
0
  JOS.attribute("value", FPL->getValueAsString(/*Radix=*/10));
1471
0
}
1472
22
void JSONNodeDumper::VisitFloatingLiteral(const FloatingLiteral *FL) {
1473
22
  llvm::SmallString<16> Buffer;
1474
22
  FL->getValue().toString(Buffer);
1475
22
  JOS.attribute("value", Buffer);
1476
22
}
1477
12
void JSONNodeDumper::VisitStringLiteral(const StringLiteral *SL) {
1478
12
  std::string Buffer;
1479
12
  llvm::raw_string_ostream SS(Buffer);
1480
12
  SL->outputString(SS);
1481
12
  JOS.attribute("value", SS.str());
1482
12
}
1483
5
void JSONNodeDumper::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *BLE) {
1484
5
  JOS.attribute("value", BLE->getValue());
1485
5
}
1486
1487
18
void JSONNodeDumper::VisitIfStmt(const IfStmt *IS) {
1488
18
  attributeOnlyIfTrue("hasInit", IS->hasInitStorage());
1489
18
  attributeOnlyIfTrue("hasVar", IS->hasVarStorage());
1490
18
  attributeOnlyIfTrue("hasElse", IS->hasElseStorage());
1491
18
  attributeOnlyIfTrue("isConstexpr", IS->isConstexpr());
1492
18
}
1493
1494
2
void JSONNodeDumper::VisitSwitchStmt(const SwitchStmt *SS) {
1495
2
  attributeOnlyIfTrue("hasInit", SS->hasInitStorage());
1496
2
  attributeOnlyIfTrue("hasVar", SS->hasVarStorage());
1497
2
}
1498
4
void JSONNodeDumper::VisitCaseStmt(const CaseStmt *CS) {
1499
4
  attributeOnlyIfTrue("isGNURange", CS->caseStmtIsGNURange());
1500
4
}
1501
1502
2
void JSONNodeDumper::VisitLabelStmt(const LabelStmt *LS) {
1503
2
  JOS.attribute("name", LS->getName());
1504
2
  JOS.attribute("declId", createPointerRepresentation(LS->getDecl()));
1505
2
  attributeOnlyIfTrue("sideEntry", LS->isSideEntry());
1506
2
}
1507
1
void JSONNodeDumper::VisitGotoStmt(const GotoStmt *GS) {
1508
1
  JOS.attribute("targetLabelDeclId",
1509
1
                createPointerRepresentation(GS->getLabel()));
1510
1
}
1511
1512
2
void JSONNodeDumper::VisitWhileStmt(const WhileStmt *WS) {
1513
2
  attributeOnlyIfTrue("hasVar", WS->hasVarStorage());
1514
2
}
1515
1516
2
void JSONNodeDumper::VisitObjCAtCatchStmt(const ObjCAtCatchStmt* OACS) {
1517
  // FIXME: it would be nice for the ASTNodeTraverser would handle the catch
1518
  // parameter the same way for C++ and ObjC rather. In this case, C++ gets a
1519
  // null child node and ObjC gets no child node.
1520
2
  attributeOnlyIfTrue("isCatchAll", OACS->getCatchParamDecl() == nullptr);
1521
2
}
1522
1523
0
void JSONNodeDumper::VisitNullTemplateArgument(const TemplateArgument &TA) {
1524
0
  JOS.attribute("isNull", true);
1525
0
}
1526
28
void JSONNodeDumper::VisitTypeTemplateArgument(const TemplateArgument &TA) {
1527
28
  JOS.attribute("type", createQualType(TA.getAsType()));
1528
28
}
1529
void JSONNodeDumper::VisitDeclarationTemplateArgument(
1530
0
    const TemplateArgument &TA) {
1531
0
  JOS.attribute("decl", createBareDeclRef(TA.getAsDecl()));
1532
0
}
1533
0
void JSONNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &TA) {
1534
0
  JOS.attribute("isNullptr", true);
1535
0
}
1536
3
void JSONNodeDumper::VisitIntegralTemplateArgument(const TemplateArgument &TA) {
1537
3
  JOS.attribute("value", TA.getAsIntegral().getSExtValue());
1538
3
}
1539
2
void JSONNodeDumper::VisitTemplateTemplateArgument(const TemplateArgument &TA) {
1540
  // FIXME: cannot just call dump() on the argument, as that doesn't specify
1541
  // the output format.
1542
2
}
1543
void JSONNodeDumper::VisitTemplateExpansionTemplateArgument(
1544
0
    const TemplateArgument &TA) {
1545
  // FIXME: cannot just call dump() on the argument, as that doesn't specify
1546
  // the output format.
1547
0
}
1548
void JSONNodeDumper::VisitExpressionTemplateArgument(
1549
0
    const TemplateArgument &TA) {
1550
0
  JOS.attribute("isExpr", true);
1551
0
}
1552
0
void JSONNodeDumper::VisitPackTemplateArgument(const TemplateArgument &TA) {
1553
0
  JOS.attribute("isPack", true);
1554
0
}
1555
1556
3
StringRef JSONNodeDumper::getCommentCommandName(unsigned CommandID) const {
1557
3
  if (Traits)
1558
3
    return Traits->getCommandInfo(CommandID)->Name;
1559
0
  if (const comments::CommandInfo *Info =
1560
0
          comments::CommandTraits::getBuiltinCommandInfo(CommandID))
1561
0
    return Info->Name;
1562
0
  return "<invalid>";
1563
0
}
1564
1565
void JSONNodeDumper::visitTextComment(const comments::TextComment *C,
1566
21
                                      const comments::FullComment *) {
1567
21
  JOS.attribute("text", C->getText());
1568
21
}
1569
1570
void JSONNodeDumper::visitInlineCommandComment(
1571
1
    const comments::InlineCommandComment *C, const comments::FullComment *) {
1572
1
  JOS.attribute("name", getCommentCommandName(C->getCommandID()));
1573
1574
1
  switch (C->getRenderKind()) {
1575
0
  case comments::InlineCommandComment::RenderNormal:
1576
0
    JOS.attribute("renderKind", "normal");
1577
0
    break;
1578
0
  case comments::InlineCommandComment::RenderBold:
1579
0
    JOS.attribute("renderKind", "bold");
1580
0
    break;
1581
0
  case comments::InlineCommandComment::RenderEmphasized:
1582
0
    JOS.attribute("renderKind", "emphasized");
1583
0
    break;
1584
1
  case comments::InlineCommandComment::RenderMonospaced:
1585
1
    JOS.attribute("renderKind", "monospaced");
1586
1
    break;
1587
0
  case comments::InlineCommandComment::RenderAnchor:
1588
0
    JOS.attribute("renderKind", "anchor");
1589
0
    break;
1590
1
  }
1591
1592
1
  llvm::json::Array Args;
1593
2
  for (unsigned I = 0, E = C->getNumArgs(); I < E; 
++I1
)
1594
1
    Args.push_back(C->getArgText(I));
1595
1596
1
  if (!Args.empty())
1597
1
    JOS.attribute("args", std::move(Args));
1598
1
}
1599
1600
void JSONNodeDumper::visitHTMLStartTagComment(
1601
2
    const comments::HTMLStartTagComment *C, const comments::FullComment *) {
1602
2
  JOS.attribute("name", C->getTagName());
1603
2
  attributeOnlyIfTrue("selfClosing", C->isSelfClosing());
1604
2
  attributeOnlyIfTrue("malformed", C->isMalformed());
1605
1606
2
  llvm::json::Array Attrs;
1607
2
  for (unsigned I = 0, E = C->getNumAttrs(); I < E; 
++I0
)
1608
0
    Attrs.push_back(
1609
0
        {{"name", C->getAttr(I).Name}, {"value", C->getAttr(I).Value}});
1610
1611
2
  if (!Attrs.empty())
1612
0
    JOS.attribute("attrs", std::move(Attrs));
1613
2
}
1614
1615
void JSONNodeDumper::visitHTMLEndTagComment(
1616
1
    const comments::HTMLEndTagComment *C, const comments::FullComment *) {
1617
1
  JOS.attribute("name", C->getTagName());
1618
1
}
1619
1620
void JSONNodeDumper::visitBlockCommandComment(
1621
1
    const comments::BlockCommandComment *C, const comments::FullComment *) {
1622
1
  JOS.attribute("name", getCommentCommandName(C->getCommandID()));
1623
1624
1
  llvm::json::Array Args;
1625
1
  for (unsigned I = 0, E = C->getNumArgs(); I < E; 
++I0
)
1626
0
    Args.push_back(C->getArgText(I));
1627
1628
1
  if (!Args.empty())
1629
0
    JOS.attribute("args", std::move(Args));
1630
1
}
1631
1632
void JSONNodeDumper::visitParamCommandComment(
1633
4
    const comments::ParamCommandComment *C, const comments::FullComment *FC) {
1634
4
  switch (C->getDirection()) {
1635
3
  case comments::ParamCommandComment::In:
1636
3
    JOS.attribute("direction", "in");
1637
3
    break;
1638
0
  case comments::ParamCommandComment::Out:
1639
0
    JOS.attribute("direction", "out");
1640
0
    break;
1641
1
  case comments::ParamCommandComment::InOut:
1642
1
    JOS.attribute("direction", "in,out");
1643
1
    break;
1644
4
  }
1645
4
  attributeOnlyIfTrue("explicit", C->isDirectionExplicit());
1646
1647
4
  if (C->hasParamName())
1648
4
    JOS.attribute("param", C->isParamIndexValid() ? C->getParamName(FC)
1649
4
                                                  : 
C->getParamNameAsWritten()0
);
1650
1651
4
  if (C->isParamIndexValid() && !C->isVarArgParam())
1652
2
    JOS.attribute("paramIdx", C->getParamIndex());
1653
4
}
1654
1655
void JSONNodeDumper::visitTParamCommandComment(
1656
2
    const comments::TParamCommandComment *C, const comments::FullComment *FC) {
1657
2
  if (C->hasParamName())
1658
2
    JOS.attribute("param", C->isPositionValid() ? 
C->getParamName(FC)1
1659
2
                                                : 
C->getParamNameAsWritten()1
);
1660
2
  if (C->isPositionValid()) {
1661
1
    llvm::json::Array Positions;
1662
2
    for (unsigned I = 0, E = C->getDepth(); I < E; 
++I1
)
1663
1
      Positions.push_back(C->getIndex(I));
1664
1665
1
    if (!Positions.empty())
1666
1
      JOS.attribute("positions", std::move(Positions));
1667
1
  }
1668
2
}
1669
1670
void JSONNodeDumper::visitVerbatimBlockComment(
1671
1
    const comments::VerbatimBlockComment *C, const comments::FullComment *) {
1672
1
  JOS.attribute("name", getCommentCommandName(C->getCommandID()));
1673
1
  JOS.attribute("closeName", C->getCloseName());
1674
1
}
1675
1676
void JSONNodeDumper::visitVerbatimBlockLineComment(
1677
    const comments::VerbatimBlockLineComment *C,
1678
1
    const comments::FullComment *) {
1679
1
  JOS.attribute("text", C->getText());
1680
1
}
1681
1682
void JSONNodeDumper::visitVerbatimLineComment(
1683
0
    const comments::VerbatimLineComment *C, const comments::FullComment *) {
1684
0
  JOS.attribute("text", C->getText());
1685
0
}