lkml.org 
[lkml]   [2008]   [Jul]   [11]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
SubjectRe: [patch 13/17] Use WARN() in drivers/base/
From
Date

> 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
[unhandled content-type:application/pgp-signature]
\
 
 \ /
  Last update: 2008-07-12 01:13    [W:0.184 / U:0.168 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site