blob: 0136389efe6173d928747c75125ba1e848acf45d [file] [log] [blame]
George Karpenkovb293c6bb2018-07-27 18:26:40 +00001// RUN: %clang_analyze_cc1 -x objective-c -analyzer-checker=core,nullability -analyzer-output=text -Wno-objc-root-class -fblocks -verify %s
George Karpenkove15451a2018-02-23 23:26:56 +00002
George Karpenkovb293c6bb2018-07-27 18:26:40 +00003#include "../Inputs/system-header-simulator-for-nullability.h"
4
5extern int coin();
6
7@interface I : NSObject
George Karpenkove15451a2018-02-23 23:26:56 +00008- (int)initVar:(int *)var param:(int)param;
9@end
10
11@implementation I
12- (int)initVar:(int *)var param:(int)param {
Csaba Dabis4b0184b2019-05-29 20:06:09 +000013 if (param) { // expected-note{{'param' is 0}}
14 // expected-note@-1{{Taking false branch}}
George Karpenkove15451a2018-02-23 23:26:56 +000015 *var = 1;
16 return 0;
17 }
18 return 1; // expected-note{{Returning without writing to '*var'}}
19}
20@end
21
22int foo(I *i) {
23 int x; //expected-note{{'x' declared without an initial value}}
24 int out = [i initVar:&x param:0]; //expected-note{{Calling 'initVar:param:'}}
25 //expected-note@-1{{Returning from 'initVar:param:'}}
Csaba Dabis4b0184b2019-05-29 20:06:09 +000026 if (out) //expected-note{{'out' is 1}}
27 //expected-note@-1{{Taking true branch}}
George Karpenkove15451a2018-02-23 23:26:56 +000028 return x; //expected-warning{{Undefined or garbage value returned to caller}}
29 //expected-note@-1{{Undefined or garbage value returned to caller}}
30 return 0;
31}
32
33int initializer1(int *p, int x) {
Csaba Dabis4b0184b2019-05-29 20:06:09 +000034 if (x) { // expected-note{{'x' is 0}}
35 // expected-note@-1{{Taking false branch}}
George Karpenkove15451a2018-02-23 23:26:56 +000036 *p = 1;
37 return 0;
38 } else {
39 return 1; // expected-note {{Returning without writing to '*p'}}
40 }
41}
42
43int initFromBlock() {
44 __block int z;
45 ^{ // expected-note {{Calling anonymous block}}
46 int p; // expected-note{{'p' declared without an initial value}}
47 initializer1(&p, 0); // expected-note{{Calling 'initializer1'}}
48 // expected-note@-1{{Returning from 'initializer1'}}
49 z = p; // expected-warning{{Assigned value is garbage or undefined}}
50 // expected-note@-1{{Assigned value is garbage or undefined}}
51 }();
52 return z;
53}
George Karpenkovb293c6bb2018-07-27 18:26:40 +000054
55extern void expectNonNull(NSString * _Nonnull a);
56
57@interface A : NSObject
George Karpenkovb293c6bb2018-07-27 18:26:40 +000058- (void) initAMaybe;
59@end
60
61@implementation A {
62 NSString * a;
63}
64
65- (void) initAMaybe {
66 if (coin()) // expected-note{{Assuming the condition is false}}
67 // expected-note@-1{{Taking false branch}}
68 a = @"string";
69} // expected-note{{Returning without writing to 'self->a'}}
70
George Karpenkov1d08c512018-08-02 02:02:40 +000071- (void) passNullToNonnull {
George Karpenkovb293c6bb2018-07-27 18:26:40 +000072 a = nil; // expected-note{{nil object reference stored to 'a'}}
73 [self initAMaybe]; // expected-note{{Calling 'initAMaybe'}}
74 // expected-note@-1{{Returning from 'initAMaybe'}}
75 expectNonNull(a); // expected-warning{{nil passed to a callee that requires a non-null 1st parameter}}
76 // expected-note@-1{{nil passed to a callee that requires a non-null 1st parameter}}
77}
78
George Karpenkov1d08c512018-08-02 02:02:40 +000079- (void) initAMaybeWithExplicitSelf {
80 if (coin()) // expected-note{{Assuming the condition is false}}
81 // expected-note@-1{{Taking false branch}}
82 self->a = @"string";
83} // expected-note{{Returning without writing to 'self->a'}}
84
85- (void) passNullToNonnullWithExplicitSelf {
86 self->a = nil; // expected-note{{nil object reference stored to 'a'}}
87 [self initAMaybeWithExplicitSelf]; // expected-note{{Calling 'initAMaybeWithExplicitSelf'}}
88 // expected-note@-1{{Returning from 'initAMaybeWithExplicitSelf'}}
89 expectNonNull(a); // expected-warning{{nil passed to a callee that requires a non-null 1st parameter}}
90 // expected-note@-1{{nil passed to a callee that requires a non-null 1st parameter}}
91}
92
93- (void) initPassedAMaybe:(A *) param {
94 if (coin()) // expected-note{{Assuming the condition is false}}
95 // expected-note@-1{{Taking false branch}}
96 param->a = @"string";
97} // expected-note{{Returning without writing to 'param->a'}}
98
99- (void) useInitPassedAMaybe:(A *) paramA {
100 paramA->a = nil; // expected-note{{nil object reference stored to 'a'}}
101 [self initPassedAMaybe:paramA]; // expected-note{{Calling 'initPassedAMaybe:'}}
102 // expected-note@-1{{Returning from 'initPassedAMaybe:'}}
103 expectNonNull(paramA->a); // expected-warning{{nil passed to a callee that requires a non-null 1st parameter}}
104 // expected-note@-1{{nil passed to a callee that requires a non-null 1st parameter}}
105
106}
107
George Karpenkovb293c6bb2018-07-27 18:26:40 +0000108@end