https://github.com/ChuanqiXu9 updated https://github.com/llvm/llvm-project/pull/193426
>From 87acc679f00df2d31ab8f0c7e9bbe1cb670573aa Mon Sep 17 00:00:00 2001 From: Chuanqi Xu <[email protected]> Date: Wed, 22 Apr 2026 15:25:09 +0800 Subject: [PATCH 1/3] [clangd] [C++20] [Modules] Don't reuse prebuilt module files on windows Fix https://github.com/clangd/clangd/issues/2497 Note that this only works for --experimental-modules-support. Otherwise, it is a natural fallback by the design clang based tools. --- clang-tools-extra/clangd/ModulesBuilder.cpp | 20 +++++++ .../test/prebuilt_module_file_reuse.test | 58 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 clang-tools-extra/clangd/test/prebuilt_module_file_reuse.test diff --git a/clang-tools-extra/clangd/ModulesBuilder.cpp b/clang-tools-extra/clangd/ModulesBuilder.cpp index d90afea4651558..e34b2c2098307a 100644 --- a/clang-tools-extra/clangd/ModulesBuilder.cpp +++ b/clang-tools-extra/clangd/ModulesBuilder.cpp @@ -40,6 +40,23 @@ llvm::cl::opt<bool> DebugModulesBuilder( "Remember to remove them later after debugging."), llvm::cl::init(false)); +// Disable reusing prebuilt module files on windows by default. +// As clangd may lock these module files on windows to prevent +// these module files to be updated by buiild system on windows. +// +// See https://github.com/clangd/clangd/issues/2497 for details. +#ifdef _WIN32 +constexpr bool DisablePrebuiltModuleFileReuseDefault = true; +#else +constexpr bool DisablePrebuiltModuleFileReuseDefault = false; +#endif + +llvm::cl::opt<bool> DisablePrebuiltModuleFileReuse( + "disable-prebuilt-module-file-reuse", + llvm::cl::desc("Do not reuse prebuilt module files from existing build " + "artifacts."), + llvm::cl::init(DisablePrebuiltModuleFileReuseDefault)); + llvm::cl::opt<unsigned> VersionedModuleFileGCThresholdSeconds( "modules-builder-versioned-gc-threshold-seconds", llvm::cl::desc("Delete versioned copy-on-read module files whose last " @@ -1174,6 +1191,9 @@ void ModulesBuilder::ModulesBuilderImpl:: void ModulesBuilder::ModulesBuilderImpl::getPrebuiltModuleFile( StringRef ModuleName, PathRef ModuleUnitFileName, const ThreadsafeFS &TFS, ReusablePrerequisiteModules &BuiltModuleFiles) { + if (DisablePrebuiltModuleFileReuse) + return; + auto Cmd = getCDB().getCompileCommand(ModuleUnitFileName); if (!Cmd) return; diff --git a/clang-tools-extra/clangd/test/prebuilt_module_file_reuse.test b/clang-tools-extra/clangd/test/prebuilt_module_file_reuse.test new file mode 100644 index 00000000000000..bc0d667c77991b --- /dev/null +++ b/clang-tools-extra/clangd/test/prebuilt_module_file_reuse.test @@ -0,0 +1,58 @@ +# Check that clangd can optionally avoid reusing prebuilt module files. +# +# Windows uses a different default for this flag and has different escaping +# modes, so keep this test non-Windows for now. +# UNSUPPORTED: system-windows +# +# RUN: rm -fr %t +# RUN: mkdir -p %t +# RUN: split-file %s %t +# RUN: sed -e "s|DIR|%/t|g" %t/compile_commands.json.tmpl > %t/compile_commands.json.tmp +# RUN: sed -e "s|CLANG_CC|clang|g" %t/compile_commands.json.tmp > %t/compile_commands.json +# RUN: clang -std=c++20 %t/M.cppm --precompile -o %t/M.pcm +# RUN: clangd -enable-config=0 -experimental-modules-support -log=verbose \ +# RUN: -compile-commands-dir=%t -check=%t/Use.cpp 2>&1 \ +# RUN: | FileCheck %s --check-prefix=REUSE +# RUN: clangd -enable-config=0 -experimental-modules-support -log=verbose \ +# RUN: -compile-commands-dir=%t -check=%t/Use.cpp \ +# RUN: --disable-prebuilt-module-file-reuse 2>&1 \ +# RUN: | FileCheck %s --check-prefix=NOREUSE + +// REUSE: Built module M to [[PCM:.*clangd.*/module_files/.*/M\.pcm]] +// REUSE: All checks completed, 0 errors + +// NOREUSE-NOT: Reusing prebuilt module file +// NOREUSE: Built module M to [[BUILT:.*clangd.*/module_files/.*/M\.pcm]] +// NOREUSE: All checks completed, 0 errors + +#--- M.cppm +export module M; +export int value() { return 42; } + +#--- A.cppm +export module A; +import M; +export int useM() { return value(); } + +#--- Use.cpp +import A; +int use() { return useM(); } + +#--- compile_commands.json.tmpl +[ + { + "directory": "DIR", + "command": "CLANG_CC -std=c++20 DIR/M.cppm --precompile -o DIR/M-from-cdb.pcm", + "file": "DIR/M.cppm" + }, + { + "directory": "DIR", + "command": "CLANG_CC -std=c++20 -fmodule-file=M=DIR/M.pcm DIR/A.cppm --precompile -o DIR/A-from-cdb.pcm", + "file": "DIR/A.cppm" + }, + { + "directory": "DIR", + "command": "CLANG_CC -std=c++20 -fprebuilt-module-path=DIR -c DIR/Use.cpp -o DIR/Use.o", + "file": "DIR/Use.cpp" + } +] >From f861da22e0a696acd8f22bf894be5e3a344d12d7 Mon Sep 17 00:00:00 2001 From: Chuanqi Xu <[email protected]> Date: Thu, 8 Oct 2026 14:06:09 +0800 Subject: [PATCH 2/3] Update --- clang-tools-extra/clangd/ModulesBuilder.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/clang-tools-extra/clangd/ModulesBuilder.cpp b/clang-tools-extra/clangd/ModulesBuilder.cpp index e34b2c2098307a..72bb5b836d2012 100644 --- a/clang-tools-extra/clangd/ModulesBuilder.cpp +++ b/clang-tools-extra/clangd/ModulesBuilder.cpp @@ -42,20 +42,20 @@ llvm::cl::opt<bool> DebugModulesBuilder( // Disable reusing prebuilt module files on windows by default. // As clangd may lock these module files on windows to prevent -// these module files to be updated by buiild system on windows. +// these module files to be updated by build system on windows. // // See https://github.com/clangd/clangd/issues/2497 for details. #ifdef _WIN32 -constexpr bool DisablePrebuiltModuleFileReuseDefault = true; +constexpr bool ReusePrebuiltModuleFileDefault = false; #else -constexpr bool DisablePrebuiltModuleFileReuseDefault = false; +constexpr bool ReusePrebuiltModuleFileDefault = true; #endif -llvm::cl::opt<bool> DisablePrebuiltModuleFileReuse( - "disable-prebuilt-module-file-reuse", - llvm::cl::desc("Do not reuse prebuilt module files from existing build " +llvm::cl::opt<bool> ReusePrebuiltModuleFile( + "reuse-prebuilt-module-file", + llvm::cl::desc("Whether or not to reuse prebuilt module files from existing build " "artifacts."), - llvm::cl::init(DisablePrebuiltModuleFileReuseDefault)); + llvm::cl::init(ReusePrebuiltModuleFileDefault)); llvm::cl::opt<unsigned> VersionedModuleFileGCThresholdSeconds( "modules-builder-versioned-gc-threshold-seconds", @@ -1191,7 +1191,7 @@ void ModulesBuilder::ModulesBuilderImpl:: void ModulesBuilder::ModulesBuilderImpl::getPrebuiltModuleFile( StringRef ModuleName, PathRef ModuleUnitFileName, const ThreadsafeFS &TFS, ReusablePrerequisiteModules &BuiltModuleFiles) { - if (DisablePrebuiltModuleFileReuse) + if (!ReusePrebuiltModuleFile) return; auto Cmd = getCDB().getCompileCommand(ModuleUnitFileName); >From 3f817137f30b8e84492d3b20597e25ee18e9a0f5 Mon Sep 17 00:00:00 2001 From: Chuanqi Xu <[email protected]> Date: Thu, 8 Oct 2026 15:51:41 +0800 Subject: [PATCH 3/3] Update --- .../clangd/test/prebuilt_module_file_reuse.test | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/clang-tools-extra/clangd/test/prebuilt_module_file_reuse.test b/clang-tools-extra/clangd/test/prebuilt_module_file_reuse.test index bc0d667c77991b..2d0e4af188aa86 100644 --- a/clang-tools-extra/clangd/test/prebuilt_module_file_reuse.test +++ b/clang-tools-extra/clangd/test/prebuilt_module_file_reuse.test @@ -15,14 +15,13 @@ # RUN: | FileCheck %s --check-prefix=REUSE # RUN: clangd -enable-config=0 -experimental-modules-support -log=verbose \ # RUN: -compile-commands-dir=%t -check=%t/Use.cpp \ -# RUN: --disable-prebuilt-module-file-reuse 2>&1 \ +# RUN: --reuse-prebuilt-module-file=false 2>&1 \ # RUN: | FileCheck %s --check-prefix=NOREUSE -// REUSE: Built module M to [[PCM:.*clangd.*/module_files/.*/M\.pcm]] +// REUSE: Reusing prebuilt module file // REUSE: All checks completed, 0 errors // NOREUSE-NOT: Reusing prebuilt module file -// NOREUSE: Built module M to [[BUILT:.*clangd.*/module_files/.*/M\.pcm]] // NOREUSE: All checks completed, 0 errors #--- M.cppm _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
