From: mrs Date: Tue, 8 May 2007 01:31:24 +0000 (+0000) Subject: * doc/invoke.texi (Warning Options): Document that -Wempty-body X-Git-Url: http://git.sourceforge.jp/view?p=pf3gnuchains%2Fgcc-fork.git;a=commitdiff_plain;h=cc742166738d2d36fbace4160b3c278a49c215f6 * 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. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@124534 138bc75d-0d04-0410-961f-82ee72b054a4 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index c57813d4031..22b09d4fc5f 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2007-05-07 Mike Stump + + * doc/invoke.texi (Warning Options): Document that -Wempty-body + also checks for and while statements in C++. + 2007-05-07 Nathan Froyd * gcc.c (at_file_supplied): New variable. diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index b4a1c9a1b0a..e0286ff6505 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,8 @@ +2007-05-07 Mike Stump + + * parser.c (check_empty_body): Add. + (cp_parser_iteration_statement): Add call to check_empty_body. + 2007-05-05 Geoffrey Keating PR 31775 diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 63f7fec0169..4599aca0166 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 e608dbc017e..552e3b731b0 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 8c0103f28d9..6b6f3953b34 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2007-05-07 Mike Stump + + * g++.old-deja/g++.mike/empty.C: Add. + 2007-05-07 Eric Christopher * 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 00000000000..d69f3ad6243 --- /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; +}