Imported Upstream version 6.10.0.49

Former-commit-id: 1d6753294b2993e1fbf92de9366bb9544db4189b
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2020-01-16 16:38:04 +00:00
parent d94e79959b
commit 468663ddbb
48518 changed files with 2789335 additions and 61176 deletions

View File

@ -0,0 +1,29 @@
set(LLVM_LINK_COMPONENTS
Core
Support
)
add_clang_library(clangIndex
CodegenNameGenerator.cpp
CommentToXML.cpp
IndexBody.cpp
IndexDecl.cpp
IndexingAction.cpp
IndexingContext.cpp
IndexSymbol.cpp
IndexTypeSourceInfo.cpp
USRGeneration.cpp
ADDITIONAL_HEADERS
IndexingContext.h
SimpleFormatContext.h
LINK_LIBS
clangAST
clangBasic
clangFormat
clangFrontend
clangRewrite
clangSerialization
clangToolingCore
)

View File

@ -0,0 +1,228 @@
//===- CodegenNameGenerator.cpp - Codegen name generation -----------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Determines the name that the symbol will get for code generation.
//
//===----------------------------------------------------------------------===//
#include "clang/Index/CodegenNameGenerator.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/Mangle.h"
#include "clang/AST/VTableBuilder.h"
#include "clang/Basic/TargetInfo.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Mangler.h"
#include "llvm/Support/raw_ostream.h"
using namespace clang;
using namespace clang::index;
struct CodegenNameGenerator::Implementation {
std::unique_ptr<MangleContext> MC;
llvm::DataLayout DL;
Implementation(ASTContext &Ctx)
: MC(Ctx.createMangleContext()),
DL(Ctx.getTargetInfo().getDataLayout()) {}
bool writeName(const Decl *D, raw_ostream &OS) {
// First apply frontend mangling.
SmallString<128> FrontendBuf;
llvm::raw_svector_ostream FrontendBufOS(FrontendBuf);
if (auto *FD = dyn_cast<FunctionDecl>(D)) {
if (FD->isDependentContext())
return true;
if (writeFuncOrVarName(FD, FrontendBufOS))
return true;
} else if (auto *VD = dyn_cast<VarDecl>(D)) {
if (writeFuncOrVarName(VD, FrontendBufOS))
return true;
} else if (auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
MC->mangleObjCMethodNameWithoutSize(MD, OS);
return false;
} else if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
writeObjCClassName(ID, FrontendBufOS);
} else {
return true;
}
// Now apply backend mangling.
llvm::Mangler::getNameWithPrefix(OS, FrontendBufOS.str(), DL);
return false;
}
std::string getName(const Decl *D) {
std::string Name;
{
llvm::raw_string_ostream OS(Name);
writeName(D, OS);
}
return Name;
}
enum ObjCKind {
ObjCClass,
ObjCMetaclass,
};
std::vector<std::string> getAllManglings(const ObjCContainerDecl *OCD) {
StringRef ClassName;
if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(OCD))
ClassName = OID->getObjCRuntimeNameAsString();
else if (const auto *OID = dyn_cast<ObjCImplementationDecl>(OCD))
ClassName = OID->getObjCRuntimeNameAsString();
if (ClassName.empty())
return {};
auto Mangle = [&](ObjCKind Kind, StringRef ClassName) -> std::string {
SmallString<40> Mangled;
auto Prefix = getClassSymbolPrefix(Kind, OCD->getASTContext());
llvm::Mangler::getNameWithPrefix(Mangled, Prefix + ClassName, DL);
return Mangled.str();
};
return {
Mangle(ObjCClass, ClassName),
Mangle(ObjCMetaclass, ClassName),
};
}
std::vector<std::string> getAllManglings(const Decl *D) {
if (const auto *OCD = dyn_cast<ObjCContainerDecl>(D))
return getAllManglings(OCD);
if (!(isa<CXXRecordDecl>(D) || isa<CXXMethodDecl>(D)))
return {};
const NamedDecl *ND = cast<NamedDecl>(D);
ASTContext &Ctx = ND->getASTContext();
std::unique_ptr<MangleContext> M(Ctx.createMangleContext());
std::vector<std::string> Manglings;
auto hasDefaultCXXMethodCC = [](ASTContext &C, const CXXMethodDecl *MD) {
auto DefaultCC = C.getDefaultCallingConvention(/*IsVariadic=*/false,
/*IsCSSMethod=*/true);
auto CC = MD->getType()->getAs<FunctionProtoType>()->getCallConv();
return CC == DefaultCC;
};
if (const auto *CD = dyn_cast_or_null<CXXConstructorDecl>(ND)) {
Manglings.emplace_back(getMangledStructor(CD, Ctor_Base));
if (Ctx.getTargetInfo().getCXXABI().isItaniumFamily())
if (!CD->getParent()->isAbstract())
Manglings.emplace_back(getMangledStructor(CD, Ctor_Complete));
if (Ctx.getTargetInfo().getCXXABI().isMicrosoft())
if (CD->hasAttr<DLLExportAttr>() && CD->isDefaultConstructor())
if (!(hasDefaultCXXMethodCC(Ctx, CD) && CD->getNumParams() == 0))
Manglings.emplace_back(getMangledStructor(CD, Ctor_DefaultClosure));
} else if (const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND)) {
Manglings.emplace_back(getMangledStructor(DD, Dtor_Base));
if (Ctx.getTargetInfo().getCXXABI().isItaniumFamily()) {
Manglings.emplace_back(getMangledStructor(DD, Dtor_Complete));
if (DD->isVirtual())
Manglings.emplace_back(getMangledStructor(DD, Dtor_Deleting));
}
} else if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(ND)) {
Manglings.emplace_back(getName(ND));
if (MD->isVirtual())
if (const auto *TIV = Ctx.getVTableContext()->getThunkInfo(MD))
for (const auto &T : *TIV)
Manglings.emplace_back(getMangledThunk(MD, T));
}
return Manglings;
}
private:
bool writeFuncOrVarName(const NamedDecl *D, raw_ostream &OS) {
if (MC->shouldMangleDeclName(D)) {
if (const auto *CtorD = dyn_cast<CXXConstructorDecl>(D))
MC->mangleCXXCtor(CtorD, Ctor_Complete, OS);
else if (const auto *DtorD = dyn_cast<CXXDestructorDecl>(D))
MC->mangleCXXDtor(DtorD, Dtor_Complete, OS);
else
MC->mangleName(D, OS);
return false;
} else {
IdentifierInfo *II = D->getIdentifier();
if (!II)
return true;
OS << II->getName();
return false;
}
}
void writeObjCClassName(const ObjCInterfaceDecl *D, raw_ostream &OS) {
OS << getClassSymbolPrefix(ObjCClass, D->getASTContext());
OS << D->getObjCRuntimeNameAsString();
}
static StringRef getClassSymbolPrefix(ObjCKind Kind, const ASTContext &Context) {
if (Context.getLangOpts().ObjCRuntime.isGNUFamily())
return Kind == ObjCMetaclass ? "_OBJC_METACLASS_" : "_OBJC_CLASS_";
return Kind == ObjCMetaclass ? "OBJC_METACLASS_$_" : "OBJC_CLASS_$_";
}
std::string getMangledStructor(const NamedDecl *ND, unsigned StructorType) {
std::string FrontendBuf;
llvm::raw_string_ostream FOS(FrontendBuf);
if (const auto *CD = dyn_cast_or_null<CXXConstructorDecl>(ND))
MC->mangleCXXCtor(CD, static_cast<CXXCtorType>(StructorType), FOS);
else if (const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND))
MC->mangleCXXDtor(DD, static_cast<CXXDtorType>(StructorType), FOS);
std::string BackendBuf;
llvm::raw_string_ostream BOS(BackendBuf);
llvm::Mangler::getNameWithPrefix(BOS, FOS.str(), DL);
return BOS.str();
}
std::string getMangledThunk(const CXXMethodDecl *MD, const ThunkInfo &T) {
std::string FrontendBuf;
llvm::raw_string_ostream FOS(FrontendBuf);
MC->mangleThunk(MD, T, FOS);
std::string BackendBuf;
llvm::raw_string_ostream BOS(BackendBuf);
llvm::Mangler::getNameWithPrefix(BOS, FOS.str(), DL);
return BOS.str();
}
};
CodegenNameGenerator::CodegenNameGenerator(ASTContext &Ctx)
: Impl(new Implementation(Ctx)) {
}
CodegenNameGenerator::~CodegenNameGenerator() {
}
bool CodegenNameGenerator::writeName(const Decl *D, raw_ostream &OS) {
return Impl->writeName(D, OS);
}
std::string CodegenNameGenerator::getName(const Decl *D) {
return Impl->getName(D);
}
std::vector<std::string> CodegenNameGenerator::getAllManglings(const Decl *D) {
return Impl->getAllManglings(D);
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,453 @@
//===- IndexBody.cpp - Indexing statements --------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "IndexingContext.h"
#include "clang/AST/RecursiveASTVisitor.h"
using namespace clang;
using namespace clang::index;
namespace {
class BodyIndexer : public RecursiveASTVisitor<BodyIndexer> {
IndexingContext &IndexCtx;
const NamedDecl *Parent;
const DeclContext *ParentDC;
SmallVector<Stmt*, 16> StmtStack;
typedef RecursiveASTVisitor<BodyIndexer> base;
Stmt *getParentStmt() const {
return StmtStack.size() < 2 ? nullptr : StmtStack.end()[-2];
}
public:
BodyIndexer(IndexingContext &indexCtx,
const NamedDecl *Parent, const DeclContext *DC)
: IndexCtx(indexCtx), Parent(Parent), ParentDC(DC) { }
bool shouldWalkTypesOfTypeLocs() const { return false; }
bool dataTraverseStmtPre(Stmt *S) {
StmtStack.push_back(S);
return true;
}
bool dataTraverseStmtPost(Stmt *S) {
assert(StmtStack.back() == S);
StmtStack.pop_back();
return true;
}
bool TraverseTypeLoc(TypeLoc TL) {
IndexCtx.indexTypeLoc(TL, Parent, ParentDC);
return true;
}
bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
IndexCtx.indexNestedNameSpecifierLoc(NNS, Parent, ParentDC);
return true;
}
SymbolRoleSet getRolesForRef(const Expr *E,
SmallVectorImpl<SymbolRelation> &Relations) {
SymbolRoleSet Roles{};
assert(!StmtStack.empty() && E == StmtStack.back());
if (StmtStack.size() == 1)
return Roles;
auto It = StmtStack.end()-2;
while (isa<CastExpr>(*It) || isa<ParenExpr>(*It)) {
if (auto ICE = dyn_cast<ImplicitCastExpr>(*It)) {
if (ICE->getCastKind() == CK_LValueToRValue)
Roles |= (unsigned)(unsigned)SymbolRole::Read;
}
if (It == StmtStack.begin())
break;
--It;
}
const Stmt *Parent = *It;
if (auto BO = dyn_cast<BinaryOperator>(Parent)) {
if (BO->getOpcode() == BO_Assign && BO->getLHS()->IgnoreParenCasts() == E)
Roles |= (unsigned)SymbolRole::Write;
} else if (auto UO = dyn_cast<UnaryOperator>(Parent)) {
if (UO->isIncrementDecrementOp()) {
Roles |= (unsigned)SymbolRole::Read;
Roles |= (unsigned)SymbolRole::Write;
} else if (UO->getOpcode() == UO_AddrOf) {
Roles |= (unsigned)SymbolRole::AddressOf;
}
} else if (auto CA = dyn_cast<CompoundAssignOperator>(Parent)) {
if (CA->getLHS()->IgnoreParenCasts() == E) {
Roles |= (unsigned)SymbolRole::Read;
Roles |= (unsigned)SymbolRole::Write;
}
} else if (auto CE = dyn_cast<CallExpr>(Parent)) {
if (CE->getCallee()->IgnoreParenCasts() == E) {
addCallRole(Roles, Relations);
if (auto *ME = dyn_cast<MemberExpr>(E)) {
if (auto *CXXMD = dyn_cast_or_null<CXXMethodDecl>(ME->getMemberDecl()))
if (CXXMD->isVirtual() && !ME->hasQualifier()) {
Roles |= (unsigned)SymbolRole::Dynamic;
auto BaseTy = ME->getBase()->IgnoreImpCasts()->getType();
if (!BaseTy.isNull())
if (auto *CXXRD = BaseTy->getPointeeCXXRecordDecl())
Relations.emplace_back((unsigned)SymbolRole::RelationReceivedBy,
CXXRD);
}
}
} else if (auto CXXOp = dyn_cast<CXXOperatorCallExpr>(CE)) {
if (CXXOp->getNumArgs() > 0 && CXXOp->getArg(0)->IgnoreParenCasts() == E) {
OverloadedOperatorKind Op = CXXOp->getOperator();
if (Op == OO_Equal) {
Roles |= (unsigned)SymbolRole::Write;
} else if ((Op >= OO_PlusEqual && Op <= OO_PipeEqual) ||
Op == OO_LessLessEqual || Op == OO_GreaterGreaterEqual ||
Op == OO_PlusPlus || Op == OO_MinusMinus) {
Roles |= (unsigned)SymbolRole::Read;
Roles |= (unsigned)SymbolRole::Write;
} else if (Op == OO_Amp) {
Roles |= (unsigned)SymbolRole::AddressOf;
}
}
}
}
return Roles;
}
void addCallRole(SymbolRoleSet &Roles,
SmallVectorImpl<SymbolRelation> &Relations) {
Roles |= (unsigned)SymbolRole::Call;
if (auto *FD = dyn_cast<FunctionDecl>(ParentDC))
Relations.emplace_back((unsigned)SymbolRole::RelationCalledBy, FD);
else if (auto *MD = dyn_cast<ObjCMethodDecl>(ParentDC))
Relations.emplace_back((unsigned)SymbolRole::RelationCalledBy, MD);
}
bool VisitDeclRefExpr(DeclRefExpr *E) {
SmallVector<SymbolRelation, 4> Relations;
SymbolRoleSet Roles = getRolesForRef(E, Relations);
return IndexCtx.handleReference(E->getDecl(), E->getLocation(),
Parent, ParentDC, Roles, Relations, E);
}
bool VisitMemberExpr(MemberExpr *E) {
SourceLocation Loc = E->getMemberLoc();
if (Loc.isInvalid())
Loc = E->getLocStart();
SmallVector<SymbolRelation, 4> Relations;
SymbolRoleSet Roles = getRolesForRef(E, Relations);
return IndexCtx.handleReference(E->getMemberDecl(), Loc,
Parent, ParentDC, Roles, Relations, E);
}
bool indexDependentReference(
const Expr *E, const Type *T, const DeclarationNameInfo &NameInfo,
llvm::function_ref<bool(const NamedDecl *ND)> Filter) {
if (!T)
return true;
const TemplateSpecializationType *TST =
T->getAs<TemplateSpecializationType>();
if (!TST)
return true;
TemplateName TN = TST->getTemplateName();
const ClassTemplateDecl *TD =
dyn_cast_or_null<ClassTemplateDecl>(TN.getAsTemplateDecl());
if (!TD)
return true;
CXXRecordDecl *RD = TD->getTemplatedDecl();
if (!RD->hasDefinition())
return true;
RD = RD->getDefinition();
std::vector<const NamedDecl *> Symbols =
RD->lookupDependentName(NameInfo.getName(), Filter);
// FIXME: Improve overload handling.
if (Symbols.size() != 1)
return true;
SourceLocation Loc = NameInfo.getLoc();
if (Loc.isInvalid())
Loc = E->getLocStart();
SmallVector<SymbolRelation, 4> Relations;
SymbolRoleSet Roles = getRolesForRef(E, Relations);
return IndexCtx.handleReference(Symbols[0], Loc, Parent, ParentDC, Roles,
Relations, E);
}
bool VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E) {
const DeclarationNameInfo &Info = E->getMemberNameInfo();
return indexDependentReference(
E, E->getBaseType().getTypePtrOrNull(), Info,
[](const NamedDecl *D) { return D->isCXXInstanceMember(); });
}
bool VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
const DeclarationNameInfo &Info = E->getNameInfo();
const NestedNameSpecifier *NNS = E->getQualifier();
return indexDependentReference(
E, NNS->getAsType(), Info,
[](const NamedDecl *D) { return !D->isCXXInstanceMember(); });
}
bool VisitDesignatedInitExpr(DesignatedInitExpr *E) {
for (DesignatedInitExpr::Designator &D : llvm::reverse(E->designators())) {
if (D.isFieldDesignator() && D.getField())
return IndexCtx.handleReference(D.getField(), D.getFieldLoc(), Parent,
ParentDC, SymbolRoleSet(), {}, E);
}
return true;
}
bool VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
SmallVector<SymbolRelation, 4> Relations;
SymbolRoleSet Roles = getRolesForRef(E, Relations);
return IndexCtx.handleReference(E->getDecl(), E->getLocation(),
Parent, ParentDC, Roles, Relations, E);
}
bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
auto isDynamic = [](const ObjCMessageExpr *MsgE)->bool {
if (MsgE->getReceiverKind() != ObjCMessageExpr::Instance)
return false;
if (auto *RecE = dyn_cast<ObjCMessageExpr>(
MsgE->getInstanceReceiver()->IgnoreParenCasts())) {
if (RecE->getMethodFamily() == OMF_alloc)
return false;
}
return true;
};
if (ObjCMethodDecl *MD = E->getMethodDecl()) {
SymbolRoleSet Roles{};
SmallVector<SymbolRelation, 2> Relations;
addCallRole(Roles, Relations);
Stmt *Containing = getParentStmt();
auto IsImplicitProperty = [](const PseudoObjectExpr *POE) -> bool {
const auto *E = POE->getSyntacticForm();
if (const auto *BinOp = dyn_cast<BinaryOperator>(E))
E = BinOp->getLHS();
const auto *PRE = dyn_cast<ObjCPropertyRefExpr>(E);
if (!PRE)
return false;
if (PRE->isExplicitProperty())
return false;
if (const ObjCMethodDecl *Getter = PRE->getImplicitPropertyGetter()) {
// Class properties that are explicitly defined using @property
// declarations are represented implicitly as there is no ivar for
// class properties.
if (Getter->isClassMethod() &&
Getter->getCanonicalDecl()->findPropertyDecl())
return false;
}
return true;
};
bool IsPropCall = Containing && isa<PseudoObjectExpr>(Containing);
// Implicit property message sends are not 'implicit'.
if ((E->isImplicit() || IsPropCall) &&
!(IsPropCall &&
IsImplicitProperty(cast<PseudoObjectExpr>(Containing))))
Roles |= (unsigned)SymbolRole::Implicit;
if (isDynamic(E)) {
Roles |= (unsigned)SymbolRole::Dynamic;
if (auto *RecD = E->getReceiverInterface())
Relations.emplace_back((unsigned)SymbolRole::RelationReceivedBy, RecD);
}
return IndexCtx.handleReference(MD, E->getSelectorStartLoc(),
Parent, ParentDC, Roles, Relations, E);
}
return true;
}
bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
if (E->isClassReceiver())
IndexCtx.handleReference(E->getClassReceiver(), E->getReceiverLocation(),
Parent, ParentDC);
if (E->isExplicitProperty()) {
SmallVector<SymbolRelation, 2> Relations;
SymbolRoleSet Roles = getRolesForRef(E, Relations);
return IndexCtx.handleReference(E->getExplicitProperty(), E->getLocation(),
Parent, ParentDC, Roles, Relations, E);
} else if (const ObjCMethodDecl *Getter = E->getImplicitPropertyGetter()) {
// Class properties that are explicitly defined using @property
// declarations are represented implicitly as there is no ivar for class
// properties.
if (Getter->isClassMethod()) {
if (const auto *PD = Getter->getCanonicalDecl()->findPropertyDecl()) {
SmallVector<SymbolRelation, 2> Relations;
SymbolRoleSet Roles = getRolesForRef(E, Relations);
return IndexCtx.handleReference(PD, E->getLocation(), Parent,
ParentDC, Roles, Relations, E);
}
}
}
// No need to do a handleReference for the objc method, because there will
// be a message expr as part of PseudoObjectExpr.
return true;
}
bool VisitMSPropertyRefExpr(MSPropertyRefExpr *E) {
return IndexCtx.handleReference(E->getPropertyDecl(), E->getMemberLoc(),
Parent, ParentDC, SymbolRoleSet(), {}, E);
}
bool VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
return IndexCtx.handleReference(E->getProtocol(), E->getProtocolIdLoc(),
Parent, ParentDC, SymbolRoleSet(), {}, E);
}
bool passObjCLiteralMethodCall(const ObjCMethodDecl *MD, const Expr *E) {
SymbolRoleSet Roles{};
SmallVector<SymbolRelation, 2> Relations;
addCallRole(Roles, Relations);
Roles |= (unsigned)SymbolRole::Implicit;
return IndexCtx.handleReference(MD, E->getLocStart(),
Parent, ParentDC, Roles, Relations, E);
}
bool VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
if (ObjCMethodDecl *MD = E->getBoxingMethod()) {
return passObjCLiteralMethodCall(MD, E);
}
return true;
}
bool VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
if (ObjCMethodDecl *MD = E->getDictWithObjectsMethod()) {
return passObjCLiteralMethodCall(MD, E);
}
return true;
}
bool VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
if (ObjCMethodDecl *MD = E->getArrayWithObjectsMethod()) {
return passObjCLiteralMethodCall(MD, E);
}
return true;
}
bool VisitCXXConstructExpr(CXXConstructExpr *E) {
SymbolRoleSet Roles{};
SmallVector<SymbolRelation, 2> Relations;
addCallRole(Roles, Relations);
return IndexCtx.handleReference(E->getConstructor(), E->getLocation(),
Parent, ParentDC, Roles, Relations, E);
}
bool TraverseCXXOperatorCallExpr(CXXOperatorCallExpr *E,
DataRecursionQueue *Q = nullptr) {
if (E->getOperatorLoc().isInvalid())
return true; // implicit.
return base::TraverseCXXOperatorCallExpr(E, Q);
}
bool VisitDeclStmt(DeclStmt *S) {
if (IndexCtx.shouldIndexFunctionLocalSymbols()) {
IndexCtx.indexDeclGroupRef(S->getDeclGroup());
return true;
}
DeclGroupRef DG = S->getDeclGroup();
for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) {
const Decl *D = *I;
if (!D)
continue;
if (!isFunctionLocalSymbol(D))
IndexCtx.indexTopLevelDecl(D);
}
return true;
}
bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C,
Expr *Init) {
if (C->capturesThis() || C->capturesVLAType())
return true;
if (C->capturesVariable() && IndexCtx.shouldIndexFunctionLocalSymbols())
return IndexCtx.handleReference(C->getCapturedVar(), C->getLocation(),
Parent, ParentDC, SymbolRoleSet());
// FIXME: Lambda init-captures.
return true;
}
// RecursiveASTVisitor visits both syntactic and semantic forms, duplicating
// the things that we visit. Make sure to only visit the semantic form.
// Also visit things that are in the syntactic form but not the semantic one,
// for example the indices in DesignatedInitExprs.
bool TraverseInitListExpr(InitListExpr *S, DataRecursionQueue *Q = nullptr) {
auto visitForm = [&](InitListExpr *Form) {
for (Stmt *SubStmt : Form->children()) {
if (!TraverseStmt(SubStmt, Q))
return false;
}
return true;
};
auto visitSyntacticDesignatedInitExpr = [&](DesignatedInitExpr *E) -> bool {
for (DesignatedInitExpr::Designator &D : llvm::reverse(E->designators())) {
if (D.isFieldDesignator())
return IndexCtx.handleReference(D.getField(), D.getFieldLoc(),
Parent, ParentDC, SymbolRoleSet(),
{}, E);
}
return true;
};
InitListExpr *SemaForm = S->isSemanticForm() ? S : S->getSemanticForm();
InitListExpr *SyntaxForm = S->isSemanticForm() ? S->getSyntacticForm() : S;
if (SemaForm) {
// Visit things present in syntactic form but not the semantic form.
if (SyntaxForm) {
for (Expr *init : SyntaxForm->inits()) {
if (auto *DIE = dyn_cast<DesignatedInitExpr>(init))
visitSyntacticDesignatedInitExpr(DIE);
}
}
return visitForm(SemaForm);
}
// No semantic, try the syntactic.
if (SyntaxForm) {
return visitForm(SyntaxForm);
}
return true;
}
bool VisitOffsetOfExpr(OffsetOfExpr *S) {
for (unsigned I = 0, E = S->getNumComponents(); I != E; ++I) {
const OffsetOfNode &Component = S->getComponent(I);
if (Component.getKind() == OffsetOfNode::Field)
IndexCtx.handleReference(Component.getField(), Component.getLocEnd(),
Parent, ParentDC, SymbolRoleSet(), {});
// FIXME: Try to resolve dependent field references.
}
return true;
}
};
} // anonymous namespace
void IndexingContext::indexBody(const Stmt *S, const NamedDecl *Parent,
const DeclContext *DC) {
if (!S)
return;
if (!DC)
DC = Parent->getLexicalDeclContext();
BodyIndexer(*this, Parent, DC).TraverseStmt(const_cast<Stmt*>(S));
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,266 @@
//===- IndexTypeSourceInfo.cpp - Indexing types ---------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "IndexingContext.h"
#include "clang/AST/RecursiveASTVisitor.h"
using namespace clang;
using namespace index;
namespace {
class TypeIndexer : public RecursiveASTVisitor<TypeIndexer> {
IndexingContext &IndexCtx;
const NamedDecl *Parent;
const DeclContext *ParentDC;
bool IsBase;
SmallVector<SymbolRelation, 3> Relations;
typedef RecursiveASTVisitor<TypeIndexer> base;
public:
TypeIndexer(IndexingContext &indexCtx, const NamedDecl *parent,
const DeclContext *DC, bool isBase, bool isIBType)
: IndexCtx(indexCtx), Parent(parent), ParentDC(DC), IsBase(isBase) {
if (IsBase) {
assert(Parent);
Relations.emplace_back((unsigned)SymbolRole::RelationBaseOf, Parent);
}
if (isIBType) {
assert(Parent);
Relations.emplace_back((unsigned)SymbolRole::RelationIBTypeOf, Parent);
}
}
bool shouldWalkTypesOfTypeLocs() const { return false; }
#define TRY_TO(CALL_EXPR) \
do { \
if (!CALL_EXPR) \
return false; \
} while (0)
bool VisitTypedefTypeLoc(TypedefTypeLoc TL) {
SourceLocation Loc = TL.getNameLoc();
TypedefNameDecl *ND = TL.getTypedefNameDecl();
if (ND->isTransparentTag()) {
TagDecl *Underlying = ND->getUnderlyingType()->getAsTagDecl();
return IndexCtx.handleReference(Underlying, Loc, Parent,
ParentDC, SymbolRoleSet(), Relations);
}
if (IsBase) {
TRY_TO(IndexCtx.handleReference(ND, Loc,
Parent, ParentDC, SymbolRoleSet()));
if (auto *CD = TL.getType()->getAsCXXRecordDecl()) {
TRY_TO(IndexCtx.handleReference(CD, Loc, Parent, ParentDC,
(unsigned)SymbolRole::Implicit,
Relations));
}
} else {
TRY_TO(IndexCtx.handleReference(ND, Loc,
Parent, ParentDC, SymbolRoleSet(),
Relations));
}
return true;
}
bool traverseParamVarHelper(ParmVarDecl *D) {
TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
if (D->getTypeSourceInfo())
TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
return true;
}
bool TraverseParmVarDecl(ParmVarDecl *D) {
// Avoid visiting default arguments from the definition that were already
// visited in the declaration.
// FIXME: A free function definition can have default arguments.
// Avoiding double visitaiton of default arguments should be handled by the
// visitor probably with a bit in the AST to indicate if the attached
// default argument was 'inherited' or written in source.
if (auto FD = dyn_cast<FunctionDecl>(D->getDeclContext())) {
if (FD->isThisDeclarationADefinition()) {
return traverseParamVarHelper(D);
}
}
return base::TraverseParmVarDecl(D);
}
bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
IndexCtx.indexNestedNameSpecifierLoc(NNS, Parent, ParentDC);
return true;
}
bool VisitTagTypeLoc(TagTypeLoc TL) {
TagDecl *D = TL.getDecl();
if (D->getParentFunctionOrMethod())
return true;
if (TL.isDefinition()) {
IndexCtx.indexTagDecl(D);
return true;
}
return IndexCtx.handleReference(D, TL.getNameLoc(),
Parent, ParentDC, SymbolRoleSet(),
Relations);
}
bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
return IndexCtx.handleReference(TL.getIFaceDecl(), TL.getNameLoc(),
Parent, ParentDC, SymbolRoleSet(), Relations);
}
bool VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) {
IndexCtx.handleReference(TL.getProtocol(i), TL.getProtocolLoc(i),
Parent, ParentDC, SymbolRoleSet(), Relations);
}
return true;
}
template<typename TypeLocType>
bool HandleTemplateSpecializationTypeLoc(TypeLocType TL) {
if (const auto *T = TL.getTypePtr()) {
if (IndexCtx.shouldIndexImplicitTemplateInsts()) {
if (CXXRecordDecl *RD = T->getAsCXXRecordDecl())
IndexCtx.handleReference(RD, TL.getTemplateNameLoc(),
Parent, ParentDC, SymbolRoleSet(), Relations);
} else {
if (const TemplateDecl *D = T->getTemplateName().getAsTemplateDecl())
IndexCtx.handleReference(D, TL.getTemplateNameLoc(),
Parent, ParentDC, SymbolRoleSet(), Relations);
}
}
return true;
}
bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
return HandleTemplateSpecializationTypeLoc(TL);
}
bool VisitDeducedTemplateSpecializationTypeLoc(DeducedTemplateSpecializationTypeLoc TL) {
return HandleTemplateSpecializationTypeLoc(TL);
}
bool VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
const DependentNameType *DNT = TL.getTypePtr();
const NestedNameSpecifier *NNS = DNT->getQualifier();
const Type *T = NNS->getAsType();
if (!T)
return true;
const TemplateSpecializationType *TST =
T->getAs<TemplateSpecializationType>();
if (!TST)
return true;
TemplateName TN = TST->getTemplateName();
const ClassTemplateDecl *TD =
dyn_cast_or_null<ClassTemplateDecl>(TN.getAsTemplateDecl());
if (!TD)
return true;
CXXRecordDecl *RD = TD->getTemplatedDecl();
if (!RD->hasDefinition())
return true;
RD = RD->getDefinition();
DeclarationName Name(DNT->getIdentifier());
std::vector<const NamedDecl *> Symbols = RD->lookupDependentName(
Name, [](const NamedDecl *ND) { return isa<TypeDecl>(ND); });
if (Symbols.size() != 1)
return true;
return IndexCtx.handleReference(Symbols[0], TL.getNameLoc(), Parent,
ParentDC, SymbolRoleSet(), Relations);
}
bool TraverseStmt(Stmt *S) {
IndexCtx.indexBody(S, Parent, ParentDC);
return true;
}
};
} // anonymous namespace
void IndexingContext::indexTypeSourceInfo(TypeSourceInfo *TInfo,
const NamedDecl *Parent,
const DeclContext *DC,
bool isBase,
bool isIBType) {
if (!TInfo || TInfo->getTypeLoc().isNull())
return;
indexTypeLoc(TInfo->getTypeLoc(), Parent, DC, isBase, isIBType);
}
void IndexingContext::indexTypeLoc(TypeLoc TL,
const NamedDecl *Parent,
const DeclContext *DC,
bool isBase,
bool isIBType) {
if (TL.isNull())
return;
if (!DC)
DC = Parent->getLexicalDeclContext();
TypeIndexer(*this, Parent, DC, isBase, isIBType).TraverseTypeLoc(TL);
}
void IndexingContext::indexNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
const NamedDecl *Parent,
const DeclContext *DC) {
if (!NNS)
return;
if (NestedNameSpecifierLoc Prefix = NNS.getPrefix())
indexNestedNameSpecifierLoc(Prefix, Parent, DC);
if (!DC)
DC = Parent->getLexicalDeclContext();
SourceLocation Loc = NNS.getLocalBeginLoc();
switch (NNS.getNestedNameSpecifier()->getKind()) {
case NestedNameSpecifier::Identifier:
case NestedNameSpecifier::Global:
case NestedNameSpecifier::Super:
break;
case NestedNameSpecifier::Namespace:
handleReference(NNS.getNestedNameSpecifier()->getAsNamespace(),
Loc, Parent, DC, SymbolRoleSet());
break;
case NestedNameSpecifier::NamespaceAlias:
handleReference(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(),
Loc, Parent, DC, SymbolRoleSet());
break;
case NestedNameSpecifier::TypeSpec:
case NestedNameSpecifier::TypeSpecWithTemplate:
indexTypeLoc(NNS.getTypeLoc(), Parent, DC);
break;
}
}
void IndexingContext::indexTagDecl(const TagDecl *D,
ArrayRef<SymbolRelation> Relations) {
if (!shouldIndex(D))
return;
if (!shouldIndexFunctionLocalSymbols() && isFunctionLocalSymbol(D))
return;
if (handleDecl(D, /*Roles=*/SymbolRoleSet(), Relations)) {
if (D->isThisDeclarationADefinition()) {
indexNestedNameSpecifierLoc(D->getQualifierLoc(), D);
if (auto CXXRD = dyn_cast<CXXRecordDecl>(D)) {
for (const auto &I : CXXRD->bases()) {
indexTypeSourceInfo(I.getTypeSourceInfo(), CXXRD, CXXRD, /*isBase=*/true);
}
}
indexDeclContext(D);
}
}
}

