|
| 1 | +// RUN: %clang_cc1 -fsyntax-only -verify -Wmissing-format-attribute %s |
| 2 | + |
| 3 | +#include <stdarg.h> |
| 4 | +#include <stdio.h> |
| 5 | +#include <uchar.h> |
| 6 | +#include <wchar.h> |
| 7 | + |
| 8 | +__attribute__((__format__ (__scanf__, 1, 4))) |
| 9 | +void f1(char *out, const size_t len, const char *format, ... /* args */) |
| 10 | +{ |
| 11 | + va_list args; |
| 12 | + vsnprintf(out, len, format, args); // expected-warning {{diagnostic behavior may be improved by adding the 'printf' format attribute to the declaration of 'f1'}} |
| 13 | + // CHECK-FIXES: __attribute__((format(printf, 3, 4))) |
| 14 | +} |
| 15 | + |
| 16 | +__attribute__((__format__ (__printf__, 1, 4))) |
| 17 | +void f2(char *out, const size_t len, const char *format, ... /* args */) |
| 18 | +{ |
| 19 | + va_list args; |
| 20 | + vsnprintf(out, len, format, args); // expected-warning {{diagnostic behavior may be improved by adding the 'printf' format attribute to the declaration of 'f2'}} |
| 21 | + // CHECK-FIXES: __attribute__((format(printf, 3, 4))) |
| 22 | +} |
| 23 | + |
| 24 | +void f3(char *out, va_list args) |
| 25 | +{ |
| 26 | + vprintf(out, args); // expected-warning {{diagnostic behavior may be improved by adding the 'printf' format attribute to the declaration of 'f3'}} |
| 27 | + // CHECK-FIXES: __attribute__((format(printf, 1, 0))) |
| 28 | + vscanf(out, args); // expected-warning {{diagnostic behavior may be improved by adding the 'scanf' format attribute to the declaration of 'f3'}} |
| 29 | + // CHECK-FIXES: __attribute__((format(scanf, 1, 0))) |
| 30 | +} |
| 31 | + |
| 32 | +void f4(char* out, ... /* args */) |
| 33 | +{ |
| 34 | + va_list args; |
| 35 | + vprintf("test", args); // no warning |
| 36 | + |
| 37 | + const char *ch; |
| 38 | + vscanf(ch, args); // no warning |
| 39 | +} |
| 40 | + |
| 41 | +void f5(va_list args) |
| 42 | +{ |
| 43 | + char *ch; |
| 44 | + vscanf(ch, args); // no warning |
| 45 | +} |
| 46 | + |
| 47 | +void f6(char *out, va_list args) |
| 48 | +{ |
| 49 | + char *ch; |
| 50 | + vscanf(ch, args); // no warning |
| 51 | + vprintf("test", args); // no warning |
| 52 | +} |
| 53 | + |
| 54 | +void f7(const char *out, ... /* args */) |
| 55 | +{ |
| 56 | + va_list args; |
| 57 | + |
| 58 | + vscanf(out, &args[0]); // expected-warning {{diagnostic behavior may be improved by adding the 'scanf' format attribute to the declaration of 'f7'}} |
| 59 | + // CHECK-FIXES: __attribute__((format(scanf, 1, 0))) |
| 60 | + vprintf(out, &args[0]); // expected-warning {{diagnostic behavior may be improved by adding the 'printf' format attribute to the declaration of 'f7'}} |
| 61 | + // CHECK-FIXES: __attribute__((format(printf, 1, 0))) |
| 62 | +} |
| 63 | + |
| 64 | +__attribute__((format(scanf, 1, 0))) |
| 65 | +__attribute__((format(printf, 1, 2))) |
| 66 | +void f8(const char *out, ... /* args */) |
| 67 | +{ |
| 68 | + va_list args; |
| 69 | + |
| 70 | + vscanf(out, &args[0]); // no warning |
| 71 | + vprintf(out, &args[0]); // no warning |
| 72 | +} |
| 73 | + |
| 74 | +void f9(const char out[], ... /* args */) |
| 75 | +{ |
| 76 | + va_list args; |
| 77 | + vscanf(out, args); // expected-warning {{diagnostic behavior may be improved by adding the 'scanf' format attribute to the declaration of 'f9'}} |
| 78 | + // CHECK-FIXES: __attribute__((format(scanf, 1, 2))) |
| 79 | + char *ch; |
| 80 | + vprintf(ch, args); // no warning |
| 81 | +} |
| 82 | + |
| 83 | +void f10(const wchar_t *out, ... /* args */) |
| 84 | +{ |
| 85 | + va_list args; |
| 86 | + vprintf(out, args); // expected-warning {{incompatible pointer types passing 'const wchar_t *' (aka 'const int *') to parameter of type 'const char *'}} |
| 87 | + // expected-warning {{diagnostic behavior may be improved by adding the 'printf' format attribute to the declaration of 'f10'}} |
| 88 | + // CHECK-FIXES: __attribute__((format(printf, 1, 2))) |
| 89 | + vscanf((const char *) out, args); // no warning |
| 90 | + // expected-warning {{diagnostic behavior may be improved by adding the 'printf' format attribute to the declaration of 'f10'}} |
| 91 | + // CHECK-FIXES: __attribute__((format(scanf, 1, 2))) |
| 92 | + vscanf((char *) out, args); // no warning |
| 93 | + // expected-warning {{diagnostic behavior may be improved by adding the 'printf' format attribute to the declaration of 'f10'}} |
| 94 | + // CHECK-FIXES: __attribute__((format(scanf, 1, 2))) |
| 95 | +} |
| 96 | + |
| 97 | +__attribute__((format(printf, 1, 2))) // expected-error {{format argument not a string type}} |
| 98 | +void f11(const wchar_t *out, ... /* args */); |
| 99 | + |
| 100 | +void f12(const char16_t *out, ... /* args */) |
| 101 | +{ |
| 102 | + va_list args; |
| 103 | + vscanf(out, args); // expected-warning {{incompatible pointer types passing 'const char16_t *' (aka 'const unsigned short *') to parameter of type 'const char *'}} |
| 104 | + // expected-warning {{diagnostic behavior may be improved by adding the 'scanf' format attribute to the declaration of 'f12'}} |
| 105 | + // CHECK-FIXES: __attribute__((format(scanf, 1, 2))) |
| 106 | +} |
| 107 | + |
| 108 | +__attribute__((format(printf, 1, 2))) // expected-error {{format argument not a string type}} |
| 109 | +void f13(const char16_t *out, ... /* args */); |
| 110 | + |
| 111 | +void f14(const char32_t *out, ... /* args */) |
| 112 | +{ |
| 113 | + va_list args; |
| 114 | + vscanf(out, args); // expected-warning {{incompatible pointer types passing 'const char32_t *' (aka 'const unsigned int *') to parameter of type 'const char *'}} |
| 115 | + // expected-warning {{diagnostic behavior may be improved by adding the 'scanf' format attribute to the declaration of 'f14'}} |
| 116 | + // CHECK-FIXES: __attribute__((format(scanf, 1, 2))) |
| 117 | +} |
| 118 | + |
| 119 | +__attribute__((format(scanf, 1, 2))) // expected-error {{format argument not a string type}} |
| 120 | +void f15(const char32_t *out, ... /* args */); |
| 121 | + |
| 122 | +void f16(const unsigned char *out, ... /* args */) |
| 123 | +{ |
| 124 | + va_list args; |
| 125 | + vprintf(out, args); // expected-warning {{passing 'const unsigned char *' to parameter of type 'const char *' converts between pointers to integer types where one is of the unique plain 'char' type and the other is not}} |
| 126 | + // expected-warning {{diagnostic behavior may be improved by adding the 'printf' format attribute to the declaration of 'f16'}} |
| 127 | + // CHECK-FIXES: __attribute__((format(printf, 1, 2))) |
| 128 | + vscanf((const char *) out, args); // no warning |
| 129 | + // expected-warning {{diagnostic behavior may be improved by adding the 'scanf' format attribute to the declaration of 'f16'}} |
| 130 | + // CHECK-FIXES: __attribute__((format(scanf, 1, 2))) |
| 131 | + vscanf((char *) out, args); // no warning |
| 132 | + // expected-warning {{diagnostic behavior may be improved by adding the 'scanf' format attribute to the declaration of 'f16'}} |
| 133 | + // CHECK-FIXES: __attribute__((format(scanf, 1, 2))) |
| 134 | +} |
| 135 | + |
| 136 | +__attribute__((format(printf, 1, 2))) |
| 137 | +void f17(const unsigned char *out, ... /* args */) |
| 138 | +{ |
| 139 | + va_list args; |
| 140 | + vprintf(out, args); // expected-warning {{passing 'const unsigned char *' to parameter of type 'const char *' converts between pointers to integer types where one is of the unique plain 'char' type and the other is not}} |
| 141 | + vscanf((const char *) out, args); // expected-warning {{diagnostic behavior may be improved by adding the 'scanf' format attribute to the declaration of 'f17'}} |
| 142 | + // CHECK-FIXES: __attribute__((format(scanf, 1, 2))) |
| 143 | + vprintf((const char *) out, args); // no warning |
| 144 | + vscanf((char *) out, args); // expected-warning {{diagnostic behavior may be improved by adding the 'scanf' format attribute to the declaration of 'f17'}} |
| 145 | + // CHECK-FIXES: __attribute__((format(scanf, 1, 2))) |
| 146 | + vprintf((char *) out, args); // no warning |
| 147 | +} |
| 148 | + |
| 149 | +void f18(signed char *out, ... /* args */) |
| 150 | +{ |
| 151 | + va_list args; |
| 152 | + vscanf(out, args); // expected-warning {{passing 'signed char *' to parameter of type 'const char *' converts between pointers to integer types where one is of the unique plain 'char' type and the other is not}} |
| 153 | + // expected-warning {{diagnostic behavior may be improved by adding the 'scanf' format attribute to the declaration of 'f18'}} |
| 154 | + // CHECK-FIXES: __attribute__((format(scanf, 1, 2))) |
| 155 | + vscanf((const char *) out, args); // expected-warning {{diagnostic behavior may be improved by adding the 'scanf' format attribute to the declaration of 'f18'}} |
| 156 | + // CHECK-FIXES: __attribute__((format(scanf, 1, 2))) |
| 157 | + vprintf((char *) out, args); // expected-warning {{diagnostic behavior may be improved by adding the 'scanf' format attribute to the declaration of 'f18'}} |
| 158 | + // CHECK-FIXES: __attribute__((format(printf, 1, 2))) |
| 159 | +} |
| 160 | + |
| 161 | +__attribute__((format(scanf, 1, 2))) |
| 162 | +void f19(signed char *out, ... /* args */) |
| 163 | +{ |
| 164 | + va_list args; |
| 165 | + vprintf(out, args); // expected-warning {{passing 'signed char *' to parameter of type 'const char *' converts between pointers to integer types where one is of the unique plain 'char' type and the other is not}} |
| 166 | + // expected-warning {{diagnostic behavior may be improved by adding the 'printf' format attribute to the declaration of 'f19'}} |
| 167 | + // CHECK-FIXES: __attribute__((format(printf, 1, 2))) |
| 168 | + vscanf((const char *) out, args); // no warning |
| 169 | + vprintf((const char *) out, args); // expected-warning {{diagnostic behavior may be improved by adding the 'printf' format attribute to the declaration of 'f19'}} |
| 170 | + // CHECK-FIXES: __attribute__((format(printf, 1, 2))) |
| 171 | + vscanf((char *) out, args); // no warning |
| 172 | + vprintf((char *) out, args); // expected-warning {{diagnostic behavior may be improved by adding the 'printf' format attribute to the declaration of 'f19'}} |
| 173 | + // CHECK-FIXES: __attribute__((format(printf, 1, 2))) |
| 174 | +} |
| 175 | + |
| 176 | +__attribute__((format(printf, 1, 2))) |
| 177 | +void f20(unsigned char out[], ... /* args */) |
| 178 | +{ |
| 179 | + va_list args; |
| 180 | + vprintf(out, args); // expected-warning {{passing 'unsigned char *' to parameter of type 'const char *' converts between pointers to integer types where one is of the unique plain 'char' type and the other is not}} |
| 181 | + vscanf(out, args); // expected-warning {{passing 'unsigned char *' to parameter of type 'const char *' converts between pointers to integer types where one is of the unique plain 'char' type and the other is not}} |
| 182 | + // expected-warning {{diagnostic behavior may be improved by adding the 'scanf' format attribute to the declaration of 'f20'}} |
| 183 | + // CHECK-FIXES: __attribute__((format(scanf, 1, 2))) |
| 184 | +} |
| 185 | + |
| 186 | +void f21(char* out) { |
| 187 | + va_list args; |
| 188 | + const char* ch; |
| 189 | + vsprintf(out, ch, args); // no warning |
| 190 | + vscanf(out, args); // expected-warning {{diagnostic behavior may be improved by adding the 'printf' format attribute to the declaration of 'f21'}} |
| 191 | + // CHECK-FIXES: __attribute__((format(scanf, 1, 0))) |
| 192 | +} |
| 193 | + |
| 194 | +void f22(const char *out, ... /* args */) |
| 195 | +{ |
| 196 | + int a; |
| 197 | + printf(out, a); // expected-warning {{diagnostic behavior may be improved by adding the 'printf' format attribute to the declaration of 'f22'}} |
| 198 | + // CHECK-FIXES: __attribute__((format(printf, 1, 0))) |
| 199 | + printf(out, 1); // expected-warning {{diagnostic behavior may be improved by adding the 'printf' format attribute to the declaration of 'f22'}} |
| 200 | + // CHECK-FIXES: __attribute__((format(printf, 1, 0))) |
| 201 | +} |
| 202 | + |
| 203 | +__attribute__((format(printf, 1, 2))) |
| 204 | +void f23(const char *out, ... /* args */) |
| 205 | +{ |
| 206 | + int a; |
| 207 | + printf(out, a); // no warning |
| 208 | + printf(out, 1); // no warning |
| 209 | +} |
| 210 | + |
| 211 | +void f24(char* ch, const char *out, ... /* args */) |
| 212 | +{ |
| 213 | + va_list args; |
| 214 | + printf(ch, args); // expected-warning {{diagnostic behavior may be improved by adding the 'printf' format attribute to the declaration of 'f24}} |
| 215 | + // CHECK-FIXES: __attribute__((format(printf, 1, 3))) |
| 216 | + int a; |
| 217 | + printf(out, a); // expected-warning {{diagnostic behavior may be improved by adding the 'printf' format attribute to the declaration of 'f24'}} |
| 218 | + // CHECK-FIXES: __attribute__((format(printf, 2, 0))) |
| 219 | + printf(out, 1); // expected-warning {{diagnostic behavior may be improved by adding the 'printf' format attribute to the declaration of 'f24'}} |
| 220 | + // CHECK-FIXES: __attribute__((format(printf, 2, 0))) |
| 221 | + printf(out, args); // expected-warning {{diagnostic behavior may be improved by adding the 'printf' format attribute to the declaration of 'f24'}} |
| 222 | + // CHECK-FIXES: __attribute__((format(printf, 2, 3))) |
| 223 | +} |
0 commit comments