Skip to content

Commit 1c08eb2

Browse files
OSS-Fuzz Teamcopybara-github
authored andcommitted
Expose incremental indexing support in the indexer CLI
Indexer-PiperOrigin-RevId: 807878969
1 parent 89f82e1 commit 1c08eb2

File tree

14 files changed

+1534
-63
lines changed

14 files changed

+1534
-63
lines changed

infra/indexer/frontend/frontend.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,13 @@ clang::tooling::CommandLineArguments ExtraArgumentsAdjuster(
118118
// perform indexing on a compilation database.
119119
std::vector<std::pair<std::unique_ptr<clang::tooling::FrontendActionFactory>,
120120
clang::tooling::ArgumentsAdjuster>>
121-
GetIndexActions(FileCopier& file_copier, MergeQueue& merge_queue) {
121+
GetIndexActions(FileCopier& file_copier, MergeQueue& merge_queue,
122+
bool support_incremental_indexing) {
122123
std::vector<std::pair<std::unique_ptr<clang::tooling::FrontendActionFactory>,
123124
clang::tooling::ArgumentsAdjuster>>
124125
actions;
125-
auto index_action =
126-
std::make_unique<IndexActionFactory>(file_copier, merge_queue);
126+
auto index_action = std::make_unique<IndexActionFactory>(
127+
file_copier, merge_queue, support_incremental_indexing);
127128
auto adjuster = clang::tooling::combineAdjusters(RemoveClangArgumentsAdjuster,
128129
ExtraArgumentsAdjuster);
129130
actions.push_back(

infra/indexer/frontend/frontend.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ std::vector<std::string> ParseCommandLine(absl::string_view commandLine);
3939
// perform indexing on a compilation database.
4040
std::vector<std::pair<std::unique_ptr<clang::tooling::FrontendActionFactory>,
4141
clang::tooling::ArgumentsAdjuster>>
42-
GetIndexActions(FileCopier& file_copier, MergeQueue& merge_queue);
42+
GetIndexActions(FileCopier& file_copier, MergeQueue& merge_queue,
43+
bool support_incremental_indexing = false);
4344
} // namespace indexer
4445
} // namespace oss_fuzz
4546

infra/indexer/frontend/index_action.cc

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "indexer/frontend/index_action.h"
1616

1717
#include <memory>
18+
#include <optional>
1819
#include <string>
1920
#include <utility>
2021
#include <vector>
@@ -29,6 +30,8 @@
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 {
4144
namespace indexer {
4245
class 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

6285
bool IndexAction::BeginSourceFileAction(clang::CompilerInstance& compiler) {
6386
CHECK(index_);
@@ -79,15 +102,20 @@ void IndexAction::EndSourceFileAction() { merge_queue_.Add(std::move(index_)); }
79102

80103
std::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

85109
IndexActionFactory::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

89116
std::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

infra/indexer/frontend/index_action.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define OSS_FUZZ_INFRA_INDEXER_FRONTEND_INDEX_ACTION_H_
1717

1818
#include <memory>
19+
#include <string>
1920

2021
#include "indexer/index/file_copier.h"
2122
#include "indexer/index/in_memory_index.h"
@@ -32,28 +33,32 @@ namespace indexer {
3233
// indexer/frontend.h should be used instead.
3334
class IndexAction : public clang::ASTFrontendAction {
3435
public:
35-
explicit IndexAction(FileCopier& file_copier, MergeQueue& merge_queue);
36+
explicit IndexAction(FileCopier& file_copier, MergeQueue& merge_queue,
37+
bool support_incremental_indexing = false);
3638

3739
bool BeginSourceFileAction(clang::CompilerInstance& compiler) override;
3840
void EndSourceFileAction() override;
3941

4042
std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(
41-
clang::CompilerInstance& compiler, llvm::StringRef) override;
43+
clang::CompilerInstance& compiler, llvm::StringRef path) override;
4244

4345
private:
4446
std::unique_ptr<InMemoryIndex> index_;
4547
MergeQueue& merge_queue_;
48+
bool support_incremental_indexing_;
4649
};
4750

4851
class IndexActionFactory : public clang::tooling::FrontendActionFactory {
4952
public:
50-
explicit IndexActionFactory(FileCopier& file_copier, MergeQueue& merge_queue);
53+
IndexActionFactory(FileCopier& file_copier, MergeQueue& merge_queue,
54+
bool support_incremental_indexing = false);
5155

5256
std::unique_ptr<clang::FrontendAction> create() override;
5357

5458
private:
5559
FileCopier& file_copier_;
5660
MergeQueue& merge_queue_;
61+
const bool support_incremental_indexing_;
5762
};
5863
} // namespace indexer
5964
} // namespace oss_fuzz

infra/indexer/index/file_copier.cc

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,20 @@ void PreparePath(std::string& path) {
4141

4242
FileCopier::FileCopier(absl::string_view base_path,
4343
absl::string_view index_path,
44-
const std::vector<std::string>& extra_paths)
44+
const std::vector<std::string>& extra_paths,
45+
bool exist_ok)
4546
: base_path_(base_path),
4647
extra_paths_(extra_paths),
47-
index_path_(index_path) {
48+
index_path_(index_path),
49+
exist_ok_(exist_ok) {
4850
PreparePath(base_path_);
4951
for (std::string& extra_path : extra_paths_) {
5052
PreparePath(extra_path);
5153
}
5254
}
5355

5456
std::string FileCopier::AbsoluteToIndexPath(absl::string_view path) const {
55-
CHECK(path.starts_with("/")) << "Absolute path expected";
57+
CHECK(path.starts_with("/")) << "Absolute path expected: " << path;
5658

5759
std::string result = std::string(path);
5860
if (!base_path_.empty() && absl::StartsWith(path, base_path_)) {
@@ -70,12 +72,12 @@ std::string FileCopier::AbsoluteToIndexPath(absl::string_view path) const {
7072
}
7173

7274
void FileCopier::RegisterIndexedFile(absl::string_view index_path) {
73-
absl::MutexLock lock(&mutex_);
75+
absl::MutexLock lock(mutex_);
7476
indexed_files_.emplace(index_path);
7577
}
7678

7779
void FileCopier::CopyIndexedFiles() {
78-
absl::MutexLock lock(&mutex_);
80+
absl::MutexLock lock(mutex_);
7981

8082
for (const std::string& indexed_path : indexed_files_) {
8183
std::filesystem::path src_path = indexed_path;
@@ -101,9 +103,13 @@ void FileCopier::CopyIndexedFiles() {
101103
<< dst_path.parent_path()
102104
<< " (error: " << error_code.message() << ")";
103105

104-
QCHECK(std::filesystem::copy_file(src_path, dst_path, error_code))
105-
<< "Failed to copy file " << src_path << " to " << dst_path
106-
<< " (error: " << error_code.message() << ")";
106+
using std::filesystem::copy_options;
107+
const copy_options options =
108+
exist_ok_ ? copy_options::skip_existing : copy_options::none;
109+
std::filesystem::copy_file(src_path, dst_path, options, error_code);
110+
QCHECK(!error_code) << "Failed to copy file " << src_path << " to "
111+
<< dst_path << " (error: " << error_code.message()
112+
<< ")";
107113
}
108114
}
109115
} // namespace indexer

infra/indexer/index/file_copier.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ namespace indexer {
3333
class FileCopier {
3434
public:
3535
FileCopier(absl::string_view base_path, absl::string_view index_path,
36-
const std::vector<std::string>& extra_paths);
36+
const std::vector<std::string>& extra_paths,
37+
bool exist_ok = false);
3738
FileCopier(const FileCopier&) = delete;
3839

3940
// Takes an absolute path. Rewrites this path into the representation it will
@@ -51,6 +52,7 @@ class FileCopier {
5152
std::string base_path_;
5253
std::vector<std::string> extra_paths_;
5354
const std::filesystem::path index_path_;
55+
const bool exist_ok_;
5456

5557
absl::Mutex mutex_;
5658
absl::flat_hash_set<std::string> indexed_files_ ABSL_GUARDED_BY(mutex_);

0 commit comments

Comments
 (0)