clang  9.0.0svn
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"
11 
12 using namespace clang;
13 using namespace index;
14 
15 namespace {
16 
17 class TypeIndexer : public RecursiveASTVisitor<TypeIndexer> {
18  IndexingContext &IndexCtx;
19  const NamedDecl *Parent;
20  const DeclContext *ParentDC;
21  bool IsBase;
23 
25 
26 public:
27  TypeIndexer(IndexingContext &indexCtx, const NamedDecl *parent,
28  const DeclContext *DC, bool isBase, bool isIBType)
29  : IndexCtx(indexCtx), Parent(parent), ParentDC(DC), IsBase(isBase) {
30  if (IsBase) {
31  assert(Parent);
32  Relations.emplace_back((unsigned)SymbolRole::RelationBaseOf, Parent);
33  }
34  if (isIBType) {
35  assert(Parent);
36  Relations.emplace_back((unsigned)SymbolRole::RelationIBTypeOf, Parent);
37  }
38  }
39 
40  bool shouldWalkTypesOfTypeLocs() const { return false; }
41 
42 #define TRY_TO(CALL_EXPR) \
43  do { \
44  if (!CALL_EXPR) \
45  return false; \
46  } while (0)
47 
48  bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TTPL) {
49  SourceLocation Loc = TTPL.getNameLoc();
50  TemplateTypeParmDecl *TTPD = TTPL.getDecl();
51  return IndexCtx.handleReference(TTPD, Loc, Parent, ParentDC,
52  SymbolRoleSet());
53  }
54 
55  bool VisitTypedefTypeLoc(TypedefTypeLoc TL) {
56  SourceLocation Loc = TL.getNameLoc();
58  if (ND->isTransparentTag()) {
59  TagDecl *Underlying = ND->getUnderlyingType()->getAsTagDecl();
60  return IndexCtx.handleReference(Underlying, Loc, Parent,
61  ParentDC, SymbolRoleSet(), Relations);
62  }
63  if (IsBase) {
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,
69  Relations));
70  }
71  } else {
72  TRY_TO(IndexCtx.handleReference(ND, Loc,
73  Parent, ParentDC, SymbolRoleSet(),
74  Relations));
75  }
76  return true;
77  }
78 
79  bool traverseParamVarHelper(ParmVarDecl *D) {
80  TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
81  if (D->getTypeSourceInfo())
82  TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
83  return true;
84  }
85 
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);
96  }
97  }
98 
99  return base::TraverseParmVarDecl(D);
100  }
101 
102  bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
103  IndexCtx.indexNestedNameSpecifierLoc(NNS, Parent, ParentDC);
104  return true;
105  }
106 
107  bool VisitTagTypeLoc(TagTypeLoc TL) {
108  TagDecl *D = TL.getDecl();
109  if (!IndexCtx.shouldIndexFunctionLocalSymbols() &&
111  return true;
112 
113  if (TL.isDefinition()) {
114  IndexCtx.indexTagDecl(D);
115  return true;
116  }
117 
118  return IndexCtx.handleReference(D, TL.getNameLoc(),
119  Parent, ParentDC, SymbolRoleSet(),
120  Relations);
121  }
122 
123  bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
124  return IndexCtx.handleReference(TL.getIFaceDecl(), TL.getNameLoc(),
125  Parent, ParentDC, SymbolRoleSet(), Relations);
126  }
127 
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);
132  }
133  return true;
134  }
135 
136  template<typename TypeLocType>
137  bool HandleTemplateSpecializationTypeLoc(TypeLocType TL) {
138  if (const auto *T = TL.getTypePtr()) {
139  if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) {
140  if (!RD->isImplicit() || IndexCtx.shouldIndexImplicitInstantiation()) {
141  IndexCtx.handleReference(RD, TL.getTemplateNameLoc(), Parent,
142  ParentDC, SymbolRoleSet(), Relations);
143  return true;
144  }
145  }
146  if (const TemplateDecl *D = T->getTemplateName().getAsTemplateDecl())
147  IndexCtx.handleReference(D, TL.getTemplateNameLoc(), Parent, ParentDC,
148  SymbolRoleSet(), Relations);
149  }
150  return true;
151  }
152 
153  bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
154  return HandleTemplateSpecializationTypeLoc(TL);
155  }
156 
157  bool VisitDeducedTemplateSpecializationTypeLoc(DeducedTemplateSpecializationTypeLoc TL) {
158  return HandleTemplateSpecializationTypeLoc(TL);
159  }
160 
161  bool VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
162  const DependentNameType *DNT = TL.getTypePtr();
163  const NestedNameSpecifier *NNS = DNT->getQualifier();
164  const Type *T = NNS->getAsType();
165  if (!T)
166  return true;
167  const TemplateSpecializationType *TST =
169  if (!TST)
170  return true;
171  TemplateName TN = TST->getTemplateName();
172  const ClassTemplateDecl *TD =
173  dyn_cast_or_null<ClassTemplateDecl>(TN.getAsTemplateDecl());
174  if (!TD)
175  return true;
176  CXXRecordDecl *RD = TD->getTemplatedDecl();
177  if (!RD->hasDefinition())
178  return true;
179  RD = RD->getDefinition();
180  DeclarationName Name(DNT->getIdentifier());
181  std::vector<const NamedDecl *> Symbols = RD->lookupDependentName(
182  Name, [](const NamedDecl *ND) { return isa<TypeDecl>(ND); });
183  if (Symbols.size() != 1)
184  return true;
185  return IndexCtx.handleReference(Symbols[0], TL.getNameLoc(), Parent,
186  ParentDC, SymbolRoleSet(), Relations);
187  }
188 
189  bool TraverseStmt(Stmt *S) {
190  IndexCtx.indexBody(S, Parent, ParentDC);
191  return true;
192  }
193 };
194 
195 } // anonymous namespace
196 
198  const NamedDecl *Parent,
199  const DeclContext *DC,
200  bool isBase,
201  bool isIBType) {
202  if (!TInfo || TInfo->getTypeLoc().isNull())
203  return;
204 
205  indexTypeLoc(TInfo->getTypeLoc(), Parent, DC, isBase, isIBType);
206 }
207 
209  const NamedDecl *Parent,
210  const DeclContext *DC,
211  bool isBase,
212  bool isIBType) {
213  if (TL.isNull())
214  return;
215 
216  if (!DC)
217  DC = Parent->getLexicalDeclContext();
218  TypeIndexer(*this, Parent, DC, isBase, isIBType).TraverseTypeLoc(TL);
219 }
220 
222  const NamedDecl *Parent,
223  const DeclContext *DC) {
224  if (!NNS)
225  return;
226 
227  if (NestedNameSpecifierLoc Prefix = NNS.getPrefix())
228  indexNestedNameSpecifierLoc(Prefix, Parent, DC);
229 
230  if (!DC)
231  DC = Parent->getLexicalDeclContext();
232  SourceLocation Loc = NNS.getLocalBeginLoc();
233 
234  switch (NNS.getNestedNameSpecifier()->getKind()) {
238  break;
239 
241  handleReference(NNS.getNestedNameSpecifier()->getAsNamespace(),
242  Loc, Parent, DC, SymbolRoleSet());
243  break;
245  handleReference(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(),
246  Loc, Parent, DC, SymbolRoleSet());
247  break;
248 
251  indexTypeLoc(NNS.getTypeLoc(), Parent, DC);
252  break;
253  }
254 }
255 
257  ArrayRef<SymbolRelation> Relations) {
258  if (!shouldIndex(D))
259  return;
260  if (!shouldIndexFunctionLocalSymbols() && isFunctionLocalSymbol(D))
261  return;
262 
263  if (handleDecl(D, /*Roles=*/SymbolRoleSet(), Relations)) {
264  if (D->isThisDeclarationADefinition()) {
265  indexNestedNameSpecifierLoc(D->getQualifierLoc(), D);
266  if (auto CXXRD = dyn_cast<CXXRecordDecl>(D)) {
267  for (const auto &I : CXXRD->bases()) {
268  indexTypeSourceInfo(I.getTypeSourceInfo(), CXXRD, CXXRD, /*isBase=*/true);
269  }
270  }
271  indexDeclContext(D);
272  }
273  }
274 }
ObjCInterfaceDecl * getIFaceDecl() const
Definition: TypeLoc.h:1056
TemplateTypeParmDecl * getDecl() const
Definition: TypeLoc.h:739
Wrapper for source info for tag types.
Definition: TypeLoc.h:703
const TypeClass * getTypePtr() const
Definition: TypeLoc.h:405
Stmt - This represents one statement.
Definition: Stmt.h:65
Microsoft&#39;s &#39;__super&#39; specifier, stored as a CXXRecordDecl* of the class it appeared in...
Represents a qualified type name for which the type name is dependent.
Definition: Type.h:5261
NestedNameSpecifier * getQualifier() const
Retrieve the qualification on this type.
Definition: Type.h:5280
void indexTypeLoc(TypeLoc TL, const NamedDecl *Parent, const DeclContext *DC=nullptr, bool isBase=false, bool isIBType=false)
The base class of the type hierarchy.
Definition: Type.h:1414
Wrapper for source info for typedefs.
Definition: TypeLoc.h:666
A container of type source information.
Definition: Decl.h:86
const DeclContext * getParentFunctionOrMethod() const
If this decl is defined inside a function/method/block it returns the corresponding DeclContext...
Definition: DeclBase.cpp:253
SourceLocation getLocalBeginLoc() const
Retrieve the location of the beginning of this component of the nested-name-specifier.
An identifier, stored as an IdentifierInfo*.
TRY_TO(TraverseType(T->getPointeeType()))
const T * getAs() const
Member-template getAs<specific type>&#39;.
Definition: Type.h:6805
A namespace, stored as a NamespaceDecl*.
SpecifierKind getKind() const
Determine what kind of nested name specifier is stored.
bool hasDefinition() const
Definition: DeclCXX.h:775
Represents a parameter to a function.
Definition: Decl.h:1559
std::vector< const NamedDecl * > lookupDependentName(const DeclarationName &Name, llvm::function_ref< bool(const NamedDecl *ND)> Filter)
Performs an imprecise lookup of a dependent name in this class.
Base wrapper for a particular "section" of type source info.
Definition: TypeLoc.h:56
A C++ nested-name-specifier augmented with source location information.
TemplateDecl * getAsTemplateDecl() const
Retrieve the underlying template declaration that this template name refers to, if known...
TemplateName getTemplateName() const
Retrieve the name of the template that we are specializing.
Definition: Type.h:4953
bool isFunctionLocalSymbol(const Decl *D)
Definition: IndexSymbol.cpp:52
const Type * getAsType() const
Retrieve the type stored in this nested name specifier.
SourceLocation getProtocolLoc(unsigned i) const
Definition: TypeLoc.h:985
NamespaceAliasDecl * getAsNamespaceAlias() const
Retrieve the namespace alias stored in this nested name specifier.
NestedNameSpecifierLoc getQualifierLoc() const
Retrieve the nested-name-specifier (with source-location information) that qualifies the name of this...
Definition: Decl.h:752
bool shouldIndexFunctionLocalSymbols() const
bool isNull() const
Definition: TypeLoc.h:118
Wrapper for source info for ObjC interfaces.
Definition: TypeLoc.h:1051
DeclContext * getLexicalDeclContext()
getLexicalDeclContext - The declaration context where this Decl was lexically declared (LexicalDC)...
Definition: DeclBase.h:821
SourceLocation getNameLoc() const
Definition: TypeLoc.h:1060
bool isDefinition() const
True if the tag was defined in this type specifier.
Definition: TypeLoc.h:710
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
CXXRecordDecl * getTemplatedDecl() const
Get the underlying class declarations of the template.
NodeId Parent
Definition: ASTDiff.cpp:191
unsigned SymbolRoleSet
Definition: IndexSymbol.h:127
ObjCProtocolDecl * getProtocol(unsigned i) const
Definition: TypeLoc.h:995
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1636
SourceLocation getNameLoc() const
Definition: TypeLoc.h:2053
bool isTransparentTag() const
Determines if this typedef shares a name and spelling location with its underlying tag type...
Definition: Decl.h:3005
TagDecl * getDecl() const
Definition: TypeLoc.h:707
NamespaceDecl * getAsNamespace() const
Retrieve the namespace stored in this nested name specifier.
Declaration of a template type parameter.
DeclContext * getDeclContext()
Definition: DeclBase.h:431
CXXRecordDecl * getDefinition() const
Definition: DeclCXX.h:768
const IdentifierInfo * getIdentifier() const
Retrieve the type named by the typename specifier as an identifier.
Definition: Type.h:5287
NestedNameSpecifierLoc getPrefix() const
Return the prefix of this nested-name-specifier.
Represents a C++ template name within the type system.
Definition: TemplateName.h:187
A namespace alias, stored as a NamespaceAliasDecl*.
TagDecl * getAsTagDecl() const
Retrieves the TagDecl that this type refers to, either because the type is a TagType or because it is...
Definition: Type.cpp:1644
bool handleReference(const NamedDecl *D, SourceLocation Loc, const NamedDecl *Parent, const DeclContext *DC, SymbolRoleSet Roles=SymbolRoleSet(), ArrayRef< SymbolRelation > Relations=None, const Expr *RefE=nullptr, const Decl *RefD=nullptr)
Encodes a location in the source.
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:3069
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
bool shouldIndexImplicitInstantiation() const
NestedNameSpecifierLoc getQualifierLoc() const
Retrieve the nested-name-specifier (with source-location information) that qualifies the name of this...
Definition: Decl.h:3296
Base class for declarations which introduce a typedef-name.
Definition: Decl.h:2921
Dataflow Directional Tag Classes.
NestedNameSpecifier * getNestedNameSpecifier() const
Retrieve the nested-name-specifier to which this instance refers.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1264
The base class of all kinds of template declarations (e.g., class, function, etc.).
Definition: DeclTemplate.h:403
QualType getType() const
Get the type for which this source info wrapper provides information.
Definition: TypeLoc.h:130
QualType getUnderlyingType() const
Definition: Decl.h:2976
The name of a declaration.
A type that was preceded by the &#39;template&#39; keyword, stored as a Type*.
void indexTypeSourceInfo(TypeSourceInfo *TInfo, const NamedDecl *Parent, const DeclContext *DC=nullptr, bool isBase=false, bool isIBType=false)
TypedefNameDecl * getTypedefNameDecl() const
Definition: TypeLoc.h:670
void indexNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS, const NamedDecl *Parent, const DeclContext *DC=nullptr)
void indexTagDecl(const TagDecl *D, ArrayRef< SymbolRelation > Relations=None)
TypeSourceInfo * getTypeSourceInfo() const
Definition: Decl.h:715
TypeLoc getTypeLoc() const
Return the TypeLoc wrapper for the type source info.
Definition: TypeLoc.h:240
bool isThisDeclarationADefinition() const
Return true if this declaration is a completion definition of the type.
Definition: Decl.h:3169
Represents a C++ struct/union/class.
Definition: DeclCXX.h:299
unsigned getNumProtocols() const
Definition: TypeLoc.h:981
Declaration of a class template.
TypeLoc getTypeLoc() const
For a nested-name-specifier that refers to a type, retrieve the type with source-location information...
SourceLocation getNameLoc() const
Definition: TypeLoc.h:519
Represents a type template specialization; the template must be a class template, a type alias templa...
Definition: Type.h:4890
void indexBody(const Stmt *S, const NamedDecl *Parent, const DeclContext *DC=nullptr)
Definition: IndexBody.cpp:471
Wrapper for template type parameters.
Definition: TypeLoc.h:734
This represents a decl that may have a name.
Definition: Decl.h:248
The global specifier &#39;::&#39;. There is no stored value.