Ted Kremenek | 2dabd42 | 2009-01-22 18:53:15 +0000 | [diff] [blame] | 1 | // RUN: clang -analyze -checker-cfref --analyzer-store-basic --verify -fblocks %s && |
| 2 | // RUN: clang -analyze -checker-cfref --analyzer-store-region --verify -fblocks %s |
| 3 | |
Ted Kremenek | 9f67ede | 2008-10-01 05:05:46 +0000 | [diff] [blame] | 4 | |
| 5 | // Reduced test case from crash in <rdar://problem/6253157> |
| 6 | @class NSObject; |
| 7 | @interface A @end |
| 8 | @implementation A |
| 9 | - (void)foo:(void (^)(NSObject *x))block { |
| 10 | if (!((block != ((void *)0)))) {} |
| 11 | } |
| 12 | @end |
| 13 | |
Ted Kremenek | 6dfe2f5 | 2008-10-18 22:20:20 +0000 | [diff] [blame] | 14 | // Reduced test case from crash in PR 2796; |
| 15 | // http://llvm.org/bugs/show_bug.cgi?id=2796 |
| 16 | |
| 17 | unsigned foo(unsigned x) { return __alignof__((x)) + sizeof(x); } |
Ted Kremenek | 9253b0f | 2008-10-20 23:14:31 +0000 | [diff] [blame] | 18 | |
| 19 | // Improvement to path-sensitivity involving compound assignments. |
| 20 | // Addresses false positive in <rdar://problem/6268365> |
| 21 | // |
| 22 | |
| 23 | unsigned r6268365Aux(); |
| 24 | |
| 25 | void r6268365() { |
| 26 | unsigned x = 0; |
| 27 | x &= r6268365Aux(); |
| 28 | unsigned j = 0; |
| 29 | |
| 30 | if (x == 0) ++j; |
| 31 | if (x == 0) x = x / j; // no-warning |
| 32 | } |
| 33 | |
Ted Kremenek | c13b6e2 | 2008-10-20 23:40:25 +0000 | [diff] [blame] | 34 | void divzeroassume(unsigned x, unsigned j) { |
| 35 | x /= j; |
| 36 | if (j == 0) x /= 0; // no-warning |
| 37 | if (j == 0) x /= j; // no-warning |
| 38 | if (j == 0) x = x / 0; // no-warning |
| 39 | } |
| 40 | |
| 41 | void divzeroassumeB(unsigned x, unsigned j) { |
| 42 | x = x / j; |
| 43 | if (j == 0) x /= 0; // no-warning |
| 44 | if (j == 0) x /= j; // no-warning |
| 45 | if (j == 0) x = x / 0; // no-warning |
| 46 | } |
| 47 | |
Ted Kremenek | 76dba7b | 2008-11-13 05:05:34 +0000 | [diff] [blame] | 48 | // InitListExpr processing |
| 49 | |
| 50 | typedef float __m128 __attribute__((__vector_size__(16), __may_alias__)); |
| 51 | __m128 return128() { |
Ted Kremenek | 062e2f9 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 52 | // This compound literal has a Vector type. We currently just |
| 53 | // return UnknownVal. |
Ted Kremenek | 76dba7b | 2008-11-13 05:05:34 +0000 | [diff] [blame] | 54 | return __extension__(__m128) { 0.0f, 0.0f, 0.0f, 0.0f }; |
| 55 | } |
| 56 | |
Ted Kremenek | 062e2f9 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 57 | typedef long long __v2di __attribute__ ((__vector_size__ (16))); |
| 58 | typedef long long __m128i __attribute__ ((__vector_size__ (16), __may_alias__)); |
| 59 | __m128i vec128i(long long __q1, long long __q0) { |
| 60 | // This compound literal returns true for both isVectorType() and |
| 61 | // isIntegerType(). |
| 62 | return __extension__ (__m128i)(__v2di){ __q0, __q1 }; |
| 63 | } |
| 64 | |
Ted Kremenek | 8322d6a | 2008-12-09 00:14:48 +0000 | [diff] [blame] | 65 | // Zero-sized VLAs. |
| 66 | void check_zero_sized_VLA(int x) { |
| 67 | if (x) |
| 68 | return; |
| 69 | |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 70 | int vla[x]; // expected-warning{{VLAs with no elements have undefined behavior}} |
| 71 | } |
| 72 | |
| 73 | void check_uninit_sized_VLA() { |
| 74 | int x; |
| 75 | 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] | 76 | } |
Ted Kremenek | 062e2f9 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 77 | |
Ted Kremenek | 55f7bcb | 2008-12-15 18:51:00 +0000 | [diff] [blame] | 78 | // sizeof(void) |
| 79 | // - Tests a regression reported in PR 3211: http://llvm.org/bugs/show_bug.cgi?id=3211 |
| 80 | void handle_sizeof_void(unsigned flag) { |
| 81 | int* p = 0; |
| 82 | |
| 83 | if (flag) { |
| 84 | if (sizeof(void) == 1) |
| 85 | return; |
| 86 | // Infeasible. |
| 87 | *p = 1; // no-warning |
| 88 | } |
| 89 | |
| 90 | void* q; |
| 91 | |
| 92 | if (!flag) { |
| 93 | if (sizeof(*q) == 1) |
| 94 | return; |
| 95 | // Infeasibe. |
| 96 | *p = 1; // no-warning |
| 97 | } |
| 98 | |
| 99 | // Infeasible. |
| 100 | *p = 1; // no-warning |
| 101 | } |
| 102 | |
Ted Kremenek | d76d47e | 2009-01-27 18:29:03 +0000 | [diff] [blame] | 103 | // PR 3422 |
| 104 | void pr3422_helper(char *p); |
| 105 | void pr3422() { |
| 106 | char buf[100]; |
| 107 | char *q = &buf[10]; |
| 108 | pr3422_helper(&q[1]); |
| 109 | } |
| 110 | |
Ted Kremenek | a3d1eb8 | 2009-02-14 05:55:08 +0000 | [diff] [blame^] | 111 | // PR 3543 (handle empty statement expressions) |
| 112 | int pr_3543(void) { |
| 113 | ({}); |
| 114 | } |
| 115 | |
| 116 | |