Coverage Report

Created: 2020-02-25 14:32

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