Skip to content

swift-api-digester: teach the tool to serialize USRs for nominal type. #15463

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 1 commit into from
Mar 23, 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 test/api-digester/Inputs/APINotesLeft/APINotesTest.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#import <Foundation.h>
extern int ANTGlobalValue;

@interface NewType
Expand All @@ -16,3 +17,7 @@ extern int ANTGlobalValue;
-(void) ProtMemberFunc2;
-(void) ProtMemberFunc3;
@end

@interface AnimalStatusDescriptor
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingAttributes:(nonnull NSDictionary<NSString*, id> *)attributes;
@end
7 changes: 7 additions & 0 deletions test/api-digester/Inputs/APINotesRight/APINotesTest.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#import <objc_generics.h>
extern int ANTGlobalValue;

@interface NewType
Expand All @@ -14,3 +15,9 @@ extern int ANTGlobalValue;
@protocol ObjcProt
-(void) ProtMemberFunc;
@end

typedef NSString * AnimalAttributeName NS_STRING_ENUM;

@interface AnimalStatusDescriptor
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingAttributes:(nonnull NSDictionary<AnimalAttributeName, id> *)attributes;
@end
2 changes: 2 additions & 0 deletions test/api-digester/Inputs/cake.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ public func foo2(_ a: Int = #line, b: S1) {}
public enum Number: Int {
case one
}

public func foo3(_ a: [Int: String]) {}
1 change: 1 addition & 0 deletions test/api-digester/Outputs/apinotes-diags.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ APINotesTest(APINotesTest.h): Func SwiftTypeWithMethodLeft.getPropertyA() has be
APINotesTest(APINotesTest.h): Protocol SwiftTypeWithMethodLeft has been renamed to Protocol SwiftTypeWithMethodRight

/* Type Changes */
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingAttributes(_:) has parameter 0 type change from [String : Any] to [AnimalAttributeName : Any]

/* Decl Attribute changes */
22 changes: 22 additions & 0 deletions test/api-digester/Outputs/apinotes-migrator-gen.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
[
{
"DiffItemKind": "CommonDiffItem",
"NodeKind": "Function",
"NodeAnnotation": "TypeRewritten",
"ChildIndex": "1:0",
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(im)animalStatusDescriptorByAddingAttributes:",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "AnimalAttributeName",
"ModuleName": "APINotesTest"
},
{
"DiffItemKind": "CommonDiffItem",
"NodeKind": "Function",
"NodeAnnotation": "TypeRewritten",
"ChildIndex": "1:0",
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(im)animalStatusDescriptorByAddingAttributes:",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "AnimalAttributeName",
"ModuleName": "APINotesTest"
},
{
"DiffItemKind": "CommonDiffItem",
"NodeKind": "Function",
Expand Down
97 changes: 78 additions & 19 deletions test/api-digester/Outputs/cake.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"kind": "TypeNominal",
"name": "C0",
"printedName": "C0<T1, T2, T3>",
"usr": "s:4cake2C0C",
"children": [
{
"kind": "TypeNominal",
Expand Down Expand Up @@ -188,7 +189,8 @@
{
"kind": "TypeNominal",
"name": "S1",
"printedName": "S1"
"printedName": "S1",
"usr": "s:4cake2S1V"
}
]
}
Expand Down Expand Up @@ -249,11 +251,13 @@
"kind": "TypeNominal",
"name": "Optional",
"printedName": "C1?",
"usr": "s:Sq",
"children": [
{
"kind": "TypeNominal",
"name": "C1",
"printedName": "C1"
"printedName": "C1",
"usr": "s:4cake2C1C"
}
]
}
Expand All @@ -277,11 +281,13 @@
"kind": "TypeNominal",
"name": "Optional",
"printedName": "C1?",
"usr": "s:Sq",
"children": [
{
"kind": "TypeNominal",
"name": "C1",
"printedName": "C1"
"printedName": "C1",
"usr": "s:4cake2C1C"
}
]
}
Expand Down Expand Up @@ -316,7 +322,8 @@
{
"kind": "TypeNominal",
"name": "C1",
"printedName": "C1"
"printedName": "C1",
"usr": "s:4cake2C1C"
}
]
},
Expand All @@ -337,7 +344,8 @@
{
"kind": "TypeNominal",
"name": "C1",
"printedName": "C1"
"printedName": "C1",
"usr": "s:4cake2C1C"
}
]
}
Expand All @@ -355,7 +363,8 @@
{
"kind": "TypeNominal",
"name": "C1",
"printedName": "C1"
"printedName": "C1",
"usr": "s:4cake2C1C"
}
]
}
Expand All @@ -379,12 +388,14 @@
"kind": "TypeNominal",
"name": "Int",
"printedName": "Int",
"hasDefaultArg": true
"hasDefaultArg": true,
"usr": "s:Si"
},
{
"kind": "TypeNominal",
"name": "S1",
"printedName": "S1"
"printedName": "S1",
"usr": "s:4cake2S1V"
}
]
},
Expand All @@ -406,12 +417,50 @@
"kind": "TypeNominal",
"name": "Int",
"printedName": "Int",
"hasDefaultArg": true
"hasDefaultArg": true,
"usr": "s:Si"
},
{
"kind": "TypeNominal",
"name": "S1",
"printedName": "S1"
"printedName": "S1",
"usr": "s:4cake2S1V"
}
]
},
{
"kind": "Function",
"name": "foo3",
"printedName": "foo3(_:)",
"declKind": "Func",
"usr": "s:4cake4foo3yys10DictionaryVySiSSGF",
"location": "",
"moduleName": "cake",
"children": [
{
"kind": "TypeNominal",
"name": "Void",
"printedName": "()"
},
{
"kind": "TypeNominal",
"name": "Dictionary",
"printedName": "[Int : String]",
"usr": "s:s10DictionaryV",
"children": [
{
"kind": "TypeNominal",
"name": "Int",
"printedName": "Int",
"usr": "s:Si"
},
{
"kind": "TypeNominal",
"name": "String",
"printedName": "String",
"usr": "s:SS"
}
]
}
]
},
Expand Down Expand Up @@ -447,7 +496,8 @@
{
"kind": "TypeNominal",
"name": "Number",
"printedName": "Number"
"printedName": "Number",
"usr": "s:4cake6NumberO"
},
{
"kind": "TypeNominal",
Expand All @@ -457,7 +507,8 @@
{
"kind": "TypeNominal",
"name": "Number",
"printedName": "Number"
"printedName": "Number",
"usr": "s:4cake6NumberO"
}
]
}
Expand All @@ -477,7 +528,8 @@
{
"kind": "TypeNominal",
"name": "Int",
"printedName": "Int"
"printedName": "Int",
"usr": "s:Si"
}
]
},
Expand All @@ -493,7 +545,8 @@
{
"kind": "TypeNominal",
"name": "Int",
"printedName": "Int"
"printedName": "Int",
"usr": "s:Si"
},
{
"kind": "Getter",
Expand All @@ -507,7 +560,8 @@
{
"kind": "TypeNominal",
"name": "Int",
"printedName": "Int"
"printedName": "Int",
"usr": "s:Si"
}
]
}
Expand All @@ -526,18 +580,21 @@
"kind": "TypeNominal",
"name": "Optional",
"printedName": "Number?",
"usr": "s:Sq",
"children": [
{
"kind": "TypeNominal",
"name": "Number",
"printedName": "Number"
"printedName": "Number",
"usr": "s:4cake6NumberO"
}
]
},
{
"kind": "TypeNominal",
"name": "Int",
"printedName": "Int"
"printedName": "Int",
"usr": "s:Si"
}
]
},
Expand All @@ -553,7 +610,8 @@
{
"kind": "TypeNominal",
"name": "Int",
"printedName": "Int"
"printedName": "Int",
"usr": "s:Si"
},
{
"kind": "Getter",
Expand All @@ -567,7 +625,8 @@
{
"kind": "TypeNominal",
"name": "Int",
"printedName": "Int"
"printedName": "Int",
"usr": "s:Si"
}
]
}
Expand Down
4 changes: 2 additions & 2 deletions test/api-digester/apinotes-diags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RUN: %empty-directory(%t.mod)
// RUN: %empty-directory(%t.sdk)
// RUN: %empty-directory(%t.module-cache)
// RUN: %api-digester -dump-sdk -module APINotesTest -o %t.dump1.json -module-cache-path %t.module-cache -sdk %t.sdk -swift-version 3 -I %S/Inputs/APINotesLeft
// RUN: %api-digester -dump-sdk -module APINotesTest -o %t.dump2.json -module-cache-path %t.module-cache -sdk %t.sdk -swift-version 3 -I %S/Inputs/APINotesRight
// RUN: %api-digester %clang-importer-sdk-nosource -dump-sdk -module APINotesTest -o %t.dump1.json -module-cache-path %t.module-cache -swift-version 3 -I %S/Inputs/APINotesLeft
// RUN: %api-digester %clang-importer-sdk-nosource -dump-sdk -module APINotesTest -o %t.dump2.json -module-cache-path %t.module-cache -swift-version 3 -I %S/Inputs/APINotesRight
// RUN: %api-digester -diagnose-sdk -print-module -input-paths %t.dump1.json -input-paths %t.dump2.json > %t.result
// RUN: diff -u %S/Outputs/apinotes-diags.txt %t.result
4 changes: 2 additions & 2 deletions test/api-digester/apinotes-migrator-gen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RUN: %empty-directory(%t.mod)
// RUN: %empty-directory(%t.sdk)
// RUN: %empty-directory(%t.module-cache)
// RUN: %api-digester -dump-sdk -module APINotesTest -o %t.dump1.json -module-cache-path %t.module-cache -sdk %t.sdk -swift-version 3 -I %S/Inputs/APINotesLeft
// RUN: %api-digester -dump-sdk -module APINotesTest -o %t.dump2.json -module-cache-path %t.module-cache -sdk %t.sdk -swift-version 3 -I %S/Inputs/APINotesRight
// RUN: %api-digester -dump-sdk -module APINotesTest -o %t.dump1.json -module-cache-path %t.module-cache %clang-importer-sdk-nosource -swift-version 3 -I %S/Inputs/APINotesLeft
// RUN: %api-digester -dump-sdk -module APINotesTest -o %t.dump2.json -module-cache-path %t.module-cache %clang-importer-sdk-nosource -swift-version 3 -I %S/Inputs/APINotesRight
// RUN: %api-digester -compare-sdk --input-paths %t.dump1.json -input-paths %t.dump2.json -o %t.result -json
// RUN: diff -u %S/Outputs/apinotes-migrator-gen.json %t.result
8 changes: 4 additions & 4 deletions test/api-digester/compare-dump.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// RUN: %empty-directory(%t.mod)
// RUN: %empty-directory(%t.sdk)
// RUN: %empty-directory(%t.module-cache)
// RUN: %swift -emit-module -o %t.mod/cake1.swiftmodule %S/Inputs/cake1.swift -parse-as-library -I %S/Inputs/APINotesLeft
// RUN: %swift -emit-module -o %t.mod/cake2.swiftmodule %S/Inputs/cake2.swift -parse-as-library -I %S/Inputs/APINotesRight
// RUN: %api-digester -dump-sdk -module cake1 -o %t.dump1.json -module-cache-path %t.module-cache -sdk %t.sdk -swift-version 3 -I %t.mod -I %S/Inputs/APINotesLeft
// RUN: %api-digester -dump-sdk -module cake2 -o %t.dump2.json -module-cache-path %t.module-cache -sdk %t.sdk -swift-version 3 -I %t.mod -I %S/Inputs/APINotesRight
// RUN: %swift -emit-module -o %t.mod/cake1.swiftmodule %S/Inputs/cake1.swift -parse-as-library -I %S/Inputs/APINotesLeft %clang-importer-sdk-nosource
// RUN: %swift -emit-module -o %t.mod/cake2.swiftmodule %S/Inputs/cake2.swift -parse-as-library -I %S/Inputs/APINotesRight %clang-importer-sdk-nosource
// RUN: %api-digester -dump-sdk -module cake1 -o %t.dump1.json -module-cache-path %t.module-cache %clang-importer-sdk-nosource -swift-version 3 -I %t.mod -I %S/Inputs/APINotesLeft
// RUN: %api-digester -dump-sdk -module cake2 -o %t.dump2.json -module-cache-path %t.module-cache %clang-importer-sdk-nosource -swift-version 3 -I %t.mod -I %S/Inputs/APINotesRight
// RUN: %api-digester -diagnose-sdk -print-module --input-paths %t.dump1.json -input-paths %t.dump2.json > %t.result
// RUN: diff -u %S/Outputs/Cake.txt %t.result
17 changes: 15 additions & 2 deletions tools/swift-api-digester/swift-api-digester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,9 +511,12 @@ bool SDKNodeType::classof(const SDKNode *N) {
}

