IndexingContext.cpp revision b526a871af40b84d9878eded54a181bf4003b376
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 markEntityOccurrenceInFile(D, Loc); 232 233 DInfo.entityInfo = &DInfo.EntInfo; 234 DInfo.cursor = Cursor; 235 DInfo.loc = getIndexLoc(Loc); 236 DInfo.isImplicit = D->isImplicit(); 237 238 AttrListInfo AttrList(D, *this, SA); 239 DInfo.attributes = AttrList.getAttrs(); 240 DInfo.numAttributes = AttrList.getNumAttrs(); 241 242 getContainerInfo(D->getDeclContext(), DInfo.Container); 243 DInfo.container = &DInfo.Container; 244 if (DInfo.isContainer) { 245 getContainerInfo(getEntityContainer(D), DInfo.DeclAsContainer); 246 DInfo.declAsContainer = &DInfo.DeclAsContainer; 247 } 248 249 CB.indexDeclaration(ClientData, &DInfo); 250 return true; 251} 252 253bool IndexingContext::handleObjCContainer(const ObjCContainerDecl *D, 254 SourceLocation Loc, CXCursor Cursor, 255 ObjCContainerDeclInfo &ContDInfo) { 256 ContDInfo.ObjCContDeclInfo.declInfo = &ContDInfo; 257 return handleDecl(D, Loc, Cursor, ContDInfo); 258} 259 260bool IndexingContext::handleFunction(const FunctionDecl *D) { 261 DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(), 262 D->isThisDeclarationADefinition()); 263 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 264} 265 266bool IndexingContext::handleVar(const VarDecl *D) { 267 DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(), 268 /*isContainer=*/false); 269 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 270} 271 272bool IndexingContext::handleField(const FieldDecl *D) { 273 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true, 274 /*isContainer=*/false); 275 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 276} 277 278bool IndexingContext::handleEnumerator(const EnumConstantDecl *D) { 279 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true, 280 /*isContainer=*/false); 281 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 282} 283 284bool IndexingContext::handleTagDecl(const TagDecl *D) { 285 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(D)) 286 return handleCXXRecordDecl(CXXRD, D); 287 288 DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(), 289 D->isThisDeclarationADefinition()); 290 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 291} 292 293bool IndexingContext::handleTypedefName(const TypedefNameDecl *D) { 294 DeclInfo DInfo(!D->isFirstDeclaration(), /*isDefinition=*/true, 295 /*isContainer=*/false); 296 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 297} 298 299bool IndexingContext::handleObjCClass(const ObjCClassDecl *D) { 300 const ObjCClassDecl::ObjCClassRef *Ref = D->getForwardDecl(); 301 ObjCInterfaceDecl *IFaceD = Ref->getInterface(); 302 SourceLocation Loc = Ref->getLocation(); 303 bool isRedeclaration = IFaceD->getLocation() != Loc; 304 305 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true, isRedeclaration, 306 /*isImplementation=*/false); 307 return handleObjCContainer(IFaceD, Loc, 308 MakeCursorObjCClassRef(IFaceD, Loc, CXTU), ContDInfo); 309} 310 311bool IndexingContext::handleObjCInterface(const ObjCInterfaceDecl *D) { 312 StrAdapter SA(*this); 313 314 CXIdxBaseClassInfo BaseClass; 315 EntityInfo BaseEntity; 316 BaseClass.cursor = clang_getNullCursor(); 317 if (ObjCInterfaceDecl *SuperD = D->getSuperClass()) { 318 getEntityInfo(SuperD, BaseEntity, SA); 319 SourceLocation SuperLoc = D->getSuperClassLoc(); 320 BaseClass.base = &BaseEntity; 321 BaseClass.cursor = MakeCursorObjCSuperClassRef(SuperD, SuperLoc, CXTU); 322 BaseClass.loc = getIndexLoc(SuperLoc); 323 } 324 325 ObjCProtocolListInfo ProtInfo(D->getReferencedProtocols(), *this, SA); 326 327 ObjCInterfaceDeclInfo InterInfo(D); 328 InterInfo.ObjCProtoListInfo = ProtInfo.getListInfo(); 329 InterInfo.ObjCInterDeclInfo.containerInfo = &InterInfo.ObjCContDeclInfo; 330 InterInfo.ObjCInterDeclInfo.superInfo = D->getSuperClass() ? &BaseClass : 0; 331 InterInfo.ObjCInterDeclInfo.protocols = &InterInfo.ObjCProtoListInfo; 332 333 return handleObjCContainer(D, D->getLocation(), getCursor(D), InterInfo); 334} 335 336bool IndexingContext::handleObjCImplementation( 337 const ObjCImplementationDecl *D) { 338 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/false, 339 /*isRedeclaration=*/true, 340 /*isImplementation=*/true); 341 return handleObjCContainer(D, D->getLocation(), getCursor(D), ContDInfo); 342} 343 344bool IndexingContext::handleObjCForwardProtocol(const ObjCProtocolDecl *D, 345 SourceLocation Loc, 346 bool isRedeclaration) { 347 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true, 348 isRedeclaration, 349 /*isImplementation=*/false); 350 return handleObjCContainer(D, Loc, MakeCursorObjCProtocolRef(D, Loc, CXTU), 351 ContDInfo); 352} 353 354bool IndexingContext::handleObjCProtocol(const ObjCProtocolDecl *D) { 355 StrAdapter SA(*this); 356 ObjCProtocolListInfo ProtListInfo(D->getReferencedProtocols(), *this, SA); 357 358 ObjCProtocolDeclInfo ProtInfo(D); 359 ProtInfo.ObjCProtoRefListInfo = ProtListInfo.getListInfo(); 360 361 return handleObjCContainer(D, D->getLocation(), getCursor(D), ProtInfo); 362} 363 364bool IndexingContext::handleObjCCategory(const ObjCCategoryDecl *D) { 365 ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/false); 366 EntityInfo ClassEntity; 367 StrAdapter SA(*this); 368 const ObjCInterfaceDecl *IFaceD = D->getClassInterface(); 369 SourceLocation ClassLoc = D->getLocation(); 370 SourceLocation CategoryLoc = D->IsClassExtension() ? ClassLoc 371 : D->getCategoryNameLoc(); 372 getEntityInfo(IFaceD, ClassEntity, SA); 373 374 CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo; 375 if (IFaceD) { 376 CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity; 377 CatDInfo.ObjCCatDeclInfo.classCursor = 378 MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU); 379 } else { 380 CatDInfo.ObjCCatDeclInfo.objcClass = 0; 381 CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor(); 382 } 383 CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc); 384 return handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo); 385} 386 387bool IndexingContext::handleObjCCategoryImpl(const ObjCCategoryImplDecl *D) { 388 const ObjCCategoryDecl *CatD = D->getCategoryDecl(); 389 ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/true); 390 EntityInfo ClassEntity; 391 StrAdapter SA(*this); 392 const ObjCInterfaceDecl *IFaceD = CatD->getClassInterface(); 393 SourceLocation ClassLoc = D->getLocation(); 394 SourceLocation CategoryLoc = ClassLoc; //FIXME: D->getCategoryNameLoc(); 395 getEntityInfo(IFaceD, ClassEntity, SA); 396 397 CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo; 398 if (IFaceD) { 399 CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity; 400 CatDInfo.ObjCCatDeclInfo.classCursor = 401 MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU); 402 } else { 403 CatDInfo.ObjCCatDeclInfo.objcClass = 0; 404 CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor(); 405 } 406 CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc); 407 return handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo); 408} 409 410bool IndexingContext::handleObjCMethod(const ObjCMethodDecl *D) { 411 DeclInfo DInfo(!D->isCanonicalDecl(), D->isThisDeclarationADefinition(), 412 D->isThisDeclarationADefinition()); 413 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 414} 415 416bool IndexingContext::handleSynthesizedObjCProperty( 417 const ObjCPropertyImplDecl *D) { 418 ObjCPropertyDecl *PD = D->getPropertyDecl(); 419 return handleReference(PD, D->getLocation(), getCursor(D), 0, D->getDeclContext()); 420} 421 422bool IndexingContext::handleSynthesizedObjCMethod(const ObjCMethodDecl *D, 423 SourceLocation Loc) { 424 DeclInfo DInfo(/*isRedeclaration=*/true, /*isDefinition=*/true, 425 /*isContainer=*/false); 426 return handleDecl(D, Loc, getCursor(D), DInfo); 427} 428 429bool IndexingContext::handleObjCProperty(const ObjCPropertyDecl *D) { 430 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/false, 431 /*isContainer=*/false); 432 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 433} 434 435bool IndexingContext::handleNamespace(const NamespaceDecl *D) { 436 DeclInfo DInfo(/*isRedeclaration=*/!D->isOriginalNamespace(), 437 /*isDefinition=*/true, 438 /*isContainer=*/true); 439 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 440} 441 442bool IndexingContext::handleClassTemplate(const ClassTemplateDecl *D) { 443 return handleCXXRecordDecl(D->getTemplatedDecl(), D); 444} 445 446bool IndexingContext::handleFunctionTemplate(const FunctionTemplateDecl *D) { 447 DeclInfo DInfo(/*isRedeclaration=*/!D->isCanonicalDecl(), 448 /*isDefinition=*/D->isThisDeclarationADefinition(), 449 /*isContainer=*/D->isThisDeclarationADefinition()); 450 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 451} 452 453bool IndexingContext::handleTypeAliasTemplate(const TypeAliasTemplateDecl *D) { 454 DeclInfo DInfo(/*isRedeclaration=*/!D->isCanonicalDecl(), 455 /*isDefinition=*/true, /*isContainer=*/false); 456 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 457} 458 459bool IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc, 460 const NamedDecl *Parent, 461 const DeclContext *DC, 462 const Expr *E, 463 CXIdxEntityRefKind Kind) { 464 if (!D) 465 return false; 466 467 CXCursor Cursor = E ? MakeCXCursor(const_cast<Expr*>(E), 468 const_cast<Decl*>(cast<Decl>(DC)), CXTU) 469 : getRefCursor(D, Loc); 470 return handleReference(D, Loc, Cursor, Parent, DC, E, Kind); 471} 472 473bool IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc, 474 CXCursor Cursor, 475 const NamedDecl *Parent, 476 const DeclContext *DC, 477 const Expr *E, 478 CXIdxEntityRefKind Kind) { 479 if (!CB.indexEntityReference) 480 return false; 481 482 if (!D) 483 return false; 484 if (Loc.isInvalid()) 485 return false; 486 if (D->getParentFunctionOrMethod()) 487 return false; 488 if (isNotFromSourceFile(D->getLocation())) 489 return false; 490 if (D->isImplicit() && shouldIgnoreIfImplicit(D)) 491 return false; 492 493 if (suppressRefs()) { 494 if (markEntityOccurrenceInFile(D, Loc)) 495 return false; // already occurred. 496 } 497 498 StrAdapter SA(*this); 499 EntityInfo RefEntity, ParentEntity; 500 getEntityInfo(D, RefEntity, SA); 501 if (!RefEntity.USR) 502 return false; 503 504 getEntityInfo(Parent, ParentEntity, SA); 505 506 ContainerInfo Container; 507 getContainerInfo(DC, Container); 508 509 CXIdxEntityRefInfo Info = { Cursor, 510 getIndexLoc(Loc), 511 &RefEntity, 512 Parent ? &ParentEntity : 0, 513 &Container, 514 Kind }; 515 CB.indexEntityReference(ClientData, &Info); 516 return true; 517} 518 519bool IndexingContext::isNotFromSourceFile(SourceLocation Loc) const { 520 if (Loc.isInvalid()) 521 return true; 522 SourceManager &SM = Ctx->getSourceManager(); 523 SourceLocation FileLoc = SM.getFileLoc(Loc); 524 FileID FID = SM.getFileID(FileLoc); 525 return SM.getFileEntryForID(FID) == 0; 526} 527 528void IndexingContext::addContainerInMap(const DeclContext *DC, 529 CXIdxClientContainer container) { 530 if (!DC) 531 return; 532 533 assert(getScopedContext(DC) == DC); 534 ContainerMapTy::iterator I = ContainerMap.find(DC); 535 if (I == ContainerMap.end()) { 536 if (container) 537 ContainerMap[DC] = container; 538 return; 539 } 540 // Allow changing the container of a previously seen DeclContext so we 541 // can handle invalid user code, like a function re-definition. 542 if (container) 543 I->second = container; 544 else 545 ContainerMap.erase(I); 546} 547 548CXIdxClientEntity IndexingContext::getClientEntity(const Decl *D) const { 549 if (!D) 550 return 0; 551 EntityMapTy::const_iterator I = EntityMap.find(D); 552 if (I == EntityMap.end()) 553 return 0; 554 return I->second; 555} 556 557void IndexingContext::setClientEntity(const Decl *D, CXIdxClientEntity client) { 558 if (!D) 559 return; 560 EntityMap[D] = client; 561} 562 563bool IndexingContext::handleCXXRecordDecl(const CXXRecordDecl *RD, 564 const NamedDecl *OrigD) { 565 if (RD->isThisDeclarationADefinition()) { 566 StrAdapter SA(*this); 567 CXXClassDeclInfo CXXDInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(), 568 /*isDefinition=*/RD->isThisDeclarationADefinition()); 569 CXXBasesListInfo BaseList(RD, *this, SA); 570 CXXDInfo.CXXClassInfo.declInfo = &CXXDInfo; 571 CXXDInfo.CXXClassInfo.bases = BaseList.getBases(); 572 CXXDInfo.CXXClassInfo.numBases = BaseList.getNumBases(); 573 574 return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), CXXDInfo); 575 } 576 577 DeclInfo DInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(), 578 /*isDefinition=*/RD->isThisDeclarationADefinition(), 579 /*isContainer=*/RD->isThisDeclarationADefinition()); 580 return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), DInfo); 581} 582 583bool IndexingContext::markEntityOccurrenceInFile(const NamedDecl *D, 584 SourceLocation Loc) { 585 SourceManager &SM = Ctx->getSourceManager(); 586 D = getEntityDecl(D); 587 588 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 589 FileID FID = LocInfo.first; 590 if (FID.isInvalid()) 591 return true; 592 593 const FileEntry *FE = SM.getFileEntryForID(FID); 594 if (!FE) 595 return true; 596 RefFileOccurence RefOccur(FE, D); 597 std::pair<llvm::DenseSet<RefFileOccurence>::iterator, bool> 598 res = RefFileOccurences.insert(RefOccur); 599 if (!res.second) 600 return true; // already in map. 601 602 return false; 603} 604 605const NamedDecl *IndexingContext::getEntityDecl(const NamedDecl *D) const { 606 assert(D); 607 D = cast<NamedDecl>(D->getCanonicalDecl()); 608 609 if (const ObjCImplementationDecl * 610 ImplD = dyn_cast<ObjCImplementationDecl>(D)) { 611 return getEntityDecl(ImplD->getClassInterface()); 612 613 } else if (const ObjCCategoryImplDecl * 614 CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) { 615 return getEntityDecl(CatImplD->getCategoryDecl()); 616 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 617 if (FunctionTemplateDecl *TemplD = FD->getDescribedFunctionTemplate()) 618 return getEntityDecl(TemplD); 619 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { 620 if (ClassTemplateDecl *TemplD = RD->getDescribedClassTemplate()) 621 return getEntityDecl(TemplD); 622 } 623 624 return D; 625} 626 627const DeclContext * 628IndexingContext::getEntityContainer(const Decl *D) const { 629 const DeclContext *DC = dyn_cast<DeclContext>(D); 630 if (DC) 631 return DC; 632 633 if (const ClassTemplateDecl *ClassTempl = dyn_cast<ClassTemplateDecl>(D)) { 634 DC = ClassTempl->getTemplatedDecl(); 635 } if (const FunctionTemplateDecl * 636 FuncTempl = dyn_cast<FunctionTemplateDecl>(D)) { 637 DC = FuncTempl->getTemplatedDecl(); 638 } 639 640 return DC; 641} 642 643const DeclContext * 644IndexingContext::getScopedContext(const DeclContext *DC) const { 645 // Local contexts are ignored for indexing. 646 const DeclContext *FuncCtx = cast<Decl>(DC)->getParentFunctionOrMethod(); 647 if (FuncCtx) 648 return FuncCtx; 649 650 // We consider enums always scoped for indexing. 651 if (isa<TagDecl>(DC)) 652 return DC; 653 654 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) { 655 if (NS->isAnonymousNamespace()) 656 return getScopedContext(NS->getParent()); 657 return NS; 658 } 659 660 return DC->getRedeclContext(); 661} 662 663CXIdxClientContainer 664IndexingContext::getClientContainerForDC(const DeclContext *DC) const { 665 if (!DC) 666 return 0; 667 668 DC = getScopedContext(DC); 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