blob: 6c5b2e520ef4731dfacf028965c23cf916fb25a7 [file] [log] [blame]
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +00001//===- IndexDecl.cpp - Indexing declarations ------------------------------===//
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 "clang/Index/IndexDataConsumer.h"
12#include "clang/AST/DeclVisitor.h"
13
14using namespace clang;
15using namespace index;
16
Argyrios Kyrtzidis34a0c1a2016-03-03 05:33:54 +000017#define TRY_TO(CALL_EXPR) \
18 do { \
19 if (!CALL_EXPR) \
20 return false; \
21 } while (0)
22
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +000023namespace {
24
25class IndexingDeclVisitor : public ConstDeclVisitor<IndexingDeclVisitor, bool> {
26 IndexingContext &IndexCtx;
27
28public:
29 explicit IndexingDeclVisitor(IndexingContext &indexCtx)
30 : IndexCtx(indexCtx) { }
31
32 bool Handled = true;
33
34 bool VisitDecl(const Decl *D) {
35 Handled = false;
36 return true;
37 }
38
39 /// \brief Returns true if the given method has been defined explicitly by the
40 /// user.
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();
46 }
47
48 void handleDeclarator(const DeclaratorDecl *D,
Argyrios Kyrtzidisde0f5082017-01-11 21:01:07 +000049 const NamedDecl *Parent = nullptr,
50 bool isIBType = false) {
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +000051 if (!Parent) Parent = D;
52
Argyrios Kyrtzidisde0f5082017-01-11 21:01:07 +000053 IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), Parent,
54 Parent->getLexicalDeclContext(),
55 /*isBase=*/false, isIBType);
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +000056 IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), Parent);
57 if (IndexCtx.shouldIndexFunctionLocalSymbols()) {
58 // Only index parameters in definitions, parameters in declarations are
59 // not useful.
60 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) {
61 auto *DC = Parm->getDeclContext();
62 if (auto *FD = dyn_cast<FunctionDecl>(DC)) {
63 if (FD->isThisDeclarationADefinition())
64 IndexCtx.handleDecl(Parm);
65 } else if (auto *MD = dyn_cast<ObjCMethodDecl>(DC)) {
66 if (MD->isThisDeclarationADefinition())
67 IndexCtx.handleDecl(Parm);
68 } else {
69 IndexCtx.handleDecl(Parm);
70 }
71 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
72 if (FD->isThisDeclarationADefinition()) {
David Majnemer59f77922016-06-24 04:05:48 +000073 for (auto PI : FD->parameters()) {
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +000074 IndexCtx.handleDecl(PI);
75 }
76 }
77 }
78 }
79 }
80
Argyrios Kyrtzidis806faaf2016-10-25 21:11:22 +000081 bool handleObjCMethod(const ObjCMethodDecl *D,
82 const ObjCPropertyDecl *AssociatedProp = nullptr) {
83 SmallVector<SymbolRelation, 4> Relations;
84 SmallVector<const ObjCMethodDecl*, 4> Overriden;
85
86 D->getOverriddenMethods(Overriden);
87 for(auto overridden: Overriden) {
88 Relations.emplace_back((unsigned) SymbolRole::RelationOverrideOf,
89 overridden);
90 }
91 if (AssociatedProp)
92 Relations.emplace_back((unsigned)SymbolRole::RelationAccessorOf,
93 AssociatedProp);
94
95 if (!IndexCtx.handleDecl(D, (unsigned)SymbolRole::Dynamic, Relations))
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +000096 return false;
97 IndexCtx.indexTypeSourceInfo(D->getReturnTypeSourceInfo(), D);
Argyrios Kyrtzidisde0f5082017-01-11 21:01:07 +000098 bool hasIBActionAndFirst = D->hasAttr<IBActionAttr>();
99 for (const auto *I : D->parameters()) {
100 handleDeclarator(I, D, /*isIBType=*/hasIBActionAndFirst);
101 hasIBActionAndFirst = false;
102 }
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000103
104 if (D->isThisDeclarationADefinition()) {
105 const Stmt *Body = D->getBody();
106 if (Body) {
107 IndexCtx.indexBody(Body, D, D);
108 }
109 }
110 return true;
111 }
112
113 bool VisitFunctionDecl(const FunctionDecl *D) {
114 if (D->isDeleted())
115 return true;
116
117 SymbolRoleSet Roles{};
118 SmallVector<SymbolRelation, 4> Relations;
119 if (auto *CXXMD = dyn_cast<CXXMethodDecl>(D)) {
120 if (CXXMD->isVirtual())
121 Roles |= (unsigned)SymbolRole::Dynamic;
122 for (auto I = CXXMD->begin_overridden_methods(),
123 E = CXXMD->end_overridden_methods(); I != E; ++I) {
124 Relations.emplace_back((unsigned)SymbolRole::RelationOverrideOf, *I);
125 }
126 }
127
128 if (!IndexCtx.handleDecl(D, Roles, Relations))
129 return false;
130 handleDeclarator(D);
131
132 if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
133 // Constructor initializers.
134 for (const auto *Init : Ctor->inits()) {
135 if (Init->isWritten()) {
136 IndexCtx.indexTypeSourceInfo(Init->getTypeSourceInfo(), D);
137 if (const FieldDecl *Member = Init->getAnyMember())
138 IndexCtx.handleReference(Member, Init->getMemberLocation(), D, D,
139 (unsigned)SymbolRole::Write);
140 IndexCtx.indexBody(Init->getInit(), D, D);
141 }
142 }
143 }
144
145 if (D->isThisDeclarationADefinition()) {
146 const Stmt *Body = D->getBody();
147 if (Body) {
148 IndexCtx.indexBody(Body, D, D);
149 }
150 }
151 return true;
152 }
153
154 bool VisitVarDecl(const VarDecl *D) {
155 if (!IndexCtx.handleDecl(D))
156 return false;
157 handleDeclarator(D);
158 IndexCtx.indexBody(D->getInit(), D);
159 return true;
160 }
161
162 bool VisitFieldDecl(const FieldDecl *D) {
163 if (!IndexCtx.handleDecl(D))
164 return false;
165 handleDeclarator(D);
166 if (D->isBitField())
167 IndexCtx.indexBody(D->getBitWidth(), D);
168 else if (D->hasInClassInitializer())
169 IndexCtx.indexBody(D->getInClassInitializer(), D);
170 return true;
171 }
172
173 bool VisitObjCIvarDecl(const ObjCIvarDecl *D) {
174 if (D->getSynthesize()) {
175 // For synthesized ivars, use the location of the ObjC implementation,
176 // not the location of the property.
177 // Otherwise the header file containing the @interface will have different
178 // indexing contents based on whether the @implementation was present or
179 // not in the translation unit.
180 return IndexCtx.handleDecl(D,
181 cast<Decl>(D->getDeclContext())->getLocation(),
182 (unsigned)SymbolRole::Implicit);
183 }
184 if (!IndexCtx.handleDecl(D))
185 return false;
186 handleDeclarator(D);
187 return true;
188 }
189
190 bool VisitMSPropertyDecl(const MSPropertyDecl *D) {
191 handleDeclarator(D);
192 return true;
193 }
194
195 bool VisitEnumConstantDecl(const EnumConstantDecl *D) {
196 if (!IndexCtx.handleDecl(D))
197 return false;
198 IndexCtx.indexBody(D->getInitExpr(), D);
199 return true;
200 }
201
202 bool VisitTypedefNameDecl(const TypedefNameDecl *D) {
203 if (!IndexCtx.handleDecl(D))
204 return false;
205 IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D);
206 return true;
207 }
208
209 bool VisitTagDecl(const TagDecl *D) {
210 // Non-free standing tags are handled in indexTypeSourceInfo.
211 if (D->isFreeStanding()) {
212 if (D->isThisDeclarationADefinition()) {
213 IndexCtx.indexTagDecl(D);
214 } else {
215 auto *Parent = dyn_cast<NamedDecl>(D->getDeclContext());
216 return IndexCtx.handleReference(D, D->getLocation(), Parent,
217 D->getLexicalDeclContext(),
218 SymbolRoleSet());
219 }
220 }
221 return true;
222 }
223
Argyrios Kyrtzidis34a0c1a2016-03-03 05:33:54 +0000224 bool handleReferencedProtocols(const ObjCProtocolList &ProtList,
225 const ObjCContainerDecl *ContD) {
226 ObjCInterfaceDecl::protocol_loc_iterator LI = ProtList.loc_begin();
227 for (ObjCInterfaceDecl::protocol_iterator
228 I = ProtList.begin(), E = ProtList.end(); I != E; ++I, ++LI) {
229 SourceLocation Loc = *LI;
230 ObjCProtocolDecl *PD = *I;
231 TRY_TO(IndexCtx.handleReference(PD, Loc, ContD, ContD,
232 SymbolRoleSet(),
233 SymbolRelation{(unsigned)SymbolRole::RelationBaseOf, ContD}));
234 }
235 return true;
236 }
237
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000238 bool VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
239 if (D->isThisDeclarationADefinition()) {
Argyrios Kyrtzidis34a0c1a2016-03-03 05:33:54 +0000240 TRY_TO(IndexCtx.handleDecl(D));
241 if (auto *SuperD = D->getSuperClass()) {
242 TRY_TO(IndexCtx.handleReference(SuperD, D->getSuperClassLoc(), D, D,
243 SymbolRoleSet(),
244 SymbolRelation{(unsigned)SymbolRole::RelationBaseOf, D}));
245 }
246 TRY_TO(handleReferencedProtocols(D->getReferencedProtocols(), D));
247 TRY_TO(IndexCtx.indexDeclContext(D));
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000248 } else {
Argyrios Kyrtzidis542f38f2016-03-09 02:12:46 +0000249 return IndexCtx.handleReference(D, D->getLocation(), nullptr,
250 D->getDeclContext(), SymbolRoleSet());
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000251 }
252 return true;
253 }
254
255 bool VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
256 if (D->isThisDeclarationADefinition()) {
Argyrios Kyrtzidis34a0c1a2016-03-03 05:33:54 +0000257 TRY_TO(IndexCtx.handleDecl(D));
258 TRY_TO(handleReferencedProtocols(D->getReferencedProtocols(), D));
259 TRY_TO(IndexCtx.indexDeclContext(D));
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000260 } else {
Argyrios Kyrtzidis542f38f2016-03-09 02:12:46 +0000261 return IndexCtx.handleReference(D, D->getLocation(), nullptr,
262 D->getDeclContext(), SymbolRoleSet());
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000263 }
264 return true;
265 }
266
267 bool VisitObjCImplementationDecl(const ObjCImplementationDecl *D) {
268 const ObjCInterfaceDecl *Class = D->getClassInterface();
269 if (!Class)
270 return true;
271
272 if (Class->isImplicitInterfaceDecl())
273 IndexCtx.handleDecl(Class);
274
275 if (!IndexCtx.handleDecl(D))
276 return false;
277
278 // Index the ivars first to make sure the synthesized ivars are indexed
279 // before indexing the methods that can reference them.
280 for (const auto *IvarI : D->ivars())
281 IndexCtx.indexDecl(IvarI);
282 for (const auto *I : D->decls()) {
283 if (!isa<ObjCIvarDecl>(I))
284 IndexCtx.indexDecl(I);
285 }
286
287 return true;
288 }
289
290 bool VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
Argyrios Kyrtzidis806faaf2016-10-25 21:11:22 +0000291 const ObjCInterfaceDecl *C = D->getClassInterface();
292 if (C)
293 TRY_TO(IndexCtx.handleReference(C, D->getLocation(), D, D,
294 SymbolRoleSet(), SymbolRelation{
295 (unsigned)SymbolRole::RelationExtendedBy, D
296 }));
297 SourceLocation CategoryLoc = D->getCategoryNameLoc();
298 if (!CategoryLoc.isValid())
299 CategoryLoc = D->getLocation();
300 TRY_TO(IndexCtx.handleDecl(D, CategoryLoc));
301 TRY_TO(handleReferencedProtocols(D->getReferencedProtocols(), D));
302 TRY_TO(IndexCtx.indexDeclContext(D));
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000303 return true;
304 }
305
306 bool VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) {
307 const ObjCCategoryDecl *Cat = D->getCategoryDecl();
308 if (!Cat)
309 return true;
Argyrios Kyrtzidis806faaf2016-10-25 21:11:22 +0000310 const ObjCInterfaceDecl *C = D->getClassInterface();
311 if (C)
312 TRY_TO(IndexCtx.handleReference(C, D->getLocation(), D, D,
313 SymbolRoleSet()));
314 SourceLocation CategoryLoc = D->getCategoryNameLoc();
315 if (!CategoryLoc.isValid())
316 CategoryLoc = D->getLocation();
317 if (!IndexCtx.handleDecl(D, CategoryLoc))
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000318 return false;
319 IndexCtx.indexDeclContext(D);
320 return true;
321 }
322
323 bool VisitObjCMethodDecl(const ObjCMethodDecl *D) {
324 // Methods associated with a property, even user-declared ones, are
325 // handled when we handle the property.
326 if (D->isPropertyAccessor())
327 return true;
328
329 handleObjCMethod(D);
330 return true;
331 }
332
333 bool VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
334 if (ObjCMethodDecl *MD = D->getGetterMethodDecl())
335 if (MD->getLexicalDeclContext() == D->getLexicalDeclContext())
Argyrios Kyrtzidis806faaf2016-10-25 21:11:22 +0000336 handleObjCMethod(MD, D);
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000337 if (ObjCMethodDecl *MD = D->getSetterMethodDecl())
338 if (MD->getLexicalDeclContext() == D->getLexicalDeclContext())
Argyrios Kyrtzidis806faaf2016-10-25 21:11:22 +0000339 handleObjCMethod(MD, D);
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000340 if (!IndexCtx.handleDecl(D))
341 return false;
Argyrios Kyrtzidisde0f5082017-01-11 21:01:07 +0000342 if (IBOutletCollectionAttr *attr = D->getAttr<IBOutletCollectionAttr>())
343 IndexCtx.indexTypeSourceInfo(attr->getInterfaceLoc(), D,
344 D->getLexicalDeclContext(), false, true);
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000345 IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D);
346 return true;
347 }
348
349 bool VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
350 ObjCPropertyDecl *PD = D->getPropertyDecl();
351 if (!IndexCtx.handleReference(PD, D->getLocation(),
352 /*Parent=*/cast<NamedDecl>(D->getDeclContext()),
353 D->getDeclContext(), SymbolRoleSet(), {},
354 /*RefE=*/nullptr, D))
355 return false;
356
357 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
358 return true;
359 assert(D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize);
360
361 if (ObjCIvarDecl *IvarD = D->getPropertyIvarDecl()) {
362 if (!IvarD->getSynthesize())
363 IndexCtx.handleReference(IvarD, D->getPropertyIvarDeclLoc(), nullptr,
364 D->getDeclContext(), SymbolRoleSet());
365 }
366
367 auto *ImplD = cast<ObjCImplDecl>(D->getDeclContext());
368 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) {
369 if (MD->isPropertyAccessor() &&
370 !hasUserDefined(MD, ImplD))
371 IndexCtx.handleDecl(MD, D->getLocation(), SymbolRoleSet(), {}, ImplD);
372 }
373 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) {
374 if (MD->isPropertyAccessor() &&
375 !hasUserDefined(MD, ImplD))
376 IndexCtx.handleDecl(MD, D->getLocation(), SymbolRoleSet(), {}, ImplD);
377 }
378 return true;
379 }
380
381 bool VisitNamespaceDecl(const NamespaceDecl *D) {
382 if (!IndexCtx.handleDecl(D))
383 return false;
384 IndexCtx.indexDeclContext(D);
385 return true;
386 }
387
388 bool VisitUsingDecl(const UsingDecl *D) {
389 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
390 const NamedDecl *Parent = dyn_cast<NamedDecl>(DC);
391
392 IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), Parent,
393 D->getLexicalDeclContext());
394 for (const auto *I : D->shadows())
395 IndexCtx.handleReference(I->getUnderlyingDecl(), D->getLocation(), Parent,
396 D->getLexicalDeclContext(), SymbolRoleSet());
397 return true;
398 }
399
400 bool VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
401 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
402 const NamedDecl *Parent = dyn_cast<NamedDecl>(DC);
403
404 IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), Parent,
405 D->getLexicalDeclContext());
406 return IndexCtx.handleReference(D->getNominatedNamespaceAsWritten(),
407 D->getLocation(), Parent,
408 D->getLexicalDeclContext(),
409 SymbolRoleSet());
410 }
411
412 bool VisitClassTemplateSpecializationDecl(const
413 ClassTemplateSpecializationDecl *D) {
414 // FIXME: Notify subsequent callbacks if info comes from implicit
415 // instantiation.
416 if (D->isThisDeclarationADefinition())
417 IndexCtx.indexTagDecl(D);
418 return true;
419 }
420
421 bool VisitTemplateDecl(const TemplateDecl *D) {
422 // FIXME: Template parameters.
423 return Visit(D->getTemplatedDecl());
424 }
425
426 bool VisitFriendDecl(const FriendDecl *D) {
427 if (auto ND = D->getFriendDecl()) {
428 // FIXME: Ignore a class template in a dependent context, these are not
429 // linked properly with their redeclarations, ending up with duplicate
430 // USRs.
431 // See comment "Friend templates are visible in fairly strange ways." in
432 // SemaTemplate.cpp which precedes code that prevents the friend template
433 // from becoming visible from the enclosing context.
434 if (isa<ClassTemplateDecl>(ND) && D->getDeclContext()->isDependentContext())
435 return true;
436 return Visit(ND);
437 }
438 if (auto Ty = D->getFriendType()) {
439 IndexCtx.indexTypeSourceInfo(Ty, cast<NamedDecl>(D->getDeclContext()));
440 }
441 return true;
442 }
443
444 bool VisitImportDecl(const ImportDecl *D) {
445 return IndexCtx.importedModule(D);
446 }
447};
448
449} // anonymous namespace
450
451bool IndexingContext::indexDecl(const Decl *D) {
452 if (D->isImplicit() && shouldIgnoreIfImplicit(D))
453 return true;
454
455 if (isTemplateImplicitInstantiation(D))
456 return true;
457
458 IndexingDeclVisitor Visitor(*this);
459 bool ShouldContinue = Visitor.Visit(D);
460 if (!ShouldContinue)
461 return false;
462
463 if (!Visitor.Handled && isa<DeclContext>(D))
464 return indexDeclContext(cast<DeclContext>(D));
465
466 return true;
467}
468
469bool IndexingContext::indexDeclContext(const DeclContext *DC) {
470 for (const auto *I : DC->decls())
471 if (!indexDecl(I))
472 return false;
473 return true;
474}
475
476bool IndexingContext::indexTopLevelDecl(const Decl *D) {
477 if (D->getLocation().isInvalid())
478 return true;
479
480 if (isa<ObjCMethodDecl>(D))
481 return true; // Wait for the objc container.
482
483 return indexDecl(D);
484}
485
486bool IndexingContext::indexDeclGroupRef(DeclGroupRef DG) {
487 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
488 if (!indexTopLevelDecl(*I))
489 return false;
490 return true;
491}