This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Adjust compile flags so they work when applied to other file(type)s.
ClosedPublic

Authored by sammccall on Dec 22 2021, 8:01 AM.

Details

Summary

It's reasonable to want to use the command from one file to compile another.
In particular, the command from a translation unit to parse a related header:

{"file": "foo.h", "command": "clang foo.cpp"}

This is largely what InterpolatingCompilationDatabase tries to do.
To do this correctly can require nontrivial changes to the argv, because the
file extension affects semantics. e.g. here we must add "-x c++header".

When external tools compile commands for different files, we should apply the
same adjustments. This is better than telling people to "fix their tools":

  • simple e.g. python scripts shouldn't have to interpret clang argv
  • this is a good way to represent the intent "parse header X in the context of file Y", which can work even if X is not self-contained. clangd does not support this today, but some other tools do, and we may one day.

This issue is discussed in https://github.com/clangd/clangd/issues/519

Diff Detail

Event Timeline

sammccall created this revision.Dec 22 2021, 8:01 AM
sammccall requested review of this revision.Dec 22 2021, 8:01 AM
kadircet accepted this revision.Jan 4 2022, 1:26 AM

thanks, lgtm!

clang-tools-extra/clangd/CompileCommands.cpp
287

maybe perform this before inserting -- Filename and do it once afterwards?

295

agreed it already inserts -- in some cases today, i think i was trying to be conservative at the time. i don't see any reason for not doing that all the time (possibly apart from test failures and tons of new places that don't expect -- in the flags).

This revision is now accepted and ready to land.Jan 4 2022, 1:26 AM