From 965779b4ad0f0abbdc8ab0addd2fae14165a08f4 Mon Sep 17 00:00:00 2001 From: Aldy Hernandez Date: Fri, 22 Feb 2019 13:46:01 +0000 Subject: [PATCH] re PR middle-end/85598 (Incorrect -Wformat-truncation in a loop only at -O2 and -O3) PR middle-end/85598 * gimple-ssa-sprintf.c (pass_sprintf_length::execute): Enable loop analysis for pass. From-SVN: r269115 --- gcc/ChangeLog | 6 ++++++ gcc/gimple-ssa-sprintf.c | 14 ++++++++++++++ gcc/testsuite/gcc.dg/pr85598.c | 16 ++++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/pr85598.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index c4d16b9b70a..9d79670582d 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2019-02-22 Aldy Hernandez + + PR middle-end/85598 + * gimple-ssa-sprintf.c (pass_sprintf_length::execute): Enable loop + analysis for pass. + 2019-02-22 Thiago Macieira PR target/89444 diff --git a/gcc/gimple-ssa-sprintf.c b/gcc/gimple-ssa-sprintf.c index 8e6016fc42f..4fe666ff0de 100644 --- a/gcc/gimple-ssa-sprintf.c +++ b/gcc/gimple-ssa-sprintf.c @@ -65,6 +65,8 @@ along with GCC; see the file COPYING3. If not see #include "tree-ssa-propagate.h" #include "calls.h" #include "cfgloop.h" +#include "tree-scalar-evolution.h" +#include "tree-ssa-loop.h" #include "intl.h" #include "langhooks.h" @@ -4200,10 +4202,22 @@ pass_sprintf_length::execute (function *fun) init_target_to_host_charmap (); calculate_dominance_info (CDI_DOMINATORS); + bool use_scev = optimize > 0 && flag_printf_return_value; + if (use_scev) + { + loop_optimizer_init (LOOPS_NORMAL); + scev_initialize (); + } sprintf_dom_walker sprintf_dom_walker; sprintf_dom_walker.walk (ENTRY_BLOCK_PTR_FOR_FN (fun)); + if (use_scev) + { + scev_finalize (); + loop_optimizer_finalize (); + } + /* Clean up object size info. */ fini_object_sizes (); return 0; diff --git a/gcc/testsuite/gcc.dg/pr85598.c b/gcc/testsuite/gcc.dg/pr85598.c new file mode 100644 index 00000000000..c84b63f8084 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr85598.c @@ -0,0 +1,16 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -Wall" } */ + +typedef __SIZE_TYPE__ size_t; +extern void __chk_fail (void); +extern int snprintf (char *, size_t, const char *, ...); + +int main() +{ + char temp[100]; + unsigned int x; + char *str = temp; + for(x=0; x<256; ++x) { + snprintf(str, 4, "%%%02X", x); + } +} -- 2.30.2