Consumers of the compilation database (compile_commands.json) currently resort to fragile hacks to separate the compiler executable(s) from the compiler arguments in the 'command' entry. Some (most?) consumers only care about the compiler arguments and not about the compiler executable invoked. Separating those from the args turns out to be non-trivial if not impossible done reliable with the current format specification. This is because sometimes there are compiler "wrappers", like ccache, involved.
As result, the consumers try to strip the wrappers to determine the actual compiler arguments. See for example
- https://reviews.llvm.org/D64297
- https://github.com/include-what-you-use/include-what-you-use/issues/789
But this approach is obviously fragile, as one never knows all possible wrappers.
Hence this extends the JSON compliation database format by an optional compiler arguments entry. This change is backwards compatible
{ "directory": "/home/user/code/myproject/build-release", "compiler_arguments: "-Itest/9f86d08@@simple_test@exe -Itest -I../test -I../include -fdiagnostics-color=always -DNDEBUG -pipe -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Wextra -Wpedantic -Werror -std=gnu11 -O3 -pthread -UNDEBUG -MD -MQ 'test/9f86d08@@simple_test@exe/simple_test.c.o' -MF 'test/9f86d08@@simple_test@exe/simple_test.c.o.d' -o 'test/9f86d08@@simple_test@exe/simple_test.c.o' -c ../test/simple_test.c", "command": "ccache cc -Itest/9f86d08@@simple_test@exe -Itest -I../test -I../include -fdiagnostics-color=always -DNDEBUG -pipe -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Wextra -Wpedantic -Werror -std=gnu11 -O3 -pthread -UNDEBUG -MD -MQ 'test/9f86d08@@simple_test@exe/simple_test.c.o' -MF 'test/9f86d08@@simple_test@exe/simple_test.c.o.d' -o 'test/9f86d08@@simple_test@exe/simple_test.c.o' -c ../test/simple_test.c", "file": "../test/simple_test.c" },
Compilation Database consuming tools could check if the format already includes the new entry, and fall back to the (legacy) "command" entry if not.
Compilation Database producing tools, usually build systems like meson, should be easily able to create the new entry for the compiler arguments, since they have knowledge about what is a compiler argument and what is a compiler executable. The problem is that this semantic is lost in the current format of the compilation database.
Related bug: https://bugs.llvm.org/show_bug.cgi?id=45829