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 "CXTranslationUnit.h"
|
|
|
|
#include "CIndexDiagnostic.h"
|
|
|
|
|
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
|
|
|
#include "clang/AST/DeclObjC.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace cxindex;
|
|
|
|
using namespace cxcursor;
|
|
|
|
|
2011-11-12 10:16:30 +08:00
|
|
|
IndexingContext::ObjCProtocolListInfo::ObjCProtocolListInfo(
|
|
|
|
const ObjCProtocolList &ProtList,
|
|
|
|
IndexingContext &IdxCtx,
|
|
|
|
StrAdapter &SA) {
|
|
|
|
ObjCInterfaceDecl::protocol_loc_iterator LI = ProtList.loc_begin();
|
|
|
|
for (ObjCInterfaceDecl::protocol_iterator
|
|
|
|
I = ProtList.begin(), E = ProtList.end(); I != E; ++I, ++LI) {
|
|
|
|
SourceLocation Loc = *LI;
|
|
|
|
ObjCProtocolDecl *PD = *I;
|
|
|
|
ProtEntities.push_back(CXIdxEntityInfo());
|
|
|
|
IdxCtx.getEntityInfo(PD, ProtEntities.back(), SA);
|
|
|
|
CXIdxObjCProtocolRefInfo ProtInfo = { 0,
|
|
|
|
MakeCursorObjCProtocolRef(PD, Loc, IdxCtx.CXTU),
|
|
|
|
IdxCtx.getIndexLoc(Loc) };
|
|
|
|
ProtInfos.push_back(ProtInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = ProtInfos.size(); i != e; ++i)
|
|
|
|
ProtInfos[i].protocol = &ProtEntities[i];
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = ProtInfos.size(); i != e; ++i)
|
|
|
|
Prots.push_back(&ProtInfos[i]);
|
|
|
|
}
|
|
|
|
|
2011-10-18 03:48:19 +08:00
|
|
|
const char *IndexingContext::StrAdapter::toCStr(StringRef Str) {
|
|
|
|
if (Str.empty())
|
|
|
|
return "";
|
|
|
|
if (Str.data()[Str.size()] == '\0')
|
|
|
|
return Str.data();
|
|
|
|
Scratch += Str;
|
|
|
|
Scratch.push_back('\0');
|
|
|
|
return Scratch.data() + (Scratch.size() - Str.size() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IndexingContext::setASTContext(ASTContext &ctx) {
|
|
|
|
Ctx = &ctx;
|
|
|
|
static_cast<ASTUnit*>(CXTU->TUData)->setASTContext(&ctx);
|
|
|
|
}
|
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
void IndexingContext::enteredMainFile(const FileEntry *File) {
|
|
|
|
if (File && CB.enteredMainFile) {
|
|
|
|
CXIdxClientFile idxFile = CB.enteredMainFile(ClientData, (CXFile)File, 0);
|
|
|
|
FileMap[File] = idxFile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-18 03:48:19 +08:00
|
|
|
void IndexingContext::ppIncludedFile(SourceLocation hashLoc,
|
|
|
|
StringRef filename,
|
|
|
|
const FileEntry *File,
|
|
|
|
bool isImport, bool isAngled) {
|
|
|
|
if (!CB.ppIncludedFile)
|
|
|
|
return;
|
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
StrAdapter SA(*this);
|
2011-10-18 03:48:19 +08:00
|
|
|
CXIdxIncludedFileInfo Info = { getIndexLoc(hashLoc),
|
|
|
|
SA.toCStr(filename),
|
2011-11-11 08:23:36 +08:00
|
|
|
(CXFile)File,
|
2011-10-18 03:48:19 +08:00
|
|
|
isImport, isAngled };
|
2011-11-11 08:23:36 +08:00
|
|
|
CXIdxClientFile idxFile = CB.ppIncludedFile(ClientData, &Info);
|
|
|
|
FileMap[File] = idxFile;
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
2011-11-12 10:16:30 +08:00
|
|
|
void IndexingContext::startedTranslationUnit() {
|
2011-11-11 08:23:36 +08:00
|
|
|
CXIdxClientContainer idxCont = 0;
|
2011-10-18 03:48:19 +08:00
|
|
|
if (CB.startedTranslationUnit)
|
|
|
|
idxCont = CB.startedTranslationUnit(ClientData, 0);
|
|
|
|
addContainerInMap(Ctx->getTranslationUnitDecl(), idxCont);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IndexingContext::handleDiagnostic(const StoredDiagnostic &StoredDiag) {
|
|
|
|
if (!CB.diagnostic)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CXStoredDiagnostic CXDiag(StoredDiag, Ctx->getLangOptions());
|
|
|
|
CB.diagnostic(ClientData, &CXDiag, 0);
|
|
|
|
}
|
|
|
|
|
2011-11-15 14:20:16 +08:00
|
|
|
void IndexingContext::handleDiagnostic(CXDiagnostic CXDiag) {
|
|
|
|
if (!CB.diagnostic)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CB.diagnostic(ClientData, CXDiag, 0);
|
|
|
|
}
|
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
void IndexingContext::handleDecl(const NamedDecl *D,
|
|
|
|
SourceLocation Loc, CXCursor Cursor,
|
|
|
|
DeclInfo &DInfo) {
|
|
|
|
if (!CB.indexDeclaration)
|
|
|
|
return;
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
StrAdapter SA(*this);
|
|
|
|
getEntityInfo(D, DInfo.CXEntInfo, SA);
|
|
|
|
DInfo.entityInfo = &DInfo.CXEntInfo;
|
|
|
|
DInfo.cursor = Cursor;
|
|
|
|
DInfo.loc = getIndexLoc(Loc);
|
|
|
|
DInfo.container = getIndexContainer(D);
|
2011-11-15 06:39:19 +08:00
|
|
|
DInfo.isImplicit = D->isImplicit();
|
2011-11-11 08:23:36 +08:00
|
|
|
|
2011-11-12 10:16:30 +08:00
|
|
|
CXIdxClientContainer clientCont = 0;
|
2011-11-15 06:39:19 +08:00
|
|
|
CXIdxDeclOut DeclOut = { DInfo.isContainer ? &clientCont : 0 };
|
2011-11-12 10:16:30 +08:00
|
|
|
CB.indexDeclaration(ClientData, &DInfo, &DeclOut);
|
2011-11-11 08:23:36 +08:00
|
|
|
|
2011-11-15 06:39:19 +08:00
|
|
|
if (DInfo.isContainer)
|
2011-11-12 10:16:30 +08:00
|
|
|
addContainerInMap(cast<DeclContext>(D), clientCont);
|
2011-11-11 08:23:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void IndexingContext::handleObjCContainer(const ObjCContainerDecl *D,
|
|
|
|
SourceLocation Loc, CXCursor Cursor,
|
|
|
|
ObjCContainerDeclInfo &ContDInfo) {
|
2011-11-12 10:16:30 +08:00
|
|
|
ContDInfo.ObjCContDeclInfo.declInfo = &ContDInfo;
|
2011-11-15 06:39:19 +08:00
|
|
|
handleDecl(D, Loc, Cursor, ContDInfo);
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
void IndexingContext::handleFunction(const FunctionDecl *D) {
|
2011-11-15 06:39:19 +08:00
|
|
|
DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(),
|
|
|
|
D->isThisDeclarationADefinition());
|
|
|
|
handleDecl(D, D->getLocation(), getCursor(D), DInfo);
|
2011-11-11 08:23:36 +08:00
|
|
|
}
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
void IndexingContext::handleVar(const VarDecl *D) {
|
2011-11-15 06:39:19 +08:00
|
|
|
DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(),
|
|
|
|
/*isContainer=*/false);
|
|
|
|
handleDecl(D, D->getLocation(), getCursor(D), DInfo);
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void IndexingContext::handleField(const FieldDecl *D) {
|
2011-11-15 06:39:19 +08:00
|
|
|
DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true,
|
|
|
|
/*isContainer=*/false);
|
|
|
|
handleDecl(D, D->getLocation(), getCursor(D), DInfo);
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void IndexingContext::handleEnumerator(const EnumConstantDecl *D) {
|
2011-11-15 06:39:19 +08:00
|
|
|
DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true,
|
|
|
|
/*isContainer=*/false);
|
|
|
|
handleDecl(D, D->getLocation(), getCursor(D), DInfo);
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void IndexingContext::handleTagDecl(const TagDecl *D) {
|
2011-11-15 06:39:19 +08:00
|
|
|
DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(),
|
|
|
|
D->isThisDeclarationADefinition());
|
|
|
|
handleDecl(D, D->getLocation(), getCursor(D), DInfo);
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void IndexingContext::handleTypedef(const TypedefDecl *D) {
|
2011-11-15 06:39:19 +08:00
|
|
|
DeclInfo DInfo(!D->isFirstDeclaration(), /*isDefinition=*/true,
|
|
|
|
/*isContainer=*/false);
|
|
|
|
handleDecl(D, D->getLocation(), getCursor(D), DInfo);
|
2011-11-11 08:23:36 +08:00
|
|
|
}
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
void IndexingContext::handleObjCClass(const ObjCClassDecl *D) {
|
|
|
|
const ObjCClassDecl::ObjCClassRef *Ref = D->getForwardDecl();
|
|
|
|
ObjCInterfaceDecl *IFaceD = Ref->getInterface();
|
|
|
|
SourceLocation Loc = Ref->getLocation();
|
|
|
|
bool isRedeclaration = IFaceD->getLocation() != Loc;
|
2011-11-15 06:39:19 +08:00
|
|
|
|
|
|
|
ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true, isRedeclaration,
|
|
|
|
/*isImplementation=*/false);
|
2011-11-11 08:23:36 +08:00
|
|
|
handleObjCContainer(IFaceD, Loc, MakeCursorObjCClassRef(IFaceD, Loc, CXTU),
|
2011-11-15 06:39:19 +08:00
|
|
|
ContDInfo);
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void IndexingContext::handleObjCInterface(const ObjCInterfaceDecl *D) {
|
2011-11-12 10:16:30 +08:00
|
|
|
StrAdapter SA(*this);
|
|
|
|
|
|
|
|
CXIdxBaseClassInfo BaseClass;
|
|
|
|
CXIdxEntityInfo BaseEntity;
|
|
|
|
BaseClass.cursor = clang_getNullCursor();
|
|
|
|
if (ObjCInterfaceDecl *SuperD = D->getSuperClass()) {
|
|
|
|
getEntityInfo(SuperD, BaseEntity, SA);
|
|
|
|
SourceLocation SuperLoc = D->getSuperClassLoc();
|
|
|
|
BaseClass.base = &BaseEntity;
|
|
|
|
BaseClass.cursor = MakeCursorObjCSuperClassRef(SuperD, SuperLoc, CXTU);
|
|
|
|
BaseClass.loc = getIndexLoc(SuperLoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjCProtocolListInfo ProtInfo(D->getReferencedProtocols(), *this, SA);
|
|
|
|
|
2011-11-15 06:39:19 +08:00
|
|
|
ObjCInterfaceDeclInfo InterInfo(D);
|
|
|
|
InterInfo.ObjCProtoListInfo = ProtInfo.getListInfo();
|
|
|
|
InterInfo.ObjCInterDeclInfo.containerInfo = &InterInfo.ObjCContDeclInfo;
|
2011-11-12 10:16:30 +08:00
|
|
|
InterInfo.ObjCInterDeclInfo.superInfo = D->getSuperClass() ? &BaseClass : 0;
|
2011-11-15 06:39:19 +08:00
|
|
|
InterInfo.ObjCInterDeclInfo.protocols = &InterInfo.ObjCProtoListInfo;
|
2011-11-12 10:16:30 +08:00
|
|
|
|
2011-11-15 06:39:19 +08:00
|
|
|
handleObjCContainer(D, D->getLocation(), getCursor(D), InterInfo);
|
2011-11-11 08:23:36 +08:00
|
|
|
}
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
void IndexingContext::handleObjCImplementation(
|
|
|
|
const ObjCImplementationDecl *D) {
|
|
|
|
const ObjCInterfaceDecl *Class = D->getClassInterface();
|
2011-11-15 06:39:19 +08:00
|
|
|
ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/false,
|
2011-11-11 08:23:36 +08:00
|
|
|
/*isRedeclaration=*/!Class->isImplicitInterfaceDecl(),
|
2011-11-15 06:39:19 +08:00
|
|
|
/*isImplementation=*/true);
|
|
|
|
handleObjCContainer(D, D->getLocation(), getCursor(D), ContDInfo);
|
2011-11-11 08:23:36 +08:00
|
|
|
}
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
void IndexingContext::handleObjCForwardProtocol(const ObjCProtocolDecl *D,
|
|
|
|
SourceLocation Loc,
|
|
|
|
bool isRedeclaration) {
|
2011-11-15 06:39:19 +08:00
|
|
|
ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true,
|
|
|
|
isRedeclaration,
|
|
|
|
/*isImplementation=*/false);
|
2011-11-11 08:23:36 +08:00
|
|
|
handleObjCContainer(D, Loc, MakeCursorObjCProtocolRef(D, Loc, CXTU),
|
2011-11-15 06:39:19 +08:00
|
|
|
ContDInfo);
|
2011-11-11 08:23:36 +08:00
|
|
|
}
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
void IndexingContext::handleObjCProtocol(const ObjCProtocolDecl *D) {
|
|
|
|
StrAdapter SA(*this);
|
2011-11-12 10:16:30 +08:00
|
|
|
ObjCProtocolListInfo ProtListInfo(D->getReferencedProtocols(), *this, SA);
|
2011-11-11 08:23:36 +08:00
|
|
|
|
2011-11-15 06:39:19 +08:00
|
|
|
ObjCProtocolDeclInfo ProtInfo(D);
|
|
|
|
ProtInfo.ObjCProtoRefListInfo = ProtListInfo.getListInfo();
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2011-11-15 06:39:19 +08:00
|
|
|
handleObjCContainer(D, D->getLocation(), getCursor(D), ProtInfo);
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void IndexingContext::handleObjCCategory(const ObjCCategoryDecl *D) {
|
2011-11-15 06:39:19 +08:00
|
|
|
ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/false);
|
2011-11-11 08:23:36 +08:00
|
|
|
CXIdxEntityInfo ClassEntity;
|
|
|
|
StrAdapter SA(*this);
|
2011-11-15 14:20:16 +08:00
|
|
|
const ObjCInterfaceDecl *IFaceD = D->getClassInterface();
|
|
|
|
SourceLocation ClassLoc = D->getLocation();
|
|
|
|
SourceLocation CategoryLoc = D->getCategoryNameLoc();
|
2011-11-11 08:23:36 +08:00
|
|
|
getEntityInfo(D->getClassInterface(), ClassEntity, SA);
|
|
|
|
|
2011-11-12 10:16:30 +08:00
|
|
|
CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo;
|
|
|
|
CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity;
|
2011-11-15 14:20:16 +08:00
|
|
|
CatDInfo.ObjCCatDeclInfo.classCursor =
|
|
|
|
MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU);
|
|
|
|
CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc);
|
|
|
|
handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo);
|
2011-11-11 08:23:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void IndexingContext::handleObjCCategoryImpl(const ObjCCategoryImplDecl *D) {
|
|
|
|
const ObjCCategoryDecl *CatD = D->getCategoryDecl();
|
2011-11-15 06:39:19 +08:00
|
|
|
ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/true);
|
2011-11-11 08:23:36 +08:00
|
|
|
CXIdxEntityInfo ClassEntity;
|
|
|
|
StrAdapter SA(*this);
|
|
|
|
getEntityInfo(CatD->getClassInterface(), ClassEntity, SA);
|
|
|
|
|
2011-11-12 10:16:30 +08:00
|
|
|
CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo;
|
|
|
|
CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity;
|
2011-11-15 06:39:19 +08:00
|
|
|
handleObjCContainer(D, D->getLocation(), getCursor(D), CatDInfo);
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void IndexingContext::handleObjCMethod(const ObjCMethodDecl *D) {
|
2011-11-15 06:39:19 +08:00
|
|
|
DeclInfo DInfo(!D->isCanonicalDecl(), D->isThisDeclarationADefinition(),
|
|
|
|
D->isThisDeclarationADefinition());
|
|
|
|
handleDecl(D, D->getLocation(), getCursor(D), DInfo);
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void IndexingContext::handleObjCProperty(const ObjCPropertyDecl *D) {
|
2011-11-15 06:39:19 +08:00
|
|
|
DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/false,
|
|
|
|
/*isContainer=*/false);
|
|
|
|
handleDecl(D, D->getLocation(), getCursor(D), DInfo);
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc,
|
|
|
|
const NamedDecl *Parent,
|
|
|
|
const DeclContext *DC,
|
2011-10-18 23:50:50 +08:00
|
|
|
const Expr *E,
|
|
|
|
CXIdxEntityRefKind Kind) {
|
2011-10-18 03:48:19 +08:00
|
|
|
if (Loc.isInvalid())
|
|
|
|
return;
|
|
|
|
if (!CB.indexEntityReference)
|
|
|
|
return;
|
|
|
|
if (isNotFromSourceFile(D->getLocation()))
|
|
|
|
return;
|
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
StrAdapter SA(*this);
|
2011-10-28 21:37:11 +08:00
|
|
|
CXCursor Cursor = E ? MakeCXCursor(const_cast<Expr*>(E),
|
|
|
|
const_cast<Decl*>(cast<Decl>(DC)), CXTU)
|
|
|
|
: getRefCursor(D, Loc);
|
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
CXIdxEntityInfo RefEntity, ParentEntity;
|
|
|
|
getEntityInfo(D, RefEntity, SA);
|
|
|
|
getEntityInfo(Parent, ParentEntity, SA);
|
2011-10-28 21:37:11 +08:00
|
|
|
CXIdxEntityRefInfo Info = { Cursor,
|
2011-10-18 03:48:19 +08:00
|
|
|
getIndexLoc(Loc),
|
2011-11-11 08:23:36 +08:00
|
|
|
&RefEntity,
|
|
|
|
&ParentEntity,
|
2011-10-18 23:50:50 +08:00
|
|
|
getIndexContainerForDC(DC),
|
|
|
|
Kind };
|
2011-10-18 03:48:19 +08:00
|
|
|
CB.indexEntityReference(ClientData, &Info);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IndexingContext::isNotFromSourceFile(SourceLocation Loc) const {
|
|
|
|
if (Loc.isInvalid())
|
|
|
|
return true;
|
|
|
|
SourceManager &SM = Ctx->getSourceManager();
|
|
|
|
SourceLocation FileLoc = SM.getFileLoc(Loc);
|
|
|
|
FileID FID = SM.getFileID(FileLoc);
|
|
|
|
return SM.getFileEntryForID(FID) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IndexingContext::addContainerInMap(const DeclContext *DC,
|
2011-11-11 08:23:36 +08:00
|
|
|
CXIdxClientContainer container) {
|
2011-10-18 03:48:19 +08:00
|
|
|
assert(getScopedContext(DC) == DC);
|
|
|
|
ContainerMapTy::iterator I = ContainerMap.find(DC);
|
|
|
|
if (I == ContainerMap.end()) {
|
|
|
|
if (container)
|
|
|
|
ContainerMap[DC] = container;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Allow changing the container of a previously seen DeclContext so we
|
|
|
|
// can handle invalid user code, like a function re-definition.
|
|
|
|
if (container)
|
|
|
|
I->second = container;
|
|
|
|
else
|
|
|
|
ContainerMap.erase(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
const NamedDecl *IndexingContext::getEntityDecl(const NamedDecl *D) const {
|
|
|
|
assert(D);
|
|
|
|
D = cast<NamedDecl>(D->getCanonicalDecl());
|
|
|
|
|
|
|
|
if (const ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(D)) {
|
|
|
|
if (Cat->IsClassExtension())
|
|
|
|
return getEntityDecl(Cat->getClassInterface());
|
|
|
|
|
|
|
|
} else if (const ObjCImplementationDecl *
|
|
|
|
ImplD = dyn_cast<ObjCImplementationDecl>(D)) {
|
|
|
|
return getEntityDecl(ImplD->getClassInterface());
|
|
|
|
|
|
|
|
} else if (const ObjCCategoryImplDecl *
|
|
|
|
CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) {
|
|
|
|
return getEntityDecl(CatImplD->getCategoryDecl());
|
|
|
|
}
|
|
|
|
|
|
|
|
return D;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DeclContext *
|
|
|
|
IndexingContext::getScopedContext(const DeclContext *DC) const {
|
|
|
|
// Local contexts are ignored for indexing.
|
|
|
|
const DeclContext *FuncCtx = cast<Decl>(DC)->getParentFunctionOrMethod();
|
|
|
|
if (FuncCtx)
|
|
|
|
return FuncCtx;
|
|
|
|
|
|
|
|
// We consider enums always scoped for indexing.
|
|
|
|
if (isa<TagDecl>(DC))
|
|
|
|
return DC;
|
|
|
|
|
|
|
|
if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
|
|
|
|
if (NS->isAnonymousNamespace())
|
|
|
|
return getScopedContext(NS->getParent());
|
|
|
|
return NS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DC->getRedeclContext();
|
|
|
|
}
|
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
CXIdxClientContainer
|
2011-10-18 03:48:19 +08:00
|
|
|
IndexingContext::getIndexContainerForDC(const DeclContext *DC) const {
|
|
|
|
DC = getScopedContext(DC);
|
|
|
|
ContainerMapTy::const_iterator I = ContainerMap.find(DC);
|
|
|
|
// assert(I != ContainerMap.end() &&
|
|
|
|
// "Failed to include a scoped context in the container map");
|
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
CXIdxClientFile IndexingContext::getIndexFile(const FileEntry *File) {
|
2011-10-18 03:48:19 +08:00
|
|
|
if (!File)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
FileMapTy::iterator FI = FileMap.find(File);
|
|
|
|
if (FI != FileMap.end())
|
|
|
|
return FI->second;
|
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
return 0;
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CXIdxLoc IndexingContext::getIndexLoc(SourceLocation Loc) const {
|
|
|
|
CXIdxLoc idxLoc = { {0, 0}, 0 };
|
|
|
|
if (Loc.isInvalid())
|
|
|
|
return idxLoc;
|
|
|
|
|
|
|
|
idxLoc.ptr_data[0] = (void*)this;
|
|
|
|
idxLoc.int_data = Loc.getRawEncoding();
|
|
|
|
return idxLoc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IndexingContext::translateLoc(SourceLocation Loc,
|
2011-11-11 08:23:36 +08:00
|
|
|
CXIdxClientFile *indexFile, CXFile *file,
|
2011-10-18 03:48:19 +08:00
|
|
|
unsigned *line, unsigned *column,
|
|
|
|
unsigned *offset) {
|
|
|
|
if (Loc.isInvalid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SourceManager &SM = Ctx->getSourceManager();
|
|
|
|
Loc = SM.getFileLoc(Loc);
|
|
|
|
|
|
|
|
std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
|
|
|
|
FileID FID = LocInfo.first;
|
|
|
|
unsigned FileOffset = LocInfo.second;
|
|
|
|
|
|
|
|
if (FID.isInvalid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const FileEntry *FE = SM.getFileEntryForID(FID);
|
|
|
|
if (indexFile)
|
|
|
|
*indexFile = getIndexFile(FE);
|
|
|
|
if (file)
|
|
|
|
*file = (void *)FE;
|
|
|
|
if (line)
|
|
|
|
*line = SM.getLineNumber(FID, FileOffset);
|
|
|
|
if (column)
|
|
|
|
*column = SM.getColumnNumber(FID, FileOffset);
|
|
|
|
if (offset)
|
|
|
|
*offset = FileOffset;
|
|
|
|
}
|
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
void IndexingContext::getEntityInfo(const NamedDecl *D,
|
|
|
|
CXIdxEntityInfo &EntityInfo,
|
|
|
|
StrAdapter &SA) {
|
|
|
|
D = getEntityDecl(D);
|
|
|
|
EntityInfo.kind = CXIdxEntity_Unexposed;
|
|
|
|
|
|
|
|
if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
|
|
|
|
switch (TD->getTagKind()) {
|
|
|
|
case TTK_Struct:
|
|
|
|
EntityInfo.kind = CXIdxEntity_Struct; break;
|
|
|
|
case TTK_Union:
|
|
|
|
EntityInfo.kind = CXIdxEntity_Union; break;
|
|
|
|
case TTK_Class:
|
|
|
|
EntityInfo.kind = CXIdxEntity_CXXClass; break;
|
|
|
|
case TTK_Enum:
|
|
|
|
EntityInfo.kind = CXIdxEntity_Enum; break;
|
|
|
|
}
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
} else {
|
|
|
|
switch (D->getKind()) {
|
|
|
|
case Decl::Typedef:
|
|
|
|
EntityInfo.kind = CXIdxEntity_Typedef; break;
|
|
|
|
case Decl::Function:
|
|
|
|
EntityInfo.kind = CXIdxEntity_Function; break;
|
|
|
|
case Decl::Var:
|
|
|
|
EntityInfo.kind = CXIdxEntity_Variable; break;
|
|
|
|
case Decl::Field:
|
|
|
|
EntityInfo.kind = CXIdxEntity_Field; break;
|
|
|
|
case Decl::EnumConstant:
|
|
|
|
EntityInfo.kind = CXIdxEntity_EnumConstant; break;
|
|
|
|
case Decl::ObjCInterface:
|
|
|
|
EntityInfo.kind = CXIdxEntity_ObjCClass; break;
|
|
|
|
case Decl::ObjCProtocol:
|
|
|
|
EntityInfo.kind = CXIdxEntity_ObjCProtocol; break;
|
|
|
|
case Decl::ObjCCategory:
|
|
|
|
EntityInfo.kind = CXIdxEntity_ObjCCategory; break;
|
|
|
|
case Decl::ObjCMethod:
|
2011-11-15 06:39:19 +08:00
|
|
|
if (cast<ObjCMethodDecl>(D)->isInstanceMethod())
|
|
|
|
EntityInfo.kind = CXIdxEntity_ObjCInstanceMethod;
|
|
|
|
else
|
|
|
|
EntityInfo.kind = CXIdxEntity_ObjCClassMethod;
|
|
|
|
break;
|
2011-11-11 08:23:36 +08:00
|
|
|
case Decl::ObjCProperty:
|
|
|
|
EntityInfo.kind = CXIdxEntity_ObjCProperty; break;
|
|
|
|
case Decl::ObjCIvar:
|
|
|
|
EntityInfo.kind = CXIdxEntity_ObjCIvar; break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
if (IdentifierInfo *II = D->getIdentifier()) {
|
|
|
|
EntityInfo.name = SA.toCStr(II->getName());
|
|
|
|
|
|
|
|
} else if (isa<RecordDecl>(D) || isa<NamespaceDecl>(D)) {
|
2011-11-11 08:23:36 +08:00
|
|
|
EntityInfo.name = 0; // anonymous record/namespace.
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
} else {
|
|
|
|
unsigned Begin = SA.getCurSize();
|
|
|
|
{
|
|
|
|
llvm::raw_svector_ostream OS(SA.getBuffer());
|
|
|
|
D->printName(OS);
|
|
|
|
}
|
|
|
|
EntityInfo.name = SA.getCStr(Begin);
|
|
|
|
}
|
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
{
|
|
|
|
unsigned Begin = SA.getCurSize();
|
|
|
|
bool Ignore = getDeclCursorUSR(D, SA.getBuffer());
|
|
|
|
if (Ignore) {
|
|
|
|
EntityInfo.USR = "";
|
|
|
|
} else {
|
|
|
|
EntityInfo.USR = SA.getCStr(Begin);
|
|
|
|
}
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CXCursor IndexingContext::getRefCursor(const NamedDecl *D, SourceLocation Loc) {
|
|
|
|
if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
|
|
|
|
return MakeCursorTypeRef(TD, Loc, CXTU);
|
|
|
|
if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D))
|
|
|
|
return MakeCursorObjCClassRef(ID, Loc, CXTU);
|
|
|
|
if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D))
|
|
|
|
return MakeCursorObjCProtocolRef(PD, Loc, CXTU);
|
|
|
|
|
|
|
|
//assert(0 && "not yet");
|
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|