clang 3.9增加了-Wall
警告选项,其中包括-Wexpansion-to-defined
警告选项,该选项会产生以下警告:
宏定义展开为“defined”的行为未定义
如果defined
在#if
表达式之外被使用,包括将宏用于#if
表达式的情况。例如,以下代码:
// in some file:
#define HAS_GNU (defined(__GNUC__) && !defined(__clang__))
// possibly in another file:
#if defined(__clang__) || HAS_GNU
/* ... */
#endif
产生
test.cc:5:27: warning: macro expansion producing 'defined' has undefined behavior [-Wexpansion-to-defined]
#if defined(__clang__) || HAS_GNU
^
test.cc:3:18: note: expanded from macro 'HAS_GNU'
#define HAS_GNU (defined(__GNUC__) && !defined(__clang__))
^
test.cc:5:27: warning: macro expansion producing 'defined' has undefined behavior [-Wexpansion-to-defined]
test.cc:3:40: note: expanded from macro 'HAS_GNU'
#define HAS_GNU (defined(__GNUC__) && !defined(__clang__))
那么,做这件事的“正确”方式是什么?