C预处理器在使用"按位和"时编译异常。

时间:2015-01-03 22:36:58

标签: c c-preprocessor 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北欧芯片。 还有一个dissasambly片段,但没有看到任何特殊的,只是(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>

提前致谢。

1 个答案:

答案 0 :(得分:4)

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

if (DBG | BLE & ENABLED) ...

成为

if (0x10ul | 0x1000ul & 0x07)

但是& has higher precedence than |所以这被解析为

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

总是如此。

您应该将marco更改为

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