aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorDirk Mueller <dmueller@suse.com>2006-01-20 09:30:22 +0000
committerRichard Biener <rguenth@gcc.gnu.org>2006-01-20 09:30:22 +0000
commit74ac79fa800af6de648e984cc5576c20b817839d (patch)
treebc92b7d88bae96e3177ed2217eb031fe56a30961 /gcc
parent577b02d86397e9f5a09392d2ea9f722b8a250709 (diff)
downloadgcc-74ac79fa800af6de648e984cc5576c20b817839d.zip
gcc-74ac79fa800af6de648e984cc5576c20b817839d.tar.gz
gcc-74ac79fa800af6de648e984cc5576c20b817839d.tar.bz2
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
Diffstat (limited to 'gcc')
-rw-r--r--gcc/ChangeLog11
-rw-r--r--gcc/c-common.c30
-rw-r--r--gcc/c-common.h1
-rw-r--r--gcc/c-parser.c2
-rw-r--r--gcc/c-typeck.c31
-rw-r--r--gcc/cp/ChangeLog7
-rw-r--r--gcc/cp/parser.c14
-rw-r--r--gcc/cp/semantics.c1
-rw-r--r--gcc/testsuite/ChangeLog5
-rw-r--r--gcc/testsuite/g++.dg/warn/empty-body.C12
10 files changed, 79 insertions, 35 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 4e21b29..0a23c22 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -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.
diff --git a/gcc/c-common.c b/gcc/c-common.c
index 2a7c1e7..60035ea 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 6da433c..ba7dc5c 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 e594d1f..a5fa523 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 06ffefd..c6f658b 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 5fabf49..b54d930 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -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
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index d2b41e5..cf19fbf 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 512b789..6121d43 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 b41928e..c0cc342 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -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
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 0000000..6ca8900
--- /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" }
+}