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