This is an archive of the discontinued LLVM Phabricator instance.

[Driver] Make "upgrade" of -include to include-pch optional; disable in clangd
ClosedPublic

Authored by sammccall on May 4 2022, 4:15 PM.

Details

Summary

If clang is passed "-include foo.h", it will rewrite to "-include-pch foo.h.pch"
before passing it to cc1, if foo.h.pch exists.

Existence is checked, but validity is not. This is probably a reasonable
assumption for the compiler itself, but not for clang-based tools where the
actual compiler may be a different version of clang, or even GCC.
In the end, we lose our -include, we gain a -include-pch that can't be used,
and the file often fails to parse.

I would like to turn this off for all non-clang invocations (i.e.
createInvocationFromCommandLine), but we have explicit tests of this behavior
for libclang and I can't work out the implications of changing it.

Instead this patch:

  • makes it optional in the driver, default on (no change)
  • makes it optional in createInvocationFromCommandLine, default on (no change)
  • changes driver to do IO through the VFS so it can be tested
  • tests the option
  • turns the option off in clangd where the problem was reported

Subsequent patches should make libclang opt in explicitly and flip the default
for all other tools. It's probably also time to extract an options struct
for createInvocationFromCommandLine.

Fixes https://github.com/clangd/clangd/issues/856
Fixes https://github.com/clangd/vscode-clangd/issues/324

Diff Detail

Event Timeline

sammccall created this revision.May 4 2022, 4:15 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 4 2022, 4:15 PM
sammccall requested review of this revision.May 4 2022, 4:15 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptMay 4 2022, 4:15 PM

Kadir, sending this your way as it looks like you investigated #856.

I do plan to flip the global default, but not for libclang and friends :-( I feel like a coward.

kadircet accepted this revision.May 5 2022, 6:02 AM

thanks, LGTM!

as discussed offline, let's move forward with what you proposed (i.e. this patch, and then flip the default while making libclang opt-in)

clang/include/clang/Driver/Driver.h
275

nit: reflow the comment

clang/lib/Frontend/CreateInvocationFromCommandLine.cpp
51

this comment feels a little bit outdated. we're definitely not turning this off and also probing is done in VFS now.
So maybe drop this and rather have a comment in header, pointing out possible failure modes due to version mismatch when this is turned on?

This revision is now accepted and ready to land.May 5 2022, 6:02 AM