This is an archive of the discontinued LLVM Phabricator instance.

[clang] Don't create import decls without -fmodules
ClosedPublic

Authored by kadircet on Jun 6 2023, 7:38 AM.

Details

Summary

When modules are disabled, there's no loaded module for these import
decls to point at. This results in crashes when there are modulemap
files but no -fmodules flag (this configuration is used for layering
check violations).

This patch makes sure import declarations are introduced only when
modules are enabled, which makes this case similar to textual headers
(no import decls are created for #include of textual headers from a
modulemap).

Diff Detail

Event Timeline

kadircet created this revision.Jun 6 2023, 7:38 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 6 2023, 7:38 AM
Herald added a subscriber: ChuanqiXu. · View Herald Transcript
kadircet requested review of this revision.Jun 6 2023, 7:38 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 6 2023, 7:38 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

@dblaikie this also updates getSourceDescriptor-crash.cpp, which was making use of generation of implicit import decl. I assumed that test is trying to make sure ImportDecl is represented properly, and incidentally relied on implicit generation of it. LMK if that isn't the case.

kadircet updated this revision to Diff 528863.Jun 6 2023, 7:45 AM
  • Test layering check violation diagnostics
ChuanqiXu added inline comments.Jun 6 2023, 7:15 PM
clang/test/Modules/getSourceDescriptor-crash.cpp
2

It is better to use split-file. You can find the examples by searching in the same dir.

sammccall accepted this revision.Jun 14 2023, 5:57 AM

The internal state when modules are off but layering check is on is really counterintuitive, and clearly not all configurations have been tested :-(
I'm pretty sure this is the right behavior, though.

Just readability nits.

clang/lib/Sema/SemaModule.cpp
638–639

It's been a couple of weeks since we looked at this together, and I already had trouble following the logic here.

I think it would be slightly clearer by:

  • flipping the order (LangOpts.Modules is "more fundanmental"/a prerequisite)
  • moving this var *after* the comment that explains it
  • adding a brief comment for the extra clause.

Something like

// If we are really importing a module (not just checking layering)
// due to an #include in the main file, synthesize an ImportDecl.
bool ShouldAddImport = getLangOpts().Modules && !IsInModuleIncludes;
if (ShouldAddImport) {

I'd also consider inlining+dropping the ShouldAddImport variable, since the comment explains pretty well what the condition represents

all up to you though

This revision is now accepted and ready to land.Jun 14 2023, 5:57 AM
This revision was landed with ongoing or failed builds.Jun 16 2023, 12:26 AM
This revision was automatically updated to reflect the committed changes.