From 62e00e94746f4ec3c3205ddff90f78c2e06f88a4 Mon Sep 17 00:00:00 2001 From: Dirk Mueller Date: Wed, 14 Mar 2007 19:33:17 +0000 Subject: [PATCH] c-common.h (empty_body_warning): Rename to empty_if_body_warning. 2007-03-14 Dirk Mueller * c-common.h (empty_body_warning): Rename to empty_if_body_warning. * c-common.c (empty_if_body_warning): Rephrase diagnostic message. * c-parser.c (c_parser_if_body): Always add an empty statement in case of empty body. * c-parser.c (c_parser_do_statement): Warn about empty body in do/while statement. * c-typeck (c_finish_if_stmt): Call empty_if_body_warning. * doc/invoke.texi (-Wempty-body): Update documentation. * cp/semantics.c (c_finish_if_stmt): Call empty_if_body_warning. (finish_do_body): Warn about empty body in do/while statement. * g++.dg/warn/do-empty.C: New. * gcc.dg/do-empty.c: New. * gcc.dg/if-empty-1.c: Update. * gcc.dg/20001116-1.c: Update. * gcc.dg/pr23165.c: Update. From-SVN: r122928 --- gcc/ChangeLog | 11 +++++++++++ gcc/c-common.c | 29 +++++++++++++--------------- gcc/c-common.h | 2 +- gcc/c-parser.c | 5 ++++- gcc/c-typeck.c | 2 +- gcc/cp/ChangeLog | 5 +++++ gcc/cp/semantics.c | 11 +++++++++-- gcc/doc/invoke.texi | 9 +++++---- gcc/testsuite/ChangeLog | 8 ++++++++ gcc/testsuite/g++.dg/warn/do-empty.C | 15 ++++++++++++++ gcc/testsuite/gcc.dg/20001116-1.c | 2 +- gcc/testsuite/gcc.dg/do-empty.c | 15 ++++++++++++++ gcc/testsuite/gcc.dg/if-empty-1.c | 4 ++-- gcc/testsuite/gcc.dg/pr23165.c | 2 +- 14 files changed, 91 insertions(+), 29 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/do-empty.C create mode 100644 gcc/testsuite/gcc.dg/do-empty.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 3dbd1301cec..1259974f21f 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,14 @@ +2007-03-14 Dirk Mueller + + * c-common.h (empty_body_warning): Rename to empty_if_body_warning. + * c-common.c (empty_if_body_warning): Rephrase diagnostic message. + * c-parser.c (c_parser_if_body): Always add an empty statement in case + of empty body. + * c-parser.c (c_parser_do_statement): Warn about empty body in + do/while statement. + * c-typeck (c_finish_if_stmt): Call empty_if_body_warning. + * doc/invoke.texi (-Wempty-body): Update documentation. + 2007-03-14 Manuel Lopez-Ibanez PR c/21438 diff --git a/gcc/c-common.c b/gcc/c-common.c index f82a84b5914..36b87eba206 100644 --- a/gcc/c-common.c +++ b/gcc/c-common.c @@ -1056,26 +1056,23 @@ strict_aliasing_warning (tree otype, tree type, tree expr) block. */ void -empty_body_warning (tree inner_then, tree inner_else) +empty_if_body_warning (tree inner_then, tree inner_else) { - if (warn_empty_body) - { - if (TREE_CODE (inner_then) == STATEMENT_LIST - && STATEMENT_LIST_TAIL (inner_then)) - inner_then = STATEMENT_LIST_TAIL (inner_then)->stmt; + 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 (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, "%Hempty body in an if-statement", - EXPR_LOCUS (inner_then)); + if (IS_EMPTY_STMT (inner_then) && !inner_else) + warning (OPT_Wempty_body, "%Hsuggest braces around empty body " + "in an % statement", EXPR_LOCUS (inner_then)); - if (inner_else && IS_EMPTY_STMT (inner_else)) - warning (OPT_Wempty_body, "%Hempty body in an else-statement", - EXPR_LOCUS (inner_else)); - } + 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 diff --git a/gcc/c-common.h b/gcc/c-common.h index b2739c3efb3..08ce4600ee6 100644 --- a/gcc/c-common.h +++ b/gcc/c-common.h @@ -670,7 +670,7 @@ extern tree fix_string_type (tree); struct varray_head_tag; extern void constant_expression_warning (tree); extern void strict_aliasing_warning (tree, tree, tree); -extern void empty_body_warning (tree, tree); +extern void empty_if_body_warning (tree, tree); extern tree convert_and_check (tree, tree); extern void overflow_warning (tree); extern void warn_logical_operator (enum tree_code, tree, tree); diff --git a/gcc/c-parser.c b/gcc/c-parser.c index 665b494e231..b1f40a47faa 100644 --- a/gcc/c-parser.c +++ b/gcc/c-parser.c @@ -3844,7 +3844,7 @@ c_parser_if_body (c_parser *parser, bool *if_p) && c_parser_peek_2nd_token (parser)->type == CPP_COLON)) c_parser_label (parser); *if_p = c_parser_next_token_is_keyword (parser, RID_IF); - if (warn_empty_body && c_parser_next_token_is (parser, CPP_SEMICOLON)) + if (c_parser_next_token_is (parser, CPP_SEMICOLON)) add_stmt (build_empty_stmt ()); c_parser_statement_after_labels (parser); return c_end_compound_stmt (block, flag_isoc99); @@ -3953,6 +3953,9 @@ c_parser_do_statement (c_parser *parser) location_t loc; gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO)); c_parser_consume_token (parser); + if (c_parser_next_token_is (parser, CPP_SEMICOLON)) + warning (OPT_Wempty_body, + "suggest braces around empty body in % statement"); block = c_begin_compound_stmt (flag_isoc99); loc = c_parser_peek_token (parser)->location; save_break = c_break_label; diff --git a/gcc/c-typeck.c b/gcc/c-typeck.c index eca10668ee6..6986405e8a9 100644 --- a/gcc/c-typeck.c +++ b/gcc/c-typeck.c @@ -7229,7 +7229,7 @@ c_finish_if_stmt (location_t if_locus, tree cond, tree then_block, &if_locus); } - empty_body_warning (then_block, else_block); + 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); diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index e324712dd47..1409d2b974a 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,8 @@ +2007-03-14 Dirk Mueller + + * cp/semantics.c (c_finish_if_stmt): Call empty_if_body_warning. + (finish_do_body): Warn about empty body in do/while statement. + 2007-03-14 Manuel Lopez-Ibanez * class.c (warn_hidden): Add OPT_Woverloaded_virtual to warning. diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index e016b0a40de..e2603531e88 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -701,7 +701,7 @@ finish_if_stmt (tree if_stmt) TREE_CHAIN (if_stmt) = NULL; add_stmt (do_poplevel (scope)); finish_stmt (); - empty_body_warning (THEN_CLAUSE (if_stmt), ELSE_CLAUSE (if_stmt)); + empty_if_body_warning (THEN_CLAUSE (if_stmt), ELSE_CLAUSE (if_stmt)); } /* Begin a while-statement. Returns a newly created WHILE_STMT if @@ -754,7 +754,14 @@ begin_do_stmt (void) void finish_do_body (tree do_stmt) { - DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt)); + tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt)); + + if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body)) + body = STATEMENT_LIST_TAIL (body)->stmt; + + if (IS_EMPTY_STMT (body)) + warning (OPT_Wempty_body, + "suggest explicit braces around empty body in % statement"); } /* Finish a do-statement, which may be given by DO_STMT, and whose diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index e8f184d48be..fa9ed7f4d2b 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -3136,8 +3136,9 @@ functions. This warning can be independently controlled by @option{-Wmissing-parameter-type}. @item -An empty body occurs in an @samp{if} or @samp{else} statement. This -warning can be independently controlled by @option{-Wempty-body}. +An empty body occurs in an @samp{if}, @samp{else} or +@samp{do while} statement. This warning can be independently +controlled by @option{-Wempty-body}. @item A pointer is compared against integer zero with @samp{<}, @samp{<=}, @@ -3397,8 +3398,8 @@ changed by the conversion like in @code{abs (2.0)}. @item -Wempty-body @opindex Wempty-body -An empty body occurs in an @samp{if} or @samp{else} statement. -This warning is also enabled by @option{-Wextra}. +An empty body occurs in an @samp{if}, @samp{else} or @samp{do while} +statement. This warning is also enabled by @option{-Wextra}. @item -Wsign-compare @opindex Wsign-compare diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index d3713ed19ec..e0d27c55562 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,11 @@ +2007-03-14 Dirk Mueller + + * g++.dg/warn/do-empty.C: New. + * gcc.dg/do-empty.c: New. + * gcc.dg/if-empty-1.c: Update. + * gcc.dg/20001116-1.c: Update. + * gcc.dg/pr23165.c: Update. + 2007-03-14 Jakub Jelinek * gfortran.dg/module_implicit_conversion.f90: New test. diff --git a/gcc/testsuite/g++.dg/warn/do-empty.C b/gcc/testsuite/g++.dg/warn/do-empty.C new file mode 100644 index 00000000000..350261d48ed --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/do-empty.C @@ -0,0 +1,15 @@ +/* Test diagnostics for empty bodies in do / while. */ +/* { dg-do compile } */ +/* { dg-options "-Wempty-body" } */ + +void +f (int x) +{ + do + ; /* { dg-warning "empty body in" } */ + while (x--); + + do + {} /* { dg-bogus "empty body in" } */ + while (++x < 10); +} diff --git a/gcc/testsuite/gcc.dg/20001116-1.c b/gcc/testsuite/gcc.dg/20001116-1.c index 3ea2f381d07..8b1e919c66f 100644 --- a/gcc/testsuite/gcc.dg/20001116-1.c +++ b/gcc/testsuite/gcc.dg/20001116-1.c @@ -7,5 +7,5 @@ void foo (int x) { if (x) - ; /* { dg-warning "empty body in an if-statement" } */ + ; /* { dg-warning "empty body in an" } */ } diff --git a/gcc/testsuite/gcc.dg/do-empty.c b/gcc/testsuite/gcc.dg/do-empty.c new file mode 100644 index 00000000000..350261d48ed --- /dev/null +++ b/gcc/testsuite/gcc.dg/do-empty.c @@ -0,0 +1,15 @@ +/* Test diagnostics for empty bodies in do / while. */ +/* { dg-do compile } */ +/* { dg-options "-Wempty-body" } */ + +void +f (int x) +{ + do + ; /* { dg-warning "empty body in" } */ + while (x--); + + do + {} /* { dg-bogus "empty body in" } */ + while (++x < 10); +} diff --git a/gcc/testsuite/gcc.dg/if-empty-1.c b/gcc/testsuite/gcc.dg/if-empty-1.c index 9785c72a181..a129832c337 100644 --- a/gcc/testsuite/gcc.dg/if-empty-1.c +++ b/gcc/testsuite/gcc.dg/if-empty-1.c @@ -7,7 +7,7 @@ void f (int x) { if (x) - ; /* { dg-warning "warning: empty body in an if-statement" } */ + ; /* { dg-warning "warning: empty body in an" } */ if (x) ; /* By design we don't warn in this case. */ else @@ -15,7 +15,7 @@ f (int x) if (x) (void)0; else - ; /* { dg-warning "warning: empty body in an else-statement" } */ + ; /* { dg-warning "warning: empty body in an" } */ if (x) (void)0; else diff --git a/gcc/testsuite/gcc.dg/pr23165.c b/gcc/testsuite/gcc.dg/pr23165.c index 94e7daebc90..49194a1462d 100644 --- a/gcc/testsuite/gcc.dg/pr23165.c +++ b/gcc/testsuite/gcc.dg/pr23165.c @@ -3,7 +3,7 @@ void foo (void) { if (0) - a: ; /* { dg-warning "empty body in an if-statement" } */ + a: ; /* { dg-warning "empty body in an" } */ }