xref: /minix3/external/bsd/llvm/dist/clang/test/Analysis/inlining/retain-count-self-init.m (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc// RUN: %clang_cc1 -analyze -analyzer-checker=core,osx.cocoa.RetainCount,osx.cocoa.SelfInit -analyzer-config ipa=dynamic-bifurcate -verify %s
2*f4a2713aSLionel Sambuc
3*f4a2713aSLionel Sambuctypedef signed char BOOL;
4*f4a2713aSLionel Sambuctypedef struct objc_class *Class;
5*f4a2713aSLionel Sambuctypedef struct objc_object {
6*f4a2713aSLionel Sambuc    Class isa;
7*f4a2713aSLionel Sambuc} *id;
8*f4a2713aSLionel Sambuc@protocol NSObject  - (BOOL)isEqual:(id)object; @end
9*f4a2713aSLionel Sambuc@interface NSObject <NSObject> {}
10*f4a2713aSLionel Sambuc+(id)alloc;
11*f4a2713aSLionel Sambuc+(id)new;
12*f4a2713aSLionel Sambuc- (oneway void)release;
13*f4a2713aSLionel Sambuc-(id)init;
14*f4a2713aSLionel Sambuc-(id)autorelease;
15*f4a2713aSLionel Sambuc-(id)copy;
16*f4a2713aSLionel Sambuc- (Class)class;
17*f4a2713aSLionel Sambuc-(id)retain;
18*f4a2713aSLionel Sambuc@end
19*f4a2713aSLionel Sambuc
20*f4a2713aSLionel Sambuc// We do not want to overhelm user with error messages in case they forgot to
21*f4a2713aSLionel Sambuc// assign to self and check that the result of [super init] is non-nil. So
22*f4a2713aSLionel Sambuc// stop tracking the receiver of init with respect to Retain Release checker.
23*f4a2713aSLionel Sambuc// radar://12115830
24*f4a2713aSLionel Sambuc@interface ParentOfCell : NSObject
25*f4a2713aSLionel Sambuc- (id)initWithInt: (int)inInt;
26*f4a2713aSLionel Sambuc@end
27*f4a2713aSLionel Sambuc@interface Cell : ParentOfCell{
28*f4a2713aSLionel Sambuc  int x;
29*f4a2713aSLionel Sambuc}
30*f4a2713aSLionel Sambuc- (id)init;
31*f4a2713aSLionel Sambuc+ (void)test;
32*f4a2713aSLionel Sambuc@property int x;
33*f4a2713aSLionel Sambuc@end
34*f4a2713aSLionel Sambuc@implementation Cell
35*f4a2713aSLionel Sambuc@synthesize x;
36*f4a2713aSLionel Sambuc- (id) init {
37*f4a2713aSLionel Sambuc  [super init];
38*f4a2713aSLionel Sambuc  self.x = 3; // no-warning
39*f4a2713aSLionel Sambuc  return self; // expected-warning {{Returning 'self' while it is not set to the result of '[(super or self)}}
40*f4a2713aSLionel Sambuc}
41*f4a2713aSLionel Sambuc- (id) initWithInt: (int)inInt {
42*f4a2713aSLionel Sambuc  [super initWithInt: inInt];
43*f4a2713aSLionel Sambuc  self.x = inInt; // no-warning
44*f4a2713aSLionel Sambuc  return self; // expected-warning {{Returning 'self' while it is not set to the result of '[(super or self)}}
45*f4a2713aSLionel Sambuc}
46*f4a2713aSLionel Sambuc- (id) init2 {
47*f4a2713aSLionel Sambuc  [self init]; // The call [self init] is inlined. We will warn inside the inlined body.
48*f4a2713aSLionel Sambuc  self.x = 2; // no-warning
49*f4a2713aSLionel Sambuc  return self;
50*f4a2713aSLionel Sambuc}
51*f4a2713aSLionel Sambuc
52*f4a2713aSLionel Sambuc- (id) initWithIntGood: (int)inInt {
53*f4a2713aSLionel Sambuc    if (self = [super initWithInt: inInt]) {
54*f4a2713aSLionel Sambuc      self.x = inInt;
55*f4a2713aSLionel Sambuc    }
56*f4a2713aSLionel Sambuc    return self;
57*f4a2713aSLionel Sambuc}
58*f4a2713aSLionel Sambuc+ (void) test {
59*f4a2713aSLionel Sambuc  Cell *sharedCell1 = [[Cell alloc] init];
60*f4a2713aSLionel Sambuc  [sharedCell1 release];
61*f4a2713aSLionel Sambuc  Cell *sharedCell2 = [[Cell alloc] initWithInt: 3];
62*f4a2713aSLionel Sambuc  [sharedCell2 release];
63*f4a2713aSLionel Sambuc  Cell *sharedCell3 = [[Cell alloc] initWithIntGood: 3];
64*f4a2713aSLionel Sambuc  [sharedCell3 release];
65*f4a2713aSLionel Sambuc}
66*f4a2713aSLionel Sambuc
67*f4a2713aSLionel Sambuc@end
68*f4a2713aSLionel Sambuc
69