From 626c34b5c945cc9c577bd0509d297b81df083d3f Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Tue, 30 Sep 2008 09:52:41 +0000 Subject: [PATCH] c-common.c (empty_if_body_warning): Remove. 2008-09-30 Paolo Bonzini * c-common.c (empty_if_body_warning): Remove. * c-common.h (empty_if_body_warning): Remove. * c-parser.c (c_parser_if_body, c_parser_else_body): Implement here the -Wempty-body warning for `if' and `else' statements. * c-typeck.c (c_finish_if_stmt): Do not call empty_body_warning. cp: 2008-09-30 Paolo Bonzini * parser.c (cp_parser_selection_statement): Implement here the -Wempty-body warning for `if' and `else' statements. * semantics.c (finish_if_stmt): Do not call empty_body_warning. testsuite: 2008-09-30 Paolo Bonzini * g++.dg/warn/if-empty-1.C: Copy from gcc.dg/if-empty-1.c. From-SVN: r140780 --- gcc/ChangeLog | 8 ++++++++ gcc/c-common.c | 25 ------------------------- gcc/c-common.h | 1 - gcc/c-parser.c | 7 +++++++ gcc/c-typeck.c | 2 -- gcc/cp/ChangeLog | 6 ++++++ gcc/cp/parser.c | 24 ++++++++++++++++++++++-- gcc/cp/semantics.c | 1 - gcc/testsuite/ChangeLog | 4 ++++ gcc/testsuite/g++.dg/warn/if-empty-1.C | 23 +++++++++++++++++++++++ 10 files changed, 70 insertions(+), 31 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/if-empty-1.C diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 5a9b482..68de57a 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,11 @@ +2008-09-30 Paolo Bonzini + + * c-common.c (empty_if_body_warning): Remove. + * c-common.h (empty_if_body_warning): Remove. + * c-parser.c (c_parser_if_body, c_parser_else_body): Implement + here the -Wempty-body warning for `if' and `else' statements. + * c-typeck.c (c_finish_if_stmt): Do not call empty_body_warning. + 2008-09-29 H.J. Lu * config/i386/i386.opt: Add msse2avx. diff --git a/gcc/c-common.c b/gcc/c-common.c index 5d69211..7665b62 100644 --- a/gcc/c-common.c +++ b/gcc/c-common.c @@ -1291,31 +1291,6 @@ strict_aliasing_warning (tree otype, tree type, tree expr) return false; } -/* Print a warning about if (); or if () .. else; constructs - via the special empty statement node that we create. INNER_THEN - and INNER_ELSE are the statement lists of the if and the else - block. */ - -void -empty_if_body_warning (tree inner_then, tree inner_else) -{ - if (TREE_CODE (inner_then) == STATEMENT_LIST - && STATEMENT_LIST_TAIL (inner_then)) - inner_then = STATEMENT_LIST_TAIL (inner_then)->stmt; - - if (inner_else && TREE_CODE (inner_else) == STATEMENT_LIST - && STATEMENT_LIST_TAIL (inner_else)) - inner_else = STATEMENT_LIST_TAIL (inner_else)->stmt; - - if (IS_EMPTY_STMT (inner_then) && !inner_else) - warning (OPT_Wempty_body, "%Hsuggest braces around empty body " - "in an % statement", EXPR_LOCUS (inner_then)); - - else if (inner_else && IS_EMPTY_STMT (inner_else)) - warning (OPT_Wempty_body, "%Hsuggest braces around empty body " - "in an % statement", EXPR_LOCUS (inner_else)); -} - /* Warn for unlikely, improbable, or stupid DECL declarations of `main'. */ diff --git a/gcc/c-common.h b/gcc/c-common.h index 15ea573..622100a 100644 --- a/gcc/c-common.h +++ b/gcc/c-common.h @@ -726,7 +726,6 @@ struct varray_head_tag; extern void constant_expression_warning (tree); extern void constant_expression_error (tree); extern bool strict_aliasing_warning (tree, tree, tree); -extern void empty_if_body_warning (tree, tree); extern void warnings_for_convert_and_check (tree, tree, tree); extern tree convert_and_check (tree, tree); extern void overflow_warning (tree); diff --git a/gcc/c-parser.c b/gcc/c-parser.c index 2b64c86..9597660 100644 --- a/gcc/c-parser.c +++ b/gcc/c-parser.c @@ -3858,8 +3858,12 @@ c_parser_if_body (c_parser *parser, bool *if_p) *if_p = c_parser_next_token_is_keyword (parser, RID_IF); if (c_parser_next_token_is (parser, CPP_SEMICOLON)) { + location_t loc = c_parser_peek_token (parser)->location; add_stmt (build_empty_stmt ()); c_parser_consume_token (parser); + if (!c_parser_next_token_is_keyword (parser, RID_ELSE)) + warning_at (loc, OPT_Wempty_body, + "suggest braces around empty body in an % statement"); } else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) add_stmt (c_parser_compound_statement (parser)); @@ -3883,6 +3887,9 @@ c_parser_else_body (c_parser *parser) c_parser_label (parser); if (c_parser_next_token_is (parser, CPP_SEMICOLON)) { + warning_at (c_parser_peek_token (parser)->location, + OPT_Wempty_body, + "suggest braces around empty body in an % statement"); add_stmt (build_empty_stmt ()); c_parser_consume_token (parser); } diff --git a/gcc/c-typeck.c b/gcc/c-typeck.c index 1424405..c6474ed 100644 --- a/gcc/c-typeck.c +++ b/gcc/c-typeck.c @@ -7440,8 +7440,6 @@ c_finish_if_stmt (location_t if_locus, tree cond, tree then_block, &if_locus); } - empty_if_body_warning (then_block, else_block); - stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block); SET_EXPR_LOCATION (stmt, if_locus); add_stmt (stmt); diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 8c722d8..5e5c742 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2008-09-30 Paolo Bonzini + + * parser.c (cp_parser_selection_statement): Implement here the + -Wempty-body warning for `if' and `else' statements. + * semantics.c (finish_if_stmt): Do not call empty_body_warning. + 2008-09-25 Paolo Carlini PR c++/37649 diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index f28c76d..c368a1c 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -7155,7 +7155,17 @@ 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; - cp_parser_implicitly_scoped_statement (parser, &nested_if); + 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 ()); + 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 % statement"); + } + else + cp_parser_implicitly_scoped_statement (parser, &nested_if); parser->in_statement = in_statement; finish_then_clause (statement); @@ -7168,7 +7178,17 @@ cp_parser_selection_statement (cp_parser* parser, bool *if_p) cp_lexer_consume_token (parser->lexer); begin_else_clause (statement); /* Parse the else-clause. */ - cp_parser_implicitly_scoped_statement (parser, NULL); + if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) + { + warning_at (cp_lexer_peek_token (parser->lexer)->location, + OPT_Wempty_body, "suggest braces around " + "empty body in an % statement"); + add_stmt (build_empty_stmt ()); + cp_lexer_consume_token (parser->lexer); + } + else + cp_parser_implicitly_scoped_statement (parser, NULL); + finish_else_clause (statement); /* If we are currently parsing a then-clause, then diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 176a7fd..13e9a0f 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -695,7 +695,6 @@ finish_if_stmt (tree if_stmt) TREE_CHAIN (if_stmt) = NULL; add_stmt (do_poplevel (scope)); finish_stmt (); - empty_if_body_warning (THEN_CLAUSE (if_stmt), ELSE_CLAUSE (if_stmt)); } /* Begin a while-statement. Returns a newly created WHILE_STMT if diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index df91766..28086b3 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,5 +1,9 @@ 2008-09-30 Paolo Bonzini + * g++.dg/warn/if-empty-1.C: Copy from gcc.dg/if-empty-1.c. + +2008-09-30 Paolo Bonzini + PR testsuite/36891 PR testsuite/37675 * gcc.dg/torture/pr36891.c: Add -msse on 32-bit i386. diff --git a/gcc/testsuite/g++.dg/warn/if-empty-1.C b/gcc/testsuite/g++.dg/warn/if-empty-1.C new file mode 100644 index 0000000..b29a6cb --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/if-empty-1.C @@ -0,0 +1,23 @@ +/* Test diagnostics for empty bodies in if / else. */ +/* Origin: Joseph Myers */ +/* { dg-do compile } */ +/* { dg-options "-Wempty-body" } */ + +void +f (int x) +{ + if (x) + ; /* { dg-warning "suggest braces around empty body in an" } */ + if (x) + ; /* By design we don't warn in this case. */ + else + (void)0; + if (x) + (void)0; + else + ; /* { dg-warning "suggest braces around empty body in an" } */ + if (x) + (void)0; + else + (void)0; +} -- 2.7.4