This patch adds fix-it hints to our warning for old-style structure member designators, showing how to modernize them to C99 form.
For example: modernize-named-inits.c:19:5: warning: obsolete use of designated initializer with ‘:’ [-Wpedantic] foo: 1, ^ . = In conjunction with the not-yet-in-trunk -fdiagnostics-generate-patch, this can generate patches like this: --- modernize-named-inits.c +++ modernize-named-inits.c @@ -16,7 +16,7 @@ /* Old-style named initializers. */ struct foo old_style_f = { - foo: 1, - bar: 2, + .foo= 1, + .bar= 2, }; Successfully bootstrapped®rtested on x86_64-pc-linux-gnu. OK for trunk? gcc/c/ChangeLog: * c-parser.c (c_parser_initelt): Provide fix-it hints for modernizing old-style structure member designator to C99 style. gcc/testsuite/ChangeLog: * gcc.dg/modernize-named-inits.c: New test case. --- gcc/c/c-parser.c | 17 +++++++----- gcc/testsuite/gcc.dg/modernize-named-inits.c | 39 ++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/modernize-named-inits.c diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c index c245e70..a6281fc 100644 --- a/gcc/c/c-parser.c +++ b/gcc/c/c-parser.c @@ -4459,13 +4459,18 @@ c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack) && c_parser_peek_2nd_token (parser)->type == CPP_COLON) { /* Old-style structure member designator. */ - set_init_label (c_parser_peek_token (parser)->location, - c_parser_peek_token (parser)->value, - c_parser_peek_token (parser)->location, - braced_init_obstack); + location_t name_loc = c_parser_peek_token (parser)->location; + tree fieldname = c_parser_peek_token (parser)->value; + location_t colon_loc = c_parser_peek_2nd_token (parser)->location; + set_init_label (name_loc, fieldname, name_loc, braced_init_obstack); /* Use the colon as the error location. */ - pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic, - "obsolete use of designated initializer with %<:%>"); + rich_location richloc (line_table, colon_loc); + /* Provide fix-it hints for converting to C99 style i.e. + from "NAME:" to ".NAME =". */ + richloc.add_fixit_insert (name_loc, "."); + richloc.add_fixit_replace (colon_loc, "="); + pedwarn_at_rich_loc (&richloc, OPT_Wpedantic, + "obsolete use of designated initializer with %<:%>"); c_parser_consume_token (parser); c_parser_consume_token (parser); } diff --git a/gcc/testsuite/gcc.dg/modernize-named-inits.c b/gcc/testsuite/gcc.dg/modernize-named-inits.c new file mode 100644 index 0000000..4e16494 --- /dev/null +++ b/gcc/testsuite/gcc.dg/modernize-named-inits.c @@ -0,0 +1,39 @@ +/* { dg-do compile } */ +/* { dg-options "-fdiagnostics-show-caret -std=c99 -pedantic" } */ + +struct foo +{ + int foo; + int bar; +}; + +union u +{ + int color; + int shape; +}; + +/* Old-style named initializers. */ + +struct foo old_style_f = { + foo: 1, /* { dg-warning "5: obsolete use of designated initializer with ':'" } */ +/* { dg-begin-multiline-output "" } + foo: 1, + ^ + . = + { dg-end-multiline-output "" } */ + + bar : 1, /* { dg-warning "9: obsolete use of designated initializer with ':'" } */ + /* { dg-begin-multiline-output "" } + bar : 1, + ^ + . = + { dg-end-multiline-output "" } */ +}; + +union u old_style_u = { color: 3 }; /* { dg-warning "30: obsolete use of designated initializer with ':'" } */ +/* { dg-begin-multiline-output "" } + union u old_style_u = { color: 3 }; + ^ + . = + { dg-end-multiline-output "" } */ -- 1.8.5.3