From eaaa8f4f1259f3f955537e0c0355938f9765f0cd Mon Sep 17 00:00:00 2001 From: Garo Brik Date: Mon, 28 Sep 2020 22:55:20 -0400 Subject: [PATCH] default project root to current working directory --- README.md | 18 ++++++------- src/indexer/IndexerMain.cpp | 50 ++++++++++++++++++------------------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 4ebbfbc3b7..05a8a3b764 100644 --- a/README.md +++ b/README.md @@ -27,9 +27,9 @@ Here's how you would build an index of the lsif-clang tool on Ubuntu 20.04. apt install llvm-10 clang clang-10 libclang-10-dev cmake `# install dependencies` git clone https://github.com/sourcegraph/lsif-clang && cd lsif-clang `# get the code` cmake -B build `# configure lsif-clang` -make -C build -j8 `# build lsif-clang` +make -C build -j16 install `# build and install lsif-clang` ln -s $(pwd)/build/compile_commands.json ./ `# link the compilation database to the project root` -./build/bin/lsif-clang --project-root=$(pwd) --executor=all-TUs compile_commands.json > dump.lsif `# generate an index` +lsif-clang --executor=all-TUs compile_commands.json > dump.lsif `# generate an index` ``` The following sections provide detailed explanations of each step and variations on the commands for different platforms and build systems. @@ -63,7 +63,7 @@ Here is a minimal example, known extra steps for specific platforms follow: ```sh cmake -B build -make -C build -j8 +make -C build -j16 install ``` ### MacOS @@ -74,7 +74,7 @@ cmake -B build -DPATH_TO_LLVM=/usr/local/opt/lib ## Generate a compilation database -`lsif-clang` itself is configured to do this automatically, so to test the that the tool built properly you can simply sym-link it to the project root from the build directory and skip to [running lsif-clang](). +`lsif-clang` itself is configured to do this automatically, so to test the that the tool built properly you can simply sym-link it to the project root from the build directory and skip to [running lsif-clang](#run-lsif-clang). From the project root: ```sh @@ -116,22 +116,22 @@ ninja -t compdb | jq '[ .[] | select(.command | startswith("/usr/bin/c++")) ] > Use the [bazel-compilation-database](https://github.com/grailbio/bazel-compilation-database) tool. -### Make and others +### If all else fails -Install the [Bear](https://github.com/rizsotto/Bear) tool and run `bear make`, or `bear `. This tool is build system agnostic so it's a good fallback option. +Install the [Bear](https://github.com/rizsotto/Bear) tool and run `bear make`, or `bear `. This will intercept the actual commands used to build your project and generate a compilation database from them. This is a last resort as it requires you to compile your entire project from scratch before compiling it a second time with `lsif-clang`, which can take quite a while. ## Run lsif-clang Once you have a `compile_commands.json` in the root of your project's source, you can use the following command to index the entire project: ```sh -lsif-clang --project-root=$(pwd) --executor=all-TUs compile_commands.json > dump.lsif +lsif-clang --executor=all-TUs compile_commands.json > dump.lsif ``` To index individual files, use: ```sh -lsif-clang --project-root=$(pwd) file1.cpp file2.cpp ... > dump.lsif +lsif-clang file1.cpp file2.cpp ... > dump.lsif ``` ### MacOS @@ -152,4 +152,4 @@ $ lsif-clang \ ## Test the output -You can use the [lsif-validate]() tool for basic sanity checking, or [upload the index to a Sourcegraph instance]() to see the hovers, definitions, and references in action. +You can use the [lsif-validate](https://github.com/sourcegraph/lsif-test) tool for basic sanity checking, or [upload the index to a Sourcegraph instance](https://docs.sourcegraph.com/user/code_intelligence/lsif_quickstart) to see the hovers, definitions, and references in action. diff --git a/src/indexer/IndexerMain.cpp b/src/indexer/IndexerMain.cpp index eb745e70d2..3567a35e8a 100644 --- a/src/indexer/IndexerMain.cpp +++ b/src/indexer/IndexerMain.cpp @@ -24,10 +24,8 @@ #include "clang/Tooling/Tooling.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Signals.h" - -namespace clang { -namespace clangd { -namespace { +#include +#include // static llvm::cl::opt Format( // "format", llvm::cl::desc("Format of the index to be written"), @@ -46,12 +44,12 @@ static llvm::cl::opt Debug( llvm::cl::desc("Enable verbose debug output."), llvm::cl::init(false)); -class IndexActionFactory : public tooling::FrontendActionFactory { +class IndexActionFactory : public clang::tooling::FrontendActionFactory { public: - IndexActionFactory(IndexFileIn &Result) : Result(Result) {} + IndexActionFactory(clang::clangd::IndexFileIn &Result) : Result(Result) {} - std::unique_ptr create() override { - SymbolCollector::Options Opts; + std::unique_ptr create() override { + clang::clangd::SymbolCollector::Options Opts; Opts.CountReferences = true; Opts.CollectMainFileSymbols = true; Opts.StoreAllDocumentation = true; @@ -60,11 +58,11 @@ class IndexActionFactory : public tooling::FrontendActionFactory { IndexOpts.IndexParametersInDeclarations = true; IndexOpts.IndexImplicitInstantiation = true; IndexOpts.IndexMacrosInPreprocessor = true; - IndexOpts.SystemSymbolFilter = index::IndexingOptions::SystemSymbolFilterKind::All; + IndexOpts.SystemSymbolFilter = clang::index::IndexingOptions::SystemSymbolFilterKind::All; return createStaticIndexingAction( Opts, IndexOpts, - [&](SymbolSlab S) { + [&](clang::clangd::SymbolSlab S) { // Merge as we go. std::lock_guard Lock(SymbolsMu); for (const auto &Sym : S) { @@ -74,7 +72,7 @@ class IndexActionFactory : public tooling::FrontendActionFactory { Symbols.insert(Sym); } }, - [&](RefSlab S) { + [&](clang::clangd::RefSlab S) { std::lock_guard Lock(SymbolsMu); for (const auto &Sym : S) { // Deduplication happens during insertion. @@ -82,7 +80,7 @@ class IndexActionFactory : public tooling::FrontendActionFactory { Refs.insert(Sym.first, Ref); } }, - [&](RelationSlab S) { + [&](clang::clangd::RelationSlab S) { std::lock_guard Lock(SymbolsMu); for (const auto &R : S) { Relations.insert(R); @@ -100,17 +98,13 @@ class IndexActionFactory : public tooling::FrontendActionFactory { } private: - IndexFileIn &Result; + clang::clangd::IndexFileIn &Result; std::mutex SymbolsMu; - SymbolSlab::Builder Symbols; - RefSlab::Builder Refs; - RelationSlab::Builder Relations; + clang::clangd::SymbolSlab::Builder Symbols; + clang::clangd::RefSlab::Builder Refs; + clang::clangd::RelationSlab::Builder Relations; }; -} // namespace -} // namespace clangd -} // namespace clang - int main(int argc, const char **argv) { llvm::sys::PrintStackTraceOnErrorSignal(argv[0]); @@ -119,11 +113,11 @@ int main(int argc, const char **argv) { Example usage for a project using CMake compile commands: - $ clangd-indexer --executor=all-TUs compile_commands.json > clangd.dex + $ lsif-clang --executor=all-TUs compile_commands.json > clangd.dex Example usage for file sequence index without flags: - $ clangd-indexer File1.cpp File2.cpp ... FileN.cpp > clangd.dex + $ lsif-clang File1.cpp File2.cpp ... FileN.cpp > clangd.dex )"; auto Executor = clang::tooling::createExecutorFromCommandLineArgs( @@ -137,7 +131,7 @@ int main(int argc, const char **argv) { // Collect symbols found in each translation unit, merging as we go. clang::clangd::IndexFileIn Data; auto Err = Executor->get()->execute( - std::make_unique(Data), + std::make_unique(Data), clang::tooling::getStripPluginsAdjuster()); if (Err) { llvm::errs() << llvm::toString(std::move(Err)) << "\n"; @@ -146,8 +140,14 @@ int main(int argc, const char **argv) { // Emit collected data. clang::clangd::IndexFileOut Out(Data); Out.Format = clang::clangd::IndexFileFormat::LSIF; - Out.ProjectRoot = "file://" + clang::clangd::ProjectRoot; - Out.Debug = clang::clangd::Debug; + if (ProjectRoot == "") { + llvm::SmallString<128> CurrentPath; + llvm::sys::fs::current_path(CurrentPath); + Out.ProjectRoot = std::string("file://") + CurrentPath.c_str(); + } else { + Out.ProjectRoot = ProjectRoot; + } + Out.Debug = Debug; writeLSIF(Out, llvm::outs()); return 0; }