aboutsummaryrefslogtreecommitdiff
path: root/gcc/c/c-parser.c
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2017-01-04 17:40:59 +0000
committerDavid Malcolm <dmalcolm@gcc.gnu.org>2017-01-04 17:40:59 +0000
commit5dd9a9d0c9294455077ba6401701fd3566dcf07c (patch)
tree881a273cbb54d8222e066d9f714a5334e21e14fb /gcc/c/c-parser.c
parentd0a0bfd9394f303ac9ada8803c72ad846bbab990 (diff)
downloadgcc-5dd9a9d0c9294455077ba6401701fd3566dcf07c.zip
gcc-5dd9a9d0c9294455077ba6401701fd3566dcf07c.tar.gz
gcc-5dd9a9d0c9294455077ba6401701fd3566dcf07c.tar.bz2
C FE: implement fix-it hint for -Wmissing-braces
gcc/c/ChangeLog: * c-parser.c (c_parser_declaration_or_fndef): Create a rich_location at init_loc and parse it to start_init. (last_init_list_comma): New global. (c_parser_braced_init): Update last_init_list_comma when parsing commas. Pass it to pop_init_level. Pass location of closing brace to pop_init_level. (c_parser_postfix_expression_after_paren_type): Create a rich_location at type_loc and parse it to start_init. (c_parser_omp_declare_reduction): Likewise for loc. * c-tree.h (start_init): Add rich_location * param. (pop_init_level): Add location_t param. * c-typeck.c (struct initializer_stack): Add field "missing_brace_richloc". (start_init): Add richloc param, use it to initialize the stack node's missing_brace_richloc. (last_init_list_comma): New decl. (finish_implicit_inits): Pass last_init_list_comma to pop_init_level. (push_init_level): When finding missing open braces, add fix-it hints to the richloc. (pop_init_level): Add "insert_before" param and pass it when calling pop_init_level. Add fixits about missing close braces to any richloc. Use the richloc for the -Wmissing-braces warning. (set_designator): Pass last_init_list_comma to pop_init_level. (process_init_element): Likewise. gcc/testsuite/ChangeLog: * gcc.dg/Wmissing-braces-fixits.c: New test case. From-SVN: r244061
Diffstat (limited to 'gcc/c/c-parser.c')
-rw-r--r--gcc/c/c-parser.c26
1 files changed, 19 insertions, 7 deletions
diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c
index 6d443da..c7679c2 100644
--- a/gcc/c/c-parser.c
+++ b/gcc/c/c-parser.c
@@ -1847,8 +1847,9 @@ c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
c_parser_consume_token (parser);
if (auto_type_p)
{
- start_init (NULL_TREE, asm_name, global_bindings_p ());
init_loc = c_parser_peek_token (parser)->location;
+ rich_location richloc (line_table, init_loc);
+ start_init (NULL_TREE, asm_name, global_bindings_p (), &richloc);
init = c_parser_expr_no_commas (parser, NULL);
if (TREE_CODE (init.value) == COMPONENT_REF
&& DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
@@ -1904,8 +1905,9 @@ c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
|| !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
c_finish_omp_declare_simd (parser, d, NULL_TREE,
omp_declare_simd_clauses);
- start_init (d, asm_name, global_bindings_p ());
init_loc = c_parser_peek_token (parser)->location;
+ rich_location richloc (line_table, init_loc);
+ start_init (d, asm_name, global_bindings_p (), &richloc);
init = c_parser_initializer (parser);
finish_init ();
}
@@ -4325,6 +4327,11 @@ c_parser_initializer (c_parser *parser)
}
}
+/* The location of the last comma within the current initializer list,
+ or UNKNOWN_LOCATION if not within one. */
+
+location_t last_init_list_comma;
+
/* Parse a braced initializer list. TYPE is the type specified for a
compound literal, and NULL_TREE for other initializers and for
nested braced lists. NESTED_P is true for nested braced lists,
@@ -4362,7 +4369,10 @@ c_parser_braced_init (c_parser *parser, tree type, bool nested_p,
if (parser->error)
break;
if (c_parser_next_token_is (parser, CPP_COMMA))
- c_parser_consume_token (parser);
+ {
+ last_init_list_comma = c_parser_peek_token (parser)->location;
+ c_parser_consume_token (parser);
+ }
else
break;
if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
@@ -4376,13 +4386,13 @@ c_parser_braced_init (c_parser *parser, tree type, bool nested_p,
ret.original_code = ERROR_MARK;
ret.original_type = NULL;
c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
- pop_init_level (brace_loc, 0, &braced_init_obstack);
+ pop_init_level (brace_loc, 0, &braced_init_obstack, last_init_list_comma);
obstack_free (&braced_init_obstack, NULL);
return ret;
}
location_t close_loc = next_tok->location;
c_parser_consume_token (parser);
- ret = pop_init_level (brace_loc, 0, &braced_init_obstack);
+ ret = pop_init_level (brace_loc, 0, &braced_init_obstack, close_loc);
obstack_free (&braced_init_obstack, NULL);
set_c_expr_source_range (&ret, brace_loc, close_loc);
return ret;
@@ -8218,7 +8228,8 @@ c_parser_postfix_expression_after_paren_type (c_parser *parser,
tree type_expr = NULL_TREE;
bool type_expr_const = true;
check_compound_literal_type (type_loc, type_name);
- start_init (NULL_TREE, NULL, 0);
+ rich_location richloc (line_table, type_loc);
+ start_init (NULL_TREE, NULL, 0, &richloc);
type = groktypename (type_name, &type_expr, &type_expr_const);
start_loc = c_parser_peek_token (parser)->location;
if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
@@ -17140,8 +17151,9 @@ c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
else
{
tree st = push_stmt_list ();
- start_init (omp_priv, NULL_TREE, 0);
location_t loc = c_parser_peek_token (parser)->location;
+ rich_location richloc (line_table, loc);
+ start_init (omp_priv, NULL_TREE, 0, &richloc);
struct c_expr init = c_parser_initializer (parser);
finish_init ();
finish_decl (omp_priv, loc, init.value,