Skip to content

Commit e80a5fc

Browse files
committed
ABI/API checker: populate several lazily computed attributes to nodes
ABI/API checker should check semantic differences of two modules. Adhering too strictly to the actual ASTs could yield false positives. This patch populates ObjC, Dynamic and Final to the attribute list if AST APIs say so. rdar://50217247
1 parent 299f3a5 commit e80a5fc

File tree

9 files changed

+107
-39
lines changed

9 files changed

+107
-39
lines changed

test/api-digester/Inputs/stdlib-stable-abi.json

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21902,6 +21902,41 @@
2190221902
],
2190321903
"funcSelfKind": "NonMutating"
2190421904
},
21905+
{
21906+
"kind": "Function",
21907+
"name": "_isConcrete",
21908+
"printedName": "_isConcrete(_:)",
21909+
"children": [
21910+
{
21911+
"kind": "TypeNominal",
21912+
"name": "Bool",
21913+
"printedName": "Swift.Bool",
21914+
"usr": "s:Sb"
21915+
},
21916+
{
21917+
"kind": "TypeNominal",
21918+
"name": "Metatype",
21919+
"printedName": "τ_0_0.Type",
21920+
"children": [
21921+
{
21922+
"kind": "TypeNominal",
21923+
"name": "GenericTypeParam",
21924+
"printedName": "τ_0_0"
21925+
}
21926+
]
21927+
}
21928+
],
21929+
"declKind": "Func",
21930+
"usr": "s:s11_isConcreteySbxmlF",
21931+
"moduleName": "Swift",
21932+
"genericSig": "<τ_0_0>",
21933+
"sugared_genericSig": "<T>",
21934+
"declAttributes": [
21935+
"Transparent",
21936+
"AlwaysEmitIntoClient"
21937+
],
21938+
"funcSelfKind": "NonMutating"
21939+
},
2190521940
{
2190621941
"kind": "Function",
2190721942
"name": "_isBitwiseTakable",
@@ -35458,7 +35493,8 @@
3545835493
"Final",
3545935494
"ObjCNonLazyRealization",
3546035495
"UsableFromInline",
35461-
"FixedLayout"
35496+
"FixedLayout",
35497+
"ObjC"
3546235498
],
3546335499
"superclassUsr": "c:@M@Swift@objc(cs)__ContiguousArrayStorageBase",
3546435500
"superclassNames": [
@@ -49970,7 +50006,8 @@
4997050006
"declAttributes": [
4997150007
"ObjCNonLazyRealization",
4997250008
"UsableFromInline",
49973-
"FixedLayout"
50009+
"FixedLayout",
50010+
"ObjC"
4997450011
],
4997550012
"superclassUsr": "c:@M@Swift@objc(cs)__SwiftNativeNSDictionary",
4997650013
"superclassNames": [
@@ -49986,7 +50023,8 @@
4998650023
"moduleName": "Swift",
4998750024
"declAttributes": [
4998850025
"UsableFromInline",
49989-
"FixedLayout"
50026+
"FixedLayout",
50027+
"ObjC"
4999050028
],
4999150029
"superclassUsr": "c:@M@Swift@objc(cs)__RawDictionaryStorage",
4999250030
"superclassNames": [
@@ -115758,7 +115796,8 @@
115758115796
"declAttributes": [
115759115797
"ObjCNonLazyRealization",
115760115798
"UsableFromInline",
115761-
"FixedLayout"
115799+
"FixedLayout",
115800+
"ObjC"
115762115801
],
115763115802
"superclassUsr": "c:@M@Swift@objc(cs)__SwiftNativeNSSet",
115764115803
"superclassNames": [
@@ -115774,7 +115813,8 @@
115774115813
"moduleName": "Swift",
115775115814
"declAttributes": [
115776115815
"UsableFromInline",
115777-
"FixedLayout"
115816+
"FixedLayout",
115817+
"ObjC"
115778115818
],
115779115819
"superclassUsr": "c:@M@Swift@objc(cs)__RawSetStorage",
115780115820
"superclassNames": [
@@ -147323,7 +147363,8 @@
147323147363
"moduleName": "Swift",
147324147364
"declAttributes": [
147325147365
"UsableFromInline",
147326-
"FixedLayout"
147366+
"FixedLayout",
147367+
"ObjC"
147327147368
],
147328147369
"superclassUsr": "c:@M@Swift@objc(cs)__SwiftNativeNSArray",
147329147370
"superclassNames": [
@@ -147711,7 +147752,8 @@
147711147752
"moduleName": "Swift",
147712147753
"declAttributes": [
147713147754
"FixedLayout",
147714-
"UsableFromInline"
147755+
"UsableFromInline",
147756+
"ObjC"
147715147757
],
147716147758
"superclassUsr": "c:@M@Swift@objc(cs)__SwiftNativeNSArrayWithContiguousStorage",
147717147759
"superclassNames": [
@@ -299699,7 +299741,7 @@
299699299741
"funcSelfKind": "NonMutating"
299700299742
}
299701299743
],
299702-
"json_format_version": 4,
299744+
"json_format_version": 5,
299703299745
"tool_arguments": [
299704299746
"-dump-sdk",
299705299747
"-module",
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
1-
// We shouldn't see this. rdar://54797231
2-
cake: Class C4 is now with @objc
1+
cake: Func FrozenKind.__derived_enum_equals(_:_:) has been removed
2+
cake: Accessor GlobalLetChangedToVar.Get() is a new API without @available attribute
3+
cake: Accessor GlobalVarChangedToLet.Get() is a new API without @available attribute
4+
cake: Accessor GlobalVarChangedToLet.Modify() is a new API without @available attribute
5+
cake: Accessor GlobalVarChangedToLet.Set() is a new API without @available attribute
6+
cake: Enum FrozenKind is now with @frozen
7+
cake: Enum IceKind is now with @frozen
8+
cake: Func FrozenKind.==(_:_:) is a new API without @available attribute
9+
cake: Var C1.CIIns1 is no longer a stored property
10+
cake: Var C1.CIIns2 is no longer a stored property
11+
cake: Var RemoveSetters.Value is no longer a stored property

test/api-digester/Outputs/cake-abi.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1826,5 +1826,5 @@
18261826
]
18271827
}
18281828
],
1829-
"json_format_version": 4
1829+
"json_format_version": 5
18301830
}

test/api-digester/Outputs/cake.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1674,5 +1674,5 @@
16741674
]
16751675
}
16761676
],
1677-
"json_format_version": 4
1677+
"json_format_version": 5
16781678
}

