
The checks for the 'z' and 't' format specifiers added in the original PR #143653 had some issues and were overly strict, causing some build failures and were consequently reverted at4c85bf2fe8
. In the latest commit27c58629ec
, I relaxed the checks for the 'z' and 't' format specifiers, so warnings are now only issued when they are used with mismatched types. The original intent of these checks was to diagnose code that assumes the underlying type of `size_t` is `unsigned` or `unsigned long`, for example: ```c printf("%zu", 1ul); // Not portable, but not an error when size_t is unsigned long ``` However, it produced a significant number of false positives. This was partly because Clang does not treat the `typedef` `size_t` and `__size_t` as having a common "sugar" type, and partly because a large amount of existing code either assumes `unsigned` (or `unsigned long`) is `size_t`, or they define the equivalent of size_t in their own way (such as sanitizer_internal_defs.h).2e67dcfdcd/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h (L203)
33 lines
1.4 KiB
C
33 lines
1.4 KiB
C
// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -verify %s
|
|
|
|
int printf(char const *, ...);
|
|
|
|
#include <stddef.h>
|
|
|
|
void test(void) {
|
|
// size_t
|
|
printf("%zu", (size_t)0); // no-warning
|
|
printf("%zu", sizeof(int)); // no-warning
|
|
printf("%zu", (size_t)0 + sizeof(int)); // no-warning
|
|
printf("%zu", (double)42); // expected-warning {{format specifies type 'size_t' (aka 'unsigned long') but the argument has type 'double'}}
|
|
// intmax_t / uintmax_t
|
|
printf("%jd", (double)42); // expected-warning {{format specifies type 'intmax_t' (aka 'long') but the argument has type 'double'}}
|
|
printf("%ju", (double)42); // expected-warning {{format specifies type 'uintmax_t' (aka 'unsigned long') but the argument has type 'double'}}
|
|
|
|
// ptrdiff_t
|
|
printf("%td", (double)42); // expected-warning {{format specifies type 'ptrdiff_t' (aka 'long') but the argument has type 'double'}}
|
|
}
|
|
|
|
void test_writeback(void) {
|
|
printf("%jn", (long*)0); // no-warning
|
|
printf("%jn", (unsigned long*)0); // no-warning
|
|
printf("%jn", (int*)0); // expected-warning{{format specifies type 'intmax_t *' (aka 'long *') but the argument has type 'int *'}}
|
|
|
|
printf("%zn", (long*)0); // no-warning
|
|
// FIXME: Warn about %zn with non-ssize_t argument.
|
|
|
|
printf("%tn", (long*)0); // no-warning
|
|
printf("%tn", (unsigned long*)0); // no-warning
|
|
printf("%tn", (int*)0); // expected-warning{{format specifies type 'ptrdiff_t *' (aka 'long *') but the argument has type 'int *'}}
|
|
}
|