blob: 8ec8b6be5891f191898c67bf442739fd189f5c99 [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
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```
17tools/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 Wuaa891f172019-04-05 14:56:5222## Getting clangd
23
Andrew Williamsbbc1a1e2021-07-21 01:51:2224See [instructions](https://clangd.llvm.org/installation.html#installing-clangd).
Haojian Wuaa891f172019-04-05 14:56:5225
26**Googlers:** clangd has been installed on your glinux by default, just use
27`/usr/bin/clangd`.
28
Jesse McKennaf454c392020-06-05 01:10:1629Alternative: download clangd from the official [Releases](https://github.com/clangd/clangd/releases)
30page.
31
Jesse McKenna3a64a192020-06-26 16:14:1732Note: clangd 10.0.0 does not work with Chromium; use one of the more recent
33pre-release versions of 11 or later on the Releases page.
Jesse McKennaf454c392020-06-05 01:10:1634
35If you prefer to build clangd locally, use the following command to build from
36LLVM source, and you will get the binary at
Haojian Wuaa891f172019-04-05 14:56:5237`out/Release/tools/clang/third_party/llvm/build/bin/clangd`.
38
39```
40tools/clang/scripts/build_clang_tools_extra.py --fetch out/Release clangd
41```
42
43## Setting Up
44
David Benjaminf676adb12019-05-07 07:19:10451. Make sure generated ninja files are up-to-date.
Haojian Wuaa891f172019-04-05 14:56:5246
47```
David Benjaminf676adb12019-05-07 07:19:1048gn gen out/Release
Haojian Wuaa891f172019-04-05 14:56:5249```
50
512. Generate the compilation database, clangd needs it to know how to build a
52source file.
53
54```
55tools/clang/scripts/generate_compdb.py -p out/Release > compile_commands.json
56```
57
Jesse McKennaf454c392020-06-05 01:10:1658Note: the compilation database is not regenerated automatically. You need to
59regenerate it manually whenever build rules change, e.g., when you have new files
Fergal Daly6e028572020-06-02 09:43:0560checked in or when you sync to head.
Haojian Wuaa891f172019-04-05 14:56:5261
Jesse McKenna454b946f2020-05-08 18:23:1062If using Windows PowerShell, use the following command instead to set the
63output's encoding to UTF-8 (otherwise Clangd will hit "YAML:1:4: error: Got
64empty plain scalar" while parsing it).
65
66```
67tools/clang/scripts/generate_compdb.py -p out/Release | out-file -encoding utf8 compile_commands.json
68```
69
David Benjaminf676adb12019-05-07 07:19:10703. Optional: build chrome normally. This ensures generated headers exist and are
71up-to-date. clangd will still work without this step, but it may give errors or
72inaccurate results for files which depend on generated headers.
73
74```
75ninja -C out/Release chrome
76```
77
784. Use clangd in your favourite editor, see detailed [instructions](
Andrew Williamsbbc1a1e2021-07-21 01:51:2279https://clangd.llvm.org/installation.html#editor-plugins).
Haojian Wuaa891f172019-04-05 14:56:5280
Ivan Šandrkde3cc762020-12-02 16:55:2881## Background Indexing
Haojian Wuaa891f172019-04-05 14:56:5282
Ivan Šandrkde3cc762020-12-02 16:55:2883clangd builds an incremental index of your project (all files listed in the
84compilation database). The index improves code navigation features
85(go-to-definition, find-references) and code completion.
Haojian Wuaa891f172019-04-05 14:56:5286
Ivan Šandrkde3cc762020-12-02 16:55:2887* 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 clangds knowledge about your
93 codebase to files you are currently editing.
94
Haojian Wuaa891f172019-04-05 14:56:5295Note: the first index time may take hours (for reference, it took 2~3 hours on
Ivan Šandrkde3cc762020-12-02 16:55:2896a 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 Wuaa891f172019-04-05 14:56:5298
99## Questions
100
101If you have any questions, reach out to clangd-dev@lists.llvm.org.