View File

@ -0,0 +1,211 @@
//===- IndexingAction.cpp - Frontend index action -------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "clang/Index/IndexingAction.h"
#include "IndexingContext.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Frontend/MultiplexConsumer.h"
#include "clang/Index/IndexDataConsumer.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Serialization/ASTReader.h"
using namespace clang;
using namespace clang::index;
void IndexDataConsumer::_anchor() {}
bool IndexDataConsumer::handleDeclOccurence(const Decl *D, SymbolRoleSet Roles,
ArrayRef<SymbolRelation> Relations,
FileID FID, unsigned Offset,
ASTNodeInfo ASTNode) {
return true;
}
bool IndexDataConsumer::handleMacroOccurence(const IdentifierInfo *Name,
const MacroInfo *MI, SymbolRoleSet Roles,
FileID FID, unsigned Offset) {
return true;
}
bool IndexDataConsumer::handleModuleOccurence(const ImportDecl *ImportD,
SymbolRoleSet Roles,
FileID FID, unsigned Offset) {
return true;
}
namespace {
class IndexASTConsumer : public ASTConsumer {
std::shared_ptr<Preprocessor> PP;
IndexingContext &IndexCtx;
public:
IndexASTConsumer(std::shared_ptr<Preprocessor> PP, IndexingContext &IndexCtx)
: PP(std::move(PP)), IndexCtx(IndexCtx) {}
protected:
void Initialize(ASTContext &Context) override {
IndexCtx.setASTContext(Context);
IndexCtx.getDataConsumer().initialize(Context);
IndexCtx.getDataConsumer().setPreprocessor(PP);
}
bool HandleTopLevelDecl(DeclGroupRef DG) override {
return IndexCtx.indexDeclGroupRef(DG);
}
void HandleInterestingDecl(DeclGroupRef DG) override {
// Ignore deserialized decls.
}
void HandleTopLevelDeclInObjCContainer(DeclGroupRef DG) override {
IndexCtx.indexDeclGroupRef(DG);
}
void HandleTranslationUnit(ASTContext &Ctx) override {
}
};
class IndexActionBase {
protected:
std::shared_ptr<IndexDataConsumer> DataConsumer;
IndexingContext IndexCtx;
IndexActionBase(std::shared_ptr<IndexDataConsumer> dataConsumer,
IndexingOptions Opts)
: DataConsumer(std::move(dataConsumer)),
IndexCtx(Opts, *DataConsumer) {}
std::unique_ptr<IndexASTConsumer>
createIndexASTConsumer(CompilerInstance &CI) {
return llvm::make_unique<IndexASTConsumer>(CI.getPreprocessorPtr(),
IndexCtx);
}
void finish() {
DataConsumer->finish();
}
};
class IndexAction : public ASTFrontendAction, IndexActionBase {
public:
IndexAction(std::shared_ptr<IndexDataConsumer> DataConsumer,
IndexingOptions Opts)
: IndexActionBase(std::move(DataConsumer), Opts) {}
protected:
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
StringRef InFile) override {
return createIndexASTConsumer(CI);
}
void EndSourceFileAction() override {
FrontendAction::EndSourceFileAction();
finish();
}
};
class WrappingIndexAction : public WrapperFrontendAction, IndexActionBase {
bool IndexActionFailed = false;
public:
WrappingIndexAction(std::unique_ptr<FrontendAction> WrappedAction,
std::shared_ptr<IndexDataConsumer> DataConsumer,
IndexingOptions Opts)
: WrapperFrontendAction(std::move(WrappedAction)),
IndexActionBase(std::move(DataConsumer), Opts) {}
protected:
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
StringRef InFile) override;
void EndSourceFileAction() override;
};
} // anonymous namespace
void WrappingIndexAction::EndSourceFileAction() {
// Invoke wrapped action's method.
WrapperFrontendAction::EndSourceFileAction();
if (!IndexActionFailed)
finish();
}
std::unique_ptr<ASTConsumer>
WrappingIndexAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
auto OtherConsumer = WrapperFrontendAction::CreateASTConsumer(CI, InFile);
if (!OtherConsumer) {
IndexActionFailed = true;
return nullptr;
}
std::vector<std::unique_ptr<ASTConsumer>> Consumers;
Consumers.push_back(std::move(OtherConsumer));
Consumers.push_back(createIndexASTConsumer(CI));
return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
}
std::unique_ptr<FrontendAction>
index::createIndexingAction(std::shared_ptr<IndexDataConsumer> DataConsumer,
IndexingOptions Opts,
std::unique_ptr<FrontendAction> WrappedAction) {
if (WrappedAction)
return llvm::make_unique<WrappingIndexAction>(std::move(WrappedAction),
std::move(DataConsumer),
Opts);
return llvm::make_unique<IndexAction>(std::move(DataConsumer), Opts);
}
static bool topLevelDeclVisitor(void *context, const Decl *D) {
IndexingContext &IndexCtx = *static_cast<IndexingContext*>(context);
return IndexCtx.indexTopLevelDecl(D);
}
static void indexTranslationUnit(ASTUnit &Unit, IndexingContext &IndexCtx) {
Unit.visitLocalTopLevelDecls(&IndexCtx, topLevelDeclVisitor);
}
void index::indexASTUnit(ASTUnit &Unit,
std::shared_ptr<IndexDataConsumer> DataConsumer,
IndexingOptions Opts) {
IndexingContext IndexCtx(Opts, *DataConsumer);
IndexCtx.setASTContext(Unit.getASTContext());
DataConsumer->initialize(Unit.getASTContext());
DataConsumer->setPreprocessor(Unit.getPreprocessorPtr());
indexTranslationUnit(Unit, IndexCtx);
DataConsumer->finish();
}
void index::indexTopLevelDecls(ASTContext &Ctx, ArrayRef<const Decl *> Decls,
std::shared_ptr<IndexDataConsumer> DataConsumer,
IndexingOptions Opts) {
IndexingContext IndexCtx(Opts, *DataConsumer);
IndexCtx.setASTContext(Ctx);
DataConsumer->initialize(Ctx);
for (const Decl *D : Decls)
IndexCtx.indexTopLevelDecl(D);
DataConsumer->finish();
}
void index::indexModuleFile(serialization::ModuleFile &Mod,
ASTReader &Reader,
std::shared_ptr<IndexDataConsumer> DataConsumer,
IndexingOptions Opts) {
ASTContext &Ctx = Reader.getContext();
IndexingContext IndexCtx(Opts, *DataConsumer);
IndexCtx.setASTContext(Ctx);
DataConsumer->initialize(Ctx);
for (const Decl *D : Reader.getModuleFileLevelDecls(Mod)) {
IndexCtx.indexTopLevelDecl(D);
}
DataConsumer->finish();
}

