IndexingContext.cpp revision b1febb646bf7a2f319ad894c9833968c52d21711
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.SemanticContainer); 243 getContainerInfo(D->getLexicalDeclContext(), DInfo.LexicalContainer); 244 DInfo.semanticContainer = &DInfo.SemanticContainer; 245 DInfo.lexicalContainer = &DInfo.LexicalContainer; 246 if (DInfo.isContainer) { 247 getContainerInfo(getEntityContainer(D), DInfo.DeclAsContainer); 248 DInfo.declAsContainer = &DInfo.DeclAsContainer; 249 } 250 251 CB.indexDeclaration(ClientData, &DInfo); 252 return true; 253} 254 255bool IndexingContext::handleObjCContainer(const ObjCContainerDecl *D, 256 SourceLocation Loc, CXCursor Cursor, 257 ObjCContainerDeclInfo &ContDInfo) { 258 ContDInfo.ObjCContDeclInfo.declInfo = &ContDInfo; 259 return handleDecl(D, Loc, Cursor, ContDInfo); 260} 261 262bool IndexingContext::handleFunction(const FunctionDecl *D) { 263 DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(), 264 D->isThisDeclarationADefinition()); 265 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 266} 267 268bool IndexingContext::handleVar(const VarDecl *D) { 269 DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(), 270 /*isContainer=*/false); 271 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 272} 273 274bool IndexingContext::handleField(const FieldDecl *D) { 275 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true, 276 /*isContainer=*/false); 277 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 278} 279 280bool IndexingContext::handleEnumerator(const EnumConstantDecl *D) { 281 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true, 282 /*isContainer=*/false); 283 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 284} 285 286bool IndexingContext::handleTagDecl(const TagDecl *D) { 287 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(D)) 288 return handleCXXRecordDecl(CXXRD, D); 289 290 DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(), 291 D->isThisDeclarationADefinition()); 292 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 293} 294 295bool IndexingContext::handleTypedefName(const TypedefNameDecl *D) { 296 DeclInfo DInfo(!D->isFirstDeclaration(), /*isDefinition=*/true, 297 /*isContainer=*/false); 298 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 299} 300 301bool IndexingContext::handleObjCClass(const ObjCClassDecl *D) { 302 const ObjCClassDecl::ObjCClassRef *Ref = D->getForwardDecl(); 303 ObjCInterfaceDecl *IFaceD = Ref->getInterface(); 304 SourceLocation Loc = Ref->getLocation(); 305 bool isRedeclaration = IFaceD->getLocation() != Loc; 306 307 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true, isRedeclaration, 308 /*isImplementation=*/false); 309 return handleObjCContainer(IFaceD, Loc, 310 MakeCursorObjCClassRef(IFaceD, Loc, CXTU), ContDInfo); 311} 312 313bool IndexingContext::handleObjCInterface(const ObjCInterfaceDecl *D) { 314 StrAdapter SA(*this); 315 316 CXIdxBaseClassInfo BaseClass; 317 EntityInfo BaseEntity; 318 BaseClass.cursor = clang_getNullCursor(); 319 if (ObjCInterfaceDecl *SuperD = D->getSuperClass()) { 320 getEntityInfo(SuperD, BaseEntity, SA); 321 SourceLocation SuperLoc = D->getSuperClassLoc(); 322 BaseClass.base = &BaseEntity; 323 BaseClass.cursor = MakeCursorObjCSuperClassRef(SuperD, SuperLoc, CXTU); 324 BaseClass.loc = getIndexLoc(SuperLoc); 325 } 326 327 ObjCProtocolListInfo ProtInfo(D->getReferencedProtocols(), *this, SA); 328 329 ObjCInterfaceDeclInfo InterInfo(D); 330 InterInfo.ObjCProtoListInfo = ProtInfo.getListInfo(); 331 InterInfo.ObjCInterDeclInfo.containerInfo = &InterInfo.ObjCContDeclInfo; 332 InterInfo.ObjCInterDeclInfo.superInfo = D->getSuperClass() ? &BaseClass : 0; 333 InterInfo.ObjCInterDeclInfo.protocols = &InterInfo.ObjCProtoListInfo; 334 335 return handleObjCContainer(D, D->getLocation(), getCursor(D), InterInfo); 336} 337 338bool IndexingContext::handleObjCImplementation( 339 const ObjCImplementationDecl *D) { 340 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/false, 341 /*isRedeclaration=*/true, 342 /*isImplementation=*/true); 343 return handleObjCContainer(D, D->getLocation(), getCursor(D), ContDInfo); 344} 345 346bool IndexingContext::handleObjCForwardProtocol(const ObjCProtocolDecl *D, 347 SourceLocation Loc, 348 bool isRedeclaration) { 349 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true, 350 isRedeclaration, 351 /*isImplementation=*/false); 352 return handleObjCContainer(D, Loc, MakeCursorObjCProtocolRef(D, Loc, CXTU), 353 ContDInfo); 354} 355 356bool IndexingContext::handleObjCProtocol(const ObjCProtocolDecl *D) { 357 StrAdapter SA(*this); 358 ObjCProtocolListInfo ProtListInfo(D->getReferencedProtocols(), *this, SA); 359 360 ObjCProtocolDeclInfo ProtInfo(D); 361 ProtInfo.ObjCProtoRefListInfo = ProtListInfo.getListInfo(); 362 363 return handleObjCContainer(D, D->getLocation(), getCursor(D), ProtInfo); 364} 365 366bool IndexingContext::handleObjCCategory(const ObjCCategoryDecl *D) { 367 ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/false); 368 EntityInfo ClassEntity; 369 StrAdapter SA(*this); 370 const ObjCInterfaceDecl *IFaceD = D->getClassInterface(); 371 SourceLocation ClassLoc = D->getLocation(); 372 SourceLocation CategoryLoc = D->IsClassExtension() ? ClassLoc 373 : D->getCategoryNameLoc(); 374 getEntityInfo(IFaceD, ClassEntity, SA); 375 376 CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo; 377 if (IFaceD) { 378 CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity; 379 CatDInfo.ObjCCatDeclInfo.classCursor = 380 MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU); 381 } else { 382 CatDInfo.ObjCCatDeclInfo.objcClass = 0; 383 CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor(); 384 } 385 CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc); 386 return handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo); 387} 388 389bool IndexingContext::handleObjCCategoryImpl(const ObjCCategoryImplDecl *D) { 390 const ObjCCategoryDecl *CatD = D->getCategoryDecl(); 391 ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/true); 392 EntityInfo ClassEntity; 393 StrAdapter SA(*this); 394 const ObjCInterfaceDecl *IFaceD = CatD->getClassInterface(); 395 SourceLocation ClassLoc = D->getLocation(); 396 SourceLocation CategoryLoc = ClassLoc; //FIXME: D->getCategoryNameLoc(); 397 getEntityInfo(IFaceD, ClassEntity, SA); 398 399 CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo; 400 if (IFaceD) { 401 CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity; 402 CatDInfo.ObjCCatDeclInfo.classCursor = 403 MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU); 404 } else { 405 CatDInfo.ObjCCatDeclInfo.objcClass = 0; 406 CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor(); 407 } 408 CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc); 409 return handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo); 410} 411 412bool IndexingContext::handleObjCMethod(const ObjCMethodDecl *D) { 413 DeclInfo DInfo(!D->isCanonicalDecl(), D->isThisDeclarationADefinition(), 414 D->isThisDeclarationADefinition()); 415 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 416} 417 418bool IndexingContext::handleSynthesizedObjCProperty( 419 const ObjCPropertyImplDecl *D) { 420 ObjCPropertyDecl *PD = D->getPropertyDecl(); 421 return handleReference(PD, D->getLocation(), getCursor(D), 0, D->getDeclContext()); 422} 423 424bool IndexingContext::handleSynthesizedObjCMethod(const ObjCMethodDecl *D, 425 SourceLocation Loc) { 426 DeclInfo DInfo(/*isRedeclaration=*/true, /*isDefinition=*/true, 427 /*isContainer=*/false); 428 return handleDecl(D, Loc, getCursor(D), DInfo); 429} 430 431bool IndexingContext::handleObjCProperty(const ObjCPropertyDecl *D) { 432 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/false, 433 /*isContainer=*/false); 434 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 435} 436 437bool IndexingContext::handleNamespace(const NamespaceDecl *D) { 438 DeclInfo DInfo(/*isRedeclaration=*/!D->isOriginalNamespace(), 439 /*isDefinition=*/true, 440 /*isContainer=*/true); 441 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 442} 443 444bool IndexingContext::handleClassTemplate(const ClassTemplateDecl *D) { 445 return handleCXXRecordDecl(D->getTemplatedDecl(), D); 446} 447 448bool IndexingContext::handleFunctionTemplate(const FunctionTemplateDecl *D) { 449 DeclInfo DInfo(/*isRedeclaration=*/!D->isCanonicalDecl(), 450 /*isDefinition=*/D->isThisDeclarationADefinition(), 451 /*isContainer=*/D->isThisDeclarationADefinition()); 452 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 453} 454 455bool IndexingContext::handleTypeAliasTemplate(const TypeAliasTemplateDecl *D) { 456 DeclInfo DInfo(/*isRedeclaration=*/!D->isCanonicalDecl(), 457 /*isDefinition=*/true, /*isContainer=*/false); 458 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 459} 460 461bool IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc, 462 const NamedDecl *Parent, 463 const DeclContext *DC, 464 const Expr *E, 465 CXIdxEntityRefKind Kind) { 466 if (!D) 467 return false; 468 469 CXCursor Cursor = E ? MakeCXCursor(const_cast<Expr*>(E), 470 const_cast<Decl*>(cast<Decl>(DC)), CXTU) 471 : getRefCursor(D, Loc); 472 return handleReference(D, Loc, Cursor, Parent, DC, E, Kind); 473} 474 475bool IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc, 476 CXCursor Cursor, 477 const NamedDecl *Parent, 478 const DeclContext *DC, 479 const Expr *E, 480 CXIdxEntityRefKind Kind) { 481 if (!CB.indexEntityReference) 482 return false; 483 484 if (!D) 485 return false; 486 if (Loc.isInvalid()) 487 return false; 488 if (D->getParentFunctionOrMethod()) 489 return false; 490 if (isNotFromSourceFile(D->getLocation())) 491 return false; 492 if (D->isImplicit() && shouldIgnoreIfImplicit(D)) 493 return false; 494 495 if (suppressRefs()) { 496 if (markEntityOccurrenceInFile(D, Loc)) 497 return false; // already occurred. 498 } 499 500 StrAdapter SA(*this); 501 EntityInfo RefEntity, ParentEntity; 502 getEntityInfo(D, RefEntity, SA); 503 if (!RefEntity.USR) 504 return false; 505 506 getEntityInfo(Parent, ParentEntity, SA); 507 508 ContainerInfo Container; 509 getContainerInfo(DC, Container); 510 511 CXIdxEntityRefInfo Info = { Kind, 512 Cursor, 513 getIndexLoc(Loc), 514 &RefEntity, 515 Parent ? &ParentEntity : 0, 516 &Container }; 517 CB.indexEntityReference(ClientData, &Info); 518 return true; 519} 520 521bool IndexingContext::isNotFromSourceFile(SourceLocation Loc) const { 522 if (Loc.isInvalid()) 523 return true; 524 SourceManager &SM = Ctx->getSourceManager(); 525 SourceLocation FileLoc = SM.getFileLoc(Loc); 526 FileID FID = SM.getFileID(FileLoc); 527 return SM.getFileEntryForID(FID) == 0; 528} 529 530void IndexingContext::addContainerInMap(const DeclContext *DC, 531 CXIdxClientContainer container) { 532 if (!DC) 533 return; 534 535 assert(getScopedContext(DC) == DC); 536 ContainerMapTy::iterator I = ContainerMap.find(DC); 537 if (I == ContainerMap.end()) { 538 if (container) 539 ContainerMap[DC] = container; 540 return; 541 } 542 // Allow changing the container of a previously seen DeclContext so we 543 // can handle invalid user code, like a function re-definition. 544 if (container) 545 I->second = container; 546 else 547 ContainerMap.erase(I); 548} 549 550CXIdxClientEntity IndexingContext::getClientEntity(const Decl *D) const { 551 if (!D) 552 return 0; 553 EntityMapTy::const_iterator I = EntityMap.find(D); 554 if (I == EntityMap.end()) 555 return 0; 556 return I->second; 557} 558 559void IndexingContext::setClientEntity(const Decl *D, CXIdxClientEntity client) { 560 if (!D) 561 return; 562 EntityMap[D] = client; 563} 564 565bool IndexingContext::handleCXXRecordDecl(const CXXRecordDecl *RD, 566 const NamedDecl *OrigD) { 567 if (RD->isThisDeclarationADefinition()) { 568 StrAdapter SA(*this); 569 CXXClassDeclInfo CXXDInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(), 570 /*isDefinition=*/RD->isThisDeclarationADefinition()); 571 CXXBasesListInfo BaseList(RD, *this, SA); 572 CXXDInfo.CXXClassInfo.declInfo = &CXXDInfo; 573 CXXDInfo.CXXClassInfo.bases = BaseList.getBases(); 574 CXXDInfo.CXXClassInfo.numBases = BaseList.getNumBases(); 575 576 return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), CXXDInfo); 577 } 578 579 DeclInfo DInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(), 580 /*isDefinition=*/RD->isThisDeclarationADefinition(), 581 /*isContainer=*/RD->isThisDeclarationADefinition()); 582 return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), DInfo); 583} 584 585bool IndexingContext::markEntityOccurrenceInFile(const NamedDecl *D, 586 SourceLocation Loc) { 587 SourceManager &SM = Ctx->getSourceManager(); 588 D = getEntityDecl(D); 589 590 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 591 FileID FID = LocInfo.first; 592 if (FID.isInvalid()) 593 return true; 594 595 const FileEntry *FE = SM.getFileEntryForID(FID); 596 if (!FE) 597 return true; 598 RefFileOccurence RefOccur(FE, D); 599 std::pair<llvm::DenseSet<RefFileOccurence>::iterator, bool> 600 res = RefFileOccurences.insert(RefOccur); 601 if (!res.second) 602 return true; // already in map. 603 604 return false; 605} 606 607const NamedDecl *IndexingContext::getEntityDecl(const NamedDecl *D) const { 608 assert(D); 609 D = cast<NamedDecl>(D->getCanonicalDecl()); 610 611 if (const ObjCImplementationDecl * 612 ImplD = dyn_cast<ObjCImplementationDecl>(D)) { 613 return getEntityDecl(ImplD->getClassInterface()); 614 615 } else if (const ObjCCategoryImplDecl * 616 CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) { 617 return getEntityDecl(CatImplD->getCategoryDecl()); 618 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 619 if (FunctionTemplateDecl *TemplD = FD->getDescribedFunctionTemplate()) 620 return getEntityDecl(TemplD); 621 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { 622 if (ClassTemplateDecl *TemplD = RD->getDescribedClassTemplate()) 623 return getEntityDecl(TemplD); 624 } 625 626 return D; 627} 628 629const DeclContext * 630IndexingContext::getEntityContainer(const Decl *D) const { 631 const DeclContext *DC = dyn_cast<DeclContext>(D); 632 if (DC) 633 return DC; 634 635 if (const ClassTemplateDecl *ClassTempl = dyn_cast<ClassTemplateDecl>(D)) { 636 DC = ClassTempl->getTemplatedDecl(); 637 } if (const FunctionTemplateDecl * 638 FuncTempl = dyn_cast<FunctionTemplateDecl>(D)) { 639 DC = FuncTempl->getTemplatedDecl(); 640 } 641 642 return DC; 643} 644 645const DeclContext * 646IndexingContext::getScopedContext(const DeclContext *DC) const { 647 // Local contexts are ignored for indexing. 648 const DeclContext *FuncCtx = cast<Decl>(DC)->getParentFunctionOrMethod(); 649 if (FuncCtx) 650 return FuncCtx; 651 652 // We consider enums always scoped for indexing. 653 if (isa<TagDecl>(DC)) 654 return DC; 655 656 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) { 657 if (NS->isAnonymousNamespace()) 658 return getScopedContext(NS->getParent()); 659 return NS; 660 } 661 662 return DC->getRedeclContext(); 663} 664 665CXIdxClientContainer 666IndexingContext::getClientContainerForDC(const DeclContext *DC) const { 667 if (!DC) 668 return 0; 669 670 DC = getScopedContext(DC); 671 ContainerMapTy::const_iterator I = ContainerMap.find(DC); 672 if (I == ContainerMap.end()) 673 return 0; 674 675 return I->second; 676} 677 678CXIdxClientFile IndexingContext::getIndexFile(const FileEntry *File) { 679 if (!File) 680 return 0; 681 682 FileMapTy::iterator FI = FileMap.find(File); 683 if (FI != FileMap.end()) 684 return FI->second; 685 686 return 0; 687} 688 689CXIdxLoc IndexingContext::getIndexLoc(SourceLocation Loc) const { 690 CXIdxLoc idxLoc = { {0, 0}, 0 }; 691 if (Loc.isInvalid()) 692 return idxLoc; 693 694 idxLoc.ptr_data[0] = (void*)this; 695 idxLoc.int_data = Loc.getRawEncoding(); 696 return idxLoc; 697} 698 699void IndexingContext::translateLoc(SourceLocation Loc, 700 CXIdxClientFile *indexFile, CXFile *file, 701 unsigned *line, unsigned *column, 702 unsigned *offset) { 703 if (Loc.isInvalid()) 704 return; 705 706 SourceManager &SM = Ctx->getSourceManager(); 707 Loc = SM.getFileLoc(Loc); 708 709 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 710 FileID FID = LocInfo.first; 711 unsigned FileOffset = LocInfo.second; 712 713 if (FID.isInvalid()) 714 return; 715 716 const FileEntry *FE = SM.getFileEntryForID(FID); 717 if (indexFile) 718 *indexFile = getIndexFile(FE); 719 if (file) 720 *file = (void *)FE; 721 if (line) 722 *line = SM.getLineNumber(FID, FileOffset); 723 if (column) 724 *column = SM.getColumnNumber(FID, FileOffset); 725 if (offset) 726 *offset = FileOffset; 727} 728 729void IndexingContext::getEntityInfo(const NamedDecl *D, 730 EntityInfo &EntityInfo, 731 StrAdapter &SA) { 732 if (!D) 733 return; 734 735 D = getEntityDecl(D); 736 EntityInfo.cursor = getCursor(D); 737 EntityInfo.Dcl = D; 738 EntityInfo.IndexCtx = this; 739 EntityInfo.kind = CXIdxEntity_Unexposed; 740 EntityInfo.templateKind = CXIdxEntity_NonTemplate; 741 EntityInfo.lang = CXIdxEntityLang_C; 742 743 if (const TagDecl *TD = dyn_cast<TagDecl>(D)) { 744 switch (TD->getTagKind()) { 745 case TTK_Struct: 746 EntityInfo.kind = CXIdxEntity_Struct; break; 747 case TTK_Union: 748 EntityInfo.kind = CXIdxEntity_Union; break; 749 case TTK_Class: 750 EntityInfo.kind = CXIdxEntity_CXXClass; 751 EntityInfo.lang = CXIdxEntityLang_CXX; 752 break; 753 case TTK_Enum: 754 EntityInfo.kind = CXIdxEntity_Enum; break; 755 } 756 757 if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D)) { 758 // FIXME: isPOD check is not sufficient, a POD can contain methods, 759 // we want a isCStructLike check. 760 if (CXXRec->hasDefinition() && !CXXRec->isPOD()) 761 EntityInfo.lang = CXIdxEntityLang_CXX; 762 } 763 764 if (isa<ClassTemplatePartialSpecializationDecl>(D)) { 765 EntityInfo.templateKind = CXIdxEntity_TemplatePartialSpecialization; 766 } else if (isa<ClassTemplateSpecializationDecl>(D)) { 767 EntityInfo.templateKind = CXIdxEntity_TemplateSpecialization; 768 } 769 770 } else { 771 switch (D->getKind()) { 772 case Decl::Typedef: 773 EntityInfo.kind = CXIdxEntity_Typedef; break; 774 case Decl::Function: 775 EntityInfo.kind = CXIdxEntity_Function; 776 break; 777 case Decl::Var: 778 EntityInfo.kind = CXIdxEntity_Variable; 779 if (isa<CXXRecordDecl>(D->getDeclContext())) { 780 EntityInfo.kind = CXIdxEntity_CXXStaticVariable; 781 EntityInfo.lang = CXIdxEntityLang_CXX; 782 } 783 break; 784 case Decl::Field: 785 EntityInfo.kind = CXIdxEntity_Field; 786 if (const CXXRecordDecl * 787 CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) { 788 // FIXME: isPOD check is not sufficient, a POD can contain methods, 789 // we want a isCStructLike check. 790 if (!CXXRec->isPOD()) 791 EntityInfo.lang = CXIdxEntityLang_CXX; 792 } 793 break; 794 case Decl::EnumConstant: 795 EntityInfo.kind = CXIdxEntity_EnumConstant; break; 796 case Decl::ObjCInterface: 797 EntityInfo.kind = CXIdxEntity_ObjCClass; 798 EntityInfo.lang = CXIdxEntityLang_ObjC; 799 break; 800 case Decl::ObjCProtocol: 801 EntityInfo.kind = CXIdxEntity_ObjCProtocol; 802 EntityInfo.lang = CXIdxEntityLang_ObjC; 803 break; 804 case Decl::ObjCCategory: 805 EntityInfo.kind = CXIdxEntity_ObjCCategory; 806 EntityInfo.lang = CXIdxEntityLang_ObjC; 807 break; 808 case Decl::ObjCMethod: 809 if (cast<ObjCMethodDecl>(D)->isInstanceMethod()) 810 EntityInfo.kind = CXIdxEntity_ObjCInstanceMethod; 811 else 812 EntityInfo.kind = CXIdxEntity_ObjCClassMethod; 813 EntityInfo.lang = CXIdxEntityLang_ObjC; 814 break; 815 case Decl::ObjCProperty: 816 EntityInfo.kind = CXIdxEntity_ObjCProperty; 817 EntityInfo.lang = CXIdxEntityLang_ObjC; 818 break; 819 case Decl::ObjCIvar: 820 EntityInfo.kind = CXIdxEntity_ObjCIvar; 821 EntityInfo.lang = CXIdxEntityLang_ObjC; 822 break; 823 case Decl::Namespace: 824 EntityInfo.kind = CXIdxEntity_CXXNamespace; 825 EntityInfo.lang = CXIdxEntityLang_CXX; 826 break; 827 case Decl::NamespaceAlias: 828 EntityInfo.kind = CXIdxEntity_CXXNamespaceAlias; 829 EntityInfo.lang = CXIdxEntityLang_CXX; 830 break; 831 case Decl::CXXConstructor: 832 EntityInfo.kind = CXIdxEntity_CXXConstructor; 833 EntityInfo.lang = CXIdxEntityLang_CXX; 834 break; 835 case Decl::CXXDestructor: 836 EntityInfo.kind = CXIdxEntity_CXXDestructor; 837 EntityInfo.lang = CXIdxEntityLang_CXX; 838 break; 839 case Decl::CXXConversion: 840 EntityInfo.kind = CXIdxEntity_CXXConversionFunction; 841 EntityInfo.lang = CXIdxEntityLang_CXX; 842 break; 843 case Decl::CXXMethod: { 844 const CXXMethodDecl *MD = cast<CXXMethodDecl>(D); 845 if (MD->isStatic()) 846 EntityInfo.kind = CXIdxEntity_CXXStaticMethod; 847 else 848 EntityInfo.kind = CXIdxEntity_CXXInstanceMethod; 849 EntityInfo.lang = CXIdxEntityLang_CXX; 850 break; 851 } 852 case Decl::ClassTemplate: 853 EntityInfo.kind = CXIdxEntity_CXXClass; 854 EntityInfo.templateKind = CXIdxEntity_Template; 855 break; 856 case Decl::FunctionTemplate: 857 EntityInfo.kind = CXIdxEntity_Function; 858 EntityInfo.templateKind = CXIdxEntity_Template; 859 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>( 860 cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) { 861 if (isa<CXXConstructorDecl>(MD)) 862 EntityInfo.kind = CXIdxEntity_CXXConstructor; 863 else if (isa<CXXDestructorDecl>(MD)) 864 EntityInfo.kind = CXIdxEntity_CXXDestructor; 865 else if (isa<CXXConversionDecl>(MD)) 866 EntityInfo.kind = CXIdxEntity_CXXConversionFunction; 867 else { 868 if (MD->isStatic()) 869 EntityInfo.kind = CXIdxEntity_CXXStaticMethod; 870 else 871 EntityInfo.kind = CXIdxEntity_CXXInstanceMethod; 872 } 873 } 874 break; 875 case Decl::TypeAliasTemplate: 876 EntityInfo.kind = CXIdxEntity_CXXTypeAlias; 877 EntityInfo.templateKind = CXIdxEntity_Template; 878 break; 879 case Decl::TypeAlias: 880 EntityInfo.kind = CXIdxEntity_CXXTypeAlias; 881 EntityInfo.lang = CXIdxEntityLang_CXX; 882 break; 883 default: 884 break; 885 } 886 } 887 888 if (EntityInfo.kind == CXIdxEntity_Unexposed) 889 return; 890 891 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 892 if (FD->getTemplatedKind() == 893 FunctionDecl::TK_FunctionTemplateSpecialization) 894 EntityInfo.templateKind = CXIdxEntity_TemplateSpecialization; 895 } 896 897 if (EntityInfo.templateKind != CXIdxEntity_NonTemplate) 898 EntityInfo.lang = CXIdxEntityLang_CXX; 899 900 if (IdentifierInfo *II = D->getIdentifier()) { 901 EntityInfo.name = SA.toCStr(II->getName()); 902 903 } else if (isa<TagDecl>(D) || isa<FieldDecl>(D) || isa<NamespaceDecl>(D)) { 904 EntityInfo.name = 0; // anonymous tag/field/namespace. 905 906 } else { 907 llvm::SmallString<256> StrBuf; 908 { 909 llvm::raw_svector_ostream OS(StrBuf); 910 D->printName(OS); 911 } 912 EntityInfo.name = SA.copyCStr(StrBuf.str()); 913 } 914 915 { 916 llvm::SmallString<512> StrBuf; 917 bool Ignore = getDeclCursorUSR(D, StrBuf); 918 if (Ignore) { 919 EntityInfo.USR = 0; 920 } else { 921 EntityInfo.USR = SA.copyCStr(StrBuf.str()); 922 } 923 } 924} 925 926void IndexingContext::getContainerInfo(const DeclContext *DC, 927 ContainerInfo &ContInfo) { 928 ContInfo.cursor = getCursor(cast<Decl>(DC)); 929 ContInfo.DC = DC; 930 ContInfo.IndexCtx = this; 931} 932 933CXCursor IndexingContext::getRefCursor(const NamedDecl *D, SourceLocation Loc) { 934 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D)) 935 return MakeCursorTypeRef(TD, Loc, CXTU); 936 if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) 937 return MakeCursorObjCClassRef(ID, Loc, CXTU); 938 if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) 939 return MakeCursorObjCProtocolRef(PD, Loc, CXTU); 940 if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) 941 return MakeCursorTemplateRef(Template, Loc, CXTU); 942 if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(D)) 943 return MakeCursorNamespaceRef(Namespace, Loc, CXTU); 944 if (const NamespaceAliasDecl *Namespace = dyn_cast<NamespaceAliasDecl>(D)) 945 return MakeCursorNamespaceRef(Namespace, Loc, CXTU); 946 if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) 947 return MakeCursorMemberRef(Field, Loc, CXTU); 948 949 return clang_getNullCursor(); 950} 951 952bool IndexingContext::shouldIgnoreIfImplicit(const NamedDecl *D) { 953 if (isa<ObjCInterfaceDecl>(D)) 954 return false; 955 if (isa<ObjCCategoryDecl>(D)) 956 return false; 957 if (isa<ObjCIvarDecl>(D)) 958 return false; 959 if (isa<ObjCMethodDecl>(D)) 960 return false; 961 return true; 962} 963