clangd / vscode-clangd

Visual Studio Code extension for clangd
https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd
MIT License
629 stars 106 forks source link

Headers mistreated as cpp files #566

Open planetoryd opened 9 months ago

planetoryd commented 9 months ago
I[22:54:45.625] clangd version 16.0.6
I[22:54:45.625] Features: linux
I[22:54:45.625] PID: 1034753
I[22:54:45.625] Working directory: /home/noume/c-algos
I[22:54:45.625] argv[0]: /usr/bin/clangd
I[22:54:45.625] argv[1]: --enable-config
I[22:54:45.625] Starting LSP over stdin/stdout
I[22:54:45.626] <-- initialize(0)
I[22:54:45.627] --> reply:initialize(0) 1 ms
I[22:54:45.628] <-- initialized
I[22:54:45.633] <-- textDocument/didOpen
I[22:54:45.633] <-- textDocument/didOpen
I[22:54:45.633] --> textDocument/publishDiagnostics
I[22:54:45.634] Loaded compilation database from /home/noume/c-algos/compile_flags.txt
I[22:54:45.634] Failed to find compilation database for /usr/include/sys/cdefs.h
I[22:54:45.634] ASTWorker building file /home/noume/c-algos/algorithms/basic _client_server_tcp/client/TCP_receiver.c version 1 with command 
[/home/noume/c-algos]
/usr/bin/clang -xc -xc-header -xc -Wall -resource-dir=/usr/lib/clang/16 -- "/home/noume/c-algos/algorithms/basic _client_server_tcp/client/TCP_receiver.c"
I[22:54:45.634] ASTWorker building file /usr/include/sys/cdefs.h version 1 with command clangd fallback
[/usr/include/sys]
/usr/bin/clang -xobjective-c++-header -resource-dir=/usr/lib/clang/16 -- /usr/include/sys/cdefs.h
I[22:54:45.634] <-- textDocument/documentSymbol(1)
I[22:54:45.635] <-- textDocument/codeAction(2)
I[22:54:45.635] --> textDocument/clangd.fileStatus
I[22:54:45.635] --> textDocument/clangd.fileStatus
I[22:54:45.639] <-- textDocument/documentLink(3)
I[22:54:45.640] <-- textDocument/inlayHint(4)
I[22:54:45.648] Indexing c++17 standard library in the context of /usr/include/sys/cdefs.h
I[22:54:45.650] Built preamble of size 245752 for file /usr/include/sys/cdefs.h version 1 in 0.01 seconds
I[22:54:45.650] --> workspace/semanticTokens/refresh(0)
I[22:54:45.651] <-- reply(0)
I[22:54:45.651] <-- textDocument/semanticTokens/full(5)
I[22:54:45.674] Indexing c17 standard library in the context of /home/noume/c-algos/algorithms/basic _client_server_tcp/client/TCP_receiver.c
I[22:54:45.696] Built preamble of size 578208 for file /home/noume/c-algos/algorithms/basic _client_server_tcp/client/TCP_receiver.c version 1 in 0.06 seconds
I[22:54:45.696] --> workspace/semanticTokens/refresh(1)
I[22:54:45.696] --> textDocument/clangd.fileStatus
I[22:54:45.697] <-- reply(1)
I[22:54:45.705] --> textDocument/publishDiagnostics
I[22:54:45.705] --> textDocument/clangd.fileStatus
I[22:54:45.731] --> textDocument/publishDiagnostics
I[22:54:45.731] --> reply:textDocument/documentSymbol(1) 96 ms
I[22:54:45.731] --> reply:textDocument/codeAction(2) 96 ms
I[22:54:45.731] --> reply:textDocument/documentLink(3) 91 ms
I[22:54:45.731] --> reply:textDocument/inlayHint(4) 91 ms
I[22:54:45.732] --> reply:textDocument/semanticTokens/full(5) 80 ms
I[22:54:45.733] --> textDocument/clangd.fileStatus
I[22:54:45.771] Indexed c17 standard library: 2839 symbols, 0 filtered
I[22:54:45.935] <-- textDocument/foldingRange(6)
I[22:54:45.936] --> reply:textDocument/foldingRange(6) 1 ms
I[22:54:46.187] <-- textDocument/semanticTokens/full/delta(7)
I[22:54:46.190] --> reply:textDocument/semanticTokens/full/delta(7) 2 ms
I[22:54:46.190] --> textDocument/clangd.fileStatus
I[22:54:46.781] Indexed c++17 standard library (incomplete due to errors): 8459 symbols, 1671 filtered

I am in a directory with only individual .c files because it's https://github.com/AllAlgorithms/c

image

planetoryd commented 9 months ago

image

planetoryd commented 9 months ago

image

A fork of yours seeems to work https://open-vsx.org/vscode/item?itemName=EclipseSource.vscode-clangd-multi-project

HighCommander4 commented 9 months ago

I'm sorry, it's not clear to me from your screenshots what the problem is.

Can you please describe what you're expecting to happen, and what happens that's different from what you expect?

planetoryd commented 9 months ago

The header files were interpreted as c++ files, but I am actually looking at C code.

I have configured it to use -xc and -xc-header in the config files in project root, but it doesn't work.

The version of vscode plugin in this repo doesn't work as expected, but I found a fork that works.

HighCommander4 commented 9 months ago

Ah, I see.

The reason the config file in the project root does not apply to system headers is that project config files are scoped to the directory they're located in (i.e. they will only apply to files in that directory tree).

One way to make this work is to use the --compile-commands-dir command line option to clangd with the project directory as argument (assuming the directory contains a compile_commands.json file). Clangd will then use this compile_commands.json file for any file opened by the clangd instance, even if it's outside the directory. If the commands in this file contain -xc, then opened headers will be parsed as C headers.

planetoryd commented 9 months ago

I just generated one item of compile_commands with bear (It's compiled with cc directly). Does it mean i have to run it for every file. In any case it feels awkward

HighCommander4 commented 9 months ago

Ah, I overlooked you were using compile_flags.txt.

I checked, --compile-commands-dir actually works with compile_flags.txt as well. So, just putting -xc into compile_flags.txt and using --compile-commands-dir should do it.