2016-02-13 07:10:59 +08:00
|
|
|
//===- IndexingContext.cpp - Indexing context data ------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-02-13 07:10:59 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "IndexingContext.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
2019-11-16 09:31:55 +08:00
|
|
|
#include "clang/AST/Attr.h"
|
2016-02-13 07:10:59 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2019-11-16 09:31:55 +08:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
|
|
|
#include "clang/Basic/SourceLocation.h"
|
2016-02-13 07:10:59 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2019-11-16 09:31:55 +08:00
|
|
|
#include "clang/Index/IndexDataConsumer.h"
|
2016-02-13 07:10:59 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace index;
|
|
|
|
|
2017-04-21 13:42:46 +08:00
|
|
|
static bool isGeneratedDecl(const Decl *D) {
|
|
|
|
if (auto *attr = D->getAttr<ExternalSourceSymbolAttr>()) {
|
|
|
|
return attr->getGeneratedDeclaration();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IndexingContext::shouldIndex(const Decl *D) {
|
|
|
|
return !isGeneratedDecl(D);
|
|
|
|
}
|
|
|
|
|
2017-04-24 22:04:58 +08:00
|
|
|
const LangOptions &IndexingContext::getLangOpts() const {
|
|
|
|
return Ctx->getLangOpts();
|
|
|
|
}
|
|
|
|
|
2016-02-13 07:10:59 +08:00
|
|
|
bool IndexingContext::shouldIndexFunctionLocalSymbols() const {
|
|
|
|
return IndexOpts.IndexFunctionLocals;
|
|
|
|
}
|
|
|
|
|
2018-07-10 05:49:06 +08:00
|
|
|
bool IndexingContext::shouldIndexImplicitInstantiation() const {
|
|
|
|
return IndexOpts.IndexImplicitInstantiation;
|
|
|
|
}
|
|
|
|
|
2019-02-11 21:02:21 +08:00
|
|
|
bool IndexingContext::shouldIndexParametersInDeclarations() const {
|
|
|
|
return IndexOpts.IndexParametersInDeclarations;
|
|
|
|
}
|
|
|
|
|
2019-02-21 17:52:33 +08:00
|
|
|
bool IndexingContext::shouldIndexTemplateParameters() const {
|
|
|
|
return IndexOpts.IndexTemplateParameters;
|
|
|
|
}
|
|
|
|
|
2016-02-13 07:10:59 +08:00
|
|
|
bool IndexingContext::handleDecl(const Decl *D,
|
|
|
|
SymbolRoleSet Roles,
|
|
|
|
ArrayRef<SymbolRelation> Relations) {
|
2017-02-17 12:49:41 +08:00
|
|
|
return handleDecl(D, D->getLocation(), Roles, Relations);
|
2016-02-13 07:10:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IndexingContext::handleDecl(const Decl *D, SourceLocation Loc,
|
|
|
|
SymbolRoleSet Roles,
|
|
|
|
ArrayRef<SymbolRelation> Relations,
|
|
|
|
const DeclContext *DC) {
|
|
|
|
if (!DC)
|
|
|
|
DC = D->getDeclContext();
|
2017-02-17 12:49:41 +08:00
|
|
|
|
|
|
|
const Decl *OrigD = D;
|
|
|
|
if (isa<ObjCPropertyImplDecl>(D)) {
|
|
|
|
D = cast<ObjCPropertyImplDecl>(D)->getPropertyDecl();
|
|
|
|
}
|
2016-02-13 07:10:59 +08:00
|
|
|
return handleDeclOccurrence(D, Loc, /*IsRef=*/false, cast<Decl>(DC),
|
|
|
|
Roles, Relations,
|
2017-02-17 12:49:41 +08:00
|
|
|
nullptr, OrigD, DC);
|
2016-02-13 07:10:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc,
|
|
|
|
const NamedDecl *Parent,
|
|
|
|
const DeclContext *DC,
|
|
|
|
SymbolRoleSet Roles,
|
|
|
|
ArrayRef<SymbolRelation> Relations,
|
|
|
|
const Expr *RefE,
|
|
|
|
const Decl *RefD) {
|
2017-02-26 13:37:56 +08:00
|
|
|
if (!shouldIndexFunctionLocalSymbols() && isFunctionLocalSymbol(D))
|
2016-02-13 07:10:59 +08:00
|
|
|
return true;
|
|
|
|
|
2019-02-21 17:52:33 +08:00
|
|
|
if (!shouldIndexTemplateParameters() &&
|
|
|
|
(isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
|
|
|
|
isa<TemplateTemplateParmDecl>(D))) {
|
2016-02-13 07:10:59 +08:00
|
|
|
return true;
|
2019-02-21 17:52:33 +08:00
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2016-02-13 07:10:59 +08:00
|
|
|
return handleDeclOccurrence(D, Loc, /*IsRef=*/true, Parent, Roles, Relations,
|
|
|
|
RefE, RefD, DC);
|
|
|
|
}
|
|
|
|
|
2018-09-18 23:02:56 +08:00
|
|
|
static void reportModuleReferences(const Module *Mod,
|
|
|
|
ArrayRef<SourceLocation> IdLocs,
|
|
|
|
const ImportDecl *ImportD,
|
|
|
|
IndexDataConsumer &DataConsumer) {
|
|
|
|
if (!Mod)
|
|
|
|
return;
|
|
|
|
reportModuleReferences(Mod->Parent, IdLocs.drop_back(), ImportD,
|
|
|
|
DataConsumer);
|
2019-12-16 17:33:56 +08:00
|
|
|
DataConsumer.handleModuleOccurrence(
|
|
|
|
ImportD, Mod, (SymbolRoleSet)SymbolRole::Reference, IdLocs.back());
|
2018-09-18 23:02:56 +08:00
|
|
|
}
|
|
|
|
|
2016-02-13 07:10:59 +08:00
|
|
|
bool IndexingContext::importedModule(const ImportDecl *ImportD) {
|
2018-09-18 23:02:56 +08:00
|
|
|
if (ImportD->isInvalidDecl())
|
|
|
|
return true;
|
|
|
|
|
2016-02-29 15:56:07 +08:00
|
|
|
SourceLocation Loc;
|
|
|
|
auto IdLocs = ImportD->getIdentifierLocs();
|
|
|
|
if (!IdLocs.empty())
|
2018-09-18 23:02:56 +08:00
|
|
|
Loc = IdLocs.back();
|
2016-02-29 15:56:07 +08:00
|
|
|
else
|
|
|
|
Loc = ImportD->getLocation();
|
2016-02-13 07:10:59 +08:00
|
|
|
|
2018-04-09 22:12:51 +08:00
|
|
|
SourceManager &SM = Ctx->getSourceManager();
|
|
|
|
FileID FID = SM.getFileID(SM.getFileLoc(Loc));
|
2016-02-13 07:10:59 +08:00
|
|
|
if (FID.isInvalid())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
bool Invalid = false;
|
|
|
|
const SrcMgr::SLocEntry &SEntry = SM.getSLocEntry(FID, &Invalid);
|
|
|
|
if (Invalid || !SEntry.isFile())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (SEntry.getFile().getFileCharacteristic() != SrcMgr::C_User) {
|
|
|
|
switch (IndexOpts.SystemSymbolFilter) {
|
|
|
|
case IndexingOptions::SystemSymbolFilterKind::None:
|
|
|
|
return true;
|
2016-07-15 02:51:55 +08:00
|
|
|
case IndexingOptions::SystemSymbolFilterKind::DeclarationsOnly:
|
2016-02-13 07:10:59 +08:00
|
|
|
case IndexingOptions::SystemSymbolFilterKind::All:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 23:02:56 +08:00
|
|
|
const Module *Mod = ImportD->getImportedModule();
|
|
|
|
if (!ImportD->isImplicit() && Mod->Parent && !IdLocs.empty()) {
|
|
|
|
reportModuleReferences(Mod->Parent, IdLocs.drop_back(), ImportD,
|
|
|
|
DataConsumer);
|
|
|
|
}
|
|
|
|
|
2016-07-15 02:51:55 +08:00
|
|
|
SymbolRoleSet Roles = (unsigned)SymbolRole::Declaration;
|
2016-02-13 07:10:59 +08:00
|
|
|
if (ImportD->isImplicit())
|
|
|
|
Roles |= (unsigned)SymbolRole::Implicit;
|
|
|
|
|
2019-12-16 17:33:56 +08:00
|
|
|
return DataConsumer.handleModuleOccurrence(ImportD, Mod, Roles, Loc);
|
2016-02-13 07:10:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IndexingContext::isTemplateImplicitInstantiation(const Decl *D) {
|
|
|
|
TemplateSpecializationKind TKind = TSK_Undeclared;
|
|
|
|
if (const ClassTemplateSpecializationDecl *
|
|
|
|
SD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
|
|
|
|
TKind = SD->getSpecializationKind();
|
2016-11-08 05:20:15 +08:00
|
|
|
} else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
2016-02-13 07:10:59 +08:00
|
|
|
TKind = FD->getTemplateSpecializationKind();
|
2016-11-08 05:20:15 +08:00
|
|
|
} else if (auto *VD = dyn_cast<VarDecl>(D)) {
|
|
|
|
TKind = VD->getTemplateSpecializationKind();
|
2017-05-24 00:23:28 +08:00
|
|
|
} else if (const auto *RD = dyn_cast<CXXRecordDecl>(D)) {
|
|
|
|
if (RD->getInstantiatedFromMemberClass())
|
|
|
|
TKind = RD->getTemplateSpecializationKind();
|
2017-05-24 00:35:50 +08:00
|
|
|
} else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
|
|
|
|
if (ED->getInstantiatedFromMemberEnum())
|
|
|
|
TKind = ED->getTemplateSpecializationKind();
|
2017-05-24 00:47:01 +08:00
|
|
|
} else if (isa<FieldDecl>(D) || isa<TypedefNameDecl>(D) ||
|
|
|
|
isa<EnumConstantDecl>(D)) {
|
2017-05-24 00:25:06 +08:00
|
|
|
if (const auto *Parent = dyn_cast<Decl>(D->getDeclContext()))
|
|
|
|
return isTemplateImplicitInstantiation(Parent);
|
2016-02-13 07:10:59 +08:00
|
|
|
}
|
|
|
|
switch (TKind) {
|
|
|
|
case TSK_Undeclared:
|
2020-02-19 19:27:12 +08:00
|
|
|
// Instantiation maybe not happen yet when we see a SpecializationDecl,
|
|
|
|
// e.g. when the type doesn't need to be complete, we still treat it as an
|
|
|
|
// instantiation as we'd like to keep the canonicalized result consistent.
|
|
|
|
return isa<ClassTemplateSpecializationDecl>(D);
|
2016-02-13 07:10:59 +08:00
|
|
|
case TSK_ExplicitSpecialization:
|
|
|
|
return false;
|
|
|
|
case TSK_ImplicitInstantiation:
|
|
|
|
case TSK_ExplicitInstantiationDeclaration:
|
|
|
|
case TSK_ExplicitInstantiationDefinition:
|
|
|
|
return true;
|
|
|
|
}
|
2016-02-15 08:36:52 +08:00
|
|
|
llvm_unreachable("invalid TemplateSpecializationKind");
|
2016-02-13 07:10:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IndexingContext::shouldIgnoreIfImplicit(const Decl *D) {
|
|
|
|
if (isa<ObjCInterfaceDecl>(D))
|
|
|
|
return false;
|
|
|
|
if (isa<ObjCCategoryDecl>(D))
|
|
|
|
return false;
|
|
|
|
if (isa<ObjCIvarDecl>(D))
|
|
|
|
return false;
|
|
|
|
if (isa<ObjCMethodDecl>(D))
|
|
|
|
return false;
|
|
|
|
if (isa<ImportDecl>(D))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-24 00:25:06 +08:00
|
|
|
static const CXXRecordDecl *
|
|
|
|
getDeclContextForTemplateInstationPattern(const Decl *D) {
|
|
|
|
if (const auto *CTSD =
|
|
|
|
dyn_cast<ClassTemplateSpecializationDecl>(D->getDeclContext()))
|
|
|
|
return CTSD->getTemplateInstantiationPattern();
|
|
|
|
else if (const auto *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext()))
|
|
|
|
return RD->getInstantiatedFromMemberClass();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-02-13 07:10:59 +08:00
|
|
|
static const Decl *adjustTemplateImplicitInstantiation(const Decl *D) {
|
|
|
|
if (const ClassTemplateSpecializationDecl *
|
|
|
|
SD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
|
2020-02-19 19:27:12 +08:00
|
|
|
const auto *Template = SD->getTemplateInstantiationPattern();
|
|
|
|
if (Template)
|
|
|
|
return Template;
|
|
|
|
// Fallback to primary template if no instantiation is available yet (e.g.
|
|
|
|
// the type doesn't need to be complete).
|
|
|
|
return SD->getSpecializedTemplate()->getTemplatedDecl();
|
2016-11-08 05:20:15 +08:00
|
|
|
} else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
2016-02-13 07:10:59 +08:00
|
|
|
return FD->getTemplateInstantiationPattern();
|
2016-11-08 05:20:15 +08:00
|
|
|
} else if (auto *VD = dyn_cast<VarDecl>(D)) {
|
|
|
|
return VD->getTemplateInstantiationPattern();
|
2017-05-24 00:23:28 +08:00
|
|
|
} else if (const auto *RD = dyn_cast<CXXRecordDecl>(D)) {
|
|
|
|
return RD->getInstantiatedFromMemberClass();
|
2017-05-24 00:35:50 +08:00
|
|
|
} else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
|
|
|
|
return ED->getInstantiatedFromMemberEnum();
|
2017-05-24 00:27:42 +08:00
|
|
|
} else if (isa<FieldDecl>(D) || isa<TypedefNameDecl>(D)) {
|
|
|
|
const auto *ND = cast<NamedDecl>(D);
|
2017-05-24 00:25:06 +08:00
|
|
|
if (const CXXRecordDecl *Pattern =
|
2017-05-24 00:27:42 +08:00
|
|
|
getDeclContextForTemplateInstationPattern(ND)) {
|
|
|
|
for (const NamedDecl *BaseND : Pattern->lookup(ND->getDeclName())) {
|
|
|
|
if (BaseND->isImplicit())
|
2017-05-15 22:26:22 +08:00
|
|
|
continue;
|
2017-05-24 00:27:42 +08:00
|
|
|
if (BaseND->getKind() == ND->getKind())
|
|
|
|
return BaseND;
|
2017-05-15 22:26:22 +08:00
|
|
|
}
|
|
|
|
}
|
2017-05-24 00:47:01 +08:00
|
|
|
} else if (const auto *ECD = dyn_cast<EnumConstantDecl>(D)) {
|
|
|
|
if (const auto *ED = dyn_cast<EnumDecl>(ECD->getDeclContext())) {
|
|
|
|
if (const EnumDecl *Pattern = ED->getInstantiatedFromMemberEnum()) {
|
|
|
|
for (const NamedDecl *BaseECD : Pattern->lookup(ECD->getDeclName()))
|
|
|
|
return BaseECD;
|
|
|
|
}
|
|
|
|
}
|
2016-02-13 07:10:59 +08:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-04-01 04:18:22 +08:00
|
|
|
static bool isDeclADefinition(const Decl *D, const DeclContext *ContainerDC, ASTContext &Ctx) {
|
2016-02-13 07:10:59 +08:00
|
|
|
if (auto VD = dyn_cast<VarDecl>(D))
|
|
|
|
return VD->isThisDeclarationADefinition(Ctx);
|
|
|
|
|
|
|
|
if (auto FD = dyn_cast<FunctionDecl>(D))
|
|
|
|
return FD->isThisDeclarationADefinition();
|
|
|
|
|
|
|
|
if (auto TD = dyn_cast<TagDecl>(D))
|
|
|
|
return TD->isThisDeclarationADefinition();
|
|
|
|
|
|
|
|
if (auto MD = dyn_cast<ObjCMethodDecl>(D))
|
2016-04-01 04:18:22 +08:00
|
|
|
return MD->isThisDeclarationADefinition() || isa<ObjCImplDecl>(ContainerDC);
|
2016-02-13 07:10:59 +08:00
|
|
|
|
|
|
|
if (isa<TypedefNameDecl>(D) ||
|
|
|
|
isa<EnumConstantDecl>(D) ||
|
|
|
|
isa<FieldDecl>(D) ||
|
|
|
|
isa<MSPropertyDecl>(D) ||
|
|
|
|
isa<ObjCImplDecl>(D) ||
|
|
|
|
isa<ObjCPropertyImplDecl>(D))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-13 06:05:30 +08:00
|
|
|
/// Whether the given NamedDecl should be skipped because it has no name.
|
|
|
|
static bool shouldSkipNamelessDecl(const NamedDecl *ND) {
|
2017-08-16 01:20:37 +08:00
|
|
|
return (ND->getDeclName().isEmpty() && !isa<TagDecl>(ND) &&
|
|
|
|
!isa<ObjCCategoryDecl>(ND)) || isa<CXXDeductionGuideDecl>(ND);
|
2017-07-13 06:05:30 +08:00
|
|
|
}
|
|
|
|
|
2016-02-13 07:10:59 +08:00
|
|
|
static const Decl *adjustParent(const Decl *Parent) {
|
|
|
|
if (!Parent)
|
|
|
|
return nullptr;
|
|
|
|
for (;; Parent = cast<Decl>(Parent->getDeclContext())) {
|
|
|
|
if (isa<TranslationUnitDecl>(Parent))
|
|
|
|
return nullptr;
|
|
|
|
if (isa<LinkageSpecDecl>(Parent) || isa<BlockDecl>(Parent))
|
|
|
|
continue;
|
|
|
|
if (auto NS = dyn_cast<NamespaceDecl>(Parent)) {
|
|
|
|
if (NS->isAnonymousNamespace())
|
|
|
|
continue;
|
|
|
|
} else if (auto RD = dyn_cast<RecordDecl>(Parent)) {
|
|
|
|
if (RD->isAnonymousStructOrUnion())
|
|
|
|
continue;
|
2017-07-13 06:05:30 +08:00
|
|
|
} else if (auto ND = dyn_cast<NamedDecl>(Parent)) {
|
|
|
|
if (shouldSkipNamelessDecl(ND))
|
2016-02-13 07:10:59 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return Parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const Decl *getCanonicalDecl(const Decl *D) {
|
|
|
|
D = D->getCanonicalDecl();
|
|
|
|
if (auto TD = dyn_cast<TemplateDecl>(D)) {
|
2017-07-18 15:20:53 +08:00
|
|
|
if (auto TTD = TD->getTemplatedDecl()) {
|
|
|
|
D = TTD;
|
|
|
|
assert(D->isCanonicalDecl());
|
|
|
|
}
|
2016-02-13 07:10:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return D;
|
|
|
|
}
|
|
|
|
|
2017-03-24 00:34:47 +08:00
|
|
|
static bool shouldReportOccurrenceForSystemDeclOnlyMode(
|
|
|
|
bool IsRef, SymbolRoleSet Roles, ArrayRef<SymbolRelation> Relations) {
|
|
|
|
if (!IsRef)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
auto acceptForRelation = [](SymbolRoleSet roles) -> bool {
|
|
|
|
bool accept = false;
|
|
|
|
applyForEachSymbolRoleInterruptible(roles, [&accept](SymbolRole r) -> bool {
|
|
|
|
switch (r) {
|
|
|
|
case SymbolRole::RelationChildOf:
|
|
|
|
case SymbolRole::RelationBaseOf:
|
|
|
|
case SymbolRole::RelationOverrideOf:
|
|
|
|
case SymbolRole::RelationExtendedBy:
|
|
|
|
case SymbolRole::RelationAccessorOf:
|
|
|
|
case SymbolRole::RelationIBTypeOf:
|
|
|
|
accept = true;
|
|
|
|
return false;
|
|
|
|
case SymbolRole::Declaration:
|
|
|
|
case SymbolRole::Definition:
|
|
|
|
case SymbolRole::Reference:
|
|
|
|
case SymbolRole::Read:
|
|
|
|
case SymbolRole::Write:
|
|
|
|
case SymbolRole::Call:
|
|
|
|
case SymbolRole::Dynamic:
|
|
|
|
case SymbolRole::AddressOf:
|
|
|
|
case SymbolRole::Implicit:
|
2018-07-09 16:44:05 +08:00
|
|
|
case SymbolRole::Undefinition:
|
2017-03-24 00:34:47 +08:00
|
|
|
case SymbolRole::RelationReceivedBy:
|
|
|
|
case SymbolRole::RelationCalledBy:
|
|
|
|
case SymbolRole::RelationContainedBy:
|
2017-04-20 18:43:22 +08:00
|
|
|
case SymbolRole::RelationSpecializationOf:
|
2019-03-08 16:30:20 +08:00
|
|
|
case SymbolRole::NameReference:
|
2017-03-24 00:34:47 +08:00
|
|
|
return true;
|
|
|
|
}
|
2017-03-25 00:59:14 +08:00
|
|
|
llvm_unreachable("Unsupported SymbolRole value!");
|
2017-03-24 00:34:47 +08:00
|
|
|
});
|
|
|
|
return accept;
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto &Rel : Relations) {
|
|
|
|
if (acceptForRelation(Rel.Roles))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-13 07:10:59 +08:00
|
|
|
bool IndexingContext::handleDeclOccurrence(const Decl *D, SourceLocation Loc,
|
|
|
|
bool IsRef, const Decl *Parent,
|
|
|
|
SymbolRoleSet Roles,
|
|
|
|
ArrayRef<SymbolRelation> Relations,
|
|
|
|
const Expr *OrigE,
|
|
|
|
const Decl *OrigD,
|
|
|
|
const DeclContext *ContainerDC) {
|
|
|
|
if (D->isImplicit() && !isa<ObjCMethodDecl>(D))
|
|
|
|
return true;
|
2017-07-13 06:05:30 +08:00
|
|
|
if (!isa<NamedDecl>(D) || shouldSkipNamelessDecl(cast<NamedDecl>(D)))
|
2016-02-13 07:10:59 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
SourceManager &SM = Ctx->getSourceManager();
|
2018-04-09 22:12:51 +08:00
|
|
|
FileID FID = SM.getFileID(SM.getFileLoc(Loc));
|
2016-02-13 07:10:59 +08:00
|
|
|
if (FID.isInvalid())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
bool Invalid = false;
|
|
|
|
const SrcMgr::SLocEntry &SEntry = SM.getSLocEntry(FID, &Invalid);
|
|
|
|
if (Invalid || !SEntry.isFile())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (SEntry.getFile().getFileCharacteristic() != SrcMgr::C_User) {
|
|
|
|
switch (IndexOpts.SystemSymbolFilter) {
|
|
|
|
case IndexingOptions::SystemSymbolFilterKind::None:
|
|
|
|
return true;
|
|
|
|
case IndexingOptions::SystemSymbolFilterKind::DeclarationsOnly:
|
2017-03-24 00:34:47 +08:00
|
|
|
if (!shouldReportOccurrenceForSystemDeclOnlyMode(IsRef, Roles, Relations))
|
2016-02-13 07:10:59 +08:00
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
case IndexingOptions::SystemSymbolFilterKind::All:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-20 16:08:56 +08:00
|
|
|
if (!OrigD)
|
|
|
|
OrigD = D;
|
|
|
|
|
2016-02-13 07:10:59 +08:00
|
|
|
if (isTemplateImplicitInstantiation(D)) {
|
|
|
|
if (!IsRef)
|
|
|
|
return true;
|
|
|
|
D = adjustTemplateImplicitInstantiation(D);
|
|
|
|
if (!D)
|
|
|
|
return true;
|
|
|
|
assert(!isTemplateImplicitInstantiation(D));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsRef)
|
|
|
|
Roles |= (unsigned)SymbolRole::Reference;
|
2017-02-17 12:49:41 +08:00
|
|
|
else if (isDeclADefinition(OrigD, ContainerDC, *Ctx))
|
2016-02-13 07:10:59 +08:00
|
|
|
Roles |= (unsigned)SymbolRole::Definition;
|
|
|
|
else
|
|
|
|
Roles |= (unsigned)SymbolRole::Declaration;
|
|
|
|
|
|
|
|
D = getCanonicalDecl(D);
|
|
|
|
Parent = adjustParent(Parent);
|
|
|
|
if (Parent)
|
|
|
|
Parent = getCanonicalDecl(Parent);
|
|
|
|
|
|
|
|
SmallVector<SymbolRelation, 6> FinalRelations;
|
|
|
|
FinalRelations.reserve(Relations.size()+1);
|
|
|
|
|
|
|
|
auto addRelation = [&](SymbolRelation Rel) {
|
2019-03-31 16:48:19 +08:00
|
|
|
auto It = llvm::find_if(FinalRelations, [&](SymbolRelation Elem) -> bool {
|
|
|
|
return Elem.RelatedSymbol == Rel.RelatedSymbol;
|
|
|
|
});
|
2016-02-13 07:10:59 +08:00
|
|
|
if (It != FinalRelations.end()) {
|
|
|
|
It->Roles |= Rel.Roles;
|
|
|
|
} else {
|
|
|
|
FinalRelations.push_back(Rel);
|
|
|
|
}
|
|
|
|
Roles |= Rel.Roles;
|
|
|
|
};
|
|
|
|
|
2017-01-12 04:51:10 +08:00
|
|
|
if (Parent) {
|
2017-02-26 13:37:56 +08:00
|
|
|
if (IsRef || (!isa<ParmVarDecl>(D) && isFunctionLocalSymbol(D))) {
|
2017-01-12 05:01:07 +08:00
|
|
|
addRelation(SymbolRelation{
|
|
|
|
(unsigned)SymbolRole::RelationContainedBy,
|
|
|
|
Parent
|
|
|
|
});
|
2017-02-26 13:37:56 +08:00
|
|
|
} else {
|
2017-01-12 05:01:07 +08:00
|
|
|
addRelation(SymbolRelation{
|
|
|
|
(unsigned)SymbolRole::RelationChildOf,
|
|
|
|
Parent
|
|
|
|
});
|
2017-01-12 04:51:10 +08:00
|
|
|
}
|
2016-02-13 07:10:59 +08:00
|
|
|
}
|
2017-01-12 04:51:10 +08:00
|
|
|
|
2016-02-13 07:10:59 +08:00
|
|
|
for (auto &Rel : Relations) {
|
|
|
|
addRelation(SymbolRelation(Rel.Roles,
|
|
|
|
Rel.RelatedSymbol->getCanonicalDecl()));
|
|
|
|
}
|
|
|
|
|
2018-04-09 22:12:51 +08:00
|
|
|
IndexDataConsumer::ASTNodeInfo Node{OrigE, OrigD, Parent, ContainerDC};
|
2019-12-16 17:33:56 +08:00
|
|
|
return DataConsumer.handleDeclOccurrence(D, Roles, FinalRelations, Loc, Node);
|
2016-02-13 07:10:59 +08:00
|
|
|
}
|
2018-07-09 16:44:05 +08:00
|
|
|
|
|
|
|
void IndexingContext::handleMacroDefined(const IdentifierInfo &Name,
|
|
|
|
SourceLocation Loc,
|
|
|
|
const MacroInfo &MI) {
|
[index] Improve macro indexing support
The major change here is to index macro occurrences in more places than
before, specifically
* In non-expansion references such as `#if`, `#ifdef`, etc.
* When the macro is a reference to a builtin macro such as __LINE__.
* When using the preprocessor state instead of callbacks, we now include
all definition locations and undefinitions instead of just the latest
one (which may also have had the wrong location previously).
* When indexing an existing module file (.pcm), we now include module
macros, and we no longer report unrelated preprocessor macros during
indexing the module, which could have caused duplication.
Additionally, we now correctly obey the system symbol filter for macros,
so by default in system headers only definition/undefinition occurrences
are reported, but it can be configured to report references as well if
desired.
Extends FileIndexRecord to support occurrences of macros. Since the
design of this type is to keep a single list of entities organized by
source location, we incorporate macros into the existing DeclOccurrence
struct.
Differential Revision: https://reviews.llvm.org/D99758
2021-03-24 06:22:58 +08:00
|
|
|
if (!shouldIndexMacroOccurrence(/*IsRef=*/false, Loc))
|
|
|
|
return;
|
2018-07-09 16:44:05 +08:00
|
|
|
SymbolRoleSet Roles = (unsigned)SymbolRole::Definition;
|
2019-12-16 17:33:56 +08:00
|
|
|
DataConsumer.handleMacroOccurrence(&Name, &MI, Roles, Loc);
|
2018-07-09 16:44:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void IndexingContext::handleMacroUndefined(const IdentifierInfo &Name,
|
|
|
|
SourceLocation Loc,
|
|
|
|
const MacroInfo &MI) {
|
[index] Improve macro indexing support
The major change here is to index macro occurrences in more places than
before, specifically
* In non-expansion references such as `#if`, `#ifdef`, etc.
* When the macro is a reference to a builtin macro such as __LINE__.
* When using the preprocessor state instead of callbacks, we now include
all definition locations and undefinitions instead of just the latest
one (which may also have had the wrong location previously).
* When indexing an existing module file (.pcm), we now include module
macros, and we no longer report unrelated preprocessor macros during
indexing the module, which could have caused duplication.
Additionally, we now correctly obey the system symbol filter for macros,
so by default in system headers only definition/undefinition occurrences
are reported, but it can be configured to report references as well if
desired.
Extends FileIndexRecord to support occurrences of macros. Since the
design of this type is to keep a single list of entities organized by
source location, we incorporate macros into the existing DeclOccurrence
struct.
Differential Revision: https://reviews.llvm.org/D99758
2021-03-24 06:22:58 +08:00
|
|
|
if (!shouldIndexMacroOccurrence(/*IsRef=*/false, Loc))
|
|
|
|
return;
|
2018-07-09 16:44:05 +08:00
|
|
|
SymbolRoleSet Roles = (unsigned)SymbolRole::Undefinition;
|
2019-12-16 17:33:56 +08:00
|
|
|
DataConsumer.handleMacroOccurrence(&Name, &MI, Roles, Loc);
|
2018-07-09 16:44:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void IndexingContext::handleMacroReference(const IdentifierInfo &Name,
|
|
|
|
SourceLocation Loc,
|
|
|
|
const MacroInfo &MI) {
|
[index] Improve macro indexing support
The major change here is to index macro occurrences in more places than
before, specifically
* In non-expansion references such as `#if`, `#ifdef`, etc.
* When the macro is a reference to a builtin macro such as __LINE__.
* When using the preprocessor state instead of callbacks, we now include
all definition locations and undefinitions instead of just the latest
one (which may also have had the wrong location previously).
* When indexing an existing module file (.pcm), we now include module
macros, and we no longer report unrelated preprocessor macros during
indexing the module, which could have caused duplication.
Additionally, we now correctly obey the system symbol filter for macros,
so by default in system headers only definition/undefinition occurrences
are reported, but it can be configured to report references as well if
desired.
Extends FileIndexRecord to support occurrences of macros. Since the
design of this type is to keep a single list of entities organized by
source location, we incorporate macros into the existing DeclOccurrence
struct.
Differential Revision: https://reviews.llvm.org/D99758
2021-03-24 06:22:58 +08:00
|
|
|
if (!shouldIndexMacroOccurrence(/*IsRef=*/true, Loc))
|
|
|
|
return;
|
2018-07-09 16:44:05 +08:00
|
|
|
SymbolRoleSet Roles = (unsigned)SymbolRole::Reference;
|
2019-12-16 17:33:56 +08:00
|
|
|
DataConsumer.handleMacroOccurrence(&Name, &MI, Roles, Loc);
|
2018-07-09 16:44:05 +08:00
|
|
|
}
|
[index] Improve macro indexing support
The major change here is to index macro occurrences in more places than
before, specifically
* In non-expansion references such as `#if`, `#ifdef`, etc.
* When the macro is a reference to a builtin macro such as __LINE__.
* When using the preprocessor state instead of callbacks, we now include
all definition locations and undefinitions instead of just the latest
one (which may also have had the wrong location previously).
* When indexing an existing module file (.pcm), we now include module
macros, and we no longer report unrelated preprocessor macros during
indexing the module, which could have caused duplication.
Additionally, we now correctly obey the system symbol filter for macros,
so by default in system headers only definition/undefinition occurrences
are reported, but it can be configured to report references as well if
desired.
Extends FileIndexRecord to support occurrences of macros. Since the
design of this type is to keep a single list of entities organized by
source location, we incorporate macros into the existing DeclOccurrence
struct.
Differential Revision: https://reviews.llvm.org/D99758
2021-03-24 06:22:58 +08:00
|
|
|
|
|
|
|
bool IndexingContext::shouldIndexMacroOccurrence(bool IsRef,
|
|
|
|
SourceLocation Loc) {
|
|
|
|
if (!IndexOpts.IndexMacros)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (IndexOpts.SystemSymbolFilter) {
|
|
|
|
case IndexingOptions::SystemSymbolFilterKind::None:
|
|
|
|
break;
|
|
|
|
case IndexingOptions::SystemSymbolFilterKind::DeclarationsOnly:
|
|
|
|
if (!IsRef)
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
case IndexingOptions::SystemSymbolFilterKind::All:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceManager &SM = Ctx->getSourceManager();
|
|
|
|
FileID FID = SM.getFileID(SM.getFileLoc(Loc));
|
|
|
|
if (FID.isInvalid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool Invalid = false;
|
|
|
|
const SrcMgr::SLocEntry &SEntry = SM.getSLocEntry(FID, &Invalid);
|
|
|
|
if (Invalid || !SEntry.isFile())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return SEntry.getFile().getFileCharacteristic() == SrcMgr::C_User;
|
|
|
|
}
|