George Karpenkov | e99ba6e | 2018-08-10 22:27:04 +0000 | [diff] [blame^] | 1 | // RUN: %clang_analyze_cc1 -fblocks -analyze -analyzer-checker=core,nullability,apiModeling,debug.ExprInspection -verify %s |
George Karpenkov | 2301c5a | 2018-03-23 00:16:03 +0000 | [diff] [blame] | 2 | |
| 3 | #include "Inputs/system-header-simulator-for-nullability.h" |
| 4 | |
George Karpenkov | e99ba6e | 2018-08-10 22:27:04 +0000 | [diff] [blame^] | 5 | void clang_analyzer_warnIfReached(); |
| 6 | |
George Karpenkov | 2301c5a | 2018-03-23 00:16:03 +0000 | [diff] [blame] | 7 | NSString* _Nonnull trust_nonnull_framework_annotation() { |
| 8 | NSString* out = [NSString generateString]; |
| 9 | if (out) {} |
| 10 | return out; // no-warning |
| 11 | } |
| 12 | |
George Karpenkov | 7744c7f | 2018-05-31 00:28:13 +0000 | [diff] [blame] | 13 | NSString* _Nonnull trust_instancemsg_annotation(NSString* _Nonnull param) { |
| 14 | NSString* out = [param stringByAppendingString:@"string"]; |
| 15 | if (out) {} |
| 16 | return out; // no-warning |
| 17 | } |
| 18 | |
| 19 | NSString* _Nonnull distrust_instancemsg_noannotation(NSString* param) { |
| 20 | if (param) {} |
| 21 | NSString* out = [param stringByAppendingString:@"string"]; |
| 22 | if (out) {} |
| 23 | return out; // expected-warning{{}} |
| 24 | } |
| 25 | |
| 26 | NSString* _Nonnull trust_analyzer_knowledge(NSString* param) { |
| 27 | if (!param) |
| 28 | return @""; |
| 29 | NSString* out = [param stringByAppendingString:@"string"]; |
| 30 | if (out) {} |
| 31 | return out; // no-warning |
| 32 | } |
| 33 | |
| 34 | NSString* _Nonnull trust_assume_nonnull_macro() { |
| 35 | NSString* out = [NSString generateImplicitlyNonnullString]; |
| 36 | if (out) {} |
| 37 | return out; // no-warning |
| 38 | } |
| 39 | |
George Karpenkov | 2301c5a | 2018-03-23 00:16:03 +0000 | [diff] [blame] | 40 | NSString* _Nonnull distrust_without_annotation() { |
| 41 | NSString* out = [NSString generatePossiblyNullString]; |
| 42 | if (out) {} |
| 43 | return out; // expected-warning{{}} |
| 44 | } |
| 45 | |
| 46 | NSString* _Nonnull nonnull_please_trust_me(); |
| 47 | |
| 48 | NSString* _Nonnull distrust_local_nonnull_annotation() { |
| 49 | NSString* out = nonnull_please_trust_me(); |
| 50 | if (out) {} |
| 51 | return out; // expected-warning{{}} |
| 52 | } |
| 53 | |
| 54 | NSString* _Nonnull trust_c_function() { |
| 55 | NSString* out = getString(); |
| 56 | if (out) {}; |
| 57 | return out; // no-warning |
| 58 | } |
| 59 | |
| 60 | NSString* _Nonnull distrust_unannoted_function() { |
| 61 | NSString* out = getPossiblyNullString(); |
| 62 | if (out) {}; |
| 63 | return out; // expected-warning{{}} |
| 64 | } |
| 65 | |
| 66 | NSString * _Nonnull distrustProtocol(id<MyProtocol> o) { |
| 67 | NSString* out = [o getString]; |
| 68 | if (out) {}; |
| 69 | return out; // expected-warning{{}} |
| 70 | } |
George Karpenkov | 7744c7f | 2018-05-31 00:28:13 +0000 | [diff] [blame] | 71 | |
George Karpenkov | e99ba6e | 2018-08-10 22:27:04 +0000 | [diff] [blame^] | 72 | // If the return value is non-nil, the index is non-nil. |
| 73 | NSString *_Nonnull retImpliesIndex(NSString *s, |
| 74 | NSDictionary *dic) { |
| 75 | id obj = dic[s]; |
| 76 | if (s) {} |
| 77 | if (obj) |
| 78 | return s; // no-warning |
| 79 | return @"foo"; |
| 80 | } |
| 81 | |
| 82 | NSString *_Nonnull retImpliesIndexOtherMethod(NSString *s, |
| 83 | NSDictionary *dic) { |
| 84 | id obj = [dic objectForKey:s]; |
| 85 | if (s) {} |
| 86 | if (obj) |
| 87 | return s; // no-warning |
| 88 | return @"foo"; |
| 89 | } |
| 90 | |
| 91 | NSString *_Nonnull retImpliesIndexOnRHS(NSString *s, |
| 92 | NSDictionary *dic) { |
| 93 | id obj = dic[s]; |
| 94 | if (s) {} |
| 95 | if (nil != obj) |
| 96 | return s; // no-warning |
| 97 | return @"foo"; |
| 98 | } |
| 99 | |
| 100 | NSString *_Nonnull retImpliesIndexReverseCheck(NSString *s, |
| 101 | NSDictionary *dic) { |
| 102 | id obj = dic[s]; |
| 103 | if (s) {} |
| 104 | if (!obj) |
| 105 | return @"foo"; |
| 106 | return s; // no-warning |
| 107 | } |
| 108 | |
| 109 | NSString *_Nonnull retImpliesIndexReverseCheckOnRHS(NSString *s, |
| 110 | NSDictionary *dic) { |
| 111 | id obj = dic[s]; |
| 112 | if (s) {} |
| 113 | if (nil == obj) |
| 114 | return @"foo"; |
| 115 | return s; // no-warning |
| 116 | } |
| 117 | |
| 118 | NSString *_Nonnull retImpliesIndexWrongBranch(NSString *s, |
| 119 | NSDictionary *dic) { |
| 120 | id obj = dic[s]; |
| 121 | if (s) {} |
| 122 | if (!obj) |
| 123 | return s; // expected-warning{{}} |
| 124 | return @"foo"; |
| 125 | } |
| 126 | |
| 127 | NSString *_Nonnull retImpliesIndexWrongBranchOnRHS(NSString *s, |
| 128 | NSDictionary *dic) { |
| 129 | id obj = dic[s]; |
| 130 | if (s) {} |
| 131 | if (nil == obj) |
| 132 | return s; // expected-warning{{}} |
| 133 | return @"foo"; |
| 134 | } |
| 135 | |
| 136 | // The return value could still be nil for a non-nil index. |
| 137 | NSDictionary *_Nonnull indexDoesNotImplyRet(NSString *s, |
| 138 | NSDictionary *dic) { |
| 139 | id obj = dic[s]; |
| 140 | if (obj) {} |
| 141 | if (s) |
| 142 | return obj; // expected-warning{{}} |
| 143 | return [[NSDictionary alloc] init]; |
| 144 | } |
| 145 | |
| 146 | // The return value could still be nil for a non-nil index. |
| 147 | NSDictionary *_Nonnull notIndexImpliesNotRet(NSString *s, |
| 148 | NSDictionary *dic) { |
| 149 | id obj = dic[s]; |
| 150 | if (!s) { |
| 151 | if (obj != nil) { |
| 152 | clang_analyzer_warnIfReached(); // no-warning |
| 153 | } |
| 154 | } |
| 155 | return [[NSDictionary alloc] init]; |
| 156 | } |
| 157 | |
| 158 | NSString *_Nonnull checkAssumeOnMutableDictionary(NSMutableDictionary *d, |
| 159 | NSString *k, |
| 160 | NSString *val) { |
| 161 | d[k] = val; |
| 162 | if (k) {} |
| 163 | return k; // no-warning |
| 164 | } |
| 165 | |
| 166 | NSString *_Nonnull checkAssumeOnMutableDictionaryOtherMethod(NSMutableDictionary *d, |
| 167 | NSString *k, |
| 168 | NSString *val) { |
| 169 | [d setObject:val forKey:k]; |
| 170 | if (k) {} |
| 171 | return k; // no-warning |
| 172 | } |