View File

@ -0,0 +1,420 @@
//===- IndexingContext.cpp - Indexing context data ------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "IndexingContext.h"
#include "clang/Index/IndexDataConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/DeclObjC.h"
#include "clang/Basic/SourceManager.h"
using namespace clang;
using namespace index;
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);
}
const LangOptions &IndexingContext::getLangOpts() const {
return Ctx->getLangOpts();
}
bool IndexingContext::shouldIndexFunctionLocalSymbols() const {
return IndexOpts.IndexFunctionLocals;
}
bool IndexingContext::handleDecl(const Decl *D,
SymbolRoleSet Roles,
ArrayRef<SymbolRelation> Relations) {
return handleDecl(D, D->getLocation(), Roles, Relations);
}
bool IndexingContext::handleDecl(const Decl *D, SourceLocation Loc,
SymbolRoleSet Roles,
ArrayRef<SymbolRelation> Relations,
const DeclContext *DC) {
if (!DC)
DC = D->getDeclContext();
const Decl *OrigD = D;
if (isa<ObjCPropertyImplDecl>(D)) {
D = cast<ObjCPropertyImplDecl>(D)->getPropertyDecl();
}
return handleDeclOccurrence(D, Loc, /*IsRef=*/false, cast<Decl>(DC),
Roles, Relations,
nullptr, OrigD, DC);
}
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) {
if (!shouldIndexFunctionLocalSymbols() && isFunctionLocalSymbol(D))
return true;
if (isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D))
return true;
return handleDeclOccurrence(D, Loc, /*IsRef=*/true, Parent, Roles, Relations,
RefE, RefD, DC);
}
bool IndexingContext::importedModule(const ImportDecl *ImportD) {
SourceLocation Loc;
auto IdLocs = ImportD->getIdentifierLocs();
if (!IdLocs.empty())
Loc = IdLocs.front();
else
Loc = ImportD->getLocation();
SourceManager &SM = Ctx->getSourceManager();
Loc = SM.getFileLoc(Loc);
if (Loc.isInvalid())
return true;
FileID FID;
unsigned Offset;
std::tie(FID, Offset) = SM.getDecomposedLoc(Loc);
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:
case IndexingOptions::SystemSymbolFilterKind::All:
break;
}
}
SymbolRoleSet Roles = (unsigned)SymbolRole::Declaration;
if (ImportD->isImplicit())
Roles |= (unsigned)SymbolRole::Implicit;
return DataConsumer.handleModuleOccurence(ImportD, Roles, FID, Offset);
}
bool IndexingContext::isTemplateImplicitInstantiation(const Decl *D) {
TemplateSpecializationKind TKind = TSK_Undeclared;
if (const ClassTemplateSpecializationDecl *
SD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
TKind = SD->getSpecializationKind();
} else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
TKind = FD->getTemplateSpecializationKind();
} else if (auto *VD = dyn_cast<VarDecl>(D)) {
TKind = VD->getTemplateSpecializationKind();
} else if (const auto *RD = dyn_cast<CXXRecordDecl>(D)) {
if (RD->getInstantiatedFromMemberClass())
TKind = RD->getTemplateSpecializationKind();
} else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
if (ED->getInstantiatedFromMemberEnum())
TKind = ED->getTemplateSpecializationKind();
} else if (isa<FieldDecl>(D) || isa<TypedefNameDecl>(D) ||
isa<EnumConstantDecl>(D)) {
if (const auto *Parent = dyn_cast<Decl>(D->getDeclContext()))
return isTemplateImplicitInstantiation(Parent);
}
switch (TKind) {
case TSK_Undeclared:
case TSK_ExplicitSpecialization:
return false;
case TSK_ImplicitInstantiation:
case TSK_ExplicitInstantiationDeclaration:
case TSK_ExplicitInstantiationDefinition:
return true;
}
llvm_unreachable("invalid TemplateSpecializationKind");
}
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;
}
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;
}
static const Decl *adjustTemplateImplicitInstantiation(const Decl *D) {
if (const ClassTemplateSpecializationDecl *
SD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
return SD->getTemplateInstantiationPattern();
} else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
return FD->getTemplateInstantiationPattern();
} else if (auto *VD = dyn_cast<VarDecl>(D)) {
return VD->getTemplateInstantiationPattern();
} else if (const auto *RD = dyn_cast<CXXRecordDecl>(D)) {
return RD->getInstantiatedFromMemberClass();
} else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
return ED->getInstantiatedFromMemberEnum();
} else if (isa<FieldDecl>(D) || isa<TypedefNameDecl>(D)) {
const auto *ND = cast<NamedDecl>(D);
if (const CXXRecordDecl *Pattern =
getDeclContextForTemplateInstationPattern(ND)) {
for (const NamedDecl *BaseND : Pattern->lookup(ND->getDeclName())) {
if (BaseND->isImplicit())
continue;
if (BaseND->getKind() == ND->getKind())
return BaseND;
}
}
} 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;
}
}
}
return nullptr;
}
static bool isDeclADefinition(const Decl *D, const DeclContext *ContainerDC, ASTContext &Ctx) {
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))
return MD->isThisDeclarationADefinition() || isa<ObjCImplDecl>(ContainerDC);
if (isa<TypedefNameDecl>(D) ||
isa<EnumConstantDecl>(D) ||
isa<FieldDecl>(D) ||
isa<MSPropertyDecl>(D) ||
isa<ObjCImplDecl>(D) ||
isa<ObjCPropertyImplDecl>(D))
return true;
return false;
}
/// Whether the given NamedDecl should be skipped because it has no name.
static bool shouldSkipNamelessDecl(const NamedDecl *ND) {
return (ND->getDeclName().isEmpty() && !isa<TagDecl>(ND) &&
!isa<ObjCCategoryDecl>(ND)) || isa<CXXDeductionGuideDecl>(ND);
}
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;
} else if (auto ND = dyn_cast<NamedDecl>(Parent)) {
if (shouldSkipNamelessDecl(ND))
continue;
}
return Parent;
}
}
static const Decl *getCanonicalDecl(const Decl *D) {
D = D->getCanonicalDecl();
if (auto TD = dyn_cast<TemplateDecl>(D)) {
if (auto TTD = TD->getTemplatedDecl()) {
D = TTD;
assert(D->isCanonicalDecl());
}
}
return D;
}
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:
case SymbolRole::RelationReceivedBy:
case SymbolRole::RelationCalledBy:
case SymbolRole::RelationContainedBy:
case SymbolRole::RelationSpecializationOf:
return true;
}
llvm_unreachable("Unsupported SymbolRole value!");
});
return accept;
};
for (auto &Rel : Relations) {
if (acceptForRelation(Rel.Roles))
return true;
}
return false;
}
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;
if (!isa<NamedDecl>(D) || shouldSkipNamelessDecl(cast<NamedDecl>(D)))
return true;
SourceManager &SM = Ctx->getSourceManager();
Loc = SM.getFileLoc(Loc);
if (Loc.isInvalid())
return true;
FileID FID;
unsigned Offset;
std::tie(FID, Offset) = SM.getDecomposedLoc(Loc);
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:
if (!shouldReportOccurrenceForSystemDeclOnlyMode(IsRef, Roles, Relations))
return true;
break;
case IndexingOptions::SystemSymbolFilterKind::All:
break;
}
}
if (isTemplateImplicitInstantiation(D)) {
if (!IsRef)
return true;
D = adjustTemplateImplicitInstantiation(D);
if (!D)
return true;
assert(!isTemplateImplicitInstantiation(D));
}
if (!OrigD)
OrigD = D;
if (IsRef)
Roles |= (unsigned)SymbolRole::Reference;
else if (isDeclADefinition(OrigD, ContainerDC, *Ctx))
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) {
auto It = std::find_if(FinalRelations.begin(), FinalRelations.end(),
[&](SymbolRelation Elem)->bool {
return Elem.RelatedSymbol == Rel.RelatedSymbol;
});
if (It != FinalRelations.end()) {
It->Roles |= Rel.Roles;
} else {
FinalRelations.push_back(Rel);
}
Roles |= Rel.Roles;
};
if (Parent) {
if (IsRef || (!isa<ParmVarDecl>(D) && isFunctionLocalSymbol(D))) {
addRelation(SymbolRelation{
(unsigned)SymbolRole::RelationContainedBy,
Parent
});
} else {
addRelation(SymbolRelation{
(unsigned)SymbolRole::RelationChildOf,
Parent
});
}
}
for (auto &Rel : Relations) {
addRelation(SymbolRelation(Rel.Roles,
Rel.RelatedSymbol->getCanonicalDecl()));
}
IndexDataConsumer::ASTNodeInfo Node{ OrigE, OrigD, Parent, ContainerDC };
return DataConsumer.handleDeclOccurence(D, Roles, FinalRelations, FID, Offset,
Node);
}

