1 //===- IndexTypeSourceInfo.cpp - Indexing types ---------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "IndexingContext.h"
10 #include "clang/AST/RecursiveASTVisitor.h"
12 using namespace clang;
13 using namespace index;
17 class TypeIndexer : public RecursiveASTVisitor<TypeIndexer> {
18 IndexingContext &IndexCtx;
19 const NamedDecl *Parent;
20 const DeclContext *ParentDC;
22 SmallVector<SymbolRelation, 3> Relations;
24 typedef RecursiveASTVisitor<TypeIndexer> base;
27 TypeIndexer(IndexingContext &indexCtx, const NamedDecl *parent,
28 const DeclContext *DC, bool isBase, bool isIBType)
29 : IndexCtx(indexCtx), Parent(parent), ParentDC(DC), IsBase(isBase) {
32 Relations.emplace_back((unsigned)SymbolRole::RelationBaseOf, Parent);
36 Relations.emplace_back((unsigned)SymbolRole::RelationIBTypeOf, Parent);
40 bool shouldWalkTypesOfTypeLocs() const { return false; }
42 #define TRY_TO(CALL_EXPR) \
48 bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TTPL) {
49 SourceLocation Loc = TTPL.getNameLoc();
50 TemplateTypeParmDecl *TTPD = TTPL.getDecl();
51 return IndexCtx.handleReference(TTPD, Loc, Parent, ParentDC,
55 bool VisitTypedefTypeLoc(TypedefTypeLoc TL) {
56 SourceLocation Loc = TL.getNameLoc();
57 TypedefNameDecl *ND = TL.getTypedefNameDecl();
58 if (ND->isTransparentTag()) {
59 TagDecl *Underlying = ND->getUnderlyingType()->getAsTagDecl();
60 return IndexCtx.handleReference(Underlying, Loc, Parent,
61 ParentDC, SymbolRoleSet(), Relations);
64 TRY_TO(IndexCtx.handleReference(ND, Loc,
65 Parent, ParentDC, SymbolRoleSet()));
66 if (auto *CD = TL.getType()->getAsCXXRecordDecl()) {
67 TRY_TO(IndexCtx.handleReference(CD, Loc, Parent, ParentDC,
68 (unsigned)SymbolRole::Implicit,
72 TRY_TO(IndexCtx.handleReference(ND, Loc,
73 Parent, ParentDC, SymbolRoleSet(),
79 bool traverseParamVarHelper(ParmVarDecl *D) {
80 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
81 if (D->getTypeSourceInfo())
82 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
86 bool TraverseParmVarDecl(ParmVarDecl *D) {
87 // Avoid visiting default arguments from the definition that were already
88 // visited in the declaration.
89 // FIXME: A free function definition can have default arguments.
90 // Avoiding double visitaiton of default arguments should be handled by the
91 // visitor probably with a bit in the AST to indicate if the attached
92 // default argument was 'inherited' or written in source.
93 if (auto FD = dyn_cast<FunctionDecl>(D->getDeclContext())) {
94 if (FD->isThisDeclarationADefinition()) {
95 return traverseParamVarHelper(D);
99 return base::TraverseParmVarDecl(D);
102 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
103 IndexCtx.indexNestedNameSpecifierLoc(NNS, Parent, ParentDC);
107 bool VisitTagTypeLoc(TagTypeLoc TL) {
108 TagDecl *D = TL.getDecl();
109 if (!IndexCtx.shouldIndexFunctionLocalSymbols() &&
110 D->getParentFunctionOrMethod())
113 if (TL.isDefinition()) {
114 IndexCtx.indexTagDecl(D);
118 return IndexCtx.handleReference(D, TL.getNameLoc(),
119 Parent, ParentDC, SymbolRoleSet(),
123 bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
124 return IndexCtx.handleReference(TL.getIFaceDecl(), TL.getNameLoc(),
125 Parent, ParentDC, SymbolRoleSet(), Relations);
128 bool VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
129 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) {
130 IndexCtx.handleReference(TL.getProtocol(i), TL.getProtocolLoc(i),
131 Parent, ParentDC, SymbolRoleSet(), Relations);
136 void HandleTemplateSpecializationTypeLoc(TemplateName TemplName,
137 SourceLocation TemplNameLoc,
138 CXXRecordDecl *ResolvedClass,
140 // In presence of type aliases, the resolved class was never written in
141 // the code so don't report it.
142 if (!IsTypeAlias && ResolvedClass &&
143 (!ResolvedClass->isImplicit() ||
144 IndexCtx.shouldIndexImplicitInstantiation())) {
145 IndexCtx.handleReference(ResolvedClass, TemplNameLoc, Parent, ParentDC,
146 SymbolRoleSet(), Relations);
147 } else if (const TemplateDecl *D = TemplName.getAsTemplateDecl()) {
148 IndexCtx.handleReference(D, TemplNameLoc, Parent, ParentDC,
149 SymbolRoleSet(), Relations);
153 bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
154 auto *T = TL.getTypePtr();
157 HandleTemplateSpecializationTypeLoc(
158 T->getTemplateName(), TL.getTemplateNameLoc(), T->getAsCXXRecordDecl(),
163 bool VisitDeducedTemplateSpecializationTypeLoc(DeducedTemplateSpecializationTypeLoc TL) {
164 auto *T = TL.getTypePtr();
167 HandleTemplateSpecializationTypeLoc(
168 T->getTemplateName(), TL.getTemplateNameLoc(), T->getAsCXXRecordDecl(),
169 /*IsTypeAlias=*/false);
173 bool VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
174 const DependentNameType *DNT = TL.getTypePtr();
175 const NestedNameSpecifier *NNS = DNT->getQualifier();
176 const Type *T = NNS->getAsType();
179 const TemplateSpecializationType *TST =
180 T->getAs<TemplateSpecializationType>();
183 TemplateName TN = TST->getTemplateName();
184 const ClassTemplateDecl *TD =
185 dyn_cast_or_null<ClassTemplateDecl>(TN.getAsTemplateDecl());
188 CXXRecordDecl *RD = TD->getTemplatedDecl();
189 if (!RD->hasDefinition())
191 RD = RD->getDefinition();
192 DeclarationName Name(DNT->getIdentifier());
193 std::vector<const NamedDecl *> Symbols = RD->lookupDependentName(
194 Name, [](const NamedDecl *ND) { return isa<TypeDecl>(ND); });
195 if (Symbols.size() != 1)
197 return IndexCtx.handleReference(Symbols[0], TL.getNameLoc(), Parent,
198 ParentDC, SymbolRoleSet(), Relations);
201 bool TraverseStmt(Stmt *S) {
202 IndexCtx.indexBody(S, Parent, ParentDC);
207 } // anonymous namespace
209 void IndexingContext::indexTypeSourceInfo(TypeSourceInfo *TInfo,
210 const NamedDecl *Parent,
211 const DeclContext *DC,
214 if (!TInfo || TInfo->getTypeLoc().isNull())
217 indexTypeLoc(TInfo->getTypeLoc(), Parent, DC, isBase, isIBType);
220 void IndexingContext::indexTypeLoc(TypeLoc TL,
221 const NamedDecl *Parent,
222 const DeclContext *DC,
229 DC = Parent->getLexicalDeclContext();
230 TypeIndexer(*this, Parent, DC, isBase, isIBType).TraverseTypeLoc(TL);
233 void IndexingContext::indexNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
234 const NamedDecl *Parent,
235 const DeclContext *DC) {
239 if (NestedNameSpecifierLoc Prefix = NNS.getPrefix())
240 indexNestedNameSpecifierLoc(Prefix, Parent, DC);
243 DC = Parent->getLexicalDeclContext();
244 SourceLocation Loc = NNS.getLocalBeginLoc();
246 switch (NNS.getNestedNameSpecifier()->getKind()) {
247 case NestedNameSpecifier::Identifier:
248 case NestedNameSpecifier::Global:
249 case NestedNameSpecifier::Super:
252 case NestedNameSpecifier::Namespace:
253 handleReference(NNS.getNestedNameSpecifier()->getAsNamespace(),
254 Loc, Parent, DC, SymbolRoleSet());
256 case NestedNameSpecifier::NamespaceAlias:
257 handleReference(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(),
258 Loc, Parent, DC, SymbolRoleSet());
261 case NestedNameSpecifier::TypeSpec:
262 case NestedNameSpecifier::TypeSpecWithTemplate:
263 indexTypeLoc(NNS.getTypeLoc(), Parent, DC);
268 void IndexingContext::indexTagDecl(const TagDecl *D,
269 ArrayRef<SymbolRelation> Relations) {
272 if (!shouldIndexFunctionLocalSymbols() && isFunctionLocalSymbol(D))
275 if (handleDecl(D, /*Roles=*/SymbolRoleSet(), Relations)) {
276 if (D->isThisDeclarationADefinition()) {
277 indexNestedNameSpecifierLoc(D->getQualifierLoc(), D);
278 if (auto CXXRD = dyn_cast<CXXRecordDecl>(D)) {
279 for (const auto &I : CXXRD->bases()) {
280 indexTypeSourceInfo(I.getTypeSourceInfo(), CXXRD, CXXRD, /*isBase=*/true);