c/c++ # and ## in macros以及宏debug

可变参数宏

  1999年的ISO C标准里规定了可变参数宏,语法和函数类似,比如:

  #define debug(format, ...) fprintf (stderr, format, __VA_ARGS__) 

  其中的"…"表示可变参数,实际调用时,它们会替代宏体里的__VA_ARGS__。GCC支持更复杂的形式,可以给可变参数取个名字,如下所示。

  #define debug(format, args...) fprintf (stderr, format, args) 

  有了名字之后,代码显得更具有可读性。内核中的例子为:

  ++++ include/linux/kernel.h  

  244 #define pr_info(fmt,arg...) \  

  245     printk(KERN_INFO fmt,##arg) 

  其中的pr_info和上面的debug形式除了"##"外,几近相同。"##"主要针对参数为空的情况。既然称为可变参数,那传递空参数也是可以的。如果没有使用"##",传递空参数时,比如:

  debug ("A message"); 

  宏展开后,其中的字符串后面会多个多余的逗号,而"##"则会使预处理器去掉这个多余的逗号。

  扩展宏定义中, "#" 和 "##"的含义。如下:

https://stackoverflow.com/questions/4364971/and-in-macros

https://stackoverflow.com/questions/1644868/define-macro-for-debug-printing-in-c

#define INT64CONST(x)  (x##L)
#define UINT64CONST(x) (x##UL)
#define LOCAL_FCINFO(name, nargs) \
/* use union with FunctionCallInfoBaseData to guarantee alignment */ \
union \
{ \
FunctionCallInfoBaseData fcinfo; \
/* ensure enough space for nargs args is available */ \
char fcinfo_data[SizeForFunctionCallInfo(nargs)]; \
} name##data; \
FunctionCallInfo name = &name##data.fcinfo

#define forfour(cell1, list1, cell2, list2, cell3, list3, cell4, list4) \
for (ForFourState cell1##__state = {(list1), (list2), (list3), (list4), 0}; \
multi_for_advance_cell(cell1, cell1##__state, l1, i), \
multi_for_advance_cell(cell2, cell1##__state, l2, i), \
multi_for_advance_cell(cell3, cell1##__state, l3, i), \
multi_for_advance_cell(cell4, cell1##__state, l4, i), \
(cell1 != NULL && cell2 != NULL && cell3 != NULL && cell4 != NULL); \
cell1##__state.i++)

#define makeNode(_type_)   ((_type_ *) newNode(sizeof(_type_),T_##_type_))

"#"  代表和一个字符串相连接

When a macro parameter is used with a leading ‘#’, the preprocessor replaces it with the literal text of the actual argument, converted to a string constant.

 "##"  代表和一个符号相连接,符号可以是变量,或另一个宏符号。

 

 就相当于如下:

for (ForFourState lct__state = {(list1), (list2), (list3), (list4), 0}; 
         multi_for_advance_cell(lct, lct__state, l1, i), \
         multi_for_advance_cell(cell2, lct__state, l2, i), \
         multi_for_advance_cell(cell3, lct__state, l3, i), \
         multi_for_advance_cell(cell4, lct__state, l4, i), \
         (cell1 != NULL && cell2 != NULL && cell3 != NULL && cell4 != NULL); 
         lct__state.i++)
#define multi_for_advance_cell(cell, state, l, i) \
(cell = (state.l != NIL && state.i < state.l->length) ? \
&state.l->elements[state.i] : NULL)

宏debug

  gcc本身是支持宏debug的,机制类似内联函数。

  gcc -gdwarf-2 -g3 test.c

  -g3级别支持debuginfo中包含可被gdb识别的宏信息,参见https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/pdf/developer_guide/red_hat_enterprise_linux-7-developer_guide-en-us.pdf

https://developer.apple.com/library/archive/documentation/DeveloperTools/gdb/gdb/gdb_10.html

  展开宏定义:

  (gdb) macro exp MACRO3(0)

  expands to: (((++(0))+100)+200)

  打印宏定义:

  (gdb) p CLIENT6_BEGIN_FLG

  $1 = "<Client6>"

  C和C++编译器是集成的,编译一般分为四个步骤:

  1. 预处理(preprocessing)  ----------------- cpp/ gcc -E 
  2. 编译(compilation) ------------------ cc1 / gcc -S
  3. 汇编(assembly)  -------------------- as
  4. 连接(linking) --------------------- ld 

 

 但是通过clion没有debug进去,应该是没有专门到-E阶段,vs的优势是支持debug到宏内。

posted @ 2021-12-04 20:39  zhjh256  阅读(137)  评论(0编辑  收藏  举报