re PR c/59855 (Support sparse-style __attribute__((designated_init)) on structures...
authorTom Tromey <tromey@redhat.com>
Wed, 30 Jul 2014 15:02:59 +0000 (15:02 +0000)
committerTom Tromey <tromey@gcc.gnu.org>
Wed, 30 Jul 2014 15:02:59 +0000 (15:02 +0000)
2014-07-30  Tom Tromey  <tromey@redhat.com>

PR c/59855
* doc/invoke.texi (Warning Options): Document -Wdesignated-init.
* doc/extend.texi (Type Attributes): Document designated_init
attribute.

2014-07-30  Tom Tromey  <tromey@redhat.com>

PR c/59855
* c.opt (Wdesignated-init): New option.
* c-common.c (c_common_attribute_table): Add "designated_init".
(handle_designated_init): New function.

2014-07-30  Tom Tromey  <tromey@redhat.com>

* c-typeck.c (struct constructor_stack) <designator_depth>: New
field.
(really_start_incremental_init, push_init_level): Initialize
designator_depth.
(pop_init_level): Set global designator_depth.
(process_init_element): Check for designated_init attribute.

From-SVN: r213293

gcc/ChangeLog
gcc/c-family/ChangeLog
gcc/c-family/c-common.c
gcc/c-family/c.opt
gcc/c/ChangeLog
gcc/c/c-typeck.c
gcc/doc/extend.texi
gcc/doc/invoke.texi
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/Wdesignated-init.c [new file with mode: 0644]

index de722907770c614672a05ecf47cc5ee1dec71df4..888217de1bf7bad9218cf11adec9abf748cef7fe 100644 (file)
@@ -1,3 +1,10 @@
+2014-07-30  Tom Tromey  <tromey@redhat.com>
+
+       PR c/59855
+       * doc/invoke.texi (Warning Options): Document -Wdesignated-init.
+       * doc/extend.texi (Type Attributes): Document designated_init
+       attribute.
+
 2014-07-30  Roman Gareev  <gareevroman@gmail.com>
 
        * graphite-isl-ast-to-gimple.c:
index 81ec2568af24ce3577231cabe3c3092107cfa8a0..31d10b070c2cf508fdc0d548dfc3b384e4e1e9b1 100644 (file)
@@ -1,3 +1,10 @@
+2014-07-30  Tom Tromey  <tromey@redhat.com>
+
+       PR c/59855
+       * c.opt (Wdesignated-init): New option.
+       * c-common.c (c_common_attribute_table): Add "designated_init".
+       (handle_designated_init): New function.
+
 2014-07-24  Marek Polacek  <polacek@redhat.com>
 
        PR c/57653
index ee89fca78d51da5674eb67d9508866935cf77b5b..79d0f2f214ff00a50021a2e4ac349a1409a4ec70 100644 (file)
@@ -380,6 +380,7 @@ static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
                                               bool *);
 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
                                                 bool *);
+static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
 
 static void check_function_nonnull (tree, int, tree *);
 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
@@ -773,6 +774,8 @@ const struct attribute_spec c_common_attribute_table[] =
                              handle_alloc_align_attribute, false },
   { "assume_aligned",        1, 2, false, true, true,
                              handle_assume_aligned_attribute, false },
+  { "designated_init",        0, 0, false, true, false,
+                             handle_designated_init_attribute, false },
   { NULL,                     0, 0, false, false, false, NULL, false }
 };
 
@@ -9275,6 +9278,21 @@ handle_returns_nonnull_attribute (tree *node, tree, tree, int,
   return NULL_TREE;
 }
 
+/* Handle a "designated_init" attribute; arguments as in
+   struct attribute_spec.handler.  */
+
+static tree
+handle_designated_init_attribute (tree *node, tree name, tree, int,
+                                 bool *no_add_attrs)
+{
+  if (TREE_CODE (*node) != RECORD_TYPE)
+    {
+      error ("%qE attribute is only valid on %<struct%> type", name);
+      *no_add_attrs = true;
+    }
+  return NULL_TREE;
+}
+
 \f
 /* Check for valid arguments being passed to a function with FNTYPE.
    There are NARGS arguments in the array ARGARRAY.  */
index c318cad951d2b726ef63f14d782d0b96f9e981a4..ac3eb440372d290860ffde53d39921bdf3b401f0 100644 (file)
@@ -359,6 +359,10 @@ Wdeprecated
 C C++ ObjC ObjC++ Var(warn_deprecated) Init(1) Warning
 Warn if a deprecated compiler feature, class, method, or field is used
 
+Wdesignated-init
+C ObjC Var(warn_designated_init) Init(1) Warning
+Warn about positional initialization of structs requiring designated initializers
+
 Wdiscarded-qualifiers
 C ObjC Var(warn_discarded_qualifiers) Init(1) Warning
 Warn if type qualifiers on pointers are discarded
