Jordan Rose | 1acb394 | 2013-05-29 20:50:34 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -disable-free -analyzer-eagerly-assume -analyzer-checker=core,deadcode,debug.ExprInspection -verify %s |
| 2 | |
| 3 | void clang_analyzer_eval(int); |
Ted Kremenek | e5cfd52 | 2011-05-25 23:57:29 +0000 | [diff] [blame] | 4 | |
Ted Kremenek | e5cfd52 | 2011-05-25 23:57:29 +0000 | [diff] [blame] | 5 | int size_rdar9373039 = 1; |
Ted Kremenek | e5cfd52 | 2011-05-25 23:57:29 +0000 | [diff] [blame] | 6 | int foo_rdar9373039(const char *); |
| 7 | |
| 8 | int rdar93730392() { |
| 9 | int x; |
| 10 | int j = 0; |
| 11 | |
| 12 | for (int i = 0 ; i < size_rdar9373039 ; ++i) |
| 13 | x = 1; |
| 14 | |
| 15 | int extra = (2 + foo_rdar9373039 ("Clang") + ((4 - ((unsigned int) (2 + foo_rdar9373039 ("Clang")) % 4)) % 4)) + (2 + foo_rdar9373039 ("1.0") + ((4 - ((unsigned int) (2 + foo_rdar9373039 ("1.0")) % 4)) % 4)); // expected-warning {{never read}} |
| 16 | |
| 17 | for (int i = 0 ; i < size_rdar9373039 ; ++i) |
| 18 | j += x; // expected-warning {{garbage}} |
| 19 | |
| 20 | return j; |
| 21 | } |
| 22 | |
Jordy Rose | 22043b5 | 2011-06-08 22:47:39 +0000 | [diff] [blame] | 23 | |
| 24 | int PR8962 (int *t) { |
| 25 | // This should look through the __extension__ no-op. |
| 26 | if (__extension__ (t)) return 0; |
| 27 | return *t; // expected-warning {{null pointer}} |
| 28 | } |
| 29 | |
| 30 | int PR8962_b (int *t) { |
| 31 | // This should still ignore the nested casts |
| 32 | // which aren't handled by a single IgnoreParens() |
| 33 | if (((int)((int)t))) return 0; |
| 34 | return *t; // expected-warning {{null pointer}} |
| 35 | } |
| 36 | |
Jordy Rose | 7fead31 | 2011-06-09 05:44:04 +0000 | [diff] [blame] | 37 | int PR8962_c (int *t) { |
| 38 | // If the last element in a StmtExpr was a ParenExpr, it's still live |
| 39 | if (({ (t ? (_Bool)0 : (_Bool)1); })) return 0; |
| 40 | return *t; // no-warning |
| 41 | } |
| 42 | |
| 43 | int PR8962_d (int *t) { |
| 44 | // If the last element in a StmtExpr is an __extension__, it's still live |
| 45 | if (({ __extension__(t ? (_Bool)0 : (_Bool)1); })) return 0; |
| 46 | return *t; // no-warning |
| 47 | } |
| 48 | |
Jordy Rose | ac73ea8 | 2011-06-10 08:49:37 +0000 | [diff] [blame] | 49 | int PR8962_e (int *t) { |
| 50 | // Redundant casts can mess things up! |
| 51 | // Environment used to skip through NoOp casts, but LiveVariables didn't! |
| 52 | if (({ (t ? (int)(int)0L : (int)(int)1L); })) return 0; |
| 53 | return *t; // no-warning |
| 54 | } |
| 55 | |
| 56 | int PR8962_f (int *t) { |
| 57 | // The StmtExpr isn't a block-level expression here, |
| 58 | // the __extension__ is. But the value should be attached to the StmtExpr |
| 59 | // anyway. Make sure the block-level check is /before/ IgnoreParens. |
| 60 | if ( __extension__({ |
| 61 | _Bool r; |
| 62 | if (t) r = 0; |
| 63 | else r = 1; |
| 64 | r; |
| 65 | }) ) return 0; |
| 66 | return *t; // no-warning |
| 67 | } |
Ted Kremenek | beedc5f | 2011-10-20 19:33:06 +0000 | [diff] [blame] | 68 | |
| 69 | // This previously crashed logic in the analyzer engine when evaluating locations. |
| 70 | void rdar10308201_aux(unsigned val); |
| 71 | void rdar10308201 (int valA, void *valB, unsigned valC) { |
| 72 | unsigned actual_base, lines; |
| 73 | if (valC == 0) { |
| 74 | actual_base = (unsigned)valB; |
| 75 | for (;;) { |
| 76 | if (valA & (1<<0)) |
| 77 | rdar10308201_aux(actual_base); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
Anna Zaks | cdcc653 | 2011-11-01 22:41:06 +0000 | [diff] [blame] | 82 | typedef struct Struct103 { |
| 83 | unsigned i; |
| 84 | } Struct103; |
| 85 | typedef unsigned int size_t; |
| 86 | void __my_memset_chk(char*, int, size_t); |
| 87 | static int radar10367606(int t) { |
| 88 | Struct103 overall; |
| 89 | ((__builtin_object_size ((char *) &overall, 0) != (size_t) -1) ? __builtin___memset_chk ((char *) &overall, 0, sizeof(Struct103), __builtin_object_size ((char *) &overall, 0)) : __my_memset_chk ((char *) &overall, 0, sizeof(Struct103))); |
| 90 | return 0; |
| 91 | } |
Ted Kremenek | beedc5f | 2011-10-20 19:33:06 +0000 | [diff] [blame] | 92 | |
Anna Zaks | 2d950b1 | 2011-11-01 22:41:14 +0000 | [diff] [blame] | 93 | /* Caching out on a sink node. */ |
| 94 | extern int fooR10376675(); |
| 95 | extern int* bazR10376675(); |
| 96 | extern int nR10376675; |
| 97 | void barR10376675(int *x) { |
| 98 | int *pm; |
| 99 | if (nR10376675 * 2) { |
| 100 | int *pk = bazR10376675(); |
| 101 | pm = pk; //expected-warning {{never read}} |
| 102 | } |
| 103 | do { |
| 104 | *x = fooR10376675(); |
| 105 | } while (0); |
| 106 | } |
Ted Kremenek | 729aa06 | 2011-11-14 20:05:54 +0000 | [diff] [blame] | 107 | |
| 108 | // Test accesses to wide character strings doesn't break the analyzer. |
| 109 | typedef int wchar_t; |
| 110 | struct rdar10385775 { |
| 111 | wchar_t *name; |
| 112 | }; |
| 113 | void RDar10385775(struct rdar10385775* p) { |
| 114 | p->name = L"a"; |
| 115 | } |
Ted Kremenek | 7f9b1d9 | 2012-02-21 00:46:29 +0000 | [diff] [blame] | 116 | |
| 117 | // Test double loop of array and array literals. Previously this |
| 118 | // resulted in a false positive uninitailized value warning. |
| 119 | void rdar10686586() { |
| 120 | int array1[] = { 1, 2, 3, 0 }; |
| 121 | int array2[] = { 1, 2, 3, 0 }; |
| 122 | int *array[] = { array1, array2 }; |
| 123 | int sum = 0; |
| 124 | for (int i = 0; i < 2; i++) { |
| 125 | for (int j = 0; j < 4; j++) { |
| 126 | sum += array[i][j]; // no-warning |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
Ted Kremenek | 469841a | 2012-07-25 21:58:25 +0000 | [diff] [blame] | 131 | // This example tests CFG handling of '||' nested in a ternary expression, |
| 132 | // and seeing that the analyzer doesn't crash. |
| 133 | int isctype(char c, unsigned long f) |
| 134 | { |
| 135 | return (c < 1 || c > 10) ? 0 : !!(c & f); |
| 136 | } |
| 137 | |
Ted Kremenek | c957319 | 2012-09-07 19:09:51 +0000 | [diff] [blame] | 138 | // Test that symbolic array offsets are modeled conservatively. |
| 139 | // This was triggering a false "use of uninitialized value" warning. |
| 140 | void rdar_12075238__aux(unsigned long y); |
| 141 | int rdar_12075238_(unsigned long count) { |
| 142 | if ((count < 3) || (count > 6)) |
| 143 | return 0; |
| 144 | |
| 145 | unsigned long array[6]; |
| 146 | unsigned long i = 0; |
| 147 | for (; i <= count - 2; i++) |
| 148 | { |
| 149 | array[i] = i; |
| 150 | } |
| 151 | array[count - 1] = i; |
| 152 | rdar_12075238__aux(array[2]); // no-warning |
| 153 | return 0; |
| 154 | } |
| 155 | |
Ted Kremenek | 707a865 | 2013-01-11 22:35:39 +0000 | [diff] [blame] | 156 | // Test that we handle an uninitialized value within a logical expression. |
| 157 | void PR14635(int *p) { |
| 158 | int a = 0, b; |
| 159 | *p = a || b; // expected-warning {{Assigned value is garbage or undefined}} |
| 160 | } |
| 161 | |
Ted Kremenek | 9195caf | 2013-01-11 23:36:25 +0000 | [diff] [blame] | 162 | // Test handling floating point values with unary '!'. |
| 163 | int PR14634(int x) { |
| 164 | double y = (double)x; |
| 165 | return !y; |
| 166 | } |
| 167 | |
Jordan Rose | 3e5ebf1 | 2013-04-06 01:42:02 +0000 | [diff] [blame] | 168 | |
| 169 | // PR15684: If a checker generates a sink node after generating a regular node |
| 170 | // and no state changes between the two, graph trimming would consider the two |
| 171 | // the same node, forming a loop. |
| 172 | struct PR15684 { |
| 173 | void (*callback)(int); |
| 174 | }; |
| 175 | void sinkAfterRegularNode(struct PR15684 *context) { |
| 176 | int uninitialized; |
| 177 | context->callback(uninitialized); // expected-warning {{uninitialized}} |
| 178 | } |
| 179 | |
Jordan Rose | 1acb394 | 2013-05-29 20:50:34 +0000 | [diff] [blame] | 180 | |
| 181 | // PR16131: C permits variables to be declared extern void. |
| 182 | static void PR16131(int x) { |
| 183 | extern void v; |
| 184 | |
| 185 | int *ip = (int *)&v; |
| 186 | char *cp = (char *)&v; |
| 187 | clang_analyzer_eval(ip == cp); // expected-warning{{TRUE}} |
| 188 | // expected-warning@-1 {{comparison of distinct pointer types}} |
| 189 | |
| 190 | *ip = 42; |
| 191 | clang_analyzer_eval(*ip == 42); // expected-warning{{TRUE}} |
| 192 | clang_analyzer_eval(*(int *)&v == 42); // expected-warning{{TRUE}} |
| 193 | } |