1 // RUN: %clang_analyze_cc1 -x objective-c -analyzer-checker=core,nullability -analyzer-output=text -Wno-objc-root-class -fblocks -verify %s
3 #include "../Inputs/system-header-simulator-for-nullability.h"
7 @interface I : NSObject
8 - (int)initVar:(int *)var param:(int)param;
12 - (int)initVar:(int *)var param:(int)param {
13 if (param) { // expected-note{{'param' is 0}}
14 // expected-note@-1{{Taking false branch}}
18 return 1; // expected-note{{Returning without writing to '*var'}}
19 // expected-note@-1{{Returning the value 1, which participates in a condition later}}
24 int x; //expected-note{{'x' declared without an initial value}}
25 int out = [i initVar:&x param:0]; //expected-note{{Calling 'initVar:param:'}}
26 //expected-note@-1{{Returning from 'initVar:param:'}}
27 if (out) //expected-note{{'out' is 1}}
28 //expected-note@-1{{Taking true branch}}
29 return x; //expected-warning{{Undefined or garbage value returned to caller}}
30 //expected-note@-1{{Undefined or garbage value returned to caller}}
34 int initializer1(int *p, int x) {
35 if (x) { // expected-note{{'x' is 0}}
36 // expected-note@-1{{Taking false branch}}
40 return 1; // expected-note {{Returning without writing to '*p'}}
44 int initFromBlock(void) {
46 ^{ // expected-note {{Calling anonymous block}}
47 int p; // expected-note{{'p' declared without an initial value}}
48 initializer1(&p, 0); // expected-note{{Calling 'initializer1'}}
49 // expected-note@-1{{Returning from 'initializer1'}}
50 z = p; // expected-warning{{Assigned value is garbage or undefined}}
51 // expected-note@-1{{Assigned value is garbage or undefined}}
56 extern void expectNonNull(NSString * _Nonnull a);
58 @interface A : NSObject
67 if (coin()) // expected-note{{Assuming the condition is false}}
68 // expected-note@-1{{Taking false branch}}
70 } // expected-note{{Returning without writing to 'self->a'}}
72 - (void) passNullToNonnull {
73 a = nil; // expected-note{{nil object reference stored to 'a'}}
74 [self initAMaybe]; // expected-note{{Calling 'initAMaybe'}}
75 // expected-note@-1{{Returning from 'initAMaybe'}}
76 expectNonNull(a); // expected-warning{{nil passed to a callee that requires a non-null 1st parameter}}
77 // expected-note@-1{{nil passed to a callee that requires a non-null 1st parameter}}
80 - (void) initAMaybeWithExplicitSelf {
81 if (coin()) // expected-note{{Assuming the condition is false}}
82 // expected-note@-1{{Taking false branch}}
84 } // expected-note{{Returning without writing to 'self->a'}}
86 - (void) passNullToNonnullWithExplicitSelf {
87 self->a = nil; // expected-note{{nil object reference stored to 'a'}}
88 [self initAMaybeWithExplicitSelf]; // expected-note{{Calling 'initAMaybeWithExplicitSelf'}}
89 // expected-note@-1{{Returning from 'initAMaybeWithExplicitSelf'}}
90 expectNonNull(a); // expected-warning{{nil passed to a callee that requires a non-null 1st parameter}}
91 // expected-note@-1{{nil passed to a callee that requires a non-null 1st parameter}}
94 - (void) initPassedAMaybe:(A *) param {
95 if (coin()) // expected-note{{Assuming the condition is false}}
96 // expected-note@-1{{Taking false branch}}
98 } // expected-note{{Returning without writing to 'param->a'}}
100 - (void) useInitPassedAMaybe:(A *) paramA {
101 paramA->a = nil; // expected-note{{nil object reference stored to 'a'}}
102 [self initPassedAMaybe:paramA]; // expected-note{{Calling 'initPassedAMaybe:'}}
103 // expected-note@-1{{Returning from 'initPassedAMaybe:'}}
104 expectNonNull(paramA->a); // expected-warning{{nil passed to a callee that requires a non-null 1st parameter}}
105 // expected-note@-1{{nil passed to a callee that requires a non-null 1st parameter}}