* 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 <bonzini@gnu.org>
* 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 <bonzini@gnu.org>
* g++.dg/warn/if-empty-1.C: Copy from gcc.dg/if-empty-1.c.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@140780
138bc75d-0d04-0410-961f-
82ee72b054a4
+2008-09-30 Paolo Bonzini <bonzini@gnu.org>
+
+ * 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 <hongjiu.lu@intel.com>
* config/i386/i386.opt: Add msse2avx.
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 %<if%> 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 %<else%> statement", EXPR_LOCUS (inner_else));
-}
-
/* Warn for unlikely, improbable, or stupid DECL declarations
of `main'. */
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);
*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 %<if%> statement");
}
else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
add_stmt (c_parser_compound_statement (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 %<else%> statement");
add_stmt (build_empty_stmt ());
c_parser_consume_token (parser);
}
&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);
+2008-09-30 Paolo Bonzini <bonzini@gnu.org>
+
+ * 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 <paolo.carlini@oracle.com>
PR c++/37649
/* 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 %<if%> statement");
+ }
+ else
+ cp_parser_implicitly_scoped_statement (parser, &nested_if);
parser->in_statement = in_statement;
finish_then_clause (statement);
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 %<else%> 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
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
2008-09-30 Paolo Bonzini <bonzini@gnu.org>
+ * g++.dg/warn/if-empty-1.C: Copy from gcc.dg/if-empty-1.c.
+
+2008-09-30 Paolo Bonzini <bonzini@gnu.org>
+
PR testsuite/36891
PR testsuite/37675
* gcc.dg/torture/pr36891.c: Add -msse on 32-bit i386.
--- /dev/null
+/* Test diagnostics for empty bodies in if / else. */
+/* Origin: Joseph Myers <joseph@codesourcery.com> */
+/* { 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;
+}