Skip to content

[SourceKit] Report synthesized protocol conformances for DocSupport request. rdar://36882292 #14248

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 3 commits into from
Jan 30, 2018
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
5 changes: 5 additions & 0 deletions include/swift/AST/ASTPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ StringRef getCodePlaceholder();
void printEnumElementsAsCases(
llvm::DenseSet<EnumElementDecl *> &UnhandledElements,
llvm::raw_ostream &OS);

void getInheritedForPrinting(const Decl *decl,
llvm::function_ref<bool(const Decl*)> shouldPrint,
llvm::SmallVectorImpl<TypeLoc> &Results);

} // namespace swift

#endif // LLVM_SWIFT_AST_ASTPRINTER_H
84 changes: 40 additions & 44 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,12 +606,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
bool openBracket = true, bool closeBracket = true);
void printNominalDeclGenericParams(NominalTypeDecl *decl);
void printNominalDeclGenericRequirements(NominalTypeDecl *decl);
void printInherited(const Decl *decl, ArrayRef<TypeLoc> inherited);

void printInherited(const NominalTypeDecl *D);
void printInherited(const ExtensionDecl *D);
void printInherited(const GenericTypeParamDecl *D);
void printInherited(const AssociatedTypeDecl *D);
void printInherited(const Decl *decl);

void printEnumElement(EnumElementDecl *elt);

Expand Down Expand Up @@ -1665,29 +1660,10 @@ void PrintAST::printNominalDeclGenericRequirements(NominalTypeDecl *decl) {
printGenericSignature(GenericSig, PrintRequirements | InnermostOnly);
}

void PrintAST::printInherited(const Decl *decl, ArrayRef<TypeLoc> inherited) {
void PrintAST::printInherited(const Decl *decl) {
SmallVector<TypeLoc, 6> TypesToPrint;
for (auto TL : inherited) {
if (auto Ty = TL.getType()) {
if (auto NTD = Ty->getAnyNominal())
if (!shouldPrint(NTD))
continue;
}
TypesToPrint.push_back(TL);
}

auto &ctx = decl->getASTContext();
for (auto attr : decl->getAttrs().getAttributes<SynthesizedProtocolAttr>()) {
if (auto *proto = ctx.getProtocol(attr->getProtocolKind())) {
if (!shouldPrint(proto))
continue;
if (attr->getProtocolKind() == KnownProtocolKind::RawRepresentable &&
isa<EnumDecl>(decl) &&
cast<EnumDecl>(decl)->hasRawType())
continue;
TypesToPrint.push_back(TypeLoc::withoutLoc(proto->getDeclaredType()));
}
}
getInheritedForPrinting(decl, [this](const Decl* D) { return shouldPrint(D); },
TypesToPrint);
if (TypesToPrint.empty())
return;

Expand All @@ -1700,22 +1676,6 @@ void PrintAST::printInherited(const Decl *decl, ArrayRef<TypeLoc> inherited) {
});
}

void PrintAST::printInherited(const NominalTypeDecl *D) {
printInherited(D, D->getInherited());
}

void PrintAST::printInherited(const ExtensionDecl *D) {
printInherited(D, D->getInherited());
}

void PrintAST::printInherited(const GenericTypeParamDecl *D) {
printInherited(D, D->getInherited());
}

void PrintAST::printInherited(const AssociatedTypeDecl *D) {
printInherited(D, D->getInherited());
}

static void getModuleEntities(const clang::Module *ClangMod,
SmallVectorImpl<ModuleEntity> &ModuleEnts) {
if (!ClangMod)
Expand Down Expand Up @@ -4308,3 +4268,39 @@ void swift::printEnumElementsAsCases(
OS << ": " << getCodePlaceholder() << "\n";
});
}

