Does Clangd work with Clang plugins?

I followed the example on the tutorial and now my toy plugin can output some customized diagnostic messages. I add the plugin to my build by using set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fplugin=/xxxxx/lib/ToyPlugin.so"), And this works and shows in compile_commands.json.

But it seems that even I use --compile-commands-dir when invoke Clangd, the plugin was not loaded, the diagnostics I wrote not shown, so how can I tell clangd to load a plugin when invoke?

The first thing to check would be that clangd is indeed using a compile command that includes the plugin argument. If you look at the clangd log, when you open a file there should be a line that says something like ASTWorker building file <filename> with command <command>. Do the plugin arguments appear in that command?

Hi, I found the log:

I[11:04:09.838] clangd version 12.0.0 (https://github.com/llvm/llvm-project.git b0b7541aec94b0397c1f64ef2332b5b0bf34af2b)
I[11:04:09.838] PID: 31650
I[11:04:09.838] Working directory: /home/liu/source/helloworld
I[11:04:09.838] argv[0]: /home/liu/source/llvm-project/build-debug/bin/clangd
I[11:04:09.838] argv[1]: --compile-commands-dir=/home/liu/source/helloworld/cmake-build-debug/
I[11:04:09.838] Starting LSP over stdin/stdout
I[11:04:09.843] <-- initialize(0)
I[11:04:09.843] Client supports legacy semanticHighlights notification and standard semanticTokens request, choosing the latter (no notifications).
I[11:04:09.844] --> reply:initialize(0) 0 ms
I[11:04:09.886] <-- initialized
I[11:04:09.887] <-- textDocument/didOpen
I[11:04:09.888] Loaded compilation database from /home/liu/source/helloworld/cmake-build-debug/
I[11:04:09.888] --> window/workDoneProgress/create(0)
I[11:04:09.888] Enqueueing 1 commands for indexing
I[11:04:09.889] ASTWorker building file /home/liu/source/helloworld/main.cpp version 1 with command 
[/home/liu/source/helloworld/cmake-build-debug]
/home/liu/source/llvm-project/build-debug/bin/clang++ --driver-mode=g++ -fplugin=/home/liu/source/llvm-project/build-debug/lib/CallSuperAttr.so -g -o CMakeFiles/helloworld.dir/main.cpp.o -c /home/liu/source/helloworld/main.cpp -fsyntax-only -resource-dir=/home/liu/source/llvm-project/build-debug/lib/clang/12.0.0
I[11:04:09.892] --> textDocument/clangd.fileStatus
I[11:04:09.897] <-- textDocument/documentSymbol(1)
I[11:04:09.897] <-- textDocument/codeAction(2)
I[11:04:09.897] <-- textDocument/documentLink(3)
I[11:04:09.949] --> textDocument/publishDiagnostics
I[11:04:09.950] --> reply:textDocument/documentSymbol(1) 53 ms
I[11:04:09.950] --> reply:textDocument/codeAction(2) 53 ms
I[11:04:09.950] --> reply:textDocument/documentLink(3) 53 ms
I[11:04:09.950] --> textDocument/clangd.fileStatus
I[11:04:09.954] <-- reply(0)
I[11:04:09.954] --> $/progress
I[11:04:09.955] --> $/progress
I[11:04:10.194] <-- textDocument/semanticTokens/full(4)
I[11:04:10.195] --> reply:textDocument/semanticTokens/full(4) 0 ms
I[11:04:10.195] --> textDocument/clangd.fileStatus
I[11:04:10.242] <-- textDocument/codeAction(5)
I[11:04:10.242] --> reply:textDocument/codeAction(5) 0 ms
I[11:04:10.243] --> textDocument/clangd.fileStatus
I[11:04:11.564] <-- textDocument/codeAction(6)
I[11:04:11.565] --> reply:textDocument/codeAction(6) 0 ms
I[11:04:11.565] --> textDocument/clangd.fileStatus

Look like the ASTWorker get my compile command right, but my custom diagnostic not shown.

Thanks. Based on this, I don’t think you’re doing anything wrong in terms of configuration. You may be running into a bug or limitation in clangd. I would encourage you to file a clangd issue, ideally with a sample plugin attached, so this can be investigated further.

I did a fully debugging and afraid that except clang itself, other tools, not only clangd, but also clang-check, won’t load plugins. I’v uploaded a patch to fix this.