This is an archive of the discontinued LLVM Phabricator instance.

[clang][modules] Always let Obj-C decls query the ExternalASTSource for complete redecl chains
Needs ReviewPublic

Authored by teemperor on Oct 21 2021, 9:48 AM.

Details

Summary

ObjCInterfaceDecls and ObjCProtocolDecls currently have a flag that indicates whether they
need to query the ExternalASTSource to complete their redeclaration chain. This flag is currently
only set when LangOpts.Modules == true. The idea behind that seems to be that only with
LangOpts.Modules == true we have a chance to find a definition for an existing declaration via the
ExternalASTSource, so we optimize the non-modules case by avoiding the work of looking for an
external definition.

The idea that LangOpts.Modules implies that the ExternalASTSource can provide a definition
doesn't work within LLDB where we are not always setting the Modules flag but we always have an
ExternalASTSource that can complete the redeclaration chain (and provide a definition).

This patch sets the flag whenever we have any valid ExternalASTSource so that the ExternalASTSource
in LLDB is also queried for definitions. In Clang this patch maintains the current behaviour for parsing
with enabled and disabled modules. The only behaviour change in Clang is that we now also query the
ExternalASTSource for Obj-C interface/protocol definitions when using (chained) PCHs.

Diff Detail

Event Timeline

teemperor requested review of this revision.Oct 21 2021, 9:48 AM
teemperor created this revision.
  • An alternative implementation would be to extend the ExternalASTSource interface with something such as bool CanCompleteRedeclChains() const that returns true/false whether we need to query the ExternalASTSource. That would also allow us to preserve the old behaviour for the PCH case.
  • I haven't done any benchmarks on how much (if at all) this slows down compiling with a PCH. Given that the same slowdown happens by just passing -fmodules without importing any modules, I don't think there is any meaningful slowdown.
  • No Clang test yet as I believe this can only be tested via a unit test where I add a custom ExternalASTSource which is always a bunch of boilerplate. I'll add one later if there are no objections against the current direction of the patch.
  • See also D101950 where LLDB now implements CompleteRedeclChain but doesn't receive any calls for Obj-C decls without this patch.

This seems reasonable to me. I have some questions though:

  • Could LLDB potentially enable modules to get the desired behavior?
  • Can't we query the CompilerInvocation (FrontendOptions?) to determine whether we have PCH enabled and preserve the current semantics if so?
  • Can you provide a test that demonstrates the LLDB use-case?

I'm not that familiar with redeclaration chain deserialization, so I'd be more comfortable if someone else took a look as well. @rsmith and @vsapsai, do you have opinion on this?