blob: 87bedd778aefa66e7b0c83ba1d5c444aa242db2a [file] [log] [blame]
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +00001//===- IndexingContext.cpp - Indexing context data ------------------------===//
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"
11#include "clang/Index/IndexDataConsumer.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/AST/DeclTemplate.h"
14#include "clang/AST/DeclObjC.h"
15#include "clang/Basic/SourceManager.h"
16
17using namespace clang;
18using namespace index;
19
20bool IndexingContext::shouldIndexFunctionLocalSymbols() const {
21 return IndexOpts.IndexFunctionLocals;
22}
23
24bool IndexingContext::handleDecl(const Decl *D,
25 SymbolRoleSet Roles,
26 ArrayRef<SymbolRelation> Relations) {
27 return handleDeclOccurrence(D, D->getLocation(), /*IsRef=*/false,
28 cast<Decl>(D->getDeclContext()), Roles, Relations,
29 nullptr, nullptr, D->getDeclContext());
30}
31
32bool IndexingContext::handleDecl(const Decl *D, SourceLocation Loc,
33 SymbolRoleSet Roles,
34 ArrayRef<SymbolRelation> Relations,
35 const DeclContext *DC) {
36 if (!DC)
37 DC = D->getDeclContext();
38 return handleDeclOccurrence(D, Loc, /*IsRef=*/false, cast<Decl>(DC),
39 Roles, Relations,
40 nullptr, nullptr, DC);
41}
42
43bool IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc,
44 const NamedDecl *Parent,
45 const DeclContext *DC,
46 SymbolRoleSet Roles,
47 ArrayRef<SymbolRelation> Relations,
48 const Expr *RefE,
49 const Decl *RefD) {
50 if (!shouldIndexFunctionLocalSymbols() && isFunctionLocalDecl(D))
51 return true;
52
53 if (isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D))
54 return true;
55
56 return handleDeclOccurrence(D, Loc, /*IsRef=*/true, Parent, Roles, Relations,
57 RefE, RefD, DC);
58}
59
60bool IndexingContext::importedModule(const ImportDecl *ImportD) {
Argyrios Kyrtzidis113387e2016-02-29 07:56:07 +000061 SourceLocation Loc;
62 auto IdLocs = ImportD->getIdentifierLocs();
63 if (!IdLocs.empty())
64 Loc = IdLocs.front();
65 else
66 Loc = ImportD->getLocation();
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +000067 SourceManager &SM = Ctx->getSourceManager();
68 Loc = SM.getFileLoc(Loc);
69 if (Loc.isInvalid())
70 return true;
71
72 FileID FID;
73 unsigned Offset;
74 std::tie(FID, Offset) = SM.getDecomposedLoc(Loc);
75 if (FID.isInvalid())
76 return true;
77
78 bool Invalid = false;
79 const SrcMgr::SLocEntry &SEntry = SM.getSLocEntry(FID, &Invalid);
80 if (Invalid || !SEntry.isFile())
81 return true;
82
83 if (SEntry.getFile().getFileCharacteristic() != SrcMgr::C_User) {
84 switch (IndexOpts.SystemSymbolFilter) {
85 case IndexingOptions::SystemSymbolFilterKind::None:
86 case IndexingOptions::SystemSymbolFilterKind::DeclarationsOnly:
87 return true;
88 case IndexingOptions::SystemSymbolFilterKind::All:
89 break;
90 }
91 }
92
Argyrios Kyrtzidis113387e2016-02-29 07:56:07 +000093 SymbolRoleSet Roles = (unsigned)SymbolRole::Reference;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +000094 if (ImportD->isImplicit())
95 Roles |= (unsigned)SymbolRole::Implicit;
96
97 return DataConsumer.handleModuleOccurence(ImportD, Roles, FID, Offset);
98}
99
100bool IndexingContext::isFunctionLocalDecl(const Decl *D) {
101 assert(D);
102
103 if (isa<TemplateTemplateParmDecl>(D))
104 return true;
105
Argyrios Kyrtzidis8757fc52016-03-04 04:24:32 +0000106 if (isa<ObjCTypeParamDecl>(D))
107 return true;
108
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000109 if (!D->getParentFunctionOrMethod())
110 return false;
111
112 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
113 switch (ND->getFormalLinkage()) {
114 case NoLinkage:
115 case VisibleNoLinkage:
116 case InternalLinkage:
117 return true;
118 case UniqueExternalLinkage:
119 llvm_unreachable("Not a sema linkage");
120 case ExternalLinkage:
121 return false;
122 }
123 }
124
125 return true;
126}
127
128bool IndexingContext::isTemplateImplicitInstantiation(const Decl *D) {
129 TemplateSpecializationKind TKind = TSK_Undeclared;
130 if (const ClassTemplateSpecializationDecl *
131 SD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
132 TKind = SD->getSpecializationKind();
133 }
134 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
135 TKind = FD->getTemplateSpecializationKind();
136 }
137 switch (TKind) {
138 case TSK_Undeclared:
139 case TSK_ExplicitSpecialization:
140 return false;
141 case TSK_ImplicitInstantiation:
142 case TSK_ExplicitInstantiationDeclaration:
143 case TSK_ExplicitInstantiationDefinition:
144 return true;
145 }
Saleem Abdulrasool9b0ac332016-02-15 00:36:52 +0000146 llvm_unreachable("invalid TemplateSpecializationKind");
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000147}
148
149bool IndexingContext::shouldIgnoreIfImplicit(const Decl *D) {
150 if (isa<ObjCInterfaceDecl>(D))
151 return false;
152 if (isa<ObjCCategoryDecl>(D))
153 return false;
154 if (isa<ObjCIvarDecl>(D))
155 return false;
156 if (isa<ObjCMethodDecl>(D))
157 return false;
158 if (isa<ImportDecl>(D))
159 return false;
160 return true;
161}
162
163static const Decl *adjustTemplateImplicitInstantiation(const Decl *D) {
164 if (const ClassTemplateSpecializationDecl *
165 SD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
166 return SD->getTemplateInstantiationPattern();
167 }
168 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
169 return FD->getTemplateInstantiationPattern();
170 }
171 return nullptr;
172}
173
174static bool isDeclADefinition(const Decl *D, ASTContext &Ctx) {
175 if (auto VD = dyn_cast<VarDecl>(D))
176 return VD->isThisDeclarationADefinition(Ctx);
177
178 if (auto FD = dyn_cast<FunctionDecl>(D))
179 return FD->isThisDeclarationADefinition();
180
181 if (auto TD = dyn_cast<TagDecl>(D))
182 return TD->isThisDeclarationADefinition();
183
184 if (auto MD = dyn_cast<ObjCMethodDecl>(D))
185 return MD->isThisDeclarationADefinition();
186
187 if (isa<TypedefNameDecl>(D) ||
188 isa<EnumConstantDecl>(D) ||
189 isa<FieldDecl>(D) ||
190 isa<MSPropertyDecl>(D) ||
191 isa<ObjCImplDecl>(D) ||
192 isa<ObjCPropertyImplDecl>(D))
193 return true;
194
195 return false;
196}
197
198static const Decl *adjustParent(const Decl *Parent) {
199 if (!Parent)
200 return nullptr;
201 for (;; Parent = cast<Decl>(Parent->getDeclContext())) {
202 if (isa<TranslationUnitDecl>(Parent))
203 return nullptr;
204 if (isa<LinkageSpecDecl>(Parent) || isa<BlockDecl>(Parent))
205 continue;
206 if (auto NS = dyn_cast<NamespaceDecl>(Parent)) {
207 if (NS->isAnonymousNamespace())
208 continue;
209 } else if (auto EnumD = dyn_cast<EnumDecl>(Parent)) {
210 // Move enumerators under anonymous enum to the enclosing parent.
211 if (EnumD->getDeclName().isEmpty())
212 continue;
213 } else if (auto RD = dyn_cast<RecordDecl>(Parent)) {
214 if (RD->isAnonymousStructOrUnion())
215 continue;
216 } else if (auto FD = dyn_cast<FieldDecl>(Parent)) {
217 if (FD->getDeclName().isEmpty())
218 continue;
219 }
220 return Parent;
221 }
222}
223
224static const Decl *getCanonicalDecl(const Decl *D) {
225 D = D->getCanonicalDecl();
226 if (auto TD = dyn_cast<TemplateDecl>(D)) {
227 D = TD->getTemplatedDecl();
228 assert(D->isCanonicalDecl());
229 }
230
231 return D;
232}
233
234bool IndexingContext::handleDeclOccurrence(const Decl *D, SourceLocation Loc,
235 bool IsRef, const Decl *Parent,
236 SymbolRoleSet Roles,
237 ArrayRef<SymbolRelation> Relations,
238 const Expr *OrigE,
239 const Decl *OrigD,
240 const DeclContext *ContainerDC) {
241 if (D->isImplicit() && !isa<ObjCMethodDecl>(D))
242 return true;
243 if (!isa<NamedDecl>(D) ||
244 (cast<NamedDecl>(D)->getDeclName().isEmpty() &&
245 !isa<TagDecl>(D) && !isa<ObjCCategoryDecl>(D)))
246 return true;
247
248 SourceManager &SM = Ctx->getSourceManager();
249 Loc = SM.getFileLoc(Loc);
250 if (Loc.isInvalid())
251 return true;
252
253 FileID FID;
254 unsigned Offset;
255 std::tie(FID, Offset) = SM.getDecomposedLoc(Loc);
256 if (FID.isInvalid())
257 return true;
258
259 bool Invalid = false;
260 const SrcMgr::SLocEntry &SEntry = SM.getSLocEntry(FID, &Invalid);
261 if (Invalid || !SEntry.isFile())
262 return true;
263
264 if (SEntry.getFile().getFileCharacteristic() != SrcMgr::C_User) {
265 switch (IndexOpts.SystemSymbolFilter) {
266 case IndexingOptions::SystemSymbolFilterKind::None:
267 return true;
268 case IndexingOptions::SystemSymbolFilterKind::DeclarationsOnly:
269 if (IsRef)
270 return true;
271 break;
272 case IndexingOptions::SystemSymbolFilterKind::All:
273 break;
274 }
275 }
276
277 if (isTemplateImplicitInstantiation(D)) {
278 if (!IsRef)
279 return true;
280 D = adjustTemplateImplicitInstantiation(D);
281 if (!D)
282 return true;
283 assert(!isTemplateImplicitInstantiation(D));
284 }
285
286 if (!OrigD)
287 OrigD = D;
288
289 if (IsRef)
290 Roles |= (unsigned)SymbolRole::Reference;
291 else if (isDeclADefinition(D, *Ctx))
292 Roles |= (unsigned)SymbolRole::Definition;
293 else
294 Roles |= (unsigned)SymbolRole::Declaration;
295
296 D = getCanonicalDecl(D);
297 if (D->isImplicit() && !isa<ObjCMethodDecl>(D)) {
298 // operator new declarations will link to the implicit one as canonical.
299 return true;
300 }
301 Parent = adjustParent(Parent);
302 if (Parent)
303 Parent = getCanonicalDecl(Parent);
304 assert(!Parent || !Parent->isImplicit() ||
305 isa<ObjCInterfaceDecl>(Parent) || isa<ObjCMethodDecl>(Parent));
306
307 SmallVector<SymbolRelation, 6> FinalRelations;
308 FinalRelations.reserve(Relations.size()+1);
309
310 auto addRelation = [&](SymbolRelation Rel) {
311 auto It = std::find_if(FinalRelations.begin(), FinalRelations.end(),
312 [&](SymbolRelation Elem)->bool {
313 return Elem.RelatedSymbol == Rel.RelatedSymbol;
314 });
315 if (It != FinalRelations.end()) {
316 It->Roles |= Rel.Roles;
317 } else {
318 FinalRelations.push_back(Rel);
319 }
320 Roles |= Rel.Roles;
321 };
322
323 if (!IsRef && Parent && !cast<DeclContext>(Parent)->isFunctionOrMethod()) {
324 addRelation(SymbolRelation{(unsigned)SymbolRole::RelationChildOf, Parent});
325 }
326 for (auto &Rel : Relations) {
327 addRelation(SymbolRelation(Rel.Roles,
328 Rel.RelatedSymbol->getCanonicalDecl()));
329 }
330
331 IndexDataConsumer::ASTNodeInfo Node{ OrigE, OrigD, Parent, ContainerDC };
332 return DataConsumer.handleDeclOccurence(D, Roles, FinalRelations, FID, Offset,
333 Node);
334}