使用 "bitwise and" 时 C 预处理器编译异常

C preprocessor compiling anomaly when using "bitwise and"

为了调试目的,我在头文件中有以下程序代码。我的意图是在编译期间决定哪些消息应该打印到控制台端口,或者如果程序不是在调试模式下编译则根本不打印它们。

#define ALL 0xffffffff    
#define ENABLED  (ERROR | WARNING | INFO)    //Enabled error messages which needs to be printed out.

//Error msg types
#define ERROR         0x1ul
#define WARNING       0x2ul
#define INFO          0x4ul
#define DBG           0x10ul
#define ENTER         0x20ul

#define STORAGE            0x40ul
#define BOND_MANAGER       0x80ul
#define ANCS_EVENT         0x100ul
#define NOTIF_EVENT        0x200ul
#define BLE_EVENT          0x400ul
#define ANCS_APP           0x800ul
#define BLE                0x1000ul
#define PERIPHERALS        0x2000ul
#define PWM                0x4000ul
#define LEDS               0x8000ul

#define DebugMsgStr(a,b) do { \
        if (a & ENABLED) \
            simple_uart_putstring(b); \     //print to console port
    }while(0)

#define DebugMsg(a,...) do { \
        if (a & ENABLED) {\
            uint8_t data[100];          \
            snprintf(data,100,"%x - %x - %x",a,ENABLED,a&ENABLED);  \  //These two lines only used for debugging the debug_printf
            simple_uart_putstring(data); \                             //These two lines only used for debugging the debug_printf
            snprintf(data,100,__VA_ARGS__);  \
            simple_uart_putstring(data); }\     //print to console port
    }while(0)

我在代码中打印消息:

DebugMsg(ERROR| BLE ,"[ERROR-BLE]This should be seen",..);
DebugMsg(DBG | BLE ,"[DBG-BLE]This should not been logged",..);

仅当启用 DBG 级别或模块 BLE 调试时才应记录最后一条消息。尽管当前未启用 DBG 级别,但我看到以下日志:

8010 - 7 - 8000[LEDS-DBG] effect_counter:2
1010 - 7 - 10[BLE-DBG] Timer Tick: 0x0000745d

所以

0x8010 & 0x7 == 0x8000.    
0x1010 & 0x7 == 0x10

怎么会这样?我正在使用 arm-gcc 编译为 cortex-m0 nrf51822 Nordic 芯片。 还有一个 dissambly 片段,但没有看到任何特别的东西,只是 if(a&ENABLED) 根本没有编译。

DebugMsg(DBG | BLE,"[BLE-DBG] Timer Tick: 0x%08x\r\n",currentTime);

R7 --> this probably contains a pointer to currentTime variable

0001587a:   adds r1, r7, #0           -->r1 = r7
0001587c:   adds r1, #12               -->r1 = r1+12

0001587e:   ldr r2, [pc, #248]      ; (0x15978 <ble_ancs_c_connection_moretriage_timer+272>)
00015880:   ldr r3, [pc, #248]      ; (0x1597c <ble_ancs_c_connection_moretriage_timer+276>)

00015882:   movs r0, #7                --> r0 -ba 0x7
00015884:   str r0, [sp, #0]           --> save to stack 

00015886:   movs r0, #16               -->r0 ->ba 0x10
00015888:   str r0, [sp, #4]           --> save to stack

0001588a:   adds r0, r1, #0            --> r0 = r1
0001588c:   movs r1, #100   ; 0x64     --> r1 = 100

0001588e:   bl 0x22c74 <snprintf>
00015892:   adds r3, r7, #0
00015894:   adds r3, #12
00015896:   adds r0, r3, #0
00015898:   bl 0x21a08 <simple_uart_putstring>
0001589c:   ldr r3, [r7, #112]      ; 0x70
0001589e:   adds r1, r7, #0
000158a0:   adds r1, #12
000158a2:   ldr r2, [pc, #220]      ; (0x15980 <ble_ancs_c_connection_moretriage_timer+280>)
000158a4:   adds r0, r1, #0
000158a6:   movs r1, #100   ; 0x64
000158a8:   bl 0x22c74 <snprintf>
000158ac:   adds r3, r7, #0
000158ae:   adds r3, #12
000158b0:   adds r0, r3, #0
000158b2:   bl 0x21a08 <simple_uart_putstring>

提前致谢。

DebugMsg(DBG | BLE ,"[ERROR-BLE]This should be seen",..) 扩展为

if (DBG | BLE & ENABLED) ...

变成

if (0x10ul | 0x1000ul & 0x07)

但是 & | 所以这被解析为

if (0x10 | (0x1000 & 0x07))

永远正确。

您应该将 marco 更改为

#define DebugMsgStr(a,b) do { \
    if ((a) & ENABLED) \