test/api-digester/Outputs/clang-module-dump.txt

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"protocolReq": true,
3434
"objc_name": "anotherFunctionFromProt",
3535
"declAttributes": [
36-
"ObjC"
36+
"ObjC",
37+
"Dynamic"
3738
],
3839
"reqNewWitnessTableEntry": true,
3940
"funcSelfKind": "NonMutating"
@@ -44,7 +45,8 @@
4445
"moduleName": "Foo",
4546
"objc_name": "AnotherObjcProt",
4647
"declAttributes": [
47-
"ObjC"
48+
"ObjC",
49+
"Dynamic"
4850
]
4951
},
5052
{
@@ -76,7 +78,8 @@
7678
"isOpen": true,
7779
"objc_name": "someFunction",
7880
"declAttributes": [
79-
"ObjC"
81+
"ObjC",
82+
"Dynamic"
8083
],
8184
"funcSelfKind": "NonMutating"
8285
},
@@ -100,7 +103,8 @@
100103
"objc_name": "init",
101104
"declAttributes": [
102105
"Override",
103-
"ObjC"
106+
"ObjC",
107+
"Dynamic"
104108
]
105109
}
106110
],
@@ -110,7 +114,8 @@
110114
"isOpen": true,
111115
"objc_name": "ClangInterface",
112116
"declAttributes": [
113-
"ObjC"
117+
"ObjC",
118+
"Dynamic"
114119
],
115120
"superclassUsr": "c:objc(cs)NSObject",
116121
"superclassNames": [
@@ -161,7 +166,8 @@
161166
"protocolReq": true,
162167
"objc_name": "someFunctionFromProt",
163168
"declAttributes": [
164-
"ObjC"
169+
"ObjC",
170+
"Dynamic"
165171
],
166172
"reqNewWitnessTableEntry": true,
167173
"funcSelfKind": "NonMutating"
@@ -172,9 +178,10 @@
172178
"moduleName": "Foo",
173179
"objc_name": "ObjcProt",
174180
"declAttributes": [
175-
"ObjC"
181+
"ObjC",
182+
"Dynamic"
176183
]
177184
}
178185
],
179-
"json_format_version": 4
186+
"json_format_version": 5
180187
}

test/api-digester/Outputs/empty-baseline.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"kind": "Root",
33
"name": "TopLevel",
44
"printedName": "TopLevel",
5-
"json_format_version": 4
5+
"json_format_version": 5
66
}

test/api-digester/compare-dump-binary-vs-interface.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
// RUN: %empty-directory(%t.sdk)
44
// RUN: %empty-directory(%t.module-cache)
55

6+
// The goal of this test to make sure flag -use-interface-for-module works.
7+
// We first build .swiftinterface with -enable-library-evolution
8+
// Secondly, we We first build .swiftmodule without -enable-library-evolution
9+
// Using swift-api-digester to load via .swiftinterface and .swiftmodule should
10+
// always give us some difference.
11+
612
// Generate .swiftinterface file for module cake
713
// RUN: %target-swift-frontend -typecheck -emit-parseable-module-interface-path %t.mod1/cake.swiftinterface %S/Inputs/cake_baseline/cake.swift -I %S/Inputs/APINotesLeft %clang-importer-sdk-nosource -parse-as-library -enable-library-evolution -disable-objc-attr-requires-foundation-module -module-cache-path %t.module-cache
814

