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"
|
2012-12-04 17:25:21 +08:00
|
|
|
#include "CIndexDiagnostic.h"
|
|
|
|
#include "CIndexer.h"
|
2013-01-19 16:09:44 +08:00
|
|
|
#include "CLog.h"
|
2011-10-18 03:48:19 +08:00
|
|
|
#include "CXCursor.h"
|
|
|
|
#include "CXSourceLocation.h"
|
|
|
|
#include "CXString.h"
|
2012-12-04 17:25:21 +08:00
|
|
|
#include "CXTranslationUnit.h"
|
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#include "clang/AST/DeclVisitor.h"
|
2011-10-18 03:48:19 +08:00
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
2012-12-04 17:25:21 +08:00
|
|
|
#include "clang/Frontend/CompilerInvocation.h"
|
2012-01-21 00:28:04 +08:00
|
|
|
#include "clang/Frontend/FrontendAction.h"
|
2011-10-18 03:48:19 +08:00
|
|
|
#include "clang/Frontend/Utils.h"
|
2013-01-02 18:28:36 +08:00
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
2011-10-18 03:48:19 +08:00
|
|
|
#include "clang/Lex/PPCallbacks.h"
|
2012-12-07 03:41:16 +08:00
|
|
|
#include "clang/Lex/PPConditionalDirectiveRecord.h"
|
2013-01-02 18:28:36 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2012-12-04 17:25:21 +08:00
|
|
|
#include "clang/Sema/SemaConsumer.h"
|
2011-10-18 03:48:19 +08:00
|
|
|
#include "llvm/Support/CrashRecoveryContext.h"
|
2012-12-04 17:25:21 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2012-12-07 03:41:16 +08:00
|
|
|
#include "llvm/Support/Mutex.h"
|
|
|
|
#include "llvm/Support/MutexGuard.h"
|
2013-08-20 00:14:33 +08:00
|
|
|
#include <cstdio>
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace cxtu;
|
|
|
|
using namespace cxindex;
|
|
|
|
|
2011-12-01 10:42:50 +08:00
|
|
|
static void indexDiagnostics(CXTranslationUnit TU, IndexingContext &IdxCtx);
|
|
|
|
|
2011-10-18 03:48:19 +08:00
|
|
|
namespace {
|
|
|
|
|
2012-12-07 03:41:16 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Skip Parsed Bodies
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifdef LLVM_ON_WIN32
|
|
|
|
|
|
|
|
// FIXME: On windows it is disabled since current implementation depends on
|
|
|
|
// file inodes.
|
|
|
|
|
|
|
|
class SessionSkipBodyData { };
|
|
|
|
|
|
|
|
class TUSkipBodyControl {
|
|
|
|
public:
|
|
|
|
TUSkipBodyControl(SessionSkipBodyData &sessionData,
|
2013-01-31 22:27:38 +08:00
|
|
|
PPConditionalDirectiveRecord &ppRec,
|
|
|
|
Preprocessor &pp) { }
|
2012-12-07 03:41:16 +08:00
|
|
|
bool isParsed(SourceLocation Loc, FileID FID, const FileEntry *FE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
void finished() { }
|
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
/// \brief A "region" in source code identified by the file/offset of the
|
|
|
|
/// preprocessor conditional directive that it belongs to.
|
|
|
|
/// Multiple, non-consecutive ranges can be parts of the same region.
|
|
|
|
///
|
|
|
|
/// As an example of different regions separated by preprocessor directives:
|
|
|
|
///
|
|
|
|
/// \code
|
|
|
|
/// #1
|
|
|
|
/// #ifdef BLAH
|
|
|
|
/// #2
|
|
|
|
/// #ifdef CAKE
|
|
|
|
/// #3
|
|
|
|
/// #endif
|
|
|
|
/// #2
|
|
|
|
/// #endif
|
|
|
|
/// #1
|
|
|
|
/// \endcode
|
|
|
|
///
|
|
|
|
/// There are 3 regions, with non-consecutive parts:
|
|
|
|
/// #1 is identified as the beginning of the file
|
|
|
|
/// #2 is identified as the location of "#ifdef BLAH"
|
|
|
|
/// #3 is identified as the location of "#ifdef CAKE"
|
|
|
|
///
|
|
|
|
class PPRegion {
|
2013-08-02 05:42:11 +08:00
|
|
|
llvm::sys::fs::UniqueID UniqueID;
|
2012-12-07 03:41:16 +08:00
|
|
|
time_t ModTime;
|
|
|
|
unsigned Offset;
|
|
|
|
public:
|
2013-08-02 17:06:39 +08:00
|
|
|
PPRegion() : UniqueID(0, 0), ModTime(), Offset() {}
|
2013-08-02 05:42:11 +08:00
|
|
|
PPRegion(llvm::sys::fs::UniqueID UniqueID, unsigned offset, time_t modTime)
|
|
|
|
: UniqueID(UniqueID), ModTime(modTime), Offset(offset) {}
|
2012-12-07 03:41:16 +08:00
|
|
|
|
2013-08-02 05:42:11 +08:00
|
|
|
const llvm::sys::fs::UniqueID &getUniqueID() const { return UniqueID; }
|
2012-12-07 03:41:16 +08:00
|
|
|
unsigned getOffset() const { return Offset; }
|
|
|
|
time_t getModTime() const { return ModTime; }
|
|
|
|
|
|
|
|
bool isInvalid() const { return *this == PPRegion(); }
|
|
|
|
|
|
|
|
friend bool operator==(const PPRegion &lhs, const PPRegion &rhs) {
|
2013-08-02 05:42:11 +08:00
|
|
|
return lhs.UniqueID == rhs.UniqueID && lhs.Offset == rhs.Offset &&
|
|
|
|
lhs.ModTime == rhs.ModTime;
|
2012-12-07 03:41:16 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef llvm::DenseSet<PPRegion> PPRegionSetTy;
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
template <> struct isPodLike<PPRegion> {
|
|
|
|
static const bool value = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct DenseMapInfo<PPRegion> {
|
|
|
|
static inline PPRegion getEmptyKey() {
|
2013-08-02 05:42:11 +08:00
|
|
|
return PPRegion(llvm::sys::fs::UniqueID(0, 0), unsigned(-1), 0);
|
2012-12-07 03:41:16 +08:00
|
|
|
}
|
|
|
|
static inline PPRegion getTombstoneKey() {
|
2013-08-02 05:42:11 +08:00
|
|
|
return PPRegion(llvm::sys::fs::UniqueID(0, 0), unsigned(-2), 0);
|
2012-12-07 03:41:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned getHashValue(const PPRegion &S) {
|
|
|
|
llvm::FoldingSetNodeID ID;
|
2013-08-02 05:42:11 +08:00
|
|
|
const llvm::sys::fs::UniqueID &UniqueID = S.getUniqueID();
|
|
|
|
ID.AddInteger(UniqueID.getFile());
|
|
|
|
ID.AddInteger(UniqueID.getDevice());
|
2012-12-07 03:41:16 +08:00
|
|
|
ID.AddInteger(S.getOffset());
|
|
|
|
ID.AddInteger(S.getModTime());
|
|
|
|
return ID.ComputeHash();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isEqual(const PPRegion &LHS, const PPRegion &RHS) {
|
|
|
|
return LHS == RHS;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class SessionSkipBodyData {
|
|
|
|
llvm::sys::Mutex Mux;
|
|
|
|
PPRegionSetTy ParsedRegions;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SessionSkipBodyData() : Mux(/*recursive=*/false) {}
|
|
|
|
~SessionSkipBodyData() {
|
|
|
|
//llvm::errs() << "RegionData: " << Skipped.size() << " - " << Skipped.getMemorySize() << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void copyTo(PPRegionSetTy &Set) {
|
|
|
|
llvm::MutexGuard MG(Mux);
|
|
|
|
Set = ParsedRegions;
|
|
|
|
}
|
|
|
|
|
|
|
|
void update(ArrayRef<PPRegion> Regions) {
|
|
|
|
llvm::MutexGuard MG(Mux);
|
|
|
|
ParsedRegions.insert(Regions.begin(), Regions.end());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TUSkipBodyControl {
|
|
|
|
SessionSkipBodyData &SessionData;
|
|
|
|
PPConditionalDirectiveRecord &PPRec;
|
|
|
|
Preprocessor &PP;
|
|
|
|
|
|
|
|
PPRegionSetTy ParsedRegions;
|
|
|
|
SmallVector<PPRegion, 32> NewParsedRegions;
|
|
|
|
PPRegion LastRegion;
|
|
|
|
bool LastIsParsed;
|
|
|
|
|
|
|
|
public:
|
|
|
|
TUSkipBodyControl(SessionSkipBodyData &sessionData,
|
|
|
|
PPConditionalDirectiveRecord &ppRec,
|
|
|
|
Preprocessor &pp)
|
|
|
|
: SessionData(sessionData), PPRec(ppRec), PP(pp) {
|
|
|
|
SessionData.copyTo(ParsedRegions);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isParsed(SourceLocation Loc, FileID FID, const FileEntry *FE) {
|
|
|
|
PPRegion region = getRegion(Loc, FID, FE);
|
|
|
|
if (region.isInvalid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check common case, consecutive functions in the same region.
|
|
|
|
if (LastRegion == region)
|
|
|
|
return LastIsParsed;
|
|
|
|
|
|
|
|
LastRegion = region;
|
|
|
|
LastIsParsed = ParsedRegions.count(region);
|
|
|
|
if (!LastIsParsed)
|
|
|
|
NewParsedRegions.push_back(region);
|
|
|
|
return LastIsParsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void finished() {
|
|
|
|
SessionData.update(NewParsedRegions);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
PPRegion getRegion(SourceLocation Loc, FileID FID, const FileEntry *FE) {
|
|
|
|
SourceLocation RegionLoc = PPRec.findConditionalDirectiveRegionLoc(Loc);
|
|
|
|
if (RegionLoc.isInvalid()) {
|
2013-08-02 05:42:11 +08:00
|
|
|
if (isParsedOnceInclude(FE)) {
|
|
|
|
const llvm::sys::fs::UniqueID &ID = FE->getUniqueID();
|
|
|
|
return PPRegion(ID, 0, FE->getModificationTime());
|
|
|
|
}
|
2012-12-07 03:41:16 +08:00
|
|
|
return PPRegion();
|
|
|
|
}
|
|
|
|
|
|
|
|
const SourceManager &SM = PPRec.getSourceManager();
|
|
|
|
assert(RegionLoc.isFileID());
|
|
|
|
FileID RegionFID;
|
|
|
|
unsigned RegionOffset;
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(RegionFID, RegionOffset) = SM.getDecomposedLoc(RegionLoc);
|
2012-12-07 03:41:16 +08:00
|
|
|
|
|
|
|
if (RegionFID != FID) {
|
2013-08-02 05:42:11 +08:00
|
|
|
if (isParsedOnceInclude(FE)) {
|
|
|
|
const llvm::sys::fs::UniqueID &ID = FE->getUniqueID();
|
|
|
|
return PPRegion(ID, 0, FE->getModificationTime());
|
|
|
|
}
|
2012-12-07 03:41:16 +08:00
|
|
|
return PPRegion();
|
|
|
|
}
|
|
|
|
|
2013-08-02 05:42:11 +08:00
|
|
|
const llvm::sys::fs::UniqueID &ID = FE->getUniqueID();
|
|
|
|
return PPRegion(ID, RegionOffset, FE->getModificationTime());
|
2012-12-07 03:41:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isParsedOnceInclude(const FileEntry *FE) {
|
|
|
|
return PP.getHeaderSearchInfo().isFileMultipleIncludeGuarded(FE);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2011-10-18 03:48:19 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// 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) { }
|
|
|
|
|
2014-03-15 15:47:46 +08:00
|
|
|
void FileChanged(SourceLocation Loc, FileChangeReason Reason,
|
|
|
|
SrcMgr::CharacteristicKind FileType, FileID PrevFID) override {
|
2011-11-11 08:23:36 +08:00
|
|
|
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
|
|
|
|
2014-03-15 15:47:46 +08:00
|
|
|
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
|
|
|
|
StringRef FileName, bool IsAngled,
|
|
|
|
CharSourceRange FilenameRange, const FileEntry *File,
|
|
|
|
StringRef SearchPath, StringRef RelativePath,
|
|
|
|
const Module *Imported) override {
|
2011-10-18 03:48:19 +08:00
|
|
|
bool isImport = (IncludeTok.is(tok::identifier) &&
|
|
|
|
IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import);
|
2012-10-18 08:17:05 +08:00
|
|
|
IndexCtx.ppIncludedFile(HashLoc, FileName, File, isImport, IsAngled,
|
|
|
|
Imported);
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// MacroDefined - This hook is called whenever a macro definition is seen.
|
2014-03-15 15:47:46 +08:00
|
|
|
void MacroDefined(const Token &Id, const MacroDirective *MD) override {}
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
/// MacroUndefined - This hook is called whenever a macro #undef is seen.
|
|
|
|
/// MI is released immediately following this callback.
|
2014-03-15 15:47:46 +08:00
|
|
|
void MacroUndefined(const Token &MacroNameTok,
|
|
|
|
const MacroDirective *MD) override {}
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
/// MacroExpands - This is called by when a macro invocation is found.
|
2014-03-15 15:47:46 +08:00
|
|
|
void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD,
|
|
|
|
SourceRange Range, const MacroArgs *Args) override {}
|
2012-12-07 03:41:16 +08:00
|
|
|
|
2011-10-18 03:48:19 +08:00
|
|
|
/// 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.
|
2014-03-15 15:47:46 +08:00
|
|
|
void SourceRangeSkipped(SourceRange Range) override {}
|
2011-10-18 03:48:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// IndexingConsumer
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
class IndexingConsumer : public ASTConsumer {
|
|
|
|
IndexingContext &IndexCtx;
|
2012-12-07 03:41:16 +08:00
|
|
|
TUSkipBodyControl *SKCtrl;
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
public:
|
2012-12-07 03:41:16 +08:00
|
|
|
IndexingConsumer(IndexingContext &indexCtx, TUSkipBodyControl *skCtrl)
|
|
|
|
: IndexCtx(indexCtx), SKCtrl(skCtrl) { }
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
// ASTConsumer Implementation
|
|
|
|
|
2014-03-15 15:47:46 +08:00
|
|
|
void Initialize(ASTContext &Context) override {
|
2011-10-18 03:48:19 +08:00
|
|
|
IndexCtx.setASTContext(Context);
|
2011-11-12 10:16:30 +08:00
|
|
|
IndexCtx.startedTranslationUnit();
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
2014-03-15 15:47:46 +08:00
|
|
|
void HandleTranslationUnit(ASTContext &Ctx) override {
|
2012-12-07 03:41:16 +08:00
|
|
|
if (SKCtrl)
|
|
|
|
SKCtrl->finished();
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
2014-03-15 15:47:46 +08:00
|
|
|
bool HandleTopLevelDecl(DeclGroupRef DG) override {
|
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.
|
2014-03-15 15:47:46 +08:00
|
|
|
void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override {
|
2011-10-18 03:48:19 +08:00
|
|
|
// 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.
|
2014-03-15 15:47:46 +08:00
|
|
|
void HandleInterestingDecl(DeclGroupRef D) override {}
|
2012-02-11 04:10:44 +08:00
|
|
|
|
2014-03-15 15:47:46 +08:00
|
|
|
void HandleTagDeclDefinition(TagDecl *D) override {
|
2012-02-15 06:23:11 +08:00
|
|
|
if (!IndexCtx.shouldIndexImplicitTemplateInsts())
|
|
|
|
return;
|
|
|
|
|
2012-02-11 04:10:44 +08:00
|
|
|
if (IndexCtx.isTemplateImplicitInstantiation(D))
|
|
|
|
IndexCtx.indexDecl(D);
|
|
|
|
}
|
|
|
|
|
2014-03-15 15:47:46 +08:00
|
|
|
void HandleCXXImplicitFunctionInstantiation(FunctionDecl *D) override {
|
2012-02-15 06:23:11 +08:00
|
|
|
if (!IndexCtx.shouldIndexImplicitTemplateInsts())
|
|
|
|
return;
|
|
|
|
|
2012-02-11 04:10:44 +08:00
|
|
|
IndexCtx.indexDecl(D);
|
|
|
|
}
|
2012-12-07 03:41:16 +08:00
|
|
|
|
2014-03-15 15:47:46 +08:00
|
|
|
bool shouldSkipFunctionBody(Decl *D) override {
|
2012-12-07 03:41:16 +08:00
|
|
|
if (!SKCtrl) {
|
|
|
|
// Always skip bodies.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SourceManager &SM = IndexCtx.getASTContext().getSourceManager();
|
|
|
|
SourceLocation Loc = D->getLocation();
|
|
|
|
if (Loc.isMacroID())
|
|
|
|
return false;
|
|
|
|
if (SM.isInSystemHeader(Loc))
|
|
|
|
return true; // always skip bodies from system headers.
|
|
|
|
|
|
|
|
FileID FID;
|
|
|
|
unsigned Offset;
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(FID, Offset) = SM.getDecomposedLoc(Loc);
|
2012-12-07 03:41:16 +08:00
|
|
|
// Don't skip bodies from main files; this may be revisited.
|
|
|
|
if (SM.getMainFileID() == FID)
|
|
|
|
return false;
|
|
|
|
const FileEntry *FE = SM.getFileEntryForID(FID);
|
|
|
|
if (!FE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return SKCtrl->isParsed(Loc, FID, FE);
|
|
|
|
}
|
2011-10-18 03:48:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
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:
|
|
|
|
|
2014-03-15 15:47:46 +08:00
|
|
|
void HandleDiagnostic(DiagnosticsEngine::Level level,
|
|
|
|
const Diagnostic &Info) override {
|
2011-10-18 03:48:19 +08:00
|
|
|
if (level >= DiagnosticsEngine::Error)
|
|
|
|
Errors.push_back(StoredDiagnostic(level, Info));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// IndexingFrontendAction
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
class IndexingFrontendAction : public ASTFrontendAction {
|
|
|
|
IndexingContext IndexCtx;
|
2011-12-01 10:42:50 +08:00
|
|
|
CXTranslationUnit CXTU;
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2012-12-07 03:41:16 +08:00
|
|
|
SessionSkipBodyData *SKData;
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<TUSkipBodyControl> SKCtrl;
|
2012-12-07 03:41:16 +08:00
|
|
|
|
2011-10-18 03:48:19 +08:00
|
|
|
public:
|
|
|
|
IndexingFrontendAction(CXClientData clientData,
|
|
|
|
IndexerCallbacks &indexCallbacks,
|
|
|
|
unsigned indexOptions,
|
2012-12-07 03:41:16 +08:00
|
|
|
CXTranslationUnit cxTU,
|
|
|
|
SessionSkipBodyData *skData)
|
2011-12-01 10:42:50 +08:00
|
|
|
: IndexCtx(clientData, indexCallbacks, indexOptions, cxTU),
|
2012-12-07 03:41:16 +08:00
|
|
|
CXTU(cxTU), SKData(skData) { }
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2014-03-15 15:47:46 +08:00
|
|
|
ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
|
|
|
|
StringRef InFile) override {
|
2012-10-04 05:05:51 +08:00
|
|
|
PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
|
|
|
|
|
|
|
|
if (!PPOpts.ImplicitPCHInclude.empty()) {
|
|
|
|
IndexCtx.importedPCH(
|
|
|
|
CI.getFileManager().getFile(PPOpts.ImplicitPCHInclude));
|
|
|
|
}
|
2012-10-03 00:10:41 +08:00
|
|
|
|
2011-10-18 03:48:19 +08:00
|
|
|
IndexCtx.setASTContext(CI.getASTContext());
|
|
|
|
Preprocessor &PP = CI.getPreprocessor();
|
|
|
|
PP.addPPCallbacks(new IndexPPCallbacks(PP, IndexCtx));
|
2012-01-18 02:48:07 +08:00
|
|
|
IndexCtx.setPreprocessor(PP);
|
2012-12-07 03:41:16 +08:00
|
|
|
|
|
|
|
if (SKData) {
|
|
|
|
PPConditionalDirectiveRecord *
|
|
|
|
PPRec = new PPConditionalDirectiveRecord(PP.getSourceManager());
|
|
|
|
PP.addPPCallbacks(PPRec);
|
|
|
|
SKCtrl.reset(new TUSkipBodyControl(*SKData, *PPRec, PP));
|
|
|
|
}
|
|
|
|
|
|
|
|
return new IndexingConsumer(IndexCtx, SKCtrl.get());
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
2014-03-15 15:47:46 +08:00
|
|
|
void EndSourceFileAction() override {
|
2011-12-01 10:42:50 +08:00
|
|
|
indexDiagnostics(CXTU, IndexCtx);
|
|
|
|
}
|
|
|
|
|
2014-03-15 15:47:46 +08:00
|
|
|
TranslationUnitKind getTranslationUnitKind() override {
|
2012-02-15 06:23:11 +08:00
|
|
|
if (IndexCtx.shouldIndexImplicitTemplateInsts())
|
|
|
|
return TU_Complete;
|
|
|
|
else
|
|
|
|
return TU_Prefix;
|
|
|
|
}
|
2014-03-15 15:47:46 +08:00
|
|
|
bool hasCodeCompletionSupport() const override { return false; }
|
2011-10-18 03:48:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2011-11-15 14:20:16 +08:00
|
|
|
// clang_indexSourceFileUnit Implementation
|
2011-10-18 03:48:19 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-07 03:41:16 +08:00
|
|
|
struct IndexSessionData {
|
|
|
|
CXIndex CIdx;
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<SessionSkipBodyData> SkipBodyData;
|
2012-12-07 03:41:16 +08:00
|
|
|
|
|
|
|
explicit IndexSessionData(CXIndex cIdx)
|
|
|
|
: CIdx(cIdx), SkipBodyData(new SessionSkipBodyData) {}
|
|
|
|
};
|
|
|
|
|
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);
|
2012-12-07 03:41:16 +08:00
|
|
|
CXIndexAction cxIdxAction = 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;
|
2014-02-13 03:12:37 +08:00
|
|
|
|
|
|
|
// Set up the initial return value.
|
|
|
|
ITUI->result = CXError_Failure;
|
|
|
|
|
2011-10-18 03:48:19 +08:00
|
|
|
if (out_TU)
|
|
|
|
*out_TU = 0;
|
2014-02-13 03:12:37 +08:00
|
|
|
bool requestedToGetTU = (out_TU != 0);
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2014-02-13 03:12:37 +08:00
|
|
|
if (!cxIdxAction) {
|
|
|
|
ITUI->result = CXError_InvalidArguments;
|
2011-10-18 03:48:19 +08:00
|
|
|
return;
|
2014-02-13 03:12:37 +08:00
|
|
|
}
|
|
|
|
if (!client_index_callbacks || index_callbacks_size == 0) {
|
|
|
|
ITUI->result = CXError_InvalidArguments;
|
2011-10-18 03:48:19 +08:00
|
|
|
return;
|
2014-02-13 03:12:37 +08:00
|
|
|
}
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2012-12-07 03:41:16 +08:00
|
|
|
IndexSessionData *IdxSession = static_cast<IndexSessionData *>(cxIdxAction);
|
|
|
|
CIndexer *CXXIdx = static_cast<CIndexer *>(IdxSession->CIdx);
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2012-03-28 10:18:05 +08:00
|
|
|
if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
|
2012-03-28 10:49:54 +08:00
|
|
|
setThreadBackgroundPriority();
|
2012-03-28 10:18:05 +08:00
|
|
|
|
2013-03-30 05:51:44 +08:00
|
|
|
bool CaptureDiagnostics = !Logger::isLoggingEnabled();
|
|
|
|
|
|
|
|
CaptureDiagnosticConsumer *CaptureDiag = 0;
|
|
|
|
if (CaptureDiagnostics)
|
|
|
|
CaptureDiag = new CaptureDiagnosticConsumer();
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
// Configure the diagnostics.
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine>
|
2012-10-24 06:26:28 +08:00
|
|
|
Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions,
|
|
|
|
CaptureDiag,
|
2013-05-04 07:07:45 +08:00
|
|
|
/*ShouldOwnClient=*/true));
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this function.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
|
|
|
|
llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
|
|
|
|
DiagCleanup(Diags.getPtr());
|
2014-03-08 04:03:18 +08:00
|
|
|
|
|
|
|
std::unique_ptr<std::vector<const char *>> Args(
|
|
|
|
new std::vector<const char *>());
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<CompilerInvocation>
|
2011-10-18 03:48:19 +08:00
|
|
|
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;
|
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<MemBufferOwner> BufOwner(new MemBufferOwner());
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2012-03-28 05:38:03 +08:00
|
|
|
if (index_options & CXIndexOpt_SuppressWarnings)
|
|
|
|
CInvok->getDiagnosticOpts().IgnoreWarnings = true;
|
|
|
|
|
2011-11-28 12:55:55 +08:00
|
|
|
ASTUnit *Unit = ASTUnit::create(CInvok.getPtr(), Diags,
|
2013-03-30 05:51:44 +08:00
|
|
|
CaptureDiagnostics,
|
2012-07-12 04:59:04 +08:00
|
|
|
/*UserFilesAreVolatile=*/true);
|
2014-04-16 02:16:25 +08:00
|
|
|
if (!Unit) {
|
|
|
|
ITUI->result = CXError_InvalidArguments;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<CXTUOwner> CXTU(
|
|
|
|
new CXTUOwner(MakeCXTranslationUnit(CXXIdx, Unit)));
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<CXTUOwner>
|
|
|
|
CXTUCleanup(CXTU.get());
|
|
|
|
|
2012-12-07 03:41:16 +08:00
|
|
|
// Enable the skip-parsed-bodies optimization only for C++; this may be
|
|
|
|
// revisited.
|
|
|
|
bool SkipBodies = (index_options & CXIndexOpt_SkipParsedBodiesInSession) &&
|
|
|
|
CInvok->getLangOpts()->CPlusPlus;
|
|
|
|
if (SkipBodies)
|
|
|
|
CInvok->getFrontendOpts().SkipFunctionBodies = true;
|
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<IndexingFrontendAction> IndexAction;
|
2011-10-18 03:48:19 +08:00
|
|
|
IndexAction.reset(new IndexingFrontendAction(client_data, CB,
|
2012-12-07 03:41:16 +08:00
|
|
|
index_options, CXTU->getTU(),
|
|
|
|
SkipBodies ? IdxSession->SkipBodyData.get() : 0));
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<IndexingFrontendAction>
|
|
|
|
IndexActionCleanup(IndexAction.get());
|
|
|
|
|
2011-11-28 12:56:00 +08:00
|
|
|
bool Persistent = requestedToGetTU;
|
|
|
|
bool OnlyLocalDecls = false;
|
|
|
|
bool PrecompilePreamble = false;
|
|
|
|
bool CacheCodeCompletionResults = false;
|
|
|
|
PreprocessorOptions &PPOpts = CInvok->getPreprocessorOpts();
|
2012-03-07 09:51:17 +08:00
|
|
|
PPOpts.AllowPCHWithCompilerErrors = true;
|
2011-11-28 12:56:00 +08:00
|
|
|
|
|
|
|
if (requestedToGetTU) {
|
|
|
|
OnlyLocalDecls = CXXIdx->getOnlyLocalDecls();
|
|
|
|
PrecompilePreamble = TU_options & CXTranslationUnit_PrecompiledPreamble;
|
|
|
|
// FIXME: Add a flag for modules.
|
|
|
|
CacheCodeCompletionResults
|
|
|
|
= TU_options & CXTranslationUnit_CacheCompletionResults;
|
|
|
|
}
|
|
|
|
|
2012-10-06 09:18:35 +08:00
|
|
|
if (TU_options & CXTranslationUnit_DetailedPreprocessingRecord) {
|
|
|
|
PPOpts.DetailedRecord = true;
|
|
|
|
}
|
2012-10-03 00:10:41 +08:00
|
|
|
|
2012-10-10 10:12:50 +08:00
|
|
|
if (!requestedToGetTU && !CInvok->getLangOpts()->Modules)
|
|
|
|
PPOpts.DetailedRecord = false;
|
|
|
|
|
2012-04-11 10:11:16 +08:00
|
|
|
DiagnosticErrorTrap DiagTrap(*Diags);
|
|
|
|
bool Success = ASTUnit::LoadFromCompilerInvocationAction(CInvok.getPtr(), Diags,
|
2011-10-18 03:48:19 +08:00
|
|
|
IndexAction.get(),
|
2011-11-28 12:56:00 +08:00
|
|
|
Unit,
|
|
|
|
Persistent,
|
2012-06-29 05:03:08 +08:00
|
|
|
CXXIdx->getClangResourcesPath(),
|
2011-11-28 12:56:00 +08:00
|
|
|
OnlyLocalDecls,
|
2013-03-30 05:51:44 +08:00
|
|
|
CaptureDiagnostics,
|
2011-11-28 12:56:00 +08:00
|
|
|
PrecompilePreamble,
|
2012-07-12 04:59:04 +08:00
|
|
|
CacheCodeCompletionResults,
|
|
|
|
/*IncludeBriefCommentsInCodeCompletion=*/false,
|
|
|
|
/*UserFilesAreVolatile=*/true);
|
2012-04-11 10:11:16 +08:00
|
|
|
if (DiagTrap.hasErrorOccurred() && CXXIdx->getDisplayDiagnostics())
|
|
|
|
printDiagsToStderr(Unit);
|
|
|
|
|
2014-02-13 03:12:37 +08:00
|
|
|
if (isASTReadError(Unit)) {
|
|
|
|
ITUI->result = CXError_ASTReadError;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-11 10:11:16 +08:00
|
|
|
if (!Success)
|
2011-10-18 03:48:19 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (out_TU)
|
|
|
|
*out_TU = CXTU->takeTU();
|
|
|
|
|
2014-02-13 03:12:37 +08:00
|
|
|
ITUI->result = CXError_Success;
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
// FIXME: Only deserialize inclusion directives.
|
|
|
|
|
|
|
|
PreprocessingRecord::iterator I, E;
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(I, E) = Unit.getLocalPreprocessingEntities();
|
2011-11-15 14:20:16 +08:00
|
|
|
|
2012-10-10 10:12:47 +08:00
|
|
|
bool isModuleFile = Unit.isModuleFile();
|
2011-11-15 14:20:16 +08:00
|
|
|
for (; I != E; ++I) {
|
|
|
|
PreprocessedEntity *PPE = *I;
|
|
|
|
|
|
|
|
if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) {
|
2012-10-18 08:17:05 +08:00
|
|
|
SourceLocation Loc = ID->getSourceRange().getBegin();
|
|
|
|
// Modules have synthetic main files as input, give an invalid location
|
|
|
|
// if the location points to such a file.
|
|
|
|
if (isModuleFile && Unit.isInMainFileID(Loc))
|
|
|
|
Loc = SourceLocation();
|
|
|
|
IdxCtx.ppIncludedFile(Loc, ID->getFileName(),
|
|
|
|
ID->getFile(),
|
|
|
|
ID->getKind() == InclusionDirective::Import,
|
|
|
|
!ID->wasInQuotes(), ID->importedModule());
|
2011-11-15 14:20:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-03 09:58:28 +08:00
|
|
|
static bool topLevelDeclVisitor(void *context, const Decl *D) {
|
2012-10-03 05:09:13 +08:00
|
|
|
IndexingContext &IdxCtx = *static_cast<IndexingContext*>(context);
|
|
|
|
IdxCtx.indexTopLevelDecl(D);
|
|
|
|
if (IdxCtx.shouldAbort())
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2011-11-15 14:20:16 +08:00
|
|
|
|
2012-10-03 05:09:13 +08:00
|
|
|
static void indexTranslationUnit(ASTUnit &Unit, IndexingContext &IdxCtx) {
|
2012-10-03 09:58:28 +08:00
|
|
|
Unit.visitLocalTopLevelDecls(&IdxCtx, topLevelDeclVisitor);
|
2011-11-15 14:20:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void indexDiagnostics(CXTranslationUnit TU, IndexingContext &IdxCtx) {
|
2011-12-01 10:42:50 +08:00
|
|
|
if (!IdxCtx.hasDiagnosticCallback())
|
|
|
|
return;
|
|
|
|
|
|
|
|
CXDiagnosticSetImpl *DiagSet = cxdiag::lazyCreateDiags(TU);
|
|
|
|
IdxCtx.handleDiagnosticSet(DiagSet);
|
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;
|
|
|
|
|
2014-02-13 03:12:37 +08:00
|
|
|
// Set up the initial return value.
|
|
|
|
ITUI->result = CXError_Failure;
|
|
|
|
|
|
|
|
// Check arguments.
|
2014-02-11 23:02:48 +08:00
|
|
|
if (isNotUsableTU(TU)) {
|
2014-02-11 22:34:14 +08:00
|
|
|
LOG_BAD_TU(TU);
|
2014-02-13 03:12:37 +08:00
|
|
|
ITUI->result = CXError_InvalidArguments;
|
2011-11-15 14:20:16 +08:00
|
|
|
return;
|
2014-02-11 22:34:14 +08:00
|
|
|
}
|
2014-02-13 03:12:37 +08:00
|
|
|
if (!client_index_callbacks || index_callbacks_size == 0) {
|
|
|
|
ITUI->result = CXError_InvalidArguments;
|
2011-11-15 14:20:16 +08:00
|
|
|
return;
|
2014-02-13 03:12:37 +08:00
|
|
|
}
|
2011-11-15 14:20:16 +08:00
|
|
|
|
2013-01-27 05:49:50 +08:00
|
|
|
CIndexer *CXXIdx = TU->CIdx;
|
2012-03-28 10:18:05 +08:00
|
|
|
if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
|
2012-03-28 10:49:54 +08:00
|
|
|
setThreadBackgroundPriority();
|
2012-03-28 10:18:05 +08:00
|
|
|
|
2011-11-15 14:20:16 +08:00
|
|
|
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);
|
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<IndexingContext> IndexCtx;
|
2011-11-15 14:20:16 +08:00
|
|
|
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());
|
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<IndexingConsumer> IndexConsumer;
|
2012-12-07 03:41:16 +08:00
|
|
|
IndexConsumer.reset(new IndexingConsumer(*IndexCtx, 0));
|
2011-11-15 14:20:16 +08:00
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<IndexingConsumer>
|
|
|
|
IndexConsumerCleanup(IndexConsumer.get());
|
|
|
|
|
2013-01-27 02:53:38 +08:00
|
|
|
ASTUnit *Unit = cxtu::getASTUnit(TU);
|
2011-11-15 14:20:16 +08:00
|
|
|
if (!Unit)
|
|
|
|
return;
|
|
|
|
|
2012-09-26 03:29:50 +08:00
|
|
|
ASTUnit::ConcurrencyCheck Check(*Unit);
|
|
|
|
|
2012-10-04 05:05:51 +08:00
|
|
|
if (const FileEntry *PCHFile = Unit->getPCHFile())
|
|
|
|
IndexCtx->importedPCH(PCHFile);
|
|
|
|
|
2011-11-15 14:20:16 +08:00
|
|
|
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);
|
|
|
|
|
2014-02-13 03:12:37 +08:00
|
|
|
ITUI->result = CXError_Success;
|
2011-11-15 14:20:16 +08:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2011-12-14 02:47:45 +08:00
|
|
|
if (const ObjCCategoryDeclInfo *CatInfo = dyn_cast<ObjCCategoryDeclInfo>(DI))
|
|
|
|
return CatInfo->ObjCCatDeclInfo.protocols;
|
|
|
|
|
2011-11-15 06:39:19 +08:00
|
|
|
return 0;
|
2011-11-12 10:16:30 +08:00
|
|
|
}
|
|
|
|
|
2012-02-29 01:50:33 +08:00
|
|
|
const CXIdxObjCPropertyDeclInfo *
|
|
|
|
clang_index_getObjCPropertyDeclInfo(const CXIdxDeclInfo *DInfo) {
|
|
|
|
if (!DInfo)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
|
|
|
|
if (const ObjCPropertyDeclInfo *PropInfo = dyn_cast<ObjCPropertyDeclInfo>(DI))
|
|
|
|
return &PropInfo->ObjCPropDeclInfo;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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;
|
2011-11-29 20:31:20 +08:00
|
|
|
const ContainerInfo *Container = static_cast<const ContainerInfo *>(info);
|
2011-11-22 15:24:51 +08:00
|
|
|
return Container->IndexCtx->getClientContainerForDC(Container->DC);
|
|
|
|
}
|
|
|
|
|
|
|
|
void clang_index_setClientContainer(const CXIdxContainerInfo *info,
|
|
|
|
CXIdxClientContainer client) {
|
|
|
|
if (!info)
|
|
|
|
return;
|
2011-11-29 20:31:20 +08:00
|
|
|
const ContainerInfo *Container = static_cast<const ContainerInfo *>(info);
|
2011-11-22 15:24:51 +08:00
|
|
|
Container->IndexCtx->addContainerInMap(Container->DC, client);
|
|
|
|
}
|
|
|
|
|
|
|
|
CXIdxClientEntity clang_index_getClientEntity(const CXIdxEntityInfo *info) {
|
|
|
|
if (!info)
|
|
|
|
return 0;
|
2011-11-29 20:31:20 +08:00
|
|
|
const EntityInfo *Entity = static_cast<const EntityInfo *>(info);
|
2011-11-22 15:24:51 +08:00
|
|
|
return Entity->IndexCtx->getClientEntity(Entity->Dcl);
|
|
|
|
}
|
|
|
|
|
|
|
|
void clang_index_setClientEntity(const CXIdxEntityInfo *info,
|
|
|
|
CXIdxClientEntity client) {
|
|
|
|
if (!info)
|
|
|
|
return;
|
2011-11-29 20:31:20 +08:00
|
|
|
const EntityInfo *Entity = static_cast<const EntityInfo *>(info);
|
2011-11-22 15:24:51 +08:00
|
|
|
Entity->IndexCtx->setClientEntity(Entity->Dcl, client);
|
|
|
|
}
|
|
|
|
|
|
|
|
CXIndexAction clang_IndexAction_create(CXIndex CIdx) {
|
2012-12-07 03:41:16 +08:00
|
|
|
return new IndexSessionData(CIdx);
|
2011-11-22 15:24:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void clang_IndexAction_dispose(CXIndexAction idxAction) {
|
2012-12-07 03:41:16 +08:00
|
|
|
if (idxAction)
|
|
|
|
delete static_cast<IndexSessionData *>(idxAction);
|
2011-11-22 15:24:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2013-01-11 02:54:52 +08:00
|
|
|
LOG_FUNC_SECTION {
|
|
|
|
*Log << source_filename << ": ";
|
|
|
|
for (int i = 0; i != num_command_line_args; ++i)
|
|
|
|
*Log << command_line_args[i] << " ";
|
|
|
|
}
|
2011-11-22 15:24:51 +08:00
|
|
|
|
|
|
|
IndexSourceFileInfo ITUI = { idxAction, client_data, index_callbacks,
|
|
|
|
index_callbacks_size, index_options,
|
|
|
|
source_filename, command_line_args,
|
|
|
|
num_command_line_args, unsaved_files,
|
2014-02-13 03:12:37 +08:00
|
|
|
num_unsaved_files, out_TU, TU_options,
|
|
|
|
CXError_Failure };
|
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) {
|
2013-01-11 02:54:52 +08:00
|
|
|
LOG_FUNC_SECTION {
|
|
|
|
*Log << 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"
|
|
|
|
|