This revision was automatically updated to reflect the committed changes.
Closed by commit rL370338: [Index] Added a ShouldSkipFunctionBody callback to 
libIndex, and refactored… (authored by gribozavr, committed by ).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D66879?vs=217627&id=217836#toc

Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D66879/new/

https://reviews.llvm.org/D66879

Files:
  cfe/trunk/include/clang/Index/IndexingAction.h
  cfe/trunk/lib/Index/IndexingAction.cpp
  cfe/trunk/tools/libclang/Indexing.cpp
  clang-tools-extra/trunk/clangd/index/IndexAction.cpp

Index: cfe/trunk/include/clang/Index/IndexingAction.h
===================================================================
--- cfe/trunk/include/clang/Index/IndexingAction.h
+++ cfe/trunk/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 @@
 };
 
 /// 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>
Index: cfe/trunk/lib/Index/IndexingAction.cpp
===================================================================
--- cfe/trunk/lib/Index/IndexingAction.cpp
+++ cfe/trunk/lib/Index/IndexingAction.cpp
@@ -57,14 +57,17 @@
   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 @@
   void HandleTranslationUnit(ASTContext &Ctx) override {
     DataConsumer->finish();
   }
+
+  bool shouldSkipFunctionBody(Decl *D) override {
+    return ShouldSkipFunctionBody(D);
+  }
 };
 
 class IndexAction final : public ASTFrontendAction {
@@ -108,18 +115,20 @@
 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>
Index: cfe/trunk/tools/libclang/Indexing.cpp
===================================================================
--- cfe/trunk/tools/libclang/Indexing.cpp
+++ cfe/trunk/tools/libclang/Indexing.cpp
@@ -297,54 +297,20 @@
 
 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 @@
     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 @@
       return TU_Prefix;
   }
   bool hasCodeCompletionSupport() const override { return false; }
+
+  void EndSourceFileAction() override {
+    if (ParsedLocsTracker)
+      ParsedLocsTracker->syncWithStorage();
+  }
 };
 
 //===----------------------------------------------------------------------===//
Index: clang-tools-extra/trunk/clangd/index/IndexAction.cpp
===================================================================
--- clang-tools-extra/trunk/clangd/index/IndexAction.cpp
+++ clang-tools-extra/trunk/clangd/index/IndexAction.cpp
@@ -121,30 +121,6 @@
   IncludeGraph &IG;
 };
 
-/// An ASTConsumer that instructs the parser to skip bodies of functions in the
-/// files that should not be processed.
-class SkipProcessedFunctions : public ASTConsumer {
-public:
-  SkipProcessedFunctions(std::function<bool(FileID)> FileFilter)
-      : ShouldIndexFile(std::move(FileFilter)), Context(nullptr) {
-    assert(this->ShouldIndexFile);
-  }
-
-  void Initialize(ASTContext &Context) override { this->Context = &Context; }
-  bool shouldSkipFunctionBody(Decl *D) override {
-    assert(Context && "Initialize() was never called.");
-    auto &SM = Context->getSourceManager();
-    auto FID = SM.getFileID(SM.getExpansionLoc(D->getLocation()));
-    if (!FID.isValid())
-      return false;
-    return !ShouldIndexFile(FID);
-  }
-
-private:
-  std::function<bool(FileID)> ShouldIndexFile;
-  const ASTContext *Context;
-};
-
 // Wraps the index action and reports index data after each translation unit.
 class IndexAction : public ASTFrontendAction {
 public:
@@ -169,12 +145,15 @@
       CI.getPreprocessor().addPPCallbacks(
           std::make_unique<IncludeGraphCollector>(CI.getSourceManager(), IG));
 
-    std::vector<std::unique_ptr<ASTConsumer>> Consumers;
-    Consumers.push_back(std::make_unique<SkipProcessedFunctions>(
-        [this](FileID FID) { return Collector->shouldIndexFile(FID); }));
-    Consumers.push_back(index::createIndexingASTConsumer(
-        Collector, Opts, CI.getPreprocessorPtr()));
-    return std::make_unique<MultiplexConsumer>(std::move(Consumers));
+    return index::createIndexingASTConsumer(
+        Collector, Opts, CI.getPreprocessorPtr(),
+        /*ShouldSkipFunctionBody=*/[this](const Decl *D) {
+          auto &SM = D->getASTContext().getSourceManager();
+          auto FID = SM.getFileID(SM.getExpansionLoc(D->getLocation()));
+          if (!FID.isValid())
+            return false;
+          return !Collector->shouldIndexFile(FID);
+        });
   }
 
   bool BeginInvocation(CompilerInstance &CI) override {
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to