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