Skip to content

Commit 20a48e5

Browse files
authored
swift-api-digester: teach the tool to serialize USRs for nominal type. (#15463)
This allows us to map back from a type to the declaration in the dump, so that we can perform more fine-grained analysis like whether a string has been changed to string enum.
1 parent b6c2e13 commit 20a48e5

File tree

10 files changed

+138
-29
lines changed

10 files changed

+138
-29
lines changed

test/api-digester/Inputs/APINotesLeft/APINotesTest.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#import <Foundation.h>
12
extern int ANTGlobalValue;
23

34
@interface NewType
@@ -16,3 +17,7 @@ extern int ANTGlobalValue;
1617
-(void) ProtMemberFunc2;
1718
-(void) ProtMemberFunc3;
1819
@end
20+
21+
@interface AnimalStatusDescriptor
22+
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingAttributes:(nonnull NSDictionary<NSString*, id> *)attributes;
23+
@end

test/api-digester/Inputs/APINotesRight/APINotesTest.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#import <objc_generics.h>
12
extern int ANTGlobalValue;
23

34
@interface NewType
@@ -14,3 +15,9 @@ extern int ANTGlobalValue;
1415
@protocol ObjcProt
1516
-(void) ProtMemberFunc;
1617
@end
18+
19+
typedef NSString * AnimalAttributeName NS_STRING_ENUM;
20+
21+
@interface AnimalStatusDescriptor
22+
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingAttributes:(nonnull NSDictionary<AnimalAttributeName, id> *)attributes;
23+
@end

test/api-digester/Inputs/cake.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ public func foo2(_ a: Int = #line, b: S1) {}
3535
public enum Number: Int {
3636
case one
3737
}
38+
39+
public func foo3(_ a: [Int: String]) {}

test/api-digester/Outputs/apinotes-diags.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ APINotesTest(APINotesTest.h): Func SwiftTypeWithMethodLeft.getPropertyA() has be
1111
APINotesTest(APINotesTest.h): Protocol SwiftTypeWithMethodLeft has been renamed to Protocol SwiftTypeWithMethodRight
1212

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

1516
/* Decl Attribute changes */

test/api-digester/Outputs/apinotes-migrator-gen.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
11
[
2+
{
3+
"DiffItemKind": "CommonDiffItem",
4+
"NodeKind": "Function",
5+
"NodeAnnotation": "TypeRewritten",
6+
"ChildIndex": "1:0",
7+
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(im)animalStatusDescriptorByAddingAttributes:",
8+
"LeftComment": "String",
9+
"RightUsr": "",
10+
"RightComment": "AnimalAttributeName",
11+
"ModuleName": "APINotesTest"
12+
},
13+
{
14+
"DiffItemKind": "CommonDiffItem",
15+
"NodeKind": "Function",
16+
"NodeAnnotation": "TypeRewritten",
17+
"ChildIndex": "1:0",
18+
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(im)animalStatusDescriptorByAddingAttributes:",
19+
"LeftComment": "String",
20+
"RightUsr": "",
21+
"RightComment": "AnimalAttributeName",
22+
"ModuleName": "APINotesTest"
23+
},
224
{
325
"DiffItemKind": "CommonDiffItem",
426
"NodeKind": "Function",

test/api-digester/Outputs/cake.json

Lines changed: 78 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"kind": "TypeNominal",
4444
"name": "C0",
4545
"printedName": "C0<T1, T2, T3>",
46+
"usr": "s:4cake2C0C",
4647
"children": [
4748
{
4849
"kind": "TypeNominal",
@@ -188,7 +189,8 @@
188189
{
189190
"kind": "TypeNominal",
190191
"name": "S1",
191-
"printedName": "S1"
192+
"printedName": "S1",
193+
"usr": "s:4cake2S1V"
192194
}
193195
]
194196
}
@@ -249,11 +251,13 @@
249251
"kind": "TypeNominal",
250252
"name": "Optional",
251253
"printedName": "C1?",
254+
"usr": "s:Sq",
252255
"children": [
253256
{
254257
"kind": "TypeNominal",
255258
"name": "C1",
256-
"printedName": "C1"
259+
"printedName": "C1",
260+
"usr": "s:4cake2C1C"
257261
}
258262
]
259263
}
@@ -277,11 +281,13 @@
277281
"kind": "TypeNominal",
278282
"name": "Optional",
279283
"printedName": "C1?",
284+
"usr": "s:Sq",
280285
"children": [
281286
{
282287
"kind": "TypeNominal",
283288
"name": "C1",
284-
"printedName": "C1"
289+
"printedName": "C1",
290+
"usr": "s:4cake2C1C"
285291
}
286292
]
287293
}
@@ -316,7 +322,8 @@
316322
{
317323
"kind": "TypeNominal",
318324
"name": "C1",
319-
"printedName": "C1"
325+
"printedName": "C1",
326+
"usr": "s:4cake2C1C"
320327
}
321328
]
322329
},
@@ -337,7 +344,8 @@
337344
{
338345
"kind": "TypeNominal",
339346
"name": "C1",
340-
"printedName": "C1"
347+
"printedName": "C1",
348+
"usr": "s:4cake2C1C"
341349
}
342350
]
343351
}
@@ -355,7 +363,8 @@
355363
{
356364
"kind": "TypeNominal",
357365
"name": "C1",
358-
"printedName": "C1"
366+
"printedName": "C1",
367+
"usr": "s:4cake2C1C"
359368
}
360369
]
361370
}
@@ -379,12 +388,14 @@
379388
"kind": "TypeNominal",
380389
"name": "Int",
381390
"printedName": "Int",
382-
"hasDefaultArg": true
391+
"hasDefaultArg": true,
392+
"usr": "s:Si"
383393
},
384394
{
385395
"kind": "TypeNominal",
386396
"name": "S1",
387-
"printedName": "S1"
397+
"printedName": "S1",
398+
"usr": "s:4cake2S1V"
388399
}
389400
]
390401
},
@@ -406,12 +417,50 @@
406417
"kind": "TypeNominal",
407418
"name": "Int",
408419
"printedName": "Int",
409-
"hasDefaultArg": true
420+
"hasDefaultArg": true,
421+
"usr": "s:Si"
410422
},
411423
{
412424
"kind": "TypeNominal",
413425
"name": "S1",
414-
"printedName": "S1"
426+
"printedName": "S1",
427+
"usr": "s:4cake2S1V"
428+
}
429+
]
430+
},
431+
{
432+
"kind": "Function",
433+
"name": "foo3",
434+
"printedName": "foo3(_:)",
435+
"declKind": "Func",
436+
"usr": "s:4cake4foo3yys10DictionaryVySiSSGF",
437+
"location": "",
438+
"moduleName": "cake",
439+
"children": [
440+
{
441+
"kind": "TypeNominal",
442+
"name": "Void",
443+
"printedName": "()"
444+
},
445+
{
446+
"kind": "TypeNominal",
447+
"name": "Dictionary",
448+
"printedName": "[Int : String]",
449+
"usr": "s:s10DictionaryV",
450+
"children": [
451+
{
452+
"kind": "TypeNominal",
453+
"name": "Int",
454+
"printedName": "Int",
455+
"usr": "s:Si"
456+
},
457+
{
458+
"kind": "TypeNominal",
459+
"name": "String",
460+
"printedName": "String",
461+
"usr": "s:SS"
462+
}
463+
]
415464
}
416465
]
417466
},
@@ -447,7 +496,8 @@
447496
{
448497
"kind": "TypeNominal",
449498
"name": "Number",
450-
"printedName": "Number"
499+
"printedName": "Number",
500+
"usr": "s:4cake6NumberO"
451501
},
452502
{
453503
"kind": "TypeNominal",
@@ -457,7 +507,8 @@
457507
{
458508
"kind": "TypeNominal",
459509
"name": "Number",
460-
"printedName": "Number"
510+
"printedName": "Number",
511+
"usr": "s:4cake6NumberO"
461512
}
462513
]
463514
}
@@ -477,7 +528,8 @@
477528
{
478529
"kind": "TypeNominal",
479530
"name": "Int",
480-
"printedName": "Int"
531+
"printedName": "Int",
532+
"usr": "s:Si"
481533
}
482534
]
483535
},
@@ -493,7 +545,8 @@
493545
{
494546
"kind": "TypeNominal",
495547
"name": "Int",
496-
"printedName": "Int"
548+
"printedName": "Int",
549+
"usr": "s:Si"
497550
},
498551
{
499552
"kind": "Getter",
@@ -507,7 +560,8 @@
507560
{
508561
"kind": "TypeNominal",
509562
"name": "Int",
510-
"printedName": "Int"
563+
"printedName": "Int",
564+
"usr": "s:Si"
511565
}
512566
]
513567
}
@@ -526,18 +580,21 @@
526580
"kind": "TypeNominal",
527581
"name": "Optional",
528582
"printedName": "Number?",
583+
"usr": "s:Sq",
529584
"children": [
530585
{
531586
"kind": "TypeNominal",
532587
"name": "Number",
533-
"printedName": "Number"
588+
"printedName": "Number",
589+
"usr": "s:4cake6NumberO"
534590
}
535591
]
536592
},
537593
{
538594
"kind": "TypeNominal",
539595
"name": "Int",
540-
"printedName": "Int"
596+
"printedName": "Int",
597+
"usr": "s:Si"
541598
}
542599
]
543600
},
@@ -553,7 +610,8 @@
553610
{
554611
"kind": "TypeNominal",
555612
"name": "Int",
556-
"printedName": "Int"
613+
"printedName": "Int",
614+
"usr": "s:Si"
557615
},
558616
{
559617
"kind": "Getter",
@@ -567,7 +625,8 @@
567625
{
568626
"kind": "TypeNominal",
569627
"name": "Int",
570-
"printedName": "Int"
628+
"printedName": "Int",
629+
"usr": "s:Si"
571630
}
572631
]
573632
}

