clang: lib/Index/IndexTypeSourceInfo.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
15#include "llvm/ADT/ScopeExit.h"
16
17using namespace clang;
18using namespace index;
19
20namespace {
21
23 IndexingContext &IndexCtx;
24 const NamedDecl *Parent;
25 const DeclContext *ParentDC;
26 bool IsBase;
27 SmallVector<SymbolRelation, 3> Relations;
28
29 typedef RecursiveASTVisitor 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
56 return IndexCtx.handleReference(TTPD, Loc, Parent, ParentDC,
58 }
59
60 bool VisitTypedefTypeLoc(TypedefTypeLoc TL) {
62 TypedefNameDecl *ND = TL.getDecl();
65 return IndexCtx.handleReference(Underlying, Loc, Parent,
67 }
68 if (IsBase) {
69 TRY_TO(IndexCtx.handleReference(ND, Loc,
72 TRY_TO(IndexCtx.handleReference(CD, Loc, Parent, ParentDC,
73 (unsigned)SymbolRole::Implicit,
74 Relations));
75 }
76 } else {
77 TRY_TO(IndexCtx.handleReference(ND, Loc,
79 Relations));
80 }
81 return true;
82 }
83
84 bool VisitAutoTypeLoc(AutoTypeLoc TL) {
87 ParentDC);
88 return true;
89 }
90
91 bool traverseParamVarHelper(ParmVarDecl *D) {
95 return true;
96 }
97
98 bool TraverseParmVarDecl(ParmVarDecl *D) {
99
100
101
102
103
104
105 if (auto FD = dyn_cast(D->getDeclContext())) {
106 if (FD->isThisDeclarationADefinition()) {
107 return traverseParamVarHelper(D);
108 }
109 }
110
111 return base::TraverseParmVarDecl(D);
112 }
113
115 IndexCtx.indexNestedNameSpecifierLoc(NNS, Parent, ParentDC);
116 return true;
117 }
118
119 bool VisitTagTypeLoc(TagTypeLoc TL) {
120 TagDecl *D = TL.getDecl();
121 if (!IndexCtx.shouldIndexFunctionLocalSymbols() &&
123 return true;
124
126 IndexCtx.indexTagDecl(D);
127 return true;
128 }
129
130 return IndexCtx.handleReference(D, TL.getNameLoc(),
132 Relations);
133 }
134
135 bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
138 }
139
140 bool VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
141 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) {
144 }
145 return true;
146 }
147
148 void HandleTemplateSpecializationTypeLoc(TemplateName TemplName,
149 SourceLocation TemplNameLoc,
150 CXXRecordDecl *ResolvedClass,
151 bool IsTypeAlias) {
152
153
154 if (!IsTypeAlias && ResolvedClass &&
156 IndexCtx.shouldIndexImplicitInstantiation())) {
157 IndexCtx.handleReference(ResolvedClass, TemplNameLoc, Parent, ParentDC,
159 } else if (const TemplateDecl *D = TemplName.getAsTemplateDecl()) {
160 IndexCtx.handleReference(D, TemplNameLoc, Parent, ParentDC,
162 }
163 }
164
165 bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
167 if ()
168 return true;
169 HandleTemplateSpecializationTypeLoc(
171 T->isTypeAlias());
172 return true;
173 }
174
175 bool TraverseTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL,
176 bool TraverseQualifier) {
177 if (!WalkUpFromTemplateSpecializationTypeLoc(TL))
178 return false;
179 if (!TraverseTemplateName(TL.getTypePtr()->getTemplateName()))
180 return false;
181
182
183
184 SmallVector<SymbolRelation, 3> SavedRelations = Relations;
185 Relations.clear();
186 auto ResetSavedRelations =
187 llvm::make_scope_exit([&] { this->Relations = SavedRelations; });
188 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
189 if (!TraverseTemplateArgumentLoc(TL.getArgLoc(I)))
190 return false;
191 }
192
193 return true;
194 }
195
196 bool VisitDeducedTemplateSpecializationTypeLoc(DeducedTemplateSpecializationTypeLoc TL) {
198 if ()
199 return true;
200 HandleTemplateSpecializationTypeLoc(
202 false);
203 return true;
204 }
205
206 bool VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
207 std::vector<const NamedDecl *> Symbols =
208 IndexCtx.getResolver()->resolveDependentNameType(TL.getTypePtr());
209 if (Symbols.size() != 1)
210 return true;
211 return IndexCtx.handleReference(Symbols[0], TL.getNameLoc(), Parent,
213 }
214
215 bool TraverseStmt(Stmt *S) {
216 IndexCtx.indexBody(S, Parent, ParentDC);
217 return true;
218 }
219};
220
221}
222
226 bool isBase,
227 bool isIBType) {
229 return;
230
232}
233
237 bool isBase,
238 bool isIBType) {
240 return;
241
242 if (!DC)
244 TypeIndexer(*this, Parent, DC, isBase, isIBType).TraverseTypeLoc(TL);
245}
246
250 if (!DC)
253 Qualifier.getKind()) {
257 break;
258
264 break;
265 }
266
269 break;
270 }
271}
272
276 return;
278 return;
279
283 if (auto CXXRD = dyn_cast(D)) {
284 for (const auto &I : CXXRD->bases()) {
285 indexTypeSourceInfo(I.getTypeSourceInfo(), CXXRD, CXXRD, true);
286 }
287 }
289 }
290 }
291}
This file provides AST data structures related to concepts.
bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc QualifierLoc)
#define TRY_TO(CALL_EXPR)
Definition IndexTypeSourceInfo.cpp:47
Defines the clang::TypeLoc interface and its subclasses.
SourceLocation getConceptNameLoc() const
TemplateDecl * getNamedConcept() const
const TypeClass * getTypePtr() const
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
const DeclContext * getParentFunctionOrMethod(bool LexicalParent=false) const
If this decl is defined inside a function/method/block it returns the corresponding DeclContext,...
bool isImplicit() const
isImplicit - Indicates whether the declaration was implicitly generated by the implementation.
DeclContext * getDeclContext()
DeclContext * getLexicalDeclContext()
getLexicalDeclContext - The declaration context where this Decl was lexically declared (LexicalDC).
NestedNameSpecifierLoc getQualifierLoc() const
Retrieve the nested-name-specifier (with source-location information) that qualifies the name of this...
TypeSourceInfo * getTypeSourceInfo() const
SourceLocation getTemplateNameLoc() const
SourceLocation getNameLoc() const
SourceLocation getNameLoc() const
This represents a decl that may have a name.
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::".
@ MicrosoftSuper
Microsoft's '__super' specifier, stored as a CXXRecordDecl* of the class it appeared in.
@ Global
The global specifier '::'. There is no stored value.
@ Type
A type, stored as a Type*.
@ Namespace
A namespace-like entity, stored as a NamespaceBaseDecl*.
ObjCInterfaceDecl * getIFaceDecl() const
SourceLocation getNameLoc() const
ObjCProtocolDecl * getProtocol(unsigned i) const
unsigned getNumProtocols() const
SourceLocation getProtocolLoc(unsigned i) const
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.
bool isThisDeclarationADefinition() const
Return true if this declaration is a completion definition of the type.
NestedNameSpecifierLoc getQualifierLoc() const
Retrieve the nested-name-specifier (with source-location information) that qualifies the name of this...
TagDecl * getDecl() const
SourceLocation getNameLoc() const
bool isDefinition() const
True if the tag was defined in this type specifier.
TemplateDecl * getAsTemplateDecl(bool IgnoreDeduced=false) const
Retrieve the underlying template declaration that this template name refers to, if known.
unsigned getNumArgs() const
TemplateArgumentLoc getArgLoc(unsigned i) const
SourceLocation getTemplateNameLoc() const
Declaration of a template type parameter.
Wrapper for template type parameters.
TemplateTypeParmDecl * getDecl() const
Base wrapper for a particular "section" of type source info.
QualType getType() const
Get the type for which this source info wrapper provides information.
A container of type source information.
TypeLoc getTypeLoc() const
Return the TypeLoc wrapper for the type source info.
SourceLocation getNameLoc() const
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
TagDecl * castAsTagDecl() const
QualType getUnderlyingType() const
bool isTransparentTag() const
Determines if this typedef shares a name and spelling location with its underlying tag type,...
bool handleDecl(const Decl *D, SymbolRoleSet Roles=SymbolRoleSet(), ArrayRef< SymbolRelation > Relations={})
bool shouldIndex(const Decl *D)
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={})
Definition IndexTypeSourceInfo.cpp:273
bool shouldIndexFunctionLocalSymbols() const
void indexNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS, const NamedDecl *Parent, const DeclContext *DC=nullptr)
Definition IndexTypeSourceInfo.cpp:247
void indexTypeLoc(TypeLoc TL, const NamedDecl *Parent, const DeclContext *DC=nullptr, bool isBase=false, bool isIBType=false)
Definition IndexTypeSourceInfo.cpp:234
void indexTypeSourceInfo(TypeSourceInfo *TInfo, const NamedDecl *Parent, const DeclContext *DC=nullptr, bool isBase=false, bool isIBType=false)
Definition IndexTypeSourceInfo.cpp:223
bool isFunctionLocalSymbol(const Decl *D)
The JSON file list parser is used to communicate input to InstallAPI.
@ TemplateName
The identifier is a template name. FIXME: Add an annotation for that.
const FunctionProtoType * T