1. av_printf_format 是一个函数属性宏(format属性),定义在attributes.h文件中。
#if defined(__GNUC__) || defined(__clang__)
# define av_builtin_constant_p __builtin_constant_p
# define av_printf_format(fmtpos, attrpos) __attribute__((__format__(__printf__, fmtpos, attrpos)))
#else
# define av_builtin_constant_p(x) 0
# define av_printf_format(fmtpos, attrpos)
#endif
2. format方法格式:format (archetype, string-index, first-to-check)
format 属性指定一个函数的参数风格与printf/scanf/strfting/strfmon的风格一样,这些参数需要根据格式化字符串进行检测。
- archetype:指定怎样解析函数的格式化字符串,它的值为
printf
,scanf
,strftime
,gnu_printf
,gnu_scanf
,gnu_strftime
orstrfmon
. - string-index:指定函数的第几个参数是格式化字符串。
- first-to-check:指定从函数的第几个参数开始按照格式化串进行类型检测。
3.示例
static av_printf_format(3, 4) void url_add_option(char *buf, int buf_size, const char *fmt, ...)
//url_add_option的格式化字符串的解析按printf的标准进行。
//url_add_option的第3个参数,即fmt,是格式化字符串。
//编译器需要从url_add_option的第四个参数开始按fmt进行类型检测。
4. 参考
Using the GNU Compiler Collection (GCC)
https://gcc.gnu.org/onlinedocs/gcc/index.html#SEC_Contents
6.33.1 Common Function Attributes
https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes