blob: b4955d03a85aa1b98da6b262d725ffa570d0e34c [file] [log] [blame] [view]
Haojian Wuaa891f172019-04-05 14:56:521# Clangd
2
3## Introduction
4
Andrew Williamsbbc1a1e2021-07-21 01:51:225[clangd](https://clangd.llvm.org/) is a clang-based [language server](https://langserver.org/).
Haojian Wuaa891f172019-04-05 14:56:526It brings IDE features (e.g. diagnostics, code completion, code navigations) to
7your editor.
8
Ivan Šandrkde3cc762020-12-02 16:55:289## Quick Start
10
Kadir Cetinkaya235b7142021-09-21 07:24:3811* **Googlers**: clangd is available by default on glinux (`/usr/bin/clangd`)
Ivan Šandrkde3cc762020-12-02 16:55:2812* 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```
16tools/clang/scripts/generate_compdb.py -p out/<build> > compile_commands.json
17```
Kadir Cetinkaya235b7142021-09-21 07:24:3818* 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 Šandrkde3cc762020-12-02 16:55:2822* Use clangd in your favourite editor
23
Haojian Wuaa891f172019-04-05 14:56:5224## Getting clangd
25
Andrew Williamsbbc1a1e2021-07-21 01:51:2226See [instructions](https://clangd.llvm.org/installation.html#installing-clangd).
Haojian Wuaa891f172019-04-05 14:56:5227
28**Googlers:** clangd has been installed on your glinux by default, just use
29`/usr/bin/clangd`.
30
Jesse McKennaf454c392020-06-05 01:10:1631Alternative: download clangd from the official [Releases](https://github.com/clangd/clangd/releases)
32page.
33
Jesse McKenna3a64a192020-06-26 16:14:1734Note: clangd 10.0.0 does not work with Chromium; use one of the more recent
35pre-release versions of 11 or later on the Releases page.
Jesse McKennaf454c392020-06-05 01:10:1636
37If you prefer to build clangd locally, use the following command to build from
38LLVM source, and you will get the binary at
Kadir Cetinkaya235b7142021-09-21 07:24:3839`out/Default/tools/clang/third_party/llvm/build/bin/clangd`.
Haojian Wuaa891f172019-04-05 14:56:5240
41```
Kadir Cetinkaya235b7142021-09-21 07:24:3842tools/clang/scripts/build_clang_tools_extra.py --fetch out/Default clangd
Haojian Wuaa891f172019-04-05 14:56:5243```
44
45## Setting Up
46
David Benjaminf676adb12019-05-07 07:19:10471. Make sure generated ninja files are up-to-date.
Haojian Wuaa891f172019-04-05 14:56:5248
49```
Kadir Cetinkaya235b7142021-09-21 07:24:3850gn gen out/Default
Haojian Wuaa891f172019-04-05 14:56:5251```
52
532. Generate the compilation database, clangd needs it to know how to build a
54source file.
55
56```
Kadir Cetinkaya235b7142021-09-21 07:24:3857tools/clang/scripts/generate_compdb.py -p out/Default > compile_commands.json
Haojian Wuaa891f172019-04-05 14:56:5258```
59
Jesse McKennaf454c392020-06-05 01:10:1660Note: the compilation database is not regenerated automatically. You need to
61regenerate it manually whenever build rules change, e.g., when you have new files
Fergal Daly6e028572020-06-02 09:43:0562checked in or when you sync to head.
Haojian Wuaa891f172019-04-05 14:56:5263
Jesse McKenna454b946f2020-05-08 18:23:1064If using Windows PowerShell, use the following command instead to set the
65output's encoding to UTF-8 (otherwise Clangd will hit "YAML:1:4: error: Got
66empty plain scalar" while parsing it).
67
68```
Kadir Cetinkaya235b7142021-09-21 07:24:3869tools/clang/scripts/generate_compdb.py -p out/Default | out-file -encoding utf8 compile_commands.json
Jesse McKenna454b946f2020-05-08 18:23:1070```
71
David Benjaminf676adb12019-05-07 07:19:10723. Optional: build chrome normally. This ensures generated headers exist and are
73up-to-date. clangd will still work without this step, but it may give errors or
74inaccurate results for files which depend on generated headers.
75
76```
Kadir Cetinkaya235b7142021-09-21 07:24:3877ninja -C out/Default chrome
David Benjaminf676adb12019-05-07 07:19:1078```
79
Kadir Cetinkaya235b7142021-09-21 07:24:38804. 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
845. Use clangd in your favourite editor, see detailed [instructions](
Andrew Williamsbbc1a1e2021-07-21 01:51:2285https://clangd.llvm.org/installation.html#editor-plugins).
Haojian Wuaa891f172019-04-05 14:56:5286
Ivan Šandrkde3cc762020-12-02 16:55:2887## Background Indexing
Haojian Wuaa891f172019-04-05 14:56:5288
Ivan Šandrkde3cc762020-12-02 16:55:2889clangd builds an incremental index of your project (all files listed in the
90compilation database). The index improves code navigation features
91(go-to-definition, find-references) and code completion.
Haojian Wuaa891f172019-04-05 14:56:5292
Ivan Šandrkde3cc762020-12-02 16:55:2893* 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 Wuaa891f172019-04-05 14:56:52101Note: the first index time may take hours (for reference, it took 2~3 hours on
Ivan Šandrkde3cc762020-12-02 16:55:28102a 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 Wuaa891f172019-04-05 14:56:52104
Kadir Cetinkaya235b7142021-09-21 07:24:38105Note: [Remote-index service](https://github.com/clangd/chrome-remote-index/blob/main/docs/index.md)
106replaces background-index with some downsides like being ~a day old (Clangd will
107still know about your changes in the current editing session) and not covering
108all configurations (not available for mac&windows specific code or non-main
109branches).
110
Haojian Wuaa891f172019-04-05 14:56:52111## Questions
112
Kadir Cetinkaya235b7142021-09-21 07:24:38113If you have any questions, reach out to
114[clangd/clangd](https://github.com/clangd/clangd) or [email protected].