[PATCH] D132502: [clang][modules] Consider M affecting after mapping M.Private to M_Private

Jan Svoboda via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 23 14:28:37 PDT 2022


jansvoboda11 created this revision.
jansvoboda11 added reviewers: Bigcheese, benlangmuir.
Herald added a project: All.
jansvoboda11 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Wwhen Clang encounters `@import M.Private` during implicit build, it precompiles module `M` and looks through its submodules. If the `Private` submodule is not found, Clang assumes `@import M_Private`. In the dependency scanner, we don't capture the dependency on `M`, since
it's not imported. It's an affecting module, though: compilation of the import statement will fail when implicit modules are disabled and `M` is not precompiled and explicitly provided. This patch fixes that.

Depends on D132430 <https://reviews.llvm.org/D132430>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D132502

Files:
  clang/include/clang/Lex/Preprocessor.h
  clang/lib/Frontend/CompilerInstance.cpp
  clang/lib/Lex/PPDirectives.cpp
  clang/test/Modules/implicit-map-dot-private.m


Index: clang/test/Modules/implicit-map-dot-private.m
===================================================================
--- clang/test/Modules/implicit-map-dot-private.m
+++ clang/test/Modules/implicit-map-dot-private.m
@@ -1,5 +1,17 @@
 // RUN: rm -rf %t
-// RUN: %clang_cc1 -verify -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -F %S/Inputs/implicit-private-canonical -emit-pch -o %t-A.pch %s -Wprivate-module
+
+// Implicit modules.
+// RUN: %clang_cc1 -verify -fmodules -fimplicit-module-maps -fmodules-cache-path=%t \
+// RUN:   -F %S/Inputs/implicit-private-canonical -fsyntax-only %s -Wprivate-module
+
+// Explicit modules.
+// RUN: %clang_cc1 -x objective-c -fmodules -emit-module -fmodule-name=A -o %t/A.pcm \
+// RUN:   %S/Inputs/implicit-private-canonical/A.framework/Modules/module.modulemap -Wprivate-module
+// RUN: %clang_cc1 -x objective-c -fmodules -emit-module -fmodule-name=A_Private -o %t/A_Private.pcm \
+// RUN:   %S/Inputs/implicit-private-canonical/A.framework/Modules/module.private.modulemap -Wprivate-module
+// RUN: %clang_cc1 -verify -fmodules -fimplicit-module-maps -fno-implicit-modules \
+// RUN:   -fmodule-file=A=%t/A.pcm -fmodule-file=A_Private=%t/A_Private.pcm \
+// RUN:   -F %S/Inputs/implicit-private-canonical -fsyntax-only %s -Wprivate-module
 
 #ifndef HEADER
 #define HEADER
Index: clang/lib/Lex/PPDirectives.cpp
===================================================================
--- clang/lib/Lex/PPDirectives.cpp
+++ clang/lib/Lex/PPDirectives.cpp
@@ -2281,13 +2281,8 @@
     if (Imported) {
       Action = Import;
     } else if (Imported.isMissingExpected()) {
-      Module *M = static_cast<Module *>(Imported)->getTopLevelModule();
-      if (!BuildingSubmoduleStack.empty()) {
-        if (Imported != BuildingSubmoduleStack.back().M)
-          BuildingSubmoduleStack.back().M->AffectingModules.insert(M);
-      } else {
-        AffectingModules.insert(M);
-      }
+      markModuleAsAffecting(
+          static_cast<Module *>(Imported)->getTopLevelModule());
       // We failed to find a submodule that we assumed would exist (because it
       // was in the directory of an umbrella header, for instance), but no
       // actual module containing it exists (because the umbrella header is
Index: clang/lib/Frontend/CompilerInstance.cpp
===================================================================
--- clang/lib/Frontend/CompilerInstance.cpp
+++ clang/lib/Frontend/CompilerInstance.cpp
@@ -2014,8 +2014,7 @@
     // match Foo_Private and emit a warning asking for the user to write
     // @import Foo_Private instead. FIXME: remove this when existing clients
     // migrate off of Foo.Private syntax.
-    if (!Sub && PP->getLangOpts().ImplicitModules && Name == "Private" &&
-        Module == Module->getTopLevelModule()) {
+    if (!Sub && Name == "Private" && Module == Module->getTopLevelModule()) {
       SmallString<128> PrivateModule(Module->Name);
       PrivateModule.append("_Private");
 
@@ -2029,6 +2028,7 @@
         Sub = loadModule(ImportLoc, PrivPath, Visibility, IsInclusionDirective);
       if (Sub) {
         MapPrivateSubModToTopLevel = true;
+        getPreprocessor().markModuleAsAffecting(Module);
         if (!getDiagnostics().isIgnored(
                 diag::warn_no_priv_submodule_use_toplevel, ImportLoc)) {
           getDiagnostics().Report(Path[I].second,
Index: clang/include/clang/Lex/Preprocessor.h
===================================================================
--- clang/include/clang/Lex/Preprocessor.h
+++ clang/include/clang/Lex/Preprocessor.h
@@ -1335,6 +1335,16 @@
 
   /// \}
 
+  /// Mark the given module as affecting the current module or translation unit.
+  void markModuleAsAffecting(Module *M) {
+    if (!BuildingSubmoduleStack.empty()) {
+      if (M != BuildingSubmoduleStack.back().M)
+        BuildingSubmoduleStack.back().M->AffectingModules.insert(M);
+    } else {
+      AffectingModules.insert(M);
+    }
+  }
+
   /// Get the set of top-level modules that affected preprocessing, but were not
   /// imported.
   const llvm::SmallSetVector<Module *, 2> &getAffectingModules() const {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132502.454964.patch
Type: text/x-patch
Size: 4135 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220823/481072ce/attachment-0001.bin>


More information about the cfe-commits mailing list