summaryrefslogtreecommitdiff
path: root/clang/test/Analysis/trustnonnullchecker_test.m
blob: 81eac863d5eb2d68866ac58347908077d508388b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Temporarily disabling the test, it failes the "system is over-constrained"
// assertion in *non* optimized builds.
// REQUIRES: rdar44992170
// RUN: %clang_analyze_cc1 -fblocks -analyze -analyzer-checker=core,nullability,apiModeling,debug.ExprInspection  -verify %s

#include "Inputs/system-header-simulator-for-nullability.h"

void clang_analyzer_warnIfReached();

NSString* _Nonnull trust_nonnull_framework_annotation() {
  NSString* out = [NSString generateString];
  if (out) {}
  return out; // no-warning
}

NSString* _Nonnull trust_instancemsg_annotation(NSString* _Nonnull param) {
  NSString* out = [param stringByAppendingString:@"string"];
  if (out) {}
  return out; // no-warning
}

NSString* _Nonnull distrust_instancemsg_noannotation(NSString* param) {
  if (param) {}
  NSString* out = [param stringByAppendingString:@"string"];
  if (out) {}
  return out; // expected-warning{{}}
}

NSString* _Nonnull trust_analyzer_knowledge(NSString* param) {
  if (!param)
    return @"";
  NSString* out = [param stringByAppendingString:@"string"];
  if (out) {}
  return out; // no-warning
}

NSString* _Nonnull trust_assume_nonnull_macro() {
  NSString* out = [NSString generateImplicitlyNonnullString];
  if (out) {}
  return out; // no-warning
}

NSString* _Nonnull distrust_without_annotation() {
  NSString* out = [NSString generatePossiblyNullString];
  if (out) {}
  return out; // expected-warning{{}}
}

NSString* _Nonnull nonnull_please_trust_me();

NSString* _Nonnull distrust_local_nonnull_annotation() {
  NSString* out = nonnull_please_trust_me();
  if (out) {}
  return out; // expected-warning{{}}
}

NSString* _Nonnull trust_c_function() {
  NSString* out = getString();
  if (out) {};
  return out; // no-warning
}

NSString* _Nonnull distrust_unannoted_function() {
  NSString* out = getPossiblyNullString();
  if (out) {};
  return out; // expected-warning{{}}
}

NSString * _Nonnull distrustProtocol(id<MyProtocol> o) {
  NSString* out = [o getString];
  if (out) {};
  return out; // expected-warning{{}}
}

// If the return value is non-nil, the index is non-nil.
NSString *_Nonnull retImpliesIndex(NSString *s,
                                   NSDictionary *dic) {
  id obj = dic[s];
  if (s) {}
  if (obj)
    return s; // no-warning
  return @"foo";
}

NSString *_Nonnull retImpliesIndexOtherMethod(NSString *s,
                                   NSDictionary *dic) {
  id obj = [dic objectForKey:s];
  if (s) {}
  if (obj)
    return s; // no-warning
  return @"foo";
}

NSString *_Nonnull retImpliesIndexOnRHS(NSString *s,
                                        NSDictionary *dic) {
  id obj = dic[s];
  if (s) {}
  if (nil != obj)
    return s; // no-warning
  return @"foo";
}

NSString *_Nonnull retImpliesIndexReverseCheck(NSString *s,
                                               NSDictionary *dic) {
  id obj = dic[s];
  if (s) {}
  if (!obj)
    return @"foo";
  return s; // no-warning
}

NSString *_Nonnull retImpliesIndexReverseCheckOnRHS(NSString *s,
                                                    NSDictionary *dic) {
  id obj = dic[s];
  if (s) {}
  if (nil == obj)
    return @"foo";
  return s; // no-warning
}

NSString *_Nonnull retImpliesIndexWrongBranch(NSString *s,
                                              NSDictionary *dic) {
  id obj = dic[s];
  if (s) {}
  if (!obj)
    return s; // expected-warning{{}}
  return @"foo";
}

NSString *_Nonnull retImpliesIndexWrongBranchOnRHS(NSString *s,
                                                   NSDictionary *dic) {
  id obj = dic[s];
  if (s) {}
  if (nil == obj)
    return s; // expected-warning{{}}
  return @"foo";
}

// The return value could still be nil for a non-nil index.
NSDictionary *_Nonnull indexDoesNotImplyRet(NSString *s,
                                            NSDictionary *dic) {
  id obj = dic[s];
  if (obj) {}
  if (s)
    return obj; // expected-warning{{}}
  return [[NSDictionary alloc] init];
}

// The return value could still be nil for a non-nil index.
NSDictionary *_Nonnull notIndexImpliesNotRet(NSString *s,
                                             NSDictionary *dic) {
  id obj = dic[s];
  if (!s) {
    if (obj != nil) {
      clang_analyzer_warnIfReached(); // no-warning
    }
  }
  return [[NSDictionary alloc] init];
}

NSString *_Nonnull checkAssumeOnMutableDictionary(NSMutableDictionary *d,
                                                  NSString *k,
                                                  NSString *val) {
  d[k] = val;
  if (k) {}
  return k; // no-warning
}

NSString *_Nonnull checkAssumeOnMutableDictionaryOtherMethod(NSMutableDictionary *d,
                                                             NSString *k,
                                                             NSString *val) {
  [d setObject:val forKey:k];
  if (k) {}
  return k; // no-warning
}

// Check that we don't crash when the added assumption is enough
// to make the state unfeasible.
@class DummyClass;
@interface DictionarySubclass : NSDictionary {
  DummyClass *g;
  DictionarySubclass *d;
}
@end
@implementation DictionarySubclass
- (id) objectForKey:(id)e {
  if (e) {}
  return d;
}
- (void) coder {
  for (id e in g) {
    id f = [self objectForKey:e];
    if (f)
      (void)e;
  }
}
@end