View File

@ -0,0 +1,127 @@
//===- IndexingContext.h - Indexing context data ----------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_LIB_INDEX_INDEXINGCONTEXT_H
#define LLVM_CLANG_LIB_INDEX_INDEXINGCONTEXT_H
#include "clang/Basic/LLVM.h"
#include "clang/Index/IndexSymbol.h"
#include "clang/Index/IndexingAction.h"
#include "llvm/ADT/ArrayRef.h"
namespace clang {
class ASTContext;
class Decl;
class DeclGroupRef;
class ImportDecl;
class TagDecl;
class TypeSourceInfo;
class NamedDecl;
class ObjCMethodDecl;
class DeclContext;
class NestedNameSpecifierLoc;
class Stmt;
class Expr;
class TypeLoc;
class SourceLocation;
namespace index {
class IndexDataConsumer;
class IndexingContext {
IndexingOptions IndexOpts;
IndexDataConsumer &DataConsumer;
ASTContext *Ctx = nullptr;
public:
IndexingContext(IndexingOptions IndexOpts, IndexDataConsumer &DataConsumer)
: IndexOpts(IndexOpts), DataConsumer(DataConsumer) {}
const IndexingOptions &getIndexOpts() const { return IndexOpts; }
IndexDataConsumer &getDataConsumer() { return DataConsumer; }
void setASTContext(ASTContext &ctx) { Ctx = &ctx; }
bool shouldIndex(const Decl *D);
const LangOptions &getLangOpts() const;
bool shouldSuppressRefs() const {
return false;
}
bool shouldIndexFunctionLocalSymbols() const;
bool shouldIndexImplicitTemplateInsts() const {
return false;
}
static bool isTemplateImplicitInstantiation(const Decl *D);
bool handleDecl(const Decl *D, SymbolRoleSet Roles = SymbolRoleSet(),
ArrayRef<SymbolRelation> Relations = None);
bool handleDecl(const Decl *D, SourceLocation Loc,
SymbolRoleSet Roles = SymbolRoleSet(),
ArrayRef<SymbolRelation> Relations = None,
const DeclContext *DC = nullptr);
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);
bool importedModule(const ImportDecl *ImportD);
bool indexDecl(const Decl *D);
void indexTagDecl(const TagDecl *D,
ArrayRef<SymbolRelation> Relations = None);
void indexTypeSourceInfo(TypeSourceInfo *TInfo, const NamedDecl *Parent,
const DeclContext *DC = nullptr,
bool isBase = false,
bool isIBType = false);
void indexTypeLoc(TypeLoc TL, const NamedDecl *Parent,
const DeclContext *DC = nullptr,
bool isBase = false,
bool isIBType = false);
void indexNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
const NamedDecl *Parent,
const DeclContext *DC = nullptr);
bool indexDeclContext(const DeclContext *DC);
void indexBody(const Stmt *S, const NamedDecl *Parent,
const DeclContext *DC = nullptr);
bool indexTopLevelDecl(const Decl *D);
bool indexDeclGroupRef(DeclGroupRef DG);
private:
bool shouldIgnoreIfImplicit(const Decl *D);
bool handleDeclOccurrence(const Decl *D, SourceLocation Loc,
bool IsRef, const Decl *Parent,
SymbolRoleSet Roles,
ArrayRef<SymbolRelation> Relations,
const Expr *RefE,
const Decl *RefD,
const DeclContext *ContainerDC);
};
} // end namespace index
} // end namespace clang
#endif

