YCM is code-completion engine plugin for vim.
It uses clangd to provide completions and highlight syntax errors.
Because RTEMS is cross complied, a bit of extra work is required to get things working with clangd.
compile_commands.json
First clangd will need the compiler flag. The easiest way to pass these into clang through ycm is to place a compile_commands.json at the top of the project. As pointed out by many others bear can be used to easily generate one. (personal thanks to prashant on discord for showing me the light).
bear -- ./waf
.clangd
when targeting the erc32 bsp, waf uses the -mcpu-cypress flag. This caused ycm’s clang to crash.
To resolve this add a .clangd file to the top of your project further tweak clang’s runtime.
Here is the .clangd that I used when exploring the rtems examples repo. The goal was to remove the mcpu flag and force add the assumed standard included path.
(I’ve snipped the absolute paths for public viewing)
CompileFlags:
CompilationDatabase: /------/work/rtems/rtems-examples
Compiler: /------/work/rtems/quick-start/rtems/6/bin/sparc-rtems6-gcc
BuiltinHeaders: QueryDriver
Remove:
- -mcpu=*
Add:
- -isystem
- /-----/work/rtems/quick-start/rtems/6/lib/gcc/sparc-rtems6/13.3.0/include
vimrc
Lastly clangd requires an additional flag when it is invoked by ycm to switch into a cross compiler mode. this can be done in your _vimrc.
(again modified absolute paths)
let g:ycm_clangd_args = [
\ '--query-driver=/-----/work/rtems/quick-start/rtems/6/bin/*',
\ ]
Useful Clangd Debugging
When figuring this out I found it very helpful to just invoke clangd directly bypassing ycm. Be sure to use the clang held by ycm i.e. the one you built when you installed ycm.
# example running clangd on a file in the rtems example repo
~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clangd/output/bin/clangd --check=classic_api/triple_period/int.c --compile-commands-dir=$PWD --log=verbose --query-driver=$PATH_TO_RTEMS_BIN_same_as_vimrv
Look for unknown flag errors and include directories being ignored.
Lastly I believe vscode does something similar for its c syntax engine, and this maybe of some help there.
If there are general vim and ycm questions I’ll be happy to help.