clangd / clangd

clangd language server
https://clangd.llvm.org
Apache License 2.0
1.51k stars 63 forks source link

Warn users if a source file is missing a compile command and clangd has to infer it from another source file #1505

Open shan-weiqiang opened 1 year ago

shan-weiqiang commented 1 year ago

clangd infer compile command for a source file from anther source file.

I[09:59:03.144] ASTWorker building file /home/shan/playground/dds/xway_os_ide_toolchain/demo/vsomeip_demo/client-example.cpp version 1 with command inferred from /home/shan/playground/dds/xway_os_ide_toolchain/demo/api/dag_demo/swc/sw_component_api.cpp [/home/shan/playground/dds/xway_os_ide_toolchain/demo/build/app/dag_demo/swc] /usr/bin/c++ --driver-mode=g++ -Ddag_demo_swc_user_EXPORTS -I/home/shan/playground/dds/xway_os_ide_toolchain/demo/./external_type -I/home/shan/playground/dds/xway_os_ide_toolchain/demo/static_code -I/home/shan/playground/dds/xway_os_ide_toolchain/demo/api -I/home/shan/playground/dds/xway_os_ide_toolchain/demo/app/dag_demo/swc/. -g -O3 -Wall -Werror -fPIC -fno-strict-aliasing -fPIC -Wall -c -std=gnu++14 -I/home/shan/playground/dds/xway_os_ide_toolchain/xway_sdk/include -I/home/shan/playground/dds/xway_os_ide_toolchain/code_engine/static_code -I/home/shan/playground/dds/xway_os_ide_toolchain/demo/build/middleware -isystem/home/shan/.local/include -resource-dir=/home/shan/clangd/clangd_15.0.3/lib/clang/15.0.3 -- /home/shan/playground/dds/xway_os_ide_toolchain/demo/vsomeip_demo/client-example.cpp

They are totally two different files. What i learned from clangd doc is that clangd will infer command for header files, NOT for source files:

If no compilation database is found, a very simple command like clang foo.cc is used. For a real project this will often fail to find #includes, but it allows clangd to work on toy examples without configuration.

This kinds of heuristics are definitely not what i want. What happens under the hood?

Output of clangd --version: clangd version 15.0.3 (https://github.com/llvm/llvm-project 4a2c05b05ed07f1f620e94f6524a8b4b2760a0b1)

Editor/LSP plugin: vscode

Operating system: x86-64 Ubuntu

HighCommander4 commented 1 year ago

Generally, a compile_commands.json file will contain an entry for every source file in a project, but not header files.

For this reason, clangd's "command inference" feature is usually needed only for editing header files.

However, if a source file's entry is missing, clangd will infer a command for it from another source file, as this is more likely to produce a useful result than using the fallback command (which e.g. will not contain any -I flags).

So, in your case: does the project's compile_commands.json contain an entry for /home/shan/playground/dds/xway_os_ide_toolchain/demo/vsomeip_demo/client-example.cpp?

shan-weiqiang commented 1 year ago

No, the compile_commands.json contains entry for /home/shan/playground/dds/xway_os_ide_toolchain/demo/api/dag_demo/swc/sw_component_api.cpp , not for /home/shan/playground/dds/xway_os_ide_toolchain/demo/vsomeip_demo/client-example.cpp.

My project tree illustration: /home/shan/playground/dds/xway_os_ide_toolchain/demo/api/dag_demo/swc/sw_component_api.cpp /home/shan/playground/dds/xway_os_ide_toolchain/demo/vsomeip_demo/client-example.cpp /home/shan/playground/dds/xway_os_ide_toolchain/demo/build/compile_commands.json

client-example.cpp file does not participate building.

So, if clangd find compile_commands.json file but can not retrieve the entry of exact match, it will do heuristics like this?

HighCommander4 commented 1 year ago

So, if clangd find compile_commands.json file but can not retrieve the entry of exact match, it will do heuristics like this?

Right, as that seems better than the alternative of using the fallback command which will contain no -I flags.

Is there something different that you expect / would like clangd to do when opening a source file whose entry is not present in the compile_commands.json?

shan-weiqiang commented 1 year ago

I would follow the strict principle, do not try to provide info that might be wrong. Just let the user know that clangd do not have enough infomation, use the fallback clang sourc.cpp. Or at least, give a reminder that the file is interpreted using heristics.

For so many times, people struggle to debug some problem and finally find that they are troubled by misleading infomation.

HighCommander4 commented 1 year ago

Or at least, give a reminder that the file is interpreted using heristics.

Thanks; I updated the title of the issue to reflect this suggestion.