Skip to content

[SymbolGraph] rename symbol graph files for cross-import overlays #38039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/SymbolGraphGen/SymbolGraphASTWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ SymbolGraph *SymbolGraphASTWalker::getModuleSymbolGraph(const Decl *D) {

if (this->M.getNameStr().equals(M->getNameStr())) {
return &MainGraph;
} else if (MainGraph.DeclaringModule.hasValue() &&
MainGraph.DeclaringModule.getValue()->getNameStr().equals(M->getNameStr())) {
// Cross-import overlay modules already appear as "extensions" of their declaring module; we
// should put actual extensions of that module into the main graph
return &MainGraph;
}
auto Found = ExtendedModuleGraphs.find(M->getNameStr());
if (Found != ExtendedModuleGraphs.end()) {
Expand Down
26 changes: 7 additions & 19 deletions lib/SymbolGraphGen/SymbolGraphGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,14 @@ namespace {
int serializeSymbolGraph(SymbolGraph &SG,
const SymbolGraphOptions &Options) {
SmallString<256> FileName;
if (SG.DeclaringModule.hasValue()) {
// Save a cross-import overlay symbol graph as `MainModule@BystandingModule[@BystandingModule...]@OverlayModule.symbols.json`
//
// The overlay module's name is added as a disambiguator in case an overlay
// declares multiple modules for the same set of imports.
FileName.append(SG.DeclaringModule.getValue()->getNameStr());
for (auto BystanderModule : SG.BystanderModules) {
FileName.push_back('@');
FileName.append(BystanderModule.str());
}

FileName.append(SG.M.getNameStr());
if (SG.ExtendedModule.hasValue()) {
FileName.push_back('@');
FileName.append(SG.M.getNameStr());
} else {
FileName.append(SG.M.getNameStr());

if (SG.ExtendedModule.hasValue()) {
FileName.push_back('@');
FileName.append(SG.ExtendedModule.getValue()->getNameStr());
}
FileName.append(SG.ExtendedModule.getValue()->getNameStr());
} else if (SG.DeclaringModule.hasValue()) {
// Treat cross-import overlay modules as "extensions" of their declaring module
FileName.push_back('@');
FileName.append(SG.DeclaringModule.getValue()->getNameStr());
}
FileName.append(".symbols.json");

Expand Down
32 changes: 27 additions & 5 deletions test/SymbolGraph/Module/CrossImport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
// RUN: %target-build-swift %s -module-name _A_B -I %t -emit-module -emit-module-path %t/
// RUN: cp -r %S/Inputs/CrossImport/A.swiftcrossimport %t/
// RUN: %target-swift-symbolgraph-extract -module-name A -I %t -pretty-print -output-dir %t
// RUN: %FileCheck %s --input-file %t/A@B@_A_B.symbols.json
// RUN: %FileCheck %s --input-file %t/[email protected] --check-prefix CHECK-MOD
// RUN: %FileCheck %s --input-file %t/[email protected] --check-prefix CHECK-A
// RUN: %FileCheck %s --input-file %t/[email protected] --check-prefix CHECK-MOD
// RUN: %FileCheck %s --input-file %t/[email protected] --check-prefix CHECK-B

@_exported import A
import B
Expand All @@ -15,7 +18,26 @@ extension A {
}
}

// CHECK: module
// CHECK-NEXT: "name": "A"
// CHECK-NEXT: bystanders
// CHECK-NEXT: B
public struct LocalStruct {}

extension LocalStruct {
public func someFunc() {}
}

extension B {
public func untransmogrify() -> A {
return A(x: self.y)
}
}

// CHECK-MOD: module
// CHECK-MOD-NEXT: "name": "A"
// CHECK-MOD-NEXT: bystanders
// CHECK-MOD-NEXT: B

// CHECK-A-NOT: s:1BAAV4_A_BE14untransmogrify1AAEVyF
// CHECK-A-DAG: s:1AAAV4_A_BE12transmogrify1BAEVyF
// CHECK-A-DAG: s:4_A_B11LocalStructV
// CHECK-A-DAG: s:4_A_B11LocalStructV8someFuncyyF

// CHECK-B: s:1BAAV4_A_BE14untransmogrify1AAEVyF
4 changes: 4 additions & 0 deletions test/SymbolGraph/Module/Inputs/CrossImport/A.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
public struct A {
public var x: Int

public init(x: Int) {
self.x = x
}
}