https://github.com/jansvoboda11 created 
https://github.com/llvm/llvm-project/pull/179610

Previously, `-fmodules-single-module-parse-mode` only prevented module 
compilation/loading when initiated from an `#include` or `#import` directive. 
This PR does the same for `@import`, `#pragma clang module import` and `#pragma 
clang module load`. This is done by sinking the logic down into 
`CompilerInstance::loadModule()`.

>From 4556d185bd5629a8116a446b83791d45ee6a40b9 Mon Sep 17 00:00:00 2001
From: Jan Svoboda <[email protected]>
Date: Tue, 3 Feb 2026 21:28:07 -0800
Subject: [PATCH] [clang][modules] Support every import syntax in
 single-module-parse-mode

Previously, `-fmodules-single-module-parse-mode` only prevented module 
compilation/loading when initiated from an `#include` or `#import` directive. 
This PR does the same for `@import`, `#pragma clang module import` and `#pragma 
clang module load`. This is done by sinking the logic down into 
`CompilerInstance::loadModule()`.
---
 clang/lib/Frontend/CompilerInstance.cpp       | 16 +++++++++
 clang/lib/Lex/PPDirectives.cpp                |  9 ++---
 .../single-module-parse-mode-compiles.m       | 36 +++++++++++++++++++
 3 files changed, 54 insertions(+), 7 deletions(-)
 create mode 100644 clang/test/Modules/single-module-parse-mode-compiles.m

diff --git a/clang/lib/Frontend/CompilerInstance.cpp 
b/clang/lib/Frontend/CompilerInstance.cpp
index ae17e5467c712..6f30ff4ba7346 100644
--- a/clang/lib/Frontend/CompilerInstance.cpp
+++ b/clang/lib/Frontend/CompilerInstance.cpp
@@ -1984,6 +1984,22 @@ CompilerInstance::loadModule(SourceLocation ImportLoc,
     // * `Preprocessor::HandleHeaderIncludeOrImport` will never call this
     //   function as the `#include` or `#import` is textual.
 
+    MM.cacheModuleLoad(*Path[0].getIdentifierInfo(), Module);
+  } else if (getPreprocessorOpts().SingleModuleParseMode) {
+    // This mimics how findOrCompileModuleAndReadAST() find the module.
+    Module = getPreprocessor().getHeaderSearchInfo().lookupModule(
+        ModuleName, ImportLoc, true, !IsInclusionDirective);
+    if (Module) {
+      // Mark the module and its submodules as if they were loaded from a PCM.
+      std::vector Worklist{Module};
+      while (!Worklist.empty()) {
+        auto *M = Worklist.back();
+        Worklist.pop_back();
+        M->IsFromModuleFile = true;
+        for (auto *SubM : M->submodules())
+          Worklist.push_back(SubM);
+      }
+    }
     MM.cacheModuleLoad(*Path[0].getIdentifierInfo(), Module);
   } else {
     SourceLocation ModuleNameEndLoc = Path.back().getLoc().getLocWithOffset(
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 8d4c9c49f756a..c4b9f9e61c876 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -2494,15 +2494,10 @@ Preprocessor::ImportAction 
Preprocessor::HandleHeaderIncludeOrImport(
       (getLangOpts().CPlusPlusModules || getLangOpts().Modules) &&
       ModuleToImport && !ModuleToImport->isHeaderUnit();
 
-  if (MaybeTranslateInclude && (UsableHeaderUnit || UsableClangHeaderModule) &&
-      PPOpts.SingleModuleParseMode) {
-    Action = IncludeLimitReached;
-  }
   // Determine whether we should try to import the module for this #include, if
   // there is one. Don't do so if precompiled module support is disabled or we
   // are processing this module textually (because we're building the module).
-  else if (MaybeTranslateInclude &&
-           (UsableHeaderUnit || UsableClangHeaderModule)) {
+  if (MaybeTranslateInclude && (UsableHeaderUnit || UsableClangHeaderModule)) {
     // If this include corresponds to a module but that module is
     // unavailable, diagnose the situation and bail out.
     // FIXME: Remove this; loadModule does the same check (but produces
@@ -2539,7 +2534,7 @@ Preprocessor::ImportAction 
Preprocessor::HandleHeaderIncludeOrImport(
            "the imported module is different than the suggested one");
 
     if (Imported) {
-      Action = Import;
+      Action = PPOpts.SingleModuleParseMode ? Skip : Import;
     } else if (Imported.isMissingExpected()) {
       markClangModuleAsAffecting(
           static_cast<Module *>(Imported)->getTopLevelModule());
diff --git a/clang/test/Modules/single-module-parse-mode-compiles.m 
b/clang/test/Modules/single-module-parse-mode-compiles.m
new file mode 100644
index 0000000000000..8e9b92c86fe61
--- /dev/null
+++ b/clang/test/Modules/single-module-parse-mode-compiles.m
@@ -0,0 +1,36 @@
+// This test checks that with -fmodules-single-module-parse-mode, no modules 
get
+// compiled into PCM files from any of the import syntax Clang supports.
+
+// RUN: rm -rf %t
+// RUN: split-file %s %t
+// RUN: mkdir %t/cache
+
+// With -fmodules-single-module-parse-mode, no modules get compiled.
+// RUN: %clang_cc1 -x objective-c -fmodules -fmodules-cache-path=%t/cache \
+// RUN:   -emit-module %t/module.modulemap -fmodule-name=Mod -o %t/Mod.pcm \
+// RUN:   -fmodules-single-module-parse-mode
+// RUN: find %t/cache -name "*.pcm" | count 0
+
+// Without -fmodules-single-module-parse-mode, loaded modules get compiled.
+// RUN: %clang_cc1 -x objective-c -fmodules -fmodules-cache-path=%t/cache \
+// RUN:   -emit-module %t/module.modulemap -fmodule-name=Mod -o %t/Mod.pcm
+// RUN: find %t/cache -name "*.pcm" | count 5
+
+//--- module.modulemap
+module Mod { header "Mod.h" }
+module Load1 { header "Load1.h" }
+module Load2 { header "Load2.h" }
+module Load3 { header "Load3.h" }
+module Load4 { header "Load4.h" }
+module Load5 { header "Load5.h" }
+//--- Mod.h
+#include "Load1.h"
+#import "Load2.h"
+@import Load3;
+#pragma clang module import Load4
+#pragma clang module load Load5
+//--- Load1.h
+//--- Load2.h
+//--- Load3.h
+//--- Load4.h
+//--- Load5.h

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to