From 38686bd40275830dd4f62ee6a9b809ff89346b5a Mon Sep 17 00:00:00 2001 From: Jan Svoboda Date: Wed, 31 Aug 2022 11:29:42 -0700 Subject: [PATCH 1/2] [importer] Adjust for new dependency scanner API --- .../ClangModuleDependencyScanner.cpp | 62 ++++++++----------- .../module_deps_cache_reuse.swift | 2 - 2 files changed, 26 insertions(+), 38 deletions(-) diff --git a/lib/ClangImporter/ClangModuleDependencyScanner.cpp b/lib/ClangImporter/ClangModuleDependencyScanner.cpp index 097f03ed76427..14391e372755c 100644 --- a/lib/ClangImporter/ClangModuleDependencyScanner.cpp +++ b/lib/ClangImporter/ClangModuleDependencyScanner.cpp @@ -28,6 +28,22 @@ using namespace swift; using namespace clang::tooling; using namespace clang::tooling::dependencies; +static std::string lookupModuleOutput(const ModuleID &MID, + ModuleOutputKind MOK) { + // Deciding the output paths is done in swift-driver. + switch (MOK) { + case ModuleOutputKind::ModuleFile: + return ""; + case ModuleOutputKind::DependencyFile: + return ""; + case ModuleOutputKind::DependencyTargets: + return MID.ModuleName + "-" + MID.ContextHash; + case ModuleOutputKind::DiagnosticSerializationFile: + return ""; + } + llvm_unreachable("Fully covered switch above!"); +} + // Add search paths. // Note: This is handled differently for the Clang importer itself, which // adds search paths to Clang's data structures rather than to its @@ -119,13 +135,6 @@ void ClangImporter::recordModuleDependencies( for (const auto &fileDep : clangModuleDep.FileDeps) { fileDeps.push_back(fileDep.getKey().str()); } - // Inherit all Clang driver args when creating the clang importer. - ArrayRef allArgs = Impl.ClangArgs; - ClangImporterOptions Opts; - - // Ensure the arguments we collected is sufficient to create a Clang - // invocation. - assert(createClangInvocation(this, Opts, nullptr, allArgs)); std::vector swiftArgs; // We are using Swift frontend mode. @@ -142,22 +151,13 @@ void ClangImporter::recordModuleDependencies( addClangArg(arg); }; - // Add all args inherited from creating the importer. - auto It = allArgs.begin(); + // Add args reported by the scanner. + auto It = clangModuleDep.BuildArguments.begin(); + auto ItEnd = clangModuleDep.BuildArguments.end(); + // Skip -cc1. + ++It; - { - StringRef arg = *It; - if (arg == "clang" || - arg.endswith(llvm::sys::path::get_separator().str() + "clang")) { - // Remove the initial path to clang executable argument, to avoid - // treating it as an executable input to compilation. It is not needed - // because the consumer of this command-line will invoke the emit-PCM - // action via swift-frontend. - It += 1; - } - } - - while(It != allArgs.end()) { + while(It != ItEnd) { StringRef arg = *It; // Remove the -target arguments because we should use the target triple // specified with `-clang-target` on the scanner invocation, or @@ -169,22 +169,11 @@ void ClangImporter::recordModuleDependencies( // specified in the interface file. It += 1; } else { - addClangArg(*It); + addClangFrontendArg(*It); ++ It; } } - // Add the equivalent of the old `getAdditionalArgsWithoutModulePaths`. - // TODO: Should we be passing all cc1 args (ie. - // `getCanonicalCommandLineWithoutModulePaths`)? - addClangFrontendArg("-fno-implicit-modules"); - addClangFrontendArg("-emit-module"); - addClangFrontendArg(Twine("-fmodule-name=") + clangModuleDep.ID.ModuleName); - if (clangModuleDep.IsSystem) - addClangFrontendArg("-fsystem-module"); - if (clangModuleDep.BuildInvocation.getLangOpts()->NeededByPCHOrCompilationUsesPCH) - addClangFrontendArg("-fmodule-related-to-pch"); - // If the scanner is invoked with '-clang-target', ensure this is the target // used to build this PCM. if (Impl.SwiftContext.LangOpts.ClangTarget.hasValue()) { @@ -267,7 +256,7 @@ Optional ClangImporter::getModuleDependencies( auto clangDependencies = cache.getClangScannerTool().getFullDependencies( commandLineArgs, workingDir, cache.getAlreadySeenClangModules(), - moduleName); + lookupModuleOutput, moduleName); if (!clangDependencies) { auto errorStr = toString(clangDependencies.takeError()); // We ignore the "module 'foo' not found" error, the Swift dependency @@ -322,7 +311,8 @@ bool ClangImporter::addBridgingHeaderDependencies( ctx.SourceMgr.getFileSystem()->getCurrentWorkingDirectory().get(); auto clangDependencies = cache.getClangScannerTool().getFullDependencies( - commandLineArgs, workingDir, cache.getAlreadySeenClangModules()); + commandLineArgs, workingDir, cache.getAlreadySeenClangModules(), + lookupModuleOutput); if (!clangDependencies) { // FIXME: Route this to a normal diagnostic. llvm::logAllUnhandledErrors(clangDependencies.takeError(), llvm::errs()); diff --git a/test/ScanDependencies/module_deps_cache_reuse.swift b/test/ScanDependencies/module_deps_cache_reuse.swift index 4d0a8654c815d..acec559905f7f 100644 --- a/test/ScanDependencies/module_deps_cache_reuse.swift +++ b/test/ScanDependencies/module_deps_cache_reuse.swift @@ -114,8 +114,6 @@ import SubE // CHECK-NEXT: "-frontend" // CHECK-NEXT: "-only-use-extra-clang-opts // CHECK-NOT: "BUILD_DIR/bin/clang" -// CHECK: "-Xcc" -// CHECK-NEXT: "-fsyntax-only", // CHECK: "-fsystem-module", // CHECK: "-emit-pcm", // CHECK: "-module-name", From c5bd61bf25e63a0fd06e9fd2ded62b5d7b457815 Mon Sep 17 00:00:00 2001 From: Artem Chikin Date: Tue, 27 Sep 2022 10:24:21 -0700 Subject: [PATCH 2/2] [Dependency Scanning] Adapt 'ClangModuleDependencyScanner' output to clang frontend-only results coming from scan-deps --- .../ClangModuleDependencyScanner.cpp | 54 +++++-------------- .../module_deps_cache_reuse.swift | 1 - 2 files changed, 14 insertions(+), 41 deletions(-) diff --git a/lib/ClangImporter/ClangModuleDependencyScanner.cpp b/lib/ClangImporter/ClangModuleDependencyScanner.cpp index 14391e372755c..4e6ba6b90c012 100644 --- a/lib/ClangImporter/ClangModuleDependencyScanner.cpp +++ b/lib/ClangImporter/ClangModuleDependencyScanner.cpp @@ -137,56 +137,32 @@ void ClangImporter::recordModuleDependencies( } std::vector swiftArgs; - // We are using Swift frontend mode. - swiftArgs.push_back("-frontend"); - // We pass the entire argument list via -Xcc, so the invocation should - // use extra clang options alone. - swiftArgs.push_back("-only-use-extra-clang-opts"); auto addClangArg = [&](Twine arg) { swiftArgs.push_back("-Xcc"); swiftArgs.push_back(arg.str()); }; - auto addClangFrontendArg = [&](Twine arg) { - addClangArg("-Xclang"); - addClangArg(arg); - }; - // Add args reported by the scanner. - auto It = clangModuleDep.BuildArguments.begin(); - auto ItEnd = clangModuleDep.BuildArguments.end(); - // Skip -cc1. - ++It; - - while(It != ItEnd) { - StringRef arg = *It; - // Remove the -target arguments because we should use the target triple - // specified with `-clang-target` on the scanner invocation, or - // from the depending Swift modules. - if (arg == "-target") { - It += 2; - } else if (arg.startswith("-fapinotes-swift-version=")) { - // Remove the apinotes version because we should use the language version - // specified in the interface file. - It += 1; - } else { - addClangFrontendArg(*It); - ++ It; - } - } + // We are using Swift frontend mode. + swiftArgs.push_back("-frontend"); - // If the scanner is invoked with '-clang-target', ensure this is the target - // used to build this PCM. - if (Impl.SwiftContext.LangOpts.ClangTarget.hasValue()) { - llvm::Triple triple = Impl.SwiftContext.LangOpts.ClangTarget.getValue(); - addClangArg("-target"); - addClangArg(triple.str()); - } + // We pass the entire argument list via -Xcc, so the invocation should + // use extra clang options alone. + swiftArgs.push_back("-only-use-extra-clang-opts"); // Swift frontend action: -emit-pcm swiftArgs.push_back("-emit-pcm"); swiftArgs.push_back("-module-name"); swiftArgs.push_back(clangModuleDep.ID.ModuleName); + // Ensure that the resulting PCM build invocation uses Clang frontend directly + swiftArgs.push_back("-direct-clang-cc1-module-build"); + + // Swift frontend option for input file path (Foo.modulemap). + swiftArgs.push_back(clangModuleDep.ClangModuleMapFile); + + // Add args reported by the scanner. + llvm::for_each(clangModuleDep.BuildArguments, addClangArg); + // Pass down search paths to the -emit-module action. // Unlike building Swift modules, we need to include all search paths to // the clang invocation to build PCMs because transitive headers can only @@ -201,8 +177,6 @@ void ClangImporter::recordModuleDependencies( addClangArg((path.IsSystem ? "-Fsystem": "-F") + path.Path); } - // Swift frontend option for input file path (Foo.modulemap). - swiftArgs.push_back(clangModuleDep.ClangModuleMapFile); // Module-level dependencies. llvm::StringSet<> alreadyAddedModules; auto dependencies = ModuleDependencies::forClangModule( diff --git a/test/ScanDependencies/module_deps_cache_reuse.swift b/test/ScanDependencies/module_deps_cache_reuse.swift index acec559905f7f..a5e3005af4bdb 100644 --- a/test/ScanDependencies/module_deps_cache_reuse.swift +++ b/test/ScanDependencies/module_deps_cache_reuse.swift @@ -114,7 +114,6 @@ import SubE // CHECK-NEXT: "-frontend" // CHECK-NEXT: "-only-use-extra-clang-opts // CHECK-NOT: "BUILD_DIR/bin/clang" -// CHECK: "-fsystem-module", // CHECK: "-emit-pcm", // CHECK: "-module-name", // CHECK-NEXT: "C"