Coverage Report

Created: 2020-09-15 12:33

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