Haojian Wu | aa891f17 | 2019-04-05 14:56:52 | [diff] [blame] | 1 | # Clangd |
| 2 | |
| 3 | ## Introduction |
| 4 | |
| 5 | [clangd](https://clang.llvm.org/extra/clangd/) is a clang-based [language server](http://langserver.org/). |
| 6 | It brings IDE features (e.g. diagnostics, code completion, code navigations) to |
| 7 | your editor. |
| 8 | |
| 9 | ## Getting clangd |
| 10 | |
| 11 | See [instructions](https://clang.llvm.org/extra/clangd/Installation.html#installing-clangd). |
| 12 | |
| 13 | **Googlers:** clangd has been installed on your glinux by default, just use |
| 14 | `/usr/bin/clangd`. |
| 15 | |
| 16 | Alternative: use the following command to build clangd from LLVM source, and you |
| 17 | will get the binary at |
| 18 | `out/Release/tools/clang/third_party/llvm/build/bin/clangd`. |
| 19 | |
| 20 | ``` |
| 21 | tools/clang/scripts/build_clang_tools_extra.py --fetch out/Release clangd |
| 22 | ``` |
| 23 | |
| 24 | ## Setting Up |
| 25 | |
David Benjamin | f676adb1 | 2019-05-07 07:19:10 | [diff] [blame] | 26 | 1. Make sure generated ninja files are up-to-date. |
Haojian Wu | aa891f17 | 2019-04-05 14:56:52 | [diff] [blame] | 27 | |
| 28 | ``` |
David Benjamin | f676adb1 | 2019-05-07 07:19:10 | [diff] [blame] | 29 | gn gen out/Release |
Haojian Wu | aa891f17 | 2019-04-05 14:56:52 | [diff] [blame] | 30 | ``` |
| 31 | |
| 32 | 2. Generate the compilation database, clangd needs it to know how to build a |
| 33 | source file. |
| 34 | |
| 35 | ``` |
| 36 | tools/clang/scripts/generate_compdb.py -p out/Release > compile_commands.json |
| 37 | ``` |
| 38 | |
| 39 | Note: the compilation database is not re-generated automatically, you'd need to |
| 40 | regenerate it manually when you have new files checked in. |
| 41 | |
David Benjamin | f676adb1 | 2019-05-07 07:19:10 | [diff] [blame] | 42 | 3. Optional: build chrome normally. This ensures generated headers exist and are |
| 43 | up-to-date. clangd will still work without this step, but it may give errors or |
| 44 | inaccurate results for files which depend on generated headers. |
| 45 | |
| 46 | ``` |
| 47 | ninja -C out/Release chrome |
| 48 | ``` |
| 49 | |
| 50 | 4. Use clangd in your favourite editor, see detailed [instructions]( |
Haojian Wu | aa891f17 | 2019-04-05 14:56:52 | [diff] [blame] | 51 | https://clang.llvm.org/extra/clangd/Installation.html#getting-started-with-clangd). |
| 52 | |
| 53 | ## Index |
| 54 | |
| 55 | By default, clangd only knows the files you are currently editing. To provide |
| 56 | project-wide code navigations (e.g. find references), clangd neesds a |
| 57 | project-wide index. |
| 58 | |
| 59 | You can pass an **experimental** `--background-index` command line argument to |
| 60 | clangd, clangd will incrementally build an index of Chromium in the background. |
| 61 | Note: the first index time may take hours (for reference, it took 2~3 hours on |
| 62 | a 48-core, 64GB machine). |
| 63 | |
| 64 | A full index of Chromium (including v8, blink) takes ~550 MB disk space and ~2.7 |
| 65 | GB memory in clangd. |
| 66 | |
| 67 | ## Questions |
| 68 | |
| 69 | If you have any questions, reach out to [email protected]. |