public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* enable -Wformat-truncation with -Og (PR 79691)
@ 2017-02-24  5:07 Martin Sebor
  2017-02-24 10:14 ` Richard Biener
  0 siblings, 1 reply; 5+ messages in thread
From: Martin Sebor @ 2017-02-24  5:07 UTC (permalink / raw)
  To: Gcc Patch List

[-- Attachment #1: Type: text/plain, Size: 355 bytes --]

Bug 79691 - -Wformat-truncation suppressed by (and only by) -Og
points out that the gimple-ssa-sprintf pass doesn't run when
this optimization option is used.  That's because I forgot to
add it to the set of optimization passes that run with that
option.  The attached trivial patch tested on x86_64 corrects
the oversight.

Is this okay for 7.0?

Martin

[-- Attachment #2: gcc-79691.diff --]
[-- Type: text/x-patch, Size: 1678 bytes --]

PR tree-optimization/79691 - -Wformat-truncation suppressed by (and only by) -Og

gcc/ChangeLog:

	PR c/79691
	* passes.def (pass_all_optimizations_g): Enable pass_sprintf_length.

gcc/testsuite/ChangeLog:

	PR c/79691
	* gcc.dg/tree-ssa/pr79691.c: New test.

diff --git a/gcc/passes.def b/gcc/passes.def
index c09ec22..6b0f05b 100644
--- a/gcc/passes.def
+++ b/gcc/passes.def
@@ -364,6 +364,7 @@ along with GCC; see the file COPYING3.  If not see
       NEXT_PASS (pass_object_sizes);
       /* Fold remaining builtins.  */
       NEXT_PASS (pass_fold_builtins);
+      NEXT_PASS (pass_sprintf_length, true);
       /* Copy propagation also copy-propagates constants, this is necessary
          to forward object-size and builtin folding results properly.  */
       NEXT_PASS (pass_copy_prop);
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr79691.c b/gcc/testsuite/gcc.dg/tree-ssa/pr79691.c
new file mode 100644
index 0000000..badbfcd
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr79691.c
@@ -0,0 +1,27 @@
+/* PR tree-optimization/79691 - -Wformat-truncation suppressed by
+   (and only by) -Og
+
+   { dg-compile }
+   { dg-options "-Og -Wall" } */
+
+char d[2];
+
+/* Verify -Wformat-overflow works.  */
+void f (void)
+{
+  __builtin_sprintf (d, "%i", 123);   /* { dg-warning "directive writing 3 bytes" } */
+}
+
+/* Verify -Wformat-truncation works.  */
+void g (void)
+{
+  __builtin_snprintf (d, sizeof d, "%i", 1234);   /* { dg-warning "output truncated writing 4 bytes" } */
+}
+
+/* Verify -fprintf-return-value works.  */
+int h (void)
+{
+  return __builtin_snprintf (0, 0, "%i", 12345);
+}
+
+/* { dg-final { scan-tree-dump-not "snprintf" "optimized" } } */

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: enable -Wformat-truncation with -Og (PR 79691)
  2017-02-24  5:07 enable -Wformat-truncation with -Og (PR 79691) Martin Sebor
@ 2017-02-24 10:14 ` Richard Biener
  2017-02-24 10:22   ` Jakub Jelinek
  2017-02-24 18:20   ` Martin Sebor
  0 siblings, 2 replies; 5+ messages in thread
From: Richard Biener @ 2017-02-24 10:14 UTC (permalink / raw)
  To: Martin Sebor; +Cc: Gcc Patch List

On Fri, Feb 24, 2017 at 1:35 AM, Martin Sebor <msebor@gmail.com> wrote:
> Bug 79691 - -Wformat-truncation suppressed by (and only by) -Og
> points out that the gimple-ssa-sprintf pass doesn't run when
> this optimization option is used.  That's because I forgot to
> add it to the set of optimization passes that run with that
> option.  The attached trivial patch tested on x86_64 corrects
> the oversight.
>
> Is this okay for 7.0?

Any reason for the placement before copy-prop?  I'd have done it
after pass_late_warn_uninitialized for example.

Also doesn't pass_sprintf_length rely on get_range_info ()?  With -Og
nothing populates those so you'll always get effectively VARYING ranges.

Richard.

