Skip to content

Commit d930cbd

Browse files
authored
Merge pull request #20833 from akyrtzi/api-checker-optional-protocol
[api-digester] Don't report as API breaking when an ObjC protocol gets a new optional method
2 parents 8935b89 + 0413358 commit d930cbd

File tree

7 files changed

+32
-1
lines changed

7 files changed

+32
-1
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#import <Foundation.h>
2+
3+
@protocol ObjcProt
4+
-(void) someFunctionFromProt;
5+
6+
-(void) someFunctionFromProt2;
7+
8+
@optional
9+
-(void) someOptionalFunctionFromProt;
10+
@end
11+
12+
@interface ClangInterface: NSObject <ObjcProt>
13+
- (void)someFunction;
14+
@end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Foo {
2+
header "foo.h"
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Foo: Func ObjcProt.someFunctionFromProt2() has been added as a protocol requirement
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %empty-directory(%t.module-cache)
2+
// RUN: %api-digester -dump-sdk -module Foo -o %t.dump1.json -module-cache-path %t.module-cache %clang-importer-sdk-nosource -I %S/Inputs/Foo -avoid-location
3+
// RUN: %api-digester -dump-sdk -module Foo -o %t.dump2.json -module-cache-path %t.module-cache %clang-importer-sdk-nosource -I %S/Inputs/Foo-new-version -avoid-location
4+
// RUN: %api-digester -diagnose-sdk -print-module --input-paths %t.dump1.json -input-paths %t.dump2.json -o %t.result
5+
6+
// RUN: %clang -E -P -x c %S/Outputs/Foo-diff.txt -o - | sed '/^\s*$/d' > %t.expected
7+
// RUN: %clang -E -P -x c %t.result -o - | sed '/^\s*$/d' > %t.result.tmp
8+
// RUN: diff -u %t.expected %t.result.tmp

tools/swift-api-digester/ModuleAnalyzerNodes.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,10 @@ StringRef SDKNodeDecl::getFullyQualifiedName() const {
393393
return getSDKContext().buffer(OS.str());
394394
}
395395

396+
bool SDKNodeDecl::isNonOptionalProtocolRequirement() const {
397+
return isProtocolRequirement() && !hasDeclAttribute(DAK_Optional);
398+
}
399+
396400
bool SDKNodeDecl::hasDeclAttribute(DeclAttrKind DAKind) const {
397401
return std::find(DeclAttributes.begin(), DeclAttributes.end(), DAKind) !=
398402
DeclAttributes.end();

tools/swift-api-digester/ModuleAnalyzerNodes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ class SDKNodeDecl: public SDKNode {
322322
StringRef getFullyQualifiedName() const;
323323
bool isDeprecated() const { return IsDeprecated; };
324324
bool isProtocolRequirement() const { return IsProtocolReq; }
325+
bool isNonOptionalProtocolRequirement() const;
325326
bool hasDeclAttribute(DeclAttrKind DAKind) const;
326327
bool isImplicit() const { return IsImplicit; };
327328
bool isStatic() const { return IsStatic; };

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ class PrunePass : public MatchedNodeListener, public SDKTreeDiffPass {
978978
}
979979
// Complain about added protocol requirements
980980
if (auto *D = dyn_cast<SDKNodeDecl>(Right)) {
981-
if (D->isProtocolRequirement()) {
981+
if (D->isNonOptionalProtocolRequirement()) {
982982
bool ShouldComplain = !D->isOverriding();
983983
// We should allow added associated types with default.
984984
if (auto ATD = dyn_cast<SDKNodeDeclAssociatedType>(D)) {

0 commit comments

Comments
 (0)