re PR c++/5520 (Add a warning to detect empty body of if statements (like in the C frontend))

2006-01-20  Dirk Mueller  <dmueller@suse.com>

        PR c++/5520
        * c-parser.c (c_parser_if_body): Use build_empty_stmt()
        instead of a special NOP marker.
        * c-typeck.c (c_finish_if_stmt): Remove obsoleted special
        NOP marker handling.
        * c-common.h (empty_body_warning): Add forward declaration.
        * c-common.c (empty_body_warning): Add (from c_finish_if_stmt).
        Now uses IS_EMPTY_STMT() instead of special NOP markers.

        * semantics.c (finish_if_stmt): Call empty_body_warning.
        * parser.c (cp_parser_implicitly_scoped_statement):
        Mark empty statement with an empty stmt.

        * g++.dg/warn/empty-body.C: New.

From-SVN: r110019
This commit is contained in:
Dirk Mueller 2006-01-20 09:30:22 +00:00 committed by Richard Biener
parent 577b02d863
commit 74ac79fa80
10 changed files with 79 additions and 35 deletions

View File

@ -1,3 +1,14 @@
2006-01-20 Dirk Mueller <dmueller@suse.com>
PR c++/5520
* c-parser.c (c_parser_if_body): Use build_empty_stmt()
instead of a special NOP marker.
* c-typeck.c (c_finish_if_stmt): Remove obsoleted special
NOP marker handling.
* c-common.h (empty_body_warning): Add forward declaration.
* c-common.c (empty_body_warning): Add (from c_finish_if_stmt).
Now uses IS_EMPTY_STMT() instead of special NOP markers.
2005-01-20 Paul Brook <paul@codesourcery.com>
* Makefile.in (lang_checks): Keep going if mkdir fails.

View File

@ -994,6 +994,36 @@ strict_aliasing_warning(tree otype, tree type, tree expr)
}
}
/* 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_body_warning (tree inner_then, tree inner_else)
{
if (extra_warnings)
{
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_Wextra, "%Hempty body in an if-statement",
EXPR_LOCUS (inner_then));
if (inner_else && IS_EMPTY_STMT (inner_else))
warning (OPT_Wextra, "%Hempty body in an else-statement",
EXPR_LOCUS (inner_else));
}
}
/* Nonzero if constant C has a value that is permissible
for type TYPE (an INTEGER_TYPE). */

View File

@ -655,6 +655,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 tree convert_and_check (tree, tree);
extern void overflow_warning (tree);
extern void unsigned_conversion_warning (tree, tree);

View File

@ -3804,7 +3804,7 @@ c_parser_if_body (c_parser *parser, bool *if_p)
c_parser_label (parser);
*if_p = c_parser_next_token_is_keyword (parser, RID_IF);
if (extra_warnings && c_parser_next_token_is (parser, CPP_SEMICOLON))
add_stmt (build1 (NOP_EXPR, NULL_TREE, NULL_TREE));
add_stmt (build_empty_stmt ());
c_parser_statement_after_labels (parser);
return c_end_compound_stmt (block, flag_isoc99);
}

View File

@ -7186,36 +7186,7 @@ c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
&if_locus);
}
/* Diagnose ";" via the special empty statement node that we create. */
if (extra_warnings)
{
tree *inner_then = &then_block, *inner_else = &else_block;
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 (TREE_CODE (*inner_then) == NOP_EXPR && !TREE_TYPE (*inner_then))
{
if (!*inner_else)
warning (0, "%Hempty body in an if-statement",
EXPR_LOCUS (*inner_then));
*inner_then = alloc_stmt_list ();
}
if (*inner_else
&& TREE_CODE (*inner_else) == NOP_EXPR
&& !TREE_TYPE (*inner_else))
{
warning (0, "%Hempty body in an else-statement",
EXPR_LOCUS (*inner_else));
*inner_else = alloc_stmt_list ();
}
}
empty_body_warning (then_block, else_block);
stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
SET_EXPR_LOCATION (stmt, if_locus);

View File

@ -1,3 +1,10 @@
2006-01-20 Dirk Mueller <dmueller@suse.com>
PR c++/5520
* semantics.c (finish_if_stmt): Call empty_body_warning.
* parser.c (cp_parser_implicitly_scoped_statement):
Mark empty statement with an empty stmt.
2006-01-19 Mark Mitchell <mark@codesourcery.com>
PR c++/22136

View File

@ -6827,8 +6827,17 @@ cp_parser_implicitly_scoped_statement (cp_parser* parser)
{
tree statement;
/* Mark if () ; with a special NOP_EXPR. */
if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
{
cp_lexer_consume_token (parser->lexer);
statement = add_stmt (build_empty_stmt ());
}
/* if a compound is opened, we simply parse the statement directly. */
else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
statement = cp_parser_compound_statement (parser, NULL, false);
/* If the token is not a `{', then we must take special action. */
if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
else
{
/* Create a compound-statement. */
statement = begin_compound_stmt (0);
@ -6837,9 +6846,6 @@ cp_parser_implicitly_scoped_statement (cp_parser* parser)
/* Finish the dummy compound-statement. */
finish_compound_stmt (statement);
}
/* Otherwise, we simply parse the statement directly. */
else
statement = cp_parser_compound_statement (parser, NULL, false);
/* Return the statement. */
return statement;

View File

@ -670,6 +670,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));
}
/* Begin a while-statement. Returns a newly created WHILE_STMT if

View File

@ -1,3 +1,8 @@
2006-01-20 Dirk Mueller <dmueller@suse.com>
PR c++/5520
* g++.dg/warn/empty-body.C: New.
2006-01-19 Mark Mitchell <mark@codesourcery.com>
PR c++/22136

View File

@ -0,0 +1,12 @@
// PR c++/5520
// { dg-options "-O2 -Wextra" }
void breakme()
{
if(1) ; // { dg-warning "empty body" }
if(1) {} // { dg-bogus "empty body" }
if(1) (void)0; // { dg-bogus "empty body" }
if(1) {} else; // { dg-warning "empty body" }
if(1) {} else (void)0; // // { dg-bogus "empty body" }
if(1) ; else (void)0; // // { dg-bogus "empty body" }
}