2013-09-28 04:21:48 +08:00
|
|
|
// RUN: %clang_cc1 -analyze -analyzer-checker=core,alpha.osx.cocoa.InstanceVariableInvalidation -DRUN_IVAR_INVALIDATION -verify %s
|
|
|
|
// RUN: %clang_cc1 -analyze -analyzer-checker=core,alpha.osx.cocoa.MissingInvalidationMethod -DRUN_MISSING_INVALIDATION_METHOD -verify %s
|
2013-01-11 07:34:16 +08:00
|
|
|
extern void __assert_fail (__const char *__assertion, __const char *__file,
|
|
|
|
unsigned int __line, __const char *__function)
|
|
|
|
__attribute__ ((__noreturn__));
|
|
|
|
|
|
|
|
#define assert(expr) \
|
|
|
|
((expr) ? (void)(0) : __assert_fail (#expr, __FILE__, __LINE__, __func__))
|
2012-09-27 02:55:16 +08:00
|
|
|
|
|
|
|
@protocol NSObject
|
|
|
|
@end
|
|
|
|
@interface NSObject <NSObject> {}
|
|
|
|
+(id)alloc;
|
|
|
|
+(id)new;
|
|
|
|
-(id)init;
|
|
|
|
-(id)autorelease;
|
|
|
|
-(id)copy;
|
|
|
|
- (Class)class;
|
|
|
|
-(id)retain;
|
2012-09-29 08:20:38 +08:00
|
|
|
-(id)description;
|
2012-09-27 02:55:16 +08:00
|
|
|
@end
|
2012-09-29 08:20:38 +08:00
|
|
|
@class NSString;
|
|
|
|
|
|
|
|
extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
|
2012-09-27 02:55:16 +08:00
|
|
|
|
|
|
|
@protocol Invalidation1 <NSObject>
|
|
|
|
- (void) invalidate __attribute__((annotate("objc_instance_variable_invalidator")));
|
|
|
|
@end
|
|
|
|
|
|
|
|
@protocol Invalidation2 <NSObject>
|
|
|
|
- (void) invalidate __attribute__((annotate("objc_instance_variable_invalidator")));
|
|
|
|
@end
|
|
|
|
|
|
|
|
@protocol Invalidation3 <NSObject>
|
|
|
|
- (void) invalidate __attribute__((annotate("objc_instance_variable_invalidator")));
|
2012-09-29 08:20:38 +08:00
|
|
|
- (void) invalidate2 __attribute__((annotate("objc_instance_variable_invalidator")));
|
2012-09-27 02:55:16 +08:00
|
|
|
@end
|
|
|
|
|
2013-01-11 11:52:40 +08:00
|
|
|
@protocol Invalidation3;
|
|
|
|
@protocol Invalidation2;
|
|
|
|
|
2012-09-27 02:55:16 +08:00
|
|
|
@interface Invalidation2Class <Invalidation2>
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface Invalidation1Class <Invalidation1>
|
|
|
|
@end
|
|
|
|
|
2013-01-11 11:52:37 +08:00
|
|
|
@interface ClassWithInvalidationMethodInCategory <NSObject>
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface ClassWithInvalidationMethodInCategory ()
|
|
|
|
- (void) invalidate __attribute__((annotate("objc_instance_variable_invalidator")));
|
|
|
|
@end
|
|
|
|
|
2012-09-27 02:55:16 +08:00
|
|
|
@interface SomeInvalidationImplementingObject: NSObject <Invalidation3, Invalidation2> {
|
2012-09-29 08:20:38 +08:00
|
|
|
SomeInvalidationImplementingObject *ObjA; // invalidation in the parent
|
2012-09-27 02:55:16 +08:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation SomeInvalidationImplementingObject
|
|
|
|
- (void)invalidate{
|
|
|
|
ObjA = 0;
|
|
|
|
}
|
2012-09-29 08:20:38 +08:00
|
|
|
- (void)invalidate2 {
|
|
|
|
[self invalidate];
|
|
|
|
}
|
2012-09-27 02:55:16 +08:00
|
|
|
@end
|
|
|
|
|
|
|
|
@interface SomeSubclassInvalidatableObject : SomeInvalidationImplementingObject {
|
2012-09-29 08:20:38 +08:00
|
|
|
SomeInvalidationImplementingObject *Ivar1; // regular ivar
|
|
|
|
SomeInvalidationImplementingObject *Ivar2; // regular ivar, sending invalidate message
|
|
|
|
SomeInvalidationImplementingObject *_Ivar3; // no property, call -description
|
|
|
|
SomeInvalidationImplementingObject *_Ivar4; // no property, provide as argument to NSLog()
|
|
|
|
|
|
|
|
SomeInvalidationImplementingObject *_Prop1; // partially implemented property, set to 0 with dot syntax
|
|
|
|
SomeInvalidationImplementingObject *_Prop2; // fully implemented prop, set to 0 with dot syntax
|
|
|
|
SomeInvalidationImplementingObject *_propIvar; // property with custom named ivar, set to 0 via setter
|
|
|
|
Invalidation1Class *MultipleProtocols; // regular ivar belonging to a different class
|
|
|
|
Invalidation2Class *MultInheritance; // regular ivar belonging to a different class
|
|
|
|
SomeInvalidationImplementingObject *_Prop3; // property, invalidate via sending a message to a getter method
|
|
|
|
SomeInvalidationImplementingObject *_Prop4; // property with @synthesize, invalidate via property
|
|
|
|
SomeInvalidationImplementingObject *_Prop5; // property with @synthesize, invalidate via getter method
|
2012-10-19 03:17:57 +08:00
|
|
|
SomeInvalidationImplementingObject *_Prop8;
|
2012-09-28 03:45:08 +08:00
|
|
|
|
2013-02-09 07:55:43 +08:00
|
|
|
// Ivars invalidated by the partial invalidator.
|
|
|
|
SomeInvalidationImplementingObject *Ivar9;
|
|
|
|
SomeInvalidationImplementingObject *_Prop10;
|
|
|
|
SomeInvalidationImplementingObject *Ivar11;
|
|
|
|
|
2012-09-29 08:20:38 +08:00
|
|
|
// No warnings on these as they are not invalidatable.
|
|
|
|
NSObject *NIvar1;
|
2012-09-27 02:55:16 +08:00
|
|
|
NSObject *NObj2;
|
|
|
|
NSObject *_NProp1;
|
|
|
|
NSObject *_NpropIvar;
|
|
|
|
}
|
|
|
|
|
|
|
|
@property (assign) SomeInvalidationImplementingObject* Prop0;
|
|
|
|
@property (nonatomic, assign) SomeInvalidationImplementingObject* Prop1;
|
|
|
|
@property (assign) SomeInvalidationImplementingObject* Prop2;
|
|
|
|
@property (assign) SomeInvalidationImplementingObject* Prop3;
|
2012-09-29 08:20:38 +08:00
|
|
|
@property (assign) SomeInvalidationImplementingObject *Prop5;
|
|
|
|
@property (assign) SomeInvalidationImplementingObject *Prop4;
|
2012-09-28 05:57:14 +08:00
|
|
|
|
2012-09-29 08:20:38 +08:00
|
|
|
@property (assign) SomeInvalidationImplementingObject* Prop6; // automatically synthesized prop
|
|
|
|
@property (assign) SomeInvalidationImplementingObject* Prop7; // automatically synthesized prop
|
|
|
|
@property (assign) SomeInvalidationImplementingObject *SynthIvarProp;
|
2012-09-28 05:57:14 +08:00
|
|
|
|
2012-09-27 02:55:16 +08:00
|
|
|
@property (assign) NSObject* NProp0;
|
|
|
|
@property (nonatomic, assign) NSObject* NProp1;
|
|
|
|
@property (assign) NSObject* NProp2;
|
|
|
|
|
|
|
|
-(void)setProp1: (SomeInvalidationImplementingObject*) InO;
|
|
|
|
-(void)setNProp1: (NSObject*) InO;
|
|
|
|
|
|
|
|
-(void)invalidate;
|
|
|
|
|
2013-02-09 07:55:43 +08:00
|
|
|
// Partial invalidators invalidate only some ivars. They are guaranteed to be
|
|
|
|
// called before the invalidation methods.
|
|
|
|
-(void)partialInvalidator1 __attribute__((annotate("objc_instance_variable_invalidator_partial")));
|
|
|
|
-(void)partialInvalidator2 __attribute__((annotate("objc_instance_variable_invalidator_partial")));
|
2012-09-27 02:55:16 +08:00
|
|
|
@end
|
|
|
|
|
2012-10-17 03:36:37 +08:00
|
|
|
@interface SomeSubclassInvalidatableObject()
|
|
|
|
@property (assign) SomeInvalidationImplementingObject* Prop8;
|
2013-02-09 07:55:43 +08:00
|
|
|
@property (assign) SomeInvalidationImplementingObject* Prop10;
|
2012-10-17 03:36:37 +08:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation SomeSubclassInvalidatableObject{
|
|
|
|
@private
|
|
|
|
SomeInvalidationImplementingObject *Ivar5;
|
2013-01-11 11:52:37 +08:00
|
|
|
ClassWithInvalidationMethodInCategory *Ivar13;
|
2012-10-17 03:36:37 +08:00
|
|
|
}
|
2012-09-27 02:55:16 +08:00
|
|
|
|
2012-09-29 08:20:38 +08:00
|
|
|
@synthesize Prop7 = _propIvar;
|
|
|
|
@synthesize Prop3 = _Prop3;
|
|
|
|
@synthesize Prop5 = _Prop5;
|
|
|
|
@synthesize Prop4 = _Prop4;
|
2012-10-19 03:17:57 +08:00
|
|
|
@synthesize Prop8 = _Prop8;
|
2013-02-09 07:55:43 +08:00
|
|
|
@synthesize Prop10 = _Prop10;
|
2012-09-29 08:20:38 +08:00
|
|
|
|
2012-09-27 02:55:16 +08:00
|
|
|
|
|
|
|
- (void) setProp1: (SomeInvalidationImplementingObject*) InObj {
|
|
|
|
_Prop1 = InObj;
|
|
|
|
}
|
|
|
|
|
2012-09-29 08:20:38 +08:00
|
|
|
- (void) setProp2: (SomeInvalidationImplementingObject*) InObj {
|
|
|
|
_Prop2 = InObj;
|
2012-09-27 02:55:16 +08:00
|
|
|
}
|
2012-09-29 08:20:38 +08:00
|
|
|
- (SomeInvalidationImplementingObject*) Prop2 {
|
|
|
|
return _Prop2;
|
2012-09-27 02:55:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@synthesize NProp2 = _NpropIvar;
|
|
|
|
|
|
|
|
- (void) setNProp1: (NSObject*) InObj {
|
|
|
|
_NProp1 = InObj;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) invalidate {
|
2012-09-29 08:20:38 +08:00
|
|
|
[Ivar2 invalidate];
|
2012-09-28 05:57:14 +08:00
|
|
|
self.Prop0 = 0;
|
2012-09-27 02:55:16 +08:00
|
|
|
self.Prop1 = 0;
|
|
|
|
[self setProp2:0];
|
|
|
|
[self setProp3:0];
|
2012-09-29 08:20:38 +08:00
|
|
|
[[self Prop5] invalidate2];
|
|
|
|
[self.Prop4 invalidate];
|
2012-10-19 03:17:57 +08:00
|
|
|
[self.Prop8 invalidate];
|
2012-09-29 08:20:38 +08:00
|
|
|
self.Prop6 = 0;
|
|
|
|
[[self Prop7] invalidate];
|
|
|
|
|
|
|
|
[_Ivar3 description];
|
|
|
|
NSLog(@"%@", _Ivar4);
|
2012-09-27 02:55:16 +08:00
|
|
|
[super invalidate];
|
2012-09-29 08:20:38 +08:00
|
|
|
}
|
2013-02-09 07:55:47 +08:00
|
|
|
#if RUN_IVAR_INVALIDATION
|
|
|
|
// expected-warning@-2 {{Instance variable Ivar1 needs to be invalidated}}
|
|
|
|
// expected-warning@-3 {{Instance variable MultipleProtocols needs to be invalidated}}
|
|
|
|
// expected-warning@-4 {{Instance variable MultInheritance needs to be invalidated}}
|
|
|
|
// expected-warning@-5 {{Property SynthIvarProp needs to be invalidated or set to nil}}
|
|
|
|
// expected-warning@-6 {{Instance variable _Ivar3 needs to be invalidated}}
|
|
|
|
// expected-warning@-7 {{Instance variable _Ivar4 needs to be invalidated}}
|
|
|
|
// expected-warning@-8 {{Instance variable Ivar5 needs to be invalidated or set to nil}}
|
|
|
|
// expected-warning@-9 {{Instance variable Ivar13 needs to be invalidated or set to nil}}
|
|
|
|
#endif
|
2013-02-09 07:55:43 +08:00
|
|
|
|
|
|
|
-(void)partialInvalidator1 {
|
|
|
|
[Ivar9 invalidate];
|
|
|
|
[_Prop10 invalidate];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)partialInvalidator2 {
|
|
|
|
[Ivar11 invalidate];
|
|
|
|
}
|
|
|
|
|
2012-09-27 02:55:16 +08:00
|
|
|
@end
|
2013-01-08 03:12:56 +08:00
|
|
|
|
|
|
|
// Example, where the same property is inherited through
|
|
|
|
// the parent and directly through a protocol. If a property backing ivar is
|
|
|
|
// synthesized in the parent, let the parent invalidate it.
|
|
|
|
|
|
|
|
@protocol IDEBuildable <NSObject>
|
|
|
|
@property (readonly, strong) id <Invalidation2> ObjB;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface Parent : NSObject <IDEBuildable, Invalidation2> {
|
|
|
|
Invalidation2Class *_ObjB; // Invalidation of ObjB happens in the parent.
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface Child: Parent <Invalidation2, IDEBuildable>
|
|
|
|
@end
|
|
|
|
|
2013-01-11 07:34:16 +08:00
|
|
|
@implementation Parent{
|
|
|
|
@private
|
|
|
|
Invalidation2Class *Ivar10;
|
|
|
|
Invalidation2Class *Ivar11;
|
|
|
|
Invalidation2Class *Ivar12;
|
|
|
|
}
|
|
|
|
|
2013-01-08 03:12:56 +08:00
|
|
|
@synthesize ObjB = _ObjB;
|
|
|
|
- (void)invalidate{
|
|
|
|
_ObjB = ((void*)0);
|
2013-01-11 07:34:16 +08:00
|
|
|
|
|
|
|
assert(Ivar10 == 0);
|
|
|
|
|
|
|
|
if (__builtin_expect(!(Ivar11 == ((void*)0)), 0))
|
|
|
|
assert(0);
|
|
|
|
|
|
|
|
assert(0 == Ivar12);
|
|
|
|
|
2013-01-08 03:12:56 +08:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation Child
|
|
|
|
- (void)invalidate{
|
|
|
|
// no-warning
|
|
|
|
}
|
|
|
|
@end
|
2013-01-11 04:59:51 +08:00
|
|
|
|
|
|
|
@protocol Invalidation <NSObject>
|
|
|
|
- (void)invalidate __attribute__((annotate("objc_instance_variable_invalidator")));
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface Foo : NSObject <Invalidation>
|
|
|
|
@end
|
|
|
|
|
|
|
|
@class FooBar;
|
|
|
|
@protocol FooBar_Protocol <NSObject>
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface MissingInvalidationMethod : Foo <FooBar_Protocol>
|
2013-02-09 07:55:47 +08:00
|
|
|
@property (assign) MissingInvalidationMethod *foobar15_warn;
|
2013-02-09 09:09:27 +08:00
|
|
|
#if RUN_IVAR_INVALIDATION
|
2013-02-09 07:55:47 +08:00
|
|
|
// expected-warning@-2 {{Property foobar15_warn needs to be invalidated; no invalidation method is defined in the @implementation for MissingInvalidationMethod}}
|
|
|
|
#endif
|
2013-01-11 04:59:51 +08:00
|
|
|
@end
|
|
|
|
@implementation MissingInvalidationMethod
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface MissingInvalidationMethod2 : Foo <FooBar_Protocol> {
|
2013-02-09 07:55:47 +08:00
|
|
|
Foo *Ivar1;
|
2013-02-09 09:09:27 +08:00
|
|
|
#if RUN_IVAR_INVALIDATION
|
2013-02-09 07:55:47 +08:00
|
|
|
// expected-warning@-2 {{Instance variable Ivar1 needs to be invalidated; no invalidation method is defined in the @implementation for MissingInvalidationMethod2}}
|
|
|
|
#endif
|
2013-01-11 04:59:51 +08:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
@implementation MissingInvalidationMethod2
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface MissingInvalidationMethodDecl : NSObject {
|
2013-02-09 07:55:47 +08:00
|
|
|
Foo *Ivar1;
|
|
|
|
#if RUN_MISSING_INVALIDATION_METHOD
|
|
|
|
// expected-warning@-2 {{Instance variable Ivar1 needs to be invalidated; no invalidation method is declared for MissingInvalidationMethodDecl}}
|
|
|
|
#endif
|
2013-01-11 04:59:51 +08:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
@implementation MissingInvalidationMethodDecl
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface MissingInvalidationMethodDecl2 : NSObject {
|
|
|
|
@private
|
2013-02-09 07:55:47 +08:00
|
|
|
Foo *_foo1;
|
|
|
|
#if RUN_MISSING_INVALIDATION_METHOD
|
|
|
|
// expected-warning@-2 {{Instance variable _foo1 needs to be invalidated; no invalidation method is declared for MissingInvalidationMethodDecl2}}
|
|
|
|
#endif
|
2013-01-11 04:59:51 +08:00
|
|
|
}
|
2013-01-11 06:44:16 +08:00
|
|
|
@property (strong) Foo *bar1;
|
2013-01-11 04:59:51 +08:00
|
|
|
@end
|
|
|
|
@implementation MissingInvalidationMethodDecl2
|
|
|
|
@end
|
2013-02-09 07:55:43 +08:00
|
|
|
|
|
|
|
@interface InvalidatedInPartial : SomeInvalidationImplementingObject {
|
|
|
|
SomeInvalidationImplementingObject *Ivar1;
|
|
|
|
SomeInvalidationImplementingObject *Ivar2;
|
|
|
|
}
|
|
|
|
-(void)partialInvalidator __attribute__((annotate("objc_instance_variable_invalidator_partial")));
|
|
|
|
@end
|
|
|
|
@implementation InvalidatedInPartial
|
|
|
|
-(void)partialInvalidator {
|
|
|
|
[Ivar1 invalidate];
|
|
|
|
Ivar2 = 0;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface NotInvalidatedInPartial : SomeInvalidationImplementingObject {
|
|
|
|
SomeInvalidationImplementingObject *Ivar1;
|
|
|
|
}
|
|
|
|
-(void)partialInvalidator __attribute__((annotate("objc_instance_variable_invalidator_partial")));
|
|
|
|
-(void)partialInvalidatorCallsPartial __attribute__((annotate("objc_instance_variable_invalidator_partial")));
|
|
|
|
@end
|
|
|
|
@implementation NotInvalidatedInPartial
|
|
|
|
-(void)partialInvalidator {
|
|
|
|
}
|
|
|
|
-(void)partialInvalidatorCallsPartial {
|
|
|
|
[self partialInvalidator];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)invalidate {
|
2013-02-09 07:55:47 +08:00
|
|
|
}
|
|
|
|
#if RUN_IVAR_INVALIDATION
|
|
|
|
// expected-warning@-2 {{Instance variable Ivar1 needs to be invalidated or set to nil}}
|
|
|
|
#endif
|
2013-02-09 07:55:43 +08:00
|
|
|
@end
|
|
|
|
|
2013-04-24 10:49:16 +08:00
|
|
|
@interface SomeNotInvalidatedInPartial : SomeInvalidationImplementingObject {
|
|
|
|
SomeInvalidationImplementingObject *Ivar1;
|
|
|
|
SomeInvalidationImplementingObject *Ivar2;
|
|
|
|
#if RUN_IVAR_INVALIDATION
|
|
|
|
// expected-warning@-2 {{Instance variable Ivar2 needs to be invalidated or set to nil}}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
-(void)partialInvalidator __attribute__((annotate("objc_instance_variable_invalidator_partial")));
|
|
|
|
-(void)partialInvalidatorCallsPartial __attribute__((annotate("objc_instance_variable_invalidator_partial")));
|
|
|
|
@end
|
|
|
|
@implementation SomeNotInvalidatedInPartial {
|
|
|
|
SomeInvalidationImplementingObject *Ivar3;
|
|
|
|
#if RUN_IVAR_INVALIDATION
|
|
|
|
// expected-warning@-2 {{Instance variable Ivar3 needs to be invalidated or set to nil}}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
-(void)partialInvalidator {
|
|
|
|
Ivar1 = 0;
|
|
|
|
}
|
|
|
|
-(void)partialInvalidatorCallsPartial {
|
|
|
|
[self partialInvalidator];
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface OnlyPartialDeclsBase : NSObject
|
|
|
|
-(void)partialInvalidator __attribute__((annotate("objc_instance_variable_invalidator_partial")));
|
|
|
|
@end
|
|
|
|
@implementation OnlyPartialDeclsBase
|
|
|
|
-(void)partialInvalidator {}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface OnlyPartialDecls : OnlyPartialDeclsBase {
|
|
|
|
SomeInvalidationImplementingObject *Ivar1;
|
|
|
|
#if RUN_IVAR_INVALIDATION
|
|
|
|
// expected-warning@-2 {{Instance variable Ivar1 needs to be invalidated; no invalidation method is defined in the @implementation for OnlyPartialDecls}}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
@implementation OnlyPartialDecls
|
|
|
|
@end
|
|
|
|
|
2013-02-09 07:55:43 +08:00
|
|
|
// False negative.
|
|
|
|
@interface PartialCallsFull : SomeInvalidationImplementingObject {
|
|
|
|
SomeInvalidationImplementingObject *Ivar1;
|
|
|
|
}
|
|
|
|
-(void)partialInvalidator __attribute__((annotate("objc_instance_variable_invalidator_partial")));
|
|
|
|
@end
|
|
|
|
@implementation PartialCallsFull
|
|
|
|
-(void)partialInvalidator {
|
|
|
|
[self invalidate];
|
|
|
|
} // TODO: It would be nice to check that the full invalidation method actually invalidates the ivar.
|
|
|
|
@end
|
|
|
|
|