avoid constant conditional expression warning

Discussion in 'C Programming' started by Sensorflo, Jan 7, 2004.

  1. Sensorflo

    Sensorflo Guest

    Im writting my own logger. Below is a very simplified snip of code,
    which illustrates my question. A message is Logged with the macro Log(
    priority, msg ). The message is only actually logged, if priority >=
    LOG_PRIORITY. But since priority and LOG_PRIORITY are both constants,
    the whole test is constant, which raises a warning. And I would like
    to be able to compile with no warnings, because I have read somewhere
    that gpl code should compile without warnings. If this test fails, no
    code at all should be generated for efficiency reasons. I made a
    workaround, see defintion of test with NO_WARNING==1. But it contains
    an unnessairy assignment, and if the compiler isnt smart enough also
    an unnessairy comparsion during runtime.

    Again my goals:
    - no code is produced if priority test fails
    - no code is produced for the test itself, since it is a constant test
    - no warnings
    - fast, no unnessairy stuff

    In your opinion, Which goals are more important, which are less
    important?
    What would you do fullfill these goals?

    Thank you

    Sensorflo



    #include "stdio.h"
    #define NO_WARNING 1
    #define LOG_PRIORITY 2

    static int sDummy;

    #if NO_WARNING
    #define test(expr) ( (sDummy = (expr)) != 0 )
    #else
    #define test(expr) ( expr )
    #endif

    #define Log( priority, msg ) \
    do { \
    if ( test( priority>=LOG_PRIORITY ) ) \
    puts(msg); \
    } while( test(0) )

    int main()
    { Log( 3, "hello" );
    }
     
    Sensorflo, Jan 7, 2004
    #1
    1. Advertising

  2. Sensorflo

    Ed Morton Guest

    Sensorflo wrote:
    > Im writting my own logger. Below is a very simplified snip of code,
    > which illustrates my question. A message is Logged with the macro Log(
    > priority, msg ). The message is only actually logged, if priority >=
    > LOG_PRIORITY. But since priority and LOG_PRIORITY are both constants,
    > the whole test is constant, which raises a warning. And I would like
    > to be able to compile with no warnings,


    <snip>

    Is it really your compiler that's generating the warning or is it lint?
    I've never seen that warning from a compiler so I can't advise you on
    that and I doubt if this will work for that case, but to stop lint
    complaining, add a "/* CONSTANTCONDITION */" (or abbreviated to "/*
    CONSTCOND */") comment beside the constant condition, e.g.:

    #define Log( priority, msg ) \
    do { \
    if ( priority>=LOG_PRIORITY /* CONSTCOND */ ) \
    puts(msg); \
    } while( 0 /* CONSTCOND */ )

    Regards,

    Ed.
     
    Ed Morton, Jan 7, 2004
    #2
    1. Advertising

  3. Sensorflo

    Derk Gwen Guest

    (Sensorflo) wrote:
    # Im writting my own logger. Below is a very simplified snip of code,
    # which illustrates my question. A message is Logged with the macro Log(
    # priority, msg ). The message is only actually logged, if priority >=
    # LOG_PRIORITY. But since priority and LOG_PRIORITY are both constants,
    # the whole test is constant, which raises a warning. And I would like
    # to be able to compile with no warnings, because I have read somewhere
    # that gpl code should compile without warnings. If this test fails, no
    # code at all should be generated for efficiency reasons. I made a

    You can also use cpp-#if to avoid a cc-if.

    Or stuff a sock into your nanny compiler's mouth. If your code is correct,
    the warning is just noise. Changing the code to get rid of the noise instead
    of getting rid of the noise maker is what's known as the tail wagging the dog.
    If you've got an optimiser turned--and in some cases even without--code which
    cannot be executed at all is painlessly excised from the object code. There
    will be no runtime test, no wasted space, no robot rules of order, no air.
    And your program remains readable and correct.

    --
    Derk Gwen http://derkgwen.250free.com/html/index.html
    So....that would make Bethany part black?
     
    Derk Gwen, Jan 7, 2004
    #3
    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. Replies:
    13
    Views:
    12,936
    Kai-Uwe Bux
    Jan 22, 2007
  2. Philipp
    Replies:
    26
    Views:
    2,308
    Ramon F Herrera
    Nov 25, 2007
  3. Adem
    Replies:
    42
    Views:
    2,823
    James Kuyper
    Nov 4, 2008
  4. Adem
    Replies:
    45
    Views:
    1,783
    James Kuyper
    Nov 4, 2008
  5. Fritz Bayer

    Conditional constant in regular expression

    Fritz Bayer, Apr 9, 2005, in forum: Perl Misc
    Replies:
    7
    Views:
    192
    Fritz Bayer
    Apr 10, 2005
Loading...

Share This Page