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-25 22:34:44 +00:00
|
|
|
#include "CXSourceLocation.h"
|
2010-01-28 00:27:43 +00:00
|
|
|
#include "CIndexDiagnostic.h"
|
2010-01-05 19:32:54 +00:00
|
|
|
|
2010-01-22 22:44:15 +00:00
|
|
|
#include "clang/Basic/Version.h"
|
2010-01-28 00:56:43 +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-28 00:56:43 +00:00
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
2010-01-06 23:43:31 +00:00
|
|
|
#include "clang/Lex/Lexer.h"
|
2010-01-22 19:49:59 +00:00
|
|
|
#include "clang/Lex/Preprocessor.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"
|
2010-02-18 23:07:20 +00:00
|
|
|
#include "llvm/System/Signals.h"
|
2009-10-19 21:44:57 +00:00
|
|
|
|
2010-03-13 21:22:49 +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;
|
2010-02-17 00:41:08 +00:00
|
|
|
using namespace clang::cxstring;
|
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
|
|
|
#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-02-17 21:12:23 +00:00
|
|
|
#define NUM_CRASH_STRINGS 32
|
2010-01-07 22:49:05 +00:00
|
|
|
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-02-17 00:41:40 +00:00
|
|
|
|
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.
|
|
|
|
{
|
2010-02-17 00:41:40 +00:00
|
|
|
llvm::raw_svector_ostream Out(AggStr);
|
2010-01-07 22:49:05 +00:00
|
|
|
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;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-07 23:13:53 +00:00
|
|
|
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);
|
2010-03-05 22:43:25 +00:00
|
|
|
Out << "ClangCIndex [" << getClangFullVersion() << "]"
|
|
|
|
<< "[createTranslationUnitFromSourceFile]: clang";
|
2010-01-07 22:49:05 +00:00
|
|
|
for (llvm::SmallVectorImpl<const char*>::iterator I=Args.begin(),
|
|
|
|
E=Args.end(); I!=E; ++I)
|
|
|
|
Out << ' ' << *I;
|
|
|
|
}
|
|
|
|
crashtracerSlot = SetCrashTracerInfo(CrashString.c_str(),
|
|
|
|
AggregateString);
|
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-07 22:49:05 +00:00
|
|
|
~ArgsCrashTracerInfo() {
|
|
|
|
ResetCrashTracerInfo(crashtracerSlot);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|
2010-01-06 03:42:32 +00:00
|
|
|
|
2010-01-22 19:49:59 +00:00
|
|
|
/// \brief The result of comparing two source ranges.
|
|
|
|
enum RangeComparisonResult {
|
|
|
|
/// \brief Either the ranges overlap or one of the ranges is invalid.
|
|
|
|
RangeOverlap,
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-22 19:49:59 +00:00
|
|
|
/// \brief The first range ends before the second range starts.
|
|
|
|
RangeBefore,
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-22 19:49:59 +00:00
|
|
|
/// \brief The first range starts after the second range ends.
|
|
|
|
RangeAfter
|
|
|
|
};
|
|
|
|
|
2010-02-17 00:41:40 +00:00
|
|
|
/// \brief Compare two source ranges to determine their relative position in
|
2010-01-22 19:49:59 +00:00
|
|
|
/// the translation unit.
|
2010-02-17 00:41:40 +00:00
|
|
|
static RangeComparisonResult RangeCompare(SourceManager &SM,
|
|
|
|
SourceRange R1,
|
2010-01-22 19:49:59 +00:00
|
|
|
SourceRange R2) {
|
|
|
|
assert(R1.isValid() && "First range is invalid?");
|
|
|
|
assert(R2.isValid() && "Second range is invalid?");
|
2010-02-14 10:02:57 +00:00
|
|
|
if (R1.getEnd() == R2.getBegin() ||
|
|
|
|
SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin()))
|
2010-01-22 19:49:59 +00:00
|
|
|
return RangeBefore;
|
2010-02-14 10:02:57 +00:00
|
|
|
if (R2.getEnd() == R1.getBegin() ||
|
|
|
|
SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin()))
|
2010-01-22 19:49:59 +00:00
|
|
|
return RangeAfter;
|
|
|
|
return RangeOverlap;
|
|
|
|
}
|
|
|
|
|
2010-02-14 01:47:29 +00:00
|
|
|
/// \brief Translate a Clang source range into a CIndex source range.
|
|
|
|
///
|
|
|
|
/// Clang internally represents ranges where the end location points to the
|
|
|
|
/// start of the token at the end. However, for external clients it is more
|
|
|
|
/// useful to have a CXSourceRange be a proper half-open interval. This routine
|
|
|
|
/// does the appropriate translation.
|
2010-02-17 00:41:40 +00:00
|
|
|
CXSourceRange cxloc::translateSourceRange(const SourceManager &SM,
|
2010-02-14 01:47:29 +00:00
|
|
|
const LangOptions &LangOpts,
|
|
|
|
SourceRange R) {
|
|
|
|
// We want the last character in this location, so we will adjust the
|
Implement serialization and lazy deserialization of the preprocessing
record (which includes all macro instantiations and definitions). As
with all lay deserialization, this introduces a new external source
(here, an external preprocessing record source) that loads all of the
preprocessed entities prior to iterating over the entities.
The preprocessing record is an optional part of the precompiled header
that is disabled by default (enabled with
-detailed-preprocessing-record). When the preprocessor given to the
PCH writer has a preprocessing record, that record is written into the
PCH file. When the PCH reader is given a PCH file that contains a
preprocessing record, it will be lazily loaded (which, effectively,
implicitly adds -detailed-preprocessing-record). This is the first
case where we have sections of the precompiled header that are
added/removed based on a compilation flag, which is
unfortunate. However, this data consumes ~550k in the PCH file for
Cocoa.h (out of ~9.9MB), and there is a non-trivial cost to gathering
this detailed preprocessing information, so it's too expensive to turn
on by default. In the future, we should investigate a better encoding
of this information.
llvm-svn: 99002
2010-03-19 21:51:54 +00:00
|
|
|
// location accordingly.
|
|
|
|
// FIXME: How do do this with a macro instantiation location?
|
2010-02-14 01:47:29 +00:00
|
|
|
SourceLocation EndLoc = R.getEnd();
|
Implement serialization and lazy deserialization of the preprocessing
record (which includes all macro instantiations and definitions). As
with all lay deserialization, this introduces a new external source
(here, an external preprocessing record source) that loads all of the
preprocessed entities prior to iterating over the entities.
The preprocessing record is an optional part of the precompiled header
that is disabled by default (enabled with
-detailed-preprocessing-record). When the preprocessor given to the
PCH writer has a preprocessing record, that record is written into the
PCH file. When the PCH reader is given a PCH file that contains a
preprocessing record, it will be lazily loaded (which, effectively,
implicitly adds -detailed-preprocessing-record). This is the first
case where we have sections of the precompiled header that are
added/removed based on a compilation flag, which is
unfortunate. However, this data consumes ~550k in the PCH file for
Cocoa.h (out of ~9.9MB), and there is a non-trivial cost to gathering
this detailed preprocessing information, so it's too expensive to turn
on by default. In the future, we should investigate a better encoding
of this information.
llvm-svn: 99002
2010-03-19 21:51:54 +00:00
|
|
|
if (!EndLoc.isInvalid() && EndLoc.isFileID()) {
|
|
|
|
unsigned Length = Lexer::MeasureTokenLength(EndLoc, SM, LangOpts);
|
2010-02-14 01:47:29 +00:00
|
|
|
EndLoc = EndLoc.getFileLocWithOffset(Length);
|
|
|
|
}
|
|
|
|
|
|
|
|
CXSourceRange Result = { { (void *)&SM, (void *)&LangOpts },
|
|
|
|
R.getBegin().getRawEncoding(),
|
|
|
|
EndLoc.getRawEncoding() };
|
|
|
|
return Result;
|
|
|
|
}
|
2010-01-19 21:36:55 +00:00
|
|
|
|
2010-01-06 03:42:32 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2010-01-22 19:49:59 +00:00
|
|
|
// Cursor visitor.
|
2010-01-06 03:42:32 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-08-31 00:59:03 +00:00
|
|
|
namespace {
|
2010-02-17 00:41:40 +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>,
|
2010-01-21 23:27:09 +00:00
|
|
|
public TypeLocVisitor<CursorVisitor, bool>,
|
|
|
|
public StmtVisitor<CursorVisitor, bool>
|
2010-01-21 16:28:34 +00:00
|
|
|
{
|
2010-01-22 19:49:59 +00:00
|
|
|
/// \brief The translation unit we are traversing.
|
2010-01-20 23:57:43 +00:00
|
|
|
ASTUnit *TU;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-22 19:49:59 +00:00
|
|
|
/// \brief The parent cursor whose children we are traversing.
|
2010-01-20 20:59:29 +00:00
|
|
|
CXCursor Parent;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-22 19:49:59 +00:00
|
|
|
/// \brief The declaration that serves at the parent of any statement or
|
|
|
|
/// expression nodes.
|
2010-01-21 17:29:07 +00:00
|
|
|
Decl *StmtParent;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-22 19:49:59 +00:00
|
|
|
/// \brief The visitor function.
|
2010-01-20 20:59:29 +00:00
|
|
|
CXCursorVisitor Visitor;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-22 19:49:59 +00:00
|
|
|
/// \brief The opaque client data, to be passed along to the visitor.
|
2010-01-20 20:59:29 +00:00
|
|
|
CXClientData ClientData;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
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-22 19:49:59 +00:00
|
|
|
|
|
|
|
/// \brief When valid, a source range to which the cursor should restrict
|
|
|
|
/// its search.
|
|
|
|
SourceRange RegionOfInterest;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
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-21 23:27:09 +00:00
|
|
|
using StmtVisitor<CursorVisitor, bool>::Visit;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
|
|
|
/// \brief Determine whether this particular source range comes before, comes
|
|
|
|
/// after, or overlaps the region of interest.
|
2010-01-22 19:49:59 +00:00
|
|
|
///
|
2010-02-14 10:02:57 +00:00
|
|
|
/// \param R a half-open source range retrieved from the abstract syntax tree.
|
2010-02-17 00:41:40 +00:00
|
|
|
RangeComparisonResult CompareRegionOfInterest(SourceRange R);
|
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
public:
|
2010-02-17 00:41:40 +00:00
|
|
|
CursorVisitor(ASTUnit *TU, CXCursorVisitor Visitor, CXClientData ClientData,
|
|
|
|
unsigned MaxPCHLevel,
|
2010-01-22 19:49:59 +00:00
|
|
|
SourceRange RegionOfInterest = SourceRange())
|
2010-02-17 00:41:40 +00:00
|
|
|
: TU(TU), Visitor(Visitor), ClientData(ClientData),
|
2010-01-22 19:49:59 +00:00
|
|
|
MaxPCHLevel(MaxPCHLevel), RegionOfInterest(RegionOfInterest)
|
2010-01-20 20:59:29 +00:00
|
|
|
{
|
|
|
|
Parent.kind = CXCursor_NoDeclFound;
|
|
|
|
Parent.data[0] = 0;
|
|
|
|
Parent.data[1] = 0;
|
|
|
|
Parent.data[2] = 0;
|
2010-01-21 17:29:07 +00:00
|
|
|
StmtParent = 0;
|
2010-01-20 20:59:29 +00:00
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-22 19:49:59 +00:00
|
|
|
bool Visit(CXCursor Cursor, bool CheckedRegionOfInterest = false);
|
2010-03-20 00:41:21 +00:00
|
|
|
|
|
|
|
std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
|
|
|
|
getPreprocessedEntities();
|
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
bool VisitChildren(CXCursor Parent);
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-21 16:28:34 +00:00
|
|
|
// Declaration visitors
|
2010-02-18 05:46:33 +00:00
|
|
|
bool VisitAttributes(Decl *D);
|
2010-01-20 20:59:29 +00:00
|
|
|
bool VisitDeclContext(DeclContext *DC);
|
2010-02-18 22:36:18 +00:00
|
|
|
bool VisitTranslationUnitDecl(TranslationUnitDecl *D);
|
|
|
|
bool VisitTypedefDecl(TypedefDecl *D);
|
|
|
|
bool VisitTagDecl(TagDecl *D);
|
2010-02-18 18:47:08 +00:00
|
|
|
bool VisitEnumConstantDecl(EnumConstantDecl *D);
|
2010-02-18 22:36:18 +00:00
|
|
|
bool VisitDeclaratorDecl(DeclaratorDecl *DD);
|
2010-02-18 18:47:08 +00:00
|
|
|
bool VisitFunctionDecl(FunctionDecl *ND);
|
2010-02-18 22:36:18 +00:00
|
|
|
bool VisitFieldDecl(FieldDecl *D);
|
|
|
|
bool VisitVarDecl(VarDecl *);
|
|
|
|
bool VisitObjCMethodDecl(ObjCMethodDecl *ND);
|
2010-02-18 18:47:08 +00:00
|
|
|
bool VisitObjCContainerDecl(ObjCContainerDecl *D);
|
2010-02-18 22:36:18 +00:00
|
|
|
bool VisitObjCCategoryDecl(ObjCCategoryDecl *ND);
|
|
|
|
bool VisitObjCProtocolDecl(ObjCProtocolDecl *PID);
|
|
|
|
bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
|
2010-02-18 18:47:08 +00:00
|
|
|
bool VisitObjCImplDecl(ObjCImplDecl *D);
|
2010-02-18 22:36:18 +00:00
|
|
|
bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
|
2010-01-22 00:50:27 +00:00
|
|
|
bool VisitObjCImplementationDecl(ObjCImplementationDecl *D);
|
|
|
|
// FIXME: ObjCPropertyDecl requires TypeSourceInfo, getter/setter locations,
|
|
|
|
// etc.
|
2010-02-18 22:36:18 +00:00
|
|
|
// FIXME: ObjCCompatibleAliasDecl requires aliased-class locations.
|
|
|
|
bool VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
|
|
|
|
bool VisitObjCClassDecl(ObjCClassDecl *D);
|
2010-01-21 16:28:34 +00:00
|
|
|
|
|
|
|
// Type visitors
|
2010-01-21 17:29:07 +00:00
|
|
|
// FIXME: QualifiedTypeLoc doesn't provide any location information
|
|
|
|
bool VisitBuiltinTypeLoc(BuiltinTypeLoc TL);
|
2010-01-21 16:28:34 +00:00
|
|
|
bool VisitTypedefTypeLoc(TypedefTypeLoc TL);
|
2010-01-21 17:29:07 +00:00
|
|
|
bool VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL);
|
|
|
|
bool VisitTagTypeLoc(TagTypeLoc TL);
|
|
|
|
// FIXME: TemplateTypeParmTypeLoc doesn't provide any location information
|
|
|
|
bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL);
|
|
|
|
bool VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL);
|
|
|
|
bool VisitPointerTypeLoc(PointerTypeLoc TL);
|
|
|
|
bool VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL);
|
|
|
|
bool VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL);
|
|
|
|
bool VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL);
|
|
|
|
bool VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL);
|
|
|
|
bool VisitFunctionTypeLoc(FunctionTypeLoc TL);
|
|
|
|
bool VisitArrayTypeLoc(ArrayTypeLoc TL);
|
2010-01-21 20:48:56 +00:00
|
|
|
// FIXME: Implement for TemplateSpecializationTypeLoc
|
|
|
|
// FIXME: Implement visitors here when the unimplemented TypeLocs get
|
|
|
|
// implemented
|
|
|
|
bool VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL);
|
|
|
|
bool VisitTypeOfTypeLoc(TypeOfTypeLoc TL);
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-21 23:27:09 +00:00
|
|
|
// Statement visitors
|
|
|
|
bool VisitStmt(Stmt *S);
|
|
|
|
bool VisitDeclStmt(DeclStmt *S);
|
2010-01-22 01:00:11 +00:00
|
|
|
// FIXME: LabelStmt label?
|
|
|
|
bool VisitIfStmt(IfStmt *S);
|
|
|
|
bool VisitSwitchStmt(SwitchStmt *S);
|
2010-01-25 16:12:32 +00:00
|
|
|
bool VisitWhileStmt(WhileStmt *S);
|
|
|
|
bool VisitForStmt(ForStmt *S);
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-23 00:40:08 +00:00
|
|
|
// Expression visitors
|
|
|
|
bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
|
|
|
|
bool VisitExplicitCastExpr(ExplicitCastExpr *E);
|
|
|
|
bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
|
2010-03-08 16:40:19 +00:00
|
|
|
bool VisitObjCMessageExpr(ObjCMessageExpr *E);
|
2010-01-20 20:59:29 +00:00
|
|
|
};
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
} // end anonymous namespace
|
2009-11-30 20:42:43 +00:00
|
|
|
|
2010-01-22 19:49:59 +00:00
|
|
|
RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) {
|
|
|
|
return RangeCompare(TU->getSourceManager(), R, RegionOfInterest);
|
|
|
|
}
|
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
/// \brief Visit the given cursor and, if requested by the visitor,
|
|
|
|
/// its children.
|
|
|
|
///
|
2010-01-22 19:49:59 +00:00
|
|
|
/// \param Cursor the cursor to visit.
|
|
|
|
///
|
|
|
|
/// \param CheckRegionOfInterest if true, then the caller already checked that
|
|
|
|
/// this cursor is within the region of interest.
|
|
|
|
///
|
2010-01-20 20:59:29 +00:00
|
|
|
/// \returns true if the visitation should be aborted, false if it
|
|
|
|
/// should continue.
|
2010-01-22 19:49:59 +00:00
|
|
|
bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) {
|
2010-01-20 20:59:29 +00:00
|
|
|
if (clang_isInvalid(Cursor.kind))
|
|
|
|
return false;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
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-22 19:49:59 +00:00
|
|
|
// If we have a range of interest, and this cursor doesn't intersect with it,
|
|
|
|
// we're done.
|
|
|
|
if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) {
|
2010-02-14 08:32:05 +00:00
|
|
|
SourceRange Range =
|
|
|
|
cxloc::translateCXSourceRange(clang_getCursorExtent(Cursor));
|
|
|
|
if (Range.isInvalid() || CompareRegionOfInterest(Range))
|
2010-01-22 19:49:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-02-17 00:41:40 +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-25 16:45:46 +00:00
|
|
|
return false;
|
2010-01-20 20:59:29 +00:00
|
|
|
}
|
2009-11-30 20:42:43 +00:00
|
|
|
|
2010-03-20 00:41:21 +00:00
|
|
|
std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
|
|
|
|
CursorVisitor::getPreprocessedEntities() {
|
|
|
|
PreprocessingRecord &PPRec
|
|
|
|
= *TU->getPreprocessor().getPreprocessingRecord();
|
|
|
|
|
|
|
|
bool OnlyLocalDecls
|
|
|
|
= !TU->isMainFileAST() && TU->getOnlyLocalDecls();
|
|
|
|
|
|
|
|
// There is no region of interest; we have to walk everything.
|
|
|
|
if (RegionOfInterest.isInvalid())
|
|
|
|
return std::make_pair(PPRec.begin(OnlyLocalDecls),
|
|
|
|
PPRec.end(OnlyLocalDecls));
|
|
|
|
|
|
|
|
// Find the file in which the region of interest lands.
|
|
|
|
SourceManager &SM = TU->getSourceManager();
|
|
|
|
std::pair<FileID, unsigned> Begin
|
|
|
|
= SM.getDecomposedInstantiationLoc(RegionOfInterest.getBegin());
|
|
|
|
std::pair<FileID, unsigned> End
|
|
|
|
= SM.getDecomposedInstantiationLoc(RegionOfInterest.getEnd());
|
|
|
|
|
|
|
|
// The region of interest spans files; we have to walk everything.
|
|
|
|
if (Begin.first != End.first)
|
|
|
|
return std::make_pair(PPRec.begin(OnlyLocalDecls),
|
|
|
|
PPRec.end(OnlyLocalDecls));
|
|
|
|
|
|
|
|
ASTUnit::PreprocessedEntitiesByFileMap &ByFileMap
|
|
|
|
= TU->getPreprocessedEntitiesByFile();
|
|
|
|
if (ByFileMap.empty()) {
|
|
|
|
// Build the mapping from files to sets of preprocessed entities.
|
|
|
|
for (PreprocessingRecord::iterator E = PPRec.begin(OnlyLocalDecls),
|
|
|
|
EEnd = PPRec.end(OnlyLocalDecls);
|
|
|
|
E != EEnd; ++E) {
|
|
|
|
std::pair<FileID, unsigned> P
|
|
|
|
= SM.getDecomposedInstantiationLoc((*E)->getSourceRange().getBegin());
|
|
|
|
ByFileMap[P.first].push_back(*E);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_pair(ByFileMap[Begin.first].begin(),
|
|
|
|
ByFileMap[Begin.first].end());
|
|
|
|
}
|
|
|
|
|
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.
|
2010-02-17 00:41:40 +00:00
|
|
|
bool CursorVisitor::VisitChildren(CXCursor Cursor) {
|
2010-01-21 23:27:09 +00:00
|
|
|
if (clang_isReference(Cursor.kind)) {
|
|
|
|
// By definition, references have no children.
|
|
|
|
return false;
|
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
|
|
|
// Set the Parent field to Cursor, then back to its old value once we're
|
2010-01-20 20:59:29 +00:00
|
|
|
// done.
|
|
|
|
class SetParentRAII {
|
|
|
|
CXCursor &Parent;
|
2010-01-21 17:29:07 +00:00
|
|
|
Decl *&StmtParent;
|
2010-01-20 20:59:29 +00:00
|
|
|
CXCursor OldParent;
|
2010-01-21 17:29:07 +00:00
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
public:
|
2010-01-21 17:29:07 +00:00
|
|
|
SetParentRAII(CXCursor &Parent, Decl *&StmtParent, CXCursor NewParent)
|
2010-02-17 00:41:40 +00:00
|
|
|
: Parent(Parent), StmtParent(StmtParent), OldParent(Parent)
|
2010-01-20 20:59:29 +00:00
|
|
|
{
|
|
|
|
Parent = NewParent;
|
2010-01-21 17:29:07 +00:00
|
|
|
if (clang_isDeclaration(Parent.kind))
|
|
|
|
StmtParent = getCursorDecl(Parent);
|
2010-01-20 20:59:29 +00:00
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
~SetParentRAII() {
|
|
|
|
Parent = OldParent;
|
2010-01-21 17:29:07 +00:00
|
|
|
if (clang_isDeclaration(Parent.kind))
|
|
|
|
StmtParent = getCursorDecl(Parent);
|
2010-01-20 20:59:29 +00:00
|
|
|
}
|
2010-01-21 17:29:07 +00:00
|
|
|
} SetParent(Parent, StmtParent, Cursor);
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
if (clang_isDeclaration(Cursor.kind)) {
|
|
|
|
Decl *D = getCursorDecl(Cursor);
|
|
|
|
assert(D && "Invalid declaration cursor");
|
2010-02-18 18:47:01 +00:00
|
|
|
return VisitAttributes(D) || Visit(D);
|
2010-01-20 20:59:29 +00:00
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-21 23:27:09 +00:00
|
|
|
if (clang_isStatement(Cursor.kind))
|
|
|
|
return Visit(getCursorStmt(Cursor));
|
|
|
|
if (clang_isExpression(Cursor.kind))
|
|
|
|
return Visit(getCursorExpr(Cursor));
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
if (clang_isTranslationUnit(Cursor.kind)) {
|
2010-01-20 23:57:43 +00:00
|
|
|
ASTUnit *CXXUnit = getCursorASTUnit(Cursor);
|
2010-01-22 19:49:59 +00:00
|
|
|
if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() &&
|
|
|
|
RegionOfInterest.isInvalid()) {
|
2010-01-20 21:13:59 +00:00
|
|
|
const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls();
|
|
|
|
for (std::vector<Decl*>::const_iterator it = TLDs.begin(),
|
|
|
|
ie = TLDs.end(); it != ie; ++it) {
|
2010-01-22 19:49:59 +00:00
|
|
|
if (Visit(MakeCXCursor(*it, CXXUnit), true))
|
2010-01-20 21:13:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-03-19 05:22:59 +00:00
|
|
|
} else if (VisitDeclContext(
|
|
|
|
CXXUnit->getASTContext().getTranslationUnitDecl()))
|
|
|
|
return true;
|
2010-03-19 03:57:57 +00:00
|
|
|
|
2010-03-19 05:22:59 +00:00
|
|
|
// Walk the preprocessing record.
|
2010-03-20 01:11:56 +00:00
|
|
|
if (CXXUnit->getPreprocessor().getPreprocessingRecord()) {
|
2010-03-19 05:22:59 +00:00
|
|
|
// FIXME: Once we have the ability to deserialize a preprocessing record,
|
|
|
|
// do so.
|
2010-03-20 00:41:21 +00:00
|
|
|
PreprocessingRecord::iterator E, EEnd;
|
|
|
|
for (llvm::tie(E, EEnd) = getPreprocessedEntities(); E != EEnd; ++E) {
|
2010-03-19 05:22:59 +00:00
|
|
|
if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
|
|
|
|
if (Visit(MakeMacroInstantiationCursor(MI, CXXUnit)))
|
|
|
|
return true;
|
2010-03-20 00:41:21 +00:00
|
|
|
|
2010-03-19 05:22:59 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
|
|
|
|
if (Visit(MakeMacroDefinitionCursor(MD, CXXUnit)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-01-20 21:13:59 +00:00
|
|
|
return false;
|
2010-01-20 20:59:29 +00:00
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
// Nothing to visit at the moment.
|
|
|
|
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-02-18 05:46:33 +00:00
|
|
|
|
2010-02-14 10:02:57 +00:00
|
|
|
CXCursor Cursor = MakeCXCursor(*I, TU);
|
|
|
|
|
2010-01-22 19:49:59 +00:00
|
|
|
if (RegionOfInterest.isValid()) {
|
2010-02-14 10:02:57 +00:00
|
|
|
SourceRange Range =
|
|
|
|
cxloc::translateCXSourceRange(clang_getCursorExtent(Cursor));
|
|
|
|
if (Range.isInvalid())
|
2010-01-22 19:49:59 +00:00
|
|
|
continue;
|
2010-02-14 10:02:57 +00:00
|
|
|
|
|
|
|
switch (CompareRegionOfInterest(Range)) {
|
2010-01-22 19:49:59 +00:00
|
|
|
case RangeBefore:
|
|
|
|
// This declaration comes before the region of interest; skip it.
|
|
|
|
continue;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-22 19:49:59 +00:00
|
|
|
case RangeAfter:
|
|
|
|
// This declaration comes after the region of interest; we're done.
|
|
|
|
return false;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-22 19:49:59 +00:00
|
|
|
case RangeOverlap:
|
|
|
|
// This declaration overlaps the region of interest; visit it.
|
|
|
|
break;
|
2010-02-17 00:41:40 +00:00
|
|
|
}
|
2010-01-22 19:49:59 +00:00
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-02-14 10:02:57 +00:00
|
|
|
if (Visit(Cursor, true))
|
2010-01-20 20:59:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
return false;
|
2010-01-13 00:22:49 +00:00
|
|
|
}
|
|
|
|
|
2010-01-22 00:50:27 +00:00
|
|
|
bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
|
|
|
|
llvm_unreachable("Translation units are visited directly by Visit()");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) {
|
|
|
|
if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
|
|
|
|
return Visit(TSInfo->getTypeLoc());
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-22 00:50:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitTagDecl(TagDecl *D) {
|
|
|
|
return VisitDeclContext(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) {
|
|
|
|
if (Expr *Init = D->getInitExpr())
|
|
|
|
return Visit(MakeCXCursor(Init, StmtParent, TU));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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) {
|
2010-01-21 17:29:07 +00:00
|
|
|
if (VisitDeclaratorDecl(ND))
|
|
|
|
return true;
|
|
|
|
|
2010-01-21 23:27:09 +00:00
|
|
|
if (ND->isThisDeclarationADefinition() &&
|
|
|
|
Visit(MakeCXCursor(ND->getBody(), StmtParent, TU)))
|
|
|
|
return true;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-01-13 00:22:49 +00:00
|
|
|
|
2010-01-22 00:50:27 +00:00
|
|
|
bool CursorVisitor::VisitFieldDecl(FieldDecl *D) {
|
|
|
|
if (VisitDeclaratorDecl(D))
|
|
|
|
return true;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-22 00:50:27 +00:00
|
|
|
if (Expr *BitWidth = D->getBitWidth())
|
|
|
|
return Visit(MakeCXCursor(BitWidth, StmtParent, TU));
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-22 00:50:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitVarDecl(VarDecl *D) {
|
|
|
|
if (VisitDeclaratorDecl(D))
|
|
|
|
return true;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-22 00:50:27 +00:00
|
|
|
if (Expr *Init = D->getInit())
|
|
|
|
return Visit(MakeCXCursor(Init, StmtParent, TU));
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-22 00:50:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
|
2010-03-08 14:59:44 +00:00
|
|
|
if (TypeSourceInfo *TSInfo = ND->getResultTypeSourceInfo())
|
|
|
|
if (Visit(TSInfo->getTypeLoc()))
|
|
|
|
return true;
|
|
|
|
|
2010-02-17 00:41:40 +00:00
|
|
|
for (ObjCMethodDecl::param_iterator P = ND->param_begin(),
|
2010-01-22 00:50:27 +00:00
|
|
|
PEnd = ND->param_end();
|
|
|
|
P != PEnd; ++P) {
|
|
|
|
if (Visit(MakeCXCursor(*P, TU)))
|
|
|
|
return true;
|
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-22 00:50:27 +00:00
|
|
|
if (ND->isThisDeclarationADefinition() &&
|
|
|
|
Visit(MakeCXCursor(ND->getBody(), StmtParent, TU)))
|
|
|
|
return true;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-22 00:50:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-21 23:27:09 +00:00
|
|
|
bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) {
|
|
|
|
return VisitDeclContext(D);
|
|
|
|
}
|
|
|
|
|
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-02-17 00:41:40 +00:00
|
|
|
|
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;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-21 23:27:09 +00:00
|
|
|
return VisitObjCContainerDecl(ND);
|
2010-01-13 00:22:49 +00:00
|
|
|
}
|
|
|
|
|
2010-01-22 00:50:27 +00:00
|
|
|
bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
|
|
|
|
ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin();
|
|
|
|
for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
|
|
|
|
E = PID->protocol_end(); I != E; ++I, ++PL)
|
|
|
|
if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
|
|
|
|
return true;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-22 00:50:27 +00:00
|
|
|
return VisitObjCContainerDecl(PID);
|
|
|
|
}
|
|
|
|
|
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-02-17 00:41:40 +00:00
|
|
|
D->getSuperClassLoc(),
|
2010-01-20 23:57:43 +00:00
|
|
|
TU)))
|
2010-01-20 20:59:29 +00:00
|
|
|
return true;
|
2010-02-17 00:41:40 +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;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-21 23:27:09 +00:00
|
|
|
return VisitObjCContainerDecl(D);
|
2010-01-13 00:22:49 +00:00
|
|
|
}
|
|
|
|
|
2010-01-22 00:50:27 +00:00
|
|
|
bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) {
|
|
|
|
return VisitObjCContainerDecl(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
|
2010-03-19 20:39:03 +00:00
|
|
|
// 'ID' could be null when dealing with invalid code.
|
|
|
|
if (ObjCInterfaceDecl *ID = D->getClassInterface())
|
|
|
|
if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU)))
|
|
|
|
return true;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-22 00:50:27 +00:00
|
|
|
return VisitObjCImplDecl(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
|
|
|
|
#if 0
|
|
|
|
// Issue callbacks for super class.
|
|
|
|
// FIXME: No source location information!
|
|
|
|
if (D->getSuperClass() &&
|
|
|
|
Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
|
2010-02-17 00:41:40 +00:00
|
|
|
D->getSuperClassLoc(),
|
2010-01-22 00:50:27 +00:00
|
|
|
TU)))
|
2010-01-21 23:27:09 +00:00
|
|
|
return true;
|
2010-01-22 00:50:27 +00:00
|
|
|
#endif
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-22 00:50:27 +00:00
|
|
|
return VisitObjCImplDecl(D);
|
2010-01-13 00:22:49 +00:00
|
|
|
}
|
|
|
|
|
2010-01-22 00:50:27 +00:00
|
|
|
bool CursorVisitor::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
|
|
|
|
ObjCForwardProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
|
|
|
|
for (ObjCForwardProtocolDecl::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;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
|
|
|
return false;
|
2010-01-13 00:22:49 +00:00
|
|
|
}
|
|
|
|
|
2010-01-22 00:50:27 +00:00
|
|
|
bool CursorVisitor::VisitObjCClassDecl(ObjCClassDecl *D) {
|
|
|
|
for (ObjCClassDecl::iterator C = D->begin(), CEnd = D->end(); C != CEnd; ++C)
|
|
|
|
if (Visit(MakeCursorObjCClassRef(C->getInterface(), C->getLocation(), TU)))
|
|
|
|
return true;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-22 00:50:27 +00:00
|
|
|
return false;
|
2010-01-13 00:22:49 +00:00
|
|
|
}
|
2009-10-18 16:11:04 +00:00
|
|
|
|
2010-01-21 17:29:07 +00:00
|
|
|
bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
|
|
|
|
ASTContext &Context = TU->getASTContext();
|
|
|
|
|
|
|
|
// Some builtin types (such as Objective-C's "id", "sel", and
|
|
|
|
// "Class") have associated declarations. Create cursors for those.
|
|
|
|
QualType VisitType;
|
|
|
|
switch (TL.getType()->getAs<BuiltinType>()->getKind()) {
|
2010-02-18 22:32:43 +00:00
|
|
|
case BuiltinType::Void:
|
2010-01-21 17:29:07 +00:00
|
|
|
case BuiltinType::Bool:
|
2010-02-18 22:32:43 +00:00
|
|
|
case BuiltinType::Char_U:
|
|
|
|
case BuiltinType::UChar:
|
2010-01-21 17:29:07 +00:00
|
|
|
case BuiltinType::Char16:
|
|
|
|
case BuiltinType::Char32:
|
2010-02-18 22:32:43 +00:00
|
|
|
case BuiltinType::UShort:
|
|
|
|
case BuiltinType::UInt:
|
|
|
|
case BuiltinType::ULong:
|
|
|
|
case BuiltinType::ULongLong:
|
|
|
|
case BuiltinType::UInt128:
|
2010-01-21 17:29:07 +00:00
|
|
|
case BuiltinType::Char_S:
|
2010-02-18 22:32:43 +00:00
|
|
|
case BuiltinType::SChar:
|
|
|
|
case BuiltinType::WChar:
|
|
|
|
case BuiltinType::Short:
|
2010-01-21 17:29:07 +00:00
|
|
|
case BuiltinType::Int:
|
|
|
|
case BuiltinType::Long:
|
2010-02-18 18:52:18 +00:00
|
|
|
case BuiltinType::LongLong:
|
2010-02-18 22:32:43 +00:00
|
|
|
case BuiltinType::Int128:
|
|
|
|
case BuiltinType::Float:
|
|
|
|
case BuiltinType::Double:
|
|
|
|
case BuiltinType::LongDouble:
|
2010-01-21 17:29:07 +00:00
|
|
|
case BuiltinType::NullPtr:
|
|
|
|
case BuiltinType::Overload:
|
2010-02-18 22:32:43 +00:00
|
|
|
case BuiltinType::Dependent:
|
2010-01-21 17:29:07 +00:00
|
|
|
break;
|
2010-02-18 22:32:43 +00:00
|
|
|
|
|
|
|
case BuiltinType::UndeducedAuto: // FIXME: Deserves a cursor?
|
2010-01-21 17:29:07 +00:00
|
|
|
break;
|
2010-02-18 22:32:43 +00:00
|
|
|
|
2010-01-21 17:29:07 +00:00
|
|
|
case BuiltinType::ObjCId:
|
|
|
|
VisitType = Context.getObjCIdType();
|
|
|
|
break;
|
2010-02-18 22:32:43 +00:00
|
|
|
|
|
|
|
case BuiltinType::ObjCClass:
|
|
|
|
VisitType = Context.getObjCClassType();
|
|
|
|
break;
|
|
|
|
|
2010-01-21 17:29:07 +00:00
|
|
|
case BuiltinType::ObjCSel:
|
|
|
|
VisitType = Context.getObjCSelType();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!VisitType.isNull()) {
|
|
|
|
if (const TypedefType *Typedef = VisitType->getAs<TypedefType>())
|
2010-02-17 00:41:40 +00:00
|
|
|
return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(),
|
2010-01-21 17:29:07 +00:00
|
|
|
TU));
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-21 16:28:34 +00:00
|
|
|
bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
|
|
|
|
return Visit(MakeCursorTypeRef(TL.getTypedefDecl(), TL.getNameLoc(), TU));
|
|
|
|
}
|
|
|
|
|
2010-01-21 17:29:07 +00:00
|
|
|
bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
|
|
|
|
return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) {
|
|
|
|
return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
|
|
|
|
if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
|
|
|
|
if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I),
|
|
|
|
TU)))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
|
|
|
|
if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseTypeLoc()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (TL.hasProtocolsAsWritten()) {
|
|
|
|
for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
|
2010-02-17 00:41:40 +00:00
|
|
|
if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I),
|
2010-01-21 17:29:07 +00:00
|
|
|
TL.getProtocolLoc(I),
|
|
|
|
TU)))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-21 17:29:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) {
|
|
|
|
return Visit(TL.getPointeeLoc());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
|
|
|
|
return Visit(TL.getPointeeLoc());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
|
|
|
|
return Visit(TL.getPointeeLoc());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
|
2010-02-17 00:41:40 +00:00
|
|
|
return Visit(TL.getPointeeLoc());
|
2010-01-21 17:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
|
2010-02-17 00:41:40 +00:00
|
|
|
return Visit(TL.getPointeeLoc());
|
2010-01-21 17:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
|
|
|
|
if (Visit(TL.getResultLoc()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
|
2010-04-07 00:27:13 +00:00
|
|
|
if (Decl *D = TL.getArg(I))
|
|
|
|
if (Visit(MakeCXCursor(D, TU)))
|
|
|
|
return true;
|
2010-01-21 17:29:07 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) {
|
|
|
|
if (Visit(TL.getElementLoc()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (Expr *Size = TL.getSizeExpr())
|
|
|
|
return Visit(MakeCXCursor(Size, StmtParent, TU));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-21 20:48:56 +00:00
|
|
|
bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
|
|
|
|
return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
|
|
|
|
if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
|
|
|
|
return Visit(TSInfo->getTypeLoc());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-21 23:27:09 +00:00
|
|
|
bool CursorVisitor::VisitStmt(Stmt *S) {
|
|
|
|
for (Stmt::child_iterator Child = S->child_begin(), ChildEnd = S->child_end();
|
|
|
|
Child != ChildEnd; ++Child) {
|
2010-01-25 00:40:30 +00:00
|
|
|
if (*Child && Visit(MakeCXCursor(*Child, StmtParent, TU)))
|
2010-01-21 23:27:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-21 23:27:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitDeclStmt(DeclStmt *S) {
|
|
|
|
for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
|
|
|
|
D != DEnd; ++D) {
|
2010-01-25 16:12:32 +00:00
|
|
|
if (*D && Visit(MakeCXCursor(*D, TU)))
|
2010-01-21 23:27:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-21 23:27:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-22 01:00:11 +00:00
|
|
|
bool CursorVisitor::VisitIfStmt(IfStmt *S) {
|
|
|
|
if (VarDecl *Var = S->getConditionVariable()) {
|
|
|
|
if (Visit(MakeCXCursor(Var, TU)))
|
|
|
|
return true;
|
2010-02-17 00:41:40 +00:00
|
|
|
}
|
|
|
|
|
2010-01-25 16:12:32 +00:00
|
|
|
if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
|
|
|
|
return true;
|
2010-01-22 01:00:11 +00:00
|
|
|
if (S->getThen() && Visit(MakeCXCursor(S->getThen(), StmtParent, TU)))
|
|
|
|
return true;
|
|
|
|
if (S->getElse() && Visit(MakeCXCursor(S->getElse(), StmtParent, TU)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitSwitchStmt(SwitchStmt *S) {
|
|
|
|
if (VarDecl *Var = S->getConditionVariable()) {
|
|
|
|
if (Visit(MakeCXCursor(Var, TU)))
|
|
|
|
return true;
|
2010-02-17 00:41:40 +00:00
|
|
|
}
|
|
|
|
|
2010-01-25 16:12:32 +00:00
|
|
|
if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
|
2010-01-22 01:00:11 +00:00
|
|
|
return true;
|
2010-01-25 16:12:32 +00:00
|
|
|
if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
|
|
|
|
return true;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-25 16:12:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-01-22 01:00:11 +00:00
|
|
|
|
2010-01-25 16:12:32 +00:00
|
|
|
bool CursorVisitor::VisitWhileStmt(WhileStmt *S) {
|
|
|
|
if (VarDecl *Var = S->getConditionVariable()) {
|
|
|
|
if (Visit(MakeCXCursor(Var, TU)))
|
|
|
|
return true;
|
2010-02-17 00:41:40 +00:00
|
|
|
}
|
|
|
|
|
2010-01-25 16:12:32 +00:00
|
|
|
if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
|
|
|
|
return true;
|
|
|
|
if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitForStmt(ForStmt *S) {
|
|
|
|
if (S->getInit() && Visit(MakeCXCursor(S->getInit(), StmtParent, TU)))
|
|
|
|
return true;
|
|
|
|
if (VarDecl *Var = S->getConditionVariable()) {
|
|
|
|
if (Visit(MakeCXCursor(Var, TU)))
|
|
|
|
return true;
|
2010-02-17 00:41:40 +00:00
|
|
|
}
|
|
|
|
|
2010-01-25 16:12:32 +00:00
|
|
|
if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
|
|
|
|
return true;
|
|
|
|
if (S->getInc() && Visit(MakeCXCursor(S->getInc(), StmtParent, TU)))
|
|
|
|
return true;
|
2010-01-22 01:00:11 +00:00
|
|
|
if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
|
|
|
|
return true;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-22 01:00:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-23 00:40:08 +00:00
|
|
|
bool CursorVisitor::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
|
|
|
|
if (E->isArgumentType()) {
|
|
|
|
if (TypeSourceInfo *TSInfo = E->getArgumentTypeInfo())
|
|
|
|
return Visit(TSInfo->getTypeLoc());
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-23 00:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-23 00:40:08 +00:00
|
|
|
return VisitExpr(E);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) {
|
|
|
|
if (TypeSourceInfo *TSInfo = E->getTypeInfoAsWritten())
|
|
|
|
if (Visit(TSInfo->getTypeLoc()))
|
|
|
|
return true;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-23 00:40:08 +00:00
|
|
|
return VisitCastExpr(E);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
|
|
|
|
if (TypeSourceInfo *TSInfo = E->getTypeSourceInfo())
|
|
|
|
if (Visit(TSInfo->getTypeLoc()))
|
|
|
|
return true;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-23 00:40:08 +00:00
|
|
|
return VisitExpr(E);
|
|
|
|
}
|
|
|
|
|
2010-03-08 16:40:19 +00:00
|
|
|
bool CursorVisitor::VisitObjCMessageExpr(ObjCMessageExpr *E) {
|
|
|
|
ObjCMessageExpr::ClassInfo CI = E->getClassInfo();
|
|
|
|
if (CI.Decl && Visit(MakeCursorObjCClassRef(CI.Decl, CI.Loc, TU)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return VisitExpr(E);
|
|
|
|
}
|
|
|
|
|
2010-02-18 05:46:33 +00:00
|
|
|
bool CursorVisitor::VisitAttributes(Decl *D) {
|
|
|
|
for (const Attr *A = D->getAttrs(); A; A = A->getNext())
|
|
|
|
if (Visit(MakeCXCursor(A, D, TU)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-10-18 16:11:04 +00:00
|
|
|
extern "C" {
|
2010-02-18 23:07:20 +00:00
|
|
|
CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
|
|
|
|
int displayDiagnostics) {
|
2010-01-22 20:35:53 +00:00
|
|
|
CIndexer *CIdxr = new CIndexer();
|
2009-10-20 14:46:24 +00:00
|
|
|
if (excludeDeclarationsFromPCH)
|
|
|
|
CIdxr->setOnlyLocalDecls();
|
2010-02-18 23:07:20 +00:00
|
|
|
if (displayDiagnostics)
|
|
|
|
CIdxr->setDisplayDiagnostics();
|
2009-10-20 14:46:24 +00:00
|
|
|
return CIdxr;
|
2009-08-27 19:51:58 +00:00
|
|
|
}
|
|
|
|
|
2009-12-01 03:14:51 +00:00
|
|
|
void clang_disposeIndex(CXIndex CIdx) {
|
2010-01-29 00:47:48 +00:00
|
|
|
if (CIdx)
|
|
|
|
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) {
|
2010-01-29 00:47:48 +00:00
|
|
|
if (CIdx) {
|
|
|
|
CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
|
|
|
|
CXXIdx->setUseExternalASTGeneration(value);
|
|
|
|
}
|
2009-12-03 01:54:28 +00:00
|
|
|
}
|
|
|
|
|
2009-12-01 03:14:51 +00:00
|
|
|
CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
|
2010-02-18 18:08:43 +00:00
|
|
|
const char *ast_filename) {
|
2010-01-29 00:47:48 +00:00
|
|
|
if (!CIdx)
|
|
|
|
return 0;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2009-10-16 20:01:17 +00:00
|
|
|
CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
|
2009-11-30 20:42:43 +00:00
|
|
|
|
2010-04-05 23:52:57 +00:00
|
|
|
llvm::IntrusiveRefCntPtr<Diagnostic> Diags;
|
|
|
|
return ASTUnit::LoadFromPCHFile(ast_filename, Diags,
|
2010-02-18 18:08:43 +00:00
|
|
|
CXXIdx->getOnlyLocalDecls(),
|
|
|
|
0, 0, 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,
|
2010-01-23 00:14:00 +00:00
|
|
|
const char **command_line_args,
|
|
|
|
unsigned num_unsaved_files,
|
2010-02-18 18:08:43 +00:00
|
|
|
struct CXUnsavedFile *unsaved_files) {
|
2010-01-29 00:47:48 +00:00
|
|
|
if (!CIdx)
|
|
|
|
return 0;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2009-10-20 14:46:24 +00:00
|
|
|
CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
|
|
|
|
|
2010-01-28 00:27:43 +00:00
|
|
|
// Configure the diagnostics.
|
|
|
|
DiagnosticOptions DiagOpts;
|
2010-04-05 23:52:57 +00:00
|
|
|
llvm::IntrusiveRefCntPtr<Diagnostic> Diags;
|
|
|
|
Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0);
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-23 00:14:00 +00:00
|
|
|
llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
|
|
|
|
for (unsigned I = 0; I != num_unsaved_files; ++I) {
|
2010-04-05 22:42:27 +00:00
|
|
|
llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
|
2010-02-17 00:41:40 +00:00
|
|
|
const llvm::MemoryBuffer *Buffer
|
2010-04-05 22:42:27 +00:00
|
|
|
= llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
|
2010-01-23 00:14:00 +00:00
|
|
|
RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename,
|
|
|
|
Buffer));
|
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
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);
|
2010-03-19 16:15:56 +00:00
|
|
|
Args.push_back("-Xclang");
|
|
|
|
Args.push_back("-detailed-preprocessing-record");
|
2010-01-28 00:27:43 +00:00
|
|
|
unsigned NumErrors = Diags->getNumErrors();
|
2010-02-17 00:41:40 +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
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2009-12-05 02:17:18 +00:00
|
|
|
llvm::OwningPtr<ASTUnit> Unit(
|
|
|
|
ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
|
2010-04-05 21:10:19 +00:00
|
|
|
Diags,
|
2009-12-13 03:46:13 +00:00
|
|
|
CXXIdx->getClangResourcesPath(),
|
2009-12-05 02:17:18 +00:00
|
|
|
CXXIdx->getOnlyLocalDecls(),
|
2010-01-23 00:14:00 +00:00
|
|
|
RemappedFiles.data(),
|
2010-02-18 18:08:43 +00:00
|
|
|
RemappedFiles.size(),
|
2010-03-19 16:15:56 +00:00
|
|
|
/*CaptureDiagnostics=*/true));
|
2010-02-17 00:41:40 +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.
|
2010-02-18 23:07:20 +00:00
|
|
|
if (NumErrors != Diags->getNumErrors()) {
|
2010-03-05 22:43:29 +00:00
|
|
|
// Make sure to check that 'Unit' is non-NULL.
|
|
|
|
if (CXXIdx->getDisplayDiagnostics() && Unit.get()) {
|
2010-04-05 18:10:21 +00:00
|
|
|
for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(),
|
|
|
|
DEnd = Unit->stored_diag_end();
|
2010-02-18 23:07:20 +00:00
|
|
|
D != DEnd; ++D) {
|
|
|
|
CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions());
|
2010-02-22 23:17:23 +00:00
|
|
|
CXString Msg = clang_formatDiagnostic(&Diag,
|
|
|
|
clang_defaultDiagnosticDisplayOptions());
|
|
|
|
fprintf(stderr, "%s\n", clang_getCString(Msg));
|
|
|
|
clang_disposeString(Msg);
|
2010-02-18 23:07:20 +00:00
|
|
|
}
|
2010-02-22 23:17:23 +00:00
|
|
|
#ifdef LLVM_ON_WIN32
|
|
|
|
// On Windows, force a flush, since there may be multiple copies of
|
|
|
|
// stderr and stdout in the file system, all with different buffers
|
|
|
|
// but writing to the same device.
|
|
|
|
fflush(stderr);
|
2010-03-26 01:34:51 +00:00
|
|
|
#endif
|
2010-02-18 23:07:20 +00:00
|
|
|
}
|
|
|
|
}
|
2009-12-05 02:17:18 +00:00
|
|
|
|
|
|
|
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");
|
2010-03-13 21:22:49 +00:00
|
|
|
char astTmpFile[L_tmpnam];
|
|
|
|
argv.push_back(tmpnam(astTmpFile));
|
2009-10-22 00:03:57 +00:00
|
|
|
|
2010-01-23 00:14:00 +00:00
|
|
|
// Remap any unsaved files to temporary files.
|
|
|
|
std::vector<llvm::sys::Path> TemporaryFiles;
|
|
|
|
std::vector<std::string> RemapArgs;
|
|
|
|
if (RemapFiles(num_unsaved_files, unsaved_files, RemapArgs, TemporaryFiles))
|
|
|
|
return 0;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-23 00:14:00 +00:00
|
|
|
// The pointers into the elements of RemapArgs are stable because we
|
|
|
|
// won't be adding anything to RemapArgs after this point.
|
|
|
|
for (unsigned i = 0, e = RemapArgs.size(); i != e; ++i)
|
|
|
|
argv.push_back(RemapArgs[i].c_str());
|
2010-02-17 00:41:40 +00:00
|
|
|
|
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
|
|
|
|
2010-01-28 06:00:51 +00:00
|
|
|
// Generate a temporary name for the diagnostics file.
|
2010-03-13 21:22:49 +00:00
|
|
|
char tmpFileResults[L_tmpnam];
|
|
|
|
char *tmpResultsFileName = tmpnam(tmpFileResults);
|
|
|
|
llvm::sys::Path DiagnosticsFile(tmpResultsFileName);
|
2010-01-28 06:00:51 +00:00
|
|
|
TemporaryFiles.push_back(DiagnosticsFile);
|
|
|
|
argv.push_back("-fdiagnostics-binary");
|
|
|
|
|
2010-03-19 16:15:56 +00:00
|
|
|
argv.push_back("-Xclang");
|
|
|
|
argv.push_back("-detailed-preprocessing-record");
|
|
|
|
|
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;
|
2010-01-28 06:00:51 +00:00
|
|
|
const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DiagnosticsFile,
|
|
|
|
NULL };
|
2009-10-22 03:24:01 +00:00
|
|
|
llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
|
2010-01-28 00:56:43 +00:00
|
|
|
/* redirects */ &Redirects[0],
|
2009-10-22 03:24:01 +00:00
|
|
|
/* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
|
2009-11-30 20:42:43 +00:00
|
|
|
|
2010-01-28 00:56:43 +00:00
|
|
|
if (!ErrMsg.empty()) {
|
|
|
|
std::string AllArgs;
|
2009-10-22 03:24:01 +00:00
|
|
|
for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
|
2010-01-28 00:56:43 +00:00
|
|
|
I != E; ++I) {
|
|
|
|
AllArgs += ' ';
|
2009-10-26 22:08:39 +00:00
|
|
|
if (*I)
|
2010-01-28 00:56:43 +00:00
|
|
|
AllArgs += *I;
|
2009-10-26 22:08:39 +00:00
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-02-23 20:23:45 +00:00
|
|
|
Diags->Report(diag::err_fe_invoking) << AllArgs << ErrMsg;
|
2009-10-22 03:24:01 +00:00
|
|
|
}
|
2009-10-18 11:19:36 +00:00
|
|
|
|
2010-04-05 21:10:19 +00:00
|
|
|
ASTUnit *ATU = ASTUnit::LoadFromPCHFile(astTmpFile, Diags,
|
2010-01-23 00:14:00 +00:00
|
|
|
CXXIdx->getOnlyLocalDecls(),
|
|
|
|
RemappedFiles.data(),
|
2010-02-18 18:08:43 +00:00
|
|
|
RemappedFiles.size(),
|
|
|
|
/*CaptureDiagnostics=*/true);
|
|
|
|
if (ATU) {
|
|
|
|
LoadSerializedDiagnostics(DiagnosticsFile,
|
|
|
|
num_unsaved_files, unsaved_files,
|
|
|
|
ATU->getFileManager(),
|
|
|
|
ATU->getSourceManager(),
|
2010-04-05 18:10:21 +00:00
|
|
|
ATU->getStoredDiagnostics());
|
2010-02-18 23:07:20 +00:00
|
|
|
} else if (CXXIdx->getDisplayDiagnostics()) {
|
|
|
|
// We failed to load the ASTUnit, but we can still deserialize the
|
|
|
|
// diagnostics and emit them.
|
|
|
|
FileManager FileMgr;
|
2010-03-16 00:06:06 +00:00
|
|
|
Diagnostic Diag;
|
|
|
|
SourceManager SourceMgr(Diag);
|
2010-02-18 23:07:20 +00:00
|
|
|
// FIXME: Faked LangOpts!
|
|
|
|
LangOptions LangOpts;
|
|
|
|
llvm::SmallVector<StoredDiagnostic, 4> Diags;
|
|
|
|
LoadSerializedDiagnostics(DiagnosticsFile,
|
|
|
|
num_unsaved_files, unsaved_files,
|
|
|
|
FileMgr, SourceMgr, Diags);
|
|
|
|
for (llvm::SmallVector<StoredDiagnostic, 4>::iterator D = Diags.begin(),
|
|
|
|
DEnd = Diags.end();
|
|
|
|
D != DEnd; ++D) {
|
|
|
|
CXStoredDiagnostic Diag(*D, LangOpts);
|
2010-02-22 23:17:23 +00:00
|
|
|
CXString Msg = clang_formatDiagnostic(&Diag,
|
|
|
|
clang_defaultDiagnosticDisplayOptions());
|
|
|
|
fprintf(stderr, "%s\n", clang_getCString(Msg));
|
|
|
|
clang_disposeString(Msg);
|
2010-02-18 23:07:20 +00:00
|
|
|
}
|
2010-02-22 23:17:23 +00:00
|
|
|
|
|
|
|
#ifdef LLVM_ON_WIN32
|
|
|
|
// On Windows, force a flush, since there may be multiple copies of
|
|
|
|
// stderr and stdout in the file system, all with different buffers
|
|
|
|
// but writing to the same device.
|
|
|
|
fflush(stderr);
|
|
|
|
#endif
|
2010-02-18 18:08:43 +00:00
|
|
|
}
|
2010-01-28 06:00:51 +00:00
|
|
|
|
2010-02-18 23:35:40 +00:00
|
|
|
if (ATU) {
|
|
|
|
// Make the translation unit responsible for destroying all temporary files.
|
|
|
|
for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
|
|
|
|
ATU->addTemporaryFile(TemporaryFiles[i]);
|
|
|
|
ATU->addTemporaryFile(llvm::sys::Path(ATU->getPCHFileName()));
|
|
|
|
} else {
|
|
|
|
// Destroy all of the temporary files now; they can't be referenced any
|
|
|
|
// longer.
|
|
|
|
llvm::sys::Path(astTmpFile).eraseFromDisk();
|
|
|
|
for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
|
|
|
|
TemporaryFiles[i].eraseFromDisk();
|
|
|
|
}
|
|
|
|
|
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) {
|
2010-01-29 00:47:48 +00:00
|
|
|
if (CTUnit)
|
|
|
|
delete static_cast<ASTUnit *>(CTUnit);
|
2009-09-17 18:33:27 +00:00
|
|
|
}
|
2009-11-30 20:42:43 +00:00
|
|
|
|
2009-12-01 03:14:51 +00:00
|
|
|
CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
|
2010-01-29 00:47:48 +00:00
|
|
|
if (!CTUnit)
|
2010-02-17 00:41:08 +00:00
|
|
|
return createCXString("");
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2009-09-03 18:19:54 +00:00
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
|
2010-02-17 00:41:08 +00:00
|
|
|
return createCXString(CXXUnit->getOriginalSourceFileName(), 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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-01-22 21:44:22 +00:00
|
|
|
extern "C" {
|
|
|
|
CXSourceLocation clang_getNullLocation() {
|
2010-01-28 00:27:43 +00:00
|
|
|
CXSourceLocation Result = { { 0, 0 }, 0 };
|
2010-01-22 21:44:22 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_equalLocations(CXSourceLocation loc1, CXSourceLocation loc2) {
|
2010-01-30 23:58:27 +00:00
|
|
|
return (loc1.ptr_data[0] == loc2.ptr_data[0] &&
|
|
|
|
loc1.ptr_data[1] == loc2.ptr_data[1] &&
|
|
|
|
loc1.int_data == loc2.int_data);
|
2010-01-22 21:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CXSourceLocation clang_getLocation(CXTranslationUnit tu,
|
|
|
|
CXFile file,
|
|
|
|
unsigned line,
|
|
|
|
unsigned column) {
|
|
|
|
if (!tu)
|
|
|
|
return clang_getNullLocation();
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-22 21:44:22 +00:00
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
|
|
|
|
SourceLocation SLoc
|
|
|
|
= CXXUnit->getSourceManager().getLocation(
|
2010-02-17 00:41:40 +00:00
|
|
|
static_cast<const FileEntry *>(file),
|
2010-01-22 21:44:22 +00:00
|
|
|
line, column);
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-02-14 01:47:36 +00:00
|
|
|
return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc);
|
2010-01-22 21:44:22 +00:00
|
|
|
}
|
|
|
|
|
2010-01-28 00:27:43 +00:00
|
|
|
CXSourceRange clang_getNullRange() {
|
|
|
|
CXSourceRange Result = { { 0, 0 }, 0, 0 };
|
|
|
|
return Result;
|
|
|
|
}
|
2010-02-14 10:02:57 +00:00
|
|
|
|
2010-01-22 21:44:22 +00:00
|
|
|
CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) {
|
2010-01-28 00:27:43 +00:00
|
|
|
if (begin.ptr_data[0] != end.ptr_data[0] ||
|
|
|
|
begin.ptr_data[1] != end.ptr_data[1])
|
|
|
|
return clang_getNullRange();
|
2010-02-17 00:41:40 +00:00
|
|
|
|
|
|
|
CXSourceRange Result = { { begin.ptr_data[0], begin.ptr_data[1] },
|
2010-01-28 00:27:43 +00:00
|
|
|
begin.int_data, end.int_data };
|
2010-01-22 21:44:22 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2010-01-26 19:19:08 +00:00
|
|
|
void clang_getInstantiationLocation(CXSourceLocation location,
|
|
|
|
CXFile *file,
|
|
|
|
unsigned *line,
|
|
|
|
unsigned *column,
|
|
|
|
unsigned *offset) {
|
2010-01-19 21:36:55 +00:00
|
|
|
SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
|
|
|
|
|
2010-02-14 01:47:36 +00:00
|
|
|
if (!location.ptr_data[0] || Loc.isInvalid()) {
|
2010-01-26 19:19:08 +00:00
|
|
|
if (file)
|
|
|
|
*file = 0;
|
|
|
|
if (line)
|
|
|
|
*line = 0;
|
|
|
|
if (column)
|
|
|
|
*column = 0;
|
|
|
|
if (offset)
|
|
|
|
*offset = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-14 01:47:36 +00:00
|
|
|
const SourceManager &SM =
|
|
|
|
*static_cast<const SourceManager*>(location.ptr_data[0]);
|
2010-01-19 21:36:55 +00:00
|
|
|
SourceLocation InstLoc = SM.getInstantiationLoc(Loc);
|
|
|
|
|
|
|
|
if (file)
|
|
|
|
*file = (void *)SM.getFileEntryForID(SM.getFileID(InstLoc));
|
|
|
|
if (line)
|
|
|
|
*line = SM.getInstantiationLineNumber(InstLoc);
|
|
|
|
if (column)
|
|
|
|
*column = SM.getInstantiationColumnNumber(InstLoc);
|
2010-01-26 03:07:15 +00:00
|
|
|
if (offset)
|
2010-01-26 19:19:08 +00:00
|
|
|
*offset = SM.getDecomposedLoc(InstLoc).second;
|
2010-01-26 03:07:15 +00:00
|
|
|
}
|
|
|
|
|
2010-01-19 21:36:55 +00:00
|
|
|
CXSourceLocation clang_getRangeStart(CXSourceRange range) {
|
2010-02-17 00:41:40 +00:00
|
|
|
CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
|
2010-01-28 00:27:43 +00:00
|
|
|
range.begin_int_data };
|
2010-01-19 21:36:55 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CXSourceLocation clang_getRangeEnd(CXSourceRange range) {
|
2010-02-14 01:47:36 +00:00
|
|
|
CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
|
2010-01-28 00:27:43 +00:00
|
|
|
range.end_int_data };
|
2010-01-19 21:36:55 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2010-01-22 21:44:22 +00:00
|
|
|
} // end: extern "C"
|
|
|
|
|
2010-01-13 21:46:36 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CXFile Operations.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
extern "C" {
|
2010-02-17 00:41:20 +00:00
|
|
|
CXString clang_getFileName(CXFile SFile) {
|
2010-01-18 22:46:11 +00:00
|
|
|
if (!SFile)
|
2010-02-17 00:41:20 +00:00
|
|
|
return createCXString(NULL);
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2009-10-27 14:35:18 +00:00
|
|
|
FileEntry *FEnt = static_cast<FileEntry *>(SFile);
|
2010-02-17 00:41:20 +00:00
|
|
|
return createCXString(FEnt->getName());
|
2009-10-27 14:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
time_t clang_getFileTime(CXFile SFile) {
|
2010-01-18 22:46:11 +00:00
|
|
|
if (!SFile)
|
|
|
|
return 0;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2009-10-27 14:35:18 +00:00
|
|
|
FileEntry *FEnt = static_cast<FileEntry *>(SFile);
|
|
|
|
return FEnt->getModificationTime();
|
2009-09-25 21:45:39 +00:00
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-22 21:44:22 +00:00
|
|
|
CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) {
|
|
|
|
if (!tu)
|
|
|
|
return 0;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-22 21:44:22 +00:00
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-22 21:44:22 +00:00
|
|
|
FileManager &FMgr = CXXUnit->getFileManager();
|
|
|
|
const FileEntry *File = FMgr.getFile(file_name, file_name+strlen(file_name));
|
|
|
|
return const_cast<FileEntry *>(File);
|
|
|
|
}
|
2010-02-17 00:41:40 +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();
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-13 21:46:36 +00:00
|
|
|
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();
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-13 21:46:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2009-09-25 21:45:39 +00:00
|
|
|
|
2010-02-02 05:00:22 +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-13 21:46:36 +00:00
|
|
|
extern "C" {
|
2010-02-17 00:41:40 +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;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
// Set the PCHLevel to filter out unwanted decls if requested.
|
|
|
|
if (CXXUnit->getOnlyLocalDecls()) {
|
|
|
|
PCHLevel = 0;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-20 20:59:29 +00:00
|
|
|
// If the main input was an AST, bump the level.
|
|
|
|
if (CXXUnit->isMainFileAST())
|
|
|
|
++PCHLevel;
|
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
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)
|
2010-02-17 00:41:08 +00:00
|
|
|
return createCXString("");
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-20 21:45:58 +00:00
|
|
|
if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
|
2010-02-17 00:41:08 +00:00
|
|
|
return createCXString(OMD->getSelector().getAsString());
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-20 21:45:58 +00:00
|
|
|
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.
|
2010-02-17 00:41:08 +00:00
|
|
|
return createCXString(CIMP->getIdentifier()->getNameStart());
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-20 21:45:58 +00:00
|
|
|
if (ND->getIdentifier())
|
2010-02-17 00:41:08 +00:00
|
|
|
return createCXString(ND->getIdentifier()->getNameStart());
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-02-17 00:41:08 +00:00
|
|
|
return createCXString("");
|
2010-01-20 21:45:58 +00:00
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2009-12-01 03:14:51 +00:00
|
|
|
CXString clang_getCursorSpelling(CXCursor C) {
|
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;
|
2010-02-17 00:41:08 +00:00
|
|
|
return 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;
|
2010-02-17 00:41:08 +00:00
|
|
|
return 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-02-17 00:41:08 +00:00
|
|
|
return 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");
|
|
|
|
|
2010-02-17 00:41:08 +00:00
|
|
|
return createCXString(getCursorContext(C).getTypeDeclType(Type).
|
|
|
|
getAsString());
|
2010-01-21 16:28:34 +00:00
|
|
|
}
|
|
|
|
|
2009-11-30 20:42:49 +00:00
|
|
|
default:
|
2010-02-17 00:41:08 +00:00
|
|
|
return 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-02-17 00:41:08 +00:00
|
|
|
return createCXString("");
|
2010-01-19 23:20:36 +00:00
|
|
|
}
|
|
|
|
|
Introduce the notion of a "preprocessing record", which keeps track of
the macro definitions and macro instantiations that are found
during preprocessing. Preprocessing records are *not* generated by
default; rather, we provide a PPCallbacks subclass that hooks into the
existing callback mechanism to record this activity.
The only client of preprocessing records is CIndex, which keeps track
of macro definitions and instantations so that they can be exposed via
cursors. At present, only token annotation uses these facilities, and
only for macro instantiations; both will change in the near
future. However, with this change, token annotation properly annotates
macro instantiations that do not produce any tokens and instantiations
of macros that are later undef'd, improving our consistency.
Preprocessing directives that are not macro definitions are still
handled by clang_annotateTokens() via re-lexing, so that we don't have
to track every preprocessing directive in the preprocessing record.
Performance impact of preprocessing records is still TBD, although it
is limited to CIndex and therefore out of the path of the main compiler.
llvm-svn: 98836
2010-03-18 17:52:52 +00:00
|
|
|
if (C.kind == CXCursor_MacroInstantiation)
|
|
|
|
return createCXString(getCursorMacroInstantiation(C)->getName()
|
|
|
|
->getNameStart());
|
|
|
|
|
2010-03-18 18:04:21 +00:00
|
|
|
if (C.kind == CXCursor_MacroDefinition)
|
|
|
|
return createCXString(getCursorMacroDefinition(C)->getName()
|
|
|
|
->getNameStart());
|
|
|
|
|
2010-01-25 16:56:17 +00:00
|
|
|
if (clang_isDeclaration(C.kind))
|
|
|
|
return getDeclSpelling(getCursorDecl(C));
|
2010-02-17 00:41:32 +00:00
|
|
|
|
2010-02-17 00:41:08 +00:00
|
|
|
return createCXString("");
|
2009-09-02 18:26:48 +00:00
|
|
|
}
|
|
|
|
|
2010-02-17 00:41:32 +00:00
|
|
|
CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
|
2009-08-31 00:59:03 +00:00
|
|
|
switch (Kind) {
|
2010-02-17 00:41:32 +00:00
|
|
|
case CXCursor_FunctionDecl:
|
|
|
|
return createCXString("FunctionDecl");
|
|
|
|
case CXCursor_TypedefDecl:
|
|
|
|
return createCXString("TypedefDecl");
|
|
|
|
case CXCursor_EnumDecl:
|
|
|
|
return createCXString("EnumDecl");
|
|
|
|
case CXCursor_EnumConstantDecl:
|
|
|
|
return createCXString("EnumConstantDecl");
|
|
|
|
case CXCursor_StructDecl:
|
|
|
|
return createCXString("StructDecl");
|
|
|
|
case CXCursor_UnionDecl:
|
|
|
|
return createCXString("UnionDecl");
|
|
|
|
case CXCursor_ClassDecl:
|
|
|
|
return createCXString("ClassDecl");
|
|
|
|
case CXCursor_FieldDecl:
|
|
|
|
return createCXString("FieldDecl");
|
|
|
|
case CXCursor_VarDecl:
|
|
|
|
return createCXString("VarDecl");
|
|
|
|
case CXCursor_ParmDecl:
|
|
|
|
return createCXString("ParmDecl");
|
|
|
|
case CXCursor_ObjCInterfaceDecl:
|
|
|
|
return createCXString("ObjCInterfaceDecl");
|
|
|
|
case CXCursor_ObjCCategoryDecl:
|
|
|
|
return createCXString("ObjCCategoryDecl");
|
|
|
|
case CXCursor_ObjCProtocolDecl:
|
|
|
|
return createCXString("ObjCProtocolDecl");
|
|
|
|
case CXCursor_ObjCPropertyDecl:
|
|
|
|
return createCXString("ObjCPropertyDecl");
|
|
|
|
case CXCursor_ObjCIvarDecl:
|
|
|
|
return createCXString("ObjCIvarDecl");
|
|
|
|
case CXCursor_ObjCInstanceMethodDecl:
|
|
|
|
return createCXString("ObjCInstanceMethodDecl");
|
|
|
|
case CXCursor_ObjCClassMethodDecl:
|
|
|
|
return createCXString("ObjCClassMethodDecl");
|
|
|
|
case CXCursor_ObjCImplementationDecl:
|
|
|
|
return createCXString("ObjCImplementationDecl");
|
|
|
|
case CXCursor_ObjCCategoryImplDecl:
|
|
|
|
return createCXString("ObjCCategoryImplDecl");
|
|
|
|
case CXCursor_UnexposedDecl:
|
|
|
|
return createCXString("UnexposedDecl");
|
|
|
|
case CXCursor_ObjCSuperClassRef:
|
|
|
|
return createCXString("ObjCSuperClassRef");
|
|
|
|
case CXCursor_ObjCProtocolRef:
|
|
|
|
return createCXString("ObjCProtocolRef");
|
|
|
|
case CXCursor_ObjCClassRef:
|
|
|
|
return createCXString("ObjCClassRef");
|
|
|
|
case CXCursor_TypeRef:
|
|
|
|
return createCXString("TypeRef");
|
|
|
|
case CXCursor_UnexposedExpr:
|
|
|
|
return createCXString("UnexposedExpr");
|
|
|
|
case CXCursor_DeclRefExpr:
|
|
|
|
return createCXString("DeclRefExpr");
|
|
|
|
case CXCursor_MemberRefExpr:
|
|
|
|
return createCXString("MemberRefExpr");
|
|
|
|
case CXCursor_CallExpr:
|
|
|
|
return createCXString("CallExpr");
|
|
|
|
case CXCursor_ObjCMessageExpr:
|
|
|
|
return createCXString("ObjCMessageExpr");
|
|
|
|
case CXCursor_UnexposedStmt:
|
|
|
|
return createCXString("UnexposedStmt");
|
|
|
|
case CXCursor_InvalidFile:
|
|
|
|
return createCXString("InvalidFile");
|
2010-03-19 20:39:05 +00:00
|
|
|
case CXCursor_InvalidCode:
|
|
|
|
return createCXString("InvalidCode");
|
2010-02-17 00:41:32 +00:00
|
|
|
case CXCursor_NoDeclFound:
|
|
|
|
return createCXString("NoDeclFound");
|
|
|
|
case CXCursor_NotImplemented:
|
|
|
|
return createCXString("NotImplemented");
|
|
|
|
case CXCursor_TranslationUnit:
|
|
|
|
return createCXString("TranslationUnit");
|
2010-02-18 03:09:07 +00:00
|
|
|
case CXCursor_UnexposedAttr:
|
|
|
|
return createCXString("UnexposedAttr");
|
|
|
|
case CXCursor_IBActionAttr:
|
|
|
|
return createCXString("attribute(ibaction)");
|
2010-03-18 00:42:48 +00:00
|
|
|
case CXCursor_IBOutletAttr:
|
|
|
|
return createCXString("attribute(iboutlet)");
|
|
|
|
case CXCursor_PreprocessingDirective:
|
|
|
|
return createCXString("preprocessing directive");
|
2010-03-18 18:04:21 +00:00
|
|
|
case CXCursor_MacroDefinition:
|
|
|
|
return createCXString("macro definition");
|
2010-03-18 15:23:44 +00:00
|
|
|
case CXCursor_MacroInstantiation:
|
|
|
|
return createCXString("macro instantiation");
|
2009-08-31 00:59:03 +00:00
|
|
|
}
|
2010-02-17 00:41:32 +00:00
|
|
|
|
2010-01-16 02:02:09 +00:00
|
|
|
llvm_unreachable("Unhandled CXCursorKind");
|
2010-02-17 00:41:32 +00:00
|
|
|
return createCXString(NULL);
|
2009-08-27 19:51:58 +00:00
|
|
|
}
|
2009-08-31 00:59:03 +00:00
|
|
|
|
2010-02-17 00:41:32 +00:00
|
|
|
enum CXChildVisitResult GetCursorVisitor(CXCursor cursor,
|
|
|
|
CXCursor parent,
|
2010-01-22 19:49:59 +00:00
|
|
|
CXClientData client_data) {
|
|
|
|
CXCursor *BestCursor = static_cast<CXCursor *>(client_data);
|
|
|
|
*BestCursor = cursor;
|
|
|
|
return CXChildVisit_Recurse;
|
|
|
|
}
|
2010-02-17 00:41:32 +00:00
|
|
|
|
2010-01-22 21:44:22 +00:00
|
|
|
CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
|
|
|
|
if (!TU)
|
2010-01-14 01:51:23 +00:00
|
|
|
return clang_getNullCursor();
|
2010-02-17 00:41:32 +00:00
|
|
|
|
2010-01-22 21:44:22 +00:00
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
|
|
|
|
|
2010-03-05 21:16:25 +00:00
|
|
|
ASTUnit::ConcurrencyCheck Check(*CXXUnit);
|
|
|
|
|
2010-01-25 22:34:44 +00:00
|
|
|
SourceLocation SLoc = cxloc::translateSourceLocation(Loc);
|
2010-01-22 19:49:59 +00:00
|
|
|
CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound);
|
|
|
|
if (SLoc.isValid()) {
|
2010-02-14 10:02:57 +00:00
|
|
|
SourceRange RegionOfInterest(SLoc, SLoc.getFileLocWithOffset(1));
|
2010-02-17 00:41:32 +00:00
|
|
|
|
2010-01-22 19:49:59 +00:00
|
|
|
// FIXME: Would be great to have a "hint" cursor, then walk from that
|
|
|
|
// hint cursor upward until we find a cursor whose source range encloses
|
|
|
|
// the region of interest, rather than starting from the translation unit.
|
|
|
|
CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
|
2010-02-17 00:41:32 +00:00
|
|
|
CursorVisitor CursorVis(CXXUnit, GetCursorVisitor, &Result,
|
2010-01-22 19:49:59 +00:00
|
|
|
Decl::MaxPCHLevel, RegionOfInterest);
|
|
|
|
CursorVis.VisitChildren(Parent);
|
2009-09-15 20:25:34 +00:00
|
|
|
}
|
2010-02-17 00:41:32 +00:00
|
|
|
return Result;
|
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;
|
|
|
|
}
|
|
|
|
|
2010-03-18 00:42:48 +00:00
|
|
|
unsigned clang_isPreprocessing(enum CXCursorKind K) {
|
|
|
|
return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing;
|
|
|
|
}
|
|
|
|
|
2010-03-08 21:17:29 +00:00
|
|
|
unsigned clang_isUnexposed(enum CXCursorKind K) {
|
|
|
|
switch (K) {
|
|
|
|
case CXCursor_UnexposedDecl:
|
|
|
|
case CXCursor_UnexposedExpr:
|
|
|
|
case CXCursor_UnexposedStmt:
|
|
|
|
case CXCursor_UnexposedAttr:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-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) {
|
2010-02-17 00:41:40 +00:00
|
|
|
case CXCursor_ObjCSuperClassRef: {
|
2010-01-18 23:41:10 +00:00
|
|
|
std::pair<ObjCInterfaceDecl *, SourceLocation> P
|
|
|
|
= getCursorObjCSuperClassRef(C);
|
2010-01-25 22:34:44 +00:00
|
|
|
return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
|
2010-01-18 23:41:10 +00:00
|
|
|
}
|
|
|
|
|
2010-02-17 00:41:40 +00:00
|
|
|
case CXCursor_ObjCProtocolRef: {
|
2010-01-18 23:41:10 +00:00
|
|
|
std::pair<ObjCProtocolDecl *, SourceLocation> P
|
|
|
|
= getCursorObjCProtocolRef(C);
|
2010-01-25 22:34:44 +00:00
|
|
|
return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
|
2010-01-18 23:41:10 +00:00
|
|
|
}
|
|
|
|
|
2010-02-17 00:41:40 +00:00
|
|
|
case CXCursor_ObjCClassRef: {
|
2010-01-18 23:41:10 +00:00
|
|
|
std::pair<ObjCInterfaceDecl *, SourceLocation> P
|
|
|
|
= getCursorObjCClassRef(C);
|
2010-01-25 22:34:44 +00:00
|
|
|
return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
|
2010-01-18 23:41:10 +00:00
|
|
|
}
|
2010-01-21 16:28:34 +00:00
|
|
|
|
2010-02-17 00:41:40 +00:00
|
|
|
case CXCursor_TypeRef: {
|
2010-01-21 16:28:34 +00:00
|
|
|
std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
|
2010-01-25 22:34:44 +00:00
|
|
|
return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
|
2010-01-21 16:28:34 +00:00
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
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))
|
2010-02-17 00:41:40 +00:00
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C),
|
2010-01-19 23:20:36 +00:00
|
|
|
getLocationFromExpr(getCursorExpr(C)));
|
|
|
|
|
2010-03-18 00:42:48 +00:00
|
|
|
if (C.kind == CXCursor_PreprocessingDirective) {
|
|
|
|
SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin();
|
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C), L);
|
|
|
|
}
|
2010-03-18 15:23:44 +00:00
|
|
|
|
|
|
|
if (C.kind == CXCursor_MacroInstantiation) {
|
Introduce the notion of a "preprocessing record", which keeps track of
the macro definitions and macro instantiations that are found
during preprocessing. Preprocessing records are *not* generated by
default; rather, we provide a PPCallbacks subclass that hooks into the
existing callback mechanism to record this activity.
The only client of preprocessing records is CIndex, which keeps track
of macro definitions and instantations so that they can be exposed via
cursors. At present, only token annotation uses these facilities, and
only for macro instantiations; both will change in the near
future. However, with this change, token annotation properly annotates
macro instantiations that do not produce any tokens and instantiations
of macros that are later undef'd, improving our consistency.
Preprocessing directives that are not macro definitions are still
handled by clang_annotateTokens() via re-lexing, so that we don't have
to track every preprocessing directive in the preprocessing record.
Performance impact of preprocessing records is still TBD, although it
is limited to CIndex and therefore out of the path of the main compiler.
llvm-svn: 98836
2010-03-18 17:52:52 +00:00
|
|
|
SourceLocation L
|
|
|
|
= cxcursor::getCursorMacroInstantiation(C)->getSourceRange().getBegin();
|
2010-03-18 15:23:44 +00:00
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C), L);
|
|
|
|
}
|
2010-03-18 18:04:21 +00:00
|
|
|
|
|
|
|
if (C.kind == CXCursor_MacroDefinition) {
|
|
|
|
SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation();
|
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C), L);
|
|
|
|
}
|
2010-03-18 00:42:48 +00:00
|
|
|
|
2010-01-28 00:27:43 +00:00
|
|
|
if (!getCursorDecl(C))
|
|
|
|
return clang_getNullLocation();
|
2010-01-18 22:46:11 +00:00
|
|
|
|
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-03-22 15:53:50 +00:00
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C), 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) {
|
2010-02-17 00:41:40 +00:00
|
|
|
case CXCursor_ObjCSuperClassRef: {
|
2010-01-19 00:34:46 +00:00
|
|
|
std::pair<ObjCInterfaceDecl *, SourceLocation> P
|
|
|
|
= getCursorObjCSuperClassRef(C);
|
2010-01-25 22:34:44 +00:00
|
|
|
return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
|
2010-01-19 00:34:46 +00:00
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
|
|
|
case CXCursor_ObjCProtocolRef: {
|
2010-01-19 00:34:46 +00:00
|
|
|
std::pair<ObjCProtocolDecl *, SourceLocation> P
|
|
|
|
= getCursorObjCProtocolRef(C);
|
2010-01-25 22:34:44 +00:00
|
|
|
return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
|
2010-01-19 00:34:46 +00:00
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
|
|
|
case CXCursor_ObjCClassRef: {
|
2010-01-19 00:34:46 +00:00
|
|
|
std::pair<ObjCInterfaceDecl *, SourceLocation> P
|
|
|
|
= getCursorObjCClassRef(C);
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-25 22:34:44 +00:00
|
|
|
return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
|
2010-01-19 00:34:46 +00:00
|
|
|
}
|
2010-01-21 16:28:34 +00:00
|
|
|
|
2010-02-17 00:41:40 +00:00
|
|
|
case CXCursor_TypeRef: {
|
2010-01-21 16:28:34 +00:00
|
|
|
std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
|
2010-01-25 22:34:44 +00:00
|
|
|
return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
|
2010-01-21 16:28:34 +00:00
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
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))
|
2010-02-17 00:41:40 +00:00
|
|
|
return cxloc::translateSourceRange(getCursorContext(C),
|
2010-01-19 23:20:36 +00:00
|
|
|
getCursorExpr(C)->getSourceRange());
|
2010-01-22 19:49:59 +00:00
|
|
|
|
|
|
|
if (clang_isStatement(C.kind))
|
2010-02-17 00:41:40 +00:00
|
|
|
return cxloc::translateSourceRange(getCursorContext(C),
|
2010-01-22 19:49:59 +00:00
|
|
|
getCursorStmt(C)->getSourceRange());
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-03-18 00:42:48 +00:00
|
|
|
if (C.kind == CXCursor_PreprocessingDirective) {
|
|
|
|
SourceRange R = cxcursor::getCursorPreprocessingDirective(C);
|
|
|
|
return cxloc::translateSourceRange(getCursorContext(C), R);
|
|
|
|
}
|
2010-03-18 15:23:44 +00:00
|
|
|
|
|
|
|
if (C.kind == CXCursor_MacroInstantiation) {
|
Introduce the notion of a "preprocessing record", which keeps track of
the macro definitions and macro instantiations that are found
during preprocessing. Preprocessing records are *not* generated by
default; rather, we provide a PPCallbacks subclass that hooks into the
existing callback mechanism to record this activity.
The only client of preprocessing records is CIndex, which keeps track
of macro definitions and instantations so that they can be exposed via
cursors. At present, only token annotation uses these facilities, and
only for macro instantiations; both will change in the near
future. However, with this change, token annotation properly annotates
macro instantiations that do not produce any tokens and instantiations
of macros that are later undef'd, improving our consistency.
Preprocessing directives that are not macro definitions are still
handled by clang_annotateTokens() via re-lexing, so that we don't have
to track every preprocessing directive in the preprocessing record.
Performance impact of preprocessing records is still TBD, although it
is limited to CIndex and therefore out of the path of the main compiler.
llvm-svn: 98836
2010-03-18 17:52:52 +00:00
|
|
|
SourceRange R = cxcursor::getCursorMacroInstantiation(C)->getSourceRange();
|
2010-03-18 15:23:44 +00:00
|
|
|
return cxloc::translateSourceRange(getCursorContext(C), R);
|
|
|
|
}
|
2010-03-18 18:04:21 +00:00
|
|
|
|
|
|
|
if (C.kind == CXCursor_MacroDefinition) {
|
|
|
|
SourceRange R = cxcursor::getCursorMacroDefinition(C)->getSourceRange();
|
|
|
|
return cxloc::translateSourceRange(getCursorContext(C), R);
|
|
|
|
}
|
2010-03-18 00:42:48 +00:00
|
|
|
|
2010-01-28 00:27:43 +00:00
|
|
|
if (!getCursorDecl(C))
|
|
|
|
return clang_getNullRange();
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-19 00:34:46 +00:00
|
|
|
Decl *D = getCursorDecl(C);
|
2010-03-22 15:53:50 +00:00
|
|
|
return cxloc::translateSourceRange(getCursorContext(C), D->getSourceRange());
|
2010-01-19 00:34:46 +00:00
|
|
|
}
|
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();
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-20 23:57:43 +00:00
|
|
|
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-02-17 00:41:40 +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 23:57:43 +00:00
|
|
|
return MakeCXCursor(D, CXXUnit);
|
2010-01-19 23:20:36 +00:00
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
2010-03-18 18:23:03 +00:00
|
|
|
if (C.kind == CXCursor_MacroInstantiation) {
|
|
|
|
if (MacroDefinition *Def = getCursorMacroInstantiation(C)->getDefinition())
|
|
|
|
return MakeMacroDefinitionCursor(Def, CXXUnit);
|
|
|
|
}
|
|
|
|
|
2010-01-19 01:20:04 +00:00
|
|
|
if (!clang_isReference(C.kind))
|
|
|
|
return clang_getNullCursor();
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-19 01:20:04 +00:00
|
|
|
switch (C.kind) {
|
|
|
|
case CXCursor_ObjCSuperClassRef:
|
2010-01-20 23:57:43 +00:00
|
|
|
return MakeCXCursor(getCursorObjCSuperClassRef(C).first, CXXUnit);
|
2010-02-17 00:41:40 +00:00
|
|
|
|
|
|
|
case CXCursor_ObjCProtocolRef: {
|
2010-01-20 23:57:43 +00:00
|
|
|
return MakeCXCursor(getCursorObjCProtocolRef(C).first, CXXUnit);
|
2010-02-17 00:41:40 +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
|
|
|
|
2010-02-17 00:41:40 +00:00
|
|
|
case CXCursor_TypeRef:
|
2010-01-21 16:28:34 +00:00
|
|
|
return MakeCXCursor(getCursorTypeRef(C).first, CXXUnit);
|
2010-02-17 00:41:40 +00:00
|
|
|
|
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-02-17 00:41:40 +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();
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-20 23:57:43 +00:00
|
|
|
ASTUnit *CXXUnit = getCursorASTUnit(C);
|
2010-02-17 00:41:40 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-03-18 18:23:03 +00:00
|
|
|
if (C.kind == CXCursor_MacroInstantiation)
|
|
|
|
return clang_getCursorReferenced(C);
|
|
|
|
|
2010-01-19 19:34:47 +00:00
|
|
|
if (!clang_isDeclaration(C.kind))
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
Decl *D = getCursorDecl(C);
|
|
|
|
if (!D)
|
|
|
|
return clang_getNullCursor();
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-19 19:34:47 +00:00
|
|
|
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:
|
2010-02-11 01:04:33 +00:00
|
|
|
if (TagDecl *Def = cast<TagDecl>(D)->getDefinition())
|
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: {
|
2010-02-01 20:16:42 +00:00
|
|
|
// Ask the variable if it has a definition.
|
|
|
|
if (VarDecl *Def = cast<VarDecl>(D)->getDefinition())
|
|
|
|
return MakeCXCursor(Def, CXXUnit);
|
|
|
|
return clang_getNullCursor();
|
2010-01-19 19:34:47 +00:00
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-19 19:34:47 +00:00
|
|
|
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();
|
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-19 19:34:47 +00:00
|
|
|
case Decl::ClassTemplate: {
|
|
|
|
if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl()
|
2010-02-11 01:04:33 +00:00
|
|
|
->getDefinition())
|
2010-01-19 19:34:47 +00:00
|
|
|
return MakeCXCursor(
|
2010-02-17 00:41:40 +00:00
|
|
|
cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(),
|
2010-01-20 23:57:43 +00:00
|
|
|
CXXUnit);
|
2010-01-19 19:34:47 +00:00
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
case Decl::Using: {
|
|
|
|
UsingDecl *Using = cast<UsingDecl>(D);
|
|
|
|
CXCursor Def = clang_getNullCursor();
|
2010-02-17 00:41:40 +00:00
|
|
|
for (UsingDecl::shadow_iterator S = Using->shadow_begin(),
|
|
|
|
SEnd = Using->shadow_end();
|
2010-01-19 19:34:47 +00:00
|
|
|
S != SEnd; ++S) {
|
|
|
|
if (Def != clang_getNullCursor()) {
|
|
|
|
// FIXME: We have no way to return multiple results.
|
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
2010-02-17 00:41:40 +00:00
|
|
|
Def = clang_getCursorDefinition(MakeCXCursor((*S)->getTargetDecl(),
|
2010-01-20 23:57:43 +00:00
|
|
|
CXXUnit));
|
2010-01-19 19:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Def;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Decl::UsingShadow:
|
|
|
|
return clang_getCursorDefinition(
|
2010-02-17 00:41:40 +00:00
|
|
|
MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(),
|
2010-01-20 23:57:43 +00:00
|
|
|
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();
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-19 19:34:47 +00:00
|
|
|
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-02-17 00:41:40 +00:00
|
|
|
|
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-02-17 00:41:40 +00:00
|
|
|
MakeCXCursor(*Forward->protocol_begin(),
|
2010-01-20 23:57:43 +00:00
|
|
|
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-02-17 00:41:40 +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-02-17 00:41:40 +00:00
|
|
|
|
2010-02-18 23:07:20 +00:00
|
|
|
void clang_enableStackTraces(void) {
|
|
|
|
llvm::sys::PrintStackTraceOnErrorSignal();
|
|
|
|
}
|
|
|
|
|
2010-01-13 21:46:36 +00:00
|
|
|
} // end: extern "C"
|
|
|
|
|
2010-01-26 17:06:03 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Token-based Operations.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/* CXToken layout:
|
|
|
|
* int_data[0]: a CXTokenKind
|
|
|
|
* int_data[1]: starting token location
|
|
|
|
* int_data[2]: token length
|
|
|
|
* int_data[3]: reserved
|
2010-02-17 00:41:40 +00:00
|
|
|
* ptr_data: for identifiers and keywords, an IdentifierInfo*.
|
2010-01-26 17:06:03 +00:00
|
|
|
* otherwise unused.
|
|
|
|
*/
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
CXTokenKind clang_getTokenKind(CXToken CXTok) {
|
|
|
|
return static_cast<CXTokenKind>(CXTok.int_data[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) {
|
|
|
|
switch (clang_getTokenKind(CXTok)) {
|
|
|
|
case CXToken_Identifier:
|
|
|
|
case CXToken_Keyword:
|
|
|
|
// We know we have an IdentifierInfo*, so use that.
|
2010-02-17 00:41:08 +00:00
|
|
|
return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data)
|
|
|
|
->getNameStart());
|
2010-01-26 17:06:03 +00:00
|
|
|
|
|
|
|
case CXToken_Literal: {
|
|
|
|
// We have stashed the starting pointer in the ptr_data field. Use it.
|
|
|
|
const char *Text = static_cast<const char *>(CXTok.ptr_data);
|
2010-02-17 00:41:08 +00:00
|
|
|
return createCXString(llvm::StringRef(Text, CXTok.int_data[2]));
|
2010-01-26 17:06:03 +00:00
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-26 17:06:03 +00:00
|
|
|
case CXToken_Punctuation:
|
|
|
|
case CXToken_Comment:
|
|
|
|
break;
|
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
|
|
|
// We have to find the starting buffer pointer the hard way, by
|
2010-01-26 17:06:03 +00:00
|
|
|
// deconstructing the source location.
|
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
|
|
|
|
if (!CXXUnit)
|
2010-02-17 00:41:08 +00:00
|
|
|
return createCXString("");
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-26 17:06:03 +00:00
|
|
|
SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]);
|
|
|
|
std::pair<FileID, unsigned> LocInfo
|
|
|
|
= CXXUnit->getSourceManager().getDecomposedLoc(Loc);
|
2010-03-16 00:06:06 +00:00
|
|
|
bool Invalid = false;
|
2010-03-16 14:14:31 +00:00
|
|
|
llvm::StringRef Buffer
|
2010-03-16 00:06:06 +00:00
|
|
|
= CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid);
|
|
|
|
if (Invalid)
|
2010-03-15 22:54:52 +00:00
|
|
|
return createCXString("");
|
2010-01-26 17:06:03 +00:00
|
|
|
|
2010-03-16 14:14:31 +00:00
|
|
|
return createCXString(Buffer.substr(LocInfo.second, CXTok.int_data[2]));
|
2010-01-26 17:06:03 +00:00
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-26 17:06:03 +00:00
|
|
|
CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) {
|
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
|
|
|
|
if (!CXXUnit)
|
|
|
|
return clang_getNullLocation();
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-26 17:06:03 +00:00
|
|
|
return cxloc::translateSourceLocation(CXXUnit->getASTContext(),
|
|
|
|
SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
|
|
|
|
}
|
|
|
|
|
|
|
|
CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) {
|
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
|
2010-01-28 00:27:43 +00:00
|
|
|
if (!CXXUnit)
|
|
|
|
return clang_getNullRange();
|
2010-02-17 00:41:40 +00:00
|
|
|
|
|
|
|
return cxloc::translateSourceRange(CXXUnit->getASTContext(),
|
2010-01-26 17:06:03 +00:00
|
|
|
SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
|
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-26 17:06:03 +00:00
|
|
|
void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range,
|
|
|
|
CXToken **Tokens, unsigned *NumTokens) {
|
|
|
|
if (Tokens)
|
|
|
|
*Tokens = 0;
|
|
|
|
if (NumTokens)
|
|
|
|
*NumTokens = 0;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-26 17:06:03 +00:00
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
|
|
|
|
if (!CXXUnit || !Tokens || !NumTokens)
|
|
|
|
return;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-03-05 21:16:25 +00:00
|
|
|
ASTUnit::ConcurrencyCheck Check(*CXXUnit);
|
|
|
|
|
2010-02-14 08:31:57 +00:00
|
|
|
SourceRange R = cxloc::translateCXSourceRange(Range);
|
2010-01-26 17:06:03 +00:00
|
|
|
if (R.isInvalid())
|
|
|
|
return;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-26 17:06:03 +00:00
|
|
|
SourceManager &SourceMgr = CXXUnit->getSourceManager();
|
|
|
|
std::pair<FileID, unsigned> BeginLocInfo
|
|
|
|
= SourceMgr.getDecomposedLoc(R.getBegin());
|
|
|
|
std::pair<FileID, unsigned> EndLocInfo
|
|
|
|
= SourceMgr.getDecomposedLoc(R.getEnd());
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-26 17:06:03 +00:00
|
|
|
// Cannot tokenize across files.
|
|
|
|
if (BeginLocInfo.first != EndLocInfo.first)
|
|
|
|
return;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
|
|
|
// Create a lexer
|
2010-03-16 00:06:06 +00:00
|
|
|
bool Invalid = false;
|
2010-03-16 14:14:31 +00:00
|
|
|
llvm::StringRef Buffer
|
2010-03-16 00:06:06 +00:00
|
|
|
= SourceMgr.getBufferData(BeginLocInfo.first, &Invalid);
|
2010-03-16 20:26:15 +00:00
|
|
|
if (Invalid)
|
|
|
|
return;
|
2010-03-15 22:54:52 +00:00
|
|
|
|
2010-01-26 17:06:03 +00:00
|
|
|
Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
|
|
|
|
CXXUnit->getASTContext().getLangOptions(),
|
2010-03-16 14:14:31 +00:00
|
|
|
Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end());
|
2010-01-26 17:06:03 +00:00
|
|
|
Lex.SetCommentRetentionState(true);
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-26 17:06:03 +00:00
|
|
|
// Lex tokens until we hit the end of the range.
|
2010-03-16 14:14:31 +00:00
|
|
|
const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second;
|
2010-01-26 17:06:03 +00:00
|
|
|
llvm::SmallVector<CXToken, 32> CXTokens;
|
|
|
|
Token Tok;
|
|
|
|
do {
|
|
|
|
// Lex the next token
|
|
|
|
Lex.LexFromRawLexer(Tok);
|
|
|
|
if (Tok.is(tok::eof))
|
|
|
|
break;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-26 17:06:03 +00:00
|
|
|
// Initialize the CXToken.
|
|
|
|
CXToken CXTok;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-26 17:06:03 +00:00
|
|
|
// - Common fields
|
|
|
|
CXTok.int_data[1] = Tok.getLocation().getRawEncoding();
|
|
|
|
CXTok.int_data[2] = Tok.getLength();
|
|
|
|
CXTok.int_data[3] = 0;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-26 17:06:03 +00:00
|
|
|
// - Kind-specific fields
|
|
|
|
if (Tok.isLiteral()) {
|
|
|
|
CXTok.int_data[0] = CXToken_Literal;
|
|
|
|
CXTok.ptr_data = (void *)Tok.getLiteralData();
|
|
|
|
} else if (Tok.is(tok::identifier)) {
|
2010-03-15 22:54:52 +00:00
|
|
|
// Lookup the identifier to determine whether we have a keyword.
|
2010-01-26 17:06:03 +00:00
|
|
|
std::pair<FileID, unsigned> LocInfo
|
|
|
|
= SourceMgr.getDecomposedLoc(Tok.getLocation());
|
2010-03-16 00:06:06 +00:00
|
|
|
bool Invalid = false;
|
2010-03-16 14:14:31 +00:00
|
|
|
llvm::StringRef Buf
|
2010-03-16 00:06:06 +00:00
|
|
|
= CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid);
|
|
|
|
if (Invalid)
|
2010-03-15 22:54:52 +00:00
|
|
|
return;
|
|
|
|
|
2010-03-16 14:14:31 +00:00
|
|
|
const char *StartPos = Buf.data() + LocInfo.second;
|
2010-01-26 17:06:03 +00:00
|
|
|
IdentifierInfo *II
|
|
|
|
= CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok, StartPos);
|
|
|
|
CXTok.int_data[0] = II->getTokenID() == tok::identifier?
|
|
|
|
CXToken_Identifier
|
|
|
|
: CXToken_Keyword;
|
|
|
|
CXTok.ptr_data = II;
|
|
|
|
} else if (Tok.is(tok::comment)) {
|
|
|
|
CXTok.int_data[0] = CXToken_Comment;
|
|
|
|
CXTok.ptr_data = 0;
|
|
|
|
} else {
|
|
|
|
CXTok.int_data[0] = CXToken_Punctuation;
|
|
|
|
CXTok.ptr_data = 0;
|
|
|
|
}
|
|
|
|
CXTokens.push_back(CXTok);
|
|
|
|
} while (Lex.getBufferLocation() <= EffectiveBufferEnd);
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-26 17:06:03 +00:00
|
|
|
if (CXTokens.empty())
|
|
|
|
return;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-26 17:06:03 +00:00
|
|
|
*Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size());
|
|
|
|
memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size());
|
|
|
|
*NumTokens = CXTokens.size();
|
|
|
|
}
|
2010-01-26 18:31:56 +00:00
|
|
|
|
|
|
|
typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData;
|
|
|
|
|
2010-02-17 00:41:40 +00:00
|
|
|
enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
|
|
|
|
CXCursor parent,
|
2010-01-26 18:31:56 +00:00
|
|
|
CXClientData client_data) {
|
|
|
|
AnnotateTokensData *Data = static_cast<AnnotateTokensData *>(client_data);
|
|
|
|
|
|
|
|
// We only annotate the locations of declarations, simple
|
|
|
|
// references, and expressions which directly reference something.
|
|
|
|
CXCursorKind Kind = clang_getCursorKind(cursor);
|
|
|
|
if (clang_isDeclaration(Kind) || clang_isReference(Kind)) {
|
|
|
|
// Okay: We can annotate the location of this declaration with the
|
|
|
|
// declaration or reference
|
|
|
|
} else if (clang_isExpression(cursor.kind)) {
|
|
|
|
if (Kind != CXCursor_DeclRefExpr &&
|
|
|
|
Kind != CXCursor_MemberRefExpr &&
|
|
|
|
Kind != CXCursor_ObjCMessageExpr)
|
|
|
|
return CXChildVisit_Recurse;
|
|
|
|
|
|
|
|
CXCursor Referenced = clang_getCursorReferenced(cursor);
|
|
|
|
if (Referenced == cursor || Referenced == clang_getNullCursor())
|
|
|
|
return CXChildVisit_Recurse;
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-26 18:31:56 +00:00
|
|
|
// Okay: we can annotate the location of this expression
|
2010-03-19 05:22:59 +00:00
|
|
|
} else if (clang_isPreprocessing(cursor.kind)) {
|
|
|
|
// We can always annotate a preprocessing directive/macro instantiation.
|
2010-01-26 18:31:56 +00:00
|
|
|
} else {
|
|
|
|
// Nothing to annotate
|
|
|
|
return CXChildVisit_Recurse;
|
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-26 18:31:56 +00:00
|
|
|
CXSourceLocation Loc = clang_getCursorLocation(cursor);
|
|
|
|
(*Data)[Loc.int_data] = cursor;
|
|
|
|
return CXChildVisit_Recurse;
|
|
|
|
}
|
|
|
|
|
2010-01-26 17:06:03 +00:00
|
|
|
void clang_annotateTokens(CXTranslationUnit TU,
|
|
|
|
CXToken *Tokens, unsigned NumTokens,
|
|
|
|
CXCursor *Cursors) {
|
2010-01-26 18:31:56 +00:00
|
|
|
if (NumTokens == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Any token we don't specifically annotate will have a NULL cursor.
|
2010-01-26 17:06:03 +00:00
|
|
|
for (unsigned I = 0; I != NumTokens; ++I)
|
|
|
|
Cursors[I] = clang_getNullCursor();
|
2010-01-26 18:31:56 +00:00
|
|
|
|
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
|
|
|
|
if (!CXXUnit || !Tokens)
|
|
|
|
return;
|
|
|
|
|
2010-03-05 21:16:25 +00:00
|
|
|
ASTUnit::ConcurrencyCheck Check(*CXXUnit);
|
|
|
|
|
2010-03-19 05:22:59 +00:00
|
|
|
// Determine the region of interest, which contains all of the tokens.
|
2010-01-26 18:31:56 +00:00
|
|
|
SourceRange RegionOfInterest;
|
|
|
|
RegionOfInterest.setBegin(
|
|
|
|
cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0])));
|
|
|
|
SourceLocation End
|
2010-04-05 16:10:30 +00:00
|
|
|
= cxloc::translateSourceLocation(clang_getTokenLocation(TU,
|
2010-03-19 05:22:59 +00:00
|
|
|
Tokens[NumTokens - 1]));
|
2010-02-14 10:02:57 +00:00
|
|
|
RegionOfInterest.setEnd(CXXUnit->getPreprocessor().getLocForEndOfToken(End));
|
2010-03-19 05:22:59 +00:00
|
|
|
|
|
|
|
// A mapping from the source locations found when re-lexing or traversing the
|
|
|
|
// region of interest to the corresponding cursors.
|
2010-01-26 18:31:56 +00:00
|
|
|
AnnotateTokensData Annotated;
|
|
|
|
|
2010-03-19 05:22:59 +00:00
|
|
|
// Relex the tokens within the source range to look for preprocessing
|
|
|
|
// directives.
|
2010-03-18 00:42:48 +00:00
|
|
|
SourceManager &SourceMgr = CXXUnit->getSourceManager();
|
|
|
|
std::pair<FileID, unsigned> BeginLocInfo
|
|
|
|
= SourceMgr.getDecomposedLoc(RegionOfInterest.getBegin());
|
|
|
|
std::pair<FileID, unsigned> EndLocInfo
|
|
|
|
= SourceMgr.getDecomposedLoc(RegionOfInterest.getEnd());
|
|
|
|
|
|
|
|
llvm::StringRef Buffer;
|
2010-03-19 05:22:59 +00:00
|
|
|
bool Invalid = false;
|
|
|
|
if (BeginLocInfo.first == EndLocInfo.first &&
|
|
|
|
((Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid)),true) &&
|
|
|
|
!Invalid) {
|
2010-03-18 00:42:48 +00:00
|
|
|
Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
|
|
|
|
CXXUnit->getASTContext().getLangOptions(),
|
Introduce the notion of a "preprocessing record", which keeps track of
the macro definitions and macro instantiations that are found
during preprocessing. Preprocessing records are *not* generated by
default; rather, we provide a PPCallbacks subclass that hooks into the
existing callback mechanism to record this activity.
The only client of preprocessing records is CIndex, which keeps track
of macro definitions and instantations so that they can be exposed via
cursors. At present, only token annotation uses these facilities, and
only for macro instantiations; both will change in the near
future. However, with this change, token annotation properly annotates
macro instantiations that do not produce any tokens and instantiations
of macros that are later undef'd, improving our consistency.
Preprocessing directives that are not macro definitions are still
handled by clang_annotateTokens() via re-lexing, so that we don't have
to track every preprocessing directive in the preprocessing record.
Performance impact of preprocessing records is still TBD, although it
is limited to CIndex and therefore out of the path of the main compiler.
llvm-svn: 98836
2010-03-18 17:52:52 +00:00
|
|
|
Buffer.begin(), Buffer.data() + BeginLocInfo.second,
|
|
|
|
Buffer.end());
|
2010-03-18 00:42:48 +00:00
|
|
|
Lex.SetCommentRetentionState(true);
|
|
|
|
|
|
|
|
// Lex tokens in raw mode until we hit the end of the range, to avoid
|
|
|
|
// entering #includes or expanding macros.
|
2010-03-18 15:23:44 +00:00
|
|
|
while (true) {
|
2010-03-18 00:42:48 +00:00
|
|
|
Token Tok;
|
|
|
|
Lex.LexFromRawLexer(Tok);
|
|
|
|
|
|
|
|
reprocess:
|
|
|
|
if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
|
|
|
|
// We have found a preprocessing directive. Gobble it up so that we
|
|
|
|
// don't see it while preprocessing these tokens later, but keep track of
|
|
|
|
// all of the token locations inside this preprocessing directive so that
|
|
|
|
// we can annotate them appropriately.
|
|
|
|
//
|
|
|
|
// FIXME: Some simple tests here could identify macro definitions and
|
|
|
|
// #undefs, to provide specific cursor kinds for those.
|
|
|
|
std::vector<SourceLocation> Locations;
|
|
|
|
do {
|
|
|
|
Locations.push_back(Tok.getLocation());
|
|
|
|
Lex.LexFromRawLexer(Tok);
|
|
|
|
} while (!Tok.isAtStartOfLine() && !Tok.is(tok::eof));
|
|
|
|
|
|
|
|
using namespace cxcursor;
|
|
|
|
CXCursor Cursor
|
|
|
|
= MakePreprocessingDirectiveCursor(SourceRange(Locations.front(),
|
|
|
|
Locations.back()),
|
|
|
|
CXXUnit);
|
|
|
|
for (unsigned I = 0, N = Locations.size(); I != N; ++I) {
|
|
|
|
Annotated[Locations[I].getRawEncoding()] = Cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Tok.isAtStartOfLine())
|
|
|
|
goto reprocess;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-03-18 15:23:44 +00:00
|
|
|
if (Tok.is(tok::eof))
|
2010-03-18 00:42:48 +00:00
|
|
|
break;
|
|
|
|
}
|
Introduce the notion of a "preprocessing record", which keeps track of
the macro definitions and macro instantiations that are found
during preprocessing. Preprocessing records are *not* generated by
default; rather, we provide a PPCallbacks subclass that hooks into the
existing callback mechanism to record this activity.
The only client of preprocessing records is CIndex, which keeps track
of macro definitions and instantations so that they can be exposed via
cursors. At present, only token annotation uses these facilities, and
only for macro instantiations; both will change in the near
future. However, with this change, token annotation properly annotates
macro instantiations that do not produce any tokens and instantiations
of macros that are later undef'd, improving our consistency.
Preprocessing directives that are not macro definitions are still
handled by clang_annotateTokens() via re-lexing, so that we don't have
to track every preprocessing directive in the preprocessing record.
Performance impact of preprocessing records is still TBD, although it
is limited to CIndex and therefore out of the path of the main compiler.
llvm-svn: 98836
2010-03-18 17:52:52 +00:00
|
|
|
}
|
2010-03-19 05:22:59 +00:00
|
|
|
|
|
|
|
// Annotate all of the source locations in the region of interest that map to
|
|
|
|
// a specific cursor.
|
|
|
|
CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
|
|
|
|
CursorVisitor AnnotateVis(CXXUnit, AnnotateTokensVisitor, &Annotated,
|
|
|
|
Decl::MaxPCHLevel, RegionOfInterest);
|
|
|
|
AnnotateVis.VisitChildren(Parent);
|
2010-03-18 00:42:48 +00:00
|
|
|
|
2010-01-26 18:31:56 +00:00
|
|
|
for (unsigned I = 0; I != NumTokens; ++I) {
|
|
|
|
// Determine whether we saw a cursor at this token's location.
|
|
|
|
AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]);
|
|
|
|
if (Pos == Annotated.end())
|
|
|
|
continue;
|
2010-03-18 00:42:48 +00:00
|
|
|
|
2010-01-26 18:31:56 +00:00
|
|
|
Cursors[I] = Pos->second;
|
2010-03-18 00:42:48 +00:00
|
|
|
}
|
2010-01-26 17:06:03 +00:00
|
|
|
}
|
|
|
|
|
2010-02-17 00:41:40 +00:00
|
|
|
void clang_disposeTokens(CXTranslationUnit TU,
|
2010-01-26 17:06:03 +00:00
|
|
|
CXToken *Tokens, unsigned NumTokens) {
|
2010-01-26 18:31:56 +00:00
|
|
|
free(Tokens);
|
2010-01-26 17:06:03 +00:00
|
|
|
}
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-26 17:06:03 +00:00
|
|
|
} // end: extern "C"
|
|
|
|
|
2010-03-03 06:36:57 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Operations for querying linkage of a cursor.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
CXLinkageKind clang_getCursorLinkage(CXCursor cursor) {
|
2010-03-19 05:22:59 +00:00
|
|
|
if (!clang_isDeclaration(cursor.kind))
|
|
|
|
return CXLinkage_Invalid;
|
|
|
|
|
2010-03-03 06:36:57 +00:00
|
|
|
Decl *D = cxcursor::getCursorDecl(cursor);
|
|
|
|
if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D))
|
|
|
|
switch (ND->getLinkage()) {
|
|
|
|
case NoLinkage: return CXLinkage_NoLinkage;
|
|
|
|
case InternalLinkage: return CXLinkage_Internal;
|
|
|
|
case UniqueExternalLinkage: return CXLinkage_UniqueExternal;
|
|
|
|
case ExternalLinkage: return CXLinkage_External;
|
|
|
|
};
|
|
|
|
|
|
|
|
return CXLinkage_Invalid;
|
|
|
|
}
|
|
|
|
} // end: extern "C"
|
|
|
|
|
2010-01-13 21:46:36 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// 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);
|
|
|
|
}
|
2010-01-22 22:44:15 +00:00
|
|
|
|
2010-01-13 21:46:36 +00:00
|
|
|
} // end: extern "C"
|
2010-01-22 22:44:15 +00:00
|
|
|
|
2010-02-17 00:41:08 +00:00
|
|
|
namespace clang { namespace cxstring {
|
|
|
|
CXString createCXString(const char *String, bool DupString){
|
|
|
|
CXString Str;
|
|
|
|
if (DupString) {
|
|
|
|
Str.Spelling = strdup(String);
|
|
|
|
Str.MustFreeString = 1;
|
|
|
|
} else {
|
|
|
|
Str.Spelling = String;
|
|
|
|
Str.MustFreeString = 0;
|
|
|
|
}
|
|
|
|
return Str;
|
|
|
|
}
|
|
|
|
|
|
|
|
CXString createCXString(llvm::StringRef String, bool DupString) {
|
|
|
|
CXString Result;
|
|
|
|
if (DupString || (!String.empty() && String.data()[String.size()] != 0)) {
|
|
|
|
char *Spelling = (char *)malloc(String.size() + 1);
|
|
|
|
memmove(Spelling, String.data(), String.size());
|
|
|
|
Spelling[String.size()] = 0;
|
|
|
|
Result.Spelling = Spelling;
|
|
|
|
Result.MustFreeString = 1;
|
|
|
|
} else {
|
|
|
|
Result.Spelling = String.data();
|
|
|
|
Result.MustFreeString = 0;
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
|
2010-01-22 22:44:15 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Misc. utility functions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2010-02-17 00:41:40 +00:00
|
|
|
|
2010-01-22 22:44:15 +00:00
|
|
|
extern "C" {
|
|
|
|
|
2010-02-12 22:54:40 +00:00
|
|
|
CXString clang_getClangVersion() {
|
2010-02-17 00:41:08 +00:00
|
|
|
return createCXString(getClangFullVersion());
|
2010-01-22 22:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // end: extern "C"
|