> Martin

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: enable -Wformat-truncation with -Og (PR 79691)
  2017-02-24 10:14 ` Richard Biener
@ 2017-02-24 10:22   ` Jakub Jelinek
  2017-02-24 18:20   ` Martin Sebor
  1 sibling, 0 replies; 5+ messages in thread
From: Jakub Jelinek @ 2017-02-24 10:22 UTC (permalink / raw)
  To: Richard Biener; +Cc: Martin Sebor, Gcc Patch List

On Fri, Feb 24, 2017 at 11:10:51AM +0100, Richard Biener wrote:
> On Fri, Feb 24, 2017 at 1:35 AM, Martin Sebor <msebor@gmail.com> wrote:
> > Bug 79691 - -Wformat-truncation suppressed by (and only by) -Og
> > points out that the gimple-ssa-sprintf pass doesn't run when
> > this optimization option is used.  That's because I forgot to
> > add it to the set of optimization passes that run with that
> > option.  The attached trivial patch tested on x86_64 corrects
> > the oversight.
> >
> > Is this okay for 7.0?
> 
> Any reason for the placement before copy-prop?  I'd have done it
> after pass_late_warn_uninitialized for example.
> 
> Also doesn't pass_sprintf_length rely on get_range_info ()?  With -Og
> nothing populates those so you'll always get effectively VARYING ranges.

-O0 and -O1 are the same case with that though.

	Jakub

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: enable -Wformat-truncation with -Og (PR 79691)
  2017-02-24 10:14 ` Richard Biener
  2017-02-24 10:22   ` Jakub Jelinek
@ 2017-02-24 18:20   ` Martin Sebor
  2017-02-27  8:49     ` Richard Biener
  1 sibling, 1 reply; 5+ messages in thread
From: Martin Sebor @ 2017-02-24 18:20 UTC (permalink / raw)
  To: Richard Biener; +Cc: Gcc Patch List

On 02/24/2017 03:10 AM, Richard Biener wrote:
> On Fri, Feb 24, 2017 at 1:35 AM, Martin Sebor <msebor@gmail.com> wrote:
>> Bug 79691 - -Wformat-truncation suppressed by (and only by) -Og
>> points out that the gimple-ssa-sprintf pass doesn't run when
>> this optimization option is used.  That's because I forgot to
>> add it to the set of optimization passes that run with that
>> option.  The attached trivial patch tested on x86_64 corrects
>> the oversight.
>>
>> Is this okay for 7.0?
>
> Any reason for the placement before copy-prop?  I'd have done it
> after pass_late_warn_uninitialized for example.

I wanted to make sure that folded sprintf return values would be
eligible for further copy propagation.  E.g., that a + b would
be folded into a constant:

   int foo (void)
   {
     int a = snprintf (0, 0, "%i", 123);
     int b = snprintf (0, 0, "%i", 1234);
     return a + b;
   }

But I could have easily missed some important use case where this
placement will compromise the warning.  I don't have any tests
for this one way or the other so I'm happy to go with your
recommendation.  Let me know which you think is more appropriate
(if you have a suggestion for a test case I'd be grateful).

>
> Also doesn't pass_sprintf_length rely on get_range_info ()?  With -Og
> nothing populates those so you'll always get effectively VARYING ranges.

It does when it's available but as Jakub noted, it works without
it as well (at -O0).

Thanks
Martin

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: enable -Wformat-truncation with -Og (PR 79691)
  2017-02-24 18:20   ` Martin Sebor
@ 2017-02-27  8:49     ` Richard Biener
  0 siblings, 0 replies; 5+ messages in thread
From: Richard Biener @ 2017-02-27  8:49 UTC (permalink / raw)
  To: Martin Sebor; +Cc: Gcc Patch List

On Fri, Feb 24, 2017 at 6:51 PM, Martin Sebor <msebor@gmail.com> wrote:
> On 02/24/2017 03:10 AM, Richard Biener wrote:
>>
>> On Fri, Feb 24, 2017 at 1:35 AM, Martin Sebor <msebor@gmail.com> wrote:
>>>
>>> Bug 79691 - -Wformat-truncation suppressed by (and only by) -Og
>>> points out that the gimple-ssa-sprintf pass doesn't run when
>>> this optimization option is used.  That's because I forgot to
>>> add it to the set of optimization passes that run with that
>>> option.  The attached trivial patch tested on x86_64 corrects
>>> the oversight.
>>>
>>> Is this okay for 7.0?
>>
>>
>> Any reason for the placement before copy-prop?  I'd have done it
>> after pass_late_warn_uninitialized for example.
>
>
> I wanted to make sure that folded sprintf return values would be
> eligible for further copy propagation.  E.g., that a + b would
> be folded into a constant:
>
>   int foo (void)
>   {
>     int a = snprintf (0, 0, "%i", 123);
>     int b = snprintf (0, 0, "%i", 1234);
>     return a + b;
>   }
>
> But I could have easily missed some important use case where this
> placement will compromise the warning.  I don't have any tests
> for this one way or the other so I'm happy to go with your
> recommendation.  Let me know which you think is more appropriate
> (if you have a suggestion for a test case I'd be grateful).

Hmm, I see.  Note that I'd have expected this kind of constant folding
to happen in gimple-fold.c, but well.

>>
>> Also doesn't pass_sprintf_length rely on get_range_info ()?  With -Og
>> nothing populates those so you'll always get effectively VARYING ranges.
>
>
> It does when it's available but as Jakub noted, it works without
> it as well (at -O0).

Good.

Your original patch is ok.

Thanks,
Richard.

> Thanks
> Martin

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-02-27  8:47 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-02-24  5:07 enable -Wformat-truncation with -Og (PR 79691) Martin Sebor
2017-02-24 10:14 ` Richard Biener
2017-02-24 10:22   ` Jakub Jelinek
2017-02-24 18:20   ` Martin Sebor
2017-02-27  8:49     ` Richard Biener

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).