Skip to content

Commit

Permalink
Merging r351459:
Browse files Browse the repository at this point in the history
------------------------------------------------------------------------
r351459 | arphaman | 2019-01-17 19:12:45 +0100 (Thu, 17 Jan 2019) | 13 lines

[ObjC] Follow-up r350768 and allow the use of unavailable methods that are
declared in a parent class from within the @implementation context

This commit extends r350768 and allows the use of methods marked as unavailable
that are declared in a parent class/category from within the @implementation of
the class where the method is marked as unavailable.
This allows users to call init that's marked as unavailable even if they don't
define it.

rdar://47134898

Differential Revision: https://reviews.llvm.org/D56816

------------------------------------------------------------------------

llvm-svn: 351535
  • Loading branch information
zmodem committed Jan 18, 2019
1 parent c4fa34c commit ad1624f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
8 changes: 3 additions & 5 deletions clang/lib/Sema/SemaDeclAttr.cpp
Expand Up @@ -7365,13 +7365,11 @@ ShouldDiagnoseAvailabilityInContext(Sema &S, AvailabilityResult K,
return true;
} else if (K == AR_Unavailable) {
// It is perfectly fine to refer to an 'unavailable' Objective-C method
// when it's actually defined and is referenced from within the
// @implementation itself. In this context, we interpret unavailable as a
// form of access control.
// when it is referenced from within the @implementation itself. In this
// context, we interpret unavailable as a form of access control.
if (const auto *MD = dyn_cast<ObjCMethodDecl>(OffendingDecl)) {
if (const auto *Impl = dyn_cast<ObjCImplDecl>(C)) {
if (MD->getClassInterface() == Impl->getClassInterface() &&
MD->isDefined())
if (MD->getClassInterface() == Impl->getClassInterface())
return true;
}
}
Expand Down
22 changes: 20 additions & 2 deletions clang/test/SemaObjC/call-unavailable-init-in-self.m
Expand Up @@ -5,13 +5,24 @@ @interface NSObject
+ (instancetype)new;
+ (instancetype)alloc;

- (void)declaredInSuper;

@end

@interface NSObject (Category)

- (void)declaredInSuperCategory;

@end

@interface Sub: NSObject

- (instancetype)init __attribute__((unavailable)); // expected-note 4 {{'init' has been explicitly marked unavailable here}}

- (void)notImplemented __attribute__((unavailable)); // expected-note {{'notImplemented' has been explicitly marked unavailable here}}
- (void)notImplemented __attribute__((unavailable));

- (void)declaredInSuper __attribute__((unavailable));
- (void)declaredInSuperCategory __attribute__((unavailable));

@end

Expand All @@ -34,7 +45,14 @@ - (instancetype) init {
}

- (void)reportUseOfUnimplemented {
[self notImplemented]; // expected-error {{'notImplemented' is unavailable}}
[self notImplemented];
}

- (void)allowSuperCallUsingSelf {
[self declaredInSuper];
[[Sub alloc] declaredInSuper];
[self declaredInSuperCategory];
[[Sub alloc] declaredInSuperCategory];
}

@end
Expand Down
4 changes: 2 additions & 2 deletions clang/test/SemaObjC/infer-availability-from-init.m
Expand Up @@ -47,12 +47,12 @@ void usenotmyobject() {
}

@interface FromSelf : NSObject
-(instancetype)init __attribute__((unavailable)); // expected-note {{'init' has been explicitly marked unavailable here}}
-(instancetype)init __attribute__((unavailable));
+(FromSelf*)another_one;
@end

@implementation FromSelf
+(FromSelf*)another_one {
[self new]; // expected-error{{'new' is unavailable}}
[self new];
}
@end

0 comments on commit ad1624f

Please sign in to comment.