annotate clang/test/Sema/attr-format.c @ 236:c4bab56944e8 llvm-original

LLVM 16
author kono
date Wed, 09 Nov 2022 17:45:10 +0900
parents 1d019706d866
children 1f2b6ac9f198
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //RUN: %clang_cc1 -fsyntax-only -verify %s
anatofuz
parents:
diff changeset
2
anatofuz
parents:
diff changeset
3 #include <stdarg.h>
anatofuz
parents:
diff changeset
4
236
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
5 void a(const char *a, ...) __attribute__((format(printf, 1, 2))); // no-error
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
6 void b(const char *a, ...) __attribute__((format(printf, 1, 1))); // expected-error {{'format' attribute parameter 3 is out of bounds}}
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
7 void c(const char *a, ...) __attribute__((format(printf, 0, 2))); // expected-error {{'format' attribute parameter 2 is out of bounds}}
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
8 void d(const char *a, int c) __attribute__((format(printf, 1, 2))); // expected-warning {{GCC requires a function with the 'format' attribute to be variadic}}
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
9 void e(char *str, int c, ...) __attribute__((format(printf, 2, 3))); // expected-error {{format argument not a string type}}
150
anatofuz
parents:
diff changeset
10
236
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
11 typedef const char *xpto;
150
anatofuz
parents:
diff changeset
12 void f(xpto c, va_list list) __attribute__((format(printf, 1, 0))); // no-error
236
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
13 void g(xpto c) __attribute__((format(printf, 1, 0))); // no-error
150
anatofuz
parents:
diff changeset
14
236
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
15 void y(char *str) __attribute__((format(strftime, 1, 0))); // no-error
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
16 void z(char *str, int c, ...) __attribute__((format(strftime, 1, 2))); // expected-error {{strftime format attribute requires 3rd parameter to be 0}}
150
anatofuz
parents:
diff changeset
17
anatofuz
parents:
diff changeset
18 int (*f_ptr)(char*,...) __attribute__((format(printf, 1,2))); // no-error
anatofuz
parents:
diff changeset
19 int (*f2_ptr)(double,...) __attribute__((format(printf, 1, 2))); // expected-error {{format argument not a string type}}
anatofuz
parents:
diff changeset
20
anatofuz
parents:
diff changeset
21 struct _mystruct {
anatofuz
parents:
diff changeset
22 int (*printf)(const char *format, ...) __attribute__((__format__(printf, 1, 2))); // no-error
anatofuz
parents:
diff changeset
23 int (*printf2)(double format, ...) __attribute__((__format__(printf, 1, 2))); // expected-error {{format argument not a string type}}
anatofuz
parents:
diff changeset
24 };
anatofuz
parents:
diff changeset
25
anatofuz
parents:
diff changeset
26 typedef int (*f3_ptr)(char*,...) __attribute__((format(printf,1,0))); // no-error
anatofuz
parents:
diff changeset
27
anatofuz
parents:
diff changeset
28 // <rdar://problem/6623513>
anatofuz
parents:
diff changeset
29 int rdar6623513(void *, const char*, const char*, ...)
anatofuz
parents:
diff changeset
30 __attribute__ ((format (printf, 3, 0)));
anatofuz
parents:
diff changeset
31
anatofuz
parents:
diff changeset
32 int rdar6623513_aux(int len, const char* s) {
anatofuz
parents:
diff changeset
33 rdar6623513(0, "hello", "%.*s", len, s);
anatofuz
parents:
diff changeset
34 }
anatofuz
parents:
diff changeset
35
anatofuz
parents:
diff changeset
36 // same as format(printf(...))...
236
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
37 void a2(const char *a, ...) __attribute__((format(printf0, 1, 2))); // no-error
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
38 void b2(const char *a, ...) __attribute__((format(printf0, 1, 1))); // expected-error {{'format' attribute parameter 3 is out of bounds}}
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
39 void c2(const char *a, ...) __attribute__((format(printf0, 0, 2))); // expected-error {{'format' attribute parameter 2 is out of bounds}}
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
40 void d2(const char *a, int c) __attribute__((format(printf0, 1, 2))); // expected-warning {{GCC requires a function with the 'format' attribute to be variadic}}
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
41 void e2(char *str, int c, ...) __attribute__((format(printf0, 2, 3))); // expected-error {{format argument not a string type}}
150
anatofuz
parents:
diff changeset
42
anatofuz
parents:
diff changeset
43 // FreeBSD usage
236
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
44 #define __printf0like(fmt, va) __attribute__((__format__(__printf0__, fmt, va)))
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
45 void null(int i, const char *a, ...) __printf0like(2, 0); // no-error
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
46 void null(int i, const char *a, ...) { // expected-note{{passing argument to parameter 'a' here}}
150
anatofuz
parents:
diff changeset
47 if (a)
anatofuz
parents:
diff changeset
48 (void)0/* vprintf(...) would go here */;
anatofuz
parents:
diff changeset
49 }
anatofuz
parents:
diff changeset
50
anatofuz
parents:
diff changeset
51 void callnull(void){
anatofuz
parents:
diff changeset
52 null(0, 0); // no error
anatofuz
parents:
diff changeset
53 null(0, (char*)0); // no error
anatofuz
parents:
diff changeset
54 null(0, (void*)0); // no error
anatofuz
parents:
diff changeset
55 null(0, (int*)0); // expected-warning {{incompatible pointer types}}
anatofuz
parents:
diff changeset
56 }
anatofuz
parents:
diff changeset
57
anatofuz
parents:
diff changeset
58 // FreeBSD kernel extensions
236
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
59 void a3(const char *a, ...) __attribute__((format(freebsd_kprintf, 1, 2))); // no-error
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
60 void b3(const char *a, ...) __attribute__((format(freebsd_kprintf, 1, 1))); // expected-error {{'format' attribute parameter 3 is out of bounds}}
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
61 void c3(const char *a, ...) __attribute__((format(freebsd_kprintf, 0, 2))); // expected-error {{'format' attribute parameter 2 is out of bounds}}
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
62 void d3(const char *a, int c) __attribute__((format(freebsd_kprintf, 1, 2))); // expected-warning {{GCC requires a function with the 'format' attribute to be variadic}}
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
63 void e3(char *str, int c, ...) __attribute__((format(freebsd_kprintf, 2, 3))); // expected-error {{format argument not a string type}}
150
anatofuz
parents:
diff changeset
64
anatofuz
parents:
diff changeset
65 // PR4470
anatofuz
parents:
diff changeset
66 int xx_vprintf(const char *, va_list);
anatofuz
parents:
diff changeset
67
anatofuz
parents:
diff changeset
68 const char *foo(const char *format) __attribute__((format_arg(1)));
anatofuz
parents:
diff changeset
69
anatofuz
parents:
diff changeset
70 void __attribute__((format(printf, 1, 0)))
anatofuz
parents:
diff changeset
71 foo2(const char *fmt, va_list va) {
anatofuz
parents:
diff changeset
72 xx_vprintf(foo(fmt), va);
anatofuz
parents:
diff changeset
73 }
anatofuz
parents:
diff changeset
74
anatofuz
parents:
diff changeset
75 // PR6542
236
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
76 extern void gcc_format(const char *, ...)
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
77 __attribute__((__format__(__gcc_diag__, 1, 2)));
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
78 extern void gcc_cformat(const char *, ...)
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
79 __attribute__((__format__(__gcc_cdiag__, 1, 2)));
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
80 extern void gcc_cxxformat(const char *, ...)
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
81 __attribute__((__format__(__gcc_cxxdiag__, 1, 2)));
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
82 extern void gcc_tformat(const char *, ...)
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
83 __attribute__((__format__(__gcc_tdiag__, 1, 2)));
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
84
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
85 const char *foo3(const char *format) __attribute__((format_arg("foo"))); // expected-error{{'format_arg' attribute requires parameter 1 to be an integer constant}}
150
anatofuz
parents:
diff changeset
86
236
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
87 void call_nonvariadic(void) {
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
88 d3("%i", 123);
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
89 d3("%d", 123);
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
90 d3("%s", 123); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}}
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
91 }
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
92
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
93 __attribute__((format(printf, 1, 2))) void forward_fixed(const char *fmt, int i) { // expected-warning{{GCC requires a function with the 'format' attribute to be variadic}}
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
94 forward_fixed(fmt, i);
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
95 a(fmt, i);
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
96 }