Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions infra/indexer/frontend/frontend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@ clang::tooling::CommandLineArguments ExtraArgumentsAdjuster(
// perform indexing on a compilation database.
std::vector<std::pair<std::unique_ptr<clang::tooling::FrontendActionFactory>,
clang::tooling::ArgumentsAdjuster>>
GetIndexActions(FileCopier& file_copier, MergeQueue& merge_queue) {
GetIndexActions(FileCopier& file_copier, MergeQueue& merge_queue,
bool support_incremental_indexing) {
std::vector<std::pair<std::unique_ptr<clang::tooling::FrontendActionFactory>,
clang::tooling::ArgumentsAdjuster>>
actions;
auto index_action =
std::make_unique<IndexActionFactory>(file_copier, merge_queue);
auto index_action = std::make_unique<IndexActionFactory>(
file_copier, merge_queue, support_incremental_indexing);
auto adjuster = clang::tooling::combineAdjusters(RemoveClangArgumentsAdjuster,
ExtraArgumentsAdjuster);
actions.push_back(
Expand Down
3 changes: 2 additions & 1 deletion infra/indexer/frontend/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ std::vector<std::string> ParseCommandLine(absl::string_view commandLine);
// perform indexing on a compilation database.
std::vector<std::pair<std::unique_ptr<clang::tooling::FrontendActionFactory>,
clang::tooling::ArgumentsAdjuster>>
GetIndexActions(FileCopier& file_copier, MergeQueue& merge_queue);
GetIndexActions(FileCopier& file_copier, MergeQueue& merge_queue,
bool support_incremental_indexing = false);
} // namespace indexer
} // namespace oss_fuzz

Expand Down
43 changes: 35 additions & 8 deletions infra/indexer/frontend/index_action.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "absl/strings/match.h"
#include "absl/strings/string_view.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/Basic/FileEntry.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/Pragma.h"
#include "clang/Lex/Preprocessor.h"
Expand All @@ -41,23 +43,43 @@ namespace oss_fuzz {
namespace indexer {
class AstConsumer : public clang::ASTConsumer {
public:
explicit AstConsumer(InMemoryIndex& index, clang::CompilerInstance& compiler)
: index_(index), compiler_(compiler) {}
AstConsumer(InMemoryIndex& index, clang::CompilerInstance& compiler,
bool support_incremental_indexing = false)
: index_(index),
compiler_(compiler),
support_incremental_indexing_(support_incremental_indexing) {}
~AstConsumer() override = default;

void HandleTranslationUnit(clang::ASTContext& context) override {
if (support_incremental_indexing_) {
const clang::SourceManager& source_manager = context.getSourceManager();
const clang::FileID main_file_id = source_manager.getMainFileID();
const clang::OptionalFileEntryRef main_file =
source_manager.getFileEntryRefForID(main_file_id);
CHECK(main_file.has_value()) << "Couldn't retrieve the main file entry";

const clang::FileManager& file_manager = source_manager.getFileManager();
llvm::SmallString<256> absolute_path(main_file->getName());
file_manager.makeAbsolutePath(absolute_path);

index_.SetTranslationUnit({absolute_path.data(), absolute_path.size()});
}

AstVisitor visitor(index_, context, compiler_);
visitor.TraverseDecl(context.getTranslationUnitDecl());
}

private:
InMemoryIndex& index_;
clang::CompilerInstance& compiler_;
const bool support_incremental_indexing_;
};

IndexAction::IndexAction(FileCopier& file_copier, MergeQueue& merge_queue)
IndexAction::IndexAction(FileCopier& file_copier, MergeQueue& merge_queue,
bool support_incremental_indexing)
: index_(std::make_unique<InMemoryIndex>(file_copier)),
merge_queue_(merge_queue) {}
merge_queue_(merge_queue),
support_incremental_indexing_(support_incremental_indexing) {}

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

std::unique_ptr<clang::ASTConsumer> IndexAction::CreateASTConsumer(
clang::CompilerInstance& compiler, llvm::StringRef path) {
return std::make_unique<AstConsumer>(*index_, compiler);
return std::make_unique<AstConsumer>(*index_, compiler,
support_incremental_indexing_);
}

IndexActionFactory::IndexActionFactory(FileCopier& file_copier,
MergeQueue& merge_queue)
: file_copier_(file_copier), merge_queue_(merge_queue) {}
MergeQueue& merge_queue,
bool support_incremental_indexing)
: file_copier_(file_copier),
merge_queue_(merge_queue),
support_incremental_indexing_(support_incremental_indexing) {}

std::unique_ptr<clang::FrontendAction> IndexActionFactory::create() {
return std::make_unique<IndexAction>(file_copier_, merge_queue_);
return std::make_unique<IndexAction>(file_copier_, merge_queue_,
support_incremental_indexing_);
}
} // namespace indexer
} // namespace oss_fuzz
11 changes: 8 additions & 3 deletions infra/indexer/frontend/index_action.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define OSS_FUZZ_INFRA_INDEXER_FRONTEND_INDEX_ACTION_H_

#include <memory>
#include <string>

#include "indexer/index/file_copier.h"
#include "indexer/index/in_memory_index.h"
Expand All @@ -32,28 +33,32 @@ namespace indexer {
// indexer/frontend.h should be used instead.
class IndexAction : public clang::ASTFrontendAction {
public:
explicit IndexAction(FileCopier& file_copier, MergeQueue& merge_queue);
explicit IndexAction(FileCopier& file_copier, MergeQueue& merge_queue,
bool support_incremental_indexing = false);

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

std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(
clang::CompilerInstance& compiler, llvm::StringRef) override;
clang::CompilerInstance& compiler, llvm::StringRef path) override;

private:
std::unique_ptr<InMemoryIndex> index_;
MergeQueue& merge_queue_;
bool support_incremental_indexing_;
};

class IndexActionFactory : public clang::tooling::FrontendActionFactory {
public:
explicit IndexActionFactory(FileCopier& file_copier, MergeQueue& merge_queue);
IndexActionFactory(FileCopier& file_copier, MergeQueue& merge_queue,
bool support_incremental_indexing = false);

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

private:
FileCopier& file_copier_;
MergeQueue& merge_queue_;
const bool support_incremental_indexing_;
};
} // namespace indexer
} // namespace oss_fuzz
Expand Down
Loading
Loading