319 lines · plain
1// RUN: %clang_cc1 -triple %itanium_abi_triple -fsyntax-only -fblocks -verify %s2// RUN: %clang_cc1 -triple %itanium_abi_triple -fdiagnostics-parseable-fixits -fblocks %s 2>&1 | FileCheck %s3 4@class NSString;5extern void NSLog(NSString *, ...);6int printf(const char * restrict, ...) ;7 8void test_integer_correction (int x) {9 printf("%d", x); // no-warning10 printf("%s", x); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}}11 printf("%lf", x); // expected-warning{{format specifies type 'double' but the argument has type 'int'}}12 // CHECK: fix-it:"{{.*}}":{10:11-10:13}:"%d"13 // CHECK: fix-it:"{{.*}}":{11:11-11:14}:"%d"14 15 NSLog(@"%d", x); // no-warning16 NSLog(@"%s", x); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}}17 NSLog(@"%lf", x); // expected-warning{{format specifies type 'double' but the argument has type 'int'}}18 NSLog(@"%@", x); // expected-warning{{format specifies type 'id' but the argument has type 'int'}}19 // CHECK: fix-it:"{{.*}}":{16:11-16:13}:"%d"20 // CHECK: fix-it:"{{.*}}":{17:11-17:14}:"%d"21 // CHECK: fix-it:"{{.*}}":{18:11-18:13}:"%d"22}23 24void test_string_correction (char *x) {25 printf("%d", x); // expected-warning{{format specifies type 'int' but the argument has type 'char *'}}26 printf("%s", x); // no-warning27 printf("%lf", x); // expected-warning{{format specifies type 'double' but the argument has type 'char *'}}28 // CHECK: fix-it:"{{.*}}":{25:11-25:13}:"%s"29 // CHECK: fix-it:"{{.*}}":{27:11-27:14}:"%s"30 31 NSLog(@"%d", x); // expected-warning{{format specifies type 'int' but the argument has type 'char *'}}32 NSLog(@"%s", x); // no-warning33 NSLog(@"%lf", x); // expected-warning{{format specifies type 'double' but the argument has type 'char *'}}34 NSLog(@"%@", x); // expected-warning{{format specifies type 'id' but the argument has type 'char *'}}35 // CHECK: fix-it:"{{.*}}":{31:11-31:13}:"%s"36 // CHECK: fix-it:"{{.*}}":{33:11-33:14}:"%s"37 // CHECK: fix-it:"{{.*}}":{34:11-34:13}:"%s"38}39 40void test_object_correction (id x) {41 NSLog(@"%d", x); // expected-warning{{format specifies type 'int' but the argument has type 'id'}}42 NSLog(@"%s", x); // expected-warning{{format specifies type 'char *' but the argument has type 'id'}}43 NSLog(@"%lf", x); // expected-warning{{format specifies type 'double' but the argument has type 'id'}}44 NSLog(@"%@", x); // no-warning45 // CHECK: fix-it:"{{.*}}":{41:11-41:13}:"%@"46 // CHECK: fix-it:"{{.*}}":{42:11-42:13}:"%@"47 // CHECK: fix-it:"{{.*}}":{43:11-43:14}:"%@"48}49 50typedef const struct __CFString * __attribute__((NSObject)) CFStringRef;51void test_cf_object_correction (CFStringRef x) {52 NSLog(@"%d", x); // expected-warning{{format specifies type 'int' but the argument has type 'CFStringRef'}}53 NSLog(@"%s", x); // expected-warning{{format specifies type 'char *' but the argument has type 'CFStringRef'}}54 NSLog(@"%lf", x); // expected-warning{{format specifies type 'double' but the argument has type 'CFStringRef'}}55 NSLog(@"%@", x); // no-warning56 // CHECK: fix-it:"{{.*}}":{52:11-52:13}:"%@"57 // CHECK: fix-it:"{{.*}}":{53:11-53:13}:"%@"58 // CHECK: fix-it:"{{.*}}":{54:11-54:14}:"%@"59}60 61typedef void (^block_t)(void);62void test_block_correction (block_t x) {63 NSLog(@"%d", x); // expected-warning{{format specifies type 'int' but the argument has type 'block_t'}}64 NSLog(@"%s", x); // expected-warning{{format specifies type 'char *' but the argument has type 'block_t'}}65 NSLog(@"%lf", x); // expected-warning{{format specifies type 'double' but the argument has type 'block_t'}}66 NSLog(@"%@", x); // no-warning67 // CHECK: fix-it:"{{.*}}":{63:11-63:13}:"%@"68 // CHECK: fix-it:"{{.*}}":{64:11-64:13}:"%@"69 // CHECK: fix-it:"{{.*}}":{65:11-65:14}:"%@"70}71 72void test_class_correction (Class x) {73 NSLog(@"%d", x); // expected-warning{{format specifies type 'int' but the argument has type 'Class'}}74 NSLog(@"%s", x); // expected-warning{{format specifies type 'char *' but the argument has type 'Class'}}75 NSLog(@"%lf", x); // expected-warning{{format specifies type 'double' but the argument has type 'Class'}}76 NSLog(@"%@", x); // no-warning77 // CHECK: fix-it:"{{.*}}":{73:11-73:13}:"%@"78 // CHECK: fix-it:"{{.*}}":{74:11-74:13}:"%@"79 // CHECK: fix-it:"{{.*}}":{75:11-75:14}:"%@"80}81 82 83typedef enum : int { NSUTF8StringEncoding = 8 } NSStringEncoding;84void test_fixed_enum_correction(NSStringEncoding x) {85 NSLog(@"%@", x); // expected-warning{{format specifies type 'id' but the argument has underlying type 'int'}}86 // CHECK: fix-it:"{{.*}}":{85:11-85:13}:"%d"87}88 89typedef __SIZE_TYPE__ size_t;90enum SomeSize : size_t { IntegerSize = sizeof(int) };91void test_named_fixed_enum_correction(enum SomeSize x) {92 NSLog(@"%@", x); // expected-warning{{format specifies type 'id' but the argument has underlying type 'size_t' (aka}}93 // CHECK: fix-it:"{{.*}}":{92:11-92:13}:"%zu"94}95 96 97typedef unsigned char uint8_t;98void test_char(char c, signed char s, unsigned char u, uint8_t n) {99 NSLog(@"%s", c); // expected-warning{{format specifies type 'char *' but the argument has type 'char'}}100 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%c"101 102 NSLog(@"%lf", c); // expected-warning{{format specifies type 'double' but the argument has type 'char'}}103 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:14}:"%c"104 105 NSLog(@"%@", c); // expected-warning{{format specifies type 'id' but the argument has type 'char'}}106 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%c"107 108 NSLog(@"%c", c); // no-warning109 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%c"110 111 112 NSLog(@"%s", s); // expected-warning{{format specifies type 'char *' but the argument has type 'signed char'}}113 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%c"114 115 NSLog(@"%lf", s); // expected-warning{{format specifies type 'double' but the argument has type 'signed char'}}116 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:14}:"%c"117 118 NSLog(@"%@", s); // expected-warning{{format specifies type 'id' but the argument has type 'signed char'}}119 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%c"120 121 NSLog(@"%c", s); // no-warning122 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%c"123 124 125 NSLog(@"%s", u); // expected-warning{{format specifies type 'char *' but the argument has type 'unsigned char'}}126 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%c"127 128 NSLog(@"%lf", u); // expected-warning{{format specifies type 'double' but the argument has type 'unsigned char'}}129 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:14}:"%c"130 131 NSLog(@"%@", u); // expected-warning{{format specifies type 'id' but the argument has type 'unsigned char'}}132 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%c"133 134 NSLog(@"%c", u); // no-warning135 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%c"136 137 138 NSLog(@"%s", n); // expected-warning{{format specifies type 'char *' but the argument has type 'uint8_t' (aka 'unsigned char')}}139 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%hhu"140 141 NSLog(@"%lf", n); // expected-warning{{format specifies type 'double' but the argument has type 'uint8_t' (aka 'unsigned char')}}142 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:14}:"%hhu"143 144 NSLog(@"%@", n); // expected-warning{{format specifies type 'id' but the argument has type 'uint8_t' (aka 'unsigned char')}}145 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%hhu"146 147 NSLog(@"%c", n); // no-warning148 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%hhu"149 150 151 NSLog(@"%s", 'a'); // expected-warning{{format specifies type 'char *' but the argument has type 'char'}}152 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%c"153 154 NSLog(@"%lf", 'a'); // expected-warning{{format specifies type 'double' but the argument has type 'char'}}155 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:14}:"%c"156 157 NSLog(@"%@", 'a'); // expected-warning{{format specifies type 'id' but the argument has type 'char'}}158 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%c"159 160 NSLog(@"%c", 'a'); // no-warning161 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%c"162 163 164 NSLog(@"%s", 'abcd'); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}}165 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%d"166 167 NSLog(@"%lf", 'abcd'); // expected-warning{{format specifies type 'double' but the argument has type 'int'}}168 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:14}:"%d"169 170 NSLog(@"%@", 'abcd'); // expected-warning{{format specifies type 'id' but the argument has type 'int'}}171 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%d"172 173 NSLog(@"%hhd", 'a'); // expected-warning{{format specifies type 'char' but the argument has type 'int'}}174 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:15}:"%d"175 176 NSLog(@"%hhu", 'a'); // expected-warning{{format specifies type 'unsigned char' but the argument has type 'int'}}177 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:15}:"%d"178}179 180void multichar_constants_false_negative(void) {181 // The value of a multi-character constant is implementation-defined, but182 // almost certainly shouldn't be printed with %c. However, the current183 // type-checker expects %c to correspond to an integer argument, because184 // many C library functions like fgetc() actually return an int (using -1185 // as a sentinel).186 NSLog(@"%c", 'abcd'); // missing-warning{{format specifies type 'char' but the argument has type 'int'}}187 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%d"188}189 190 191void test_percent_C(void) {192 const unsigned short data = 'a';193 NSLog(@"%C", data); // no-warning194 195 NSLog(@"%C", 0x260300); // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'int'}}196 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%d"197 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:"(unsigned short)"198 199 typedef unsigned short unichar;200 201 NSLog(@"%C", 0x260300); // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'int'}}202 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%d"203 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:"(unichar)"204 205 NSLog(@"%C", data ? 0x2F0000 : 0x260300); // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'int'}}206 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%d"207 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:"(unichar)("208 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:42-[[@LINE-3]]:42}:")"209 210 NSLog(@"%C", 0.0); // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'double'}}211 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%f"212 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:"(unichar)"213 214 NSLog(@"%C", (char)0x260300);215 216 NSLog(@"%C", 'a'); // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'char'}}217 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%c"218 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:22}:"(unichar)"219}220 221 222void testSignedness(long i, unsigned long u) {223 printf("%d", u); // expected-warning{{format specifies type 'int' but the argument has type 'unsigned long'}}224 printf("%i", u); // expected-warning{{format specifies type 'int' but the argument has type 'unsigned long'}}225 printf("%u", i); // expected-warning{{format specifies type 'unsigned int' but the argument has type 'long'}}226 227 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:11-[[@LINE-4]]:13}:"%lu"228 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:11-[[@LINE-4]]:13}:"%lu"229 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:11-[[@LINE-4]]:13}:"%ld"230 231 printf("%+d", u); // expected-warning{{format specifies type 'int' but the argument has type 'unsigned long'}}232 233 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:11-[[@LINE-2]]:14}:"%+ld"234}235 236void testSizeTypes(void) {237 printf("%zu", 0.f); // expected-warning-re{{format specifies type 'size_t' (aka '{{.+}}') but the argument has type 'float'}}238 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:14}:"%f"239 240 printf("%zd", 0.f); // expected-warning-re{{format specifies type 'signed size_t' (aka '{{.+}}') but the argument has type 'float'}}241 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:14}:"%f"242 243 short x;244#if !defined(__ANDROID__) && !defined(__Fuchsia__)245 printf("%zn", &x); // expected-warning-re{{format specifies type 'signed size_t *' (aka '{{.+}}') but the argument has type 'short *'}}246#else247 printf("%zn", &x); // expected-warning-re{{format specifies type 'signed size_t *' (aka '{{.+}}') but the argument has type 'short *'}}248 // expected-warning@-1 {{'%n' specifier not supported on this platform}}249#endif // !defined(__ANDROID__) && !defined(__Fuchsia__)250 // PrintfSpecifier::fixType doesn't handle %n, so a fix-it is not emitted,251 // see the comment in PrintfSpecifier::fixType in PrintfFormatString.cpp.252}253 254typedef __PTRDIFF_TYPE__ ptrdiff_t;255#define __UNSIGNED_PTRDIFF_TYPE__ \256 __typeof__(_Generic((__PTRDIFF_TYPE__)0, \257 long long int : (unsigned long long int)0, \258 long int : (unsigned long int)0, \259 int : (unsigned int)0, \260 short : (unsigned short)0, \261 signed char : (unsigned char)0))262 263void testPtrDiffTypes(void) {264 __UNSIGNED_PTRDIFF_TYPE__ p1 = 0;265 printf("%tu", p1); // No warning.266 267 printf("%tu", 0.f); // expected-warning-re{{format specifies type 'unsigned ptrdiff_t' (aka '{{.+}}') but the argument has type 'float'}}268 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:14}:"%f"269 270 ptrdiff_t p2 = 0;271 printf("%td", p2); // No warning.272 273 printf("%td", 0.f); // expected-warning-re{{format specifies type 'ptrdiff_t' (aka '{{.+}}') but the argument has type 'float'}}274 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:14}:"%f"275 276 ptrdiff_t p3 = 0;277#if !defined(__ANDROID__) && !defined(__Fuchsia__)278 printf("%tn", &p3); // No warning.279#else280 printf("%tn", &p3); // expected-warning{{'%n' specifier not supported on this platform}}281#endif // !defined(__ANDROID__) && !defined(__Fuchsia__)282 283 short x;284#if !defined(__ANDROID__) && !defined(__Fuchsia__)285 printf("%tn", &x); // expected-warning-re{{format specifies type 'ptrdiff_t *' (aka '{{.+}}') but the argument has type 'short *'}}286 // PrintfSpecifier::fixType doesn't handle %n, so a fix-it is not emitted,287 // see the comment in PrintfSpecifier::fixType in PrintfFormatString.cpp.288#else289 printf("%tn", &x); // expected-warning-re{{format specifies type 'ptrdiff_t *' (aka '{{.+}}') but the argument has type 'short *'}}290 // expected-warning@-1 {{'%n' specifier not supported on this platform}}291#endif // !defined(__ANDROID__) && !defined(__Fuchsia__)292}293 294void testEnum(void) {295 typedef enum {296 ImplicitA = 1,297 ImplicitB = 2298 } Implicit;299 300 typedef enum {301 ImplicitLLA = 0,302 ImplicitLLB = ~0ULL303 } ImplicitLongLong;304 305 typedef enum : short {306 ExplicitA = 0,307 ExplicitB308 } ExplicitShort;309 310 printf("%f", (Implicit)0); // expected-warning{{format specifies type 'double' but the argument has underlying type}}311 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%{{[du]}}"312 313 printf("%f", (ImplicitLongLong)0); // expected-warning{{format specifies type 'double' but the argument has underlying type}}314 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%{{l*[du]}}"315 316 printf("%f", (ExplicitShort)0); // expected-warning{{format specifies type 'double' but the argument has underlying type 'short'}}317 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%hd"318}319