lkml.org 
[lkml]   [2001]   [Aug]   [24]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
SubjectRe: macro conflict
From
Date
>>>>> "David" == David Woodhouse <dwmw2@infradead.org> writes:

> printf ("%d %d\n", min(foo, 10), min (bar, 20)); }

David> Well, ideally both of them would BUG() and the user would have
David> to explicitly cast one (or both) of the arguments so the types
David> match. But as Keith pointed out, it won't work.

Why would both BUG? 20 is a signed integer and bar _could be_ a signed
char. This is fine. As a matter of fact, both constants are positive
and fit in the range so I don't really think this is a bug in either
case. I guess the constants should be written as 10U and 20U. There
are other problems as the code without a specifically type char will
have bugs on the ARM (and others with different sign default). I
don't think that the casting handles this well either.

I did a little more beautification. Gcc does warn if you compare a
pointer to an integral type. Do you need more? The bug_paste macro
would pollute the name-space, but it is nice to see where things go
wrong.

fwiw,
Bill Pringlemeir.

[test.c]
#define bug_paste_chain(a,b) a##b
#define bug_paste(a) bug_paste_chain(BUG_AT_LINE_,a)
#define min(x,y) \
\
({extern void bug_paste(__LINE__) (void); \
typeof(x) _x = 0; typeof(y) _y = 0; \
if ((_x-1>0 && _y-1<0) || (_x-1<0 && _y-1>0)) \
bug_paste(__LINE__)(); \
_x = (x), _y = (y); (_x>_y)?_y:_x; \
})

#include <stdio.h>
int main(int argc, char *argv[])
{
signed char cx = 1, cy = 2;
signed short sx = 1, sy = 2;
signed int ix = 1, iy = 2;
signed long lx = 1, ly = 2;
unsigned char Cx = 1, Cy = 2;
unsigned short Sx = 1, Sy = 2;
unsigned int Ix = 1, Iy = 2;
unsigned long Lx = 1, Ly = 2;

cx = min(cx,cy); Cx = min(Cy,Cx); sx = min(sx,sy);
Sx = min(Sx,Sy); ix = min(iy,ix); Ix = min(Iy,Ix);
lx = min(ly,ly); Lx = min(Lx,Ly);

/* No warning. */
/* Lx = (typeof(Lx))min(Lx,&Ly); */

printf("%d %d %hd %hd %d %d %ld %ld\n", cx, Cx,
sx,Sx,ix,Ix,lx,Lx);

cx = -1; Cx = 0;
cx = min(cx,cy); sx = min(cx,sy);
ix = min(cx,ix); lx = min(cx,ly);
Cx = min(Cx,Cx); Sx = min(Cx,Sy);
Ix = min(Ly,Ix); Lx = min(Ix,Ly);

/* correctly gives warning! Promotion to int before compare. */
/* Ix = min(Cy,Ix); Lx = min(Cx,Ly); */

printf("%d %d %hd %hd %d %d %ld %ld\n", cx, Cx,
sx,Sx,ix,Ix,lx,Lx);

/* BUG? printf ("%d\n", min(Iy, 10)); */
printf ("%d\n", min(cx, 20));

return 0;
}


-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

\
 
 \ /
  Last update: 2005-03-22 12:58    [W:0.174 / U:0.040 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site