Design of clangd

A language server’s main responsibilites are:

Request handling

clangd is based on the clang compiler, and at its core runs the clang parser in a loop. The parser produces diagnostics as it encounters problems, and the end result is a clang AST. The AST is saved to answer queries like “what kind of symbol is under the cursor”.

There is one such loop for each open file. The TUScheduler class manages a collection of ASTWorkers, each running on its own thread. Most operations run on these threads, though code completion is a notable exception.

threads and request handling

Index

C/C++/Objective-C are designed so that you can parse one source file at a time, without needing to see the whole program. This means that the resulting AST lacks certain information:

To solve this, clangd maintains a database of symbols found anywhere in the program, called the index. This is extracted from each open file as it is parsed, and also by parsing the whole project in the background.

the clangd index