Created attachment 39666 [details] unreduced testcase Consider the attached unreduced testcase. The number of redundant warnings increases with the -O level due to inlining: markus@x4 /tmp % gcc -O1 -c -Wformat-length algapi.i crypto/algapi.c: In function ‘crypto_inst_setname’: crypto/algapi.c:817:6: warning: output may be truncated at or before format character ‘)’ at offset 5 [-Wformat-length=] crypto/algapi.c:817:6: note: format output between 3 and 66 bytes into a destination of size 64 markus@x4 /tmp % cc -O2 -c -Wformat-length algapi.i crypto/algapi.c: In function ‘crypto_inst_setname’: crypto/algapi.c:817:6: warning: output may be truncated at or before format character ‘)’ at offset 5 [-Wformat-length=] crypto/algapi.c:817:6: note: format output between 3 and 66 bytes into a destination of size 64 crypto/algapi.c: In function ‘crypto_alloc_instance2’: crypto/algapi.c:817:6: warning: output may be truncated at or before format character ‘)’ at offset 5 [-Wformat-length=] crypto/algapi.c:817:6: note: format output between 3 and 66 bytes into a destination of size 64 markus@x4 /tmp % gcc -O3 -c -Wformat-length algapi.i crypto/algapi.c: In function ‘crypto_inst_setname’: crypto/algapi.c:817:6: warning: output may be truncated at or before format character ‘)’ at offset 5 [-Wformat-length=] crypto/algapi.c:817:6: note: format output between 3 and 66 bytes into a destination of size 64 crypto/algapi.c: In function ‘crypto_alloc_instance2’: crypto/algapi.c:817:6: warning: output may be truncated at or before format character ‘)’ at offset 5 [-Wformat-length=] crypto/algapi.c:817:6: note: format output between 3 and 66 bytes into a destination of size 64 crypto/algapi.c: In function ‘crypto_alloc_instance’: crypto/algapi.c:817:6: warning: output may be truncated at or before format character ‘)’ at offset 5 [-Wformat-length=] crypto/algapi.c:817:6: note: format output between 3 and 66 bytes into a destination of size 64
The output with the latest patch (https://gcc.gnu.org/ml/gcc-patches/2016-09/msg01477.html) I see the warnings below, but only with -Wformat-length=2. If I understand correctly, you're wondering if (or implying that) there should be just one warning in each compiler invocation, regardless of the optimization level. It seems reasonable in this case but I confess I don't know if it's possible. The pass relies on optimization for constant propagation and value range propagation, and those in turn depend on inlining. With more function calls inlined it's possible for the same sprintf call to be duplicated in more callers and result in more warnings than otherwise. Maybe there's a way to set TREE_NO_WARNING for the call statement after it has been diagnosed. Let me look into it. (set -x; for o in 0 1 2 3; do /build/gcc-77676/gcc/xgcc -B /build/gcc-77676/gcc -O$o -S -Wformat-length=2 ~/tmp/algapi.i; done) + for o in 0 1 2 3 + /build/gcc-77676/gcc/xgcc -B /build/gcc-77676/gcc -O0 -S -Wformat-length=2 /home/msebor/tmp/algapi.i crypto/algapi.c: In function ‘crypto_inst_setname’: crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-length=] crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64 + for o in 0 1 2 3 + /build/gcc-77676/gcc/xgcc -B /build/gcc-77676/gcc -O1 -S -Wformat-length=2 /home/msebor/tmp/algapi.i crypto/algapi.c: In function ‘crypto_inst_setname’: crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-length=] crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64 + for o in 0 1 2 3 + /build/gcc-77676/gcc/xgcc -B /build/gcc-77676/gcc -O2 -S -Wformat-length=2 /home/msebor/tmp/algapi.i crypto/algapi.c: In function ‘crypto_inst_setname’: crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-length=] crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64 crypto/algapi.c: In function ‘crypto_alloc_instance2’: crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-length=] crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64 + for o in 0 1 2 3 + /build/gcc-77676/gcc/xgcc -B /build/gcc-77676/gcc -O3 -S -Wformat-length=2 /home/msebor/tmp/algapi.i crypto/algapi.c: In function ‘crypto_inst_setname’: crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-length=] crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64 crypto/algapi.c: In function ‘crypto_alloc_instance2’: crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-length=] crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64 crypto/algapi.c: In function ‘crypto_alloc_instance’: crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-length=] crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64
This seems to have been fixed as a result of introducing -Wformat-truncation in r244210. Resolving as a duplicate of 77708. For the record, here's the output at the default setting of the -Wformat-length and -Wformat-truncation options with today's top of trunk: $ (set -x; for o in 0 1 2; do gcc -O$o -S -Wformat -Wformat-length -Wformat-truncation algapi.i; done) + for o in 0 1 2 + /build/gcc-svn/gcc/xgcc -B /build/gcc-svn/gcc -O0 -S -Wformat -Wformat-length -Wformat-truncation algapi.i + for o in 0 1 2 + /build/gcc-svn/gcc/xgcc -B /build/gcc-svn/gcc -O1 -S -Wformat -Wformat-length -Wformat-truncation algapi.i + for o in 0 1 2 + /build/gcc-svn/gcc/xgcc -B /build/gcc-svn/gcc -O2 -S -Wformat -Wformat-length -Wformat-truncation algapi.i And here's the output at level 2: $ (set -x; for o in 0 1 2 3; do gcc -O$o -S -Wformat -Wformat-length=2 -Wformat-truncation=2 algapi.i; done) + for o in 0 1 2 3 + /build/gcc-svn/gcc/xgcc -B /build/gcc-svn/gcc -O0 -S -Wformat -Wformat-length=2 -Wformat-truncation=2 algapi.i crypto/algapi.c: In function ‘crypto_inst_setname’: crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-truncation=] crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64 + for o in 0 1 2 3 + /build/gcc-svn/gcc/xgcc -B /build/gcc-svn/gcc -O1 -S -Wformat -Wformat-length=2 -Wformat-truncation=2 algapi.i crypto/algapi.c: In function ‘crypto_inst_setname’: crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-truncation=] crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64 + for o in 0 1 2 3 + /build/gcc-svn/gcc/xgcc -B /build/gcc-svn/gcc -O2 -S -Wformat -Wformat-length=2 -Wformat-truncation=2 algapi.i crypto/algapi.c: In function ‘crypto_inst_setname’: crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-truncation=] crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64 crypto/algapi.c: In function ‘crypto_alloc_instance2’: crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-truncation=] crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64 + for o in 0 1 2 3 + /build/gcc-svn/gcc/xgcc -B /build/gcc-svn/gcc -O3 -S -Wformat -Wformat-length=2 -Wformat-truncation=2 algapi.i crypto/algapi.c: In function ‘crypto_inst_setname’: crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-truncation=] crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64 crypto/algapi.c: In function ‘crypto_alloc_instance2’: crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-truncation=] crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64 crypto/algapi.c: In function ‘crypto_alloc_instance’: crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-truncation=] crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64 *** This bug has been marked as a duplicate of bug 77708 ***