blob: 768863728a2f92c8639c28e562a8871d01598bcc [file] [log] [blame]
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001//===- 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"
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +000015#include "clang/AST/DeclCXX.h"
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +000016#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000017
18using namespace clang;
19using namespace cxindex;
20using namespace cxcursor;
21
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +000022IndexingContext::ObjCProtocolListInfo::ObjCProtocolListInfo(
23 const ObjCProtocolList &ProtList,
24 IndexingContext &IdxCtx,
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +000025 ScratchAlloc &SA) {
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +000026 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;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +000031 ProtEntities.push_back(EntityInfo());
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +000032 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);
Argyrios Kyrtzidis90fd67a2011-12-15 00:04:56 +000037
38 if (IdxCtx.suppressRefs())
39 IdxCtx.markEntityOccurrenceInFile(PD, Loc);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +000040 }
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
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +000049
50IBOutletCollectionInfo::IBOutletCollectionInfo(
51 const IBOutletCollectionInfo &other)
Argyrios Kyrtzidis0b289282012-01-20 01:38:51 +000052 : AttrInfo(CXIdxAttr_IBOutletCollection, other.cursor, other.loc, other.A) {
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +000053
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
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +000070 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()) {
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000104 IdxCtx.getEntityInfo(InterD, IBInfo.ClassInfo, SA);
105 IBInfo.IBCollInfo.objcClass = &IBInfo.ClassInfo;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000106 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
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +0000116AttrListInfo::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
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000131IndexingContext::CXXBasesListInfo::CXXBasesListInfo(const CXXRecordDecl *D,
132 IndexingContext &IdxCtx,
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +0000133 ScratchAlloc &SA) {
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000134 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());
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000138 const NamedDecl *BaseD = 0;
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +0000139 QualType T = Base.getType();
140 SourceLocation Loc = getBaseLoc(Base);
141
142 if (const TypedefType *TDT = T->getAs<TypedefType>()) {
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000143 BaseD = TDT->getDecl();
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +0000144 } 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
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000151 if (BaseD)
152 IdxCtx.getEntityInfo(BaseD, BaseEntities.back(), SA);
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000153 CXIdxBaseClassInfo BaseInfo = { 0,
154 MakeCursorCXXBaseSpecifier(&Base, IdxCtx.CXTU),
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +0000155 IdxCtx.getIndexLoc(Loc) };
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000156 BaseInfos.push_back(BaseInfo);
157 }
158
159 for (unsigned i = 0, e = BaseInfos.size(); i != e; ++i) {
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000160 if (BaseEntities[i].name && BaseEntities[i].USR)
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000161 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
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +0000168SourceLocation 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
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +0000191const char *ScratchAlloc::toCStr(StringRef Str) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000192 if (Str.empty())
193 return "";
194 if (Str.data()[Str.size()] == '\0')
195 return Str.data();
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000196 return copyCStr(Str);
197}
198
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +0000199const char *ScratchAlloc::copyCStr(StringRef Str) {
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000200 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;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000204}
205
206void IndexingContext::setASTContext(ASTContext &ctx) {
207 Ctx = &ctx;
208 static_cast<ASTUnit*>(CXTU->TUData)->setASTContext(&ctx);
209}
210
Argyrios Kyrtzidis7fe90f32012-01-17 18:48:07 +0000211void IndexingContext::setPreprocessor(Preprocessor &PP) {
212 static_cast<ASTUnit*>(CXTU->TUData)->setPreprocessor(&PP);
213}
214
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000215bool IndexingContext::shouldAbort() {
216 if (!CB.abortQuery)
217 return false;
218 return CB.abortQuery(ClientData, 0);
219}
220
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000221void IndexingContext::enteredMainFile(const FileEntry *File) {
222 if (File && CB.enteredMainFile) {
223 CXIdxClientFile idxFile = CB.enteredMainFile(ClientData, (CXFile)File, 0);
224 FileMap[File] = idxFile;
225 }
226}
227
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000228void IndexingContext::ppIncludedFile(SourceLocation hashLoc,
229 StringRef filename,
230 const FileEntry *File,
231 bool isImport, bool isAngled) {
232 if (!CB.ppIncludedFile)
233 return;
234
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +0000235 ScratchAlloc SA(*this);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000236 CXIdxIncludedFileInfo Info = { getIndexLoc(hashLoc),
237 SA.toCStr(filename),
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000238 (CXFile)File,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000239 isImport, isAngled };
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000240 CXIdxClientFile idxFile = CB.ppIncludedFile(ClientData, &Info);
241 FileMap[File] = idxFile;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000242}
243
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +0000244void IndexingContext::startedTranslationUnit() {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000245 CXIdxClientContainer idxCont = 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000246 if (CB.startedTranslationUnit)
247 idxCont = CB.startedTranslationUnit(ClientData, 0);
248 addContainerInMap(Ctx->getTranslationUnitDecl(), idxCont);
249}
250
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000251void IndexingContext::handleDiagnosticSet(CXDiagnostic CXDiagSet) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000252 if (!CB.diagnostic)
253 return;
254
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000255 CB.diagnostic(ClientData, CXDiagSet, 0);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000256}
257
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000258bool IndexingContext::handleDecl(const NamedDecl *D,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000259 SourceLocation Loc, CXCursor Cursor,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000260 DeclInfo &DInfo) {
Argyrios Kyrtzidis144b6c02011-11-18 00:26:46 +0000261 if (!CB.indexDeclaration || !D)
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000262 return false;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000263 if (D->isImplicit() && shouldIgnoreIfImplicit(D))
264 return false;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000265
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +0000266 ScratchAlloc SA(*this);
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000267 getEntityInfo(D, DInfo.EntInfo, SA);
Argyrios Kyrtzidisdb4d7a52012-01-14 02:05:51 +0000268 if ((!indexFunctionLocalSymbols() && !DInfo.EntInfo.USR)
269 || Loc.isInvalid())
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000270 return false;
Argyrios Kyrtzidis144b6c02011-11-18 00:26:46 +0000271
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +0000272 if (suppressRefs())
273 markEntityOccurrenceInFile(D, Loc);
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000274
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000275 DInfo.entityInfo = &DInfo.EntInfo;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000276 DInfo.cursor = Cursor;
277 DInfo.loc = getIndexLoc(Loc);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000278 DInfo.isImplicit = D->isImplicit();
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000279
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000280 AttrListInfo AttrList(D, *this, SA);
281 DInfo.attributes = AttrList.getAttrs();
282 DInfo.numAttributes = AttrList.getNumAttrs();
283
Argyrios Kyrtzidisb1febb62011-12-07 20:44:19 +0000284 getContainerInfo(D->getDeclContext(), DInfo.SemanticContainer);
285 getContainerInfo(D->getLexicalDeclContext(), DInfo.LexicalContainer);
286 DInfo.semanticContainer = &DInfo.SemanticContainer;
287 DInfo.lexicalContainer = &DInfo.LexicalContainer;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000288 if (DInfo.isContainer) {
289 getContainerInfo(getEntityContainer(D), DInfo.DeclAsContainer);
290 DInfo.declAsContainer = &DInfo.DeclAsContainer;
291 }
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000292
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000293 CB.indexDeclaration(ClientData, &DInfo);
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000294 return true;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000295}
296
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000297bool IndexingContext::handleObjCContainer(const ObjCContainerDecl *D,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000298 SourceLocation Loc, CXCursor Cursor,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000299 ObjCContainerDeclInfo &ContDInfo) {
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +0000300 ContDInfo.ObjCContDeclInfo.declInfo = &ContDInfo;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000301 return handleDecl(D, Loc, Cursor, ContDInfo);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000302}
303
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000304bool IndexingContext::handleFunction(const FunctionDecl *D) {
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000305 DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(),
306 D->isThisDeclarationADefinition());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000307 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000308}
309
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000310bool IndexingContext::handleVar(const VarDecl *D) {
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000311 DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(),
312 /*isContainer=*/false);
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000313 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000314}
315
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000316bool IndexingContext::handleField(const FieldDecl *D) {
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000317 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true,
318 /*isContainer=*/false);
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000319 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000320}
321
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000322bool IndexingContext::handleEnumerator(const EnumConstantDecl *D) {
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000323 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true,
324 /*isContainer=*/false);
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000325 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000326}
327
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000328bool IndexingContext::handleTagDecl(const TagDecl *D) {
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000329 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(D))
330 return handleCXXRecordDecl(CXXRD, D);
331
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000332 DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(),
333 D->isThisDeclarationADefinition());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000334 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000335}
336
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000337bool IndexingContext::handleTypedefName(const TypedefNameDecl *D) {
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000338 DeclInfo DInfo(!D->isFirstDeclaration(), /*isDefinition=*/true,
339 /*isContainer=*/false);
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000340 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000341}
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000342
Douglas Gregor375bb142011-12-27 22:43:10 +0000343bool IndexingContext::handleObjCInterface(const ObjCInterfaceDecl *D) {
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +0000344 // For @class forward declarations, suppress them the same way as references.
Douglas Gregor375bb142011-12-27 22:43:10 +0000345 if (!D->isThisDeclarationADefinition()) {
346 if (suppressRefs() && markEntityOccurrenceInFile(D, D->getLocation()))
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +0000347 return false; // already occurred.
Douglas Gregor375bb142011-12-27 22:43:10 +0000348
349 // FIXME: This seems like the wrong definition for redeclaration.
Douglas Gregoref96ee02012-01-14 16:38:05 +0000350 bool isRedeclaration = D->hasDefinition() || D->getPreviousDecl();
Douglas Gregor375bb142011-12-27 22:43:10 +0000351 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true, isRedeclaration,
352 /*isImplementation=*/false);
353 return handleObjCContainer(D, D->getLocation(),
354 MakeCursorObjCClassRef(D, D->getLocation(),
355 CXTU),
356 ContDInfo);
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +0000357 }
358
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +0000359 ScratchAlloc SA(*this);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +0000360
361 CXIdxBaseClassInfo BaseClass;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000362 EntityInfo BaseEntity;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +0000363 BaseClass.cursor = clang_getNullCursor();
364 if (ObjCInterfaceDecl *SuperD = D->getSuperClass()) {
365 getEntityInfo(SuperD, BaseEntity, SA);
366 SourceLocation SuperLoc = D->getSuperClassLoc();
367 BaseClass.base = &BaseEntity;
368 BaseClass.cursor = MakeCursorObjCSuperClassRef(SuperD, SuperLoc, CXTU);
369 BaseClass.loc = getIndexLoc(SuperLoc);
Argyrios Kyrtzidis90fd67a2011-12-15 00:04:56 +0000370
371 if (suppressRefs())
372 markEntityOccurrenceInFile(SuperD, SuperLoc);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +0000373 }
374
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000375 ObjCProtocolList EmptyProtoList;
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000376 ObjCProtocolListInfo ProtInfo(D->isThisDeclarationADefinition()
377 ? D->getReferencedProtocols()
378 : EmptyProtoList,
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000379 *this, SA);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +0000380
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000381 ObjCInterfaceDeclInfo InterInfo(D);
382 InterInfo.ObjCProtoListInfo = ProtInfo.getListInfo();
383 InterInfo.ObjCInterDeclInfo.containerInfo = &InterInfo.ObjCContDeclInfo;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +0000384 InterInfo.ObjCInterDeclInfo.superInfo = D->getSuperClass() ? &BaseClass : 0;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000385 InterInfo.ObjCInterDeclInfo.protocols = &InterInfo.ObjCProtoListInfo;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +0000386
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000387 return handleObjCContainer(D, D->getLocation(), getCursor(D), InterInfo);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000388}
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000389
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000390bool IndexingContext::handleObjCImplementation(
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000391 const ObjCImplementationDecl *D) {
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000392 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/false,
Argyrios Kyrtzidise7bbab92011-11-15 06:20:24 +0000393 /*isRedeclaration=*/true,
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000394 /*isImplementation=*/true);
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000395 return handleObjCContainer(D, D->getLocation(), getCursor(D), ContDInfo);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000396}
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000397
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000398bool IndexingContext::handleObjCProtocol(const ObjCProtocolDecl *D) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000399 if (!D->isThisDeclarationADefinition()) {
400 if (suppressRefs() && markEntityOccurrenceInFile(D, D->getLocation()))
401 return false; // already occurred.
402
403 // FIXME: This seems like the wrong definition for redeclaration.
Douglas Gregoref96ee02012-01-14 16:38:05 +0000404 bool isRedeclaration = D->hasDefinition() || D->getPreviousDecl();
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000405 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true,
406 isRedeclaration,
407 /*isImplementation=*/false);
408 return handleObjCContainer(D, D->getLocation(),
409 MakeCursorObjCProtocolRef(D, D->getLocation(),
410 CXTU),
411 ContDInfo);
412 }
413
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +0000414 ScratchAlloc SA(*this);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000415 ObjCProtocolList EmptyProtoList;
416 ObjCProtocolListInfo ProtListInfo(D->isThisDeclarationADefinition()
417 ? D->getReferencedProtocols()
418 : EmptyProtoList,
419 *this, SA);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +0000420
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000421 ObjCProtocolDeclInfo ProtInfo(D);
422 ProtInfo.ObjCProtoRefListInfo = ProtListInfo.getListInfo();
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +0000423
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000424 return handleObjCContainer(D, D->getLocation(), getCursor(D), ProtInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000425}
426
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000427bool IndexingContext::handleObjCCategory(const ObjCCategoryDecl *D) {
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000428 ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/false);
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000429 EntityInfo ClassEntity;
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +0000430 ScratchAlloc SA(*this);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +0000431 const ObjCInterfaceDecl *IFaceD = D->getClassInterface();
432 SourceLocation ClassLoc = D->getLocation();
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000433 SourceLocation CategoryLoc = D->IsClassExtension() ? ClassLoc
434 : D->getCategoryNameLoc();
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +0000435 getEntityInfo(IFaceD, ClassEntity, SA);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000436
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +0000437 if (suppressRefs())
438 markEntityOccurrenceInFile(IFaceD, ClassLoc);
439
Argyrios Kyrtzidisc10a4c82011-12-13 18:47:45 +0000440 ObjCProtocolListInfo ProtInfo(D->getReferencedProtocols(), *this, SA);
441
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +0000442 CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo;
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +0000443 if (IFaceD) {
444 CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity;
445 CatDInfo.ObjCCatDeclInfo.classCursor =
446 MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU);
447 } else {
448 CatDInfo.ObjCCatDeclInfo.objcClass = 0;
449 CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor();
450 }
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +0000451 CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc);
Argyrios Kyrtzidisc10a4c82011-12-13 18:47:45 +0000452 CatDInfo.ObjCProtoListInfo = ProtInfo.getListInfo();
453 CatDInfo.ObjCCatDeclInfo.protocols = &CatDInfo.ObjCProtoListInfo;
454
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000455 return handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000456}
457
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000458bool IndexingContext::handleObjCCategoryImpl(const ObjCCategoryImplDecl *D) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000459 const ObjCCategoryDecl *CatD = D->getCategoryDecl();
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000460 ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/true);
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000461 EntityInfo ClassEntity;
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +0000462 ScratchAlloc SA(*this);
Argyrios Kyrtzidis3e340a62011-11-16 02:35:05 +0000463 const ObjCInterfaceDecl *IFaceD = CatD->getClassInterface();
464 SourceLocation ClassLoc = D->getLocation();
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000465 SourceLocation CategoryLoc = D->getCategoryNameLoc();
Argyrios Kyrtzidis3e340a62011-11-16 02:35:05 +0000466 getEntityInfo(IFaceD, ClassEntity, SA);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000467
Argyrios Kyrtzidis05b78462012-01-23 21:28:38 +0000468 if (suppressRefs())
469 markEntityOccurrenceInFile(IFaceD, ClassLoc);
470
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +0000471 CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo;
Argyrios Kyrtzidis3e340a62011-11-16 02:35:05 +0000472 if (IFaceD) {
473 CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity;
474 CatDInfo.ObjCCatDeclInfo.classCursor =
475 MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU);
476 } else {
477 CatDInfo.ObjCCatDeclInfo.objcClass = 0;
478 CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor();
479 }
480 CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc);
Argyrios Kyrtzidisc10a4c82011-12-13 18:47:45 +0000481 CatDInfo.ObjCCatDeclInfo.protocols = 0;
482
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000483 return handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000484}
485
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000486bool IndexingContext::handleObjCMethod(const ObjCMethodDecl *D) {
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000487 DeclInfo DInfo(!D->isCanonicalDecl(), D->isThisDeclarationADefinition(),
488 D->isThisDeclarationADefinition());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000489 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000490}
491
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000492bool IndexingContext::handleSynthesizedObjCProperty(
493 const ObjCPropertyImplDecl *D) {
494 ObjCPropertyDecl *PD = D->getPropertyDecl();
495 return handleReference(PD, D->getLocation(), getCursor(D), 0, D->getDeclContext());
496}
497
498bool IndexingContext::handleSynthesizedObjCMethod(const ObjCMethodDecl *D,
499 SourceLocation Loc) {
500 DeclInfo DInfo(/*isRedeclaration=*/true, /*isDefinition=*/true,
501 /*isContainer=*/false);
502 return handleDecl(D, Loc, getCursor(D), DInfo);
503}
504
505bool IndexingContext::handleObjCProperty(const ObjCPropertyDecl *D) {
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000506 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/false,
507 /*isContainer=*/false);
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000508 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000509}
510
Argyrios Kyrtzidis68478b02011-12-07 05:52:06 +0000511bool IndexingContext::handleNamespace(const NamespaceDecl *D) {
512 DeclInfo DInfo(/*isRedeclaration=*/!D->isOriginalNamespace(),
513 /*isDefinition=*/true,
514 /*isContainer=*/true);
515 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
516}
517
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000518bool IndexingContext::handleClassTemplate(const ClassTemplateDecl *D) {
519 return handleCXXRecordDecl(D->getTemplatedDecl(), D);
520}
521
522bool IndexingContext::handleFunctionTemplate(const FunctionTemplateDecl *D) {
523 DeclInfo DInfo(/*isRedeclaration=*/!D->isCanonicalDecl(),
524 /*isDefinition=*/D->isThisDeclarationADefinition(),
525 /*isContainer=*/D->isThisDeclarationADefinition());
526 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
527}
528
529bool IndexingContext::handleTypeAliasTemplate(const TypeAliasTemplateDecl *D) {
530 DeclInfo DInfo(/*isRedeclaration=*/!D->isCanonicalDecl(),
531 /*isDefinition=*/true, /*isContainer=*/false);
532 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
533}
534
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000535bool IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000536 const NamedDecl *Parent,
537 const DeclContext *DC,
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +0000538 const Expr *E,
539 CXIdxEntityRefKind Kind) {
Argyrios Kyrtzidisd6c82092011-11-16 02:35:01 +0000540 if (!D)
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000541 return false;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000542
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000543 CXCursor Cursor = E ? MakeCXCursor(const_cast<Expr*>(E),
544 const_cast<Decl*>(cast<Decl>(DC)), CXTU)
545 : getRefCursor(D, Loc);
546 return handleReference(D, Loc, Cursor, Parent, DC, E, Kind);
547}
548
549bool IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc,
550 CXCursor Cursor,
551 const NamedDecl *Parent,
552 const DeclContext *DC,
553 const Expr *E,
554 CXIdxEntityRefKind Kind) {
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000555 if (!CB.indexEntityReference)
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000556 return false;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000557
558 if (!D)
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000559 return false;
560 if (Loc.isInvalid())
561 return false;
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +0000562 if (!indexFunctionLocalSymbols() && D->getParentFunctionOrMethod())
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000563 return false;
564 if (isNotFromSourceFile(D->getLocation()))
565 return false;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000566 if (D->isImplicit() && shouldIgnoreIfImplicit(D))
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000567 return false;
Argyrios Kyrtzidis144b6c02011-11-18 00:26:46 +0000568
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000569 if (suppressRefs()) {
570 if (markEntityOccurrenceInFile(D, Loc))
571 return false; // already occurred.
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +0000572 }
573
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +0000574 ScratchAlloc SA(*this);
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000575 EntityInfo RefEntity, ParentEntity;
576 getEntityInfo(D, RefEntity, SA);
577 if (!RefEntity.USR)
578 return false;
579
580 getEntityInfo(Parent, ParentEntity, SA);
581
582 ContainerInfo Container;
583 getContainerInfo(DC, Container);
584
Argyrios Kyrtzidisb1febb62011-12-07 20:44:19 +0000585 CXIdxEntityRefInfo Info = { Kind,
586 Cursor,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000587 getIndexLoc(Loc),
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000588 &RefEntity,
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +0000589 Parent ? &ParentEntity : 0,
Argyrios Kyrtzidisb1febb62011-12-07 20:44:19 +0000590 &Container };
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000591 CB.indexEntityReference(ClientData, &Info);
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000592 return true;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000593}
594
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000595bool IndexingContext::isNotFromSourceFile(SourceLocation Loc) const {
596 if (Loc.isInvalid())
597 return true;
598 SourceManager &SM = Ctx->getSourceManager();
599 SourceLocation FileLoc = SM.getFileLoc(Loc);
600 FileID FID = SM.getFileID(FileLoc);
601 return SM.getFileEntryForID(FID) == 0;
602}
603
604void IndexingContext::addContainerInMap(const DeclContext *DC,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000605 CXIdxClientContainer container) {
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000606 if (!DC)
607 return;
608
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000609 ContainerMapTy::iterator I = ContainerMap.find(DC);
610 if (I == ContainerMap.end()) {
611 if (container)
612 ContainerMap[DC] = container;
613 return;
614 }
615 // Allow changing the container of a previously seen DeclContext so we
616 // can handle invalid user code, like a function re-definition.
617 if (container)
618 I->second = container;
619 else
620 ContainerMap.erase(I);
621}
622
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000623CXIdxClientEntity IndexingContext::getClientEntity(const Decl *D) const {
624 if (!D)
625 return 0;
626 EntityMapTy::const_iterator I = EntityMap.find(D);
627 if (I == EntityMap.end())
628 return 0;
629 return I->second;
630}
631
632void IndexingContext::setClientEntity(const Decl *D, CXIdxClientEntity client) {
633 if (!D)
634 return;
635 EntityMap[D] = client;
636}
637
638bool IndexingContext::handleCXXRecordDecl(const CXXRecordDecl *RD,
639 const NamedDecl *OrigD) {
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000640 if (RD->isThisDeclarationADefinition()) {
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +0000641 ScratchAlloc SA(*this);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000642 CXXClassDeclInfo CXXDInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(),
643 /*isDefinition=*/RD->isThisDeclarationADefinition());
644 CXXBasesListInfo BaseList(RD, *this, SA);
645 CXXDInfo.CXXClassInfo.declInfo = &CXXDInfo;
646 CXXDInfo.CXXClassInfo.bases = BaseList.getBases();
647 CXXDInfo.CXXClassInfo.numBases = BaseList.getNumBases();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000648
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000649 return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), CXXDInfo);
650 }
651
652 DeclInfo DInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(),
653 /*isDefinition=*/RD->isThisDeclarationADefinition(),
654 /*isContainer=*/RD->isThisDeclarationADefinition());
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000655 return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), DInfo);
656}
657
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000658bool IndexingContext::markEntityOccurrenceInFile(const NamedDecl *D,
659 SourceLocation Loc) {
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +0000660 if (!D || Loc.isInvalid())
661 return true;
662
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000663 SourceManager &SM = Ctx->getSourceManager();
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000664 D = getEntityDecl(D);
665
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +0000666 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SM.getFileLoc(Loc));
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000667 FileID FID = LocInfo.first;
668 if (FID.isInvalid())
669 return true;
670
671 const FileEntry *FE = SM.getFileEntryForID(FID);
672 if (!FE)
673 return true;
674 RefFileOccurence RefOccur(FE, D);
675 std::pair<llvm::DenseSet<RefFileOccurence>::iterator, bool>
676 res = RefFileOccurences.insert(RefOccur);
677 if (!res.second)
678 return true; // already in map.
679
680 return false;
681}
682
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000683const NamedDecl *IndexingContext::getEntityDecl(const NamedDecl *D) const {
684 assert(D);
685 D = cast<NamedDecl>(D->getCanonicalDecl());
686
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000687 if (const ObjCImplementationDecl *
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000688 ImplD = dyn_cast<ObjCImplementationDecl>(D)) {
689 return getEntityDecl(ImplD->getClassInterface());
690
691 } else if (const ObjCCategoryImplDecl *
692 CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) {
693 return getEntityDecl(CatImplD->getCategoryDecl());
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000694 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
695 if (FunctionTemplateDecl *TemplD = FD->getDescribedFunctionTemplate())
696 return getEntityDecl(TemplD);
697 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
698 if (ClassTemplateDecl *TemplD = RD->getDescribedClassTemplate())
699 return getEntityDecl(TemplD);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000700 }
701
702 return D;
703}
704
705const DeclContext *
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000706IndexingContext::getEntityContainer(const Decl *D) const {
707 const DeclContext *DC = dyn_cast<DeclContext>(D);
708 if (DC)
709 return DC;
710
711 if (const ClassTemplateDecl *ClassTempl = dyn_cast<ClassTemplateDecl>(D)) {
712 DC = ClassTempl->getTemplatedDecl();
713 } if (const FunctionTemplateDecl *
714 FuncTempl = dyn_cast<FunctionTemplateDecl>(D)) {
715 DC = FuncTempl->getTemplatedDecl();
716 }
717
718 return DC;
719}
720
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000721CXIdxClientContainer
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000722IndexingContext::getClientContainerForDC(const DeclContext *DC) const {
723 if (!DC)
724 return 0;
725
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000726 ContainerMapTy::const_iterator I = ContainerMap.find(DC);
Argyrios Kyrtzidis3e340a62011-11-16 02:35:05 +0000727 if (I == ContainerMap.end())
728 return 0;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000729
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000730 return I->second;
731}
732
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000733CXIdxClientFile IndexingContext::getIndexFile(const FileEntry *File) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000734 if (!File)
735 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000736
737 FileMapTy::iterator FI = FileMap.find(File);
738 if (FI != FileMap.end())
739 return FI->second;
740
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000741 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000742}
743
744CXIdxLoc IndexingContext::getIndexLoc(SourceLocation Loc) const {
745 CXIdxLoc idxLoc = { {0, 0}, 0 };
746 if (Loc.isInvalid())
747 return idxLoc;
748
749 idxLoc.ptr_data[0] = (void*)this;
750 idxLoc.int_data = Loc.getRawEncoding();
751 return idxLoc;
752}
753
754void IndexingContext::translateLoc(SourceLocation Loc,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000755 CXIdxClientFile *indexFile, CXFile *file,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000756 unsigned *line, unsigned *column,
757 unsigned *offset) {
758 if (Loc.isInvalid())
759 return;
760
761 SourceManager &SM = Ctx->getSourceManager();
762 Loc = SM.getFileLoc(Loc);
763
764 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
765 FileID FID = LocInfo.first;
766 unsigned FileOffset = LocInfo.second;
767
768 if (FID.isInvalid())
769 return;
770
771 const FileEntry *FE = SM.getFileEntryForID(FID);
772 if (indexFile)
773 *indexFile = getIndexFile(FE);
774 if (file)
775 *file = (void *)FE;
776 if (line)
777 *line = SM.getLineNumber(FID, FileOffset);
778 if (column)
779 *column = SM.getColumnNumber(FID, FileOffset);
780 if (offset)
781 *offset = FileOffset;
782}
783
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000784void IndexingContext::getEntityInfo(const NamedDecl *D,
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000785 EntityInfo &EntityInfo,
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +0000786 ScratchAlloc &SA) {
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +0000787 if (!D)
788 return;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000789
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000790 D = getEntityDecl(D);
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000791 EntityInfo.cursor = getCursor(D);
792 EntityInfo.Dcl = D;
793 EntityInfo.IndexCtx = this;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000794 EntityInfo.kind = CXIdxEntity_Unexposed;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000795 EntityInfo.templateKind = CXIdxEntity_NonTemplate;
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +0000796 EntityInfo.lang = CXIdxEntityLang_C;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000797
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +0000798 if (D->hasAttrs()) {
799 AttrListInfo *attrs = SA.allocate<AttrListInfo>();
800 new (attrs) AttrListInfo(D, *this, SA);
801 EntityInfo.AttrList = attrs;
802 EntityInfo.attributes = attrs->getAttrs();
803 EntityInfo.numAttributes = attrs->getNumAttrs();
804 }
805
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000806 if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
807 switch (TD->getTagKind()) {
808 case TTK_Struct:
809 EntityInfo.kind = CXIdxEntity_Struct; break;
810 case TTK_Union:
811 EntityInfo.kind = CXIdxEntity_Union; break;
812 case TTK_Class:
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +0000813 EntityInfo.kind = CXIdxEntity_CXXClass;
814 EntityInfo.lang = CXIdxEntityLang_CXX;
815 break;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000816 case TTK_Enum:
817 EntityInfo.kind = CXIdxEntity_Enum; break;
818 }
819
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000820 if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D))
821 if (!CXXRec->isCLike())
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +0000822 EntityInfo.lang = CXIdxEntityLang_CXX;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000823
824 if (isa<ClassTemplatePartialSpecializationDecl>(D)) {
825 EntityInfo.templateKind = CXIdxEntity_TemplatePartialSpecialization;
826 } else if (isa<ClassTemplateSpecializationDecl>(D)) {
827 EntityInfo.templateKind = CXIdxEntity_TemplateSpecialization;
828 }
829
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000830 } else {
831 switch (D->getKind()) {
832 case Decl::Typedef:
833 EntityInfo.kind = CXIdxEntity_Typedef; break;
834 case Decl::Function:
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000835 EntityInfo.kind = CXIdxEntity_Function;
836 break;
Argyrios Kyrtzidisdb4d7a52012-01-14 02:05:51 +0000837 case Decl::ParmVar:
838 EntityInfo.kind = CXIdxEntity_Variable;
839 break;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000840 case Decl::Var:
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000841 EntityInfo.kind = CXIdxEntity_Variable;
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +0000842 if (isa<CXXRecordDecl>(D->getDeclContext())) {
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000843 EntityInfo.kind = CXIdxEntity_CXXStaticVariable;
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +0000844 EntityInfo.lang = CXIdxEntityLang_CXX;
845 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000846 break;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000847 case Decl::Field:
Argyrios Kyrtzidisda5f2d22011-12-05 22:05:28 +0000848 EntityInfo.kind = CXIdxEntity_Field;
849 if (const CXXRecordDecl *
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +0000850 CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
851 // FIXME: isPOD check is not sufficient, a POD can contain methods,
852 // we want a isCStructLike check.
Argyrios Kyrtzidisda5f2d22011-12-05 22:05:28 +0000853 if (!CXXRec->isPOD())
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +0000854 EntityInfo.lang = CXIdxEntityLang_CXX;
855 }
Argyrios Kyrtzidisda5f2d22011-12-05 22:05:28 +0000856 break;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000857 case Decl::EnumConstant:
858 EntityInfo.kind = CXIdxEntity_EnumConstant; break;
859 case Decl::ObjCInterface:
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +0000860 EntityInfo.kind = CXIdxEntity_ObjCClass;
861 EntityInfo.lang = CXIdxEntityLang_ObjC;
862 break;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000863 case Decl::ObjCProtocol:
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +0000864 EntityInfo.kind = CXIdxEntity_ObjCProtocol;
865 EntityInfo.lang = CXIdxEntityLang_ObjC;
866 break;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000867 case Decl::ObjCCategory:
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +0000868 EntityInfo.kind = CXIdxEntity_ObjCCategory;
869 EntityInfo.lang = CXIdxEntityLang_ObjC;
870 break;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000871 case Decl::ObjCMethod:
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000872 if (cast<ObjCMethodDecl>(D)->isInstanceMethod())
873 EntityInfo.kind = CXIdxEntity_ObjCInstanceMethod;
874 else
875 EntityInfo.kind = CXIdxEntity_ObjCClassMethod;
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +0000876 EntityInfo.lang = CXIdxEntityLang_ObjC;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000877 break;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000878 case Decl::ObjCProperty:
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +0000879 EntityInfo.kind = CXIdxEntity_ObjCProperty;
880 EntityInfo.lang = CXIdxEntityLang_ObjC;
881 break;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000882 case Decl::ObjCIvar:
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +0000883 EntityInfo.kind = CXIdxEntity_ObjCIvar;
884 EntityInfo.lang = CXIdxEntityLang_ObjC;
885 break;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000886 case Decl::Namespace:
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +0000887 EntityInfo.kind = CXIdxEntity_CXXNamespace;
888 EntityInfo.lang = CXIdxEntityLang_CXX;
889 break;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000890 case Decl::NamespaceAlias:
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +0000891 EntityInfo.kind = CXIdxEntity_CXXNamespaceAlias;
892 EntityInfo.lang = CXIdxEntityLang_CXX;
893 break;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000894 case Decl::CXXConstructor:
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +0000895 EntityInfo.kind = CXIdxEntity_CXXConstructor;
896 EntityInfo.lang = CXIdxEntityLang_CXX;
897 break;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000898 case Decl::CXXDestructor:
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +0000899 EntityInfo.kind = CXIdxEntity_CXXDestructor;
900 EntityInfo.lang = CXIdxEntityLang_CXX;
901 break;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000902 case Decl::CXXConversion:
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +0000903 EntityInfo.kind = CXIdxEntity_CXXConversionFunction;
904 EntityInfo.lang = CXIdxEntityLang_CXX;
905 break;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000906 case Decl::CXXMethod: {
907 const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
908 if (MD->isStatic())
909 EntityInfo.kind = CXIdxEntity_CXXStaticMethod;
910 else
911 EntityInfo.kind = CXIdxEntity_CXXInstanceMethod;
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +0000912 EntityInfo.lang = CXIdxEntityLang_CXX;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000913 break;
914 }
915 case Decl::ClassTemplate:
916 EntityInfo.kind = CXIdxEntity_CXXClass;
917 EntityInfo.templateKind = CXIdxEntity_Template;
918 break;
919 case Decl::FunctionTemplate:
920 EntityInfo.kind = CXIdxEntity_Function;
921 EntityInfo.templateKind = CXIdxEntity_Template;
922 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
923 cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
924 if (isa<CXXConstructorDecl>(MD))
925 EntityInfo.kind = CXIdxEntity_CXXConstructor;
926 else if (isa<CXXDestructorDecl>(MD))
927 EntityInfo.kind = CXIdxEntity_CXXDestructor;
928 else if (isa<CXXConversionDecl>(MD))
929 EntityInfo.kind = CXIdxEntity_CXXConversionFunction;
930 else {
931 if (MD->isStatic())
932 EntityInfo.kind = CXIdxEntity_CXXStaticMethod;
933 else
934 EntityInfo.kind = CXIdxEntity_CXXInstanceMethod;
935 }
936 }
937 break;
938 case Decl::TypeAliasTemplate:
939 EntityInfo.kind = CXIdxEntity_CXXTypeAlias;
940 EntityInfo.templateKind = CXIdxEntity_Template;
941 break;
942 case Decl::TypeAlias:
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +0000943 EntityInfo.kind = CXIdxEntity_CXXTypeAlias;
944 EntityInfo.lang = CXIdxEntityLang_CXX;
945 break;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000946 default:
947 break;
948 }
949 }
950
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000951 if (EntityInfo.kind == CXIdxEntity_Unexposed)
952 return;
953
954 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
955 if (FD->getTemplatedKind() ==
956 FunctionDecl::TK_FunctionTemplateSpecialization)
957 EntityInfo.templateKind = CXIdxEntity_TemplateSpecialization;
958 }
959
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +0000960 if (EntityInfo.templateKind != CXIdxEntity_NonTemplate)
961 EntityInfo.lang = CXIdxEntityLang_CXX;
962
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000963 if (IdentifierInfo *II = D->getIdentifier()) {
964 EntityInfo.name = SA.toCStr(II->getName());
965
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000966 } else if (isa<TagDecl>(D) || isa<FieldDecl>(D) || isa<NamespaceDecl>(D)) {
967 EntityInfo.name = 0; // anonymous tag/field/namespace.
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000968
969 } else {
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000970 llvm::SmallString<256> StrBuf;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000971 {
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000972 llvm::raw_svector_ostream OS(StrBuf);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000973 D->printName(OS);
974 }
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000975 EntityInfo.name = SA.copyCStr(StrBuf.str());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000976 }
977
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000978 {
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000979 llvm::SmallString<512> StrBuf;
980 bool Ignore = getDeclCursorUSR(D, StrBuf);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000981 if (Ignore) {
Argyrios Kyrtzidis144b6c02011-11-18 00:26:46 +0000982 EntityInfo.USR = 0;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000983 } else {
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000984 EntityInfo.USR = SA.copyCStr(StrBuf.str());
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000985 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000986 }
987}
988
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000989void IndexingContext::getContainerInfo(const DeclContext *DC,
990 ContainerInfo &ContInfo) {
991 ContInfo.cursor = getCursor(cast<Decl>(DC));
992 ContInfo.DC = DC;
993 ContInfo.IndexCtx = this;
994}
995
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000996CXCursor IndexingContext::getRefCursor(const NamedDecl *D, SourceLocation Loc) {
997 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
998 return MakeCursorTypeRef(TD, Loc, CXTU);
999 if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D))
1000 return MakeCursorObjCClassRef(ID, Loc, CXTU);
1001 if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D))
1002 return MakeCursorObjCProtocolRef(PD, Loc, CXTU);
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001003 if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D))
1004 return MakeCursorTemplateRef(Template, Loc, CXTU);
1005 if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(D))
1006 return MakeCursorNamespaceRef(Namespace, Loc, CXTU);
1007 if (const NamespaceAliasDecl *Namespace = dyn_cast<NamespaceAliasDecl>(D))
1008 return MakeCursorNamespaceRef(Namespace, Loc, CXTU);
1009 if (const FieldDecl *Field = dyn_cast<FieldDecl>(D))
1010 return MakeCursorMemberRef(Field, Loc, CXTU);
1011
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001012 return clang_getNullCursor();
1013}
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001014
1015bool IndexingContext::shouldIgnoreIfImplicit(const NamedDecl *D) {
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +00001016 if (isa<ObjCInterfaceDecl>(D))
1017 return false;
1018 if (isa<ObjCCategoryDecl>(D))
1019 return false;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001020 if (isa<ObjCIvarDecl>(D))
1021 return false;
1022 if (isa<ObjCMethodDecl>(D))
1023 return false;
1024 return true;
1025}