1*ccec40d9SNico Weber // RUN: %clang_cc1 -verify -Wno-string-plus-int -Warray-bounds-pointer-arithmetic %s
23b74063aSKaelyn Uhrain 
swallow(const char * x)33b74063aSKaelyn Uhrain void swallow (const char *x) { (void)x; }
test_pointer_arithmetic(int n)43b74063aSKaelyn Uhrain void test_pointer_arithmetic(int n) {
53b74063aSKaelyn Uhrain   const char hello[] = "Hello world!"; // expected-note 2 {{declared here}}
63b74063aSKaelyn Uhrain   const char *helloptr = hello;
73b74063aSKaelyn Uhrain 
83b74063aSKaelyn Uhrain   swallow("Hello world!" + 6); // no-warning
93b74063aSKaelyn Uhrain   swallow("Hello world!" - 6); // expected-warning {{refers before the beginning of the array}}
103b74063aSKaelyn Uhrain   swallow("Hello world!" + 14); // expected-warning {{refers past the end of the array}}
113b74063aSKaelyn Uhrain   swallow("Hello world!" + 13); // no-warning
123b74063aSKaelyn Uhrain 
133b74063aSKaelyn Uhrain   swallow(hello + 6); // no-warning
143b74063aSKaelyn Uhrain   swallow(hello - 6); // expected-warning {{refers before the beginning of the array}}
153b74063aSKaelyn Uhrain   swallow(hello + 14); // expected-warning {{refers past the end of the array}}
163b74063aSKaelyn Uhrain   swallow(hello + 13); // no-warning
173b74063aSKaelyn Uhrain 
183b74063aSKaelyn Uhrain   swallow(helloptr + 6); // no-warning
193b74063aSKaelyn Uhrain   swallow(helloptr - 6); // no-warning
203b74063aSKaelyn Uhrain   swallow(helloptr + 14); // no-warning
213b74063aSKaelyn Uhrain   swallow(helloptr + 13); // no-warning
223b74063aSKaelyn Uhrain 
233b74063aSKaelyn Uhrain   double numbers[2]; // expected-note {{declared here}}
243b74063aSKaelyn Uhrain   swallow((char*)numbers + sizeof(double)); // no-warning
253b74063aSKaelyn Uhrain   swallow((char*)numbers + 60); // expected-warning {{refers past the end of the array}}
263b74063aSKaelyn Uhrain 
273b74063aSKaelyn Uhrain   char buffer[5]; // expected-note 2 {{declared here}}
283b74063aSKaelyn Uhrain   // TODO: Add FixIt notes for adding parens around non-ptr part of arith expr
293b74063aSKaelyn Uhrain   swallow(buffer + sizeof("Hello")-1); // expected-warning {{refers past the end of the array}}
303b74063aSKaelyn Uhrain   swallow(buffer + (sizeof("Hello")-1)); // no-warning
313b74063aSKaelyn Uhrain   if (n > 0 && n <= 6) swallow(buffer + 6 - n); // expected-warning {{refers past the end of the array}}
323b74063aSKaelyn Uhrain   if (n > 0 && n <= 6) swallow(buffer + (6 - n)); // no-warning
333b74063aSKaelyn Uhrain }
34