This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] add __attribute__ ((designated_init))
- From: Tom Tromey <tromey at redhat dot com>
- To: gcc-patches at gcc dot gnu dot org
- Date: Mon, 20 Jan 2014 13:42:45 -0700
- Subject: [PATCH] add __attribute__ ((designated_init))
- Authentication-results: sourceware.org; auth=none
This adds __attribute__((designated_init)) and -Wdesignated-init, a
feature from sparse. This is also PR c/59855.
The name of the attribute and the option were chosen to be compatible
with sparse. I added this warning to -Wall since it seemed like the
kind of warning users would ordinarily want -- if they are not using
the attribute, they won't ever see it anyhow.
I wrote a new test case covering the same cases that the sparse test
case covers. I also added tests for applying the attribute to
non-struct types; note that in this case sparse issues a warning but
gcc issues an error. I think an error is more appropriate.
Built and regtested on x86-64 Fedora 20. It's been a while but I
think I did it properly.
Tom
---
gcc/ChangeLog | 7 +++
gcc/c-family/ChangeLog | 7 +++
gcc/c-family/c-common.c | 17 +++++++
gcc/c-family/c.opt | 4 ++
gcc/c/ChangeLog | 9 ++++
gcc/c/c-typeck.c | 12 +++++
gcc/doc/extend.texi | 11 +++++
gcc/doc/invoke.texi | 10 +++-
gcc/testsuite/ChangeLog | 5 ++
gcc/testsuite/gcc.dg/Wdesignated-init.c | 82 +++++++++++++++++++++++++++++++++
10 files changed, 163 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/gcc.dg/Wdesignated-init.c
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 652d2c9..77e4d54 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,10 @@
+2014-01-20 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-01-19 John David Anglin <danglin@gcc.gnu.org>
* config/pa/pa.c (pa_attr_length_millicode_call): Correct length of
diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog
index fede01f..7593791 100644
--- a/gcc/c-family/ChangeLog
+++ b/gcc/c-family/ChangeLog
@@ -1,3 +1,10 @@
+2014-01-20 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-01-15 Laurent Alfonsi <laurent.alfonsi@st.com>
PR c++/49718
diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c
index 35958ea..e0a0653 100644
--- a/gcc/c-family/c-common.c
+++ b/gcc/c-family/c-common.c
@@ -377,6 +377,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 (tree *, tree, tree, int, bool *);
static void check_function_nonnull (tree, int, tree *);
static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
@@ -766,6 +767,8 @@ const struct attribute_spec c_common_attribute_table[] =
handle_omp_declare_simd_attribute, false },
{ "omp declare target", 0, 0, true, false, false,
handle_omp_declare_target_attribute, false },
+ { "designated_init", 0, 0, false, true, false,
+ handle_designated_init, false },
{ NULL, 0, 0, false, false, false, NULL, false }
};
@@ -9133,6 +9136,20 @@ 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 (tree *node, tree, tree, int, bool *no_add_attrs)
+{
+ if (TREE_CODE (*node) != RECORD_TYPE)
+ {
+ error ("designated_init attribute is only valid on struct type");
+ *no_add_attrs = true;
+ }
+ return NULL_TREE;
+}
+
/* Check for valid arguments being passed to a function with FNTYPE.
There are NARGS arguments in the array ARGARRAY. */
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 38ae58e..29ac4d0 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -355,6 +355,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(0) Warning LangEnabledBy(C ObjC,Wall)
+Warn about positional initialization of structs requiring designated initializers
+
Wdiv-by-zero
C ObjC C++ ObjC++ Var(warn_div_by_zero) Init(1) Warning
Warn about compile-time integer division by zero
diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog
index 4754bdf..c34fe93 100644
--- a/gcc/c/ChangeLog
+++ b/gcc/c/ChangeLog
@@ -1,3 +1,12 @@
+2014-01-20 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-01-15 Jakub Jelinek <jakub@redhat.com>
PR c/58943
diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c
index 09049e2..31a1f5a 100644
--- a/gcc/c/c-typeck.c
+++ b/gcc/c/c-typeck.c
@@ -6885,6 +6885,7 @@ struct constructor_stack
char outer;
char incremental;
char designated;
+ int designator_depth;
};
static struct constructor_stack *constructor_stack;
@@ -7056,6 +7057,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;
@@ -7201,6 +7203,7 @@ push_init_level (int implicit, struct obstack * braced_init_obstack)
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;
@@ -7501,6 +7504,7 @@ pop_init_level (int implicit, struct obstack * braced_init_obstack)
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)
@@ -8558,6 +8562,14 @@ process_init_element (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 (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)
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index 2f4f91d..7cefeb1 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -5742,6 +5742,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 struct 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 only emits a warning for this attribute when
+@option{-Wdesignated-init} is given.
+
@end table
To specify multiple attributes, separate them by commas within the
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index ac4b597..fd5fb18 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -241,7 +241,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-deprecated -Wno-deprecated-declarations @gol
+-Wdesignated-init -Wdisabled-optimization @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
@@ -3340,6 +3341,7 @@ Options} and @ref{Objective-C and Objective-C++ Dialect Options}.
-Warray-bounds @r{(only with} @option{-O2}@r{)} @gol
-Wc++11-compat @gol
-Wchar-subscripts @gol
+-Wdesignated-init @gol
-Wenum-compare @r{(in C/ObjC; this is on by default in C++)} @gol
-Wimplicit-int @r{(C and Objective-C only)} @gol
-Wimplicit-function-declaration @r{(C and Objective-C only)} @gol
@@ -5094,6 +5096,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 -Wdesignated-init @r{(C and Objective-C only)}
+Issue a warning 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
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 665280c..57eea75 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2014-01-20 Tom Tromey <tromey@redhat.com>
+
+ PR c/59855
+ * gcc.dg/Wdesignated-init.c: New file.
+
2014-01-19 Uros Bizjak <ubizjak@gmail.com>
* g++.dg/pr49718.C: Add "-mno-explicit-relocs" for alpha*-*-* targets.
diff --git a/gcc/testsuite/gcc.dg/Wdesignated-init.c b/gcc/testsuite/gcc.dg/Wdesignated-init.c
new file mode 100644
index 0000000..72cda8b
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wdesignated-init.c
@@ -0,0 +1,82 @@
+/* PR c/59855 */
+/* { dg-do compile } */
+/* { dg-options "-Wdesignated-init" } */
+
+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 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 Des da[] = {
+ { .x = 1, .y = 2 },
+ { 5, 5 } /* { dg-warning "(positional|near initialization)" } */
+};
--
1.8.4.2