Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > avoid constant conditional expression warning

Reply
Thread Tools

avoid constant conditional expression warning

 
 
Sensorflo
Guest
Posts: n/a
 
      01-07-2004
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" );
}
 
Reply With Quote
 
 
 
 
Ed Morton
Guest
Posts: n/a
 
      01-07-2004


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.

 
Reply With Quote
 
 
 
 
Derk Gwen
Guest
Posts: n/a
 
      01-07-2004
(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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Case expression must be constant expression Philipp Java 26 11-25-2007 10:10 PM
"error C2057: expected constant expression", "error C2466: cannot allocate an array of constant size 0". Why doesn't my simple program work??? hn.ft.pris@gmail.com C++ 13 01-22-2007 02:03 PM
Conditional constant in regular expression Fritz Bayer Perl Misc 7 04-10-2005 09:43 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57