915
// Generate .swiftmodule file for module cake
10-
// RUN: %target-swift-frontend -emit-module -o %t.mod1/cake.swiftmodule %S/Inputs/cake_baseline/cake.swift -I %S/Inputs/APINotesLeft %clang-importer-sdk-nosource -parse-as-library -disable-objc-attr-requires-foundation-module -module-cache-path %t.module-cache -enable-library-evolution
16+
// RUN: %target-swift-frontend -emit-module -o %t.mod1/cake.swiftmodule %S/Inputs/cake_baseline/cake.swift -I %S/Inputs/APINotesLeft %clang-importer-sdk-nosource -parse-as-library -disable-objc-attr-requires-foundation-module -module-cache-path %t.module-cache
1117

1218
// Dump Json file for cake ABI via .swiftmodule file
1319
// RUN: %api-digester -dump-sdk -module cake -o - -module-cache-path %t.module-cache %clang-importer-sdk-nosource -I %t.mod1 -I %S/Inputs/APINotesLeft -abi > %t.dump1.json

tools/swift-api-digester/ModuleAnalyzerNodes.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,24 @@ SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, Type Ty, TypeInitInfo Info) :
12761276
}
12771277
}
12781278

1279+
static std::vector<DeclAttrKind> collectDeclAttributes(Decl *D) {
1280+
std::vector<DeclAttrKind> Results;
1281+
for (auto *Attr: D->getAttrs())
1282+
Results.push_back(Attr->getKind());
1283+
if (auto *VD = dyn_cast<ValueDecl>(D)) {
1284+
#define HANDLE(COND, KIND_NAME) \
1285+
if (VD->COND && !llvm::is_contained(Results, DeclAttrKind::KIND_NAME)) \
1286+
Results.emplace_back(DeclAttrKind::KIND_NAME);
1287+
// These attributes may be semantically applicable to the current decl but absent from
1288+
// the actual AST. Populting them to the nodes ensure we don't have false positives.
1289+
HANDLE(isObjC(), DAK_ObjC)
1290+
HANDLE(isFinal(), DAK_Final)
1291+
HANDLE(isDynamic(), DAK_Dynamic)
1292+
#undef HANDLE
1293+
}
1294+
return Results;
1295+
}
1296+
12791297
SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, Decl *D):
12801298
Ctx(Ctx), DKind(D->getKind()),
12811299
Location(calculateLocation(Ctx, D)),
@@ -1292,22 +1310,8 @@ SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, Decl *D):
12921310
ObjCName(Ctx.getObjcName(D)),
12931311
IsImplicit(D->isImplicit()),
12941312
IsDeprecated(D->getAttrs().getDeprecated(D->getASTContext())),
1295-
IsABIPlaceholder(isABIPlaceholderRecursive(D)) {
1296-
1297-
// Force some attributes that are lazily computed.
1298-
// FIXME: we should use these AST predicates directly instead of looking at
1299-
// the attributes rdar://50217247.
1300-
if (auto *VD = dyn_cast<ValueDecl>(D)) {
1301-
(void) VD->isObjC();
1302-
(void) VD->isFinal();
1303-
(void) VD->isDynamic();
1304-
}
1305-
1306-
// Capture all attributes.
1307-
auto AllAttrs = D->getAttrs();
1308-
std::transform(AllAttrs.begin(), AllAttrs.end(), std::back_inserter(DeclAttrs),
1309-
[](DeclAttribute *attr) { return attr->getKind(); });
1310-
}
1313+
IsABIPlaceholder(isABIPlaceholderRecursive(D)),
1314+
DeclAttrs(collectDeclAttributes(D)) {}
13111315

13121316
SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, OperatorDecl *OD):
13131317
SDKNodeInitInfo(Ctx, cast<Decl>(OD)) {

tools/swift-api-digester/ModuleAnalyzerNodes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ namespace api {
6262
///
6363
/// When the json format changes in a way that requires version-specific handling, this number should be incremented.
6464
/// This ensures we could have backward compatibility so that version changes in the format won't stop the checker from working.
65-
const uint8_t DIGESTER_JSON_VERSION = 4; // Add objc_name field
65+
const uint8_t DIGESTER_JSON_VERSION = 5; // Populate ObjC, Dynamic and Final to attribute list
6666
const uint8_t DIGESTER_JSON_DEFAULT_VERSION = 0; // Use this version number for files before we have a version number in json.
6767

6868
class SDKNode;

0 commit comments

Comments
 (0)