class SDKNodeTypeNominal : public SDKNodeType {
StringRef USR;
public:
SDKNodeTypeNominal(SDKNodeInitInfo Info) : SDKNodeType(Info,
SDKNodeKind::TypeNominal) {}
SDKNodeKind::TypeNominal), USR(Info.USR) {}
// Get the usr of the correspoding nominal type decl.
StringRef getUsr() const { return USR; }
static bool classof(const SDKNode *N);
};

Expand Down Expand Up @@ -1283,6 +1286,10 @@ SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, Type Ty,
TypeInfo(TypeInfo) {
if (isFunctionTypeNoEscape(Ty))
TypeAttrs.push_back(TypeAttrKind::TAK_noescape);
// If this is a nominal type, get its Usr.
if (auto *ND = Ty->getAnyNominal()) {
USR = calculateUsr(Ctx, ND);
}
}

SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, ValueDecl *VD)
Expand Down Expand Up @@ -1752,6 +1759,12 @@ namespace swift {
out.mapRequired(getKeyContent(Ctx, KeyKind::KK_hasDefaultArg).data(),
HasDefault);
}
// Serialize nominal type's USR.
if (auto NT = dyn_cast<SDKNodeTypeNominal>(value)) {
auto Usr = NT->getUsr();
if (!Usr.empty())
out.mapRequired(getKeyContent(Ctx, KeyKind::KK_usr).data(), Usr);
}
}
if (!value->isLeaf()) {
ArrayRef<SDKNode *> Children = value->getChildren();
Expand Down Expand Up @@ -3416,7 +3429,7 @@ static int compareSDKs(StringRef LeftPath, StringRef RightPath,

llvm::errs() << "Dumping diff to " << DiffPath << '\n';
std::vector<OverloadedFuncInfo> Overloads;
OverloadMemberFunctionEmitter::collectDiffItems(RightModule, Overloads);
// OverloadMemberFunctionEmitter::collectDiffItems(RightModule, Overloads);

std::error_code EC;
llvm::raw_fd_ostream Fs(DiffPath, EC, llvm::sys::fs::F_None);
Expand Down