Skip to content

Render symbol graphs for cross-import overlay modules, if present #34922

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 5 commits into from
Dec 12, 2020
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
2 changes: 1 addition & 1 deletion lib/SymbolGraphGen/FormatVersion.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@

#define SWIFT_SYMBOLGRAPH_FORMAT_MAJOR 0
#define SWIFT_SYMBOLGRAPH_FORMAT_MINOR 5
#define SWIFT_SYMBOLGRAPH_FORMAT_PATCH 0
#define SWIFT_SYMBOLGRAPH_FORMAT_PATCH 1

#endif // SWIFT_SYMBOLGRAPHGEN_FORMATVERSION_H
22 changes: 20 additions & 2 deletions lib/SymbolGraphGen/SymbolGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ SymbolGraph::SymbolGraph(SymbolGraphASTWalker &Walker,
M(M),
ExtendedModule(ExtendedModule),
Ctx(Ctx),
ModuleVersion(ModuleVersion) {}
ModuleVersion(ModuleVersion) {
if (auto *DM = M.getDeclaringModuleIfCrossImportOverlay()) {
DeclaringModule = DM;
SmallVector<Identifier, 1> Bystanders;
if (M.getRequiredBystandersIfCrossImportOverlay(DM, Bystanders)) {
BystanderModules = Bystanders;
}
}
}

// MARK: - Utilities

Expand Down Expand Up @@ -499,7 +507,17 @@ void SymbolGraph::serialize(llvm::json::OStream &OS) {
}); // end metadata:

OS.attributeObject("module", [&](){
OS.attribute("name", M.getNameStr());
if (DeclaringModule) {
// A cross-import overlay can be considered part of its declaring module
OS.attribute("name", (*DeclaringModule)->getNameStr());
std::vector<StringRef> B;
for (auto BModule : BystanderModules) {
B.push_back(BModule.str());
}
OS.attribute("bystanders", B);
} else {
OS.attribute("name", M.getNameStr());
}
AttributeRAII Platform("platform", OS);

auto *MainFile = M.getFiles().front();
Expand Down
10 changes: 10 additions & 0 deletions lib/SymbolGraphGen/SymbolGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ struct SymbolGraph {
The module whose types were extended in `M`.
*/
Optional<ModuleDecl *> ExtendedModule;

/**
The module declaring `M`, if `M` is a cross-import overlay.
*/
Optional<ModuleDecl *> DeclaringModule;

/**
The modules that must be imported alongside `DeclaringModule` for `M` to be imported, if `M` is a cross-import overlay.
*/
SmallVector<Identifier, 1> BystanderModules;

/**
A context for allocations.
Expand Down
23 changes: 20 additions & 3 deletions lib/SymbolGraphGen/SymbolGraphGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,27 @@ using namespace symbolgraphgen;
namespace {
int serializeSymbolGraph(SymbolGraph &SG,
const SymbolGraphOptions &Options) {
SmallString<256> FileName(SG.M.getNameStr());
if (SG.ExtendedModule.hasValue()) {
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.push_back('@');
FileName.append(SG.ExtendedModule.getValue()->getNameStr());
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(".symbols.json");

Expand Down
21 changes: 21 additions & 0 deletions test/SymbolGraph/Module/CrossImport.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %S/Inputs/CrossImport/A.swift -I %t -module-name A -emit-module -emit-module-path %t/
// RUN: %target-build-swift %S/Inputs/CrossImport/B.swift -I %t -module-name B -emit-module -emit-module-path %t/
// 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

@_exported import A
import B

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

// CHECK: module
// CHECK-NEXT: "name": "A"
// CHECK-NEXT: bystanders
// CHECK-NEXT: B
3 changes: 3 additions & 0 deletions test/SymbolGraph/Module/Inputs/CrossImport/A.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public struct A {
public var x: Int
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
%YAML 1.2
---
version: 1
modules:
- name: _A_B
7 changes: 7 additions & 0 deletions test/SymbolGraph/Module/Inputs/CrossImport/B.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public struct B {
public var y: Int

public init(y: Int) {
self.y = y
}
}
30 changes: 26 additions & 4 deletions tools/driver/swift_symbolgraph_extract_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,12 @@ int swift_symbolgraph_extract_main(ArrayRef<const char *> Args, const char *Argv
}

auto M = CI.getASTContext().getModuleByName(options::ModuleName);
SmallVector<Identifier, 32> VisibleModuleNames;
CI.getASTContext().getVisibleTopLevelModuleNames(VisibleModuleNames);
if (!M) {
llvm::errs()
<< "Couldn't load module '" << options::ModuleName << '\''
<< " in the current SDK and search paths.\n";
SmallVector<Identifier, 32> VisibleModuleNames;
CI.getASTContext().getVisibleTopLevelModuleNames(VisibleModuleNames);

if (VisibleModuleNames.empty()) {
llvm::errs() << "Could not find any modules.\n";
Expand All @@ -241,7 +241,29 @@ int swift_symbolgraph_extract_main(ArrayRef<const char *> Args, const char *Argv

const auto &MainFile = M->getMainFile(FileUnitKind::SerializedAST);
llvm::errs() << "Emitting symbol graph for module file: " << MainFile.getModuleDefiningPath() << '\n';

int Success = symbolgraphgen::emitSymbolGraphForModule(M, Options);

// Look for cross-import overlays that the given module imports.

// Clear out the diagnostic printer before looking for cross-import overlay modules,
// since some SDK modules can cause errors in the getModuleByName() call. The call
// itself will properly return nullptr after this failure, so for our purposes we
// don't need to print these errors.
CI.removeDiagnosticConsumer(&DiagPrinter);

for (const auto &ModuleName : VisibleModuleNames) {
if (ModuleName.str().startswith("_")) {
auto CIM = CI.getASTContext().getModuleByName(ModuleName.str());
if (CIM && CIM->isCrossImportOverlayOf(M)) {
const auto &CIMainFile = CIM->getMainFile(FileUnitKind::SerializedAST);
llvm::errs() << "Emitting symbol graph for cross-import overlay module file: "
<< CIMainFile.getModuleDefiningPath() << '\n';

Success |= symbolgraphgen::emitSymbolGraphForModule(CIM, Options);
}
}
}

return symbolgraphgen::emitSymbolGraphForModule(M,
Options);
return Success;
}