[U-Boot] [PATCH] Add assert() for debug assertions

Mike Frysinger vapier at gentoo.org
Wed Jun 22 23:23:26 CEST 2011


On Wednesday, June 22, 2011 17:04:49 Simon Glass wrote:
> +/*
> + * An assertion is run-time check done in debug mode only. If DEBUG is not
> + * defined then it is skipped. It does not BUG or halt U-Boot, but tries
> to + * continue execution in any case. It is hoped that all failing
> assertions + * are found before release, and after release it is hoped
> that they don't + * matter. But in any case these failing assertions
> cannot be fixed with a + * BUG-type reset (which may just do the same
> assertion again).
> + */
> +#define assert(x)	\
> +	({ if (!(x)) printf("Assertion failure '%s' %s line %d\n", \
> +		#x, __FILE__, __LINE__); })
>  #else
>  #define debug(fmt,args...)
>  #define debugX(level,fmt,args...)
> +#define assert(x)
>  #endif	/* DEBUG */

the trouble with ifdef magic like this is that errors/warnings can be 
introduced when DEBUG isnt defined, and then only noticed when DEBUG is 
defined.  so how about:

#ifdef DEBUG
# define _DEBUG 1
#else
# define _DEBUG 2
#endif
#define assert(x) \
	do { \
		if ((x) && _DEBUG) \
			printf("%s:%s():%i: assertion failure: %s\n", \
				__FILE__, __func__, __LINE__, #x); \
	} while (0)

(and yes, i know debug() and debugX() are also broken this way)
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20110622/2d06b6b5/attachment.pgp 


More information about the U-Boot mailing list