11cb2d742eb6635aeab6132ee5f0b5781d39487d7Nico Weber// RUN: %clang_cc1 -verify -Wno-string-plus-int -Warray-bounds-pointer-arithmetic %s
2cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain
3cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrainvoid swallow (const char *x) { (void)x; }
4cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrainvoid test_pointer_arithmetic(int n) {
5cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain  const char hello[] = "Hello world!"; // expected-note 2 {{declared here}}
6cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain  const char *helloptr = hello;
7cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain
8cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain  swallow("Hello world!" + 6); // no-warning
9cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain  swallow("Hello world!" - 6); // expected-warning {{refers before the beginning of the array}}
10cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain  swallow("Hello world!" + 14); // expected-warning {{refers past the end of the array}}
11cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain  swallow("Hello world!" + 13); // no-warning
12cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain
13cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain  swallow(hello + 6); // no-warning
14cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain  swallow(hello - 6); // expected-warning {{refers before the beginning of the array}}
15cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain  swallow(hello + 14); // expected-warning {{refers past the end of the array}}
16cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain  swallow(hello + 13); // no-warning
17cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain
18cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain  swallow(helloptr + 6); // no-warning
19cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain  swallow(helloptr - 6); // no-warning
20cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain  swallow(helloptr + 14); // no-warning
21cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain  swallow(helloptr + 13); // no-warning
22cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain
23cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain  double numbers[2]; // expected-note {{declared here}}
24cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain  swallow((char*)numbers + sizeof(double)); // no-warning
25cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain  swallow((char*)numbers + 60); // expected-warning {{refers past the end of the array}}
26cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain
27cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain  char buffer[5]; // expected-note 2 {{declared here}}
28cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain  // TODO: Add FixIt notes for adding parens around non-ptr part of arith expr
29cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain  swallow(buffer + sizeof("Hello")-1); // expected-warning {{refers past the end of the array}}
30cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain  swallow(buffer + (sizeof("Hello")-1)); // no-warning
31cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain  if (n > 0 && n <= 6) swallow(buffer + 6 - n); // expected-warning {{refers past the end of the array}}
32cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain  if (n > 0 && n <= 6) swallow(buffer + (6 - n)); // no-warning
33cd9ae488ede3e26e65fe173ae90b489ce45c8b6aKaelyn Uhrain}
34