2009-08-26 22:36:44 +00:00
|
|
|
//===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Clang-C Source Indexing library.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang-c/Index.h"
|
2009-08-28 15:28:48 +00:00
|
|
|
#include "clang/Index/Program.h"
|
|
|
|
#include "clang/Index/Indexer.h"
|
2009-09-04 15:44:05 +00:00
|
|
|
#include "clang/Index/ASTLocation.h"
|
|
|
|
#include "clang/Index/Utils.h"
|
2009-08-28 15:28:48 +00:00
|
|
|
#include "clang/AST/DeclVisitor.h"
|
2009-09-22 19:25:29 +00:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2009-09-02 13:28:54 +00:00
|
|
|
#include "clang/AST/Decl.h"
|
2009-08-29 12:56:35 +00:00
|
|
|
#include "clang/Basic/FileManager.h"
|
2009-08-31 14:26:51 +00:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2009-08-29 12:56:35 +00:00
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
|
|
|
#include <cstdio>
|
2009-10-15 20:04:39 +00:00
|
|
|
#include <dlfcn.h>
|
2009-10-15 22:10:56 +00:00
|
|
|
#include <sys/wait.h>
|
2009-10-15 23:21:22 +00:00
|
|
|
#include <vector>
|
2009-10-15 20:04:39 +00:00
|
|
|
#include "llvm/System/Path.h"
|
|
|
|
|
2009-08-28 15:28:48 +00:00
|
|
|
using namespace clang;
|
|
|
|
using namespace idx;
|
|
|
|
|
2009-08-31 00:59:03 +00:00
|
|
|
namespace {
|
|
|
|
|
2009-09-23 17:52:52 +00:00
|
|
|
static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE)
|
|
|
|
{
|
|
|
|
NamedDecl *D = DRE->getDecl();
|
|
|
|
if (isa<VarDecl>(D))
|
|
|
|
return CXCursor_VarRef;
|
|
|
|
else if (isa<FunctionDecl>(D))
|
|
|
|
return CXCursor_FunctionRef;
|
|
|
|
else if (isa<EnumConstantDecl>(D))
|
|
|
|
return CXCursor_EnumConstantRef;
|
|
|
|
else
|
|
|
|
return CXCursor_NotImplemented;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// Will be useful one day.
|
2009-09-22 19:25:29 +00:00
|
|
|
class CRefVisitor : public StmtVisitor<CRefVisitor> {
|
|
|
|
CXDecl CDecl;
|
|
|
|
CXDeclIterator Callback;
|
|
|
|
CXClientData CData;
|
|
|
|
|
|
|
|
void Call(enum CXCursorKind CK, Stmt *SRef) {
|
|
|
|
CXCursor C = { CK, CDecl, SRef };
|
|
|
|
Callback(CDecl, C, CData);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
|
|
|
|
CDecl(C), Callback(cback), CData(D) {}
|
|
|
|
|
|
|
|
void VisitStmt(Stmt *S) {
|
|
|
|
for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end();
|
|
|
|
C != CEnd; ++C)
|
|
|
|
Visit(*C);
|
|
|
|
}
|
|
|
|
void VisitDeclRefExpr(DeclRefExpr *Node) {
|
2009-09-23 17:52:52 +00:00
|
|
|
Call(TranslateDeclRefExpr(Node), Node);
|
2009-09-22 19:25:29 +00:00
|
|
|
}
|
|
|
|
void VisitMemberExpr(MemberExpr *Node) {
|
|
|
|
Call(CXCursor_MemberRef, Node);
|
|
|
|
}
|
|
|
|
void VisitObjCMessageExpr(ObjCMessageExpr *Node) {
|
|
|
|
Call(CXCursor_ObjCSelectorRef, Node);
|
|
|
|
}
|
|
|
|
void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
|
|
|
|
Call(CXCursor_ObjCIvarRef, Node);
|
|
|
|
}
|
|
|
|
};
|
2009-09-23 17:52:52 +00:00
|
|
|
#endif
|
2009-09-22 19:25:29 +00:00
|
|
|
|
2009-08-31 00:59:03 +00:00
|
|
|
// Translation Unit Visitor.
|
|
|
|
class TUVisitor : public DeclVisitor<TUVisitor> {
|
|
|
|
CXTranslationUnit TUnit;
|
|
|
|
CXTranslationUnitIterator Callback;
|
2009-09-01 15:55:40 +00:00
|
|
|
CXClientData CData;
|
|
|
|
|
|
|
|
void Call(enum CXCursorKind CK, NamedDecl *ND) {
|
2009-09-22 19:25:29 +00:00
|
|
|
CXCursor C = { CK, ND, 0 };
|
2009-09-01 15:55:40 +00:00
|
|
|
Callback(TUnit, C, CData);
|
|
|
|
}
|
2009-08-31 00:59:03 +00:00
|
|
|
public:
|
2009-09-01 15:55:40 +00:00
|
|
|
TUVisitor(CXTranslationUnit CTU,
|
|
|
|
CXTranslationUnitIterator cback, CXClientData D) :
|
|
|
|
TUnit(CTU), Callback(cback), CData(D) {}
|
2009-08-31 00:59:03 +00:00
|
|
|
|
|
|
|
void VisitTranslationUnitDecl(TranslationUnitDecl *D) {
|
|
|
|
VisitDeclContext(dyn_cast<DeclContext>(D));
|
|
|
|
}
|
|
|
|
void VisitDeclContext(DeclContext *DC) {
|
|
|
|
for (DeclContext::decl_iterator
|
|
|
|
I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
|
|
|
|
Visit(*I);
|
|
|
|
}
|
2009-09-01 15:55:40 +00:00
|
|
|
void VisitTypedefDecl(TypedefDecl *ND) {
|
|
|
|
Call(CXCursor_TypedefDecl, ND);
|
2009-08-31 00:59:03 +00:00
|
|
|
}
|
|
|
|
void VisitTagDecl(TagDecl *ND) {
|
2009-09-02 13:28:54 +00:00
|
|
|
switch (ND->getTagKind()) {
|
|
|
|
case TagDecl::TK_struct:
|
|
|
|
Call(CXCursor_StructDecl, ND);
|
|
|
|
break;
|
|
|
|
case TagDecl::TK_class:
|
|
|
|
Call(CXCursor_ClassDecl, ND);
|
|
|
|
break;
|
|
|
|
case TagDecl::TK_union:
|
|
|
|
Call(CXCursor_UnionDecl, ND);
|
|
|
|
break;
|
|
|
|
case TagDecl::TK_enum:
|
|
|
|
Call(CXCursor_EnumDecl, ND);
|
|
|
|
break;
|
|
|
|
}
|
2009-08-31 00:59:03 +00:00
|
|
|
}
|
2009-09-03 15:49:00 +00:00
|
|
|
void VisitVarDecl(VarDecl *ND) {
|
|
|
|
Call(CXCursor_VarDecl, ND);
|
|
|
|
}
|
2009-08-31 00:59:03 +00:00
|
|
|
void VisitFunctionDecl(FunctionDecl *ND) {
|
2009-09-02 13:28:54 +00:00
|
|
|
Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
|
|
|
|
: CXCursor_FunctionDecl, ND);
|
2009-08-31 00:59:03 +00:00
|
|
|
}
|
|
|
|
void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
|
2009-09-01 15:55:40 +00:00
|
|
|
Call(CXCursor_ObjCInterfaceDecl, ND);
|
2009-08-31 00:59:03 +00:00
|
|
|
}
|
|
|
|
void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
|
2009-09-01 15:55:40 +00:00
|
|
|
Call(CXCursor_ObjCCategoryDecl, ND);
|
2009-08-31 00:59:03 +00:00
|
|
|
}
|
|
|
|
void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
|
2009-09-01 15:55:40 +00:00
|
|
|
Call(CXCursor_ObjCProtocolDecl, ND);
|
2009-08-31 00:59:03 +00:00
|
|
|
}
|
2009-09-02 13:28:54 +00:00
|
|
|
void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
|
|
|
|
Call(CXCursor_ObjCClassDefn, ND);
|
|
|
|
}
|
|
|
|
void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
|
|
|
|
Call(CXCursor_ObjCCategoryDefn, ND);
|
|
|
|
}
|
2009-08-31 00:59:03 +00:00
|
|
|
};
|
|
|
|
|
2009-09-02 13:28:54 +00:00
|
|
|
// Declaration visitor.
|
|
|
|
class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
|
|
|
|
CXDecl CDecl;
|
|
|
|
CXDeclIterator Callback;
|
|
|
|
CXClientData CData;
|
|
|
|
|
|
|
|
void Call(enum CXCursorKind CK, NamedDecl *ND) {
|
2009-09-03 15:49:00 +00:00
|
|
|
// Disable the callback when the context is equal to the visiting decl.
|
|
|
|
if (CDecl == ND && !clang_isReference(CK))
|
|
|
|
return;
|
2009-09-22 19:25:29 +00:00
|
|
|
CXCursor C = { CK, ND, 0 };
|
2009-09-02 13:28:54 +00:00
|
|
|
Callback(CDecl, C, CData);
|
|
|
|
}
|
2009-08-31 00:59:03 +00:00
|
|
|
public:
|
2009-09-02 13:28:54 +00:00
|
|
|
CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
|
|
|
|
CDecl(C), Callback(cback), CData(D) {}
|
|
|
|
|
2009-09-03 15:49:00 +00:00
|
|
|
void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
|
|
|
|
// Issue callbacks for the containing class.
|
|
|
|
Call(CXCursor_ObjCClassRef, ND);
|
|
|
|
// FIXME: Issue callbacks for protocol refs.
|
|
|
|
VisitDeclContext(dyn_cast<DeclContext>(ND));
|
|
|
|
}
|
2009-09-02 13:28:54 +00:00
|
|
|
void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
|
2009-09-03 15:49:00 +00:00
|
|
|
// Issue callbacks for super class.
|
2009-09-02 18:26:48 +00:00
|
|
|
if (D->getSuperClass())
|
|
|
|
Call(CXCursor_ObjCSuperClassRef, D);
|
|
|
|
|
2009-09-04 15:44:05 +00:00
|
|
|
for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
|
|
|
|
E = D->protocol_end(); I != E; ++I)
|
|
|
|
Call(CXCursor_ObjCProtocolRef, *I);
|
2009-09-02 13:28:54 +00:00
|
|
|
VisitDeclContext(dyn_cast<DeclContext>(D));
|
|
|
|
}
|
2009-09-04 15:44:05 +00:00
|
|
|
void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
|
|
|
|
for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
|
|
|
|
E = PID->protocol_end(); I != E; ++I)
|
|
|
|
Call(CXCursor_ObjCProtocolRef, *I);
|
|
|
|
|
|
|
|
VisitDeclContext(dyn_cast<DeclContext>(PID));
|
|
|
|
}
|
2009-09-02 13:28:54 +00:00
|
|
|
void VisitTagDecl(TagDecl *D) {
|
|
|
|
VisitDeclContext(dyn_cast<DeclContext>(D));
|
|
|
|
}
|
|
|
|
void VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
|
|
|
|
VisitDeclContext(dyn_cast<DeclContext>(D));
|
|
|
|
}
|
|
|
|
void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
|
|
|
|
VisitDeclContext(dyn_cast<DeclContext>(D));
|
|
|
|
}
|
2009-08-31 00:59:03 +00:00
|
|
|
void VisitDeclContext(DeclContext *DC) {
|
|
|
|
for (DeclContext::decl_iterator
|
|
|
|
I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
|
|
|
|
Visit(*I);
|
|
|
|
}
|
|
|
|
void VisitEnumConstantDecl(EnumConstantDecl *ND) {
|
2009-09-02 13:28:54 +00:00
|
|
|
Call(CXCursor_EnumConstantDecl, ND);
|
2009-08-31 00:59:03 +00:00
|
|
|
}
|
|
|
|
void VisitFieldDecl(FieldDecl *ND) {
|
2009-09-02 13:28:54 +00:00
|
|
|
Call(CXCursor_FieldDecl, ND);
|
|
|
|
}
|
2009-09-03 15:49:00 +00:00
|
|
|
void VisitVarDecl(VarDecl *ND) {
|
|
|
|
Call(CXCursor_VarDecl, ND);
|
|
|
|
}
|
|
|
|
void VisitParmVarDecl(ParmVarDecl *ND) {
|
|
|
|
Call(CXCursor_ParmDecl, ND);
|
|
|
|
}
|
2009-09-02 13:28:54 +00:00
|
|
|
void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
|
|
|
|
Call(CXCursor_ObjCPropertyDecl, ND);
|
2009-08-31 00:59:03 +00:00
|
|
|
}
|
|
|
|
void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
|
2009-09-02 13:28:54 +00:00
|
|
|
Call(CXCursor_ObjCIvarDecl, ND);
|
|
|
|
}
|
2009-09-03 15:49:00 +00:00
|
|
|
void VisitFunctionDecl(FunctionDecl *ND) {
|
|
|
|
if (ND->isThisDeclarationADefinition()) {
|
|
|
|
VisitDeclContext(dyn_cast<DeclContext>(ND));
|
2009-09-23 17:52:52 +00:00
|
|
|
#if 0
|
|
|
|
// Not currently needed.
|
|
|
|
CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
|
2009-09-22 19:25:29 +00:00
|
|
|
CRefVisitor RVisit(CDecl, Callback, CData);
|
2009-09-23 17:52:52 +00:00
|
|
|
RVisit.Visit(Body);
|
|
|
|
#endif
|
2009-09-03 15:49:00 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-02 13:28:54 +00:00
|
|
|
void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
|
|
|
|
if (ND->getBody()) {
|
|
|
|
Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
|
|
|
|
: CXCursor_ObjCClassMethodDefn, ND);
|
2009-09-03 15:49:00 +00:00
|
|
|
VisitDeclContext(dyn_cast<DeclContext>(ND));
|
2009-09-02 13:28:54 +00:00
|
|
|
} else
|
|
|
|
Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
|
|
|
|
: CXCursor_ObjCClassMethodDecl, ND);
|
2009-08-31 00:59:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-08-27 19:51:58 +00:00
|
|
|
extern "C" {
|
2009-08-26 22:36:44 +00:00
|
|
|
|
2009-10-15 20:04:39 +00:00
|
|
|
static const char *clangPath;
|
|
|
|
|
2009-08-27 19:51:58 +00:00
|
|
|
CXIndex clang_createIndex()
|
2009-08-28 15:28:48 +00:00
|
|
|
{
|
2009-09-21 03:03:22 +00:00
|
|
|
// FIXME: Program is leaked.
|
2009-10-15 20:04:39 +00:00
|
|
|
|
|
|
|
// Find the location where this library lives (libCIndex.dylib).
|
|
|
|
// We do the lookup here to avoid poking dladdr too many times.
|
|
|
|
// This silly cast below avoids a C++ warning.
|
|
|
|
Dl_info info;
|
|
|
|
if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0)
|
|
|
|
assert(0 && "Call to dladdr() failed");
|
|
|
|
|
|
|
|
llvm::sys::Path CIndexPath(info.dli_fname);
|
|
|
|
std::string CIndexDir = CIndexPath.getDirname();
|
|
|
|
|
|
|
|
// We now have the CIndex directory, locate clang relative to it.
|
|
|
|
std::string ClangPath = CIndexDir + "/../bin/clang";
|
|
|
|
|
|
|
|
clangPath = ClangPath.c_str();
|
|
|
|
|
2009-09-21 03:03:22 +00:00
|
|
|
return new Indexer(*new Program());
|
2009-08-27 19:51:58 +00:00
|
|
|
}
|
|
|
|
|
2009-09-17 18:33:27 +00:00
|
|
|
void clang_disposeIndex(CXIndex CIdx)
|
|
|
|
{
|
|
|
|
assert(CIdx && "Passed null CXIndex");
|
|
|
|
delete static_cast<Indexer *>(CIdx);
|
|
|
|
}
|
|
|
|
|
2009-08-28 15:28:48 +00:00
|
|
|
// FIXME: need to pass back error info.
|
|
|
|
CXTranslationUnit clang_createTranslationUnit(
|
|
|
|
CXIndex CIdx, const char *ast_filename)
|
2009-08-27 19:51:58 +00:00
|
|
|
{
|
2009-08-28 15:28:48 +00:00
|
|
|
assert(CIdx && "Passed null CXIndex");
|
|
|
|
Indexer *CXXIdx = static_cast<Indexer *>(CIdx);
|
|
|
|
std::string astName(ast_filename);
|
|
|
|
std::string ErrMsg;
|
|
|
|
|
2009-09-21 03:03:39 +00:00
|
|
|
return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getDiagnostics(),
|
|
|
|
CXXIdx->getFileManager(), &ErrMsg);
|
2009-08-27 19:51:58 +00:00
|
|
|
}
|
|
|
|
|
2009-10-15 20:04:39 +00:00
|
|
|
CXTranslationUnit clang_createTranslationUnitFromSourceFile(
|
|
|
|
CXIndex CIdx,
|
|
|
|
const char *source_filename,
|
|
|
|
int num_command_line_args, const char **command_line_args)
|
|
|
|
{
|
2009-10-15 23:21:22 +00:00
|
|
|
// Build up the arguments for involing clang.
|
|
|
|
std::vector<const char *> argv;
|
|
|
|
argv.push_back(clangPath);
|
|
|
|
argv.push_back("-emit-ast");
|
|
|
|
argv.push_back(source_filename);
|
|
|
|
argv.push_back("-o");
|
2009-10-15 20:50:09 +00:00
|
|
|
// Generate a temporary name for the AST file.
|
|
|
|
char astTmpFile[L_tmpnam];
|
2009-10-15 23:21:22 +00:00
|
|
|
argv.push_back(tmpnam(astTmpFile));
|
2009-10-15 20:04:39 +00:00
|
|
|
for (int i = num_command_line_args; i < num_command_line_args; i++)
|
2009-10-15 23:21:22 +00:00
|
|
|
argv.push_back(command_line_args[i]);
|
|
|
|
argv.push_back(NULL);
|
|
|
|
|
2009-10-15 20:04:39 +00:00
|
|
|
// Generate the AST file in a separate process.
|
|
|
|
pid_t child_pid = fork();
|
|
|
|
if (child_pid == 0) { // Child process
|
|
|
|
|
|
|
|
// Execute the command, passing the appropriate arguments.
|
2009-10-15 23:21:22 +00:00
|
|
|
execv(argv[0], (char *const *)&argv[0]);
|
2009-10-15 20:04:39 +00:00
|
|
|
|
|
|
|
// If execv returns, it failed.
|
|
|
|
assert(0 && "execv() failed");
|
2009-10-15 20:50:09 +00:00
|
|
|
}
|
|
|
|
// This is run by the parent.
|
|
|
|
int child_status;
|
|
|
|
pid_t tpid;
|
|
|
|
do { // Wait for the child to terminate.
|
|
|
|
tpid = wait(&child_status);
|
|
|
|
} while (tpid != child_pid);
|
|
|
|
|
|
|
|
// Finally, we create the translation unit from the ast file.
|
2009-10-15 22:23:48 +00:00
|
|
|
ASTUnit *ATU = static_cast<ASTUnit *>(
|
|
|
|
clang_createTranslationUnit(CIdx, astTmpFile));
|
|
|
|
ATU->unlinkTemporaryFile();
|
|
|
|
return ATU;
|
2009-10-15 20:04:39 +00:00
|
|
|
}
|
|
|
|
|
2009-09-17 18:33:27 +00:00
|
|
|
void clang_disposeTranslationUnit(
|
|
|
|
CXTranslationUnit CTUnit)
|
|
|
|
{
|
|
|
|
assert(CTUnit && "Passed null CXTranslationUnit");
|
|
|
|
delete static_cast<ASTUnit *>(CTUnit);
|
|
|
|
}
|
|
|
|
|
2009-09-03 15:49:00 +00:00
|
|
|
const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit)
|
|
|
|
{
|
|
|
|
assert(CTUnit && "Passed null CXTranslationUnit");
|
2009-09-03 18:19:54 +00:00
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
|
|
|
|
return CXXUnit->getOriginalSourceFileName().c_str();
|
2009-09-03 15:49:00 +00:00
|
|
|
}
|
2009-08-28 16:30:07 +00:00
|
|
|
|
2009-09-02 13:28:54 +00:00
|
|
|
void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
|
|
|
|
CXTranslationUnitIterator callback,
|
|
|
|
CXClientData CData)
|
2009-08-27 19:51:58 +00:00
|
|
|
{
|
2009-08-28 15:28:48 +00:00
|
|
|
assert(CTUnit && "Passed null CXTranslationUnit");
|
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
|
|
|
|
ASTContext &Ctx = CXXUnit->getASTContext();
|
|
|
|
|
2009-09-01 15:55:40 +00:00
|
|
|
TUVisitor DVisit(CTUnit, callback, CData);
|
2009-08-28 15:28:48 +00:00
|
|
|
DVisit.Visit(Ctx.getTranslationUnitDecl());
|
2009-08-27 19:51:58 +00:00
|
|
|
}
|
|
|
|
|
2009-09-02 13:28:54 +00:00
|
|
|
void clang_loadDeclaration(CXDecl Dcl,
|
|
|
|
CXDeclIterator callback,
|
|
|
|
CXClientData CData)
|
2009-08-27 19:51:58 +00:00
|
|
|
{
|
2009-09-02 13:28:54 +00:00
|
|
|
assert(Dcl && "Passed null CXDecl");
|
|
|
|
|
|
|
|
CDeclVisitor DVisit(Dcl, callback, CData);
|
|
|
|
DVisit.Visit(static_cast<Decl *>(Dcl));
|
2009-08-27 19:51:58 +00:00
|
|
|
}
|
|
|
|
|
2009-08-28 12:07:44 +00:00
|
|
|
// Some notes on CXEntity:
|
|
|
|
//
|
|
|
|
// - Since the 'ordinary' namespace includes functions, data, typedefs,
|
|
|
|
// ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one
|
|
|
|
// entity for 2 different types). For example:
|
|
|
|
//
|
|
|
|
// module1.m: @interface Foo @end Foo *x;
|
|
|
|
// module2.m: void Foo(int);
|
|
|
|
//
|
|
|
|
// - Since the unique name spans translation units, static data/functions
|
|
|
|
// within a CXTranslationUnit are *not* currently represented by entities.
|
|
|
|
// As a result, there will be no entity for the following:
|
|
|
|
//
|
|
|
|
// module.m: static void Foo() { }
|
|
|
|
//
|
|
|
|
|
|
|
|
|
2009-08-27 19:51:58 +00:00
|
|
|
const char *clang_getDeclarationName(CXEntity)
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
const char *clang_getURI(CXEntity)
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
CXEntity clang_getEntity(const char *URI)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// CXDecl Operations.
|
|
|
|
//
|
|
|
|
CXEntity clang_getEntityFromDecl(CXDecl)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2009-08-31 00:59:03 +00:00
|
|
|
const char *clang_getDeclSpelling(CXDecl AnonDecl)
|
|
|
|
{
|
|
|
|
assert(AnonDecl && "Passed null CXDecl");
|
|
|
|
NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
|
2009-09-02 13:28:54 +00:00
|
|
|
|
|
|
|
if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) {
|
|
|
|
return OMD->getSelector().getAsString().c_str();
|
|
|
|
}
|
2009-08-31 00:59:03 +00:00
|
|
|
if (ND->getIdentifier())
|
|
|
|
return ND->getIdentifier()->getName();
|
2009-09-02 13:28:54 +00:00
|
|
|
else
|
2009-08-31 00:59:03 +00:00
|
|
|
return "";
|
|
|
|
}
|
2009-09-02 18:26:48 +00:00
|
|
|
|
2009-09-25 21:32:34 +00:00
|
|
|
unsigned clang_getDeclLine(CXDecl AnonDecl)
|
|
|
|
{
|
|
|
|
assert(AnonDecl && "Passed null CXDecl");
|
|
|
|
NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
|
|
|
|
SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
|
|
|
|
return SourceMgr.getSpellingLineNumber(ND->getLocation());
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_getDeclColumn(CXDecl AnonDecl)
|
|
|
|
{
|
|
|
|
assert(AnonDecl && "Passed null CXDecl");
|
|
|
|
NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
|
|
|
|
SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
|
2009-09-25 22:15:54 +00:00
|
|
|
return SourceMgr.getSpellingColumnNumber(ND->getLocation());
|
2009-09-25 21:32:34 +00:00
|
|
|
}
|
|
|
|
|
2009-09-25 21:45:39 +00:00
|
|
|
const char *clang_getDeclSource(CXDecl AnonDecl)
|
|
|
|
{
|
|
|
|
assert(AnonDecl && "Passed null CXDecl");
|
|
|
|
NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
|
|
|
|
SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
|
|
|
|
return SourceMgr.getBufferName(ND->getLocation());
|
|
|
|
}
|
|
|
|
|
2009-09-02 18:26:48 +00:00
|
|
|
const char *clang_getCursorSpelling(CXCursor C)
|
|
|
|
{
|
|
|
|
assert(C.decl && "CXCursor has null decl");
|
|
|
|
NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
|
|
|
|
|
|
|
|
if (clang_isReference(C.kind)) {
|
|
|
|
switch (C.kind) {
|
2009-09-02 18:58:52 +00:00
|
|
|
case CXCursor_ObjCSuperClassRef:
|
|
|
|
{
|
2009-09-02 18:26:48 +00:00
|
|
|
ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
|
|
|
|
assert(OID && "clang_getCursorLine(): Missing interface decl");
|
|
|
|
return OID->getSuperClass()->getIdentifier()->getName();
|
2009-09-02 18:58:52 +00:00
|
|
|
}
|
2009-09-03 15:49:00 +00:00
|
|
|
case CXCursor_ObjCClassRef:
|
|
|
|
{
|
2009-10-01 00:31:07 +00:00
|
|
|
if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND)) {
|
|
|
|
return OID->getIdentifier()->getName();
|
|
|
|
}
|
2009-09-03 15:49:00 +00:00
|
|
|
ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
|
|
|
|
assert(OID && "clang_getCursorLine(): Missing category decl");
|
|
|
|
return OID->getClassInterface()->getIdentifier()->getName();
|
|
|
|
}
|
2009-09-04 15:44:05 +00:00
|
|
|
case CXCursor_ObjCProtocolRef:
|
|
|
|
{
|
|
|
|
ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
|
|
|
|
assert(OID && "clang_getCursorLine(): Missing protocol decl");
|
|
|
|
return OID->getIdentifier()->getName();
|
|
|
|
}
|
2009-09-22 19:25:29 +00:00
|
|
|
case CXCursor_ObjCSelectorRef:
|
|
|
|
{
|
|
|
|
ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
|
|
|
|
static_cast<Stmt *>(C.stmt));
|
|
|
|
assert(OME && "clang_getCursorLine(): Missing message expr");
|
|
|
|
return OME->getSelector().getAsString().c_str();
|
|
|
|
}
|
|
|
|
case CXCursor_VarRef:
|
|
|
|
case CXCursor_FunctionRef:
|
|
|
|
case CXCursor_EnumConstantRef:
|
|
|
|
{
|
|
|
|
DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
|
|
|
|
static_cast<Stmt *>(C.stmt));
|
|
|
|
assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
|
|
|
|
return DRE->getDecl()->getIdentifier()->getName();
|
|
|
|
}
|
2009-09-02 18:26:48 +00:00
|
|
|
default:
|
|
|
|
return "<not implemented>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return clang_getDeclSpelling(C.decl);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *clang_getCursorKindSpelling(enum CXCursorKind Kind)
|
2009-08-31 00:59:03 +00:00
|
|
|
{
|
|
|
|
switch (Kind) {
|
|
|
|
case CXCursor_FunctionDecl: return "FunctionDecl";
|
|
|
|
case CXCursor_TypedefDecl: return "TypedefDecl";
|
|
|
|
case CXCursor_EnumDecl: return "EnumDecl";
|
|
|
|
case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
|
2009-09-02 13:28:54 +00:00
|
|
|
case CXCursor_StructDecl: return "StructDecl";
|
|
|
|
case CXCursor_UnionDecl: return "UnionDecl";
|
|
|
|
case CXCursor_ClassDecl: return "ClassDecl";
|
2009-08-31 00:59:03 +00:00
|
|
|
case CXCursor_FieldDecl: return "FieldDecl";
|
|
|
|
case CXCursor_VarDecl: return "VarDecl";
|
|
|
|
case CXCursor_ParmDecl: return "ParmDecl";
|
|
|
|
case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
|
|
|
|
case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
|
|
|
|
case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
|
|
|
|
case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
|
|
|
|
case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
|
2009-09-02 13:28:54 +00:00
|
|
|
case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
|
|
|
|
case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
|
|
|
|
case CXCursor_FunctionDefn: return "FunctionDefn";
|
|
|
|
case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
|
|
|
|
case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
|
|
|
|
case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
|
|
|
|
case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
|
2009-09-02 18:26:48 +00:00
|
|
|
case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
|
2009-09-04 15:44:05 +00:00
|
|
|
case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
|
2009-09-03 15:49:00 +00:00
|
|
|
case CXCursor_ObjCClassRef: return "ObjCClassRef";
|
2009-09-22 19:25:29 +00:00
|
|
|
case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
|
|
|
|
|
|
|
|
case CXCursor_VarRef: return "VarRef";
|
|
|
|
case CXCursor_FunctionRef: return "FunctionRef";
|
|
|
|
case CXCursor_EnumConstantRef: return "EnumConstantRef";
|
|
|
|
case CXCursor_MemberRef: return "MemberRef";
|
|
|
|
|
2009-09-15 20:25:34 +00:00
|
|
|
case CXCursor_InvalidFile: return "InvalidFile";
|
|
|
|
case CXCursor_NoDeclFound: return "NoDeclFound";
|
|
|
|
case CXCursor_NotImplemented: return "NotImplemented";
|
2009-08-31 00:59:03 +00:00
|
|
|
default: return "<not implemented>";
|
|
|
|
}
|
2009-08-27 19:51:58 +00:00
|
|
|
}
|
2009-08-31 00:59:03 +00:00
|
|
|
|
2009-09-04 15:44:05 +00:00
|
|
|
static enum CXCursorKind TranslateKind(Decl *D) {
|
|
|
|
switch (D->getKind()) {
|
|
|
|
case Decl::Function: return CXCursor_FunctionDecl;
|
|
|
|
case Decl::Typedef: return CXCursor_TypedefDecl;
|
|
|
|
case Decl::Enum: return CXCursor_EnumDecl;
|
|
|
|
case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
|
|
|
|
case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
|
|
|
|
case Decl::Field: return CXCursor_FieldDecl;
|
|
|
|
case Decl::Var: return CXCursor_VarDecl;
|
|
|
|
case Decl::ParmVar: return CXCursor_ParmDecl;
|
|
|
|
case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
|
2009-09-22 19:25:29 +00:00
|
|
|
case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
|
|
|
|
case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
|
2009-09-04 15:44:05 +00:00
|
|
|
case Decl::ObjCMethod: {
|
|
|
|
ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
|
|
|
|
if (MD->isInstanceMethod())
|
|
|
|
return CXCursor_ObjCInstanceMethodDecl;
|
|
|
|
return CXCursor_ObjCClassMethodDecl;
|
|
|
|
}
|
|
|
|
default: break;
|
|
|
|
}
|
2009-09-15 20:25:34 +00:00
|
|
|
return CXCursor_NotImplemented;
|
2009-09-04 15:44:05 +00:00
|
|
|
}
|
2009-08-27 19:51:58 +00:00
|
|
|
//
|
|
|
|
// CXCursor Operations.
|
|
|
|
//
|
2009-09-04 15:44:05 +00:00
|
|
|
CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
|
2009-08-27 19:51:58 +00:00
|
|
|
unsigned line, unsigned column)
|
|
|
|
{
|
2009-09-04 15:44:05 +00:00
|
|
|
assert(CTUnit && "Passed null CXTranslationUnit");
|
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
|
|
|
|
|
|
|
|
FileManager &FMgr = CXXUnit->getFileManager();
|
|
|
|
const FileEntry *File = FMgr.getFile(source_name,
|
2009-09-15 20:25:34 +00:00
|
|
|
source_name+strlen(source_name));
|
|
|
|
if (!File) {
|
2009-09-22 19:25:29 +00:00
|
|
|
CXCursor C = { CXCursor_InvalidFile, 0, 0 };
|
2009-09-15 20:25:34 +00:00
|
|
|
return C;
|
|
|
|
}
|
2009-09-04 15:44:05 +00:00
|
|
|
SourceLocation SLoc =
|
|
|
|
CXXUnit->getSourceManager().getLocation(File, line, column);
|
|
|
|
|
|
|
|
ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc);
|
|
|
|
|
2009-09-29 19:44:27 +00:00
|
|
|
Decl *Dcl = ALoc.getParentDecl();
|
2009-09-29 19:45:58 +00:00
|
|
|
if (ALoc.isNamedRef())
|
|
|
|
Dcl = ALoc.AsNamedRef().ND;
|
2009-09-29 19:44:27 +00:00
|
|
|
Stmt *Stm = ALoc.dyn_AsStmt();
|
2009-09-23 17:52:52 +00:00
|
|
|
if (Dcl) {
|
|
|
|
if (Stm) {
|
|
|
|
if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
|
|
|
|
CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
|
|
|
|
return C;
|
|
|
|
} else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
|
|
|
|
CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
|
|
|
|
return C;
|
2009-10-01 00:31:07 +00:00
|
|
|
}
|
2009-09-23 17:52:52 +00:00
|
|
|
// Fall through...treat as a decl, not a ref.
|
|
|
|
}
|
2009-10-01 00:31:07 +00:00
|
|
|
if (ALoc.isNamedRef()) {
|
|
|
|
if (isa<ObjCInterfaceDecl>(Dcl)) {
|
|
|
|
CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() };
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
if (isa<ObjCProtocolDecl>(Dcl)) {
|
|
|
|
CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() };
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
|
2009-09-15 20:25:34 +00:00
|
|
|
return C;
|
|
|
|
}
|
2009-09-22 19:25:29 +00:00
|
|
|
CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
|
2009-09-15 20:25:34 +00:00
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
|
|
|
CXCursor clang_getCursorFromDecl(CXDecl AnonDecl)
|
|
|
|
{
|
|
|
|
assert(AnonDecl && "Passed null CXDecl");
|
|
|
|
NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
|
2009-09-04 15:44:05 +00:00
|
|
|
|
2009-09-22 19:25:29 +00:00
|
|
|
CXCursor C = { TranslateKind(ND), ND, 0 };
|
2009-09-04 15:44:05 +00:00
|
|
|
return C;
|
2009-08-27 19:51:58 +00:00
|
|
|
}
|
|
|
|
|
2009-09-15 20:25:34 +00:00
|
|
|
unsigned clang_isInvalid(enum CXCursorKind K)
|
|
|
|
{
|
|
|
|
return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
|
|
|
|
}
|
|
|
|
|
2009-08-31 00:59:03 +00:00
|
|
|
unsigned clang_isDeclaration(enum CXCursorKind K)
|
|
|
|
{
|
|
|
|
return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
|
|
|
|
}
|
2009-08-31 14:26:51 +00:00
|
|
|
|
2009-09-02 18:26:48 +00:00
|
|
|
unsigned clang_isReference(enum CXCursorKind K)
|
|
|
|
{
|
|
|
|
return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_isDefinition(enum CXCursorKind K)
|
|
|
|
{
|
|
|
|
return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
|
|
|
|
}
|
|
|
|
|
2009-09-04 15:44:05 +00:00
|
|
|
CXCursorKind clang_getCursorKind(CXCursor C)
|
|
|
|
{
|
|
|
|
return C.kind;
|
|
|
|
}
|
|
|
|
|
2009-09-25 21:32:34 +00:00
|
|
|
static Decl *getDeclFromExpr(Stmt *E) {
|
|
|
|
if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
|
|
|
|
return RefExpr->getDecl();
|
|
|
|
if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
|
|
|
|
return ME->getMemberDecl();
|
|
|
|
if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
|
|
|
|
return RE->getDecl();
|
|
|
|
|
|
|
|
if (CallExpr *CE = dyn_cast<CallExpr>(E))
|
|
|
|
return getDeclFromExpr(CE->getCallee());
|
|
|
|
if (CastExpr *CE = dyn_cast<CastExpr>(E))
|
|
|
|
return getDeclFromExpr(CE->getSubExpr());
|
|
|
|
if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
|
|
|
|
return OME->getMethodDecl();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-09-04 15:44:05 +00:00
|
|
|
CXDecl clang_getCursorDecl(CXCursor C)
|
|
|
|
{
|
2009-09-25 21:32:34 +00:00
|
|
|
if (clang_isDeclaration(C.kind))
|
|
|
|
return C.decl;
|
|
|
|
|
|
|
|
if (clang_isReference(C.kind)) {
|
2009-10-01 00:31:07 +00:00
|
|
|
if (C.stmt) {
|
2009-10-05 17:58:19 +00:00
|
|
|
if (C.kind == CXCursor_ObjCClassRef ||
|
|
|
|
C.kind == CXCursor_ObjCProtocolRef)
|
2009-10-01 00:31:07 +00:00
|
|
|
return static_cast<Stmt *>(C.stmt);
|
|
|
|
else
|
|
|
|
return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
|
|
|
|
} else
|
2009-09-25 21:32:34 +00:00
|
|
|
return C.decl;
|
|
|
|
}
|
|
|
|
return 0;
|
2009-09-04 15:44:05 +00:00
|
|
|
}
|
|
|
|
|
2009-10-01 00:31:07 +00:00
|
|
|
|
2009-09-02 18:26:48 +00:00
|
|
|
static SourceLocation getLocationFromCursor(CXCursor C,
|
|
|
|
SourceManager &SourceMgr,
|
|
|
|
NamedDecl *ND) {
|
|
|
|
if (clang_isReference(C.kind)) {
|
|
|
|
switch (C.kind) {
|
2009-10-01 00:31:07 +00:00
|
|
|
case CXCursor_ObjCClassRef:
|
|
|
|
{
|
|
|
|
if (isa<ObjCInterfaceDecl>(ND)) {
|
|
|
|
// FIXME: This is a hack (storing the parent decl in the stmt slot).
|
|
|
|
NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt);
|
|
|
|
return parentDecl->getLocation();
|
|
|
|
}
|
|
|
|
ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
|
|
|
|
assert(OID && "clang_getCursorLine(): Missing category decl");
|
|
|
|
return OID->getClassInterface()->getLocation();
|
|
|
|
}
|
2009-09-04 15:44:05 +00:00
|
|
|
case CXCursor_ObjCSuperClassRef:
|
2009-09-02 18:58:52 +00:00
|
|
|
{
|
2009-09-02 18:26:48 +00:00
|
|
|
ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
|
|
|
|
assert(OID && "clang_getCursorLine(): Missing interface decl");
|
2009-09-02 18:58:52 +00:00
|
|
|
return OID->getSuperClassLoc();
|
|
|
|
}
|
2009-09-04 15:44:05 +00:00
|
|
|
case CXCursor_ObjCProtocolRef:
|
|
|
|
{
|
|
|
|
ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
|
|
|
|
assert(OID && "clang_getCursorLine(): Missing protocol decl");
|
|
|
|
return OID->getLocation();
|
|
|
|
}
|
2009-09-22 19:25:29 +00:00
|
|
|
case CXCursor_ObjCSelectorRef:
|
|
|
|
{
|
|
|
|
ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
|
|
|
|
static_cast<Stmt *>(C.stmt));
|
|
|
|
assert(OME && "clang_getCursorLine(): Missing message expr");
|
|
|
|
return OME->getLeftLoc(); /* FIXME: should be a range */
|
|
|
|
}
|
|
|
|
case CXCursor_VarRef:
|
|
|
|
case CXCursor_FunctionRef:
|
|
|
|
case CXCursor_EnumConstantRef:
|
|
|
|
{
|
|
|
|
DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
|
|
|
|
static_cast<Stmt *>(C.stmt));
|
|
|
|
assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
|
|
|
|
return DRE->getLocation();
|
|
|
|
}
|
2009-09-02 18:26:48 +00:00
|
|
|
default:
|
2009-09-02 18:58:52 +00:00
|
|
|
return SourceLocation();
|
2009-09-02 18:26:48 +00:00
|
|
|
}
|
|
|
|
} else { // We have a declaration or a definition.
|
2009-09-04 15:44:05 +00:00
|
|
|
SourceLocation SLoc;
|
|
|
|
switch (ND->getKind()) {
|
|
|
|
case Decl::ObjCInterface:
|
|
|
|
{
|
|
|
|
SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Decl::ObjCProtocol:
|
|
|
|
{
|
|
|
|
SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
SLoc = ND->getLocation();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-09-02 18:26:48 +00:00
|
|
|
if (SLoc.isInvalid())
|
|
|
|
return SourceLocation();
|
2009-09-02 18:58:52 +00:00
|
|
|
return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
|
2009-09-02 18:26:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-31 14:26:51 +00:00
|
|
|
unsigned clang_getCursorLine(CXCursor C)
|
|
|
|
{
|
|
|
|
assert(C.decl && "CXCursor has null decl");
|
|
|
|
NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
|
|
|
|
SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
|
2009-09-02 18:26:48 +00:00
|
|
|
|
|
|
|
SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
|
2009-08-31 14:26:51 +00:00
|
|
|
return SourceMgr.getSpellingLineNumber(SLoc);
|
|
|
|
}
|
2009-09-02 18:26:48 +00:00
|
|
|
|
2009-08-31 14:26:51 +00:00
|
|
|
unsigned clang_getCursorColumn(CXCursor C)
|
|
|
|
{
|
|
|
|
assert(C.decl && "CXCursor has null decl");
|
|
|
|
NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
|
|
|
|
SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
|
2009-09-02 18:26:48 +00:00
|
|
|
|
|
|
|
SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
|
2009-08-31 14:26:51 +00:00
|
|
|
return SourceMgr.getSpellingColumnNumber(SLoc);
|
|
|
|
}
|
|
|
|
const char *clang_getCursorSource(CXCursor C)
|
|
|
|
{
|
|
|
|
assert(C.decl && "CXCursor has null decl");
|
|
|
|
NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
|
|
|
|
SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
|
2009-09-02 18:26:48 +00:00
|
|
|
|
|
|
|
SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
|
2009-08-31 14:26:51 +00:00
|
|
|
return SourceMgr.getBufferName(SLoc);
|
2009-08-27 19:51:58 +00:00
|
|
|
}
|
|
|
|
|
2009-09-23 17:52:52 +00:00
|
|
|
void clang_getDefinitionSpellingAndExtent(CXCursor C,
|
|
|
|
const char **startBuf,
|
|
|
|
const char **endBuf,
|
|
|
|
unsigned *startLine,
|
|
|
|
unsigned *startColumn,
|
|
|
|
unsigned *endLine,
|
|
|
|
unsigned *endColumn)
|
|
|
|
{
|
|
|
|
assert(C.decl && "CXCursor has null decl");
|
|
|
|
NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
|
|
|
|
FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
|
|
|
|
CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
|
|
|
|
|
|
|
|
SourceManager &SM = FD->getASTContext().getSourceManager();
|
|
|
|
*startBuf = SM.getCharacterData(Body->getLBracLoc());
|
|
|
|
*endBuf = SM.getCharacterData(Body->getRBracLoc());
|
|
|
|
*startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
|
|
|
|
*startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
|
|
|
|
*endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
|
|
|
|
*endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-27 19:51:58 +00:00
|
|
|
} // end extern "C"
|