[clang] [clang][Tooling] Insert global module fragment during header insertion (PR #173724)

Chuanqi Xu via cfe-commits cfe-commits at lists.llvm.org
Sun Dec 28 18:21:31 PST 2025


================
@@ -594,6 +594,66 @@ TEST_F(HeaderIncludesTest, CanDeleteAfterCode) {
   EXPECT_EQ(Expected, remove(Code, "\"b.h\""));
 }
 
+TEST_F(HeaderIncludesTest, InsertGlobalModuleFragmentDeclInterfaceUnit) {
+  // Ensure the header insertion comes with a global module fragment decl (i.e.
+  // a 'module;' line) when:
+  //     - the input file is an module interface unit, and
+  //     - no tokens excluding comments and whitespaces exist before the module
+  //       declaration.
+  std::string Code = R"cpp(// comments
+
+// more comments
+
+export module foo;
+
+int main() {
+    std::vector<int> ints {};
+})cpp";
+  std::string Expected = R"cpp(// comments
+
+// more comments
+
+module;
+#include <vector>
+export module foo;
+
+int main() {
+    std::vector<int> ints {};
+})cpp";
----------------
ChuanqiXu9 wrote:

Maybe we can do something similar in https://github.com/llvm/llvm-project/issues/172966

e.g, we should ask the tools to insert `import std;` instead of `#include`.

We can file an issue for it as the first step.

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


More information about the cfe-commits mailing list