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