| 1 |
// RUN: %clang_cc1 -fsyntax-only -verify -std=c23 -Wmissing-format-attribute %s |
| 2 |
// RUN: %clang_cc1 -fsyntax-only -std=c23 -Wmissing-format-attribute -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s |
| 3 |
|
| 4 |
typedef unsigned long size_t; |
| 5 |
typedef long ssize_t; |
| 6 |
typedef __builtin_va_list va_list; |
| 7 |
|
| 8 |
[[gnu::format(printf, 1, 2)]] |
| 9 |
int printf(const char *, ...); |
| 10 |
|
| 11 |
[[gnu::format(scanf, 1, 2)]] |
| 12 |
int scanf(const char *, ...); |
| 13 |
|
| 14 |
[[gnu::format(printf, 1, 0)]] |
| 15 |
int vprintf(const char *, va_list); |
| 16 |
|
| 17 |
[[gnu::format(scanf, 1, 0)]] |
| 18 |
int vscanf(const char *, va_list); |
| 19 |
|
| 20 |
[[gnu::format(printf, 2, 0)]] |
| 21 |
int vsprintf(char *, const char *, va_list); |
| 22 |
|
| 23 |
struct tm { unsigned i; }; |
| 24 |
[[gnu::format(strftime, 3, 0)]] |
| 25 |
size_t strftime(char *, size_t, const char *, const struct tm *); |
| 26 |
|
| 27 |
[[gnu::format(strfmon, 3, 4)]] |
| 28 |
ssize_t strfmon(char *, size_t, const char *, ...); |
| 29 |
|
| 30 |
[[gnu::format_matches(printf, 1, "%d %f \"'")]] |
| 31 |
int custom_print(const char *, va_list); |
| 32 |
|
| 33 |
// CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:1-[[@LINE+1]]:1}:"{{\[\[}}gnu::format(printf, 1, 0)]] " |
| 34 |
void f1(const char *fmt, va_list args) // #f1 |
| 35 |
{ |
| 36 |
vprintf(fmt, args); // expected-warning {{diagnostic behavior may be improved by adding the 'format(printf, 1, 0)' attribute to the declaration of 'f1'}} |
| 37 |
// expected-note@#f1 {{'f1' declared here}} |
| 38 |
} |
| 39 |
|
| 40 |
// CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:1-[[@LINE+1]]:1}:"{{\[\[}}gnu::format(scanf, 1, 0)]] " |
| 41 |
void f2(const char *fmt, va_list args) // #f2 |
| 42 |
{ |
| 43 |
vscanf(fmt, args); // expected-warning {{diagnostic behavior may be improved by adding the 'format(scanf, 1, 0)' attribute to the declaration of 'f2'}} |
| 44 |
// expected-note@#f2 {{'f2' declared here}} |
| 45 |
} |
| 46 |
|
| 47 |
// CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:1-[[@LINE+1]]:1}:"{{\[\[}}gnu::format(printf, 1, 2)]] " |
| 48 |
void f3(const char *fmt, ... /* args */) // #f3 |
| 49 |
{ |
| 50 |
va_list args; |
| 51 |
vprintf(fmt, args); // expected-warning {{diagnostic behavior may be improved by adding the 'format(printf, 1, 2)' attribute to the declaration of 'f3'}} |
| 52 |
// expected-note@#f3 {{'f3' declared here}} |
| 53 |
} |
| 54 |
|
| 55 |
// CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:1-[[@LINE+1]]:1}:"{{\[\[}}gnu::format(scanf, 1, 2)]] " |
| 56 |
void f4(const char *fmt, ... /* args */) // #f4 |
| 57 |
{ |
| 58 |
va_list args; |
| 59 |
vscanf(fmt, args); // expected-warning {{diagnostic behavior may be improved by adding the 'format(scanf, 1, 2)' attribute to the declaration of 'f4'}} |
| 60 |
// expected-note@#f4 {{'f4' declared here}} |
| 61 |
} |
| 62 |
|
| 63 |
// CHECK: fix-it:"{{.*}}":{[[@LINE+2]]:1-[[@LINE+2]]:1}:"{{\[\[}}gnu::format(printf, 2, 3)]] " |
| 64 |
[[gnu::format(printf, 1, 3)]] |
| 65 |
void f5(char *out, const char *format, ... /* args */) // #f5 |
| 66 |
{ |
| 67 |
va_list args; |
| 68 |
vsprintf(out, format, args); // expected-warning {{diagnostic behavior may be improved by adding the 'format(printf, 2, 3)' attribute to the declaration of 'f5'}} |
| 69 |
// expected-note@#f5 {{'f5' declared here}} |
| 70 |
} |
| 71 |
|
| 72 |
// CHECK: fix-it:"{{.*}}":{[[@LINE+2]]:1-[[@LINE+2]]:1}:"{{\[\[}}gnu::format(printf, 2, 3)]] " |
| 73 |
[[gnu::format(scanf, 1, 3)]] |
| 74 |
void f6(char *out, const char *format, ... /* args */) // #f6 |
| 75 |
{ |
| 76 |
va_list args; |
| 77 |
vsprintf(out, format, args); // expected-warning {{diagnostic behavior may be improved by adding the 'format(printf, 2, 3)' attribute to the declaration of 'f6'}} |
| 78 |
// expected-note@#f6 {{'f6' declared here}} |
| 79 |
} |
| 80 |
|
| 81 |
// Ok, out is not passed to print functions. |
| 82 |
void f7(char* out, ... /* args */) |
| 83 |
{ |
| 84 |
va_list args; |
| 85 |
|
| 86 |
const char *ch = "format"; |
| 87 |
vprintf(ch, args); |
| 88 |
vprintf("test", args); |
| 89 |
} |
| 90 |
|
| 91 |
// Ok, format string is not passed to format functions. |
| 92 |
void f8(va_list args) |
| 93 |
{ |
| 94 |
const char * const ch = "format"; |
| 95 |
vprintf(ch, args); |
| 96 |
vprintf("test", args); |
| 97 |
|
| 98 |
vscanf(ch, args); |
| 99 |
vscanf("test", args); |
| 100 |
|
| 101 |
char out[10]; |
| 102 |
|
| 103 |
struct tm tm_arg; |
| 104 |
tm_arg.i = 0; |
| 105 |
strftime(out, sizeof(out), ch, &tm_arg); |
| 106 |
strftime(out, sizeof(out), "test", &tm_arg); |
| 107 |
|
| 108 |
strfmon(out, sizeof(out), ch); |
| 109 |
strfmon(out, sizeof(out), "test"); |
| 110 |
} |
| 111 |
|
| 112 |
// CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:1-[[@LINE+1]]:1}:"{{\[\[}}gnu::format_matches(printf, 1, \"%d %f \\\"'\")]] " |
| 113 |
void f9(const char *fmt, ...) // #f9 |
| 114 |
{ |
| 115 |
va_list args; |
| 116 |
custom_print(fmt, args); // expected-warning {{diagnostic behavior may be improved by adding the 'format_matches(printf, 1, "%d %f \"'")' attribute to the declaration of 'f9'}} |
| 117 |
// expected-note@#f9 {{'f9' declared here}} |
| 118 |
} |
| 119 |
|
| 120 |
// CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:1-[[@LINE+1]]:1}:"{{\[\[}}gnu::format(scanf, 1, 2)]] " |
| 121 |
void f10(const char *out, ... /* args */) // #f10 |
| 122 |
{ |
| 123 |
va_list args; |
| 124 |
vscanf(out, args); // expected-warning {{diagnostic behavior may be improved by adding the 'format(scanf, 1, 2)' attribute to the declaration of 'f10'}} |
| 125 |
// expected-note@#f10 {{'f10' declared here}} |
| 126 |
vprintf(out, args); // expected-warning {{passing 'scanf' format string where 'printf' format string is expected}} |
| 127 |
} |
| 128 |
|
| 129 |
// CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:1-[[@LINE+1]]:1}:"{{\[\[}}gnu::format(printf, 1, 2)]] " |
| 130 |
void f11(const char out[], ... /* args */) // #f11 |
| 131 |
{ |
| 132 |
va_list args; |
| 133 |
char ch[10] = "format"; |
| 134 |
vprintf(ch, args); |
| 135 |
vsprintf(ch, out, args); // expected-warning {{diagnostic behavior may be improved by adding the 'format(printf, 1, 2)' attribute to the declaration of 'f11'}} |
| 136 |
// expected-note@#f11 {{'f11' declared here}} |
| 137 |
} |
| 138 |
|
| 139 |
// CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:1-[[@LINE+1]]:1}:"{{\[\[}}gnu::format(printf, 1, 0)]] " |
| 140 |
void f12(char* out) // #f12 |
| 141 |
{ |
| 142 |
va_list args; |
| 143 |
const char *ch = "format"; |
| 144 |
vsprintf(out, ch, args); |
| 145 |
vprintf(out, args); // expected-warning {{diagnostic behavior may be improved by adding the 'format(printf, 1, 0)' attribute to the declaration of 'f12'}} |
| 146 |
// expected-note@#f12 {{'f12' declared here}} |
| 147 |
} |
| 148 |
|
| 149 |
// CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:1-[[@LINE+1]]:1}:"{{\[\[}}gnu::format(printf, 1, 0)]] " |
| 150 |
void f13(char *out, va_list args) // #f13 |
| 151 |
{ |
| 152 |
vprintf(out, args); // expected-warning {{diagnostic behavior may be improved by adding the 'format(printf, 1, 0)' attribute to the declaration of 'f13'}} |
| 153 |
// expected-note@#f13 {{'f13' declared here}} |
| 154 |
vscanf(out, args); // expected-warning {{passing 'printf' format string where 'scanf' format string is expected}} |
| 155 |
} |
| 156 |
|
| 157 |
// CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:1-[[@LINE+1]]:1}:"{{\[\[}}gnu::format(scanf, 1, 2)]] " |
| 158 |
void f14(char *out, ... /* args */) // #f14 |
| 159 |
{ |
| 160 |
va_list args; |
| 161 |
vscanf(out, args); // expected-warning {{diagnostic behavior may be improved by adding the 'format(scanf, 1, 2)' attribute to the declaration of 'f14'}} |
| 162 |
// expected-note@#f14 {{'f14' declared here}} |
| 163 |
vscanf(out, args); |
| 164 |
} |
| 165 |
|
| 166 |
// CHECK: fix-it:"{{.*}}":{[[@LINE+2]]:1-[[@LINE+2]]:1}:"{{\[\[}}gnu::format(printf, 1, 3)]] " |
| 167 |
// CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:1-[[@LINE+1]]:1}:"{{\[\[}}gnu::format(printf, 2, 3)]] " |
| 168 |
void f15(char *ch, const char *out, ... /* args */) // #f15 |
| 169 |
{ |
| 170 |
va_list args; |
| 171 |
vprintf(ch, args); // expected-warning {{diagnostic behavior may be improved by adding the 'format(printf, 1, 3)' attribute to the declaration of 'f15'}} |
| 172 |
// expected-note@#f15 {{'f15' declared here}} |
| 173 |
vprintf(out, args); // expected-warning {{diagnostic behavior may be improved by adding the 'format(printf, 2, 3)' attribute to the declaration of 'f15'}} |
| 174 |
// expected-note@#f15 {{'f15' declared here}} |
| 175 |
} |
| 176 |
|
| 177 |
// CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:1-[[@LINE+1]]:1}:"{{\[\[}}gnu::format(printf, 1, 2)]] " |
| 178 |
void f16(const char *a, ...) // #f16 |
| 179 |
{ |
| 180 |
va_list args; |
| 181 |
const char *const b = a; |
| 182 |
vprintf(b, args); // expected-warning {{diagnostic behavior may be improved by adding the 'format(printf, 1, 2)' attribute to the declaration of 'f16'}} |
| 183 |
// expected-note@#f16 {{'f16' declared here}} |
| 184 |
} |
| 185 |
|
| 186 |
// CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:1-[[@LINE+1]]:1}:"{{\[\[}}gnu::format(printf, 1, 2)]] " |
| 187 |
void f17(char *fmt, unsigned x, unsigned y, unsigned z) // #f17 |
| 188 |
{ |
| 189 |
printf(fmt, x, y, z); // expected-warning {{diagnostic behavior may be improved by adding the 'format(printf, 1, 2)' attribute to the declaration of 'f17'}} |
| 190 |
// expected-note@#f17 {{'f17' declared here}} |
| 191 |
} |
| 192 |
|
| 193 |
void f18(char *fmt, unsigned x, unsigned y, unsigned z) // #f18 |
| 194 |
{ |
| 195 |
// Arguments are not passed in the same order. |
| 196 |
printf(fmt, x, z, y); |
| 197 |
} |
| 198 |
|
| 199 |
void f19(char *out, ... /* args */) |
| 200 |
{ |
| 201 |
printf(out, 1); // No warning, arguments are not passed to printf. |
| 202 |
} |
| 203 |
|
| 204 |
// CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:1-[[@LINE+1]]:1}:"{{\[\[}}gnu::format(strftime, 3, 0)]] " |
| 205 |
void f20(char *out, const size_t len, const char *format) // #f20 |
| 206 |
{ |
| 207 |
struct tm tm_arg; |
| 208 |
tm_arg.i = 0; |
| 209 |
strftime(out, len, format, &tm_arg); // expected-warning {{diagnostic behavior may be improved by adding the 'format(strftime, 3, 0)' attribute to the declaration of 'f20'}} |
| 210 |
// expected-note@#f20 {{'f20' declared here}} |
| 211 |
} |
| 212 |
|
| 213 |
// CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:1-[[@LINE+1]]:1}:"{{\[\[}}gnu::format(strfmon, 3, 4)]] " |
| 214 |
void f21(char *out, const size_t len, const char *format, int x, int y) // #f21 |
| 215 |
{ |
| 216 |
strfmon(out, len, format, x, y); // expected-warning {{diagnostic behavior may be improved by adding the 'format(strfmon, 3, 4)' attribute to the declaration of 'f21'}} |
| 217 |
// expected-note@#f21 {{'f21' declared here}} |
| 218 |
} |
| 219 |
|
| 220 |
// CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:1-[[@LINE+1]]:1}:"{{\[\[}}gnu::format(printf, 1, 2)]] " |
| 221 |
void f22(const char *fmt, ... /* args */); // #f22 |
| 222 |
|
| 223 |
void f22(const char *fmt, ... /* args */) |
| 224 |
{ |
| 225 |
va_list args; |
| 226 |
vprintf(fmt, args); // expected-warning {{diagnostic behavior may be improved by adding the 'format(printf, 1, 2)' attribute to the declaration of 'f22'}} |
| 227 |
// expected-note@#f22 {{'f22' declared here}} |
| 228 |
} |