IndexingContext.cpp revision c10a4c8baff3164bee9b7fc293679a5a5a90eb74
1//===- CIndexHigh.cpp - Higher level API functions ------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10#include "IndexingContext.h" 11#include "CXTranslationUnit.h" 12#include "CIndexDiagnostic.h" 13 14#include "clang/Frontend/ASTUnit.h" 15#include "clang/AST/DeclCXX.h" 16#include "clang/AST/DeclTemplate.h" 17 18using namespace clang; 19using namespace cxindex; 20using namespace cxcursor; 21 22IndexingContext::ObjCProtocolListInfo::ObjCProtocolListInfo( 23 const ObjCProtocolList &ProtList, 24 IndexingContext &IdxCtx, 25 StrAdapter &SA) { 26 ObjCInterfaceDecl::protocol_loc_iterator LI = ProtList.loc_begin(); 27 for (ObjCInterfaceDecl::protocol_iterator 28 I = ProtList.begin(), E = ProtList.end(); I != E; ++I, ++LI) { 29 SourceLocation Loc = *LI; 30 ObjCProtocolDecl *PD = *I; 31 ProtEntities.push_back(EntityInfo()); 32 IdxCtx.getEntityInfo(PD, ProtEntities.back(), SA); 33 CXIdxObjCProtocolRefInfo ProtInfo = { 0, 34 MakeCursorObjCProtocolRef(PD, Loc, IdxCtx.CXTU), 35 IdxCtx.getIndexLoc(Loc) }; 36 ProtInfos.push_back(ProtInfo); 37 } 38 39 for (unsigned i = 0, e = ProtInfos.size(); i != e; ++i) 40 ProtInfos[i].protocol = &ProtEntities[i]; 41 42 for (unsigned i = 0, e = ProtInfos.size(); i != e; ++i) 43 Prots.push_back(&ProtInfos[i]); 44} 45 46IndexingContext::AttrListInfo::AttrListInfo(const Decl *D, 47 IndexingContext &IdxCtx, 48 StrAdapter &SA) { 49 for (AttrVec::const_iterator AttrI = D->attr_begin(), AttrE = D->attr_end(); 50 AttrI != AttrE; ++AttrI) { 51 const Attr *A = *AttrI; 52 CXCursor C = MakeCXCursor(A, const_cast<Decl *>(D), IdxCtx.CXTU); 53 CXIdxLoc Loc = IdxCtx.getIndexLoc(A->getLocation()); 54 switch (C.kind) { 55 default: 56 Attrs.push_back(AttrInfo(CXIdxAttr_Unexposed, C, Loc, A)); 57 break; 58 case CXCursor_IBActionAttr: 59 Attrs.push_back(AttrInfo(CXIdxAttr_IBAction, C, Loc, A)); 60 break; 61 case CXCursor_IBOutletAttr: 62 Attrs.push_back(AttrInfo(CXIdxAttr_IBOutlet, C, Loc, A)); 63 break; 64 case CXCursor_IBOutletCollectionAttr: 65 IBCollAttrs.push_back(IBOutletCollectionInfo(C, Loc, A)); 66 break; 67 } 68 } 69 70 for (unsigned i = 0, e = IBCollAttrs.size(); i != e; ++i) { 71 IBOutletCollectionInfo &IBInfo = IBCollAttrs[i]; 72 CXAttrs.push_back(&IBInfo); 73 74 const IBOutletCollectionAttr * 75 IBAttr = cast<IBOutletCollectionAttr>(IBInfo.A); 76 IBInfo.IBCollInfo.attrInfo = &IBInfo; 77 IBInfo.IBCollInfo.classLoc = IdxCtx.getIndexLoc(IBAttr->getInterfaceLoc()); 78 IBInfo.IBCollInfo.objcClass = 0; 79 IBInfo.IBCollInfo.classCursor = clang_getNullCursor(); 80 QualType Ty = IBAttr->getInterface(); 81 if (const ObjCInterfaceType *InterTy = Ty->getAs<ObjCInterfaceType>()) { 82 if (const ObjCInterfaceDecl *InterD = InterTy->getInterface()) { 83 IdxCtx.getEntityInfo(InterD, IBInfo.ClassInfo, SA); 84 IBInfo.IBCollInfo.objcClass = &IBInfo.ClassInfo; 85 IBInfo.IBCollInfo.classCursor = MakeCursorObjCClassRef(InterD, 86 IBAttr->getInterfaceLoc(), IdxCtx.CXTU); 87 } 88 } 89 } 90 91 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) 92 CXAttrs.push_back(&Attrs[i]); 93} 94 95IndexingContext::CXXBasesListInfo::CXXBasesListInfo(const CXXRecordDecl *D, 96 IndexingContext &IdxCtx, 97 IndexingContext::StrAdapter &SA) { 98 for (CXXRecordDecl::base_class_const_iterator 99 I = D->bases_begin(), E = D->bases_end(); I != E; ++I) { 100 const CXXBaseSpecifier &Base = *I; 101 BaseEntities.push_back(EntityInfo()); 102 const NamedDecl *BaseD = 0; 103 QualType T = Base.getType(); 104 SourceLocation Loc = getBaseLoc(Base); 105 106 if (const TypedefType *TDT = T->getAs<TypedefType>()) { 107 BaseD = TDT->getDecl(); 108 } else if (const TemplateSpecializationType * 109 TST = T->getAs<TemplateSpecializationType>()) { 110 BaseD = TST->getTemplateName().getAsTemplateDecl(); 111 } else if (const RecordType *RT = T->getAs<RecordType>()) { 112 BaseD = RT->getDecl(); 113 } 114 115 if (BaseD) 116 IdxCtx.getEntityInfo(BaseD, BaseEntities.back(), SA); 117 CXIdxBaseClassInfo BaseInfo = { 0, 118 MakeCursorCXXBaseSpecifier(&Base, IdxCtx.CXTU), 119 IdxCtx.getIndexLoc(Loc) }; 120 BaseInfos.push_back(BaseInfo); 121 } 122 123 for (unsigned i = 0, e = BaseInfos.size(); i != e; ++i) { 124 if (BaseEntities[i].name && BaseEntities[i].USR) 125 BaseInfos[i].base = &BaseEntities[i]; 126 } 127 128 for (unsigned i = 0, e = BaseInfos.size(); i != e; ++i) 129 CXBases.push_back(&BaseInfos[i]); 130} 131 132SourceLocation IndexingContext::CXXBasesListInfo::getBaseLoc( 133 const CXXBaseSpecifier &Base) const { 134 SourceLocation Loc = Base.getSourceRange().getBegin(); 135 TypeLoc TL; 136 if (Base.getTypeSourceInfo()) 137 TL = Base.getTypeSourceInfo()->getTypeLoc(); 138 if (TL.isNull()) 139 return Loc; 140 141 if (const QualifiedTypeLoc *QL = dyn_cast<QualifiedTypeLoc>(&TL)) 142 TL = QL->getUnqualifiedLoc(); 143 144 if (const ElaboratedTypeLoc *EL = dyn_cast<ElaboratedTypeLoc>(&TL)) 145 return EL->getNamedTypeLoc().getBeginLoc(); 146 if (const DependentNameTypeLoc *DL = dyn_cast<DependentNameTypeLoc>(&TL)) 147 return DL->getNameLoc(); 148 if (const DependentTemplateSpecializationTypeLoc * 149 DTL = dyn_cast<DependentTemplateSpecializationTypeLoc>(&TL)) 150 return DTL->getNameLoc(); 151 152 return Loc; 153} 154 155const char *IndexingContext::StrAdapter::toCStr(StringRef Str) { 156 if (Str.empty()) 157 return ""; 158 if (Str.data()[Str.size()] == '\0') 159 return Str.data(); 160 return copyCStr(Str); 161} 162 163const char *IndexingContext::StrAdapter::copyCStr(StringRef Str) { 164 char *buf = IdxCtx.StrScratch.Allocate<char>(Str.size() + 1); 165 std::uninitialized_copy(Str.begin(), Str.end(), buf); 166 buf[Str.size()] = '\0'; 167 return buf; 168} 169 170void IndexingContext::setASTContext(ASTContext &ctx) { 171 Ctx = &ctx; 172 static_cast<ASTUnit*>(CXTU->TUData)->setASTContext(&ctx); 173} 174 175bool IndexingContext::shouldAbort() { 176 if (!CB.abortQuery) 177 return false; 178 return CB.abortQuery(ClientData, 0); 179} 180 181void IndexingContext::enteredMainFile(const FileEntry *File) { 182 if (File && CB.enteredMainFile) { 183 CXIdxClientFile idxFile = CB.enteredMainFile(ClientData, (CXFile)File, 0); 184 FileMap[File] = idxFile; 185 } 186} 187 188void IndexingContext::ppIncludedFile(SourceLocation hashLoc, 189 StringRef filename, 190 const FileEntry *File, 191 bool isImport, bool isAngled) { 192 if (!CB.ppIncludedFile) 193 return; 194 195 StrAdapter SA(*this); 196 CXIdxIncludedFileInfo Info = { getIndexLoc(hashLoc), 197 SA.toCStr(filename), 198 (CXFile)File, 199 isImport, isAngled }; 200 CXIdxClientFile idxFile = CB.ppIncludedFile(ClientData, &Info); 201 FileMap[File] = idxFile; 202} 203 204void IndexingContext::startedTranslationUnit() { 205 CXIdxClientContainer idxCont = 0; 206 if (CB.startedTranslationUnit) 207 idxCont = CB.startedTranslationUnit(ClientData, 0); 208 addContainerInMap(Ctx->getTranslationUnitDecl(), idxCont); 209} 210 211void IndexingContext::handleDiagnosticSet(CXDiagnostic CXDiagSet) { 212 if (!CB.diagnostic) 213 return; 214 215 CB.diagnostic(ClientData, CXDiagSet, 0); 216} 217 218bool IndexingContext::handleDecl(const NamedDecl *D, 219 SourceLocation Loc, CXCursor Cursor, 220 DeclInfo &DInfo) { 221 if (!CB.indexDeclaration || !D) 222 return false; 223 if (D->isImplicit() && shouldIgnoreIfImplicit(D)) 224 return false; 225 226 StrAdapter SA(*this); 227 getEntityInfo(D, DInfo.EntInfo, SA); 228 if (!DInfo.EntInfo.USR || Loc.isInvalid()) 229 return false; 230 231 if (suppressRefs()) 232 markEntityOccurrenceInFile(D, Loc); 233 234 DInfo.entityInfo = &DInfo.EntInfo; 235 DInfo.cursor = Cursor; 236 DInfo.loc = getIndexLoc(Loc); 237 DInfo.isImplicit = D->isImplicit(); 238 239 AttrListInfo AttrList(D, *this, SA); 240 DInfo.attributes = AttrList.getAttrs(); 241 DInfo.numAttributes = AttrList.getNumAttrs(); 242 243 getContainerInfo(D->getDeclContext(), DInfo.SemanticContainer); 244 getContainerInfo(D->getLexicalDeclContext(), DInfo.LexicalContainer); 245 DInfo.semanticContainer = &DInfo.SemanticContainer; 246 DInfo.lexicalContainer = &DInfo.LexicalContainer; 247 if (DInfo.isContainer) { 248 getContainerInfo(getEntityContainer(D), DInfo.DeclAsContainer); 249 DInfo.declAsContainer = &DInfo.DeclAsContainer; 250 } 251 252 CB.indexDeclaration(ClientData, &DInfo); 253 return true; 254} 255 256bool IndexingContext::handleObjCContainer(const ObjCContainerDecl *D, 257 SourceLocation Loc, CXCursor Cursor, 258 ObjCContainerDeclInfo &ContDInfo) { 259 ContDInfo.ObjCContDeclInfo.declInfo = &ContDInfo; 260 return handleDecl(D, Loc, Cursor, ContDInfo); 261} 262 263bool IndexingContext::handleFunction(const FunctionDecl *D) { 264 DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(), 265 D->isThisDeclarationADefinition()); 266 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 267} 268 269bool IndexingContext::handleVar(const VarDecl *D) { 270 DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(), 271 /*isContainer=*/false); 272 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 273} 274 275bool IndexingContext::handleField(const FieldDecl *D) { 276 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true, 277 /*isContainer=*/false); 278 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 279} 280 281bool IndexingContext::handleEnumerator(const EnumConstantDecl *D) { 282 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true, 283 /*isContainer=*/false); 284 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 285} 286 287bool IndexingContext::handleTagDecl(const TagDecl *D) { 288 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(D)) 289 return handleCXXRecordDecl(CXXRD, D); 290 291 DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(), 292 D->isThisDeclarationADefinition()); 293 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 294} 295 296bool IndexingContext::handleTypedefName(const TypedefNameDecl *D) { 297 DeclInfo DInfo(!D->isFirstDeclaration(), /*isDefinition=*/true, 298 /*isContainer=*/false); 299 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 300} 301 302bool IndexingContext::handleObjCClass(const ObjCClassDecl *D) { 303 const ObjCClassDecl::ObjCClassRef *Ref = D->getForwardDecl(); 304 ObjCInterfaceDecl *IFaceD = Ref->getInterface(); 305 SourceLocation Loc = Ref->getLocation(); 306 bool isRedeclaration = IFaceD->getLocation() != Loc; 307 308 // For @class forward declarations, suppress them the same way as references. 309 if (suppressRefs()) { 310 if (markEntityOccurrenceInFile(IFaceD, Loc)) 311 return false; // already occurred. 312 } 313 314 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true, isRedeclaration, 315 /*isImplementation=*/false); 316 return handleObjCContainer(IFaceD, Loc, 317 MakeCursorObjCClassRef(IFaceD, Loc, CXTU), ContDInfo); 318} 319 320bool IndexingContext::handleObjCInterface(const ObjCInterfaceDecl *D) { 321 StrAdapter SA(*this); 322 323 CXIdxBaseClassInfo BaseClass; 324 EntityInfo BaseEntity; 325 BaseClass.cursor = clang_getNullCursor(); 326 if (ObjCInterfaceDecl *SuperD = D->getSuperClass()) { 327 getEntityInfo(SuperD, BaseEntity, SA); 328 SourceLocation SuperLoc = D->getSuperClassLoc(); 329 BaseClass.base = &BaseEntity; 330 BaseClass.cursor = MakeCursorObjCSuperClassRef(SuperD, SuperLoc, CXTU); 331 BaseClass.loc = getIndexLoc(SuperLoc); 332 } 333 334 ObjCProtocolListInfo ProtInfo(D->getReferencedProtocols(), *this, SA); 335 336 ObjCInterfaceDeclInfo InterInfo(D); 337 InterInfo.ObjCProtoListInfo = ProtInfo.getListInfo(); 338 InterInfo.ObjCInterDeclInfo.containerInfo = &InterInfo.ObjCContDeclInfo; 339 InterInfo.ObjCInterDeclInfo.superInfo = D->getSuperClass() ? &BaseClass : 0; 340 InterInfo.ObjCInterDeclInfo.protocols = &InterInfo.ObjCProtoListInfo; 341 342 return handleObjCContainer(D, D->getLocation(), getCursor(D), InterInfo); 343} 344 345bool IndexingContext::handleObjCImplementation( 346 const ObjCImplementationDecl *D) { 347 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/false, 348 /*isRedeclaration=*/true, 349 /*isImplementation=*/true); 350 return handleObjCContainer(D, D->getLocation(), getCursor(D), ContDInfo); 351} 352 353bool IndexingContext::handleObjCForwardProtocol(const ObjCProtocolDecl *D, 354 SourceLocation Loc, 355 bool isRedeclaration) { 356 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true, 357 isRedeclaration, 358 /*isImplementation=*/false); 359 return handleObjCContainer(D, Loc, MakeCursorObjCProtocolRef(D, Loc, CXTU), 360 ContDInfo); 361} 362 363bool IndexingContext::handleObjCProtocol(const ObjCProtocolDecl *D) { 364 StrAdapter SA(*this); 365 ObjCProtocolListInfo ProtListInfo(D->getReferencedProtocols(), *this, SA); 366 367 ObjCProtocolDeclInfo ProtInfo(D); 368 ProtInfo.ObjCProtoRefListInfo = ProtListInfo.getListInfo(); 369 370 return handleObjCContainer(D, D->getLocation(), getCursor(D), ProtInfo); 371} 372 373bool IndexingContext::handleObjCCategory(const ObjCCategoryDecl *D) { 374 ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/false); 375 EntityInfo ClassEntity; 376 StrAdapter SA(*this); 377 const ObjCInterfaceDecl *IFaceD = D->getClassInterface(); 378 SourceLocation ClassLoc = D->getLocation(); 379 SourceLocation CategoryLoc = D->IsClassExtension() ? ClassLoc 380 : D->getCategoryNameLoc(); 381 getEntityInfo(IFaceD, ClassEntity, SA); 382 383 if (suppressRefs()) 384 markEntityOccurrenceInFile(IFaceD, ClassLoc); 385 386 ObjCProtocolListInfo ProtInfo(D->getReferencedProtocols(), *this, SA); 387 388 CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo; 389 if (IFaceD) { 390 CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity; 391 CatDInfo.ObjCCatDeclInfo.classCursor = 392 MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU); 393 } else { 394 CatDInfo.ObjCCatDeclInfo.objcClass = 0; 395 CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor(); 396 } 397 CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc); 398 CatDInfo.ObjCProtoListInfo = ProtInfo.getListInfo(); 399 CatDInfo.ObjCCatDeclInfo.protocols = &CatDInfo.ObjCProtoListInfo; 400 401 return handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo); 402} 403 404bool IndexingContext::handleObjCCategoryImpl(const ObjCCategoryImplDecl *D) { 405 const ObjCCategoryDecl *CatD = D->getCategoryDecl(); 406 ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/true); 407 EntityInfo ClassEntity; 408 StrAdapter SA(*this); 409 const ObjCInterfaceDecl *IFaceD = CatD->getClassInterface(); 410 SourceLocation ClassLoc = D->getLocation(); 411 SourceLocation CategoryLoc = D->getCategoryNameLoc(); 412 getEntityInfo(IFaceD, ClassEntity, SA); 413 414 CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo; 415 if (IFaceD) { 416 CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity; 417 CatDInfo.ObjCCatDeclInfo.classCursor = 418 MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU); 419 } else { 420 CatDInfo.ObjCCatDeclInfo.objcClass = 0; 421 CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor(); 422 } 423 CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc); 424 CatDInfo.ObjCCatDeclInfo.protocols = 0; 425 426 return handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo); 427} 428 429bool IndexingContext::handleObjCMethod(const ObjCMethodDecl *D) { 430 DeclInfo DInfo(!D->isCanonicalDecl(), D->isThisDeclarationADefinition(), 431 D->isThisDeclarationADefinition()); 432 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 433} 434 435bool IndexingContext::handleSynthesizedObjCProperty( 436 const ObjCPropertyImplDecl *D) { 437 ObjCPropertyDecl *PD = D->getPropertyDecl(); 438 return handleReference(PD, D->getLocation(), getCursor(D), 0, D->getDeclContext()); 439} 440 441bool IndexingContext::handleSynthesizedObjCMethod(const ObjCMethodDecl *D, 442 SourceLocation Loc) { 443 DeclInfo DInfo(/*isRedeclaration=*/true, /*isDefinition=*/true, 444 /*isContainer=*/false); 445 return handleDecl(D, Loc, getCursor(D), DInfo); 446} 447 448bool IndexingContext::handleObjCProperty(const ObjCPropertyDecl *D) { 449 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/false, 450 /*isContainer=*/false); 451 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 452} 453 454bool IndexingContext::handleNamespace(const NamespaceDecl *D) { 455 DeclInfo DInfo(/*isRedeclaration=*/!D->isOriginalNamespace(), 456 /*isDefinition=*/true, 457 /*isContainer=*/true); 458 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 459} 460 461bool IndexingContext::handleClassTemplate(const ClassTemplateDecl *D) { 462 return handleCXXRecordDecl(D->getTemplatedDecl(), D); 463} 464 465bool IndexingContext::handleFunctionTemplate(const FunctionTemplateDecl *D) { 466 DeclInfo DInfo(/*isRedeclaration=*/!D->isCanonicalDecl(), 467 /*isDefinition=*/D->isThisDeclarationADefinition(), 468 /*isContainer=*/D->isThisDeclarationADefinition()); 469 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 470} 471 472bool IndexingContext::handleTypeAliasTemplate(const TypeAliasTemplateDecl *D) { 473 DeclInfo DInfo(/*isRedeclaration=*/!D->isCanonicalDecl(), 474 /*isDefinition=*/true, /*isContainer=*/false); 475 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 476} 477 478bool IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc, 479 const NamedDecl *Parent, 480 const DeclContext *DC, 481 const Expr *E, 482 CXIdxEntityRefKind Kind) { 483 if (!D) 484 return false; 485 486 CXCursor Cursor = E ? MakeCXCursor(const_cast<Expr*>(E), 487 const_cast<Decl*>(cast<Decl>(DC)), CXTU) 488 : getRefCursor(D, Loc); 489 return handleReference(D, Loc, Cursor, Parent, DC, E, Kind); 490} 491 492bool IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc, 493 CXCursor Cursor, 494 const NamedDecl *Parent, 495 const DeclContext *DC, 496 const Expr *E, 497 CXIdxEntityRefKind Kind) { 498 if (!CB.indexEntityReference) 499 return false; 500 501 if (!D) 502 return false; 503 if (Loc.isInvalid()) 504 return false; 505 if (D->getParentFunctionOrMethod()) 506 return false; 507 if (isNotFromSourceFile(D->getLocation())) 508 return false; 509 if (D->isImplicit() && shouldIgnoreIfImplicit(D)) 510 return false; 511 512 if (suppressRefs()) { 513 if (markEntityOccurrenceInFile(D, Loc)) 514 return false; // already occurred. 515 } 516 517 StrAdapter SA(*this); 518 EntityInfo RefEntity, ParentEntity; 519 getEntityInfo(D, RefEntity, SA); 520 if (!RefEntity.USR) 521 return false; 522 523 getEntityInfo(Parent, ParentEntity, SA); 524 525 ContainerInfo Container; 526 getContainerInfo(DC, Container); 527 528 CXIdxEntityRefInfo Info = { Kind, 529 Cursor, 530 getIndexLoc(Loc), 531 &RefEntity, 532 Parent ? &ParentEntity : 0, 533 &Container }; 534 CB.indexEntityReference(ClientData, &Info); 535 return true; 536} 537 538bool IndexingContext::isNotFromSourceFile(SourceLocation Loc) const { 539 if (Loc.isInvalid()) 540 return true; 541 SourceManager &SM = Ctx->getSourceManager(); 542 SourceLocation FileLoc = SM.getFileLoc(Loc); 543 FileID FID = SM.getFileID(FileLoc); 544 return SM.getFileEntryForID(FID) == 0; 545} 546 547void IndexingContext::addContainerInMap(const DeclContext *DC, 548 CXIdxClientContainer container) { 549 if (!DC) 550 return; 551 552 ContainerMapTy::iterator I = ContainerMap.find(DC); 553 if (I == ContainerMap.end()) { 554 if (container) 555 ContainerMap[DC] = container; 556 return; 557 } 558 // Allow changing the container of a previously seen DeclContext so we 559 // can handle invalid user code, like a function re-definition. 560 if (container) 561 I->second = container; 562 else 563 ContainerMap.erase(I); 564} 565 566CXIdxClientEntity IndexingContext::getClientEntity(const Decl *D) const { 567 if (!D) 568 return 0; 569 EntityMapTy::const_iterator I = EntityMap.find(D); 570 if (I == EntityMap.end()) 571 return 0; 572 return I->second; 573} 574 575void IndexingContext::setClientEntity(const Decl *D, CXIdxClientEntity client) { 576 if (!D) 577 return; 578 EntityMap[D] = client; 579} 580 581bool IndexingContext::handleCXXRecordDecl(const CXXRecordDecl *RD, 582 const NamedDecl *OrigD) { 583 if (RD->isThisDeclarationADefinition()) { 584 StrAdapter SA(*this); 585 CXXClassDeclInfo CXXDInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(), 586 /*isDefinition=*/RD->isThisDeclarationADefinition()); 587 CXXBasesListInfo BaseList(RD, *this, SA); 588 CXXDInfo.CXXClassInfo.declInfo = &CXXDInfo; 589 CXXDInfo.CXXClassInfo.bases = BaseList.getBases(); 590 CXXDInfo.CXXClassInfo.numBases = BaseList.getNumBases(); 591 592 return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), CXXDInfo); 593 } 594 595 DeclInfo DInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(), 596 /*isDefinition=*/RD->isThisDeclarationADefinition(), 597 /*isContainer=*/RD->isThisDeclarationADefinition()); 598 return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), DInfo); 599} 600 601bool IndexingContext::markEntityOccurrenceInFile(const NamedDecl *D, 602 SourceLocation Loc) { 603 if (!D || Loc.isInvalid()) 604 return true; 605 606 SourceManager &SM = Ctx->getSourceManager(); 607 D = getEntityDecl(D); 608 609 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SM.getFileLoc(Loc)); 610 FileID FID = LocInfo.first; 611 if (FID.isInvalid()) 612 return true; 613 614 const FileEntry *FE = SM.getFileEntryForID(FID); 615 if (!FE) 616 return true; 617 RefFileOccurence RefOccur(FE, D); 618 std::pair<llvm::DenseSet<RefFileOccurence>::iterator, bool> 619 res = RefFileOccurences.insert(RefOccur); 620 if (!res.second) 621 return true; // already in map. 622 623 return false; 624} 625 626const NamedDecl *IndexingContext::getEntityDecl(const NamedDecl *D) const { 627 assert(D); 628 D = cast<NamedDecl>(D->getCanonicalDecl()); 629 630 if (const ObjCImplementationDecl * 631 ImplD = dyn_cast<ObjCImplementationDecl>(D)) { 632 return getEntityDecl(ImplD->getClassInterface()); 633 634 } else if (const ObjCCategoryImplDecl * 635 CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) { 636 return getEntityDecl(CatImplD->getCategoryDecl()); 637 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 638 if (FunctionTemplateDecl *TemplD = FD->getDescribedFunctionTemplate()) 639 return getEntityDecl(TemplD); 640 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { 641 if (ClassTemplateDecl *TemplD = RD->getDescribedClassTemplate()) 642 return getEntityDecl(TemplD); 643 } 644 645 return D; 646} 647 648const DeclContext * 649IndexingContext::getEntityContainer(const Decl *D) const { 650 const DeclContext *DC = dyn_cast<DeclContext>(D); 651 if (DC) 652 return DC; 653 654 if (const ClassTemplateDecl *ClassTempl = dyn_cast<ClassTemplateDecl>(D)) { 655 DC = ClassTempl->getTemplatedDecl(); 656 } if (const FunctionTemplateDecl * 657 FuncTempl = dyn_cast<FunctionTemplateDecl>(D)) { 658 DC = FuncTempl->getTemplatedDecl(); 659 } 660 661 return DC; 662} 663 664CXIdxClientContainer 665IndexingContext::getClientContainerForDC(const DeclContext *DC) const { 666 if (!DC) 667 return 0; 668 669 ContainerMapTy::const_iterator I = ContainerMap.find(DC); 670 if (I == ContainerMap.end()) 671 return 0; 672 673 return I->second; 674} 675 676CXIdxClientFile IndexingContext::getIndexFile(const FileEntry *File) { 677 if (!File) 678 return 0; 679 680 FileMapTy::iterator FI = FileMap.find(File); 681 if (FI != FileMap.end()) 682 return FI->second; 683 684 return 0; 685} 686 687CXIdxLoc IndexingContext::getIndexLoc(SourceLocation Loc) const { 688 CXIdxLoc idxLoc = { {0, 0}, 0 }; 689 if (Loc.isInvalid()) 690 return idxLoc; 691 692 idxLoc.ptr_data[0] = (void*)this; 693 idxLoc.int_data = Loc.getRawEncoding(); 694 return idxLoc; 695} 696 697void IndexingContext::translateLoc(SourceLocation Loc, 698 CXIdxClientFile *indexFile, CXFile *file, 699 unsigned *line, unsigned *column, 700 unsigned *offset) { 701 if (Loc.isInvalid()) 702 return; 703 704 SourceManager &SM = Ctx->getSourceManager(); 705 Loc = SM.getFileLoc(Loc); 706 707 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 708 FileID FID = LocInfo.first; 709 unsigned FileOffset = LocInfo.second; 710 711 if (FID.isInvalid()) 712 return; 713 714 const FileEntry *FE = SM.getFileEntryForID(FID); 715 if (indexFile) 716 *indexFile = getIndexFile(FE); 717 if (file) 718 *file = (void *)FE; 719 if (line) 720 *line = SM.getLineNumber(FID, FileOffset); 721 if (column) 722 *column = SM.getColumnNumber(FID, FileOffset); 723 if (offset) 724 *offset = FileOffset; 725} 726 727void IndexingContext::getEntityInfo(const NamedDecl *D, 728 EntityInfo &EntityInfo, 729 StrAdapter &SA) { 730 if (!D) 731 return; 732 733 D = getEntityDecl(D); 734 EntityInfo.cursor = getCursor(D); 735 EntityInfo.Dcl = D; 736 EntityInfo.IndexCtx = this; 737 EntityInfo.kind = CXIdxEntity_Unexposed; 738 EntityInfo.templateKind = CXIdxEntity_NonTemplate; 739 EntityInfo.lang = CXIdxEntityLang_C; 740 741 if (const TagDecl *TD = dyn_cast<TagDecl>(D)) { 742 switch (TD->getTagKind()) { 743 case TTK_Struct: 744 EntityInfo.kind = CXIdxEntity_Struct; break; 745 case TTK_Union: 746 EntityInfo.kind = CXIdxEntity_Union; break; 747 case TTK_Class: 748 EntityInfo.kind = CXIdxEntity_CXXClass; 749 EntityInfo.lang = CXIdxEntityLang_CXX; 750 break; 751 case TTK_Enum: 752 EntityInfo.kind = CXIdxEntity_Enum; break; 753 } 754 755 if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D)) { 756 // FIXME: isPOD check is not sufficient, a POD can contain methods, 757 // we want a isCStructLike check. 758 if (CXXRec->hasDefinition() && !CXXRec->isPOD()) 759 EntityInfo.lang = CXIdxEntityLang_CXX; 760 } 761 762 if (isa<ClassTemplatePartialSpecializationDecl>(D)) { 763 EntityInfo.templateKind = CXIdxEntity_TemplatePartialSpecialization; 764 } else if (isa<ClassTemplateSpecializationDecl>(D)) { 765 EntityInfo.templateKind = CXIdxEntity_TemplateSpecialization; 766 } 767 768 } else { 769 switch (D->getKind()) { 770 case Decl::Typedef: 771 EntityInfo.kind = CXIdxEntity_Typedef; break; 772 case Decl::Function: 773 EntityInfo.kind = CXIdxEntity_Function; 774 break; 775 case Decl::Var: 776 EntityInfo.kind = CXIdxEntity_Variable; 777 if (isa<CXXRecordDecl>(D->getDeclContext())) { 778 EntityInfo.kind = CXIdxEntity_CXXStaticVariable; 779 EntityInfo.lang = CXIdxEntityLang_CXX; 780 } 781 break; 782 case Decl::Field: 783 EntityInfo.kind = CXIdxEntity_Field; 784 if (const CXXRecordDecl * 785 CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) { 786 // FIXME: isPOD check is not sufficient, a POD can contain methods, 787 // we want a isCStructLike check. 788 if (!CXXRec->isPOD()) 789 EntityInfo.lang = CXIdxEntityLang_CXX; 790 } 791 break; 792 case Decl::EnumConstant: 793 EntityInfo.kind = CXIdxEntity_EnumConstant; break; 794 case Decl::ObjCInterface: 795 EntityInfo.kind = CXIdxEntity_ObjCClass; 796 EntityInfo.lang = CXIdxEntityLang_ObjC; 797 break; 798 case Decl::ObjCProtocol: 799 EntityInfo.kind = CXIdxEntity_ObjCProtocol; 800 EntityInfo.lang = CXIdxEntityLang_ObjC; 801 break; 802 case Decl::ObjCCategory: 803 EntityInfo.kind = CXIdxEntity_ObjCCategory; 804 EntityInfo.lang = CXIdxEntityLang_ObjC; 805 break; 806 case Decl::ObjCMethod: 807 if (cast<ObjCMethodDecl>(D)->isInstanceMethod()) 808 EntityInfo.kind = CXIdxEntity_ObjCInstanceMethod; 809 else 810 EntityInfo.kind = CXIdxEntity_ObjCClassMethod; 811 EntityInfo.lang = CXIdxEntityLang_ObjC; 812 break; 813 case Decl::ObjCProperty: 814 EntityInfo.kind = CXIdxEntity_ObjCProperty; 815 EntityInfo.lang = CXIdxEntityLang_ObjC; 816 break; 817 case Decl::ObjCIvar: 818 EntityInfo.kind = CXIdxEntity_ObjCIvar; 819 EntityInfo.lang = CXIdxEntityLang_ObjC; 820 break; 821 case Decl::Namespace: 822 EntityInfo.kind = CXIdxEntity_CXXNamespace; 823 EntityInfo.lang = CXIdxEntityLang_CXX; 824 break; 825 case Decl::NamespaceAlias: 826 EntityInfo.kind = CXIdxEntity_CXXNamespaceAlias; 827 EntityInfo.lang = CXIdxEntityLang_CXX; 828 break; 829 case Decl::CXXConstructor: 830 EntityInfo.kind = CXIdxEntity_CXXConstructor; 831 EntityInfo.lang = CXIdxEntityLang_CXX; 832 break; 833 case Decl::CXXDestructor: 834 EntityInfo.kind = CXIdxEntity_CXXDestructor; 835 EntityInfo.lang = CXIdxEntityLang_CXX; 836 break; 837 case Decl::CXXConversion: 838 EntityInfo.kind = CXIdxEntity_CXXConversionFunction; 839 EntityInfo.lang = CXIdxEntityLang_CXX; 840 break; 841 case Decl::CXXMethod: { 842 const CXXMethodDecl *MD = cast<CXXMethodDecl>(D); 843 if (MD->isStatic()) 844 EntityInfo.kind = CXIdxEntity_CXXStaticMethod; 845 else 846 EntityInfo.kind = CXIdxEntity_CXXInstanceMethod; 847 EntityInfo.lang = CXIdxEntityLang_CXX; 848 break; 849 } 850 case Decl::ClassTemplate: 851 EntityInfo.kind = CXIdxEntity_CXXClass; 852 EntityInfo.templateKind = CXIdxEntity_Template; 853 break; 854 case Decl::FunctionTemplate: 855 EntityInfo.kind = CXIdxEntity_Function; 856 EntityInfo.templateKind = CXIdxEntity_Template; 857 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>( 858 cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) { 859 if (isa<CXXConstructorDecl>(MD)) 860 EntityInfo.kind = CXIdxEntity_CXXConstructor; 861 else if (isa<CXXDestructorDecl>(MD)) 862 EntityInfo.kind = CXIdxEntity_CXXDestructor; 863 else if (isa<CXXConversionDecl>(MD)) 864 EntityInfo.kind = CXIdxEntity_CXXConversionFunction; 865 else { 866 if (MD->isStatic()) 867 EntityInfo.kind = CXIdxEntity_CXXStaticMethod; 868 else 869 EntityInfo.kind = CXIdxEntity_CXXInstanceMethod; 870 } 871 } 872 break; 873 case Decl::TypeAliasTemplate: 874 EntityInfo.kind = CXIdxEntity_CXXTypeAlias; 875 EntityInfo.templateKind = CXIdxEntity_Template; 876 break; 877 case Decl::TypeAlias: 878 EntityInfo.kind = CXIdxEntity_CXXTypeAlias; 879 EntityInfo.lang = CXIdxEntityLang_CXX; 880 break; 881 default: 882 break; 883 } 884 } 885 886 if (EntityInfo.kind == CXIdxEntity_Unexposed) 887 return; 888 889 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 890 if (FD->getTemplatedKind() == 891 FunctionDecl::TK_FunctionTemplateSpecialization) 892 EntityInfo.templateKind = CXIdxEntity_TemplateSpecialization; 893 } 894 895 if (EntityInfo.templateKind != CXIdxEntity_NonTemplate) 896 EntityInfo.lang = CXIdxEntityLang_CXX; 897 898 if (IdentifierInfo *II = D->getIdentifier()) { 899 EntityInfo.name = SA.toCStr(II->getName()); 900 901 } else if (isa<TagDecl>(D) || isa<FieldDecl>(D) || isa<NamespaceDecl>(D)) { 902 EntityInfo.name = 0; // anonymous tag/field/namespace. 903 904 } else { 905 llvm::SmallString<256> StrBuf; 906 { 907 llvm::raw_svector_ostream OS(StrBuf); 908 D->printName(OS); 909 } 910 EntityInfo.name = SA.copyCStr(StrBuf.str()); 911 } 912 913 { 914 llvm::SmallString<512> StrBuf; 915 bool Ignore = getDeclCursorUSR(D, StrBuf); 916 if (Ignore) { 917 EntityInfo.USR = 0; 918 } else { 919 EntityInfo.USR = SA.copyCStr(StrBuf.str()); 920 } 921 } 922} 923 924void IndexingContext::getContainerInfo(const DeclContext *DC, 925 ContainerInfo &ContInfo) { 926 ContInfo.cursor = getCursor(cast<Decl>(DC)); 927 ContInfo.DC = DC; 928 ContInfo.IndexCtx = this; 929} 930 931CXCursor IndexingContext::getRefCursor(const NamedDecl *D, SourceLocation Loc) { 932 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D)) 933 return MakeCursorTypeRef(TD, Loc, CXTU); 934 if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) 935 return MakeCursorObjCClassRef(ID, Loc, CXTU); 936 if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) 937 return MakeCursorObjCProtocolRef(PD, Loc, CXTU); 938 if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) 939 return MakeCursorTemplateRef(Template, Loc, CXTU); 940 if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(D)) 941 return MakeCursorNamespaceRef(Namespace, Loc, CXTU); 942 if (const NamespaceAliasDecl *Namespace = dyn_cast<NamespaceAliasDecl>(D)) 943 return MakeCursorNamespaceRef(Namespace, Loc, CXTU); 944 if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) 945 return MakeCursorMemberRef(Field, Loc, CXTU); 946 947 return clang_getNullCursor(); 948} 949 950bool IndexingContext::shouldIgnoreIfImplicit(const NamedDecl *D) { 951 if (isa<ObjCInterfaceDecl>(D)) 952 return false; 953 if (isa<ObjCCategoryDecl>(D)) 954 return false; 955 if (isa<ObjCIvarDecl>(D)) 956 return false; 957 if (isa<ObjCMethodDecl>(D)) 958 return false; 959 return true; 960} 961