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