test/api-digester/apinotes-diags.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// RUN: %empty-directory(%t.mod)
33
// RUN: %empty-directory(%t.sdk)
44
// RUN: %empty-directory(%t.module-cache)
5-
// 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
6-
// 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
5+
// 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
6+
// 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
77
// RUN: %api-digester -diagnose-sdk -print-module -input-paths %t.dump1.json -input-paths %t.dump2.json > %t.result
88
// RUN: diff -u %S/Outputs/apinotes-diags.txt %t.result

test/api-digester/apinotes-migrator-gen.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// RUN: %empty-directory(%t.mod)
33
// RUN: %empty-directory(%t.sdk)
44
// RUN: %empty-directory(%t.module-cache)
5-
// 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
6-
// 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
5+
// 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
6+
// 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
77
// RUN: %api-digester -compare-sdk --input-paths %t.dump1.json -input-paths %t.dump2.json -o %t.result -json
88
// RUN: diff -u %S/Outputs/apinotes-migrator-gen.json %t.result

test/api-digester/compare-dump.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// RUN: %empty-directory(%t.mod)
22
// RUN: %empty-directory(%t.sdk)
33
// RUN: %empty-directory(%t.module-cache)
4-
// RUN: %swift -emit-module -o %t.mod/cake1.swiftmodule %S/Inputs/cake1.swift -parse-as-library -I %S/Inputs/APINotesLeft
5-
// RUN: %swift -emit-module -o %t.mod/cake2.swiftmodule %S/Inputs/cake2.swift -parse-as-library -I %S/Inputs/APINotesRight
6-
// 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
7-
// 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
4+
// RUN: %swift -emit-module -o %t.mod/cake1.swiftmodule %S/Inputs/cake1.swift -parse-as-library -I %S/Inputs/APINotesLeft %clang-importer-sdk-nosource
5+
// RUN: %swift -emit-module -o %t.mod/cake2.swiftmodule %S/Inputs/cake2.swift -parse-as-library -I %S/Inputs/APINotesRight %clang-importer-sdk-nosource
6+
// 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
7+
// 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
88
// RUN: %api-digester -diagnose-sdk -print-module --input-paths %t.dump1.json -input-paths %t.dump2.json > %t.result
99
// RUN: diff -u %S/Outputs/Cake.txt %t.result

