This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
PING^5: [PATCH] C/C++: Implement -Wempty-body for do/while, clarify diagnostic
On Saturday, 3. March 2007, Manuel López-Ibáñez wrote:
> there is another entry in invoke.texi for Wempty-body, please update
> that entry as well as you did with the text in Wextra.
>
> Also, the two warnings in empty_if_body_warning cannot be true at the
> same time, so I think it is better to use 'else if' for the second
> one.
Thanks. Updated patch attached below. Its been almost 4 months now. I still
need approval for c-parser.c and c-typeck.c. In case I cannot get those I'll
consider leaving out the C parts and file a bugreport about it.
> > > > > The C++ and documentation parts are OK; you need a C maintainer to
> > > > > look over the C bits (though they look OK to me).
Thanks,
Dirk
2007-01-02 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 (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.
--- cp/semantics.c (revision 121595)
+++ cp/semantics.c (working copy)
@@ -697,7 +697,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
@@ -750,7 +750,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 %<do%> statement");
}
/* Finish a do-statement, which may be given by DO_STMT, and whose
--- c-common.h (revision 121595)
+++ c-common.h (working copy)
@@ -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 check_main_parameter_types (tree decl);
--- c-common.c (revision 121595)
+++ c-common.c (working copy)
@@ -1008,26 +1009,23 @@ strict_aliasing_warning (tree otype, tre
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 (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 (inner_else && IS_EMPTY_STMT (inner_else))
- warning (OPT_Wempty_body, "%Hempty body in an else-statement",
- EXPR_LOCUS (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
--- c-parser.c (revision 121595)
+++ c-parser.c (working copy)
@@ -3844,7 +3844,7 @@ c_parser_if_body (c_parser *parser, bool
&& 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 %<do%> statement");
block = c_begin_compound_stmt (flag_isoc99);
loc = c_parser_peek_token (parser)->location;
save_break = c_break_label;
--- c-typeck.c (revision 121595)
+++ c-typeck.c (working copy)
@@ -7216,7 +7245,7 @@ c_finish_if_stmt (location_t if_locus, t
&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);
--- doc/invoke.texi
+++ doc/invoke.texi
@@ -3131,8 +3131,9 @@ functions. This warning can be independ
@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{<=},
@@ -3390,8 +3391,8 @@ like @code{unsigned ui = -1}; and conver
@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
--- testsuite/gcc.dg/do-empty.c (revision 0)
+++ testsuite/gcc.dg/do-empty.c (revision 0)
@@ -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);
+}
Index: testsuite/gcc.dg/if-empty-1.c
===================================================================
--- testsuite/gcc.dg/if-empty-1.c (revision 121595)
+++ testsuite/gcc.dg/if-empty-1.c (working copy)
@@ -7,7 +7,7 @@ void
f (int x)
{
if (x)
- ; /* { dg-warning "warning: empty body in an if-statement" } */
+ ; /* { dg-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 "empty body in an" } */
if (x)
(void)0;
else
Index: testsuite/g++.dg/warn/do-empty.C
===================================================================
--- testsuite/g++.dg/warn/do-empty.C (revision 0)
+++ testsuite/g++.dg/warn/do-empty.C (revision 0)
@@ -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);
+}
Index: testsuite/gcc.dg/20001116-1.c
===================================================================
--- testsuite/gcc.dg/20001116-1.c (revision 121595)
+++ testsuite/gcc.dg/20001116-1.c (working copy)
@@ -7,5 +7,5 @@
void foo (int x)
{
if (x)
- ; /* { dg-warning "empty body in an if-statement" } */
+ ; /* { dg-warning "empty body in an" } */
}
Index: testsuite/gcc.dg/pr23165.c
===================================================================
--- testsuite/gcc.dg/pr23165.c (revision 121595)
+++ testsuite/gcc.dg/pr23165.c (working copy)
@@ -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" } */
}