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 llvm::json::Object JSONNodeDumper::createBareDeclRef(const Decl *D) { 226 llvm::json::Object Ret{{"id", createPointerRepresentation(D)}}; 227 if (!D) 228 return Ret; 229 230 Ret["kind"] = (llvm::Twine(D->getDeclKindName()) + "Decl").str(); 231 if (const auto *ND = dyn_cast<NamedDecl>(D)) 232 Ret["name"] = ND->getDeclName().getAsString(); 233 if (const auto *VD = dyn_cast<ValueDecl>(D)) 234 Ret["type"] = createQualType(VD->getType()); 235 return Ret; 236 } 237 238 llvm::json::Array JSONNodeDumper::createCastPath(const CastExpr *C) { 239 llvm::json::Array Ret; 240 if (C->path_empty()) 241 return Ret; 242 243 for (auto I = C->path_begin(), E = C->path_end(); I != E; ++I) { 244 const CXXBaseSpecifier *Base = *I; 245 const auto *RD = 246 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 247 248 llvm::json::Object Val{{"name", RD->getName()}}; 249 if (Base->isVirtual()) 250 Val["isVirtual"] = true; 251 Ret.push_back(std::move(Val)); 252 } 253 return Ret; 254 } 255 256 #define FIELD2(Name, Flag) if (RD->Flag()) Ret[Name] = true 257 #define FIELD1(Flag) FIELD2(#Flag, Flag) 258 259 static llvm::json::Object 260 createDefaultConstructorDefinitionData(const CXXRecordDecl *RD) { 261 llvm::json::Object Ret; 262 263 FIELD2("exists", hasDefaultConstructor); 264 FIELD2("trivial", hasTrivialDefaultConstructor); 265 FIELD2("nonTrivial", hasNonTrivialDefaultConstructor); 266 FIELD2("userProvided", hasUserProvidedDefaultConstructor); 267 FIELD2("isConstexpr", hasConstexprDefaultConstructor); 268 FIELD2("needsImplicit", needsImplicitDefaultConstructor); 269 FIELD2("defaultedIsConstexpr", defaultedDefaultConstructorIsConstexpr); 270 271 return Ret; 272 } 273 274 static llvm::json::Object 275 createCopyConstructorDefinitionData(const CXXRecordDecl *RD) { 276 llvm::json::Object Ret; 277 278 FIELD2("simple", hasSimpleCopyConstructor); 279 FIELD2("trivial", hasTrivialCopyConstructor); 280 FIELD2("nonTrivial", hasNonTrivialCopyConstructor); 281 FIELD2("userDeclared", hasUserDeclaredCopyConstructor); 282 FIELD2("hasConstParam", hasCopyConstructorWithConstParam); 283 FIELD2("implicitHasConstParam", implicitCopyConstructorHasConstParam); 284 FIELD2("needsImplicit", needsImplicitCopyConstructor); 285 FIELD2("needsOverloadResolution", needsOverloadResolutionForCopyConstructor); 286 if (!RD->needsOverloadResolutionForCopyConstructor()) 287 FIELD2("defaultedIsDeleted", defaultedCopyConstructorIsDeleted); 288 289 return Ret; 290 } 291 292 static llvm::json::Object 293 createMoveConstructorDefinitionData(const CXXRecordDecl *RD) { 294 llvm::json::Object Ret; 295 296 FIELD2("exists", hasMoveConstructor); 297 FIELD2("simple", hasSimpleMoveConstructor); 298 FIELD2("trivial", hasTrivialMoveConstructor); 299 FIELD2("nonTrivial", hasNonTrivialMoveConstructor); 300 FIELD2("userDeclared", hasUserDeclaredMoveConstructor); 301 FIELD2("needsImplicit", needsImplicitMoveConstructor); 302 FIELD2("needsOverloadResolution", needsOverloadResolutionForMoveConstructor); 303 if (!RD->needsOverloadResolutionForMoveConstructor()) 304 FIELD2("defaultedIsDeleted", defaultedMoveConstructorIsDeleted); 305 306 return Ret; 307 } 308 309 static llvm::json::Object 310 createCopyAssignmentDefinitionData(const CXXRecordDecl *RD) { 311 llvm::json::Object Ret; 312 313 FIELD2("trivial", hasTrivialCopyAssignment); 314 FIELD2("nonTrivial", hasNonTrivialCopyAssignment); 315 FIELD2("hasConstParam", hasCopyAssignmentWithConstParam); 316 FIELD2("implicitHasConstParam", implicitCopyAssignmentHasConstParam); 317 FIELD2("userDeclared", hasUserDeclaredCopyAssignment); 318 FIELD2("needsImplicit", needsImplicitCopyAssignment); 319 FIELD2("needsOverloadResolution", needsOverloadResolutionForCopyAssignment); 320 321 return Ret; 322 } 323 324 static llvm::json::Object 325 createMoveAssignmentDefinitionData(const CXXRecordDecl *RD) { 326 llvm::json::Object Ret; 327 328 FIELD2("exists", hasMoveAssignment); 329 FIELD2("simple", hasSimpleMoveAssignment); 330 FIELD2("trivial", hasTrivialMoveAssignment); 331 FIELD2("nonTrivial", hasNonTrivialMoveAssignment); 332 FIELD2("userDeclared", hasUserDeclaredMoveAssignment); 333 FIELD2("needsImplicit", needsImplicitMoveAssignment); 334 FIELD2("needsOverloadResolution", needsOverloadResolutionForMoveAssignment); 335 336 return Ret; 337 } 338 339 static llvm::json::Object 340 createDestructorDefinitionData(const CXXRecordDecl *RD) { 341 llvm::json::Object Ret; 342 343 FIELD2("simple", hasSimpleDestructor); 344 FIELD2("irrelevant", hasIrrelevantDestructor); 345 FIELD2("trivial", hasTrivialDestructor); 346 FIELD2("nonTrivial", hasNonTrivialDestructor); 347 FIELD2("userDeclared", hasUserDeclaredDestructor); 348 FIELD2("needsImplicit", needsImplicitDestructor); 349 FIELD2("needsOverloadResolution", needsOverloadResolutionForDestructor); 350 if (!RD->needsOverloadResolutionForDestructor()) 351 FIELD2("defaultedIsDeleted", defaultedDestructorIsDeleted); 352 353 return Ret; 354 } 355 356 llvm::json::Object 357 JSONNodeDumper::createCXXRecordDefinitionData(const CXXRecordDecl *RD) { 358 llvm::json::Object Ret; 359 360 // This data is common to all C++ classes. 361 FIELD1(isGenericLambda); 362 FIELD1(isLambda); 363 FIELD1(isEmpty); 364 FIELD1(isAggregate); 365 FIELD1(isStandardLayout); 366 FIELD1(isTriviallyCopyable); 367 FIELD1(isPOD); 368 FIELD1(isTrivial); 369 FIELD1(isPolymorphic); 370 FIELD1(isAbstract); 371 FIELD1(isLiteral); 372 FIELD1(canPassInRegisters); 373 FIELD1(hasUserDeclaredConstructor); 374 FIELD1(hasConstexprNonCopyMoveConstructor); 375 FIELD1(hasMutableFields); 376 FIELD1(hasVariantMembers); 377 FIELD2("canConstDefaultInit", allowConstDefaultInit); 378 379 Ret["defaultCtor"] = createDefaultConstructorDefinitionData(RD); 380 Ret["copyCtor"] = createCopyConstructorDefinitionData(RD); 381 Ret["moveCtor"] = createMoveConstructorDefinitionData(RD); 382 Ret["copyAssign"] = createCopyAssignmentDefinitionData(RD); 383 Ret["moveAssign"] = createMoveAssignmentDefinitionData(RD); 384 Ret["dtor"] = createDestructorDefinitionData(RD); 385 386 return Ret; 387 } 388 389 #undef FIELD1 390 #undef FIELD2 391 392 std::string JSONNodeDumper::createAccessSpecifier(AccessSpecifier AS) { 393 switch (AS) { 394 case AS_none: return "none"; 395 case AS_private: return "private"; 396 case AS_protected: return "protected"; 397 case AS_public: return "public"; 398 } 399 llvm_unreachable("Unknown access specifier"); 400 } 401 402 llvm::json::Object 403 JSONNodeDumper::createCXXBaseSpecifier(const CXXBaseSpecifier &BS) { 404 llvm::json::Object Ret; 405 406 Ret["type"] = createQualType(BS.getType()); 407 Ret["access"] = createAccessSpecifier(BS.getAccessSpecifier()); 408 Ret["writtenAccess"] = 409 createAccessSpecifier(BS.getAccessSpecifierAsWritten()); 410 if (BS.isVirtual()) 411 Ret["isVirtual"] = true; 412 if (BS.isPackExpansion()) 413 Ret["isPackExpansion"] = true; 414 415 return Ret; 416 } 417 418 void JSONNodeDumper::VisitTypedefType(const TypedefType *TT) { 419 JOS.attribute("decl", createBareDeclRef(TT->getDecl())); 420 } 421 422 void JSONNodeDumper::VisitFunctionType(const FunctionType *T) { 423 FunctionType::ExtInfo E = T->getExtInfo(); 424 attributeOnlyIfTrue("noreturn", E.getNoReturn()); 425 attributeOnlyIfTrue("producesResult", E.getProducesResult()); 426 if (E.getHasRegParm()) 427 JOS.attribute("regParm", E.getRegParm()); 428 JOS.attribute("cc", FunctionType::getNameForCallConv(E.getCC())); 429 } 430 431 void JSONNodeDumper::VisitFunctionProtoType(const FunctionProtoType *T) { 432 FunctionProtoType::ExtProtoInfo E = T->getExtProtoInfo(); 433 attributeOnlyIfTrue("trailingReturn", E.HasTrailingReturn); 434 attributeOnlyIfTrue("const", T->isConst()); 435 attributeOnlyIfTrue("volatile", T->isVolatile()); 436 attributeOnlyIfTrue("restrict", T->isRestrict()); 437 attributeOnlyIfTrue("variadic", E.Variadic); 438 switch (E.RefQualifier) { 439 case RQ_LValue: JOS.attribute("refQualifier", "&"); break; 440 case RQ_RValue: JOS.attribute("refQualifier", "&&"); break; 441 case RQ_None: break; 442 } 443 switch (E.ExceptionSpec.Type) { 444 case EST_DynamicNone: 445 case EST_Dynamic: { 446 JOS.attribute("exceptionSpec", "throw"); 447 llvm::json::Array Types; 448 for (QualType QT : E.ExceptionSpec.Exceptions) 449 Types.push_back(createQualType(QT)); 450 JOS.attribute("exceptionTypes", std::move(Types)); 451 } break; 452 case EST_MSAny: 453 JOS.attribute("exceptionSpec", "throw"); 454 JOS.attribute("throwsAny", true); 455 break; 456 case EST_BasicNoexcept: 457 JOS.attribute("exceptionSpec", "noexcept"); 458 break; 459 case EST_NoexceptTrue: 460 case EST_NoexceptFalse: 461 JOS.attribute("exceptionSpec", "noexcept"); 462 JOS.attribute("conditionEvaluatesTo", 463 E.ExceptionSpec.Type == EST_NoexceptTrue); 464 //JOS.attributeWithCall("exceptionSpecExpr", 465 // [this, E]() { Visit(E.ExceptionSpec.NoexceptExpr); }); 466 break; 467 468 // FIXME: I cannot find a way to trigger these cases while dumping the AST. I 469 // suspect you can only run into them when executing an AST dump from within 470 // the debugger, which is not a use case we worry about for the JSON dumping 471 // feature. 472 case EST_DependentNoexcept: 473 case EST_Unevaluated: 474 case EST_Uninstantiated: 475 case EST_Unparsed: 476 case EST_None: break; 477 } 478 VisitFunctionType(T); 479 } 480 481 void JSONNodeDumper::VisitNamedDecl(const NamedDecl *ND) { 482 if (ND && ND->getDeclName()) 483 JOS.attribute("name", ND->getNameAsString()); 484 } 485 486 void JSONNodeDumper::VisitTypedefDecl(const TypedefDecl *TD) { 487 VisitNamedDecl(TD); 488 JOS.attribute("type", createQualType(TD->getUnderlyingType())); 489 } 490 491 void JSONNodeDumper::VisitTypeAliasDecl(const TypeAliasDecl *TAD) { 492 VisitNamedDecl(TAD); 493 JOS.attribute("type", createQualType(TAD->getUnderlyingType())); 494 } 495 496 void JSONNodeDumper::VisitNamespaceDecl(const NamespaceDecl *ND) { 497 VisitNamedDecl(ND); 498 attributeOnlyIfTrue("isInline", ND->isInline()); 499 if (!ND->isOriginalNamespace()) 500 JOS.attribute("originalNamespace", 501 createBareDeclRef(ND->getOriginalNamespace())); 502 } 503 504 void JSONNodeDumper::VisitUsingDirectiveDecl(const UsingDirectiveDecl *UDD) { 505 JOS.attribute("nominatedNamespace", 506 createBareDeclRef(UDD->getNominatedNamespace())); 507 } 508 509 void JSONNodeDumper::VisitNamespaceAliasDecl(const NamespaceAliasDecl *NAD) { 510 VisitNamedDecl(NAD); 511 JOS.attribute("aliasedNamespace", 512 createBareDeclRef(NAD->getAliasedNamespace())); 513 } 514 515 void JSONNodeDumper::VisitUsingDecl(const UsingDecl *UD) { 516 std::string Name; 517 if (const NestedNameSpecifier *NNS = UD->getQualifier()) { 518 llvm::raw_string_ostream SOS(Name); 519 NNS->print(SOS, UD->getASTContext().getPrintingPolicy()); 520 } 521 Name += UD->getNameAsString(); 522 JOS.attribute("name", Name); 523 } 524 525 void JSONNodeDumper::VisitUsingShadowDecl(const UsingShadowDecl *USD) { 526 JOS.attribute("target", createBareDeclRef(USD->getTargetDecl())); 527 } 528 529 void JSONNodeDumper::VisitVarDecl(const VarDecl *VD) { 530 VisitNamedDecl(VD); 531 JOS.attribute("type", createQualType(VD->getType())); 532 533 StorageClass SC = VD->getStorageClass(); 534 if (SC != SC_None) 535 JOS.attribute("storageClass", VarDecl::getStorageClassSpecifierString(SC)); 536 switch (VD->getTLSKind()) { 537 case VarDecl::TLS_Dynamic: JOS.attribute("tls", "dynamic"); break; 538 case VarDecl::TLS_Static: JOS.attribute("tls", "static"); break; 539 case VarDecl::TLS_None: break; 540 } 541 attributeOnlyIfTrue("nrvo", VD->isNRVOVariable()); 542 attributeOnlyIfTrue("inline", VD->isInline()); 543 attributeOnlyIfTrue("constexpr", VD->isConstexpr()); 544 attributeOnlyIfTrue("modulePrivate", VD->isModulePrivate()); 545 if (VD->hasInit()) { 546 switch (VD->getInitStyle()) { 547 case VarDecl::CInit: JOS.attribute("init", "c"); break; 548 case VarDecl::CallInit: JOS.attribute("init", "call"); break; 549 case VarDecl::ListInit: JOS.attribute("init", "list"); break; 550 } 551 } 552 attributeOnlyIfTrue("isParameterPack", VD->isParameterPack()); 553 } 554 555 void JSONNodeDumper::VisitFieldDecl(const FieldDecl *FD) { 556 VisitNamedDecl(FD); 557 JOS.attribute("type", createQualType(FD->getType())); 558 attributeOnlyIfTrue("mutable", FD->isMutable()); 559 attributeOnlyIfTrue("modulePrivate", FD->isModulePrivate()); 560 attributeOnlyIfTrue("isBitfield", FD->isBitField()); 561 attributeOnlyIfTrue("hasInClassInitializer", FD->hasInClassInitializer()); 562 } 563 564 void JSONNodeDumper::VisitFunctionDecl(const FunctionDecl *FD) { 565 VisitNamedDecl(FD); 566 JOS.attribute("type", createQualType(FD->getType())); 567 StorageClass SC = FD->getStorageClass(); 568 if (SC != SC_None) 569 JOS.attribute("storageClass", VarDecl::getStorageClassSpecifierString(SC)); 570 attributeOnlyIfTrue("inline", FD->isInlineSpecified()); 571 attributeOnlyIfTrue("virtual", FD->isVirtualAsWritten()); 572 attributeOnlyIfTrue("pure", FD->isPure()); 573 attributeOnlyIfTrue("explicitlyDeleted", FD->isDeletedAsWritten()); 574 attributeOnlyIfTrue("constexpr", FD->isConstexpr()); 575 if (FD->isDefaulted()) 576 JOS.attribute("explicitlyDefaulted", 577 FD->isDeleted() ? "deleted" : "default"); 578 } 579 580 void JSONNodeDumper::VisitEnumDecl(const EnumDecl *ED) { 581 VisitNamedDecl(ED); 582 if (ED->isFixed()) 583 JOS.attribute("fixedUnderlyingType", createQualType(ED->getIntegerType())); 584 if (ED->isScoped()) 585 JOS.attribute("scopedEnumTag", 586 ED->isScopedUsingClassTag() ? "class" : "struct"); 587 } 588 void JSONNodeDumper::VisitEnumConstantDecl(const EnumConstantDecl *ECD) { 589 VisitNamedDecl(ECD); 590 JOS.attribute("type", createQualType(ECD->getType())); 591 } 592 593 void JSONNodeDumper::VisitRecordDecl(const RecordDecl *RD) { 594 VisitNamedDecl(RD); 595 JOS.attribute("tagUsed", RD->getKindName()); 596 attributeOnlyIfTrue("completeDefinition", RD->isCompleteDefinition()); 597 } 598 void JSONNodeDumper::VisitCXXRecordDecl(const CXXRecordDecl *RD) { 599 VisitRecordDecl(RD); 600 601 // All other information requires a complete definition. 602 if (!RD->isCompleteDefinition()) 603 return; 604 605 JOS.attribute("definitionData", createCXXRecordDefinitionData(RD)); 606 if (RD->getNumBases()) { 607 JOS.attributeArray("bases", [this, RD] { 608 for (const auto &Spec : RD->bases()) 609 JOS.value(createCXXBaseSpecifier(Spec)); 610 }); 611 } 612 } 613 614 void JSONNodeDumper::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) { 615 VisitNamedDecl(D); 616 JOS.attribute("tagUsed", D->wasDeclaredWithTypename() ? "typename" : "class"); 617 JOS.attribute("depth", D->getDepth()); 618 JOS.attribute("index", D->getIndex()); 619 attributeOnlyIfTrue("isParameterPack", D->isParameterPack()); 620 } 621 622 void JSONNodeDumper::VisitNonTypeTemplateParmDecl( 623 const NonTypeTemplateParmDecl *D) { 624 VisitNamedDecl(D); 625 JOS.attribute("type", createQualType(D->getType())); 626 JOS.attribute("depth", D->getDepth()); 627 JOS.attribute("index", D->getIndex()); 628 attributeOnlyIfTrue("isParameterPack", D->isParameterPack()); 629 } 630 631 void JSONNodeDumper::VisitTemplateTemplateParmDecl( 632 const TemplateTemplateParmDecl *D) { 633 VisitNamedDecl(D); 634 JOS.attribute("depth", D->getDepth()); 635 JOS.attribute("index", D->getIndex()); 636 attributeOnlyIfTrue("isParameterPack", D->isParameterPack()); 637 } 638 639 void JSONNodeDumper::VisitLinkageSpecDecl(const LinkageSpecDecl *LSD) { 640 StringRef Lang; 641 switch (LSD->getLanguage()) { 642 case LinkageSpecDecl::lang_c: Lang = "C"; break; 643 case LinkageSpecDecl::lang_cxx: Lang = "C++"; break; 644 } 645 JOS.attribute("language", Lang); 646 attributeOnlyIfTrue("hasBraces", LSD->hasBraces()); 647 } 648 649 void JSONNodeDumper::VisitAccessSpecDecl(const AccessSpecDecl *ASD) { 650 JOS.attribute("access", createAccessSpecifier(ASD->getAccess())); 651 } 652 653 void JSONNodeDumper::VisitFriendDecl(const FriendDecl *FD) { 654 if (const TypeSourceInfo *T = FD->getFriendType()) 655 JOS.attribute("type", createQualType(T->getType())); 656 } 657 658 void JSONNodeDumper::VisitObjCIvarDecl(const ObjCIvarDecl *D) { 659 VisitNamedDecl(D); 660 JOS.attribute("type", createQualType(D->getType())); 661 attributeOnlyIfTrue("synthesized", D->getSynthesize()); 662 switch (D->getAccessControl()) { 663 case ObjCIvarDecl::None: JOS.attribute("access", "none"); break; 664 case ObjCIvarDecl::Private: JOS.attribute("access", "private"); break; 665 case ObjCIvarDecl::Protected: JOS.attribute("access", "protected"); break; 666 case ObjCIvarDecl::Public: JOS.attribute("access", "public"); break; 667 case ObjCIvarDecl::Package: JOS.attribute("access", "package"); break; 668 } 669 } 670 671 void JSONNodeDumper::VisitObjCMethodDecl(const ObjCMethodDecl *D) { 672 VisitNamedDecl(D); 673 JOS.attribute("returnType", createQualType(D->getReturnType())); 674 JOS.attribute("instance", D->isInstanceMethod()); 675 attributeOnlyIfTrue("variadic", D->isVariadic()); 676 } 677 678 void JSONNodeDumper::VisitObjCTypeParamDecl(const ObjCTypeParamDecl *D) { 679 VisitNamedDecl(D); 680 JOS.attribute("type", createQualType(D->getUnderlyingType())); 681 attributeOnlyIfTrue("bounded", D->hasExplicitBound()); 682 switch (D->getVariance()) { 683 case ObjCTypeParamVariance::Invariant: 684 break; 685 case ObjCTypeParamVariance::Covariant: 686 JOS.attribute("variance", "covariant"); 687 break; 688 case ObjCTypeParamVariance::Contravariant: 689 JOS.attribute("variance", "contravariant"); 690 break; 691 } 692 } 693 694 void JSONNodeDumper::VisitObjCCategoryDecl(const ObjCCategoryDecl *D) { 695 VisitNamedDecl(D); 696 JOS.attribute("interface", createBareDeclRef(D->getClassInterface())); 697 JOS.attribute("implementation", createBareDeclRef(D->getImplementation())); 698 699 llvm::json::Array Protocols; 700 for (const auto* P : D->protocols()) 701 Protocols.push_back(createBareDeclRef(P)); 702 if (!Protocols.empty()) 703 JOS.attribute("protocols", std::move(Protocols)); 704 } 705 706 void JSONNodeDumper::VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) { 707 VisitNamedDecl(D); 708 JOS.attribute("interface", createBareDeclRef(D->getClassInterface())); 709 JOS.attribute("categoryDecl", createBareDeclRef(D->getCategoryDecl())); 710 } 711 712 void JSONNodeDumper::VisitObjCProtocolDecl(const ObjCProtocolDecl *D) { 713 VisitNamedDecl(D); 714 715 llvm::json::Array Protocols; 716 for (const auto *P : D->protocols()) 717 Protocols.push_back(createBareDeclRef(P)); 718 if (!Protocols.empty()) 719 JOS.attribute("protocols", std::move(Protocols)); 720 } 721 722 void JSONNodeDumper::VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) { 723 VisitNamedDecl(D); 724 JOS.attribute("super", createBareDeclRef(D->getSuperClass())); 725 JOS.attribute("implementation", createBareDeclRef(D->getImplementation())); 726 727 llvm::json::Array Protocols; 728 for (const auto* P : D->protocols()) 729 Protocols.push_back(createBareDeclRef(P)); 730 if (!Protocols.empty()) 731 JOS.attribute("protocols", std::move(Protocols)); 732 } 733 734 void JSONNodeDumper::VisitObjCImplementationDecl( 735 const ObjCImplementationDecl *D) { 736 VisitNamedDecl(D); 737 JOS.attribute("super", createBareDeclRef(D->getSuperClass())); 738 JOS.attribute("interface", createBareDeclRef(D->getClassInterface())); 739 } 740 741 void JSONNodeDumper::VisitObjCCompatibleAliasDecl( 742 const ObjCCompatibleAliasDecl *D) { 743 VisitNamedDecl(D); 744 JOS.attribute("interface", createBareDeclRef(D->getClassInterface())); 745 } 746 747 void JSONNodeDumper::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) { 748 VisitNamedDecl(D); 749 JOS.attribute("type", createQualType(D->getType())); 750 751 switch (D->getPropertyImplementation()) { 752 case ObjCPropertyDecl::None: break; 753 case ObjCPropertyDecl::Required: JOS.attribute("control", "required"); break; 754 case ObjCPropertyDecl::Optional: JOS.attribute("control", "optional"); break; 755 } 756 757 ObjCPropertyDecl::PropertyAttributeKind Attrs = D->getPropertyAttributes(); 758 if (Attrs != ObjCPropertyDecl::OBJC_PR_noattr) { 759 if (Attrs & ObjCPropertyDecl::OBJC_PR_getter) 760 JOS.attribute("getter", createBareDeclRef(D->getGetterMethodDecl())); 761 if (Attrs & ObjCPropertyDecl::OBJC_PR_setter) 762 JOS.attribute("setter", createBareDeclRef(D->getSetterMethodDecl())); 763 attributeOnlyIfTrue("readonly", Attrs & ObjCPropertyDecl::OBJC_PR_readonly); 764 attributeOnlyIfTrue("assign", Attrs & ObjCPropertyDecl::OBJC_PR_assign); 765 attributeOnlyIfTrue("readwrite", 766 Attrs & ObjCPropertyDecl::OBJC_PR_readwrite); 767 attributeOnlyIfTrue("retain", Attrs & ObjCPropertyDecl::OBJC_PR_retain); 768 attributeOnlyIfTrue("copy", Attrs & ObjCPropertyDecl::OBJC_PR_copy); 769 attributeOnlyIfTrue("nonatomic", 770 Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic); 771 attributeOnlyIfTrue("atomic", Attrs & ObjCPropertyDecl::OBJC_PR_atomic); 772 attributeOnlyIfTrue("weak", Attrs & ObjCPropertyDecl::OBJC_PR_weak); 773 attributeOnlyIfTrue("strong", Attrs & ObjCPropertyDecl::OBJC_PR_strong); 774 attributeOnlyIfTrue("unsafe_unretained", 775 Attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained); 776 attributeOnlyIfTrue("class", Attrs & ObjCPropertyDecl::OBJC_PR_class); 777 attributeOnlyIfTrue("nullability", 778 Attrs & ObjCPropertyDecl::OBJC_PR_nullability); 779 attributeOnlyIfTrue("null_resettable", 780 Attrs & ObjCPropertyDecl::OBJC_PR_null_resettable); 781 } 782 } 783 784 void JSONNodeDumper::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) { 785 VisitNamedDecl(D->getPropertyDecl()); 786 JOS.attribute("implKind", D->getPropertyImplementation() == 787 ObjCPropertyImplDecl::Synthesize 788 ? "synthesize" 789 : "dynamic"); 790 JOS.attribute("propertyDecl", createBareDeclRef(D->getPropertyDecl())); 791 JOS.attribute("ivarDecl", createBareDeclRef(D->getPropertyIvarDecl())); 792 } 793 794 void JSONNodeDumper::VisitBlockDecl(const BlockDecl *D) { 795 attributeOnlyIfTrue("variadic", D->isVariadic()); 796 attributeOnlyIfTrue("capturesThis", D->capturesCXXThis()); 797 } 798 799 void JSONNodeDumper::VisitDeclRefExpr(const DeclRefExpr *DRE) { 800 JOS.attribute("referencedDecl", createBareDeclRef(DRE->getDecl())); 801 if (DRE->getDecl() != DRE->getFoundDecl()) 802 JOS.attribute("foundReferencedDecl", 803 createBareDeclRef(DRE->getFoundDecl())); 804 } 805 806 void JSONNodeDumper::VisitPredefinedExpr(const PredefinedExpr *PE) { 807 JOS.attribute("name", PredefinedExpr::getIdentKindName(PE->getIdentKind())); 808 } 809 810 void JSONNodeDumper::VisitUnaryOperator(const UnaryOperator *UO) { 811 JOS.attribute("isPostfix", UO->isPostfix()); 812 JOS.attribute("opcode", UnaryOperator::getOpcodeStr(UO->getOpcode())); 813 if (!UO->canOverflow()) 814 JOS.attribute("canOverflow", false); 815 } 816 817 void JSONNodeDumper::VisitBinaryOperator(const BinaryOperator *BO) { 818 JOS.attribute("opcode", BinaryOperator::getOpcodeStr(BO->getOpcode())); 819 } 820 821 void JSONNodeDumper::VisitCompoundAssignOperator( 822 const CompoundAssignOperator *CAO) { 823 VisitBinaryOperator(CAO); 824 JOS.attribute("computeLHSType", createQualType(CAO->getComputationLHSType())); 825 JOS.attribute("computeResultType", 826 createQualType(CAO->getComputationResultType())); 827 } 828 829 void JSONNodeDumper::VisitMemberExpr(const MemberExpr *ME) { 830 // Note, we always write this Boolean field because the information it conveys 831 // is critical to understanding the AST node. 832 JOS.attribute("isArrow", ME->isArrow()); 833 JOS.attribute("referencedMemberDecl", 834 createPointerRepresentation(ME->getMemberDecl())); 835 } 836 837 void JSONNodeDumper::VisitCXXNewExpr(const CXXNewExpr *NE) { 838 attributeOnlyIfTrue("isGlobal", NE->isGlobalNew()); 839 attributeOnlyIfTrue("isArray", NE->isArray()); 840 attributeOnlyIfTrue("isPlacement", NE->getNumPlacementArgs() != 0); 841 switch (NE->getInitializationStyle()) { 842 case CXXNewExpr::NoInit: break; 843 case CXXNewExpr::CallInit: JOS.attribute("initStyle", "call"); break; 844 case CXXNewExpr::ListInit: JOS.attribute("initStyle", "list"); break; 845 } 846 if (const FunctionDecl *FD = NE->getOperatorNew()) 847 JOS.attribute("operatorNewDecl", createBareDeclRef(FD)); 848 if (const FunctionDecl *FD = NE->getOperatorDelete()) 849 JOS.attribute("operatorDeleteDecl", createBareDeclRef(FD)); 850 } 851 void JSONNodeDumper::VisitCXXDeleteExpr(const CXXDeleteExpr *DE) { 852 attributeOnlyIfTrue("isGlobal", DE->isGlobalDelete()); 853 attributeOnlyIfTrue("isArray", DE->isArrayForm()); 854 attributeOnlyIfTrue("isArrayAsWritten", DE->isArrayFormAsWritten()); 855 if (const FunctionDecl *FD = DE->getOperatorDelete()) 856 JOS.attribute("operatorDeleteDecl", createBareDeclRef(FD)); 857 } 858 859 void JSONNodeDumper::VisitCXXThisExpr(const CXXThisExpr *TE) { 860 attributeOnlyIfTrue("implicit", TE->isImplicit()); 861 } 862 863 void JSONNodeDumper::VisitCastExpr(const CastExpr *CE) { 864 JOS.attribute("castKind", CE->getCastKindName()); 865 llvm::json::Array Path = createCastPath(CE); 866 if (!Path.empty()) 867 JOS.attribute("path", std::move(Path)); 868 // FIXME: This may not be useful information as it can be obtusely gleaned 869 // from the inner[] array. 870 if (const NamedDecl *ND = CE->getConversionFunction()) 871 JOS.attribute("conversionFunc", createBareDeclRef(ND)); 872 } 873 874 void JSONNodeDumper::VisitImplicitCastExpr(const ImplicitCastExpr *ICE) { 875 VisitCastExpr(ICE); 876 attributeOnlyIfTrue("isPartOfExplicitCast", ICE->isPartOfExplicitCast()); 877 } 878 879 void JSONNodeDumper::VisitCallExpr(const CallExpr *CE) { 880 attributeOnlyIfTrue("adl", CE->usesADL()); 881 } 882 883 void JSONNodeDumper::VisitUnaryExprOrTypeTraitExpr( 884 const UnaryExprOrTypeTraitExpr *TTE) { 885 switch (TTE->getKind()) { 886 case UETT_SizeOf: JOS.attribute("name", "sizeof"); break; 887 case UETT_AlignOf: JOS.attribute("name", "alignof"); break; 888 case UETT_VecStep: JOS.attribute("name", "vec_step"); break; 889 case UETT_PreferredAlignOf: JOS.attribute("name", "__alignof"); break; 890 case UETT_OpenMPRequiredSimdAlign: 891 JOS.attribute("name", "__builtin_omp_required_simd_align"); break; 892 } 893 if (TTE->isArgumentType()) 894 JOS.attribute("argType", createQualType(TTE->getArgumentType())); 895 } 896 897 void JSONNodeDumper::VisitUnresolvedLookupExpr( 898 const UnresolvedLookupExpr *ULE) { 899 JOS.attribute("usesADL", ULE->requiresADL()); 900 JOS.attribute("name", ULE->getName().getAsString()); 901 902 JOS.attributeArray("lookups", [this, ULE] { 903 for (const NamedDecl *D : ULE->decls()) 904 JOS.value(createBareDeclRef(D)); 905 }); 906 } 907 908 void JSONNodeDumper::VisitAddrLabelExpr(const AddrLabelExpr *ALE) { 909 JOS.attribute("name", ALE->getLabel()->getName()); 910 JOS.attribute("labelDeclId", createPointerRepresentation(ALE->getLabel())); 911 } 912 913 void JSONNodeDumper::VisitIntegerLiteral(const IntegerLiteral *IL) { 914 JOS.attribute("value", 915 IL->getValue().toString( 916 /*Radix=*/10, IL->getType()->isSignedIntegerType())); 917 } 918 void JSONNodeDumper::VisitCharacterLiteral(const CharacterLiteral *CL) { 919 // FIXME: This should probably print the character literal as a string, 920 // rather than as a numerical value. It would be nice if the behavior matched 921 // what we do to print a string literal; right now, it is impossible to tell 922 // the difference between 'a' and L'a' in C from the JSON output. 923 JOS.attribute("value", CL->getValue()); 924 } 925 void JSONNodeDumper::VisitFixedPointLiteral(const FixedPointLiteral *FPL) { 926 JOS.attribute("value", FPL->getValueAsString(/*Radix=*/10)); 927 } 928 void JSONNodeDumper::VisitFloatingLiteral(const FloatingLiteral *FL) { 929 JOS.attribute("value", FL->getValueAsApproximateDouble()); 930 } 931 void JSONNodeDumper::VisitStringLiteral(const StringLiteral *SL) { 932 std::string Buffer; 933 llvm::raw_string_ostream SS(Buffer); 934 SL->outputString(SS); 935 JOS.attribute("value", SS.str()); 936 } 937 void JSONNodeDumper::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *BLE) { 938 JOS.attribute("value", BLE->getValue()); 939 } 940 941 void JSONNodeDumper::VisitIfStmt(const IfStmt *IS) { 942 attributeOnlyIfTrue("hasInit", IS->hasInitStorage()); 943 attributeOnlyIfTrue("hasVar", IS->hasVarStorage()); 944 attributeOnlyIfTrue("hasElse", IS->hasElseStorage()); 945 attributeOnlyIfTrue("isConstexpr", IS->isConstexpr()); 946 } 947 948 void JSONNodeDumper::VisitSwitchStmt(const SwitchStmt *SS) { 949 attributeOnlyIfTrue("hasInit", SS->hasInitStorage()); 950 attributeOnlyIfTrue("hasVar", SS->hasVarStorage()); 951 } 952 void JSONNodeDumper::VisitCaseStmt(const CaseStmt *CS) { 953 attributeOnlyIfTrue("isGNURange", CS->caseStmtIsGNURange()); 954 } 955 956 void JSONNodeDumper::VisitLabelStmt(const LabelStmt *LS) { 957 JOS.attribute("name", LS->getName()); 958 JOS.attribute("declId", createPointerRepresentation(LS->getDecl())); 959 } 960 void JSONNodeDumper::VisitGotoStmt(const GotoStmt *GS) { 961 JOS.attribute("targetLabelDeclId", 962 createPointerRepresentation(GS->getLabel())); 963 } 964 965 void JSONNodeDumper::VisitWhileStmt(const WhileStmt *WS) { 966 attributeOnlyIfTrue("hasVar", WS->hasVarStorage()); 967 } 968 969 void JSONNodeDumper::VisitObjCAtCatchStmt(const ObjCAtCatchStmt* OACS) { 970 // FIXME: it would be nice for the ASTNodeTraverser would handle the catch 971 // parameter the same way for C++ and ObjC rather. In this case, C++ gets a 972 // null child node and ObjC gets no child node. 973 attributeOnlyIfTrue("isCatchAll", OACS->getCatchParamDecl() == nullptr); 974 } 975 976 StringRef JSONNodeDumper::getCommentCommandName(unsigned CommandID) const { 977 if (Traits) 978 return Traits->getCommandInfo(CommandID)->Name; 979 if (const comments::CommandInfo *Info = 980 comments::CommandTraits::getBuiltinCommandInfo(CommandID)) 981 return Info->Name; 982 return "<invalid>"; 983 } 984 985 void JSONNodeDumper::visitTextComment(const comments::TextComment *C, 986 const comments::FullComment *) { 987 JOS.attribute("text", C->getText()); 988 } 989 990 void JSONNodeDumper::visitInlineCommandComment( 991 const comments::InlineCommandComment *C, const comments::FullComment *) { 992 JOS.attribute("name", getCommentCommandName(C->getCommandID())); 993 994 switch (C->getRenderKind()) { 995 case comments::InlineCommandComment::RenderNormal: 996 JOS.attribute("renderKind", "normal"); 997 break; 998 case comments::InlineCommandComment::RenderBold: 999 JOS.attribute("renderKind", "bold"); 1000 break; 1001 case comments::InlineCommandComment::RenderEmphasized: 1002 JOS.attribute("renderKind", "emphasized"); 1003 break; 1004 case comments::InlineCommandComment::RenderMonospaced: 1005 JOS.attribute("renderKind", "monospaced"); 1006 break; 1007 } 1008 1009 llvm::json::Array Args; 1010 for (unsigned I = 0, E = C->getNumArgs(); I < E; ++I) 1011 Args.push_back(C->getArgText(I)); 1012 1013 if (!Args.empty()) 1014 JOS.attribute("args", std::move(Args)); 1015 } 1016 1017 void JSONNodeDumper::visitHTMLStartTagComment( 1018 const comments::HTMLStartTagComment *C, const comments::FullComment *) { 1019 JOS.attribute("name", C->getTagName()); 1020 attributeOnlyIfTrue("selfClosing", C->isSelfClosing()); 1021 attributeOnlyIfTrue("malformed", C->isMalformed()); 1022 1023 llvm::json::Array Attrs; 1024 for (unsigned I = 0, E = C->getNumAttrs(); I < E; ++I) 1025 Attrs.push_back( 1026 {{"name", C->getAttr(I).Name}, {"value", C->getAttr(I).Value}}); 1027 1028 if (!Attrs.empty()) 1029 JOS.attribute("attrs", std::move(Attrs)); 1030 } 1031 1032 void JSONNodeDumper::visitHTMLEndTagComment( 1033 const comments::HTMLEndTagComment *C, const comments::FullComment *) { 1034 JOS.attribute("name", C->getTagName()); 1035 } 1036 1037 void JSONNodeDumper::visitBlockCommandComment( 1038 const comments::BlockCommandComment *C, const comments::FullComment *) { 1039 JOS.attribute("name", getCommentCommandName(C->getCommandID())); 1040 1041 llvm::json::Array Args; 1042 for (unsigned I = 0, E = C->getNumArgs(); I < E; ++I) 1043 Args.push_back(C->getArgText(I)); 1044 1045 if (!Args.empty()) 1046 JOS.attribute("args", std::move(Args)); 1047 } 1048 1049 void JSONNodeDumper::visitParamCommandComment( 1050 const comments::ParamCommandComment *C, const comments::FullComment *FC) { 1051 switch (C->getDirection()) { 1052 case comments::ParamCommandComment::In: 1053 JOS.attribute("direction", "in"); 1054 break; 1055 case comments::ParamCommandComment::Out: 1056 JOS.attribute("direction", "out"); 1057 break; 1058 case comments::ParamCommandComment::InOut: 1059 JOS.attribute("direction", "in,out"); 1060 break; 1061 } 1062 attributeOnlyIfTrue("explicit", C->isDirectionExplicit()); 1063 1064 if (C->hasParamName()) 1065 JOS.attribute("param", C->isParamIndexValid() ? C->getParamName(FC) 1066 : C->getParamNameAsWritten()); 1067 1068 if (C->isParamIndexValid() && !C->isVarArgParam()) 1069 JOS.attribute("paramIdx", C->getParamIndex()); 1070 } 1071 1072 void JSONNodeDumper::visitTParamCommandComment( 1073 const comments::TParamCommandComment *C, const comments::FullComment *FC) { 1074 if (C->hasParamName()) 1075 JOS.attribute("param", C->isPositionValid() ? C->getParamName(FC) 1076 : C->getParamNameAsWritten()); 1077 if (C->isPositionValid()) { 1078 llvm::json::Array Positions; 1079 for (unsigned I = 0, E = C->getDepth(); I < E; ++I) 1080 Positions.push_back(C->getIndex(I)); 1081 1082 if (!Positions.empty()) 1083 JOS.attribute("positions", std::move(Positions)); 1084 } 1085 } 1086 1087 void JSONNodeDumper::visitVerbatimBlockComment( 1088 const comments::VerbatimBlockComment *C, const comments::FullComment *) { 1089 JOS.attribute("name", getCommentCommandName(C->getCommandID())); 1090 JOS.attribute("closeName", C->getCloseName()); 1091 } 1092 1093 void JSONNodeDumper::visitVerbatimBlockLineComment( 1094 const comments::VerbatimBlockLineComment *C, 1095 const comments::FullComment *) { 1096 JOS.attribute("text", C->getText()); 1097 } 1098 1099 void JSONNodeDumper::visitVerbatimLineComment( 1100 const comments::VerbatimLineComment *C, const comments::FullComment *) { 1101 JOS.attribute("text", C->getText()); 1102 } 1103