diff --git a/lib/ClangImporter/ClangModuleDependencyScanner.cpp b/lib/ClangImporter/ClangModuleDependencyScanner.cpp index 61c02e854c337..c1dab9e58afca 100644 --- a/lib/ClangImporter/ClangModuleDependencyScanner.cpp +++ b/lib/ClangImporter/ClangModuleDependencyScanner.cpp @@ -81,25 +81,6 @@ std::vector ClangImporter::getClangDepScanningInvocationArguments( return commandLineArgs; } -static std::unique_ptr -getClangPrefixMapper(DependencyScanningTool &clangScanningTool, - ModuleDeps &clangModuleDep, - clang::CompilerInvocation &depsInvocation) { - std::unique_ptr Mapper; - if (clangModuleDep.IncludeTreeID) { - Mapper = std::make_unique(); - } else if (clangModuleDep.CASFileSystemRootID) { - assert(clangScanningTool.getCachingFileSystem()); - Mapper = std::make_unique( - clangScanningTool.getCachingFileSystem()); - } - - if (Mapper) - DepscanPrefixMapping::configurePrefixMapper(depsInvocation, *Mapper); - - return Mapper; -} - ModuleDependencyVector ClangImporter::bridgeClangModuleDependencies( const ASTContext &ctx, clang::tooling::dependencies::DependencyScanningTool &clangScanningTool, @@ -147,63 +128,30 @@ ModuleDependencyVector ClangImporter::bridgeClangModuleDependencies( // Swift frontend option for input file path (Foo.modulemap). swiftArgs.push_back(remapPath(clangModuleDep.ClangModuleMapFile)); - // Handle VFSOverlay. If caching is enabled, there is no need for overlay. - if (!ctx.CASOpts.EnableCaching) { - for (auto &overlay : ctx.SearchPathOpts.VFSOverlayFiles) { - swiftArgs.push_back("-vfsoverlay"); - swiftArgs.push_back(remapPath(overlay)); - } - } - - // Add args reported by the scanner. - - // Round-trip clang args to canonicalize and clear the options that swift - // compiler doesn't need. - clang::CompilerInvocation depsInvocation; - clang::DiagnosticsEngine clangDiags(new clang::DiagnosticIDs(), - new clang::DiagnosticOptions(), - new clang::IgnoringDiagConsumer()); - - llvm::SmallVector clangArgs; - llvm::for_each( - clangModuleDep.getBuildArguments(), - [&](const std::string &Arg) { clangArgs.push_back(Arg.c_str()); }); - - bool success = clang::CompilerInvocation::CreateFromArgs( - depsInvocation, clangArgs, clangDiags); - (void)success; - assert(success && "clang option from dep scanner round trip failed"); - - // Create a prefix mapper that matches clang's configuration. - auto Mapper = - getClangPrefixMapper(clangScanningTool, clangModuleDep, depsInvocation); - - // Clear the cache key for module. The module key is computed from clang - // invocation, not swift invocation. - depsInvocation.getFrontendOpts().ModuleCacheKeys.clear(); - depsInvocation.getFrontendOpts().PathPrefixMappings.clear(); - depsInvocation.getFrontendOpts().OutputFile.clear(); + auto invocation = clangModuleDep.getUnderlyingCompilerInvocation(); + // Clear some options from clang scanner. + invocation.getMutFrontendOpts().ModuleCacheKeys.clear(); + invocation.getMutFrontendOpts().PathPrefixMappings.clear(); + invocation.getMutFrontendOpts().OutputFile.clear(); // Reset CASOptions since that should be coming from swift. - depsInvocation.getCASOpts() = clang::CASOptions(); - depsInvocation.getFrontendOpts().CASIncludeTreeID.clear(); + invocation.getMutCASOpts() = clang::CASOptions(); + invocation.getMutFrontendOpts().CASIncludeTreeID.clear(); // FIXME: workaround for rdar://105684525: find the -ivfsoverlay option // from clang scanner and pass to swift. - for (auto overlay : depsInvocation.getHeaderSearchOpts().VFSOverlayFiles) { - if (llvm::is_contained(ctx.SearchPathOpts.VFSOverlayFiles, overlay)) - continue; - swiftArgs.push_back("-vfsoverlay"); - swiftArgs.push_back(overlay); + if (!ctx.CASOpts.EnableCaching) { + auto &overlayFiles = invocation.getMutHeaderSearchOpts().VFSOverlayFiles; + for (auto overlay : overlayFiles) { + swiftArgs.push_back("-vfsoverlay"); + swiftArgs.push_back(overlay); + } + // Clear overlay files since they are forwarded from swift to clang. + overlayFiles.clear(); } - llvm::BumpPtrAllocator allocator; - llvm::StringSaver saver(allocator); - clangArgs.clear(); - depsInvocation.generateCC1CommandLine( - clangArgs, - [&saver](const llvm::Twine &T) { return saver.save(T).data(); }); - + // Add args reported by the scanner. + auto clangArgs = invocation.getCC1CommandLine(); llvm::for_each(clangArgs, addClangArg); // CASFileSystemRootID. @@ -221,10 +169,7 @@ ModuleDependencyVector ClangImporter::bridgeClangModuleDependencies( swiftArgs.push_back("-clang-include-tree-root"); swiftArgs.push_back(IncludeTree); } - - std::string mappedPCMPath = pcmPath; - if (Mapper) - Mapper->mapInPlace(mappedPCMPath); + std::string mappedPCMPath = remapPath(pcmPath); std::vector LinkLibraries; for (const auto &ll : clangModuleDep.LinkLibraries) diff --git a/test/ScanDependencies/preserve_used_vfs.swift b/test/ScanDependencies/preserve_used_vfs.swift index 4aef2c36d45b9..745c7918ef987 100644 --- a/test/ScanDependencies/preserve_used_vfs.swift +++ b/test/ScanDependencies/preserve_used_vfs.swift @@ -1,17 +1,45 @@ // REQUIRES: objc_interop // RUN: %empty-directory(%t) // RUN: %empty-directory(%t/module-cache) -// RUN: %empty-directory(%t/redirects) +// RUN: %empty-directory(%t/inputs) // RUN: split-file %s %t -// RUN: sed -e "s|OUT_DIR|%t/redirects|g" -e "s|IN_DIR|%S/Inputs/CHeaders|g" %t/overlay_template.yaml > %t/overlay.yaml +// RUN: sed -e "s|OUT_DIR|%t/redirects|g" -e "s|IN_DIR|%t/inputs|g" %t/overlay_template.yaml > %t/overlay.yaml -// RUN: %target-swift-frontend -scan-dependencies -module-load-mode prefer-interface -module-cache-path %t/module-cache %t/test.swift -o %t/deps.json -I %S/Inputs/CHeaders -I %S/Inputs/Swift -disable-implicit-concurrency-module-import -disable-implicit-string-processing-module-import -Xcc -ivfsoverlay -Xcc %t/overlay.yaml +// RUN: %target-swift-frontend -scan-dependencies -module-load-mode prefer-serialized -module-cache-path %t/module-cache %t/test.swift -o %t/deps.json -I %t/inputs -I %S/Inputs/Swift -disable-implicit-concurrency-module-import -disable-implicit-string-processing-module-import -Xcc -ivfsoverlay -Xcc %t/overlay.yaml // RUN: %validate-json %t/deps.json | %FileCheck %s +// RUN: %{python} %S/../CAS/Inputs/BuildCommandExtractor.py %t/deps.json clang:SwiftShims > %t/shim.cmd +// RUN: %swift_frontend_plain @%t/shim.cmd +// RUN: %{python} %S/../CAS/Inputs/BuildCommandExtractor.py %t/deps.json Swift > %t/swift.cmd +// RUN: %swift_frontend_plain @%t/swift.cmd +// RUN: %{python} %S/../CAS/Inputs/BuildCommandExtractor.py %t/deps.json SwiftOnoneSupport > %t/onone.cmd +// RUN: %swift_frontend_plain @%t/onone.cmd +// RUN: %{python} %S/../CAS/Inputs/BuildCommandExtractor.py %t/deps.json clang:F > %t/F.cmd +// RUN: %swift_frontend_plain @%t/F.cmd +// RUN: %{python} %S/../CAS/Inputs/BuildCommandExtractor.py %t/deps.json F > %t/SwiftF.cmd +// RUN: %swift_frontend_plain @%t/SwiftF.cmd + +// RUN: %{python} %S/../CAS/Inputs/GenerateExplicitModuleMap.py %t/deps.json > %t/map.json +// RUN: %{python} %S/../CAS/Inputs/BuildCommandExtractor.py %t/deps.json Test > %t/MyApp.cmd +// RUN: echo "\"-disable-implicit-string-processing-module-import\"" >> %t/MyApp.cmd +// RUN: echo "\"-disable-implicit-concurrency-module-import\"" >> %t/MyApp.cmd +// RUN: echo "\"-disable-implicit-swift-modules\"" >> %t/MyApp.cmd +// RUN: echo "\"-explicit-swift-module-map-file\"" >> %t/MyApp.cmd +// RUN: echo "\"%t/map.json\"" >> %t/MyApp.cmd + +// RUN: %target-swift-frontend @%t/MyApp.cmd %t/test.swift -Xcc -ivfsoverlay -Xcc %t/overlay.yaml \ +// RUN: -emit-module -o %t/Test.swiftmodule + //--- redirects/RedirectedF.h void funcRedirectedF(void); +//--- redirects/modulemap +module F { + header "F_2.h" + export * +} + //--- overlay_template.yaml { 'version': 0, @@ -20,8 +48,11 @@ void funcRedirectedF(void); { 'name': 'IN_DIR', 'type': 'directory', 'contents': [ - { 'name': 'F.h', 'type': 'file', + { 'name': 'F_2.h', 'type': 'file', 'external-contents': 'OUT_DIR/RedirectedF.h' + }, + { 'name': 'module.modulemap', 'type': 'file', + 'external-contents': 'OUT_DIR/modulemap' } ] }, @@ -31,6 +62,8 @@ void funcRedirectedF(void); //--- test.swift import F +func testF() { funcRedirectedF() } + // CHECK: "mainModuleName": "deps" /// --------Main module // CHECK-LABEL: "modulePath": "deps.swiftmodule", @@ -68,7 +101,4 @@ import F // CHECK: "commandLine": [ // CHECK: "-vfsoverlay", // CHECK-NEXT: "{{.*}}{{/|\\}}preserve_used_vfs.swift.tmp{{/|\\}}overlay.yaml", -// CHECK: "-ivfsoverlay", -// CHECK-NEXT: "-Xcc", -// CHECK-NEXT: "{{.*}}{{/|\\}}preserve_used_vfs.swift.tmp{{/|\\}}overlay.yaml", // CHECK: ],