aboutsummaryrefslogtreecommitdiff
path: root/gcc/c/c-parser.c
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2015-05-12 20:57:38 +0000
committerDavid Malcolm <dmalcolm@gcc.gnu.org>2015-05-12 20:57:38 +0000
commitc3388e62499a6d2f931247a73cabf9184366248e (patch)
tree59ac6b4ba6d59fb6b6bc0da54eaa2c8c9b71068d /gcc/c/c-parser.c
parentf06ed65044c0cba7f9cb8d6d8a8b99ee81953e4e (diff)
downloadgcc-c3388e62499a6d2f931247a73cabf9184366248e.zip
gcc-c3388e62499a6d2f931247a73cabf9184366248e.tar.gz
gcc-c3388e62499a6d2f931247a73cabf9184366248e.tar.bz2
Implement -Wmisleading-indentation
gcc/ChangeLog: * doc/invoke.texi (Warning Options): Add -Wmisleading-indentation. (-Wmisleading-indentation): New option. * Makefile.in (C_COMMON_OBJS): Add c-family/c-indentation.o. gcc/c-family/ChangeLog: * c-common.h (warn_for_misleading_indentation): New prototype. * c-indentation.c: New file. * c.opt (Wmisleading-indentation): New option. gcc/c/ChangeLog: * c-parser.c (c_parser_if_body): Add param "if_loc", use it to add a call to warn_for_misleading_indentation. (c_parser_else_body): Likewise, adding param "else_loc". (c_parser_if_statement): Check for misleading indentation. (c_parser_while_statement): Likewise. (c_parser_for_statement): Likewise. gcc/cp/ChangeLog: * parser.c (cp_parser_selection_statement): Add location and guard_kind arguments to calls to cp_parser_implicitly_scoped_statement. (cp_parser_iteration_statement): Likewise for calls to cp_parser_already_scoped_statement. (cp_parser_implicitly_scoped_statement): Add "guard_loc" and "guard_kind" params; use them to warn for misleading indentation. (cp_parser_already_scoped_statement): Likewise. gcc/testsuite/ChangeLog: * c-c++-common/Wmisleading-indentation.c: New testcase. * c-c++-common/Wmisleading-indentation-2.c: New testcase. * c-c++-common/Wmisleading-indentation-2.md: New file. libcpp/ChangeLog: * directives.c (do_line): Set seen_line_directive on line_table. (do_linemarker): Likewise. * include/line-map.h (struct line_maps): Add new field "seen_line_directive". From-SVN: r223098
Diffstat (limited to 'gcc/c/c-parser.c')
-rw-r--r--gcc/c/c-parser.c51
1 files changed, 43 insertions, 8 deletions
diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c
index bf0e4c57..36438d0 100644
--- a/gcc/c/c-parser.c
+++ b/gcc/c/c-parser.c
@@ -5185,7 +5185,7 @@ c_parser_c99_block_statement (c_parser *parser)
parser->in_if_block. */
static tree
-c_parser_if_body (c_parser *parser, bool *if_p)
+c_parser_if_body (c_parser *parser, bool *if_p, location_t if_loc)
{
tree block = c_begin_compound_stmt (flag_isoc99);
location_t body_loc = c_parser_peek_token (parser)->location;
@@ -5203,7 +5203,15 @@ c_parser_if_body (c_parser *parser, bool *if_p)
else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
add_stmt (c_parser_compound_statement (parser));
else
- c_parser_statement_after_labels (parser);
+ {
+ c_parser_statement_after_labels (parser);
+ if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
+ warn_for_misleading_indentation (if_loc, body_loc,
+ c_parser_peek_token (parser)->location,
+ c_parser_peek_token (parser)->type,
+ "if");
+ }
+
return c_end_compound_stmt (body_loc, block, flag_isoc99);
}
@@ -5212,9 +5220,9 @@ c_parser_if_body (c_parser *parser, bool *if_p)
specially for the sake of -Wempty-body warnings. */
static tree
-c_parser_else_body (c_parser *parser)
+c_parser_else_body (c_parser *parser, location_t else_loc)
{
- location_t else_loc = c_parser_peek_token (parser)->location;
+ location_t body_loc = c_parser_peek_token (parser)->location;
tree block = c_begin_compound_stmt (flag_isoc99);
c_parser_all_labels (parser);
if (c_parser_next_token_is (parser, CPP_SEMICOLON))
@@ -5227,8 +5235,14 @@ c_parser_else_body (c_parser *parser)
c_parser_consume_token (parser);
}
else
- c_parser_statement_after_labels (parser);
- return c_end_compound_stmt (else_loc, block, flag_isoc99);
+ {
+ c_parser_statement_after_labels (parser);
+ warn_for_misleading_indentation (else_loc, body_loc,
+ c_parser_peek_token (parser)->location,
+ c_parser_peek_token (parser)->type,
+ "else");
+ }
+ return c_end_compound_stmt (body_loc, block, flag_isoc99);
}
/* Parse an if statement (C90 6.6.4, C99 6.8.4).
@@ -5250,6 +5264,7 @@ c_parser_if_statement (c_parser *parser)
tree if_stmt;
gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
+ location_t if_loc = c_parser_peek_token (parser)->location;
c_parser_consume_token (parser);
block = c_begin_compound_stmt (flag_isoc99);
loc = c_parser_peek_token (parser)->location;
@@ -5261,12 +5276,13 @@ c_parser_if_statement (c_parser *parser)
}
in_if_block = parser->in_if_block;
parser->in_if_block = true;
- first_body = c_parser_if_body (parser, &first_if);
+ first_body = c_parser_if_body (parser, &first_if, if_loc);
parser->in_if_block = in_if_block;
if (c_parser_next_token_is_keyword (parser, RID_ELSE))
{
+ location_t else_loc = c_parser_peek_token (parser)->location;
c_parser_consume_token (parser);
- second_body = c_parser_else_body (parser);
+ second_body = c_parser_else_body (parser, else_loc);
}
else
second_body = NULL_TREE;
@@ -5346,6 +5362,7 @@ c_parser_while_statement (c_parser *parser, bool ivdep)
tree block, cond, body, save_break, save_cont;
location_t loc;
gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
+ location_t while_loc = c_parser_peek_token (parser)->location;
c_parser_consume_token (parser);
block = c_begin_compound_stmt (flag_isoc99);
loc = c_parser_peek_token (parser)->location;
@@ -5362,7 +5379,16 @@ c_parser_while_statement (c_parser *parser, bool ivdep)
c_break_label = NULL_TREE;
save_cont = c_cont_label;
c_cont_label = NULL_TREE;
+
+ location_t body_loc = UNKNOWN_LOCATION;
+ if (c_parser_peek_token (parser)->type != CPP_OPEN_BRACE)
+ body_loc = c_parser_peek_token (parser)->location;
body = c_parser_c99_block_statement (parser);
+ warn_for_misleading_indentation (while_loc, body_loc,
+ c_parser_peek_token (parser)->location,
+ c_parser_peek_token (parser)->type,
+ "while");
+
c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
c_break_label = save_break;
@@ -5640,7 +5666,16 @@ c_parser_for_statement (c_parser *parser, bool ivdep)
c_break_label = NULL_TREE;
save_cont = c_cont_label;
c_cont_label = NULL_TREE;
+
+ location_t body_loc = UNKNOWN_LOCATION;
+ if (c_parser_peek_token (parser)->type != CPP_OPEN_BRACE)
+ body_loc = c_parser_peek_token (parser)->location;
body = c_parser_c99_block_statement (parser);
+ warn_for_misleading_indentation (for_loc, body_loc,
+ c_parser_peek_token (parser)->location,
+ c_parser_peek_token (parser)->type,
+ "for");
+
if (is_foreach_statement)
objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
else