Eregrith
Eregrith

Reputation: 4366

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 ?

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 :

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 ?

Upvotes: 4

Views: 3711

Answers (1)

Dirk
Dirk

Reputation: 31061

It seems, that the support for printf style strings is hard-coded into the compiler. In particular, the compiler has a special format function attribute which can be used to annotate printf style functions.

Upvotes: 1

Related Questions