clang 24.0.0git
IndexTypeSourceInfo.cpp
Go to the documentation of this file.
1//===- IndexTypeSourceInfo.cpp - Indexing types ---------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "IndexingContext.h"
13#include "clang/AST/TypeLoc.h"
15#include "llvm/ADT/ScopeExit.h"
16
17using namespace clang;
18using namespace index;
19
20namespace {
21
22class TypeIndexer : public RecursiveASTVisitor<TypeIndexer> {
23 IndexingContext &IndexCtx;
24 const NamedDecl *Parent;
25 const DeclContext *ParentDC;
26 bool IsBase;
27 SmallVector<SymbolRelation, 3> Relations;
28
29 typedef RecursiveASTVisitor<TypeIndexer> base;
30
31public:
32 TypeIndexer(IndexingContext &indexCtx, const NamedDecl *parent,
33 const DeclContext *DC, bool isBase, bool isIBType)
34 : IndexCtx(indexCtx), Parent(parent), ParentDC(DC), IsBase(isBase) {
35 if (IsBase) {
36 assert(Parent);
37 Relations.emplace_back((unsigned)SymbolRole::RelationBaseOf, Parent);
38 }
39 if (isIBType) {
40 assert(Parent);
41 Relations.emplace_back((unsigned)SymbolRole::RelationIBTypeOf, Parent);
42 }
43 }
44
45 bool shouldWalkTypesOfTypeLocs() const { return false; }
46
47#define TRY_TO(CALL_EXPR) \
48 do { \
49 if (!CALL_EXPR) \
50 return false; \
51 } while (0)
52
53 bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TTPL) {
54 SourceLocation Loc = TTPL.getNameLoc();
55 TemplateTypeParmDecl *TTPD = TTPL.getDecl();
56 return IndexCtx.handleReference(TTPD, Loc, Parent, ParentDC,
58 }
59
60 bool handleTypeNameReference(NamedDecl *ND, SourceLocation Loc, QualType T) {
61 if (auto *TND = dyn_cast<TypedefNameDecl>(ND))
62 if (TND->isTransparentTag())
63 ND = TND->getUnderlyingType()->castAsTagDecl();
64 if (IsBase && !isa<TagDecl>(ND)) {
65 TRY_TO(
66 IndexCtx.handleReference(ND, Loc, Parent, ParentDC, SymbolRoleSet()));
67 if (auto *CD = T->getAsCXXRecordDecl()) {
68 TRY_TO(IndexCtx.handleReference(CD, Loc, Parent, ParentDC,
69 (unsigned)SymbolRole::Implicit,
70 Relations));
71 }
72 return true;
73 }
74 return IndexCtx.handleReference(ND, Loc, Parent, ParentDC, SymbolRoleSet(),
75 Relations);
76 }
77
78 bool VisitTypedefTypeLoc(TypedefTypeLoc TL) {
79 return handleTypeNameReference(TL.getDecl(), TL.getNameLoc(), TL.getType());
80 }
81
82 // A type found via a using-declaration, e.g. `ns2::A` after
83 // `namespace ns2 { using ns1::A; }`. Like DeclRefExpr, report the target
84 // declaration rather than the UsingShadowDecl.
85 bool VisitUsingTypeLoc(UsingTypeLoc TL) {
86 return handleTypeNameReference(TL.getDecl()->getTargetDecl(),
87 TL.getNameLoc(), TL.getType());
88 }
89
90 bool VisitAutoTypeLoc(AutoTypeLoc TL) {
91 if (auto *C = TL.getNamedConcept().getAsTemplateDecl())
92 return IndexCtx.handleReference(C, TL.getConceptNameLoc(), Parent,
93 ParentDC);
94 return true;
95 }
96
97 bool traverseParamVarHelper(ParmVarDecl *D) {
99 if (D->getTypeSourceInfo())
100 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
101 return true;
102 }
103
104 bool TraverseParmVarDecl(ParmVarDecl *D) {
105 // Avoid visiting default arguments from the definition that were already
106 // visited in the declaration.
107 // FIXME: A free function definition can have default arguments.
108 // Avoiding double visitaiton of default arguments should be handled by the
109 // visitor probably with a bit in the AST to indicate if the attached
110 // default argument was 'inherited' or written in source.
111 if (auto FD = dyn_cast<FunctionDecl>(D->getDeclContext())) {
112 if (FD->isThisDeclarationADefinition()) {
113 return traverseParamVarHelper(D);
114 }
115 }
116
117 return base::TraverseParmVarDecl(D);
118 }
119
120 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
121 IndexCtx.indexNestedNameSpecifierLoc(NNS, Parent, ParentDC);
122 return true;
123 }
124
125 bool VisitTagTypeLoc(TagTypeLoc TL) {
126 TagDecl *D = TL.getDecl();
127 if (!IndexCtx.shouldIndexFunctionLocalSymbols() &&
129 return true;
130
131 if (TL.isDefinition()) {
132 IndexCtx.indexTagDecl(D);
133 return true;
134 }
135
136 return IndexCtx.handleReference(D, TL.getNameLoc(),
137 Parent, ParentDC, SymbolRoleSet(),
138 Relations);
139 }
140
141 bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
142 return IndexCtx.handleReference(TL.getIFaceDecl(), TL.getNameLoc(),
143 Parent, ParentDC, SymbolRoleSet(), Relations);
144 }
145
146 bool VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
147 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) {
148 IndexCtx.handleReference(TL.getProtocol(i), TL.getProtocolLoc(i),
149 Parent, ParentDC, SymbolRoleSet(), Relations);
150 }
151 return true;
152 }
153
154 void HandleTemplateSpecializationTypeLoc(TemplateName TemplName,
155 SourceLocation TemplNameLoc,
156 CXXRecordDecl *ResolvedClass,
157 bool IsTypeAlias) {
158 // In presence of type aliases, the resolved class was never written in
159 // the code so don't report it.
160 if (!IsTypeAlias && ResolvedClass &&
161 (!ResolvedClass->isImplicit() ||
162 IndexCtx.shouldIndexImplicitInstantiation())) {
163 IndexCtx.handleReference(ResolvedClass, TemplNameLoc, Parent, ParentDC,
164 SymbolRoleSet(), Relations);
165 } else if (const TemplateDecl *D = TemplName.getAsTemplateDecl()) {
166 IndexCtx.handleReference(D, TemplNameLoc, Parent, ParentDC,
167 SymbolRoleSet(), Relations);
168 }
169 }
170
171 bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
172 auto *T = TL.getTypePtr();
173 if (!T)
174 return true;
175 HandleTemplateSpecializationTypeLoc(
176 T->getTemplateName(), TL.getTemplateNameLoc(), T->getAsCXXRecordDecl(),
177 T->isTypeAlias());
178 return true;
179 }
180
181 bool TraverseTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL,
182 bool TraverseQualifier) {
183 if (!WalkUpFromTemplateSpecializationTypeLoc(TL))
184 return false;
185 if (!TraverseTemplateName(TL.getTypePtr()->getTemplateName()))
186 return false;
187
188 // The relations we have to `Parent` do not apply to our template arguments,
189 // so clear them while visiting the args.
190 SmallVector<SymbolRelation, 3> SavedRelations = Relations;
191 Relations.clear();
192 llvm::scope_exit ResetSavedRelations(
193 [&] { this->Relations = SavedRelations; });
194 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
195 if (!TraverseTemplateArgumentLoc(TL.getArgLoc(I)))
196 return false;
197 }
198
199 return true;
200 }
201
202 bool VisitDeducedTemplateSpecializationTypeLoc(DeducedTemplateSpecializationTypeLoc TL) {
203 auto *T = TL.getTypePtr();
204 if (!T)
205 return true;
206 HandleTemplateSpecializationTypeLoc(
207 T->getTemplateName(), TL.getTemplateNameLoc(), T->getAsCXXRecordDecl(),
208 /*IsTypeAlias=*/false);
209 return true;
210 }
211
212 bool VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
213 std::vector<const NamedDecl *> Symbols =
214 IndexCtx.getResolver()->resolveDependentNameType(TL.getTypePtr());
215 if (Symbols.size() != 1)
216 return true;
217 return IndexCtx.handleReference(Symbols[0], TL.getNameLoc(), Parent,
218 ParentDC, SymbolRoleSet(), Relations);
219 }
220
221 bool TraverseStmt(Stmt *S) {
222 IndexCtx.indexBody(S, Parent, ParentDC);
223 return true;
224 }
225};
226
227} // anonymous namespace
228
230 const NamedDecl *Parent,
231 const DeclContext *DC,
232 bool isBase,
233 bool isIBType) {
234 if (!TInfo || TInfo->getTypeLoc().isNull())
235 return;
236
237 indexTypeLoc(TInfo->getTypeLoc(), Parent, DC, isBase, isIBType);
238}
239
241 const NamedDecl *Parent,
242 const DeclContext *DC,
243 bool isBase,
244 bool isIBType) {
245 if (TL.isNull())
246 return;
247
248 if (!DC)
249 DC = Parent->getLexicalDeclContext();
250 TypeIndexer(*this, Parent, DC, isBase, isIBType).TraverseTypeLoc(TL);
251}
252
254 NestedNameSpecifierLoc QualifierLoc, const NamedDecl *Parent,
255 const DeclContext *DC) {
256 if (!DC)
257 DC = Parent->getLexicalDeclContext();
258 switch (NestedNameSpecifier Qualifier = QualifierLoc.getNestedNameSpecifier();
259 Qualifier.getKind()) {
263 break;
264
266 auto [Namespace, Prefix] = QualifierLoc.castAsNamespaceAndPrefix();
267 indexNestedNameSpecifierLoc(Prefix, Parent, DC);
268 handleReference(Namespace, QualifierLoc.getLocalBeginLoc(), Parent, DC,
269 SymbolRoleSet());
270 break;
271 }
272
274 indexTypeLoc(QualifierLoc.castAsTypeLoc(), Parent, DC);
275 break;
276 }
277}
278
280 ArrayRef<SymbolRelation> Relations) {
281 if (!shouldIndex(D))
282 return;
284 return;
285
286 if (handleDecl(D, /*Roles=*/SymbolRoleSet(), Relations)) {
289 if (auto CXXRD = dyn_cast<CXXRecordDecl>(D)) {
290 for (const auto &I : CXXRD->bases()) {
291 indexTypeSourceInfo(I.getTypeSourceInfo(), CXXRD, CXXRD, /*isBase=*/true);
292 }
293 }
295 }
296 }
297}
This file provides AST data structures related to concepts.
bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc QualifierLoc)
#define TRY_TO(CALL_EXPR)
Defines the clang::TypeLoc interface and its subclasses.
TemplateName getNamedConcept() const
Definition TypeLoc.h:2558
SourceLocation getConceptNameLoc() const
Definition TypeLoc.h:2546
const TypeClass * getTypePtr() const
Definition TypeLoc.h:433
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition DeclBase.h:1467
const DeclContext * getParentFunctionOrMethod(bool LexicalParent=false) const
If this decl is defined inside a function/method/block it returns the corresponding DeclContext,...
Definition DeclBase.cpp:344
bool isImplicit() const
isImplicit - Indicates whether the declaration was implicitly generated by the implementation.
Definition DeclBase.h:602
DeclContext * getDeclContext()
Definition DeclBase.h:457
DeclContext * getLexicalDeclContext()
getLexicalDeclContext - The declaration context where this Decl was lexically declared (LexicalDC).
Definition DeclBase.h:936
NestedNameSpecifierLoc getQualifierLoc() const
Retrieve the nested-name-specifier (with source-location information) that qualifies the name of this...
Definition Decl.h:847
TypeSourceInfo * getTypeSourceInfo() const
Definition Decl.h:811
SourceLocation getTemplateNameLoc() const
Definition TypeLoc.h:2640
SourceLocation getNameLoc() const
Definition TypeLoc.h:2720
SourceLocation getNameLoc() const
Definition TypeLoc.h:761
This represents a decl that may have a name.
Definition Decl.h:276
A C++ nested-name-specifier augmented with source location information.
NestedNameSpecifier getNestedNameSpecifier() const
Retrieve the nested-name-specifier to which this instance refers.
NamespaceAndPrefixLoc castAsNamespaceAndPrefix() const
For a nested-name-specifier that refers to a namespace, retrieve the namespace and its prefix.
TypeLoc castAsTypeLoc() const
For a nested-name-specifier that refers to a type, retrieve the type with source-location information...
SourceLocation getLocalBeginLoc() const
Retrieve the location of the beginning of this component of the nested-name-specifier.
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
@ MicrosoftSuper
Microsoft's '__super' specifier, stored as a CXXRecordDecl* of the class it appeared in.
@ Global
The global specifier '::'. There is no stored value.
@ Namespace
A namespace-like entity, stored as a NamespaceBaseDecl*.
ObjCInterfaceDecl * getIFaceDecl() const
Definition TypeLoc.h:1305
SourceLocation getNameLoc() const
Definition TypeLoc.h:1309
ObjCProtocolDecl * getProtocol(unsigned i) const
Definition TypeLoc.h:1244
unsigned getNumProtocols() const
Definition TypeLoc.h:1230
SourceLocation getProtocolLoc(unsigned i) const
Definition TypeLoc.h:1234
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
Encodes a location in the source.
Represents the declaration of a struct/union/class/enum.
Definition Decl.h:3898
bool isThisDeclarationADefinition() const
Return true if this declaration is a completion definition of the type.
Definition Decl.h:3994
NestedNameSpecifierLoc getQualifierLoc() const
Retrieve the nested-name-specifier (with source-location information) that qualifies the name of this...
Definition Decl.h:4152
TagDecl * getDecl() const
Definition TypeLoc.h:796
SourceLocation getNameLoc() const
Definition TypeLoc.h:822
bool isDefinition() const
True if the tag was defined in this type specifier.
Definition TypeLoc.cpp:305
TemplateDecl * getAsTemplateDecl(bool IgnoreDeduced=false) const
Retrieve the underlying template declaration that this template name refers to, if known.
TemplateArgumentLoc getArgLoc(unsigned i) const
Definition TypeLoc.h:1948
SourceLocation getTemplateNameLoc() const
Definition TypeLoc.h:1936
Declaration of a template type parameter.
Wrapper for template type parameters.
Definition TypeLoc.h:881
TemplateTypeParmDecl * getDecl() const
Definition TypeLoc.h:883
Base wrapper for a particular "section" of type source info.
Definition TypeLoc.h:59
QualType getType() const
Get the type for which this source info wrapper provides information.
Definition TypeLoc.h:133
bool isNull() const
Definition TypeLoc.h:121
A container of type source information.
Definition TypeBase.h:8651
TypeLoc getTypeLoc() const
Return the TypeLoc wrapper for the type source info.
Definition TypeLoc.h:267
SourceLocation getNameLoc() const
Definition TypeLoc.h:547
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition Type.h:26
bool handleDecl(const Decl *D, SymbolRoleSet Roles=SymbolRoleSet(), ArrayRef< SymbolRelation > Relations={})
bool indexDeclContext(const DeclContext *DC)
bool handleReference(const NamedDecl *D, SourceLocation Loc, const NamedDecl *Parent, const DeclContext *DC, SymbolRoleSet Roles=SymbolRoleSet(), ArrayRef< SymbolRelation > Relations={}, const Expr *RefE=nullptr)
void indexTagDecl(const TagDecl *D, ArrayRef< SymbolRelation > Relations={})
void indexNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS, const NamedDecl *Parent, const DeclContext *DC=nullptr)
void indexTypeLoc(TypeLoc TL, const NamedDecl *Parent, const DeclContext *DC=nullptr, bool isBase=false, bool isIBType=false)
void indexTypeSourceInfo(TypeSourceInfo *TInfo, const NamedDecl *Parent, const DeclContext *DC=nullptr, bool isBase=false, bool isIBType=false)
bool isFunctionLocalSymbol(const Decl *D)
unsigned SymbolRoleSet
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ TemplateName
The identifier is a template name. FIXME: Add an annotation for that.
Definition Parser.h:61