clang  8.0.0svn
IndexTypeSourceInfo.cpp
Go to the documentation of this file.
1 //===- IndexTypeSourceInfo.cpp - Indexing types ---------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "IndexingContext.h"
12 
13 using namespace clang;
14 using namespace index;
15 
16 namespace {
17 
18 class TypeIndexer : public RecursiveASTVisitor<TypeIndexer> {
19  IndexingContext &IndexCtx;
20  const NamedDecl *Parent;
21  const DeclContext *ParentDC;
22  bool IsBase;
24 
26 
27 public:
28  TypeIndexer(IndexingContext &indexCtx, const NamedDecl *parent,
29  const DeclContext *DC, bool isBase, bool isIBType)
30  : IndexCtx(indexCtx), Parent(parent), ParentDC(DC), IsBase(isBase) {
31  if (IsBase) {
32  assert(Parent);
33  Relations.emplace_back((unsigned)SymbolRole::RelationBaseOf, Parent);
34  }
35  if (isIBType) {
36  assert(Parent);
37  Relations.emplace_back((unsigned)SymbolRole::RelationIBTypeOf, Parent);
38  }
39  }
40 
41  bool shouldWalkTypesOfTypeLocs() const { return false; }
42 
43 #define TRY_TO(CALL_EXPR) \
44  do { \
45  if (!CALL_EXPR) \
46  return false; \
47  } while (0)
48 
49  bool VisitTypedefTypeLoc(TypedefTypeLoc TL) {
50  SourceLocation Loc = TL.getNameLoc();
52  if (ND->isTransparentTag()) {
53  TagDecl *Underlying = ND->getUnderlyingType()->getAsTagDecl();
54  return IndexCtx.handleReference(Underlying, Loc, Parent,
55  ParentDC, SymbolRoleSet(), Relations);
56  }
57  if (IsBase) {
58  TRY_TO(IndexCtx.handleReference(ND, Loc,
59  Parent, ParentDC, SymbolRoleSet()));
60  if (auto *CD = TL.getType()->getAsCXXRecordDecl()) {
61  TRY_TO(IndexCtx.handleReference(CD, Loc, Parent, ParentDC,
62  (unsigned)SymbolRole::Implicit,
63  Relations));
64  }
65  } else {
66  TRY_TO(IndexCtx.handleReference(ND, Loc,
67  Parent, ParentDC, SymbolRoleSet(),
68  Relations));
69  }
70  return true;
71  }
72 
73  bool traverseParamVarHelper(ParmVarDecl *D) {
74  TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
75  if (D->getTypeSourceInfo())
76  TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
77  return true;
78  }
79 
80  bool TraverseParmVarDecl(ParmVarDecl *D) {
81  // Avoid visiting default arguments from the definition that were already
82  // visited in the declaration.
83  // FIXME: A free function definition can have default arguments.
84  // Avoiding double visitaiton of default arguments should be handled by the
85  // visitor probably with a bit in the AST to indicate if the attached
86  // default argument was 'inherited' or written in source.
87  if (auto FD = dyn_cast<FunctionDecl>(D->getDeclContext())) {
88  if (FD->isThisDeclarationADefinition()) {
89  return traverseParamVarHelper(D);
90  }
91  }
92 
93  return base::TraverseParmVarDecl(D);
94  }
95 
96  bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
97  IndexCtx.indexNestedNameSpecifierLoc(NNS, Parent, ParentDC);
98  return true;
99  }
100 
101  bool VisitTagTypeLoc(TagTypeLoc TL) {
102  TagDecl *D = TL.getDecl();
103  if (!IndexCtx.shouldIndexFunctionLocalSymbols() &&
105  return true;
106 
107  if (TL.isDefinition()) {
108  IndexCtx.indexTagDecl(D);
109  return true;
110  }
111 
112  return IndexCtx.handleReference(D, TL.getNameLoc(),
113  Parent, ParentDC, SymbolRoleSet(),
114  Relations);
115  }
116 
117  bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
118  return IndexCtx.handleReference(TL.getIFaceDecl(), TL.getNameLoc(),
119  Parent, ParentDC, SymbolRoleSet(), Relations);
120  }
121 
122  bool VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
123  for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) {
124  IndexCtx.handleReference(TL.getProtocol(i), TL.getProtocolLoc(i),
125  Parent, ParentDC, SymbolRoleSet(), Relations);
126  }
127  return true;
128  }
129 
130  template<typename TypeLocType>
131  bool HandleTemplateSpecializationTypeLoc(TypeLocType TL) {
132  if (const auto *T = TL.getTypePtr()) {
133  if (IndexCtx.shouldIndexImplicitInstantiation()) {
134  if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) {
135  IndexCtx.handleReference(RD, TL.getTemplateNameLoc(),
136  Parent, ParentDC, SymbolRoleSet(), Relations);
137  return true;
138  }
139  }
140  if (const TemplateDecl *D = T->getTemplateName().getAsTemplateDecl())
141  IndexCtx.handleReference(D, TL.getTemplateNameLoc(), Parent, ParentDC,
142  SymbolRoleSet(), Relations);
143  }
144  return true;
145  }
146 
147  bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
148  return HandleTemplateSpecializationTypeLoc(TL);
149  }
150 
151  bool VisitDeducedTemplateSpecializationTypeLoc(DeducedTemplateSpecializationTypeLoc TL) {
152  return HandleTemplateSpecializationTypeLoc(TL);
153  }
154 
155  bool VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
156  const DependentNameType *DNT = TL.getTypePtr();
157  const NestedNameSpecifier *NNS = DNT->getQualifier();
158  const Type *T = NNS->getAsType();
159  if (!T)
160  return true;
161  const TemplateSpecializationType *TST =
163  if (!TST)
164  return true;
165  TemplateName TN = TST->getTemplateName();
166  const ClassTemplateDecl *TD =
167  dyn_cast_or_null<ClassTemplateDecl>(TN.getAsTemplateDecl());
168  if (!TD)
169  return true;
170  CXXRecordDecl *RD = TD->getTemplatedDecl();
171  if (!RD->hasDefinition())
172  return true;
173  RD = RD->getDefinition();
174  DeclarationName Name(DNT->getIdentifier());
175  std::vector<const NamedDecl *> Symbols = RD->lookupDependentName(
176  Name, [](const NamedDecl *ND) { return isa<TypeDecl>(ND); });
177  if (Symbols.size() != 1)
178  return true;
179  return IndexCtx.handleReference(Symbols[0], TL.getNameLoc(), Parent,
180  ParentDC, SymbolRoleSet(), Relations);
181  }
182 
183  bool TraverseStmt(Stmt *S) {
184  IndexCtx.indexBody(S, Parent, ParentDC);
185  return true;
186  }
187 };
188 
189 } // anonymous namespace
190 
192  const NamedDecl *Parent,
193  const DeclContext *DC,
194  bool isBase,
195  bool isIBType) {
196  if (!TInfo || TInfo->getTypeLoc().isNull())
197  return;
198 
199  indexTypeLoc(TInfo->getTypeLoc(), Parent, DC, isBase, isIBType);
200 }
201 
203  const NamedDecl *Parent,
204  const DeclContext *DC,
205  bool isBase,
206  bool isIBType) {
207  if (TL.isNull())
208  return;
209 
210  if (!DC)
211  DC = Parent->getLexicalDeclContext();
212  TypeIndexer(*this, Parent, DC, isBase, isIBType).TraverseTypeLoc(TL);
213 }
214 
216  const NamedDecl *Parent,
217  const DeclContext *DC) {
218  if (!NNS)
219  return;
220 
221  if (NestedNameSpecifierLoc Prefix = NNS.getPrefix())
222  indexNestedNameSpecifierLoc(Prefix, Parent, DC);
223 
224  if (!DC)
225  DC = Parent->getLexicalDeclContext();
226  SourceLocation Loc = NNS.getLocalBeginLoc();
227 
228  switch (NNS.getNestedNameSpecifier()->getKind()) {
232  break;
233 
235  handleReference(NNS.getNestedNameSpecifier()->getAsNamespace(),
236  Loc, Parent, DC, SymbolRoleSet());
237  break;
239  handleReference(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(),
240  Loc, Parent, DC, SymbolRoleSet());
241  break;
242 
245  indexTypeLoc(NNS.getTypeLoc(), Parent, DC);
246  break;
247  }
248 }
249 
251  ArrayRef<SymbolRelation> Relations) {
252  if (!shouldIndex(D))
253  return;
254  if (!shouldIndexFunctionLocalSymbols() && isFunctionLocalSymbol(D))
255  return;
256 
257  if (handleDecl(D, /*Roles=*/SymbolRoleSet(), Relations)) {
258  if (D->isThisDeclarationADefinition()) {
259  indexNestedNameSpecifierLoc(D->getQualifierLoc(), D);
260  if (auto CXXRD = dyn_cast<CXXRecordDecl>(D)) {
261  for (const auto &I : CXXRD->bases()) {
262  indexTypeSourceInfo(I.getTypeSourceInfo(), CXXRD, CXXRD, /*isBase=*/true);
263  }
264  }
265  indexDeclContext(D);
266  }
267  }
268 }
ObjCInterfaceDecl * getIFaceDecl() const
Definition: TypeLoc.h:1054
Wrapper for source info for tag types.
Definition: TypeLoc.h:701
const TypeClass * getTypePtr() const
Definition: TypeLoc.h:403
Stmt - This represents one statement.
Definition: Stmt.h:66
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:5216
NestedNameSpecifier * getQualifier() const
Retrieve the qualification on this type.
Definition: Type.h:5235
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:1411
Wrapper for source info for typedefs.
Definition: TypeLoc.h:664
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:254
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:6734
A namespace, stored as a NamespaceDecl*.
SpecifierKind getKind() const
Determine what kind of nested name specifier is stored.
bool hasDefinition() const
Definition: DeclCXX.h:776
Represents a parameter to a function.
Definition: Decl.h:1551
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:57
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:4908
bool isFunctionLocalSymbol(const Decl *D)
Definition: IndexSymbol.cpp:53
const Type * getAsType() const
Retrieve the type stored in this nested name specifier.
SourceLocation getProtocolLoc(unsigned i) const
Definition: TypeLoc.h:983
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:119
Wrapper for source info for ObjC interfaces.
Definition: TypeLoc.h:1049
DeclContext * getLexicalDeclContext()
getLexicalDeclContext - The declaration context where this Decl was lexically declared (LexicalDC)...
Definition: DeclBase.h:821
SourceLocation getNameLoc() const
Definition: TypeLoc.h:1058
bool isDefinition() const
True if the tag was defined in this type specifier.
Definition: TypeLoc.h:708
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:192
unsigned SymbolRoleSet
Definition: IndexSymbol.h:124
ObjCProtocolDecl * getProtocol(unsigned i) const
Definition: TypeLoc.h:993
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1613
SourceLocation getNameLoc() const
Definition: TypeLoc.h:2018
bool isTransparentTag() const
Determines if this typedef shares a name and spelling location with its underlying tag type...
Definition: Decl.h:3009
TagDecl * getDecl() const
Definition: TypeLoc.h:705
NamespaceDecl * getAsNamespace() const
Retrieve the namespace stored in this nested name specifier.
DeclContext * getDeclContext()
Definition: DeclBase.h:427
CXXRecordDecl * getDefinition() const
Definition: DeclCXX.h:769
const IdentifierInfo * getIdentifier() const
Retrieve the type named by the typename specifier as an identifier.
Definition: Type.h:5242
NestedNameSpecifierLoc getPrefix() const
Return the prefix of this nested-name-specifier.
Represents a C++ template name within the type system.
Definition: TemplateName.h:178
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:1621
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:3073
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:3300
Base class for declarations which introduce a typedef-name.
Definition: Decl.h:2925
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:1262
The base class of all kinds of template declarations (e.g., class, function, etc.).
Definition: DeclTemplate.h:399
QualType getType() const
Get the type for which this source info wrapper provides information.
Definition: TypeLoc.h:131
QualType getUnderlyingType() const
Definition: Decl.h:2980
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:668
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:238
bool isThisDeclarationADefinition() const
Return true if this declaration is a completion definition of the type.
Definition: Decl.h:3173
Represents a C++ struct/union/class.
Definition: DeclCXX.h:300
unsigned getNumProtocols() const
Definition: TypeLoc.h:979
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:517
Represents a type template specialization; the template must be a class template, a type alias templa...
Definition: Type.h:4845
void indexBody(const Stmt *S, const NamedDecl *Parent, const DeclContext *DC=nullptr)
Definition: IndexBody.cpp:461
This represents a decl that may have a name.
Definition: Decl.h:248
The global specifier &#39;::&#39;. There is no stored value.