Error “Failed to prepare a compiler instance” with avr-gcc

I’m trying to get YouCompleteMe in Vim working. It seems to be using clangd. I compile as follows:

bear -- make

Which generates compile_commands.json:

[
  {
    "arguments": [
      "/usr/bin/avr-gcc",
      "-c",
      "-mmcu=attiny1616",
      "-Iavr-pack/include",
      "-Bavr-pack/gcc/dev/attiny1616",
      "-o",
      "main.o",
      "main.c"
    ],
    "directory": "/home/user/minimal-clang",
    "file": "/home/user/minimal-clang/main.c",
    "output": "/home/user/minimal-clang/main.o"
  }
]

But Vim throws the following error:

I[12:32:48.563] argv[0]: /home/user/usr/share/dotfiles/vim/pack/minpac/start/YouCompleteMe/third_party/ycmd/third_party/clangd/output/bin/clangd
I[12:32:48.563] argv[1]: -query-driver=/usr/bin/avr-gcc
I[12:32:48.563] argv[2]: -header-insertion-decorators=0
I[12:32:48.563] argv[3]: -resource-dir=/home/user/usr/share/dotfiles/vim/pack/minpac/start/YouCompleteMe/third_party/ycmd/third_party/clang/lib/clang/13.0.0
I[12:32:48.563] argv[4]: -limit-results=500
I[12:32:48.563] Starting LSP over stdin/stdout
I[12:32:48.563] <-- initialize(1)
I[12:32:48.564] --> reply:initialize(1) 0 ms
I[12:32:48.566] <-- initialized
I[12:32:48.566] <-- workspace/didChangeConfiguration
I[12:32:48.567] <-- textDocument/didOpen
I[12:32:48.567] Loaded compilation database from /home/user/minimal-clang/compile_commands.json
I[12:32:48.568] Enqueueing 1 commands for indexing
I[12:32:48.577] System includes extractor: successfully executed /usr/bin/avr-gcc
  got includes: "/usr/lib/gcc/avr/5.4.0/include, /usr/lib/gcc/avr/5.4.0/include-fixed, /usr/lib/gcc/avr/5.4.0/../../../avr/include"
  got target: "avr"
I[12:32:48.577] System includes extractor: successfully executed /usr/bin/avr-gcc
  got includes: "/usr/lib/gcc/avr/5.4.0/include, /usr/lib/gcc/avr/5.4.0/include-fixed, /usr/lib/gcc/avr/5.4.0/../../../avr/include"
  got target: "avr"
