Re: [patch 13/17] Use WARN() in drivers/base/

From: Johannes Berg
Date: Fri Jul 11 2008 - 19:11:19 EST



> 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

Attachment: signature.asc
Description: This is a digitally signed message part