Skip to content

Commit 9ae2dd5

Browse files
kleinesfilmroellchennico
authored andcommitted
Documentation: Consolidate and extend clangd configuration docs
These were previously spread over multiple editor documentation files, so we consolidate them into one while dropping outdated (or unexplained) options. A new ClangdConfiguration file is created that will house the common config. Furthermore, since clangd 20 supports the header style options (implemented by yours truly), document that this feature is now available and what AngledHeaders globs to use for Serenity specifically.
1 parent 89deb3e commit 9ae2dd5

6 files changed

+76
-95
lines changed

Documentation/ClangdConfiguration.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Configuring the clangd Language Server
2+
3+
clangd is recommended as a C++ language server in general, as it is most likely to work on all platforms.
4+
5+
CLion ships with a special build of clangd that usually works fine. Most other editors can be configured to use any build of clangd via the LSP or dedicated extensions/plugins. Refer to the editor-specific documentation on how to install and enable such extensions.
6+
7+
## `.clangd`
8+
9+
clangd uses `compile_commands.json` files to understand the project. CMake will generate these in directories such as Build/x86_64, Build/x86_64clang, and Build/lagom.
10+
Depending on which configuration (architecture and toolchain) you use most, set the CompilationDatabase configuration item in the below `.clangd` file accordingly.
11+
12+
The recommended `.clangd` file (at the root of your Serenity checkout) is as follows; it should work out of the box and can be adjusted as needed:
13+
14+
```yaml
15+
CompileFlags:
16+
# Add compilation flags to remove errors, or to make clangd behave like you’re compiling a specific system configuration.
17+
Add: []
18+
# You can remove unwanted flags such as those that aren't supported by the current version of clang.
19+
Remove: []
20+
# Build/x86_64 is also possible if you don’t have the Clang toolchain, but doesn’t work as well.
21+
CompilationDatabase: Build/x86_64clang
22+
23+
Style:
24+
# clangd 20+: Use correct include style.
25+
AngledHeaders: ["AK/.*", "Userland/.*", "Kernel/.*", "Applications/.*", "Lib.*/.*"]
26+
27+
Diagnostics:
28+
UnusedIncludes: Strict
29+
MissingIncludes: Loose
30+
```
31+
32+
The UnusedIncludes and MissingIncludes flags are used to disable the [Include Cleaner](https://clangd.llvm.org/design/include-cleaner) feature of newer clangd releases.
33+
It can be re-enabled if you don't mind the noisy inlay hints and problems.
34+
35+
Adding and removing compiler flags can fix many bugs, but also be useful for better code comprehension. Here are some tips:
36+
37+
- Add `-D__serenity__` if you are not using the Serenity toolchain clangd, so that clangd treats all code as being compiled for Serenity and not for your host system.
38+
- Add the flags `-DKERNEL` or `-DPREKERNEL` if you want clangd to behave like the Kernel or Prekernel compilation, respectively.
39+
- When using a GCC compilation database (e.g. Build/x86_64), clangd will frequently complain about command-line arguments at the top of files. For instance: `clang: Unknown argument: '-mpreferred-stack-boundary=3'`. To fix this, simply add `-mno-<flag name>` to the `Add` list. In the example, that would be `-mno-preferred-stack-boundary`.
40+
- There are also known issues with the GCC compilation database for the Kernel, here `-mno-sse` and `-mno-8087` may help.
41+
42+
Run `./Meta/serenity.sh run` at least once to generate the `compile_commands.json` file. Every time a new source is added or the compilation commands get adjusted (through CMake) you need to rerun `./Meta/serenity.sh build` or any other build command in order to update the compile commands. Until you do so, clangd will not be aware of the new source or report incorrect compile errors.
43+
44+
### clangd Command-line Arguments
45+
46+
When using the system clangd, it has to be configured to find the cross-compiler’s built-in include paths. Otherwise, there will always be errors like “file \<new\> not found”. The clangd command-line flag for this is `--query-driver=SERENITY_PATH/Toolchain/Local/**/*` where you have to replace SERENITY_PATH with the Serenity source directory. Some editors have placeholders you can use here (e.g. VSCode’s `${workspaceFolder}`).
47+
48+
For clangd 19 and below, `--header-insertion=never` is strongly recommended, as this prevents clangd from inserting incorrectly-styled includes. See the [corresponding clangd issue](https://github.com/clangd/clangd/issues/1247). From clangd 20 onwards, this option is not needed anymore and replaced by the `AngledHeaders` directive that configures clangd’s include style correctly.
49+
50+
## Using Serenity Toolchain clangd
51+
52+
The LLVM/Clang toolchain for Serenity is capable of building a version of clangd that is aware of the Serenity targets and their configuration. In general, it is recommended to use this clangd, as it will additionally always be up-to-date. It does, however, require you to build the Clang toolchain. See [AdvancedBuildInstructions](AdvancedBuildInstructions.md#serenity-aware-clang-tools) for how to build this clangd. Then, configure your editor to use the clangd executable situated at `Toolchain/Local/clang/bin/clangd`.
53+
54+
## Known Issues
55+
56+
- Some distribution clangd packages still have issues identifying paths to the serenity cross-compilers' builtin include paths after supplying the `--query-driver` option. This has been seen on at least Debian. If the inlay hints suggest that `<new>` cannot be found, first triple check your configuration matches the `.clangd` file from above, verify that you've run the OS via `Meta/serenity.sh run`, and quadruple check your clangd command-line arguments. If all of the above are correct, building `clangd` from the serenity clang toolchain is known to work.
57+
- clangd has a tendency to crash when stressing bleeding edge compiler features. Sometimes, just opening `AK/Variant.h` is enough. You can usually just restart clangd. If that doesn't help, close currently open C++ files and/or switch branches before restarting, which helps sometimes.

Documentation/EmacsConfiguration.md

+7-32
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,13 @@ Emacs can be configured with `lsp-mode` and `clangd` to work well.
44

55
### clangd
66

7-
The official clangd extension can be used for C++ comprehension. You
8-
can use the following `.clangd` file placed in the project root:
7+
The official clangd extension can be used for C++ comprehension. Refer to [ClangdConfiguration](ClangdConfiguration.md) for how to configure clangd.
98

10-
```yaml
11-
CompileFlags:
12-
CompilationDatabase: Build/x86_64
13-
Add:
14-
- "-D__serenity__"
15-
- "-UNO_TLS"
16-
- "-I/path/to/serenity/Toolchain/Local/x86_64/x86_64-pc-serenity/include/c++/13.1.0"
17-
- "-I/path/to/serenity/Toolchain/Local/x86_64/x86_64-pc-serenity/include/c++/13.1.0/x86_64-pc-serenity"
18-
19-
Diagnostics:
20-
UnusedIncludes: None
21-
MissingIncludes: None
22-
```
23-
24-
You will need to change `/path/to/serenity` and change `13.1.0` to
25-
whatever your GCC toolchain version at the time is.
9+
There are a few different ways to specify which clangd to use:
2610

27-
Run cmake (`Meta/serenity.sh run` or similar) at least once for this
28-
to work, as it will generate the `Build/x86_64/compile_commands.json`
29-
that is needed by `clangd`.
11+
- By default, without configuration `lsp-mode` will try to find and use your system `clangd`. This is the easiest solution, but your system clangd might be out of date.
12+
- You can manually specify any `clangd` binary with `lsp-clangd-binary-path`.
13+
- You can have `lsp-mode` manage your `clangd` installation with emacs' `lsp-install-server`. This will install a `clangd` binary for you.
3014

3115
### lsp-mode
3216

@@ -35,21 +19,12 @@ that is needed by `clangd`.
3519
:hook ((c++-mode) . lsp-deferred)
3620
:commands lsp
3721
:config
22+
;; clangd arguments, refer to ClangdConfiguration.md for what other arguments may be needed.
3823
(setq lsp-clients-clangd-args '("-j=4" "-background-index" "--log=error" "--clang-tidy" "--enable-config"))
39-
;; Optionally, set the location of clangd -- See notes below for options.
24+
;; Optionally, set the location of clangd -- See above for options.
4025
(setq lsp-clangd-binary-path "/usr/bin/clangd"))
41-
4226
```
4327

44-
### clangd
45-
46-
There are a few different ways to specify which clangd to use:
47-
48-
- By default, without configuration `lsp-mode` will try to find and use your system `clangd`. This is the easiest solution, but your system clangd might be out of date.
49-
- You can manually specify any `clangd` binary with `lsp-clangd-binary-path`, as shown in the use-package example above.
50-
- You can have `lsp-mode` manage your `clangd` installation with emacs' `lsp-install-server`. This will install a `clangd` binary for you.
51-
- You can build the LLVM toolchain, including `clangd`, from Serenity's repository. This is an advanced option that is not currently documented.
52-
5328
### clang-format
5429

5530
There are multiple packages to handle auto formatting with

Documentation/HelixConfiguration.md

+4-17
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,16 @@
11
# Helix Configuration
22

3-
Helix comes with support for `clangd` and `clang-format` out of the box! However, a small bit of configuration is needed for it to work correctly with SerenityOS.
3+
Helix comes with support for `clangd` and `clang-format` out of the box! Refer to [ClangdConfiguration](ClangdConfiguration.md) for how to configure clangd.
44

5-
The following `.clangd` should be placed in the project root:
6-
7-
```yaml
8-
CompileFlags:
9-
CompilationDatabase: Build/x86_64 # Or whatever architecture you're targeting, e.g. aarch64
10-
Add: [-D__serenity__]
11-
12-
Diagnostics:
13-
UnusedIncludes: None
14-
MissingIncludes: None
15-
```
16-
17-
You also need to configure the clangd server to detect headers properly from the Serenity toolchain. To do this, create a `.helix/languages.toml` file in the project root:
5+
To configure clangd command-line arguments, create a `.helix/languages.toml` file in the project root:
186

197
```toml
208
[language-server.serenity]
219
command = "clangd"
22-
args = ["--query-driver=/path/to/serenity/Toolchain/Local/**/*", "--header-insertion=never"]
10+
# clangd arguments, refer to ClangdConfiguration.md for what may be needed.
11+
args = []
2312

2413
[[language]]
2514
name = "cpp"
2615
language-servers = ["serenity"]
2716
```
28-
29-
> Make sure to replace `/path/to/serenity` with the actual path in the snippet above!

Documentation/NvimConfiguration.md

+1-11
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,4 @@ nmap <silent>gs :CocCommand clangd.switchSourceHeader vsplit<CR>
183183

184184
# Configure .clangd
185185

186-
> **Note**: Every time a new source is added or the compilation commands get adjusted
187-
> (through CMake) you need to rerun `./Meta/serenity.sh rebuild`.
188-
189-
Link `ln -s /path/to/serenity/Build/x86_64/compile_commands.json /path/to/serenity/compile_commands.json`.
190-
191-
Create `/path/to/serenity/.clangd` (replace `/path/to/serenity`
192-
with your SerenityOS directory) with content of the clangd section in the
193-
[VSCodeConfiguration.md](./VSCodeConfiguration.md).
194-
195-
> **Note**: You can add a `Remove` part, where you can remove unwanted flags
196-
> such as those that aren't supported by the current version of clang.
186+
Configure `.clangd` as explained in [ClangdConfiguration](ClangdConfiguration.md).

Documentation/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ Make sure to read the basic [Build Instructions](BuildInstructions.md) first.
3030
- [Running On Raspberry Pi](RunningOnRaspberryPi.md)
3131
- [Known Hardware Compatibility](HardwareCompatibility.md)
3232

33-
## Configuring Editors
33+
## Configuring Editors and Language Servers
3434

35+
- [clangd (all editors)](ClangdConfiguration.md)
3536
- [CLion](CLionConfiguration.md)
3637
- [Emacs](EmacsConfiguration.md)
3738
- [Helix](HelixConfiguration.md)

Documentation/VSCodeConfiguration.md

+5-34
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,7 @@ The recommended extensions for VS Code include:
1414

1515
Clangd has the best support for cross-compiling workflows, especially if configured as noted below. The Microsoft C/C++ tools can work, but require a lot more configuration and may not understand the sysroot in use.
1616

17-
### clangd
18-
19-
The official clangd extension can be used for C++ comprehension. It is recommended in general, as it is most likely to work on all platforms.
20-
21-
clangd uses `compile_commands.json` files to understand the project. CMake will generate these in either Build/x86_64, Build/x86_64clang, and Build/lagom.
22-
Depending on which configuration you use most, set the CompilationDatabase configuration item in the below `.clangd` file accordingly. It goes at the root of your checkout (`serenity/.clangd`):
23-
24-
```yaml
25-
CompileFlags:
26-
Add: [-D__serenity__]
27-
CompilationDatabase: Build/x86_64
28-
29-
Diagnostics:
30-
UnusedIncludes: None
31-
MissingIncludes: None
32-
```
33-
34-
The UnusedIncludes and MissingIncludes flags are used to disable the [Include Cleaner](https://clangd.llvm.org/design/include-cleaner) feature of newer clangd releases.
35-
It can be re-enabled if you don't mind the noisy inlay hints and problems in the problem view.
36-
37-
Run `./Meta/serenity.sh run` at least once to generate the `compile_commands.json` file.
38-
39-
In addition to the `.clangd` file, the `settings.json` file below has a required `clangd.arguments` entry for `--query-driver` that allows clangd to find the cross-compiler's built-in include paths.
40-
41-
#### Known issues
42-
43-
- Some distribution clangd packages still have issues identifying paths to the serenity cross-compilers' builtin include paths after supplying the `--query-driver` option from `settings.json`. This has been seen on at least Debian. If the inlay hints suggest that `<new>` cannot be found, first triple check your configuration matches the `.clangd` file from this section, verify that you've run the OS via `Meta/serenity.sh run`, and quadruple check your `clangd.arguments` section in the project-local `settings.json` file. If all of the above are correct, building `clangd` from the serenity clang toolchain is known to work. See [AdvancedBuildInstructions](AdvancedBuildInstructions.md#serenity-aware-clang-tools) for steps on how to build it from source. After building from source, be sure to set `clangd.path` in your `settings.json` to `${workspaceFolder}/Toolchain/Local/clang/bin/clangd`.
44-
45-
- clangd has a tendency to crash when stressing bleeding edge compiler features. You can usually just restart it via the command palette. If that doesn't help, close currently open C++ files and/or switch branches before restarting, which helps sometimes.
17+
See [ClangdConfiguration](ClangdConfiguration.md) for information on how to configure clangd.
4618

4719
### DSL syntax highlighting
4820

@@ -149,11 +121,10 @@ These belong in the `.vscode/settings.json` of Serenity.
149121
// git commit message length
150122
"git.inputValidationLength": 72,
151123
"git.inputValidationSubjectLength": 72,
152-
// Tell clangd to ask the cross-compilers for their builtin include paths
153-
"clangd.arguments": [
154-
"--query-driver=${workspaceFolder}/Toolchain/Local/**/*",
155-
"--header-insertion=never" // See https://github.com/clangd/clangd/issues/1247
156-
]
124+
// See ClangdConfiguration.md for arguments that may be needed.
125+
"clangd.arguments": [],
126+
// Set if needed.
127+
"clangd.path": "..."
157128
}
158129
```
159130

0 commit comments

Comments
 (0)