Daniel Dunbar | d7d5f02 | 2009-03-24 02:24:46 +0000 | [diff] [blame] | 1 | // RUN: clang-cc -analyze -checker-cfref --analyzer-store=basic -analyzer-constraints=basic --verify -fblocks %s && |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 2 | // RUN: clang-cc -analyze -checker-cfref --analyzer-store=basic -analyzer-constraints=range --verify -fblocks %s |
| 3 | |
| 4 | // NOWORK: clang-cc -analyze -checker-cfref --analyzer-store=region -analyzer-constraints=basic --verify -fblocks %s && |
| 5 | // NOWORK: clang-cc -analyze -checker-cfref --analyzer-store=region -analyzer-constraints=range --verify -fblocks %s |
Ted Kremenek | 2dabd42 | 2009-01-22 18:53:15 +0000 | [diff] [blame] | 6 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 7 | typedef struct objc_selector *SEL; |
| 8 | typedef signed char BOOL; |
| 9 | typedef int NSInteger; |
| 10 | typedef unsigned int NSUInteger; |
| 11 | typedef struct _NSZone NSZone; |
| 12 | @class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; |
| 13 | @protocol NSObject - (BOOL)isEqual:(id)object; @end |
| 14 | @protocol NSCopying - (id)copyWithZone:(NSZone *)zone; @end |
| 15 | @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; @end |
| 16 | @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; @end |
| 17 | @interface NSObject <NSObject> {} - (id)init; @end |
| 18 | extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone); |
| 19 | @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> |
| 20 | - (NSUInteger)length; |
| 21 | + (id)stringWithUTF8String:(const char *)nullTerminatedCString; |
| 22 | @end extern NSString * const NSBundleDidLoadNotification; |
| 23 | @interface NSAssertionHandler : NSObject {} |
| 24 | + (NSAssertionHandler *)currentHandler; |
| 25 | - (void)handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format,...; |
| 26 | @end |
| 27 | extern NSString * const NSConnectionReplyMode; |
Ted Kremenek | 693de5d | 2009-03-23 15:42:58 +0000 | [diff] [blame] | 28 | typedef float CGFloat; |
| 29 | typedef struct _NSPoint { |
| 30 | CGFloat x; |
| 31 | CGFloat y; |
| 32 | } NSPoint; |
| 33 | typedef struct _NSSize { |
| 34 | CGFloat width; |
| 35 | CGFloat height; |
| 36 | } NSSize; |
| 37 | typedef struct _NSRect { |
| 38 | NSPoint origin; |
| 39 | NSSize size; |
| 40 | } NSRect; |
Ted Kremenek | 9f67ede | 2008-10-01 05:05:46 +0000 | [diff] [blame] | 41 | |
| 42 | // Reduced test case from crash in <rdar://problem/6253157> |
Ted Kremenek | 9f67ede | 2008-10-01 05:05:46 +0000 | [diff] [blame] | 43 | @interface A @end |
| 44 | @implementation A |
| 45 | - (void)foo:(void (^)(NSObject *x))block { |
| 46 | if (!((block != ((void *)0)))) {} |
| 47 | } |
| 48 | @end |
| 49 | |
Ted Kremenek | 6dfe2f5 | 2008-10-18 22:20:20 +0000 | [diff] [blame] | 50 | // Reduced test case from crash in PR 2796; |
| 51 | // http://llvm.org/bugs/show_bug.cgi?id=2796 |
| 52 | |
| 53 | unsigned foo(unsigned x) { return __alignof__((x)) + sizeof(x); } |
Ted Kremenek | 9253b0f | 2008-10-20 23:14:31 +0000 | [diff] [blame] | 54 | |
| 55 | // Improvement to path-sensitivity involving compound assignments. |
| 56 | // Addresses false positive in <rdar://problem/6268365> |
| 57 | // |
| 58 | |
| 59 | unsigned r6268365Aux(); |
| 60 | |
| 61 | void r6268365() { |
| 62 | unsigned x = 0; |
| 63 | x &= r6268365Aux(); |
| 64 | unsigned j = 0; |
| 65 | |
| 66 | if (x == 0) ++j; |
| 67 | if (x == 0) x = x / j; // no-warning |
| 68 | } |
| 69 | |
Ted Kremenek | c13b6e2 | 2008-10-20 23:40:25 +0000 | [diff] [blame] | 70 | void divzeroassume(unsigned x, unsigned j) { |
| 71 | x /= j; |
| 72 | if (j == 0) x /= 0; // no-warning |
| 73 | if (j == 0) x /= j; // no-warning |
| 74 | if (j == 0) x = x / 0; // no-warning |
| 75 | } |
| 76 | |
| 77 | void divzeroassumeB(unsigned x, unsigned j) { |
| 78 | x = x / j; |
| 79 | if (j == 0) x /= 0; // no-warning |
| 80 | if (j == 0) x /= j; // no-warning |
| 81 | if (j == 0) x = x / 0; // no-warning |
| 82 | } |
| 83 | |
Ted Kremenek | 76dba7b | 2008-11-13 05:05:34 +0000 | [diff] [blame] | 84 | // InitListExpr processing |
| 85 | |
| 86 | typedef float __m128 __attribute__((__vector_size__(16), __may_alias__)); |
| 87 | __m128 return128() { |
Ted Kremenek | 062e2f9 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 88 | // This compound literal has a Vector type. We currently just |
| 89 | // return UnknownVal. |
Ted Kremenek | 76dba7b | 2008-11-13 05:05:34 +0000 | [diff] [blame] | 90 | return __extension__(__m128) { 0.0f, 0.0f, 0.0f, 0.0f }; |
| 91 | } |
| 92 | |
Ted Kremenek | 062e2f9 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 93 | typedef long long __v2di __attribute__ ((__vector_size__ (16))); |
| 94 | typedef long long __m128i __attribute__ ((__vector_size__ (16), __may_alias__)); |
| 95 | __m128i vec128i(long long __q1, long long __q0) { |
| 96 | // This compound literal returns true for both isVectorType() and |
| 97 | // isIntegerType(). |
| 98 | return __extension__ (__m128i)(__v2di){ __q0, __q1 }; |
| 99 | } |
| 100 | |
Ted Kremenek | 8322d6a | 2008-12-09 00:14:48 +0000 | [diff] [blame] | 101 | // Zero-sized VLAs. |
| 102 | void check_zero_sized_VLA(int x) { |
| 103 | if (x) |
| 104 | return; |
| 105 | |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 106 | int vla[x]; // expected-warning{{VLAs with no elements have undefined behavior}} |
| 107 | } |
| 108 | |
| 109 | void check_uninit_sized_VLA() { |
| 110 | int x; |
Ted Kremenek | b8adaf9 | 2009-04-02 02:52:13 +0000 | [diff] [blame] | 111 | int vla[x]; // expected-warning{{The expression used to specify the number of elements in the variable-length array (VLA) 'vla' evaluates to an undefined or garbage value}} |
Ted Kremenek | 8322d6a | 2008-12-09 00:14:48 +0000 | [diff] [blame] | 112 | } |
Ted Kremenek | 062e2f9 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 113 | |
Ted Kremenek | 55f7bcb | 2008-12-15 18:51:00 +0000 | [diff] [blame] | 114 | // sizeof(void) |
| 115 | // - Tests a regression reported in PR 3211: http://llvm.org/bugs/show_bug.cgi?id=3211 |
| 116 | void handle_sizeof_void(unsigned flag) { |
| 117 | int* p = 0; |
| 118 | |
| 119 | if (flag) { |
| 120 | if (sizeof(void) == 1) |
| 121 | return; |
| 122 | // Infeasible. |
| 123 | *p = 1; // no-warning |
| 124 | } |
| 125 | |
| 126 | void* q; |
| 127 | |
| 128 | if (!flag) { |
| 129 | if (sizeof(*q) == 1) |
| 130 | return; |
| 131 | // Infeasibe. |
| 132 | *p = 1; // no-warning |
| 133 | } |
| 134 | |
| 135 | // Infeasible. |
| 136 | *p = 1; // no-warning |
| 137 | } |
| 138 | |
Ted Kremenek | d76d47e | 2009-01-27 18:29:03 +0000 | [diff] [blame] | 139 | // PR 3422 |
| 140 | void pr3422_helper(char *p); |
| 141 | void pr3422() { |
| 142 | char buf[100]; |
| 143 | char *q = &buf[10]; |
| 144 | pr3422_helper(&q[1]); |
| 145 | } |
| 146 | |
Ted Kremenek | a3d1eb8 | 2009-02-14 05:55:08 +0000 | [diff] [blame] | 147 | // PR 3543 (handle empty statement expressions) |
| 148 | int pr_3543(void) { |
| 149 | ({}); |
| 150 | } |
| 151 | |
Ted Kremenek | 265a305 | 2009-02-24 02:23:11 +0000 | [diff] [blame] | 152 | // <rdar://problem/6611677> |
| 153 | // This test case test the use of a vector type within an array subscript |
| 154 | // expression. |
| 155 | typedef long long __a64vector __attribute__((__vector_size__(8))); |
| 156 | typedef long long __a128vector __attribute__((__vector_size__(16))); |
| 157 | static inline __a64vector __attribute__((__always_inline__, __nodebug__)) |
| 158 | my_test_mm_movepi64_pi64(__a128vector a) { |
| 159 | return (__a64vector)a[0]; |
| 160 | } |
| 161 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 162 | // Test basic tracking of ivars associated with 'self'. |
| 163 | @interface SelfIvarTest : NSObject { |
| 164 | int flag; |
| 165 | } |
| 166 | - (void)test_self_tracking; |
| 167 | @end |
| 168 | |
| 169 | @implementation SelfIvarTest |
| 170 | - (void)test_self_tracking { |
| 171 | char *p = 0; |
| 172 | char c; |
| 173 | |
| 174 | if (flag) |
| 175 | p = "hello"; |
| 176 | |
| 177 | if (flag) |
| 178 | c = *p; // no-warning |
| 179 | } |
| 180 | @end |
Ted Kremenek | a3d1eb8 | 2009-02-14 05:55:08 +0000 | [diff] [blame] | 181 | |
Ted Kremenek | 7de20fe | 2009-03-11 02:29:48 +0000 | [diff] [blame] | 182 | // PR 3770 |
| 183 | char pr3770(int x) { |
| 184 | int y = x & 0x2; |
| 185 | char *p = 0; |
| 186 | if (y == 1) |
| 187 | p = "hello"; |
| 188 | |
| 189 | if (y == 1) |
| 190 | return p[0]; // no-warning |
| 191 | |
| 192 | return 'a'; |
| 193 | } |
| 194 | |
Ted Kremenek | 344d4c8 | 2009-03-11 18:17:16 +0000 | [diff] [blame] | 195 | // PR 3772 |
Ted Kremenek | fa6228d | 2009-03-11 02:52:39 +0000 | [diff] [blame] | 196 | // - We just want to test that this doesn't crash the analyzer. |
| 197 | typedef struct st ST; |
| 198 | struct st { char *name; }; |
| 199 | extern ST *Cur_Pu; |
| 200 | |
Ted Kremenek | 344d4c8 | 2009-03-11 18:17:16 +0000 | [diff] [blame] | 201 | void pr3772(void) |
Ted Kremenek | fa6228d | 2009-03-11 02:52:39 +0000 | [diff] [blame] | 202 | { |
| 203 | static ST *last_Cur_Pu; |
| 204 | if (last_Cur_Pu == Cur_Pu) { |
| 205 | return; |
| 206 | } |
| 207 | } |
| 208 | |
Ted Kremenek | 344d4c8 | 2009-03-11 18:17:16 +0000 | [diff] [blame] | 209 | // PR 3780 - This tests that StmtIterator isn't broken for VLAs in DeclGroups. |
| 210 | void pr3780(int sz) { typedef double MAT[sz][sz]; } |
Ted Kremenek | fa6228d | 2009-03-11 02:52:39 +0000 | [diff] [blame] | 211 | |
Ted Kremenek | ec099f1 | 2009-03-18 22:10:22 +0000 | [diff] [blame] | 212 | // <rdar://problem/6695527> - Test that we don't symbolicate doubles before |
| 213 | // we are ready to do something with them. |
| 214 | int rdar6695527(double x) { |
| 215 | if (!x) { return 0; } |
| 216 | return 1; |
| 217 | } |
Ted Kremenek | 693de5d | 2009-03-23 15:42:58 +0000 | [diff] [blame] | 218 | |
| 219 | // <rdar://problem/6708148> - Test that we properly invalidate structs |
| 220 | // passed-by-reference to a function. |
| 221 | void pr6708148_invalidate(NSRect *x); |
| 222 | void pr6708148_use(NSRect x); |
| 223 | void pr6708148_test(void) { |
| 224 | NSRect x; |
| 225 | pr6708148_invalidate(&x); |
| 226 | pr6708148_use(x); // no-warning |
| 227 | } |
| 228 | |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 229 | // Handle both kinds of noreturn attributes for pruning paths. |
| 230 | void rdar_6777003_noret() __attribute__((noreturn)); |
| 231 | void rdar_6777003_analyzer_noret() __attribute__((analyzer_noreturn)); |
| 232 | |
| 233 | void rdar_6777003(int x) { |
| 234 | int *p = 0; |
| 235 | |
| 236 | if (x == 1) { |
| 237 | rdar_6777003_noret(); |
| 238 | *p = 1; // no-warning; |
| 239 | } |
| 240 | |
| 241 | if (x == 2) { |
| 242 | rdar_6777003_analyzer_noret(); |
| 243 | *p = 1; // no-warning; |
| 244 | } |
| 245 | |
| 246 | *p = 1; // expected-warning{{Dereference of null pointer}} |
| 247 | } |
| 248 | |
Ted Kremenek | af48fdd | 2009-04-21 22:38:05 +0000 | [diff] [blame] | 249 | // For pointer arithmetic, --/++ should be treated as preserving non-nullness, |
| 250 | // regardless of how well the underlying StoreManager reasons about pointer |
| 251 | // arithmetic. |
| 252 | // <rdar://problem/6777209> |
Ted Kremenek | af48fdd | 2009-04-21 22:38:05 +0000 | [diff] [blame] | 253 | void rdar_6777209(char *p) { |
| 254 | if (p == 0) |
| 255 | return; |
| 256 | |
| 257 | ++p; |
| 258 | |
| 259 | // This branch should always be infeasible. |
| 260 | if (p == 0) |
| 261 | *p = 'c'; // no-warning |
| 262 | } |
Ted Kremenek | b3cfd58 | 2009-04-23 17:49:43 +0000 | [diff] [blame] | 263 | |
| 264 | // PR 4033. A symbolic 'void *' pointer can be used as the address for a |
| 265 | // computed goto. |
| 266 | typedef void *Opcode; |
| 267 | Opcode pr_4033_getOpcode(); |
| 268 | void pr_4033(void) { |
| 269 | next_opcode: |
| 270 | { |
| 271 | Opcode op = pr_4033_getOpcode(); |
| 272 | if (op) goto *op; |
| 273 | } |
| 274 | } |
| 275 | |
Ted Kremenek | 956a37d | 2009-05-01 23:35:18 +0000 | [diff] [blame] | 276 | // Test invalidating pointers-to-pointers with slightly different types. This |
| 277 | // example came from a recent false positive due to a regression where the |
| 278 | // branch condition was falsely reported as being uninitialized. |
| 279 | void invalidate_by_ref(char **x); |
| 280 | int test_invalidate_by_ref() { |
| 281 | unsigned short y; |
| 282 | invalidate_by_ref((char**) &y); |
| 283 | if (y) // no-warning |
| 284 | return 1; |
| 285 | return 0; |
| 286 | } |
| 287 | |