IndexingContext.cpp revision da5f2d2360ace0ea32e4f3194906be4899a76c7d
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 if (const RecordType *RT = Base.getType()->getAs<RecordType>()) 104 BaseD = RT->getDecl(); 105 else if (const TypedefType *TDT = Base.getType()->getAs<TypedefType>()) 106 BaseD = TDT->getDecl(); 107 if (BaseD) 108 IdxCtx.getEntityInfo(BaseD, BaseEntities.back(), SA); 109 CXIdxBaseClassInfo BaseInfo = { 0, 110 MakeCursorCXXBaseSpecifier(&Base, IdxCtx.CXTU), 111 IdxCtx.getIndexLoc(Base.getSourceRange().getBegin()) }; 112 BaseInfos.push_back(BaseInfo); 113 } 114 115 for (unsigned i = 0, e = BaseInfos.size(); i != e; ++i) { 116 if (BaseEntities[i].name && BaseEntities[i].USR) 117 BaseInfos[i].base = &BaseEntities[i]; 118 } 119 120 for (unsigned i = 0, e = BaseInfos.size(); i != e; ++i) 121 CXBases.push_back(&BaseInfos[i]); 122} 123 124const char *IndexingContext::StrAdapter::toCStr(StringRef Str) { 125 if (Str.empty()) 126 return ""; 127 if (Str.data()[Str.size()] == '\0') 128 return Str.data(); 129 return copyCStr(Str); 130} 131 132const char *IndexingContext::StrAdapter::copyCStr(StringRef Str) { 133 char *buf = IdxCtx.StrScratch.Allocate<char>(Str.size() + 1); 134 std::uninitialized_copy(Str.begin(), Str.end(), buf); 135 buf[Str.size()] = '\0'; 136 return buf; 137} 138 139void IndexingContext::setASTContext(ASTContext &ctx) { 140 Ctx = &ctx; 141 static_cast<ASTUnit*>(CXTU->TUData)->setASTContext(&ctx); 142} 143 144bool IndexingContext::shouldAbort() { 145 if (!CB.abortQuery) 146 return false; 147 return CB.abortQuery(ClientData, 0); 148} 149 150void IndexingContext::enteredMainFile(const FileEntry *File) { 151 if (File && CB.enteredMainFile) { 152 CXIdxClientFile idxFile = CB.enteredMainFile(ClientData, (CXFile)File, 0); 153 FileMap[File] = idxFile; 154 } 155} 156 157void IndexingContext::ppIncludedFile(SourceLocation hashLoc, 158 StringRef filename, 159 const FileEntry *File, 160 bool isImport, bool isAngled) { 161 if (!CB.ppIncludedFile) 162 return; 163 164 StrAdapter SA(*this); 165 CXIdxIncludedFileInfo Info = { getIndexLoc(hashLoc), 166 SA.toCStr(filename), 167 (CXFile)File, 168 isImport, isAngled }; 169 CXIdxClientFile idxFile = CB.ppIncludedFile(ClientData, &Info); 170 FileMap[File] = idxFile; 171} 172 173void IndexingContext::startedTranslationUnit() { 174 CXIdxClientContainer idxCont = 0; 175 if (CB.startedTranslationUnit) 176 idxCont = CB.startedTranslationUnit(ClientData, 0); 177 addContainerInMap(Ctx->getTranslationUnitDecl(), idxCont); 178} 179 180void IndexingContext::handleDiagnosticSet(CXDiagnostic CXDiagSet) { 181 if (!CB.diagnostic) 182 return; 183 184 CB.diagnostic(ClientData, CXDiagSet, 0); 185} 186 187bool IndexingContext::handleDecl(const NamedDecl *D, 188 SourceLocation Loc, CXCursor Cursor, 189 DeclInfo &DInfo) { 190 if (!CB.indexDeclaration || !D) 191 return false; 192 if (D->isImplicit() && shouldIgnoreIfImplicit(D)) 193 return false; 194 195 StrAdapter SA(*this); 196 getEntityInfo(D, DInfo.EntInfo, SA); 197 if (!DInfo.EntInfo.USR || Loc.isInvalid()) 198 return false; 199 200 markEntityOccurrenceInFile(D, Loc); 201 202 DInfo.entityInfo = &DInfo.EntInfo; 203 DInfo.cursor = Cursor; 204 DInfo.loc = getIndexLoc(Loc); 205 DInfo.isImplicit = D->isImplicit(); 206 207 AttrListInfo AttrList(D, *this, SA); 208 DInfo.attributes = AttrList.getAttrs(); 209 DInfo.numAttributes = AttrList.getNumAttrs(); 210 211 getContainerInfo(D->getDeclContext(), DInfo.Container); 212 DInfo.container = &DInfo.Container; 213 if (DInfo.isContainer) { 214 getContainerInfo(getEntityContainer(D), DInfo.DeclAsContainer); 215 DInfo.declAsContainer = &DInfo.DeclAsContainer; 216 } 217 218 CB.indexDeclaration(ClientData, &DInfo); 219 return true; 220} 221 222bool IndexingContext::handleObjCContainer(const ObjCContainerDecl *D, 223 SourceLocation Loc, CXCursor Cursor, 224 ObjCContainerDeclInfo &ContDInfo) { 225 ContDInfo.ObjCContDeclInfo.declInfo = &ContDInfo; 226 return handleDecl(D, Loc, Cursor, ContDInfo); 227} 228 229bool IndexingContext::handleFunction(const FunctionDecl *D) { 230 DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(), 231 D->isThisDeclarationADefinition()); 232 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 233} 234 235bool IndexingContext::handleVar(const VarDecl *D) { 236 DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(), 237 /*isContainer=*/false); 238 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 239} 240 241bool IndexingContext::handleField(const FieldDecl *D) { 242 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true, 243 /*isContainer=*/false); 244 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 245} 246 247bool IndexingContext::handleEnumerator(const EnumConstantDecl *D) { 248 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true, 249 /*isContainer=*/false); 250 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 251} 252 253bool IndexingContext::handleTagDecl(const TagDecl *D) { 254 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(D)) 255 return handleCXXRecordDecl(CXXRD, D); 256 257 DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(), 258 D->isThisDeclarationADefinition()); 259 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 260} 261 262bool IndexingContext::handleTypedefName(const TypedefNameDecl *D) { 263 DeclInfo DInfo(!D->isFirstDeclaration(), /*isDefinition=*/true, 264 /*isContainer=*/false); 265 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 266} 267 268bool IndexingContext::handleObjCClass(const ObjCClassDecl *D) { 269 const ObjCClassDecl::ObjCClassRef *Ref = D->getForwardDecl(); 270 ObjCInterfaceDecl *IFaceD = Ref->getInterface(); 271 SourceLocation Loc = Ref->getLocation(); 272 bool isRedeclaration = IFaceD->getLocation() != Loc; 273 274 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true, isRedeclaration, 275 /*isImplementation=*/false); 276 return handleObjCContainer(IFaceD, Loc, 277 MakeCursorObjCClassRef(IFaceD, Loc, CXTU), ContDInfo); 278} 279 280bool IndexingContext::handleObjCInterface(const ObjCInterfaceDecl *D) { 281 StrAdapter SA(*this); 282 283 CXIdxBaseClassInfo BaseClass; 284 EntityInfo BaseEntity; 285 BaseClass.cursor = clang_getNullCursor(); 286 if (ObjCInterfaceDecl *SuperD = D->getSuperClass()) { 287 getEntityInfo(SuperD, BaseEntity, SA); 288 SourceLocation SuperLoc = D->getSuperClassLoc(); 289 BaseClass.base = &BaseEntity; 290 BaseClass.cursor = MakeCursorObjCSuperClassRef(SuperD, SuperLoc, CXTU); 291 BaseClass.loc = getIndexLoc(SuperLoc); 292 } 293 294 ObjCProtocolListInfo ProtInfo(D->getReferencedProtocols(), *this, SA); 295 296 ObjCInterfaceDeclInfo InterInfo(D); 297 InterInfo.ObjCProtoListInfo = ProtInfo.getListInfo(); 298 InterInfo.ObjCInterDeclInfo.containerInfo = &InterInfo.ObjCContDeclInfo; 299 InterInfo.ObjCInterDeclInfo.superInfo = D->getSuperClass() ? &BaseClass : 0; 300 InterInfo.ObjCInterDeclInfo.protocols = &InterInfo.ObjCProtoListInfo; 301 302 return handleObjCContainer(D, D->getLocation(), getCursor(D), InterInfo); 303} 304 305bool IndexingContext::handleObjCImplementation( 306 const ObjCImplementationDecl *D) { 307 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/false, 308 /*isRedeclaration=*/true, 309 /*isImplementation=*/true); 310 return handleObjCContainer(D, D->getLocation(), getCursor(D), ContDInfo); 311} 312 313bool IndexingContext::handleObjCForwardProtocol(const ObjCProtocolDecl *D, 314 SourceLocation Loc, 315 bool isRedeclaration) { 316 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true, 317 isRedeclaration, 318 /*isImplementation=*/false); 319 return handleObjCContainer(D, Loc, MakeCursorObjCProtocolRef(D, Loc, CXTU), 320 ContDInfo); 321} 322 323bool IndexingContext::handleObjCProtocol(const ObjCProtocolDecl *D) { 324 StrAdapter SA(*this); 325 ObjCProtocolListInfo ProtListInfo(D->getReferencedProtocols(), *this, SA); 326 327 ObjCProtocolDeclInfo ProtInfo(D); 328 ProtInfo.ObjCProtoRefListInfo = ProtListInfo.getListInfo(); 329 330 return handleObjCContainer(D, D->getLocation(), getCursor(D), ProtInfo); 331} 332 333bool IndexingContext::handleObjCCategory(const ObjCCategoryDecl *D) { 334 ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/false); 335 EntityInfo ClassEntity; 336 StrAdapter SA(*this); 337 const ObjCInterfaceDecl *IFaceD = D->getClassInterface(); 338 SourceLocation ClassLoc = D->getLocation(); 339 SourceLocation CategoryLoc = D->IsClassExtension() ? ClassLoc 340 : D->getCategoryNameLoc(); 341 getEntityInfo(IFaceD, ClassEntity, SA); 342 343 CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo; 344 if (IFaceD) { 345 CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity; 346 CatDInfo.ObjCCatDeclInfo.classCursor = 347 MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU); 348 } else { 349 CatDInfo.ObjCCatDeclInfo.objcClass = 0; 350 CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor(); 351 } 352 CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc); 353 return handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo); 354} 355 356bool IndexingContext::handleObjCCategoryImpl(const ObjCCategoryImplDecl *D) { 357 const ObjCCategoryDecl *CatD = D->getCategoryDecl(); 358 ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/true); 359 EntityInfo ClassEntity; 360 StrAdapter SA(*this); 361 const ObjCInterfaceDecl *IFaceD = CatD->getClassInterface(); 362 SourceLocation ClassLoc = D->getLocation(); 363 SourceLocation CategoryLoc = ClassLoc; //FIXME: D->getCategoryNameLoc(); 364 getEntityInfo(IFaceD, ClassEntity, SA); 365 366 CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo; 367 if (IFaceD) { 368 CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity; 369 CatDInfo.ObjCCatDeclInfo.classCursor = 370 MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU); 371 } else { 372 CatDInfo.ObjCCatDeclInfo.objcClass = 0; 373 CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor(); 374 } 375 CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc); 376 return handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo); 377} 378 379bool IndexingContext::handleObjCMethod(const ObjCMethodDecl *D) { 380 DeclInfo DInfo(!D->isCanonicalDecl(), D->isThisDeclarationADefinition(), 381 D->isThisDeclarationADefinition()); 382 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 383} 384 385bool IndexingContext::handleSynthesizedObjCProperty( 386 const ObjCPropertyImplDecl *D) { 387 ObjCPropertyDecl *PD = D->getPropertyDecl(); 388 return handleReference(PD, D->getLocation(), getCursor(D), 0, D->getDeclContext()); 389} 390 391bool IndexingContext::handleSynthesizedObjCMethod(const ObjCMethodDecl *D, 392 SourceLocation Loc) { 393 DeclInfo DInfo(/*isRedeclaration=*/true, /*isDefinition=*/true, 394 /*isContainer=*/false); 395 return handleDecl(D, Loc, getCursor(D), DInfo); 396} 397 398bool IndexingContext::handleObjCProperty(const ObjCPropertyDecl *D) { 399 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/false, 400 /*isContainer=*/false); 401 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 402} 403 404bool IndexingContext::handleClassTemplate(const ClassTemplateDecl *D) { 405 return handleCXXRecordDecl(D->getTemplatedDecl(), D); 406} 407 408bool IndexingContext::handleFunctionTemplate(const FunctionTemplateDecl *D) { 409 DeclInfo DInfo(/*isRedeclaration=*/!D->isCanonicalDecl(), 410 /*isDefinition=*/D->isThisDeclarationADefinition(), 411 /*isContainer=*/D->isThisDeclarationADefinition()); 412 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 413} 414 415bool IndexingContext::handleTypeAliasTemplate(const TypeAliasTemplateDecl *D) { 416 DeclInfo DInfo(/*isRedeclaration=*/!D->isCanonicalDecl(), 417 /*isDefinition=*/true, /*isContainer=*/false); 418 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 419} 420 421bool IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc, 422 const NamedDecl *Parent, 423 const DeclContext *DC, 424 const Expr *E, 425 CXIdxEntityRefKind Kind) { 426 if (!D) 427 return false; 428 429 CXCursor Cursor = E ? MakeCXCursor(const_cast<Expr*>(E), 430 const_cast<Decl*>(cast<Decl>(DC)), CXTU) 431 : getRefCursor(D, Loc); 432 return handleReference(D, Loc, Cursor, Parent, DC, E, Kind); 433} 434 435bool IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc, 436 CXCursor Cursor, 437 const NamedDecl *Parent, 438 const DeclContext *DC, 439 const Expr *E, 440 CXIdxEntityRefKind Kind) { 441 if (!CB.indexEntityReference) 442 return false; 443 444 if (!D) 445 return false; 446 if (Loc.isInvalid()) 447 return false; 448 if (D->getParentFunctionOrMethod()) 449 return false; 450 if (isNotFromSourceFile(D->getLocation())) 451 return false; 452 if (D->isImplicit() && shouldIgnoreIfImplicit(D)) 453 return false; 454 455 if (suppressRefs()) { 456 if (markEntityOccurrenceInFile(D, Loc)) 457 return false; // already occurred. 458 } 459 460 StrAdapter SA(*this); 461 EntityInfo RefEntity, ParentEntity; 462 getEntityInfo(D, RefEntity, SA); 463 if (!RefEntity.USR) 464 return false; 465 466 getEntityInfo(Parent, ParentEntity, SA); 467 468 ContainerInfo Container; 469 getContainerInfo(DC, Container); 470 471 CXIdxEntityRefInfo Info = { Cursor, 472 getIndexLoc(Loc), 473 &RefEntity, 474 Parent ? &ParentEntity : 0, 475 &Container, 476 Kind }; 477 CB.indexEntityReference(ClientData, &Info); 478 return true; 479} 480 481bool IndexingContext::isNotFromSourceFile(SourceLocation Loc) const { 482 if (Loc.isInvalid()) 483 return true; 484 SourceManager &SM = Ctx->getSourceManager(); 485 SourceLocation FileLoc = SM.getFileLoc(Loc); 486 FileID FID = SM.getFileID(FileLoc); 487 return SM.getFileEntryForID(FID) == 0; 488} 489 490void IndexingContext::addContainerInMap(const DeclContext *DC, 491 CXIdxClientContainer container) { 492 if (!DC) 493 return; 494 495 assert(getScopedContext(DC) == DC); 496 ContainerMapTy::iterator I = ContainerMap.find(DC); 497 if (I == ContainerMap.end()) { 498 if (container) 499 ContainerMap[DC] = container; 500 return; 501 } 502 // Allow changing the container of a previously seen DeclContext so we 503 // can handle invalid user code, like a function re-definition. 504 if (container) 505 I->second = container; 506 else 507 ContainerMap.erase(I); 508} 509 510CXIdxClientEntity IndexingContext::getClientEntity(const Decl *D) const { 511 if (!D) 512 return 0; 513 EntityMapTy::const_iterator I = EntityMap.find(D); 514 if (I == EntityMap.end()) 515 return 0; 516 return I->second; 517} 518 519void IndexingContext::setClientEntity(const Decl *D, CXIdxClientEntity client) { 520 if (!D) 521 return; 522 EntityMap[D] = client; 523} 524 525bool IndexingContext::handleCXXRecordDecl(const CXXRecordDecl *RD, 526 const NamedDecl *OrigD) { 527 if (RD->isThisDeclarationADefinition()) { 528 StrAdapter SA(*this); 529 CXXClassDeclInfo CXXDInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(), 530 /*isDefinition=*/RD->isThisDeclarationADefinition()); 531 CXXBasesListInfo BaseList(RD, *this, SA); 532 CXXDInfo.CXXClassInfo.declInfo = &CXXDInfo; 533 CXXDInfo.CXXClassInfo.bases = BaseList.getBases(); 534 CXXDInfo.CXXClassInfo.numBases = BaseList.getNumBases(); 535 536 return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), CXXDInfo); 537 } 538 539 DeclInfo DInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(), 540 /*isDefinition=*/RD->isThisDeclarationADefinition(), 541 /*isContainer=*/RD->isThisDeclarationADefinition()); 542 return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), DInfo); 543} 544 545bool IndexingContext::markEntityOccurrenceInFile(const NamedDecl *D, 546 SourceLocation Loc) { 547 SourceManager &SM = Ctx->getSourceManager(); 548 D = getEntityDecl(D); 549 550 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 551 FileID FID = LocInfo.first; 552 if (FID.isInvalid()) 553 return true; 554 555 const FileEntry *FE = SM.getFileEntryForID(FID); 556 if (!FE) 557 return true; 558 RefFileOccurence RefOccur(FE, D); 559 std::pair<llvm::DenseSet<RefFileOccurence>::iterator, bool> 560 res = RefFileOccurences.insert(RefOccur); 561 if (!res.second) 562 return true; // already in map. 563 564 return false; 565} 566 567const NamedDecl *IndexingContext::getEntityDecl(const NamedDecl *D) const { 568 assert(D); 569 D = cast<NamedDecl>(D->getCanonicalDecl()); 570 571 if (const ObjCImplementationDecl * 572 ImplD = dyn_cast<ObjCImplementationDecl>(D)) { 573 return getEntityDecl(ImplD->getClassInterface()); 574 575 } else if (const ObjCCategoryImplDecl * 576 CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) { 577 return getEntityDecl(CatImplD->getCategoryDecl()); 578 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 579 if (FunctionTemplateDecl *TemplD = FD->getDescribedFunctionTemplate()) 580 return getEntityDecl(TemplD); 581 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { 582 if (ClassTemplateDecl *TemplD = RD->getDescribedClassTemplate()) 583 return getEntityDecl(TemplD); 584 } 585 586 return D; 587} 588 589const DeclContext * 590IndexingContext::getEntityContainer(const Decl *D) const { 591 const DeclContext *DC = dyn_cast<DeclContext>(D); 592 if (DC) 593 return DC; 594 595 if (const ClassTemplateDecl *ClassTempl = dyn_cast<ClassTemplateDecl>(D)) { 596 DC = ClassTempl->getTemplatedDecl(); 597 } if (const FunctionTemplateDecl * 598 FuncTempl = dyn_cast<FunctionTemplateDecl>(D)) { 599 DC = FuncTempl->getTemplatedDecl(); 600 } 601 602 return DC; 603} 604 605const DeclContext * 606IndexingContext::getScopedContext(const DeclContext *DC) const { 607 // Local contexts are ignored for indexing. 608 const DeclContext *FuncCtx = cast<Decl>(DC)->getParentFunctionOrMethod(); 609 if (FuncCtx) 610 return FuncCtx; 611 612 // We consider enums always scoped for indexing. 613 if (isa<TagDecl>(DC)) 614 return DC; 615 616 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) { 617 if (NS->isAnonymousNamespace()) 618 return getScopedContext(NS->getParent()); 619 return NS; 620 } 621 622 return DC->getRedeclContext(); 623} 624 625CXIdxClientContainer 626IndexingContext::getClientContainerForDC(const DeclContext *DC) const { 627 if (!DC) 628 return 0; 629 630 DC = getScopedContext(DC); 631 ContainerMapTy::const_iterator I = ContainerMap.find(DC); 632 if (I == ContainerMap.end()) 633 return 0; 634 635 return I->second; 636} 637 638CXIdxClientFile IndexingContext::getIndexFile(const FileEntry *File) { 639 if (!File) 640 return 0; 641 642 FileMapTy::iterator FI = FileMap.find(File); 643 if (FI != FileMap.end()) 644 return FI->second; 645 646 return 0; 647} 648 649CXIdxLoc IndexingContext::getIndexLoc(SourceLocation Loc) const { 650 CXIdxLoc idxLoc = { {0, 0}, 0 }; 651 if (Loc.isInvalid()) 652 return idxLoc; 653 654 idxLoc.ptr_data[0] = (void*)this; 655 idxLoc.int_data = Loc.getRawEncoding(); 656 return idxLoc; 657} 658 659void IndexingContext::translateLoc(SourceLocation Loc, 660 CXIdxClientFile *indexFile, CXFile *file, 661 unsigned *line, unsigned *column, 662 unsigned *offset) { 663 if (Loc.isInvalid()) 664 return; 665 666 SourceManager &SM = Ctx->getSourceManager(); 667 Loc = SM.getFileLoc(Loc); 668 669 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 670 FileID FID = LocInfo.first; 671 unsigned FileOffset = LocInfo.second; 672 673 if (FID.isInvalid()) 674 return; 675 676 const FileEntry *FE = SM.getFileEntryForID(FID); 677 if (indexFile) 678 *indexFile = getIndexFile(FE); 679 if (file) 680 *file = (void *)FE; 681 if (line) 682 *line = SM.getLineNumber(FID, FileOffset); 683 if (column) 684 *column = SM.getColumnNumber(FID, FileOffset); 685 if (offset) 686 *offset = FileOffset; 687} 688 689void IndexingContext::getEntityInfo(const NamedDecl *D, 690 EntityInfo &EntityInfo, 691 StrAdapter &SA) { 692 if (!D) 693 return; 694 695 D = getEntityDecl(D); 696 EntityInfo.cursor = getCursor(D); 697 EntityInfo.Dcl = D; 698 EntityInfo.IndexCtx = this; 699 EntityInfo.kind = CXIdxEntity_Unexposed; 700 EntityInfo.templateKind = CXIdxEntity_NonTemplate; 701 702 if (const TagDecl *TD = dyn_cast<TagDecl>(D)) { 703 switch (TD->getTagKind()) { 704 case TTK_Struct: 705 EntityInfo.kind = CXIdxEntity_Struct; break; 706 case TTK_Union: 707 EntityInfo.kind = CXIdxEntity_Union; break; 708 case TTK_Class: 709 EntityInfo.kind = CXIdxEntity_CXXClass; break; 710 case TTK_Enum: 711 EntityInfo.kind = CXIdxEntity_Enum; break; 712 } 713 714 if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D)) { 715 if (TD->getTagKind() == TTK_Struct && 716 CXXRec->hasDefinition() && !CXXRec->isPOD()) 717 EntityInfo.kind = CXIdxEntity_CXXClass; 718 } 719 720 if (isa<ClassTemplatePartialSpecializationDecl>(D)) { 721 EntityInfo.templateKind = CXIdxEntity_TemplatePartialSpecialization; 722 } else if (isa<ClassTemplateSpecializationDecl>(D)) { 723 EntityInfo.templateKind = CXIdxEntity_TemplateSpecialization; 724 } 725 726 } else { 727 switch (D->getKind()) { 728 case Decl::Typedef: 729 EntityInfo.kind = CXIdxEntity_Typedef; break; 730 case Decl::Function: 731 EntityInfo.kind = CXIdxEntity_Function; 732 break; 733 case Decl::Var: 734 EntityInfo.kind = CXIdxEntity_Variable; 735 if (isa<CXXRecordDecl>(D->getDeclContext())) 736 EntityInfo.kind = CXIdxEntity_CXXStaticVariable; 737 break; 738 case Decl::Field: 739 EntityInfo.kind = CXIdxEntity_Field; 740 if (const CXXRecordDecl * 741 CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) 742 if (!CXXRec->isPOD()) 743 EntityInfo.kind = CXIdxEntity_CXXInstanceVariable; 744 break; 745 case Decl::EnumConstant: 746 EntityInfo.kind = CXIdxEntity_EnumConstant; break; 747 case Decl::ObjCInterface: 748 EntityInfo.kind = CXIdxEntity_ObjCClass; break; 749 case Decl::ObjCProtocol: 750 EntityInfo.kind = CXIdxEntity_ObjCProtocol; break; 751 case Decl::ObjCCategory: 752 EntityInfo.kind = CXIdxEntity_ObjCCategory; break; 753 case Decl::ObjCMethod: 754 if (cast<ObjCMethodDecl>(D)->isInstanceMethod()) 755 EntityInfo.kind = CXIdxEntity_ObjCInstanceMethod; 756 else 757 EntityInfo.kind = CXIdxEntity_ObjCClassMethod; 758 break; 759 case Decl::ObjCProperty: 760 EntityInfo.kind = CXIdxEntity_ObjCProperty; break; 761 case Decl::ObjCIvar: 762 EntityInfo.kind = CXIdxEntity_ObjCIvar; break; 763 case Decl::Namespace: 764 EntityInfo.kind = CXIdxEntity_CXXNamespace; break; 765 case Decl::NamespaceAlias: 766 EntityInfo.kind = CXIdxEntity_CXXNamespaceAlias; break; 767 case Decl::CXXConstructor: 768 EntityInfo.kind = CXIdxEntity_CXXConstructor; break; 769 case Decl::CXXDestructor: 770 EntityInfo.kind = CXIdxEntity_CXXDestructor; break; 771 case Decl::CXXConversion: 772 EntityInfo.kind = CXIdxEntity_CXXConversionFunction; break; 773 case Decl::CXXMethod: { 774 const CXXMethodDecl *MD = cast<CXXMethodDecl>(D); 775 if (MD->isStatic()) 776 EntityInfo.kind = CXIdxEntity_CXXStaticMethod; 777 else 778 EntityInfo.kind = CXIdxEntity_CXXInstanceMethod; 779 break; 780 } 781 case Decl::ClassTemplate: 782 EntityInfo.kind = CXIdxEntity_CXXClass; 783 EntityInfo.templateKind = CXIdxEntity_Template; 784 break; 785 case Decl::FunctionTemplate: 786 EntityInfo.kind = CXIdxEntity_Function; 787 EntityInfo.templateKind = CXIdxEntity_Template; 788 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>( 789 cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) { 790 if (isa<CXXConstructorDecl>(MD)) 791 EntityInfo.kind = CXIdxEntity_CXXConstructor; 792 else if (isa<CXXDestructorDecl>(MD)) 793 EntityInfo.kind = CXIdxEntity_CXXDestructor; 794 else if (isa<CXXConversionDecl>(MD)) 795 EntityInfo.kind = CXIdxEntity_CXXConversionFunction; 796 else { 797 if (MD->isStatic()) 798 EntityInfo.kind = CXIdxEntity_CXXStaticMethod; 799 else 800 EntityInfo.kind = CXIdxEntity_CXXInstanceMethod; 801 } 802 } 803 break; 804 case Decl::TypeAliasTemplate: 805 EntityInfo.kind = CXIdxEntity_CXXTypeAlias; 806 EntityInfo.templateKind = CXIdxEntity_Template; 807 break; 808 case Decl::TypeAlias: 809 EntityInfo.kind = CXIdxEntity_CXXTypeAlias; break; 810 default: 811 break; 812 } 813 } 814 815 if (EntityInfo.kind == CXIdxEntity_Unexposed) 816 return; 817 818 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 819 if (FD->getTemplatedKind() == 820 FunctionDecl::TK_FunctionTemplateSpecialization) 821 EntityInfo.templateKind = CXIdxEntity_TemplateSpecialization; 822 } 823 824 if (IdentifierInfo *II = D->getIdentifier()) { 825 EntityInfo.name = SA.toCStr(II->getName()); 826 827 } else if (isa<TagDecl>(D) || isa<FieldDecl>(D) || isa<NamespaceDecl>(D)) { 828 EntityInfo.name = 0; // anonymous tag/field/namespace. 829 830 } else { 831 llvm::SmallString<256> StrBuf; 832 { 833 llvm::raw_svector_ostream OS(StrBuf); 834 D->printName(OS); 835 } 836 EntityInfo.name = SA.copyCStr(StrBuf.str()); 837 } 838 839 { 840 llvm::SmallString<512> StrBuf; 841 bool Ignore = getDeclCursorUSR(D, StrBuf); 842 if (Ignore) { 843 EntityInfo.USR = 0; 844 } else { 845 EntityInfo.USR = SA.copyCStr(StrBuf.str()); 846 } 847 } 848} 849 850void IndexingContext::getContainerInfo(const DeclContext *DC, 851 ContainerInfo &ContInfo) { 852 ContInfo.cursor = getCursor(cast<Decl>(DC)); 853 ContInfo.DC = DC; 854 ContInfo.IndexCtx = this; 855} 856 857CXCursor IndexingContext::getRefCursor(const NamedDecl *D, SourceLocation Loc) { 858 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D)) 859 return MakeCursorTypeRef(TD, Loc, CXTU); 860 if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) 861 return MakeCursorObjCClassRef(ID, Loc, CXTU); 862 if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) 863 return MakeCursorObjCProtocolRef(PD, Loc, CXTU); 864 if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) 865 return MakeCursorTemplateRef(Template, Loc, CXTU); 866 if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(D)) 867 return MakeCursorNamespaceRef(Namespace, Loc, CXTU); 868 if (const NamespaceAliasDecl *Namespace = dyn_cast<NamespaceAliasDecl>(D)) 869 return MakeCursorNamespaceRef(Namespace, Loc, CXTU); 870 if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) 871 return MakeCursorMemberRef(Field, Loc, CXTU); 872 873 return clang_getNullCursor(); 874} 875 876bool IndexingContext::shouldIgnoreIfImplicit(const NamedDecl *D) { 877 if (isa<ObjCInterfaceDecl>(D)) 878 return false; 879 if (isa<ObjCCategoryDecl>(D)) 880 return false; 881 if (isa<ObjCIvarDecl>(D)) 882 return false; 883 if (isa<ObjCMethodDecl>(D)) 884 return false; 885 return true; 886} 887