[cfe-dev] clangd, completion in header files

Sam McCall via cfe-dev cfe-dev at lists.llvm.org
Mon Apr 9 10:00:58 PDT 2018

Just landed r329582 which has does some guessing when there's no compile
command available.
Please try it out!

Related changes I'd still like to make:
 - parse *.h as obj-c++ when there's no compilation database at all (out
for review)
 - inject a diagnostic when we're using a fallback/guessed command for

On Tue, Apr 3, 2018 at 12:03 AM Jan Včelák <jv at fcelda.cz> wrote:

> 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.
> Cheers,
> Jan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180409/d1a57dd2/attachment.html>

More information about the cfe-dev mailing list