]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
PR c/71115 - [5/6 Regression] Missing warning: excess elements in struct initializer
authorMartin Sebor <msebor@redhat.com>
Sat, 5 Nov 2016 23:57:13 +0000 (23:57 +0000)
committerMartin Sebor <msebor@gcc.gnu.org>
Sat, 5 Nov 2016 23:57:13 +0000 (17:57 -0600)
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
gcc/c/c-typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/Woverride-init-1.c
gcc/testsuite/gcc.dg/Woverride-init-2.c
gcc/testsuite/gcc.dg/init-excess-2.c [new file with mode: 0644]

index d9a8b4e8767d6a2990e84f9e54d642c2ac6e3211..85f9e9a0f5b39ea86b5ebb670ce3aba8c1d66e28 100644 (file)
@@ -1,3 +1,11 @@
+2016-11-05  Martin Sebor  <msebor@redhat.com>
+
+       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.
index be4967f7ee930206a2f5e566bfb20ab87cb68fff..15a1fc9b315f05faaffcf0324788ef983b5a594f 100644 (file)
@@ -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);
 }
 \f
 /* If TYPE is an array type and EXPR is a parenthesized string
index f6ff62f2f819f2800c7f6f0a535d44290adcb6e8..ddabb4a264bf9e840329fc2f4d07b1e8930a3599 100644 (file)
@@ -1,3 +1,11 @@
+2016-11-05  Martin Sebor  <msebor@redhat.com>
+
+       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  <mliska@suse.cz>
 
        Backport from mainline
index 29eca3095cdc696ac107a204b97ec59402a685f5..b01d8a29dcccc5e6a3fd2cf44cbc2aa78ac26459 100644 (file)
@@ -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
 };
index c5490b5ad3500cc5bccd2b27cb277edb2f70750e..d0ece89eb6d71f68beb300aa8b246df9571c1855 100644 (file)
@@ -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 (file)
index 0000000..0e91b60
--- /dev/null
@@ -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 <stddef.h>
+
+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" } */
+};