Haojian Wu | aa891f17 | 2019-04-05 14:56:52 | [diff] [blame] | 1 | # Clangd |
| 2 | |
| 3 | ## Introduction |
| 4 | |
Andrew Williams | bbc1a1e | 2021-07-21 01:51:22 | [diff] [blame] | 5 | [clangd](https://clangd.llvm.org/) is a clang-based [language server](https://langserver.org/). |
Haojian Wu | aa891f17 | 2019-04-05 14:56:52 | [diff] [blame] | 6 | It brings IDE features (e.g. diagnostics, code completion, code navigations) to |
| 7 | your editor. |
| 8 | |
Ivan Šandrk | de3cc76 | 2020-12-02 16:55:28 | [diff] [blame] | 9 | ## Quick Start |
| 10 | |
| 11 | * **Googlers**: clangd weekly is available by default on glinux |
| 12 | (`/usr/bin/clangd`) |
| 13 | * Make sure generated ninja files are up-to-date |
| 14 | * Optional: build chrome normally to get generated headers |
| 15 | * Generate compilation database (note: it's not regenerated automatically): |
| 16 | ``` |
| 17 | tools/clang/scripts/generate_compdb.py -p out/<build> > compile_commands.json |
| 18 | ``` |
| 19 | * Indexing is enabled by default (since clangd 9) |
| 20 | * Use clangd in your favourite editor |
| 21 | |
Haojian Wu | aa891f17 | 2019-04-05 14:56:52 | [diff] [blame] | 22 | ## Getting clangd |
| 23 | |
Andrew Williams | bbc1a1e | 2021-07-21 01:51:22 | [diff] [blame] | 24 | See [instructions](https://clangd.llvm.org/installation.html#installing-clangd). |
Haojian Wu | aa891f17 | 2019-04-05 14:56:52 | [diff] [blame] | 25 | |
| 26 | **Googlers:** clangd has been installed on your glinux by default, just use |
| 27 | `/usr/bin/clangd`. |
| 28 | |
Jesse McKenna | f454c39 | 2020-06-05 01:10:16 | [diff] [blame] | 29 | Alternative: download clangd from the official [Releases](https://github.com/clangd/clangd/releases) |
| 30 | page. |
| 31 | |
Jesse McKenna | 3a64a19 | 2020-06-26 16:14:17 | [diff] [blame] | 32 | Note: clangd 10.0.0 does not work with Chromium; use one of the more recent |
| 33 | pre-release versions of 11 or later on the Releases page. |
Jesse McKenna | f454c39 | 2020-06-05 01:10:16 | [diff] [blame] | 34 | |
| 35 | If you prefer to build clangd locally, use the following command to build from |
| 36 | LLVM source, and you will get the binary at |
Haojian Wu | aa891f17 | 2019-04-05 14:56:52 | [diff] [blame] | 37 | `out/Release/tools/clang/third_party/llvm/build/bin/clangd`. |
| 38 | |
| 39 | ``` |
| 40 | tools/clang/scripts/build_clang_tools_extra.py --fetch out/Release clangd |
| 41 | ``` |
| 42 | |
| 43 | ## Setting Up |
| 44 | |
David Benjamin | f676adb1 | 2019-05-07 07:19:10 | [diff] [blame] | 45 | 1. Make sure generated ninja files are up-to-date. |
Haojian Wu | aa891f17 | 2019-04-05 14:56:52 | [diff] [blame] | 46 | |
| 47 | ``` |
David Benjamin | f676adb1 | 2019-05-07 07:19:10 | [diff] [blame] | 48 | gn gen out/Release |
Haojian Wu | aa891f17 | 2019-04-05 14:56:52 | [diff] [blame] | 49 | ``` |
| 50 | |
| 51 | 2. Generate the compilation database, clangd needs it to know how to build a |
| 52 | source file. |
| 53 | |
| 54 | ``` |
| 55 | tools/clang/scripts/generate_compdb.py -p out/Release > compile_commands.json |
| 56 | ``` |
| 57 | |
Jesse McKenna | f454c39 | 2020-06-05 01:10:16 | [diff] [blame] | 58 | Note: the compilation database is not regenerated automatically. You need to |
| 59 | regenerate it manually whenever build rules change, e.g., when you have new files |
Fergal Daly | 6e02857 | 2020-06-02 09:43:05 | [diff] [blame] | 60 | checked in or when you sync to head. |
Haojian Wu | aa891f17 | 2019-04-05 14:56:52 | [diff] [blame] | 61 | |
Jesse McKenna | 454b946f | 2020-05-08 18:23:10 | [diff] [blame] | 62 | If using Windows PowerShell, use the following command instead to set the |
| 63 | output's encoding to UTF-8 (otherwise Clangd will hit "YAML:1:4: error: Got |
| 64 | empty plain scalar" while parsing it). |
| 65 | |
| 66 | ``` |
| 67 | tools/clang/scripts/generate_compdb.py -p out/Release | out-file -encoding utf8 compile_commands.json |
| 68 | ``` |
| 69 | |
David Benjamin | f676adb1 | 2019-05-07 07:19:10 | [diff] [blame] | 70 | 3. Optional: build chrome normally. This ensures generated headers exist and are |
| 71 | up-to-date. clangd will still work without this step, but it may give errors or |
| 72 | inaccurate results for files which depend on generated headers. |
| 73 | |
| 74 | ``` |
| 75 | ninja -C out/Release chrome |
| 76 | ``` |
| 77 | |
| 78 | 4. Use clangd in your favourite editor, see detailed [instructions]( |
Andrew Williams | bbc1a1e | 2021-07-21 01:51:22 | [diff] [blame] | 79 | https://clangd.llvm.org/installation.html#editor-plugins). |
Haojian Wu | aa891f17 | 2019-04-05 14:56:52 | [diff] [blame] | 80 | |
Ivan Šandrk | de3cc76 | 2020-12-02 16:55:28 | [diff] [blame] | 81 | ## Background Indexing |
Haojian Wu | aa891f17 | 2019-04-05 14:56:52 | [diff] [blame] | 82 | |
Ivan Šandrk | de3cc76 | 2020-12-02 16:55:28 | [diff] [blame] | 83 | clangd builds an incremental index of your project (all files listed in the |
| 84 | compilation database). The index improves code navigation features |
| 85 | (go-to-definition, find-references) and code completion. |
Haojian Wu | aa891f17 | 2019-04-05 14:56:52 | [diff] [blame] | 86 | |
Ivan Šandrk | de3cc76 | 2020-12-02 16:55:28 | [diff] [blame] | 87 | * clangd only uses idle cores to build the index, you can limit the total amount |
| 88 | of cores by passing the *-j=\<number\>* flag; |
| 89 | * the index is saved to the `.clangd/index` in the project root; index shards |
| 90 | for common headers e.g. STL will be stored in *$HOME/.clangd/index*; |
| 91 | * background indexing can be disabled by the `--background-index=false` flag; |
| 92 | Note that, disabling background-index will limit clangd’s knowledge about your |
| 93 | codebase to files you are currently editing. |
| 94 | |
Haojian Wu | aa891f17 | 2019-04-05 14:56:52 | [diff] [blame] | 95 | Note: the first index time may take hours (for reference, it took 2~3 hours on |
Ivan Šandrk | de3cc76 | 2020-12-02 16:55:28 | [diff] [blame] | 96 | a 48-core, 64GB machine). A full index of Chromium (including v8, blink) takes |
| 97 | ~550 MB disk space and ~2.7 GB memory in clangd. |
Haojian Wu | aa891f17 | 2019-04-05 14:56:52 | [diff] [blame] | 98 | |
| 99 | ## Questions |
| 100 | |
| 101 | If you have any questions, reach out to clangd-dev@lists.llvm.org. |