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