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_DECL(D,CALL_EXPR) \
19 if (!IndexCtx.shouldIndex(D)) return true; \
24 #define TRY_TO(CALL_EXPR) \
32 class IndexingDeclVisitor : public ConstDeclVisitor<IndexingDeclVisitor, bool> {
33 IndexingContext &IndexCtx;
36 explicit IndexingDeclVisitor(IndexingContext &indexCtx)
37 : IndexCtx(indexCtx) { }
41 bool VisitDecl(const Decl *D) {
46 /// \brief Returns true if the given method has been defined explicitly by the
48 static bool hasUserDefined(const ObjCMethodDecl *D,
49 const ObjCImplDecl *Container) {
50 const ObjCMethodDecl *MD = Container->getMethod(D->getSelector(),
51 D->isInstanceMethod());
52 return MD && !MD->isImplicit() && MD->isThisDeclarationADefinition();
55 void handleTemplateArgumentLoc(const TemplateArgumentLoc &TALoc,
56 const NamedDecl *Parent,
57 const DeclContext *DC) {
58 const TemplateArgumentLocInfo &LocInfo = TALoc.getLocInfo();
59 switch (TALoc.getArgument().getKind()) {
60 case TemplateArgument::Expression:
61 IndexCtx.indexBody(LocInfo.getAsExpr(), Parent, DC);
63 case TemplateArgument::Type:
64 IndexCtx.indexTypeSourceInfo(LocInfo.getAsTypeSourceInfo(), Parent, DC);
66 case TemplateArgument::Template:
67 case TemplateArgument::TemplateExpansion:
68 IndexCtx.indexNestedNameSpecifierLoc(TALoc.getTemplateQualifierLoc(),
70 if (const TemplateDecl *TD = TALoc.getArgument()
71 .getAsTemplateOrTemplatePattern()
72 .getAsTemplateDecl()) {
73 if (const NamedDecl *TTD = TD->getTemplatedDecl())
74 IndexCtx.handleReference(TTD, TALoc.getTemplateNameLoc(), Parent, DC);
82 void handleDeclarator(const DeclaratorDecl *D,
83 const NamedDecl *Parent = nullptr,
84 bool isIBType = false) {
85 if (!Parent) Parent = D;
87 IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), Parent,
88 Parent->getLexicalDeclContext(),
89 /*isBase=*/false, isIBType);
90 IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), Parent);
91 if (IndexCtx.shouldIndexFunctionLocalSymbols()) {
92 // Only index parameters in definitions, parameters in declarations are
94 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) {
95 auto *DC = Parm->getDeclContext();
96 if (auto *FD = dyn_cast<FunctionDecl>(DC)) {
97 if (FD->isThisDeclarationADefinition())
98 IndexCtx.handleDecl(Parm);
99 } else if (auto *MD = dyn_cast<ObjCMethodDecl>(DC)) {
100 if (MD->isThisDeclarationADefinition())
101 IndexCtx.handleDecl(Parm);
103 IndexCtx.handleDecl(Parm);
105 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
106 if (FD->isThisDeclarationADefinition()) {
107 for (auto PI : FD->parameters()) {
108 IndexCtx.handleDecl(PI);
113 // Index the default parameter value for function definitions.
114 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
115 if (FD->isThisDeclarationADefinition()) {
116 for (const auto *PV : FD->parameters()) {
117 if (PV->hasDefaultArg() && !PV->hasUninstantiatedDefaultArg() &&
118 !PV->hasUnparsedDefaultArg())
119 IndexCtx.indexBody(PV->getDefaultArg(), D);
126 bool handleObjCMethod(const ObjCMethodDecl *D,
127 const ObjCPropertyDecl *AssociatedProp = nullptr) {
128 SmallVector<SymbolRelation, 4> Relations;
129 SmallVector<const ObjCMethodDecl*, 4> Overriden;
131 D->getOverriddenMethods(Overriden);
132 for(auto overridden: Overriden) {
133 Relations.emplace_back((unsigned) SymbolRole::RelationOverrideOf,
137 Relations.emplace_back((unsigned)SymbolRole::RelationAccessorOf,
140 // getLocation() returns beginning token of a method declaration, but for
141 // indexing purposes we want to point to the base name.
142 SourceLocation MethodLoc = D->getSelectorStartLoc();
143 if (MethodLoc.isInvalid())
144 MethodLoc = D->getLocation();
146 SourceLocation AttrLoc;
148 // check for (getter=/setter=)
149 if (AssociatedProp) {
150 bool isGetter = !D->param_size();
152 AssociatedProp->getGetterNameLoc():
153 AssociatedProp->getSetterNameLoc();
156 SymbolRoleSet Roles = (SymbolRoleSet)SymbolRole::Dynamic;
157 if (D->isImplicit()) {
158 if (AttrLoc.isValid()) {
161 Roles |= (SymbolRoleSet)SymbolRole::Implicit;
163 } else if (AttrLoc.isValid()) {
164 IndexCtx.handleReference(D, AttrLoc, cast<NamedDecl>(D->getDeclContext()),
165 D->getDeclContext(), 0);
168 TRY_DECL(D, IndexCtx.handleDecl(D, MethodLoc, Roles, Relations));
169 IndexCtx.indexTypeSourceInfo(D->getReturnTypeSourceInfo(), D);
170 bool hasIBActionAndFirst = D->hasAttr<IBActionAttr>();
171 for (const auto *I : D->parameters()) {
172 handleDeclarator(I, D, /*isIBType=*/hasIBActionAndFirst);
173 hasIBActionAndFirst = false;
176 if (D->isThisDeclarationADefinition()) {
177 const Stmt *Body = D->getBody();
179 IndexCtx.indexBody(Body, D, D);
185 /// Gather the declarations which the given declaration \D overrides in a
186 /// pseudo-override manner.
188 /// Pseudo-overrides occur when a class template specialization declares
189 /// a declaration that has the same name as a similar declaration in the
190 /// non-specialized template.
192 gatherTemplatePseudoOverrides(const NamedDecl *D,
193 SmallVectorImpl<SymbolRelation> &Relations) {
194 if (!IndexCtx.getLangOpts().CPlusPlus)
197 dyn_cast<ClassTemplateSpecializationDecl>(D->getLexicalDeclContext());
200 llvm::PointerUnion<ClassTemplateDecl *,
201 ClassTemplatePartialSpecializationDecl *>
202 Template = CTSD->getSpecializedTemplateOrPartial();
203 if (const auto *CTD = Template.dyn_cast<ClassTemplateDecl *>()) {
204 const CXXRecordDecl *Pattern = CTD->getTemplatedDecl();
205 bool TypeOverride = isa<TypeDecl>(D);
206 for (const NamedDecl *ND : Pattern->lookup(D->getDeclName())) {
207 if (const auto *CTD = dyn_cast<ClassTemplateDecl>(ND))
208 ND = CTD->getTemplatedDecl();
209 if (ND->isImplicit())
211 // Types can override other types.
213 if (ND->getKind() != D->getKind())
215 } else if (!isa<TypeDecl>(ND))
217 if (const auto *FD = dyn_cast<FunctionDecl>(ND)) {
218 const auto *DFD = cast<FunctionDecl>(D);
219 // Function overrides are approximated using the number of parameters.
220 if (FD->getStorageClass() != DFD->getStorageClass() ||
221 FD->getNumParams() != DFD->getNumParams())
224 Relations.emplace_back(
225 SymbolRoleSet(SymbolRole::RelationSpecializationOf), ND);
230 bool VisitFunctionDecl(const FunctionDecl *D) {
231 SymbolRoleSet Roles{};
232 SmallVector<SymbolRelation, 4> Relations;
233 if (auto *CXXMD = dyn_cast<CXXMethodDecl>(D)) {
234 if (CXXMD->isVirtual())
235 Roles |= (unsigned)SymbolRole::Dynamic;
236 for (const CXXMethodDecl *O : CXXMD->overridden_methods()) {
237 Relations.emplace_back((unsigned)SymbolRole::RelationOverrideOf, O);
240 gatherTemplatePseudoOverrides(D, Relations);
241 if (const auto *Base = D->getPrimaryTemplate())
243 SymbolRelation(SymbolRoleSet(SymbolRole::RelationSpecializationOf),
244 Base->getTemplatedDecl()));
246 TRY_DECL(D, IndexCtx.handleDecl(D, Roles, Relations));
249 if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
250 IndexCtx.handleReference(Ctor->getParent(), Ctor->getLocation(),
251 Ctor->getParent(), Ctor->getDeclContext());
253 // Constructor initializers.
254 for (const auto *Init : Ctor->inits()) {
255 if (Init->isWritten()) {
256 IndexCtx.indexTypeSourceInfo(Init->getTypeSourceInfo(), D);
257 if (const FieldDecl *Member = Init->getAnyMember())
258 IndexCtx.handleReference(Member, Init->getMemberLocation(), D, D,
259 (unsigned)SymbolRole::Write);
260 IndexCtx.indexBody(Init->getInit(), D, D);
263 } else if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(D)) {
264 if (auto TypeNameInfo = Dtor->getNameInfo().getNamedTypeInfo()) {
265 IndexCtx.handleReference(Dtor->getParent(),
266 TypeNameInfo->getTypeLoc().getLocStart(),
267 Dtor->getParent(), Dtor->getDeclContext());
269 } else if (const auto *Guide = dyn_cast<CXXDeductionGuideDecl>(D)) {
270 IndexCtx.handleReference(Guide->getDeducedTemplate()->getTemplatedDecl(),
271 Guide->getLocation(), Guide,
272 Guide->getDeclContext());
274 // Template specialization arguments.
275 if (const ASTTemplateArgumentListInfo *TemplateArgInfo =
276 D->getTemplateSpecializationArgsAsWritten()) {
277 for (const auto &Arg : TemplateArgInfo->arguments())
278 handleTemplateArgumentLoc(Arg, D, D->getLexicalDeclContext());
281 if (D->isThisDeclarationADefinition()) {
282 const Stmt *Body = D->getBody();
284 IndexCtx.indexBody(Body, D, D);
290 bool VisitVarDecl(const VarDecl *D) {
291 SmallVector<SymbolRelation, 4> Relations;
292 gatherTemplatePseudoOverrides(D, Relations);
293 TRY_DECL(D, IndexCtx.handleDecl(D, SymbolRoleSet(), Relations));
295 IndexCtx.indexBody(D->getInit(), D);
299 bool VisitDecompositionDecl(const DecompositionDecl *D) {
300 for (const auto *Binding : D->bindings())
301 TRY_DECL(Binding, IndexCtx.handleDecl(Binding));
302 return Base::VisitDecompositionDecl(D);
305 bool VisitFieldDecl(const FieldDecl *D) {
306 SmallVector<SymbolRelation, 4> Relations;
307 gatherTemplatePseudoOverrides(D, Relations);
308 TRY_DECL(D, IndexCtx.handleDecl(D, SymbolRoleSet(), Relations));
311 IndexCtx.indexBody(D->getBitWidth(), D);
312 else if (D->hasInClassInitializer())
313 IndexCtx.indexBody(D->getInClassInitializer(), D);
317 bool VisitObjCIvarDecl(const ObjCIvarDecl *D) {
318 if (D->getSynthesize()) {
319 // handled in VisitObjCPropertyImplDecl
322 TRY_DECL(D, IndexCtx.handleDecl(D));
327 bool VisitMSPropertyDecl(const MSPropertyDecl *D) {
332 bool VisitEnumConstantDecl(const EnumConstantDecl *D) {
333 TRY_DECL(D, IndexCtx.handleDecl(D));
334 IndexCtx.indexBody(D->getInitExpr(), D);
338 bool VisitTypedefNameDecl(const TypedefNameDecl *D) {
339 if (!D->isTransparentTag()) {
340 SmallVector<SymbolRelation, 4> Relations;
341 gatherTemplatePseudoOverrides(D, Relations);
342 TRY_DECL(D, IndexCtx.handleDecl(D, SymbolRoleSet(), Relations));
343 IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D);
348 bool VisitTagDecl(const TagDecl *D) {
349 // Non-free standing tags are handled in indexTypeSourceInfo.
350 if (D->isFreeStanding()) {
351 if (D->isThisDeclarationADefinition()) {
352 SmallVector<SymbolRelation, 4> Relations;
353 gatherTemplatePseudoOverrides(D, Relations);
354 IndexCtx.indexTagDecl(D, Relations);
356 SmallVector<SymbolRelation, 1> Relations;
357 gatherTemplatePseudoOverrides(D, Relations);
358 return IndexCtx.handleDecl(D, D->getLocation(), SymbolRoleSet(),
359 Relations, D->getLexicalDeclContext());
365 bool handleReferencedProtocols(const ObjCProtocolList &ProtList,
366 const ObjCContainerDecl *ContD,
367 SourceLocation SuperLoc) {
368 ObjCInterfaceDecl::protocol_loc_iterator LI = ProtList.loc_begin();
369 for (ObjCInterfaceDecl::protocol_iterator
370 I = ProtList.begin(), E = ProtList.end(); I != E; ++I, ++LI) {
371 SourceLocation Loc = *LI;
372 ObjCProtocolDecl *PD = *I;
373 SymbolRoleSet roles{};
375 roles |= (SymbolRoleSet)SymbolRole::Implicit;
376 TRY_TO(IndexCtx.handleReference(PD, Loc, ContD, ContD, roles,
377 SymbolRelation{(unsigned)SymbolRole::RelationBaseOf, ContD}));
382 bool VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
383 if (D->isThisDeclarationADefinition()) {
384 TRY_DECL(D, IndexCtx.handleDecl(D));
385 SourceLocation SuperLoc = D->getSuperClassLoc();
386 if (auto *SuperD = D->getSuperClass()) {
387 bool hasSuperTypedef = false;
388 if (auto *TInfo = D->getSuperClassTInfo()) {
389 if (auto *TT = TInfo->getType()->getAs<TypedefType>()) {
390 if (auto *TD = TT->getDecl()) {
391 hasSuperTypedef = true;
392 TRY_TO(IndexCtx.handleReference(TD, SuperLoc, D, D,
397 SymbolRoleSet superRoles{};
399 superRoles |= (SymbolRoleSet)SymbolRole::Implicit;
400 TRY_TO(IndexCtx.handleReference(SuperD, SuperLoc, D, D, superRoles,
401 SymbolRelation{(unsigned)SymbolRole::RelationBaseOf, D}));
403 TRY_TO(handleReferencedProtocols(D->getReferencedProtocols(), D,
405 TRY_TO(IndexCtx.indexDeclContext(D));
407 return IndexCtx.handleReference(D, D->getLocation(), nullptr,
408 D->getDeclContext(), SymbolRoleSet());
413 bool VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
414 if (D->isThisDeclarationADefinition()) {
415 TRY_DECL(D, IndexCtx.handleDecl(D));
416 TRY_TO(handleReferencedProtocols(D->getReferencedProtocols(), D,
417 /*superLoc=*/SourceLocation()));
418 TRY_TO(IndexCtx.indexDeclContext(D));
420 return IndexCtx.handleReference(D, D->getLocation(), nullptr,
421 D->getDeclContext(), SymbolRoleSet());
426 bool VisitObjCImplementationDecl(const ObjCImplementationDecl *D) {
427 const ObjCInterfaceDecl *Class = D->getClassInterface();
431 if (Class->isImplicitInterfaceDecl())
432 IndexCtx.handleDecl(Class);
434 TRY_DECL(D, IndexCtx.handleDecl(D));
436 // Visit implicit @synthesize property implementations first as their
437 // location is reported at the name of the @implementation block. This
438 // serves no purpose other than to simplify the FileCheck-based tests.
439 for (const auto *I : D->property_impls()) {
440 if (I->getLocation().isInvalid())
441 IndexCtx.indexDecl(I);
443 for (const auto *I : D->decls()) {
444 if (!isa<ObjCPropertyImplDecl>(I) ||
445 cast<ObjCPropertyImplDecl>(I)->getLocation().isValid())
446 IndexCtx.indexDecl(I);
452 bool VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
453 if (!IndexCtx.shouldIndex(D))
455 const ObjCInterfaceDecl *C = D->getClassInterface();
458 TRY_TO(IndexCtx.handleReference(C, D->getLocation(), D, D, SymbolRoleSet(),
460 (unsigned)SymbolRole::RelationExtendedBy, D
462 SourceLocation CategoryLoc = D->getCategoryNameLoc();
463 if (!CategoryLoc.isValid())
464 CategoryLoc = D->getLocation();
465 TRY_TO(IndexCtx.handleDecl(D, CategoryLoc));
466 TRY_TO(handleReferencedProtocols(D->getReferencedProtocols(), D,
467 /*superLoc=*/SourceLocation()));
468 TRY_TO(IndexCtx.indexDeclContext(D));
472 bool VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) {
473 const ObjCCategoryDecl *Cat = D->getCategoryDecl();
476 const ObjCInterfaceDecl *C = D->getClassInterface();
478 TRY_TO(IndexCtx.handleReference(C, D->getLocation(), D, D,
480 SourceLocation CategoryLoc = D->getCategoryNameLoc();
481 if (!CategoryLoc.isValid())
482 CategoryLoc = D->getLocation();
483 TRY_DECL(D, IndexCtx.handleDecl(D, CategoryLoc));
484 IndexCtx.indexDeclContext(D);
488 bool VisitObjCMethodDecl(const ObjCMethodDecl *D) {
489 // Methods associated with a property, even user-declared ones, are
490 // handled when we handle the property.
491 if (D->isPropertyAccessor())
498 bool VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
499 if (ObjCMethodDecl *MD = D->getGetterMethodDecl())
500 if (MD->getLexicalDeclContext() == D->getLexicalDeclContext())
501 handleObjCMethod(MD, D);
502 if (ObjCMethodDecl *MD = D->getSetterMethodDecl())
503 if (MD->getLexicalDeclContext() == D->getLexicalDeclContext())
504 handleObjCMethod(MD, D);
505 TRY_DECL(D, IndexCtx.handleDecl(D));
506 if (IBOutletCollectionAttr *attr = D->getAttr<IBOutletCollectionAttr>())
507 IndexCtx.indexTypeSourceInfo(attr->getInterfaceLoc(), D,
508 D->getLexicalDeclContext(), false, true);
509 IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D);
513 bool VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
514 ObjCPropertyDecl *PD = D->getPropertyDecl();
515 auto *Container = cast<ObjCImplDecl>(D->getDeclContext());
516 SourceLocation Loc = D->getLocation();
517 SymbolRoleSet Roles = 0;
518 SmallVector<SymbolRelation, 1> Relations;
520 if (ObjCIvarDecl *ID = D->getPropertyIvarDecl())
521 Relations.push_back({(SymbolRoleSet)SymbolRole::RelationAccessorOf, ID});
522 if (Loc.isInvalid()) {
523 Loc = Container->getLocation();
524 Roles |= (SymbolRoleSet)SymbolRole::Implicit;
526 TRY_DECL(D, IndexCtx.handleDecl(D, Loc, Roles, Relations));
528 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
531 assert(D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize);
532 SymbolRoleSet AccessorMethodRoles =
533 SymbolRoleSet(SymbolRole::Dynamic) | SymbolRoleSet(SymbolRole::Implicit);
534 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) {
535 if (MD->isPropertyAccessor() &&
536 !hasUserDefined(MD, Container))
537 IndexCtx.handleDecl(MD, Loc, AccessorMethodRoles, {}, Container);
539 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) {
540 if (MD->isPropertyAccessor() &&
541 !hasUserDefined(MD, Container))
542 IndexCtx.handleDecl(MD, Loc, AccessorMethodRoles, {}, Container);
544 if (ObjCIvarDecl *IvarD = D->getPropertyIvarDecl()) {
545 if (IvarD->getSynthesize()) {
546 // For synthesized ivars, use the location of its name in the
547 // corresponding @synthesize. If there isn't one, use the containing
548 // @implementation's location, rather than the property's location,
549 // otherwise the header file containing the @interface will have different
550 // indexing contents based on whether the @implementation was present or
551 // not in the translation unit.
552 SymbolRoleSet IvarRoles = 0;
553 SourceLocation IvarLoc = D->getPropertyIvarDeclLoc();
554 if (D->getLocation().isInvalid()) {
555 IvarLoc = Container->getLocation();
556 IvarRoles = (SymbolRoleSet)SymbolRole::Implicit;
557 } else if (D->getLocation() == IvarLoc) {
558 IvarRoles = (SymbolRoleSet)SymbolRole::Implicit;
560 TRY_DECL(IvarD, IndexCtx.handleDecl(IvarD, IvarLoc, IvarRoles));
562 IndexCtx.handleReference(IvarD, D->getPropertyIvarDeclLoc(), nullptr,
563 D->getDeclContext(), SymbolRoleSet());
569 bool VisitNamespaceDecl(const NamespaceDecl *D) {
570 TRY_DECL(D, IndexCtx.handleDecl(D));
571 IndexCtx.indexDeclContext(D);
575 bool VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
576 TRY_DECL(D, IndexCtx.handleDecl(D));
577 IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), D);
578 IndexCtx.handleReference(D->getAliasedNamespace(), D->getTargetNameLoc(), D,
579 D->getLexicalDeclContext());
583 bool VisitUsingDecl(const UsingDecl *D) {
584 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
585 const NamedDecl *Parent = dyn_cast<NamedDecl>(DC);
587 IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), Parent,
588 D->getLexicalDeclContext());
589 for (const auto *I : D->shadows())
590 IndexCtx.handleReference(I->getUnderlyingDecl(), D->getLocation(), Parent,
591 D->getLexicalDeclContext(), SymbolRoleSet());
595 bool VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
596 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
597 const NamedDecl *Parent = dyn_cast<NamedDecl>(DC);
599 // NNS for the local 'using namespace' directives is visited by the body
601 if (!D->getParentFunctionOrMethod())
602 IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), Parent,
603 D->getLexicalDeclContext());
605 return IndexCtx.handleReference(D->getNominatedNamespaceAsWritten(),
606 D->getLocation(), Parent,
607 D->getLexicalDeclContext(),
611 bool VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
612 TRY_DECL(D, IndexCtx.handleDecl(D));
613 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
614 const NamedDecl *Parent = dyn_cast<NamedDecl>(DC);
615 IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), Parent,
616 D->getLexicalDeclContext());
620 bool VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D) {
621 TRY_DECL(D, IndexCtx.handleDecl(D));
622 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
623 const NamedDecl *Parent = dyn_cast<NamedDecl>(DC);
624 IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), Parent,
625 D->getLexicalDeclContext());
629 bool VisitClassTemplateSpecializationDecl(const
630 ClassTemplateSpecializationDecl *D) {
631 // FIXME: Notify subsequent callbacks if info comes from implicit
633 llvm::PointerUnion<ClassTemplateDecl *,
634 ClassTemplatePartialSpecializationDecl *>
635 Template = D->getSpecializedTemplateOrPartial();
636 const Decl *SpecializationOf =
637 Template.is<ClassTemplateDecl *>()
638 ? (Decl *)Template.get<ClassTemplateDecl *>()
639 : Template.get<ClassTemplatePartialSpecializationDecl *>();
640 if (!D->isThisDeclarationADefinition())
641 IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), D);
642 IndexCtx.indexTagDecl(
643 D, SymbolRelation(SymbolRoleSet(SymbolRole::RelationSpecializationOf),
645 if (TypeSourceInfo *TSI = D->getTypeAsWritten())
646 IndexCtx.indexTypeSourceInfo(TSI, /*Parent=*/nullptr,
647 D->getLexicalDeclContext());
651 static bool shouldIndexTemplateParameterDefaultValue(const NamedDecl *D) {
654 // We want to index the template parameters only once when indexing the
655 // canonical declaration.
656 if (const auto *FD = dyn_cast<FunctionDecl>(D))
657 return FD->getCanonicalDecl() == FD;
658 else if (const auto *TD = dyn_cast<TagDecl>(D))
659 return TD->getCanonicalDecl() == TD;
660 else if (const auto *VD = dyn_cast<VarDecl>(D))
661 return VD->getCanonicalDecl() == VD;
665 bool VisitTemplateDecl(const TemplateDecl *D) {
667 // Index the default values for the template parameters.
668 const NamedDecl *Parent = D->getTemplatedDecl();
669 if (D->getTemplateParameters() &&
670 shouldIndexTemplateParameterDefaultValue(Parent)) {
671 const TemplateParameterList *Params = D->getTemplateParameters();
672 for (const NamedDecl *TP : *Params) {
673 if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(TP)) {
674 if (TTP->hasDefaultArgument())
675 IndexCtx.indexTypeSourceInfo(TTP->getDefaultArgumentInfo(), Parent);
676 } else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(TP)) {
677 if (NTTP->hasDefaultArgument())
678 IndexCtx.indexBody(NTTP->getDefaultArgument(), Parent);
679 } else if (const auto *TTPD = dyn_cast<TemplateTemplateParmDecl>(TP)) {
680 if (TTPD->hasDefaultArgument())
681 handleTemplateArgumentLoc(TTPD->getDefaultArgument(), Parent,
682 TP->getLexicalDeclContext());
687 return Visit(D->getTemplatedDecl());
690 bool VisitFriendDecl(const FriendDecl *D) {
691 if (auto ND = D->getFriendDecl()) {
692 // FIXME: Ignore a class template in a dependent context, these are not
693 // linked properly with their redeclarations, ending up with duplicate
695 // See comment "Friend templates are visible in fairly strange ways." in
696 // SemaTemplate.cpp which precedes code that prevents the friend template
697 // from becoming visible from the enclosing context.
698 if (isa<ClassTemplateDecl>(ND) && D->getDeclContext()->isDependentContext())
702 if (auto Ty = D->getFriendType()) {
703 IndexCtx.indexTypeSourceInfo(Ty, cast<NamedDecl>(D->getDeclContext()));
708 bool VisitImportDecl(const ImportDecl *D) {
709 return IndexCtx.importedModule(D);
712 bool VisitStaticAssertDecl(const StaticAssertDecl *D) {
713 IndexCtx.indexBody(D->getAssertExpr(),
714 dyn_cast<NamedDecl>(D->getDeclContext()),
715 D->getLexicalDeclContext());
720 } // anonymous namespace
722 bool IndexingContext::indexDecl(const Decl *D) {
723 if (D->isImplicit() && shouldIgnoreIfImplicit(D))
726 if (isTemplateImplicitInstantiation(D))
729 IndexingDeclVisitor Visitor(*this);
730 bool ShouldContinue = Visitor.Visit(D);
734 if (!Visitor.Handled && isa<DeclContext>(D))
735 return indexDeclContext(cast<DeclContext>(D));
740 bool IndexingContext::indexDeclContext(const DeclContext *DC) {
741 for (const auto *I : DC->decls())
747 bool IndexingContext::indexTopLevelDecl(const Decl *D) {
748 if (D->getLocation().isInvalid())
751 if (isa<ObjCMethodDecl>(D))
752 return true; // Wait for the objc container.
757 bool IndexingContext::indexDeclGroupRef(DeclGroupRef DG) {
758 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
759 if (!indexTopLevelDecl(*I))