Richard Smith | 2815e1a | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -std=gnu++11 -Wsometimes-uninitialized -verify %s |
| 2 | |
| 3 | bool maybe(); |
| 4 | |
| 5 | int test_if_false(bool b) { |
| 6 | int x; // expected-note {{variable}} |
| 7 | if (b) x = 1; // expected-note {{whenever 'if' condition is false}} |
| 8 | return x; // expected-warning {{sometimes uninit}} |
| 9 | } |
| 10 | |
| 11 | int test_if_true(bool b) { |
| 12 | int x; // expected-note {{variable}} |
| 13 | if (b) {} // expected-note {{whenever 'if' condition is true}} |
| 14 | else x = 1; |
| 15 | return x; // expected-warning {{sometimes uninit}} |
| 16 | } |
| 17 | |
| 18 | int test_while_false(bool b) { |
| 19 | int x; // expected-note {{variable}} |
| 20 | while (b) { // expected-note {{whenever 'while' loop exits because its condition is false}} |
| 21 | if (maybe()) { |
| 22 | x = 1; |
| 23 | break; |
| 24 | } |
| 25 | }; |
| 26 | return x; // expected-warning {{sometimes uninit}} |
| 27 | } |
| 28 | |
| 29 | int test_while_true(bool b) { |
| 30 | int x; // expected-note {{variable}} |
| 31 | while (b) { // expected-note {{whenever 'while' loop is entered}} |
| 32 | label: |
| 33 | return x; // expected-warning {{sometimes uninit}} |
| 34 | } |
| 35 | x = 0; |
| 36 | goto label; |
| 37 | } |
| 38 | |
| 39 | int test_do_while_false(bool b) { |
| 40 | int x; // expected-note {{variable}} |
| 41 | do { |
| 42 | if (maybe()) { |
| 43 | x = 1; |
| 44 | break; |
| 45 | } |
| 46 | } while (b); // expected-note {{whenever 'do' loop exits because its condition is false}} |
| 47 | return x; // expected-warning {{sometimes uninit}} |
| 48 | } |
| 49 | |
| 50 | int test_do_while_true(bool b) { |
| 51 | int x; // expected-note {{variable}} |
| 52 | goto label2; |
| 53 | do { |
| 54 | label1: |
| 55 | return x; // expected-warning {{sometimes uninit}} |
| 56 | label2: ; |
| 57 | } while (b); // expected-note {{whenever 'do' loop condition is true}} |
| 58 | x = 0; |
| 59 | goto label1; |
| 60 | } |
| 61 | |
| 62 | int test_for_false(int k) { |
| 63 | int x; // expected-note {{variable}} |
| 64 | for (int n = 0; |
| 65 | n < k; // expected-note {{whenever 'for' loop exits because its condition is false}} |
| 66 | ++n) { |
| 67 | if (maybe()) { |
| 68 | x = n; |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | return x; // expected-warning {{sometimes uninit}} |
| 73 | } |
| 74 | |
| 75 | int test_for_true(int k) { |
| 76 | int x; // expected-note {{variable}} |
| 77 | int n = 0; |
| 78 | for (; |
| 79 | n < k; // expected-note {{whenever 'for' loop is entered}} |
| 80 | ++n) { |
| 81 | label: |
| 82 | return x; // expected-warning {{sometimes uninit}} |
| 83 | } |
| 84 | x = 1; |
| 85 | goto label; |
| 86 | } |
| 87 | |
| 88 | int test_for_range_false(int k) { |
| 89 | int arr[3] = { 1, 2, 3 }; |
| 90 | int x; // expected-note {{variable}} |
| 91 | for (int &a : arr) { // expected-note {{whenever 'for' loop exits because its condition is false}} |
| 92 | if (a == k) { |
| 93 | x = &a - arr; |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | return x; // expected-warning {{sometimes uninit}} |
| 98 | } |
| 99 | |
| 100 | int test_for_range_true(int k) { |
| 101 | int arr[3] = { 1, 2, 3 }; |
| 102 | int x; // expected-note {{variable}} |
| 103 | for (int &a : arr) { // expected-note {{whenever 'for' loop is entered}} |
| 104 | goto label; |
| 105 | } |
| 106 | x = 0; |
| 107 | label: |
| 108 | return x; // expected-warning {{sometimes uninit}} |
| 109 | } |
| 110 | |
| 111 | int test_conditional_false(int k) { |
| 112 | int x; // expected-note {{variable}} |
| 113 | (void)( |
| 114 | maybe() // expected-note {{whenever '?:' condition is false}} |
| 115 | ? x = 1 : 0); |
| 116 | return x; // expected-warning {{sometimes uninit}} |
| 117 | } |
| 118 | |
| 119 | int test_conditional_true(int k) { |
| 120 | int x; // expected-note {{variable}} |
| 121 | (void)( |
| 122 | maybe() // expected-note {{whenever '?:' condition is true}} |
| 123 | ? 0 : x = 1); |
| 124 | return x; // expected-warning {{sometimes uninit}} |
| 125 | } |
| 126 | |
| 127 | int test_logical_and_false(int k) { |
| 128 | int x; // expected-note {{variable}} |
| 129 | maybe() // expected-note {{whenever '&&' condition is false}} |
| 130 | && (x = 1); |
| 131 | return x; // expected-warning {{sometimes uninit}} |
| 132 | } |
| 133 | |
| 134 | int test_logical_and_true(int k) { |
| 135 | int x; // expected-note {{variable}} |
| 136 | maybe() // expected-note {{whenever '&&' condition is true}} |
| 137 | && ({ goto skip_init; 0; }); |
| 138 | x = 1; |
| 139 | skip_init: |
| 140 | return x; // expected-warning {{sometimes uninit}} |
| 141 | } |
| 142 | |
| 143 | int test_logical_or_false(int k) { |
| 144 | int x; // expected-note {{variable}} |
| 145 | maybe() // expected-note {{whenever '||' condition is false}} |
| 146 | || ({ goto skip_init; 0; }); |
| 147 | x = 1; |
| 148 | skip_init: |
| 149 | return x; // expected-warning {{sometimes uninit}} |
| 150 | } |
| 151 | |
| 152 | int test_logical_or_true(int k) { |
| 153 | int x; // expected-note {{variable}} |
| 154 | maybe() // expected-note {{whenever '||' condition is true}} |
| 155 | || (x = 1); |
| 156 | return x; // expected-warning {{sometimes uninit}} |
| 157 | } |
| 158 | |
| 159 | int test_switch_case(int k) { |
| 160 | int x; // expected-note {{variable}} |
| 161 | switch (k) { |
| 162 | case 0: |
| 163 | x = 0; |
| 164 | break; |
| 165 | case 1: // expected-note {{whenever switch case is taken}} |
| 166 | break; |
| 167 | } |
| 168 | return x; // expected-warning {{sometimes uninit}} |
| 169 | } |
| 170 | |
| 171 | int test_switch_default(int k) { |
| 172 | int x; // expected-note {{variable}} |
| 173 | switch (k) { |
| 174 | case 0: |
| 175 | x = 0; |
| 176 | break; |
| 177 | case 1: |
| 178 | x = 1; |
| 179 | break; |
| 180 | default: // expected-note {{whenever switch default is taken}} |
| 181 | break; |
| 182 | } |
| 183 | return x; // expected-warning {{sometimes uninit}} |
| 184 | } |
| 185 | |
| 186 | int test_switch_suppress_1(int k) { |
| 187 | int x; |
| 188 | switch (k) { |
| 189 | case 0: |
| 190 | x = 0; |
| 191 | break; |
| 192 | case 1: |
| 193 | x = 1; |
| 194 | break; |
| 195 | } |
| 196 | return x; // no-warning |
| 197 | } |
| 198 | |
| 199 | int test_switch_suppress_2(int k) { |
| 200 | int x; |
| 201 | switch (k) { |
| 202 | case 0: |
| 203 | case 1: |
| 204 | switch (k) { |
| 205 | case 0: |
| 206 | return 0; |
| 207 | case 1: |
| 208 | return 1; |
| 209 | } |
| 210 | case 2: |
| 211 | case 3: |
| 212 | x = 1; |
| 213 | } |
| 214 | return x; // no-warning |
| 215 | } |
| 216 | |
| 217 | int test_multiple_notes(int k) { |
| 218 | int x; // expected-note {{variable}} |
| 219 | if (k > 0) { |
| 220 | if (k == 5) |
| 221 | x = 1; |
| 222 | else if (k == 2) // expected-note {{whenever 'if' condition is false}} |
| 223 | x = 2; |
| 224 | } else { |
| 225 | if (k == -5) |
| 226 | x = 3; |
| 227 | else if (k == -2) // expected-note {{whenever 'if' condition is false}} |
| 228 | x = 4; |
| 229 | } |
| 230 | return x; // expected-warning {{sometimes uninit}} |
| 231 | } |
| 232 | |
| 233 | int test_no_false_positive_1(int k) { |
| 234 | int x; |
| 235 | if (k) |
| 236 | x = 5; |
| 237 | while (!k) |
| 238 | maybe(); |
| 239 | return x; |
| 240 | } |
| 241 | |
| 242 | int test_no_false_positive_2() { |
| 243 | int x; |
| 244 | bool b = false; |
| 245 | if (maybe()) { |
| 246 | x = 5; |
| 247 | b = true; |
| 248 | } |
| 249 | return b ? x : 0; |
| 250 | } |
| 251 | |
| 252 | void test_null_pred_succ() { |
| 253 | int x; // expected-note {{variable}} |
| 254 | if (0) // expected-note {{whenever}} |
| 255 | foo: x = 0; |
| 256 | if (x) // expected-warning {{sometimes uninit}} |
| 257 | goto foo; |
| 258 | } |