https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105080
Richard Biener <rguenth at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |jakub at gcc dot gnu.org, | |rguenth at gcc dot gnu.org --- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> --- (In reply to Richard Biener from comment #1) > Confirmed. The issue is that at -O0 we do not use SCEV and thus range > analysis is limited, just using i < 16. > > static unsigned int > printf_strlen_execute (function *fun, bool warn_only) > { > strlen_optimize = !warn_only; > > calculate_dominance_info (CDI_DOMINATORS); > > bool use_scev = optimize > 0 && flag_printf_return_value; > if (use_scev) > { > loop_optimizer_init (LOOPS_NORMAL); > scev_initialize (); > } > > at -O0 we call this when warn_format_overflow > 0 || warn_format_trunc > 0 so > to improve we could maybe do || warn_only above. That would fix the bogus > diagnostic. Or rather always do this, even the late pass as otherwise I see a bogus diagnostic with -fno-printf-return-value even when optimizing: > ./cc1 -quiet t.c -Wall -O -fno-printf-return-value t.c:1:6: warning: return type of 'main' is not 'int' [-Wmain] 1 | void main(void) | ^~~~ t.c: In function 'main': t.c:7:55: warning: '%d' directive output may be truncated writing between 1 and 11 bytes into a region of size 3 [-Wformat-truncation=] 7 | __builtin_snprintf(foo, sizeof(foo), "%d", i); | ^~ t.c:7:54: note: directive argument in the range [-2147483647, 2147483647] 7 | __builtin_snprintf(foo, sizeof(foo), "%d", i); | ^~~~ t.c:7:17: note: '__builtin_snprintf' output between 2 and 12 bytes into a destination of size 3 7 | __builtin_snprintf(foo, sizeof(foo), "%d", i); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~