Skip to content

Commit 08250f0

Browse files
committed
[SourceKit/CursorInfo] Report the set of decls referenced in the symbol graph's "declarationFragments" field.
Resolves rdar://75809521
1 parent cb6b1ea commit 08250f0

File tree

15 files changed

+640
-22
lines changed

15 files changed

+640
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===--- FragmentInfo.h - Swift SymbolGraph Declaration Fragment Info -----===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_SYMBOLGRAPHGEN_FRAGMENTINFO_H
14+
#define SWIFT_SYMBOLGRAPHGEN_FRAGMENTINFO_H
15+
16+
#include "llvm/ADT/SmallVector.h"
17+
#include "PathComponent.h"
18+
19+
namespace swift {
20+
class ValueDecl;
21+
22+
namespace symbolgraphgen {
23+
24+
/// Summary information for a symbol referenced in a symbol graph declaration fragment.
25+
struct FragmentInfo {
26+
/// The swift decl of the referenced symbol.
27+
const ValueDecl *VD;
28+
/// The path components of the refereced symbol.
29+
SmallVector<PathComponent, 4> ParentContexts;
30+
};
31+
32+
} // end namespace symbolgraphgen
33+
} // end namespace swift
34+
35+
#endif // SWIFT_SYMBOLGRAPHGEN_FRAGMENTINFO_H

include/swift/SymbolGraphGen/SymbolGraphGen.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "swift/AST/Type.h"
1818
#include "SymbolGraphOptions.h"
1919
#include "PathComponent.h"
20+
#include "FragmentInfo.h"
2021

2122
namespace swift {
2223
class ValueDecl;
@@ -36,7 +37,8 @@ int printSymbolGraphForDecl(const ValueDecl *D, Type BaseTy,
3637
bool InSynthesizedExtension,
3738
const SymbolGraphOptions &Options,
3839
llvm::raw_ostream &OS,
39-
SmallVectorImpl<PathComponent> &ParentContexts);
40+
SmallVectorImpl<PathComponent> &ParentContexts,
41+
SmallVectorImpl<FragmentInfo> &FragmentInfo);
4042

4143
} // end namespace symbolgraphgen
4244
} // end namespace swift

lib/SymbolGraphGen/DeclarationFragmentPrinter.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ void DeclarationFragmentPrinter::printTypeRef(Type T, const TypeDecl *RefTo,
155155
if (ShouldLink) {
156156
llvm::raw_svector_ostream OS(USR);
157157
ide::printDeclUSR(RefTo, OS);
158+
if (ReferencedDecls)
159+
ReferencedDecls->insert(RefTo);
158160
}
159161
closeFragment();
160162
}

lib/SymbolGraphGen/DeclarationFragmentPrinter.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef SWIFT_SYMBOLGRAPHGEN_DECLARATIONFRAGMENTPRINTER_H
1414
#define SWIFT_SYMBOLGRAPHGEN_DECLARATIONFRAGMENTPRINTER_H
1515

