[cfe-dev] clangd, completion in header files

Jan Včelák via cfe-dev cfe-dev at lists.llvm.org
Mon Apr 2 15:03:08 PDT 2018

Thank you all for the responses.

On Mon, Mar 26, 2018 at 10:22 AM, Sam McCall wrote:
> Yeah, this is a known missing feature - guessing the right compile flags for
> headers.
> If you have a compilation database (e.g. compile_commands.json) that
> provides compile commands for headers, then clangd works as expected.
> However most build systems don't provide this information. (Bazel, for one,
> does).

I see. That makes perfect sense. We use cmake which doesn't export
compile commands for headers unfortunately.

> When there's no compile command provided, we fall back to 'clang $filename'.
> Clang treats .h files as C.
> But it's also missing a bunch of other information: include paths, defines
> etc that are likely required to get useful results.
> So I don't think just diverging from clang here will actually help many
> projects (feel free to try this out by editing compile_commands.json - if
> this works for you it'd be good to know).

I have tried adding a compile_commands.json entry for a header with
flags matching the cpp file. The headers were recognized as C++
because the former errors on keywords like namespace were gone but
something was still wrong. Some STL declarations were not found -
std::shared_ptr for instance. Running the compiler command from
terminal showed 'clang-6.0: warning: treating 'c-header' input as
'c++-header' when in C++ mode, this behavior is deprecated
[-Wdeprecated]' which made me try adding '-x c++'. That fixed the
remaining problem but it's strange as the code was already recognized
as C++ and the command contains explicit -std=c++14 option.

> What can we do then? A few ideas:

There are some good suggestions. I'm sure you will find some good
default and I'm looking forward to try it out. I can just confirm that
the heuristic used by YouCompleteMe is quite nice because that's what
I've been using so far. It's not optimal but it works reasonably well.



More information about the cfe-dev mailing list