void
swift::getInheritedForPrinting(const Decl *decl,
llvm::function_ref<bool(const Decl*)> shouldPrint,
llvm::SmallVectorImpl<TypeLoc> &Results) {
ArrayRef<TypeLoc> inherited;
if (auto td = dyn_cast<TypeDecl>(decl)) {
inherited = td->getInherited();
} else if (auto ed = dyn_cast<ExtensionDecl>(decl)) {
inherited = ed->getInherited();
}

// Collect explicit inheritted types.
for (auto TL: inherited) {
if (auto Ty = TL.getType()) {
if (auto NTD = Ty->getAnyNominal())
if (!shouldPrint(NTD))
continue;
}
Results.push_back(TL);
}

// Collect synthesized conformances.
auto &ctx = decl->getASTContext();
for (auto attr : decl->getAttrs().getAttributes<SynthesizedProtocolAttr>()) {
if (auto *proto = ctx.getProtocol(attr->getProtocolKind())) {
if (!shouldPrint(proto))
continue;
if (attr->getProtocolKind() == KnownProtocolKind::RawRepresentable &&
isa<EnumDecl>(decl) &&
cast<EnumDecl>(decl)->hasRawType())
continue;
Results.push_back(TypeLoc::withoutLoc(proto->getDeclaredType()));
}
}
}
55 changes: 55 additions & 0 deletions test/SourceKit/DocSupport/doc_clang_module.swift.response
Original file line number Diff line number Diff line change
Expand Up @@ -4550,6 +4550,18 @@ var FooSubUnnamedEnumeratorA1: Int { get }
key.offset: 36,
key.length: 205,
key.fully_annotated_decl: "<decl.struct><syntaxtype.keyword>struct</syntaxtype.keyword> <decl.name>FooEnum1</decl.name> : <ref.protocol usr=\"s:s9EquatableP\">Equatable</ref.protocol>, <ref.protocol usr=\"s:s16RawRepresentableP\">RawRepresentable</ref.protocol></decl.struct>",
key.conforms: [
{
key.kind: source.lang.swift.ref.protocol,
key.name: "Equatable",
key.usr: "s:s9EquatableP"
},
{
key.kind: source.lang.swift.ref.protocol,
key.name: "RawRepresentable",
key.usr: "s:s16RawRepresentableP"
}
],
key.entities: [
{
key.kind: source.lang.swift.decl.function.constructor,
Expand Down Expand Up @@ -4651,6 +4663,18 @@ var FooSubUnnamedEnumeratorA1: Int { get }
key.offset: 274,
key.length: 205,
key.fully_annotated_decl: "<decl.struct><syntaxtype.keyword>struct</syntaxtype.keyword> <decl.name>FooEnum2</decl.name> : <ref.protocol usr=\"s:s9EquatableP\">Equatable</ref.protocol>, <ref.protocol usr=\"s:s16RawRepresentableP\">RawRepresentable</ref.protocol></decl.struct>",
key.conforms: [
{
key.kind: source.lang.swift.ref.protocol,
key.name: "Equatable",
key.usr: "s:s9EquatableP"
},
{
key.kind: source.lang.swift.ref.protocol,
key.name: "RawRepresentable",
key.usr: "s:s16RawRepresentableP"
}
],
key.entities: [
{
key.kind: source.lang.swift.decl.function.constructor,
Expand Down Expand Up @@ -4759,6 +4783,18 @@ var FooSubUnnamedEnumeratorA1: Int { get }
key.offset: 544,
key.length: 205,
key.fully_annotated_decl: "<decl.struct><syntaxtype.keyword>struct</syntaxtype.keyword> <decl.name>FooEnum3</decl.name> : <ref.protocol usr=\"s:s9EquatableP\">Equatable</ref.protocol>, <ref.protocol usr=\"s:s16RawRepresentableP\">RawRepresentable</ref.protocol></decl.struct>",
key.conforms: [
{
key.kind: source.lang.swift.ref.protocol,
key.name: "Equatable",
key.usr: "s:s9EquatableP"
},
{
key.kind: source.lang.swift.ref.protocol,
key.name: "RawRepresentable",
key.usr: "s:s16RawRepresentableP"
}
],
key.entities: [
{
key.kind: source.lang.swift.decl.function.constructor,
Expand Down Expand Up @@ -4936,6 +4972,13 @@ var FooSubUnnamedEnumeratorA1: Int { get }
key.offset: 1011,
key.length: 337,
key.fully_annotated_decl: "<decl.struct><syntaxtype.keyword>struct</syntaxtype.keyword> <decl.name>FooRuncingOptions</decl.name> : <ref.protocol usr=\"s:s9OptionSetP\">OptionSet</ref.protocol></decl.struct>",
key.conforms: [
{
key.kind: source.lang.swift.ref.protocol,
key.name: "OptionSet",
key.usr: "s:s9OptionSetP"
}
],
key.entities: [
{
key.kind: source.lang.swift.decl.function.constructor,
Expand Down Expand Up @@ -7059,6 +7102,18 @@ var FooSubUnnamedEnumeratorA1: Int { get }
key.offset: 7408,
key.length: 214,
key.fully_annotated_decl: "<decl.struct><syntaxtype.keyword>struct</syntaxtype.keyword> <decl.name>FooSubEnum1</decl.name> : <ref.protocol usr=\"s:s9EquatableP\">Equatable</ref.protocol>, <ref.protocol usr=\"s:s16RawRepresentableP\">RawRepresentable</ref.protocol></decl.struct>",
key.conforms: [
{
key.kind: source.lang.swift.ref.protocol,
key.name: "Equatable",
key.usr: "s:s9EquatableP"
},
{
key.kind: source.lang.swift.ref.protocol,
key.name: "RawRepresentable",
key.usr: "s:s16RawRepresentableP"
}
],
key.entities: [
{
key.kind: source.lang.swift.decl.function.constructor,
Expand Down
4 changes: 3 additions & 1 deletion tools/SourceKit/lib/SwiftLang/SwiftDocSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,9 @@ static void reportRelated(ASTContext &Ctx,
// Otherwise, report the inheritance of the type alias itself.
passInheritsAndConformancesForValueDecl(TAD, Consumer);
} else if (const auto *TD = dyn_cast<TypeDecl>(D)) {
passInherits(TD->getInherited(), Consumer);
llvm::SmallVector<TypeLoc, 4> AllInherits;
getInheritedForPrinting(TD, [](const Decl* d) { return true; }, AllInherits);
passInherits(AllInherits, Consumer);
passConforms(TD->getSatisfiedProtocolRequirements(/*Sorted=*/true),
Consumer);
} else if (auto *VD = dyn_cast<ValueDecl>(D)) {
Expand Down