summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitri Gribenko <gribozavr@gmail.com>2019-08-29 11:47:34 +0000
committerDmitri Gribenko <gribozavr@gmail.com>2019-08-29 11:47:34 +0000
commitf2dd7c98997c776fe1ce7ae651cb437379e405fd (patch)
tree364f79720d30ac95703f4109fc8666af7e1cc12e
parentca7487190698eb46511a983fb2f469db2bdb8836 (diff)
downloadclang-f2dd7c98997c776fe1ce7ae651cb437379e405fd.tar.gz
[Index] Added a ShouldSkipFunctionBody callback to libIndex, and refactored clients to use it instead of inventing their own solution
Subscribers: jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D66879 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@370338 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/clang/Index/IndexingAction.h17
-rw-r--r--lib/Index/IndexingAction.cpp27
-rw-r--r--tools/libclang/Indexing.cpp72
3 files changed, 66 insertions, 50 deletions
diff --git a/include/clang/Index/IndexingAction.h b/include/clang/Index/IndexingAction.h
index ce2ff0e105..f0f10fbc88 100644
--- a/include/clang/Index/IndexingAction.h
+++ b/include/clang/Index/IndexingAction.h
@@ -9,6 +9,7 @@
#ifndef LLVM_CLANG_INDEX_INDEXINGACTION_H
#define LLVM_CLANG_INDEX_INDEXINGACTION_H
+#include "clang/AST/ASTConsumer.h"
#include "clang/Basic/LLVM.h"
#include "clang/Lex/PPCallbacks.h"
#include "clang/Lex/Preprocessor.h"
@@ -51,10 +52,18 @@ struct IndexingOptions {
};
/// Creates an ASTConsumer that indexes all symbols (macros and AST decls).
-std::unique_ptr<ASTConsumer>
-createIndexingASTConsumer(std::shared_ptr<IndexDataConsumer> DataConsumer,
- const IndexingOptions &Opts,
- std::shared_ptr<Preprocessor> PP);
+std::unique_ptr<ASTConsumer> createIndexingASTConsumer(
+ std::shared_ptr<IndexDataConsumer> DataConsumer,
+ const IndexingOptions &Opts, std::shared_ptr<Preprocessor> PP,
+ std::function<bool(const Decl *)> ShouldSkipFunctionBody);
+
+inline std::unique_ptr<ASTConsumer> createIndexingASTConsumer(
+ std::shared_ptr<IndexDataConsumer> DataConsumer,
+ const IndexingOptions &Opts, std::shared_ptr<Preprocessor> PP) {
+ return createIndexingASTConsumer(
+ std::move(DataConsumer), Opts, std::move(PP),
+ /*ShouldSkipFunctionBody=*/[](const Decl *) { return false; });
+}
/// Creates a frontend action that indexes all symbols (macros and AST decls).
std::unique_ptr<FrontendAction>
diff --git a/lib/Index/IndexingAction.cpp b/lib/Index/IndexingAction.cpp
index e707972014..6d6133e89d 100644
--- a/lib/Index/IndexingAction.cpp
+++ b/lib/Index/IndexingAction.cpp
@@ -57,14 +57,17 @@ class IndexASTConsumer final : public ASTConsumer {
std::shared_ptr<IndexDataConsumer> DataConsumer;
std::shared_ptr<IndexingContext> IndexCtx;
std::shared_ptr<Preprocessor> PP;
+ std::function<bool(const Decl *)> ShouldSkipFunctionBody;
public:
IndexASTConsumer(std::shared_ptr<IndexDataConsumer> DataConsumer,
const IndexingOptions &Opts,
- std::shared_ptr<Preprocessor> PP)
+ std::shared_ptr<Preprocessor> PP,
+ std::function<bool(const Decl *)> ShouldSkipFunctionBody)
: DataConsumer(std::move(DataConsumer)),
IndexCtx(new IndexingContext(Opts, *this->DataConsumer)),
- PP(std::move(PP)) {
+ PP(std::move(PP)),
+ ShouldSkipFunctionBody(std::move(ShouldSkipFunctionBody)) {
assert(this->DataConsumer != nullptr);
assert(this->PP != nullptr);
}
@@ -92,6 +95,10 @@ protected:
void HandleTranslationUnit(ASTContext &Ctx) override {
DataConsumer->finish();
}
+
+ bool shouldSkipFunctionBody(Decl *D) override {
+ return ShouldSkipFunctionBody(D);
+ }
};
class IndexAction final : public ASTFrontendAction {
@@ -108,18 +115,20 @@ public:
protected:
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
StringRef InFile) override {
- return std::make_unique<IndexASTConsumer>(DataConsumer, Opts,
- CI.getPreprocessorPtr());
+ return std::make_unique<IndexASTConsumer>(
+ DataConsumer, Opts, CI.getPreprocessorPtr(),
+ /*ShouldSkipFunctionBody=*/[](const Decl *) { return false; });
}
};
} // anonymous namespace
-std::unique_ptr<ASTConsumer>
-index::createIndexingASTConsumer(std::shared_ptr<IndexDataConsumer> DataConsumer,
- const IndexingOptions &Opts,
- std::shared_ptr<Preprocessor> PP) {
- return std::make_unique<IndexASTConsumer>(DataConsumer, Opts, PP);
+std::unique_ptr<ASTConsumer> index::createIndexingASTConsumer(
+ std::shared_ptr<IndexDataConsumer> DataConsumer,
+ const IndexingOptions &Opts, std::shared_ptr<Preprocessor> PP,
+ std::function<bool(const Decl *)> ShouldSkipFunctionBody) {
+ return std::make_unique<IndexASTConsumer>(DataConsumer, Opts, PP,
+ ShouldSkipFunctionBody);
}
std::unique_ptr<FrontendAction>
diff --git a/tools/libclang/Indexing.cpp b/tools/libclang/Indexing.cpp
index 372fb214a5..5e567b3276 100644
--- a/tools/libclang/Indexing.cpp
+++ b/tools/libclang/Indexing.cpp
@@ -297,54 +297,20 @@ public:
class IndexingConsumer : public ASTConsumer {
CXIndexDataConsumer &DataConsumer;
- ParsedSrcLocationsTracker *ParsedLocsTracker;
public:
IndexingConsumer(CXIndexDataConsumer &dataConsumer,
ParsedSrcLocationsTracker *parsedLocsTracker)
- : DataConsumer(dataConsumer), ParsedLocsTracker(parsedLocsTracker) {}
-
- // ASTConsumer Implementation
+ : DataConsumer(dataConsumer) {}
void Initialize(ASTContext &Context) override {
DataConsumer.setASTContext(Context);
DataConsumer.startedTranslationUnit();
}
- void HandleTranslationUnit(ASTContext &Ctx) override {
- if (ParsedLocsTracker)
- ParsedLocsTracker->syncWithStorage();
- }
-
bool HandleTopLevelDecl(DeclGroupRef DG) override {
return !DataConsumer.shouldAbort();
}
-
- bool shouldSkipFunctionBody(Decl *D) override {
- if (!ParsedLocsTracker) {
- // Always skip bodies.
- return true;
- }
-
- const SourceManager &SM = DataConsumer.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;
- std::tie(FID, Offset) = SM.getDecomposedLoc(Loc);
- // 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 ParsedLocsTracker->hasAlredyBeenParsed(Loc, FID, FE);
- }
};
//===----------------------------------------------------------------------===//
@@ -404,11 +370,38 @@ public:
std::vector<std::unique_ptr<ASTConsumer>> Consumers;
Consumers.push_back(std::make_unique<IndexingConsumer>(
*DataConsumer, ParsedLocsTracker.get()));
- Consumers.push_back(
- createIndexingASTConsumer(DataConsumer, Opts, CI.getPreprocessorPtr()));
+ Consumers.push_back(createIndexingASTConsumer(
+ DataConsumer, Opts, CI.getPreprocessorPtr(),
+ [this](const Decl *D) { return this->shouldSkipFunctionBody(D); }));
return std::make_unique<MultiplexConsumer>(std::move(Consumers));
}
+ bool shouldSkipFunctionBody(const Decl *D) {
+ if (!ParsedLocsTracker) {
+ // Always skip bodies.
+ return true;
+ }
+
+ const SourceManager &SM = D->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;
+ std::tie(FID, Offset) = SM.getDecomposedLoc(Loc);
+ // 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 ParsedLocsTracker->hasAlredyBeenParsed(Loc, FID, FE);
+ }
+
TranslationUnitKind getTranslationUnitKind() override {
if (DataConsumer->shouldIndexImplicitTemplateInsts())
return TU_Complete;
@@ -416,6 +409,11 @@ public:
return TU_Prefix;
}
bool hasCodeCompletionSupport() const override { return false; }
+
+ void EndSourceFileAction() override {
+ if (ParsedLocsTracker)
+ ParsedLocsTracker->syncWithStorage();
+ }
};
//===----------------------------------------------------------------------===//