Skip to content

Commit d4e5c6f

Browse files
committed
[SymbolGraph] Add navigator declaration fragments
Only print the type name for a type's navigator fragments. Don't print where clauses for navigator or subHeading fragments. rdar://62353465
1 parent 428d58e commit d4e5c6f

File tree

7 files changed

+208
-19
lines changed

7 files changed

+208
-19
lines changed

include/swift/AST/PrintOptions.h

+3
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ struct PrintOptions {
280280
/// Whether this print option is for printing .swiftinterface file
281281
bool IsForSwiftInterface = false;
282282

283+
/// Whether to print generic requirements in a where clause.
284+
bool PrintGenericRequirements = true;
285+
283286
/// How to print opaque return types.
284287
enum class OpaqueReturnTypePrintingMode {
285288
/// 'some P1 & P2'.

lib/AST/ASTPrinter.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,7 @@ void PrintAST::printSingleDepthOfGenericSignature(
14571457
llvm::function_ref<bool(const Requirement &)> filter) {
14581458
bool printParams = (flags & PrintParams);
14591459
bool printRequirements = (flags & PrintRequirements);
1460+
printRequirements &= Options.PrintGenericRequirements;
14601461
bool printInherited = (flags & PrintInherited);
14611462
bool swapSelfAndDependentMemberType =
14621463
(flags & SwapSelfAndDependentMemberType);

lib/SymbolGraphGen/Symbol.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ void Symbol::serializeNames(llvm::json::OStream &OS) const {
126126
getPathComponents(PathComponents);
127127

128128
OS.attribute("title", PathComponents.back());
129-
// "navigator": null
129+
Graph->serializeNavigatorDeclarationFragments("navigator", *this, OS);
130130
Graph->serializeSubheadingDeclarationFragments("subHeading", *this, OS);
131131
// "prose": null
132132
});

lib/SymbolGraphGen/SymbolGraph.cpp

+32-7
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ PrintOptions SymbolGraph::getDeclarationFragmentsPrintOptions() const {
6060
Opts.PrintUserInaccessibleAttrs = false;
6161
Opts.SkipPrivateStdlibDecls = true;
6262
Opts.SkipUnderscoredStdlibProtocols = true;
63+
Opts.PrintGenericRequirements = false;
6364

6465
Opts.ExclusiveAttrList.clear();
6566

@@ -70,6 +71,18 @@ PrintOptions SymbolGraph::getDeclarationFragmentsPrintOptions() const {
7071
return Opts;
7172
}
7273

74+
PrintOptions
75+
SymbolGraph::getSubHeadingDeclarationFragmentsPrintOptions() const {
76+
auto Options = getDeclarationFragmentsPrintOptions();
77+
Options.ArgAndParamPrinting =
78+
PrintOptions::ArgAndParamPrintingMode::ArgumentOnly;
79+
Options.VarInitializers = false;
80+
Options.PrintDefaultArgumentValue = false;
81+
Options.PrintEmptyArgumentNames = false;
82+
Options.PrintOverrideKeyword = false;
83+
return Options;
84+
}
85+
7386
bool
7487
SymbolGraph::isRequirementOrDefaultImplementation(const ValueDecl *VD) const {
7588
const auto *DC = VD->getDeclContext();
@@ -502,6 +515,24 @@ SymbolGraph::serializeDeclarationFragments(StringRef Key,
502515
S.getSymbolDecl()->print(Printer, Options);
503516
}
504517

518+
void
519+
SymbolGraph::serializeNavigatorDeclarationFragments(StringRef Key,
520+
const Symbol &S,
521+
llvm::json::OStream &OS) {
522+
DeclarationFragmentPrinter Printer(OS, Key);
523+
524+
if (const auto *TD = dyn_cast<GenericTypeDecl>(S.getSymbolDecl())) {
525+
Printer.printTypeRef(TD->getInterfaceType(), TD, TD->getName(),
526+
PrintNameContext::Normal);
527+
return;
528+
}
529+
auto Options = getSubHeadingDeclarationFragmentsPrintOptions();
530+
if (S.getSynthesizedBaseType()) {
531+
Options.setBaseType(S.getSynthesizedBaseType());
532+
}
533+
S.getSymbolDecl()->print(Printer, Options);
534+
}
535+
505536
void
506537
SymbolGraph::serializeSubheadingDeclarationFragments(StringRef Key,
507538
const Symbol &S,
@@ -511,13 +542,7 @@ SymbolGraph::serializeSubheadingDeclarationFragments(StringRef Key,
511542
if (const auto *TD = dyn_cast<GenericTypeDecl>(S.getSymbolDecl())) {
512543
Printer.printAbridgedType(TD);
513544
} else {
514-
auto Options = getDeclarationFragmentsPrintOptions();
515-
Options.ArgAndParamPrinting =
516-
PrintOptions::ArgAndParamPrintingMode::ArgumentOnly;
517-
Options.VarInitializers = false;
518-
Options.PrintDefaultArgumentValue = false;
519-
Options.PrintEmptyArgumentNames = false;
520-
Options.PrintOverrideKeyword = false;
545+
auto Options = getSubHeadingDeclarationFragmentsPrintOptions();
521546
if (S.getSynthesizedBaseType()) {
522547
Options.setBaseType(S.getSynthesizedBaseType());
523548
}

lib/SymbolGraphGen/SymbolGraph.h

+15-2
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,15 @@ struct SymbolGraph {
7575
/// Get the base print options for declaration fragments.
7676
PrintOptions getDeclarationFragmentsPrintOptions() const;
7777

78+
79+
/// Returns `true` if `VD` is the best known candidate for an
80+
/// overload set in `Owner`.
7881
bool synthesizedMemberIsBestCandidate(const ValueDecl *VD,
7982
const NominalTypeDecl *Owner) const;
8083

84+
/// Get the print options for subHeading declaration fragments.
85+
PrintOptions getSubHeadingDeclarationFragmentsPrintOptions() const;
86+
8187
// MARK: - Symbols (Nodes)
8288

8389
/**
@@ -186,13 +192,20 @@ struct SymbolGraph {
186192
serializeDeclarationFragments(StringRef Key, const Symbol &S,
187193
llvm::json::OStream &OS);
188194

189-
/// Get the overall declaration fragments for a `ValueDecl` when it is viewed
195+
/// Get the declaration fragments for a symbol when viewed in a navigator
196+
/// where there is limited horizontal space.
197+
void
198+
serializeNavigatorDeclarationFragments(StringRef Key,
199+
const Symbol &S,
200+
llvm::json::OStream &OS);
201+
202+
/// Get the declaration fragments for a symbol when it is viewed
190203
/// as a subheading and/or part of a larger group of symbol listings.
191204
void
192205
serializeSubheadingDeclarationFragments(StringRef Key, const Symbol &S,
193206
llvm::json::OStream &OS);
194207

195-
/// Get the overall declaration for a type declaration.
208+
/// Get the overall declaration for a symbol.
196209
void
197210
serializeDeclarationFragments(StringRef Key, Type T,
198211
llvm::json::OStream &OS);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift %s -module-name Navigator -emit-module -emit-module-path %t/
3+
// RUN: %target-swift-symbolgraph-extract -module-name Navigator -I %t -pretty-print -output-dir %t
4+
// RUN: %FileCheck %s --input-file %t/Navigator.symbols.json --check-prefix=MYSTRUCT
5+
// RUN: %FileCheck %s --input-file %t/Navigator.symbols.json --check-prefix=FOO
6+
// RUN: %FileCheck %s --input-file %t/Navigator.symbols.json --check-prefix=BAR
7+
public struct MyStruct<S> { public var x: S
8+
public init(x: S) {
9+
self.x = x
10+
}
11+
public func foo() where S: Sequence {}
12+
public func bar<T>(x: T) where T: Sequence {}
13+
}
14+
15+
// MYSTRUCT-LABEL: "precise": "s:9Navigator8MyStructV"
16+
// MYSTRUCT: names
17+
// MYSTRUCT-NEXT: "title": "MyStruct",
18+
// MYSTRUCT-NEXT: "navigator": [
19+
// MYSTRUCT-NEXT: {
20+
// MYSTRUCT-NEXT: "kind": "typeIdentifier"
21+
// MYSTRUCT-NEXT: "spelling": "MyStruct"
22+
// MYSTRUCT-NEXT: "preciseIdentifier": "s:9Navigator8MyStructV"
23+
// MYSTRUCT-NEXT: }
24+
// MYSTRUCT-NEXT: ]
25+
26+
// FOO-LABEL: "precise": "s:9Navigator8MyStructV3fooyySTRzlF"
27+
// FOO: names
28+
// FOO-NEXT: "title": "foo()",
29+
// FOO-NEXT: "navigator": [
30+
// FOO-NEXT: {
31+
// FOO-NEXT: "kind": "keyword"
32+
// FOO-NEXT: "spelling": "func"
33+
// FOO-NEXT: }
34+
// FOO-NEXT: {
35+
// FOO-NEXT: "kind": "text"
36+
// FOO-NEXT: "spelling": " "
37+
// FOO-NEXT: }
38+
// FOO-NEXT: {
39+
// FOO-NEXT: "kind": "identifier"
40+
// FOO-NEXT: "spelling": "foo"
41+
// FOO-NEXT: }
42+
// FOO-NEXT: {
43+
// FOO-NEXT: "kind": "text"
44+
// FOO-NEXT: "spelling": "()"
45+
// FOO-NEXT: }
46+
// FOO-NEXT: ]
47+
48+
// BAR-LABEL: "precise": "s:9Navigator8MyStructV3bar1xyqd___tSTRd__lF"
49+
// BAR: names
50+
// BAR-NEXT: "title": "bar(x:)",
51+
// BAR-NEXT: "navigator": [
52+
// BAR-NEXT: {
53+
// BAR-NEXT: "kind": "keyword",
54+
// BAR-NEXT: "spelling": "func"
55+
// BAR-NEXT: }
56+
// BAR-NEXT: {
57+
// BAR-NEXT: "kind": "text",
58+
// BAR-NEXT: "spelling": " "
59+
// BAR-NEXT: }
60+
// BAR-NEXT: {
61+
// BAR-NEXT: "kind": "identifier",
62+
// BAR-NEXT: "spelling": "bar"
63+
// BAR-NEXT: }
64+
// BAR-NEXT: {
65+
// BAR-NEXT: "kind": "text",
66+
// BAR-NEXT: "spelling": "<"
67+
// BAR-NEXT: }
68+
// BAR-NEXT: {
69+
// BAR-NEXT: "kind": "genericParameter",
70+
// BAR-NEXT: "spelling": "T"
71+
// BAR-NEXT: }
72+
// BAR-NEXT: {
73+
// BAR-NEXT: "kind": "text",
74+
// BAR-NEXT: "spelling": ">("
75+
// BAR-NEXT: }
76+
// BAR-NEXT: {
77+
// BAR-NEXT: "kind": "externalParam",
78+
// BAR-NEXT: "spelling": "x"
79+
// BAR-NEXT: }
80+
// BAR-NEXT: {
81+
// BAR-NEXT: "kind": "text",
82+
// BAR-NEXT: "spelling": ": T"
83+
// BAR-NEXT: }
84+
// BAR-NEXT: {
85+
// BAR-NEXT: "kind": "text",
86+
// BAR-NEXT: "spelling": ")"
87+
// BAR-NEXT: }
88+
// BAR-NEXT: ]

test/SymbolGraph/Symbols/Mixins/DeclarationFragments/SubheadingDeclarationFragments.swift

+68-9
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,74 @@
33
// RUN: %target-swift-symbolgraph-extract -module-name SubheadingDeclarationFragments -I %t -pretty-print -output-dir %t
44
// RUN: %FileCheck %s --input-file %t/SubheadingDeclarationFragments.symbols.json
55

6-
public func foo<S>(f: @escaping () -> (), ext int: Int = 2, s: S) {}
6+
public func foo<S>(f: @escaping () -> (), ext int: Int = 2, s: S) where S: Sequence {}
77

88
// Subheading fragments should not contain internalParam kinds.
99

10-
// CHECK-LABEL: subHeading
11-
// {
12-
// "kind": "externalParam",
13-
// CHECK: "spelling": "ext"
14-
// CHECK-NEXT: },
15-
// CHECK-NEXT: {
16-
// CHECK-NEXT: "kind": "text"
17-
// CHECK-NEXT: "spelling": ": "
10+
// CHECK-LABEL: "precise": "s:30SubheadingDeclarationFragments3foo1f3ext1syyyc_SixtSTRzlF"
11+
// CHECK: names
12+
// CHECK-NEXT: "title": "foo(f:ext:s:)"
13+
// CHECK: "subHeading": [
14+
// CHECK-NEXT: {
15+
// CHECK-NEXT: "kind": "keyword",
16+
// CHECK-NEXT: "spelling": "func"
17+
// CHECK-NEXT: },
18+
// CHECK-NEXT: {
19+
// CHECK-NEXT: "kind": "text",
20+
// CHECK-NEXT: "spelling": " "
21+
// CHECK-NEXT: },
22+
// CHECK-NEXT: {
23+
// CHECK-NEXT: "kind": "identifier",
24+
// CHECK-NEXT: "spelling": "foo"
25+
// CHECK-NEXT: },
26+
// CHECK-NEXT: {
27+
// CHECK-NEXT: "kind": "text",
28+
// CHECK-NEXT: "spelling": "<"
29+
// CHECK-NEXT: },
30+
// CHECK-NEXT: {
31+
// CHECK-NEXT: "kind": "genericParameter",
32+
// CHECK-NEXT: "spelling": "S"
33+
// CHECK-NEXT: },
34+
// CHECK-NEXT: {
35+
// CHECK-NEXT: "kind": "text",
36+
// CHECK-NEXT: "spelling": ">("
37+
// CHECK-NEXT: },
38+
// CHECK-NEXT: {
39+
// CHECK-NEXT: "kind": "externalParam",
40+
// CHECK-NEXT: "spelling": "f"
41+
// CHECK-NEXT: },
42+
// CHECK-NEXT: {
43+
// CHECK-NEXT: "kind": "text",
44+
// CHECK-NEXT: "spelling": ": () -> (), "
45+
// CHECK-NEXT: },
46+
// CHECK-NEXT: {
47+
// CHECK-NEXT: "kind": "externalParam",
48+
// CHECK-NEXT: "spelling": "ext"
49+
// CHECK-NEXT: },
50+
// CHECK-NEXT: {
51+
// CHECK-NEXT: "kind": "text",
52+
// CHECK-NEXT: "spelling": ": "
53+
// CHECK-NEXT: },
54+
// CHECK-NEXT: {
55+
// CHECK-NEXT: "kind": "typeIdentifier",
56+
// CHECK-NEXT: "spelling": "Int",
57+
// CHECK-NEXT: "preciseIdentifier": "s:Si"
58+
// CHECK-NEXT: },
59+
// CHECK-NEXT: {
60+
// CHECK-NEXT: "kind": "text",
61+
// CHECK-NEXT: "spelling": ", "
62+
// CHECK-NEXT: },
63+
// CHECK-NEXT: {
64+
// CHECK-NEXT: "kind": "externalParam",
65+
// CHECK-NEXT: "spelling": "s"
66+
// CHECK-NEXT: },
67+
// CHECK-NEXT: {
68+
// CHECK-NEXT: "kind": "text",
69+
// CHECK-NEXT: "spelling": ": S"
70+
// CHECK-NEXT: },
71+
// CHECK-NEXT: {
72+
// CHECK-NEXT: "kind": "text",
73+
// CHECK-NEXT: "spelling": ")"
74+
// CHECK-NEXT: }
75+
// CHECK-NEXT: ]
76+

0 commit comments

Comments
 (0)