Suppressing Compiler Warning About Tricky Macro

Discussion in 'C Programming' started by Michael B Allen, Jul 22, 2007.

  1. Hi,

    I have a macro that looks like the following:

    #define MMSG msgno_loc0(LOC0, LOC1) && msgno_mmsg0

    which if used in some code like:

    MMSG("foo=%s", foo);

    would expand to [1]:

    loc0(LOC0, LOC1) && msgno_mmsg0("foo=%s", foo);

    My problem is that the GCC 64 bit compiler complains about this with:

    warning: value computed is not used

    Can someone recommend a change that can eliminate these warnings?

    I'm drawing a blank.

    Mike

    [1] LOC0 and LOC1 are not shown expaned for the sake of simplicity
    Michael B Allen, Jul 22, 2007
    #1
    1. Advertising

  2. On Jul 21, 10:52 pm, Michael B Allen <> wrote:
    > Hi,
    >
    > I have a macro that looks like the following:
    >
    > #define MMSG msgno_loc0(LOC0, LOC1) && msgno_mmsg0
    >
    > which if used in some code like:
    >
    > MMSG("foo=%s", foo);
    >
    > would expand to [1]:
    >
    > loc0(LOC0, LOC1) && msgno_mmsg0("foo=%s", foo);
    >
    > My problem is that the GCC 64 bit compiler complains about this with:
    >
    > warning: value computed is not used
    >
    > Can someone recommend a change that can eliminate these warnings?


    The warning doesn't appear to be coming from the preprocessor. The
    fully expanded version probably contains an expression who value is
    discarded, without seeing the fully expanded version (and possibly the
    appropriate associated definitions) it is difficult to determine
    exactly where the problem is.

    Robert Gamble
    Robert Gamble, Jul 22, 2007
    #2
    1. Advertising

  3. On Jul 22, 4:52 am, Michael B Allen <> wrote:
    > Hi,
    >
    > I have a macro that looks like the following:
    >
    > #define MMSG msgno_loc0(LOC0, LOC1) && msgno_mmsg0
    >
    > which if used in some code like:
    >
    > MMSG("foo=%s", foo);
    >
    > would expand to [1]:
    >
    > loc0(LOC0, LOC1) && msgno_mmsg0("foo=%s", foo);
    >
    > My problem is that the GCC 64 bit compiler complains about this with:
    >
    > warning: value computed is not used
    >
    > Can someone recommend a change that can eliminate these warnings?

    #define MMSG_MODIFIED msgno_loc0(LOC0, LOC1); msgno_mmsg0

    OR:

    bool bValue = MMSG("foo=%s", foo);

    Note that this line:
    loc0(LOC0, LOC1) && msgno_mmsg0("foo=%s", foo);
    (actually should be: msgno_loc0(LOC0, LOC1) && msgno_mmsg0("foo=%s",
    foo); )

    Calls two functions msgno_loc0 and msgno_mmsg0 which in turn return
    (supposably) bool values
    These two boolean values are ANDed with the && operator and the final
    ADDed value is discarded since you're not
    assigning it to another variable nor using it in a if condition.

    Abdo Haji-Ali
    Programmer
    In|Framez
    Abdo Haji-Ali, Jul 22, 2007
    #3
  4. On Sun, 22 Jul 2007 03:38:13 -0000
    Robert Gamble <> wrote:

    > On Jul 21, 10:52 pm, Michael B Allen <> wrote:
    > > Hi,
    > >
    > > I have a macro that looks like the following:
    > >
    > > #define MMSG msgno_loc0(LOC0, LOC1) && msgno_mmsg0
    > >
    > > which if used in some code like:
    > >
    > > MMSG("foo=%s", foo);
    > >
    > > would expand to [1]:
    > >
    > > msgno_loc0(LOC0, LOC1) && msgno_mmsg0("foo=%s", foo);

    ^^^^^^corrected
    > >
    > > My problem is that the GCC 64 bit compiler complains about this with:
    > >
    > > warning: value computed is not used
    > >
    > > Can someone recommend a change that can eliminate these warnings?

    >
    > The warning doesn't appear to be coming from the preprocessor. The
    > fully expanded version probably contains an expression who value is
    > discarded, without seeing the fully expanded version (and possibly the
    > appropriate associated definitions) it is difficult to determine
    > exactly where the problem is.


    Hi Robert,

    The LOC0 and LOC1 macros just expand to a filename, line number and
    possibly a function name.

    The msgno_loc0 and msgno_mmsg0 symbols are functions. Additionally
    msgno_loc0 function always returns 1.

    What I did do that seems to be ok for now is:

    #define MMSG if (msgno_loc0(LOC0, LOC1)) msgno_mmsg0

    but in hind sight now I'm not entirely clear as to why I didn't just do:

    #define MMSG msgno_loc0(LOC0, LOC1); msgno_mmsg0

    Mike
    Michael B Allen, Jul 22, 2007
    #4
  5. On Sat, 21 Jul 2007 22:52:28 -0400, Michael B Allen <>
    wrote:

    >Hi,
    >
    >I have a macro that looks like the following:
    >
    > #define MMSG msgno_loc0(LOC0, LOC1) && msgno_mmsg0
    >
    >which if used in some code like:
    >
    > MMSG("foo=%s", foo);
    >
    >would expand to [1]:
    >
    > loc0(LOC0, LOC1) && msgno_mmsg0("foo=%s", foo);


    No, but hopefully it is just a typo. The left operand of the &&
    operator would be
    msgno_loc0(LOC0, LOC1)
    It is important to cut and paste the code, not retype it.

    >
    >My problem is that the GCC 64 bit compiler complains about this with:
    >
    > warning: value computed is not used


    Look at the above statement. The && operator evaluates its left
    operand. If not zero, it evaluates the right operand. If that is
    also not zero, the result is set to 1. If either operand is zero, the
    result is set to 0. But the result is NEVER used. It is not assigned
    to a variable; it is not part of an if/while/?:/for construct. After
    going through the trouble of calling the first function, calling the
    second function (only if the first returns a non-zero value), and
    computing the boolean AND, the result is thrown in the bit bucket,
    never to be seen again.

    If either of the two functions have side effects, this may be a
    reasonable thing to do. However, in general, it's not.

    >
    >Can someone recommend a change that can eliminate these warnings?


    There are two obvious (to me) ways to silence the warning.

    If you care about the result, assign it to a variable or use
    it in one of the conditional constructs.

    If you don't care about the result of the expression but only
    about the side effects of the function calls, then change the macro to
    something like

    #define MMSG \
    if (msgno_loc0(LOC0, LOC1)) \
    msgno_mmsg0

    Since you chose to have the arguments to msgno_mmsg0 outside the
    macro, you cannot define the macro expansion with the usual
    do(...)while(0); construct.


    Remove del for email
    Barry Schwarz, Jul 22, 2007
    #5
  6. Michael B Allen

    Eric Sosman Guest

    Michael B Allen wrote:
    > On Sun, 22 Jul 2007 03:38:13 -0000
    > Robert Gamble <> wrote:
    >
    >> On Jul 21, 10:52 pm, Michael B Allen <> wrote:
    >>> Hi,
    >>>
    >>> I have a macro that looks like the following:
    >>>
    >>> #define MMSG msgno_loc0(LOC0, LOC1) && msgno_mmsg0
    >>>
    >>> which if used in some code like:
    >>>
    >>> MMSG("foo=%s", foo);
    >>>
    >>> would expand to [1]:
    >>>
    >>> msgno_loc0(LOC0, LOC1) && msgno_mmsg0("foo=%s", foo);

    > ^^^^^^corrected
    >>> My problem is that the GCC 64 bit compiler complains about this with:
    >>>
    >>> warning: value computed is not used
    >>>
    >>> Can someone recommend a change that can eliminate these warnings?

    >> The warning doesn't appear to be coming from the preprocessor. The
    >> fully expanded version probably contains an expression who value is
    >> discarded, without seeing the fully expanded version (and possibly the
    >> appropriate associated definitions) it is difficult to determine
    >> exactly where the problem is.

    >
    > Hi Robert,
    >
    > The LOC0 and LOC1 macros just expand to a filename, line number and
    > possibly a function name.
    >
    > The msgno_loc0 and msgno_mmsg0 symbols are functions. Additionally
    > msgno_loc0 function always returns 1.
    >
    > What I did do that seems to be ok for now is:
    >
    > #define MMSG if (msgno_loc0(LOC0, LOC1)) msgno_mmsg0
    >
    > but in hind sight now I'm not entirely clear as to why I didn't just do:
    >
    > #define MMSG msgno_loc0(LOC0, LOC1); msgno_mmsg0


    Probably because both of these will produce unpleasant
    surprises in contexts like

    if (phaseOfMoon == FULL)
    MMSG("foo=%s", foo);
    else
    MMSG("bar=%s", bar);

    --
    Eric Sosman
    lid
    Eric Sosman, Jul 22, 2007
    #6
  7. On Sun, 22 Jul 2007 12:07:33 -0400, Michael B Allen <>
    wrote:

    >On Sun, 22 Jul 2007 03:38:13 -0000
    >Robert Gamble <> wrote:
    >
    >> On Jul 21, 10:52 pm, Michael B Allen <> wrote:
    >> > Hi,
    >> >
    >> > I have a macro that looks like the following:
    >> >
    >> > #define MMSG msgno_loc0(LOC0, LOC1) && msgno_mmsg0
    >> >
    >> > which if used in some code like:
    >> >
    >> > MMSG("foo=%s", foo);
    >> >
    >> > would expand to [1]:
    >> >
    >> > msgno_loc0(LOC0, LOC1) && msgno_mmsg0("foo=%s", foo);

    > ^^^^^^corrected
    >> >
    >> > My problem is that the GCC 64 bit compiler complains about this with:
    >> >
    >> > warning: value computed is not used
    >> >
    >> > Can someone recommend a change that can eliminate these warnings?

    >>
    >> The warning doesn't appear to be coming from the preprocessor. The
    >> fully expanded version probably contains an expression who value is
    >> discarded, without seeing the fully expanded version (and possibly the
    >> appropriate associated definitions) it is difficult to determine
    >> exactly where the problem is.

    >
    >Hi Robert,
    >
    >The LOC0 and LOC1 macros just expand to a filename, line number and
    >possibly a function name.
    >
    >The msgno_loc0 and msgno_mmsg0 symbols are functions. Additionally
    >msgno_loc0 function always returns 1.
    >
    >What I did do that seems to be ok for now is:
    >
    > #define MMSG if (msgno_loc0(LOC0, LOC1)) msgno_mmsg0
    >
    >but in hind sight now I'm not entirely clear as to why I didn't just do:
    >
    > #define MMSG msgno_loc0(LOC0, LOC1); msgno_mmsg0


    Maybe because you subconsciously realized that they are not
    equivalent. In the first case, msgno_mmsg0 is called only if
    msgno_loc0 returns a non-zero value. In the second, msgno_mmsg0 is
    called unconditionally.


    Remove del for email
    Barry Schwarz, Jul 22, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Hansen

    Suppressing "Parameter not used" Warning

    Dave Hansen, Oct 3, 2005, in forum: C Programming
    Replies:
    40
    Views:
    1,063
    Anonymous 7843
    Oct 14, 2005
  2. Replies:
    9
    Views:
    521
    CBFalconer
    Apr 25, 2006
  3. filmil
    Replies:
    3
    Views:
    449
  4. Ian Collins

    Re: Multi-instructions macro and compiler warning

    Ian Collins, Oct 16, 2011, in forum: C Programming
    Replies:
    12
    Views:
    919
    Keith Thompson
    Oct 17, 2011
  5. Jean-Christophe

    Re: Multi-instructions macro and compiler warning

    Jean-Christophe, Oct 16, 2011, in forum: C Programming
    Replies:
    3
    Views:
    378
    Ian Collins
    Oct 16, 2011
Loading...

Share This Page