[clangd] Use flags from open files when opening headers they include

Authored by sammccall on Feb 23 2021, 5:43 PM.


[clangd] Use flags from open files when opening headers they include

Currently our strategy for getting header compile flags is something like:

A) look for flags for the header in compile_commands.json

This basically never works, build systems don't generate this info.

B) try to match to an impl file in compile_commands.json and use its flags

This only (mostly) works if the headers are in the same project.

C) give up and use fallback flags

This kind of works for stdlib in the default configuration, and
otherwise doesn't.

Obviously there are big gaps here.

This patch inserts a new attempt between A and B: if the header is
transitively included by any open file (whether same project or not),
then we use its compile command.

This doesn't make any attempt to solve some related problems:

  • parsing non-self-contained header files in context (importing PP state)
  • using the compile flags of non-opened candidate files found in the index

Fixes https://github.com/clangd/clangd/issues/123
Fixes https://github.com/clangd/clangd/issues/695
See https://github.com/clangd/clangd/issues/519

Differential Revision: https://reviews.llvm.org/D97351