1// RUN: %clang_analyze_cc1 -x objective-c -analyzer-checker=core,nullability -analyzer-output=text -Wno-objc-root-class -fblocks -verify %s
2
3#include "../Inputs/system-header-simulator-for-nullability.h"
4
5extern int coin();
6
7@interface I : NSObject
8- (int)initVar:(int *)var param:(int)param;
9@end
10
11@implementation I
12- (int)initVar:(int *)var param:(int)param {
13  if (param) { // expected-note{{'param' is 0}}
14               // expected-note@-1{{Taking false branch}}
15    *var = 1;
16    return 0;
17  }
18  return 1; // expected-note{{Returning without writing to '*var'}}
19  // expected-note@-1{{Returning the value 1, which participates in a condition later}}
20}
21@end
22
23int foo(I *i) {
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}}
31  return 0;
32}
33
34int initializer1(int *p, int x) {
35  if (x) { // expected-note{{'x' is 0}}
36           // expected-note@-1{{Taking false branch}}
37    *p = 1;
38    return 0;
39  } else {
40    return 1; // expected-note {{Returning without writing to '*p'}}
41  }
42}
43
44int initFromBlock() {
45  __block int z;
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}}
52  }();
53  return z;
54}
55
56extern void expectNonNull(NSString * _Nonnull a);
57
58@interface A : NSObject
59- (void) initAMaybe;
60@end
61
62@implementation A {
63  NSString * a;
64}
65
66- (void) initAMaybe {
67  if (coin()) // expected-note{{Assuming the condition is false}}
68              // expected-note@-1{{Taking false branch}}
69    a = @"string";
70} // expected-note{{Returning without writing to 'self->a'}}
71
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}}
78}
79
80- (void) initAMaybeWithExplicitSelf {
81  if (coin()) // expected-note{{Assuming the condition is false}}
82              // expected-note@-1{{Taking false branch}}
83    self->a = @"string";
84} // expected-note{{Returning without writing to 'self->a'}}
85
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}}
92}
93
94- (void) initPassedAMaybe:(A *) param {
95  if (coin()) // expected-note{{Assuming the condition is false}}
96              // expected-note@-1{{Taking false branch}}
97    param->a = @"string";
98} // expected-note{{Returning without writing to 'param->a'}}
99
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}}
106
107}
108
109@end
110