在这个可变参数宏中,“va_num”是什么意思?

时间:2017-06-26 06:39:16

标签: c gcc visual-c++ c99

似乎不同的编译器处理带有可变参数的宏的方式略有不同。

对于与C99兼容的编译器,使用特殊标识符#define MACRO_1(param1, ...) func(param1, __VA_ARGS__) 来表示可变参数列表:

#define MACRO_2(param1, args...) func(param1, args) // "args..." is the name

对于GNU C编译器,可以为可变参数列表指定名称:

#define uefi_call_wrapper(func, va_num, ...) func(__VA_ARGS__)

但我在ACPICA代码中看到以下定义:

uefi_call_wrapper(BS->SetWatchdogTimer, 4, 0, 0x0, 0, NULL);

对这样的宏的调用是这样的:

va_num

看起来像C99方法,但#ifdef USE_EFI_FUNCTION_WRAPPER #define __VA_NARG__(...) \ __VA_NARG_(_0, ## __VA_ARGS__, __RSEQ_N()) #define __VA_NARG_(...) \ __VA_ARG_N(__VA_ARGS__) #define __VA_ARG_N( \ _0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,N,...) N #define __RSEQ_N() \ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 #define __VA_ARG_NSUFFIX__(prefix,...) \ __VA_ARG_NSUFFIX_N(prefix, __VA_NARG__(__VA_ARGS__)) #define __VA_ARG_NSUFFIX_N(prefix,nargs) \ __VA_ARG_NSUFFIX_N_(prefix, nargs) #define __VA_ARG_NSUFFIX_N_(prefix,nargs) \ prefix ## nargs /* Prototypes of EFI cdecl -> stdcall trampolines */ UINT64 efi_call0(void *func); UINT64 efi_call1(void *func, UINT64 arg1); UINT64 efi_call2(void *func, UINT64 arg1, UINT64 arg2); UINT64 efi_call3(void *func, UINT64 arg1, UINT64 arg2, UINT64 arg3); UINT64 efi_call4(void *func, UINT64 arg1, UINT64 arg2, UINT64 arg3, UINT64 arg4); UINT64 efi_call5(void *func, UINT64 arg1, UINT64 arg2, UINT64 arg3, UINT64 arg4, UINT64 arg5); UINT64 efi_call6(void *func, UINT64 arg1, UINT64 arg2, UINT64 arg3, UINT64 arg4, UINT64 arg5, UINT64 arg6); UINT64 efi_call7(void *func, UINT64 arg1, UINT64 arg2, UINT64 arg3, UINT64 arg4, UINT64 arg5, UINT64 arg6, UINT64 arg7); UINT64 efi_call8(void *func, UINT64 arg1, UINT64 arg2, UINT64 arg3, UINT64 arg4, UINT64 arg5, UINT64 arg6, UINT64 arg7, UINT64 arg8); UINT64 efi_call9(void *func, UINT64 arg1, UINT64 arg2, UINT64 arg3, UINT64 arg4, UINT64 arg5, UINT64 arg6, UINT64 arg7, UINT64 arg8, UINT64 arg9); UINT64 efi_call10(void *func, UINT64 arg1, UINT64 arg2, UINT64 arg3, UINT64 arg4, UINT64 arg5, UINT64 arg6, UINT64 arg7, UINT64 arg8, UINT64 arg9, UINT64 arg10); /* Front-ends to efi_callX to avoid compiler warnings */ #define _cast64_efi_call0(f) \ efi_call0(f) #define _cast64_efi_call1(f,a1) \ efi_call1(f, (UINT64)(a1)) #define _cast64_efi_call2(f,a1,a2) \ efi_call2(f, (UINT64)(a1), (UINT64)(a2)) #define _cast64_efi_call3(f,a1,a2,a3) \ efi_call3(f, (UINT64)(a1), (UINT64)(a2), (UINT64)(a3)) #define _cast64_efi_call4(f,a1,a2,a3,a4) \ efi_call4(f, (UINT64)(a1), (UINT64)(a2), (UINT64)(a3), (UINT64)(a4)) #define _cast64_efi_call5(f,a1,a2,a3,a4,a5) \ efi_call5(f, (UINT64)(a1), (UINT64)(a2), (UINT64)(a3), (UINT64)(a4), \ (UINT64)(a5)) #define _cast64_efi_call6(f,a1,a2,a3,a4,a5,a6) \ efi_call6(f, (UINT64)(a1), (UINT64)(a2), (UINT64)(a3), (UINT64)(a4), \ (UINT64)(a5), (UINT64)(a6)) #define _cast64_efi_call7(f,a1,a2,a3,a4,a5,a6,a7) \ efi_call7(f, (UINT64)(a1), (UINT64)(a2), (UINT64)(a3), (UINT64)(a4), \ (UINT64)(a5), (UINT64)(a6), (UINT64)(a7)) #define _cast64_efi_call8(f,a1,a2,a3,a4,a5,a6,a7,a8) \ efi_call8(f, (UINT64)(a1), (UINT64)(a2), (UINT64)(a3), (UINT64)(a4), \ (UINT64)(a5), (UINT64)(a6), (UINT64)(a7), (UINT64)(a8)) #define _cast64_efi_call9(f,a1,a2,a3,a4,a5,a6,a7,a8,a9) \ efi_call9(f, (UINT64)(a1), (UINT64)(a2), (UINT64)(a3), (UINT64)(a4), \ (UINT64)(a5), (UINT64)(a6), (UINT64)(a7), (UINT64)(a8), \ (UINT64)(a9)) #define _cast64_efi_call10(f,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10) \ efi_call10(f, (UINT64)(a1), (UINT64)(a2), (UINT64)(a3), (UINT64)(a4), \ (UINT64)(a5), (UINT64)(a6), (UINT64)(a7), (UINT64)(a8), \ (UINT64)(a9), (UINT64)(a10)) /* main wrapper (va_num ignored) */ #define uefi_call_wrapper(func,va_num,...) \ __VA_ARG_NSUFFIX__(_cast64_efi_call, __VA_ARGS__) (func , ##__VA_ARGS__) #else #define uefi_call_wrapper(func, va_num, ...) func(__VA_ARGS__) #endif 似乎毫无用处。为什么要定义?

ADD 1 - 宏的完整定义

{{1}}

1 个答案:

答案 0 :(得分:4)

va_num只是传递给函数的普通参数,宏只是忽略它。

用法1:

当在不同的编译器设置中使用宏时,通常会使用这种用法,以便在所有这些设置中调用相同的函数,但映射到不同的func,这可能(或可能不)支持va_num参数。在这种特殊情况下,func可能不支持va_num参数,因此宏只会忽略它。

用法2:

也可用于处理旧代码,使其可以轻松移植到新版本的宏。

回答您的问题,va_num参数不是特殊参数。它与任何其他参数一样使用,与__VA_ARGS__或类似物无关。宏的编写者决定将该名称赋予该参数,只是为了声明它应该包含传递的参数的数量,但没有更多的发烧友和编译器被激活。