Skip to content

[SourceKit/CursorInfo] Report the set of decls referenced in the symbol graph's "declarationFragments" field. #36713

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
35 changes: 35 additions & 0 deletions include/swift/SymbolGraphGen/FragmentInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//===--- FragmentInfo.h - Swift SymbolGraph Declaration Fragment Info -----===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_SYMBOLGRAPHGEN_FRAGMENTINFO_H
#define SWIFT_SYMBOLGRAPHGEN_FRAGMENTINFO_H

#include "llvm/ADT/SmallVector.h"
#include "PathComponent.h"

namespace swift {
class ValueDecl;

namespace symbolgraphgen {

/// Summary information for a symbol referenced in a symbol graph declaration fragment.
struct FragmentInfo {
/// The swift decl of the referenced symbol.
const ValueDecl *VD;
/// The path components of the refereced symbol.
SmallVector<PathComponent, 4> ParentContexts;
};

} // end namespace symbolgraphgen
} // end namespace swift

#endif // SWIFT_SYMBOLGRAPHGEN_FRAGMENTINFO_H
4 changes: 3 additions & 1 deletion include/swift/SymbolGraphGen/SymbolGraphGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "swift/AST/Type.h"
#include "SymbolGraphOptions.h"
#include "PathComponent.h"
#include "FragmentInfo.h"

namespace swift {
class ValueDecl;
Expand All @@ -36,7 +37,8 @@ int printSymbolGraphForDecl(const ValueDecl *D, Type BaseTy,
bool InSynthesizedExtension,
const SymbolGraphOptions &Options,
llvm::raw_ostream &OS,
SmallVectorImpl<PathComponent> &ParentContexts);
SmallVectorImpl<PathComponent> &ParentContexts,
SmallVectorImpl<FragmentInfo> &FragmentInfo);

} // end namespace symbolgraphgen
} // end namespace swift
Expand Down
2 changes: 2 additions & 0 deletions lib/SymbolGraphGen/DeclarationFragmentPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ void DeclarationFragmentPrinter::printTypeRef(Type T, const TypeDecl *RefTo,
if (ShouldLink) {
llvm::raw_svector_ostream OS(USR);
ide::printDeclUSR(RefTo, OS);
if (ReferencedDecls)
ReferencedDecls->insert(RefTo);
}
closeFragment();
}
Expand Down
8 changes: 7 additions & 1 deletion lib/SymbolGraphGen/DeclarationFragmentPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef SWIFT_SYMBOLGRAPHGEN_DECLARATIONFRAGMENTPRINTER_H
#define SWIFT_SYMBOLGRAPHGEN_DECLARATIONFRAGMENTPRINTER_H

#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Support/JSON.h"
#include "swift/AST/ASTPrinter.h"
#include "swift/Basic/LLVM.h"
Expand Down Expand Up @@ -73,6 +74,9 @@ class DeclarationFragmentPrinter : public ASTPrinter {

SmallString<256> USR;

/// Stores the set of decls referenced in the fragment if non-null.
SmallPtrSet<const Decl*, 8> *ReferencedDecls;

StringRef getKindSpelling(FragmentKind Kind) const;

/// Open a new kind of fragment without committing its spelling.
Expand All @@ -86,10 +90,12 @@ class DeclarationFragmentPrinter : public ASTPrinter {
public:
DeclarationFragmentPrinter(const SymbolGraph *SG,
llvm::json::OStream &OS,
Optional<StringRef> Key = None)
Optional<StringRef> Key = None,
SmallPtrSet<const Decl*, 8> *ReferencedDecls = nullptr)
: SG(SG),
OS(OS),
Kind(FragmentKind::None),
ReferencedDecls(ReferencedDecls),
NumFragments(0) {
if (Key) {
OS.attributeBegin(*Key);
Expand Down
28 changes: 28 additions & 0 deletions lib/SymbolGraphGen/Symbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "Symbol.h"
#include "SymbolGraph.h"
#include "SymbolGraphASTWalker.h"
#include "DeclarationFragmentPrinter.h"

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

void Symbol::
getFragmentInfo(SmallVectorImpl<FragmentInfo> &FragmentInfos) const {
SmallPtrSet<const Decl*, 8> Referenced;

auto Options = Graph->getDeclarationFragmentsPrintOptions();
if (getBaseType()) {
Options.setBaseType(getBaseType());
Options.PrintAsMember = true;
}

llvm::json::OStream OS(llvm::nulls());
OS.object([&]{
DeclarationFragmentPrinter Printer(Graph, OS, {"ignored"}, &Referenced);
getSymbolDecl()->print(Printer, Options);
});

for (auto *D: Referenced) {
if (!Symbol::supportsKind(D->getKind()))
continue;
if (auto *VD = dyn_cast<ValueDecl>(D)) {
FragmentInfos.push_back(FragmentInfo{VD, {}});
Symbol RefSym(Graph, VD, nullptr);
RefSym.getPathComponents(FragmentInfos.back().ParentContexts);
}
}
}

void Symbol::printPath(llvm::raw_ostream &OS) const {
SmallVector<PathComponent, 8> Components;
getPathComponents(Components);
Expand Down
7 changes: 7 additions & 0 deletions lib/SymbolGraphGen/Symbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "swift/Basic/LLVM.h"
#include "swift/Markup/Markup.h"
#include "swift/SymbolGraphGen/PathComponent.h"
#include "swift/SymbolGraphGen/FragmentInfo.h"

namespace swift {
namespace symbolgraphgen {
Expand Down Expand Up @@ -98,8 +99,14 @@ class Symbol {
return SynthesizedBaseTypeDecl;
}

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

/// Retrieve information about all symbols referenced in the declaration
/// fragment printed for this symbol.
void getFragmentInfo(SmallVectorImpl<FragmentInfo> &FragmentInfo) const;

/// Print the symbol path to an output stream.
void printPath(llvm::raw_ostream &OS) const;

Expand Down
4 changes: 3 additions & 1 deletion lib/SymbolGraphGen/SymbolGraphGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ printSymbolGraphForDecl(const ValueDecl *D, Type BaseTy,
bool InSynthesizedExtension,
const SymbolGraphOptions &Options,
llvm::raw_ostream &OS,
SmallVectorImpl<PathComponent> &ParentContexts) {
SmallVectorImpl<PathComponent> &ParentContexts,
SmallVectorImpl<FragmentInfo> &FragmentInfo) {
if (!Symbol::supportsKind(D->getKind()))
return EXIT_FAILURE;

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

Symbol MySym(&Graph, D, NTD, BaseTy);
MySym.getPathComponents(ParentContexts);
MySym.getFragmentInfo(FragmentInfo);
Graph.recordNode(MySym);
Graph.serialize(JOS);
return EXIT_SUCCESS;
Expand Down
1 change: 1 addition & 0 deletions test/SourceKit/CursorInfo/cursor_symbol_graph_objc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ func callObjC() {

// REQUIRES: objc_interop
// 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
//
// CHECK: SYMBOL GRAPH BEGIN
// CHECK: {
// CHECK: "metadata": {
Expand Down
Loading