Clang does not throw -Wformat errors

Considering a small .c program:

#include <stdio.h>

int main(void) {
    printf("%hd\n", 1.0f);
    return 0;
}

I would expect to see some kind of warning or error thrown due to the incorrect string formatting (%hd instead of %f).

If I enter into an environment with llvm_14 via:

with import <nixpkgs> {};
mkShell.override { stdenv = llvmPackages_14.stdenv; } {
    buildInputs = [
    ];
    shellHook = ''
    '';
}
$ nix-shell

Compiling this program with clang (on linux) yields no warnings or errors:

$ which clang
/nix/store/qfa5jcmmz8iv43x72z3vlvyxj3l08f98-clang-wrapper-14.0.0-rc4/bin/clang
$ clang -O1 -Werror -Wall -Wformat "-std=c11" -o "main_c" "main.c"

Yet, compiling with clang++ does produce an error:

$ which clang++
/nix/store/qfa5jcmmz8iv43x72z3vlvyxj3l08f98-clang-wrapper-14.0.0-rc4/bin/clang++
$ cp main.c main.cpp
$ clang++ -O1 -Werror -Wall -Wformat "-std=c++11" -o "main_cpp" "main.cpp"
main.cpp:4:21: error: format specifies type 'short' but the argument has type 'float' [-Werror,-Wformat]
    printf("%hd\n", 1.0f);
            ~~~     ^~~~
            %f
1 error generated.

When attempting the same comparison with a locally installed version of clang (not via nix), clang succeeded in producing an error here. Is this a bug in the nix packages version of clang? I had originally noticed this behavior 2-3 years ago, chalked it up as a bug and figured it would be resolved down the line. Anyone have any insight what’s going on here?

(I am new to the nix Discourse; not 100% sure I have the correct channel for this inquiry, please advise if this should be relocated.)

1 Like