George Karpenkov | b293c6bb | 2018-07-27 18:26:40 +0000 | [diff] [blame] | 1 | // RUN: %clang_analyze_cc1 -x objective-c -analyzer-checker=core,nullability -analyzer-output=text -Wno-objc-root-class -fblocks -verify %s |
George Karpenkov | e15451a | 2018-02-23 23:26:56 +0000 | [diff] [blame] | 2 | |
George Karpenkov | b293c6bb | 2018-07-27 18:26:40 +0000 | [diff] [blame] | 3 | #include "../Inputs/system-header-simulator-for-nullability.h" |
| 4 | |
| 5 | extern int coin(); |
| 6 | |
| 7 | @interface I : NSObject |
George Karpenkov | e15451a | 2018-02-23 23:26:56 +0000 | [diff] [blame] | 8 | - (int)initVar:(int *)var param:(int)param; |
| 9 | @end |
| 10 | |
| 11 | @implementation I |
| 12 | - (int)initVar:(int *)var param:(int)param { |
Csaba Dabis | 4b0184b | 2019-05-29 20:06:09 +0000 | [diff] [blame] | 13 | if (param) { // expected-note{{'param' is 0}} |
| 14 | // expected-note@-1{{Taking false branch}} |
George Karpenkov | e15451a | 2018-02-23 23:26:56 +0000 | [diff] [blame] | 15 | *var = 1; |
| 16 | return 0; |
| 17 | } |
| 18 | return 1; // expected-note{{Returning without writing to '*var'}} |
| 19 | } |
| 20 | @end |
| 21 | |
| 22 | int 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 Dabis | 4b0184b | 2019-05-29 20:06:09 +0000 | [diff] [blame] | 26 | if (out) //expected-note{{'out' is 1}} |
| 27 | //expected-note@-1{{Taking true branch}} |
George Karpenkov | e15451a | 2018-02-23 23:26:56 +0000 | [diff] [blame] | 28 | 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 | |
| 33 | int initializer1(int *p, int x) { |
Csaba Dabis | 4b0184b | 2019-05-29 20:06:09 +0000 | [diff] [blame] | 34 | if (x) { // expected-note{{'x' is 0}} |
| 35 | // expected-note@-1{{Taking false branch}} |
George Karpenkov | e15451a | 2018-02-23 23:26:56 +0000 | [diff] [blame] | 36 | *p = 1; |
| 37 | return 0; |
| 38 | } else { |
| 39 | return 1; // expected-note {{Returning without writing to '*p'}} |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | int 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 Karpenkov | b293c6bb | 2018-07-27 18:26:40 +0000 | [diff] [blame] | 54 | |
| 55 | extern void expectNonNull(NSString * _Nonnull a); |
| 56 | |
| 57 | @interface A : NSObject |
George Karpenkov | b293c6bb | 2018-07-27 18:26:40 +0000 | [diff] [blame] | 58 | - (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 Karpenkov | 1d08c51 | 2018-08-02 02:02:40 +0000 | [diff] [blame] | 71 | - (void) passNullToNonnull { |
George Karpenkov | b293c6bb | 2018-07-27 18:26:40 +0000 | [diff] [blame] | 72 | 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 Karpenkov | 1d08c51 | 2018-08-02 02:02:40 +0000 | [diff] [blame] | 79 | - (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 Karpenkov | b293c6bb | 2018-07-27 18:26:40 +0000 | [diff] [blame] | 108 | @end |