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{{Taking false branch}}
14    *var = 1;
15    return 0;
16  }
17  return 1; // expected-note{{Returning without writing to '*var'}}
18}
19@end
20
21int foo(I *i) {
22  int x;                            //expected-note{{'x' declared without an initial value}}
23  int out = [i initVar:&x param:0]; //expected-note{{Calling 'initVar:param:'}}
24                                    //expected-note@-1{{Returning from 'initVar:param:'}}
25  if (out)                          // expected-note{{Taking true branch}}
26    return x;                       //expected-warning{{Undefined or garbage value returned to caller}}
27                                    //expected-note@-1{{Undefined or garbage value returned to caller}}
28  return 0;
29}
30
31int initializer1(int *p, int x) {
32  if (x) { // expected-note{{Taking false branch}}
33    *p = 1;
34    return 0;
35  } else {
36    return 1; // expected-note {{Returning without writing to '*p'}}
37  }
38}
39
40int initFromBlock() {
41  __block int z;
42  ^{                     // expected-note {{Calling anonymous block}}
43    int p;               // expected-note{{'p' declared without an initial value}}
44    initializer1(&p, 0); // expected-note{{Calling 'initializer1'}}
45                         // expected-note@-1{{Returning from 'initializer1'}}
46    z = p;               // expected-warning{{Assigned value is garbage or undefined}}
47                         // expected-note@-1{{Assigned value is garbage or undefined}}
48  }();
49  return z;
50}
51
52extern void expectNonNull(NSString * _Nonnull a);
53
54@interface A : NSObject
55- (void) func;
56- (void) initAMaybe;
57@end
58
59@implementation A {
60  NSString * a;
61}
62
63- (void) initAMaybe {
64  if (coin()) // expected-note{{Assuming the condition is false}}
65              // expected-note@-1{{Taking false branch}}
66    a = @"string";
67} // expected-note{{Returning without writing to 'self->a'}}
68
69- (void) func {
70  a = nil; // expected-note{{nil object reference stored to 'a'}}
71  [self initAMaybe]; // expected-note{{Calling 'initAMaybe'}}
72                     // expected-note@-1{{Returning from 'initAMaybe'}}
73  expectNonNull(a); // expected-warning{{nil passed to a callee that requires a non-null 1st parameter}}
74                    // expected-note@-1{{nil passed to a callee that requires a non-null 1st parameter}}
75}
76
77@end
78