tools/swift-api-digester/swift-api-digester.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,9 +511,12 @@ bool SDKNodeType::classof(const SDKNode *N) {
511511
}
512512

513513
class SDKNodeTypeNominal : public SDKNodeType {
514+
StringRef USR;
514515
public:
515516
SDKNodeTypeNominal(SDKNodeInitInfo Info) : SDKNodeType(Info,
516-
SDKNodeKind::TypeNominal) {}
517+
SDKNodeKind::TypeNominal), USR(Info.USR) {}
518+
// Get the usr of the correspoding nominal type decl.
519+
StringRef getUsr() const { return USR; }
517520
static bool classof(const SDKNode *N);
518521
};
519522

@@ -1286,6 +1289,10 @@ SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, Type Ty,
12861289
TypeInfo(TypeInfo) {
12871290
if (isFunctionTypeNoEscape(Ty))
12881291
TypeAttrs.push_back(TypeAttrKind::TAK_noescape);
1292+
// If this is a nominal type, get its Usr.
1293+
if (auto *ND = Ty->getAnyNominal()) {
1294+
USR = calculateUsr(Ctx, ND);
1295+
}
12891296
}
12901297

12911298
SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, ValueDecl *VD)
@@ -1760,6 +1767,12 @@ namespace swift {
17601767
out.mapRequired(getKeyContent(Ctx, KeyKind::KK_hasDefaultArg).data(),
17611768
HasDefault);
17621769
}
1770+
// Serialize nominal type's USR.
1771+
if (auto NT = dyn_cast<SDKNodeTypeNominal>(value)) {
1772+
auto Usr = NT->getUsr();
1773+
if (!Usr.empty())
1774+
out.mapRequired(getKeyContent(Ctx, KeyKind::KK_usr).data(), Usr);
1775+
}
17631776
}
17641777
if (!value->isLeaf()) {
17651778
ArrayRef<SDKNode *> Children = value->getChildren();
@@ -3424,7 +3437,7 @@ static int compareSDKs(StringRef LeftPath, StringRef RightPath,
34243437

34253438
llvm::errs() << "Dumping diff to " << DiffPath << '\n';
34263439
std::vector<OverloadedFuncInfo> Overloads;
3427-
OverloadMemberFunctionEmitter::collectDiffItems(RightModule, Overloads);
3440+
// OverloadMemberFunctionEmitter::collectDiffItems(RightModule, Overloads);
34283441

34293442
std::error_code EC;
34303443
llvm::raw_fd_ostream Fs(DiffPath, EC, llvm::sys::fs::F_None);

0 commit comments

Comments
 (0)