diff mbox

Provide inlining context in strict-overflow warnings

Message ID 5373590A.2020605@redhat.com
State New
Headers show

Commit Message

Florian Weimer May 14, 2014, 11:52 a.m. UTC
On 05/14/2014 11:56 AM, Jakub Jelinek wrote:
> On Tue, May 13, 2014 at 09:27:08PM +0200, Florian Weimer wrote:
>> Patterns that trigger the optimization and warning can form after
>> inlining, and it can be rather difficult to figure out what exactly
>> is causing the warning.  The inlining context at least provides
>> additional hints, enabling developers to substitute the arguments
>> and discover what, precisely, is happening.
>>
>> More context is provided with -g than without, but I think this is
>> acceptable.
>>
>> I bootstrapped and tested the attached patch on
>> x86_64-redhat-linux-gnu, with no new regressions.
>
> This looks wrong.  If you want to print inline context, you just should
> use %K%s and pass a tree with the right EXPR_LOCATION and TREE_BLOCK.

Oh, I didn't realize that %K prints the context recursively.  Nice.

> So perhaps:
>    if (stmt == NULL)
>      warning_at (input_location, OPT_Wstrict_overflow, "%s", warnmsg);
>    else
>      warning_at (gimple_location (stmt), OPT_Wstrict_overflow, "%K%s",
> 		build_empty_stmt (gimple_location (stmt)), warnmsg);
> (or add something similar to %K that will take location_t or change
> %K to take location_t instead of tree now that we have both a block and
> locus in location_t.

I looked at the existing %K users and all but one use expressions, so 
the build_empty_stmt route seems to be most reasonable approach.  Thanks 
for the suggestion, new patch attached.
diff mbox

Patch

gcc/

2014-05-13  Florian Weimer  <fweimer@redhat.com>

	* fold-const.c (fold_undefer_overflow_warnings): Print inlining
	information.

gcc/testsuite/

2014-05-13  Florian Weimer  <fweimer@redhat.com>

	* c-c++-common/Wstrict-overflow-1.c: New test.

diff --git a/gcc/fold-const.c b/gcc/fold-const.c
index 0fcb87f..33fb629 100644
--- a/gcc/fold-const.c
+++ b/gcc/fold-const.c
@@ -233,7 +233,6 @@  void
 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
 {
   const char *warnmsg;
-  location_t locus;
 
   gcc_assert (fold_deferring_overflow_warnings > 0);
   --fold_deferring_overflow_warnings;
@@ -264,10 +263,10 @@  fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
     return;
 
   if (stmt == NULL)
-    locus = input_location;
+    warning_at (input_location, OPT_Wstrict_overflow, "%s", warnmsg);
   else
-    locus = gimple_location (stmt);
-  warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
+    warning_at (gimple_location (stmt), OPT_Wstrict_overflow, "%K%s",
+		build_empty_stmt (gimple_location (stmt)), warnmsg);
 }
 
 /* Stop deferring overflow warnings, ignoring any deferred
diff --git a/gcc/testsuite/c-c++-common/Wstrict-overflow-1.c b/gcc/testsuite/c-c++-common/Wstrict-overflow-1.c
new file mode 100644
index 0000000..1624a33
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/Wstrict-overflow-1.c
@@ -0,0 +1,24 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -g -Wstrict-overflow" } */
+
+int f (void);
+
+static int g (int a, int b, int c)
+{
+  if (a + b < c) /* { dg-warning "assuming signed overflow" } */
+    return -1;
+  return f ();
+}
+
+static int h (int a, int b)
+{
+  return g(a, 1, b);
+}
+
+int k (int a)
+{
+  return h(a, a);
+}
+
+/* { dg-message "inlined from.*:15:" "inlined 15" { target *-*-* } 0 } */
+/* { dg-message "inlined from.*:20:" "inlined 20" { target *-*-* } 0 } */