From 7130126a3231f50b8eeea662e664e19db06f51c7 Mon Sep 17 00:00:00 2001 From: Naveen Seth Hanig Date: Wed, 1 Oct 2025 03:51:58 +0200 Subject: [PATCH 1/4] [clang][DependencyScanning] Reset options generated for named module compilations. The driver-generated -cc1 command-lines for C++ named module inputs introduce some command-line options which affect the canonical module build command (and therefore the context hash). This resets those options. --- .../DependencyScanning/ModuleDepCollector.cpp | 4 + ...modules-context-hash-from-named-module.cpp | 111 ++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 clang/test/ClangScanDeps/modules-context-hash-from-named-module.cpp diff --git a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp index d67178c153e88..a117bec0d656e 100644 --- a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp +++ b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp @@ -263,6 +263,10 @@ makeCommonInvocationForModuleBuild(CompilerInvocation CI) { // units. CI.getFrontendOpts().Inputs.clear(); CI.getFrontendOpts().OutputFile.clear(); + CI.getFrontendOpts().GenReducedBMI = false; + CI.getFrontendOpts().ModuleOutputPath.clear(); + CI.getHeaderSearchOpts().ModulesSkipHeaderSearchPaths = false; + CI.getHeaderSearchOpts().ModulesSkipDiagnosticOptions = false; // LLVM options are not going to affect the AST CI.getFrontendOpts().LLVMArgs.clear(); diff --git a/clang/test/ClangScanDeps/modules-context-hash-from-named-module.cpp b/clang/test/ClangScanDeps/modules-context-hash-from-named-module.cpp new file mode 100644 index 0000000000000..04148953b5683 --- /dev/null +++ b/clang/test/ClangScanDeps/modules-context-hash-from-named-module.cpp @@ -0,0 +1,111 @@ +// Check that the driver-generated command line options for C++ module inputs +// do not unexpectedly change the context hash of imported Clang modules +// compared to the command line for an otherwise equal non-module input. + +// RUN: rm -rf %t +// RUN: split-file %s %t + +//--- main.cpp +#include "root.h" +import A; +import B; + +auto main() -> int { return 1; } + +//--- A.cppm +module; +#include "root.h" +export module A; + +//--- B.cppm +module; +#include "root.h" +export module B; + +//--- module.modulemap +module root { header "root.h" } + +//--- root.h +// empty + +// RUN: %clang++ -std=c++23 -fmodules %t/main.cpp %t/A.cppm %t/B.cppm -fsyntax-only -fdriver-only -MJ %t/deps.json +// RUN: sed -e '1s/^/[/' -e '$s/,$/]/' %t/deps.json | sed 's:\\\\\?:/:g' > %t/compile_commands.json +// RUN: clang-scan-deps -compilation-database=%t/compile_commands.json -format experimental-full > %t/result.json +// RUN: cat %t/result.json + +// RUN: cat %t/result.json | sed 's:\\\\\?:/:g' | FileCheck %s -DPREFIX=%/t + +// CHECK: { +// CHECK-NEXT: "modules": [ +// CHECK-NEXT: { +// CHECK-NEXT: "clang-module-deps": [], +// CHECK-NEXT: "clang-modulemap-file": "[[PREFIX]]/module.modulemap", +// CHECK: "context-hash": "[[HASH_ROOT:.*]]", +// CHECK-NEXT: "file-deps": [ +// CHECK-NEXT: "[[PREFIX]]/module.modulemap", +// CHECK-NEXT: "[[PREFIX]]/root.h" +// CHECK-NEXT: ], +// CHECK-NEXT: "link-libraries": [], +// CHECK-NEXT: "name": "root" +// CHECK-NEXT: } +// CHECK-NEXT: ], +// CHECK-NEXT: "translation-units": [ +// CHECK-NEXT: { +// CHECK-NEXT: "commands": [ +// CHECK-NEXT: { +// CHECK-NEXT: "clang-context-hash": "{{.*}}", +// CHECK-NEXT: "named-module-deps": [ +// CHECK-NEXT: "A", +// CHECK-NEXT: "B" +// CHECK-NEXT: ], +// CHECK-NEXT: "clang-module-deps": [ +// CHECK-NEXT: { +// CHECK-NEXT: "context-hash": "[[HASH_ROOT]]", +// CHECK-NEXT: "module-name": "root" +// CHECK-NEXT: } +// CHECK-NEXT: ], +// CHECK: "file-deps": [ +// CHECK-NEXT: "[[PREFIX]]/main.cpp" +// CHECK-NEXT: ], +// CHECK-NEXT: "input-file": "[[PREFIX]]/main.cpp" +// CHECK-NEXT: } +// CHECK-NEXT: ] +// CHECK-NEXT: }, +// CHECK-NEXT: { +// CHECK-NEXT: "commands": [ +// CHECK-NEXT: { +// CHECK-NEXT: "clang-context-hash": "{{.*}}", +// CHECK-NEXT: "named-module": "A", +// CHECK-NEXT: "clang-module-deps": [ +// CHECK-NEXT: { +// CHECK-NEXT: "context-hash": "[[HASH_ROOT]]", +// CHECK-NEXT: "module-name": "root" +// CHECK-NEXT: } +// CHECK-NEXT: ], +// CHECK: "file-deps": [ +// CHECK-NEXT: "[[PREFIX]]/A.cppm" +// CHECK-NEXT: ], +// CHECK-NEXT: "input-file": "[[PREFIX]]/A.cppm" +// CHECK-NEXT: } +// CHECK-NEXT: ] +// CHECK-NEXT: }, +// CHECK-NEXT: { +// CHECK-NEXT: "commands": [ +// CHECK-NEXT: { +// CHECK-NEXT: "clang-context-hash": "{{.*}}", +// CHECK-NEXT: "named-module": "B", +// CHECK-NEXT: "clang-module-deps": [ +// CHECK-NEXT: { +// CHECK-NEXT: "context-hash": "[[HASH_ROOT]]", +// CHECK-NEXT: "module-name": "root" +// CHECK-NEXT: } +// CHECK-NEXT: ], +// CHECK: "file-deps": [ +// CHECK-NEXT: "[[PREFIX]]/B.cppm" +// CHECK-NEXT: ], +// CHECK-NEXT: "input-file": "[[PREFIX]]/B.cppm" +// CHECK-NEXT: } +// CHECK-NEXT: ] +// CHECK-NEXT: } +// CHECK-NEXT: ] +// CHECK-NEXT: } From b230b52be816894829360d4da9f9515ff48e1661 Mon Sep 17 00:00:00 2001 From: Naveen Seth Hanig Date: Wed, 1 Oct 2025 09:58:50 +0200 Subject: [PATCH 2/4] Fix test for windows --- .../ClangScanDeps/modules-context-hash-from-named-module.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/test/ClangScanDeps/modules-context-hash-from-named-module.cpp b/clang/test/ClangScanDeps/modules-context-hash-from-named-module.cpp index 04148953b5683..af3fad9a2e8e0 100644 --- a/clang/test/ClangScanDeps/modules-context-hash-from-named-module.cpp +++ b/clang/test/ClangScanDeps/modules-context-hash-from-named-module.cpp @@ -28,7 +28,7 @@ module root { header "root.h" } //--- root.h // empty -// RUN: %clang++ -std=c++23 -fmodules %t/main.cpp %t/A.cppm %t/B.cppm -fsyntax-only -fdriver-only -MJ %t/deps.json +// RUN: %clang -std=c++23 -fmodules %t/main.cpp %t/A.cppm %t/B.cppm -fsyntax-only -fdriver-only -MJ %t/deps.json // RUN: sed -e '1s/^/[/' -e '$s/,$/]/' %t/deps.json | sed 's:\\\\\?:/:g' > %t/compile_commands.json // RUN: clang-scan-deps -compilation-database=%t/compile_commands.json -format experimental-full > %t/result.json // RUN: cat %t/result.json From 358852bcf8134a8fd03d97f751fd6466be2bfa25 Mon Sep 17 00:00:00 2001 From: Naveen Seth Hanig Date: Sat, 4 Oct 2025 15:27:05 +0200 Subject: [PATCH 3/4] Also reset options when scanning --- clang/lib/Tooling/DependencyScanning/DependencyScannerImpl.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/clang/lib/Tooling/DependencyScanning/DependencyScannerImpl.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScannerImpl.cpp index e1f4d0d361185..b0096d8e6b08b 100644 --- a/clang/lib/Tooling/DependencyScanning/DependencyScannerImpl.cpp +++ b/clang/lib/Tooling/DependencyScanning/DependencyScannerImpl.cpp @@ -509,6 +509,8 @@ bool initializeScanCompilerInstance( ScanInstance.getFrontendOpts().DisableFree = false; ScanInstance.getFrontendOpts().GenerateGlobalModuleIndex = false; ScanInstance.getFrontendOpts().UseGlobalModuleIndex = false; + ScanInstance.getFrontendOpts().GenReducedBMI = false; + ScanInstance.getFrontendOpts().ModuleOutputPath.clear(); // This will prevent us compiling individual modules asynchronously since // FileManager is not thread-safe, but it does improve performance for now. ScanInstance.getFrontendOpts().ModulesShareFileManager = true; From 4b449c891fb6ca16d2bcdfb514baff7e9b2a7b32 Mon Sep 17 00:00:00 2001 From: Naveen Seth Hanig Date: Sat, 4 Oct 2025 13:54:25 +0200 Subject: [PATCH 4/4] Address review feedback from jansvoboda11 and clean up test --- ...modules-context-hash-from-named-module.cpp | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/clang/test/ClangScanDeps/modules-context-hash-from-named-module.cpp b/clang/test/ClangScanDeps/modules-context-hash-from-named-module.cpp index af3fad9a2e8e0..c27202234f4c5 100644 --- a/clang/test/ClangScanDeps/modules-context-hash-from-named-module.cpp +++ b/clang/test/ClangScanDeps/modules-context-hash-from-named-module.cpp @@ -1,6 +1,6 @@ -// Check that the driver-generated command line options for C++ module inputs -// do not unexpectedly change the context hash of imported Clang modules -// compared to the command line for an otherwise equal non-module input. +// Checks that driver-generated options for C++ module inputs preserve the +// canonical module build commands compared to an equivalent non-module input, +// and that they do not produce additional internal scanning PCMs. // RUN: rm -rf %t // RUN: split-file %s %t @@ -28,12 +28,18 @@ module root { header "root.h" } //--- root.h // empty -// RUN: %clang -std=c++23 -fmodules %t/main.cpp %t/A.cppm %t/B.cppm -fsyntax-only -fdriver-only -MJ %t/deps.json -// RUN: sed -e '1s/^/[/' -e '$s/,$/]/' %t/deps.json | sed 's:\\\\\?:/:g' > %t/compile_commands.json -// RUN: clang-scan-deps -compilation-database=%t/compile_commands.json -format experimental-full > %t/result.json -// RUN: cat %t/result.json - -// RUN: cat %t/result.json | sed 's:\\\\\?:/:g' | FileCheck %s -DPREFIX=%/t +// RUN: %clang -std=c++23 -fmodules \ +// RUN: -fmodules-cache-path=%t/modules-cache \ +// RUN: %t/main.cpp %t/A.cppm %t/B.cppm \ +// RUN: -fsyntax-only -fdriver-only -MJ %t/deps.json +// +// RUN: sed -e '1s/^/[/' -e '$s/,$/]/' -e 's:\\\\\?:/:g' %t/deps.json \ +// RUN: > %t/compile_commands.json +// +// RUN: clang-scan-deps \ +// RUN: -compilation-database=%t/compile_commands.json \ +// RUN: -format experimental-full \ +// RUN: | sed 's:\\\\\?:/:g' | FileCheck %s -DPREFIX=%/t // CHECK: { // CHECK-NEXT: "modules": [ @@ -109,3 +115,7 @@ module root { header "root.h" } // CHECK-NEXT: } // CHECK-NEXT: ] // CHECK-NEXT: } + +// This tests that the scanner doesn't produce multiple internal scanning PCMs +// for our single Clang module (root). +// RUN: find %t/modules-cache -name "*.pcm" | wc -l | grep 1