16+
#include "llvm/ADT/SmallPtrSet.h"
1617
#include "llvm/Support/JSON.h"
1718
#include "swift/AST/ASTPrinter.h"
1819
#include "swift/Basic/LLVM.h"
@@ -73,6 +74,9 @@ class DeclarationFragmentPrinter : public ASTPrinter {
7374

7475
SmallString<256> USR;
7576

77+
/// Stores the set of decls referenced in the fragment if non-null.
78+
SmallPtrSet<const Decl*, 8> *ReferencedDecls;
79+
7680
StringRef getKindSpelling(FragmentKind Kind) const;
7781

7882
/// Open a new kind of fragment without committing its spelling.
@@ -86,10 +90,12 @@ class DeclarationFragmentPrinter : public ASTPrinter {
8690
public:
8791
DeclarationFragmentPrinter(const SymbolGraph *SG,
8892
llvm::json::OStream &OS,
89-
Optional<StringRef> Key = None)
93+
Optional<StringRef> Key = None,
94+
SmallPtrSet<const Decl*, 8> *ReferencedDecls = nullptr)
9095
: SG(SG),
9196
OS(OS),
9297
Kind(FragmentKind::None),
98+
ReferencedDecls(ReferencedDecls),
9399
NumFragments(0) {
94100
if (Key) {
95101
OS.attributeBegin(*Key);

lib/SymbolGraphGen/Symbol.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "Symbol.h"
2222
#include "SymbolGraph.h"
2323
#include "SymbolGraphASTWalker.h"
24+
#include "DeclarationFragmentPrinter.h"
2425

2526
using namespace swift;
2627
using namespace symbolgraphgen;
@@ -518,6 +519,33 @@ Symbol::getPathComponents(SmallVectorImpl<PathComponent> &Components) const {
518519
std::reverse(Components.begin(), Components.end());
519520
}
520521

522+
void Symbol::
523+
getFragmentInfo(SmallVectorImpl<FragmentInfo> &FragmentInfos) const {
524+
SmallPtrSet<const Decl*, 8> Referenced;
525+
526+
auto Options = Graph->getDeclarationFragmentsPrintOptions();
527+
if (getBaseType()) {
528+
Options.setBaseType(getBaseType());
529+
Options.PrintAsMember = true;
530+
}
531+
532+
llvm::json::OStream OS(llvm::nulls());
533+
OS.object([&]{
534+
DeclarationFragmentPrinter Printer(Graph, OS, {"ignored"}, &Referenced);
535+
getSymbolDecl()->print(Printer, Options);
536+
});
537+
538+
for (auto *D: Referenced) {
539+
if (!Symbol::supportsKind(D->getKind()))
540+
continue;
541+
if (auto *VD = dyn_cast<ValueDecl>(D)) {
542+
FragmentInfos.push_back(FragmentInfo{VD, {}});
543+
Symbol RefSym(Graph, VD, nullptr);
544+
RefSym.getPathComponents(FragmentInfos.back().ParentContexts);
545+
}
546+
}
547+
}
548+
521549
void Symbol::printPath(llvm::raw_ostream &OS) const {
522550
SmallVector<PathComponent, 8> Components;
523551
getPathComponents(Components);

lib/SymbolGraphGen/Symbol.h

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "swift/Basic/LLVM.h"
2020
#include "swift/Markup/Markup.h"
2121
#include "swift/SymbolGraphGen/PathComponent.h"
22+
#include "swift/SymbolGraphGen/FragmentInfo.h"
2223

2324
namespace swift {
2425
namespace symbolgraphgen {
@@ -98,8 +99,14 @@ class Symbol {
9899
return SynthesizedBaseTypeDecl;
99100
}
100101

102+
/// Reteive the path components associated with this symbol, from outermost
103+
/// to innermost (this symbol).
101104
void getPathComponents(SmallVectorImpl<PathComponent> &Components) const;
102105

106+
/// Retrieve information about all symbols referenced in the declaration
107+
/// fragment printed for this symbol.
108+
void getFragmentInfo(SmallVectorImpl<FragmentInfo> &FragmentInfo) const;
109+
103110
/// Print the symbol path to an output stream.
104111
void printPath(llvm::raw_ostream &OS) const;
105112

lib/SymbolGraphGen/SymbolGraphGen.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ printSymbolGraphForDecl(const ValueDecl *D, Type BaseTy,
100100
bool InSynthesizedExtension,
101101
const SymbolGraphOptions &Options,
102102
llvm::raw_ostream &OS,
103-
SmallVectorImpl<PathComponent> &ParentContexts) {
103+
SmallVectorImpl<PathComponent> &ParentContexts,
104+
SmallVectorImpl<FragmentInfo> &FragmentInfo) {
104105
if (!Symbol::supportsKind(D->getKind()))
105106
return EXIT_FAILURE;
106107

@@ -116,6 +117,7 @@ printSymbolGraphForDecl(const ValueDecl *D, Type BaseTy,
116117

117118
Symbol MySym(&Graph, D, NTD, BaseTy);
118119
MySym.getPathComponents(ParentContexts);
120+
MySym.getFragmentInfo(FragmentInfo);
119121
Graph.recordNode(MySym);
120122
Graph.serialize(JOS);
121123
return EXIT_SUCCESS;

test/SourceKit/CursorInfo/cursor_symbol_graph_objc.swift

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ func callObjC() {
66

77
// REQUIRES: objc_interop
88
// RUN: %sourcekitd-test -req=cursor -pos=4:3 -req-opts=retrieve_symbol_graph=1 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp -target %target-triple %s | %FileCheck %s
9+
//
910
// CHECK: SYMBOL GRAPH BEGIN
1011
// CHECK: {
1112
// CHECK: "metadata": {

0 commit comments

Comments
 (0)