1515#include " indexer/frontend/index_action.h"
1616
1717#include < memory>
18+ #include < optional>
1819#include < string>
1920#include < utility>
2021#include < vector>
2930#include " absl/strings/match.h"
3031#include " absl/strings/string_view.h"
3132#include " clang/AST/ASTConsumer.h"
33+ #include " clang/Basic/FileEntry.h"
34+ #include " clang/Basic/SourceLocation.h"
3235#include " clang/Frontend/CompilerInstance.h"
3336#include " clang/Lex/Pragma.h"
3437#include " clang/Lex/Preprocessor.h"
@@ -41,23 +44,43 @@ namespace oss_fuzz {
4144namespace indexer {
4245class AstConsumer : public clang ::ASTConsumer {
4346 public:
44- explicit AstConsumer (InMemoryIndex& index, clang::CompilerInstance& compiler)
45- : index_(index), compiler_(compiler) {}
47+ AstConsumer (InMemoryIndex& index, clang::CompilerInstance& compiler,
48+ bool support_incremental_indexing = false )
49+ : index_(index),
50+ compiler_ (compiler),
51+ support_incremental_indexing_(support_incremental_indexing) {}
4652 ~AstConsumer () override = default ;
4753
4854 void HandleTranslationUnit (clang::ASTContext& context) override {
55+ if (support_incremental_indexing_) {
56+ const clang::SourceManager& source_manager = context.getSourceManager ();
57+ const clang::FileID main_file_id = source_manager.getMainFileID ();
58+ const clang::OptionalFileEntryRef main_file =
59+ source_manager.getFileEntryRefForID (main_file_id);
60+ CHECK (main_file.has_value ()) << " Couldn't retrieve the main file entry" ;
61+
62+ const clang::FileManager& file_manager = source_manager.getFileManager ();
63+ llvm::SmallString<256 > absolute_path (main_file->getName ());
64+ file_manager.makeAbsolutePath (absolute_path);
65+
66+ index_.SetTranslationUnit ({absolute_path.data (), absolute_path.size ()});
67+ }
68+
4969 AstVisitor visitor (index_, context, compiler_);
5070 visitor.TraverseDecl (context.getTranslationUnitDecl ());
5171 }
5272
5373 private:
5474 InMemoryIndex& index_;
5575 clang::CompilerInstance& compiler_;
76+ const bool support_incremental_indexing_;
5677};
5778
58- IndexAction::IndexAction (FileCopier& file_copier, MergeQueue& merge_queue)
79+ IndexAction::IndexAction (FileCopier& file_copier, MergeQueue& merge_queue,
80+ bool support_incremental_indexing)
5981 : index_(std::make_unique<InMemoryIndex>(file_copier)),
60- merge_queue_ (merge_queue) {}
82+ merge_queue_(merge_queue),
83+ support_incremental_indexing_(support_incremental_indexing) {}
6184
6285bool IndexAction::BeginSourceFileAction (clang::CompilerInstance& compiler) {
6386 CHECK (index_);
@@ -79,15 +102,20 @@ void IndexAction::EndSourceFileAction() { merge_queue_.Add(std::move(index_)); }
79102
80103std::unique_ptr<clang::ASTConsumer> IndexAction::CreateASTConsumer (
81104 clang::CompilerInstance& compiler, llvm::StringRef path) {
82- return std::make_unique<AstConsumer>(*index_, compiler);
105+ return std::make_unique<AstConsumer>(*index_, compiler,
106+ support_incremental_indexing_);
83107}
84108
85109IndexActionFactory::IndexActionFactory (FileCopier& file_copier,
86- MergeQueue& merge_queue)
87- : file_copier_(file_copier), merge_queue_(merge_queue) {}
110+ MergeQueue& merge_queue,
111+ bool support_incremental_indexing)
112+ : file_copier_(file_copier),
113+ merge_queue_(merge_queue),
114+ support_incremental_indexing_(support_incremental_indexing) {}
88115
89116std::unique_ptr<clang::FrontendAction> IndexActionFactory::create () {
90- return std::make_unique<IndexAction>(file_copier_, merge_queue_);
117+ return std::make_unique<IndexAction>(file_copier_, merge_queue_,
118+ support_incremental_indexing_);
91119}
92120} // namespace indexer
93121} // namespace oss_fuzz
0 commit comments