diff --git a/clang/include/clang/Frontend/FrontendActions.h b/clang/include/clang/Frontend/FrontendActions.h --- a/clang/include/clang/Frontend/FrontendActions.h +++ b/clang/include/clang/Frontend/FrontendActions.h @@ -34,6 +34,17 @@ bool usesPreprocessorOnly() const override { return false; } }; +/// Preprocessor-based frontend action that also loads PCH files. +class ReadPCHAndPreprocessAction : public FrontendAction { + void ExecuteAction() override; + + std::unique_ptr CreateASTConsumer(CompilerInstance &CI, + StringRef InFile) override; + +public: + bool usesPreprocessorOnly() const override { return false; } +}; + class DumpCompilerOptionsAction : public FrontendAction { std::unique_ptr CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override { diff --git a/clang/lib/Frontend/FrontendActions.cpp b/clang/lib/Frontend/FrontendActions.cpp --- a/clang/lib/Frontend/FrontendActions.cpp +++ b/clang/lib/Frontend/FrontendActions.cpp @@ -62,6 +62,27 @@ void InitOnlyAction::ExecuteAction() { } +// Basically PreprocessOnlyAction::ExecuteAction. +void ReadPCHAndPreprocessAction::ExecuteAction() { + Preprocessor &PP = getCompilerInstance().getPreprocessor(); + + // Ignore unknown pragmas. + PP.IgnorePragmas(); + + Token Tok; + // Start parsing the specified input file. + PP.EnterMainSourceFile(); + do { + PP.Lex(Tok); + } while (Tok.isNot(tok::eof)); +} + +std::unique_ptr +ReadPCHAndPreprocessAction::CreateASTConsumer(CompilerInstance &CI, + StringRef InFile) { + return std::make_unique(); +} + //===----------------------------------------------------------------------===// // AST Consumer Actions //===----------------------------------------------------------------------===// 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 @@ -73,6 +73,8 @@ if (!Compiler.hasDiagnostics()) return false; + Compiler.getPreprocessorOpts().AllowPCHWithDifferentModulesCachePath = true; + // Use the dependency scanning optimized file system if we can. if (DepFS) { const CompilerInvocation &CI = Compiler.getInvocation(); @@ -133,7 +135,7 @@ // the impact of strict context hashing. Compiler.getHeaderSearchOpts().ModulesStrictContextHash = true; - auto Action = std::make_unique(); + auto Action = std::make_unique(); const bool Result = Compiler.ExecuteAction(*Action); if (!DepFS) FileMgr->clearStatCache(); diff --git a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp --- a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp +++ b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp @@ -156,6 +156,9 @@ MDC.MainFile = std::string( Instance.getSourceManager().getFileEntryForID(MainFileID)->getName()); + if (!Instance.getPreprocessorOpts().ImplicitPCHInclude.empty()) + MDC.FileDeps.push_back(Instance.getPreprocessorOpts().ImplicitPCHInclude); + for (const Module *M : DirectModularDeps) handleTopLevelModule(M); diff --git a/clang/test/ClangScanDeps/modules-pch.c b/clang/test/ClangScanDeps/modules-pch.c --- a/clang/test/ClangScanDeps/modules-pch.c +++ b/clang/test/ClangScanDeps/modules-pch.c @@ -9,5 +9,52 @@ // Scan dependencies of the TU: // // RUN: sed "s|DIR|%/t|g" %S/Inputs/modules-pch/cdb_tu.json > %t/cdb.json +// RUN: echo -%t > %t/result_tu.json +// FIXME: Make this work with '-mode preprocess-minimized-sources'. // RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-full \ -// RUN: -generate-modules-path-args -module-files-dir %t/build +// RUN: -generate-modules-path-args -module-files-dir %t/build -mode preprocess >> %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: "-emit-module", +// 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: }