1f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// RUN: %clang_cc1 -verify -Wno-string-plus-int -Warray-bounds-pointer-arithmetic %s
2f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
3f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void swallow (const char *x) { (void)x; }
4f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void test_pointer_arithmetic(int n) {
5f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const char hello[] = "Hello world!"; // expected-note 2 {{declared here}}
6f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const char *helloptr = hello;
7a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
8a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  swallow("Hello world!" + 6); // no-warning
9f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  swallow("Hello world!" - 6); // expected-warning {{refers before the beginning of the array}}
10f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  swallow("Hello world!" + 14); // expected-warning {{refers past the end of the array}}
11f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  swallow("Hello world!" + 13); // no-warning
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  swallow(hello + 6); // no-warning
14f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  swallow(hello - 6); // expected-warning {{refers before the beginning of the array}}
15f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  swallow(hello + 14); // expected-warning {{refers past the end of the array}}
16f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  swallow(hello + 13); // no-warning
1723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
18f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  swallow(helloptr + 6); // no-warning
19f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  swallow(helloptr - 6); // no-warning
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  swallow(helloptr + 14); // no-warning
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  swallow(helloptr + 13); // no-warning
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  double numbers[2]; // expected-note {{declared here}}
246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  swallow((char*)numbers + sizeof(double)); // no-warning
256e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  swallow((char*)numbers + 60); // expected-warning {{refers past the end of the array}}
266e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
27f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  char buffer[5]; // expected-note 2 {{declared here}}
28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // TODO: Add FixIt notes for adding parens around non-ptr part of arith expr
29f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  swallow(buffer + sizeof("Hello")-1); // expected-warning {{refers past the end of the array}}
30f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  swallow(buffer + (sizeof("Hello")-1)); // no-warning
31f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (n > 0 && n <= 6) swallow(buffer + 6 - n); // expected-warning {{refers past the end of the array}}
32f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (n > 0 && n <= 6) swallow(buffer + (6 - n)); // no-warning
33f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
34116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch