Skip to content

Commit de07b1e

Browse files
committed
[clang][deps] Support object files
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
1 parent 20bd214 commit de07b1e

File tree

9 files changed

+47
-1
lines changed

9 files changed

+47
-1
lines changed

clang/lib/Tooling/DependencyScanning/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
set(LLVM_LINK_COMPONENTS
2+
${LLVM_TARGETS_TO_BUILD}
23
Core
34
Support
45
)
@@ -16,6 +17,7 @@ add_clang_library(clangDependencyScanning
1617
LINK_LIBS
1718
clangAST
1819
clangBasic
20+
clangCodeGen
1921
clangDriver
2022
clangFrontend
2123
clangFrontendTool

clang/lib/Tooling/DependencyScanning/DependencyScanningService.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
10+
#include "llvm/Support/TargetSelect.h"
1011

1112
using namespace clang;
1213
using namespace tooling;
@@ -16,4 +17,10 @@ DependencyScanningService::DependencyScanningService(
1617
ScanningMode Mode, ScanningOutputFormat Format, bool ReuseFileManager,
1718
bool SkipExcludedPPRanges)
1819
: Mode(Mode), Format(Format), ReuseFileManager(ReuseFileManager),
19-
SkipExcludedPPRanges(SkipExcludedPPRanges) {}
20+
SkipExcludedPPRanges(SkipExcludedPPRanges) {
21+
// Initialize targets for object file support.
22+
llvm::InitializeAllTargets();
23+
llvm::InitializeAllTargetMCs();
24+
llvm::InitializeAllAsmPrinters();
25+
llvm::InitializeAllAsmParsers();
26+
}

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "clang/Tooling/DependencyScanning/DependencyScanningWorker.h"
10+
#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
1011
#include "clang/Frontend/CompilerInstance.h"
1112
#include "clang/Frontend/CompilerInvocation.h"
1213
#include "clang/Frontend/FrontendActions.h"
@@ -153,7 +154,15 @@ DependencyScanningWorker::DependencyScanningWorker(
153154
DependencyScanningService &Service)
154155
: Format(Service.getFormat()) {
155156
DiagOpts = new DiagnosticOptions();
157+
156158
PCHContainerOps = std::make_shared<PCHContainerOperations>();
159+
PCHContainerOps->registerReader(
160+
std::make_unique<ObjectFilePCHContainerReader>());
161+
// We don't need to write object files, but the current PCH implementation
162+
// requires the writer to be registered as well.
163+
PCHContainerOps->registerWriter(
164+
std::make_unique<ObjectFilePCHContainerWriter>());
165+
157166
RealFS = llvm::vfs::createPhysicalFileSystem();
158167
if (Service.canSkipExcludedPPRanges())
159168
PPSkipMappings =
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"directory": "DIR",
4+
"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",
5+
"file": "DIR/tu.c"
6+
}
7+
]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// mod_tu.h
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module ModTU {
2+
header "mod_tu.h"
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// pch.h
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// tu.c
2+
3+
#include "mod_tu.h"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: rm -rf %t && mkdir %t
2+
// RUN: cp %S/Inputs/modules-pch/* %t
3+
4+
// Explicitly build the PCH:
5+
//
6+
// RUN: %clang -x c-header %t/pch.h -fmodules -gmodules -fimplicit-module-maps \
7+
// RUN: -fmodules-cache-path=%t/cache -o %t/pch.h.gch
8+
9+
// Scan dependencies of the TU:
10+
//
11+
// RUN: sed "s|DIR|%/t|g" %S/Inputs/modules-pch/cdb_tu.json > %t/cdb_tu.json
12+
// RUN: clang-scan-deps -compilation-database %t/cdb_tu.json -format experimental-full \
13+
// RUN: -generate-modules-path-args -module-files-dir %t/build

0 commit comments

Comments
 (0)