I[12:32:48.577] ASTWorker building file /home/user/minimal-clang/main.c version 1 with command 
[/home/user/minimal-clang]
/usr/bin/avr-gcc --target=avr -c -mmcu=attiny1616 -Iavr-pack/include -Bavr-pack/gcc/dev/attiny1616 -o main.o -isystem /usr/lib/gcc/avr/5.4.0/include -isystem /usr/lib/gcc/avr/5.4.0/include-fixed -isystem /usr/lib/gcc/avr/5.4.0/../../../avr/include -resource-dir=/home/user/usr/share/dotfiles/vim/pack/minpac/start/YouCompleteMe/third_party/ycmd/third_party/clang/lib/clang/13.0.0 -- /home/marco/minimal-clang/main.c
E[12:32:48.579] Indexing /home/user/minimal-clang/main.c failed: Couldn't build compiler instance
E[12:32:48.579] Could not build a preamble for file /home/user/minimal-clang/main.c version 1: CreateTargetInfo() return null
E[12:32:48.579] Failed to prepare a compiler instance: valid target CPU values are: avr1, avr2, avr25, avr3, avr31, avr35, avr4, avr5, avr51, avr6, avrxmega1, avrxmega2, avrxmega3, avrxmega4, avrxmega5, avrxmega6, avrxmega7, avrtiny, at90s1200, attiny11, attiny12, attiny15, attiny28, at90s2313, at90s2323, at90s2333, at90s2343, attiny22, attiny26, at86rf401, at90s4414, at90s4433, at90s4434, at90s8515, at90c8534, at90s8535, ata5272, attiny13, attiny13a, attiny2313, attiny2313a, attiny24, attiny24a, attiny4313, attiny44, attiny44a, attiny84, attiny84a, attiny25, attiny45, attiny85, attiny261, attiny261a, attiny441, attiny461, attiny461a, attiny841, attiny861, attiny861a, attiny87, attiny43u, attiny48, attiny88, attiny828, at43usb355, at76c711, atmega103, at43usb320, attiny167, at90usb82, at90usb162, ata5505, atmega8u2, atmega16u2, atmega32u2, attiny1634, atmega8, ata6289, atmega8a, ata6285, ata6286, atmega48, atmega48a, atmega48pa, atmega48pb, atmega48p, atmega88, atmega88a, atmega88p, atmega88pa, atmega88pb, atmega8515, atmega8535, atmega8hva, at90pwm1, at90pwm2, at90pwm2b, at90pwm3, at90pwm3b, at90pwm81, ata5790, ata5795, atmega16, atmega16a, atmega161, atmega162, atmega163, atmega164a, atmega164p, atmega164pa, atmega165, atmega165a, atmega165p, atmega165pa, atmega168, atmega168a, atmega168p, atmega168pa, atmega168pb, atmega169, atmega169a, atmega169p, atmega169pa, atmega32, atmega32a, atmega323, atmega324a, atmega324p, atmega324pa, atmega324pb, atmega325, atmega325a, atmega325p, atmega325pa, atmega3250, atmega3250a, atmega3250p, atmega3250pa, atmega328, atmega328p, atmega328pb, atmega329, atmega329a, atmega329p, atmega329pa, atmega3290, atmega3290a, atmega3290p, atmega3290pa, atmega406, atmega64, atmega64a, atmega640, atmega644, atmega644a, atmega644p, atmega644pa, atmega645, atmega645a, atmega645p, atmega649, atmega649a, atmega649p, atmega6450, atmega6450a, atmega6450p, atmega6490, atmega6490a, atmega6490p, atmega64rfr2, atmega644rfr2, atmega16hva, atmega16hva2, atmega16hvb, atmega16hvbrevb, atmega32hvb, atmega32hvbrevb, atmega64hve, at90can32, at90can64, at90pwm161, at90pwm216, at90pwm316, atmega32c1, atmega64c1, atmega16m1, atmega32m1, atmega64m1, atmega16u4, atmega32u4, atmega32u6, at90usb646, at90usb647, at90scr100, at94k, m3000, atmega128, atmega128a, atmega1280, atmega1281, atmega1284, atmega1284p, atmega128rfa1, atmega128rfr2, atmega1284rfr2, at90can128, at90usb1286, at90usb1287, atmega2560, atmega2561, atmega256rfr2, atmega2564rfr2, atxmega16a4, atxmega16a4u, atxmega16c4, atxmega16d4, atxmega32a4, atxmega32a4u, atxmega32c4, atxmega32d4, atxmega32e5, atxmega16e5, atxmega8e5, atxmega32x1, atxmega64a3, atxmega64a3u, atxmega64a4u, atxmega64b1, atxmega64b3, atxmega64c3, atxmega64d3, atxmega64d4, atxmega64a1, atxmega64a1u, atxmega128a3, atxmega128a3u, atxmega128b1, atxmega128b3, atxmega128c3, atxmega128d3, atxmega128d4, atxmega192a3, atxmega192a3u, atxmega192c3, atxmega192d3, atxmega256a3, atxmega256a3u, atxmega256a3b, atxmega256a3bu, atxmega256c3, atxmega256d3, atxmega384c3, atxmega384d3, atxmega128a1, atxmega128a1u, atxmega128a4u, attiny4, attiny5, attiny9, attiny10, attiny20, attiny40, attiny102, attiny104

It seems like my CPU is not supported by clangd. How can this be resolved? Can clangd somehow be instructed to use a different CPU? I’m not interested in the binary, I’m just after the editor support.

Any ideas are much appreciated. If you need more information, please let me know.

clangd version 13.0.0
avr-gcc (GCC) 5.4.0
VIM - Vi IMproved 8.2
bear 3.0.17

You can create a clangd config file and use the a CompileFlags section to remove the offending flags (--target and/or -mmcu) from your compile command with Remove, and if necessary add new flags with Add.

Thanks, that was a nudge in the right direction. I haven’t got it quite working, yet. But clangd is running without errors now. Small steps forward. Thanks a lot for the quick reply.

Ok, for anyone else stumbling over this thread; I don’t want to withhold my solution. The following seems to work:

CompileFlags:
  Remove: --target=avr
  Add: [-I/usr/lib/avr/include, -D__AVR_DEV_LIB_NAME__=tn1616]

Place that in a file called .clangd in the project directory or alternatively $HOME/.config/clangd/config.yaml.