1 //===- IndexingContext.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 "CIndexDiagnostic.h"
12 #include "CXTranslationUnit.h"
13 #include "clang/AST/Attr.h"
14 #include "clang/AST/DeclCXX.h"
15 #include "clang/AST/DeclTemplate.h"
16 #include "clang/Frontend/ASTUnit.h"
17
18 using namespace clang;
19 using namespace cxindex;
20 using namespace cxcursor;
21
ObjCProtocolListInfo(const ObjCProtocolList & ProtList,IndexingContext & IdxCtx,ScratchAlloc & SA)22 IndexingContext::ObjCProtocolListInfo::ObjCProtocolListInfo(
23 const ObjCProtocolList &ProtList,
24 IndexingContext &IdxCtx,
25 ScratchAlloc &SA) {
26 ObjCInterfaceDecl::protocol_loc_iterator LI = ProtList.loc_begin();
27 for (ObjCInterfaceDecl::protocol_iterator
28 I = ProtList.begin(), E = ProtList.end(); I != E; ++I, ++LI) {
29 SourceLocation Loc = *LI;
30 ObjCProtocolDecl *PD = *I;
31 ProtEntities.push_back(EntityInfo());
32 IdxCtx.getEntityInfo(PD, ProtEntities.back(), SA);
33 CXIdxObjCProtocolRefInfo ProtInfo = { nullptr,
34 MakeCursorObjCProtocolRef(PD, Loc, IdxCtx.CXTU),
35 IdxCtx.getIndexLoc(Loc) };
36 ProtInfos.push_back(ProtInfo);
37
38 if (IdxCtx.shouldSuppressRefs())
39 IdxCtx.markEntityOccurrenceInFile(PD, Loc);
40 }
41
42 for (unsigned i = 0, e = ProtInfos.size(); i != e; ++i)
43 ProtInfos[i].protocol = &ProtEntities[i];
44
45 for (unsigned i = 0, e = ProtInfos.size(); i != e; ++i)
46 Prots.push_back(&ProtInfos[i]);
47 }
48
49
IBOutletCollectionInfo(const IBOutletCollectionInfo & other)50 IBOutletCollectionInfo::IBOutletCollectionInfo(
51 const IBOutletCollectionInfo &other)
52 : AttrInfo(CXIdxAttr_IBOutletCollection, other.cursor, other.loc, other.A) {
53
54 IBCollInfo.attrInfo = this;
55 IBCollInfo.classCursor = other.IBCollInfo.classCursor;
56 IBCollInfo.classLoc = other.IBCollInfo.classLoc;
57 if (other.IBCollInfo.objcClass) {
58 ClassInfo = other.ClassInfo;
59 IBCollInfo.objcClass = &ClassInfo;
60 } else
61 IBCollInfo.objcClass = nullptr;
62 }
63
AttrListInfo(const Decl * D,IndexingContext & IdxCtx)64 AttrListInfo::AttrListInfo(const Decl *D, IndexingContext &IdxCtx)
65 : SA(IdxCtx), ref_cnt(0) {
66
67 if (!D->hasAttrs())
68 return;
69
70 for (const auto *A : D->attrs()) {
71 CXCursor C = MakeCXCursor(A, D, IdxCtx.CXTU);
72 CXIdxLoc Loc = IdxCtx.getIndexLoc(A->getLocation());
73 switch (C.kind) {
74 default:
75 Attrs.push_back(AttrInfo(CXIdxAttr_Unexposed, C, Loc, A));
76 break;
77 case CXCursor_IBActionAttr:
78 Attrs.push_back(AttrInfo(CXIdxAttr_IBAction, C, Loc, A));
79 break;
80 case CXCursor_IBOutletAttr:
81 Attrs.push_back(AttrInfo(CXIdxAttr_IBOutlet, C, Loc, A));
82 break;
83 case CXCursor_IBOutletCollectionAttr:
84 IBCollAttrs.push_back(IBOutletCollectionInfo(C, Loc, A));
85 break;
86 }
87 }
88
89 for (unsigned i = 0, e = IBCollAttrs.size(); i != e; ++i) {
90 IBOutletCollectionInfo &IBInfo = IBCollAttrs[i];
91 CXAttrs.push_back(&IBInfo);
92
93 const IBOutletCollectionAttr *
94 IBAttr = cast<IBOutletCollectionAttr>(IBInfo.A);
95 SourceLocation InterfaceLocStart =
96 IBAttr->getInterfaceLoc()->getTypeLoc().getLocStart();
97 IBInfo.IBCollInfo.attrInfo = &IBInfo;
98 IBInfo.IBCollInfo.classLoc = IdxCtx.getIndexLoc(InterfaceLocStart);
99 IBInfo.IBCollInfo.objcClass = nullptr;
100 IBInfo.IBCollInfo.classCursor = clang_getNullCursor();
101 QualType Ty = IBAttr->getInterface();
102 if (const ObjCObjectType *ObjectTy = Ty->getAs<ObjCObjectType>()) {
103 if (const ObjCInterfaceDecl *InterD = ObjectTy->getInterface()) {
104 IdxCtx.getEntityInfo(InterD, IBInfo.ClassInfo, SA);
105 IBInfo.IBCollInfo.objcClass = &IBInfo.ClassInfo;
106 IBInfo.IBCollInfo.classCursor =
107 MakeCursorObjCClassRef(InterD, InterfaceLocStart, 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
116 IntrusiveRefCntPtr<AttrListInfo>
create(const Decl * D,IndexingContext & IdxCtx)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);
121 }
122
CXXBasesListInfo(const CXXRecordDecl * D,IndexingContext & IdxCtx,ScratchAlloc & SA)123 IndexingContext::CXXBasesListInfo::CXXBasesListInfo(const CXXRecordDecl *D,
124 IndexingContext &IdxCtx,
125 ScratchAlloc &SA) {
126 for (const auto &Base : D->bases()) {
127 BaseEntities.push_back(EntityInfo());
128 const NamedDecl *BaseD = nullptr;
129 QualType T = Base.getType();
130 SourceLocation Loc = getBaseLoc(Base);
131
132 if (const TypedefType *TDT = T->getAs<TypedefType>()) {
133 BaseD = TDT->getDecl();
134 } else if (const TemplateSpecializationType *
135 TST = T->getAs<TemplateSpecializationType>()) {
136 BaseD = TST->getTemplateName().getAsTemplateDecl();
137 } else if (const RecordType *RT = T->getAs<RecordType>()) {
138 BaseD = RT->getDecl();
139 }
140
141 if (BaseD)
142 IdxCtx.getEntityInfo(BaseD, BaseEntities.back(), SA);
143 CXIdxBaseClassInfo BaseInfo = { nullptr,
144 MakeCursorCXXBaseSpecifier(&Base, IdxCtx.CXTU),
145 IdxCtx.getIndexLoc(Loc) };
146 BaseInfos.push_back(BaseInfo);
147 }
148
149 for (unsigned i = 0, e = BaseInfos.size(); i != e; ++i) {
150 if (BaseEntities[i].name && BaseEntities[i].USR)
151 BaseInfos[i].base = &BaseEntities[i];
152 }
153
154 for (unsigned i = 0, e = BaseInfos.size(); i != e; ++i)
155 CXBases.push_back(&BaseInfos[i]);
156 }
157
getBaseLoc(const CXXBaseSpecifier & Base) const158 SourceLocation IndexingContext::CXXBasesListInfo::getBaseLoc(
159 const CXXBaseSpecifier &Base) const {
160 SourceLocation Loc = Base.getSourceRange().getBegin();
161 TypeLoc TL;
162 if (Base.getTypeSourceInfo())
163 TL = Base.getTypeSourceInfo()->getTypeLoc();
164 if (TL.isNull())
165 return Loc;
166
167 if (QualifiedTypeLoc QL = TL.getAs<QualifiedTypeLoc>())
168 TL = QL.getUnqualifiedLoc();
169
170 if (ElaboratedTypeLoc EL = TL.getAs<ElaboratedTypeLoc>())
171 return EL.getNamedTypeLoc().getBeginLoc();
172 if (DependentNameTypeLoc DL = TL.getAs<DependentNameTypeLoc>())
173 return DL.getNameLoc();
174 if (DependentTemplateSpecializationTypeLoc DTL =
175 TL.getAs<DependentTemplateSpecializationTypeLoc>())
176 return DTL.getTemplateNameLoc();
177
178 return Loc;
179 }
180
toCStr(StringRef Str)181 const char *ScratchAlloc::toCStr(StringRef Str) {
182 if (Str.empty())
183 return "";
184 if (Str.data()[Str.size()] == '\0')
185 return Str.data();
186 return copyCStr(Str);
187 }
188
copyCStr(StringRef Str)189 const char *ScratchAlloc::copyCStr(StringRef Str) {
190 char *buf = IdxCtx.StrScratch.Allocate<char>(Str.size() + 1);
191 std::uninitialized_copy(Str.begin(), Str.end(), buf);
192 buf[Str.size()] = '\0';
193 return buf;
194 }
195
setASTContext(ASTContext & ctx)196 void IndexingContext::setASTContext(ASTContext &ctx) {
197 Ctx = &ctx;
198 cxtu::getASTUnit(CXTU)->setASTContext(&ctx);
199 }
200
setPreprocessor(Preprocessor & PP)201 void IndexingContext::setPreprocessor(Preprocessor &PP) {
202 cxtu::getASTUnit(CXTU)->setPreprocessor(&PP);
203 }
204
isFunctionLocalDecl(const Decl * D)205 bool IndexingContext::isFunctionLocalDecl(const Decl *D) {
206 assert(D);
207
208 if (!D->getParentFunctionOrMethod())
209 return false;
210
211 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
212 switch (ND->getFormalLinkage()) {
213 case NoLinkage:
214 case VisibleNoLinkage:
215 case InternalLinkage:
216 return true;
217 case UniqueExternalLinkage:
218 llvm_unreachable("Not a sema linkage");
219 case ExternalLinkage:
220 return false;
221 }
222 }
223
224 return true;
225 }
226
shouldAbort()227 bool IndexingContext::shouldAbort() {
228 if (!CB.abortQuery)
229 return false;
230 return CB.abortQuery(ClientData, nullptr);
231 }
232
enteredMainFile(const FileEntry * File)233 void IndexingContext::enteredMainFile(const FileEntry *File) {
234 if (File && CB.enteredMainFile) {
235 CXIdxClientFile idxFile =
236 CB.enteredMainFile(ClientData,
237 static_cast<CXFile>(const_cast<FileEntry *>(File)),
238 nullptr);
239 FileMap[File] = idxFile;
240 }
241 }
242
ppIncludedFile(SourceLocation hashLoc,StringRef filename,const FileEntry * File,bool isImport,bool isAngled,bool isModuleImport)243 void IndexingContext::ppIncludedFile(SourceLocation hashLoc,
244 StringRef filename,
245 const FileEntry *File,
246 bool isImport, bool isAngled,
247 bool isModuleImport) {
248 if (!CB.ppIncludedFile)
249 return;
250
251 ScratchAlloc SA(*this);
252 CXIdxIncludedFileInfo Info = { getIndexLoc(hashLoc),
253 SA.toCStr(filename),
254 static_cast<CXFile>(
255 const_cast<FileEntry *>(File)),
256 isImport, isAngled, isModuleImport };
257 CXIdxClientFile idxFile = CB.ppIncludedFile(ClientData, &Info);
258 FileMap[File] = idxFile;
259 }
260
importedModule(const ImportDecl * ImportD)261 void IndexingContext::importedModule(const ImportDecl *ImportD) {
262 if (!CB.importedASTFile)
263 return;
264
265 Module *Mod = ImportD->getImportedModule();
266 if (!Mod)
267 return;
268
269 CXIdxImportedASTFileInfo Info = {
270 static_cast<CXFile>(
271 const_cast<FileEntry *>(Mod->getASTFile())),
272 Mod,
273 getIndexLoc(ImportD->getLocation()),
274 ImportD->isImplicit()
275 };
276 CXIdxClientASTFile astFile = CB.importedASTFile(ClientData, &Info);
277 (void)astFile;
278 }
279
importedPCH(const FileEntry * File)280 void IndexingContext::importedPCH(const FileEntry *File) {
281 if (!CB.importedASTFile)
282 return;
283
284 CXIdxImportedASTFileInfo Info = {
285 static_cast<CXFile>(
286 const_cast<FileEntry *>(File)),
287 /*module=*/nullptr,
288 getIndexLoc(SourceLocation()),
289 /*isImplicit=*/false
290 };
291 CXIdxClientASTFile astFile = CB.importedASTFile(ClientData, &Info);
292 (void)astFile;
293 }
294
startedTranslationUnit()295 void IndexingContext::startedTranslationUnit() {
296 CXIdxClientContainer idxCont = nullptr;
297 if (CB.startedTranslationUnit)
298 idxCont = CB.startedTranslationUnit(ClientData, nullptr);
299 addContainerInMap(Ctx->getTranslationUnitDecl(), idxCont);
300 }
301
handleDiagnosticSet(CXDiagnostic CXDiagSet)302 void IndexingContext::handleDiagnosticSet(CXDiagnostic CXDiagSet) {
303 if (!CB.diagnostic)
304 return;
305
306 CB.diagnostic(ClientData, CXDiagSet, nullptr);
307 }
308
handleDecl(const NamedDecl * D,SourceLocation Loc,CXCursor Cursor,DeclInfo & DInfo,const DeclContext * LexicalDC)309 bool IndexingContext::handleDecl(const NamedDecl *D,
310 SourceLocation Loc, CXCursor Cursor,
311 DeclInfo &DInfo,
312 const DeclContext *LexicalDC) {
313 if (!CB.indexDeclaration || !D)
314 return false;
315 if (D->isImplicit() && shouldIgnoreIfImplicit(D))
316 return false;
317
318 ScratchAlloc SA(*this);
319 getEntityInfo(D, DInfo.EntInfo, SA);
320 if ((!shouldIndexFunctionLocalSymbols() && !DInfo.EntInfo.USR)
321 || Loc.isInvalid())
322 return false;
323
324 if (!LexicalDC)
325 LexicalDC = D->getLexicalDeclContext();
326
327 if (shouldSuppressRefs())
328 markEntityOccurrenceInFile(D, Loc);
329
330 DInfo.entityInfo = &DInfo.EntInfo;
331 DInfo.cursor = Cursor;
332 DInfo.loc = getIndexLoc(Loc);
333 DInfo.isImplicit = D->isImplicit();
334
335 DInfo.attributes = DInfo.EntInfo.attributes;
336 DInfo.numAttributes = DInfo.EntInfo.numAttributes;
337
338 getContainerInfo(D->getDeclContext(), DInfo.SemanticContainer);
339 DInfo.semanticContainer = &DInfo.SemanticContainer;
340
341 if (LexicalDC == D->getDeclContext()) {
342 DInfo.lexicalContainer = &DInfo.SemanticContainer;
343 } else if (isTemplateImplicitInstantiation(D)) {
344 // Implicit instantiations have the lexical context of where they were
345 // instantiated first. We choose instead the semantic context because:
346 // 1) at the time that we see the instantiation we have not seen the
347 // function where it occurred yet.
348 // 2) the lexical context of the first instantiation is not useful
349 // information anyway.
350 DInfo.lexicalContainer = &DInfo.SemanticContainer;
351 } else {
352 getContainerInfo(LexicalDC, DInfo.LexicalContainer);
353 DInfo.lexicalContainer = &DInfo.LexicalContainer;
354 }
355
356 if (DInfo.isContainer) {
357 getContainerInfo(getEntityContainer(D), DInfo.DeclAsContainer);
358 DInfo.declAsContainer = &DInfo.DeclAsContainer;
359 }
360
361 CB.indexDeclaration(ClientData, &DInfo);
362 return true;
363 }
364
handleObjCContainer(const ObjCContainerDecl * D,SourceLocation Loc,CXCursor Cursor,ObjCContainerDeclInfo & ContDInfo)365 bool IndexingContext::handleObjCContainer(const ObjCContainerDecl *D,
366 SourceLocation Loc, CXCursor Cursor,
367 ObjCContainerDeclInfo &ContDInfo) {
368 ContDInfo.ObjCContDeclInfo.declInfo = &ContDInfo;
369 return handleDecl(D, Loc, Cursor, ContDInfo);
370 }
371
handleFunction(const FunctionDecl * D)372 bool IndexingContext::handleFunction(const FunctionDecl *D) {
373 bool isDef = D->isThisDeclarationADefinition();
374 bool isContainer = isDef;
375 bool isSkipped = false;
376 if (D->hasSkippedBody()) {
377 isSkipped = true;
378 isDef = true;
379 isContainer = false;
380 }
381
382 DeclInfo DInfo(!D->isFirstDecl(), isDef, isContainer);
383 if (isSkipped)
384 DInfo.flags |= CXIdxDeclFlag_Skipped;
385 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
386 }
387
handleVar(const VarDecl * D)388 bool IndexingContext::handleVar(const VarDecl *D) {
389 DeclInfo DInfo(!D->isFirstDecl(), D->isThisDeclarationADefinition(),
390 /*isContainer=*/false);
391 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
392 }
393
handleField(const FieldDecl * D)394 bool IndexingContext::handleField(const FieldDecl *D) {
395 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true,
396 /*isContainer=*/false);
397 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
398 }
399
handleMSProperty(const MSPropertyDecl * D)400 bool IndexingContext::handleMSProperty(const MSPropertyDecl *D) {
401 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true,
402 /*isContainer=*/false);
403 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
404 }
405
handleEnumerator(const EnumConstantDecl * D)406 bool IndexingContext::handleEnumerator(const EnumConstantDecl *D) {
407 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true,
408 /*isContainer=*/false);
409 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
410 }
411
handleTagDecl(const TagDecl * D)412 bool IndexingContext::handleTagDecl(const TagDecl *D) {
413 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(D))
414 return handleCXXRecordDecl(CXXRD, D);
415
416 DeclInfo DInfo(!D->isFirstDecl(), D->isThisDeclarationADefinition(),
417 D->isThisDeclarationADefinition());
418 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
419 }
420
handleTypedefName(const TypedefNameDecl * D)421 bool IndexingContext::handleTypedefName(const TypedefNameDecl *D) {
422 DeclInfo DInfo(!D->isFirstDecl(), /*isDefinition=*/true,
423 /*isContainer=*/false);
424 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
425 }
426
handleObjCInterface(const ObjCInterfaceDecl * D)427 bool IndexingContext::handleObjCInterface(const ObjCInterfaceDecl *D) {
428 // For @class forward declarations, suppress them the same way as references.
429 if (!D->isThisDeclarationADefinition()) {
430 if (shouldSuppressRefs() && markEntityOccurrenceInFile(D, D->getLocation()))
431 return false; // already occurred.
432
433 // FIXME: This seems like the wrong definition for redeclaration.
434 bool isRedeclaration = D->hasDefinition() || D->getPreviousDecl();
435 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true, isRedeclaration,
436 /*isImplementation=*/false);
437 return handleObjCContainer(D, D->getLocation(),
438 MakeCursorObjCClassRef(D, D->getLocation(),
439 CXTU),
440 ContDInfo);
441 }
442
443 ScratchAlloc SA(*this);
444
445 CXIdxBaseClassInfo BaseClass;
446 EntityInfo BaseEntity;
447 BaseClass.cursor = clang_getNullCursor();
448 if (ObjCInterfaceDecl *SuperD = D->getSuperClass()) {
449 getEntityInfo(SuperD, BaseEntity, SA);
450 SourceLocation SuperLoc = D->getSuperClassLoc();
451 BaseClass.base = &BaseEntity;
452 BaseClass.cursor = MakeCursorObjCSuperClassRef(SuperD, SuperLoc, CXTU);
453 BaseClass.loc = getIndexLoc(SuperLoc);
454
455 if (shouldSuppressRefs())
456 markEntityOccurrenceInFile(SuperD, SuperLoc);
457 }
458
459 ObjCProtocolList EmptyProtoList;
460 ObjCProtocolListInfo ProtInfo(D->isThisDeclarationADefinition()
461 ? D->getReferencedProtocols()
462 : EmptyProtoList,
463 *this, SA);
464
465 ObjCInterfaceDeclInfo InterInfo(D);
466 InterInfo.ObjCProtoListInfo = ProtInfo.getListInfo();
467 InterInfo.ObjCInterDeclInfo.containerInfo = &InterInfo.ObjCContDeclInfo;
468 InterInfo.ObjCInterDeclInfo.superInfo = D->getSuperClass() ? &BaseClass
469 : nullptr;
470 InterInfo.ObjCInterDeclInfo.protocols = &InterInfo.ObjCProtoListInfo;
471
472 return handleObjCContainer(D, D->getLocation(), getCursor(D), InterInfo);
473 }
474
handleObjCImplementation(const ObjCImplementationDecl * D)475 bool IndexingContext::handleObjCImplementation(
476 const ObjCImplementationDecl *D) {
477 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/false,
478 /*isRedeclaration=*/true,
479 /*isImplementation=*/true);
480 return handleObjCContainer(D, D->getLocation(), getCursor(D), ContDInfo);
481 }
482
handleObjCProtocol(const ObjCProtocolDecl * D)483 bool IndexingContext::handleObjCProtocol(const ObjCProtocolDecl *D) {
484 if (!D->isThisDeclarationADefinition()) {
485 if (shouldSuppressRefs() && markEntityOccurrenceInFile(D, D->getLocation()))
486 return false; // already occurred.
487
488 // FIXME: This seems like the wrong definition for redeclaration.
489 bool isRedeclaration = D->hasDefinition() || D->getPreviousDecl();
490 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true,
491 isRedeclaration,
492 /*isImplementation=*/false);
493 return handleObjCContainer(D, D->getLocation(),
494 MakeCursorObjCProtocolRef(D, D->getLocation(),
495 CXTU),
496 ContDInfo);
497 }
498
499 ScratchAlloc SA(*this);
500 ObjCProtocolList EmptyProtoList;
501 ObjCProtocolListInfo ProtListInfo(D->isThisDeclarationADefinition()
502 ? D->getReferencedProtocols()
503 : EmptyProtoList,
504 *this, SA);
505
506 ObjCProtocolDeclInfo ProtInfo(D);
507 ProtInfo.ObjCProtoRefListInfo = ProtListInfo.getListInfo();
508
509 return handleObjCContainer(D, D->getLocation(), getCursor(D), ProtInfo);
510 }
511
handleObjCCategory(const ObjCCategoryDecl * D)512 bool IndexingContext::handleObjCCategory(const ObjCCategoryDecl *D) {
513 ScratchAlloc SA(*this);
514
515 ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/false);
516 EntityInfo ClassEntity;
517 const ObjCInterfaceDecl *IFaceD = D->getClassInterface();
518 SourceLocation ClassLoc = D->getLocation();
519 SourceLocation CategoryLoc = D->IsClassExtension() ? ClassLoc
520 : D->getCategoryNameLoc();
521 getEntityInfo(IFaceD, ClassEntity, SA);
522
523 if (shouldSuppressRefs())
524 markEntityOccurrenceInFile(IFaceD, ClassLoc);
525
526 ObjCProtocolListInfo ProtInfo(D->getReferencedProtocols(), *this, SA);
527
528 CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo;
529 if (IFaceD) {
530 CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity;
531 CatDInfo.ObjCCatDeclInfo.classCursor =
532 MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU);
533 } else {
534 CatDInfo.ObjCCatDeclInfo.objcClass = nullptr;
535 CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor();
536 }
537 CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc);
538 CatDInfo.ObjCProtoListInfo = ProtInfo.getListInfo();
539 CatDInfo.ObjCCatDeclInfo.protocols = &CatDInfo.ObjCProtoListInfo;
540
541 return handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo);
542 }
543
handleObjCCategoryImpl(const ObjCCategoryImplDecl * D)544 bool IndexingContext::handleObjCCategoryImpl(const ObjCCategoryImplDecl *D) {
545 ScratchAlloc SA(*this);
546
547 const ObjCCategoryDecl *CatD = D->getCategoryDecl();
548 ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/true);
549 EntityInfo ClassEntity;
550 const ObjCInterfaceDecl *IFaceD = CatD->getClassInterface();
551 SourceLocation ClassLoc = D->getLocation();
552 SourceLocation CategoryLoc = D->getCategoryNameLoc();
553 getEntityInfo(IFaceD, ClassEntity, SA);
554
555 if (shouldSuppressRefs())
556 markEntityOccurrenceInFile(IFaceD, ClassLoc);
557
558 CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo;
559 if (IFaceD) {
560 CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity;
561 CatDInfo.ObjCCatDeclInfo.classCursor =
562 MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU);
563 } else {
564 CatDInfo.ObjCCatDeclInfo.objcClass = nullptr;
565 CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor();
566 }
567 CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc);
568 CatDInfo.ObjCCatDeclInfo.protocols = nullptr;
569
570 return handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo);
571 }
572
handleObjCMethod(const ObjCMethodDecl * D)573 bool IndexingContext::handleObjCMethod(const ObjCMethodDecl *D) {
574 bool isDef = D->isThisDeclarationADefinition();
575 bool isContainer = isDef;
576 bool isSkipped = false;
577 if (D->hasSkippedBody()) {
578 isSkipped = true;
579 isDef = true;
580 isContainer = false;
581 }
582
583 DeclInfo DInfo(!D->isCanonicalDecl(), isDef, isContainer);
584 if (isSkipped)
585 DInfo.flags |= CXIdxDeclFlag_Skipped;
586 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
587 }
588
handleSynthesizedObjCProperty(const ObjCPropertyImplDecl * D)589 bool IndexingContext::handleSynthesizedObjCProperty(
590 const ObjCPropertyImplDecl *D) {
591 ObjCPropertyDecl *PD = D->getPropertyDecl();
592 return handleReference(PD, D->getLocation(), getCursor(D), nullptr,
593 D->getDeclContext());
594 }
595
handleSynthesizedObjCMethod(const ObjCMethodDecl * D,SourceLocation Loc,const DeclContext * LexicalDC)596 bool IndexingContext::handleSynthesizedObjCMethod(const ObjCMethodDecl *D,
597 SourceLocation Loc,
598 const DeclContext *LexicalDC) {
599 DeclInfo DInfo(/*isRedeclaration=*/true, /*isDefinition=*/true,
600 /*isContainer=*/false);
601 return handleDecl(D, Loc, getCursor(D), DInfo, LexicalDC);
602 }
603
handleObjCProperty(const ObjCPropertyDecl * D)604 bool IndexingContext::handleObjCProperty(const ObjCPropertyDecl *D) {
605 ScratchAlloc SA(*this);
606
607 ObjCPropertyDeclInfo DInfo;
608 EntityInfo GetterEntity;
609 EntityInfo SetterEntity;
610
611 DInfo.ObjCPropDeclInfo.declInfo = &DInfo;
612
613 if (ObjCMethodDecl *Getter = D->getGetterMethodDecl()) {
614 getEntityInfo(Getter, GetterEntity, SA);
615 DInfo.ObjCPropDeclInfo.getter = &GetterEntity;
616 } else {
617 DInfo.ObjCPropDeclInfo.getter = nullptr;
618 }
619 if (ObjCMethodDecl *Setter = D->getSetterMethodDecl()) {
620 getEntityInfo(Setter, SetterEntity, SA);
621 DInfo.ObjCPropDeclInfo.setter = &SetterEntity;
622 } else {
623 DInfo.ObjCPropDeclInfo.setter = nullptr;
624 }
625
626 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
627 }
628
handleNamespace(const NamespaceDecl * D)629 bool IndexingContext::handleNamespace(const NamespaceDecl *D) {
630 DeclInfo DInfo(/*isRedeclaration=*/!D->isOriginalNamespace(),
631 /*isDefinition=*/true,
632 /*isContainer=*/true);
633 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
634 }
635
handleClassTemplate(const ClassTemplateDecl * D)636 bool IndexingContext::handleClassTemplate(const ClassTemplateDecl *D) {
637 return handleCXXRecordDecl(D->getTemplatedDecl(), D);
638 }
639
handleFunctionTemplate(const FunctionTemplateDecl * D)640 bool IndexingContext::handleFunctionTemplate(const FunctionTemplateDecl *D) {
641 DeclInfo DInfo(/*isRedeclaration=*/!D->isCanonicalDecl(),
642 /*isDefinition=*/D->isThisDeclarationADefinition(),
643 /*isContainer=*/D->isThisDeclarationADefinition());
644 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
645 }
646
handleTypeAliasTemplate(const TypeAliasTemplateDecl * D)647 bool IndexingContext::handleTypeAliasTemplate(const TypeAliasTemplateDecl *D) {
648 DeclInfo DInfo(/*isRedeclaration=*/!D->isCanonicalDecl(),
649 /*isDefinition=*/true, /*isContainer=*/false);
650 return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
651 }
652
handleReference(const NamedDecl * D,SourceLocation Loc,const NamedDecl * Parent,const DeclContext * DC,const Expr * E,CXIdxEntityRefKind Kind)653 bool IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc,
654 const NamedDecl *Parent,
655 const DeclContext *DC,
656 const Expr *E,
657 CXIdxEntityRefKind Kind) {
658 if (!D)
659 return false;
660
661 CXCursor Cursor = E ? MakeCXCursor(E, cast<Decl>(DC), CXTU)
662 : getRefCursor(D, Loc);
663 return handleReference(D, Loc, Cursor, Parent, DC, E, Kind);
664 }
665
handleReference(const NamedDecl * D,SourceLocation Loc,CXCursor Cursor,const NamedDecl * Parent,const DeclContext * DC,const Expr * E,CXIdxEntityRefKind Kind)666 bool IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc,
667 CXCursor Cursor,
668 const NamedDecl *Parent,
669 const DeclContext *DC,
670 const Expr *E,
671 CXIdxEntityRefKind Kind) {
672 if (!CB.indexEntityReference)
673 return false;
674
675 if (!D)
676 return false;
677 if (Loc.isInvalid())
678 return false;
679 if (!shouldIndexFunctionLocalSymbols() && isFunctionLocalDecl(D))
680 return false;
681 if (isNotFromSourceFile(D->getLocation()))
682 return false;
683 if (D->isImplicit() && shouldIgnoreIfImplicit(D))
684 return false;
685
686 if (shouldSuppressRefs()) {
687 if (markEntityOccurrenceInFile(D, Loc))
688 return false; // already occurred.
689 }
690
691 ScratchAlloc SA(*this);
692 EntityInfo RefEntity, ParentEntity;
693 getEntityInfo(D, RefEntity, SA);
694 if (!RefEntity.USR)
695 return false;
696
697 getEntityInfo(Parent, ParentEntity, SA);
698
699 ContainerInfo Container;
700 getContainerInfo(DC, Container);
701
702 CXIdxEntityRefInfo Info = { Kind,
703 Cursor,
704 getIndexLoc(Loc),
705 &RefEntity,
706 Parent ? &ParentEntity : nullptr,
707 &Container };
708 CB.indexEntityReference(ClientData, &Info);
709 return true;
710 }
711
isNotFromSourceFile(SourceLocation Loc) const712 bool IndexingContext::isNotFromSourceFile(SourceLocation Loc) const {
713 if (Loc.isInvalid())
714 return true;
715 SourceManager &SM = Ctx->getSourceManager();
716 SourceLocation FileLoc = SM.getFileLoc(Loc);
717 FileID FID = SM.getFileID(FileLoc);
718 return SM.getFileEntryForID(FID) == nullptr;
719 }
720
addContainerInMap(const DeclContext * DC,CXIdxClientContainer container)721 void IndexingContext::addContainerInMap(const DeclContext *DC,
722 CXIdxClientContainer container) {
723 if (!DC)
724 return;
725
726 ContainerMapTy::iterator I = ContainerMap.find(DC);
727 if (I == ContainerMap.end()) {
728 if (container)
729 ContainerMap[DC] = container;
730 return;
731 }
732 // Allow changing the container of a previously seen DeclContext so we
733 // can handle invalid user code, like a function re-definition.
734 if (container)
735 I->second = container;
736 else
737 ContainerMap.erase(I);
738 }
739
getClientEntity(const Decl * D) const740 CXIdxClientEntity IndexingContext::getClientEntity(const Decl *D) const {
741 if (!D)
742 return nullptr;
743 EntityMapTy::const_iterator I = EntityMap.find(D);
744 if (I == EntityMap.end())
745 return nullptr;
746 return I->second;
747 }
748
setClientEntity(const Decl * D,CXIdxClientEntity client)749 void IndexingContext::setClientEntity(const Decl *D, CXIdxClientEntity client) {
750 if (!D)
751 return;
752 EntityMap[D] = client;
753 }
754
handleCXXRecordDecl(const CXXRecordDecl * RD,const NamedDecl * OrigD)755 bool IndexingContext::handleCXXRecordDecl(const CXXRecordDecl *RD,
756 const NamedDecl *OrigD) {
757 if (RD->isThisDeclarationADefinition()) {
758 ScratchAlloc SA(*this);
759 CXXClassDeclInfo CXXDInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(),
760 /*isDefinition=*/RD->isThisDeclarationADefinition());
761 CXXBasesListInfo BaseList(RD, *this, SA);
762 CXXDInfo.CXXClassInfo.declInfo = &CXXDInfo;
763 CXXDInfo.CXXClassInfo.bases = BaseList.getBases();
764 CXXDInfo.CXXClassInfo.numBases = BaseList.getNumBases();
765
766 if (shouldSuppressRefs()) {
767 // Go through bases and mark them as referenced.
768 for (unsigned i = 0, e = BaseList.getNumBases(); i != e; ++i) {
769 const CXIdxBaseClassInfo *baseInfo = BaseList.getBases()[i];
770 if (baseInfo->base) {
771 const NamedDecl *BaseD = BaseList.BaseEntities[i].Dcl;
772 SourceLocation
773 Loc = SourceLocation::getFromRawEncoding(baseInfo->loc.int_data);
774 markEntityOccurrenceInFile(BaseD, Loc);
775 }
776 }
777 }
778
779 return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), CXXDInfo);
780 }
781
782 DeclInfo DInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(),
783 /*isDefinition=*/RD->isThisDeclarationADefinition(),
784 /*isContainer=*/RD->isThisDeclarationADefinition());
785 return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), DInfo);
786 }
787
markEntityOccurrenceInFile(const NamedDecl * D,SourceLocation Loc)788 bool IndexingContext::markEntityOccurrenceInFile(const NamedDecl *D,
789 SourceLocation Loc) {
790 if (!D || Loc.isInvalid())
791 return true;
792
793 SourceManager &SM = Ctx->getSourceManager();
794 D = getEntityDecl(D);
795
796 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SM.getFileLoc(Loc));
797 FileID FID = LocInfo.first;
798 if (FID.isInvalid())
799 return true;
800
801 const FileEntry *FE = SM.getFileEntryForID(FID);
802 if (!FE)
803 return true;
804 RefFileOccurrence RefOccur(FE, D);
805 std::pair<llvm::DenseSet<RefFileOccurrence>::iterator, bool>
806 res = RefFileOccurrences.insert(RefOccur);
807 if (!res.second)
808 return true; // already in map.
809
810 return false;
811 }
812
getEntityDecl(const NamedDecl * D) const813 const NamedDecl *IndexingContext::getEntityDecl(const NamedDecl *D) const {
814 assert(D);
815 D = cast<NamedDecl>(D->getCanonicalDecl());
816
817 if (const ObjCImplementationDecl *
818 ImplD = dyn_cast<ObjCImplementationDecl>(D)) {
819 return getEntityDecl(ImplD->getClassInterface());
820
821 } else if (const ObjCCategoryImplDecl *
822 CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) {
823 return getEntityDecl(CatImplD->getCategoryDecl());
824 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
825 if (FunctionTemplateDecl *TemplD = FD->getDescribedFunctionTemplate())
826 return getEntityDecl(TemplD);
827 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
828 if (ClassTemplateDecl *TemplD = RD->getDescribedClassTemplate())
829 return getEntityDecl(TemplD);
830 }
831
832 return D;
833 }
834
835 const DeclContext *
getEntityContainer(const Decl * D) const836 IndexingContext::getEntityContainer(const Decl *D) const {
837 const DeclContext *DC = dyn_cast<DeclContext>(D);
838 if (DC)
839 return DC;
840
841 if (const ClassTemplateDecl *ClassTempl = dyn_cast<ClassTemplateDecl>(D)) {
842 DC = ClassTempl->getTemplatedDecl();
843 } else if (const FunctionTemplateDecl *
844 FuncTempl = dyn_cast<FunctionTemplateDecl>(D)) {
845 DC = FuncTempl->getTemplatedDecl();
846 }
847
848 return DC;
849 }
850
851 CXIdxClientContainer
getClientContainerForDC(const DeclContext * DC) const852 IndexingContext::getClientContainerForDC(const DeclContext *DC) const {
853 if (!DC)
854 return nullptr;
855
856 ContainerMapTy::const_iterator I = ContainerMap.find(DC);
857 if (I == ContainerMap.end())
858 return nullptr;
859
860 return I->second;
861 }
862
getIndexFile(const FileEntry * File)863 CXIdxClientFile IndexingContext::getIndexFile(const FileEntry *File) {
864 if (!File)
865 return nullptr;
866
867 FileMapTy::iterator FI = FileMap.find(File);
868 if (FI != FileMap.end())
869 return FI->second;
870
871 return nullptr;
872 }
873
getIndexLoc(SourceLocation Loc) const874 CXIdxLoc IndexingContext::getIndexLoc(SourceLocation Loc) const {
875 CXIdxLoc idxLoc = { {nullptr, nullptr}, 0 };
876 if (Loc.isInvalid())
877 return idxLoc;
878
879 idxLoc.ptr_data[0] = const_cast<IndexingContext *>(this);
880 idxLoc.int_data = Loc.getRawEncoding();
881 return idxLoc;
882 }
883
translateLoc(SourceLocation Loc,CXIdxClientFile * indexFile,CXFile * file,unsigned * line,unsigned * column,unsigned * offset)884 void IndexingContext::translateLoc(SourceLocation Loc,
885 CXIdxClientFile *indexFile, CXFile *file,
886 unsigned *line, unsigned *column,
887 unsigned *offset) {
888 if (Loc.isInvalid())
889 return;
890
891 SourceManager &SM = Ctx->getSourceManager();
892 Loc = SM.getFileLoc(Loc);
893
894 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
895 FileID FID = LocInfo.first;
896 unsigned FileOffset = LocInfo.second;
897
898 if (FID.isInvalid())
899 return;
900
901 const FileEntry *FE = SM.getFileEntryForID(FID);
902 if (indexFile)
903 *indexFile = getIndexFile(FE);
904 if (file)
905 *file = const_cast<FileEntry *>(FE);
906 if (line)
907 *line = SM.getLineNumber(FID, FileOffset);
908 if (column)
909 *column = SM.getColumnNumber(FID, FileOffset);
910 if (offset)
911 *offset = FileOffset;
912 }
913
getEntityInfo(const NamedDecl * D,EntityInfo & EntityInfo,ScratchAlloc & SA)914 void IndexingContext::getEntityInfo(const NamedDecl *D,
915 EntityInfo &EntityInfo,
916 ScratchAlloc &SA) {
917 if (!D)
918 return;
919
920 D = getEntityDecl(D);
921 EntityInfo.cursor = getCursor(D);
922 EntityInfo.Dcl = D;
923 EntityInfo.IndexCtx = this;
924 EntityInfo.kind = CXIdxEntity_Unexposed;
925 EntityInfo.templateKind = CXIdxEntity_NonTemplate;
926 EntityInfo.lang = CXIdxEntityLang_C;
927
928 if (D->hasAttrs()) {
929 EntityInfo.AttrList = AttrListInfo::create(D, *this);
930 EntityInfo.attributes = EntityInfo.AttrList->getAttrs();
931 EntityInfo.numAttributes = EntityInfo.AttrList->getNumAttrs();
932 }
933
934 if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
935 switch (TD->getTagKind()) {
936 case TTK_Struct:
937 EntityInfo.kind = CXIdxEntity_Struct; break;
938 case TTK_Union:
939 EntityInfo.kind = CXIdxEntity_Union; break;
940 case TTK_Class:
941 EntityInfo.kind = CXIdxEntity_CXXClass;
942 EntityInfo.lang = CXIdxEntityLang_CXX;
943 break;
944 case TTK_Interface:
945 EntityInfo.kind = CXIdxEntity_CXXInterface;
946 EntityInfo.lang = CXIdxEntityLang_CXX;
947 break;
948 case TTK_Enum:
949 EntityInfo.kind = CXIdxEntity_Enum; break;
950 }
951
952 if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D))
953 if (!CXXRec->isCLike())
954 EntityInfo.lang = CXIdxEntityLang_CXX;
955
956 if (isa<ClassTemplatePartialSpecializationDecl>(D)) {
957 EntityInfo.templateKind = CXIdxEntity_TemplatePartialSpecialization;
958 } else if (isa<ClassTemplateSpecializationDecl>(D)) {
959 EntityInfo.templateKind = CXIdxEntity_TemplateSpecialization;
960 }
961
962 } else {
963 switch (D->getKind()) {
964 case Decl::Typedef:
965 EntityInfo.kind = CXIdxEntity_Typedef; break;
966 case Decl::Function:
967 EntityInfo.kind = CXIdxEntity_Function;
968 break;
969 case Decl::ParmVar:
970 EntityInfo.kind = CXIdxEntity_Variable;
971 break;
972 case Decl::Var:
973 EntityInfo.kind = CXIdxEntity_Variable;
974 if (isa<CXXRecordDecl>(D->getDeclContext())) {
975 EntityInfo.kind = CXIdxEntity_CXXStaticVariable;
976 EntityInfo.lang = CXIdxEntityLang_CXX;
977 }
978 break;
979 case Decl::Field:
980 EntityInfo.kind = CXIdxEntity_Field;
981 if (const CXXRecordDecl *
982 CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
983 // FIXME: isPOD check is not sufficient, a POD can contain methods,
984 // we want a isCStructLike check.
985 if (!CXXRec->isPOD())
986 EntityInfo.lang = CXIdxEntityLang_CXX;
987 }
988 break;
989 case Decl::EnumConstant:
990 EntityInfo.kind = CXIdxEntity_EnumConstant; break;
991 case Decl::ObjCInterface:
992 EntityInfo.kind = CXIdxEntity_ObjCClass;
993 EntityInfo.lang = CXIdxEntityLang_ObjC;
994 break;
995 case Decl::ObjCProtocol:
996 EntityInfo.kind = CXIdxEntity_ObjCProtocol;
997 EntityInfo.lang = CXIdxEntityLang_ObjC;
998 break;
999 case Decl::ObjCCategory:
1000 EntityInfo.kind = CXIdxEntity_ObjCCategory;
1001 EntityInfo.lang = CXIdxEntityLang_ObjC;
1002 break;
1003 case Decl::ObjCMethod:
1004 if (cast<ObjCMethodDecl>(D)->isInstanceMethod())
1005 EntityInfo.kind = CXIdxEntity_ObjCInstanceMethod;
1006 else
1007 EntityInfo.kind = CXIdxEntity_ObjCClassMethod;
1008 EntityInfo.lang = CXIdxEntityLang_ObjC;
1009 break;
1010 case Decl::ObjCProperty:
1011 EntityInfo.kind = CXIdxEntity_ObjCProperty;
1012 EntityInfo.lang = CXIdxEntityLang_ObjC;
1013 break;
1014 case Decl::ObjCIvar:
1015 EntityInfo.kind = CXIdxEntity_ObjCIvar;
1016 EntityInfo.lang = CXIdxEntityLang_ObjC;
1017 break;
1018 case Decl::Namespace:
1019 EntityInfo.kind = CXIdxEntity_CXXNamespace;
1020 EntityInfo.lang = CXIdxEntityLang_CXX;
1021 break;
1022 case Decl::NamespaceAlias:
1023 EntityInfo.kind = CXIdxEntity_CXXNamespaceAlias;
1024 EntityInfo.lang = CXIdxEntityLang_CXX;
1025 break;
1026 case Decl::CXXConstructor:
1027 EntityInfo.kind = CXIdxEntity_CXXConstructor;
1028 EntityInfo.lang = CXIdxEntityLang_CXX;
1029 break;
1030 case Decl::CXXDestructor:
1031 EntityInfo.kind = CXIdxEntity_CXXDestructor;
1032 EntityInfo.lang = CXIdxEntityLang_CXX;
1033 break;
1034 case Decl::CXXConversion:
1035 EntityInfo.kind = CXIdxEntity_CXXConversionFunction;
1036 EntityInfo.lang = CXIdxEntityLang_CXX;
1037 break;
1038 case Decl::CXXMethod: {
1039 const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
1040 if (MD->isStatic())
1041 EntityInfo.kind = CXIdxEntity_CXXStaticMethod;
1042 else
1043 EntityInfo.kind = CXIdxEntity_CXXInstanceMethod;
1044 EntityInfo.lang = CXIdxEntityLang_CXX;
1045 break;
1046 }
1047 case Decl::ClassTemplate:
1048 EntityInfo.kind = CXIdxEntity_CXXClass;
1049 EntityInfo.templateKind = CXIdxEntity_Template;
1050 break;
1051 case Decl::FunctionTemplate:
1052 EntityInfo.kind = CXIdxEntity_Function;
1053 EntityInfo.templateKind = CXIdxEntity_Template;
1054 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
1055 cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
1056 if (isa<CXXConstructorDecl>(MD))
1057 EntityInfo.kind = CXIdxEntity_CXXConstructor;
1058 else if (isa<CXXDestructorDecl>(MD))
1059 EntityInfo.kind = CXIdxEntity_CXXDestructor;
1060 else if (isa<CXXConversionDecl>(MD))
1061 EntityInfo.kind = CXIdxEntity_CXXConversionFunction;
1062 else {
1063 if (MD->isStatic())
1064 EntityInfo.kind = CXIdxEntity_CXXStaticMethod;
1065 else
1066 EntityInfo.kind = CXIdxEntity_CXXInstanceMethod;
1067 }
1068 }
1069 break;
1070 case Decl::TypeAliasTemplate:
1071 EntityInfo.kind = CXIdxEntity_CXXTypeAlias;
1072 EntityInfo.templateKind = CXIdxEntity_Template;
1073 break;
1074 case Decl::TypeAlias:
1075 EntityInfo.kind = CXIdxEntity_CXXTypeAlias;
1076 EntityInfo.lang = CXIdxEntityLang_CXX;
1077 break;
1078 default:
1079 break;
1080 }
1081 }
1082
1083 if (EntityInfo.kind == CXIdxEntity_Unexposed)
1084 return;
1085
1086 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1087 if (FD->getTemplatedKind() ==
1088 FunctionDecl::TK_FunctionTemplateSpecialization)
1089 EntityInfo.templateKind = CXIdxEntity_TemplateSpecialization;
1090 }
1091
1092 if (EntityInfo.templateKind != CXIdxEntity_NonTemplate)
1093 EntityInfo.lang = CXIdxEntityLang_CXX;
1094
1095 if (IdentifierInfo *II = D->getIdentifier()) {
1096 EntityInfo.name = SA.toCStr(II->getName());
1097
1098 } else if (isa<TagDecl>(D) || isa<FieldDecl>(D) || isa<NamespaceDecl>(D)) {
1099 EntityInfo.name = nullptr; // anonymous tag/field/namespace.
1100
1101 } else {
1102 SmallString<256> StrBuf;
1103 {
1104 llvm::raw_svector_ostream OS(StrBuf);
1105 D->printName(OS);
1106 }
1107 EntityInfo.name = SA.copyCStr(StrBuf.str());
1108 }
1109
1110 {
1111 SmallString<512> StrBuf;
1112 bool Ignore = getDeclCursorUSR(D, StrBuf);
1113 if (Ignore) {
1114 EntityInfo.USR = nullptr;
1115 } else {
1116 EntityInfo.USR = SA.copyCStr(StrBuf.str());
1117 }
1118 }
1119 }
1120
getContainerInfo(const DeclContext * DC,ContainerInfo & ContInfo)1121 void IndexingContext::getContainerInfo(const DeclContext *DC,
1122 ContainerInfo &ContInfo) {
1123 ContInfo.cursor = getCursor(cast<Decl>(DC));
1124 ContInfo.DC = DC;
1125 ContInfo.IndexCtx = this;
1126 }
1127
getRefCursor(const NamedDecl * D,SourceLocation Loc)1128 CXCursor IndexingContext::getRefCursor(const NamedDecl *D, SourceLocation Loc) {
1129 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
1130 return MakeCursorTypeRef(TD, Loc, CXTU);
1131 if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D))
1132 return MakeCursorObjCClassRef(ID, Loc, CXTU);
1133 if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D))
1134 return MakeCursorObjCProtocolRef(PD, Loc, CXTU);
1135 if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D))
1136 return MakeCursorTemplateRef(Template, Loc, CXTU);
1137 if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(D))
1138 return MakeCursorNamespaceRef(Namespace, Loc, CXTU);
1139 if (const NamespaceAliasDecl *Namespace = dyn_cast<NamespaceAliasDecl>(D))
1140 return MakeCursorNamespaceRef(Namespace, Loc, CXTU);
1141 if (const FieldDecl *Field = dyn_cast<FieldDecl>(D))
1142 return MakeCursorMemberRef(Field, Loc, CXTU);
1143 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
1144 return MakeCursorVariableRef(Var, Loc, CXTU);
1145
1146 return clang_getNullCursor();
1147 }
1148
shouldIgnoreIfImplicit(const Decl * D)1149 bool IndexingContext::shouldIgnoreIfImplicit(const Decl *D) {
1150 if (isa<ObjCInterfaceDecl>(D))
1151 return false;
1152 if (isa<ObjCCategoryDecl>(D))
1153 return false;
1154 if (isa<ObjCIvarDecl>(D))
1155 return false;
1156 if (isa<ObjCMethodDecl>(D))
1157 return false;
1158 if (isa<ImportDecl>(D))
1159 return false;
1160 return true;
1161 }
1162
isTemplateImplicitInstantiation(const Decl * D)1163 bool IndexingContext::isTemplateImplicitInstantiation(const Decl *D) {
1164 if (const ClassTemplateSpecializationDecl *
1165 SD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
1166 return SD->getSpecializationKind() == TSK_ImplicitInstantiation;
1167 }
1168 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1169 return FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation;
1170 }
1171 return false;
1172 }
1173