From dbc2458bd2b73ecce6af99528fb4db7fe7c854b9 Mon Sep 17 00:00:00 2001 From: Martin Sebor Date: Sat, 5 Nov 2016 23:57:13 +0000 Subject: [PATCH] PR c/71115 - [5/6 Regression] Missing warning: excess elements in struct initializer PR c/71115 - [5/6 Regression] Missing warning: excess elements in struct initializer Backport from trunk. gcc/c/ChangeLog: * c-typeck.c (error_init): Use expansion_point_location_if_in_system_header. (warning_init): Same. gcc/testsuite/ChangeLog: * gcc.dg/init-excess-2.c: New test. * gcc.dg/Woverride-init-1.c: Adjust. * gcc.dg/Woverride-init-2.c: Same. From-SVN: r241878 --- gcc/c/ChangeLog | 8 +++++ gcc/c/c-typeck.c | 20 ++++++++--- gcc/testsuite/ChangeLog | 8 +++++ gcc/testsuite/gcc.dg/Woverride-init-1.c | 10 +++--- gcc/testsuite/gcc.dg/Woverride-init-2.c | 10 +++--- gcc/testsuite/gcc.dg/init-excess-2.c | 48 +++++++++++++++++++++++++ 6 files changed, 89 insertions(+), 15 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/init-excess-2.c diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog index d9a8b4e8767d..85f9e9a0f5b3 100644 --- a/gcc/c/ChangeLog +++ b/gcc/c/ChangeLog @@ -1,3 +1,11 @@ +2016-11-05 Martin Sebor + + Backport from trunk. + PR c/71115 + * c-typeck.c (error_init): Use + expansion_point_location_if_in_system_header. + (warning_init): Same. + 2016-06-03 Release Manager * GCC 5.4.0 released. diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index be4967f7ee93..15a1fc9b315f 100644 --- a/gcc/c/c-typeck.c +++ b/gcc/c/c-typeck.c @@ -5632,16 +5632,21 @@ error_init (location_t loc, const char *gmsgid) component name is taken from the spelling stack. */ static void -pedwarn_init (location_t location, int opt, const char *gmsgid) +pedwarn_init (location_t loc, int opt, const char *gmsgid) { char *ofwhat; bool warned; + /* Use the location where a macro was expanded rather than where + it was defined to make sure macros defined in system headers + but used incorrectly elsewhere are diagnosed. */ + source_location exploc = expansion_point_location_if_in_system_header (loc); + /* The gmsgid may be a format string with %< and %>. */ - warned = pedwarn (location, opt, gmsgid); + warned = pedwarn (exploc, opt, gmsgid); ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); if (*ofwhat && warned) - inform (location, "(near initialization for %qs)", ofwhat); + inform (exploc, "(near initialization for %qs)", ofwhat); } /* Issue a warning for a bad initializer component. @@ -5656,11 +5661,16 @@ warning_init (location_t loc, int opt, const char *gmsgid) char *ofwhat; bool warned; + /* Use the location where a macro was expanded rather than where + it was defined to make sure macros defined in system headers + but used incorrectly elsewhere are diagnosed. */ + source_location exploc = expansion_point_location_if_in_system_header (loc); + /* The gmsgid may be a format string with %< and %>. */ - warned = warning_at (loc, opt, gmsgid); + warned = warning_at (exploc, opt, gmsgid); ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); if (*ofwhat && warned) - inform (loc, "(near initialization for %qs)", ofwhat); + inform (exploc, "(near initialization for %qs)", ofwhat); } /* If TYPE is an array type and EXPR is a parenthesized string diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index f6ff62f2f819..ddabb4a264bf 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,11 @@ +2016-11-05 Martin Sebor + + Backport from trunk. + PR c/71115 + * gcc.dg/init-excess-2.c: New test. + * gcc.dg/Woverride-init-1.c: Adjust. + * gcc.dg/Woverride-init-2.c: Same. + 2016-11-03 Martin Liska Backport from mainline diff --git a/gcc/testsuite/gcc.dg/Woverride-init-1.c b/gcc/testsuite/gcc.dg/Woverride-init-1.c index 29eca3095cdc..b01d8a29dccc 100644 --- a/gcc/testsuite/gcc.dg/Woverride-init-1.c +++ b/gcc/testsuite/gcc.dg/Woverride-init-1.c @@ -10,19 +10,19 @@ union u { char a; long long b; }; struct s s0 = { .a = 1, .b = 2, - .a = 3, /* { dg-warning "initialized field overwritten|near init" } */ - 4, /* { dg-warning "initialized field overwritten|near init" } */ + .a = 3, /* { dg-warning "initialized field overwritten" } */ + 4, /* { dg-warning "initialized field overwritten" } */ 5 }; union u u0 = { .a = 1, - .b = 2, /* { dg-warning "initialized field overwritten|near init" } */ - .a = 3 }; /* { dg-warning "initialized field overwritten|near init" } */ + .b = 2, /* { dg-warning "initialized field overwritten" } */ + .a = 3 }; /* { dg-warning "initialized field overwritten" } */ int a[5] = { [0] = 1, [1] = 2, - [0] = 3, /* { dg-warning "initialized field overwritten|near init" } */ + [0] = 3, /* { dg-warning "initialized field overwritten" } */ [2] = 4 }; diff --git a/gcc/testsuite/gcc.dg/Woverride-init-2.c b/gcc/testsuite/gcc.dg/Woverride-init-2.c index c5490b5ad350..d0ece89eb6d7 100644 --- a/gcc/testsuite/gcc.dg/Woverride-init-2.c +++ b/gcc/testsuite/gcc.dg/Woverride-init-2.c @@ -10,19 +10,19 @@ union u { char a; long long b; }; struct s s0 = { .a = 1, .b = 2, - .a = 3, /* { dg-warning "initialized field overwritten|near init" } */ - 4, /* { dg-warning "initialized field overwritten|near init" } */ + .a = 3, /* { dg-warning "initialized field overwritten" } */ + 4, /* { dg-warning "initialized field overwritten" } */ 5 }; union u u0 = { .a = 1, - .b = 2, /* { dg-warning "initialized field overwritten|near init" } */ - .a = 3 }; /* { dg-warning "initialized field overwritten|near init" } */ + .b = 2, /* { dg-warning "initialized field overwritten" } */ + .a = 3 }; /* { dg-warning "initialized field overwritten" } */ int a[5] = { [0] = 1, [1] = 2, - [0] = 3, /* { dg-warning "initialized field overwritten|near init" } */ + [0] = 3, /* { dg-warning "initialized field overwritten" } */ [2] = 4 }; diff --git a/gcc/testsuite/gcc.dg/init-excess-2.c b/gcc/testsuite/gcc.dg/init-excess-2.c new file mode 100644 index 000000000000..0e91b60950b3 --- /dev/null +++ b/gcc/testsuite/gcc.dg/init-excess-2.c @@ -0,0 +1,48 @@ +/* Test for diagnostics about excess initializers when using a macro + defined in a system header: + c/71115 - Missing warning: excess elements in struct initializer. */ +/* { dg-do compile } */ +/* { dg-options "" } */ +/* { dg-require-effective-target int32plus } */ + +#include + +int* a[1] = { + 0, + NULL /* { dg-warning "excess elements|near init" } */ +}; + +const char str[1] = { + 0, + NULL /* { dg-warning "excess elements|near init" } */ +}; + +struct S { + int *a; +} s = { + 0, + NULL /* { dg-warning "excess elements|near init" } */ +}; + +struct __attribute__ ((designated_init)) S2 { + int *a; +} s2 = { + NULL /* { dg-warning "positional initialization|near init" } */ +}; + +union U { + int *a; +} u = { + 0, + NULL /* { dg-warning "excess elements|near init" } */ +}; + +int __attribute__ ((vector_size (16))) ivec = { + 0, 0, 0, 0, + NULL /* { dg-warning "excess elements|near init" } */ +}; + +int* scal = { + 0, + NULL /* { dg-warning "excess elements|near init" } */ +}; -- 2.47.2