From: Mike Frysinger Date: Wed, 22 Jun 2011 17:23:26 -0400 Subject: [U-Boot] [PATCH] Add assert() for debug assertions In-Reply-To: <1308776689-28115-1-git-send-email-sjg@chromium.org> References: <1308776689-28115-1-git-send-email-sjg@chromium.org> Message-ID: <201106221723.27679.vapier@gentoo.org> List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: u-boot@lists.denx.de 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