223 lines · c
1// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -verify -Wformat -Wformat-signedness %s2// RUN: %clang_cc1 -triple=x86_64-pc-win32 -std=c11 -fsyntax-only -verify -Wformat -Wformat-signedness %s3 4// Verify that -Wformat-signedness alone (without -Wformat) trigger the5// warnings. Note in gcc this will not trigger the signedness warnings as6// -Wformat is default off in gcc.7// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -verify -Wformat-signedness %s8// RUN: %clang_cc1 -triple=x86_64-pc-win32 -std=c11 -fsyntax-only -verify -Wformat-signedness %s9 10// Verify that -Wformat-signedness warnings are not reported with only -Wformat11// (gcc compat).12// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -Wformat -verify=okay %s13 14// Verify that -Wformat-signedness with -Wno-format are not reported (gcc compat).15// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -Wformat-signedness -Wno-format -verify=okay %s16// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -Wno-format -Wformat-signedness -verify=okay %s17// okay-no-diagnostics18 19int printf(const char *restrict format, ...);20int scanf(const char * restrict, ...);21 22void test_printf_bool(_Bool x)23{24 printf("%d", x); // no-warning25 printf("%u", x); // no-warning26 printf("%x", x); // no-warning27}28 29void test_printf_char(char x)30{31 printf("%c", x); // no-warning32}33 34void test_printf_unsigned_char(unsigned char x)35{36 printf("%c", x); // no-warning37}38 39void test_printf_int(int x)40{41 printf("%d", x); // no-warning42 printf("%u", x); // expected-warning{{format specifies type 'unsigned int' but the argument has type 'int', which differs in signedness}}43 printf("%x", x); // expected-warning{{format specifies type 'unsigned int' but the argument has type 'int', which differs in signedness}}44}45 46void test_printf_unsigned(unsigned x)47{48 printf("%d", x); // expected-warning{{format specifies type 'int' but the argument has type 'unsigned int', which differs in signedness}}49 printf("%u", x); // no-warning50 printf("%x", x); // no-warning51}52 53void test_printf_long(long x)54{55 printf("%ld", x); // no-warning56 printf("%lu", x); // expected-warning{{format specifies type 'unsigned long' but the argument has type 'long', which differs in signedness}}57 printf("%lx", x); // expected-warning{{format specifies type 'unsigned long' but the argument has type 'long', which differs in signedness}}58}59 60void test_printf_unsigned_long(unsigned long x)61{62 printf("%ld", x); // expected-warning{{format specifies type 'long' but the argument has type 'unsigned long', which differs in signedness}}63 printf("%lu", x); // no-warning64 printf("%lx", x); // no-warning65}66 67void test_printf_long_long(long long x)68{69 printf("%lld", x); // no-warning70 printf("%llu", x); // expected-warning{{format specifies type 'unsigned long long' but the argument has type 'long long', which differs in signedness}}71 printf("%llx", x); // expected-warning{{format specifies type 'unsigned long long' but the argument has type 'long long', which differs in signedness}}72}73 74void test_printf_unsigned_long_long(unsigned long long x)75{76 printf("%lld", x); // expected-warning{{format specifies type 'long long' but the argument has type 'unsigned long long', which differs in signedness}}77 printf("%llu", x); // no-warning78 printf("%llx", x); // no-warning79}80 81enum enum_int {82 minus_1 = -183};84 85void test_printf_enum_int(enum enum_int x)86{87 printf("%d", x); // no-warning88 printf("%u", x); // expected-warning{{format specifies type 'unsigned int' but the argument has underlying type 'int', which differs in signedness}}89 printf("%x", x); // expected-warning{{format specifies type 'unsigned int' but the argument has underlying type 'int', which differs in signedness}}90}91 92#ifndef _WIN32 // Disabled due to enums have different underlying type on _WIN3293enum enum_unsigned {94 zero = 095};96 97void test_printf_enum_unsigned(enum enum_unsigned x)98{99 printf("%d", x); // expected-warning{{format specifies type 'int' but the argument has underlying type 'unsigned int', which differs in signedness}}100 printf("%u", x); // no-warning101 printf("%x", x); // no-warning102}103 104enum enum_long {105 minus_one = -1,106 int_val = __INT_MAX__, // INT_MAX107 unsigned_val = (unsigned)(-__INT_MAX__ -1) // (unsigned)INT_MIN108};109 110void test_printf_enum_long(enum enum_long x)111{112 printf("%ld", x); // no-warning113 printf("%lu", x); // expected-warning{{format specifies type 'unsigned long' but the argument has underlying type 'long', which differs in signedness}}114 printf("%lx", x); // expected-warning{{format specifies type 'unsigned long' but the argument has underlying type 'long', which differs in signedness}}115}116 117enum enum_unsigned_long {118 uint_max_plus = (unsigned long)(__INT_MAX__ *2U +1U)+1, // (unsigned long)UINT_MAX+1119};120 121void test_printf_enum_unsigned_long(enum enum_unsigned_long x)122{123 printf("%ld", x); // expected-warning{{format specifies type 'long' but the argument has underlying type 'unsigned long', which differs in signedness}}124 printf("%lu", x); // no-warning125 printf("%lx", x); // no-warning126}127#endif128 129void test_scanf_char(char *y) {130 scanf("%c", y); // no-warning131}132 133void test_scanf_unsigned_char(unsigned char *y) {134 scanf("%c", y); // no-warning135}136 137void test_scanf_int(int *x) {138 scanf("%d", x); // no-warning139 scanf("%u", x); // expected-warning{{format specifies type 'unsigned int *' but the argument has type 'int *', which differs in signedness}}140 scanf("%x", x); // expected-warning{{format specifies type 'unsigned int *' but the argument has type 'int *', which differs in signedness}}141}142 143void test_scanf_unsigned(unsigned *x) {144 scanf("%d", x); // expected-warning{{format specifies type 'int *' but the argument has type 'unsigned int *', which differs in signedness}}145 scanf("%u", x); // no-warning146 scanf("%x", x); // no-warning147}148 149void test_scanf_long(long *x) {150 scanf("%ld", x); // no-warning151 scanf("%lu", x); // expected-warning{{format specifies type 'unsigned long *' but the argument has type 'long *', which differs in signedness}}152 scanf("%lx", x); // expected-warning{{format specifies type 'unsigned long *' but the argument has type 'long *', which differs in signedness}}153}154 155void test_scanf_unsigned_long(unsigned long *x) {156 scanf("%ld", x); // expected-warning{{format specifies type 'long *' but the argument has type 'unsigned long *', which differs in signedness}}157 scanf("%lu", x); // no-warning158 scanf("%lx", x); // no-warning159}160 161void test_scanf_longlong(long long *x) {162 scanf("%lld", x); // no-warning163 scanf("%llu", x); // expected-warning{{format specifies type 'unsigned long long *' but the argument has type 'long long *', which differs in signedness}}164 scanf("%llx", x); // expected-warning{{format specifies type 'unsigned long long *' but the argument has type 'long long *', which differs in signedness}}165}166 167void test_scanf_unsigned_longlong(unsigned long long *x) {168 scanf("%lld", x); // expected-warning{{format specifies type 'long long *' but the argument has type 'unsigned long long *', which differs in signedness}}169 scanf("%llu", x); // no-warning170 scanf("%llx", x); // no-warning171}172 173void test_scanf_enum_int(enum enum_int *x) {174 scanf("%d", x); // no-warning175 scanf("%u", x); // expected-warning{{format specifies type 'unsigned int *' but the argument has type 'enum enum_int *', which differs in signedness}}176 scanf("%x", x); // expected-warning{{format specifies type 'unsigned int *' but the argument has type 'enum enum_int *', which differs in signedness}}177}178 179#ifndef _WIN32 // Disabled due to enums have different underlying type on _WIN32180void test_scanf_enum_unsigned(enum enum_unsigned *x) {181 scanf("%d", x); // expected-warning{{format specifies type 'int *' but the argument has type 'enum enum_unsigned *', which differs in signedness}}182 scanf("%u", x); // no-warning183 scanf("%x", x); // no-warning184}185 186void test_scanf_enum_long(enum enum_long *x) {187 scanf("%ld", x); // no-warning188 scanf("%lu", x); // expected-warning{{format specifies type 'unsigned long *' but the argument has type 'enum enum_long *', which differs in signedness}}189 scanf("%lx", x); // expected-warning{{format specifies type 'unsigned long *' but the argument has type 'enum enum_long *', which differs in signedness}}190}191 192void test_scanf_enum_unsigned_long(enum enum_unsigned_long *x) {193 scanf("%ld", x); // expected-warning{{format specifies type 'long *' but the argument has type 'enum enum_unsigned_long *', which differs in signedness}}194 scanf("%lu", x); // no-warning195 scanf("%lx", x); // no-warning196}197#endif198 199// Verify that we get no warnings from <inttypes.h>200 201typedef short int int16_t;202typedef unsigned short int uint16_t;203 204void test_printf_priX16(int16_t x) {205 printf("PRId16: %" "d" /*PRId16*/ "\n", x); // no-warning206 printf("PRIi16: %" "i" /*PRIi16*/ "\n", x); // no-warning207}208 209void test_printf_unsigned_priX16(uint16_t x) {210 printf("PRIo16: %" "o" /*PRIo16*/ "\n", x); // no-warning211 printf("PRIu16: %" "u" /*PRIu16*/ "\n", x); // no-warning212 printf("PRIx16: %" "x" /*PRIx16*/ "\n", x); // no-warning213 printf("PRIX16: %" "X" /*PRIX16*/ "\n", x); // no-warning214}215 216// Verify that we can suppress a -Wformat-signedness warning by ignoring217// -Wformat (gcc compat).218void test_suppress(int x)219{220#pragma GCC diagnostic ignored "-Wformat"221 printf("%u", x);222}223