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 ObjCPropertyAttribute::Kind Attrs = D->getPropertyAttributes(); 1003 if (Attrs != ObjCPropertyAttribute::kind_noattr) { 1004 if (Attrs & ObjCPropertyAttribute::kind_getter) 1005 JOS.attribute("getter", createBareDeclRef(D->getGetterMethodDecl())); 1006 if (Attrs & ObjCPropertyAttribute::kind_setter) 1007 JOS.attribute("setter", createBareDeclRef(D->getSetterMethodDecl())); 1008 attributeOnlyIfTrue("readonly", 1009 Attrs & ObjCPropertyAttribute::kind_readonly); 1010 attributeOnlyIfTrue("assign", Attrs & ObjCPropertyAttribute::kind_assign); 1011 attributeOnlyIfTrue("readwrite", 1012 Attrs & ObjCPropertyAttribute::kind_readwrite); 1013 attributeOnlyIfTrue("retain", Attrs & ObjCPropertyAttribute::kind_retain); 1014 attributeOnlyIfTrue("copy", Attrs & ObjCPropertyAttribute::kind_copy); 1015 attributeOnlyIfTrue("nonatomic", 1016 Attrs & ObjCPropertyAttribute::kind_nonatomic); 1017 attributeOnlyIfTrue("atomic", Attrs & ObjCPropertyAttribute::kind_atomic); 1018 attributeOnlyIfTrue("weak", Attrs & ObjCPropertyAttribute::kind_weak); 1019 attributeOnlyIfTrue("strong", Attrs & ObjCPropertyAttribute::kind_strong); 1020 attributeOnlyIfTrue("unsafe_unretained", 1021 Attrs & ObjCPropertyAttribute::kind_unsafe_unretained); 1022 attributeOnlyIfTrue("class", Attrs & ObjCPropertyAttribute::kind_class); 1023 attributeOnlyIfTrue("direct", Attrs & ObjCPropertyAttribute::kind_direct); 1024 attributeOnlyIfTrue("nullability", 1025 Attrs & ObjCPropertyAttribute::kind_nullability); 1026 attributeOnlyIfTrue("null_resettable", 1027 Attrs & ObjCPropertyAttribute::kind_null_resettable); 1028 } 1029 } 1030 1031 void JSONNodeDumper::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) { 1032 VisitNamedDecl(D->getPropertyDecl()); 1033 JOS.attribute("implKind", D->getPropertyImplementation() == 1034 ObjCPropertyImplDecl::Synthesize 1035 ? "synthesize" 1036 : "dynamic"); 1037 JOS.attribute("propertyDecl", createBareDeclRef(D->getPropertyDecl())); 1038 JOS.attribute("ivarDecl", createBareDeclRef(D->getPropertyIvarDecl())); 1039 } 1040 1041 void JSONNodeDumper::VisitBlockDecl(const BlockDecl *D) { 1042 attributeOnlyIfTrue("variadic", D->isVariadic()); 1043 attributeOnlyIfTrue("capturesThis", D->capturesCXXThis()); 1044 } 1045 1046 void JSONNodeDumper::VisitObjCEncodeExpr(const ObjCEncodeExpr *OEE) { 1047 JOS.attribute("encodedType", createQualType(OEE->getEncodedType())); 1048 } 1049 1050 void JSONNodeDumper::VisitObjCMessageExpr(const ObjCMessageExpr *OME) { 1051 std::string Str; 1052 llvm::raw_string_ostream OS(Str); 1053 1054 OME->getSelector().print(OS); 1055 JOS.attribute("selector", OS.str()); 1056 1057 switch (OME->getReceiverKind()) { 1058 case ObjCMessageExpr::Instance: 1059 JOS.attribute("receiverKind", "instance"); 1060 break; 1061 case ObjCMessageExpr::Class: 1062 JOS.attribute("receiverKind", "class"); 1063 JOS.attribute("classType", createQualType(OME->getClassReceiver())); 1064 break; 1065 case ObjCMessageExpr::SuperInstance: 1066 JOS.attribute("receiverKind", "super (instance)"); 1067 JOS.attribute("superType", createQualType(OME->getSuperType())); 1068 break; 1069 case ObjCMessageExpr::SuperClass: 1070 JOS.attribute("receiverKind", "super (class)"); 1071 JOS.attribute("superType", createQualType(OME->getSuperType())); 1072 break; 1073 } 1074 1075 QualType CallReturnTy = OME->getCallReturnType(Ctx); 1076 if (OME->getType() != CallReturnTy) 1077 JOS.attribute("callReturnType", createQualType(CallReturnTy)); 1078 } 1079 1080 void JSONNodeDumper::VisitObjCBoxedExpr(const ObjCBoxedExpr *OBE) { 1081 if (const ObjCMethodDecl *MD = OBE->getBoxingMethod()) { 1082 std::string Str; 1083 llvm::raw_string_ostream OS(Str); 1084 1085 MD->getSelector().print(OS); 1086 JOS.attribute("selector", OS.str()); 1087 } 1088 } 1089 1090 void JSONNodeDumper::VisitObjCSelectorExpr(const ObjCSelectorExpr *OSE) { 1091 std::string Str; 1092 llvm::raw_string_ostream OS(Str); 1093 1094 OSE->getSelector().print(OS); 1095 JOS.attribute("selector", OS.str()); 1096 } 1097 1098 void JSONNodeDumper::VisitObjCProtocolExpr(const ObjCProtocolExpr *OPE) { 1099 JOS.attribute("protocol", createBareDeclRef(OPE->getProtocol())); 1100 } 1101 1102 void JSONNodeDumper::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *OPRE) { 1103 if (OPRE->isImplicitProperty()) { 1104 JOS.attribute("propertyKind", "implicit"); 1105 if (const ObjCMethodDecl *MD = OPRE->getImplicitPropertyGetter()) 1106 JOS.attribute("getter", createBareDeclRef(MD)); 1107 if (const ObjCMethodDecl *MD = OPRE->getImplicitPropertySetter()) 1108 JOS.attribute("setter", createBareDeclRef(MD)); 1109 } else { 1110 JOS.attribute("propertyKind", "explicit"); 1111 JOS.attribute("property", createBareDeclRef(OPRE->getExplicitProperty())); 1112 } 1113 1114 attributeOnlyIfTrue("isSuperReceiver", OPRE->isSuperReceiver()); 1115 attributeOnlyIfTrue("isMessagingGetter", OPRE->isMessagingGetter()); 1116 attributeOnlyIfTrue("isMessagingSetter", OPRE->isMessagingSetter()); 1117 } 1118 1119 void JSONNodeDumper::VisitObjCSubscriptRefExpr( 1120 const ObjCSubscriptRefExpr *OSRE) { 1121 JOS.attribute("subscriptKind", 1122 OSRE->isArraySubscriptRefExpr() ? "array" : "dictionary"); 1123 1124 if (const ObjCMethodDecl *MD = OSRE->getAtIndexMethodDecl()) 1125 JOS.attribute("getter", createBareDeclRef(MD)); 1126 if (const ObjCMethodDecl *MD = OSRE->setAtIndexMethodDecl()) 1127 JOS.attribute("setter", createBareDeclRef(MD)); 1128 } 1129 1130 void JSONNodeDumper::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *OIRE) { 1131 JOS.attribute("decl", createBareDeclRef(OIRE->getDecl())); 1132 attributeOnlyIfTrue("isFreeIvar", OIRE->isFreeIvar()); 1133 JOS.attribute("isArrow", OIRE->isArrow()); 1134 } 1135 1136 void JSONNodeDumper::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *OBLE) { 1137 JOS.attribute("value", OBLE->getValue() ? "__objc_yes" : "__objc_no"); 1138 } 1139 1140 void JSONNodeDumper::VisitDeclRefExpr(const DeclRefExpr *DRE) { 1141 JOS.attribute("referencedDecl", createBareDeclRef(DRE->getDecl())); 1142 if (DRE->getDecl() != DRE->getFoundDecl()) 1143 JOS.attribute("foundReferencedDecl", 1144 createBareDeclRef(DRE->getFoundDecl())); 1145 switch (DRE->isNonOdrUse()) { 1146 case NOUR_None: break; 1147 case NOUR_Unevaluated: JOS.attribute("nonOdrUseReason", "unevaluated"); break; 1148 case NOUR_Constant: JOS.attribute("nonOdrUseReason", "constant"); break; 1149 case NOUR_Discarded: JOS.attribute("nonOdrUseReason", "discarded"); break; 1150 } 1151 } 1152 1153 void JSONNodeDumper::VisitPredefinedExpr(const PredefinedExpr *PE) { 1154 JOS.attribute("name", PredefinedExpr::getIdentKindName(PE->getIdentKind())); 1155 } 1156 1157 void JSONNodeDumper::VisitUnaryOperator(const UnaryOperator *UO) { 1158 JOS.attribute("isPostfix", UO->isPostfix()); 1159 JOS.attribute("opcode", UnaryOperator::getOpcodeStr(UO->getOpcode())); 1160 if (!UO->canOverflow()) 1161 JOS.attribute("canOverflow", false); 1162 } 1163 1164 void JSONNodeDumper::VisitBinaryOperator(const BinaryOperator *BO) { 1165 JOS.attribute("opcode", BinaryOperator::getOpcodeStr(BO->getOpcode())); 1166 } 1167 1168 void JSONNodeDumper::VisitCompoundAssignOperator( 1169 const CompoundAssignOperator *CAO) { 1170 VisitBinaryOperator(CAO); 1171 JOS.attribute("computeLHSType", createQualType(CAO->getComputationLHSType())); 1172 JOS.attribute("computeResultType", 1173 createQualType(CAO->getComputationResultType())); 1174 } 1175 1176 void JSONNodeDumper::VisitMemberExpr(const MemberExpr *ME) { 1177 // Note, we always write this Boolean field because the information it conveys 1178 // is critical to understanding the AST node. 1179 ValueDecl *VD = ME->getMemberDecl(); 1180 JOS.attribute("name", VD && VD->getDeclName() ? VD->getNameAsString() : ""); 1181 JOS.attribute("isArrow", ME->isArrow()); 1182 JOS.attribute("referencedMemberDecl", createPointerRepresentation(VD)); 1183 switch (ME->isNonOdrUse()) { 1184 case NOUR_None: break; 1185 case NOUR_Unevaluated: JOS.attribute("nonOdrUseReason", "unevaluated"); break; 1186 case NOUR_Constant: JOS.attribute("nonOdrUseReason", "constant"); break; 1187 case NOUR_Discarded: JOS.attribute("nonOdrUseReason", "discarded"); break; 1188 } 1189 } 1190 1191 void JSONNodeDumper::VisitCXXNewExpr(const CXXNewExpr *NE) { 1192 attributeOnlyIfTrue("isGlobal", NE->isGlobalNew()); 1193 attributeOnlyIfTrue("isArray", NE->isArray()); 1194 attributeOnlyIfTrue("isPlacement", NE->getNumPlacementArgs() != 0); 1195 switch (NE->getInitializationStyle()) { 1196 case CXXNewExpr::NoInit: break; 1197 case CXXNewExpr::CallInit: JOS.attribute("initStyle", "call"); break; 1198 case CXXNewExpr::ListInit: JOS.attribute("initStyle", "list"); break; 1199 } 1200 if (const FunctionDecl *FD = NE->getOperatorNew()) 1201 JOS.attribute("operatorNewDecl", createBareDeclRef(FD)); 1202 if (const FunctionDecl *FD = NE->getOperatorDelete()) 1203 JOS.attribute("operatorDeleteDecl", createBareDeclRef(FD)); 1204 } 1205 void JSONNodeDumper::VisitCXXDeleteExpr(const CXXDeleteExpr *DE) { 1206 attributeOnlyIfTrue("isGlobal", DE->isGlobalDelete()); 1207 attributeOnlyIfTrue("isArray", DE->isArrayForm()); 1208 attributeOnlyIfTrue("isArrayAsWritten", DE->isArrayFormAsWritten()); 1209 if (const FunctionDecl *FD = DE->getOperatorDelete()) 1210 JOS.attribute("operatorDeleteDecl", createBareDeclRef(FD)); 1211 } 1212 1213 void JSONNodeDumper::VisitCXXThisExpr(const CXXThisExpr *TE) { 1214 attributeOnlyIfTrue("implicit", TE->isImplicit()); 1215 } 1216 1217 void JSONNodeDumper::VisitCastExpr(const CastExpr *CE) { 1218 JOS.attribute("castKind", CE->getCastKindName()); 1219 llvm::json::Array Path = createCastPath(CE); 1220 if (!Path.empty()) 1221 JOS.attribute("path", std::move(Path)); 1222 // FIXME: This may not be useful information as it can be obtusely gleaned 1223 // from the inner[] array. 1224 if (const NamedDecl *ND = CE->getConversionFunction()) 1225 JOS.attribute("conversionFunc", createBareDeclRef(ND)); 1226 } 1227 1228 void JSONNodeDumper::VisitImplicitCastExpr(const ImplicitCastExpr *ICE) { 1229 VisitCastExpr(ICE); 1230 attributeOnlyIfTrue("isPartOfExplicitCast", ICE->isPartOfExplicitCast()); 1231 } 1232 1233 void JSONNodeDumper::VisitCallExpr(const CallExpr *CE) { 1234 attributeOnlyIfTrue("adl", CE->usesADL()); 1235 } 1236 1237 void JSONNodeDumper::VisitUnaryExprOrTypeTraitExpr( 1238 const UnaryExprOrTypeTraitExpr *TTE) { 1239 switch (TTE->getKind()) { 1240 case UETT_SizeOf: JOS.attribute("name", "sizeof"); break; 1241 case UETT_AlignOf: JOS.attribute("name", "alignof"); break; 1242 case UETT_VecStep: JOS.attribute("name", "vec_step"); break; 1243 case UETT_PreferredAlignOf: JOS.attribute("name", "__alignof"); break; 1244 case UETT_OpenMPRequiredSimdAlign: 1245 JOS.attribute("name", "__builtin_omp_required_simd_align"); break; 1246 } 1247 if (TTE->isArgumentType()) 1248 JOS.attribute("argType", createQualType(TTE->getArgumentType())); 1249 } 1250 1251 void JSONNodeDumper::VisitSizeOfPackExpr(const SizeOfPackExpr *SOPE) { 1252 VisitNamedDecl(SOPE->getPack()); 1253 } 1254 1255 void JSONNodeDumper::VisitUnresolvedLookupExpr( 1256 const UnresolvedLookupExpr *ULE) { 1257 JOS.attribute("usesADL", ULE->requiresADL()); 1258 JOS.attribute("name", ULE->getName().getAsString()); 1259 1260 JOS.attributeArray("lookups", [this, ULE] { 1261 for (const NamedDecl *D : ULE->decls()) 1262 JOS.value(createBareDeclRef(D)); 1263 }); 1264 } 1265 1266 void JSONNodeDumper::VisitAddrLabelExpr(const AddrLabelExpr *ALE) { 1267 JOS.attribute("name", ALE->getLabel()->getName()); 1268 JOS.attribute("labelDeclId", createPointerRepresentation(ALE->getLabel())); 1269 } 1270 1271 void JSONNodeDumper::VisitCXXTypeidExpr(const CXXTypeidExpr *CTE) { 1272 if (CTE->isTypeOperand()) { 1273 QualType Adjusted = CTE->getTypeOperand(Ctx); 1274 QualType Unadjusted = CTE->getTypeOperandSourceInfo()->getType(); 1275 JOS.attribute("typeArg", createQualType(Unadjusted)); 1276 if (Adjusted != Unadjusted) 1277 JOS.attribute("adjustedTypeArg", createQualType(Adjusted)); 1278 } 1279 } 1280 1281 void JSONNodeDumper::VisitConstantExpr(const ConstantExpr *CE) { 1282 if (CE->getResultAPValueKind() != APValue::None) { 1283 std::string Str; 1284 llvm::raw_string_ostream OS(Str); 1285 CE->getAPValueResult().printPretty(OS, Ctx, CE->getType()); 1286 JOS.attribute("value", OS.str()); 1287 } 1288 } 1289 1290 void JSONNodeDumper::VisitInitListExpr(const InitListExpr *ILE) { 1291 if (const FieldDecl *FD = ILE->getInitializedFieldInUnion()) 1292 JOS.attribute("field", createBareDeclRef(FD)); 1293 } 1294 1295 void JSONNodeDumper::VisitGenericSelectionExpr( 1296 const GenericSelectionExpr *GSE) { 1297 attributeOnlyIfTrue("resultDependent", GSE->isResultDependent()); 1298 } 1299 1300 void JSONNodeDumper::VisitCXXUnresolvedConstructExpr( 1301 const CXXUnresolvedConstructExpr *UCE) { 1302 if (UCE->getType() != UCE->getTypeAsWritten()) 1303 JOS.attribute("typeAsWritten", createQualType(UCE->getTypeAsWritten())); 1304 attributeOnlyIfTrue("list", UCE->isListInitialization()); 1305 } 1306 1307 void JSONNodeDumper::VisitCXXConstructExpr(const CXXConstructExpr *CE) { 1308 CXXConstructorDecl *Ctor = CE->getConstructor(); 1309 JOS.attribute("ctorType", createQualType(Ctor->getType())); 1310 attributeOnlyIfTrue("elidable", CE->isElidable()); 1311 attributeOnlyIfTrue("list", CE->isListInitialization()); 1312 attributeOnlyIfTrue("initializer_list", CE->isStdInitListInitialization()); 1313 attributeOnlyIfTrue("zeroing", CE->requiresZeroInitialization()); 1314 attributeOnlyIfTrue("hadMultipleCandidates", CE->hadMultipleCandidates()); 1315 1316 switch (CE->getConstructionKind()) { 1317 case CXXConstructExpr::CK_Complete: 1318 JOS.attribute("constructionKind", "complete"); 1319 break; 1320 case CXXConstructExpr::CK_Delegating: 1321 JOS.attribute("constructionKind", "delegating"); 1322 break; 1323 case CXXConstructExpr::CK_NonVirtualBase: 1324 JOS.attribute("constructionKind", "non-virtual base"); 1325 break; 1326 case CXXConstructExpr::CK_VirtualBase: 1327 JOS.attribute("constructionKind", "virtual base"); 1328 break; 1329 } 1330 } 1331 1332 void JSONNodeDumper::VisitExprWithCleanups(const ExprWithCleanups *EWC) { 1333 attributeOnlyIfTrue("cleanupsHaveSideEffects", 1334 EWC->cleanupsHaveSideEffects()); 1335 if (EWC->getNumObjects()) { 1336 JOS.attributeArray("cleanups", [this, EWC] { 1337 for (const ExprWithCleanups::CleanupObject &CO : EWC->getObjects()) 1338 if (auto *BD = CO.dyn_cast<BlockDecl *>()) { 1339 JOS.value(createBareDeclRef(BD)); 1340 } else if (auto *CLE = CO.dyn_cast<CompoundLiteralExpr *>()) { 1341 llvm::json::Object Obj; 1342 Obj["id"] = createPointerRepresentation(CLE); 1343 Obj["kind"] = CLE->getStmtClassName(); 1344 JOS.value(std::move(Obj)); 1345 } else { 1346 llvm_unreachable("unexpected cleanup object type"); 1347 } 1348 }); 1349 } 1350 } 1351 1352 void JSONNodeDumper::VisitCXXBindTemporaryExpr( 1353 const CXXBindTemporaryExpr *BTE) { 1354 const CXXTemporary *Temp = BTE->getTemporary(); 1355 JOS.attribute("temp", createPointerRepresentation(Temp)); 1356 if (const CXXDestructorDecl *Dtor = Temp->getDestructor()) 1357 JOS.attribute("dtor", createBareDeclRef(Dtor)); 1358 } 1359 1360 void JSONNodeDumper::VisitMaterializeTemporaryExpr( 1361 const MaterializeTemporaryExpr *MTE) { 1362 if (const ValueDecl *VD = MTE->getExtendingDecl()) 1363 JOS.attribute("extendingDecl", createBareDeclRef(VD)); 1364 1365 switch (MTE->getStorageDuration()) { 1366 case SD_Automatic: 1367 JOS.attribute("storageDuration", "automatic"); 1368 break; 1369 case SD_Dynamic: 1370 JOS.attribute("storageDuration", "dynamic"); 1371 break; 1372 case SD_FullExpression: 1373 JOS.attribute("storageDuration", "full expression"); 1374 break; 1375 case SD_Static: 1376 JOS.attribute("storageDuration", "static"); 1377 break; 1378 case SD_Thread: 1379 JOS.attribute("storageDuration", "thread"); 1380 break; 1381 } 1382 1383 attributeOnlyIfTrue("boundToLValueRef", MTE->isBoundToLvalueReference()); 1384 } 1385 1386 void JSONNodeDumper::VisitCXXDependentScopeMemberExpr( 1387 const CXXDependentScopeMemberExpr *DSME) { 1388 JOS.attribute("isArrow", DSME->isArrow()); 1389 JOS.attribute("member", DSME->getMember().getAsString()); 1390 attributeOnlyIfTrue("hasTemplateKeyword", DSME->hasTemplateKeyword()); 1391 attributeOnlyIfTrue("hasExplicitTemplateArgs", 1392 DSME->hasExplicitTemplateArgs()); 1393 1394 if (DSME->getNumTemplateArgs()) { 1395 JOS.attributeArray("explicitTemplateArgs", [DSME, this] { 1396 for (const TemplateArgumentLoc &TAL : DSME->template_arguments()) 1397 JOS.object( 1398 [&TAL, this] { Visit(TAL.getArgument(), TAL.getSourceRange()); }); 1399 }); 1400 } 1401 } 1402 1403 void JSONNodeDumper::VisitIntegerLiteral(const IntegerLiteral *IL) { 1404 JOS.attribute("value", 1405 IL->getValue().toString( 1406 /*Radix=*/10, IL->getType()->isSignedIntegerType())); 1407 } 1408 void JSONNodeDumper::VisitCharacterLiteral(const CharacterLiteral *CL) { 1409 // FIXME: This should probably print the character literal as a string, 1410 // rather than as a numerical value. It would be nice if the behavior matched 1411 // what we do to print a string literal; right now, it is impossible to tell 1412 // the difference between 'a' and L'a' in C from the JSON output. 1413 JOS.attribute("value", CL->getValue()); 1414 } 1415 void JSONNodeDumper::VisitFixedPointLiteral(const FixedPointLiteral *FPL) { 1416 JOS.attribute("value", FPL->getValueAsString(/*Radix=*/10)); 1417 } 1418 void JSONNodeDumper::VisitFloatingLiteral(const FloatingLiteral *FL) { 1419 llvm::SmallVector<char, 16> Buffer; 1420 FL->getValue().toString(Buffer); 1421 JOS.attribute("value", Buffer); 1422 } 1423 void JSONNodeDumper::VisitStringLiteral(const StringLiteral *SL) { 1424 std::string Buffer; 1425 llvm::raw_string_ostream SS(Buffer); 1426 SL->outputString(SS); 1427 JOS.attribute("value", SS.str()); 1428 } 1429 void JSONNodeDumper::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *BLE) { 1430 JOS.attribute("value", BLE->getValue()); 1431 } 1432 1433 void JSONNodeDumper::VisitIfStmt(const IfStmt *IS) { 1434 attributeOnlyIfTrue("hasInit", IS->hasInitStorage()); 1435 attributeOnlyIfTrue("hasVar", IS->hasVarStorage()); 1436 attributeOnlyIfTrue("hasElse", IS->hasElseStorage()); 1437 attributeOnlyIfTrue("isConstexpr", IS->isConstexpr()); 1438 } 1439 1440 void JSONNodeDumper::VisitSwitchStmt(const SwitchStmt *SS) { 1441 attributeOnlyIfTrue("hasInit", SS->hasInitStorage()); 1442 attributeOnlyIfTrue("hasVar", SS->hasVarStorage()); 1443 } 1444 void JSONNodeDumper::VisitCaseStmt(const CaseStmt *CS) { 1445 attributeOnlyIfTrue("isGNURange", CS->caseStmtIsGNURange()); 1446 } 1447 1448 void JSONNodeDumper::VisitLabelStmt(const LabelStmt *LS) { 1449 JOS.attribute("name", LS->getName()); 1450 JOS.attribute("declId", createPointerRepresentation(LS->getDecl())); 1451 } 1452 void JSONNodeDumper::VisitGotoStmt(const GotoStmt *GS) { 1453 JOS.attribute("targetLabelDeclId", 1454 createPointerRepresentation(GS->getLabel())); 1455 } 1456 1457 void JSONNodeDumper::VisitWhileStmt(const WhileStmt *WS) { 1458 attributeOnlyIfTrue("hasVar", WS->hasVarStorage()); 1459 } 1460 1461 void JSONNodeDumper::VisitObjCAtCatchStmt(const ObjCAtCatchStmt* OACS) { 1462 // FIXME: it would be nice for the ASTNodeTraverser would handle the catch 1463 // parameter the same way for C++ and ObjC rather. In this case, C++ gets a 1464 // null child node and ObjC gets no child node. 1465 attributeOnlyIfTrue("isCatchAll", OACS->getCatchParamDecl() == nullptr); 1466 } 1467 1468 void JSONNodeDumper::VisitNullTemplateArgument(const TemplateArgument &TA) { 1469 JOS.attribute("isNull", true); 1470 } 1471 void JSONNodeDumper::VisitTypeTemplateArgument(const TemplateArgument &TA) { 1472 JOS.attribute("type", createQualType(TA.getAsType())); 1473 } 1474 void JSONNodeDumper::VisitDeclarationTemplateArgument( 1475 const TemplateArgument &TA) { 1476 JOS.attribute("decl", createBareDeclRef(TA.getAsDecl())); 1477 } 1478 void JSONNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &TA) { 1479 JOS.attribute("isNullptr", true); 1480 } 1481 void JSONNodeDumper::VisitIntegralTemplateArgument(const TemplateArgument &TA) { 1482 JOS.attribute("value", TA.getAsIntegral().getSExtValue()); 1483 } 1484 void JSONNodeDumper::VisitTemplateTemplateArgument(const TemplateArgument &TA) { 1485 // FIXME: cannot just call dump() on the argument, as that doesn't specify 1486 // the output format. 1487 } 1488 void JSONNodeDumper::VisitTemplateExpansionTemplateArgument( 1489 const TemplateArgument &TA) { 1490 // FIXME: cannot just call dump() on the argument, as that doesn't specify 1491 // the output format. 1492 } 1493 void JSONNodeDumper::VisitExpressionTemplateArgument( 1494 const TemplateArgument &TA) { 1495 JOS.attribute("isExpr", true); 1496 } 1497 void JSONNodeDumper::VisitPackTemplateArgument(const TemplateArgument &TA) { 1498 JOS.attribute("isPack", true); 1499 } 1500 1501 StringRef JSONNodeDumper::getCommentCommandName(unsigned CommandID) const { 1502 if (Traits) 1503 return Traits->getCommandInfo(CommandID)->Name; 1504 if (const comments::CommandInfo *Info = 1505 comments::CommandTraits::getBuiltinCommandInfo(CommandID)) 1506 return Info->Name; 1507 return "<invalid>"; 1508 } 1509 1510 void JSONNodeDumper::visitTextComment(const comments::TextComment *C, 1511 const comments::FullComment *) { 1512 JOS.attribute("text", C->getText()); 1513 } 1514 1515 void JSONNodeDumper::visitInlineCommandComment( 1516 const comments::InlineCommandComment *C, const comments::FullComment *) { 1517 JOS.attribute("name", getCommentCommandName(C->getCommandID())); 1518 1519 switch (C->getRenderKind()) { 1520 case comments::InlineCommandComment::RenderNormal: 1521 JOS.attribute("renderKind", "normal"); 1522 break; 1523 case comments::InlineCommandComment::RenderBold: 1524 JOS.attribute("renderKind", "bold"); 1525 break; 1526 case comments::InlineCommandComment::RenderEmphasized: 1527 JOS.attribute("renderKind", "emphasized"); 1528 break; 1529 case comments::InlineCommandComment::RenderMonospaced: 1530 JOS.attribute("renderKind", "monospaced"); 1531 break; 1532 case comments::InlineCommandComment::RenderAnchor: 1533 JOS.attribute("renderKind", "anchor"); 1534 break; 1535 } 1536 1537 llvm::json::Array Args; 1538 for (unsigned I = 0, E = C->getNumArgs(); I < E; ++I) 1539 Args.push_back(C->getArgText(I)); 1540 1541 if (!Args.empty()) 1542 JOS.attribute("args", std::move(Args)); 1543 } 1544 1545 void JSONNodeDumper::visitHTMLStartTagComment( 1546 const comments::HTMLStartTagComment *C, const comments::FullComment *) { 1547 JOS.attribute("name", C->getTagName()); 1548 attributeOnlyIfTrue("selfClosing", C->isSelfClosing()); 1549 attributeOnlyIfTrue("malformed", C->isMalformed()); 1550 1551 llvm::json::Array Attrs; 1552 for (unsigned I = 0, E = C->getNumAttrs(); I < E; ++I) 1553 Attrs.push_back( 1554 {{"name", C->getAttr(I).Name}, {"value", C->getAttr(I).Value}}); 1555 1556 if (!Attrs.empty()) 1557 JOS.attribute("attrs", std::move(Attrs)); 1558 } 1559 1560 void JSONNodeDumper::visitHTMLEndTagComment( 1561 const comments::HTMLEndTagComment *C, const comments::FullComment *) { 1562 JOS.attribute("name", C->getTagName()); 1563 } 1564 1565 void JSONNodeDumper::visitBlockCommandComment( 1566 const comments::BlockCommandComment *C, const comments::FullComment *) { 1567 JOS.attribute("name", getCommentCommandName(C->getCommandID())); 1568 1569 llvm::json::Array Args; 1570 for (unsigned I = 0, E = C->getNumArgs(); I < E; ++I) 1571 Args.push_back(C->getArgText(I)); 1572 1573 if (!Args.empty()) 1574 JOS.attribute("args", std::move(Args)); 1575 } 1576 1577 void JSONNodeDumper::visitParamCommandComment( 1578 const comments::ParamCommandComment *C, const comments::FullComment *FC) { 1579 switch (C->getDirection()) { 1580 case comments::ParamCommandComment::In: 1581 JOS.attribute("direction", "in"); 1582 break; 1583 case comments::ParamCommandComment::Out: 1584 JOS.attribute("direction", "out"); 1585 break; 1586 case comments::ParamCommandComment::InOut: 1587 JOS.attribute("direction", "in,out"); 1588 break; 1589 } 1590 attributeOnlyIfTrue("explicit", C->isDirectionExplicit()); 1591 1592 if (C->hasParamName()) 1593 JOS.attribute("param", C->isParamIndexValid() ? C->getParamName(FC) 1594 : C->getParamNameAsWritten()); 1595 1596 if (C->isParamIndexValid() && !C->isVarArgParam()) 1597 JOS.attribute("paramIdx", C->getParamIndex()); 1598 } 1599 1600 void JSONNodeDumper::visitTParamCommandComment( 1601 const comments::TParamCommandComment *C, const comments::FullComment *FC) { 1602 if (C->hasParamName()) 1603 JOS.attribute("param", C->isPositionValid() ? C->getParamName(FC) 1604 : C->getParamNameAsWritten()); 1605 if (C->isPositionValid()) { 1606 llvm::json::Array Positions; 1607 for (unsigned I = 0, E = C->getDepth(); I < E; ++I) 1608 Positions.push_back(C->getIndex(I)); 1609 1610 if (!Positions.empty()) 1611 JOS.attribute("positions", std::move(Positions)); 1612 } 1613 } 1614 1615 void JSONNodeDumper::visitVerbatimBlockComment( 1616 const comments::VerbatimBlockComment *C, const comments::FullComment *) { 1617 JOS.attribute("name", getCommentCommandName(C->getCommandID())); 1618 JOS.attribute("closeName", C->getCloseName()); 1619 } 1620 1621 void JSONNodeDumper::visitVerbatimBlockLineComment( 1622 const comments::VerbatimBlockLineComment *C, 1623 const comments::FullComment *) { 1624 JOS.attribute("text", C->getText()); 1625 } 1626 1627 void JSONNodeDumper::visitVerbatimLineComment( 1628 const comments::VerbatimLineComment *C, const comments::FullComment *) { 1629 JOS.attribute("text", C->getText()); 1630 } 1631