Bug 5520

Summary: Add a warning to detect empty body of if statements (like in the C frontend)
Product: gcc Reporter: jbach1
Component: c++Assignee: Gabriel Dos Reis <gdr>
Status: RESOLVED FIXED    
Severity: enhancement CC: austern, carlo, gcc-bugs, giovannibajo, mueller, nathan, simon_baldwin
Priority: P3 Keywords: diagnostic
Version: 3.4.0   
Target Milestone: 4.2.0   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2005-12-11 21:47:29
Attachments: updated patch

Description jbach1 2002-01-28 15:46:01 UTC
I think a warning shoul be raised when someone writes
if (condition);
{
}

instead of
if (condition)
{
}

(===== Watch at the ";" ====)

Actually, it can only be a mistake, and that's a pity it's not detected. It drives to severe bugs.

Release:
2.95 -> 3.4

Environment:
DOS (DJGPP)

How-To-Repeat:
int x=1;
if (x == 0);
{
   cout << "x equals zero" << endl;
}
Comment 1 jbach1 2002-01-28 15:46:01 UTC
Fix:
int x=1;
if (x == 0)
{
   cout << "x equals zero" << endl;
}
Comment 2 carlo 2002-01-29 03:09:21 UTC
From: Carlo Wood <carlo@alinoe.com>
To: jbach1@caramail.com
Cc: gcc-gnats@gcc.gnu.org
Subject: Re: preprocessor/5520: There's a warning I miss with "if{} else{}" statement
Date: Tue, 29 Jan 2002 03:09:21 +0100

 On Mon, Jan 28, 2002 at 11:41:20PM -0000, jbach1@caramail.com wrote:
 > I think a warning shoul be raised when someone writes
 > if (condition);
 
 Only *before* replacing macros please (if at all).
 Macros can be empty and be used like:
 if (condition)
   POSSIBLY_EMPTY_MACRO;
 
 -- 
 Carlo Wood <carlo@alinoe.com>
Comment 3 Nathan Sidwell 2002-09-15 12:11:48 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: confirmed. gcc has such a warning.
    
    To address carlo's point. Macros that expand to noexpression, are usually written as '((void)0)'
Comment 4 Giovanni Bajo 2003-05-09 19:01:39 UTC
Responsible-Changed-From-To: unassigned->gdr
Responsible-Changed-Why: Diagnostic mantainer
Comment 6 Falk Hueffner 2006-01-05 10:36:38 UTC
*** Bug 19773 has been marked as a duplicate of this bug. ***
Comment 7 Nathan Sidwell 2006-01-05 11:22:41 UTC
Subject: Re:  Add a warning to detect empty body of if statements
 (like in the C frontend)

mueller at kde dot org wrote:
> ------- Comment #5 from mueller at kde dot org  2006-01-05 10:19 -------
> http://gcc.gnu.org/ml/gcc-patches/2005-12/msg01627.html

Could cp_parser_implicitly_scoped_statement be flattened to use 
cp_lexer_next_token_is, rather than CLNTI_not?  that'd read better.  I'd suggest
  if(cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
    .. handle {...}
  else if (cp_lexer_next_token_is (parser->lexer (CPP_SEMICOLON))
    .. handle empty
  else
    .. handle implicitly scoped stmt

The C++ changes will be ok with that change.  Someone else will need to approve 
the C change (but it looks ok to me)

nathan

Comment 8 Dirk Mueller 2006-01-08 21:59:47 UTC
Created attachment 10596 [details]
updated patch

I agree, that makes it much more readable. updated accordingly and rediffed against current trunk. bootstrapped, regtested successfully.
Comment 9 Richard Biener 2006-01-20 09:30:30 UTC
Subject: Bug 5520

Author: rguenth
Date: Fri Jan 20 09:30:22 2006
New Revision: 110019

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=110019
Log:
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.

Added:
    trunk/gcc/testsuite/g++.dg/warn/empty-body.C
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/c-common.c
    trunk/gcc/c-common.h
    trunk/gcc/c-parser.c
    trunk/gcc/c-typeck.c
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/parser.c
    trunk/gcc/cp/semantics.c
    trunk/gcc/testsuite/ChangeLog

Comment 10 Richard Biener 2006-01-20 10:11:13 UTC
Fixed.
Comment 11 Andrew Pinski 2006-09-08 23:28:47 UTC
*** Bug 28992 has been marked as a duplicate of this bug. ***