|
Assertion macro
#343215
10/04/10 14:38
10/04/10 14:38
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
OP
Expert
|
OP
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
Heyho, I made this small assertion macro for one of my projects:
#define LFAssert(condition, message) if(!(condition)){error(message); sys_exit(NULL);}
Here is how I use it:
LFAssert(foopointer, "Some error message");
It compiles and runs great with the GCC (4.2) and LLVM (1.5, 1.6, 2.0) (same macro but printf() and abort() instead of error() und sys_exit()) but the Lite-C compiler refuses to accept the way I use it (A8.02) Is there something that I miss or is this a bug?
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: Assertion macro
[Re: WretchedSid]
#343242
10/04/10 17:36
10/04/10 17:36
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Youve missed the final ";" in your thinking... Type-out the macro-replacement manually... and keep an eye on the red ";" LFAssert(foopointer, "Some error message");becomes if(!(foopointer)){"Some error message"); sys_exit(NULL);};You see? I tend to "fake" this with a REALLY ugly hack like so...
#define LFAssert(con, msg) if(!(con)){error(msg); sys_exit(NULL);} var who_cares=0
//[[condition and message shortened just for display purposes...]]
which then becomes if(!(foopointer)){"Some error message"); sys_exit(NULL);} var who_cares=0;VERY ugly, but work MOST of the time... Mind you, I wish there was a better way...
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Assertion macro
[Re: EvilSOB]
#343244
10/04/10 17:58
10/04/10 17:58
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
OP
Expert
|
OP
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
I can't believe it... what an annoying thing -.- Really, a ; too much shouldn't be a problem and every C compiler I know of doesn't complain about it.
Thank you very much EvilSOB!
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: Assertion macro
[Re: WretchedSid]
#343249
10/04/10 18:26
10/04/10 18:26
|
Joined: Mar 2006
Posts: 3,538 WA, Australia
JibbSmart
Expert
|
Expert
Joined: Mar 2006
Posts: 3,538
WA, Australia
|
Groovy, I was just yesterday thinking about using an assert macro to make my code a bit safer, and then saw this thread today. I would personally use a function as well to avoid the hacky stuff (and potential double-definitions of who_cares [even though who_cars could be declared globally and then re-assigned in the assertion, I guess]):
function failAssert(char *msg) {
error(msg);
sys_exit(NULL);
}
#define assert(con, msg) if(!(con))failAssert(msg)
Jibb
Formerly known as JulzMighty. I made KarBOOM!
|
|
|
Re: Assertion macro
[Re: JibbSmart]
#343251
10/04/10 18:36
10/04/10 18:36
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
OP
Expert
|
OP
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
Okay, I found another thing, my fallback (#ifdef NDEBUG) looks like this:
#define LFAssert(condition, message) ((void) 0)
guess what, it isnt working... WHAT? Why? "Can't convert long to void" My other fallback would be "asm ("nop");" but this also fails (feature request: Inline assembler!) What do? D: Edit: @JulzyMighty: I used a foo variable named "iLoveEvilSOB". I guess that no one uses a variable like this Btw, I just found a solution for my new problem... Just declare the variable again -.-
Last edited by JustSid; 10/04/10 18:40.
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: Assertion macro
[Re: WretchedSid]
#343253
10/04/10 19:16
10/04/10 19:16
|
Joined: Mar 2006
Posts: 3,538 WA, Australia
JibbSmart
Expert
|
Expert
Joined: Mar 2006
Posts: 3,538
WA, Australia
|
Edit: @JulzyMighty: I used a foo variable named "iLoveEvilSOB". I guess that no one uses a variable like this You could be surprised how many people use a variable named "iLoveEvilSOB" My other fallback would be "asm ("nop");" but this also fails (feature request: Inline assembler!) ++. I wonder what jcl would say. Jibb
Formerly known as JulzMighty. I made KarBOOM!
|
|
|
Re: Assertion macro
[Re: JibbSmart]
#343258
10/04/10 20:02
10/04/10 20:02
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
No one loves EvilSOB, but he's happy that way... There is just a lot of SARCARSTIC peoples on this forum, who are jealous of my extreme intelligence and humility
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Assertion macro
[Re: EvilSOB]
#343259
10/04/10 20:04
10/04/10 20:04
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Back to serious mode now. How about this...
function failAssert(char *msg)
{
#ifdef NDEBUG
return;
#endif
error(msg);
sys_exit(NULL);
}
#define assert(con, msg) if(!(con))failAssert(msg)
Is that the idea of the assert?
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Assertion macro
[Re: EvilSOB]
#343260
10/04/10 20:21
10/04/10 20:21
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
OP
Expert
|
OP
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
I don't want this kind of assert because it isn't inlined like a #define is. Or at least it isn't inlined in the Lite-C compiler (feature request #2: inline functions).
But basically, yes, this would be the assert. Btw, I switched from the iLoveEvilSOB variable to an internal dummy variable with my own prefix.
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: Assertion macro
[Re: WretchedSid]
#343262
10/04/10 20:27
10/04/10 20:27
|
Joined: Sep 2003
Posts: 9,859
FBL
Senior Expert
|
Senior Expert
Joined: Sep 2003
Posts: 9,859
|
|
|
|
|