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