简体   繁体   English

自定义警告为printf的“格式参数太多”

[英]Custom warning as printf's “too many arguments for format”

Is there a way to tell gcc to check argument number in a function based on its parameters, similarly to what it does when giving you a "too many arguments for format" when using printf ? 有没有办法告诉gcc根据其参数检查函数中的参数号,类似于在使用printf时给出“格式参数太多”时的作用?

For example can we have a stdarg-using method with some mandatory argument that would give info on its currently required argument number and have gcc tell us when it's wrong : 例如,我们可以使用stdarg-using方法,其中包含一些必需的参数,这些参数可以提供有关其当前所需参数编号的信息,并让gcc告诉我们何时出错:

calculate_mean(4, //Number of arguments
               3, 2, 1, 10); //actual parameters

calculate_mean(5,            //Custom warning from gcc because
               1, 5, 10, 2); //there is one missing argument

Giving : 给予:

# gcc -c file.c -o file.o
gcc: Warning: calculate_mean: Too few arguments (5 required, 4 given)

Can it be done from inside the code, from a config file, or is it hardcoded inside the compiler and is not at all possible ? 它可以从代码内部,从配置文件中完成,还是在编译器内硬编码而不是完全可能的?

It seems, that the support for printf style strings is hard-coded into the compiler. 看来,对printf样式字符串的支持是硬编码到编译器中的。 In particular, the compiler has a special format function attribute which can be used to annotate printf style functions. 特别是,编译器具有特殊的format函数属性 ,可用于注释printf样式函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM