avoid constant conditional expression warning

From: Sensorflo (spamadress_at_bigfoot.com)
Date: 01/07/04

  • Next message: Lew Pitcher: "Re: Why multiplication not allowed?"
    Date: 7 Jan 2004 06:17:25 -0800
    
    

    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" );
    }


  • Next message: Lew Pitcher: "Re: Why multiplication not allowed?"

    Relevant Pages