From 50247dd938763df581825bd0d2a09fc39fbe8d85 Mon Sep 17 00:00:00 2001 From: rguenth Date: Fri, 20 Jan 2006 09:30:22 +0000 Subject: [PATCH] 2006-01-20 Dirk Mueller 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. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@110019 138bc75d-0d04-0410-961f-82ee72b054a4 --- gcc/ChangeLog | 11 +++++++++++ gcc/c-common.c | 30 ++++++++++++++++++++++++++++++ gcc/c-common.h | 1 + gcc/c-parser.c | 2 +- gcc/c-typeck.c | 31 +------------------------------ gcc/cp/ChangeLog | 7 +++++++ gcc/cp/parser.c | 14 ++++++++++---- gcc/cp/semantics.c | 1 + gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/g++.dg/warn/empty-body.C | 12 ++++++++++++ 10 files changed, 79 insertions(+), 35 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/empty-body.C diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 4e21b29490c..0a23c2214f6 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,14 @@ +2006-01-20 Dirk Mueller + + 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 * Makefile.in (lang_checks): Keep going if mkdir fails. diff --git a/gcc/c-common.c b/gcc/c-common.c index 2a7c1e777d3..60035ea601b 100644 --- a/gcc/c-common.c +++ b/gcc/c-common.c @@ -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). */ diff --git a/gcc/c-common.h b/gcc/c-common.h index 6da433c6524..ba7dc5cf6e8 100644 --- a/gcc/c-common.h +++ b/gcc/c-common.h @@ -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); diff --git a/gcc/c-parser.c b/gcc/c-parser.c index e594d1fbd34..a5fa523dd3e 100644 --- a/gcc/c-parser.c +++ b/gcc/c-parser.c @@ -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); } diff --git a/gcc/c-typeck.c b/gcc/c-typeck.c index 06ffefd3028..c6f658baf85 100644 --- a/gcc/c-typeck.c +++ b/gcc/c-typeck.c @@ -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); diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 5fabf49df71..b54d930c377 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,10 @@ +2006-01-20 Dirk Mueller + + 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 PR c++/22136 diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index d2b41e5c39d..cf19fbf1595 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -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; diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 512b78949fb..6121d43b99f 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -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 diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index b41928e4d82..c0cc34200a2 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2006-01-20 Dirk Mueller + + PR c++/5520 + * g++.dg/warn/empty-body.C: New. + 2006-01-19 Mark Mitchell PR c++/22136 diff --git a/gcc/testsuite/g++.dg/warn/empty-body.C b/gcc/testsuite/g++.dg/warn/empty-body.C new file mode 100644 index 00000000000..6ca8900fda0 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/empty-body.C @@ -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" } +} -- 2.11.0