xref: /minix3/external/bsd/llvm/dist/clang/test/Analysis/objc-subscript.m (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc// RUN: %clang_cc1 -analyze -analyzer-checker=core,osx.cocoa.RetainCount -analyzer-store=region -verify -Wno-objc-root-class %s
2*f4a2713aSLionel Sambuc
3*f4a2713aSLionel Sambuctypedef signed char BOOL;
4*f4a2713aSLionel Sambuctypedef unsigned int NSUInteger;
5*f4a2713aSLionel Sambuc
6*f4a2713aSLionel Sambuc@interface NSObject
7*f4a2713aSLionel Sambuc+(id)alloc;
8*f4a2713aSLionel Sambuc-(id)init;
9*f4a2713aSLionel Sambuc-(id)autorelease;
10*f4a2713aSLionel Sambuc-(id)copy;
11*f4a2713aSLionel Sambuc-(id)retain;
12*f4a2713aSLionel Sambuc@end
13*f4a2713aSLionel Sambuc
14*f4a2713aSLionel Sambuc@interface Subscriptable : NSObject
15*f4a2713aSLionel Sambuc- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)index;
16*f4a2713aSLionel Sambuc- (id)objectAtIndexedSubscript:(NSUInteger)index;
17*f4a2713aSLionel Sambuc
18*f4a2713aSLionel Sambuc- (void)setObject:(id)obj forKeyedSubscript:(id)key;
19*f4a2713aSLionel Sambuc- (id)objectForKeyedSubscript:(id)key;
20*f4a2713aSLionel Sambuc@end
21*f4a2713aSLionel Sambuc
22*f4a2713aSLionel Sambuc@interface Test : Subscriptable
23*f4a2713aSLionel Sambuc@end
24*f4a2713aSLionel Sambuc
25*f4a2713aSLionel Sambuc@implementation Test
26*f4a2713aSLionel Sambuc
27*f4a2713aSLionel Sambuc// <rdar://problem/6946338> for subscripting
28*f4a2713aSLionel Sambuc- (id)storeDoesNotRetain {
29*f4a2713aSLionel Sambuc  Test *cell = [[[Test alloc] init] autorelease];
30*f4a2713aSLionel Sambuc
31*f4a2713aSLionel Sambuc  NSObject *string1 = [[NSObject alloc] init]; // expected-warning {{Potential leak}}
32*f4a2713aSLionel Sambuc  cell[0] = string1;
33*f4a2713aSLionel Sambuc  cell[self] = string1;
34*f4a2713aSLionel Sambuc  cell[string1] = self;
35*f4a2713aSLionel Sambuc
36*f4a2713aSLionel Sambuc  return cell;
37*f4a2713aSLionel Sambuc}
38*f4a2713aSLionel Sambuc
39*f4a2713aSLionel Sambuc// <rdar://problem/8824416> for subscripting
40*f4a2713aSLionel Sambuc- (id)getDoesNotRetain:(BOOL)keyed {
41*f4a2713aSLionel Sambuc  if (keyed)
42*f4a2713aSLionel Sambuc    return [self[self] autorelease]; // expected-warning{{Object autoreleased too many times}}
43*f4a2713aSLionel Sambuc  else
44*f4a2713aSLionel Sambuc    return [self[0] autorelease]; // expected-warning{{Object autoreleased too many times}}
45*f4a2713aSLionel Sambuc}
46*f4a2713aSLionel Sambuc
47*f4a2713aSLionel Sambuc// <rdar://problem/9241180> for subscripting
48*f4a2713aSLionel Sambuc- (id)testUninitializedObject:(BOOL)keyed {
49*f4a2713aSLionel Sambuc  Test *o;
50*f4a2713aSLionel Sambuc  if (keyed) {
51*f4a2713aSLionel Sambuc    if (o[self]) // expected-warning {{Subscript access on an uninitialized object pointer}}
52*f4a2713aSLionel Sambuc      return o; // no-warning (sink)
53*f4a2713aSLionel Sambuc  } else {
54*f4a2713aSLionel Sambuc    if (o[0]) // expected-warning {{Subscript access on an uninitialized object pointer}}
55*f4a2713aSLionel Sambuc      return o; // no-warning (sink)
56*f4a2713aSLionel Sambuc  }
57*f4a2713aSLionel Sambuc  return self;
58*f4a2713aSLionel Sambuc}
59*f4a2713aSLionel Sambuc
60*f4a2713aSLionel Sambuc- (void)testUninitializedArgument:(id)input testCase:(unsigned)testCase {
61*f4a2713aSLionel Sambuc  NSUInteger i;
62*f4a2713aSLionel Sambuc  id o;
63*f4a2713aSLionel Sambuc
64*f4a2713aSLionel Sambuc  switch (testCase) {
65*f4a2713aSLionel Sambuc  case 0:
66*f4a2713aSLionel Sambuc    self[0] = o; // expected-warning {{Argument for subscript setter is an uninitialized value}}
67*f4a2713aSLionel Sambuc    break;
68*f4a2713aSLionel Sambuc  case 1:
69*f4a2713aSLionel Sambuc    self[i] = input; // expected-warning {{Subscript index is an uninitialized value}}
70*f4a2713aSLionel Sambuc    break;
71*f4a2713aSLionel Sambuc  case 2:
72*f4a2713aSLionel Sambuc    (void)self[i]; // expected-warning {{Subscript index is an uninitialized value}}
73*f4a2713aSLionel Sambuc    break;
74*f4a2713aSLionel Sambuc  case 3:
75*f4a2713aSLionel Sambuc    self[input] = o; // expected-warning {{Argument for subscript setter is an uninitialized value}}
76*f4a2713aSLionel Sambuc    break;
77*f4a2713aSLionel Sambuc  case 4:
78*f4a2713aSLionel Sambuc    self[o] = input; // expected-warning {{Subscript index is an uninitialized value}}
79*f4a2713aSLionel Sambuc    break;
80*f4a2713aSLionel Sambuc  case 5:
81*f4a2713aSLionel Sambuc    (void)self[o]; // expected-warning {{Subscript index is an uninitialized value}}
82*f4a2713aSLionel Sambuc    break;
83*f4a2713aSLionel Sambuc  default:
84*f4a2713aSLionel Sambuc    break;
85*f4a2713aSLionel Sambuc  }
86*f4a2713aSLionel Sambuc
87*f4a2713aSLionel Sambuc}
88*f4a2713aSLionel Sambuc
89*f4a2713aSLionel Sambuc@end
90