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 "CXTranslationUnit.h"
12 #include "CIndexDiagnostic.h"
14 #include "clang/Frontend/ASTUnit.h"
15 #include "clang/AST/DeclCXX.h"
16 #include "clang/AST/DeclTemplate.h"
18 using namespace clang;
19 using namespace cxindex;
20 using namespace cxcursor;
22 IndexingContext::ObjCProtocolListInfo::ObjCProtocolListInfo(
23 const ObjCProtocolList &ProtList,
24 IndexingContext &IdxCtx,
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);
38 if (IdxCtx.shouldSuppressRefs())
39 IdxCtx.markEntityOccurrenceInFile(PD, Loc);
42 for (unsigned i = 0, e = ProtInfos.size(); i != e; ++i)
43 ProtInfos[i].protocol = &ProtEntities[i];
45 for (unsigned i = 0, e = ProtInfos.size(); i != e; ++i)
46 Prots.push_back(&ProtInfos[i]);
50 IBOutletCollectionInfo::IBOutletCollectionInfo(
51 const IBOutletCollectionInfo &other)
52 : AttrInfo(CXIdxAttr_IBOutletCollection, other.cursor, other.loc, other.A) {
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;
61 IBCollInfo.objcClass = 0;
64 AttrListInfo::AttrListInfo(const Decl *D, IndexingContext &IdxCtx)
65 : SA(IdxCtx), ref_cnt(0) {
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());
77 Attrs.push_back(AttrInfo(CXIdxAttr_Unexposed, C, Loc, A));
79 case CXCursor_IBActionAttr:
80 Attrs.push_back(AttrInfo(CXIdxAttr_IBAction, C, Loc, A));
82 case CXCursor_IBOutletAttr:
83 Attrs.push_back(AttrInfo(CXIdxAttr_IBOutlet, C, Loc, A));
85 case CXCursor_IBOutletCollectionAttr:
86 IBCollAttrs.push_back(IBOutletCollectionInfo(C, Loc, A));
91 for (unsigned i = 0, e = IBCollAttrs.size(); i != e; ++i) {
92 IBOutletCollectionInfo &IBInfo = IBCollAttrs[i];
93 CXAttrs.push_back(&IBInfo);
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);
112 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
113 CXAttrs.push_back(&Attrs[i]);
116 IntrusiveRefCntPtr<AttrListInfo>
117 AttrListInfo::create(const Decl *D, IndexingContext &IdxCtx) {
118 ScratchAlloc SA(IdxCtx);
119 AttrListInfo *attrs = SA.allocate<AttrListInfo>();
120 return new (attrs) AttrListInfo(D, IdxCtx);
123 IndexingContext::CXXBasesListInfo::CXXBasesListInfo(const CXXRecordDecl *D,
124 IndexingContext &IdxCtx,
126 for (CXXRecordDecl::base_class_const_iterator
127 I = D->bases_begin(), E = D->bases_end(); I != E; ++I) {
128 const CXXBaseSpecifier &Base = *I;
129 BaseEntities.push_back(EntityInfo());
130 const NamedDecl *BaseD = 0;
131 QualType T = Base.getType();
132 SourceLocation Loc = getBaseLoc(Base);
134 if (const TypedefType *TDT = T->getAs<TypedefType>()) {
135 BaseD = TDT->getDecl();
136 } else if (const TemplateSpecializationType *
137 TST = T->getAs<TemplateSpecializationType>()) {
138 BaseD = TST->getTemplateName().getAsTemplateDecl();
139 } else if (const RecordType *RT = T->getAs<RecordType>()) {
140 BaseD = RT->getDecl();
144 IdxCtx.getEntityInfo(BaseD, BaseEntities.back(), SA);
145 CXIdxBaseClassInfo BaseInfo = { 0,
146 MakeCursorCXXBaseSpecifier(&Base, IdxCtx.CXTU),
147 IdxCtx.getIndexLoc(Loc) };
148 BaseInfos.push_back(BaseInfo);
151 for (unsigned i = 0, e = BaseInfos.size(); i != e; ++i) {
152 if (BaseEntities[i].name && BaseEntities[i].USR)
153 BaseInfos[i].base = &BaseEntities[i];
156 for (unsigned i = 0, e = BaseInfos.size(); i != e; ++i)
157 CXBases.push_back(&BaseInfos[i]);
160 SourceLocation IndexingContext::CXXBasesListInfo::getBaseLoc(
161 const CXXBaseSpecifier &Base) const {
162 SourceLocation Loc = Base.getSourceRange().getBegin();
164 if (Base.getTypeSourceInfo())
165 TL = Base.getTypeSourceInfo()->getTypeLoc();
169 if (const QualifiedTypeLoc *QL = dyn_cast<QualifiedTypeLoc>(&TL))
170 TL = QL->getUnqualifiedLoc();
172 if (const ElaboratedTypeLoc *EL = dyn_cast<ElaboratedTypeLoc>(&TL))
173 return EL->getNamedTypeLoc().getBeginLoc();
174 if (const DependentNameTypeLoc *DL = dyn_cast<DependentNameTypeLoc>(&TL))
175 return DL->getNameLoc();
176 if (const DependentTemplateSpecializationTypeLoc *
177 DTL = dyn_cast<DependentTemplateSpecializationTypeLoc>(&TL))
178 return DTL->getTemplateNameLoc();
183 const char *ScratchAlloc::toCStr(StringRef Str) {
186 if (Str.data()[Str.size()] == '\0')
188 return copyCStr(Str);
191 const char *ScratchAlloc::copyCStr(StringRef Str) {
192 char *buf = IdxCtx.StrScratch.Allocate<char>(Str.size() + 1);
193 std::uninitialized_copy(Str.begin(), Str.end(), buf);
194 buf[Str.size()] = '\0';
198 void IndexingContext::setASTContext(ASTContext &ctx) {
200 static_cast<ASTUnit*>(CXTU->TUData)->setASTContext(&ctx);
203 void IndexingContext::setPreprocessor(Preprocessor &PP) {
204 static_cast<ASTUnit*>(CXTU->TUData)->setPreprocessor(&PP);
207 bool IndexingContext::isFunctionLocalDecl(const Decl *D) {
210 if (!D->getParentFunctionOrMethod())
213 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
214 switch (ND->getLinkage()) {
216 case InternalLinkage:
218 case UniqueExternalLinkage:
219 case ExternalLinkage:
227 bool IndexingContext::shouldAbort() {
230 return CB.abortQuery(ClientData, 0);
233 void IndexingContext::enteredMainFile(const FileEntry *File) {
234 if (File && CB.enteredMainFile) {
235 CXIdxClientFile idxFile = CB.enteredMainFile(ClientData, (CXFile)File, 0);
236 FileMap[File] = idxFile;
240 void IndexingContext::ppIncludedFile(SourceLocation hashLoc,
242 const FileEntry *File,
243 bool isImport, bool isAngled,
244 bool isModuleImport) {
245 if (!CB.ppIncludedFile)
248 ScratchAlloc SA(*this);
249 CXIdxIncludedFileInfo Info = { getIndexLoc(hashLoc),
252 isImport, isAngled, isModuleImport };
253 CXIdxClientFile idxFile = CB.ppIncludedFile(ClientData, &Info);
254 FileMap[File] = idxFile;
257 void IndexingContext::importedModule(const ImportDecl *ImportD) {
258 if (!CB.importedASTFile)
261 Module *Mod = ImportD->getImportedModule();
264 std::string ModuleName = Mod->getFullModuleName();
266 CXIdxImportedASTFileInfo Info = {
267 (CXFile)Mod->getASTFile(),
269 getIndexLoc(ImportD->getLocation()),
270 ImportD->isImplicit()
272 CXIdxClientASTFile astFile = CB.importedASTFile(ClientData, &Info);
276 void IndexingContext::importedPCH(const FileEntry *File) {
277 if (!CB.importedASTFile)
280 CXIdxImportedASTFileInfo Info = {
283 getIndexLoc(SourceLocation()),
286 CXIdxClientASTFile astFile = CB.importedASTFile(ClientData, &Info);
290 void IndexingContext::startedTranslationUnit() {
291 CXIdxClientContainer idxCont = 0;
292 if (CB.startedTranslationUnit)
293 idxCont = CB.startedTranslationUnit(ClientData, 0);
294 addContainerInMap(Ctx->getTranslationUnitDecl(), idxCont);
297 void IndexingContext::handleDiagnosticSet(CXDiagnostic CXDiagSet) {
301 CB.diagnostic(ClientData, CXDiagSet, 0);
304 bool IndexingContext::handleDecl(const NamedDecl *D,
305 SourceLocation Loc, CXCursor Cursor,
307 const DeclContext *LexicalDC) {
308 if (!CB.indexDeclaration || !D)
310 if (D->isImplicit() && shouldIgnoreIfImplicit(D))
313 ScratchAlloc SA(*this);
314 getEntityInfo(D, DInfo.EntInfo, SA);
315 if ((!shouldIndexFunctionLocalSymbols() && !DInfo.EntInfo.USR)
320 LexicalDC = D->getLexicalDeclContext();
322 if (shouldSuppressRefs())
323 markEntityOccurrenceInFile(D, Loc);
325 DInfo.entityInfo = &DInfo.EntInfo;
326 DInfo.cursor = Cursor;
327 DInfo.loc = getIndexLoc(Loc);
328 DInfo.isImplicit = D->isImplicit();
330 DInfo.attributes = DInfo.EntInfo.attributes;
331 DInfo.numAttributes = DInfo.EntInfo.numAttributes;
333 getContainerInfo(D->getDeclContext(), DInfo.SemanticContainer);
334 DInfo.semanticContainer = &DInfo.SemanticContainer;
336 if (LexicalDC == D->getDeclContext()) {
337 DInfo.lexicalContainer = &DInfo.SemanticContainer;
338 } else if (isTemplateImplicitInstantiation(D)) {
339 // Implicit instantiations have the lexical context of where they were
340 // instantiated first. We choose instead the semantic context because:
341 // 1) at the time that we see the instantiation we have not seen the
342 // function where it occurred yet.
343 // 2) the lexical context of the first instantiation is not useful
344 // information anyway.
345 DInfo.lexicalContainer = &DInfo.SemanticContainer;
347 getContainerInfo(LexicalDC, DInfo.LexicalContainer);
348 DInfo.lexicalContainer = &DInfo.LexicalContainer;
351 if (DInfo.isContainer) {
352 getContainerInfo(getEntityContainer(D), DInfo.DeclAsContainer);
353 DInfo.declAsContainer = &DInfo.DeclAsContainer;
356 CB.indexDeclaration(ClientData, &DInfo);
360 bool IndexingContext::handleObjCContainer(const ObjCContainerDecl *D,
361 SourceLocation Loc, CXCursor Cursor,
362 ObjCContainerDeclInfo &ContDInfo) {
363 ContDInfo.ObjCContDeclInfo.declInfo = &ContDInfo;
364 return handleDecl(D, Loc, Cursor, ContDInfo);
367 bool IndexingContext::handleFunction(const FunctionDecl *D) {
368 DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(),
369 D->isThisDeclarationADefinition());
370 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
373 bool IndexingContext::handleVar(const VarDecl *D) {
374 DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(),
375 /*isContainer=*/false);
376 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
379 bool IndexingContext::handleField(const FieldDecl *D) {
380 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true,
381 /*isContainer=*/false);
382 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
385 bool IndexingContext::handleEnumerator(const EnumConstantDecl *D) {
386 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true,
387 /*isContainer=*/false);
388 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
391 bool IndexingContext::handleTagDecl(const TagDecl *D) {
392 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(D))
393 return handleCXXRecordDecl(CXXRD, D);
395 DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(),
396 D->isThisDeclarationADefinition());
397 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
400 bool IndexingContext::handleTypedefName(const TypedefNameDecl *D) {
401 DeclInfo DInfo(!D->isFirstDeclaration(), /*isDefinition=*/true,
402 /*isContainer=*/false);
403 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
406 bool IndexingContext::handleObjCInterface(const ObjCInterfaceDecl *D) {
407 // For @class forward declarations, suppress them the same way as references.
408 if (!D->isThisDeclarationADefinition()) {
409 if (shouldSuppressRefs() && markEntityOccurrenceInFile(D, D->getLocation()))
410 return false; // already occurred.
412 // FIXME: This seems like the wrong definition for redeclaration.
413 bool isRedeclaration = D->hasDefinition() || D->getPreviousDecl();
414 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true, isRedeclaration,
415 /*isImplementation=*/false);
416 return handleObjCContainer(D, D->getLocation(),
417 MakeCursorObjCClassRef(D, D->getLocation(),
422 ScratchAlloc SA(*this);
424 CXIdxBaseClassInfo BaseClass;
425 EntityInfo BaseEntity;
426 BaseClass.cursor = clang_getNullCursor();
427 if (ObjCInterfaceDecl *SuperD = D->getSuperClass()) {
428 getEntityInfo(SuperD, BaseEntity, SA);
429 SourceLocation SuperLoc = D->getSuperClassLoc();
430 BaseClass.base = &BaseEntity;
431 BaseClass.cursor = MakeCursorObjCSuperClassRef(SuperD, SuperLoc, CXTU);
432 BaseClass.loc = getIndexLoc(SuperLoc);
434 if (shouldSuppressRefs())
435 markEntityOccurrenceInFile(SuperD, SuperLoc);
438 ObjCProtocolList EmptyProtoList;
439 ObjCProtocolListInfo ProtInfo(D->isThisDeclarationADefinition()
440 ? D->getReferencedProtocols()
444 ObjCInterfaceDeclInfo InterInfo(D);
445 InterInfo.ObjCProtoListInfo = ProtInfo.getListInfo();
446 InterInfo.ObjCInterDeclInfo.containerInfo = &InterInfo.ObjCContDeclInfo;
447 InterInfo.ObjCInterDeclInfo.superInfo = D->getSuperClass() ? &BaseClass : 0;
448 InterInfo.ObjCInterDeclInfo.protocols = &InterInfo.ObjCProtoListInfo;
450 return handleObjCContainer(D, D->getLocation(), getCursor(D), InterInfo);
453 bool IndexingContext::handleObjCImplementation(
454 const ObjCImplementationDecl *D) {
455 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/false,
456 /*isRedeclaration=*/true,
457 /*isImplementation=*/true);
458 return handleObjCContainer(D, D->getLocation(), getCursor(D), ContDInfo);
461 bool IndexingContext::handleObjCProtocol(const ObjCProtocolDecl *D) {
462 if (!D->isThisDeclarationADefinition()) {
463 if (shouldSuppressRefs() && markEntityOccurrenceInFile(D, D->getLocation()))
464 return false; // already occurred.
466 // FIXME: This seems like the wrong definition for redeclaration.
467 bool isRedeclaration = D->hasDefinition() || D->getPreviousDecl();
468 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true,
470 /*isImplementation=*/false);
471 return handleObjCContainer(D, D->getLocation(),
472 MakeCursorObjCProtocolRef(D, D->getLocation(),
477 ScratchAlloc SA(*this);
478 ObjCProtocolList EmptyProtoList;
479 ObjCProtocolListInfo ProtListInfo(D->isThisDeclarationADefinition()
480 ? D->getReferencedProtocols()
484 ObjCProtocolDeclInfo ProtInfo(D);
485 ProtInfo.ObjCProtoRefListInfo = ProtListInfo.getListInfo();
487 return handleObjCContainer(D, D->getLocation(), getCursor(D), ProtInfo);
490 bool IndexingContext::handleObjCCategory(const ObjCCategoryDecl *D) {
491 ScratchAlloc SA(*this);
493 ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/false);
494 EntityInfo ClassEntity;
495 const ObjCInterfaceDecl *IFaceD = D->getClassInterface();
496 SourceLocation ClassLoc = D->getLocation();
497 SourceLocation CategoryLoc = D->IsClassExtension() ? ClassLoc
498 : D->getCategoryNameLoc();
499 getEntityInfo(IFaceD, ClassEntity, SA);
501 if (shouldSuppressRefs())
502 markEntityOccurrenceInFile(IFaceD, ClassLoc);
504 ObjCProtocolListInfo ProtInfo(D->getReferencedProtocols(), *this, SA);
506 CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo;
508 CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity;
509 CatDInfo.ObjCCatDeclInfo.classCursor =
510 MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU);
512 CatDInfo.ObjCCatDeclInfo.objcClass = 0;
513 CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor();
515 CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc);
516 CatDInfo.ObjCProtoListInfo = ProtInfo.getListInfo();
517 CatDInfo.ObjCCatDeclInfo.protocols = &CatDInfo.ObjCProtoListInfo;
519 return handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo);
522 bool IndexingContext::handleObjCCategoryImpl(const ObjCCategoryImplDecl *D) {
523 ScratchAlloc SA(*this);
525 const ObjCCategoryDecl *CatD = D->getCategoryDecl();
526 ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/true);
527 EntityInfo ClassEntity;
528 const ObjCInterfaceDecl *IFaceD = CatD->getClassInterface();
529 SourceLocation ClassLoc = D->getLocation();
530 SourceLocation CategoryLoc = D->getCategoryNameLoc();
531 getEntityInfo(IFaceD, ClassEntity, SA);
533 if (shouldSuppressRefs())
534 markEntityOccurrenceInFile(IFaceD, ClassLoc);
536 CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo;
538 CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity;
539 CatDInfo.ObjCCatDeclInfo.classCursor =
540 MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU);
542 CatDInfo.ObjCCatDeclInfo.objcClass = 0;
543 CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor();
545 CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc);
546 CatDInfo.ObjCCatDeclInfo.protocols = 0;
548 return handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo);
551 bool IndexingContext::handleObjCMethod(const ObjCMethodDecl *D) {
552 DeclInfo DInfo(!D->isCanonicalDecl(), D->isThisDeclarationADefinition(),
553 D->isThisDeclarationADefinition());
554 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
557 bool IndexingContext::handleSynthesizedObjCProperty(
558 const ObjCPropertyImplDecl *D) {
559 ObjCPropertyDecl *PD = D->getPropertyDecl();
560 return handleReference(PD, D->getLocation(), getCursor(D), 0, D->getDeclContext());
563 bool IndexingContext::handleSynthesizedObjCMethod(const ObjCMethodDecl *D,
565 const DeclContext *LexicalDC) {
566 DeclInfo DInfo(/*isRedeclaration=*/true, /*isDefinition=*/true,
567 /*isContainer=*/false);
568 return handleDecl(D, Loc, getCursor(D), DInfo, LexicalDC);
571 bool IndexingContext::handleObjCProperty(const ObjCPropertyDecl *D) {
572 ScratchAlloc SA(*this);
574 ObjCPropertyDeclInfo DInfo;
575 EntityInfo GetterEntity;
576 EntityInfo SetterEntity;
578 DInfo.ObjCPropDeclInfo.declInfo = &DInfo;
580 if (ObjCMethodDecl *Getter = D->getGetterMethodDecl()) {
581 getEntityInfo(Getter, GetterEntity, SA);
582 DInfo.ObjCPropDeclInfo.getter = &GetterEntity;
584 DInfo.ObjCPropDeclInfo.getter = 0;
586 if (ObjCMethodDecl *Setter = D->getSetterMethodDecl()) {
587 getEntityInfo(Setter, SetterEntity, SA);
588 DInfo.ObjCPropDeclInfo.setter = &SetterEntity;
590 DInfo.ObjCPropDeclInfo.setter = 0;
593 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
596 bool IndexingContext::handleNamespace(const NamespaceDecl *D) {
597 DeclInfo DInfo(/*isRedeclaration=*/!D->isOriginalNamespace(),
598 /*isDefinition=*/true,
599 /*isContainer=*/true);
600 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
603 bool IndexingContext::handleClassTemplate(const ClassTemplateDecl *D) {
604 return handleCXXRecordDecl(D->getTemplatedDecl(), D);
607 bool IndexingContext::handleFunctionTemplate(const FunctionTemplateDecl *D) {
608 DeclInfo DInfo(/*isRedeclaration=*/!D->isCanonicalDecl(),
609 /*isDefinition=*/D->isThisDeclarationADefinition(),
610 /*isContainer=*/D->isThisDeclarationADefinition());
611 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
614 bool IndexingContext::handleTypeAliasTemplate(const TypeAliasTemplateDecl *D) {
615 DeclInfo DInfo(/*isRedeclaration=*/!D->isCanonicalDecl(),
616 /*isDefinition=*/true, /*isContainer=*/false);
617 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
620 bool IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc,
621 const NamedDecl *Parent,
622 const DeclContext *DC,
624 CXIdxEntityRefKind Kind) {
628 CXCursor Cursor = E ? MakeCXCursor(const_cast<Expr*>(E),
629 const_cast<Decl*>(cast<Decl>(DC)), CXTU)
630 : getRefCursor(D, Loc);
631 return handleReference(D, Loc, Cursor, Parent, DC, E, Kind);
634 bool IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc,
636 const NamedDecl *Parent,
637 const DeclContext *DC,
639 CXIdxEntityRefKind Kind) {
640 if (!CB.indexEntityReference)
647 if (!shouldIndexFunctionLocalSymbols() && isFunctionLocalDecl(D))
649 if (isNotFromSourceFile(D->getLocation()))
651 if (D->isImplicit() && shouldIgnoreIfImplicit(D))
654 if (shouldSuppressRefs()) {
655 if (markEntityOccurrenceInFile(D, Loc))
656 return false; // already occurred.
659 ScratchAlloc SA(*this);
660 EntityInfo RefEntity, ParentEntity;
661 getEntityInfo(D, RefEntity, SA);
665 getEntityInfo(Parent, ParentEntity, SA);
667 ContainerInfo Container;
668 getContainerInfo(DC, Container);
670 CXIdxEntityRefInfo Info = { Kind,
674 Parent ? &ParentEntity : 0,
676 CB.indexEntityReference(ClientData, &Info);
680 bool IndexingContext::isNotFromSourceFile(SourceLocation Loc) const {
683 SourceManager &SM = Ctx->getSourceManager();
684 SourceLocation FileLoc = SM.getFileLoc(Loc);
685 FileID FID = SM.getFileID(FileLoc);
686 return SM.getFileEntryForID(FID) == 0;
689 void IndexingContext::addContainerInMap(const DeclContext *DC,
690 CXIdxClientContainer container) {
694 ContainerMapTy::iterator I = ContainerMap.find(DC);
695 if (I == ContainerMap.end()) {
697 ContainerMap[DC] = container;
700 // Allow changing the container of a previously seen DeclContext so we
701 // can handle invalid user code, like a function re-definition.
703 I->second = container;
705 ContainerMap.erase(I);
708 CXIdxClientEntity IndexingContext::getClientEntity(const Decl *D) const {
711 EntityMapTy::const_iterator I = EntityMap.find(D);
712 if (I == EntityMap.end())
717 void IndexingContext::setClientEntity(const Decl *D, CXIdxClientEntity client) {
720 EntityMap[D] = client;
723 bool IndexingContext::handleCXXRecordDecl(const CXXRecordDecl *RD,
724 const NamedDecl *OrigD) {
725 if (RD->isThisDeclarationADefinition()) {
726 ScratchAlloc SA(*this);
727 CXXClassDeclInfo CXXDInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(),
728 /*isDefinition=*/RD->isThisDeclarationADefinition());
729 CXXBasesListInfo BaseList(RD, *this, SA);
730 CXXDInfo.CXXClassInfo.declInfo = &CXXDInfo;
731 CXXDInfo.CXXClassInfo.bases = BaseList.getBases();
732 CXXDInfo.CXXClassInfo.numBases = BaseList.getNumBases();
734 if (shouldSuppressRefs()) {
735 // Go through bases and mark them as referenced.
736 for (unsigned i = 0, e = BaseList.getNumBases(); i != e; ++i) {
737 const CXIdxBaseClassInfo *baseInfo = BaseList.getBases()[i];
738 if (baseInfo->base) {
739 const NamedDecl *BaseD = BaseList.BaseEntities[i].Dcl;
741 Loc = SourceLocation::getFromRawEncoding(baseInfo->loc.int_data);
742 markEntityOccurrenceInFile(BaseD, Loc);
747 return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), CXXDInfo);
750 DeclInfo DInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(),
751 /*isDefinition=*/RD->isThisDeclarationADefinition(),
752 /*isContainer=*/RD->isThisDeclarationADefinition());
753 return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), DInfo);
756 bool IndexingContext::markEntityOccurrenceInFile(const NamedDecl *D,
757 SourceLocation Loc) {
758 if (!D || Loc.isInvalid())
761 SourceManager &SM = Ctx->getSourceManager();
762 D = getEntityDecl(D);
764 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SM.getFileLoc(Loc));
765 FileID FID = LocInfo.first;
769 const FileEntry *FE = SM.getFileEntryForID(FID);
772 RefFileOccurence RefOccur(FE, D);
773 std::pair<llvm::DenseSet<RefFileOccurence>::iterator, bool>
774 res = RefFileOccurences.insert(RefOccur);
776 return true; // already in map.
781 const NamedDecl *IndexingContext::getEntityDecl(const NamedDecl *D) const {
783 D = cast<NamedDecl>(D->getCanonicalDecl());
785 if (const ObjCImplementationDecl *
786 ImplD = dyn_cast<ObjCImplementationDecl>(D)) {
787 return getEntityDecl(ImplD->getClassInterface());
789 } else if (const ObjCCategoryImplDecl *
790 CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) {
791 return getEntityDecl(CatImplD->getCategoryDecl());
792 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
793 if (FunctionTemplateDecl *TemplD = FD->getDescribedFunctionTemplate())
794 return getEntityDecl(TemplD);
795 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
796 if (ClassTemplateDecl *TemplD = RD->getDescribedClassTemplate())
797 return getEntityDecl(TemplD);
804 IndexingContext::getEntityContainer(const Decl *D) const {
805 const DeclContext *DC = dyn_cast<DeclContext>(D);
809 if (const ClassTemplateDecl *ClassTempl = dyn_cast<ClassTemplateDecl>(D)) {
810 DC = ClassTempl->getTemplatedDecl();
811 } if (const FunctionTemplateDecl *
812 FuncTempl = dyn_cast<FunctionTemplateDecl>(D)) {
813 DC = FuncTempl->getTemplatedDecl();
820 IndexingContext::getClientContainerForDC(const DeclContext *DC) const {
824 ContainerMapTy::const_iterator I = ContainerMap.find(DC);
825 if (I == ContainerMap.end())
831 CXIdxClientFile IndexingContext::getIndexFile(const FileEntry *File) {
835 FileMapTy::iterator FI = FileMap.find(File);
836 if (FI != FileMap.end())
842 CXIdxLoc IndexingContext::getIndexLoc(SourceLocation Loc) const {
843 CXIdxLoc idxLoc = { {0, 0}, 0 };
847 idxLoc.ptr_data[0] = (void*)this;
848 idxLoc.int_data = Loc.getRawEncoding();
852 void IndexingContext::translateLoc(SourceLocation Loc,
853 CXIdxClientFile *indexFile, CXFile *file,
854 unsigned *line, unsigned *column,
859 SourceManager &SM = Ctx->getSourceManager();
860 Loc = SM.getFileLoc(Loc);
862 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
863 FileID FID = LocInfo.first;
864 unsigned FileOffset = LocInfo.second;
869 const FileEntry *FE = SM.getFileEntryForID(FID);
871 *indexFile = getIndexFile(FE);
875 *line = SM.getLineNumber(FID, FileOffset);
877 *column = SM.getColumnNumber(FID, FileOffset);
879 *offset = FileOffset;
882 void IndexingContext::getEntityInfo(const NamedDecl *D,
883 EntityInfo &EntityInfo,
888 D = getEntityDecl(D);
889 EntityInfo.cursor = getCursor(D);
891 EntityInfo.IndexCtx = this;
892 EntityInfo.kind = CXIdxEntity_Unexposed;
893 EntityInfo.templateKind = CXIdxEntity_NonTemplate;
894 EntityInfo.lang = CXIdxEntityLang_C;
897 EntityInfo.AttrList = AttrListInfo::create(D, *this);
898 EntityInfo.attributes = EntityInfo.AttrList->getAttrs();
899 EntityInfo.numAttributes = EntityInfo.AttrList->getNumAttrs();
902 if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
903 switch (TD->getTagKind()) {
905 EntityInfo.kind = CXIdxEntity_Struct; break;
907 EntityInfo.kind = CXIdxEntity_Union; break;
909 EntityInfo.kind = CXIdxEntity_CXXClass;
910 EntityInfo.lang = CXIdxEntityLang_CXX;
913 EntityInfo.kind = CXIdxEntity_CXXInterface;
914 EntityInfo.lang = CXIdxEntityLang_CXX;
917 EntityInfo.kind = CXIdxEntity_Enum; break;
920 if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D))
921 if (!CXXRec->isCLike())
922 EntityInfo.lang = CXIdxEntityLang_CXX;
924 if (isa<ClassTemplatePartialSpecializationDecl>(D)) {
925 EntityInfo.templateKind = CXIdxEntity_TemplatePartialSpecialization;
926 } else if (isa<ClassTemplateSpecializationDecl>(D)) {
927 EntityInfo.templateKind = CXIdxEntity_TemplateSpecialization;
931 switch (D->getKind()) {
933 EntityInfo.kind = CXIdxEntity_Typedef; break;
935 EntityInfo.kind = CXIdxEntity_Function;
938 EntityInfo.kind = CXIdxEntity_Variable;
941 EntityInfo.kind = CXIdxEntity_Variable;
942 if (isa<CXXRecordDecl>(D->getDeclContext())) {
943 EntityInfo.kind = CXIdxEntity_CXXStaticVariable;
944 EntityInfo.lang = CXIdxEntityLang_CXX;
948 EntityInfo.kind = CXIdxEntity_Field;
949 if (const CXXRecordDecl *
950 CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
951 // FIXME: isPOD check is not sufficient, a POD can contain methods,
952 // we want a isCStructLike check.
953 if (!CXXRec->isPOD())
954 EntityInfo.lang = CXIdxEntityLang_CXX;
957 case Decl::EnumConstant:
958 EntityInfo.kind = CXIdxEntity_EnumConstant; break;
959 case Decl::ObjCInterface:
960 EntityInfo.kind = CXIdxEntity_ObjCClass;
961 EntityInfo.lang = CXIdxEntityLang_ObjC;
963 case Decl::ObjCProtocol:
964 EntityInfo.kind = CXIdxEntity_ObjCProtocol;
965 EntityInfo.lang = CXIdxEntityLang_ObjC;
967 case Decl::ObjCCategory:
968 EntityInfo.kind = CXIdxEntity_ObjCCategory;
969 EntityInfo.lang = CXIdxEntityLang_ObjC;
971 case Decl::ObjCMethod:
972 if (cast<ObjCMethodDecl>(D)->isInstanceMethod())
973 EntityInfo.kind = CXIdxEntity_ObjCInstanceMethod;
975 EntityInfo.kind = CXIdxEntity_ObjCClassMethod;
976 EntityInfo.lang = CXIdxEntityLang_ObjC;
978 case Decl::ObjCProperty:
979 EntityInfo.kind = CXIdxEntity_ObjCProperty;
980 EntityInfo.lang = CXIdxEntityLang_ObjC;
983 EntityInfo.kind = CXIdxEntity_ObjCIvar;
984 EntityInfo.lang = CXIdxEntityLang_ObjC;
986 case Decl::Namespace:
987 EntityInfo.kind = CXIdxEntity_CXXNamespace;
988 EntityInfo.lang = CXIdxEntityLang_CXX;
990 case Decl::NamespaceAlias:
991 EntityInfo.kind = CXIdxEntity_CXXNamespaceAlias;
992 EntityInfo.lang = CXIdxEntityLang_CXX;
994 case Decl::CXXConstructor:
995 EntityInfo.kind = CXIdxEntity_CXXConstructor;
996 EntityInfo.lang = CXIdxEntityLang_CXX;
998 case Decl::CXXDestructor:
999 EntityInfo.kind = CXIdxEntity_CXXDestructor;
1000 EntityInfo.lang = CXIdxEntityLang_CXX;
1002 case Decl::CXXConversion:
1003 EntityInfo.kind = CXIdxEntity_CXXConversionFunction;
1004 EntityInfo.lang = CXIdxEntityLang_CXX;
1006 case Decl::CXXMethod: {
1007 const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
1009 EntityInfo.kind = CXIdxEntity_CXXStaticMethod;
1011 EntityInfo.kind = CXIdxEntity_CXXInstanceMethod;
1012 EntityInfo.lang = CXIdxEntityLang_CXX;
1015 case Decl::ClassTemplate:
1016 EntityInfo.kind = CXIdxEntity_CXXClass;
1017 EntityInfo.templateKind = CXIdxEntity_Template;
1019 case Decl::FunctionTemplate:
1020 EntityInfo.kind = CXIdxEntity_Function;
1021 EntityInfo.templateKind = CXIdxEntity_Template;
1022 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
1023 cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
1024 if (isa<CXXConstructorDecl>(MD))
1025 EntityInfo.kind = CXIdxEntity_CXXConstructor;
1026 else if (isa<CXXDestructorDecl>(MD))
1027 EntityInfo.kind = CXIdxEntity_CXXDestructor;
1028 else if (isa<CXXConversionDecl>(MD))
1029 EntityInfo.kind = CXIdxEntity_CXXConversionFunction;
1032 EntityInfo.kind = CXIdxEntity_CXXStaticMethod;
1034 EntityInfo.kind = CXIdxEntity_CXXInstanceMethod;
1038 case Decl::TypeAliasTemplate:
1039 EntityInfo.kind = CXIdxEntity_CXXTypeAlias;
1040 EntityInfo.templateKind = CXIdxEntity_Template;
1042 case Decl::TypeAlias:
1043 EntityInfo.kind = CXIdxEntity_CXXTypeAlias;
1044 EntityInfo.lang = CXIdxEntityLang_CXX;
1051 if (EntityInfo.kind == CXIdxEntity_Unexposed)
1054 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1055 if (FD->getTemplatedKind() ==
1056 FunctionDecl::TK_FunctionTemplateSpecialization)
1057 EntityInfo.templateKind = CXIdxEntity_TemplateSpecialization;
1060 if (EntityInfo.templateKind != CXIdxEntity_NonTemplate)
1061 EntityInfo.lang = CXIdxEntityLang_CXX;
1063 if (IdentifierInfo *II = D->getIdentifier()) {
1064 EntityInfo.name = SA.toCStr(II->getName());
1066 } else if (isa<TagDecl>(D) || isa<FieldDecl>(D) || isa<NamespaceDecl>(D)) {
1067 EntityInfo.name = 0; // anonymous tag/field/namespace.
1070 SmallString<256> StrBuf;
1072 llvm::raw_svector_ostream OS(StrBuf);
1075 EntityInfo.name = SA.copyCStr(StrBuf.str());
1079 SmallString<512> StrBuf;
1080 bool Ignore = getDeclCursorUSR(D, StrBuf);
1084 EntityInfo.USR = SA.copyCStr(StrBuf.str());
1089 void IndexingContext::getContainerInfo(const DeclContext *DC,
1090 ContainerInfo &ContInfo) {
1091 ContInfo.cursor = getCursor(cast<Decl>(DC));
1093 ContInfo.IndexCtx = this;
1096 CXCursor IndexingContext::getRefCursor(const NamedDecl *D, SourceLocation Loc) {
1097 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
1098 return MakeCursorTypeRef(TD, Loc, CXTU);
1099 if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D))
1100 return MakeCursorObjCClassRef(ID, Loc, CXTU);
1101 if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D))
1102 return MakeCursorObjCProtocolRef(PD, Loc, CXTU);
1103 if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D))
1104 return MakeCursorTemplateRef(Template, Loc, CXTU);
1105 if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(D))
1106 return MakeCursorNamespaceRef(Namespace, Loc, CXTU);
1107 if (const NamespaceAliasDecl *Namespace = dyn_cast<NamespaceAliasDecl>(D))
1108 return MakeCursorNamespaceRef(Namespace, Loc, CXTU);
1109 if (const FieldDecl *Field = dyn_cast<FieldDecl>(D))
1110 return MakeCursorMemberRef(Field, Loc, CXTU);
1111 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
1112 return MakeCursorVariableRef(Var, Loc, CXTU);
1114 return clang_getNullCursor();
1117 bool IndexingContext::shouldIgnoreIfImplicit(const Decl *D) {
1118 if (isa<ObjCInterfaceDecl>(D))
1120 if (isa<ObjCCategoryDecl>(D))
1122 if (isa<ObjCIvarDecl>(D))
1124 if (isa<ObjCMethodDecl>(D))
1126 if (isa<ImportDecl>(D))
1131 bool IndexingContext::isTemplateImplicitInstantiation(const Decl *D) {
1132 if (const ClassTemplateSpecializationDecl *
1133 SD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
1134 return SD->getSpecializationKind() == TSK_ImplicitInstantiation;
1136 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1137 return FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation;