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