Thread View: gnu.gcc.help
4 messages
4 total messages
Started by Glenn Carr
Wed, 06 Mar 1996 19:39
gcc 2.7.2 warning: left-hand operand of comma expression has no effect
Author: Glenn Carr
Date: Wed, 06 Mar 1996 19:39
Date: Wed, 06 Mar 1996 19:39
67 lines
3144 bytes
3144 bytes
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
gcarr@lgc.com
Landmark Graphics
Re: gcc 2.7.2 warning: left-hand operand of comma expression has no effect
Author: Glenn Carr
Date: Wed, 06 Mar 1996 20:13
Date: Wed, 06 Mar 1996 20:13
36 lines
1717 bytes
1717 bytes
Glenn Carr wrote:
> 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
Fixed this by adding (void) before &use_##var...
#define USE(var) static void * use_##var = ((void)&use_##var, (void *) &var)
>
> 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 */
>
Fixed _part_ of this problem by adding (void) before _DbgLog1...
#define DbgLog ((void)_DbgLog1(__FILE__, __LINE__, DBGLOG_TIMEFMT), _DbgLog2)
...but only when DEBUG is defined. When DEBUG is not defined, the preprocessed output
looks like this...
(void) ("The value of x is %d\n", x);
...in which case I still get the warning.
I guess (ugh) I could fall back to using Dbg(("printf args %d,%d,%d",x,y,z)),
but I really don't want to!
Re: gcc 2.7.2 warning: left-hand operand of comma expression has no effect
Author: Bill McCarthy
Date: Fri, 08 Mar 1996 03:48
Date: Fri, 08 Mar 1996 03:48
23 lines
1276 bytes
1276 bytes
In <313DEA09.237C@tulsa.lgc.com>, Glenn Carr <gcarr@tulsa.lgc.com> writes: >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 Isn't comma in the #define is a comma operator? Then you are taking the address of use_rcsid and discarding it. What will be returned to use_rcsid is the address of rcsid (a char **) cast to a (void *). Bill McCarthy actuary@nando.net Wendell, NC USA
Re: gcc 2.7.2 warning: left-hand operand of comma expression has no effect
Author: Glenn Carr
Date: Mon, 11 Mar 1996 15:53
Date: Mon, 11 Mar 1996 15:53
28 lines
1729 bytes
1729 bytes
Bill McCarthy wrote: > > In <313DEA09.237C@tulsa.lgc.com>, > Glenn Carr <gcarr@tulsa.lgc.com> writes: > > >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 > > Isn't comma in the #define is a comma operator? Then you are taking the > address of use_rcsid and discarding it. What will be returned to use_rcsid > is the address of rcsid (a char **) cast to a (void *). Yes, it is. It's a trick to inhibit the gcc warning about an unused variable for 'rcsid'. The initialization of use_rcsid 'uses' both rcsid and use_rcsid. I fixed the problem by casting the first operand of the comma operator to void... #define USE(var) static void * use_##var = ((void)&use_##var, (void *) &var)
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads