2011-10-18 03:48:19 +08:00
|
|
|
//===- CIndexHigh.cpp - Higher level API functions ------------------------===//
|
|
|
|
//
|
|
|
|
// 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/DeclVisitor.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace cxindex;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class IndexingDeclVisitor : public DeclVisitor<IndexingDeclVisitor, bool> {
|
|
|
|
IndexingContext &IndexCtx;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit IndexingDeclVisitor(IndexingContext &indexCtx)
|
|
|
|
: IndexCtx(indexCtx) { }
|
|
|
|
|
2011-12-14 02:47:41 +08:00
|
|
|
void handleDeclarator(DeclaratorDecl *D, const NamedDecl *Parent = 0) {
|
|
|
|
if (!Parent) Parent = D;
|
2012-01-14 10:05:51 +08:00
|
|
|
|
2012-02-15 06:23:11 +08:00
|
|
|
if (!IndexCtx.shouldIndexFunctionLocalSymbols()) {
|
2012-01-14 10:05:51 +08:00
|
|
|
IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), Parent);
|
|
|
|
IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), Parent);
|
|
|
|
} else {
|
|
|
|
if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) {
|
|
|
|
IndexCtx.handleVar(Parm);
|
|
|
|
} else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
for (FunctionDecl::param_iterator
|
|
|
|
PI = FD->param_begin(), PE = FD->param_end(); PI != PE; ++PI) {
|
|
|
|
IndexCtx.handleVar(*PI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-14 02:47:41 +08:00
|
|
|
}
|
|
|
|
|
2012-02-29 01:50:28 +08:00
|
|
|
void handleObjCMethod(ObjCMethodDecl *D) {
|
|
|
|
IndexCtx.handleObjCMethod(D);
|
|
|
|
if (D->isImplicit())
|
|
|
|
return;
|
|
|
|
|
|
|
|
IndexCtx.indexTypeSourceInfo(D->getResultTypeSourceInfo(), D);
|
|
|
|
for (ObjCMethodDecl::param_iterator
|
|
|
|
I = D->param_begin(), E = D->param_end(); I != E; ++I)
|
|
|
|
handleDeclarator(*I, D);
|
|
|
|
|
|
|
|
if (D->isThisDeclarationADefinition()) {
|
|
|
|
const Stmt *Body = D->getBody();
|
|
|
|
if (Body) {
|
|
|
|
IndexCtx.indexBody(Body, D, D);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-18 03:48:19 +08:00
|
|
|
bool VisitFunctionDecl(FunctionDecl *D) {
|
|
|
|
IndexCtx.handleFunction(D);
|
2011-12-14 02:47:41 +08:00
|
|
|
handleDeclarator(D);
|
2012-01-24 00:58:36 +08:00
|
|
|
|
|
|
|
if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
|
|
|
|
// Constructor initializers.
|
|
|
|
for (CXXConstructorDecl::init_iterator I = Ctor->init_begin(),
|
|
|
|
E = Ctor->init_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
CXXCtorInitializer *Init = *I;
|
|
|
|
if (Init->isWritten()) {
|
|
|
|
IndexCtx.indexTypeSourceInfo(Init->getTypeSourceInfo(), D);
|
|
|
|
if (const FieldDecl *Member = Init->getAnyMember())
|
|
|
|
IndexCtx.handleReference(Member, Init->getMemberLocation(), D, D);
|
|
|
|
IndexCtx.indexBody(Init->getInit(), D, D);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-18 03:48:19 +08:00
|
|
|
if (D->isThisDeclarationADefinition()) {
|
|
|
|
const Stmt *Body = D->getBody();
|
|
|
|
if (Body) {
|
2011-12-14 02:47:41 +08:00
|
|
|
IndexCtx.indexBody(Body, D, D);
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitVarDecl(VarDecl *D) {
|
|
|
|
IndexCtx.handleVar(D);
|
2011-12-14 02:47:41 +08:00
|
|
|
handleDeclarator(D);
|
|
|
|
IndexCtx.indexBody(D->getInit(), D);
|
2011-10-18 03:48:19 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitFieldDecl(FieldDecl *D) {
|
|
|
|
IndexCtx.handleField(D);
|
2011-12-14 02:47:41 +08:00
|
|
|
handleDeclarator(D);
|
|
|
|
if (D->isBitField())
|
|
|
|
IndexCtx.indexBody(D->getBitWidth(), D);
|
|
|
|
else if (D->hasInClassInitializer())
|
|
|
|
IndexCtx.indexBody(D->getInClassInitializer(), D);
|
2011-10-18 03:48:19 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitEnumConstantDecl(EnumConstantDecl *D) {
|
|
|
|
IndexCtx.handleEnumerator(D);
|
2011-12-14 02:47:41 +08:00
|
|
|
IndexCtx.indexBody(D->getInitExpr(), D);
|
2011-10-18 03:48:19 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-22 15:24:51 +08:00
|
|
|
bool VisitTypedefDecl(TypedefNameDecl *D) {
|
|
|
|
IndexCtx.handleTypedefName(D);
|
2011-10-18 03:48:19 +08:00
|
|
|
IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitTagDecl(TagDecl *D) {
|
|
|
|
// Non-free standing tags are handled in indexTypeSourceInfo.
|
|
|
|
if (D->isFreeStanding())
|
|
|
|
IndexCtx.indexTagDecl(D);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
|
2011-11-11 08:23:36 +08:00
|
|
|
IndexCtx.handleObjCInterface(D);
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2011-12-28 06:43:10 +08:00
|
|
|
if (D->isThisDeclarationADefinition()) {
|
|
|
|
IndexCtx.indexTUDeclsInObjCContainer();
|
|
|
|
IndexCtx.indexDeclContext(D);
|
|
|
|
}
|
2011-10-18 03:48:19 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
|
2011-11-11 08:23:36 +08:00
|
|
|
IndexCtx.handleObjCProtocol(D);
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2012-01-02 05:23:57 +08:00
|
|
|
if (D->isThisDeclarationADefinition()) {
|
|
|
|
IndexCtx.indexTUDeclsInObjCContainer();
|
|
|
|
IndexCtx.indexDeclContext(D);
|
|
|
|
}
|
2011-10-18 03:48:19 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
|
2011-11-15 14:20:24 +08:00
|
|
|
const ObjCInterfaceDecl *Class = D->getClassInterface();
|
2011-11-24 04:27:26 +08:00
|
|
|
if (!Class)
|
|
|
|
return true;
|
|
|
|
|
2011-11-15 14:20:24 +08:00
|
|
|
if (Class->isImplicitInterfaceDecl())
|
|
|
|
IndexCtx.handleObjCInterface(Class);
|
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
IndexCtx.handleObjCImplementation(D);
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
IndexCtx.indexTUDeclsInObjCContainer();
|
|
|
|
IndexCtx.indexDeclContext(D);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
|
2011-11-11 08:23:36 +08:00
|
|
|
IndexCtx.handleObjCCategory(D);
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
IndexCtx.indexTUDeclsInObjCContainer();
|
|
|
|
IndexCtx.indexDeclContext(D);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
|
2011-11-24 04:27:26 +08:00
|
|
|
const ObjCCategoryDecl *Cat = D->getCategoryDecl();
|
|
|
|
if (!Cat)
|
2011-11-11 08:23:36 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
IndexCtx.handleObjCCategoryImpl(D);
|
|
|
|
|
2011-10-18 03:48:19 +08:00
|
|
|
IndexCtx.indexTUDeclsInObjCContainer();
|
|
|
|
IndexCtx.indexDeclContext(D);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitObjCMethodDecl(ObjCMethodDecl *D) {
|
2012-02-29 01:50:28 +08:00
|
|
|
// Methods associated with a property, even user-declared ones, are
|
|
|
|
// handled when we handle the property.
|
|
|
|
if (D->isSynthesized())
|
|
|
|
return true;
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2012-02-29 01:50:28 +08:00
|
|
|
handleObjCMethod(D);
|
2011-10-18 03:48:19 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
|
2012-02-29 01:50:28 +08:00
|
|
|
if (ObjCMethodDecl *MD = D->getGetterMethodDecl())
|
|
|
|
if (MD->getLexicalDeclContext() == D->getLexicalDeclContext())
|
|
|
|
handleObjCMethod(MD);
|
|
|
|
if (ObjCMethodDecl *MD = D->getSetterMethodDecl())
|
|
|
|
if (MD->getLexicalDeclContext() == D->getLexicalDeclContext())
|
|
|
|
handleObjCMethod(MD);
|
2011-10-18 03:48:19 +08:00
|
|
|
IndexCtx.handleObjCProperty(D);
|
|
|
|
IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D);
|
|
|
|
return true;
|
2011-11-18 08:26:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
|
|
|
|
ObjCPropertyDecl *PD = D->getPropertyDecl();
|
|
|
|
IndexCtx.handleSynthesizedObjCProperty(D);
|
|
|
|
|
|
|
|
if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
|
|
|
|
return true;
|
|
|
|
assert(D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize);
|
|
|
|
|
|
|
|
if (ObjCIvarDecl *IvarD = D->getPropertyIvarDecl()) {
|
|
|
|
if (!IvarD->getSynthesize())
|
|
|
|
IndexCtx.handleReference(IvarD, D->getPropertyIvarDeclLoc(), 0,
|
|
|
|
D->getDeclContext());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) {
|
|
|
|
if (MD->isSynthesized())
|
|
|
|
IndexCtx.handleSynthesizedObjCMethod(MD, D->getLocation());
|
|
|
|
}
|
|
|
|
if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) {
|
|
|
|
if (MD->isSynthesized())
|
|
|
|
IndexCtx.handleSynthesizedObjCMethod(MD, D->getLocation());
|
|
|
|
}
|
2011-11-22 15:24:51 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-07 13:52:06 +08:00
|
|
|
bool VisitNamespaceDecl(NamespaceDecl *D) {
|
|
|
|
IndexCtx.handleNamespace(D);
|
|
|
|
IndexCtx.indexDeclContext(D);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-11 04:10:48 +08:00
|
|
|
bool VisitUsingDecl(UsingDecl *D) {
|
|
|
|
// FIXME: Parent for the following is CXIdxEntity_Unexposed with no USR,
|
|
|
|
// we should do better.
|
|
|
|
|
|
|
|
IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), D);
|
|
|
|
for (UsingDecl::shadow_iterator
|
|
|
|
I = D->shadow_begin(), E = D->shadow_end(); I != E; ++I) {
|
|
|
|
IndexCtx.handleReference((*I)->getUnderlyingDecl(), D->getLocation(),
|
|
|
|
D, D->getLexicalDeclContext());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
|
|
|
|
// FIXME: Parent for the following is CXIdxEntity_Unexposed with no USR,
|
|
|
|
// we should do better.
|
|
|
|
|
|
|
|
IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), D);
|
|
|
|
IndexCtx.handleReference(D->getNominatedNamespaceAsWritten(),
|
|
|
|
D->getLocation(), D, D->getLexicalDeclContext());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-22 15:24:51 +08:00
|
|
|
bool VisitClassTemplateDecl(ClassTemplateDecl *D) {
|
|
|
|
IndexCtx.handleClassTemplate(D);
|
|
|
|
if (D->isThisDeclarationADefinition())
|
|
|
|
IndexCtx.indexDeclContext(D->getTemplatedDecl());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-11 04:10:44 +08:00
|
|
|
bool VisitClassTemplateSpecializationDecl(
|
|
|
|
ClassTemplateSpecializationDecl *D) {
|
2012-02-15 06:23:11 +08:00
|
|
|
// FIXME: Notify subsequent callbacks if info comes from implicit
|
2012-02-11 04:10:44 +08:00
|
|
|
// instantiation.
|
2012-02-15 06:23:11 +08:00
|
|
|
if (D->isThisDeclarationADefinition() &&
|
|
|
|
(IndexCtx.shouldIndexImplicitTemplateInsts() ||
|
|
|
|
!IndexCtx.isTemplateImplicitInstantiation(D)))
|
2012-02-11 04:10:44 +08:00
|
|
|
IndexCtx.indexTagDecl(D);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-22 15:24:51 +08:00
|
|
|
bool VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
|
|
|
|
IndexCtx.handleFunctionTemplate(D);
|
|
|
|
FunctionDecl *FD = D->getTemplatedDecl();
|
2011-12-14 02:47:41 +08:00
|
|
|
handleDeclarator(FD, D);
|
2011-11-22 15:24:51 +08:00
|
|
|
if (FD->isThisDeclarationADefinition()) {
|
|
|
|
const Stmt *Body = FD->getBody();
|
|
|
|
if (Body) {
|
2011-12-14 02:47:41 +08:00
|
|
|
IndexCtx.indexBody(Body, D, FD);
|
2011-11-22 15:24:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2011-11-18 08:26:51 +08:00
|
|
|
|
2011-11-22 15:24:51 +08:00
|
|
|
bool VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
|
|
|
|
IndexCtx.handleTypeAliasTemplate(D);
|
|
|
|
IndexCtx.indexTypeSourceInfo(D->getTemplatedDecl()->getTypeSourceInfo(), D);
|
2011-11-18 08:26:51 +08:00
|
|
|
return true;
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
void IndexingContext::indexDecl(const Decl *D) {
|
2012-02-08 06:46:16 +08:00
|
|
|
if (D->isImplicit() && shouldIgnoreIfImplicit(D))
|
|
|
|
return;
|
|
|
|
|
2011-10-18 03:48:19 +08:00
|
|
|
bool Handled = IndexingDeclVisitor(*this).Visit(const_cast<Decl*>(D));
|
|
|
|
if (!Handled && isa<DeclContext>(D))
|
|
|
|
indexDeclContext(cast<DeclContext>(D));
|
|
|
|
}
|
|
|
|
|
|
|
|
void IndexingContext::indexDeclContext(const DeclContext *DC) {
|
|
|
|
for (DeclContext::decl_iterator
|
|
|
|
I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) {
|
|
|
|
indexDecl(*I);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-15 14:20:16 +08:00
|
|
|
void IndexingContext::indexTopLevelDecl(Decl *D) {
|
|
|
|
if (isNotFromSourceFile(D->getLocation()))
|
|
|
|
return;
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2011-11-15 14:20:16 +08:00
|
|
|
if (isa<ObjCMethodDecl>(D))
|
|
|
|
return; // Wait for the objc container.
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2011-11-15 14:20:16 +08:00
|
|
|
indexDecl(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IndexingContext::indexDeclGroupRef(DeclGroupRef DG) {
|
|
|
|
for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
|
|
|
|
indexTopLevelDecl(*I);
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void IndexingContext::indexTUDeclsInObjCContainer() {
|
|
|
|
for (unsigned i = 0, e = TUDeclsInObjCContainer.size(); i != e; ++i)
|
|
|
|
indexDeclGroupRef(TUDeclsInObjCContainer[i]);
|
|
|
|
TUDeclsInObjCContainer.clear();
|
|
|
|
}
|