[clang] 3b8f536 - [clang][deps] Use top-level modules as precompiled dependencies

Jan Svoboda via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 25 01:51:40 PDT 2021


Author: Jan Svoboda
Date: 2021-08-25T10:51:34+02:00
New Revision: 3b8f536fec2f7dfd5dc69c6516c20c5b48b38409

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

LOG: [clang][deps] Use top-level modules as precompiled dependencies

The `ASTReader` populates `Module::PresumedModuleMapFile` only for top-level modules, not submodules. To avoid generating empty `-fmodule-map-file=` arguments, make discovered modules depend on top-level precompiled modules. The granularity of submodules is not important here.

The documentation of `Module::PresumedModuleMapFile` says this field is non-empty only when building from preprocessed source. This means there can still be cases where the dependency scanner generates empty `-fmodule-map-file=` arguments. That's being addressed in separate patch: D108544.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D108647

Added: 
    clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/cdb_pch.json
    clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/cdb_tu.json
    clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/mod_common.h
    clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/mod_common_sub.h
    clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/mod_tu.h
    clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/module.modulemap
    clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/pch.h
    clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/tu.c
    clang/test/ClangScanDeps/modules-pch-common-submodule.c

Modified: 
    clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
index cc1f64e221ac4..66f8d97e246e2 100644
--- a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
+++ b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
@@ -257,9 +257,9 @@ void ModuleDepCollectorPP::addModulePrebuiltDeps(
     llvm::DenseSet<const Module *> &SeenSubmodules) {
   for (const Module *Import : M->Imports)
     if (Import->getTopLevelModule() != M->getTopLevelModule())
-      if (MDC.isPrebuiltModule(Import))
-        if (SeenSubmodules.insert(Import).second)
-          MD.PrebuiltModuleDeps.emplace_back(Import);
+      if (MDC.isPrebuiltModule(Import->getTopLevelModule()))
+        if (SeenSubmodules.insert(Import->getTopLevelModule()).second)
+          MD.PrebuiltModuleDeps.emplace_back(Import->getTopLevelModule());
 }
 
 void ModuleDepCollectorPP::addAllSubmoduleDeps(

diff  --git a/clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/cdb_pch.json b/clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/cdb_pch.json
new file mode 100644
index 0000000000000..dc2fc550b0191
--- /dev/null
+++ b/clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/cdb_pch.json
@@ -0,0 +1,7 @@
+[
+  {
+    "directory": "DIR",
+    "command": "clang -x c-header DIR/pch.h -fmodules -gmodules -fimplicit-module-maps -fmodules-cache-path=DIR/cache -o DIR/pch.h.gch",
+    "file": "DIR/pch.h"
+  }
+]

diff  --git a/clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/cdb_tu.json b/clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/cdb_tu.json
new file mode 100644
index 0000000000000..8aad2cc74023e
--- /dev/null
+++ b/clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/cdb_tu.json
@@ -0,0 +1,7 @@
+[
+  {
+    "directory": "DIR",
+    "command": "clang -fsyntax-only DIR/tu.c -fmodules -gmodules -fimplicit-module-maps -fmodules-cache-path=DIR/cache -include DIR/pch.h -o DIR/tu.o",
+    "file": "DIR/tu.c"
+  }
+]

diff  --git a/clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/mod_common.h b/clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/mod_common.h
new file mode 100644
index 0000000000000..e69de29bb2d1d

diff  --git a/clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/mod_common_sub.h b/clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/mod_common_sub.h
new file mode 100644
index 0000000000000..e69de29bb2d1d

diff  --git a/clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/mod_tu.h b/clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/mod_tu.h
new file mode 100644
index 0000000000000..30f05b0bd507b
--- /dev/null
+++ b/clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/mod_tu.h
@@ -0,0 +1 @@
+#include "mod_common_sub.h"

diff  --git a/clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/module.modulemap b/clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/module.modulemap
new file mode 100644
index 0000000000000..b7e404e20cef7
--- /dev/null
+++ b/clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/module.modulemap
@@ -0,0 +1,11 @@
+module ModCommon {
+  header "mod_common.h"
+
+  module ModCommonSub {
+    header "mod_common_sub.h"
+  }
+}
+
+module ModTU {
+  header "mod_tu.h"
+}

diff  --git a/clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/pch.h b/clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/pch.h
new file mode 100644
index 0000000000000..d160b835658d1
--- /dev/null
+++ b/clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/pch.h
@@ -0,0 +1 @@
+#include "mod_common.h"

diff  --git a/clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/tu.c b/clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/tu.c
new file mode 100644
index 0000000000000..085d398a5ab97
--- /dev/null
+++ b/clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/tu.c
@@ -0,0 +1 @@
+#include "mod_tu.h"

diff  --git a/clang/test/ClangScanDeps/modules-pch-common-submodule.c b/clang/test/ClangScanDeps/modules-pch-common-submodule.c
new file mode 100644
index 0000000000000..dd2ea26406dbf
--- /dev/null
+++ b/clang/test/ClangScanDeps/modules-pch-common-submodule.c
@@ -0,0 +1,140 @@
+// Check that when depending on a precompiled module, we depend on the
+// **top-level** module. Submodules don't have some information present (for
+// example the path to the modulemap file) and depending on them might cause
+// problems in the dependency scanner (e.g. generating empty `-fmodule-map-file=`
+// arguments).
+
+// RUN: rm -rf %t && mkdir %t
+// RUN: cp %S/Inputs/modules-pch-common-submodule/* %t
+
+// Scan dependencies of the PCH:
+//
+// RUN: sed "s|DIR|%/t|g" %S/Inputs/modules-pch-common-submodule/cdb_pch.json > %t/cdb.json
+// RUN: echo -%t > %t/result_pch.json
+// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-full \
+// RUN:   -generate-modules-path-args -module-files-dir %t/build >> %t/result_pch.json
+// RUN: cat %t/result_pch.json | sed 's:\\\\\?:/:g' | FileCheck %s -check-prefix=CHECK-PCH
+//
+// CHECK-PCH:      -[[PREFIX:.*]]
+// CHECK-PCH-NEXT: {
+// CHECK-PCH-NEXT:   "modules": [
+// CHECK-PCH-NEXT:     {
+// CHECK-PCH-NEXT:       "clang-module-deps": [],
+// CHECK-PCH-NEXT:       "clang-modulemap-file": "[[PREFIX]]/module.modulemap",
+// CHECK-PCH-NEXT:       "command-line": [
+// CHECK-PCH-NEXT:         "-cc1"
+// CHECK-PCH:              "-emit-module"
+// CHECK-PCH:              "-fmodules"
+// CHECK-PCH:              "-fmodule-name=ModCommon"
+// CHECK-PCH:              "-fno-implicit-modules"
+// CHECK-PCH:            ],
+// CHECK-PCH-NEXT:       "context-hash": "[[HASH_MOD_COMMON:.*]]",
+// CHECK-PCH-NEXT:       "file-deps": [
+// CHECK-PCH-NEXT:         "[[PREFIX]]/mod_common.h",
+// CHECK-PCH-NEXT:         "[[PREFIX]]/mod_common_sub.h",
+// CHECK-PCH-NEXT:         "[[PREFIX]]/module.modulemap"
+// CHECK-PCH-NEXT:       ],
+// CHECK-PCH-NEXT:       "name": "ModCommon"
+// CHECK-PCH-NEXT:     }
+// CHECK-PCH-NEXT:   ],
+// CHECK-PCH-NEXT:   "translation-units": [
+// CHECK-PCH-NEXT:     {
+// CHECK-PCH-NEXT:       "clang-context-hash": "[[HASH_PCH:.*]]",
+// CHECK-PCH-NEXT:       "clang-module-deps": [
+// CHECK-PCH-NEXT:         {
+// CHECK-PCH-NEXT:           "context-hash": "[[HASH_MOD_COMMON]]",
+// CHECK-PCH-NEXT:           "module-name": "ModCommon"
+// CHECK-PCH-NEXT:         }
+// CHECK-PCH-NEXT:       ],
+// CHECK-PCH-NEXT:       "command-line": [
+// CHECK-PCH-NEXT:         "-fno-implicit-modules"
+// CHECK-PCH-NEXT:         "-fno-implicit-module-maps"
+// CHECK-PCH-NEXT:         "-fmodule-file=[[PREFIX]]/build/[[HASH_MOD_COMMON]]/ModCommon-{{.*}}.pcm"
+// CHECK-PCH-NEXT:         "-fmodule-map-file=[[PREFIX]]/module.modulemap"
+// CHECK-PCH-NEXT:       ],
+// CHECK-PCH-NEXT:       "file-deps": [
+// CHECK-PCH-NEXT:         "[[PREFIX]]/pch.h"
+// CHECK-PCH-NEXT:       ],
+// CHECK-PCH-NEXT:       "input-file": "[[PREFIX]]/pch.h"
+// CHECK-PCH-NEXT:     }
+// CHECK-PCH-NEXT:   ]
+// CHECK-PCH-NEXT: }
+
+// Explicitly build the PCH:
+//
+// RUN: tail -n +2 %t/result_pch.json > %t/result_pch_stripped.json
+// RUN: %python %S/../../utils/module-deps-to-rsp.py %t/result_pch_stripped.json \
+// RUN:   --module-name=ModCommon > %t/mod_common.cc1.rsp
+// RUN: %python %S/../../utils/module-deps-to-rsp.py %t/result_pch_stripped.json \
+// RUN:   --tu-index=0 > %t/pch.rsp
+//
+// RUN: %clang @%t/mod_common.cc1.rsp
+// RUN: %clang -x c-header %t/pch.h -fmodules -gmodules -fimplicit-module-maps \
+// RUN:   -fmodules-cache-path=%t/cache -o %t/pch.h.gch @%t/pch.rsp
+
+// Scan dependencies of the TU:
+//
+// RUN: sed "s|DIR|%/t|g" %S/Inputs/modules-pch-common-submodule/cdb_tu.json > %t/cdb.json
+// RUN: echo -%t > %t/result_tu.json
+// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-full \
+// RUN:   -generate-modules-path-args -module-files-dir %t/build >> %t/result_tu.json
+// RUN: cat %t/result_tu.json | sed 's:\\\\\?:/:g' | FileCheck %s -check-prefix=CHECK-TU
+//
+// CHECK-TU:      -[[PREFIX:.*]]
+// CHECK-TU-NEXT: {
+// CHECK-TU-NEXT:   "modules": [
+// CHECK-TU-NEXT:     {
+// CHECK-TU-NEXT:       "clang-module-deps": [],
+// CHECK-TU-NEXT:       "clang-modulemap-file": "[[PREFIX]]/module.modulemap",
+// CHECK-TU-NEXT:       "command-line": [
+// CHECK-TU-NEXT:         "-cc1"
+// CHECK-TU:              "-fmodule-map-file=[[PREFIX]]/module.modulemap"
+// CHECK-TU:              "-emit-module"
+// CHECK-TU:              "-fmodule-file=[[PREFIX]]/build/[[HASH_MOD_COMMON:.*]]/ModCommon-{{.*}}.pcm"
+// CHECK-TU:              "-fmodules"
+// CHECK-TU:              "-fmodule-name=ModTU"
+// CHECK-TU:              "-fno-implicit-modules"
+// CHECK-TU:            ],
+// CHECK-TU-NEXT:       "context-hash": "[[HASH_MOD_TU:.*]]",
+// CHECK-TU-NEXT:       "file-deps": [
+// CHECK-TU-NEXT:         "[[PREFIX]]/mod_tu.h",
+// CHECK-TU-NEXT:         "[[PREFIX]]/module.modulemap"
+// CHECK-TU-NEXT:       ],
+// CHECK-TU-NEXT:       "name": "ModTU"
+// CHECK-TU-NEXT:     }
+// CHECK-TU-NEXT:   ],
+// CHECK-TU-NEXT:   "translation-units": [
+// CHECK-TU-NEXT:     {
+// CHECK-TU-NEXT:       "clang-context-hash": "[[HASH_TU:.*]]",
+// CHECK-TU-NEXT:       "clang-module-deps": [
+// CHECK-TU-NEXT:         {
+// CHECK-TU-NEXT:           "context-hash": "[[HASH_MOD_TU]]"
+// CHECK-TU-NEXT:           "module-name": "ModTU"
+// CHECK-TU-NEXT:         }
+// CHECK-TU-NEXT:       ],
+// CHECK-TU-NEXT:       "command-line": [
+// CHECK-TU-NEXT:         "-fno-implicit-modules",
+// CHECK-TU-NEXT:         "-fno-implicit-module-maps",
+// CHECK-TU-NEXT:         "-fmodule-file=[[PREFIX]]/build/[[HASH_MOD_TU:.*]]/ModTU-{{.*}}.pcm",
+// CHECK-TU-NEXT:         "-fmodule-map-file=[[PREFIX]]/module.modulemap"
+// CHECK-TU-NEXT:       ],
+// CHECK-TU-NEXT:       "file-deps": [
+// CHECK-TU-NEXT:         "[[PREFIX]]/tu.c",
+// CHECK-TU-NEXT:         "[[PREFIX]]/pch.h.gch"
+// CHECK-TU-NEXT:       ],
+// CHECK-TU-NEXT:       "input-file": "[[PREFIX]]/tu.c"
+// CHECK-TU-NEXT:     }
+// CHECK-TU-NEXT:   ]
+// CHECK-TU-NEXT: }
+
+// Explicitly build the TU:
+//
+// RUN: tail -n +2 %t/result_tu.json > %t/result_tu_stripped.json
+// RUN: %python %S/../../utils/module-deps-to-rsp.py %t/result_tu_stripped.json \
+// RUN:   --module-name=ModTU > %t/mod_tu.cc1.rsp
+// RUN: %python %S/../../utils/module-deps-to-rsp.py %t/result_tu_stripped.json \
+// RUN:   --tu-index=0 > %t/tu.rsp
+//
+// RUN: %clang @%t/mod_tu.cc1.rsp
+// RUN: %clang -fsyntax-only %t/tu.c -fmodules -gmodules -fimplicit-module-maps \
+// RUN:   -fmodules-cache-path=%t/cache -include %t/pch.h -o %t/tu.o @%t/tu.rsp


        


More information about the cfe-commits mailing list