1 //===- IndexDecl.cpp - Indexing declarations ------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "IndexingContext.h"
11 #include "clang/Index/IndexDataConsumer.h"
12 #include "clang/AST/DeclVisitor.h"
14 using namespace clang;
15 using namespace index;
17 #define TRY_TO(CALL_EXPR) \
25 class IndexingDeclVisitor : public ConstDeclVisitor<IndexingDeclVisitor, bool> {
26 IndexingContext &IndexCtx;
29 explicit IndexingDeclVisitor(IndexingContext &indexCtx)
30 : IndexCtx(indexCtx) { }
34 bool VisitDecl(const Decl *D) {
39 /// \brief Returns true if the given method has been defined explicitly by the
41 static bool hasUserDefined(const ObjCMethodDecl *D,
42 const ObjCImplDecl *Container) {
43 const ObjCMethodDecl *MD = Container->getMethod(D->getSelector(),
44 D->isInstanceMethod());
45 return MD && !MD->isImplicit() && MD->isThisDeclarationADefinition();
48 void handleDeclarator(const DeclaratorDecl *D,
49 const NamedDecl *Parent = nullptr) {
50 if (!Parent) Parent = D;
52 IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), Parent);
53 IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), Parent);
54 if (IndexCtx.shouldIndexFunctionLocalSymbols()) {
55 // Only index parameters in definitions, parameters in declarations are
57 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) {
58 auto *DC = Parm->getDeclContext();
59 if (auto *FD = dyn_cast<FunctionDecl>(DC)) {
60 if (FD->isThisDeclarationADefinition())
61 IndexCtx.handleDecl(Parm);
62 } else if (auto *MD = dyn_cast<ObjCMethodDecl>(DC)) {
63 if (MD->isThisDeclarationADefinition())
64 IndexCtx.handleDecl(Parm);
66 IndexCtx.handleDecl(Parm);
68 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
69 if (FD->isThisDeclarationADefinition()) {
70 for (auto PI : FD->parameters()) {
71 IndexCtx.handleDecl(PI);
78 bool handleObjCMethod(const ObjCMethodDecl *D,
79 const ObjCPropertyDecl *AssociatedProp = nullptr) {
80 SmallVector<SymbolRelation, 4> Relations;
81 SmallVector<const ObjCMethodDecl*, 4> Overriden;
83 D->getOverriddenMethods(Overriden);
84 for(auto overridden: Overriden) {
85 Relations.emplace_back((unsigned) SymbolRole::RelationOverrideOf,
89 Relations.emplace_back((unsigned)SymbolRole::RelationAccessorOf,
92 if (!IndexCtx.handleDecl(D, (unsigned)SymbolRole::Dynamic, Relations))
94 IndexCtx.indexTypeSourceInfo(D->getReturnTypeSourceInfo(), D);
95 for (const auto *I : D->parameters())
96 handleDeclarator(I, D);
98 if (D->isThisDeclarationADefinition()) {
99 const Stmt *Body = D->getBody();
101 IndexCtx.indexBody(Body, D, D);
107 bool VisitFunctionDecl(const FunctionDecl *D) {
111 SymbolRoleSet Roles{};
112 SmallVector<SymbolRelation, 4> Relations;
113 if (auto *CXXMD = dyn_cast<CXXMethodDecl>(D)) {
114 if (CXXMD->isVirtual())
115 Roles |= (unsigned)SymbolRole::Dynamic;
116 for (auto I = CXXMD->begin_overridden_methods(),
117 E = CXXMD->end_overridden_methods(); I != E; ++I) {
118 Relations.emplace_back((unsigned)SymbolRole::RelationOverrideOf, *I);
122 if (!IndexCtx.handleDecl(D, Roles, Relations))
126 if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
127 // Constructor initializers.
128 for (const auto *Init : Ctor->inits()) {
129 if (Init->isWritten()) {
130 IndexCtx.indexTypeSourceInfo(Init->getTypeSourceInfo(), D);
131 if (const FieldDecl *Member = Init->getAnyMember())
132 IndexCtx.handleReference(Member, Init->getMemberLocation(), D, D,
133 (unsigned)SymbolRole::Write);
134 IndexCtx.indexBody(Init->getInit(), D, D);
139 if (D->isThisDeclarationADefinition()) {
140 const Stmt *Body = D->getBody();
142 IndexCtx.indexBody(Body, D, D);
148 bool VisitVarDecl(const VarDecl *D) {
149 if (!IndexCtx.handleDecl(D))
152 IndexCtx.indexBody(D->getInit(), D);
156 bool VisitFieldDecl(const FieldDecl *D) {
157 if (!IndexCtx.handleDecl(D))
161 IndexCtx.indexBody(D->getBitWidth(), D);
162 else if (D->hasInClassInitializer())
163 IndexCtx.indexBody(D->getInClassInitializer(), D);
167 bool VisitObjCIvarDecl(const ObjCIvarDecl *D) {
168 if (D->getSynthesize()) {
169 // For synthesized ivars, use the location of the ObjC implementation,
170 // not the location of the property.
171 // Otherwise the header file containing the @interface will have different
172 // indexing contents based on whether the @implementation was present or
173 // not in the translation unit.
174 return IndexCtx.handleDecl(D,
175 cast<Decl>(D->getDeclContext())->getLocation(),
176 (unsigned)SymbolRole::Implicit);
178 if (!IndexCtx.handleDecl(D))
184 bool VisitMSPropertyDecl(const MSPropertyDecl *D) {
189 bool VisitEnumConstantDecl(const EnumConstantDecl *D) {
190 if (!IndexCtx.handleDecl(D))
192 IndexCtx.indexBody(D->getInitExpr(), D);
196 bool VisitTypedefNameDecl(const TypedefNameDecl *D) {
197 if (!IndexCtx.handleDecl(D))
199 IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D);
203 bool VisitTagDecl(const TagDecl *D) {
204 // Non-free standing tags are handled in indexTypeSourceInfo.
205 if (D->isFreeStanding()) {
206 if (D->isThisDeclarationADefinition()) {
207 IndexCtx.indexTagDecl(D);
209 auto *Parent = dyn_cast<NamedDecl>(D->getDeclContext());
210 return IndexCtx.handleReference(D, D->getLocation(), Parent,
211 D->getLexicalDeclContext(),
218 bool handleReferencedProtocols(const ObjCProtocolList &ProtList,
219 const ObjCContainerDecl *ContD) {
220 ObjCInterfaceDecl::protocol_loc_iterator LI = ProtList.loc_begin();
221 for (ObjCInterfaceDecl::protocol_iterator
222 I = ProtList.begin(), E = ProtList.end(); I != E; ++I, ++LI) {
223 SourceLocation Loc = *LI;
224 ObjCProtocolDecl *PD = *I;
225 TRY_TO(IndexCtx.handleReference(PD, Loc, ContD, ContD,
227 SymbolRelation{(unsigned)SymbolRole::RelationBaseOf, ContD}));
232 bool VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
233 if (D->isThisDeclarationADefinition()) {
234 TRY_TO(IndexCtx.handleDecl(D));
235 if (auto *SuperD = D->getSuperClass()) {
236 TRY_TO(IndexCtx.handleReference(SuperD, D->getSuperClassLoc(), D, D,
238 SymbolRelation{(unsigned)SymbolRole::RelationBaseOf, D}));
240 TRY_TO(handleReferencedProtocols(D->getReferencedProtocols(), D));
241 TRY_TO(IndexCtx.indexDeclContext(D));
243 return IndexCtx.handleReference(D, D->getLocation(), nullptr,
244 D->getDeclContext(), SymbolRoleSet());
249 bool VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
250 if (D->isThisDeclarationADefinition()) {
251 TRY_TO(IndexCtx.handleDecl(D));
252 TRY_TO(handleReferencedProtocols(D->getReferencedProtocols(), D));
253 TRY_TO(IndexCtx.indexDeclContext(D));
255 return IndexCtx.handleReference(D, D->getLocation(), nullptr,
256 D->getDeclContext(), SymbolRoleSet());
261 bool VisitObjCImplementationDecl(const ObjCImplementationDecl *D) {
262 const ObjCInterfaceDecl *Class = D->getClassInterface();
266 if (Class->isImplicitInterfaceDecl())
267 IndexCtx.handleDecl(Class);
269 if (!IndexCtx.handleDecl(D))
272 // Index the ivars first to make sure the synthesized ivars are indexed
273 // before indexing the methods that can reference them.
274 for (const auto *IvarI : D->ivars())
275 IndexCtx.indexDecl(IvarI);
276 for (const auto *I : D->decls()) {
277 if (!isa<ObjCIvarDecl>(I))
278 IndexCtx.indexDecl(I);
284 bool VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
285 const ObjCInterfaceDecl *C = D->getClassInterface();
287 TRY_TO(IndexCtx.handleReference(C, D->getLocation(), D, D,
288 SymbolRoleSet(), SymbolRelation{
289 (unsigned)SymbolRole::RelationExtendedBy, D
291 SourceLocation CategoryLoc = D->getCategoryNameLoc();
292 if (!CategoryLoc.isValid())
293 CategoryLoc = D->getLocation();
294 TRY_TO(IndexCtx.handleDecl(D, CategoryLoc));
295 TRY_TO(handleReferencedProtocols(D->getReferencedProtocols(), D));
296 TRY_TO(IndexCtx.indexDeclContext(D));
300 bool VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) {
301 const ObjCCategoryDecl *Cat = D->getCategoryDecl();
304 const ObjCInterfaceDecl *C = D->getClassInterface();
306 TRY_TO(IndexCtx.handleReference(C, D->getLocation(), D, D,
308 SourceLocation CategoryLoc = D->getCategoryNameLoc();
309 if (!CategoryLoc.isValid())
310 CategoryLoc = D->getLocation();
311 if (!IndexCtx.handleDecl(D, CategoryLoc))
313 IndexCtx.indexDeclContext(D);
317 bool VisitObjCMethodDecl(const ObjCMethodDecl *D) {
318 // Methods associated with a property, even user-declared ones, are
319 // handled when we handle the property.
320 if (D->isPropertyAccessor())
327 bool VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
328 if (ObjCMethodDecl *MD = D->getGetterMethodDecl())
329 if (MD->getLexicalDeclContext() == D->getLexicalDeclContext())
330 handleObjCMethod(MD, D);
331 if (ObjCMethodDecl *MD = D->getSetterMethodDecl())
332 if (MD->getLexicalDeclContext() == D->getLexicalDeclContext())
333 handleObjCMethod(MD, D);
334 if (!IndexCtx.handleDecl(D))
336 IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D);
340 bool VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
341 ObjCPropertyDecl *PD = D->getPropertyDecl();
342 if (!IndexCtx.handleReference(PD, D->getLocation(),
343 /*Parent=*/cast<NamedDecl>(D->getDeclContext()),
344 D->getDeclContext(), SymbolRoleSet(), {},
345 /*RefE=*/nullptr, D))
348 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
350 assert(D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize);
352 if (ObjCIvarDecl *IvarD = D->getPropertyIvarDecl()) {
353 if (!IvarD->getSynthesize())
354 IndexCtx.handleReference(IvarD, D->getPropertyIvarDeclLoc(), nullptr,
355 D->getDeclContext(), SymbolRoleSet());
358 auto *ImplD = cast<ObjCImplDecl>(D->getDeclContext());
359 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) {
360 if (MD->isPropertyAccessor() &&
361 !hasUserDefined(MD, ImplD))
362 IndexCtx.handleDecl(MD, D->getLocation(), SymbolRoleSet(), {}, ImplD);
364 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) {
365 if (MD->isPropertyAccessor() &&
366 !hasUserDefined(MD, ImplD))
367 IndexCtx.handleDecl(MD, D->getLocation(), SymbolRoleSet(), {}, ImplD);
372 bool VisitNamespaceDecl(const NamespaceDecl *D) {
373 if (!IndexCtx.handleDecl(D))
375 IndexCtx.indexDeclContext(D);
379 bool VisitUsingDecl(const UsingDecl *D) {
380 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
381 const NamedDecl *Parent = dyn_cast<NamedDecl>(DC);
383 IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), Parent,
384 D->getLexicalDeclContext());
385 for (const auto *I : D->shadows())
386 IndexCtx.handleReference(I->getUnderlyingDecl(), D->getLocation(), Parent,
387 D->getLexicalDeclContext(), SymbolRoleSet());
391 bool VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
392 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
393 const NamedDecl *Parent = dyn_cast<NamedDecl>(DC);
395 IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), Parent,
396 D->getLexicalDeclContext());
397 return IndexCtx.handleReference(D->getNominatedNamespaceAsWritten(),
398 D->getLocation(), Parent,
399 D->getLexicalDeclContext(),
403 bool VisitClassTemplateSpecializationDecl(const
404 ClassTemplateSpecializationDecl *D) {
405 // FIXME: Notify subsequent callbacks if info comes from implicit
407 if (D->isThisDeclarationADefinition())
408 IndexCtx.indexTagDecl(D);
412 bool VisitTemplateDecl(const TemplateDecl *D) {
413 // FIXME: Template parameters.
414 return Visit(D->getTemplatedDecl());
417 bool VisitFriendDecl(const FriendDecl *D) {
418 if (auto ND = D->getFriendDecl()) {
419 // FIXME: Ignore a class template in a dependent context, these are not
420 // linked properly with their redeclarations, ending up with duplicate
422 // See comment "Friend templates are visible in fairly strange ways." in
423 // SemaTemplate.cpp which precedes code that prevents the friend template
424 // from becoming visible from the enclosing context.
425 if (isa<ClassTemplateDecl>(ND) && D->getDeclContext()->isDependentContext())
429 if (auto Ty = D->getFriendType()) {
430 IndexCtx.indexTypeSourceInfo(Ty, cast<NamedDecl>(D->getDeclContext()));
435 bool VisitImportDecl(const ImportDecl *D) {
436 return IndexCtx.importedModule(D);
440 } // anonymous namespace
442 bool IndexingContext::indexDecl(const Decl *D) {
443 if (D->isImplicit() && shouldIgnoreIfImplicit(D))
446 if (isTemplateImplicitInstantiation(D))
449 IndexingDeclVisitor Visitor(*this);
450 bool ShouldContinue = Visitor.Visit(D);
454 if (!Visitor.Handled && isa<DeclContext>(D))
455 return indexDeclContext(cast<DeclContext>(D));
460 bool IndexingContext::indexDeclContext(const DeclContext *DC) {
461 for (const auto *I : DC->decls())
467 bool IndexingContext::indexTopLevelDecl(const Decl *D) {
468 if (D->getLocation().isInvalid())
471 if (isa<ObjCMethodDecl>(D))
472 return true; // Wait for the objc container.
477 bool IndexingContext::indexDeclGroupRef(DeclGroupRef DG) {
478 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
479 if (!indexTopLevelDecl(*I))