clangd language Server

Because NativeOS uses LLVM as a toolchain, it is very easy to integrate your text editor with clangd if it supports the language server protocol.

clangd is able to detect how to compile regular C applications (such as using the default include dir like /usr/include). But the NativeOS kernel is not a regular application, as you might have guess. The Makefile changes a lot of flags and adds a lot of paths to the include path. So it is best to override the flags and use a compilation database.

The simplest way to add a compilation database is to use a tool such as Bear. It will intercept calls to clang, extract the parameters, and build a JSON file that explains how to build the specified file. This is added to a file called compile_commands.json, which is understood by clangd and affects the way it configures the language server for each file.

How to build a compile_commands.json

Then use kcons to build a profile:

$ tools/kcons conf/I386

Then use bear to clean and build the kernel. Note you must clean and build. Otherwise, Bear will only pick the flags for the modified files and it will render clangd useless. You will have to do this every time a new file is added to the project.

$ bear -- make -C compile/I386 clean kernel

Then test with your text editor that things are working in regards to the language server. You should see that writing . after a struct variable or -> after a struct pointer causes the text editor to autocomplete fields of the struct. You should see that it is able to pick the type of each variable. You should see that writing the name of a function autocompletes the list of parameters.

Updated by danirod about 2 years ago ยท 1 revisions