From be4aa83d6ff4720fffd2c9f62c6e3082ece1937f Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Fri, 13 Jan 2017 19:27:43 +0000 Subject: [PATCH] Don't suppress bogus usage of macros from system headers in -Wformat (PR c/78304) gcc/ChangeLog: PR c/78304 * substring-locations.c (format_warning_va): Strengthen case 1 so that both endpoints of the substring must be within the format range for just the substring to be printed. gcc/testsuite/ChangeLog: PR c/78304 * gcc.dg/format/diagnostic-ranges.c (test_macro): Undef INT_FMT. (test_macro_2): New test. (test_macro_3): New test. (test_macro_4): New test. (test_non_contiguous_strings): Convert line number to line offset. * gcc.dg/format/pr78304-2.c: New test case. * gcc.dg/format/pr78304.c: New test case. From-SVN: r244453 --- gcc/ChangeLog | 7 +++ gcc/substring-locations.c | 2 + gcc/testsuite/ChangeLog | 11 ++++ .../gcc.dg/format/diagnostic-ranges.c | 53 ++++++++++++++++++- gcc/testsuite/gcc.dg/format/pr78304-2.c | 11 ++++ gcc/testsuite/gcc.dg/format/pr78304.c | 10 ++++ 6 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/format/pr78304-2.c create mode 100644 gcc/testsuite/gcc.dg/format/pr78304.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index b303681d2f2..3c74b193528 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2017-01-13 David Malcolm + + PR c/78304 + * substring-locations.c (format_warning_va): Strengthen case 1 so + that both endpoints of the substring must be within the format + range for just the substring to be printed. + 2017-01-13 Uros Bizjak * config/i386/i386.opt (msgx): Use ix86_isa_flags2 variable. diff --git a/gcc/substring-locations.c b/gcc/substring-locations.c index 8b41f2b82cf..e2d8dd7ea88 100644 --- a/gcc/substring-locations.c +++ b/gcc/substring-locations.c @@ -118,6 +118,8 @@ format_warning_va (const substring_loc &fmt_loc, else { if (fmt_substring_range.m_start >= fmt_loc_range.m_start + && fmt_substring_range.m_start <= fmt_loc_range.m_finish + && fmt_substring_range.m_finish >= fmt_loc_range.m_start && fmt_substring_range.m_finish <= fmt_loc_range.m_finish) /* Case 1. */ { diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index f9b61882e58..76dfd7e4747 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,14 @@ +2017-01-13 David Malcolm + + PR c/78304 + * gcc.dg/format/diagnostic-ranges.c (test_macro): Undef INT_FMT. + (test_macro_2): New test. + (test_macro_3): New test. + (test_macro_4): New test. + (test_non_contiguous_strings): Convert line number to line offset. + * gcc.dg/format/pr78304-2.c: New test case. + * gcc.dg/format/pr78304.c: New test case. + 2017-01-13 Uros Bizjak * gcc.target/i386/funcspec-56.inc: Add missing options and diff --git a/gcc/testsuite/gcc.dg/format/diagnostic-ranges.c b/gcc/testsuite/gcc.dg/format/diagnostic-ranges.c index e5e6ade6bbe..cab30f2d7f6 100644 --- a/gcc/testsuite/gcc.dg/format/diagnostic-ranges.c +++ b/gcc/testsuite/gcc.dg/format/diagnostic-ranges.c @@ -254,12 +254,63 @@ void test_macro (const char *msg) ~^ %s { dg-end-multiline-output "" } */ +#undef INT_FMT +} + +void test_macro_2 (const char *msg) +{ +#define PRIu32 "u" /* { dg-message "17: format string is defined here" } */ + printf("hello %" PRIu32 " world", msg); /* { dg-warning "10: format '%u' expects argument of type 'unsigned int', but argument 2 has type 'const char \\*' " } */ +/* { dg-begin-multiline-output "" } + printf("hello %" PRIu32 " world", msg); + ^~~~~~~~~ + { dg-end-multiline-output "" } */ +/* { dg-begin-multiline-output "" } + #define PRIu32 "u" + ^ + { dg-end-multiline-output "" } */ +#undef PRIu32 +} + +void test_macro_3 (const char *msg) +{ +#define FMT_STRING "hello %i world" /* { dg-warning "20: format '%i' expects argument of type 'int', but argument 2 has type 'const char \\*' " } */ + printf(FMT_STRING, msg); /* { dg-message "10: in expansion of macro 'FMT_STRING" } */ +/* { dg-begin-multiline-output "" } + #define FMT_STRING "hello %i world" + ^ + { dg-end-multiline-output "" } */ +/* { dg-begin-multiline-output "" } + printf(FMT_STRING, msg); + ^~~~~~~~~~ + { dg-end-multiline-output "" } */ +#undef FMT_STRING +} + +void test_macro_4 (const char *msg) +{ +#define FMT_STRING "hello %i world" /* { dg-warning "20: format '%i' expects argument of type 'int', but argument 2 has type 'const char \\*' " } */ + printf(FMT_STRING "\n", msg); /* { dg-message "10: in expansion of macro 'FMT_STRING" } */ +/* { dg-begin-multiline-output "" } + #define FMT_STRING "hello %i world" + ^ + { dg-end-multiline-output "" } */ +/* { dg-begin-multiline-output "" } + printf(FMT_STRING "\n", msg); + ^~~~~~~~~~ + { dg-end-multiline-output "" } */ +/* { dg-begin-multiline-output "" } + #define FMT_STRING "hello %i world" + ~^ + %s + { dg-end-multiline-output "" } */ +#undef FMT_STRING } void test_non_contiguous_strings (void) { __builtin_printf(" %" "d ", 0.5); /* { dg-warning "20: format .%d. expects argument of type .int., but argument 2 has type .double." } */ - /* { dg-message "26: format string is defined here" "" { target *-*-* } 261 } */ + /* { dg-message "26: format string is defined here" "" { target *-*-* } .-1 } */ /* { dg-begin-multiline-output "" } __builtin_printf(" %" "d ", 0.5); ^~~~ diff --git a/gcc/testsuite/gcc.dg/format/pr78304-2.c b/gcc/testsuite/gcc.dg/format/pr78304-2.c new file mode 100644 index 00000000000..5ee6d659ea2 --- /dev/null +++ b/gcc/testsuite/gcc.dg/format/pr78304-2.c @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -Wall -Wextra" } */ + +extern int printf (const char *, ...); + +# define PRIu32 "u" + +void test (long size) +{ + printf ("size: %" PRIu32 "\n", size); /* { dg-warning "expects argument of type" } */ +} diff --git a/gcc/testsuite/gcc.dg/format/pr78304.c b/gcc/testsuite/gcc.dg/format/pr78304.c new file mode 100644 index 00000000000..d0a96f6ac4b --- /dev/null +++ b/gcc/testsuite/gcc.dg/format/pr78304.c @@ -0,0 +1,10 @@ +/* { dg-do compile { target inttypes_types } } */ +/* { dg-options "-O2 -Wall -Wextra" } */ + +#include +#include + +void test (size_t size) +{ + printf ("size: %" PRIu32 "\n", size); /* { dg-warning "expects argument of type" } */ +} -- 2.30.2