c-common.h (empty_body_warning): Rename to empty_if_body_warning.

2007-03-14  Dirk Mueller  <dmueller@suse.de>

        * 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
This commit is contained in:
Dirk Mueller 2007-03-14 19:33:17 +00:00 committed by Dirk Mueller
parent adea5e16e4
commit 62e00e9474
14 changed files with 91 additions and 29 deletions

View File

@ -1,3 +1,14 @@
2007-03-14 Dirk Mueller <dmueller@suse.de>
* 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 <manu@gcc.gnu.org> 2007-03-14 Manuel Lopez-Ibanez <manu@gcc.gnu.org>
PR c/21438 PR c/21438

View File

@ -1056,9 +1056,7 @@ strict_aliasing_warning (tree otype, tree type, tree expr)
block. */ block. */
void 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 if (TREE_CODE (inner_then) == STATEMENT_LIST
&& STATEMENT_LIST_TAIL (inner_then)) && STATEMENT_LIST_TAIL (inner_then))
@ -1069,13 +1067,12 @@ empty_body_warning (tree inner_then, tree inner_else)
inner_else = STATEMENT_LIST_TAIL (inner_else)->stmt; inner_else = STATEMENT_LIST_TAIL (inner_else)->stmt;
if (IS_EMPTY_STMT (inner_then) && !inner_else) if (IS_EMPTY_STMT (inner_then) && !inner_else)
warning (OPT_Wempty_body, "%Hempty body in an if-statement", warning (OPT_Wempty_body, "%Hsuggest braces around empty body "
EXPR_LOCUS (inner_then)); "in an %<if%> statement", EXPR_LOCUS (inner_then));
if (inner_else && IS_EMPTY_STMT (inner_else)) else if (inner_else && IS_EMPTY_STMT (inner_else))
warning (OPT_Wempty_body, "%Hempty body in an else-statement", warning (OPT_Wempty_body, "%Hsuggest braces around empty body "
EXPR_LOCUS (inner_else)); "in an %<else%> statement", EXPR_LOCUS (inner_else));
}
} }
/* Warn for unlikely, improbable, or stupid DECL declarations /* Warn for unlikely, improbable, or stupid DECL declarations

View File

@ -670,7 +670,7 @@ extern tree fix_string_type (tree);
struct varray_head_tag; struct varray_head_tag;
extern void constant_expression_warning (tree); extern void constant_expression_warning (tree);
extern void strict_aliasing_warning (tree, tree, 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 tree convert_and_check (tree, tree);
extern void overflow_warning (tree); extern void overflow_warning (tree);
extern void warn_logical_operator (enum tree_code, tree, tree); extern void warn_logical_operator (enum tree_code, tree, tree);

View File

@ -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_peek_2nd_token (parser)->type == CPP_COLON))
c_parser_label (parser); c_parser_label (parser);
*if_p = c_parser_next_token_is_keyword (parser, RID_IF); *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 ()); add_stmt (build_empty_stmt ());
c_parser_statement_after_labels (parser); c_parser_statement_after_labels (parser);
return c_end_compound_stmt (block, flag_isoc99); return c_end_compound_stmt (block, flag_isoc99);
@ -3953,6 +3953,9 @@ c_parser_do_statement (c_parser *parser)
location_t loc; location_t loc;
gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO)); gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
c_parser_consume_token (parser); c_parser_consume_token (parser);
if (c_parser_next_token_is (parser, CPP_SEMICOLON))
warning (OPT_Wempty_body,
"suggest braces around empty body in %<do%> statement");
block = c_begin_compound_stmt (flag_isoc99); block = c_begin_compound_stmt (flag_isoc99);
loc = c_parser_peek_token (parser)->location; loc = c_parser_peek_token (parser)->location;
save_break = c_break_label; save_break = c_break_label;

View File

@ -7229,7 +7229,7 @@ c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
&if_locus); &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); stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
SET_EXPR_LOCATION (stmt, if_locus); SET_EXPR_LOCATION (stmt, if_locus);

View File

@ -1,3 +1,8 @@
2007-03-14 Dirk Mueller <dmueller@suse.de>
* 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 <manu@gcc.gnu.org> 2007-03-14 Manuel Lopez-Ibanez <manu@gcc.gnu.org>
* class.c (warn_hidden): Add OPT_Woverloaded_virtual to warning. * class.c (warn_hidden): Add OPT_Woverloaded_virtual to warning.

View File

@ -701,7 +701,7 @@ finish_if_stmt (tree if_stmt)
TREE_CHAIN (if_stmt) = NULL; TREE_CHAIN (if_stmt) = NULL;
add_stmt (do_poplevel (scope)); add_stmt (do_poplevel (scope));
finish_stmt (); 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 /* Begin a while-statement. Returns a newly created WHILE_STMT if
@ -754,7 +754,14 @@ begin_do_stmt (void)
void void
finish_do_body (tree do_stmt) 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 %<do%> statement");
} }
/* Finish a do-statement, which may be given by DO_STMT, and whose /* Finish a do-statement, which may be given by DO_STMT, and whose

View File

@ -3136,8 +3136,9 @@ functions. This warning can be independently controlled by
@option{-Wmissing-parameter-type}. @option{-Wmissing-parameter-type}.
@item @item
An empty body occurs in an @samp{if} or @samp{else} statement. This An empty body occurs in an @samp{if}, @samp{else} or
warning can be independently controlled by @option{-Wempty-body}. @samp{do while} statement. This warning can be independently
controlled by @option{-Wempty-body}.
@item @item
A pointer is compared against integer zero with @samp{<}, @samp{<=}, 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 @item -Wempty-body
@opindex Wempty-body @opindex Wempty-body
An empty body occurs in an @samp{if} or @samp{else} statement. An empty body occurs in an @samp{if}, @samp{else} or @samp{do while}
This warning is also enabled by @option{-Wextra}. statement. This warning is also enabled by @option{-Wextra}.
@item -Wsign-compare @item -Wsign-compare
@opindex Wsign-compare @opindex Wsign-compare

View File

@ -1,3 +1,11 @@
2007-03-14 Dirk Mueller <dmueller@suse.de>
* 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 <jakub@redhat.com> 2007-03-14 Jakub Jelinek <jakub@redhat.com>
* gfortran.dg/module_implicit_conversion.f90: New test. * gfortran.dg/module_implicit_conversion.f90: New test.

View File

@ -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);
}

View File

@ -7,5 +7,5 @@
void foo (int x) void foo (int x)
{ {
if (x) if (x)
; /* { dg-warning "empty body in an if-statement" } */ ; /* { dg-warning "empty body in an" } */
} }

View File

@ -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);
}

View File

@ -7,7 +7,7 @@ void
f (int x) f (int x)
{ {
if (x) if (x)
; /* { dg-warning "warning: empty body in an if-statement" } */ ; /* { dg-warning "warning: empty body in an" } */
if (x) if (x)
; /* By design we don't warn in this case. */ ; /* By design we don't warn in this case. */
else else
@ -15,7 +15,7 @@ f (int x)
if (x) if (x)
(void)0; (void)0;
else else
; /* { dg-warning "warning: empty body in an else-statement" } */ ; /* { dg-warning "warning: empty body in an" } */
if (x) if (x)
(void)0; (void)0;
else else

View File

@ -3,7 +3,7 @@
void foo (void) void foo (void)
{ {
if (0) if (0)
a: ; /* { dg-warning "empty body in an if-statement" } */ a: ; /* { dg-warning "empty body in an" } */
} }