LLVM provides a Language Server for C++. This is a program that runs as a daemon, providing editor features like error highlighting, jump-to-definition, and code completion. It’s a lot more productive to see these as you type, rather than having to do a bazel build.
https://clangd.llvm.org/ is the program we want.
clangd
relies on something called a “Compilation Database” so it understands the dependency graph, such as the location on disk of some header files used in a compile step. See Configuration
We need to transform Bazel’s action graph (the build steps it would perform) into a compilation database. There is a tool available to do this: Embed GitHub which is already installed in our MODULE.bazel file.
There are a few ways to configure and run the compile-commands-extractor. The simplest way works for our small example, which is to run it with the default Bazel flags over all targets in our repository:
% bazel run @hedron_compile_commands//:refresh_all
We also need clangd
installed and integrated with the editor. Instructions vary by editor. For example with Visual Studio Code, after adding the extension we see auto-completion in our magic.c
file, and can jump-to-definition on symbols in that file, including to stdlib.h
.
Note that there are yellow squiggles from clang-tidy warnings. These drive the developer to improve the code. We’ll produce these later with Bazel as well, for display in the code review.