xref: /llvm-project/clang/test/Sema/format-pointer.c (revision 0e24686af49d2f9e50438d3a27db6f1ade594855)
1 // RUN: %clang_cc1 -Wformat %s -verify
2 // RUN: %clang_cc1 -Wformat -std=c23 %s -verify
3 // RUN: %clang_cc1 -xc++ -Wformat %s -verify
4 // RUN: %clang_cc1 -xobjective-c -Wformat -fblocks %s -verify
5 // RUN: %clang_cc1 -xobjective-c++ -Wformat -fblocks %s -verify
6 // RUN: %clang_cc1 -std=c23 -Wformat %s -pedantic -verify=expected,pedantic
7 // RUN: %clang_cc1 -xc++ -Wformat %s -pedantic -verify=expected,pedantic
8 // RUN: %clang_cc1 -xobjective-c -Wformat -fblocks -pedantic %s -verify=expected,pedantic
9 
10 __attribute__((__format__(__printf__, 1, 2)))
11 int printf(const char *, ...);
12 __attribute__((__format__(__scanf__, 1, 2)))
13 int scanf(const char *, ...);
14 
15 void f(void *vp, const void *cvp, char *cp, signed char *scp, int *ip) {
16   int arr[2];
17 
18   printf("%p", cp);
19   printf("%p", cvp);
20   printf("%p", vp);
21   printf("%p", scp);
22   printf("%p", ip); // pedantic-warning {{format specifies type 'void *' but the argument has type 'int *'}}
23   printf("%p", arr); // pedantic-warning {{format specifies type 'void *' but the argument has type 'int *'}}
24 
25   scanf("%p", &vp);
26   scanf("%p", &cvp);
27   scanf("%p", (void *volatile*)&vp);
28   scanf("%p", (const void *volatile*)&cvp);
29   scanf("%p", &cp); // pedantic-warning {{format specifies type 'void **' but the argument has type 'char **'}}
30   scanf("%p", &ip); // pedantic-warning {{format specifies type 'void **' but the argument has type 'int **'}}
31   scanf("%p", &arr); // expected-warning {{format specifies type 'void **' but the argument has type 'int (*)[2]'}}
32 
33 #if !__is_identifier(nullptr)
34   typedef __typeof__(nullptr) nullptr_t;
35   nullptr_t np = nullptr;
36   nullptr_t *npp = &np;
37 
38   printf("%p", np);
39   scanf("%p", &np); // expected-warning {{format specifies type 'void **' but the argument has type 'nullptr_t *'}}
40   scanf("%p", &npp); // pedantic-warning {{format specifies type 'void **' but the argument has type 'nullptr_t **'}}
41 #endif
42 
43 #ifdef __OBJC__
44   id i = 0;
45   void (^b)(void) = ^{};
46 
47   printf("%p", i); // pedantic-warning {{format specifies type 'void *' but the argument has type 'id'}}
48   printf("%p", b); // pedantic-warning {{format specifies type 'void *' but the argument has type 'void (^)(void)'}}
49   scanf("%p", &i); // pedantic-warning {{format specifies type 'void **' but the argument has type 'id *'}}
50   scanf("%p", &b); // pedantic-warning {{format specifies type 'void **' but the argument has type 'void (^*)(void)'}}
51 #endif
52 
53 }
54