Skip to content

Commit

Permalink
[clangd] Add --query-driver flag to clangd-indexer
Browse files Browse the repository at this point in the history
When using `clangd` for cross-compiled projects, it's necessary to use the `--query-driver` flag so that `clangd` can extract the compiler's built-in header paths. However, there's no such flag for `clangd-indexer` so we're unable to build a working static index for these projects.

This patch adds a `--query-driver` flag to `clangd-indexer` for this scenario.

I saw some tests under `clang-tools-extra/clangd/test/` but I think the cross-compilation case is a bit more complex to test. Let me know if you'd like me to look into this further.

Resolves: clangd/clangd#1717

Reviewed By: nridge

Differential Revision: https://reviews.llvm.org/D157990
  • Loading branch information
HighCommander4 committed Aug 24, 2023
1 parent a0d457b commit 109bc02
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions clang-tools-extra/clangd/indexer/IndexerMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ static llvm::cl::opt<IndexFileFormat>
"binary RIFF format")),
llvm::cl::init(IndexFileFormat::RIFF));

static llvm::cl::list<std::string> QueryDriverGlobs{
"query-driver",
llvm::cl::desc(
"Comma separated list of globs for white-listing gcc-compatible "
"drivers that are safe to execute. Drivers matching any of these globs "
"will be used to extract system includes. e.g. "
"/usr/bin/**/clang-*,/path/to/repo/**/g++-*"),
llvm::cl::CommaSeparated,
};

class IndexActionFactory : public tooling::FrontendActionFactory {
public:
IndexActionFactory(IndexFileIn &Result) : Result(Result) {}
Expand Down Expand Up @@ -144,12 +154,16 @@ int main(int argc, const char **argv) {

// Collect symbols found in each translation unit, merging as we go.
clang::clangd::IndexFileIn Data;
auto Mangler = std::make_shared<clang::clangd::CommandMangler>(
clang::clangd::CommandMangler::detect());
Mangler->SystemIncludeExtractor = clang::clangd::getSystemIncludeExtractor(
static_cast<llvm::ArrayRef<std::string>>(
clang::clangd::QueryDriverGlobs));
auto Err = Executor->get()->execute(
std::make_unique<clang::clangd::IndexActionFactory>(Data),
clang::tooling::ArgumentsAdjuster(
[Mangler = std::make_shared<clang::clangd::CommandMangler>(
clang::clangd::CommandMangler::detect())](
const std::vector<std::string> &Args, llvm::StringRef File) {
[Mangler = std::move(Mangler)](const std::vector<std::string> &Args,
llvm::StringRef File) {
clang::tooling::CompileCommand Cmd;
Cmd.CommandLine = Args;
Mangler->operator()(Cmd, File);
Expand Down

0 comments on commit 109bc02

Please sign in to comment.