diff --git a/clang/lib/Tooling/DependencyScanning/CMakeLists.txt b/clang/lib/Tooling/DependencyScanning/CMakeLists.txt --- a/clang/lib/Tooling/DependencyScanning/CMakeLists.txt +++ b/clang/lib/Tooling/DependencyScanning/CMakeLists.txt @@ -1,4 +1,5 @@ set(LLVM_LINK_COMPONENTS + ${LLVM_TARGETS_TO_BUILD} Core Support ) @@ -16,6 +17,7 @@ LINK_LIBS clangAST clangBasic + clangCodeGen clangDriver clangFrontend clangFrontendTool diff --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningService.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningService.cpp --- a/clang/lib/Tooling/DependencyScanning/DependencyScanningService.cpp +++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningService.cpp @@ -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 @@ 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(); +} diff --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp --- a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp +++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp @@ -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 @@ DependencyScanningService &Service) : Format(Service.getFormat()) { DiagOpts = new DiagnosticOptions(); + PCHContainerOps = std::make_shared(); + PCHContainerOps->registerReader( + std::make_unique()); + // 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()); + 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 --- /dev/null +++ b/clang/test/ClangScanDeps/Inputs/modules-pch/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/mod_tu.h b/clang/test/ClangScanDeps/Inputs/modules-pch/mod_tu.h new file mode 100644 --- /dev/null +++ b/clang/test/ClangScanDeps/Inputs/modules-pch/mod_tu.h @@ -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 --- /dev/null +++ b/clang/test/ClangScanDeps/Inputs/modules-pch/module.modulemap @@ -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 --- /dev/null +++ b/clang/test/ClangScanDeps/Inputs/modules-pch/pch.h @@ -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 --- /dev/null +++ b/clang/test/ClangScanDeps/Inputs/modules-pch/tu.c @@ -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 --- /dev/null +++ b/clang/test/ClangScanDeps/modules-pch.c @@ -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