Author: Chuanqi Xu
Date: 2025-09-20T15:48:21+08:00
New Revision: a728f213c863e4dd19f8969a417148d2951323c0

URL: 
https://github.com/llvm/llvm-project/commit/a728f213c863e4dd19f8969a417148d2951323c0
DIFF: 
https://github.com/llvm/llvm-project/commit/a728f213c863e4dd19f8969a417148d2951323c0.diff

LOG: [Driver] [C++20] [Modules] Fix --precompile with -fmodule-output

Close https://github.com/llvm/llvm-project/issues/159780

Added: 
    

Modified: 
    clang/lib/Driver/ToolChains/Clang.cpp
    clang/test/Driver/modules.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index e7aabee273a34..f67454ee517bd 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -4051,15 +4051,24 @@ static bool RenderModulesOptions(Compilation &C, const 
Driver &D,
   // module fragment.
   CmdArgs.push_back("-fskip-odr-check-in-gmf");
 
-  if (!Args.hasArg(options::OPT_fno_modules_reduced_bmi) &&
-      (Input.getType() == driver::types::TY_CXXModule ||
-       Input.getType() == driver::types::TY_PP_CXXModule) &&
-      !Args.hasArg(options::OPT__precompile)) {
-    CmdArgs.push_back("-fmodules-reduced-bmi");
+  if (Input.getType() == driver::types::TY_CXXModule ||
+      Input.getType() == driver::types::TY_PP_CXXModule) {
+    if (!Args.hasArg(options::OPT_fno_modules_reduced_bmi))
+      CmdArgs.push_back("-fmodules-reduced-bmi");
 
     if (Args.hasArg(options::OPT_fmodule_output_EQ))
       Args.AddLastArg(CmdArgs, options::OPT_fmodule_output_EQ);
-    else
+    else if (!Args.hasArg(options::OPT__precompile) ||
+             Args.hasArg(options::OPT_fmodule_output))
+      // If --precompile is specified, we will always generate a module file if
+      // we're compiling an importable module unit. This is fine even if the
+      // compilation process won't reach the point of generating the module 
file
+      // (e.g., in the preprocessing mode), since the attached flag
+      // '-fmodule-output' is useless.
+      //
+      // But if '--precompile' is specified, it might be annoying to always
+      // generate the module file as '--precompile' will generate the module
+      // file anyway.
       CmdArgs.push_back(Args.MakeArgString(
           "-fmodule-output=" +
           getCXX20NamedModuleOutputPath(Args, Input.getBaseInput())));

diff  --git a/clang/test/Driver/modules.cpp b/clang/test/Driver/modules.cpp
index edbe8d8e92c85..4e70c0a6f882e 100644
--- a/clang/test/Driver/modules.cpp
+++ b/clang/test/Driver/modules.cpp
@@ -7,7 +7,7 @@
 // RUN: %clang -std=c++2a -x c++-module --precompile %t/foo.cpp -o %t/foo.pcm 
-v 2>&1 | FileCheck %s --check-prefix=CHECK-PRECOMPILE
 // RUN: %clang -std=gnu++2a -x c++-module --precompile %t/foo.cpp -o 
%t/foo-gnu.pcm -v 2>&1 | FileCheck %s --check-prefix=CHECK-PRECOMPILE
 //
-// CHECK-PRECOMPILE: -cc1 {{.*}} -emit-module-interface
+// CHECK-PRECOMPILE: -cc1 {{.*}} -emit-module-interface 
 // CHECK-PRECOMPILE-SAME: -o {{.*}}.pcm
 // CHECK-PRECOMPILE-SAME: -x c++
 // CHECK-PRECOMPILE-SAME: foo.cpp
@@ -41,6 +41,12 @@
 // RUN: cp %t/foo.cpp %t/foo.cppm
 // RUN: %clang -std=c++2a --precompile %t/foo.cppm -o %t/foo.pcm -v 2>&1 | 
FileCheck %s --check-prefix=CHECK-PRECOMPILE
 
+// RUN: %clang -std=c++20 -x c++-module %t/foo.cpp --precompile -o %t/foo.pcm 
-### 2>&1 | FileCheck %s --check-prefix=CHECK-PRECOMPILE-WO-MODULE-OUTPUT
+// CHECK-PRECOMPILE-WO-MODULE-OUTPUT-NOT: -fmodule-output=
+
+// RUN: %clang -std=c++20 -x c++-module %t/foo.cpp --precompile 
-fmodule-output=%t/foo.reduced.pcm -o %t/foo.pcm -### 2>&1 | FileCheck %s 
--check-prefix=CHECK-PRECOMPILE-WT-MODULE-OUTPUT
+// CHECK-PRECOMPILE-WT-MODULE-OUTPUT: -fmodule-output=
+
 //--- foo.cpp
 export module foo;
 


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

Reply via email to