Ted Kremenek | 3092dd6 | 2009-02-17 19:29:07 +0000 | [diff] [blame] | 1 | // RUN: clang -analyze -checker-cfref --analyzer-store=basic -analyzer-constraints=basic --verify -fblocks %s && |
| 2 | // RUN: clang -analyze -checker-cfref --analyzer-store=basic -analyzer-constraints=range --verify -fblocks %s && |
| 3 | // RUN: clang -analyze -checker-cfref --analyzer-store=region -analyzer-constraints=basic --verify -fblocks %s && |
| 4 | // RUN: clang -analyze -checker-cfref --analyzer-store=region -analyzer-constraints=range --verify -fblocks %s |
Ted Kremenek | 2dabd42 | 2009-01-22 18:53:15 +0000 | [diff] [blame] | 5 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame^] | 6 | typedef struct objc_selector *SEL; |
| 7 | typedef signed char BOOL; |
| 8 | typedef int NSInteger; |
| 9 | typedef unsigned int NSUInteger; |
| 10 | typedef struct _NSZone NSZone; |
| 11 | @class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; |
| 12 | @protocol NSObject - (BOOL)isEqual:(id)object; @end |
| 13 | @protocol NSCopying - (id)copyWithZone:(NSZone *)zone; @end |
| 14 | @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; @end |
| 15 | @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; @end |
| 16 | @interface NSObject <NSObject> {} - (id)init; @end |
| 17 | extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone); |
| 18 | @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> |
| 19 | - (NSUInteger)length; |
| 20 | + (id)stringWithUTF8String:(const char *)nullTerminatedCString; |
| 21 | @end extern NSString * const NSBundleDidLoadNotification; |
| 22 | @interface NSAssertionHandler : NSObject {} |
| 23 | + (NSAssertionHandler *)currentHandler; |
| 24 | - (void)handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format,...; |
| 25 | @end |
| 26 | extern NSString * const NSConnectionReplyMode; |
Ted Kremenek | 9f67ede | 2008-10-01 05:05:46 +0000 | [diff] [blame] | 27 | |
| 28 | // Reduced test case from crash in <rdar://problem/6253157> |
Ted Kremenek | 9f67ede | 2008-10-01 05:05:46 +0000 | [diff] [blame] | 29 | @interface A @end |
| 30 | @implementation A |
| 31 | - (void)foo:(void (^)(NSObject *x))block { |
| 32 | if (!((block != ((void *)0)))) {} |
| 33 | } |
| 34 | @end |
| 35 | |
Ted Kremenek | 6dfe2f5 | 2008-10-18 22:20:20 +0000 | [diff] [blame] | 36 | // Reduced test case from crash in PR 2796; |
| 37 | // http://llvm.org/bugs/show_bug.cgi?id=2796 |
| 38 | |
| 39 | unsigned foo(unsigned x) { return __alignof__((x)) + sizeof(x); } |
Ted Kremenek | 9253b0f | 2008-10-20 23:14:31 +0000 | [diff] [blame] | 40 | |
| 41 | // Improvement to path-sensitivity involving compound assignments. |
| 42 | // Addresses false positive in <rdar://problem/6268365> |
| 43 | // |
| 44 | |
| 45 | unsigned r6268365Aux(); |
| 46 | |
| 47 | void r6268365() { |
| 48 | unsigned x = 0; |
| 49 | x &= r6268365Aux(); |
| 50 | unsigned j = 0; |
| 51 | |
| 52 | if (x == 0) ++j; |
| 53 | if (x == 0) x = x / j; // no-warning |
| 54 | } |
| 55 | |
Ted Kremenek | c13b6e2 | 2008-10-20 23:40:25 +0000 | [diff] [blame] | 56 | void divzeroassume(unsigned x, unsigned j) { |
| 57 | x /= j; |
| 58 | if (j == 0) x /= 0; // no-warning |
| 59 | if (j == 0) x /= j; // no-warning |
| 60 | if (j == 0) x = x / 0; // no-warning |
| 61 | } |
| 62 | |
| 63 | void divzeroassumeB(unsigned x, unsigned j) { |
| 64 | x = x / j; |
| 65 | if (j == 0) x /= 0; // no-warning |
| 66 | if (j == 0) x /= j; // no-warning |
| 67 | if (j == 0) x = x / 0; // no-warning |
| 68 | } |
| 69 | |
Ted Kremenek | 76dba7b | 2008-11-13 05:05:34 +0000 | [diff] [blame] | 70 | // InitListExpr processing |
| 71 | |
| 72 | typedef float __m128 __attribute__((__vector_size__(16), __may_alias__)); |
| 73 | __m128 return128() { |
Ted Kremenek | 062e2f9 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 74 | // This compound literal has a Vector type. We currently just |
| 75 | // return UnknownVal. |
Ted Kremenek | 76dba7b | 2008-11-13 05:05:34 +0000 | [diff] [blame] | 76 | return __extension__(__m128) { 0.0f, 0.0f, 0.0f, 0.0f }; |
| 77 | } |
| 78 | |
Ted Kremenek | 062e2f9 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 79 | typedef long long __v2di __attribute__ ((__vector_size__ (16))); |
| 80 | typedef long long __m128i __attribute__ ((__vector_size__ (16), __may_alias__)); |
| 81 | __m128i vec128i(long long __q1, long long __q0) { |
| 82 | // This compound literal returns true for both isVectorType() and |
| 83 | // isIntegerType(). |
| 84 | return __extension__ (__m128i)(__v2di){ __q0, __q1 }; |
| 85 | } |
| 86 | |
Ted Kremenek | 8322d6a | 2008-12-09 00:14:48 +0000 | [diff] [blame] | 87 | // Zero-sized VLAs. |
| 88 | void check_zero_sized_VLA(int x) { |
| 89 | if (x) |
| 90 | return; |
| 91 | |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 92 | int vla[x]; // expected-warning{{VLAs with no elements have undefined behavior}} |
| 93 | } |
| 94 | |
| 95 | void check_uninit_sized_VLA() { |
| 96 | int x; |
| 97 | int vla[x]; // expected-warning{{The expression used to specify the number of elements in the VLA 'vla' evaluates to an undefined or garbage value.}} |
Ted Kremenek | 8322d6a | 2008-12-09 00:14:48 +0000 | [diff] [blame] | 98 | } |
Ted Kremenek | 062e2f9 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 99 | |
Ted Kremenek | 55f7bcb | 2008-12-15 18:51:00 +0000 | [diff] [blame] | 100 | // sizeof(void) |
| 101 | // - Tests a regression reported in PR 3211: http://llvm.org/bugs/show_bug.cgi?id=3211 |
| 102 | void handle_sizeof_void(unsigned flag) { |
| 103 | int* p = 0; |
| 104 | |
| 105 | if (flag) { |
| 106 | if (sizeof(void) == 1) |
| 107 | return; |
| 108 | // Infeasible. |
| 109 | *p = 1; // no-warning |
| 110 | } |
| 111 | |
| 112 | void* q; |
| 113 | |
| 114 | if (!flag) { |
| 115 | if (sizeof(*q) == 1) |
| 116 | return; |
| 117 | // Infeasibe. |
| 118 | *p = 1; // no-warning |
| 119 | } |
| 120 | |
| 121 | // Infeasible. |
| 122 | *p = 1; // no-warning |
| 123 | } |
| 124 | |
Ted Kremenek | d76d47e | 2009-01-27 18:29:03 +0000 | [diff] [blame] | 125 | // PR 3422 |
| 126 | void pr3422_helper(char *p); |
| 127 | void pr3422() { |
| 128 | char buf[100]; |
| 129 | char *q = &buf[10]; |
| 130 | pr3422_helper(&q[1]); |
| 131 | } |
| 132 | |
Ted Kremenek | a3d1eb8 | 2009-02-14 05:55:08 +0000 | [diff] [blame] | 133 | // PR 3543 (handle empty statement expressions) |
| 134 | int pr_3543(void) { |
| 135 | ({}); |
| 136 | } |
| 137 | |
Ted Kremenek | 265a305 | 2009-02-24 02:23:11 +0000 | [diff] [blame] | 138 | // <rdar://problem/6611677> |
| 139 | // This test case test the use of a vector type within an array subscript |
| 140 | // expression. |
| 141 | typedef long long __a64vector __attribute__((__vector_size__(8))); |
| 142 | typedef long long __a128vector __attribute__((__vector_size__(16))); |
| 143 | static inline __a64vector __attribute__((__always_inline__, __nodebug__)) |
| 144 | my_test_mm_movepi64_pi64(__a128vector a) { |
| 145 | return (__a64vector)a[0]; |
| 146 | } |
| 147 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame^] | 148 | // Test basic tracking of ivars associated with 'self'. |
| 149 | @interface SelfIvarTest : NSObject { |
| 150 | int flag; |
| 151 | } |
| 152 | - (void)test_self_tracking; |
| 153 | @end |
| 154 | |
| 155 | @implementation SelfIvarTest |
| 156 | - (void)test_self_tracking { |
| 157 | char *p = 0; |
| 158 | char c; |
| 159 | |
| 160 | if (flag) |
| 161 | p = "hello"; |
| 162 | |
| 163 | if (flag) |
| 164 | c = *p; // no-warning |
| 165 | } |
| 166 | @end |
Ted Kremenek | a3d1eb8 | 2009-02-14 05:55:08 +0000 | [diff] [blame] | 167 | |