2019-03-01 14:49:51 +08:00
|
|
|
//===- Indexing.cpp - Higher level API functions --------------------------===//
|
2011-10-18 03:48:19 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-10-18 03:48:19 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
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"
|
2016-05-27 22:27:13 +08:00
|
|
|
#include "CXIndexDataConsumer.h"
|
2011-10-18 03:48:19 +08:00
|
|
|
#include "CXSourceLocation.h"
|
|
|
|
#include "CXString.h"
|
2012-12-04 17:25:21 +08:00
|
|
|
#include "CXTranslationUnit.h"
|
|
|
|
#include "clang/AST/ASTConsumer.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"
|
2016-02-13 07:10:59 +08:00
|
|
|
#include "clang/Index/IndexingAction.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"
|
2016-07-19 03:02:11 +08:00
|
|
|
#include "clang/Lex/PreprocessorOptions.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"
|
2013-08-20 00:14:33 +08:00
|
|
|
#include <cstdio>
|
2019-08-07 18:57:25 +08:00
|
|
|
#include <mutex>
|
2016-05-27 22:27:13 +08:00
|
|
|
#include <utility>
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2016-02-13 07:10:59 +08:00
|
|
|
using namespace clang::index;
|
2011-10-18 03:48:19 +08:00
|
|
|
using namespace cxtu;
|
|
|
|
using namespace cxindex;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2012-12-07 03:41:16 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Skip Parsed Bodies
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// A "region" in source code identified by the file/offset of the
|
2012-12-07 03:41:16 +08:00
|
|
|
/// 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 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;
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2012-12-07 03:41:16 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class SessionSkipBodyData {
|
|
|
|
llvm::sys::Mutex Mux;
|
|
|
|
PPRegionSetTy ParsedRegions;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SessionSkipBodyData() : Mux(/*recursive=*/false) {}
|
2015-10-20 21:23:58 +08:00
|
|
|
~SessionSkipBodyData() {
|
|
|
|
//llvm::errs() << "RegionData: " << Skipped.size() << " - " << Skipped.getMemorySize() << "\n";
|
|
|
|
}
|
2012-12-07 03:41:16 +08:00
|
|
|
|
|
|
|
void copyTo(PPRegionSetTy &Set) {
|
2019-08-07 18:57:25 +08:00
|
|
|
std::lock_guard<llvm::sys::Mutex> MG(Mux);
|
2012-12-07 03:41:16 +08:00
|
|
|
Set = ParsedRegions;
|
|
|
|
}
|
|
|
|
|
|
|
|
void update(ArrayRef<PPRegion> Regions) {
|
2019-08-07 18:57:25 +08:00
|
|
|
std::lock_guard<llvm::sys::Mutex> MG(Mux);
|
2012-12-07 03:41:16 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-10-18 03:48:19 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// IndexPPCallbacks
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
class IndexPPCallbacks : public PPCallbacks {
|
|
|
|
Preprocessor &PP;
|
2016-02-13 07:10:59 +08:00
|
|
|
CXIndexDataConsumer &DataConsumer;
|
2011-11-11 08:23:36 +08:00
|
|
|
bool IsMainFileEntered;
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
public:
|
2016-02-13 07:10:59 +08:00
|
|
|
IndexPPCallbacks(Preprocessor &PP, CXIndexDataConsumer &dataConsumer)
|
|
|
|
: PP(PP), DataConsumer(dataConsumer), IsMainFileEntered(false) { }
|
2011-11-11 08:23:36 +08:00
|
|
|
|
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;
|
2016-02-13 07:10:59 +08:00
|
|
|
DataConsumer.enteredMainFile(SM.getFileEntryForID(SM.getMainFileID()));
|
2011-11-11 08:23:36 +08:00
|
|
|
}
|
|
|
|
}
|
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,
|
2018-05-11 03:05:36 +08:00
|
|
|
const Module *Imported,
|
|
|
|
SrcMgr::CharacteristicKind FileType) override {
|
2011-10-18 03:48:19 +08:00
|
|
|
bool isImport = (IncludeTok.is(tok::identifier) &&
|
|
|
|
IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import);
|
2016-02-13 07:10:59 +08:00
|
|
|
DataConsumer.ppIncludedFile(HashLoc, FileName, File, isImport, IsAngled,
|
2012-10-18 08:17:05 +08:00
|
|
|
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,
|
2017-04-27 05:05:44 +08:00
|
|
|
const MacroDefinition &MD,
|
|
|
|
const MacroDirective *UD) override {}
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
/// MacroExpands - This is called by when a macro invocation is found.
|
2015-05-04 11:15:40 +08:00
|
|
|
void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
|
2014-03-15 15:47:46 +08:00
|
|
|
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.
|
2017-09-12 04:47:42 +08:00
|
|
|
void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override {
|
|
|
|
}
|
2011-10-18 03:48:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// IndexingConsumer
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
class IndexingConsumer : public ASTConsumer {
|
2016-02-13 07:10:59 +08:00
|
|
|
CXIndexDataConsumer &DataConsumer;
|
2012-12-07 03:41:16 +08:00
|
|
|
TUSkipBodyControl *SKCtrl;
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
public:
|
2016-02-13 07:10:59 +08:00
|
|
|
IndexingConsumer(CXIndexDataConsumer &dataConsumer, TUSkipBodyControl *skCtrl)
|
|
|
|
: DataConsumer(dataConsumer), 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 {
|
2016-02-13 07:10:59 +08:00
|
|
|
DataConsumer.setASTContext(Context);
|
|
|
|
DataConsumer.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 {
|
2016-02-13 07:10:59 +08:00
|
|
|
return !DataConsumer.shouldAbort();
|
2012-02-11 04:10:44 +08:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2016-02-13 07:10:59 +08:00
|
|
|
const SourceManager &SM = DataConsumer.getASTContext().getSourceManager();
|
2012-12-07 03:41:16 +08:00
|
|
|
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 {
|
2016-02-13 07:10:59 +08:00
|
|
|
std::shared_ptr<CXIndexDataConsumer> DataConsumer;
|
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:
|
2016-02-13 07:10:59 +08:00
|
|
|
IndexingFrontendAction(std::shared_ptr<CXIndexDataConsumer> dataConsumer,
|
2012-12-07 03:41:16 +08:00
|
|
|
SessionSkipBodyData *skData)
|
2016-05-27 22:27:13 +08:00
|
|
|
: DataConsumer(std::move(dataConsumer)), SKData(skData) {}
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2014-08-11 03:56:51 +08:00
|
|
|
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
|
|
|
|
StringRef InFile) override {
|
2012-10-04 05:05:51 +08:00
|
|
|
PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
|
|
|
|
|
|
|
|
if (!PPOpts.ImplicitPCHInclude.empty()) {
|
2019-08-02 05:31:56 +08:00
|
|
|
auto File = CI.getFileManager().getFile(PPOpts.ImplicitPCHInclude);
|
|
|
|
if (File)
|
|
|
|
DataConsumer->importedPCH(*File);
|
2012-10-04 05:05:51 +08:00
|
|
|
}
|
2012-10-03 00:10:41 +08:00
|
|
|
|
2016-02-13 07:10:59 +08:00
|
|
|
DataConsumer->setASTContext(CI.getASTContext());
|
2011-10-18 03:48:19 +08:00
|
|
|
Preprocessor &PP = CI.getPreprocessor();
|
2016-02-13 07:10:59 +08:00
|
|
|
PP.addPPCallbacks(llvm::make_unique<IndexPPCallbacks>(PP, *DataConsumer));
|
2017-01-06 03:48:07 +08:00
|
|
|
DataConsumer->setPreprocessor(CI.getPreprocessorPtr());
|
2012-12-07 03:41:16 +08:00
|
|
|
|
|
|
|
if (SKData) {
|
2014-08-11 03:56:51 +08:00
|
|
|
auto *PPRec = new PPConditionalDirectiveRecord(PP.getSourceManager());
|
2014-09-10 12:53:53 +08:00
|
|
|
PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(PPRec));
|
2014-08-11 03:56:51 +08:00
|
|
|
SKCtrl = llvm::make_unique<TUSkipBodyControl>(*SKData, *PPRec, PP);
|
2012-12-07 03:41:16 +08:00
|
|
|
}
|
|
|
|
|
2016-02-13 07:10:59 +08:00
|
|
|
return llvm::make_unique<IndexingConsumer>(*DataConsumer, SKCtrl.get());
|
2011-12-01 10:42:50 +08:00
|
|
|
}
|
|
|
|
|
2014-03-15 15:47:46 +08:00
|
|
|
TranslationUnitKind getTranslationUnitKind() override {
|
2016-02-13 07:10:59 +08:00
|
|
|
if (DataConsumer->shouldIndexImplicitTemplateInsts())
|
2012-02-15 06:23:11 +08:00
|
|
|
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
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-02-13 07:10:59 +08:00
|
|
|
static IndexingOptions getIndexingOptionsFromCXOptions(unsigned index_options) {
|
|
|
|
IndexingOptions IdxOpts;
|
|
|
|
if (index_options & CXIndexOpt_IndexFunctionLocalSymbols)
|
|
|
|
IdxOpts.IndexFunctionLocals = true;
|
2018-07-20 16:08:56 +08:00
|
|
|
if (index_options & CXIndexOpt_IndexImplicitTemplateInstantiations)
|
|
|
|
IdxOpts.IndexImplicitInstantiation = true;
|
2016-02-13 07:10:59 +08:00
|
|
|
return IdxOpts;
|
|
|
|
}
|
|
|
|
|
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-10-18 03:48:19 +08:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2015-07-26 04:55:44 +08:00
|
|
|
static CXErrorCode clang_indexSourceFile_Impl(
|
|
|
|
CXIndexAction cxIdxAction, CXClientData client_data,
|
|
|
|
IndexerCallbacks *client_index_callbacks, unsigned index_callbacks_size,
|
|
|
|
unsigned index_options, const char *source_filename,
|
|
|
|
const char *const *command_line_args, int num_command_line_args,
|
|
|
|
ArrayRef<CXUnsavedFile> unsaved_files, CXTranslationUnit *out_TU,
|
|
|
|
unsigned TU_options) {
|
2011-10-18 03:48:19 +08:00
|
|
|
if (out_TU)
|
2014-06-08 16:38:04 +08:00
|
|
|
*out_TU = nullptr;
|
|
|
|
bool requestedToGetTU = (out_TU != nullptr);
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2014-02-13 03:12:37 +08:00
|
|
|
if (!cxIdxAction) {
|
2015-07-26 04:55:44 +08:00
|
|
|
return CXError_InvalidArguments;
|
2014-02-13 03:12:37 +08:00
|
|
|
}
|
|
|
|
if (!client_index_callbacks || index_callbacks_size == 0) {
|
2015-07-26 04:55:44 +08:00
|
|
|
return CXError_InvalidArguments;
|
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
|
|
|
|
2019-06-11 22:14:24 +08:00
|
|
|
CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::All;
|
|
|
|
if (TU_options & CXTranslationUnit_IgnoreNonErrorsFromIncludedFiles)
|
|
|
|
CaptureDiagnostics = CaptureDiagsKind::AllWithoutNonErrorsFromIncludes;
|
|
|
|
if (Logger::isLoggingEnabled())
|
|
|
|
CaptureDiagnostics = CaptureDiagsKind::None;
|
2013-03-30 05:51:44 +08:00
|
|
|
|
2014-06-08 16:38:04 +08:00
|
|
|
CaptureDiagnosticConsumer *CaptureDiag = nullptr;
|
2019-06-11 22:14:24 +08:00
|
|
|
if (CaptureDiagnostics != CaptureDiagsKind::None)
|
2013-03-30 05:51:44 +08:00
|
|
|
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> >
|
2014-07-05 11:08:06 +08:00
|
|
|
DiagCleanup(Diags.get());
|
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);
|
2017-01-07 03:49:01 +08:00
|
|
|
|
|
|
|
std::shared_ptr<CompilerInvocation> CInvok =
|
|
|
|
createInvocationFromCommandLine(*Args, Diags);
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
if (!CInvok)
|
2015-07-26 04:55:44 +08:00
|
|
|
return CXError_Failure;
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this function.
|
2017-01-07 03:49:01 +08:00
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<
|
|
|
|
std::shared_ptr<CompilerInvocation>,
|
|
|
|
llvm::CrashRecoveryContextDestructorCleanup<
|
|
|
|
std::shared_ptr<CompilerInvocation>>>
|
|
|
|
CInvokCleanup(&CInvok);
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
if (CInvok->getFrontendOpts().Inputs.empty())
|
2015-07-26 04:55:44 +08:00
|
|
|
return CXError_Failure;
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2014-07-07 09:23:14 +08:00
|
|
|
typedef SmallVector<std::unique_ptr<llvm::MemoryBuffer>, 8> MemBufferOwner;
|
|
|
|
std::unique_ptr<MemBufferOwner> BufOwner(new MemBufferOwner);
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
2014-07-07 09:23:14 +08:00
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<MemBufferOwner> BufOwnerCleanup(
|
|
|
|
BufOwner.get());
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2015-07-26 04:55:44 +08:00
|
|
|
for (auto &UF : unsaved_files) {
|
2014-08-28 04:03:29 +08:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer> MB =
|
2014-07-07 09:23:14 +08:00
|
|
|
llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename);
|
2014-08-28 04:03:29 +08:00
|
|
|
CInvok->getPreprocessorOpts().addRemappedFile(UF.Filename, MB.get());
|
|
|
|
BufOwner->push_back(std::move(MB));
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2015-11-20 11:36:21 +08:00
|
|
|
// Make sure to use the raw module format.
|
|
|
|
CInvok->getHeaderSearchOpts().ModuleFormat =
|
|
|
|
CXXIdx->getPCHContainerOperations()->getRawReader().getFormat();
|
|
|
|
|
2017-01-07 03:49:01 +08:00
|
|
|
auto Unit = ASTUnit::create(CInvok, Diags, CaptureDiagnostics,
|
|
|
|
/*UserFilesAreVolatile=*/true);
|
2015-07-26 04:55:44 +08:00
|
|
|
if (!Unit)
|
|
|
|
return CXError_InvalidArguments;
|
2014-04-16 02:16:25 +08:00
|
|
|
|
2017-01-07 03:49:01 +08:00
|
|
|
auto *UPtr = Unit.get();
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<CXTUOwner> CXTU(
|
2017-01-07 03:49:01 +08:00
|
|
|
new CXTUOwner(MakeCXTranslationUnit(CXXIdx, std::move(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;
|
|
|
|
|
2016-02-13 07:10:59 +08:00
|
|
|
auto DataConsumer =
|
|
|
|
std::make_shared<CXIndexDataConsumer>(client_data, CB, index_options,
|
|
|
|
CXTU->getTU());
|
|
|
|
auto InterAction = llvm::make_unique<IndexingFrontendAction>(DataConsumer,
|
|
|
|
SkipBodies ? IdxSession->SkipBodyData.get() : nullptr);
|
|
|
|
std::unique_ptr<FrontendAction> IndexAction;
|
2016-02-14 14:39:03 +08:00
|
|
|
IndexAction = createIndexingAction(DataConsumer,
|
|
|
|
getIndexingOptionsFromCXOptions(index_options),
|
|
|
|
std::move(InterAction));
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
2016-02-13 07:10:59 +08:00
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<FrontendAction>
|
2011-10-18 03:48:19 +08:00
|
|
|
IndexActionCleanup(IndexAction.get());
|
|
|
|
|
2011-11-28 12:56:00 +08:00
|
|
|
bool Persistent = requestedToGetTU;
|
|
|
|
bool OnlyLocalDecls = false;
|
|
|
|
bool PrecompilePreamble = false;
|
2015-12-15 17:30:31 +08:00
|
|
|
bool CreatePreambleOnFirstParse = false;
|
2011-11-28 12:56:00 +08:00
|
|
|
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;
|
2015-12-15 17:30:31 +08:00
|
|
|
CreatePreambleOnFirstParse =
|
|
|
|
TU_options & CXTranslationUnit_CreatePreambleOnFirstParse;
|
2011-11-28 12:56:00 +08:00
|
|
|
// 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;
|
|
|
|
|
2015-12-15 17:30:31 +08:00
|
|
|
// Unless the user specified that they want the preamble on the first parse
|
|
|
|
// set it up to be created on the first reparse. This makes the first parse
|
|
|
|
// faster, trading for a slower (first) reparse.
|
|
|
|
unsigned PrecompilePreambleAfterNParses =
|
|
|
|
!PrecompilePreamble ? 0 : 2 - CreatePreambleOnFirstParse;
|
2012-04-11 10:11:16 +08:00
|
|
|
DiagnosticErrorTrap DiagTrap(*Diags);
|
2015-06-21 02:53:08 +08:00
|
|
|
bool Success = ASTUnit::LoadFromCompilerInvocationAction(
|
2017-01-07 03:49:01 +08:00
|
|
|
std::move(CInvok), CXXIdx->getPCHContainerOperations(), Diags,
|
|
|
|
IndexAction.get(), UPtr, Persistent, CXXIdx->getClangResourcesPath(),
|
2015-12-15 17:30:31 +08:00
|
|
|
OnlyLocalDecls, CaptureDiagnostics, PrecompilePreambleAfterNParses,
|
2015-06-21 02:53:08 +08:00
|
|
|
CacheCodeCompletionResults,
|
|
|
|
/*IncludeBriefCommentsInCodeCompletion=*/false,
|
|
|
|
/*UserFilesAreVolatile=*/true);
|
2012-04-11 10:11:16 +08:00
|
|
|
if (DiagTrap.hasErrorOccurred() && CXXIdx->getDisplayDiagnostics())
|
2017-01-07 03:49:01 +08:00
|
|
|
printDiagsToStderr(UPtr);
|
2012-04-11 10:11:16 +08:00
|
|
|
|
2017-01-07 03:49:01 +08:00
|
|
|
if (isASTReadError(UPtr))
|
2015-07-26 04:55:44 +08:00
|
|
|
return CXError_ASTReadError;
|
2014-02-13 03:12:37 +08:00
|
|
|
|
2012-04-11 10:11:16 +08:00
|
|
|
if (!Success)
|
2015-07-26 04:55:44 +08:00
|
|
|
return CXError_Failure;
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
if (out_TU)
|
|
|
|
*out_TU = CXTU->takeTU();
|
|
|
|
|
2015-07-26 04:55:44 +08:00
|
|
|
return CXError_Success;
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
2011-11-15 14:20:16 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// clang_indexTranslationUnit Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-02-13 07:10:59 +08:00
|
|
|
static void indexPreprocessingRecord(ASTUnit &Unit, CXIndexDataConsumer &IdxCtx) {
|
2011-11-15 14:20:16 +08:00
|
|
|
Preprocessor &PP = Unit.getPreprocessor();
|
|
|
|
if (!PP.getPreprocessingRecord())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// FIXME: Only deserialize inclusion directives.
|
|
|
|
|
2012-10-10 10:12:47 +08:00
|
|
|
bool isModuleFile = Unit.isModuleFile();
|
2015-02-07 01:25:10 +08:00
|
|
|
for (PreprocessedEntity *PPE : Unit.getLocalPreprocessingEntities()) {
|
2011-11-15 14:20:16 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-26 04:55:44 +08:00
|
|
|
static CXErrorCode clang_indexTranslationUnit_Impl(
|
|
|
|
CXIndexAction idxAction, CXClientData client_data,
|
|
|
|
IndexerCallbacks *client_index_callbacks, unsigned index_callbacks_size,
|
|
|
|
unsigned index_options, CXTranslationUnit TU) {
|
2014-02-13 03:12:37 +08:00
|
|
|
// 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);
|
2015-07-26 04:55:44 +08:00
|
|
|
return CXError_InvalidArguments;
|
2014-02-11 22:34:14 +08:00
|
|
|
}
|
2014-02-13 03:12:37 +08:00
|
|
|
if (!client_index_callbacks || index_callbacks_size == 0) {
|
2015-07-26 04:55:44 +08:00
|
|
|
return CXError_InvalidArguments;
|
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);
|
|
|
|
|
2018-04-23 22:30:21 +08:00
|
|
|
CXIndexDataConsumer DataConsumer(client_data, CB, index_options, TU);
|
2011-11-15 14:20:16 +08:00
|
|
|
|
2013-01-27 02:53:38 +08:00
|
|
|
ASTUnit *Unit = cxtu::getASTUnit(TU);
|
2011-11-15 14:20:16 +08:00
|
|
|
if (!Unit)
|
2015-07-26 04:55:44 +08:00
|
|
|
return CXError_Failure;
|
2011-11-15 14:20:16 +08:00
|
|
|
|
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())
|
2018-04-23 22:30:21 +08:00
|
|
|
DataConsumer.importedPCH(PCHFile);
|
2012-10-04 05:05:51 +08:00
|
|
|
|
2011-11-15 14:20:16 +08:00
|
|
|
FileManager &FileMgr = Unit->getFileManager();
|
|
|
|
|
|
|
|
if (Unit->getOriginalSourceFileName().empty())
|
2018-04-23 22:30:21 +08:00
|
|
|
DataConsumer.enteredMainFile(nullptr);
|
2019-08-02 05:31:56 +08:00
|
|
|
else if (auto MainFile = FileMgr.getFile(Unit->getOriginalSourceFileName()))
|
|
|
|
DataConsumer.enteredMainFile(*MainFile);
|
2011-11-15 14:20:16 +08:00
|
|
|
else
|
2019-08-02 05:31:56 +08:00
|
|
|
DataConsumer.enteredMainFile(nullptr);
|
2011-11-15 14:20:16 +08:00
|
|
|
|
2018-04-23 22:30:21 +08:00
|
|
|
DataConsumer.setASTContext(Unit->getASTContext());
|
|
|
|
DataConsumer.startedTranslationUnit();
|
2011-11-15 14:20:16 +08:00
|
|
|
|
2018-04-23 22:30:21 +08:00
|
|
|
indexPreprocessingRecord(*Unit, DataConsumer);
|
2016-02-13 07:10:59 +08:00
|
|
|
indexASTUnit(*Unit, DataConsumer, getIndexingOptionsFromCXOptions(index_options));
|
2018-04-23 22:30:21 +08:00
|
|
|
DataConsumer.indexDiagnostics();
|
2011-11-15 14:20:16 +08:00
|
|
|
|
2015-07-26 04:55:44 +08:00
|
|
|
return CXError_Success;
|
2011-11-15 14:20:16 +08:00
|
|
|
}
|
|
|
|
|
2011-10-18 03:48:19 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// libclang public APIs.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
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)
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2011-11-12 10:16:30 +08:00
|
|
|
|
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
|
|
|
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2011-11-11 08:23:36 +08:00
|
|
|
}
|
|
|
|
|
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)
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
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
|
|
|
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2011-11-11 08:23:36 +08:00
|
|
|
}
|
|
|
|
|
2011-11-15 06:39:19 +08:00
|
|
|
const CXIdxObjCCategoryDeclInfo *
|
|
|
|
clang_index_getObjCCategoryDeclInfo(const CXIdxDeclInfo *DInfo){
|
|
|
|
if (!DInfo)
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
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
|
|
|
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2011-11-11 08:23:36 +08:00
|
|
|
}
|
|
|
|
|
2011-11-15 06:39:19 +08:00
|
|
|
const CXIdxObjCProtocolRefListInfo *
|
|
|
|
clang_index_getObjCProtocolRefListInfo(const CXIdxDeclInfo *DInfo) {
|
|
|
|
if (!DInfo)
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2011-11-12 10:16:30 +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.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;
|
|
|
|
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
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)
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2012-02-29 01:50:33 +08:00
|
|
|
|
|
|
|
const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
|
|
|
|
if (const ObjCPropertyDeclInfo *PropInfo = dyn_cast<ObjCPropertyDeclInfo>(DI))
|
|
|
|
return &PropInfo->ObjCPropDeclInfo;
|
|
|
|
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2012-02-29 01:50:33 +08:00
|
|
|
}
|
|
|
|
|
2011-11-18 08:26:51 +08:00
|
|
|
const CXIdxIBOutletCollectionAttrInfo *
|
|
|
|
clang_index_getIBOutletCollectionAttrInfo(const CXIdxAttrInfo *AInfo) {
|
|
|
|
if (!AInfo)
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2011-11-18 08:26:51 +08:00
|
|
|
|
|
|
|
const AttrInfo *DI = static_cast<const AttrInfo *>(AInfo);
|
|
|
|
if (const IBOutletCollectionInfo *
|
|
|
|
IBInfo = dyn_cast<IBOutletCollectionInfo>(DI))
|
|
|
|
return &IBInfo->IBCollInfo;
|
|
|
|
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2011-11-18 08:26:51 +08:00
|
|
|
}
|
|
|
|
|
2011-11-22 15:24:51 +08:00
|
|
|
const CXIdxCXXClassDeclInfo *
|
|
|
|
clang_index_getCXXClassDeclInfo(const CXIdxDeclInfo *DInfo) {
|
|
|
|
if (!DInfo)
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2011-11-22 15:24:51 +08:00
|
|
|
|
|
|
|
const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
|
|
|
|
if (const CXXClassDeclInfo *ClassInfo = dyn_cast<CXXClassDeclInfo>(DI))
|
|
|
|
return &ClassInfo->CXXClassInfo;
|
|
|
|
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2011-11-22 15:24:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CXIdxClientContainer
|
|
|
|
clang_index_getClientContainer(const CXIdxContainerInfo *info) {
|
|
|
|
if (!info)
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
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)
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
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) {
|
2015-11-19 00:14:27 +08:00
|
|
|
SmallVector<const char *, 4> Args;
|
|
|
|
Args.push_back("clang");
|
|
|
|
Args.append(command_line_args, command_line_args + num_command_line_args);
|
|
|
|
return clang_indexSourceFileFullArgv(
|
|
|
|
idxAction, client_data, index_callbacks, index_callbacks_size,
|
|
|
|
index_options, source_filename, Args.data(), Args.size(), unsaved_files,
|
|
|
|
num_unsaved_files, out_TU, TU_options);
|
|
|
|
}
|
|
|
|
|
|
|
|
int clang_indexSourceFileFullArgv(
|
|
|
|
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
|
|
|
|
2014-07-07 09:23:14 +08:00
|
|
|
if (num_unsaved_files && !unsaved_files)
|
|
|
|
return CXError_InvalidArguments;
|
|
|
|
|
2014-07-08 06:42:03 +08:00
|
|
|
CXErrorCode result = CXError_Failure;
|
2015-07-26 04:55:44 +08:00
|
|
|
auto IndexSourceFileImpl = [=, &result]() {
|
|
|
|
result = clang_indexSourceFile_Impl(
|
|
|
|
idxAction, client_data, index_callbacks, index_callbacks_size,
|
|
|
|
index_options, source_filename, command_line_args,
|
|
|
|
num_command_line_args,
|
|
|
|
llvm::makeArrayRef(unsaved_files, num_unsaved_files), out_TU,
|
|
|
|
TU_options);
|
|
|
|
};
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
llvm::CrashRecoveryContext CRC;
|
|
|
|
|
2015-07-26 04:55:44 +08:00
|
|
|
if (!RunSafely(CRC, IndexSourceFileImpl)) {
|
2011-11-15 14:20:16 +08:00
|
|
|
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);
|
|
|
|
}
|
2014-07-08 06:42:03 +08:00
|
|
|
|
|
|
|
return result;
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-07-26 04:55:44 +08:00
|
|
|
CXErrorCode result;
|
|
|
|
auto IndexTranslationUnitImpl = [=, &result]() {
|
|
|
|
result = clang_indexTranslationUnit_Impl(
|
|
|
|
idxAction, client_data, index_callbacks, index_callbacks_size,
|
|
|
|
index_options, TU);
|
|
|
|
};
|
2011-11-15 14:20:16 +08:00
|
|
|
|
|
|
|
llvm::CrashRecoveryContext CRC;
|
|
|
|
|
2015-07-26 04:55:44 +08:00
|
|
|
if (!RunSafely(CRC, IndexTranslationUnitImpl)) {
|
2011-11-15 14:20:16 +08:00
|
|
|
fprintf(stderr, "libclang: crash detected during indexing TU\n");
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-07-26 04:55:44 +08:00
|
|
|
return result;
|
2011-11-15 14:20:16 +08:00
|
|
|
}
|
|
|
|
|
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) {
|
2014-06-08 16:38:04 +08:00
|
|
|
if (indexFile) *indexFile = nullptr;
|
|
|
|
if (file) *file = nullptr;
|
2011-10-18 03:48:19 +08:00
|
|
|
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;
|
|
|
|
|
2016-02-13 07:10:59 +08:00
|
|
|
CXIndexDataConsumer &DataConsumer =
|
|
|
|
*static_cast<CXIndexDataConsumer*>(location.ptr_data[0]);
|
|
|
|
DataConsumer.translateLoc(Loc, indexFile, file, line, column, offset);
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CXSourceLocation clang_indexLoc_getCXSourceLocation(CXIdxLoc location) {
|
|
|
|
SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
|
|
|
|
if (!location.ptr_data[0] || Loc.isInvalid())
|
|
|
|
return clang_getNullLocation();
|
|
|
|
|
2016-02-13 07:10:59 +08:00
|
|
|
CXIndexDataConsumer &DataConsumer =
|
|
|
|
*static_cast<CXIndexDataConsumer*>(location.ptr_data[0]);
|
|
|
|
return cxloc::translateSourceLocation(DataConsumer.getASTContext(), Loc);
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|