gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:25:22 24 4
gpt4 key购买 nike

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

#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)

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

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

这总是正确的。

您应该将宏更改为

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

关于C预处理器编译异常时使用 "bitwise and",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27760283/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com