1 //===- CXIndexDataConsumer.cpp - Index data consumer for libclang----------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "CXIndexDataConsumer.h" 10 #include "CIndexDiagnostic.h" 11 #include "CXTranslationUnit.h" 12 #include "clang/AST/Attr.h" 13 #include "clang/AST/DeclCXX.h" 14 #include "clang/AST/DeclTemplate.h" 15 #include "clang/AST/DeclVisitor.h" 16 #include "clang/Frontend/ASTUnit.h" 17 18 using namespace clang; 19 using namespace clang::index; 20 using namespace cxindex; 21 using namespace cxcursor; 22 23 namespace { 24 class IndexingDeclVisitor : public ConstDeclVisitor<IndexingDeclVisitor, bool> { 25 CXIndexDataConsumer &DataConsumer; 26 SourceLocation DeclLoc; 27 const DeclContext *LexicalDC; 28 29 public: 30 IndexingDeclVisitor(CXIndexDataConsumer &dataConsumer, SourceLocation Loc, 31 const DeclContext *lexicalDC) 32 : DataConsumer(dataConsumer), DeclLoc(Loc), LexicalDC(lexicalDC) { } 33 34 bool VisitFunctionDecl(const FunctionDecl *D) { 35 DataConsumer.handleFunction(D); 36 return true; 37 } 38 39 bool VisitVarDecl(const VarDecl *D) { 40 DataConsumer.handleVar(D); 41 return true; 42 } 43 44 bool VisitFieldDecl(const FieldDecl *D) { 45 DataConsumer.handleField(D); 46 return true; 47 } 48 49 bool VisitMSPropertyDecl(const MSPropertyDecl *D) { 50 return true; 51 } 52 53 bool VisitEnumConstantDecl(const EnumConstantDecl *D) { 54 DataConsumer.handleEnumerator(D); 55 return true; 56 } 57 58 bool VisitTypedefNameDecl(const TypedefNameDecl *D) { 59 DataConsumer.handleTypedefName(D); 60 return true; 61 } 62 63 bool VisitTagDecl(const TagDecl *D) { 64 DataConsumer.handleTagDecl(D); 65 return true; 66 } 67 68 bool VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) { 69 DataConsumer.handleObjCInterface(D); 70 return true; 71 } 72 73 bool VisitObjCProtocolDecl(const ObjCProtocolDecl *D) { 74 DataConsumer.handleObjCProtocol(D); 75 return true; 76 } 77 78 bool VisitObjCImplementationDecl(const ObjCImplementationDecl *D) { 79 DataConsumer.handleObjCImplementation(D); 80 return true; 81 } 82 83 bool VisitObjCCategoryDecl(const ObjCCategoryDecl *D) { 84 DataConsumer.handleObjCCategory(D); 85 return true; 86 } 87 88 bool VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) { 89 DataConsumer.handleObjCCategoryImpl(D); 90 return true; 91 } 92 93 bool VisitObjCMethodDecl(const ObjCMethodDecl *D) { 94 if (isa<ObjCImplDecl>(LexicalDC) && !D->isThisDeclarationADefinition()) 95 DataConsumer.handleSynthesizedObjCMethod(D, DeclLoc, LexicalDC); 96 else 97 DataConsumer.handleObjCMethod(D, DeclLoc); 98 return true; 99 } 100 101 bool VisitObjCPropertyDecl(const ObjCPropertyDecl *D) { 102 DataConsumer.handleObjCProperty(D); 103 return true; 104 } 105 106 bool VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) { 107 DataConsumer.handleSynthesizedObjCProperty(D); 108 return true; 109 } 110 111 bool VisitNamespaceDecl(const NamespaceDecl *D) { 112 DataConsumer.handleNamespace(D); 113 return true; 114 } 115 116 bool VisitUsingDecl(const UsingDecl *D) { 117 return true; 118 } 119 120 bool VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) { 121 return true; 122 } 123 124 bool VisitClassTemplateDecl(const ClassTemplateDecl *D) { 125 DataConsumer.handleClassTemplate(D); 126 return true; 127 } 128 129 bool VisitClassTemplateSpecializationDecl(const 130 ClassTemplateSpecializationDecl *D) { 131 DataConsumer.handleTagDecl(D); 132 return true; 133 } 134 135 bool VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) { 136 DataConsumer.handleFunctionTemplate(D); 137 return true; 138 } 139 140 bool VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D) { 141 DataConsumer.handleTypeAliasTemplate(D); 142 return true; 143 } 144 145 bool VisitImportDecl(const ImportDecl *D) { 146 DataConsumer.importedModule(D); 147 return true; 148 } 149 }; 150 151 CXSymbolRole getSymbolRole(SymbolRoleSet Role) { 152 // CXSymbolRole mirrors low 9 bits of clang::index::SymbolRole. 153 return CXSymbolRole(static_cast<uint32_t>(Role) & ((1 << 9) - 1)); 154 } 155 } 156 157 bool CXIndexDataConsumer::handleDeclOccurrence( 158 const Decl *D, SymbolRoleSet Roles, ArrayRef<SymbolRelation> Relations, 159 SourceLocation Loc, ASTNodeInfo ASTNode) { 160 Loc = getASTContext().getSourceManager().getFileLoc(Loc); 161 162 if (Roles & (unsigned)SymbolRole::Reference) { 163 const NamedDecl *ND = dyn_cast<NamedDecl>(D); 164 if (!ND) 165 return true; 166 167 if (auto *ObjCID = dyn_cast_or_null<ObjCInterfaceDecl>(ASTNode.OrigD)) { 168 if (!ObjCID->isThisDeclarationADefinition() && 169 ObjCID->getLocation() == Loc) { 170 // The libclang API treats this as ObjCClassRef declaration. 171 IndexingDeclVisitor(*this, Loc, nullptr).Visit(ObjCID); 172 return true; 173 } 174 } 175 if (auto *ObjCPD = dyn_cast_or_null<ObjCProtocolDecl>(ASTNode.OrigD)) { 176 if (!ObjCPD->isThisDeclarationADefinition() && 177 ObjCPD->getLocation() == Loc) { 178 // The libclang API treats this as ObjCProtocolRef declaration. 179 IndexingDeclVisitor(*this, Loc, nullptr).Visit(ObjCPD); 180 return true; 181 } 182 } 183 184 CXIdxEntityRefKind Kind = CXIdxEntityRef_Direct; 185 if (Roles & (unsigned)SymbolRole::Implicit) { 186 Kind = CXIdxEntityRef_Implicit; 187 } 188 CXSymbolRole CXRole = getSymbolRole(Roles); 189 190 CXCursor Cursor; 191 if (ASTNode.OrigE) { 192 Cursor = cxcursor::MakeCXCursor(ASTNode.OrigE, 193 cast<Decl>(ASTNode.ContainerDC), 194 getCXTU()); 195 } else { 196 if (ASTNode.OrigD) { 197 if (auto *OrigND = dyn_cast<NamedDecl>(ASTNode.OrigD)) 198 Cursor = getRefCursor(OrigND, Loc); 199 else 200 Cursor = MakeCXCursor(ASTNode.OrigD, CXTU); 201 } else { 202 Cursor = getRefCursor(ND, Loc); 203 } 204 } 205 handleReference(ND, Loc, Cursor, 206 dyn_cast_or_null<NamedDecl>(ASTNode.Parent), 207 ASTNode.ContainerDC, ASTNode.OrigE, Kind, CXRole); 208 209 } else { 210 const DeclContext *LexicalDC = ASTNode.ContainerDC; 211 if (!LexicalDC) { 212 for (const auto &SymRel : Relations) { 213 if (SymRel.Roles & (unsigned)SymbolRole::RelationChildOf) 214 LexicalDC = dyn_cast<DeclContext>(SymRel.RelatedSymbol); 215 } 216 } 217 IndexingDeclVisitor(*this, Loc, LexicalDC).Visit(ASTNode.OrigD); 218 } 219 220 return !shouldAbort(); 221 } 222 223 bool CXIndexDataConsumer::handleModuleOccurrence(const ImportDecl *ImportD, 224 const Module *Mod, 225 SymbolRoleSet Roles, 226 SourceLocation Loc) { 227 if (Roles & (SymbolRoleSet)SymbolRole::Declaration) 228 IndexingDeclVisitor(*this, SourceLocation(), nullptr).Visit(ImportD); 229 return !shouldAbort(); 230 } 231 232 void CXIndexDataConsumer::finish() { 233 indexDiagnostics(); 234 } 235 236 237 CXIndexDataConsumer::ObjCProtocolListInfo::ObjCProtocolListInfo( 238 const ObjCProtocolList &ProtList, 239 CXIndexDataConsumer &IdxCtx, 240 ScratchAlloc &SA) { 241 ObjCInterfaceDecl::protocol_loc_iterator LI = ProtList.loc_begin(); 242 for (ObjCInterfaceDecl::protocol_iterator 243 I = ProtList.begin(), E = ProtList.end(); I != E; ++I, ++LI) { 244 SourceLocation Loc = *LI; 245 ObjCProtocolDecl *PD = *I; 246 ProtEntities.push_back(EntityInfo()); 247 IdxCtx.getEntityInfo(PD, ProtEntities.back(), SA); 248 CXIdxObjCProtocolRefInfo ProtInfo = { nullptr, 249 MakeCursorObjCProtocolRef(PD, Loc, IdxCtx.CXTU), 250 IdxCtx.getIndexLoc(Loc) }; 251 ProtInfos.push_back(ProtInfo); 252 253 if (IdxCtx.shouldSuppressRefs()) 254 IdxCtx.markEntityOccurrenceInFile(PD, Loc); 255 } 256 257 for (unsigned i = 0, e = ProtInfos.size(); i != e; ++i) 258 ProtInfos[i].protocol = &ProtEntities[i]; 259 260 for (unsigned i = 0, e = ProtInfos.size(); i != e; ++i) 261 Prots.push_back(&ProtInfos[i]); 262 } 263 264 265 IBOutletCollectionInfo::IBOutletCollectionInfo( 266 const IBOutletCollectionInfo &other) 267 : AttrInfo(CXIdxAttr_IBOutletCollection, other.cursor, other.loc, other.A) { 268 269 IBCollInfo.attrInfo = this; 270 IBCollInfo.classCursor = other.IBCollInfo.classCursor; 271 IBCollInfo.classLoc = other.IBCollInfo.classLoc; 272 if (other.IBCollInfo.objcClass) { 273 ClassInfo = other.ClassInfo; 274 IBCollInfo.objcClass = &ClassInfo; 275 } else 276 IBCollInfo.objcClass = nullptr; 277 } 278 279 AttrListInfo::AttrListInfo(const Decl *D, CXIndexDataConsumer &IdxCtx) 280 : SA(IdxCtx), ref_cnt(0) { 281 282 if (!D->hasAttrs()) 283 return; 284 285 for (const auto *A : D->attrs()) { 286 CXCursor C = MakeCXCursor(A, D, IdxCtx.CXTU); 287 CXIdxLoc Loc = IdxCtx.getIndexLoc(A->getLocation()); 288 switch (C.kind) { 289 default: 290 Attrs.push_back(AttrInfo(CXIdxAttr_Unexposed, C, Loc, A)); 291 break; 292 case CXCursor_IBActionAttr: 293 Attrs.push_back(AttrInfo(CXIdxAttr_IBAction, C, Loc, A)); 294 break; 295 case CXCursor_IBOutletAttr: 296 Attrs.push_back(AttrInfo(CXIdxAttr_IBOutlet, C, Loc, A)); 297 break; 298 case CXCursor_IBOutletCollectionAttr: 299 IBCollAttrs.push_back(IBOutletCollectionInfo(C, Loc, A)); 300 break; 301 } 302 } 303 304 for (unsigned i = 0, e = IBCollAttrs.size(); i != e; ++i) { 305 IBOutletCollectionInfo &IBInfo = IBCollAttrs[i]; 306 CXAttrs.push_back(&IBInfo); 307 308 const IBOutletCollectionAttr * 309 IBAttr = cast<IBOutletCollectionAttr>(IBInfo.A); 310 SourceLocation InterfaceLocStart = 311 IBAttr->getInterfaceLoc()->getTypeLoc().getBeginLoc(); 312 IBInfo.IBCollInfo.attrInfo = &IBInfo; 313 IBInfo.IBCollInfo.classLoc = IdxCtx.getIndexLoc(InterfaceLocStart); 314 IBInfo.IBCollInfo.objcClass = nullptr; 315 IBInfo.IBCollInfo.classCursor = clang_getNullCursor(); 316 QualType Ty = IBAttr->getInterface(); 317 if (const ObjCObjectType *ObjectTy = Ty->getAs<ObjCObjectType>()) { 318 if (const ObjCInterfaceDecl *InterD = ObjectTy->getInterface()) { 319 IdxCtx.getEntityInfo(InterD, IBInfo.ClassInfo, SA); 320 IBInfo.IBCollInfo.objcClass = &IBInfo.ClassInfo; 321 IBInfo.IBCollInfo.classCursor = 322 MakeCursorObjCClassRef(InterD, InterfaceLocStart, IdxCtx.CXTU); 323 } 324 } 325 } 326 327 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) 328 CXAttrs.push_back(&Attrs[i]); 329 } 330 331 IntrusiveRefCntPtr<AttrListInfo> 332 AttrListInfo::create(const Decl *D, CXIndexDataConsumer &IdxCtx) { 333 ScratchAlloc SA(IdxCtx); 334 AttrListInfo *attrs = SA.allocate<AttrListInfo>(); 335 return new (attrs) AttrListInfo(D, IdxCtx); 336 } 337 338 CXIndexDataConsumer::CXXBasesListInfo::CXXBasesListInfo(const CXXRecordDecl *D, 339 CXIndexDataConsumer &IdxCtx, 340 ScratchAlloc &SA) { 341 for (const auto &Base : D->bases()) { 342 BaseEntities.push_back(EntityInfo()); 343 const NamedDecl *BaseD = nullptr; 344 QualType T = Base.getType(); 345 SourceLocation Loc = getBaseLoc(Base); 346 347 if (const TypedefType *TDT = T->getAs<TypedefType>()) { 348 BaseD = TDT->getDecl(); 349 } else if (const TemplateSpecializationType * 350 TST = T->getAs<TemplateSpecializationType>()) { 351 BaseD = TST->getTemplateName().getAsTemplateDecl(); 352 } else if (const RecordType *RT = T->getAs<RecordType>()) { 353 BaseD = RT->getDecl(); 354 } 355 356 if (BaseD) 357 IdxCtx.getEntityInfo(BaseD, BaseEntities.back(), SA); 358 CXIdxBaseClassInfo BaseInfo = { nullptr, 359 MakeCursorCXXBaseSpecifier(&Base, IdxCtx.CXTU), 360 IdxCtx.getIndexLoc(Loc) }; 361 BaseInfos.push_back(BaseInfo); 362 } 363 364 for (unsigned i = 0, e = BaseInfos.size(); i != e; ++i) { 365 if (BaseEntities[i].name && BaseEntities[i].USR) 366 BaseInfos[i].base = &BaseEntities[i]; 367 } 368 369 for (unsigned i = 0, e = BaseInfos.size(); i != e; ++i) 370 CXBases.push_back(&BaseInfos[i]); 371 } 372 373 SourceLocation CXIndexDataConsumer::CXXBasesListInfo::getBaseLoc( 374 const CXXBaseSpecifier &Base) const { 375 SourceLocation Loc = Base.getSourceRange().getBegin(); 376 TypeLoc TL; 377 if (Base.getTypeSourceInfo()) 378 TL = Base.getTypeSourceInfo()->getTypeLoc(); 379 if (TL.isNull()) 380 return Loc; 381 382 if (QualifiedTypeLoc QL = TL.getAs<QualifiedTypeLoc>()) 383 TL = QL.getUnqualifiedLoc(); 384 385 if (ElaboratedTypeLoc EL = TL.getAs<ElaboratedTypeLoc>()) 386 return EL.getNamedTypeLoc().getBeginLoc(); 387 if (DependentNameTypeLoc DL = TL.getAs<DependentNameTypeLoc>()) 388 return DL.getNameLoc(); 389 if (DependentTemplateSpecializationTypeLoc DTL = 390 TL.getAs<DependentTemplateSpecializationTypeLoc>()) 391 return DTL.getTemplateNameLoc(); 392 393 return Loc; 394 } 395 396 const char *ScratchAlloc::toCStr(StringRef Str) { 397 if (Str.empty()) 398 return ""; 399 if (Str.data()[Str.size()] == '\0') 400 return Str.data(); 401 return copyCStr(Str); 402 } 403 404 const char *ScratchAlloc::copyCStr(StringRef Str) { 405 char *buf = IdxCtx.StrScratch.Allocate<char>(Str.size() + 1); 406 std::uninitialized_copy(Str.begin(), Str.end(), buf); 407 buf[Str.size()] = '\0'; 408 return buf; 409 } 410 411 void CXIndexDataConsumer::setASTContext(ASTContext &ctx) { 412 Ctx = &ctx; 413 cxtu::getASTUnit(CXTU)->setASTContext(&ctx); 414 } 415 416 void CXIndexDataConsumer::setPreprocessor(std::shared_ptr<Preprocessor> PP) { 417 cxtu::getASTUnit(CXTU)->setPreprocessor(std::move(PP)); 418 } 419 420 bool CXIndexDataConsumer::isFunctionLocalDecl(const Decl *D) { 421 assert(D); 422 423 if (!D->getParentFunctionOrMethod()) 424 return false; 425 426 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) { 427 switch (ND->getFormalLinkage()) { 428 case NoLinkage: 429 case InternalLinkage: 430 return true; 431 case VisibleNoLinkage: 432 case ModuleInternalLinkage: 433 case UniqueExternalLinkage: 434 llvm_unreachable("Not a sema linkage"); 435 case ModuleLinkage: 436 case ExternalLinkage: 437 return false; 438 } 439 } 440 441 return true; 442 } 443 444 bool CXIndexDataConsumer::shouldAbort() { 445 if (!CB.abortQuery) 446 return false; 447 return CB.abortQuery(ClientData, nullptr); 448 } 449 450 void CXIndexDataConsumer::enteredMainFile(const FileEntry *File) { 451 if (File && CB.enteredMainFile) { 452 CXIdxClientFile idxFile = 453 CB.enteredMainFile(ClientData, 454 static_cast<CXFile>(const_cast<FileEntry *>(File)), 455 nullptr); 456 FileMap[File] = idxFile; 457 } 458 } 459 460 void CXIndexDataConsumer::ppIncludedFile(SourceLocation hashLoc, 461 StringRef filename, 462 const FileEntry *File, 463 bool isImport, bool isAngled, 464 bool isModuleImport) { 465 if (!CB.ppIncludedFile) 466 return; 467 468 ScratchAlloc SA(*this); 469 CXIdxIncludedFileInfo Info = { getIndexLoc(hashLoc), 470 SA.toCStr(filename), 471 static_cast<CXFile>( 472 const_cast<FileEntry *>(File)), 473 isImport, isAngled, isModuleImport }; 474 CXIdxClientFile idxFile = CB.ppIncludedFile(ClientData, &Info); 475 FileMap[File] = idxFile; 476 } 477 478 void CXIndexDataConsumer::importedModule(const ImportDecl *ImportD) { 479 if (!CB.importedASTFile) 480 return; 481 482 Module *Mod = ImportD->getImportedModule(); 483 if (!Mod) 484 return; 485 486 // If the imported module is part of the top-level module that we're 487 // indexing, it doesn't correspond to an imported AST file. 488 // FIXME: This assumes that AST files and top-level modules directly 489 // correspond, which is unlikely to remain true forever. 490 if (Module *SrcMod = ImportD->getImportedOwningModule()) 491 if (SrcMod->getTopLevelModule() == Mod->getTopLevelModule()) 492 return; 493 494 CXIdxImportedASTFileInfo Info = { 495 static_cast<CXFile>( 496 const_cast<FileEntry *>(Mod->getASTFile())), 497 Mod, 498 getIndexLoc(ImportD->getLocation()), 499 ImportD->isImplicit() 500 }; 501 CXIdxClientASTFile astFile = CB.importedASTFile(ClientData, &Info); 502 (void)astFile; 503 } 504 505 void CXIndexDataConsumer::importedPCH(const FileEntry *File) { 506 if (!CB.importedASTFile) 507 return; 508 509 CXIdxImportedASTFileInfo Info = { 510 static_cast<CXFile>( 511 const_cast<FileEntry *>(File)), 512 /*module=*/nullptr, 513 getIndexLoc(SourceLocation()), 514 /*isImplicit=*/false 515 }; 516 CXIdxClientASTFile astFile = CB.importedASTFile(ClientData, &Info); 517 (void)astFile; 518 } 519 520 void CXIndexDataConsumer::startedTranslationUnit() { 521 CXIdxClientContainer idxCont = nullptr; 522 if (CB.startedTranslationUnit) 523 idxCont = CB.startedTranslationUnit(ClientData, nullptr); 524 addContainerInMap(Ctx->getTranslationUnitDecl(), idxCont); 525 } 526 527 void CXIndexDataConsumer::indexDiagnostics() { 528 if (!hasDiagnosticCallback()) 529 return; 530 531 CXDiagnosticSetImpl *DiagSet = cxdiag::lazyCreateDiags(getCXTU()); 532 handleDiagnosticSet(DiagSet); 533 } 534 535 void CXIndexDataConsumer::handleDiagnosticSet(CXDiagnostic CXDiagSet) { 536 if (!CB.diagnostic) 537 return; 538 539 CB.diagnostic(ClientData, CXDiagSet, nullptr); 540 } 541 542 bool CXIndexDataConsumer::handleDecl(const NamedDecl *D, 543 SourceLocation Loc, CXCursor Cursor, 544 DeclInfo &DInfo, 545 const DeclContext *LexicalDC, 546 const DeclContext *SemaDC) { 547 if (!CB.indexDeclaration || !D) 548 return false; 549 if (D->isImplicit() && shouldIgnoreIfImplicit(D)) 550 return false; 551 552 ScratchAlloc SA(*this); 553 getEntityInfo(D, DInfo.EntInfo, SA); 554 if ((!shouldIndexFunctionLocalSymbols() && !DInfo.EntInfo.USR) 555 || Loc.isInvalid()) 556 return false; 557 558 if (!LexicalDC) 559 LexicalDC = D->getLexicalDeclContext(); 560 561 if (shouldSuppressRefs()) 562 markEntityOccurrenceInFile(D, Loc); 563 564 DInfo.entityInfo = &DInfo.EntInfo; 565 DInfo.cursor = Cursor; 566 DInfo.loc = getIndexLoc(Loc); 567 DInfo.isImplicit = D->isImplicit(); 568 569 DInfo.attributes = DInfo.EntInfo.attributes; 570 DInfo.numAttributes = DInfo.EntInfo.numAttributes; 571 572 if (!SemaDC) 573 SemaDC = D->getDeclContext(); 574 getContainerInfo(SemaDC, DInfo.SemanticContainer); 575 DInfo.semanticContainer = &DInfo.SemanticContainer; 576 577 if (LexicalDC == SemaDC) { 578 DInfo.lexicalContainer = &DInfo.SemanticContainer; 579 } else if (isTemplateImplicitInstantiation(D)) { 580 // Implicit instantiations have the lexical context of where they were 581 // instantiated first. We choose instead the semantic context because: 582 // 1) at the time that we see the instantiation we have not seen the 583 // function where it occurred yet. 584 // 2) the lexical context of the first instantiation is not useful 585 // information anyway. 586 DInfo.lexicalContainer = &DInfo.SemanticContainer; 587 } else { 588 getContainerInfo(LexicalDC, DInfo.LexicalContainer); 589 DInfo.lexicalContainer = &DInfo.LexicalContainer; 590 } 591 592 if (DInfo.isContainer) { 593 getContainerInfo(getEntityContainer(D), DInfo.DeclAsContainer); 594 DInfo.declAsContainer = &DInfo.DeclAsContainer; 595 } 596 597 CB.indexDeclaration(ClientData, &DInfo); 598 return true; 599 } 600 601 bool CXIndexDataConsumer::handleObjCContainer(const ObjCContainerDecl *D, 602 SourceLocation Loc, CXCursor Cursor, 603 ObjCContainerDeclInfo &ContDInfo) { 604 ContDInfo.ObjCContDeclInfo.declInfo = &ContDInfo; 605 return handleDecl(D, Loc, Cursor, ContDInfo); 606 } 607 608 bool CXIndexDataConsumer::handleFunction(const FunctionDecl *D) { 609 bool isDef = D->isThisDeclarationADefinition(); 610 bool isContainer = isDef; 611 bool isSkipped = false; 612 if (D->hasSkippedBody()) { 613 isSkipped = true; 614 isDef = true; 615 isContainer = false; 616 } 617 618 DeclInfo DInfo(!D->isFirstDecl(), isDef, isContainer); 619 if (isSkipped) 620 DInfo.flags |= CXIdxDeclFlag_Skipped; 621 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 622 } 623 624 bool CXIndexDataConsumer::handleVar(const VarDecl *D) { 625 DeclInfo DInfo(!D->isFirstDecl(), D->isThisDeclarationADefinition(), 626 /*isContainer=*/false); 627 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 628 } 629 630 bool CXIndexDataConsumer::handleField(const FieldDecl *D) { 631 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true, 632 /*isContainer=*/false); 633 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 634 } 635 636 bool CXIndexDataConsumer::handleEnumerator(const EnumConstantDecl *D) { 637 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true, 638 /*isContainer=*/false); 639 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 640 } 641 642 bool CXIndexDataConsumer::handleTagDecl(const TagDecl *D) { 643 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(D)) 644 return handleCXXRecordDecl(CXXRD, D); 645 646 DeclInfo DInfo(!D->isFirstDecl(), D->isThisDeclarationADefinition(), 647 D->isThisDeclarationADefinition()); 648 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 649 } 650 651 bool CXIndexDataConsumer::handleTypedefName(const TypedefNameDecl *D) { 652 DeclInfo DInfo(!D->isFirstDecl(), /*isDefinition=*/true, 653 /*isContainer=*/false); 654 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 655 } 656 657 bool CXIndexDataConsumer::handleObjCInterface(const ObjCInterfaceDecl *D) { 658 // For @class forward declarations, suppress them the same way as references. 659 if (!D->isThisDeclarationADefinition()) { 660 if (shouldSuppressRefs() && markEntityOccurrenceInFile(D, D->getLocation())) 661 return false; // already occurred. 662 663 // FIXME: This seems like the wrong definition for redeclaration. 664 bool isRedeclaration = D->hasDefinition() || D->getPreviousDecl(); 665 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true, isRedeclaration, 666 /*isImplementation=*/false); 667 return handleObjCContainer(D, D->getLocation(), 668 MakeCursorObjCClassRef(D, D->getLocation(), 669 CXTU), 670 ContDInfo); 671 } 672 673 ScratchAlloc SA(*this); 674 675 CXIdxBaseClassInfo BaseClass; 676 EntityInfo BaseEntity; 677 BaseClass.cursor = clang_getNullCursor(); 678 if (ObjCInterfaceDecl *SuperD = D->getSuperClass()) { 679 getEntityInfo(SuperD, BaseEntity, SA); 680 SourceLocation SuperLoc = D->getSuperClassLoc(); 681 BaseClass.base = &BaseEntity; 682 BaseClass.cursor = MakeCursorObjCSuperClassRef(SuperD, SuperLoc, CXTU); 683 BaseClass.loc = getIndexLoc(SuperLoc); 684 685 if (shouldSuppressRefs()) 686 markEntityOccurrenceInFile(SuperD, SuperLoc); 687 } 688 689 ObjCProtocolList EmptyProtoList; 690 ObjCProtocolListInfo ProtInfo(D->isThisDeclarationADefinition() 691 ? D->getReferencedProtocols() 692 : EmptyProtoList, 693 *this, SA); 694 695 ObjCInterfaceDeclInfo InterInfo(D); 696 InterInfo.ObjCProtoListInfo = ProtInfo.getListInfo(); 697 InterInfo.ObjCInterDeclInfo.containerInfo = &InterInfo.ObjCContDeclInfo; 698 InterInfo.ObjCInterDeclInfo.superInfo = D->getSuperClass() ? &BaseClass 699 : nullptr; 700 InterInfo.ObjCInterDeclInfo.protocols = &InterInfo.ObjCProtoListInfo; 701 702 return handleObjCContainer(D, D->getLocation(), getCursor(D), InterInfo); 703 } 704 705 bool CXIndexDataConsumer::handleObjCImplementation( 706 const ObjCImplementationDecl *D) { 707 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/false, 708 /*isRedeclaration=*/true, 709 /*isImplementation=*/true); 710 return handleObjCContainer(D, D->getLocation(), getCursor(D), ContDInfo); 711 } 712 713 bool CXIndexDataConsumer::handleObjCProtocol(const ObjCProtocolDecl *D) { 714 if (!D->isThisDeclarationADefinition()) { 715 if (shouldSuppressRefs() && markEntityOccurrenceInFile(D, D->getLocation())) 716 return false; // already occurred. 717 718 // FIXME: This seems like the wrong definition for redeclaration. 719 bool isRedeclaration = D->hasDefinition() || D->getPreviousDecl(); 720 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true, 721 isRedeclaration, 722 /*isImplementation=*/false); 723 return handleObjCContainer(D, D->getLocation(), 724 MakeCursorObjCProtocolRef(D, D->getLocation(), 725 CXTU), 726 ContDInfo); 727 } 728 729 ScratchAlloc SA(*this); 730 ObjCProtocolList EmptyProtoList; 731 ObjCProtocolListInfo ProtListInfo(D->isThisDeclarationADefinition() 732 ? D->getReferencedProtocols() 733 : EmptyProtoList, 734 *this, SA); 735 736 ObjCProtocolDeclInfo ProtInfo(D); 737 ProtInfo.ObjCProtoRefListInfo = ProtListInfo.getListInfo(); 738 739 return handleObjCContainer(D, D->getLocation(), getCursor(D), ProtInfo); 740 } 741 742 bool CXIndexDataConsumer::handleObjCCategory(const ObjCCategoryDecl *D) { 743 ScratchAlloc SA(*this); 744 745 ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/false); 746 EntityInfo ClassEntity; 747 const ObjCInterfaceDecl *IFaceD = D->getClassInterface(); 748 SourceLocation ClassLoc = D->getLocation(); 749 SourceLocation CategoryLoc = D->IsClassExtension() ? ClassLoc 750 : D->getCategoryNameLoc(); 751 getEntityInfo(IFaceD, ClassEntity, SA); 752 753 if (shouldSuppressRefs()) 754 markEntityOccurrenceInFile(IFaceD, ClassLoc); 755 756 ObjCProtocolListInfo ProtInfo(D->getReferencedProtocols(), *this, SA); 757 758 CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo; 759 if (IFaceD) { 760 CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity; 761 CatDInfo.ObjCCatDeclInfo.classCursor = 762 MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU); 763 } else { 764 CatDInfo.ObjCCatDeclInfo.objcClass = nullptr; 765 CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor(); 766 } 767 CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc); 768 CatDInfo.ObjCProtoListInfo = ProtInfo.getListInfo(); 769 CatDInfo.ObjCCatDeclInfo.protocols = &CatDInfo.ObjCProtoListInfo; 770 771 return handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo); 772 } 773 774 bool CXIndexDataConsumer::handleObjCCategoryImpl(const ObjCCategoryImplDecl *D) { 775 ScratchAlloc SA(*this); 776 777 const ObjCCategoryDecl *CatD = D->getCategoryDecl(); 778 ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/true); 779 EntityInfo ClassEntity; 780 const ObjCInterfaceDecl *IFaceD = CatD->getClassInterface(); 781 SourceLocation ClassLoc = D->getLocation(); 782 SourceLocation CategoryLoc = D->getCategoryNameLoc(); 783 getEntityInfo(IFaceD, ClassEntity, SA); 784 785 if (shouldSuppressRefs()) 786 markEntityOccurrenceInFile(IFaceD, ClassLoc); 787 788 CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo; 789 if (IFaceD) { 790 CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity; 791 CatDInfo.ObjCCatDeclInfo.classCursor = 792 MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU); 793 } else { 794 CatDInfo.ObjCCatDeclInfo.objcClass = nullptr; 795 CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor(); 796 } 797 CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc); 798 CatDInfo.ObjCCatDeclInfo.protocols = nullptr; 799 800 return handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo); 801 } 802 803 bool CXIndexDataConsumer::handleObjCMethod(const ObjCMethodDecl *D, 804 SourceLocation Loc) { 805 bool isDef = D->isThisDeclarationADefinition(); 806 bool isContainer = isDef; 807 bool isSkipped = false; 808 if (D->hasSkippedBody()) { 809 isSkipped = true; 810 isDef = true; 811 isContainer = false; 812 } 813 814 DeclInfo DInfo(!D->isCanonicalDecl(), isDef, isContainer); 815 if (isSkipped) 816 DInfo.flags |= CXIdxDeclFlag_Skipped; 817 return handleDecl(D, Loc, getCursor(D), DInfo); 818 } 819 820 bool CXIndexDataConsumer::handleSynthesizedObjCProperty( 821 const ObjCPropertyImplDecl *D) { 822 ObjCPropertyDecl *PD = D->getPropertyDecl(); 823 auto *DC = D->getDeclContext(); 824 return handleReference(PD, D->getLocation(), getCursor(D), 825 dyn_cast<NamedDecl>(DC), DC); 826 } 827 828 bool CXIndexDataConsumer::handleSynthesizedObjCMethod(const ObjCMethodDecl *D, 829 SourceLocation Loc, 830 const DeclContext *LexicalDC) { 831 DeclInfo DInfo(/*isRedeclaration=*/true, /*isDefinition=*/true, 832 /*isContainer=*/false); 833 return handleDecl(D, Loc, getCursor(D), DInfo, LexicalDC, D->getDeclContext()); 834 } 835 836 bool CXIndexDataConsumer::handleObjCProperty(const ObjCPropertyDecl *D) { 837 ScratchAlloc SA(*this); 838 839 ObjCPropertyDeclInfo DInfo; 840 EntityInfo GetterEntity; 841 EntityInfo SetterEntity; 842 843 DInfo.ObjCPropDeclInfo.declInfo = &DInfo; 844 845 if (ObjCMethodDecl *Getter = D->getGetterMethodDecl()) { 846 getEntityInfo(Getter, GetterEntity, SA); 847 DInfo.ObjCPropDeclInfo.getter = &GetterEntity; 848 } else { 849 DInfo.ObjCPropDeclInfo.getter = nullptr; 850 } 851 if (ObjCMethodDecl *Setter = D->getSetterMethodDecl()) { 852 getEntityInfo(Setter, SetterEntity, SA); 853 DInfo.ObjCPropDeclInfo.setter = &SetterEntity; 854 } else { 855 DInfo.ObjCPropDeclInfo.setter = nullptr; 856 } 857 858 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 859 } 860 861 bool CXIndexDataConsumer::handleNamespace(const NamespaceDecl *D) { 862 DeclInfo DInfo(/*isRedeclaration=*/!D->isOriginalNamespace(), 863 /*isDefinition=*/true, 864 /*isContainer=*/true); 865 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 866 } 867 868 bool CXIndexDataConsumer::handleClassTemplate(const ClassTemplateDecl *D) { 869 return handleCXXRecordDecl(D->getTemplatedDecl(), D); 870 } 871 872 bool CXIndexDataConsumer::handleFunctionTemplate(const FunctionTemplateDecl *D) { 873 DeclInfo DInfo(/*isRedeclaration=*/!D->isCanonicalDecl(), 874 /*isDefinition=*/D->isThisDeclarationADefinition(), 875 /*isContainer=*/D->isThisDeclarationADefinition()); 876 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 877 } 878 879 bool CXIndexDataConsumer::handleTypeAliasTemplate(const TypeAliasTemplateDecl *D) { 880 DeclInfo DInfo(/*isRedeclaration=*/!D->isCanonicalDecl(), 881 /*isDefinition=*/true, /*isContainer=*/false); 882 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 883 } 884 885 bool CXIndexDataConsumer::handleReference(const NamedDecl *D, SourceLocation Loc, 886 CXCursor Cursor, 887 const NamedDecl *Parent, 888 const DeclContext *DC, 889 const Expr *E, 890 CXIdxEntityRefKind Kind, 891 CXSymbolRole Role) { 892 if (!CB.indexEntityReference) 893 return false; 894 895 if (!D || !DC) 896 return false; 897 if (Loc.isInvalid()) 898 return false; 899 if (!shouldIndexFunctionLocalSymbols() && isFunctionLocalDecl(D)) 900 return false; 901 if (isNotFromSourceFile(D->getLocation())) 902 return false; 903 if (D->isImplicit() && shouldIgnoreIfImplicit(D)) 904 return false; 905 906 if (shouldSuppressRefs()) { 907 if (markEntityOccurrenceInFile(D, Loc)) 908 return false; // already occurred. 909 } 910 911 ScratchAlloc SA(*this); 912 EntityInfo RefEntity, ParentEntity; 913 getEntityInfo(D, RefEntity, SA); 914 if (!RefEntity.USR) 915 return false; 916 917 getEntityInfo(Parent, ParentEntity, SA); 918 919 ContainerInfo Container; 920 getContainerInfo(DC, Container); 921 922 CXIdxEntityRefInfo Info = { Kind, 923 Cursor, 924 getIndexLoc(Loc), 925 &RefEntity, 926 Parent ? &ParentEntity : nullptr, 927 &Container, 928 Role }; 929 CB.indexEntityReference(ClientData, &Info); 930 return true; 931 } 932 933 bool CXIndexDataConsumer::isNotFromSourceFile(SourceLocation Loc) const { 934 if (Loc.isInvalid()) 935 return true; 936 SourceManager &SM = Ctx->getSourceManager(); 937 SourceLocation FileLoc = SM.getFileLoc(Loc); 938 FileID FID = SM.getFileID(FileLoc); 939 return SM.getFileEntryForID(FID) == nullptr; 940 } 941 942 void CXIndexDataConsumer::addContainerInMap(const DeclContext *DC, 943 CXIdxClientContainer container) { 944 if (!DC) 945 return; 946 947 ContainerMapTy::iterator I = ContainerMap.find(DC); 948 if (I == ContainerMap.end()) { 949 if (container) 950 ContainerMap[DC] = container; 951 return; 952 } 953 // Allow changing the container of a previously seen DeclContext so we 954 // can handle invalid user code, like a function re-definition. 955 if (container) 956 I->second = container; 957 else 958 ContainerMap.erase(I); 959 } 960 961 CXIdxClientEntity CXIndexDataConsumer::getClientEntity(const Decl *D) const { 962 if (!D) 963 return nullptr; 964 EntityMapTy::const_iterator I = EntityMap.find(D); 965 if (I == EntityMap.end()) 966 return nullptr; 967 return I->second; 968 } 969 970 void CXIndexDataConsumer::setClientEntity(const Decl *D, CXIdxClientEntity client) { 971 if (!D) 972 return; 973 EntityMap[D] = client; 974 } 975 976 bool CXIndexDataConsumer::handleCXXRecordDecl(const CXXRecordDecl *RD, 977 const NamedDecl *OrigD) { 978 if (RD->isThisDeclarationADefinition()) { 979 ScratchAlloc SA(*this); 980 CXXClassDeclInfo CXXDInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(), 981 /*isDefinition=*/RD->isThisDeclarationADefinition()); 982 CXXBasesListInfo BaseList(RD, *this, SA); 983 CXXDInfo.CXXClassInfo.declInfo = &CXXDInfo; 984 CXXDInfo.CXXClassInfo.bases = BaseList.getBases(); 985 CXXDInfo.CXXClassInfo.numBases = BaseList.getNumBases(); 986 987 if (shouldSuppressRefs()) { 988 // Go through bases and mark them as referenced. 989 for (unsigned i = 0, e = BaseList.getNumBases(); i != e; ++i) { 990 const CXIdxBaseClassInfo *baseInfo = BaseList.getBases()[i]; 991 if (baseInfo->base) { 992 const NamedDecl *BaseD = BaseList.BaseEntities[i].Dcl; 993 SourceLocation 994 Loc = SourceLocation::getFromRawEncoding(baseInfo->loc.int_data); 995 markEntityOccurrenceInFile(BaseD, Loc); 996 } 997 } 998 } 999 1000 return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), CXXDInfo); 1001 } 1002 1003 DeclInfo DInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(), 1004 /*isDefinition=*/RD->isThisDeclarationADefinition(), 1005 /*isContainer=*/RD->isThisDeclarationADefinition()); 1006 return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), DInfo); 1007 } 1008 1009 bool CXIndexDataConsumer::markEntityOccurrenceInFile(const NamedDecl *D, 1010 SourceLocation Loc) { 1011 if (!D || Loc.isInvalid()) 1012 return true; 1013 1014 SourceManager &SM = Ctx->getSourceManager(); 1015 D = getEntityDecl(D); 1016 1017 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SM.getFileLoc(Loc)); 1018 FileID FID = LocInfo.first; 1019 if (FID.isInvalid()) 1020 return true; 1021 1022 const FileEntry *FE = SM.getFileEntryForID(FID); 1023 if (!FE) 1024 return true; 1025 RefFileOccurrence RefOccur(FE, D); 1026 std::pair<llvm::DenseSet<RefFileOccurrence>::iterator, bool> 1027 res = RefFileOccurrences.insert(RefOccur); 1028 return !res.second; // already in map 1029 } 1030 1031 const NamedDecl *CXIndexDataConsumer::getEntityDecl(const NamedDecl *D) const { 1032 assert(D); 1033 D = cast<NamedDecl>(D->getCanonicalDecl()); 1034 1035 if (const ObjCImplementationDecl * 1036 ImplD = dyn_cast<ObjCImplementationDecl>(D)) { 1037 return getEntityDecl(ImplD->getClassInterface()); 1038 1039 } else if (const ObjCCategoryImplDecl * 1040 CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) { 1041 return getEntityDecl(CatImplD->getCategoryDecl()); 1042 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 1043 if (FunctionTemplateDecl *TemplD = FD->getDescribedFunctionTemplate()) 1044 return getEntityDecl(TemplD); 1045 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { 1046 if (ClassTemplateDecl *TemplD = RD->getDescribedClassTemplate()) 1047 return getEntityDecl(TemplD); 1048 } 1049 1050 return D; 1051 } 1052 1053 const DeclContext * 1054 CXIndexDataConsumer::getEntityContainer(const Decl *D) const { 1055 const DeclContext *DC = dyn_cast<DeclContext>(D); 1056 if (DC) 1057 return DC; 1058 1059 if (const ClassTemplateDecl *ClassTempl = dyn_cast<ClassTemplateDecl>(D)) { 1060 DC = ClassTempl->getTemplatedDecl(); 1061 } else if (const FunctionTemplateDecl * 1062 FuncTempl = dyn_cast<FunctionTemplateDecl>(D)) { 1063 DC = FuncTempl->getTemplatedDecl(); 1064 } 1065 1066 return DC; 1067 } 1068 1069 CXIdxClientContainer 1070 CXIndexDataConsumer::getClientContainerForDC(const DeclContext *DC) const { 1071 if (!DC) 1072 return nullptr; 1073 1074 ContainerMapTy::const_iterator I = ContainerMap.find(DC); 1075 if (I == ContainerMap.end()) 1076 return nullptr; 1077 1078 return I->second; 1079 } 1080 1081 CXIdxClientFile CXIndexDataConsumer::getIndexFile(const FileEntry *File) { 1082 if (!File) 1083 return nullptr; 1084 1085 FileMapTy::iterator FI = FileMap.find(File); 1086 if (FI != FileMap.end()) 1087 return FI->second; 1088 1089 return nullptr; 1090 } 1091 1092 CXIdxLoc CXIndexDataConsumer::getIndexLoc(SourceLocation Loc) const { 1093 CXIdxLoc idxLoc = { {nullptr, nullptr}, 0 }; 1094 if (Loc.isInvalid()) 1095 return idxLoc; 1096 1097 idxLoc.ptr_data[0] = const_cast<CXIndexDataConsumer *>(this); 1098 idxLoc.int_data = Loc.getRawEncoding(); 1099 return idxLoc; 1100 } 1101 1102 void CXIndexDataConsumer::translateLoc(SourceLocation Loc, 1103 CXIdxClientFile *indexFile, CXFile *file, 1104 unsigned *line, unsigned *column, 1105 unsigned *offset) { 1106 if (Loc.isInvalid()) 1107 return; 1108 1109 SourceManager &SM = Ctx->getSourceManager(); 1110 Loc = SM.getFileLoc(Loc); 1111 1112 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 1113 FileID FID = LocInfo.first; 1114 unsigned FileOffset = LocInfo.second; 1115 1116 if (FID.isInvalid()) 1117 return; 1118 1119 const FileEntry *FE = SM.getFileEntryForID(FID); 1120 if (indexFile) 1121 *indexFile = getIndexFile(FE); 1122 if (file) 1123 *file = const_cast<FileEntry *>(FE); 1124 if (line) 1125 *line = SM.getLineNumber(FID, FileOffset); 1126 if (column) 1127 *column = SM.getColumnNumber(FID, FileOffset); 1128 if (offset) 1129 *offset = FileOffset; 1130 } 1131 1132 static CXIdxEntityKind getEntityKindFromSymbolKind(SymbolKind K, SymbolLanguage L); 1133 static CXIdxEntityCXXTemplateKind 1134 getEntityKindFromSymbolProperties(SymbolPropertySet K); 1135 static CXIdxEntityLanguage getEntityLangFromSymbolLang(SymbolLanguage L); 1136 1137 void CXIndexDataConsumer::getEntityInfo(const NamedDecl *D, 1138 EntityInfo &EntityInfo, 1139 ScratchAlloc &SA) { 1140 if (!D) 1141 return; 1142 1143 D = getEntityDecl(D); 1144 EntityInfo.cursor = getCursor(D); 1145 EntityInfo.Dcl = D; 1146 EntityInfo.IndexCtx = this; 1147 1148 SymbolInfo SymInfo = getSymbolInfo(D); 1149 EntityInfo.kind = getEntityKindFromSymbolKind(SymInfo.Kind, SymInfo.Lang); 1150 EntityInfo.templateKind = getEntityKindFromSymbolProperties(SymInfo.Properties); 1151 EntityInfo.lang = getEntityLangFromSymbolLang(SymInfo.Lang); 1152 1153 if (D->hasAttrs()) { 1154 EntityInfo.AttrList = AttrListInfo::create(D, *this); 1155 EntityInfo.attributes = EntityInfo.AttrList->getAttrs(); 1156 EntityInfo.numAttributes = EntityInfo.AttrList->getNumAttrs(); 1157 } 1158 1159 if (EntityInfo.kind == CXIdxEntity_Unexposed) 1160 return; 1161 1162 if (IdentifierInfo *II = D->getIdentifier()) { 1163 EntityInfo.name = SA.toCStr(II->getName()); 1164 1165 } else if (isa<TagDecl>(D) || isa<FieldDecl>(D) || isa<NamespaceDecl>(D)) { 1166 EntityInfo.name = nullptr; // anonymous tag/field/namespace. 1167 1168 } else { 1169 SmallString<256> StrBuf; 1170 { 1171 llvm::raw_svector_ostream OS(StrBuf); 1172 D->printName(OS); 1173 } 1174 EntityInfo.name = SA.copyCStr(StrBuf.str()); 1175 } 1176 1177 { 1178 SmallString<512> StrBuf; 1179 bool Ignore = getDeclCursorUSR(D, StrBuf); 1180 if (Ignore) { 1181 EntityInfo.USR = nullptr; 1182 } else { 1183 EntityInfo.USR = SA.copyCStr(StrBuf.str()); 1184 } 1185 } 1186 } 1187 1188 void CXIndexDataConsumer::getContainerInfo(const DeclContext *DC, 1189 ContainerInfo &ContInfo) { 1190 ContInfo.cursor = getCursor(cast<Decl>(DC)); 1191 ContInfo.DC = DC; 1192 ContInfo.IndexCtx = this; 1193 } 1194 1195 CXCursor CXIndexDataConsumer::getRefCursor(const NamedDecl *D, SourceLocation Loc) { 1196 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D)) 1197 return MakeCursorTypeRef(TD, Loc, CXTU); 1198 if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) 1199 return MakeCursorObjCClassRef(ID, Loc, CXTU); 1200 if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) 1201 return MakeCursorObjCProtocolRef(PD, Loc, CXTU); 1202 if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) 1203 return MakeCursorTemplateRef(Template, Loc, CXTU); 1204 if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(D)) 1205 return MakeCursorNamespaceRef(Namespace, Loc, CXTU); 1206 if (const NamespaceAliasDecl *Namespace = dyn_cast<NamespaceAliasDecl>(D)) 1207 return MakeCursorNamespaceRef(Namespace, Loc, CXTU); 1208 if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) 1209 return MakeCursorMemberRef(Field, Loc, CXTU); 1210 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) 1211 return MakeCursorVariableRef(Var, Loc, CXTU); 1212 1213 return clang_getNullCursor(); 1214 } 1215 1216 bool CXIndexDataConsumer::shouldIgnoreIfImplicit(const Decl *D) { 1217 if (isa<ObjCInterfaceDecl>(D)) 1218 return false; 1219 if (isa<ObjCCategoryDecl>(D)) 1220 return false; 1221 if (isa<ObjCIvarDecl>(D)) 1222 return false; 1223 if (isa<ObjCMethodDecl>(D)) 1224 return false; 1225 if (isa<ImportDecl>(D)) 1226 return false; 1227 return true; 1228 } 1229 1230 bool CXIndexDataConsumer::isTemplateImplicitInstantiation(const Decl *D) { 1231 if (const ClassTemplateSpecializationDecl * 1232 SD = dyn_cast<ClassTemplateSpecializationDecl>(D)) { 1233 return SD->getSpecializationKind() == TSK_ImplicitInstantiation; 1234 } 1235 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 1236 return FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation; 1237 } 1238 return false; 1239 } 1240 1241 static CXIdxEntityKind getEntityKindFromSymbolKind(SymbolKind K, SymbolLanguage Lang) { 1242 switch (K) { 1243 case SymbolKind::Unknown: 1244 case SymbolKind::Module: 1245 case SymbolKind::Macro: 1246 case SymbolKind::ClassProperty: 1247 case SymbolKind::Using: 1248 return CXIdxEntity_Unexposed; 1249 1250 case SymbolKind::Enum: return CXIdxEntity_Enum; 1251 case SymbolKind::Struct: return CXIdxEntity_Struct; 1252 case SymbolKind::Union: return CXIdxEntity_Union; 1253 case SymbolKind::TypeAlias: 1254 if (Lang == SymbolLanguage::CXX) 1255 return CXIdxEntity_CXXTypeAlias; 1256 return CXIdxEntity_Typedef; 1257 case SymbolKind::Function: return CXIdxEntity_Function; 1258 case SymbolKind::Variable: return CXIdxEntity_Variable; 1259 case SymbolKind::Field: 1260 if (Lang == SymbolLanguage::ObjC) 1261 return CXIdxEntity_ObjCIvar; 1262 return CXIdxEntity_Field; 1263 case SymbolKind::EnumConstant: return CXIdxEntity_EnumConstant; 1264 case SymbolKind::Class: 1265 if (Lang == SymbolLanguage::ObjC) 1266 return CXIdxEntity_ObjCClass; 1267 return CXIdxEntity_CXXClass; 1268 case SymbolKind::Protocol: 1269 if (Lang == SymbolLanguage::ObjC) 1270 return CXIdxEntity_ObjCProtocol; 1271 return CXIdxEntity_CXXInterface; 1272 case SymbolKind::Extension: return CXIdxEntity_ObjCCategory; 1273 case SymbolKind::InstanceMethod: 1274 if (Lang == SymbolLanguage::ObjC) 1275 return CXIdxEntity_ObjCInstanceMethod; 1276 return CXIdxEntity_CXXInstanceMethod; 1277 case SymbolKind::ClassMethod: return CXIdxEntity_ObjCClassMethod; 1278 case SymbolKind::StaticMethod: return CXIdxEntity_CXXStaticMethod; 1279 case SymbolKind::InstanceProperty: return CXIdxEntity_ObjCProperty; 1280 case SymbolKind::StaticProperty: return CXIdxEntity_CXXStaticVariable; 1281 case SymbolKind::Namespace: return CXIdxEntity_CXXNamespace; 1282 case SymbolKind::NamespaceAlias: return CXIdxEntity_CXXNamespaceAlias; 1283 case SymbolKind::Constructor: return CXIdxEntity_CXXConstructor; 1284 case SymbolKind::Destructor: return CXIdxEntity_CXXDestructor; 1285 case SymbolKind::ConversionFunction: return CXIdxEntity_CXXConversionFunction; 1286 case SymbolKind::Parameter: return CXIdxEntity_Variable; 1287 } 1288 llvm_unreachable("invalid symbol kind"); 1289 } 1290 1291 static CXIdxEntityCXXTemplateKind 1292 getEntityKindFromSymbolProperties(SymbolPropertySet K) { 1293 if (K & (SymbolPropertySet)SymbolProperty::TemplatePartialSpecialization) 1294 return CXIdxEntity_TemplatePartialSpecialization; 1295 if (K & (SymbolPropertySet)SymbolProperty::TemplateSpecialization) 1296 return CXIdxEntity_TemplateSpecialization; 1297 if (K & (SymbolPropertySet)SymbolProperty::Generic) 1298 return CXIdxEntity_Template; 1299 return CXIdxEntity_NonTemplate; 1300 } 1301 1302 static CXIdxEntityLanguage getEntityLangFromSymbolLang(SymbolLanguage L) { 1303 switch (L) { 1304 case SymbolLanguage::C: return CXIdxEntityLang_C; 1305 case SymbolLanguage::ObjC: return CXIdxEntityLang_ObjC; 1306 case SymbolLanguage::CXX: return CXIdxEntityLang_CXX; 1307 case SymbolLanguage::Swift: return CXIdxEntityLang_Swift; 1308 } 1309 llvm_unreachable("invalid symbol language"); 1310 } 1311