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