diff options
author | Patrick Palka <ppalka@gcc.gnu.org> | 2015-08-02 17:31:55 +0000 |
---|---|---|
committer | Patrick Palka <ppalka@gcc.gnu.org> | 2015-08-02 17:31:55 +0000 |
commit | 992118a1f9192614d3916e112e3e9a833d00566c (patch) | |
tree | 4382b53b305ac8e976613091b7987f093d40123f /gcc/cp | |
parent | fea8f6c692a091dd9e7639949e45ca7445fb53a0 (diff) | |
download | gcc-992118a1f9192614d3916e112e3e9a833d00566c.zip gcc-992118a1f9192614d3916e112e3e9a833d00566c.tar.gz gcc-992118a1f9192614d3916e112e3e9a833d00566c.tar.bz2 |
Refactor entry point to -Wmisleading-indentation
gcc/c-family/ChangeLog:
* c-indentation.h (struct token_indent_info): Define.
(get_token_indent_info): Define.
(warn_for_misleading_information): Declare.
* c-common.h (warn_for_misleading_information): Remove.
* c-identation.c (warn_for_misleading_indentation):
Change declaration to take three token_indent_infos. Adjust
accordingly.
* c-identation.c (should_warn_for_misleading_indentation):
Likewise. Bail out early if the body is a compound statement.
(guard_tinfo_to_string): Define.
gcc/c/ChangeLog:
* c-parser.c (c_parser_if_body): Take token_indent_info
argument. Call warn_for_misleading_indentation even when the
body is a semicolon. Extract token_indent_infos corresponding
to the guard, body and next tokens. Adjust call to
warn_for_misleading_indentation accordingly.
(c_parser_else_body): Likewise.
(c_parser_if_statement): Likewise.
(c_parser_while_statement): Likewise.
(c_parser_for_statement): Likewise.
gcc/cp/ChangeLog:
* parser.c (cp_parser_selection_statement): Move handling of
semicolon body to ...
(cp_parser_implicitly_scoped_statement): .. here. Call
warn_for_misleading_indentation even when the body is a
semicolon. Extract token_indent_infos corresponding to the
guard, body and next tokens. Adjust call to
warn_for_misleading_indentation accordingly. Take
token_indent_info argument.
(cp_parser_already_scoped_statement): Likewise.
(cp_parser_selection_statement, cp_parser_iteration_statement):
Extract a token_indent_info corresponding to the guard token.
From-SVN: r226477
Diffstat (limited to 'gcc/cp')
-rw-r--r-- | gcc/cp/ChangeLog | 14 | ||||
-rw-r--r-- | gcc/cp/parser.c | 100 |
2 files changed, 57 insertions, 57 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index db7d616..ca3be9f 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,17 @@ +2015-08-02 Patrick Palka <ppalka@gcc.gnu.org> + + * parser.c (cp_parser_selection_statement): Move handling of + semicolon body to ... + (cp_parser_implicitly_scoped_statement): .. here. Call + warn_for_misleading_indentation even when the body is a + semicolon. Extract token_indent_infos corresponding to the + guard, body and next tokens. Adjust call to + warn_for_misleading_indentation accordingly. Take + token_indent_info argument. + (cp_parser_already_scoped_statement): Likewise. + (cp_parser_selection_statement, cp_parser_iteration_statement): + Extract a token_indent_info corresponding to the guard token. + 2015-08-01 Caroline Tice <cmtice@google.com> PR 66521 diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 5642ea2..b978fcf 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -48,6 +48,7 @@ along with GCC; see the file COPYING3. If not see #include "type-utils.h" #include "omp-low.h" #include "gomp-constants.h" +#include "c-family/c-indentation.h" /* The lexer. */ @@ -2055,9 +2056,9 @@ static void cp_parser_declaration_statement (cp_parser *); static tree cp_parser_implicitly_scoped_statement - (cp_parser *, bool *, location_t, const char *); + (cp_parser *, bool *, const token_indent_info &); static void cp_parser_already_scoped_statement - (cp_parser *, location_t, const char *); + (cp_parser *, const token_indent_info &); /* Declarations [gram.dcl.dcl] */ @@ -10120,12 +10121,14 @@ cp_parser_selection_statement (cp_parser* parser, bool *if_p) { cp_token *token; enum rid keyword; + token_indent_info guard_tinfo; if (if_p != NULL) *if_p = false; /* Peek at the next token. */ token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT); + guard_tinfo = get_token_indent_info (token); /* See what kind of keyword it is. */ keyword = token->keyword; @@ -10168,19 +10171,8 @@ cp_parser_selection_statement (cp_parser* parser, bool *if_p) /* Parse the then-clause. */ in_statement = parser->in_statement; parser->in_statement |= IN_IF_STMT; - if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) - { - location_t loc = cp_lexer_peek_token (parser->lexer)->location; - add_stmt (build_empty_stmt (loc)); - cp_lexer_consume_token (parser->lexer); - if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE)) - warning_at (loc, OPT_Wempty_body, "suggest braces around " - "empty body in an %<if%> statement"); - nested_if = false; - } - else - cp_parser_implicitly_scoped_statement (parser, &nested_if, - token->location, "if"); + cp_parser_implicitly_scoped_statement (parser, &nested_if, + guard_tinfo); parser->in_statement = in_statement; finish_then_clause (statement); @@ -10189,24 +10181,14 @@ cp_parser_selection_statement (cp_parser* parser, bool *if_p) if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE)) { + guard_tinfo + = get_token_indent_info (cp_lexer_peek_token (parser->lexer)); /* Consume the `else' keyword. */ - location_t else_tok_loc - = cp_lexer_consume_token (parser->lexer)->location; + cp_lexer_consume_token (parser->lexer); begin_else_clause (statement); /* Parse the else-clause. */ - if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) - { - location_t loc; - loc = cp_lexer_peek_token (parser->lexer)->location; - warning_at (loc, - OPT_Wempty_body, "suggest braces around " - "empty body in an %<else%> statement"); - add_stmt (build_empty_stmt (loc)); - cp_lexer_consume_token (parser->lexer); - } - else - cp_parser_implicitly_scoped_statement (parser, NULL, - else_tok_loc, "else"); + cp_parser_implicitly_scoped_statement (parser, NULL, + guard_tinfo); finish_else_clause (statement); @@ -10247,7 +10229,7 @@ cp_parser_selection_statement (cp_parser* parser, bool *if_p) parser->in_switch_statement_p = true; parser->in_statement |= IN_SWITCH_STMT; cp_parser_implicitly_scoped_statement (parser, NULL, - 0, "switch"); + guard_tinfo); parser->in_switch_statement_p = in_switch_statement_p; parser->in_statement = in_statement; @@ -10792,17 +10774,17 @@ static tree cp_parser_iteration_statement (cp_parser* parser, bool ivdep) { cp_token *token; - location_t tok_loc; enum rid keyword; tree statement; unsigned char in_statement; + token_indent_info guard_tinfo; /* Peek at the next token. */ token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION); if (!token) return error_mark_node; - tok_loc = token->location; + guard_tinfo = get_token_indent_info (token); /* Remember whether or not we are already within an iteration statement. */ @@ -10827,7 +10809,7 @@ cp_parser_iteration_statement (cp_parser* parser, bool ivdep) cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); /* Parse the dependent statement. */ parser->in_statement = IN_ITERATION_STMT; - cp_parser_already_scoped_statement (parser, tok_loc, "while"); + cp_parser_already_scoped_statement (parser, guard_tinfo); parser->in_statement = in_statement; /* We're done with the while-statement. */ finish_while_stmt (statement); @@ -10842,7 +10824,7 @@ cp_parser_iteration_statement (cp_parser* parser, bool ivdep) statement = begin_do_stmt (); /* Parse the body of the do-statement. */ parser->in_statement = IN_ITERATION_STMT; - cp_parser_implicitly_scoped_statement (parser, NULL, 0, "do"); + cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo); parser->in_statement = in_statement; finish_do_body (statement); /* Look for the `while' keyword. */ @@ -10872,7 +10854,7 @@ cp_parser_iteration_statement (cp_parser* parser, bool ivdep) /* Parse the body of the for-statement. */ parser->in_statement = IN_ITERATION_STMT; - cp_parser_already_scoped_statement (parser, tok_loc, "for"); + cp_parser_already_scoped_statement (parser, guard_tinfo); parser->in_statement = in_statement; /* We're done with the for-statement. */ @@ -11142,10 +11124,12 @@ cp_parser_declaration_statement (cp_parser* parser) static tree cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p, - location_t guard_loc, - const char *guard_kind) + const token_indent_info &guard_tinfo) { tree statement; + location_t body_loc = cp_lexer_peek_token (parser->lexer)->location; + token_indent_info body_tinfo + = get_token_indent_info (cp_lexer_peek_token (parser->lexer)); if (if_p != NULL) *if_p = false; @@ -11153,9 +11137,16 @@ cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p, /* Mark if () ; with a special NOP_EXPR. */ if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) { - location_t loc = cp_lexer_peek_token (parser->lexer)->location; cp_lexer_consume_token (parser->lexer); - statement = add_stmt (build_empty_stmt (loc)); + statement = add_stmt (build_empty_stmt (body_loc)); + + if (guard_tinfo.keyword == RID_IF + && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE)) + warning_at (body_loc, OPT_Wempty_body, + "suggest braces around empty body in an %<if%> statement"); + else if (guard_tinfo.keyword == RID_ELSE) + warning_at (body_loc, OPT_Wempty_body, + "suggest braces around empty body in an %<else%> statement"); } /* if a compound is opened, we simply parse the statement directly. */ else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) @@ -11166,20 +11157,15 @@ cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p, /* Create a compound-statement. */ statement = begin_compound_stmt (0); /* Parse the dependent-statement. */ - location_t body_loc = cp_lexer_peek_token (parser->lexer)->location; cp_parser_statement (parser, NULL_TREE, false, if_p); /* Finish the dummy compound-statement. */ finish_compound_stmt (statement); - cp_token *next_tok = cp_lexer_peek_token (parser->lexer); - if (next_tok->keyword != RID_ELSE) - { - location_t next_stmt_loc = next_tok->location; - warn_for_misleading_indentation (guard_loc, body_loc, - next_stmt_loc, next_tok->type, - guard_kind); - } } + token_indent_info next_tinfo + = get_token_indent_info (cp_lexer_peek_token (parser->lexer)); + warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo); + /* Return the statement. */ return statement; } @@ -11190,19 +11176,19 @@ cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p, scope. */ static void -cp_parser_already_scoped_statement (cp_parser* parser, location_t guard_loc, - const char *guard_kind) +cp_parser_already_scoped_statement (cp_parser* parser, + const token_indent_info &guard_tinfo) { /* If the token is a `{', then we must take special action. */ if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) { - location_t body_loc = cp_lexer_peek_token (parser->lexer)->location; + token_indent_info body_tinfo + = get_token_indent_info (cp_lexer_peek_token (parser->lexer)); + cp_parser_statement (parser, NULL_TREE, false, NULL); - cp_token *next_tok = cp_lexer_peek_token (parser->lexer); - location_t next_stmt_loc = next_tok->location; - warn_for_misleading_indentation (guard_loc, body_loc, - next_stmt_loc, next_tok->type, - guard_kind); + token_indent_info next_tinfo + = get_token_indent_info (cp_lexer_peek_token (parser->lexer)); + warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo); } else { |