aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorMike Stump <mrs@apple.com>2007-05-08 01:31:24 +0000
committerMike Stump <mrs@gcc.gnu.org>2007-05-08 01:31:24 +0000
commit841cc7daf3e285036b0468ce9eb28695a8080e98 (patch)
tree37e00df220b8ab9eb5ac7e99af6a2be48f9819c4 /gcc
parent79d37e4574ed6ca0c6fbd189b62f84cb8e90450a (diff)
downloadgcc-841cc7daf3e285036b0468ce9eb28695a8080e98.zip
gcc-841cc7daf3e285036b0468ce9eb28695a8080e98.tar.gz
gcc-841cc7daf3e285036b0468ce9eb28695a8080e98.tar.bz2
invoke.texi (Warning Options): Document that -Wempty-body also checks for and while statements in C++.
* doc/invoke.texi (Warning Options): Document that -Wempty-body also checks for and while statements in C++. cp: * parser.c (check_empty_body): Add. (cp_parser_iteration_statement): Add call to check_empty_body. testsuite: * g++.old-deja/g++.mike/empty.C: Add. From-SVN: r124534
Diffstat (limited to 'gcc')
-rw-r--r--gcc/ChangeLog5
-rw-r--r--gcc/cp/ChangeLog5
-rw-r--r--gcc/cp/parser.c47
-rw-r--r--gcc/doc/invoke.texi11
-rw-r--r--gcc/testsuite/ChangeLog4
-rw-r--r--gcc/testsuite/g++.old-deja/g++.mike/empty.C25
6 files changed, 95 insertions, 2 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index c57813d..22b09d4 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,8 @@
+2007-05-07 Mike Stump <mrs@apple.com>
+
+ * doc/invoke.texi (Warning Options): Document that -Wempty-body
+ also checks for and while statements in C++.
+
2007-05-07 Nathan Froyd <froydnj@codesourcery.com>
* gcc.c (at_file_supplied): New variable.
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index b4a1c9a..e0286ff 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,8 @@
+2007-05-07 Mike Stump <mrs@apple.com>
+
+ * parser.c (check_empty_body): Add.
+ (cp_parser_iteration_statement): Add call to check_empty_body.
+
2007-05-05 Geoffrey Keating <geoffk@apple.com>
PR 31775
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 63f7fec..4599aca 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -7065,6 +7065,51 @@ cp_parser_condition (cp_parser* parser)
return cp_parser_expression (parser, /*cast_p=*/false);
}
+/* We check for a ) immediately followed by ; with no whitespacing
+ between. This is used to issue a warning for:
+
+ while (...);
+
+ and:
+
+ for (...);
+
+ as the semicolon is probably extraneous.
+
+ On parse errors, the next token might not be a ), so do nothing in
+ that case. */
+
+static void
+check_empty_body (cp_parser* parser, const char* type)
+{
+ cp_token *token;
+ cp_token *close_paren;
+ expanded_location close_loc;
+ expanded_location semi_loc;
+
+ close_paren = cp_lexer_peek_token (parser->lexer);
+ if (close_paren->type != CPP_CLOSE_PAREN)
+ return;
+
+ close_loc = expand_location (close_paren->location);
+ token = cp_lexer_peek_nth_token (parser->lexer, 2);
+
+ if (token->type != CPP_SEMICOLON
+ || (token->flags & PREV_WHITE))
+ return;
+
+ semi_loc = expand_location (token->location);
+ if (close_loc.line == semi_loc.line
+#ifdef USE_MAPPED_LOCATION
+ && close_loc.column+1 == semi_loc.column
+#endif
+ )
+ warning (OPT_Wempty_body,
+ "suggest a space before %<;%> or explicit braces around empty "
+ "body in %<%s%> statement",
+ type);
+}
+
/* Parse an iteration-statement.
iteration-statement:
@@ -7107,6 +7152,7 @@ cp_parser_iteration_statement (cp_parser* parser)
/* Parse the condition. */
condition = cp_parser_condition (parser);
finish_while_stmt_cond (condition, statement);
+ check_empty_body (parser, "while");
/* Look for the `)'. */
cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
/* Parse the dependent statement. */
@@ -7168,6 +7214,7 @@ cp_parser_iteration_statement (cp_parser* parser)
if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
expression = cp_parser_expression (parser, /*cast_p=*/false);
finish_for_expr (expression, statement);
+ check_empty_body (parser, "for");
/* Look for the `)'. */
cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index e608dbc..552e3b7 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -3157,6 +3157,11 @@ 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 @r{(C++ only)}
+An empty body occurs in a @samp{while} or @samp{for} statement with no
+whitespacing before the semicolon. This warning can be independently
+controlled by @option{-Wempty-body}.
+
@item
A pointer is compared against integer zero with @samp{<}, @samp{<=},
@samp{>}, or @samp{>=}.
@@ -3421,8 +3426,10 @@ to them.
@item -Wempty-body
@opindex Wempty-body
-An empty body occurs in an @samp{if}, @samp{else} or @samp{do while}
-statement. This warning is also enabled by @option{-Wextra}.
+Warn if an empty body occurs in an @samp{if}, @samp{else} or @samp{do
+while} statement. Additionally, in C++, warn when an empty body occurs
+in a @samp{while} or @samp{for} statement with no whitespacing before
+the semicolon. This warning is also enabled by @option{-Wextra}.
@item -Wsign-compare
@opindex Wsign-compare
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 8c0103f..6b6f395 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,7 @@
+2007-05-07 Mike Stump <mrs@apple.com>
+
+ * g++.old-deja/g++.mike/empty.C: Add.
+
2007-05-07 Eric Christopher <echristo@apple.com>
* gcc.dg/invalid-call-1.c: Fix options for 32-bit x86.
diff --git a/gcc/testsuite/g++.old-deja/g++.mike/empty.C b/gcc/testsuite/g++.old-deja/g++.mike/empty.C
new file mode 100644
index 0000000..d69f3ad
--- /dev/null
+++ b/gcc/testsuite/g++.old-deja/g++.mike/empty.C
@@ -0,0 +1,25 @@
+// { dg-options "-W" }
+
+#define NOPE
+
+void foo() {
+ while (1); /* { dg-error "suggest a space before " } */
+ {
+ }
+ for (;;); /* { dg-error "suggest a space before " } */
+ {
+ }
+ while (1)
+ ;
+ for (;;)
+ ;
+ while (1) ;
+ for (;;) ;
+ /* These two work when using mapped locations */
+ while (1) NOPE; /* { dg-bogus "suggest a space before " "suggest" { xfail *-*-* } } */
+ for (;;) NOPE; /* { dg-bogus "suggest a space before " "suggest" { xfail *-*-* } } */
+ while (1)
+ NOPE;
+ for (;;)
+ NOPE;
+}