index 010649798972d95c3d2a0fe0edad14780e41709f..b5e7a177672262a211f359dc8d2a448082907115 100644 (file)
@@ -1,3 +1,12 @@
+2014-07-30  Tom Tromey  <tromey@redhat.com>
+
+       * c-typeck.c (struct constructor_stack) <designator_depth>: New
+       field.
+       (really_start_incremental_init, push_init_level): Initialize
+       designator_depth.
+       (pop_init_level): Set global designator_depth.
+       (process_init_element): Check for designated_init attribute.
+
 2014-07-20  Marek Polacek  <polacek@redhat.com>
 
        PR c/61852
index 06fd565f7708f2c8c9578199dd98f36e018a2442..a93893319b9a328a6c1a6c3200cfb2180ef4d2d1 100644 (file)
@@ -6956,6 +6956,7 @@ struct constructor_stack
   char outer;
   char incremental;
   char designated;
+  int designator_depth;
 };
 
 static struct constructor_stack *constructor_stack;
@@ -7127,6 +7128,7 @@ really_start_incremental_init (tree type)
   p->outer = 0;
   p->incremental = constructor_incremental;
   p->designated = constructor_designated;
+  p->designator_depth = designator_depth;
   p->next = 0;
   constructor_stack = p;
 
@@ -7276,6 +7278,7 @@ push_init_level (location_t loc, int implicit,
   p->outer = 0;
   p->incremental = constructor_incremental;
   p->designated = constructor_designated;
+  p->designator_depth = designator_depth;
   p->next = constructor_stack;
   p->range_stack = 0;
   constructor_stack = p;
@@ -7583,6 +7586,7 @@ pop_init_level (location_t loc, int implicit,
   constructor_erroneous = p->erroneous;
   constructor_incremental = p->incremental;
   constructor_designated = p->designated;
+  designator_depth = p->designator_depth;
   constructor_pending_elts = p->pending_elts;
   constructor_depth = p->depth;
   if (!p->implicit)
@@ -8652,6 +8656,15 @@ process_init_element (location_t loc, struct c_expr value, bool implicit,
   if (constructor_type == 0)
     return;
 
+  if (!implicit && warn_designated_init && !was_designated
+      && TREE_CODE (constructor_type) == RECORD_TYPE
+      && lookup_attribute ("designated_init",
+                          TYPE_ATTRIBUTES (constructor_type)))
+    warning_init (loc,
+                 OPT_Wdesignated_init,
+                 "positional initialization of field "
+                 "in %<struct%> declared with %<designated_init%> attribute");
+
   /* If we've exhausted any levels that didn't have braces,
      pop them now.  */
   while (constructor_stack->implicit)
index 53fab8d21e699264aae1f5d4bd26dde3926975e3..591aaeba3e3d1dfb3c16a7d0c90cfdfeaa989639 100644 (file)
@@ -5890,6 +5890,17 @@ and caught in another, the class must have default visibility.
 Otherwise the two shared objects are unable to use the same
 typeinfo node and exception handling will break.
 
+@item designated_init
+This attribute may only be applied to structure types.  It indicates
+that any initialization of an object of this type must use designated
+initializers rather than positional initializers.  The intent of this
+attribute is to allow the programmer to indicate that a structure's
+layout may change, and that therefore relying on positional
+initialization will result in future breakage.
+
+GCC emits warnings based on this attribute by default; use
+@option{-Wno-designated-init} to suppress them.
+
 @end table
 
 To specify multiple attributes, separate them by commas within the
index 7cebb9e4d7a8ee235ede27357c13c50ef4e30d11..5dfa5868108cbbc1461935479b5d280dfdc07ad9 100644 (file)
@@ -244,8 +244,8 @@ Objective-C and Objective-C++ Dialects}.
 -Wc++-compat -Wc++11-compat -Wcast-align  -Wcast-qual  @gol
 -Wchar-subscripts -Wclobbered  -Wcomment -Wconditionally-supported  @gol
 -Wconversion -Wcoverage-mismatch -Wdate-time -Wdelete-incomplete -Wno-cpp  @gol
--Wno-deprecated -Wno-deprecated-declarations -Wdisabled-optimization  @gol
--Wno-discarded-qualifiers @gol
+-Wno-deprecated -Wno-deprecated-declarations -Wno-designated-init @gol
+-Wdisabled-optimization -Wno-discarded-qualifiers @gol
 -Wno-div-by-zero -Wdouble-promotion -Wempty-body  -Wenum-compare @gol
 -Wno-endif-labels -Werror  -Werror=* @gol
 -Wfatal-errors  -Wfloat-equal  -Wformat  -Wformat=2 @gol
@@ -5180,6 +5180,12 @@ a suffix.  When used together with @option{-Wsystem-headers} it
 warns about such constants in system header files.  This can be useful
 when preparing code to use with the @code{FLOAT_CONST_DECIMAL64} pragma
 from the decimal floating-point extension to C99.
+
+@item -Wno-designated-init @r{(C and Objective-C only)}
+Suppress warnings when a positional initializer is used to initialize
+a structure that has been marked with the @code{designated_init}
+attribute.
+
 @end table
 
 @node Debugging Options
index 93511d5216149dab30e4744a7734ef1748725fbd..a334ad71f382cae6f6de3df3073811add3f0da10 100644 (file)
@@ -1,3 +1,8 @@
+2014-07-30  Tom Tromey  <tromey@redhat.com>
+
+       PR c/59855
+       * gcc.dg/Wdesignated-init.c: New file.
+
 2014-07-29  Jan Hubicka  <hubicka@ucw.cz>
 
        * g++.dg/ipa/devirt-34.C: New testcase.
diff --git a/gcc/testsuite/gcc.dg/Wdesignated-init.c b/gcc/testsuite/gcc.dg/Wdesignated-init.c
new file mode 100644 (file)
index 0000000..b9ca572
--- /dev/null
@@ -0,0 +1,107 @@
+/* PR c/59855 */
+/* { dg-do compile } */
+/* { dg-options "-std=gnu99" } */
+
+typedef int vvv  __attribute__((designated_init)); /* { dg-error "only valid" } */
+
+union U {
+  int a;
+  double b;
+} __attribute__((designated_init)); /* { dg-error "only valid" } */
+
+enum E { ONE, TWO }  __attribute__((designated_init)); /* { dg-error "only valid" } */
+
+struct Pok {
+  int x;
+  int y;
+};
+
+struct Des {
+  int x;
+  int y;
+} __attribute__ ((designated_init));
+
+struct Des d1 = { 5, 5 }; /* { dg-warning "(positional|near initialization)" } */
+struct Des d2 = { .x = 5, .y = 5 };
+struct Des d3 = { .x = 5, 5 }; /* { dg-warning "(positional|near initialization)" } */
+
+struct Des fd1 (void)
+{
+  return (struct Des) { 5, 5 }; /* { dg-warning "(positional|near initialization)" } */
+}
+
+struct Des fd2 (void)
+{
+  return (struct Des) { .x = 5, .y = 5 };
+}
+
+struct Des fd3 (void)
+{
+  return (struct Des) { .x = 5, 5 }; /* { dg-warning "(positional|near initialization)" } */
+}
+
+struct Wrap {
+  struct Pok p;
+  struct Des d;
+} __attribute__ ((designated_init));
+
+struct Wrap w1 = { { 0, 1 }, { 2, 3} }; /* { dg-warning "(positional|near initialization)" } */
+struct Wrap w2 = { .p = { 0, 1 }, { 2, 3} }; /* { dg-warning "(positional|near initialization)" } */
+struct Wrap w3 = { .p = { 0, 1 }, .d = { 2, 3} }; /* { dg-warning "(positional|near initialization)" } */
+struct Wrap w4 = { { 0, 1 }, .d = { 2, 3} }; /* { dg-warning "(positional|near initialization)" } */
+struct Wrap w5 = { .p = { 0, 1 }, .d = { .x = 2, .y = 3} };
+
+struct Wrap w6 = { { 0, 1 }, .d.x = 2, .d.y = 3 }; /* { dg-warning "(positional|near initialization)" } */
+struct Wrap w7 = { .p = { 0, 1 }, .d.x = 2, .d.y = 3 };
+struct Wrap w8 = { .p = { 0, 1 }, .d = { 2, 0 }, .d.y = 3 }; /* { dg-warning "(positional|near initialization)" } */
+struct Wrap w9 = { .p = { 0, 1 }, .d = { .x = 2 }, .d.y = 3 };
+
+struct Wrap fw1 (void)
+{
+  return (struct Wrap) { { 0, 1 }, { 2, 3} }; /* { dg-warning "(positional|near initialization)" } */
+};
+
+struct Wrap fw2 (void)
+{
+  return (struct Wrap) { .p = { 0, 1 }, { 2, 3} }; /* { dg-warning "(positional|near initialization)" } */
+}
+
+struct Wrap fw3 (void)
+{
+  return (struct Wrap) { .p = { 0, 1 }, .d = { 2, 3} }; /* { dg-warning "(positional|near initialization)" } */
+}
+
+struct Wrap fw4 (void)
+{
+  return (struct Wrap) { { 0, 1 }, .d = { 2, 3} }; /* { dg-warning "(positional|near initialization)" } */
+}
+
+struct Wrap fw5 (void)
+{
+  return (struct Wrap) { .p = { 0, 1 }, .d = { .x = 2, .y = 3} };
+}
+
+struct Wrap fw6 (void)
+{
+  return (struct Wrap) { { 0, 1 }, .d.x = 2, .d.y = 3 }; /* { dg-warning "(positional|near initialization)" } */
+}
+
+struct Wrap fw7 (void)
+{
+  return (struct Wrap) { .p = { 0, 1 }, .d.x = 2, .d.y = 3 };
+}
+
+struct Wrap fw8 (void)
+{
+  return (struct Wrap) { .p = { 0, 1 }, .d = { 2, 0 }, .d.y = 3 }; /* { dg-warning "(positional|near initialization)" } */
+}
+
+struct Wrap fw9 (void)
+{
+  return (struct Wrap) { .p = { 0, 1 }, .d = { .x = 2 }, .d.y = 3 };
+}
+
+struct Des da[] = {
+  { .x = 1, .y = 2 },
+  { 5, 5 } /* { dg-warning "(positional|near initialization)" } */
+};