xref: /minix3/external/bsd/llvm/dist/clang/test/Analysis/unused-ivars.m (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc// RUN: %clang_cc1 -fblocks -analyze -analyzer-checker=osx.cocoa.UnusedIvars -verify -Wno-objc-root-class %s
2*f4a2713aSLionel Sambuc
3*f4a2713aSLionel Sambuc//===--- BEGIN: Delta-debugging reduced headers. --------------------------===//
4*f4a2713aSLionel Sambuc
5*f4a2713aSLionel Sambuc@protocol NSObject
6*f4a2713aSLionel Sambuc- (id)retain;
7*f4a2713aSLionel Sambuc- (oneway void)release;
8*f4a2713aSLionel Sambuc@end
9*f4a2713aSLionel Sambuc@interface NSObject <NSObject> {}
10*f4a2713aSLionel Sambuc- (id)init;
11*f4a2713aSLionel Sambuc+ (id)alloc;
12*f4a2713aSLionel Sambuc@end
13*f4a2713aSLionel Sambuc
14*f4a2713aSLionel Sambuc//===--- END: Delta-debugging reduced headers. ----------------------------===//
15*f4a2713aSLionel Sambuc
16*f4a2713aSLionel Sambuc// This test case tests the basic functionality of the unused ivar test.
17*f4a2713aSLionel Sambuc@interface TestA {
18*f4a2713aSLionel Sambuc@private
19*f4a2713aSLionel Sambuc  int x; // expected-warning {{Instance variable 'x' in class 'TestA' is never used}}
20*f4a2713aSLionel Sambuc}
21*f4a2713aSLionel Sambuc@end
22*f4a2713aSLionel Sambuc@implementation TestA @end
23*f4a2713aSLionel Sambuc
24*f4a2713aSLionel Sambuc// This test case tests whether the unused ivar check handles blocks that
25*f4a2713aSLionel Sambuc// reference an instance variable. (<rdar://problem/7075531>)
26*f4a2713aSLionel Sambuc@interface TestB : NSObject {
27*f4a2713aSLionel Sambuc@private
28*f4a2713aSLionel Sambuc  id _ivar; // no-warning
29*f4a2713aSLionel Sambuc}
30*f4a2713aSLionel Sambuc@property (readwrite,retain) id ivar;
31*f4a2713aSLionel Sambuc@end
32*f4a2713aSLionel Sambuc
33*f4a2713aSLionel Sambuc@implementation TestB
34*f4a2713aSLionel Sambuc- (id)ivar {
35*f4a2713aSLionel Sambuc  __attribute__((__blocks__(byref))) id value = ((void*)0);
36*f4a2713aSLionel Sambuc  void (^b)() = ^{ value = _ivar; };
37*f4a2713aSLionel Sambuc  b();
38*f4a2713aSLionel Sambuc  return value;
39*f4a2713aSLionel Sambuc}
40*f4a2713aSLionel Sambuc
41*f4a2713aSLionel Sambuc- (void)setIvar:(id)newValue {
42*f4a2713aSLionel Sambuc  void (^b)() = ^{ [_ivar release]; _ivar = [newValue retain]; };
43*f4a2713aSLionel Sambuc  b();
44*f4a2713aSLionel Sambuc}
45*f4a2713aSLionel Sambuc@end
46*f4a2713aSLionel Sambuc
47*f4a2713aSLionel Sambuc//===----------------------------------------------------------------------===//
48*f4a2713aSLionel Sambuc// <rdar://problem/6260004> Detect that ivar is in use, if used in category
49*f4a2713aSLionel Sambuc//  in the same file as the implementation
50*f4a2713aSLionel Sambuc//===----------------------------------------------------------------------===//
51*f4a2713aSLionel Sambuc
52*f4a2713aSLionel Sambuc@protocol Protocol6260004
53*f4a2713aSLionel Sambuc- (id) getId;
54*f4a2713aSLionel Sambuc@end
55*f4a2713aSLionel Sambuc
56*f4a2713aSLionel Sambuc@interface RDar6260004 {
57*f4a2713aSLionel Sambuc@private
58*f4a2713aSLionel Sambuc  id x; // no-warning
59*f4a2713aSLionel Sambuc}
60*f4a2713aSLionel Sambuc@end
61*f4a2713aSLionel Sambuc@implementation RDar6260004 @end
62*f4a2713aSLionel Sambuc@implementation RDar6260004 (Protocol6260004)
63*f4a2713aSLionel Sambuc- (id) getId {
64*f4a2713aSLionel Sambuc  return x;
65*f4a2713aSLionel Sambuc}
66*f4a2713aSLionel Sambuc@end
67*f4a2713aSLionel Sambuc
68*f4a2713aSLionel Sambuc//===----------------------------------------------------------------------===//
69*f4a2713aSLionel Sambuc// <rdar://problem/7254495> - ivars referenced by lexically nested functions
70*f4a2713aSLionel Sambuc//  should not be flagged as unused
71*f4a2713aSLionel Sambuc//===----------------------------------------------------------------------===//
72*f4a2713aSLionel Sambuc
73*f4a2713aSLionel Sambuc@interface RDar7254495 {
74*f4a2713aSLionel Sambuc@private
75*f4a2713aSLionel Sambuc  int x; // no-warning
76*f4a2713aSLionel Sambuc}
77*f4a2713aSLionel Sambuc@end
78*f4a2713aSLionel Sambuc
79*f4a2713aSLionel Sambuc@implementation RDar7254495
80*f4a2713aSLionel Sambucint radar_7254495(RDar7254495 *a) {
81*f4a2713aSLionel Sambuc  return a->x;
82*f4a2713aSLionel Sambuc}
83*f4a2713aSLionel Sambuc@end
84*f4a2713aSLionel Sambuc
85*f4a2713aSLionel Sambuc//===----------------------------------------------------------------------===//
86*f4a2713aSLionel Sambuc// <rdar://problem/7353683> - consult attribute((unused)) to silence warnings
87*f4a2713aSLionel Sambuc// about unused instance variables
88*f4a2713aSLionel Sambuc//===----------------------------------------------------------------------===//
89*f4a2713aSLionel Sambuc
90*f4a2713aSLionel Sambuc@interface RDar7353683 {
91*f4a2713aSLionel Sambuc@private
92*f4a2713aSLionel Sambuc  id x __attribute__((unused));
93*f4a2713aSLionel Sambuc}
94*f4a2713aSLionel Sambuc@end
95*f4a2713aSLionel Sambuc
96*f4a2713aSLionel Sambuc@implementation RDar7353683
97*f4a2713aSLionel Sambuc@end
98*f4a2713aSLionel Sambuc//===----------------------------------------------------------------------===//
99*f4a2713aSLionel Sambuc// <rdar://problem/8481311> Unused bitfield ivars trigger cause weird
100*f4a2713aSLionel Sambuc// diagnostic: "Instance variable '' in class..."
101*f4a2713aSLionel Sambuc//===----------------------------------------------------------------------===//
102*f4a2713aSLionel Sambuc
103*f4a2713aSLionel Sambuc@interface RDar8481311 {
104*f4a2713aSLionel Sambuc@private
105*f4a2713aSLionel Sambuc    unsigned bitfield:1; // expected-warning {{Instance variable 'bitfield' in class 'RDar8481311' is never used}}
106*f4a2713aSLionel Sambuc}
107*f4a2713aSLionel Sambuc@end
108*f4a2713aSLionel Sambuc
109*f4a2713aSLionel Sambuc@implementation RDar8481311
110*f4a2713aSLionel Sambuc@end
111*f4a2713aSLionel Sambuc
112*f4a2713aSLionel Sambuc@class NSString;
113*f4a2713aSLionel Sambuc@interface Radar11059352_1 {
114*f4a2713aSLionel Sambuc@private
115*f4a2713aSLionel Sambuc    NSString *_pathString;
116*f4a2713aSLionel Sambuc}
117*f4a2713aSLionel Sambuc@property (readonly, strong) NSString *pathString;
118*f4a2713aSLionel Sambuc@end
119*f4a2713aSLionel Sambuc
120*f4a2713aSLionel Sambuc@interface Radar11059352 {
121*f4a2713aSLionel Sambuc@private
122*f4a2713aSLionel SambucRadar11059352_1 *_workspacePath;
123*f4a2713aSLionel Sambuc}
124*f4a2713aSLionel Sambuc@end
125*f4a2713aSLionel Sambuc
126*f4a2713aSLionel Sambuc@implementation Radar11059352
127*f4a2713aSLionel Sambuc
128*f4a2713aSLionel Sambuc- (void)useWorkspace {
129*f4a2713aSLionel Sambuc    NSString *workspacePathString = _workspacePath.pathString;
130*f4a2713aSLionel Sambuc}
131*f4a2713aSLionel Sambuc@end