[clang] [clang][modules] Track Included Files Per Submodule (PR #170215)

Qiongsi Wu via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 9 10:31:03 PST 2025


================
@@ -1516,19 +1518,41 @@ class Preprocessor {
   /// Mark the file as included.
   /// Returns true if this is the first time the file was included.
   bool markIncluded(FileEntryRef File) {
+    bool AlreadyIncluded = alreadyIncluded(File);
     HeaderInfo.getFileInfo(File).IsLocallyIncluded = true;
-    return IncludedFiles.insert(File).second;
+    CurSubmoduleState->IncludedFiles.insert(File);
+    if (!BuildingSubmoduleStack.empty())
+      BuildingSubmoduleStack.back().M->Includes.insert(File);
+    else if (Module *M = getCurrentModule())
+      M->Includes.insert(File);
+    else
+      Includes.insert(File);
+    return !AlreadyIncluded;
   }
 
   /// Return true if this header has already been included.
   bool alreadyIncluded(FileEntryRef File) const {
     HeaderInfo.getFileInfo(File);
-    return IncludedFiles.count(File);
+    if (CurSubmoduleState->IncludedFiles.contains(File))
+      return true;
+    // TODO: Do this more efficiently.
+    for (const auto &[Name, M] : HeaderInfo.getModuleMap().modules())
+      if (CurSubmoduleState->VisibleModules.isVisible(M))
+        if (M->Includes.contains(File))
+          return true;
----------------
qiongsiwu wrote:

Yes absolutely. Looking into this. Initially I did not find a good way to do this to avoid looping _and_ avoid storing extra information to see which modules are visible. I am thinking that I can have an extra data structure that contains the set of visible includes in `CurSubmoduleState` so we can do a single lookup without looping. 

https://github.com/llvm/llvm-project/pull/170215


More information about the cfe-commits mailing list