By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 817 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

avoid constant conditional expression warning

P: n/a
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" );
}
Nov 14 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a


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.

Nov 14 '05 #2

P: n/a
sp********@bigfoot.com (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?
Nov 14 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.