1 //===- IndexingContext.cpp - Higher level API functions -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "IndexingContext.h"
11 #include "CIndexDiagnostic.h"
12 #include "CXTranslationUnit.h"
13 #include "clang/AST/DeclCXX.h"
14 #include "clang/AST/DeclTemplate.h"
15 #include "clang/Frontend/ASTUnit.h"
17 using namespace clang;
18 using namespace cxindex;
19 using namespace cxcursor;
21 IndexingContext::ObjCProtocolListInfo::ObjCProtocolListInfo(
22 const ObjCProtocolList &ProtList,
23 IndexingContext &IdxCtx,
25 ObjCInterfaceDecl::protocol_loc_iterator LI = ProtList.loc_begin();
26 for (ObjCInterfaceDecl::protocol_iterator
27 I = ProtList.begin(), E = ProtList.end(); I != E; ++I, ++LI) {
28 SourceLocation Loc = *LI;
29 ObjCProtocolDecl *PD = *I;
30 ProtEntities.push_back(EntityInfo());
31 IdxCtx.getEntityInfo(PD, ProtEntities.back(), SA);
32 CXIdxObjCProtocolRefInfo ProtInfo = { 0,
33 MakeCursorObjCProtocolRef(PD, Loc, IdxCtx.CXTU),
34 IdxCtx.getIndexLoc(Loc) };
35 ProtInfos.push_back(ProtInfo);
37 if (IdxCtx.shouldSuppressRefs())
38 IdxCtx.markEntityOccurrenceInFile(PD, Loc);
41 for (unsigned i = 0, e = ProtInfos.size(); i != e; ++i)
42 ProtInfos[i].protocol = &ProtEntities[i];
44 for (unsigned i = 0, e = ProtInfos.size(); i != e; ++i)
45 Prots.push_back(&ProtInfos[i]);
49 IBOutletCollectionInfo::IBOutletCollectionInfo(
50 const IBOutletCollectionInfo &other)
51 : AttrInfo(CXIdxAttr_IBOutletCollection, other.cursor, other.loc, other.A) {
53 IBCollInfo.attrInfo = this;
54 IBCollInfo.classCursor = other.IBCollInfo.classCursor;
55 IBCollInfo.classLoc = other.IBCollInfo.classLoc;
56 if (other.IBCollInfo.objcClass) {
57 ClassInfo = other.ClassInfo;
58 IBCollInfo.objcClass = &ClassInfo;
60 IBCollInfo.objcClass = 0;
63 AttrListInfo::AttrListInfo(const Decl *D, IndexingContext &IdxCtx)
64 : SA(IdxCtx), ref_cnt(0) {
69 for (AttrVec::const_iterator AttrI = D->attr_begin(), AttrE = D->attr_end();
70 AttrI != AttrE; ++AttrI) {
71 const Attr *A = *AttrI;
72 CXCursor C = MakeCXCursor(A, D, IdxCtx.CXTU);
73 CXIdxLoc Loc = IdxCtx.getIndexLoc(A->getLocation());
76 Attrs.push_back(AttrInfo(CXIdxAttr_Unexposed, C, Loc, A));
78 case CXCursor_IBActionAttr:
79 Attrs.push_back(AttrInfo(CXIdxAttr_IBAction, C, Loc, A));
81 case CXCursor_IBOutletAttr:
82 Attrs.push_back(AttrInfo(CXIdxAttr_IBOutlet, C, Loc, A));
84 case CXCursor_IBOutletCollectionAttr:
85 IBCollAttrs.push_back(IBOutletCollectionInfo(C, Loc, A));
90 for (unsigned i = 0, e = IBCollAttrs.size(); i != e; ++i) {
91 IBOutletCollectionInfo &IBInfo = IBCollAttrs[i];
92 CXAttrs.push_back(&IBInfo);
94 const IBOutletCollectionAttr *
95 IBAttr = cast<IBOutletCollectionAttr>(IBInfo.A);
96 IBInfo.IBCollInfo.attrInfo = &IBInfo;
97 IBInfo.IBCollInfo.classLoc = IdxCtx.getIndexLoc(IBAttr->getInterfaceLoc());
98 IBInfo.IBCollInfo.objcClass = 0;
99 IBInfo.IBCollInfo.classCursor = clang_getNullCursor();
100 QualType Ty = IBAttr->getInterface();
101 if (const ObjCInterfaceType *InterTy = Ty->getAs<ObjCInterfaceType>()) {
102 if (const ObjCInterfaceDecl *InterD = InterTy->getInterface()) {
103 IdxCtx.getEntityInfo(InterD, IBInfo.ClassInfo, SA);
104 IBInfo.IBCollInfo.objcClass = &IBInfo.ClassInfo;
105 IBInfo.IBCollInfo.classCursor = MakeCursorObjCClassRef(InterD,
106 IBAttr->getInterfaceLoc(), IdxCtx.CXTU);
111 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
112 CXAttrs.push_back(&Attrs[i]);
115 IntrusiveRefCntPtr<AttrListInfo>
116 AttrListInfo::create(const Decl *D, IndexingContext &IdxCtx) {
117 ScratchAlloc SA(IdxCtx);
118 AttrListInfo *attrs = SA.allocate<AttrListInfo>();
119 return new (attrs) AttrListInfo(D, IdxCtx);
122 IndexingContext::CXXBasesListInfo::CXXBasesListInfo(const CXXRecordDecl *D,
123 IndexingContext &IdxCtx,
125 for (CXXRecordDecl::base_class_const_iterator
126 I = D->bases_begin(), E = D->bases_end(); I != E; ++I) {
127 const CXXBaseSpecifier &Base = *I;
128 BaseEntities.push_back(EntityInfo());
129 const NamedDecl *BaseD = 0;
130 QualType T = Base.getType();
131 SourceLocation Loc = getBaseLoc(Base);
133 if (const TypedefType *TDT = T->getAs<TypedefType>()) {
134 BaseD = TDT->getDecl();
135 } else if (const TemplateSpecializationType *
136 TST = T->getAs<TemplateSpecializationType>()) {
137 BaseD = TST->getTemplateName().getAsTemplateDecl();
138 } else if (const RecordType *RT = T->getAs<RecordType>()) {
139 BaseD = RT->getDecl();
143 IdxCtx.getEntityInfo(BaseD, BaseEntities.back(), SA);
144 CXIdxBaseClassInfo BaseInfo = { 0,
145 MakeCursorCXXBaseSpecifier(&Base, IdxCtx.CXTU),
146 IdxCtx.getIndexLoc(Loc) };
147 BaseInfos.push_back(BaseInfo);
150 for (unsigned i = 0, e = BaseInfos.size(); i != e; ++i) {
151 if (BaseEntities[i].name && BaseEntities[i].USR)
152 BaseInfos[i].base = &BaseEntities[i];
155 for (unsigned i = 0, e = BaseInfos.size(); i != e; ++i)
156 CXBases.push_back(&BaseInfos[i]);
159 SourceLocation IndexingContext::CXXBasesListInfo::getBaseLoc(
160 const CXXBaseSpecifier &Base) const {
161 SourceLocation Loc = Base.getSourceRange().getBegin();
163 if (Base.getTypeSourceInfo())
164 TL = Base.getTypeSourceInfo()->getTypeLoc();
168 if (QualifiedTypeLoc QL = TL.getAs<QualifiedTypeLoc>())
169 TL = QL.getUnqualifiedLoc();
171 if (ElaboratedTypeLoc EL = TL.getAs<ElaboratedTypeLoc>())
172 return EL.getNamedTypeLoc().getBeginLoc();
173 if (DependentNameTypeLoc DL = TL.getAs<DependentNameTypeLoc>())
174 return DL.getNameLoc();
175 if (DependentTemplateSpecializationTypeLoc DTL =
176 TL.getAs<DependentTemplateSpecializationTypeLoc>())
177 return DTL.getTemplateNameLoc();
182 const char *ScratchAlloc::toCStr(StringRef Str) {
185 if (Str.data()[Str.size()] == '\0')
187 return copyCStr(Str);
190 const char *ScratchAlloc::copyCStr(StringRef Str) {
191 char *buf = IdxCtx.StrScratch.Allocate<char>(Str.size() + 1);
192 std::uninitialized_copy(Str.begin(), Str.end(), buf);
193 buf[Str.size()] = '\0';
197 void IndexingContext::setASTContext(ASTContext &ctx) {
199 cxtu::getASTUnit(CXTU)->setASTContext(&ctx);
202 void IndexingContext::setPreprocessor(Preprocessor &PP) {
203 cxtu::getASTUnit(CXTU)->setPreprocessor(&PP);
206 bool IndexingContext::isFunctionLocalDecl(const Decl *D) {
209 if (!D->getParentFunctionOrMethod())
212 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
213 switch (ND->getLinkage()) {
215 case InternalLinkage:
217 case UniqueExternalLinkage:
218 case ExternalLinkage:
226 bool IndexingContext::shouldAbort() {
229 return CB.abortQuery(ClientData, 0);
232 void IndexingContext::enteredMainFile(const FileEntry *File) {
233 if (File && CB.enteredMainFile) {
234 CXIdxClientFile idxFile =
235 CB.enteredMainFile(ClientData,
236 static_cast<CXFile>(const_cast<FileEntry *>(File)), 0);
237 FileMap[File] = idxFile;
241 void IndexingContext::ppIncludedFile(SourceLocation hashLoc,
243 const FileEntry *File,
244 bool isImport, bool isAngled,
245 bool isModuleImport) {
246 if (!CB.ppIncludedFile)
249 ScratchAlloc SA(*this);
250 CXIdxIncludedFileInfo Info = { getIndexLoc(hashLoc),
253 const_cast<FileEntry *>(File)),
254 isImport, isAngled, isModuleImport };
255 CXIdxClientFile idxFile = CB.ppIncludedFile(ClientData, &Info);
256 FileMap[File] = idxFile;
259 void IndexingContext::importedModule(const ImportDecl *ImportD) {
260 if (!CB.importedASTFile)
263 Module *Mod = ImportD->getImportedModule();
266 std::string ModuleName = Mod->getFullModuleName();
268 CXIdxImportedASTFileInfo Info = {
270 const_cast<FileEntry *>(Mod->getASTFile())),
272 getIndexLoc(ImportD->getLocation()),
273 ImportD->isImplicit()
275 CXIdxClientASTFile astFile = CB.importedASTFile(ClientData, &Info);
279 void IndexingContext::importedPCH(const FileEntry *File) {
280 if (!CB.importedASTFile)
283 CXIdxImportedASTFileInfo Info = {
285 const_cast<FileEntry *>(File)),
287 getIndexLoc(SourceLocation()),
290 CXIdxClientASTFile astFile = CB.importedASTFile(ClientData, &Info);
294 void IndexingContext::startedTranslationUnit() {
295 CXIdxClientContainer idxCont = 0;
296 if (CB.startedTranslationUnit)
297 idxCont = CB.startedTranslationUnit(ClientData, 0);
298 addContainerInMap(Ctx->getTranslationUnitDecl(), idxCont);
301 void IndexingContext::handleDiagnosticSet(CXDiagnostic CXDiagSet) {
305 CB.diagnostic(ClientData, CXDiagSet, 0);
308 bool IndexingContext::handleDecl(const NamedDecl *D,
309 SourceLocation Loc, CXCursor Cursor,
311 const DeclContext *LexicalDC) {
312 if (!CB.indexDeclaration || !D)
314 if (D->isImplicit() && shouldIgnoreIfImplicit(D))
317 ScratchAlloc SA(*this);
318 getEntityInfo(D, DInfo.EntInfo, SA);
319 if ((!shouldIndexFunctionLocalSymbols() && !DInfo.EntInfo.USR)
324 LexicalDC = D->getLexicalDeclContext();
326 if (shouldSuppressRefs())
327 markEntityOccurrenceInFile(D, Loc);
329 DInfo.entityInfo = &DInfo.EntInfo;
330 DInfo.cursor = Cursor;
331 DInfo.loc = getIndexLoc(Loc);
332 DInfo.isImplicit = D->isImplicit();
334 DInfo.attributes = DInfo.EntInfo.attributes;
335 DInfo.numAttributes = DInfo.EntInfo.numAttributes;
337 getContainerInfo(D->getDeclContext(), DInfo.SemanticContainer);
338 DInfo.semanticContainer = &DInfo.SemanticContainer;
340 if (LexicalDC == D->getDeclContext()) {
341 DInfo.lexicalContainer = &DInfo.SemanticContainer;
342 } else if (isTemplateImplicitInstantiation(D)) {
343 // Implicit instantiations have the lexical context of where they were
344 // instantiated first. We choose instead the semantic context because:
345 // 1) at the time that we see the instantiation we have not seen the
346 // function where it occurred yet.
347 // 2) the lexical context of the first instantiation is not useful
348 // information anyway.
349 DInfo.lexicalContainer = &DInfo.SemanticContainer;
351 getContainerInfo(LexicalDC, DInfo.LexicalContainer);
352 DInfo.lexicalContainer = &DInfo.LexicalContainer;
355 if (DInfo.isContainer) {
356 getContainerInfo(getEntityContainer(D), DInfo.DeclAsContainer);
357 DInfo.declAsContainer = &DInfo.DeclAsContainer;
360 CB.indexDeclaration(ClientData, &DInfo);
364 bool IndexingContext::handleObjCContainer(const ObjCContainerDecl *D,
365 SourceLocation Loc, CXCursor Cursor,
366 ObjCContainerDeclInfo &ContDInfo) {
367 ContDInfo.ObjCContDeclInfo.declInfo = &ContDInfo;
368 return handleDecl(D, Loc, Cursor, ContDInfo);
371 bool IndexingContext::handleFunction(const FunctionDecl *D) {
372 bool isDef = D->isThisDeclarationADefinition();
373 bool isContainer = isDef;
374 bool isSkipped = false;
375 if (D->hasSkippedBody()) {
381 DeclInfo DInfo(!D->isFirstDeclaration(), isDef, isContainer);
383 DInfo.flags |= CXIdxDeclFlag_Skipped;
384 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
387 bool IndexingContext::handleVar(const VarDecl *D) {
388 DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(),
389 /*isContainer=*/false);
390 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
393 bool IndexingContext::handleField(const FieldDecl *D) {
394 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true,
395 /*isContainer=*/false);
396 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
399 bool IndexingContext::handleMSProperty(const MSPropertyDecl *D) {
400 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true,
401 /*isContainer=*/false);
402 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
405 bool IndexingContext::handleEnumerator(const EnumConstantDecl *D) {
406 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true,
407 /*isContainer=*/false);
408 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
411 bool IndexingContext::handleTagDecl(const TagDecl *D) {
412 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(D))
413 return handleCXXRecordDecl(CXXRD, D);
415 DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(),
416 D->isThisDeclarationADefinition());
417 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
420 bool IndexingContext::handleTypedefName(const TypedefNameDecl *D) {
421 DeclInfo DInfo(!D->isFirstDeclaration(), /*isDefinition=*/true,
422 /*isContainer=*/false);
423 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
426 bool IndexingContext::handleObjCInterface(const ObjCInterfaceDecl *D) {
427 // For @class forward declarations, suppress them the same way as references.
428 if (!D->isThisDeclarationADefinition()) {
429 if (shouldSuppressRefs() && markEntityOccurrenceInFile(D, D->getLocation()))
430 return false; // already occurred.
432 // FIXME: This seems like the wrong definition for redeclaration.
433 bool isRedeclaration = D->hasDefinition() || D->getPreviousDecl();
434 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true, isRedeclaration,
435 /*isImplementation=*/false);
436 return handleObjCContainer(D, D->getLocation(),
437 MakeCursorObjCClassRef(D, D->getLocation(),
442 ScratchAlloc SA(*this);
444 CXIdxBaseClassInfo BaseClass;
445 EntityInfo BaseEntity;
446 BaseClass.cursor = clang_getNullCursor();
447 if (ObjCInterfaceDecl *SuperD = D->getSuperClass()) {
448 getEntityInfo(SuperD, BaseEntity, SA);
449 SourceLocation SuperLoc = D->getSuperClassLoc();
450 BaseClass.base = &BaseEntity;
451 BaseClass.cursor = MakeCursorObjCSuperClassRef(SuperD, SuperLoc, CXTU);
452 BaseClass.loc = getIndexLoc(SuperLoc);
454 if (shouldSuppressRefs())
455 markEntityOccurrenceInFile(SuperD, SuperLoc);
458 ObjCProtocolList EmptyProtoList;
459 ObjCProtocolListInfo ProtInfo(D->isThisDeclarationADefinition()
460 ? D->getReferencedProtocols()
464 ObjCInterfaceDeclInfo InterInfo(D);
465 InterInfo.ObjCProtoListInfo = ProtInfo.getListInfo();
466 InterInfo.ObjCInterDeclInfo.containerInfo = &InterInfo.ObjCContDeclInfo;
467 InterInfo.ObjCInterDeclInfo.superInfo = D->getSuperClass() ? &BaseClass : 0;
468 InterInfo.ObjCInterDeclInfo.protocols = &InterInfo.ObjCProtoListInfo;
470 return handleObjCContainer(D, D->getLocation(), getCursor(D), InterInfo);
473 bool IndexingContext::handleObjCImplementation(
474 const ObjCImplementationDecl *D) {
475 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/false,
476 /*isRedeclaration=*/true,
477 /*isImplementation=*/true);
478 return handleObjCContainer(D, D->getLocation(), getCursor(D), ContDInfo);
481 bool IndexingContext::handleObjCProtocol(const ObjCProtocolDecl *D) {
482 if (!D->isThisDeclarationADefinition()) {
483 if (shouldSuppressRefs() && markEntityOccurrenceInFile(D, D->getLocation()))
484 return false; // already occurred.
486 // FIXME: This seems like the wrong definition for redeclaration.
487 bool isRedeclaration = D->hasDefinition() || D->getPreviousDecl();
488 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true,
490 /*isImplementation=*/false);
491 return handleObjCContainer(D, D->getLocation(),
492 MakeCursorObjCProtocolRef(D, D->getLocation(),
497 ScratchAlloc SA(*this);
498 ObjCProtocolList EmptyProtoList;
499 ObjCProtocolListInfo ProtListInfo(D->isThisDeclarationADefinition()
500 ? D->getReferencedProtocols()
504 ObjCProtocolDeclInfo ProtInfo(D);
505 ProtInfo.ObjCProtoRefListInfo = ProtListInfo.getListInfo();
507 return handleObjCContainer(D, D->getLocation(), getCursor(D), ProtInfo);
510 bool IndexingContext::handleObjCCategory(const ObjCCategoryDecl *D) {
511 ScratchAlloc SA(*this);
513 ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/false);
514 EntityInfo ClassEntity;
515 const ObjCInterfaceDecl *IFaceD = D->getClassInterface();
516 SourceLocation ClassLoc = D->getLocation();
517 SourceLocation CategoryLoc = D->IsClassExtension() ? ClassLoc
518 : D->getCategoryNameLoc();
519 getEntityInfo(IFaceD, ClassEntity, SA);
521 if (shouldSuppressRefs())
522 markEntityOccurrenceInFile(IFaceD, ClassLoc);
524 ObjCProtocolListInfo ProtInfo(D->getReferencedProtocols(), *this, SA);
526 CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo;
528 CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity;
529 CatDInfo.ObjCCatDeclInfo.classCursor =
530 MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU);
532 CatDInfo.ObjCCatDeclInfo.objcClass = 0;
533 CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor();
535 CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc);
536 CatDInfo.ObjCProtoListInfo = ProtInfo.getListInfo();
537 CatDInfo.ObjCCatDeclInfo.protocols = &CatDInfo.ObjCProtoListInfo;
539 return handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo);
542 bool IndexingContext::handleObjCCategoryImpl(const ObjCCategoryImplDecl *D) {
543 ScratchAlloc SA(*this);
545 const ObjCCategoryDecl *CatD = D->getCategoryDecl();
546 ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/true);
547 EntityInfo ClassEntity;
548 const ObjCInterfaceDecl *IFaceD = CatD->getClassInterface();
549 SourceLocation ClassLoc = D->getLocation();
550 SourceLocation CategoryLoc = D->getCategoryNameLoc();
551 getEntityInfo(IFaceD, ClassEntity, SA);
553 if (shouldSuppressRefs())
554 markEntityOccurrenceInFile(IFaceD, ClassLoc);
556 CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo;
558 CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity;
559 CatDInfo.ObjCCatDeclInfo.classCursor =
560 MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU);
562 CatDInfo.ObjCCatDeclInfo.objcClass = 0;
563 CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor();
565 CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc);
566 CatDInfo.ObjCCatDeclInfo.protocols = 0;
568 return handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo);
571 bool IndexingContext::handleObjCMethod(const ObjCMethodDecl *D) {
572 bool isDef = D->isThisDeclarationADefinition();
573 bool isContainer = isDef;
574 bool isSkipped = false;
575 if (D->hasSkippedBody()) {
581 DeclInfo DInfo(!D->isCanonicalDecl(), isDef, isContainer);
583 DInfo.flags |= CXIdxDeclFlag_Skipped;
584 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
587 bool IndexingContext::handleSynthesizedObjCProperty(
588 const ObjCPropertyImplDecl *D) {
589 ObjCPropertyDecl *PD = D->getPropertyDecl();
590 return handleReference(PD, D->getLocation(), getCursor(D), 0, D->getDeclContext());
593 bool IndexingContext::handleSynthesizedObjCMethod(const ObjCMethodDecl *D,
595 const DeclContext *LexicalDC) {
596 DeclInfo DInfo(/*isRedeclaration=*/true, /*isDefinition=*/true,
597 /*isContainer=*/false);
598 return handleDecl(D, Loc, getCursor(D), DInfo, LexicalDC);
601 bool IndexingContext::handleObjCProperty(const ObjCPropertyDecl *D) {
602 ScratchAlloc SA(*this);
604 ObjCPropertyDeclInfo DInfo;
605 EntityInfo GetterEntity;
606 EntityInfo SetterEntity;
608 DInfo.ObjCPropDeclInfo.declInfo = &DInfo;
610 if (ObjCMethodDecl *Getter = D->getGetterMethodDecl()) {
611 getEntityInfo(Getter, GetterEntity, SA);
612 DInfo.ObjCPropDeclInfo.getter = &GetterEntity;
614 DInfo.ObjCPropDeclInfo.getter = 0;
616 if (ObjCMethodDecl *Setter = D->getSetterMethodDecl()) {
617 getEntityInfo(Setter, SetterEntity, SA);
618 DInfo.ObjCPropDeclInfo.setter = &SetterEntity;
620 DInfo.ObjCPropDeclInfo.setter = 0;
623 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
626 bool IndexingContext::handleNamespace(const NamespaceDecl *D) {
627 DeclInfo DInfo(/*isRedeclaration=*/!D->isOriginalNamespace(),
628 /*isDefinition=*/true,
629 /*isContainer=*/true);
630 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
633 bool IndexingContext::handleClassTemplate(const ClassTemplateDecl *D) {
634 return handleCXXRecordDecl(D->getTemplatedDecl(), D);
637 bool IndexingContext::handleFunctionTemplate(const FunctionTemplateDecl *D) {
638 DeclInfo DInfo(/*isRedeclaration=*/!D->isCanonicalDecl(),
639 /*isDefinition=*/D->isThisDeclarationADefinition(),
640 /*isContainer=*/D->isThisDeclarationADefinition());
641 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
644 bool IndexingContext::handleTypeAliasTemplate(const TypeAliasTemplateDecl *D) {
645 DeclInfo DInfo(/*isRedeclaration=*/!D->isCanonicalDecl(),
646 /*isDefinition=*/true, /*isContainer=*/false);
647 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
650 bool IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc,
651 const NamedDecl *Parent,
652 const DeclContext *DC,
654 CXIdxEntityRefKind Kind) {
658 CXCursor Cursor = E ? MakeCXCursor(E, cast<Decl>(DC), CXTU)
659 : getRefCursor(D, Loc);
660 return handleReference(D, Loc, Cursor, Parent, DC, E, Kind);
663 bool IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc,
665 const NamedDecl *Parent,
666 const DeclContext *DC,
668 CXIdxEntityRefKind Kind) {
669 if (!CB.indexEntityReference)
676 if (!shouldIndexFunctionLocalSymbols() && isFunctionLocalDecl(D))
678 if (isNotFromSourceFile(D->getLocation()))
680 if (D->isImplicit() && shouldIgnoreIfImplicit(D))
683 if (shouldSuppressRefs()) {
684 if (markEntityOccurrenceInFile(D, Loc))
685 return false; // already occurred.
688 ScratchAlloc SA(*this);
689 EntityInfo RefEntity, ParentEntity;
690 getEntityInfo(D, RefEntity, SA);
694 getEntityInfo(Parent, ParentEntity, SA);
696 ContainerInfo Container;
697 getContainerInfo(DC, Container);
699 CXIdxEntityRefInfo Info = { Kind,
703 Parent ? &ParentEntity : 0,
705 CB.indexEntityReference(ClientData, &Info);
709 bool IndexingContext::isNotFromSourceFile(SourceLocation Loc) const {
712 SourceManager &SM = Ctx->getSourceManager();
713 SourceLocation FileLoc = SM.getFileLoc(Loc);
714 FileID FID = SM.getFileID(FileLoc);
715 return SM.getFileEntryForID(FID) == 0;
718 void IndexingContext::addContainerInMap(const DeclContext *DC,
719 CXIdxClientContainer container) {
723 ContainerMapTy::iterator I = ContainerMap.find(DC);
724 if (I == ContainerMap.end()) {
726 ContainerMap[DC] = container;
729 // Allow changing the container of a previously seen DeclContext so we
730 // can handle invalid user code, like a function re-definition.
732 I->second = container;
734 ContainerMap.erase(I);
737 CXIdxClientEntity IndexingContext::getClientEntity(const Decl *D) const {
740 EntityMapTy::const_iterator I = EntityMap.find(D);
741 if (I == EntityMap.end())
746 void IndexingContext::setClientEntity(const Decl *D, CXIdxClientEntity client) {
749 EntityMap[D] = client;
752 bool IndexingContext::handleCXXRecordDecl(const CXXRecordDecl *RD,
753 const NamedDecl *OrigD) {
754 if (RD->isThisDeclarationADefinition()) {
755 ScratchAlloc SA(*this);
756 CXXClassDeclInfo CXXDInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(),
757 /*isDefinition=*/RD->isThisDeclarationADefinition());
758 CXXBasesListInfo BaseList(RD, *this, SA);
759 CXXDInfo.CXXClassInfo.declInfo = &CXXDInfo;
760 CXXDInfo.CXXClassInfo.bases = BaseList.getBases();
761 CXXDInfo.CXXClassInfo.numBases = BaseList.getNumBases();
763 if (shouldSuppressRefs()) {
764 // Go through bases and mark them as referenced.
765 for (unsigned i = 0, e = BaseList.getNumBases(); i != e; ++i) {
766 const CXIdxBaseClassInfo *baseInfo = BaseList.getBases()[i];
767 if (baseInfo->base) {
768 const NamedDecl *BaseD = BaseList.BaseEntities[i].Dcl;
770 Loc = SourceLocation::getFromRawEncoding(baseInfo->loc.int_data);
771 markEntityOccurrenceInFile(BaseD, Loc);
776 return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), CXXDInfo);
779 DeclInfo DInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(),
780 /*isDefinition=*/RD->isThisDeclarationADefinition(),
781 /*isContainer=*/RD->isThisDeclarationADefinition());
782 return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), DInfo);
785 bool IndexingContext::markEntityOccurrenceInFile(const NamedDecl *D,
786 SourceLocation Loc) {
787 if (!D || Loc.isInvalid())
790 SourceManager &SM = Ctx->getSourceManager();
791 D = getEntityDecl(D);
793 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SM.getFileLoc(Loc));
794 FileID FID = LocInfo.first;
798 const FileEntry *FE = SM.getFileEntryForID(FID);
801 RefFileOccurence RefOccur(FE, D);
802 std::pair<llvm::DenseSet<RefFileOccurence>::iterator, bool>
803 res = RefFileOccurences.insert(RefOccur);
805 return true; // already in map.
810 const NamedDecl *IndexingContext::getEntityDecl(const NamedDecl *D) const {
812 D = cast<NamedDecl>(D->getCanonicalDecl());
814 if (const ObjCImplementationDecl *
815 ImplD = dyn_cast<ObjCImplementationDecl>(D)) {
816 return getEntityDecl(ImplD->getClassInterface());
818 } else if (const ObjCCategoryImplDecl *
819 CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) {
820 return getEntityDecl(CatImplD->getCategoryDecl());
821 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
822 if (FunctionTemplateDecl *TemplD = FD->getDescribedFunctionTemplate())
823 return getEntityDecl(TemplD);
824 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
825 if (ClassTemplateDecl *TemplD = RD->getDescribedClassTemplate())
826 return getEntityDecl(TemplD);
833 IndexingContext::getEntityContainer(const Decl *D) const {
834 const DeclContext *DC = dyn_cast<DeclContext>(D);
838 if (const ClassTemplateDecl *ClassTempl = dyn_cast<ClassTemplateDecl>(D)) {
839 DC = ClassTempl->getTemplatedDecl();
840 } else if (const FunctionTemplateDecl *
841 FuncTempl = dyn_cast<FunctionTemplateDecl>(D)) {
842 DC = FuncTempl->getTemplatedDecl();
849 IndexingContext::getClientContainerForDC(const DeclContext *DC) const {
853 ContainerMapTy::const_iterator I = ContainerMap.find(DC);
854 if (I == ContainerMap.end())
860 CXIdxClientFile IndexingContext::getIndexFile(const FileEntry *File) {
864 FileMapTy::iterator FI = FileMap.find(File);
865 if (FI != FileMap.end())
871 CXIdxLoc IndexingContext::getIndexLoc(SourceLocation Loc) const {
872 CXIdxLoc idxLoc = { {0, 0}, 0 };
876 idxLoc.ptr_data[0] = const_cast<IndexingContext *>(this);
877 idxLoc.int_data = Loc.getRawEncoding();
881 void IndexingContext::translateLoc(SourceLocation Loc,
882 CXIdxClientFile *indexFile, CXFile *file,
883 unsigned *line, unsigned *column,
888 SourceManager &SM = Ctx->getSourceManager();
889 Loc = SM.getFileLoc(Loc);
891 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
892 FileID FID = LocInfo.first;
893 unsigned FileOffset = LocInfo.second;
898 const FileEntry *FE = SM.getFileEntryForID(FID);
900 *indexFile = getIndexFile(FE);
902 *file = const_cast<FileEntry *>(FE);
904 *line = SM.getLineNumber(FID, FileOffset);
906 *column = SM.getColumnNumber(FID, FileOffset);
908 *offset = FileOffset;
911 void IndexingContext::getEntityInfo(const NamedDecl *D,
912 EntityInfo &EntityInfo,
917 D = getEntityDecl(D);
918 EntityInfo.cursor = getCursor(D);
920 EntityInfo.IndexCtx = this;
921 EntityInfo.kind = CXIdxEntity_Unexposed;
922 EntityInfo.templateKind = CXIdxEntity_NonTemplate;
923 EntityInfo.lang = CXIdxEntityLang_C;
926 EntityInfo.AttrList = AttrListInfo::create(D, *this);
927 EntityInfo.attributes = EntityInfo.AttrList->getAttrs();
928 EntityInfo.numAttributes = EntityInfo.AttrList->getNumAttrs();
931 if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
932 switch (TD->getTagKind()) {
934 EntityInfo.kind = CXIdxEntity_Struct; break;
936 EntityInfo.kind = CXIdxEntity_Union; break;
938 EntityInfo.kind = CXIdxEntity_CXXClass;
939 EntityInfo.lang = CXIdxEntityLang_CXX;
942 EntityInfo.kind = CXIdxEntity_CXXInterface;
943 EntityInfo.lang = CXIdxEntityLang_CXX;
946 EntityInfo.kind = CXIdxEntity_Enum; break;
949 if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D))
950 if (!CXXRec->isCLike())
951 EntityInfo.lang = CXIdxEntityLang_CXX;
953 if (isa<ClassTemplatePartialSpecializationDecl>(D)) {
954 EntityInfo.templateKind = CXIdxEntity_TemplatePartialSpecialization;
955 } else if (isa<ClassTemplateSpecializationDecl>(D)) {
956 EntityInfo.templateKind = CXIdxEntity_TemplateSpecialization;
960 switch (D->getKind()) {
962 EntityInfo.kind = CXIdxEntity_Typedef; break;
964 EntityInfo.kind = CXIdxEntity_Function;
967 EntityInfo.kind = CXIdxEntity_Variable;
970 EntityInfo.kind = CXIdxEntity_Variable;
971 if (isa<CXXRecordDecl>(D->getDeclContext())) {
972 EntityInfo.kind = CXIdxEntity_CXXStaticVariable;
973 EntityInfo.lang = CXIdxEntityLang_CXX;
977 EntityInfo.kind = CXIdxEntity_Field;
978 if (const CXXRecordDecl *
979 CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
980 // FIXME: isPOD check is not sufficient, a POD can contain methods,
981 // we want a isCStructLike check.
982 if (!CXXRec->isPOD())
983 EntityInfo.lang = CXIdxEntityLang_CXX;
986 case Decl::EnumConstant:
987 EntityInfo.kind = CXIdxEntity_EnumConstant; break;
988 case Decl::ObjCInterface:
989 EntityInfo.kind = CXIdxEntity_ObjCClass;
990 EntityInfo.lang = CXIdxEntityLang_ObjC;
992 case Decl::ObjCProtocol:
993 EntityInfo.kind = CXIdxEntity_ObjCProtocol;
994 EntityInfo.lang = CXIdxEntityLang_ObjC;
996 case Decl::ObjCCategory:
997 EntityInfo.kind = CXIdxEntity_ObjCCategory;
998 EntityInfo.lang = CXIdxEntityLang_ObjC;
1000 case Decl::ObjCMethod:
1001 if (cast<ObjCMethodDecl>(D)->isInstanceMethod())
1002 EntityInfo.kind = CXIdxEntity_ObjCInstanceMethod;
1004 EntityInfo.kind = CXIdxEntity_ObjCClassMethod;
1005 EntityInfo.lang = CXIdxEntityLang_ObjC;
1007 case Decl::ObjCProperty:
1008 EntityInfo.kind = CXIdxEntity_ObjCProperty;
1009 EntityInfo.lang = CXIdxEntityLang_ObjC;
1011 case Decl::ObjCIvar:
1012 EntityInfo.kind = CXIdxEntity_ObjCIvar;
1013 EntityInfo.lang = CXIdxEntityLang_ObjC;
1015 case Decl::Namespace:
1016 EntityInfo.kind = CXIdxEntity_CXXNamespace;
1017 EntityInfo.lang = CXIdxEntityLang_CXX;
1019 case Decl::NamespaceAlias:
1020 EntityInfo.kind = CXIdxEntity_CXXNamespaceAlias;
1021 EntityInfo.lang = CXIdxEntityLang_CXX;
1023 case Decl::CXXConstructor:
1024 EntityInfo.kind = CXIdxEntity_CXXConstructor;
1025 EntityInfo.lang = CXIdxEntityLang_CXX;
1027 case Decl::CXXDestructor:
1028 EntityInfo.kind = CXIdxEntity_CXXDestructor;
1029 EntityInfo.lang = CXIdxEntityLang_CXX;
1031 case Decl::CXXConversion:
1032 EntityInfo.kind = CXIdxEntity_CXXConversionFunction;
1033 EntityInfo.lang = CXIdxEntityLang_CXX;
1035 case Decl::CXXMethod: {
1036 const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
1038 EntityInfo.kind = CXIdxEntity_CXXStaticMethod;
1040 EntityInfo.kind = CXIdxEntity_CXXInstanceMethod;
1041 EntityInfo.lang = CXIdxEntityLang_CXX;
1044 case Decl::ClassTemplate:
1045 EntityInfo.kind = CXIdxEntity_CXXClass;
1046 EntityInfo.templateKind = CXIdxEntity_Template;
1048 case Decl::FunctionTemplate:
1049 EntityInfo.kind = CXIdxEntity_Function;
1050 EntityInfo.templateKind = CXIdxEntity_Template;
1051 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
1052 cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
1053 if (isa<CXXConstructorDecl>(MD))
1054 EntityInfo.kind = CXIdxEntity_CXXConstructor;
1055 else if (isa<CXXDestructorDecl>(MD))
1056 EntityInfo.kind = CXIdxEntity_CXXDestructor;
1057 else if (isa<CXXConversionDecl>(MD))
1058 EntityInfo.kind = CXIdxEntity_CXXConversionFunction;
1061 EntityInfo.kind = CXIdxEntity_CXXStaticMethod;
1063 EntityInfo.kind = CXIdxEntity_CXXInstanceMethod;
1067 case Decl::TypeAliasTemplate:
1068 EntityInfo.kind = CXIdxEntity_CXXTypeAlias;
1069 EntityInfo.templateKind = CXIdxEntity_Template;
1071 case Decl::TypeAlias:
1072 EntityInfo.kind = CXIdxEntity_CXXTypeAlias;
1073 EntityInfo.lang = CXIdxEntityLang_CXX;
1080 if (EntityInfo.kind == CXIdxEntity_Unexposed)
1083 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1084 if (FD->getTemplatedKind() ==
1085 FunctionDecl::TK_FunctionTemplateSpecialization)
1086 EntityInfo.templateKind = CXIdxEntity_TemplateSpecialization;
1089 if (EntityInfo.templateKind != CXIdxEntity_NonTemplate)
1090 EntityInfo.lang = CXIdxEntityLang_CXX;
1092 if (IdentifierInfo *II = D->getIdentifier()) {
1093 EntityInfo.name = SA.toCStr(II->getName());
1095 } else if (isa<TagDecl>(D) || isa<FieldDecl>(D) || isa<NamespaceDecl>(D)) {
1096 EntityInfo.name = 0; // anonymous tag/field/namespace.
1099 SmallString<256> StrBuf;
1101 llvm::raw_svector_ostream OS(StrBuf);
1104 EntityInfo.name = SA.copyCStr(StrBuf.str());
1108 SmallString<512> StrBuf;
1109 bool Ignore = getDeclCursorUSR(D, StrBuf);
1113 EntityInfo.USR = SA.copyCStr(StrBuf.str());
1118 void IndexingContext::getContainerInfo(const DeclContext *DC,
1119 ContainerInfo &ContInfo) {
1120 ContInfo.cursor = getCursor(cast<Decl>(DC));
1122 ContInfo.IndexCtx = this;
1125 CXCursor IndexingContext::getRefCursor(const NamedDecl *D, SourceLocation Loc) {
1126 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
1127 return MakeCursorTypeRef(TD, Loc, CXTU);
1128 if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D))
1129 return MakeCursorObjCClassRef(ID, Loc, CXTU);
1130 if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D))
1131 return MakeCursorObjCProtocolRef(PD, Loc, CXTU);
1132 if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D))
1133 return MakeCursorTemplateRef(Template, Loc, CXTU);
1134 if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(D))
1135 return MakeCursorNamespaceRef(Namespace, Loc, CXTU);
1136 if (const NamespaceAliasDecl *Namespace = dyn_cast<NamespaceAliasDecl>(D))
1137 return MakeCursorNamespaceRef(Namespace, Loc, CXTU);
1138 if (const FieldDecl *Field = dyn_cast<FieldDecl>(D))
1139 return MakeCursorMemberRef(Field, Loc, CXTU);
1140 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
1141 return MakeCursorVariableRef(Var, Loc, CXTU);
1143 return clang_getNullCursor();
1146 bool IndexingContext::shouldIgnoreIfImplicit(const Decl *D) {
1147 if (isa<ObjCInterfaceDecl>(D))
1149 if (isa<ObjCCategoryDecl>(D))
1151 if (isa<ObjCIvarDecl>(D))
1153 if (isa<ObjCMethodDecl>(D))
1155 if (isa<ImportDecl>(D))
1160 bool IndexingContext::isTemplateImplicitInstantiation(const Decl *D) {
1161 if (const ClassTemplateSpecializationDecl *
1162 SD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
1163 return SD->getSpecializationKind() == TSK_ImplicitInstantiation;
1165 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1166 return FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation;