Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
default project root to current working directory
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouxm committed Sep 29, 2020
1 parent 0c0dc7e commit eaaa8f4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 <your-build-command>`. 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 <your-build-command>`. 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
Expand All @@ -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.
50 changes: 25 additions & 25 deletions src/indexer/IndexerMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <llvm-10/llvm/ADT/SmallVector.h>
#include <llvm-10/llvm/Support/FileSystem.h>

// static llvm::cl::opt<IndexFileFormat> Format(
// "format", llvm::cl::desc("Format of the index to be written"),
Expand All @@ -46,12 +44,12 @@ static llvm::cl::opt<bool> 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<FrontendAction> create() override {
SymbolCollector::Options Opts;
std::unique_ptr<clang::FrontendAction> create() override {
clang::clangd::SymbolCollector::Options Opts;
Opts.CountReferences = true;
Opts.CollectMainFileSymbols = true;
Opts.StoreAllDocumentation = true;
Expand All @@ -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<std::mutex> Lock(SymbolsMu);
for (const auto &Sym : S) {
Expand All @@ -74,15 +72,15 @@ class IndexActionFactory : public tooling::FrontendActionFactory {
Symbols.insert(Sym);
}
},
[&](RefSlab S) {
[&](clang::clangd::RefSlab S) {
std::lock_guard<std::mutex> Lock(SymbolsMu);
for (const auto &Sym : S) {
// Deduplication happens during insertion.
for (const auto &Ref : Sym.second)
Refs.insert(Sym.first, Ref);
}
},
[&](RelationSlab S) {
[&](clang::clangd::RelationSlab S) {
std::lock_guard<std::mutex> Lock(SymbolsMu);
for (const auto &R : S) {
Relations.insert(R);
Expand All @@ -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]);

Expand All @@ -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(
Expand All @@ -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<clang::clangd::IndexActionFactory>(Data),
std::make_unique<IndexActionFactory>(Data),
clang::tooling::getStripPluginsAdjuster());
if (Err) {
llvm::errs() << llvm::toString(std::move(Err)) << "\n";
Expand All @@ -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;
}

0 comments on commit eaaa8f4

Please sign in to comment.