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