[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <1215817836.10052.2.camel@johannes.berg>
Date: Sat, 12 Jul 2008 01:10:36 +0200
From: Johannes Berg <johannes@...solutions.net>
To: Andrew Morton <akpm@...ux-foundation.org>
Cc: Arjan van de Ven <arjan@...radead.org>,
linux-kernel@...r.kernel.org, mingo@...e.hu
Subject: Re: [patch 13/17] Use WARN() in drivers/base/
> I don't suppose there's any way of tricking the preprocessor into
> supporting
>
> WARN_ON(foo == 42);
>
> as well as
>
> WARN_ON(foo == 42, "bite me!");
Here's one that abuses variadic macros and limits the number of
arguments to 19.
#include <stdarg.h>
#include <stdio.h>
#define cnt(y...) _cnt( , ## y)
#define _cnt(y...) __cnt(y,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0)
#define __cnt(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19,n,ys...) n
static void warn_on_slowpath(int argcount, ...)
{
va_list args;
char *fmt;
// print beginning of warning
if (argcount) {
va_start(args, count);
fmt = va_arg(args, char *);
vprintf(fmt, args);
va_end(args);
}
// print rest of warning
}
#define WARN_ON(test, fmt...) \
do { \
if (test) { \
warn_on_slowpath(cnt(fmt) , ## fmt); \
printf("WARN\n"); \
} \
} while (0)
int main(void)
{
WARN_ON(1);
WARN_ON(1, "asdf %d\n", 7);
return 0;
}
johannes
Download attachment "signature.asc" of type "application/pgp-signature" (837 bytes)
Powered by blists - more mailing lists