[clang][deps] Support object files
authorJan Svoboda <jan_svoboda@apple.com>
Wed, 2 Jun 2021 12:49:14 +0000 (14:49 +0200)
committerJan Svoboda <jan_svoboda@apple.com>
Fri, 4 Jun 2021 12:58:42 +0000 (14:58 +0200)
When a project uses PCH with explicit modules, the build will look like this:

1. scan PCH dependencies
2. explicitly build PCH
3. scan TU dependencies
4. explicitly build TU

Step 2 produces an object file for the PCH, which the dependency scanner needs to read in step 3. This patch adds support for this.

The `clang-scan-deps` invocation in the attached test would fail without this change.

Depends on D103516.

Reviewed By: Bigcheese

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

clang/lib/Tooling/DependencyScanning/CMakeLists.txt
clang/lib/Tooling/DependencyScanning/DependencyScanningService.cpp
clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
clang/test/ClangScanDeps/Inputs/modules-pch/cdb_tu.json [new file with mode: 0644]
clang/test/ClangScanDeps/Inputs/modules-pch/mod_tu.h [new file with mode: 0644]
clang/test/ClangScanDeps/Inputs/modules-pch/module.modulemap [new file with mode: 0644]
clang/test/ClangScanDeps/Inputs/modules-pch/pch.h [new file with mode: 0644]
clang/test/ClangScanDeps/Inputs/modules-pch/tu.c [new file with mode: 0644]
clang/test/ClangScanDeps/modules-pch.c [new file with mode: 0644]

index c6fe207..ce455e5 100644 (file)
@@ -1,4 +1,5 @@
 set(LLVM_LINK_COMPONENTS
+  ${LLVM_TARGETS_TO_BUILD}
   Core
   Support
   )
@@ -16,6 +17,7 @@ add_clang_library(clangDependencyScanning
   LINK_LIBS
   clangAST
   clangBasic
+  clangCodeGen
   clangDriver
   clangFrontend
   clangFrontendTool
index 93bb0cd..4f3e574 100644 (file)
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
+#include "llvm/Support/TargetSelect.h"
 
 using namespace clang;
 using namespace tooling;
@@ -16,4 +17,10 @@ DependencyScanningService::DependencyScanningService(
     ScanningMode Mode, ScanningOutputFormat Format, bool ReuseFileManager,
     bool SkipExcludedPPRanges)
     : Mode(Mode), Format(Format), ReuseFileManager(ReuseFileManager),
-      SkipExcludedPPRanges(SkipExcludedPPRanges) {}
+      SkipExcludedPPRanges(SkipExcludedPPRanges) {
+  // Initialize targets for object file support.
+  llvm::InitializeAllTargets();
+  llvm::InitializeAllTargetMCs();
+  llvm::InitializeAllAsmPrinters();
+  llvm::InitializeAllAsmParsers();
+}
index 63264b0..ecaeeed 100644 (file)
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/Tooling/DependencyScanning/DependencyScanningWorker.h"
+#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Frontend/CompilerInvocation.h"
 #include "clang/Frontend/FrontendActions.h"
@@ -153,7 +154,15 @@ DependencyScanningWorker::DependencyScanningWorker(
     DependencyScanningService &Service)
     : Format(Service.getFormat()) {
   DiagOpts = new DiagnosticOptions();
+
   PCHContainerOps = std::make_shared<PCHContainerOperations>();
+  PCHContainerOps->registerReader(
+      std::make_unique<ObjectFilePCHContainerReader>());
+  // We don't need to write object files, but the current PCH implementation
+  // requires the writer to be registered as well.
+  PCHContainerOps->registerWriter(
+      std::make_unique<ObjectFilePCHContainerWriter>());
+
   RealFS = llvm::vfs::createPhysicalFileSystem();
   if (Service.canSkipExcludedPPRanges())
     PPSkipMappings =
diff --git a/clang/test/ClangScanDeps/Inputs/modules-pch/cdb_tu.json b/clang/test/ClangScanDeps/Inputs/modules-pch/cdb_tu.json
new file mode 100644 (file)
index 0000000..8aad2cc
--- /dev/null
@@ -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/mod_tu.h b/clang/test/ClangScanDeps/Inputs/modules-pch/mod_tu.h
new file mode 100644 (file)
index 0000000..6237d1e
--- /dev/null
@@ -0,0 +1 @@
+// mod_tu.h
diff --git a/clang/test/ClangScanDeps/Inputs/modules-pch/module.modulemap b/clang/test/ClangScanDeps/Inputs/modules-pch/module.modulemap
new file mode 100644 (file)
index 0000000..405ba5c
--- /dev/null
@@ -0,0 +1,3 @@
+module ModTU {
+    header "mod_tu.h"
+}
diff --git a/clang/test/ClangScanDeps/Inputs/modules-pch/pch.h b/clang/test/ClangScanDeps/Inputs/modules-pch/pch.h
new file mode 100644 (file)
index 0000000..6222fce
--- /dev/null
@@ -0,0 +1 @@
+// pch.h
diff --git a/clang/test/ClangScanDeps/Inputs/modules-pch/tu.c b/clang/test/ClangScanDeps/Inputs/modules-pch/tu.c
new file mode 100644 (file)
index 0000000..228ab2b
--- /dev/null
@@ -0,0 +1,3 @@
+// tu.c
+
+#include "mod_tu.h"
diff --git a/clang/test/ClangScanDeps/modules-pch.c b/clang/test/ClangScanDeps/modules-pch.c
new file mode 100644 (file)
index 0000000..44b1a0d
--- /dev/null
@@ -0,0 +1,13 @@
+// RUN: rm -rf %t && mkdir %t
+// RUN: cp %S/Inputs/modules-pch/* %t
+
+// Explicitly build the PCH:
+//
+// RUN: %clang -x c-header %t/pch.h -fmodules -gmodules -fimplicit-module-maps \
+// RUN:   -fmodules-cache-path=%t/cache -o %t/pch.h.gch
+
+// Scan dependencies of the TU:
+//
+// RUN: sed "s|DIR|%/t|g" %S/Inputs/modules-pch/cdb_tu.json > %t/cdb_tu.json
+// RUN: clang-scan-deps -compilation-database %t/cdb_tu.json -format experimental-full \
+// RUN:   -generate-modules-path-args -module-files-dir %t/build