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