clangd / clangd

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

Show complete include chain for diagnostics in header files #1392

Open FStefanni opened 1 year ago

FStefanni commented 1 year ago

Hi,

not sure if this is where to report this feature request. Basically, when there is an error into an included file, the message reports something like this:

myheader.hpp
/path/include/coreino.hpp

In included file: <error description>
otherHeader.h(579, 5): Error occurred here
No quick fixes available

This make the error not so trivial to fix, since it only report the header name where the error originates, but not all the includes hierarchy to arrive to that include file.

For example, this would be more useful:

myheader.hpp
/path/include/coreino.hpp

In included file: <error description>
otherHeader.h(579, 5): Error occurred here
included by: another0.h(23,5)
included by: another1.h(11,4)
included by: another3.h(57,9)
myheader.hpp(2,1)
No quick fixes available

This is especially useful in cases where we are maybe cross-compiling, or in complex setups where we have to specify to clangd custom include paths, and the error is basically that clangd comes up to see as included the wrong file (i.e., in the example exist two different otherHeader.h files, and the one causing the error is the wrong one).

Regards

HighCommander4 commented 1 year ago

Agreed, this is important context for understanding a diagnostic. I would expect clangd to produce a "note" of the form "In file included from <filename>" for every element of the include chain, similar to how the compiler would display the diagnostic on the command line.

HighCommander4 commented 1 year ago

(Transferred to the clangd repo as the issue is on the server side. The textDocument/publishDiagnostics notification does not currently contain information about the intermediate elements of the include chain.)

yeswalrus commented 1 year ago

Came here to report basically exactly this, except in my situation it's even worse:

Clangd points to the top level include as being where the problem is, so I have no idea where the actual error is!

IE, in main.cpp:

include "header_with_nested_error.h",

Clangd will report an error at line 1 "in file included from main.cpp".

To diagnose the error, I'm forced to extract the commandline and run under clang, which more helpfully reports the entire chain of errors.

HighCommander4 commented 5 months ago

Some notes on this for future reference:

Note that the include chain entries are not represented in the clang API as "notes" (which are distinct diagnostics with the diagnostic level Note), they're just lines printed as an implementation detail of printing a main diagnostic. To get them to show up in clients, the clangd DiagnosticConsumer will likely need to turn them into distinct diagnostics with LSP DiagnosticSeverity.Information.