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.
|
2009-11-30 20:42:43 +00:00
|
|
|
//
|
2009-08-26 22:36:44 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2010-01-05 19:32:54 +00:00
|
|
|
// This file implements the main API hooks in the Clang-C Source Indexing
|
|
|
|
// library.
|
2009-08-26 22:36:44 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-01-05 19:32:54 +00:00
|
|
|
#include "CIndexer.h"
|
2010-01-15 20:35:54 +00:00
|
|
|
#include "CXCursor.h"
|
2010-01-05 19:32:54 +00:00
|
|
|
|
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"
|
2010-01-21 16:28:34 +00:00
|
|
|
#include "clang/AST/TypeLocVisitor.h"
|
2010-01-06 23:43:31 +00:00
|
|
|
#include "clang/Lex/Lexer.h"
|
2009-10-16 21:24:31 +00:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2009-10-18 11:19:36 +00:00
|
|
|
#include "llvm/System/Program.h"
|
2009-10-19 21:44:57 +00:00
|
|
|
|
2010-01-05 20:55:39 +00:00
|
|
|
// Needed to define L_TMPNAM on some systems.
|
|
|
|
#include <cstdio>
|
|
|
|
|
2009-08-28 15:28:48 +00:00
|
|
|
using namespace clang;
|
2010-01-15 20:35:54 +00:00
|
|
|
using namespace clang::cxcursor;
|
2009-08-28 15:28:48 +00:00
|
|
|
using namespace idx;
|
|
|
|
|
2010-01-06 03:42:32 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Crash Reporting.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
2010-01-07 22:49:05 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
#define USE_CRASHTRACER
|
2010-01-06 03:42:32 +00:00
|
|
|
#include "clang/Analysis/Support/SaveAndRestore.h"
|
|
|
|
// Integrate with crash reporter.
|
|
|
|
extern "C" const char *__crashreporter_info__;
|
2010-01-07 22:49:05 +00:00
|
|
|
#define NUM_CRASH_STRINGS 16
|
|
|
|
static unsigned crashtracer_counter = 0;
|
2010-01-07 23:13:53 +00:00
|
|
|
static unsigned crashtracer_counter_id[NUM_CRASH_STRINGS] = { 0 };
|
2010-01-07 22:49:05 +00:00
|
|
|
static const char *crashtracer_strings[NUM_CRASH_STRINGS] = { 0 };
|
|
|
|
static const char *agg_crashtracer_strings[NUM_CRASH_STRINGS] = { 0 };
|
|
|
|
|
|
|
|
static unsigned SetCrashTracerInfo(const char *str,
|
|
|
|
llvm::SmallString<1024> &AggStr) {
|
|
|
|
|
2010-01-07 23:13:53 +00:00
|
|
|
unsigned slot = 0;
|
2010-01-07 22:49:05 +00:00
|
|
|
while (crashtracer_strings[slot]) {
|
|
|
|
if (++slot == NUM_CRASH_STRINGS)
|
|
|
|
slot = 0;
|
|
|
|
}
|
|
|
|
crashtracer_strings[slot] = str;
|
2010-01-07 23:13:53 +00:00
|
|
|
crashtracer_counter_id[slot] = ++crashtracer_counter;
|
2010-01-07 22:49:05 +00:00
|
|
|
|
|
|
|
// We need to create an aggregate string because multiple threads
|
|
|
|
// may be in this method at one time. The crash reporter string
|
|
|
|
// will attempt to overapproximate the set of in-flight invocations
|
|
|
|
// of this function. Race conditions can still cause this goal
|
|
|
|
// to not be achieved.
|
|
|
|
{
|
|
|
|
llvm::raw_svector_ostream Out(AggStr);
|
|
|
|
for (unsigned i = 0; i < NUM_CRASH_STRINGS; ++i)
|
|
|
|
if (crashtracer_strings[i]) Out << crashtracer_strings[i] << '\n';
|
|
|
|
}
|
|
|
|
__crashreporter_info__ = agg_crashtracer_strings[slot] = AggStr.c_str();
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ResetCrashTracerInfo(unsigned slot) {
|
2010-01-07 23:13:53 +00:00
|
|
|
unsigned max_slot = 0;
|
|
|
|
unsigned max_value = 0;
|
|
|
|
|
|
|
|
crashtracer_strings[slot] = agg_crashtracer_strings[slot] = 0;
|
|
|
|
|
|
|
|
for (unsigned i = 0 ; i < NUM_CRASH_STRINGS; ++i)
|
|
|
|
if (agg_crashtracer_strings[i] &&
|
|
|
|
crashtracer_counter_id[i] > max_value) {
|
|
|
|
max_slot = i;
|
|
|
|
max_value = crashtracer_counter_id[i];
|
2010-01-07 22:49:05 +00:00
|
|
|
}
|
2010-01-07 23:13:53 +00:00
|
|
|
|
|
|
|
__crashreporter_info__ = agg_crashtracer_strings[max_slot];
|
2010-01-07 22:49:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class ArgsCrashTracerInfo {
|
|
|
|
llvm::SmallString<1024> CrashString;
|
|
|
|
llvm::SmallString<1024> AggregateString;
|
|
|
|
unsigned crashtracerSlot;
|
|
|
|
public:
|
|
|
|
ArgsCrashTracerInfo(llvm::SmallVectorImpl<const char*> &Args)
|
|
|
|
: crashtracerSlot(0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
llvm::raw_svector_ostream Out(CrashString);
|
|
|
|
Out << "ClangCIndex [createTranslationUnitFromSourceFile]: clang";
|
|
|
|
for (llvm::SmallVectorImpl<const char*>::iterator I=Args.begin(),
|
|
|
|
E=Args.end(); I!=E; ++I)
|
|
|
|
Out << ' ' << *I;
|
|
|
|
}
|
|
|
|
crashtracerSlot = SetCrashTracerInfo(CrashString.c_str(),
|
|
|
|
AggregateString);
|
|
|
|
}
|
|
|
|
|
|
|
|
~ArgsCrashTracerInfo() {
|
|
|
|
ResetCrashTracerInfo(crashtracerSlot);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|
2010-01-06 03:42:32 +00:00
|
|
|
#endif
|
|
|
|
|
2010-01-19 21:36:55 +00:00
|
|
|
typedef llvm::PointerIntPair<ASTContext *, 1, bool> CXSourceLocationPtr;
|
|
|
|
|
2010-01-18 22:46:11 +00:00
|
|
|
/// \brief Translate a Clang source location into a CIndex source location.
|
2010-01-19 21:36:55 +00:00
|
|
|
static CXSourceLocation translateSourceLocation(ASTContext &Context,
|
|
|
|
SourceLocation Loc,
|
|
|
|
bool AtEnd = false) {
|
|
|
|
CXSourceLocationPtr Ptr(&Context, AtEnd);
|
|
|
|
CXSourceLocation Result = { Ptr.getOpaqueValue(), Loc.getRawEncoding() };
|
|
|
|
return Result;
|
2010-01-18 22:46:11 +00:00
|
|
|
}
|
|
|
|
|
2010-01-19 00:34:46 +00:00
|
|
|
/// \brief Translate a Clang source range into a CIndex source range.
|
2010-01-19 21:36:55 +00:00
|
|
|
static CXSourceRange translateSourceRange(ASTContext &Context, SourceRange R) {
|
|
|
|
CXSourceRange Result = { &Context,
|
|
|
|
R.getBegin().getRawEncoding(),
|
|
|
|
R.getEnd().getRawEncoding() };
|
|
|
|
return Result;
|
2010-01-19 00:34:46 +00:00
|
|
|
}
|
|
|
|
|
2010-01-19 21:36:55 +00:00
|
|
|
|
2010-01-06 03:42:32 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Visitors.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-08-31 00:59:03 +00:00
|
|
|
namespace {
|
2010-01-16 00:36:30 +00:00
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
// Cursor visitor.
|
2010-01-21 16:28:34 +00:00
|
|
|
class CursorVisitor : public DeclVisitor<CursorVisitor, bool>,
|
|
|
|
public TypeLocVisitor<CursorVisitor, bool>
|
|
|
|
{
|
2010-01-20 23:57:43 +00:00
|
|
|
ASTUnit *TU;
|
2010-01-20 20:59:29 +00:00
|
|
|
CXCursor Parent;
|
|
|
|
CXCursorVisitor Visitor;
|
|
|
|
CXClientData ClientData;
|
|
|
|
|
2009-10-16 20:01:17 +00:00
|
|
|
// MaxPCHLevel - the maximum PCH level of declarations that we will pass on
|
|
|
|
// to the visitor. Declarations with a PCH level greater than this value will
|
|
|
|
// be suppressed.
|
|
|
|
unsigned MaxPCHLevel;
|
2010-01-20 20:59:29 +00:00
|
|
|
|
|
|
|
using DeclVisitor<CursorVisitor, bool>::Visit;
|
2010-01-21 16:28:34 +00:00
|
|
|
using TypeLocVisitor<CursorVisitor, bool>::Visit;
|
2010-01-20 20:59:29 +00:00
|
|
|
|
|
|
|
public:
|
2010-01-20 23:57:43 +00:00
|
|
|
CursorVisitor(ASTUnit *TU, CXCursorVisitor Visitor, CXClientData ClientData,
|
2010-01-20 20:59:29 +00:00
|
|
|
unsigned MaxPCHLevel)
|
2010-01-20 23:57:43 +00:00
|
|
|
: TU(TU), Visitor(Visitor), ClientData(ClientData), MaxPCHLevel(MaxPCHLevel)
|
2010-01-20 20:59:29 +00:00
|
|
|
{
|
|
|
|
Parent.kind = CXCursor_NoDeclFound;
|
|
|
|
Parent.data[0] = 0;
|
|
|
|
Parent.data[1] = 0;
|
|
|
|
Parent.data[2] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Visit(CXCursor Cursor);
|
|
|
|
bool VisitChildren(CXCursor Parent);
|
|
|
|
|
2010-01-21 16:28:34 +00:00
|
|
|
// Declaration visitors
|
2010-01-20 20:59:29 +00:00
|
|
|
bool VisitDeclContext(DeclContext *DC);
|
|
|
|
bool VisitTranslationUnitDecl(TranslationUnitDecl *D);
|
2010-01-21 16:28:34 +00:00
|
|
|
bool VisitDeclaratorDecl(DeclaratorDecl *DD);
|
2010-01-20 20:59:29 +00:00
|
|
|
bool VisitFunctionDecl(FunctionDecl *ND);
|
|
|
|
bool VisitObjCCategoryDecl(ObjCCategoryDecl *ND);
|
|
|
|
bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
|
|
|
|
bool VisitObjCMethodDecl(ObjCMethodDecl *ND);
|
|
|
|
bool VisitObjCProtocolDecl(ObjCProtocolDecl *PID);
|
|
|
|
bool VisitTagDecl(TagDecl *D);
|
2010-01-21 16:28:34 +00:00
|
|
|
|
|
|
|
// Type visitors
|
|
|
|
bool VisitTypedefTypeLoc(TypedefTypeLoc TL);
|
2010-01-20 20:59:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
2009-11-30 20:42:43 +00:00
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
/// \brief Visit the given cursor and, if requested by the visitor,
|
|
|
|
/// its children.
|
|
|
|
///
|
|
|
|
/// \returns true if the visitation should be aborted, false if it
|
|
|
|
/// should continue.
|
|
|
|
bool CursorVisitor::Visit(CXCursor Cursor) {
|
|
|
|
if (clang_isInvalid(Cursor.kind))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (clang_isDeclaration(Cursor.kind)) {
|
|
|
|
Decl *D = getCursorDecl(Cursor);
|
|
|
|
assert(D && "Invalid declaration cursor");
|
|
|
|
if (D->getPCHLevel() > MaxPCHLevel)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (D->isImplicit())
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-30 20:42:43 +00:00
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
switch (Visitor(Cursor, Parent, ClientData)) {
|
|
|
|
case CXChildVisit_Break:
|
|
|
|
return true;
|
2009-11-30 20:42:43 +00:00
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
case CXChildVisit_Continue:
|
|
|
|
return false;
|
2010-01-16 14:00:32 +00:00
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
case CXChildVisit_Recurse:
|
|
|
|
return VisitChildren(Cursor);
|
|
|
|
}
|
2009-11-30 20:42:43 +00:00
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
llvm_unreachable("Silly GCC, we can't get here");
|
|
|
|
}
|
2009-11-30 20:42:43 +00:00
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
/// \brief Visit the children of the given cursor.
|
|
|
|
///
|
|
|
|
/// \returns true if the visitation should be aborted, false if it
|
|
|
|
/// should continue.
|
|
|
|
bool CursorVisitor::VisitChildren(CXCursor Cursor) {
|
|
|
|
// Set the Parent field to Cursor, then back to its old value once we're
|
|
|
|
// done.
|
|
|
|
class SetParentRAII {
|
|
|
|
CXCursor &Parent;
|
|
|
|
CXCursor OldParent;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SetParentRAII(CXCursor &Parent, CXCursor NewParent)
|
|
|
|
: Parent(Parent), OldParent(Parent)
|
|
|
|
{
|
|
|
|
Parent = NewParent;
|
|
|
|
}
|
|
|
|
|
|
|
|
~SetParentRAII() {
|
|
|
|
Parent = OldParent;
|
|
|
|
}
|
|
|
|
} SetParent(Parent, Cursor);
|
|
|
|
|
|
|
|
if (clang_isDeclaration(Cursor.kind)) {
|
|
|
|
Decl *D = getCursorDecl(Cursor);
|
|
|
|
assert(D && "Invalid declaration cursor");
|
|
|
|
return Visit(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clang_isTranslationUnit(Cursor.kind)) {
|
2010-01-20 23:57:43 +00:00
|
|
|
ASTUnit *CXXUnit = getCursorASTUnit(Cursor);
|
2010-01-20 21:13:59 +00:00
|
|
|
if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls()) {
|
|
|
|
const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls();
|
|
|
|
for (std::vector<Decl*>::const_iterator it = TLDs.begin(),
|
|
|
|
ie = TLDs.end(); it != ie; ++it) {
|
2010-01-20 23:57:43 +00:00
|
|
|
if (Visit(MakeCXCursor(*it, CXXUnit)))
|
2010-01-20 21:13:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return VisitDeclContext(
|
2010-01-20 20:59:29 +00:00
|
|
|
CXXUnit->getASTContext().getTranslationUnitDecl());
|
2010-01-20 21:13:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2010-01-20 20:59:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to visit at the moment.
|
|
|
|
// FIXME: Traverse statements, declarations, etc. here.
|
|
|
|
return false;
|
2010-01-13 00:22:49 +00:00
|
|
|
}
|
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
|
2010-01-20 21:13:59 +00:00
|
|
|
llvm_unreachable("Translation units are visited directly by Visit()");
|
|
|
|
return false;
|
2010-01-13 00:22:49 +00:00
|
|
|
}
|
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
bool CursorVisitor::VisitDeclContext(DeclContext *DC) {
|
|
|
|
for (DeclContext::decl_iterator
|
|
|
|
I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) {
|
2010-01-20 23:57:43 +00:00
|
|
|
if (Visit(MakeCXCursor(*I, TU)))
|
2010-01-20 20:59:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2010-01-13 00:22:49 +00:00
|
|
|
}
|
|
|
|
|
2010-01-21 16:28:34 +00:00
|
|
|
bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) {
|
|
|
|
if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo())
|
|
|
|
if (Visit(TSInfo->getTypeLoc()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) {
|
|
|
|
// FIXME: This is wrong. We always want to visit the parameters and
|
|
|
|
// the body, if available.
|
2010-01-13 00:22:49 +00:00
|
|
|
if (ND->isThisDeclarationADefinition()) {
|
2010-01-20 20:59:29 +00:00
|
|
|
return VisitDeclContext(ND);
|
|
|
|
|
2009-09-23 17:52:52 +00:00
|
|
|
#if 0
|
2010-01-13 00:22:49 +00:00
|
|
|
// Not currently needed.
|
|
|
|
CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
|
|
|
|
CRefVisitor RVisit(CDecl, Callback, CData);
|
|
|
|
RVisit.Visit(Body);
|
2009-09-23 17:52:52 +00:00
|
|
|
#endif
|
2009-08-31 00:59:03 +00:00
|
|
|
}
|
2010-01-20 20:59:29 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2010-01-13 00:22:49 +00:00
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
|
2010-01-20 23:57:43 +00:00
|
|
|
if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(),
|
|
|
|
TU)))
|
2010-01-20 20:59:29 +00:00
|
|
|
return true;
|
|
|
|
|
2010-01-16 15:44:18 +00:00
|
|
|
ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin();
|
|
|
|
for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(),
|
|
|
|
E = ND->protocol_end(); I != E; ++I, ++PL)
|
2010-01-20 23:57:43 +00:00
|
|
|
if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
|
2010-01-20 20:59:29 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return VisitDeclContext(ND);
|
2010-01-13 00:22:49 +00:00
|
|
|
}
|
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
|
2010-01-13 00:22:49 +00:00
|
|
|
// Issue callbacks for super class.
|
2010-01-20 20:59:29 +00:00
|
|
|
if (D->getSuperClass() &&
|
|
|
|
Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
|
2010-01-20 23:57:43 +00:00
|
|
|
D->getSuperClassLoc(),
|
|
|
|
TU)))
|
2010-01-20 20:59:29 +00:00
|
|
|
return true;
|
2010-01-13 00:22:49 +00:00
|
|
|
|
2010-01-16 15:44:18 +00:00
|
|
|
ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
|
|
|
|
for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(),
|
|
|
|
E = D->protocol_end(); I != E; ++I, ++PL)
|
2010-01-20 23:57:43 +00:00
|
|
|
if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
|
2010-01-20 20:59:29 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return VisitDeclContext(D);
|
2010-01-13 00:22:49 +00:00
|
|
|
}
|
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
|
|
|
|
// FIXME: Wrong in the same way that VisitFunctionDecl is wrong.
|
2010-01-19 19:34:47 +00:00
|
|
|
if (ND->getBody())
|
2010-01-20 20:59:29 +00:00
|
|
|
return VisitDeclContext(ND);
|
|
|
|
|
|
|
|
return false;
|
2010-01-13 00:22:49 +00:00
|
|
|
}
|
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
|
2010-01-16 15:44:18 +00:00
|
|
|
ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin();
|
2010-01-13 00:22:49 +00:00
|
|
|
for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
|
2010-01-16 15:44:18 +00:00
|
|
|
E = PID->protocol_end(); I != E; ++I, ++PL)
|
2010-01-20 23:57:43 +00:00
|
|
|
if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
|
2010-01-20 20:59:29 +00:00
|
|
|
return true;
|
2010-01-13 00:22:49 +00:00
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
return VisitDeclContext(PID);
|
2010-01-13 00:22:49 +00:00
|
|
|
}
|
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
bool CursorVisitor::VisitTagDecl(TagDecl *D) {
|
|
|
|
return VisitDeclContext(D);
|
2010-01-13 00:22:49 +00:00
|
|
|
}
|
2009-10-18 16:11:04 +00:00
|
|
|
|
2010-01-21 16:28:34 +00:00
|
|
|
bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
|
|
|
|
return Visit(MakeCursorTypeRef(TL.getTypedefDecl(), TL.getNameLoc(), TU));
|
|
|
|
}
|
|
|
|
|
2010-01-12 02:34:07 +00:00
|
|
|
CXString CIndexer::createCXString(const char *String, bool DupString){
|
2009-11-09 19:13:48 +00:00
|
|
|
CXString Str;
|
|
|
|
if (DupString) {
|
|
|
|
Str.Spelling = strdup(String);
|
|
|
|
Str.MustFreeString = 1;
|
|
|
|
} else {
|
|
|
|
Str.Spelling = String;
|
|
|
|
Str.MustFreeString = 0;
|
|
|
|
}
|
|
|
|
return Str;
|
|
|
|
}
|
|
|
|
|
2009-10-18 16:11:04 +00:00
|
|
|
extern "C" {
|
2009-10-20 14:46:24 +00:00
|
|
|
CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
|
2009-12-01 03:14:51 +00:00
|
|
|
int displayDiagnostics) {
|
2009-10-20 14:46:24 +00:00
|
|
|
CIndexer *CIdxr = new CIndexer(new Program());
|
|
|
|
if (excludeDeclarationsFromPCH)
|
|
|
|
CIdxr->setOnlyLocalDecls();
|
|
|
|
if (displayDiagnostics)
|
|
|
|
CIdxr->setDisplayDiagnostics();
|
|
|
|
return CIdxr;
|
2009-08-27 19:51:58 +00:00
|
|
|
}
|
|
|
|
|
2009-12-01 03:14:51 +00:00
|
|
|
void clang_disposeIndex(CXIndex CIdx) {
|
2009-09-17 18:33:27 +00:00
|
|
|
assert(CIdx && "Passed null CXIndex");
|
2009-10-16 20:01:17 +00:00
|
|
|
delete static_cast<CIndexer *>(CIdx);
|
2009-09-17 18:33:27 +00:00
|
|
|
}
|
|
|
|
|
2009-12-03 01:54:28 +00:00
|
|
|
void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {
|
|
|
|
assert(CIdx && "Passed null CXIndex");
|
|
|
|
CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
|
|
|
|
CXXIdx->setUseExternalASTGeneration(value);
|
|
|
|
}
|
|
|
|
|
2009-08-28 15:28:48 +00:00
|
|
|
// FIXME: need to pass back error info.
|
2009-12-01 03:14:51 +00:00
|
|
|
CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
|
|
|
|
const char *ast_filename) {
|
2009-08-28 15:28:48 +00:00
|
|
|
assert(CIdx && "Passed null CXIndex");
|
2009-10-16 20:01:17 +00:00
|
|
|
CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
|
2009-11-30 20:42:43 +00:00
|
|
|
|
2009-12-03 01:45:44 +00:00
|
|
|
return ASTUnit::LoadFromPCHFile(ast_filename, CXXIdx->getDiags(),
|
|
|
|
CXXIdx->getOnlyLocalDecls(),
|
|
|
|
/* UseBumpAllocator = */ true);
|
2009-08-27 19:51:58 +00:00
|
|
|
}
|
|
|
|
|
2009-12-01 03:14:51 +00:00
|
|
|
CXTranslationUnit
|
|
|
|
clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
|
|
|
|
const char *source_filename,
|
|
|
|
int num_command_line_args,
|
|
|
|
const char **command_line_args) {
|
2009-10-20 14:46:24 +00:00
|
|
|
assert(CIdx && "Passed null CXIndex");
|
|
|
|
CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
|
|
|
|
|
2009-12-03 01:54:28 +00:00
|
|
|
if (!CXXIdx->getUseExternalASTGeneration()) {
|
|
|
|
llvm::SmallVector<const char *, 16> Args;
|
|
|
|
|
|
|
|
// The 'source_filename' argument is optional. If the caller does not
|
|
|
|
// specify it then it is assumed that the source file is specified
|
|
|
|
// in the actual argument list.
|
|
|
|
if (source_filename)
|
|
|
|
Args.push_back(source_filename);
|
|
|
|
Args.insert(Args.end(), command_line_args,
|
|
|
|
command_line_args + num_command_line_args);
|
|
|
|
|
2009-12-05 02:17:18 +00:00
|
|
|
unsigned NumErrors = CXXIdx->getDiags().getNumErrors();
|
2010-01-06 03:42:32 +00:00
|
|
|
|
2010-01-07 22:49:05 +00:00
|
|
|
#ifdef USE_CRASHTRACER
|
|
|
|
ArgsCrashTracerInfo ACTI(Args);
|
2010-01-06 03:42:32 +00:00
|
|
|
#endif
|
|
|
|
|
2009-12-05 02:17:18 +00:00
|
|
|
llvm::OwningPtr<ASTUnit> Unit(
|
|
|
|
ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
|
2009-12-13 03:46:13 +00:00
|
|
|
CXXIdx->getDiags(),
|
|
|
|
CXXIdx->getClangResourcesPath(),
|
2009-12-05 02:17:18 +00:00
|
|
|
CXXIdx->getOnlyLocalDecls(),
|
|
|
|
/* UseBumpAllocator = */ true));
|
2010-01-07 22:49:05 +00:00
|
|
|
|
2009-12-05 02:17:18 +00:00
|
|
|
// FIXME: Until we have broader testing, just drop the entire AST if we
|
|
|
|
// encountered an error.
|
|
|
|
if (NumErrors != CXXIdx->getDiags().getNumErrors())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return Unit.take();
|
2009-12-03 01:54:28 +00:00
|
|
|
}
|
|
|
|
|
2009-10-22 00:03:57 +00:00
|
|
|
// Build up the arguments for invoking 'clang'.
|
2009-10-15 23:21:22 +00:00
|
|
|
std::vector<const char *> argv;
|
2009-11-30 20:42:43 +00:00
|
|
|
|
2009-10-22 00:03:57 +00:00
|
|
|
// First add the complete path to the 'clang' executable.
|
|
|
|
llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
|
2009-10-18 16:11:04 +00:00
|
|
|
argv.push_back(ClangPath.c_str());
|
2009-11-30 20:42:43 +00:00
|
|
|
|
2009-10-22 00:03:57 +00:00
|
|
|
// Add the '-emit-ast' option as our execution mode for 'clang'.
|
2009-10-15 23:21:22 +00:00
|
|
|
argv.push_back("-emit-ast");
|
2009-11-30 20:42:43 +00:00
|
|
|
|
2009-10-22 00:03:57 +00:00
|
|
|
// The 'source_filename' argument is optional. If the caller does not
|
|
|
|
// specify it then it is assumed that the source file is specified
|
|
|
|
// in the actual argument list.
|
2009-11-30 20:42:43 +00:00
|
|
|
if (source_filename)
|
|
|
|
argv.push_back(source_filename);
|
2009-10-22 00:03:57 +00:00
|
|
|
|
2009-10-15 20:50:09 +00:00
|
|
|
// Generate a temporary name for the AST file.
|
2009-10-22 00:03:57 +00:00
|
|
|
argv.push_back("-o");
|
2009-10-15 20:50:09 +00:00
|
|
|
char astTmpFile[L_tmpnam];
|
2009-10-15 23:21:22 +00:00
|
|
|
argv.push_back(tmpnam(astTmpFile));
|
2009-10-22 00:03:57 +00:00
|
|
|
|
|
|
|
// Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
|
|
|
|
for (int i = 0; i < num_command_line_args; ++i)
|
|
|
|
if (const char *arg = command_line_args[i]) {
|
|
|
|
if (strcmp(arg, "-o") == 0) {
|
|
|
|
++i; // Also skip the matching argument.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strcmp(arg, "-emit-ast") == 0 ||
|
|
|
|
strcmp(arg, "-c") == 0 ||
|
|
|
|
strcmp(arg, "-fsyntax-only") == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep the argument.
|
|
|
|
argv.push_back(arg);
|
2009-10-20 14:46:24 +00:00
|
|
|
}
|
2009-11-30 20:42:43 +00:00
|
|
|
|
2009-10-22 00:03:57 +00:00
|
|
|
// Add the null terminator.
|
2009-10-15 23:21:22 +00:00
|
|
|
argv.push_back(NULL);
|
|
|
|
|
2009-10-26 22:14:08 +00:00
|
|
|
// Invoke 'clang'.
|
|
|
|
llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
|
|
|
|
// on Unix or NUL (Windows).
|
2009-10-22 03:24:01 +00:00
|
|
|
std::string ErrMsg;
|
2009-10-19 22:27:32 +00:00
|
|
|
const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL };
|
2009-10-22 03:24:01 +00:00
|
|
|
llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
|
|
|
|
/* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL,
|
|
|
|
/* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
|
2009-11-30 20:42:43 +00:00
|
|
|
|
2009-11-10 19:18:52 +00:00
|
|
|
if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) {
|
2009-11-30 20:42:43 +00:00
|
|
|
llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg
|
2009-11-30 20:42:49 +00:00
|
|
|
<< '\n' << "Arguments: \n";
|
2009-10-22 03:24:01 +00:00
|
|
|
for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
|
2009-10-26 22:08:39 +00:00
|
|
|
I!=E; ++I) {
|
|
|
|
if (*I)
|
|
|
|
llvm::errs() << ' ' << *I << '\n';
|
|
|
|
}
|
|
|
|
llvm::errs() << '\n';
|
2009-10-22 03:24:01 +00:00
|
|
|
}
|
2009-10-18 11:19:36 +00:00
|
|
|
|
2009-10-15 20:50:09 +00:00
|
|
|
// Finally, we create the translation unit from the ast file.
|
2009-10-15 22:23:48 +00:00
|
|
|
ASTUnit *ATU = static_cast<ASTUnit *>(
|
2009-11-30 20:42:49 +00:00
|
|
|
clang_createTranslationUnit(CIdx, astTmpFile));
|
2009-10-20 14:46:24 +00:00
|
|
|
if (ATU)
|
|
|
|
ATU->unlinkTemporaryFile();
|
2009-10-15 22:23:48 +00:00
|
|
|
return ATU;
|
2009-10-15 20:04:39 +00:00
|
|
|
}
|
|
|
|
|
2009-12-01 03:14:51 +00:00
|
|
|
void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
|
2009-09-17 18:33:27 +00:00
|
|
|
assert(CTUnit && "Passed null CXTranslationUnit");
|
|
|
|
delete static_cast<ASTUnit *>(CTUnit);
|
|
|
|
}
|
2009-11-30 20:42:43 +00:00
|
|
|
|
2009-12-01 03:14:51 +00:00
|
|
|
CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
|
2009-09-03 15:49:00 +00:00
|
|
|
assert(CTUnit && "Passed null CXTranslationUnit");
|
2009-09-03 18:19:54 +00:00
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
|
2010-01-12 00:36:38 +00:00
|
|
|
return CIndexer::createCXString(CXXUnit->getOriginalSourceFileName().c_str(),
|
|
|
|
true);
|
2009-09-03 15:49:00 +00:00
|
|
|
}
|
2009-08-28 16:30:07 +00:00
|
|
|
|
2010-01-20 00:23:15 +00:00
|
|
|
CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
|
2010-01-20 23:57:43 +00:00
|
|
|
CXCursor Result = { CXCursor_TranslationUnit, { 0, 0, TU } };
|
2010-01-20 00:23:15 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2010-01-13 21:46:36 +00:00
|
|
|
} // end: extern "C"
|
2009-08-27 19:51:58 +00:00
|
|
|
|
2010-01-19 21:36:55 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CXSourceLocation and CXSourceRange Operations.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void clang_getInstantiationLocation(CXSourceLocation location,
|
|
|
|
CXFile *file,
|
|
|
|
unsigned *line,
|
|
|
|
unsigned *column) {
|
|
|
|
CXSourceLocationPtr Ptr
|
|
|
|
= CXSourceLocationPtr::getFromOpaqueValue(location.ptr_data);
|
|
|
|
SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
|
|
|
|
|
|
|
|
if (!Ptr.getPointer() || Loc.isInvalid()) {
|
|
|
|
if (file)
|
|
|
|
*file = 0;
|
|
|
|
if (line)
|
|
|
|
*line = 0;
|
|
|
|
if (column)
|
|
|
|
*column = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: This is largely copy-paste from
|
|
|
|
///TextDiagnosticPrinter::HighlightRange. When it is clear that this is
|
|
|
|
// what we want the two routines should be refactored.
|
|
|
|
ASTContext &Context = *Ptr.getPointer();
|
|
|
|
SourceManager &SM = Context.getSourceManager();
|
|
|
|
SourceLocation InstLoc = SM.getInstantiationLoc(Loc);
|
|
|
|
|
|
|
|
if (Ptr.getInt()) {
|
|
|
|
// We want the last character in this location, so we will adjust
|
|
|
|
// the instantiation location accordingly.
|
|
|
|
|
|
|
|
// If the location is from a macro instantiation, get the end of
|
|
|
|
// the instantiation range.
|
|
|
|
if (Loc.isMacroID())
|
|
|
|
InstLoc = SM.getInstantiationRange(Loc).second;
|
|
|
|
|
|
|
|
// Measure the length token we're pointing at, so we can adjust
|
|
|
|
// the physical location in the file to point at the last
|
|
|
|
// character.
|
|
|
|
// FIXME: This won't cope with trigraphs or escaped newlines
|
|
|
|
// well. For that, we actually need a preprocessor, which isn't
|
|
|
|
// currently available here. Eventually, we'll switch the pointer
|
|
|
|
// data of CXSourceLocation/CXSourceRange to a translation unit
|
|
|
|
// (CXXUnit), so that the preprocessor will be available here. At
|
|
|
|
// that point, we can use Preprocessor::getLocForEndOfToken().
|
|
|
|
unsigned Length = Lexer::MeasureTokenLength(InstLoc, SM,
|
|
|
|
Context.getLangOptions());
|
|
|
|
if (Length > 0)
|
|
|
|
InstLoc = InstLoc.getFileLocWithOffset(Length - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file)
|
|
|
|
*file = (void *)SM.getFileEntryForID(SM.getFileID(InstLoc));
|
|
|
|
if (line)
|
|
|
|
*line = SM.getInstantiationLineNumber(InstLoc);
|
|
|
|
if (column)
|
|
|
|
*column = SM.getInstantiationColumnNumber(InstLoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
CXSourceLocation clang_getRangeStart(CXSourceRange range) {
|
|
|
|
CXSourceLocation Result = { range.ptr_data, range.begin_int_data };
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CXSourceLocation clang_getRangeEnd(CXSourceRange range) {
|
|
|
|
llvm::PointerIntPair<ASTContext *, 1, bool> Ptr;
|
|
|
|
Ptr.setPointer(static_cast<ASTContext *>(range.ptr_data));
|
|
|
|
Ptr.setInt(true);
|
|
|
|
CXSourceLocation Result = { Ptr.getOpaqueValue(), range.end_int_data };
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2010-01-13 21:46:36 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CXFile Operations.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
extern "C" {
|
2009-10-27 14:35:18 +00:00
|
|
|
const char *clang_getFileName(CXFile SFile) {
|
2010-01-18 22:46:11 +00:00
|
|
|
if (!SFile)
|
|
|
|
return 0;
|
|
|
|
|
2009-10-27 14:35:18 +00:00
|
|
|
assert(SFile && "Passed null CXFile");
|
|
|
|
FileEntry *FEnt = static_cast<FileEntry *>(SFile);
|
|
|
|
return FEnt->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
time_t clang_getFileTime(CXFile SFile) {
|
2010-01-18 22:46:11 +00:00
|
|
|
if (!SFile)
|
|
|
|
return 0;
|
|
|
|
|
2009-10-27 14:35:18 +00:00
|
|
|
assert(SFile && "Passed null CXFile");
|
|
|
|
FileEntry *FEnt = static_cast<FileEntry *>(SFile);
|
|
|
|
return FEnt->getModificationTime();
|
2009-09-25 21:45:39 +00:00
|
|
|
}
|
2010-01-13 21:46:36 +00:00
|
|
|
} // end: extern "C"
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CXCursor Operations.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
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-25 21:45:39 +00:00
|
|
|
|
2010-01-13 21:46:36 +00:00
|
|
|
extern "C" {
|
2010-01-20 20:59:29 +00:00
|
|
|
|
2010-01-20 23:57:43 +00:00
|
|
|
unsigned clang_visitChildren(CXCursor parent,
|
2010-01-20 20:59:29 +00:00
|
|
|
CXCursorVisitor visitor,
|
|
|
|
CXClientData client_data) {
|
2010-01-20 23:57:43 +00:00
|
|
|
ASTUnit *CXXUnit = getCursorASTUnit(parent);
|
2010-01-20 20:59:29 +00:00
|
|
|
|
|
|
|
unsigned PCHLevel = Decl::MaxPCHLevel;
|
|
|
|
|
|
|
|
// Set the PCHLevel to filter out unwanted decls if requested.
|
|
|
|
if (CXXUnit->getOnlyLocalDecls()) {
|
|
|
|
PCHLevel = 0;
|
|
|
|
|
|
|
|
// If the main input was an AST, bump the level.
|
|
|
|
if (CXXUnit->isMainFileAST())
|
|
|
|
++PCHLevel;
|
|
|
|
}
|
|
|
|
|
2010-01-20 23:57:43 +00:00
|
|
|
CursorVisitor CursorVis(CXXUnit, visitor, client_data, PCHLevel);
|
2010-01-20 20:59:29 +00:00
|
|
|
return CursorVis.VisitChildren(parent);
|
|
|
|
}
|
|
|
|
|
2010-01-20 21:45:58 +00:00
|
|
|
static CXString getDeclSpelling(Decl *D) {
|
|
|
|
NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D);
|
|
|
|
if (!ND)
|
|
|
|
return CIndexer::createCXString("");
|
|
|
|
|
|
|
|
if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
|
|
|
|
return CIndexer::createCXString(OMD->getSelector().getAsString().c_str(),
|
|
|
|
true);
|
|
|
|
|
|
|
|
if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
|
|
|
|
// No, this isn't the same as the code below. getIdentifier() is non-virtual
|
|
|
|
// and returns different names. NamedDecl returns the class name and
|
|
|
|
// ObjCCategoryImplDecl returns the category name.
|
|
|
|
return CIndexer::createCXString(CIMP->getIdentifier()->getNameStart());
|
|
|
|
|
|
|
|
if (ND->getIdentifier())
|
|
|
|
return CIndexer::createCXString(ND->getIdentifier()->getNameStart());
|
|
|
|
|
|
|
|
return CIndexer::createCXString("");
|
|
|
|
}
|
|
|
|
|
2009-12-01 03:14:51 +00:00
|
|
|
CXString clang_getCursorSpelling(CXCursor C) {
|
2010-01-18 17:52:42 +00:00
|
|
|
assert(getCursorDecl(C) && "CXCursor has null decl");
|
2010-01-20 00:23:15 +00:00
|
|
|
if (clang_isTranslationUnit(C.kind))
|
2010-01-20 23:57:43 +00:00
|
|
|
return clang_getTranslationUnitSpelling(C.data[2]);
|
2010-01-20 00:23:15 +00:00
|
|
|
|
2009-09-02 18:26:48 +00:00
|
|
|
if (clang_isReference(C.kind)) {
|
|
|
|
switch (C.kind) {
|
2009-11-30 20:42:49 +00:00
|
|
|
case CXCursor_ObjCSuperClassRef: {
|
2010-01-16 14:00:32 +00:00
|
|
|
ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
|
|
|
|
return CIndexer::createCXString(Super->getIdentifier()->getNameStart());
|
2009-11-30 20:42:49 +00:00
|
|
|
}
|
|
|
|
case CXCursor_ObjCClassRef: {
|
2010-01-16 17:14:40 +00:00
|
|
|
ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
|
|
|
|
return CIndexer::createCXString(Class->getIdentifier()->getNameStart());
|
2009-11-30 20:42:49 +00:00
|
|
|
}
|
|
|
|
case CXCursor_ObjCProtocolRef: {
|
2010-01-16 15:44:18 +00:00
|
|
|
ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
|
2010-01-18 23:41:10 +00:00
|
|
|
assert(OID && "getCursorSpelling(): Missing protocol decl");
|
2010-01-12 00:36:38 +00:00
|
|
|
return CIndexer::createCXString(OID->getIdentifier()->getNameStart());
|
2009-11-30 20:42:49 +00:00
|
|
|
}
|
2010-01-21 16:28:34 +00:00
|
|
|
case CXCursor_TypeRef: {
|
|
|
|
TypeDecl *Type = getCursorTypeRef(C).first;
|
|
|
|
assert(Type && "Missing type decl");
|
|
|
|
|
|
|
|
return CIndexer::createCXString(
|
|
|
|
getCursorContext(C).getTypeDeclType(Type).getAsString().c_str(),
|
|
|
|
true);
|
|
|
|
}
|
|
|
|
|
2009-11-30 20:42:49 +00:00
|
|
|
default:
|
2010-01-12 00:36:38 +00:00
|
|
|
return CIndexer::createCXString("<not implemented>");
|
2009-09-02 18:26:48 +00:00
|
|
|
}
|
|
|
|
}
|
2010-01-19 23:20:36 +00:00
|
|
|
|
|
|
|
if (clang_isExpression(C.kind)) {
|
|
|
|
Decl *D = getDeclFromExpr(getCursorExpr(C));
|
|
|
|
if (D)
|
2010-01-20 21:45:58 +00:00
|
|
|
return getDeclSpelling(D);
|
2010-01-19 23:20:36 +00:00
|
|
|
return CIndexer::createCXString("");
|
|
|
|
}
|
|
|
|
|
2010-01-20 21:45:58 +00:00
|
|
|
return getDeclSpelling(getCursorDecl(C));
|
2009-09-02 18:26:48 +00:00
|
|
|
}
|
|
|
|
|
2009-12-01 03:14:51 +00:00
|
|
|
const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) {
|
2009-08-31 00:59:03 +00:00
|
|
|
switch (Kind) {
|
2009-11-30 20:42:49 +00:00
|
|
|
case CXCursor_FunctionDecl: return "FunctionDecl";
|
|
|
|
case CXCursor_TypedefDecl: return "TypedefDecl";
|
|
|
|
case CXCursor_EnumDecl: return "EnumDecl";
|
|
|
|
case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
|
|
|
|
case CXCursor_StructDecl: return "StructDecl";
|
|
|
|
case CXCursor_UnionDecl: return "UnionDecl";
|
|
|
|
case CXCursor_ClassDecl: return "ClassDecl";
|
|
|
|
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";
|
|
|
|
case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
|
|
|
|
case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
|
2010-01-19 19:34:47 +00:00
|
|
|
case CXCursor_ObjCImplementationDecl: return "ObjCImplementationDecl";
|
|
|
|
case CXCursor_ObjCCategoryImplDecl: return "ObjCCategoryImplDecl";
|
2010-01-19 22:07:56 +00:00
|
|
|
case CXCursor_UnexposedDecl: return "UnexposedDecl";
|
2009-11-30 20:42:49 +00:00
|
|
|
case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
|
|
|
|
case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
|
|
|
|
case CXCursor_ObjCClassRef: return "ObjCClassRef";
|
2010-01-21 16:28:34 +00:00
|
|
|
case CXCursor_TypeRef: return "TypeRef";
|
2010-01-19 23:20:36 +00:00
|
|
|
case CXCursor_UnexposedExpr: return "UnexposedExpr";
|
|
|
|
case CXCursor_DeclRefExpr: return "DeclRefExpr";
|
|
|
|
case CXCursor_MemberRefExpr: return "MemberRefExpr";
|
|
|
|
case CXCursor_CallExpr: return "CallExpr";
|
|
|
|
case CXCursor_ObjCMessageExpr: return "ObjCMessageExpr";
|
|
|
|
case CXCursor_UnexposedStmt: return "UnexposedStmt";
|
2009-11-30 20:42:49 +00:00
|
|
|
case CXCursor_InvalidFile: return "InvalidFile";
|
|
|
|
case CXCursor_NoDeclFound: return "NoDeclFound";
|
|
|
|
case CXCursor_NotImplemented: return "NotImplemented";
|
2010-01-20 00:23:15 +00:00
|
|
|
case CXCursor_TranslationUnit: return "TranslationUnit";
|
2009-08-31 00:59:03 +00:00
|
|
|
}
|
2010-01-16 02:02:09 +00:00
|
|
|
|
|
|
|
llvm_unreachable("Unhandled CXCursorKind");
|
|
|
|
return NULL;
|
2009-08-27 19:51:58 +00:00
|
|
|
}
|
2009-08-31 00:59:03 +00:00
|
|
|
|
2009-11-30 20:42:43 +00:00
|
|
|
CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
|
2009-12-01 03:14:51 +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);
|
2009-11-30 20:42:43 +00:00
|
|
|
|
2009-09-04 15:44:05 +00:00
|
|
|
FileManager &FMgr = CXXUnit->getFileManager();
|
2009-11-30 20:42:43 +00:00
|
|
|
const FileEntry *File = FMgr.getFile(source_name,
|
|
|
|
source_name+strlen(source_name));
|
2010-01-14 01:51:23 +00:00
|
|
|
if (!File)
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
2009-11-30 20:42:43 +00:00
|
|
|
SourceLocation SLoc =
|
2009-09-04 15:44:05 +00:00
|
|
|
CXXUnit->getSourceManager().getLocation(File, line, column);
|
2009-11-30 20:42:43 +00:00
|
|
|
|
2009-10-28 20:44:47 +00:00
|
|
|
ASTLocation LastLoc = CXXUnit->getLastASTLocation();
|
2009-11-30 20:42:43 +00:00
|
|
|
ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc,
|
2009-10-28 20:44:47 +00:00
|
|
|
&LastLoc);
|
2010-01-14 01:51:23 +00:00
|
|
|
|
|
|
|
// FIXME: This doesn't look thread-safe.
|
2009-10-28 20:44:47 +00:00
|
|
|
if (ALoc.isValid())
|
|
|
|
CXXUnit->setLastASTLocation(ALoc);
|
2009-11-30 20:42:43 +00:00
|
|
|
|
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) {
|
2010-01-19 23:20:36 +00:00
|
|
|
if (Stm)
|
2010-01-20 23:57:43 +00:00
|
|
|
return MakeCXCursor(Stm, Dcl, CXXUnit);
|
2010-01-19 23:20:36 +00:00
|
|
|
|
2009-10-01 00:31:07 +00:00
|
|
|
if (ALoc.isNamedRef()) {
|
2010-01-16 17:14:40 +00:00
|
|
|
if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(Dcl))
|
2010-01-20 23:57:43 +00:00
|
|
|
return MakeCursorObjCClassRef(Class, ALoc.AsNamedRef().Loc, CXXUnit);
|
2010-01-16 15:44:18 +00:00
|
|
|
if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(Dcl))
|
2010-01-20 23:57:43 +00:00
|
|
|
return MakeCursorObjCProtocolRef(Proto, ALoc.AsNamedRef().Loc, CXXUnit);
|
2009-10-01 00:31:07 +00:00
|
|
|
}
|
2010-01-20 23:57:43 +00:00
|
|
|
return MakeCXCursor(Dcl, CXXUnit);
|
2009-09-15 20:25:34 +00:00
|
|
|
}
|
2010-01-20 23:34:41 +00:00
|
|
|
return MakeCXCursorInvalid(CXCursor_NoDeclFound);
|
2009-09-15 20:25:34 +00:00
|
|
|
}
|
|
|
|
|
2009-11-17 19:28:59 +00:00
|
|
|
CXCursor clang_getNullCursor(void) {
|
2010-01-20 23:34:41 +00:00
|
|
|
return MakeCXCursorInvalid(CXCursor_InvalidFile);
|
2009-11-17 19:28:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
|
2010-01-15 21:56:13 +00:00
|
|
|
return X == Y;
|
2009-11-17 19:28:59 +00:00
|
|
|
}
|
2009-11-30 20:42:43 +00:00
|
|
|
|
2009-12-01 03:14:51 +00:00
|
|
|
unsigned clang_isInvalid(enum CXCursorKind K) {
|
2009-09-15 20:25:34 +00:00
|
|
|
return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
|
|
|
|
}
|
|
|
|
|
2009-12-01 03:14:51 +00:00
|
|
|
unsigned clang_isDeclaration(enum CXCursorKind K) {
|
2009-08-31 00:59:03 +00:00
|
|
|
return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
|
|
|
|
}
|
2009-08-31 14:26:51 +00:00
|
|
|
|
2009-12-01 03:14:51 +00:00
|
|
|
unsigned clang_isReference(enum CXCursorKind K) {
|
2009-09-02 18:26:48 +00:00
|
|
|
return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
|
|
|
|
}
|
|
|
|
|
2010-01-19 23:20:36 +00:00
|
|
|
unsigned clang_isExpression(enum CXCursorKind K) {
|
|
|
|
return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_isStatement(enum CXCursorKind K) {
|
|
|
|
return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt;
|
|
|
|
}
|
|
|
|
|
2010-01-20 00:23:15 +00:00
|
|
|
unsigned clang_isTranslationUnit(enum CXCursorKind K) {
|
|
|
|
return K == CXCursor_TranslationUnit;
|
|
|
|
}
|
|
|
|
|
2009-12-01 03:14:51 +00:00
|
|
|
CXCursorKind clang_getCursorKind(CXCursor C) {
|
2009-09-04 15:44:05 +00:00
|
|
|
return C.kind;
|
|
|
|
}
|
|
|
|
|
2010-01-19 23:20:36 +00:00
|
|
|
static SourceLocation getLocationFromExpr(Expr *E) {
|
|
|
|
if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E))
|
|
|
|
return /*FIXME:*/Msg->getLeftLoc();
|
|
|
|
if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
|
|
|
|
return DRE->getLocation();
|
|
|
|
if (MemberExpr *Member = dyn_cast<MemberExpr>(E))
|
|
|
|
return Member->getMemberLoc();
|
|
|
|
if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E))
|
|
|
|
return Ivar->getLocation();
|
|
|
|
return E->getLocStart();
|
|
|
|
}
|
|
|
|
|
2010-01-18 22:46:11 +00:00
|
|
|
CXSourceLocation clang_getCursorLocation(CXCursor C) {
|
|
|
|
if (clang_isReference(C.kind)) {
|
2010-01-18 23:41:10 +00:00
|
|
|
switch (C.kind) {
|
|
|
|
case CXCursor_ObjCSuperClassRef: {
|
|
|
|
std::pair<ObjCInterfaceDecl *, SourceLocation> P
|
|
|
|
= getCursorObjCSuperClassRef(C);
|
2010-01-19 21:36:55 +00:00
|
|
|
return translateSourceLocation(P.first->getASTContext(), P.second);
|
2010-01-18 23:41:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case CXCursor_ObjCProtocolRef: {
|
|
|
|
std::pair<ObjCProtocolDecl *, SourceLocation> P
|
|
|
|
= getCursorObjCProtocolRef(C);
|
2010-01-19 21:36:55 +00:00
|
|
|
return translateSourceLocation(P.first->getASTContext(), P.second);
|
2010-01-18 23:41:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case CXCursor_ObjCClassRef: {
|
|
|
|
std::pair<ObjCInterfaceDecl *, SourceLocation> P
|
|
|
|
= getCursorObjCClassRef(C);
|
2010-01-19 21:36:55 +00:00
|
|
|
return translateSourceLocation(P.first->getASTContext(), P.second);
|
2010-01-18 23:41:10 +00:00
|
|
|
}
|
2010-01-21 16:28:34 +00:00
|
|
|
|
|
|
|
case CXCursor_TypeRef: {
|
|
|
|
std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
|
|
|
|
return translateSourceLocation(P.first->getASTContext(), P.second);
|
|
|
|
}
|
2010-01-18 23:41:10 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
// FIXME: Need a way to enumerate all non-reference cases.
|
|
|
|
llvm_unreachable("Missed a reference kind");
|
|
|
|
}
|
2010-01-18 22:46:11 +00:00
|
|
|
}
|
2010-01-19 23:20:36 +00:00
|
|
|
|
|
|
|
if (clang_isExpression(C.kind))
|
|
|
|
return translateSourceLocation(getCursorContext(C),
|
|
|
|
getLocationFromExpr(getCursorExpr(C)));
|
|
|
|
|
2010-01-18 22:46:11 +00:00
|
|
|
if (!getCursorDecl(C)) {
|
2010-01-19 21:36:55 +00:00
|
|
|
CXSourceLocation empty = { 0, 0 };
|
2010-01-18 22:46:11 +00:00
|
|
|
return empty;
|
|
|
|
}
|
|
|
|
|
2010-01-18 23:41:10 +00:00
|
|
|
Decl *D = getCursorDecl(C);
|
|
|
|
SourceLocation Loc = D->getLocation();
|
|
|
|
if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
|
|
|
|
Loc = Class->getClassLoc();
|
2010-01-19 21:36:55 +00:00
|
|
|
return translateSourceLocation(D->getASTContext(), Loc);
|
2009-10-27 14:35:18 +00:00
|
|
|
}
|
2010-01-19 00:34:46 +00:00
|
|
|
|
|
|
|
CXSourceRange clang_getCursorExtent(CXCursor C) {
|
|
|
|
if (clang_isReference(C.kind)) {
|
|
|
|
switch (C.kind) {
|
|
|
|
case CXCursor_ObjCSuperClassRef: {
|
|
|
|
std::pair<ObjCInterfaceDecl *, SourceLocation> P
|
|
|
|
= getCursorObjCSuperClassRef(C);
|
|
|
|
return translateSourceRange(P.first->getASTContext(), P.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
case CXCursor_ObjCProtocolRef: {
|
|
|
|
std::pair<ObjCProtocolDecl *, SourceLocation> P
|
|
|
|
= getCursorObjCProtocolRef(C);
|
|
|
|
return translateSourceRange(P.first->getASTContext(), P.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
case CXCursor_ObjCClassRef: {
|
|
|
|
std::pair<ObjCInterfaceDecl *, SourceLocation> P
|
|
|
|
= getCursorObjCClassRef(C);
|
|
|
|
|
|
|
|
return translateSourceRange(P.first->getASTContext(), P.second);
|
|
|
|
}
|
2010-01-21 16:28:34 +00:00
|
|
|
|
|
|
|
case CXCursor_TypeRef: {
|
|
|
|
std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
|
|
|
|
return translateSourceRange(P.first->getASTContext(), P.second);
|
|
|
|
}
|
2010-01-19 00:34:46 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
// FIXME: Need a way to enumerate all non-reference cases.
|
|
|
|
llvm_unreachable("Missed a reference kind");
|
|
|
|
}
|
|
|
|
}
|
2010-01-19 23:20:36 +00:00
|
|
|
|
|
|
|
if (clang_isExpression(C.kind))
|
|
|
|
return translateSourceRange(getCursorContext(C),
|
|
|
|
getCursorExpr(C)->getSourceRange());
|
2010-01-19 00:34:46 +00:00
|
|
|
|
|
|
|
if (!getCursorDecl(C)) {
|
2010-01-19 21:36:55 +00:00
|
|
|
CXSourceRange empty = { 0, 0, 0 };
|
2010-01-19 00:34:46 +00:00
|
|
|
return empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
Decl *D = getCursorDecl(C);
|
|
|
|
return translateSourceRange(D->getASTContext(), D->getSourceRange());
|
|
|
|
}
|
2010-01-19 01:20:04 +00:00
|
|
|
|
|
|
|
CXCursor clang_getCursorReferenced(CXCursor C) {
|
2010-01-20 23:57:43 +00:00
|
|
|
if (clang_isInvalid(C.kind))
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
ASTUnit *CXXUnit = getCursorASTUnit(C);
|
2010-01-19 19:34:47 +00:00
|
|
|
if (clang_isDeclaration(C.kind))
|
2010-01-19 01:20:04 +00:00
|
|
|
return C;
|
|
|
|
|
2010-01-19 23:20:36 +00:00
|
|
|
if (clang_isExpression(C.kind)) {
|
|
|
|
Decl *D = getDeclFromExpr(getCursorExpr(C));
|
|
|
|
if (D)
|
2010-01-20 23:57:43 +00:00
|
|
|
return MakeCXCursor(D, CXXUnit);
|
2010-01-19 23:20:36 +00:00
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
2010-01-19 01:20:04 +00:00
|
|
|
if (!clang_isReference(C.kind))
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
switch (C.kind) {
|
|
|
|
case CXCursor_ObjCSuperClassRef:
|
2010-01-20 23:57:43 +00:00
|
|
|
return MakeCXCursor(getCursorObjCSuperClassRef(C).first, CXXUnit);
|
2010-01-19 01:20:04 +00:00
|
|
|
|
|
|
|
case CXCursor_ObjCProtocolRef: {
|
2010-01-20 23:57:43 +00:00
|
|
|
return MakeCXCursor(getCursorObjCProtocolRef(C).first, CXXUnit);
|
2010-01-19 01:20:04 +00:00
|
|
|
|
|
|
|
case CXCursor_ObjCClassRef:
|
2010-01-20 23:57:43 +00:00
|
|
|
return MakeCXCursor(getCursorObjCClassRef(C).first, CXXUnit);
|
2010-01-21 16:28:34 +00:00
|
|
|
|
|
|
|
case CXCursor_TypeRef:
|
|
|
|
return MakeCXCursor(getCursorTypeRef(C).first, CXXUnit);
|
2010-01-19 01:20:04 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
// We would prefer to enumerate all non-reference cursor kinds here.
|
|
|
|
llvm_unreachable("Unhandled reference cursor kind");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-01-18 22:46:11 +00:00
|
|
|
|
2010-01-19 01:20:04 +00:00
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
2010-01-19 19:34:47 +00:00
|
|
|
CXCursor clang_getCursorDefinition(CXCursor C) {
|
2010-01-20 23:57:43 +00:00
|
|
|
if (clang_isInvalid(C.kind))
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
ASTUnit *CXXUnit = getCursorASTUnit(C);
|
|
|
|
|
2010-01-19 19:34:47 +00:00
|
|
|
bool WasReference = false;
|
2010-01-19 23:20:36 +00:00
|
|
|
if (clang_isReference(C.kind) || clang_isExpression(C.kind)) {
|
2010-01-19 19:34:47 +00:00
|
|
|
C = clang_getCursorReferenced(C);
|
|
|
|
WasReference = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!clang_isDeclaration(C.kind))
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
Decl *D = getCursorDecl(C);
|
|
|
|
if (!D)
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
switch (D->getKind()) {
|
|
|
|
// Declaration kinds that don't really separate the notions of
|
|
|
|
// declaration and definition.
|
|
|
|
case Decl::Namespace:
|
|
|
|
case Decl::Typedef:
|
|
|
|
case Decl::TemplateTypeParm:
|
|
|
|
case Decl::EnumConstant:
|
|
|
|
case Decl::Field:
|
|
|
|
case Decl::ObjCIvar:
|
|
|
|
case Decl::ObjCAtDefsField:
|
|
|
|
case Decl::ImplicitParam:
|
|
|
|
case Decl::ParmVar:
|
|
|
|
case Decl::NonTypeTemplateParm:
|
|
|
|
case Decl::TemplateTemplateParm:
|
|
|
|
case Decl::ObjCCategoryImpl:
|
|
|
|
case Decl::ObjCImplementation:
|
|
|
|
case Decl::LinkageSpec:
|
|
|
|
case Decl::ObjCPropertyImpl:
|
|
|
|
case Decl::FileScopeAsm:
|
|
|
|
case Decl::StaticAssert:
|
|
|
|
case Decl::Block:
|
|
|
|
return C;
|
|
|
|
|
|
|
|
// Declaration kinds that don't make any sense here, but are
|
|
|
|
// nonetheless harmless.
|
|
|
|
case Decl::TranslationUnit:
|
|
|
|
case Decl::Template:
|
|
|
|
case Decl::ObjCContainer:
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Declaration kinds for which the definition is not resolvable.
|
|
|
|
case Decl::UnresolvedUsingTypename:
|
|
|
|
case Decl::UnresolvedUsingValue:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Decl::UsingDirective:
|
2010-01-20 23:57:43 +00:00
|
|
|
return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(),
|
|
|
|
CXXUnit);
|
2010-01-19 19:34:47 +00:00
|
|
|
|
|
|
|
case Decl::NamespaceAlias:
|
2010-01-20 23:57:43 +00:00
|
|
|
return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), CXXUnit);
|
2010-01-19 19:34:47 +00:00
|
|
|
|
|
|
|
case Decl::Enum:
|
|
|
|
case Decl::Record:
|
|
|
|
case Decl::CXXRecord:
|
|
|
|
case Decl::ClassTemplateSpecialization:
|
|
|
|
case Decl::ClassTemplatePartialSpecialization:
|
|
|
|
if (TagDecl *Def = cast<TagDecl>(D)->getDefinition(D->getASTContext()))
|
2010-01-20 23:57:43 +00:00
|
|
|
return MakeCXCursor(Def, CXXUnit);
|
2010-01-19 19:34:47 +00:00
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
case Decl::Function:
|
|
|
|
case Decl::CXXMethod:
|
|
|
|
case Decl::CXXConstructor:
|
|
|
|
case Decl::CXXDestructor:
|
|
|
|
case Decl::CXXConversion: {
|
|
|
|
const FunctionDecl *Def = 0;
|
|
|
|
if (cast<FunctionDecl>(D)->getBody(Def))
|
2010-01-20 23:57:43 +00:00
|
|
|
return MakeCXCursor(const_cast<FunctionDecl *>(Def), CXXUnit);
|
2010-01-19 19:34:47 +00:00
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
case Decl::Var: {
|
|
|
|
VarDecl *Var = cast<VarDecl>(D);
|
|
|
|
|
|
|
|
// Variables with initializers have definitions.
|
|
|
|
const VarDecl *Def = 0;
|
|
|
|
if (Var->getDefinition(Def))
|
2010-01-20 23:57:43 +00:00
|
|
|
return MakeCXCursor(const_cast<VarDecl *>(Def), CXXUnit);
|
2010-01-19 19:34:47 +00:00
|
|
|
|
|
|
|
// extern and private_extern variables are not definitions.
|
|
|
|
if (Var->hasExternalStorage())
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
// In-line static data members do not have definitions.
|
|
|
|
if (Var->isStaticDataMember() && !Var->isOutOfLine())
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
// All other variables are themselves definitions.
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Decl::FunctionTemplate: {
|
|
|
|
const FunctionDecl *Def = 0;
|
|
|
|
if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def))
|
2010-01-20 23:57:43 +00:00
|
|
|
return MakeCXCursor(Def->getDescribedFunctionTemplate(), CXXUnit);
|
2010-01-19 19:34:47 +00:00
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
case Decl::ClassTemplate: {
|
|
|
|
if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl()
|
|
|
|
->getDefinition(D->getASTContext()))
|
|
|
|
return MakeCXCursor(
|
2010-01-20 23:57:43 +00:00
|
|
|
cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(),
|
|
|
|
CXXUnit);
|
2010-01-19 19:34:47 +00:00
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
case Decl::Using: {
|
|
|
|
UsingDecl *Using = cast<UsingDecl>(D);
|
|
|
|
CXCursor Def = clang_getNullCursor();
|
|
|
|
for (UsingDecl::shadow_iterator S = Using->shadow_begin(),
|
|
|
|
SEnd = Using->shadow_end();
|
|
|
|
S != SEnd; ++S) {
|
|
|
|
if (Def != clang_getNullCursor()) {
|
|
|
|
// FIXME: We have no way to return multiple results.
|
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
2010-01-20 23:57:43 +00:00
|
|
|
Def = clang_getCursorDefinition(MakeCXCursor((*S)->getTargetDecl(),
|
|
|
|
CXXUnit));
|
2010-01-19 19:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Def;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Decl::UsingShadow:
|
|
|
|
return clang_getCursorDefinition(
|
2010-01-20 23:57:43 +00:00
|
|
|
MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(),
|
|
|
|
CXXUnit));
|
2010-01-19 19:34:47 +00:00
|
|
|
|
|
|
|
case Decl::ObjCMethod: {
|
|
|
|
ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D);
|
|
|
|
if (Method->isThisDeclarationADefinition())
|
|
|
|
return C;
|
|
|
|
|
|
|
|
// Dig out the method definition in the associated
|
|
|
|
// @implementation, if we have it.
|
|
|
|
// FIXME: The ASTs should make finding the definition easier.
|
|
|
|
if (ObjCInterfaceDecl *Class
|
|
|
|
= dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext()))
|
|
|
|
if (ObjCImplementationDecl *ClassImpl = Class->getImplementation())
|
|
|
|
if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(),
|
|
|
|
Method->isInstanceMethod()))
|
|
|
|
if (Def->isThisDeclarationADefinition())
|
2010-01-20 23:57:43 +00:00
|
|
|
return MakeCXCursor(Def, CXXUnit);
|
2010-01-19 19:34:47 +00:00
|
|
|
|
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
case Decl::ObjCCategory:
|
|
|
|
if (ObjCCategoryImplDecl *Impl
|
|
|
|
= cast<ObjCCategoryDecl>(D)->getImplementation())
|
2010-01-20 23:57:43 +00:00
|
|
|
return MakeCXCursor(Impl, CXXUnit);
|
2010-01-19 19:34:47 +00:00
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
case Decl::ObjCProtocol:
|
|
|
|
if (!cast<ObjCProtocolDecl>(D)->isForwardDecl())
|
|
|
|
return C;
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
case Decl::ObjCInterface:
|
|
|
|
// There are two notions of a "definition" for an Objective-C
|
|
|
|
// class: the interface and its implementation. When we resolved a
|
|
|
|
// reference to an Objective-C class, produce the @interface as
|
|
|
|
// the definition; when we were provided with the interface,
|
|
|
|
// produce the @implementation as the definition.
|
|
|
|
if (WasReference) {
|
|
|
|
if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl())
|
|
|
|
return C;
|
|
|
|
} else if (ObjCImplementationDecl *Impl
|
|
|
|
= cast<ObjCInterfaceDecl>(D)->getImplementation())
|
2010-01-20 23:57:43 +00:00
|
|
|
return MakeCXCursor(Impl, CXXUnit);
|
2010-01-19 19:34:47 +00:00
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
case Decl::ObjCProperty:
|
|
|
|
// FIXME: We don't really know where to find the
|
|
|
|
// ObjCPropertyImplDecls that implement this property.
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
case Decl::ObjCCompatibleAlias:
|
|
|
|
if (ObjCInterfaceDecl *Class
|
|
|
|
= cast<ObjCCompatibleAliasDecl>(D)->getClassInterface())
|
|
|
|
if (!Class->isForwardDecl())
|
2010-01-20 23:57:43 +00:00
|
|
|
return MakeCXCursor(Class, CXXUnit);
|
2010-01-19 19:34:47 +00:00
|
|
|
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
case Decl::ObjCForwardProtocol: {
|
|
|
|
ObjCForwardProtocolDecl *Forward = cast<ObjCForwardProtocolDecl>(D);
|
|
|
|
if (Forward->protocol_size() == 1)
|
|
|
|
return clang_getCursorDefinition(
|
2010-01-20 23:57:43 +00:00
|
|
|
MakeCXCursor(*Forward->protocol_begin(),
|
|
|
|
CXXUnit));
|
2010-01-19 19:34:47 +00:00
|
|
|
|
|
|
|
// FIXME: Cannot return multiple definitions.
|
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
case Decl::ObjCClass: {
|
|
|
|
ObjCClassDecl *Class = cast<ObjCClassDecl>(D);
|
|
|
|
if (Class->size() == 1) {
|
|
|
|
ObjCInterfaceDecl *IFace = Class->begin()->getInterface();
|
|
|
|
if (!IFace->isForwardDecl())
|
2010-01-20 23:57:43 +00:00
|
|
|
return MakeCXCursor(IFace, CXXUnit);
|
2010-01-19 19:34:47 +00:00
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Cannot return multiple definitions.
|
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
case Decl::Friend:
|
|
|
|
if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl())
|
2010-01-20 23:57:43 +00:00
|
|
|
return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
|
2010-01-19 19:34:47 +00:00
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
case Decl::FriendTemplate:
|
|
|
|
if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl())
|
2010-01-20 23:57:43 +00:00
|
|
|
return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
|
2010-01-19 19:34:47 +00:00
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_isCursorDefinition(CXCursor C) {
|
|
|
|
if (!clang_isDeclaration(C.kind))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return clang_getCursorDefinition(C) == C;
|
|
|
|
}
|
|
|
|
|
2009-11-30 20:42:43 +00:00
|
|
|
void clang_getDefinitionSpellingAndExtent(CXCursor C,
|
2009-09-23 17:52:52 +00:00
|
|
|
const char **startBuf,
|
|
|
|
const char **endBuf,
|
|
|
|
unsigned *startLine,
|
|
|
|
unsigned *startColumn,
|
|
|
|
unsigned *endLine,
|
2009-12-01 03:14:51 +00:00
|
|
|
unsigned *endColumn) {
|
2010-01-15 21:56:13 +00:00
|
|
|
assert(getCursorDecl(C) && "CXCursor has null decl");
|
|
|
|
NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
|
2009-09-23 17:52:52 +00:00
|
|
|
FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
|
|
|
|
CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
|
2010-01-13 21:46:36 +00:00
|
|
|
|
2009-09-23 17:52:52 +00:00
|
|
|
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());
|
|
|
|
}
|
2010-01-13 21:46:36 +00:00
|
|
|
|
|
|
|
} // end: extern "C"
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CXString Operations.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
const char *clang_getCString(CXString string) {
|
|
|
|
return string.Spelling;
|
|
|
|
}
|
2009-09-23 17:52:52 +00:00
|
|
|
|
2010-01-13 21:46:36 +00:00
|
|
|
void clang_disposeString(CXString string) {
|
|
|
|
if (string.MustFreeString && string.Spelling)
|
|
|
|
free((void*)string.Spelling);
|
|
|
|
}
|
|
|
|
} // end: extern "C"
|