xref: /llvm-project/clang/test/Sema/pointer-addition.c (revision c3fe8ddd8385832fde9f16a261908afbaf9878a4)
1 // RUN: %clang_cc1 %s -fsyntax-only -verify=gnu,expected -pedantic -Wextra -std=c11
2 // RUN: %clang_cc1 %s -fsyntax-only -triple i686-unknown-unknown -verify=gnu,expected -pedantic -Wextra -std=c11
3 // RUN: %clang_cc1 %s -fsyntax-only -triple x86_64-unknown-unknown -verify=gnu,expected -pedantic -Wextra -std=c11
4 // RUN: %clang_cc1 %s -fsyntax-only -verify -pedantic -Wextra -Wno-gnu -std=c11
5 
6 #include <stdint.h>
7 
8 typedef struct S S; // expected-note 4 {{forward declaration of 'struct S'}}
9 extern _Atomic(S*) e;
a(S * b,void * c)10 void a(S* b, void* c) {
11   void (*fp)(int) = 0;
12   b++;       // expected-error {{arithmetic on a pointer to an incomplete type}}
13   b += 1;    // expected-error {{arithmetic on a pointer to an incomplete type}}
14   c++;       // gnu-warning {{arithmetic on a pointer to void is a GNU extension}}
15   c += 1;    // gnu-warning {{arithmetic on a pointer to void is a GNU extension}}
16   c--;       // gnu-warning {{arithmetic on a pointer to void is a GNU extension}}
17   c -= 1;    // gnu-warning {{arithmetic on a pointer to void is a GNU extension}}
18   (void) c[1]; // gnu-warning {{subscript of a pointer to void is a GNU extension}}
19   b = 1+b;   // expected-error {{arithmetic on a pointer to an incomplete type}}
20   /* The next couple tests are only pedantic warnings in gcc */
21   void (*d)(S*,void*) = a;
22   d += 1;    // gnu-warning {{arithmetic on a pointer to the function type 'void (S *, void *)' (aka 'void (struct S *, void *)') is a GNU extension}}
23   d++;       // gnu-warning {{arithmetic on a pointer to the function type 'void (S *, void *)' (aka 'void (struct S *, void *)') is a GNU extension}}
24   d--;       // gnu-warning {{arithmetic on a pointer to the function type 'void (S *, void *)' (aka 'void (struct S *, void *)') is a GNU extension}}
25   d -= 1;    // gnu-warning {{arithmetic on a pointer to the function type 'void (S *, void *)' (aka 'void (struct S *, void *)') is a GNU extension}}
26   (void)(1 + d); // gnu-warning {{arithmetic on a pointer to the function type 'void (S *, void *)' (aka 'void (struct S *, void *)') is a GNU extension}}
27   e++;       // expected-error {{arithmetic on a pointer to an incomplete type}}
28   intptr_t i = (intptr_t)b;
29   char *f = (char*)0 + i; // gnu-warning {{arithmetic on a null pointer treated as a cast from integer to pointer is a GNU extension}}
30   // Cases that don't match the GNU inttoptr idiom get a different warning.
31   f = (char*)0 - i; // expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}}
32   int *g = (int*)0 + i; // expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}}
33 }
34