avoid constant conditional expression warning

S

Sensorflo

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

Ed Morton

Sensorflo said:
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.
 
D

Derk Gwen

(e-mail address removed) (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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top