IndexingContext.cpp revision c6994005dc9f677c831b8e90bdab483cc2197c29
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 = 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 ContainerMapTy::iterator I = ContainerMap.find(DC); 536 if (I == ContainerMap.end()) { 537 if (container) 538 ContainerMap[DC] = container; 539 return; 540 } 541 // Allow changing the container of a previously seen DeclContext so we 542 // can handle invalid user code, like a function re-definition. 543 if (container) 544 I->second = container; 545 else 546 ContainerMap.erase(I); 547} 548 549CXIdxClientEntity IndexingContext::getClientEntity(const Decl *D) const { 550 if (!D) 551 return 0; 552 EntityMapTy::const_iterator I = EntityMap.find(D); 553 if (I == EntityMap.end()) 554 return 0; 555 return I->second; 556} 557 558void IndexingContext::setClientEntity(const Decl *D, CXIdxClientEntity client) { 559 if (!D) 560 return; 561 EntityMap[D] = client; 562} 563 564bool IndexingContext::handleCXXRecordDecl(const CXXRecordDecl *RD, 565 const NamedDecl *OrigD) { 566 if (RD->isThisDeclarationADefinition()) { 567 StrAdapter SA(*this); 568 CXXClassDeclInfo CXXDInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(), 569 /*isDefinition=*/RD->isThisDeclarationADefinition()); 570 CXXBasesListInfo BaseList(RD, *this, SA); 571 CXXDInfo.CXXClassInfo.declInfo = &CXXDInfo; 572 CXXDInfo.CXXClassInfo.bases = BaseList.getBases(); 573 CXXDInfo.CXXClassInfo.numBases = BaseList.getNumBases(); 574 575 return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), CXXDInfo); 576 } 577 578 DeclInfo DInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(), 579 /*isDefinition=*/RD->isThisDeclarationADefinition(), 580 /*isContainer=*/RD->isThisDeclarationADefinition()); 581 return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), DInfo); 582} 583 584bool IndexingContext::markEntityOccurrenceInFile(const NamedDecl *D, 585 SourceLocation Loc) { 586 SourceManager &SM = Ctx->getSourceManager(); 587 D = getEntityDecl(D); 588 589 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 590 FileID FID = LocInfo.first; 591 if (FID.isInvalid()) 592 return true; 593 594 const FileEntry *FE = SM.getFileEntryForID(FID); 595 if (!FE) 596 return true; 597 RefFileOccurence RefOccur(FE, D); 598 std::pair<llvm::DenseSet<RefFileOccurence>::iterator, bool> 599 res = RefFileOccurences.insert(RefOccur); 600 if (!res.second) 601 return true; // already in map. 602 603 return false; 604} 605 606const NamedDecl *IndexingContext::getEntityDecl(const NamedDecl *D) const { 607 assert(D); 608 D = cast<NamedDecl>(D->getCanonicalDecl()); 609 610 if (const ObjCImplementationDecl * 611 ImplD = dyn_cast<ObjCImplementationDecl>(D)) { 612 return getEntityDecl(ImplD->getClassInterface()); 613 614 } else if (const ObjCCategoryImplDecl * 615 CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) { 616 return getEntityDecl(CatImplD->getCategoryDecl()); 617 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 618 if (FunctionTemplateDecl *TemplD = FD->getDescribedFunctionTemplate()) 619 return getEntityDecl(TemplD); 620 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { 621 if (ClassTemplateDecl *TemplD = RD->getDescribedClassTemplate()) 622 return getEntityDecl(TemplD); 623 } 624 625 return D; 626} 627 628const DeclContext * 629IndexingContext::getEntityContainer(const Decl *D) const { 630 const DeclContext *DC = dyn_cast<DeclContext>(D); 631 if (DC) 632 return DC; 633 634 if (const ClassTemplateDecl *ClassTempl = dyn_cast<ClassTemplateDecl>(D)) { 635 DC = ClassTempl->getTemplatedDecl(); 636 } if (const FunctionTemplateDecl * 637 FuncTempl = dyn_cast<FunctionTemplateDecl>(D)) { 638 DC = FuncTempl->getTemplatedDecl(); 639 } 640 641 return DC; 642} 643 644CXIdxClientContainer 645IndexingContext::getClientContainerForDC(const DeclContext *DC) const { 646 if (!DC) 647 return 0; 648 649 ContainerMapTy::const_iterator I = ContainerMap.find(DC); 650 if (I == ContainerMap.end()) 651 return 0; 652 653 return I->second; 654} 655 656CXIdxClientFile IndexingContext::getIndexFile(const FileEntry *File) { 657 if (!File) 658 return 0; 659 660 FileMapTy::iterator FI = FileMap.find(File); 661 if (FI != FileMap.end()) 662 return FI->second; 663 664 return 0; 665} 666 667CXIdxLoc IndexingContext::getIndexLoc(SourceLocation Loc) const { 668 CXIdxLoc idxLoc = { {0, 0}, 0 }; 669 if (Loc.isInvalid()) 670 return idxLoc; 671 672 idxLoc.ptr_data[0] = (void*)this; 673 idxLoc.int_data = Loc.getRawEncoding(); 674 return idxLoc; 675} 676 677void IndexingContext::translateLoc(SourceLocation Loc, 678 CXIdxClientFile *indexFile, CXFile *file, 679 unsigned *line, unsigned *column, 680 unsigned *offset) { 681 if (Loc.isInvalid()) 682 return; 683 684 SourceManager &SM = Ctx->getSourceManager(); 685 Loc = SM.getFileLoc(Loc); 686 687 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 688 FileID FID = LocInfo.first; 689 unsigned FileOffset = LocInfo.second; 690 691 if (FID.isInvalid()) 692 return; 693 694 const FileEntry *FE = SM.getFileEntryForID(FID); 695 if (indexFile) 696 *indexFile = getIndexFile(FE); 697 if (file) 698 *file = (void *)FE; 699 if (line) 700 *line = SM.getLineNumber(FID, FileOffset); 701 if (column) 702 *column = SM.getColumnNumber(FID, FileOffset); 703 if (offset) 704 *offset = FileOffset; 705} 706 707void IndexingContext::getEntityInfo(const NamedDecl *D, 708 EntityInfo &EntityInfo, 709 StrAdapter &SA) { 710 if (!D) 711 return; 712 713 D = getEntityDecl(D); 714 EntityInfo.cursor = getCursor(D); 715 EntityInfo.Dcl = D; 716 EntityInfo.IndexCtx = this; 717 EntityInfo.kind = CXIdxEntity_Unexposed; 718 EntityInfo.templateKind = CXIdxEntity_NonTemplate; 719 EntityInfo.lang = CXIdxEntityLang_C; 720 721 if (const TagDecl *TD = dyn_cast<TagDecl>(D)) { 722 switch (TD->getTagKind()) { 723 case TTK_Struct: 724 EntityInfo.kind = CXIdxEntity_Struct; break; 725 case TTK_Union: 726 EntityInfo.kind = CXIdxEntity_Union; break; 727 case TTK_Class: 728 EntityInfo.kind = CXIdxEntity_CXXClass; 729 EntityInfo.lang = CXIdxEntityLang_CXX; 730 break; 731 case TTK_Enum: 732 EntityInfo.kind = CXIdxEntity_Enum; break; 733 } 734 735 if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D)) { 736 // FIXME: isPOD check is not sufficient, a POD can contain methods, 737 // we want a isCStructLike check. 738 if (CXXRec->hasDefinition() && !CXXRec->isPOD()) 739 EntityInfo.lang = CXIdxEntityLang_CXX; 740 } 741 742 if (isa<ClassTemplatePartialSpecializationDecl>(D)) { 743 EntityInfo.templateKind = CXIdxEntity_TemplatePartialSpecialization; 744 } else if (isa<ClassTemplateSpecializationDecl>(D)) { 745 EntityInfo.templateKind = CXIdxEntity_TemplateSpecialization; 746 } 747 748 } else { 749 switch (D->getKind()) { 750 case Decl::Typedef: 751 EntityInfo.kind = CXIdxEntity_Typedef; break; 752 case Decl::Function: 753 EntityInfo.kind = CXIdxEntity_Function; 754 break; 755 case Decl::Var: 756 EntityInfo.kind = CXIdxEntity_Variable; 757 if (isa<CXXRecordDecl>(D->getDeclContext())) { 758 EntityInfo.kind = CXIdxEntity_CXXStaticVariable; 759 EntityInfo.lang = CXIdxEntityLang_CXX; 760 } 761 break; 762 case Decl::Field: 763 EntityInfo.kind = CXIdxEntity_Field; 764 if (const CXXRecordDecl * 765 CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) { 766 // FIXME: isPOD check is not sufficient, a POD can contain methods, 767 // we want a isCStructLike check. 768 if (!CXXRec->isPOD()) 769 EntityInfo.lang = CXIdxEntityLang_CXX; 770 } 771 break; 772 case Decl::EnumConstant: 773 EntityInfo.kind = CXIdxEntity_EnumConstant; break; 774 case Decl::ObjCInterface: 775 EntityInfo.kind = CXIdxEntity_ObjCClass; 776 EntityInfo.lang = CXIdxEntityLang_ObjC; 777 break; 778 case Decl::ObjCProtocol: 779 EntityInfo.kind = CXIdxEntity_ObjCProtocol; 780 EntityInfo.lang = CXIdxEntityLang_ObjC; 781 break; 782 case Decl::ObjCCategory: 783 EntityInfo.kind = CXIdxEntity_ObjCCategory; 784 EntityInfo.lang = CXIdxEntityLang_ObjC; 785 break; 786 case Decl::ObjCMethod: 787 if (cast<ObjCMethodDecl>(D)->isInstanceMethod()) 788 EntityInfo.kind = CXIdxEntity_ObjCInstanceMethod; 789 else 790 EntityInfo.kind = CXIdxEntity_ObjCClassMethod; 791 EntityInfo.lang = CXIdxEntityLang_ObjC; 792 break; 793 case Decl::ObjCProperty: 794 EntityInfo.kind = CXIdxEntity_ObjCProperty; 795 EntityInfo.lang = CXIdxEntityLang_ObjC; 796 break; 797 case Decl::ObjCIvar: 798 EntityInfo.kind = CXIdxEntity_ObjCIvar; 799 EntityInfo.lang = CXIdxEntityLang_ObjC; 800 break; 801 case Decl::Namespace: 802 EntityInfo.kind = CXIdxEntity_CXXNamespace; 803 EntityInfo.lang = CXIdxEntityLang_CXX; 804 break; 805 case Decl::NamespaceAlias: 806 EntityInfo.kind = CXIdxEntity_CXXNamespaceAlias; 807 EntityInfo.lang = CXIdxEntityLang_CXX; 808 break; 809 case Decl::CXXConstructor: 810 EntityInfo.kind = CXIdxEntity_CXXConstructor; 811 EntityInfo.lang = CXIdxEntityLang_CXX; 812 break; 813 case Decl::CXXDestructor: 814 EntityInfo.kind = CXIdxEntity_CXXDestructor; 815 EntityInfo.lang = CXIdxEntityLang_CXX; 816 break; 817 case Decl::CXXConversion: 818 EntityInfo.kind = CXIdxEntity_CXXConversionFunction; 819 EntityInfo.lang = CXIdxEntityLang_CXX; 820 break; 821 case Decl::CXXMethod: { 822 const CXXMethodDecl *MD = cast<CXXMethodDecl>(D); 823 if (MD->isStatic()) 824 EntityInfo.kind = CXIdxEntity_CXXStaticMethod; 825 else 826 EntityInfo.kind = CXIdxEntity_CXXInstanceMethod; 827 EntityInfo.lang = CXIdxEntityLang_CXX; 828 break; 829 } 830 case Decl::ClassTemplate: 831 EntityInfo.kind = CXIdxEntity_CXXClass; 832 EntityInfo.templateKind = CXIdxEntity_Template; 833 break; 834 case Decl::FunctionTemplate: 835 EntityInfo.kind = CXIdxEntity_Function; 836 EntityInfo.templateKind = CXIdxEntity_Template; 837 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>( 838 cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) { 839 if (isa<CXXConstructorDecl>(MD)) 840 EntityInfo.kind = CXIdxEntity_CXXConstructor; 841 else if (isa<CXXDestructorDecl>(MD)) 842 EntityInfo.kind = CXIdxEntity_CXXDestructor; 843 else if (isa<CXXConversionDecl>(MD)) 844 EntityInfo.kind = CXIdxEntity_CXXConversionFunction; 845 else { 846 if (MD->isStatic()) 847 EntityInfo.kind = CXIdxEntity_CXXStaticMethod; 848 else 849 EntityInfo.kind = CXIdxEntity_CXXInstanceMethod; 850 } 851 } 852 break; 853 case Decl::TypeAliasTemplate: 854 EntityInfo.kind = CXIdxEntity_CXXTypeAlias; 855 EntityInfo.templateKind = CXIdxEntity_Template; 856 break; 857 case Decl::TypeAlias: 858 EntityInfo.kind = CXIdxEntity_CXXTypeAlias; 859 EntityInfo.lang = CXIdxEntityLang_CXX; 860 break; 861 default: 862 break; 863 } 864 } 865 866 if (EntityInfo.kind == CXIdxEntity_Unexposed) 867 return; 868 869 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 870 if (FD->getTemplatedKind() == 871 FunctionDecl::TK_FunctionTemplateSpecialization) 872 EntityInfo.templateKind = CXIdxEntity_TemplateSpecialization; 873 } 874 875 if (EntityInfo.templateKind != CXIdxEntity_NonTemplate) 876 EntityInfo.lang = CXIdxEntityLang_CXX; 877 878 if (IdentifierInfo *II = D->getIdentifier()) { 879 EntityInfo.name = SA.toCStr(II->getName()); 880 881 } else if (isa<TagDecl>(D) || isa<FieldDecl>(D) || isa<NamespaceDecl>(D)) { 882 EntityInfo.name = 0; // anonymous tag/field/namespace. 883 884 } else { 885 llvm::SmallString<256> StrBuf; 886 { 887 llvm::raw_svector_ostream OS(StrBuf); 888 D->printName(OS); 889 } 890 EntityInfo.name = SA.copyCStr(StrBuf.str()); 891 } 892 893 { 894 llvm::SmallString<512> StrBuf; 895 bool Ignore = getDeclCursorUSR(D, StrBuf); 896 if (Ignore) { 897 EntityInfo.USR = 0; 898 } else { 899 EntityInfo.USR = SA.copyCStr(StrBuf.str()); 900 } 901 } 902} 903 904void IndexingContext::getContainerInfo(const DeclContext *DC, 905 ContainerInfo &ContInfo) { 906 ContInfo.cursor = getCursor(cast<Decl>(DC)); 907 ContInfo.DC = DC; 908 ContInfo.IndexCtx = this; 909} 910 911CXCursor IndexingContext::getRefCursor(const NamedDecl *D, SourceLocation Loc) { 912 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D)) 913 return MakeCursorTypeRef(TD, Loc, CXTU); 914 if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) 915 return MakeCursorObjCClassRef(ID, Loc, CXTU); 916 if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) 917 return MakeCursorObjCProtocolRef(PD, Loc, CXTU); 918 if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) 919 return MakeCursorTemplateRef(Template, Loc, CXTU); 920 if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(D)) 921 return MakeCursorNamespaceRef(Namespace, Loc, CXTU); 922 if (const NamespaceAliasDecl *Namespace = dyn_cast<NamespaceAliasDecl>(D)) 923 return MakeCursorNamespaceRef(Namespace, Loc, CXTU); 924 if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) 925 return MakeCursorMemberRef(Field, Loc, CXTU); 926 927 return clang_getNullCursor(); 928} 929 930bool IndexingContext::shouldIgnoreIfImplicit(const NamedDecl *D) { 931 if (isa<ObjCInterfaceDecl>(D)) 932 return false; 933 if (isa<ObjCCategoryDecl>(D)) 934 return false; 935 if (isa<ObjCIvarDecl>(D)) 936 return false; 937 if (isa<ObjCMethodDecl>(D)) 938 return false; 939 return true; 940} 941