gcc 2.7.2 warning: left-hand operand of comma expression has no effect 
Author Message
 gcc 2.7.2 warning: left-hand operand of comma expression has no effect

We've just moved from gcc 2.6.3 to 2.7.2 and we're receiving the following new
warnings (when -Wall is specified)....

warning: left-hand operand of comma expression has no effect

When this section of code is encountered in our source files...

static char    *rcsid = "$Id: cfgopen.c,v 1.4 1995/12/11 16:04:35 gcarr Exp $";
#if __GNUC__ == 2
#define USE(var) static void * use_##var = (&use_##var, (void *) &var)
USE(rcsid);
#endif

(This puts the version stamp of each source file in the executable so that we
can use rcs indent to read the stamps from the exe file if there is a question about
which source was used to build.  The USE macro, fakes gcc into thinking (or used to)
that 'rcsid' is used when in fact it is not, so that gcc wouldn't complain about
unused variables if -Wunused is set.)

We also have another frequent occurrence of this problem.  We have defined a 'Dbg'
macro as follows...

void            _DbgLog1(char *pszFile, int nLine, char *pszTimeFmt);
void            _DbgLog2(char *pszFmt,...);
#ifdef DEBUG
#define Dbg    _DbgLog1(__FILE__, __LINE__, DBGLOG_TIMEFMT), _DbgLog2
#else
#define Dbg    (void)
#endif /* DEBUG */

We use this for logging debug/trace to stdout if DEBUG is defined.  'Dbg' is defined
to as two functions in order to print the __FILE__ and __LINE__ along with any printf
style parameters without explicitly including the __FILE__ and __LINE__ in the Dbg
call.

Dbg("The value of x is %d\n", x);

...which outputs something like this...

[file.c,897] The value of x is 32

Anyway, can this warning be turned off without turning off -Wall or -Wunused (warns
about unused variables and labels)?  I found this section of code in the gcc source...

...

      /* The left-hand operand of a comma expression is like an expression
         statement: with -W or -Wunused, we should warn if it doesn't have
         any side-effects, unless it was explicitly cast to (void).  */
      if ((extra_warnings || warn_unused)
           && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
                && TREE_TYPE (TREE_VALUE (list)) == void_type_node))
        warning ("left-hand operand of comma expression has no effect");

...

How do I or can I "explicitly cast to (void)" the return value(s) of these
comma-separated expressions to get rid of these warnings?  Or is there another way to
get rid of these warnings without getting rid of -Wunused?   I would prefer to work
around these two specific problems and continue to get the warning for other cases.

--
Glenn Carr

Landmark Graphics



Sun, 23 Aug 1998 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. How to update right hand pane when item selected in left hand pane tree

2. Sorting Array Error - left operand must be L-value

3. Padding numbers with left hand zeroes

4. ??? Semantics: Side Effects In Parallel Commas ???

5. how to make toolbars dock at the left hand side

6. left hand side of Windows Explorer

7. Use of comma expression in for() statement

8. side effects in expressions

9. comma as line terminator doesn't generate compiler warning

10. ? and = do not go hand-in-hand

11. Does the gcc optimizer rearrange floating point expressions?

12. BUG ? : warning C4706: assignment within conditional expression

 

 
Powered by phpBB® Forum Software