cgccclangprintf

How to force gcc to do printf checks on non-literal format strings?


const char* const non_literal_string = "Hello %d";

void my_print()
{
    /* 
     * I would like GCC to throw a warning when compiling this line because the
     * argument is not of int type.
     */
    printf(non_literal_string, "World!");
    
    //Like it does here
    printf("Hello %d", "World!");
}

Above I have declared non_literal_string as const char* const so neither the pointer nor the pointed string can change, so there are the conditions for which GCC can check that the supplied arguments have types appropriate to the specified format string.

With clang compiler these checks are performed even on non-literal format strings, this is the output if I try to compile this code with clang:

<source>:11:32: warning: format specifies type 'int' but the argument has type 'char *' [-Wformat]
   11 |     printf(non_literal_string, "World!");
      |            ~~~~~~~~~~~~~~~~~~  ^~~~~~~~
<source>:3:47: note: format string is defined here
    3 | const char* const non_literal_string = "Hello %d";
      |                                               ^~
      |                                               %s
<source>:14:24: warning: format specifies type 'int' but the argument has type 'char *' [-Wformat]
   14 |     printf("Hello %d", "World!");
      |                   ~~   ^~~~~~~~
      |                   %s

Solution

  • How to force gcc to do printf checks on non-literal format strings?

    A program does not do something that you would want the program to do. The only possible way to "force" gcc to do anything is to change it. You can modify gcc source code to do the check and implement it. You could post it as a bug or feature request to gcc bugzilla and also fund gcc development https://gcc.gnu.org/wiki/GNUToolchainFund .

    The feature is just not implemented, it's not there. There is no "forcing".