Skip to content

Commit d1adc24

Browse files
authored
Merge pull request swiftlang#81415 from artemcm/CXXInteropDarwinCycleFix
[Dependency Scanning][C++Interop] Do not query `CxxStdlib` Swift overlay for textual modules which were not built with c++interop
2 parents cda5964 + 94898aa commit d1adc24

File tree

4 files changed

+118
-32
lines changed

4 files changed

+118
-32
lines changed

include/swift/AST/ModuleDependencies.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ class ModuleDependencyInfo {
690690
storage->importedSwiftModules.assign(dependencyIDs.begin(),
691691
dependencyIDs.end());
692692
}
693-
const ArrayRef<ModuleDependencyID> getImportedSwiftDependencies() const {
693+
ArrayRef<ModuleDependencyID> getImportedSwiftDependencies() const {
694694
return storage->importedSwiftModules;
695695
}
696696

@@ -699,7 +699,7 @@ class ModuleDependencyInfo {
699699
storage->importedClangModules.assign(dependencyIDs.begin(),
700700
dependencyIDs.end());
701701
}
702-
const ArrayRef<ModuleDependencyID> getImportedClangDependencies() const {
702+
ArrayRef<ModuleDependencyID> getImportedClangDependencies() const {
703703
return storage->importedClangModules;
704704
}
705705

@@ -733,7 +733,7 @@ class ModuleDependencyInfo {
733733
}
734734
}
735735
}
736-
const ArrayRef<ModuleDependencyID> getHeaderClangDependencies() const {
736+
ArrayRef<ModuleDependencyID> getHeaderClangDependencies() const {
737737
switch (getKind()) {
738738
case swift::ModuleDependencyKind::SwiftInterface: {
739739
auto swiftInterfaceStorage =
@@ -761,7 +761,7 @@ class ModuleDependencyInfo {
761761
storage->swiftOverlayDependencies.assign(dependencyIDs.begin(),
762762
dependencyIDs.end());
763763
}
764-
const ArrayRef<ModuleDependencyID> getSwiftOverlayDependencies() const {
764+
ArrayRef<ModuleDependencyID> getSwiftOverlayDependencies() const {
765765
return storage->swiftOverlayDependencies;
766766
}
767767

@@ -771,11 +771,11 @@ class ModuleDependencyInfo {
771771
storage->crossImportOverlayModules.assign(dependencyIDs.begin(),
772772
dependencyIDs.end());
773773
}
774-
const ArrayRef<ModuleDependencyID> getCrossImportOverlayDependencies() const {
774+
ArrayRef<ModuleDependencyID> getCrossImportOverlayDependencies() const {
775775
return storage->crossImportOverlayModules;
776776
}
777777

778-
const ArrayRef<LinkLibrary> getLinkLibraries() const {
778+
ArrayRef<LinkLibrary> getLinkLibraries() const {
779779
return storage->linkLibraries;
780780
}
781781

@@ -784,7 +784,7 @@ class ModuleDependencyInfo {
784784
storage->linkLibraries.assign(linkLibraries.begin(), linkLibraries.end());
785785
}
786786

787-
const ArrayRef<std::string> getAuxiliaryFiles() const {
787+
ArrayRef<std::string> getAuxiliaryFiles() const {
788788
return storage->auxiliaryFiles;
789789
}
790790

@@ -796,7 +796,7 @@ class ModuleDependencyInfo {
796796
return false;
797797
}
798798

799-
const ArrayRef<std::string> getHeaderInputSourceFiles() const {
799+
ArrayRef<std::string> getHeaderInputSourceFiles() const {
800800
if (auto *detail = getAsSwiftInterfaceModule())
801801
return detail->textualModuleDetails.bridgingSourceFiles;
802802
if (auto *detail = getAsSwiftSourceModule())
@@ -806,7 +806,7 @@ class ModuleDependencyInfo {
806806
return {};
807807
}
808808

809-
std::vector<std::string> getCommandline() const {
809+
ArrayRef<std::string> getCommandline() const {
810810
if (auto *detail = getAsClangModule())
811811
return detail->buildCommandLine;
812812
if (auto *detail = getAsSwiftInterfaceModule())
@@ -829,7 +829,7 @@ class ModuleDependencyInfo {
829829
llvm_unreachable("Unexpected type");
830830
}
831831

832-
std::vector<std::string> getBridgingHeaderCommandline() const {
832+
ArrayRef<std::string> getBridgingHeaderCommandline() const {
833833
if (auto *detail = getAsSwiftSourceModule())
834834
return detail->bridgingHeaderBuildCommandLine;
835835
return {};

lib/DependencyScan/ModuleDependencyScanner.cpp

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ ModuleDependencyScanner::getMainModuleDependencyInfo(ModuleDecl *mainModule) {
468468
// build command to main module to ensure frontend gets the same result.
469469
// This needs to happen after visiting all the top-level decls from all
470470
// SourceFiles.
471-
auto buildArgs = mainDependencies.getCommandline();
471+
std::vector<std::string> buildArgs = mainDependencies.getCommandline();
472472
mainModule->getASTContext().forEachCanImportVersionCheck(
473473
[&](StringRef moduleName, const llvm::VersionTuple &Version,
474474
const llvm::VersionTuple &UnderlyingVersion) {
@@ -1328,25 +1328,33 @@ void ModuleDependencyScanner::resolveSwiftOverlayDependenciesForModule(
13281328
recordResult(clangDep);
13291329

13301330
// C++ Interop requires additional handling
1331-
if (ScanCompilerInvocation.getLangOptions().EnableCXXInterop) {
1332-
for (const auto &clangDepName : allClangDependencies) {
1333-
// If this Clang module is a part of the C++ stdlib, and we haven't loaded
1334-
// the overlay for it so far, it is a split libc++ module (e.g.
1335-
// std_vector). Load the CxxStdlib overlay explicitly.
1336-
const auto &clangDepInfo =
1337-
cache.findDependency(clangDepName, ModuleDependencyKind::Clang)
1338-
.value()
1339-
->getAsClangModule();
1340-
if (importer::isCxxStdModule(clangDepName, clangDepInfo->IsSystem) &&
1341-
!swiftOverlayDependencies.contains(
1342-
{clangDepName, ModuleDependencyKind::SwiftInterface}) &&
1343-
!swiftOverlayDependencies.contains(
1344-
{clangDepName, ModuleDependencyKind::SwiftBinary})) {
1345-
ScanningThreadPool.async(
1346-
scanForSwiftDependency,
1347-
getModuleImportIdentifier(ScanASTContext.Id_CxxStdlib.str()));
1348-
ScanningThreadPool.wait();
1349-
recordResult(ScanASTContext.Id_CxxStdlib.str().str());
1331+
if (ScanCompilerInvocation.getLangOptions().EnableCXXInterop &&
1332+
moduleID.Kind == ModuleDependencyKind::SwiftInterface) {
1333+
const auto &moduleInfo = cache.findKnownDependency(moduleID);
1334+
const auto commandLine = moduleInfo.getCommandline();
1335+
1336+
// If the textual interface was built without C++ interop, do not query
1337+
// the C++ Standard Library Swift overlay for its compilation.
1338+
if (llvm::find(commandLine, "-formal-cxx-interoperability-mode=off") ==
1339+
commandLine.end()) {
1340+
for (const auto &clangDepName : allClangDependencies) {
1341+
// If this Clang module is a part of the C++ stdlib, and we haven't
1342+
// loaded the overlay for it so far, it is a split libc++ module (e.g.
1343+
// std_vector). Load the CxxStdlib overlay explicitly.
1344+
const auto &clangDepInfo =
1345+
cache.findDependency(clangDepName, ModuleDependencyKind::Clang)
1346+
.value()
1347+
->getAsClangModule();
1348+
if (importer::isCxxStdModule(clangDepName, clangDepInfo->IsSystem) &&
1349+
!swiftOverlayDependencies.contains(
1350+
{clangDepName, ModuleDependencyKind::SwiftInterface}) &&
1351+
!swiftOverlayDependencies.contains(
1352+
{clangDepName, ModuleDependencyKind::SwiftBinary})) {
1353+
scanForSwiftDependency(
1354+
getModuleImportIdentifier(ScanASTContext.Id_CxxStdlib.str()));
1355+
recordResult(ScanASTContext.Id_CxxStdlib.str().str());
1356+
break;
1357+
}
13501358
}
13511359
}
13521360
}
@@ -1401,7 +1409,7 @@ void ModuleDependencyScanner::resolveCrossImportOverlayDependencies(
14011409
// Update the command-line on the main module to
14021410
// disable implicit cross-import overlay search.
14031411
auto mainDep = cache.findKnownDependency(actualMainID);
1404-
auto cmdCopy = mainDep.getCommandline();
1412+
std::vector<std::string> cmdCopy = mainDep.getCommandline();
14051413
cmdCopy.push_back("-disable-cross-import-overlay-search");
14061414
for (auto &entry : overlayFiles) {
14071415
mainDep.addAuxiliaryFile(entry.second);

lib/DependencyScan/ScanDependencies.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ generateFullDependencyGraph(const CompilerInstance &instance,
933933
moduleInfo->details = getModuleDetails();
934934

935935
// Create a link libraries set for this module
936-
auto &linkLibraries = moduleDependencyInfo.getLinkLibraries();
936+
auto linkLibraries = moduleDependencyInfo.getLinkLibraries();
937937
swiftscan_link_library_set_t *linkLibrarySet =
938938
new swiftscan_link_library_set_t;
939939
linkLibrarySet->count = linkLibraries.size();
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %empty-directory(%t/deps)
3+
// RUN: split-file %s %t
4+
5+
// RUN: %target-swift-frontend -scan-dependencies -o %t/deps.json %t/clientWithInteropDep.swift -I %t/deps -cxx-interoperability-mode=default -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -verify
6+
// RUN: cat %t/deps.json | %FileCheck %s -check-prefix=ENABLE-CHECK
7+
8+
// RUN: %target-swift-frontend -scan-dependencies -o %t/deps_no_interop_dep.json %t/clientNoInteropDep.swift -I %t/deps -cxx-interoperability-mode=default -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -verify
9+
// RUN: cat %t/deps_no_interop_dep.json | %FileCheck %s -check-prefix=DISABLE-CHECK
10+
11+
//--- deps/bar.h
12+
void bar(void);
13+
14+
//--- deps/module.modulemap
15+
module std_Bar [system] {
16+
header "bar.h"
17+
export *
18+
}
19+
20+
//--- deps/Foo.swiftinterface
21+
// swift-interface-format-version: 1.0
22+
// swift-module-flags: -module-name Foo -enable-library-evolution
23+
import std_Bar
24+
public struct Foo1 {}
25+
26+
//--- deps/FooNoInterop.swiftinterface
27+
// swift-interface-format-version: 1.0
28+
// swift-module-flags: -module-name FooNoInterop -enable-library-evolution
29+
// swift-module-flags-ignorable: -formal-cxx-interoperability-mode=off
30+
import std_Bar
31+
public struct Foo2 {}
32+
33+
//--- clientWithInteropDep.swift
34+
import Foo
35+
36+
//--- clientNoInteropDep.swift
37+
import FooNoInterop
38+
39+
// Ensure that when the 'Foo' dependency was built with C++ interop enabled,
40+
// it gets the C++ standard library overlay for its 'std_*' dependency
41+
//
42+
// 'Foo' as it appears in direct deps
43+
// ENABLE-CHECK: "swift": "Foo"
44+
// 'Foo' as it appears in source-import deps
45+
// ENABLE-CHECK: "swift": "Foo"
46+
// Actual dependency info node
47+
// ENABLE-CHECK: "swift": "Foo"
48+
// ENABLE-CHECK: "directDependencies": [
49+
// ENABLE-CHECK: {
50+
// ENABLE-CHECK: "swift": "SwiftOnoneSupport"
51+
// ENABLE-CHECK: },
52+
// ENABLE-CHECK: {
53+
// ENABLE-CHECK: "swift": "CxxStdlib"
54+
// ENABLE-CHECK: },
55+
// ENABLE-CHECK: {
56+
// ENABLE-CHECK: "clang": "std_Bar"
57+
// ENABLE-CHECK: }
58+
// ENABLE-CHECK: ],
59+
60+
// Ensure that when the 'Foo' dependency was *not* built with C++ interop enabled,
61+
// it does not get the C++ standard library overlay for its 'std_*' dependency
62+
//
63+
// 'Foo' as it appears in direct deps
64+
// DISABLE-CHECK: "swift": "FooNoInterop"
65+
// 'Foo' as it appears in source-import deps
66+
// DISABLE-CHECK: "swift": "FooNoInterop"
67+
// Actual dependency info node
68+
// DISABLE-CHECK: "swift": "FooNoInterop"
69+
// DISABLE-CHECK: "directDependencies": [
70+
// DISABLE-CHECK: {
71+
// DISABLE-CHECK: "swift": "SwiftOnoneSupport"
72+
// DISABLE-CHECK: },
73+
// DISABLE-CHECK: {
74+
// DISABLE-CHECK: "clang": "std_Bar"
75+
// DISABLE-CHECK: }
76+
// DISABLE-CHECK: ],
77+
78+

0 commit comments

Comments
 (0)