View File

@ -0,0 +1,75 @@
//===--- SimpleFormatContext.h ----------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
/// \file
///
/// \brief Defines a utility class for use of clang-format in libclang
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_LIB_INDEX_SIMPLEFORMATCONTEXT_H
#define LLVM_CLANG_LIB_INDEX_SIMPLEFORMATCONTEXT_H
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
namespace clang {
namespace index {
/// \brief A small class to be used by libclang clients to format
/// a declaration string in memory. This object is instantiated once
/// and used each time a formatting is needed.
class SimpleFormatContext {
public:
SimpleFormatContext(LangOptions Options)
: DiagOpts(new DiagnosticOptions()),
Diagnostics(new DiagnosticsEngine(new DiagnosticIDs,
DiagOpts.get())),
InMemoryFileSystem(new vfs::InMemoryFileSystem),
Files(FileSystemOptions(), InMemoryFileSystem),
Sources(*Diagnostics, Files),
Rewrite(Sources, Options) {
Diagnostics->setClient(new IgnoringDiagConsumer, true);
}
FileID createInMemoryFile(StringRef Name, StringRef Content) {
InMemoryFileSystem->addFile(Name, 0,
llvm::MemoryBuffer::getMemBuffer(Content));
const FileEntry *Entry = Files.getFile(Name);
assert(Entry != nullptr);
return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User);
}
std::string getRewrittenText(FileID ID) {
std::string Result;
llvm::raw_string_ostream OS(Result);
Rewrite.getEditBuffer(ID).write(OS);
OS.flush();
return Result;
}
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics;
IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem;
FileManager Files;
SourceManager Sources;
Rewriter Rewrite;
};
} // end namespace index
} // end namespace clang
#endif

File diff suppressed because it is too large Load Diff