Skip to content

[5.5][SymbolGraph] add sourceOrigin field for symbols implementing remote protocol requirements #37352

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
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
25 changes: 25 additions & 0 deletions lib/SymbolGraphGen/Edge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,29 @@
#include "Symbol.h"
#include "SymbolGraphASTWalker.h"

#include <queue>

using namespace swift;
using namespace symbolgraphgen;

namespace {
const ValueDecl *getForeignProtocolRequirement(const ValueDecl *VD, const ModuleDecl *M) {
std::queue<const ValueDecl *> requirements;
while (true) {
for (auto *req : VD->getSatisfiedProtocolRequirements()) {
if (req->getModuleContext()->getNameStr() != M->getNameStr())
return req;
else
requirements.push(req);
}
if (requirements.empty())
return nullptr;
VD = requirements.front();
requirements.pop();
}
}
} // end anonymous namespace

void Edge::serialize(llvm::json::OStream &OS) const {
OS.object([&](){
OS.attribute("kind", Kind.Name);
Expand Down Expand Up @@ -66,6 +86,11 @@ void Edge::serialize(llvm::json::OStream &OS) const {

if (!InheritingDecl && Source.getSynthesizedBaseTypeDecl())
InheritingDecl = Source.getSymbolDecl();

if (!InheritingDecl) {
if (const auto *ID = getForeignProtocolRequirement(Source.getSymbolDecl(), &Graph->M))
InheritingDecl = ID;
}

// If our source symbol is a inheriting decl, write in information about
// where it's inheriting docs from.
Expand Down
14 changes: 14 additions & 0 deletions test/SymbolGraph/Relationships/Synthesized/Inputs/RemoteP.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// Some Protocol
public protocol P {
func someFunc()

/// This one has docs!
func otherFunc()

func bonusFunc()
}

public extension P {
/// Extra default docs!
func extraFunc() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %S/Inputs/RemoteP.swift -module-name RemoteP -emit-module -emit-module-path %t/RemoteP.swiftmodule -emit-module-source-info-path %t/RemoteP.swiftsourceinfo -emit-module-doc-path %t/RemoteP.swiftdoc
// RUN: %target-swift-frontend %s -module-name RemoteInheritedDocs -emit-module -emit-module-path %t/RemoteInheritedDocs.swiftmodule -emit-module-source-info-path %t/RemoteInheritedDocs.swiftsourceinfo -emit-module-doc-path %t/RemoteInheritedDocs.swiftdoc -I %t

// RUN: %target-swift-symbolgraph-extract -module-name RemoteInheritedDocs -I %t -pretty-print -output-dir %t
// RUN: %FileCheck %s --input-file %t/RemoteInheritedDocs.symbols.json --check-prefix SOME
// RUN: %FileCheck %s --input-file %t/RemoteInheritedDocs.symbols.json --check-prefix OTHER
// RUN: %FileCheck %s --input-file %t/RemoteInheritedDocs.symbols.json --check-prefix BONUS
// RUN: %FileCheck %s --input-file %t/RemoteInheritedDocs.symbols.json --check-prefix INHERIT
// RUN: %FileCheck %s --input-file %t/RemoteInheritedDocs.symbols.json --check-prefix LOCAL
// RUN: %FileCheck %s --input-file %t/RemoteInheritedDocs.symbols.json --check-prefix OVERRIDE

// RUN: %target-swift-symbolgraph-extract -module-name RemoteInheritedDocs -I %t -pretty-print -output-dir %t -skip-inherited-docs
// RUN: %FileCheck %s --input-file %t/RemoteInheritedDocs.symbols.json --check-prefix SOME
// RUN: %FileCheck %s --input-file %t/RemoteInheritedDocs.symbols.json --check-prefix OTHER
// RUN: %FileCheck %s --input-file %t/RemoteInheritedDocs.symbols.json --check-prefix BONUS
// RUN: %FileCheck %s --input-file %t/RemoteInheritedDocs.symbols.json --check-prefix SKIP
// RUN: %FileCheck %s --input-file %t/RemoteInheritedDocs.symbols.json --check-prefix LOCAL
// RUN: %FileCheck %s --input-file %t/RemoteInheritedDocs.symbols.json --check-prefix OVERRIDE

// SOME: "source": "s:19RemoteInheritedDocs1SV8someFuncyyF"
// SOME-NEXT: "target": "s:19RemoteInheritedDocs1SV"
// SOME-NEXT: "sourceOrigin"
// SOME-NEXT: "identifier": "s:7RemoteP1PP8someFuncyyF"
// SOME-NEXT: "displayName": "P.someFunc()"

// OTHER: "source": "s:19RemoteInheritedDocs1SV9otherFuncyyF"
// OTHER-NEXT: "target": "s:19RemoteInheritedDocs1SV"
// OTHER-NEXT: "sourceOrigin"
// OTHER-NEXT: "identifier": "s:7RemoteP1PP9otherFuncyyF"
// OTHER-NEXT: "displayName": "P.otherFunc()"

// BONUS: "source": "s:19RemoteInheritedDocs1SV9bonusFuncyyF"
// BONUS-NEXT: "target": "s:19RemoteInheritedDocs1SV"
// BONUS-NEXT: "sourceOrigin"
// BONUS-NEXT: "identifier": "s:7RemoteP1PP9bonusFuncyyF"
// BONUS-NEXT: "displayName": "P.bonusFunc()"

// INHERIT: This one has docs!
// SKIP-NOT: This one has docs!

// LOCAL: Local docs override!

// OVERRIDE-NOT: Extra default docs!
// OVERRIDE-NOT: Extension override!

import RemoteP

// The `RemoteP.P` protocol has three methods: `someFunc` and `bonusFunc` don't have docs upstream,
// but `otherFunc` does. Regardless, each one needs a `sourceOrigin` field connecting it to
// upstream.

// `RemoteP.P` also has an extension with a default implementation for `extraFunc` that does have
// docs, but overriding it here should prevent those from appearing

public struct S: P {
public func someFunc() {}

public func otherFunc() {}

/// Local docs override!
public func bonusFunc() {}

public func extraFunc() {}
}

public extension P {
/// Extension override!
func someFunc() {}
}