File: format-pointer.c

package info (click to toggle)
llvm-toolchain-20 1%3A20.1.6-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,111,304 kB
  • sloc: cpp: 7,438,677; ansic: 1,393,822; asm: 1,012,926; python: 241,650; f90: 86,635; objc: 75,479; lisp: 42,144; pascal: 17,286; sh: 10,027; ml: 5,082; perl: 4,730; awk: 3,523; makefile: 3,349; javascript: 2,251; xml: 892; fortran: 672
file content (53 lines) | stat: -rw-r--r-- 2,400 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// RUN: %clang_cc1 -Wformat %s -verify
// RUN: %clang_cc1 -Wformat -std=c23 %s -verify
// RUN: %clang_cc1 -xc++ -Wformat %s -verify
// RUN: %clang_cc1 -xobjective-c -Wformat -fblocks %s -verify
// RUN: %clang_cc1 -xobjective-c++ -Wformat -fblocks %s -verify
// RUN: %clang_cc1 -std=c23 -Wformat %s -pedantic -verify=expected,pedantic
// RUN: %clang_cc1 -xc++ -Wformat %s -pedantic -verify=expected,pedantic
// RUN: %clang_cc1 -xobjective-c -Wformat -fblocks -pedantic %s -verify=expected,pedantic

__attribute__((__format__(__printf__, 1, 2)))
int printf(const char *, ...);
__attribute__((__format__(__scanf__, 1, 2)))
int scanf(const char *, ...);

void f(void *vp, const void *cvp, char *cp, signed char *scp, int *ip) {
  int arr[2];

  printf("%p", cp);
  printf("%p", cvp);
  printf("%p", vp);
  printf("%p", scp);
  printf("%p", ip); // pedantic-warning {{format specifies type 'void *' but the argument has type 'int *'}}
  printf("%p", arr); // pedantic-warning {{format specifies type 'void *' but the argument has type 'int *'}}

  scanf("%p", &vp);
  scanf("%p", &cvp);
  scanf("%p", (void *volatile*)&vp);
  scanf("%p", (const void *volatile*)&cvp);
  scanf("%p", &cp); // pedantic-warning {{format specifies type 'void **' but the argument has type 'char **'}}
  scanf("%p", &ip); // pedantic-warning {{format specifies type 'void **' but the argument has type 'int **'}}
  scanf("%p", &arr); // expected-warning {{format specifies type 'void **' but the argument has type 'int (*)[2]'}}

#if !__is_identifier(nullptr)
  typedef __typeof__(nullptr) nullptr_t;
  nullptr_t np = nullptr;
  nullptr_t *npp = &np;

  printf("%p", np);
  scanf("%p", &np); // expected-warning {{format specifies type 'void **' but the argument has type 'nullptr_t *'}}
  scanf("%p", &npp); // pedantic-warning {{format specifies type 'void **' but the argument has type 'nullptr_t **'}}
#endif

#ifdef __OBJC__
  id i = 0;
  void (^b)(void) = ^{};

  printf("%p", i); // pedantic-warning {{format specifies type 'void *' but the argument has type 'id'}}
  printf("%p", b); // pedantic-warning {{format specifies type 'void *' but the argument has type 'void (^)(void)'}}
  scanf("%p", &i); // pedantic-warning {{format specifies type 'void **' but the argument has type 'id *'}}
  scanf("%p", &b); // pedantic-warning {{format specifies type 'void **' but the argument has type 'void (^*)(void)'}}
#endif

}