2011-10-18 03:48:19 +08:00
|
|
|
//===- CIndexHigh.cpp - Higher level API functions ------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "IndexingContext.h"
|
|
|
|
#include "CXCursor.h"
|
|
|
|
#include "CXSourceLocation.h"
|
|
|
|
#include "CXTranslationUnit.h"
|
|
|
|
#include "CXString.h"
|
|
|
|
#include "CIndexer.h"
|
|
|
|
|
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
|
|
|
#include "clang/Frontend/CompilerInvocation.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/Utils.h"
|
|
|
|
#include "clang/Sema/SemaConsumer.h"
|
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#include "clang/AST/DeclVisitor.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "clang/Lex/PPCallbacks.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/CrashRecoveryContext.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace cxstring;
|
|
|
|
using namespace cxtu;
|
|
|
|
using namespace cxindex;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// IndexPPCallbacks
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
class IndexPPCallbacks : public PPCallbacks {
|
|
|
|
Preprocessor &PP;
|
|
|
|
IndexingContext &IndexCtx;
|
2011-11-11 08:23:36 +08:00
|
|
|
bool IsMainFileEntered;
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
IndexPPCallbacks(Preprocessor &PP, IndexingContext &indexCtx)
|
2011-11-11 08:23:36 +08:00
|
|
|
: PP(PP), IndexCtx(indexCtx), IsMainFileEntered(false) { }
|
|
|
|
|
|
|
|
virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
|
|
|
|
SrcMgr::CharacteristicKind FileType, FileID PrevFID) {
|
|
|
|
if (IsMainFileEntered)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SourceManager &SM = PP.getSourceManager();
|
|
|
|
SourceLocation MainFileLoc = SM.getLocForStartOfFile(SM.getMainFileID());
|
|
|
|
|
|
|
|
if (Loc == MainFileLoc && Reason == PPCallbacks::EnterFile) {
|
|
|
|
IsMainFileEntered = true;
|
|
|
|
IndexCtx.enteredMainFile(SM.getFileEntryForID(SM.getMainFileID()));
|
|
|
|
}
|
|
|
|
}
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
virtual void InclusionDirective(SourceLocation HashLoc,
|
|
|
|
const Token &IncludeTok,
|
|
|
|
StringRef FileName,
|
|
|
|
bool IsAngled,
|
|
|
|
const FileEntry *File,
|
|
|
|
SourceLocation EndLoc,
|
|
|
|
StringRef SearchPath,
|
|
|
|
StringRef RelativePath) {
|
|
|
|
bool isImport = (IncludeTok.is(tok::identifier) &&
|
|
|
|
IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import);
|
|
|
|
IndexCtx.ppIncludedFile(HashLoc, FileName, File, isImport, IsAngled);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// MacroDefined - This hook is called whenever a macro definition is seen.
|
|
|
|
virtual void MacroDefined(const Token &Id, const MacroInfo *MI) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/// MacroUndefined - This hook is called whenever a macro #undef is seen.
|
|
|
|
/// MI is released immediately following this callback.
|
|
|
|
virtual void MacroUndefined(const Token &MacroNameTok, const MacroInfo *MI) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/// MacroExpands - This is called by when a macro invocation is found.
|
|
|
|
virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo* MI,
|
|
|
|
SourceRange Range) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/// SourceRangeSkipped - This hook is called when a source range is skipped.
|
|
|
|
/// \param Range The SourceRange that was skipped. The range begins at the
|
|
|
|
/// #if/#else directive and ends after the #endif/#else directive.
|
|
|
|
virtual void SourceRangeSkipped(SourceRange Range) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// IndexingConsumer
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
class IndexingConsumer : public ASTConsumer {
|
|
|
|
IndexingContext &IndexCtx;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit IndexingConsumer(IndexingContext &indexCtx)
|
|
|
|
: IndexCtx(indexCtx) { }
|
|
|
|
|
|
|
|
// ASTConsumer Implementation
|
|
|
|
|
|
|
|
virtual void Initialize(ASTContext &Context) {
|
|
|
|
IndexCtx.setASTContext(Context);
|
2011-11-12 10:16:30 +08:00
|
|
|
IndexCtx.startedTranslationUnit();
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void HandleTranslationUnit(ASTContext &Ctx) {
|
|
|
|
}
|
|
|
|
|
2011-11-18 08:26:59 +08:00
|
|
|
virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
|
2011-10-18 03:48:19 +08:00
|
|
|
IndexCtx.indexDeclGroupRef(DG);
|
2011-11-18 08:26:59 +08:00
|
|
|
return !IndexCtx.shouldAbort();
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Handle the specified top-level declaration that occurred inside
|
|
|
|
/// and ObjC container.
|
|
|
|
virtual void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) {
|
|
|
|
// They will be handled after the interface is seen first.
|
|
|
|
IndexCtx.addTUDeclInObjCContainer(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief This is called by the AST reader when deserializing things.
|
|
|
|
/// The default implementation forwards to HandleTopLevelDecl but we don't
|
|
|
|
/// care about them when indexing, so have an empty definition.
|
|
|
|
virtual void HandleInterestingDecl(DeclGroupRef D) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2011-11-18 08:26:51 +08:00
|
|
|
// CaptureDiagnosticConsumer
|
2011-10-18 03:48:19 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
class CaptureDiagnosticConsumer : public DiagnosticConsumer {
|
|
|
|
SmallVector<StoredDiagnostic, 4> Errors;
|
|
|
|
public:
|
|
|
|
|
|
|
|
virtual void HandleDiagnostic(DiagnosticsEngine::Level level,
|
|
|
|
const Diagnostic &Info) {
|
|
|
|
if (level >= DiagnosticsEngine::Error)
|
|
|
|
Errors.push_back(StoredDiagnostic(level, Info));
|
|
|
|
}
|
|
|
|
|
|
|
|
DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
|
|
|
|
return new IgnoringDiagConsumer();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// IndexingFrontendAction
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
class IndexingFrontendAction : public ASTFrontendAction {
|
|
|
|
IndexingContext IndexCtx;
|
|
|
|
|
|
|
|
public:
|
|
|
|
IndexingFrontendAction(CXClientData clientData,
|
|
|
|
IndexerCallbacks &indexCallbacks,
|
|
|
|
unsigned indexOptions,
|
|
|
|
CXTranslationUnit cxTU)
|
|
|
|
: IndexCtx(clientData, indexCallbacks, indexOptions, cxTU) { }
|
|
|
|
|
|
|
|
virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
|
|
|
|
StringRef InFile) {
|
|
|
|
IndexCtx.setASTContext(CI.getASTContext());
|
|
|
|
Preprocessor &PP = CI.getPreprocessor();
|
|
|
|
PP.addPPCallbacks(new IndexPPCallbacks(PP, IndexCtx));
|
|
|
|
return new IndexingConsumer(IndexCtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual TranslationUnitKind getTranslationUnitKind() { return TU_Prefix; }
|
|
|
|
virtual bool hasCodeCompletionSupport() const { return false; }
|
|
|
|
};
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2011-11-15 14:20:16 +08:00
|
|
|
// clang_indexSourceFileUnit Implementation
|
2011-10-18 03:48:19 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-11-15 14:20:16 +08:00
|
|
|
struct IndexSourceFileInfo {
|
2011-11-22 15:24:51 +08:00
|
|
|
CXIndexAction idxAction;
|
2011-10-18 03:48:19 +08:00
|
|
|
CXClientData client_data;
|
|
|
|
IndexerCallbacks *index_callbacks;
|
|
|
|
unsigned index_callbacks_size;
|
|
|
|
unsigned index_options;
|
|
|
|
const char *source_filename;
|
|
|
|
const char *const *command_line_args;
|
|
|
|
int num_command_line_args;
|
|
|
|
struct CXUnsavedFile *unsaved_files;
|
|
|
|
unsigned num_unsaved_files;
|
|
|
|
CXTranslationUnit *out_TU;
|
|
|
|
unsigned TU_options;
|
|
|
|
int result;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MemBufferOwner {
|
|
|
|
SmallVector<const llvm::MemoryBuffer *, 8> Buffers;
|
|
|
|
|
|
|
|
~MemBufferOwner() {
|
|
|
|
for (SmallVectorImpl<const llvm::MemoryBuffer *>::iterator
|
|
|
|
I = Buffers.begin(), E = Buffers.end(); I != E; ++I)
|
|
|
|
delete *I;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2011-11-15 14:20:16 +08:00
|
|
|
static void clang_indexSourceFile_Impl(void *UserData) {
|
|
|
|
IndexSourceFileInfo *ITUI =
|
|
|
|
static_cast<IndexSourceFileInfo*>(UserData);
|
2011-11-22 15:24:51 +08:00
|
|
|
CXIndex CIdx = (CXIndex)ITUI->idxAction;
|
2011-10-18 03:48:19 +08:00
|
|
|
CXClientData client_data = ITUI->client_data;
|
|
|
|
IndexerCallbacks *client_index_callbacks = ITUI->index_callbacks;
|
|
|
|
unsigned index_callbacks_size = ITUI->index_callbacks_size;
|
|
|
|
unsigned index_options = ITUI->index_options;
|
|
|
|
const char *source_filename = ITUI->source_filename;
|
|
|
|
const char * const *command_line_args = ITUI->command_line_args;
|
|
|
|
int num_command_line_args = ITUI->num_command_line_args;
|
|
|
|
struct CXUnsavedFile *unsaved_files = ITUI->unsaved_files;
|
|
|
|
unsigned num_unsaved_files = ITUI->num_unsaved_files;
|
|
|
|
CXTranslationUnit *out_TU = ITUI->out_TU;
|
|
|
|
unsigned TU_options = ITUI->TU_options;
|
|
|
|
ITUI->result = 1; // init as error.
|
|
|
|
|
|
|
|
if (out_TU)
|
|
|
|
*out_TU = 0;
|
|
|
|
bool requestedToGetTU = (out_TU != 0);
|
|
|
|
|
|
|
|
if (!CIdx)
|
|
|
|
return;
|
|
|
|
if (!client_index_callbacks || index_callbacks_size == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
IndexerCallbacks CB;
|
|
|
|
memset(&CB, 0, sizeof(CB));
|
|
|
|
unsigned ClientCBSize = index_callbacks_size < sizeof(CB)
|
|
|
|
? index_callbacks_size : sizeof(CB);
|
|
|
|
memcpy(&CB, client_index_callbacks, ClientCBSize);
|
|
|
|
|
|
|
|
CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
|
|
|
|
|
|
|
|
(void)CXXIdx;
|
|
|
|
(void)TU_options;
|
|
|
|
|
|
|
|
CaptureDiagnosticConsumer *CaptureDiag = new CaptureDiagnosticConsumer();
|
|
|
|
|
|
|
|
// Configure the diagnostics.
|
|
|
|
DiagnosticOptions DiagOpts;
|
|
|
|
llvm::IntrusiveRefCntPtr<DiagnosticsEngine>
|
|
|
|
Diags(CompilerInstance::createDiagnostics(DiagOpts, num_command_line_args,
|
|
|
|
command_line_args,
|
|
|
|
CaptureDiag,
|
|
|
|
/*ShouldOwnClient=*/true));
|
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this function.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
|
|
|
|
llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
|
|
|
|
DiagCleanup(Diags.getPtr());
|
|
|
|
|
|
|
|
llvm::OwningPtr<std::vector<const char *> >
|
|
|
|
Args(new std::vector<const char*>());
|
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> >
|
|
|
|
ArgsCleanup(Args.get());
|
|
|
|
|
|
|
|
Args->insert(Args->end(), command_line_args,
|
|
|
|
command_line_args + num_command_line_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.
|
|
|
|
// Put the source file after command_line_args otherwise if '-x' flag is
|
|
|
|
// present it will be unused.
|
|
|
|
if (source_filename)
|
|
|
|
Args->push_back(source_filename);
|
|
|
|
|
|
|
|
llvm::IntrusiveRefCntPtr<CompilerInvocation>
|
|
|
|
CInvok(createInvocationFromCommandLine(*Args, Diags));
|
|
|
|
|
|
|
|
if (!CInvok)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this function.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<CompilerInvocation,
|
|
|
|
llvm::CrashRecoveryContextReleaseRefCleanup<CompilerInvocation> >
|
|
|
|
CInvokCleanup(CInvok.getPtr());
|
|
|
|
|
|
|
|
if (CInvok->getFrontendOpts().Inputs.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
llvm::OwningPtr<MemBufferOwner> BufOwner(new MemBufferOwner());
|
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<MemBufferOwner>
|
|
|
|
BufOwnerCleanup(BufOwner.get());
|
|
|
|
|
|
|
|
for (unsigned I = 0; I != num_unsaved_files; ++I) {
|
|
|
|
StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
|
|
|
|
const llvm::MemoryBuffer *Buffer
|
|
|
|
= llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
|
|
|
|
CInvok->getPreprocessorOpts().addRemappedFile(unsaved_files[I].Filename, Buffer);
|
|
|
|
BufOwner->Buffers.push_back(Buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since libclang is primarily used by batch tools dealing with
|
|
|
|
// (often very broken) source code, where spell-checking can have a
|
|
|
|
// significant negative impact on performance (particularly when
|
|
|
|
// precompiled headers are involved), we disable it.
|
2011-11-18 07:01:24 +08:00
|
|
|
CInvok->getLangOpts()->SpellChecking = false;
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
if (!requestedToGetTU)
|
|
|
|
CInvok->getPreprocessorOpts().DetailedRecord = false;
|
|
|
|
|
2011-11-28 12:55:55 +08:00
|
|
|
ASTUnit *Unit = ASTUnit::create(CInvok.getPtr(), Diags,
|
|
|
|
/*CaptureDiagnostics=*/true);
|
2011-10-18 03:48:19 +08:00
|
|
|
llvm::OwningPtr<CXTUOwner> CXTU(new CXTUOwner(MakeCXTranslationUnit(Unit)));
|
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<CXTUOwner>
|
|
|
|
CXTUCleanup(CXTU.get());
|
|
|
|
|
|
|
|
llvm::OwningPtr<IndexingFrontendAction> IndexAction;
|
|
|
|
IndexAction.reset(new IndexingFrontendAction(client_data, CB,
|
|
|
|
index_options, CXTU->getTU()));
|
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<IndexingFrontendAction>
|
|
|
|
IndexActionCleanup(IndexAction.get());
|
|
|
|
|
|
|
|
Unit = ASTUnit::LoadFromCompilerInvocationAction(CInvok.getPtr(), Diags,
|
|
|
|
IndexAction.get(),
|
|
|
|
Unit);
|
|
|
|
if (!Unit)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// FIXME: Set state of the ASTUnit according to the TU_options.
|
|
|
|
if (out_TU)
|
|
|
|
*out_TU = CXTU->takeTU();
|
|
|
|
|
|
|
|
ITUI->result = 0; // success.
|
|
|
|
}
|
|
|
|
|
2011-11-15 14:20:16 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// clang_indexTranslationUnit Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct IndexTranslationUnitInfo {
|
2011-11-22 15:24:51 +08:00
|
|
|
CXIndexAction idxAction;
|
2011-11-15 14:20:16 +08:00
|
|
|
CXClientData client_data;
|
|
|
|
IndexerCallbacks *index_callbacks;
|
|
|
|
unsigned index_callbacks_size;
|
|
|
|
unsigned index_options;
|
2011-11-22 15:24:51 +08:00
|
|
|
CXTranslationUnit TU;
|
2011-11-15 14:20:16 +08:00
|
|
|
int result;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
static void indexPreprocessingRecord(ASTUnit &Unit, IndexingContext &IdxCtx) {
|
|
|
|
Preprocessor &PP = Unit.getPreprocessor();
|
|
|
|
if (!PP.getPreprocessingRecord())
|
|
|
|
return;
|
|
|
|
|
|
|
|
PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
|
|
|
|
|
|
|
|
// FIXME: Only deserialize inclusion directives.
|
|
|
|
// FIXME: Only deserialize stuff from the last chained PCH, not the PCH/Module
|
|
|
|
// that it depends on.
|
|
|
|
|
|
|
|
bool OnlyLocal = !Unit.isMainFileAST() && Unit.getOnlyLocalDecls();
|
|
|
|
PreprocessingRecord::iterator I, E;
|
|
|
|
if (OnlyLocal) {
|
|
|
|
I = PPRec.local_begin();
|
|
|
|
E = PPRec.local_end();
|
|
|
|
} else {
|
|
|
|
I = PPRec.begin();
|
|
|
|
E = PPRec.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; I != E; ++I) {
|
|
|
|
PreprocessedEntity *PPE = *I;
|
|
|
|
|
|
|
|
if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) {
|
|
|
|
IdxCtx.ppIncludedFile(ID->getSourceRange().getBegin(), ID->getFileName(),
|
|
|
|
ID->getFile(), ID->getKind() == InclusionDirective::Import,
|
|
|
|
!ID->wasInQuotes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void indexTranslationUnit(ASTUnit &Unit, IndexingContext &IdxCtx) {
|
|
|
|
// FIXME: Only deserialize stuff from the last chained PCH, not the PCH/Module
|
|
|
|
// that it depends on.
|
|
|
|
|
|
|
|
bool OnlyLocal = !Unit.isMainFileAST() && Unit.getOnlyLocalDecls();
|
|
|
|
|
|
|
|
if (OnlyLocal) {
|
|
|
|
for (ASTUnit::top_level_iterator TL = Unit.top_level_begin(),
|
|
|
|
TLEnd = Unit.top_level_end();
|
|
|
|
TL != TLEnd; ++TL) {
|
|
|
|
IdxCtx.indexTopLevelDecl(*TL);
|
2011-11-18 08:26:51 +08:00
|
|
|
if (IdxCtx.shouldAbort())
|
|
|
|
return;
|
2011-11-15 14:20:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
TranslationUnitDecl *TUDecl = Unit.getASTContext().getTranslationUnitDecl();
|
|
|
|
for (TranslationUnitDecl::decl_iterator
|
|
|
|
I = TUDecl->decls_begin(), E = TUDecl->decls_end(); I != E; ++I) {
|
|
|
|
IdxCtx.indexTopLevelDecl(*I);
|
2011-11-18 08:26:51 +08:00
|
|
|
if (IdxCtx.shouldAbort())
|
|
|
|
return;
|
2011-11-15 14:20:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void indexDiagnostics(CXTranslationUnit TU, IndexingContext &IdxCtx) {
|
2011-11-18 08:26:51 +08:00
|
|
|
// FIXME: Create a CXDiagnosticSet from TU;
|
|
|
|
// IdxCtx.handleDiagnosticSet(Set);
|
2011-11-15 14:20:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void clang_indexTranslationUnit_Impl(void *UserData) {
|
|
|
|
IndexTranslationUnitInfo *ITUI =
|
|
|
|
static_cast<IndexTranslationUnitInfo*>(UserData);
|
|
|
|
CXTranslationUnit TU = ITUI->TU;
|
|
|
|
CXClientData client_data = ITUI->client_data;
|
|
|
|
IndexerCallbacks *client_index_callbacks = ITUI->index_callbacks;
|
|
|
|
unsigned index_callbacks_size = ITUI->index_callbacks_size;
|
|
|
|
unsigned index_options = ITUI->index_options;
|
|
|
|
ITUI->result = 1; // init as error.
|
|
|
|
|
|
|
|
if (!TU)
|
|
|
|
return;
|
|
|
|
if (!client_index_callbacks || index_callbacks_size == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
IndexerCallbacks CB;
|
|
|
|
memset(&CB, 0, sizeof(CB));
|
|
|
|
unsigned ClientCBSize = index_callbacks_size < sizeof(CB)
|
|
|
|
? index_callbacks_size : sizeof(CB);
|
|
|
|
memcpy(&CB, client_index_callbacks, ClientCBSize);
|
|
|
|
|
|
|
|
llvm::OwningPtr<IndexingContext> IndexCtx;
|
|
|
|
IndexCtx.reset(new IndexingContext(client_data, CB, index_options, TU));
|
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<IndexingContext>
|
|
|
|
IndexCtxCleanup(IndexCtx.get());
|
|
|
|
|
|
|
|
llvm::OwningPtr<IndexingConsumer> IndexConsumer;
|
|
|
|
IndexConsumer.reset(new IndexingConsumer(*IndexCtx));
|
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<IndexingConsumer>
|
|
|
|
IndexConsumerCleanup(IndexConsumer.get());
|
|
|
|
|
|
|
|
ASTUnit *Unit = static_cast<ASTUnit *>(TU->TUData);
|
|
|
|
if (!Unit)
|
|
|
|
return;
|
|
|
|
|
|
|
|
FileManager &FileMgr = Unit->getFileManager();
|
|
|
|
|
|
|
|
if (Unit->getOriginalSourceFileName().empty())
|
|
|
|
IndexCtx->enteredMainFile(0);
|
|
|
|
else
|
|
|
|
IndexCtx->enteredMainFile(FileMgr.getFile(Unit->getOriginalSourceFileName()));
|
|
|
|
|
|
|
|
IndexConsumer->Initialize(Unit->getASTContext());
|
|
|
|
|
|
|
|
indexPreprocessingRecord(*Unit, *IndexCtx);
|
|
|
|
indexTranslationUnit(*Unit, *IndexCtx);
|
|
|
|
indexDiagnostics(TU, *IndexCtx);
|
|
|
|
|
|
|
|
ITUI->result = 0;
|
|
|
|
}
|
|
|
|
|
2011-10-18 03:48:19 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// libclang public APIs.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
2011-11-12 10:16:30 +08:00
|
|
|
int clang_index_isEntityObjCContainerKind(CXIdxEntityKind K) {
|
|
|
|
return CXIdxEntity_ObjCClass <= K && K <= CXIdxEntity_ObjCCategory;
|
2011-11-11 08:23:36 +08:00
|
|
|
}
|
|
|
|
|
2011-11-12 10:16:30 +08:00
|
|
|
const CXIdxObjCContainerDeclInfo *
|
|
|
|
clang_index_getObjCContainerDeclInfo(const CXIdxDeclInfo *DInfo) {
|
|
|
|
if (!DInfo)
|
|
|
|
return 0;
|
|
|
|
|
2011-11-15 06:39:19 +08:00
|
|
|
const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
|
|
|
|
if (const ObjCContainerDeclInfo *
|
|
|
|
ContInfo = dyn_cast<ObjCContainerDeclInfo>(DI))
|
|
|
|
return &ContInfo->ObjCContDeclInfo;
|
2011-11-11 08:23:36 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-12 10:16:30 +08:00
|
|
|
const CXIdxObjCInterfaceDeclInfo *
|
|
|
|
clang_index_getObjCInterfaceDeclInfo(const CXIdxDeclInfo *DInfo) {
|
2011-11-15 06:39:19 +08:00
|
|
|
if (!DInfo)
|
2011-11-12 10:16:30 +08:00
|
|
|
return 0;
|
2011-11-11 08:23:36 +08:00
|
|
|
|
2011-11-15 06:39:19 +08:00
|
|
|
const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
|
|
|
|
if (const ObjCInterfaceDeclInfo *
|
|
|
|
InterInfo = dyn_cast<ObjCInterfaceDeclInfo>(DI))
|
|
|
|
return &InterInfo->ObjCInterDeclInfo;
|
2011-11-11 08:23:36 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-15 06:39:19 +08:00
|
|
|
const CXIdxObjCCategoryDeclInfo *
|
|
|
|
clang_index_getObjCCategoryDeclInfo(const CXIdxDeclInfo *DInfo){
|
|
|
|
if (!DInfo)
|
2011-11-12 10:16:30 +08:00
|
|
|
return 0;
|
2011-11-11 08:23:36 +08:00
|
|
|
|
2011-11-15 06:39:19 +08:00
|
|
|
const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
|
|
|
|
if (const ObjCCategoryDeclInfo *
|
|
|
|
CatInfo = dyn_cast<ObjCCategoryDeclInfo>(DI))
|
|
|
|
return &CatInfo->ObjCCatDeclInfo;
|
2011-11-11 08:23:36 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-15 06:39:19 +08:00
|
|
|
const CXIdxObjCProtocolRefListInfo *
|
|
|
|
clang_index_getObjCProtocolRefListInfo(const CXIdxDeclInfo *DInfo) {
|
|
|
|
if (!DInfo)
|
2011-11-12 10:16:30 +08:00
|
|
|
return 0;
|
|
|
|
|
2011-11-15 06:39:19 +08:00
|
|
|
const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
|
|
|
|
|
|
|
|
if (const ObjCInterfaceDeclInfo *
|
|
|
|
InterInfo = dyn_cast<ObjCInterfaceDeclInfo>(DI))
|
|
|
|
return InterInfo->ObjCInterDeclInfo.protocols;
|
|
|
|
|
|
|
|
if (const ObjCProtocolDeclInfo *
|
|
|
|
ProtInfo = dyn_cast<ObjCProtocolDeclInfo>(DI))
|
|
|
|
return &ProtInfo->ObjCProtoRefListInfo;
|
|
|
|
|
|
|
|
return 0;
|
2011-11-12 10:16:30 +08:00
|
|
|
}
|
|
|
|
|
2011-11-18 08:26:51 +08:00
|
|
|
const CXIdxIBOutletCollectionAttrInfo *
|
|
|
|
clang_index_getIBOutletCollectionAttrInfo(const CXIdxAttrInfo *AInfo) {
|
|
|
|
if (!AInfo)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const AttrInfo *DI = static_cast<const AttrInfo *>(AInfo);
|
|
|
|
if (const IBOutletCollectionInfo *
|
|
|
|
IBInfo = dyn_cast<IBOutletCollectionInfo>(DI))
|
|
|
|
return &IBInfo->IBCollInfo;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-22 15:24:51 +08:00
|
|
|
const CXIdxCXXClassDeclInfo *
|
|
|
|
clang_index_getCXXClassDeclInfo(const CXIdxDeclInfo *DInfo) {
|
|
|
|
if (!DInfo)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
|
|
|
|
if (const CXXClassDeclInfo *ClassInfo = dyn_cast<CXXClassDeclInfo>(DI))
|
|
|
|
return &ClassInfo->CXXClassInfo;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
CXIdxClientContainer
|
|
|
|
clang_index_getClientContainer(const CXIdxContainerInfo *info) {
|
|
|
|
if (!info)
|
|
|
|
return 0;
|
|
|
|
ContainerInfo *Container = (ContainerInfo*)info;
|
|
|
|
return Container->IndexCtx->getClientContainerForDC(Container->DC);
|
|
|
|
}
|
|
|
|
|
|
|
|
void clang_index_setClientContainer(const CXIdxContainerInfo *info,
|
|
|
|
CXIdxClientContainer client) {
|
|
|
|
if (!info)
|
|
|
|
return;
|
|
|
|
ContainerInfo *Container = (ContainerInfo*)info;
|
|
|
|
Container->IndexCtx->addContainerInMap(Container->DC, client);
|
|
|
|
}
|
|
|
|
|
|
|
|
CXIdxClientEntity clang_index_getClientEntity(const CXIdxEntityInfo *info) {
|
|
|
|
if (!info)
|
|
|
|
return 0;
|
|
|
|
EntityInfo *Entity = (EntityInfo*)info;
|
|
|
|
return Entity->IndexCtx->getClientEntity(Entity->Dcl);
|
|
|
|
}
|
|
|
|
|
|
|
|
void clang_index_setClientEntity(const CXIdxEntityInfo *info,
|
|
|
|
CXIdxClientEntity client) {
|
|
|
|
if (!info)
|
|
|
|
return;
|
|
|
|
EntityInfo *Entity = (EntityInfo*)info;
|
|
|
|
Entity->IndexCtx->setClientEntity(Entity->Dcl, client);
|
|
|
|
}
|
|
|
|
|
|
|
|
CXIndexAction clang_IndexAction_create(CXIndex CIdx) {
|
|
|
|
// For now, CXIndexAction is featureless.
|
|
|
|
return CIdx;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clang_IndexAction_dispose(CXIndexAction idxAction) {
|
|
|
|
// For now, CXIndexAction is featureless.
|
|
|
|
}
|
|
|
|
|
|
|
|
int clang_indexSourceFile(CXIndexAction idxAction,
|
|
|
|
CXClientData client_data,
|
|
|
|
IndexerCallbacks *index_callbacks,
|
|
|
|
unsigned index_callbacks_size,
|
|
|
|
unsigned index_options,
|
|
|
|
const char *source_filename,
|
|
|
|
const char * const *command_line_args,
|
|
|
|
int num_command_line_args,
|
|
|
|
struct CXUnsavedFile *unsaved_files,
|
|
|
|
unsigned num_unsaved_files,
|
|
|
|
CXTranslationUnit *out_TU,
|
|
|
|
unsigned TU_options) {
|
|
|
|
|
|
|
|
IndexSourceFileInfo ITUI = { idxAction, client_data, index_callbacks,
|
|
|
|
index_callbacks_size, index_options,
|
|
|
|
source_filename, command_line_args,
|
|
|
|
num_command_line_args, unsaved_files,
|
|
|
|
num_unsaved_files, out_TU, TU_options, 0 };
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2011-10-30 03:32:39 +08:00
|
|
|
if (getenv("LIBCLANG_NOTHREADS")) {
|
2011-11-15 14:20:16 +08:00
|
|
|
clang_indexSourceFile_Impl(&ITUI);
|
2011-10-18 03:48:19 +08:00
|
|
|
return ITUI.result;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::CrashRecoveryContext CRC;
|
|
|
|
|
2011-11-15 14:20:16 +08:00
|
|
|
if (!RunSafely(CRC, clang_indexSourceFile_Impl, &ITUI)) {
|
|
|
|
fprintf(stderr, "libclang: crash detected during indexing source file: {\n");
|
2011-10-18 03:48:19 +08:00
|
|
|
fprintf(stderr, " 'source_filename' : '%s'\n", source_filename);
|
|
|
|
fprintf(stderr, " 'command_line_args' : [");
|
|
|
|
for (int i = 0; i != num_command_line_args; ++i) {
|
|
|
|
if (i)
|
|
|
|
fprintf(stderr, ", ");
|
|
|
|
fprintf(stderr, "'%s'", command_line_args[i]);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "],\n");
|
|
|
|
fprintf(stderr, " 'unsaved_files' : [");
|
|
|
|
for (unsigned i = 0; i != num_unsaved_files; ++i) {
|
|
|
|
if (i)
|
|
|
|
fprintf(stderr, ", ");
|
|
|
|
fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename,
|
|
|
|
unsaved_files[i].Length);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "],\n");
|
|
|
|
fprintf(stderr, " 'options' : %d,\n", TU_options);
|
|
|
|
fprintf(stderr, "}\n");
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
} else if (getenv("LIBCLANG_RESOURCE_USAGE")) {
|
|
|
|
if (out_TU)
|
|
|
|
PrintLibclangResourceUsage(*out_TU);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ITUI.result;
|
|
|
|
}
|
|
|
|
|
2011-11-22 15:24:51 +08:00
|
|
|
int clang_indexTranslationUnit(CXIndexAction idxAction,
|
2011-11-15 14:20:16 +08:00
|
|
|
CXClientData client_data,
|
|
|
|
IndexerCallbacks *index_callbacks,
|
|
|
|
unsigned index_callbacks_size,
|
2011-11-22 15:24:51 +08:00
|
|
|
unsigned index_options,
|
|
|
|
CXTranslationUnit TU) {
|
2011-11-15 14:20:16 +08:00
|
|
|
|
2011-11-22 15:24:51 +08:00
|
|
|
IndexTranslationUnitInfo ITUI = { idxAction, client_data, index_callbacks,
|
|
|
|
index_callbacks_size, index_options, TU,
|
|
|
|
0 };
|
2011-11-15 14:20:16 +08:00
|
|
|
|
|
|
|
if (getenv("LIBCLANG_NOTHREADS")) {
|
|
|
|
clang_indexTranslationUnit_Impl(&ITUI);
|
|
|
|
return ITUI.result;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::CrashRecoveryContext CRC;
|
|
|
|
|
|
|
|
if (!RunSafely(CRC, clang_indexTranslationUnit_Impl, &ITUI)) {
|
|
|
|
fprintf(stderr, "libclang: crash detected during indexing TU\n");
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ITUI.result;
|
|
|
|
}
|
|
|
|
|
2011-10-18 03:48:19 +08:00
|
|
|
void clang_indexLoc_getFileLocation(CXIdxLoc location,
|
2011-11-11 08:23:36 +08:00
|
|
|
CXIdxClientFile *indexFile,
|
2011-10-18 03:48:19 +08:00
|
|
|
CXFile *file,
|
|
|
|
unsigned *line,
|
|
|
|
unsigned *column,
|
|
|
|
unsigned *offset) {
|
|
|
|
if (indexFile) *indexFile = 0;
|
|
|
|
if (file) *file = 0;
|
|
|
|
if (line) *line = 0;
|
|
|
|
if (column) *column = 0;
|
|
|
|
if (offset) *offset = 0;
|
|
|
|
|
|
|
|
SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
|
|
|
|
if (!location.ptr_data[0] || Loc.isInvalid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
IndexingContext &IndexCtx =
|
|
|
|
*static_cast<IndexingContext*>(location.ptr_data[0]);
|
|
|
|
IndexCtx.translateLoc(Loc, indexFile, file, line, column, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
CXSourceLocation clang_indexLoc_getCXSourceLocation(CXIdxLoc location) {
|
|
|
|
SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
|
|
|
|
if (!location.ptr_data[0] || Loc.isInvalid())
|
|
|
|
return clang_getNullLocation();
|
|
|
|
|
|
|
|
IndexingContext &IndexCtx =
|
|
|
|
*static_cast<IndexingContext*>(location.ptr_data[0]);
|
|
|
|
return cxloc::translateSourceLocation(IndexCtx.getASTContext(), Loc);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end: extern "C"
|
|
|
|
|