1 //===- IndexingContext.cpp - Indexing context data ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
17 using namespace clang;
18 using namespace index;
20 bool IndexingContext::shouldIndexFunctionLocalSymbols() const {
21 return IndexOpts.IndexFunctionLocals;
24 bool IndexingContext::handleDecl(const Decl *D,
26 ArrayRef<SymbolRelation> Relations) {
27 return handleDeclOccurrence(D, D->getLocation(), /*IsRef=*/false,
28 cast<Decl>(D->getDeclContext()), Roles, Relations,
29 nullptr, nullptr, D->getDeclContext());
32 bool IndexingContext::handleDecl(const Decl *D, SourceLocation Loc,
34 ArrayRef<SymbolRelation> Relations,
35 const DeclContext *DC) {
37 DC = D->getDeclContext();
38 return handleDeclOccurrence(D, Loc, /*IsRef=*/false, cast<Decl>(DC),
40 nullptr, nullptr, DC);
43 bool IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc,
44 const NamedDecl *Parent,
45 const DeclContext *DC,
47 ArrayRef<SymbolRelation> Relations,
50 if (!shouldIndexFunctionLocalSymbols() && isFunctionLocalDecl(D))
53 if (isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D))
56 return handleDeclOccurrence(D, Loc, /*IsRef=*/true, Parent, Roles, Relations,
60 bool IndexingContext::importedModule(const ImportDecl *ImportD) {
62 auto IdLocs = ImportD->getIdentifierLocs();
66 Loc = ImportD->getLocation();
67 SourceManager &SM = Ctx->getSourceManager();
68 Loc = SM.getFileLoc(Loc);
74 std::tie(FID, Offset) = SM.getDecomposedLoc(Loc);
79 const SrcMgr::SLocEntry &SEntry = SM.getSLocEntry(FID, &Invalid);
80 if (Invalid || !SEntry.isFile())
83 if (SEntry.getFile().getFileCharacteristic() != SrcMgr::C_User) {
84 switch (IndexOpts.SystemSymbolFilter) {
85 case IndexingOptions::SystemSymbolFilterKind::None:
87 case IndexingOptions::SystemSymbolFilterKind::DeclarationsOnly:
88 case IndexingOptions::SystemSymbolFilterKind::All:
93 SymbolRoleSet Roles = (unsigned)SymbolRole::Declaration;
94 if (ImportD->isImplicit())
95 Roles |= (unsigned)SymbolRole::Implicit;
97 return DataConsumer.handleModuleOccurence(ImportD, Roles, FID, Offset);
100 bool IndexingContext::isFunctionLocalDecl(const Decl *D) {
103 if (isa<TemplateTemplateParmDecl>(D))
106 if (isa<ObjCTypeParamDecl>(D))
109 if (!D->getParentFunctionOrMethod())
112 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
113 switch (ND->getFormalLinkage()) {
115 case VisibleNoLinkage:
116 case InternalLinkage:
118 case UniqueExternalLinkage:
119 llvm_unreachable("Not a sema linkage");
120 case ExternalLinkage:
128 bool IndexingContext::isTemplateImplicitInstantiation(const Decl *D) {
129 TemplateSpecializationKind TKind = TSK_Undeclared;
130 if (const ClassTemplateSpecializationDecl *
131 SD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
132 TKind = SD->getSpecializationKind();
134 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
135 TKind = FD->getTemplateSpecializationKind();
139 case TSK_ExplicitSpecialization:
141 case TSK_ImplicitInstantiation:
142 case TSK_ExplicitInstantiationDeclaration:
143 case TSK_ExplicitInstantiationDefinition:
146 llvm_unreachable("invalid TemplateSpecializationKind");
149 bool IndexingContext::shouldIgnoreIfImplicit(const Decl *D) {
150 if (isa<ObjCInterfaceDecl>(D))
152 if (isa<ObjCCategoryDecl>(D))
154 if (isa<ObjCIvarDecl>(D))
156 if (isa<ObjCMethodDecl>(D))
158 if (isa<ImportDecl>(D))
163 static const Decl *adjustTemplateImplicitInstantiation(const Decl *D) {
164 if (const ClassTemplateSpecializationDecl *
165 SD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
166 return SD->getTemplateInstantiationPattern();
168 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
169 return FD->getTemplateInstantiationPattern();
174 static bool isDeclADefinition(const Decl *D, const DeclContext *ContainerDC, ASTContext &Ctx) {
175 if (auto VD = dyn_cast<VarDecl>(D))
176 return VD->isThisDeclarationADefinition(Ctx);
178 if (auto FD = dyn_cast<FunctionDecl>(D))
179 return FD->isThisDeclarationADefinition();
181 if (auto TD = dyn_cast<TagDecl>(D))
182 return TD->isThisDeclarationADefinition();
184 if (auto MD = dyn_cast<ObjCMethodDecl>(D))
185 return MD->isThisDeclarationADefinition() || isa<ObjCImplDecl>(ContainerDC);
187 if (isa<TypedefNameDecl>(D) ||
188 isa<EnumConstantDecl>(D) ||
190 isa<MSPropertyDecl>(D) ||
191 isa<ObjCImplDecl>(D) ||
192 isa<ObjCPropertyImplDecl>(D))
198 static const Decl *adjustParent(const Decl *Parent) {
201 for (;; Parent = cast<Decl>(Parent->getDeclContext())) {
202 if (isa<TranslationUnitDecl>(Parent))
204 if (isa<LinkageSpecDecl>(Parent) || isa<BlockDecl>(Parent))
206 if (auto NS = dyn_cast<NamespaceDecl>(Parent)) {
207 if (NS->isAnonymousNamespace())
209 } else if (auto RD = dyn_cast<RecordDecl>(Parent)) {
210 if (RD->isAnonymousStructOrUnion())
212 } else if (auto FD = dyn_cast<FieldDecl>(Parent)) {
213 if (FD->getDeclName().isEmpty())
220 static const Decl *getCanonicalDecl(const Decl *D) {
221 D = D->getCanonicalDecl();
222 if (auto TD = dyn_cast<TemplateDecl>(D)) {
223 D = TD->getTemplatedDecl();
224 assert(D->isCanonicalDecl());
230 bool IndexingContext::handleDeclOccurrence(const Decl *D, SourceLocation Loc,
231 bool IsRef, const Decl *Parent,
233 ArrayRef<SymbolRelation> Relations,
236 const DeclContext *ContainerDC) {
237 if (D->isImplicit() && !isa<ObjCMethodDecl>(D))
239 if (!isa<NamedDecl>(D) ||
240 (cast<NamedDecl>(D)->getDeclName().isEmpty() &&
241 !isa<TagDecl>(D) && !isa<ObjCCategoryDecl>(D)))
244 SourceManager &SM = Ctx->getSourceManager();
245 Loc = SM.getFileLoc(Loc);
251 std::tie(FID, Offset) = SM.getDecomposedLoc(Loc);
255 bool Invalid = false;
256 const SrcMgr::SLocEntry &SEntry = SM.getSLocEntry(FID, &Invalid);
257 if (Invalid || !SEntry.isFile())
260 if (SEntry.getFile().getFileCharacteristic() != SrcMgr::C_User) {
261 switch (IndexOpts.SystemSymbolFilter) {
262 case IndexingOptions::SystemSymbolFilterKind::None:
264 case IndexingOptions::SystemSymbolFilterKind::DeclarationsOnly:
268 case IndexingOptions::SystemSymbolFilterKind::All:
273 if (isTemplateImplicitInstantiation(D)) {
276 D = adjustTemplateImplicitInstantiation(D);
279 assert(!isTemplateImplicitInstantiation(D));
286 Roles |= (unsigned)SymbolRole::Reference;
287 else if (isDeclADefinition(D, ContainerDC, *Ctx))
288 Roles |= (unsigned)SymbolRole::Definition;
290 Roles |= (unsigned)SymbolRole::Declaration;
292 D = getCanonicalDecl(D);
293 if (D->isImplicit() && !isa<ObjCMethodDecl>(D) &&
294 !(isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->getBuiltinID())) {
295 // operator new declarations will link to the implicit one as canonical.
298 Parent = adjustParent(Parent);
300 Parent = getCanonicalDecl(Parent);
301 assert((!Parent || !Parent->isImplicit() ||
302 (isa<FunctionDecl>(Parent) &&
303 cast<FunctionDecl>(Parent)->getBuiltinID()) ||
304 isa<ObjCInterfaceDecl>(Parent) || isa<ObjCMethodDecl>(Parent)) &&
305 "unexpected implicit parent!");
307 SmallVector<SymbolRelation, 6> FinalRelations;
308 FinalRelations.reserve(Relations.size()+1);
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;
315 if (It != FinalRelations.end()) {
316 It->Roles |= Rel.Roles;
318 FinalRelations.push_back(Rel);
323 if (!IsRef && Parent && !cast<DeclContext>(Parent)->isFunctionOrMethod()) {
324 addRelation(SymbolRelation{(unsigned)SymbolRole::RelationChildOf, Parent});
326 for (auto &Rel : Relations) {
327 addRelation(SymbolRelation(Rel.Roles,
328 Rel.RelatedSymbol->getCanonicalDecl()));
331 IndexDataConsumer::ASTNodeInfo Node{ OrigE, OrigD, Parent, ContainerDC };
332 return DataConsumer.handleDeclOccurence(D, Roles, FinalRelations, FID, Offset,