473,320 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

avoid constant conditional expression warning

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
2 5788


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Marty | last post by:
Hi, In VB.NET, what happen to constants when compiling? Are they copied at every place they are needed? Or the application has to refer to constants address each time it need it at run time? ...
8
by: raan | last post by:
Microsoft C++ apparently doesn't permit me to initialize a static constant with in my class scope. Ok, I think that is previously discussed here and people have suggested some hacks to circumvent...
5
by: paulo | last post by:
Can anyone please tell me how the C language interprets the following code: #include <stdio.h> int main(void) { int a = 1; int b = 10; int x = 3;
15
by: Dan Henry | last post by:
I have run across functions in the Linux kernel's MTD driver that have me scratching my head a bit. The functions have the general form: extern int bar(size_t len, size_t *retlen, unsigned char...
15
by: skibud2 | last post by:
Consider this example: u8 my_array; for (i = 0; i < (sizeof(my_array)/sizeof(u8)); i++) { ... } In the standard C specification, is the evaluation of '(sizeof(my_array)/sizeof(u8))' to 10...
3
by: _Christopher\(M2M\) | last post by:
Microsoft Visual Studio 2005 Version 8.0.50727.762 (SP.050727-7600) How do I get rid of "Warning C4307: '-' : integral constant overflow" from the following macro? #define...
26
by: kerravon | last post by:
The following C program: int main(void) { int x = -2147483648; return (0); } Produces the following warning:
11
by: kerravon | last post by:
According to 6.8 of C90, #if takes a constant expression. According to 6.4 of C90, the sizeof operator is part of a constant expression. Why then do most of my compilers barf on this? ...
9
by: Eric | last post by:
I am working on a large, old code base and attempting to move it to GCC 4.2. Throughout the code, there is stuff like: char *aVar = "aString"; or void aFunc( char *aVar) { ... } aFunc(...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.