如何处理 clang 的 (3.9) -Wexpansion-to-defined 警告?

How to deal with clang's (3.9) -Wexpansion-to-defined warning?

clang 3.9 已添加到 -Wall 警告 -Wexpansion-to-defined,它会产生

macro expansion producing 'defined' has undefined behaviour

如果 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__))

那么 'correct' 方法是什么?

您可以使用#if - #else 宏:

#if defined(__GNUC__) && !defined(__clang__)
#define HAS_GNU 1
#else
#define HAS_GNU 0
#endif

或者,如果您愿意更改使用 HAS_GNU 的代码,也许更传统的方式:

#if defined(__GNUC__) && !defined(__clang__)
#define HAS_GNU
#endif

#if defined(__clang__) || defined(HAS_GNU)

如果您在使用 3d 派对 pod 时遇到此类问题,您可能会发现这很有用

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wexpansion-to-defined"
#import <pop/POP.h>
#pragma clang diagnostic pop