为什么在内核中碰到很多 #define ... do{...} while(0) ?
有以下几点原因:
1、空语句在编译的时候会出现警告,所以有必要用#define FOO do{ } while(0)
2、给定一个基本块,可以在里面定义局部变量
3、为了能够在条件语句中使用复杂的宏定义。例如下面这段代码:
#define FOO(x) \
printf("arg is %s\n", x); \
do_something_useful(x);
如果这样用:
if (blah == 2)
F00(blah);
宏展开之后为
if (blah == 2)
printf("arg is %s\n", blah);
do_something_useful(blah);;
这样,if条件之后包含了printf()语句,而do_something_useful()调用不能按照预期那样工作。而是用do {...} while(0)定义后,就会展开成以下语句:
if (blah == 2)
do{
printf("arg is %s\n", blah);
do_something_useful(blah);
}while(0);
这是我们所期望的。
如果你希望定义一个包含多行语句和一些局部变量的时候. 一般的定义方式只能这样:
#define exch(x,y) { int tmp; tmp=x; x=y; y=tmp; }
然而在某些情况下,这样并不能正常工作. 下面是包含两个分支的if语句:
if (x > y)
exch(x,y); // Branch 1
else
do_something(); // Branch 2
但这样却只能展开成单分支的if语句,如下:
if (x > y) { // 单分支if
int tmp;
tmp = x;
x = y;
y = tmp;
}
; // 空语句
else // 错误!!! "parse error before else"
do_something();
问题是由于在语句块后直接加入分号(;)引起的. 解决办法是将语句块放入 do 和 while (0)中间.这样就得到了一条单语句, 而不是被编译器判断为语句块.现在的if语句如下:
if (x > y)
do {
int tmp;
tmp = x;
x = y;
y = tmp;
} while(0);
else
do_something();
假设有这样一个宏定义
#define macro(condition) if(condition) dosomething();
现在在程序中这样使用这个宏:
if(temp)
macro(i);
else
doanotherthing();
一切看起来很正常,但是仔细想想。这个宏会展开成:
if(temp)
if(condition) dosomething();
else
doanotherthing();
这时的else不是与第一个if语句匹配,而是错误的与第二个if语句进行了匹配,编译通过了,但是运行的结果一定是错误的。
为了避免这个错误,我们使用do{….}while(0) 把它包裹起来,成为一个独立的语法单元,从而不会与上下文发生混淆。同时因为绝大多数的编译器都能够识别do{…}while(0)这种无用的循环并进行优化,所以使用这种方法也不会导致程序的性能降低。
参考网站:https://kernelnewbies.org/FAQ/DoWhile0
原文如下:
FAQ/DoWhile0
Why do a lot of #defines in the kernel use do { ... } while(0)?
There are a couple of reasons:
-
(from Dave Miller) Empty statements give a warning from the compiler so this is why you see #define FOO do { } while(0).
-
(from Dave Miller) It gives you a basic block in which to declare local variables.
-
(from Ben Collins) It allows you to use more complex macros in conditional code. Imagine a macro of several lines of code like:
#define FOO(x) \ printf("arg is %s\n", x); \ do_something_useful(x);
if (blah == 2) FOO(blah);
if (blah == 2) printf("arg is %s\n", blah); do_something_useful(blah);;
As you can see, the if then only encompasses the printf(), and the do_something_useful() call is unconditional (not within the scope of the if), like you wanted it. So, by using a block like do { ... } while(0), you would get this:
if (blah == 2) do { printf("arg is %s\n", blah); do_something_useful(blah); } while (0);
-
(from Per Persson) As both Miller and Collins point out, you want a block statement so you can have several lines of code and declare local variables. But then the natural thing would be to just use for example:
#define exch(x,y) { int tmp; tmp=x; x=y; y=tmp; }
However that wouldn't work in some cases. The following code is meant to be an if-statement with two branches:
if (x > y) exch(x,y); // Branch 1 else do_something(); // Branch 2
But it would be interpreted as an if-statement with only one branch:
if (x > y) { // Single-branch if-statement!!! int tmp; // The one and only branch consists tmp = x; // of the block. x = y; y = tmp; } ; // empty statement else // ERROR!!! "parse error before else" do_something();
The problem is the semi-colon (;) coming directly after the block. The solution for this is to sandwich the block between do and while (0). Then we have a single statement with the capabilities of a block, but not considered as being a block statement by the compiler. Our if-statement now becomes:
if (x > y) do { int tmp; tmp = x; x = y; y = tmp; } while(0); else do_something();
-
(from Bart Trojanowski) gcc adds Statement-Expressions which provide an alternative to the do-while-0 block. They provide the above mentioned benefits and are slightly more legible.
#define FOO(arg) ({ \ typeof(arg) lcl; \ lcl = bar(arg); \ lcl; \ })