ChuanqiXu created this revision.
ChuanqiXu added reviewers: rsmith, aaron.ballman, urnathan, erichkeane,
hubert.reinterpretcast.
ChuanqiXu added a project: clang.
ChuanqiXu requested review of this revision.
Herald added a subscriber: cfe-commits.
Since the serialization code would recognize modules by names and the name of
all global module fragment is `<global>`, so that the serialization code would
complain for the same module.
This patch fixes this by using a global module fragment cache in Sema. Before
this patch, the compiler would fail on an assertion complaining the duplicated
modules.
Test Plan: check-all, an internal module library
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D115610
Files:
clang/include/clang/Sema/Sema.h
clang/lib/Sema/SemaModule.cpp
clang/test/CXX/module/module.unit/p7/Inputs/h8.h
clang/test/CXX/module/module.unit/p7/Inputs/m8.cppm
clang/test/CXX/module/module.unit/p7/t8.cpp
Index: clang/test/CXX/module/module.unit/p7/t8.cpp
===================================================================
--- /dev/null
+++ clang/test/CXX/module/module.unit/p7/t8.cpp
@@ -0,0 +1,7 @@
+// RUN: rm -fr %t
+// RUN: mkdir %t
+// RUN: %clang_cc1 -std=c++20 -emit-module-interface %S/Inputs/m8.cppm
-I%S/Inputs -o %t/m8.pcm
+// RUN: %clang_cc1 -std=c++20 -emit-module-interface -I%S/Inputs/
-fprebuilt-module-path=%t %s -verify
+// expected-no-diagnostics
+export module t8;
+import m8;
Index: clang/test/CXX/module/module.unit/p7/Inputs/m8.cppm
===================================================================
--- /dev/null
+++ clang/test/CXX/module/module.unit/p7/Inputs/m8.cppm
@@ -0,0 +1,8 @@
+module;
+#include "h8.h"
+export module m8;
+
+extern "C++" {
+ void bar();
+}
+
Index: clang/test/CXX/module/module.unit/p7/Inputs/h8.h
===================================================================
--- /dev/null
+++ clang/test/CXX/module/module.unit/p7/Inputs/h8.h
@@ -0,0 +1,4 @@
+#ifndef H8
+#define H8
+void foo();
+#endif
Index: clang/lib/Sema/SemaModule.cpp
===================================================================
--- clang/lib/Sema/SemaModule.cpp
+++ clang/lib/Sema/SemaModule.cpp
@@ -704,19 +704,24 @@
Module *Sema::PushGlobalModuleFragment(SourceLocation BeginLoc,
bool IsImplicit) {
- ModuleMap &Map = PP.getHeaderSearchInfo().getModuleMap();
- Module *GlobalModule =
- Map.createGlobalModuleFragmentForModuleUnit(BeginLoc,
getCurrentModule());
- assert(GlobalModule && "module creation should not fail");
+ // We shouldn't create new global module fragment if there is already
+ // one.
+ if (!GlobalModuleFragmentCache) {
+ ModuleMap &Map = PP.getHeaderSearchInfo().getModuleMap();
+ GlobalModuleFragmentCache = Map.createGlobalModuleFragmentForModuleUnit(
+ BeginLoc, getCurrentModule());
+ }
+
+ assert(GlobalModuleFragmentCache && "module creation should not fail");
// Enter the scope of the global module.
- ModuleScopes.push_back({BeginLoc, GlobalModule,
+ ModuleScopes.push_back({BeginLoc, GlobalModuleFragmentCache,
/*ModuleInterface=*/false,
/*ImplicitGlobalModuleFragment=*/IsImplicit,
- /*VisibleModuleSet*/{}});
- VisibleModules.setVisible(GlobalModule, BeginLoc);
+ /*VisibleModuleSet*/ {}});
+ VisibleModules.setVisible(GlobalModuleFragmentCache, BeginLoc);
- return GlobalModule;
+ return GlobalModuleFragmentCache;
}
void Sema::PopGlobalModuleFragment() {
Index: clang/include/clang/Sema/Sema.h
===================================================================
--- clang/include/clang/Sema/Sema.h
+++ clang/include/clang/Sema/Sema.h
@@ -2213,6 +2213,8 @@
};
/// The modules we're currently parsing.
llvm::SmallVector<ModuleScope, 16> ModuleScopes;
+ /// The gloabl module fragment of the current tranlation unit.
+ clang::Module *GlobalModuleFragmentCache = nullptr;
/// Namespace definitions that we will export when they finish.
llvm::SmallPtrSet<const NamespaceDecl*, 8> DeferredExportedNamespaces;
Index: clang/test/CXX/module/module.unit/p7/t8.cpp
===================================================================
--- /dev/null
+++ clang/test/CXX/module/module.unit/p7/t8.cpp
@@ -0,0 +1,7 @@
+// RUN: rm -fr %t
+// RUN: mkdir %t
+// RUN: %clang_cc1 -std=c++20 -emit-module-interface %S/Inputs/m8.cppm -I%S/Inputs -o %t/m8.pcm
+// RUN: %clang_cc1 -std=c++20 -emit-module-interface -I%S/Inputs/ -fprebuilt-module-path=%t %s -verify
+// expected-no-diagnostics
+export module t8;
+import m8;
Index: clang/test/CXX/module/module.unit/p7/Inputs/m8.cppm
===================================================================
--- /dev/null
+++ clang/test/CXX/module/module.unit/p7/Inputs/m8.cppm
@@ -0,0 +1,8 @@
+module;
+#include "h8.h"
+export module m8;
+
+extern "C++" {
+ void bar();
+}
+
Index: clang/test/CXX/module/module.unit/p7/Inputs/h8.h
===================================================================
--- /dev/null
+++ clang/test/CXX/module/module.unit/p7/Inputs/h8.h
@@ -0,0 +1,4 @@
+#ifndef H8
+#define H8
+void foo();
+#endif
Index: clang/lib/Sema/SemaModule.cpp
===================================================================
--- clang/lib/Sema/SemaModule.cpp
+++ clang/lib/Sema/SemaModule.cpp
@@ -704,19 +704,24 @@
Module *Sema::PushGlobalModuleFragment(SourceLocation BeginLoc,
bool IsImplicit) {
- ModuleMap &Map = PP.getHeaderSearchInfo().getModuleMap();
- Module *GlobalModule =
- Map.createGlobalModuleFragmentForModuleUnit(BeginLoc, getCurrentModule());
- assert(GlobalModule && "module creation should not fail");
+ // We shouldn't create new global module fragment if there is already
+ // one.
+ if (!GlobalModuleFragmentCache) {
+ ModuleMap &Map = PP.getHeaderSearchInfo().getModuleMap();
+ GlobalModuleFragmentCache = Map.createGlobalModuleFragmentForModuleUnit(
+ BeginLoc, getCurrentModule());
+ }
+
+ assert(GlobalModuleFragmentCache && "module creation should not fail");
// Enter the scope of the global module.
- ModuleScopes.push_back({BeginLoc, GlobalModule,
+ ModuleScopes.push_back({BeginLoc, GlobalModuleFragmentCache,
/*ModuleInterface=*/false,
/*ImplicitGlobalModuleFragment=*/IsImplicit,
- /*VisibleModuleSet*/{}});
- VisibleModules.setVisible(GlobalModule, BeginLoc);
+ /*VisibleModuleSet*/ {}});
+ VisibleModules.setVisible(GlobalModuleFragmentCache, BeginLoc);
- return GlobalModule;
+ return GlobalModuleFragmentCache;
}
void Sema::PopGlobalModuleFragment() {
Index: clang/include/clang/Sema/Sema.h
===================================================================
--- clang/include/clang/Sema/Sema.h
+++ clang/include/clang/Sema/Sema.h
@@ -2213,6 +2213,8 @@
};
/// The modules we're currently parsing.
llvm::SmallVector<ModuleScope, 16> ModuleScopes;
+ /// The gloabl module fragment of the current tranlation unit.
+ clang::Module *GlobalModuleFragmentCache = nullptr;
/// Namespace definitions that we will export when they finish.
llvm::SmallPtrSet<const NamespaceDecl*, 8> DeferredExportedNamespaces;
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits