public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: David Malcolm <dmalcolm@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: David Malcolm <dmalcolm@redhat.com>
Subject: [PATCH] cfg: propagate source location in gimple_split_edge [PR115564]
Date: Fri, 21 Jun 2024 18:25:41 -0400	[thread overview]
Message-ID: <20240621222541.1166986-1-dmalcolm@redhat.com> (raw)

PR analyzer/115564 reports a missing warning from the analyzer
on this infinite loop at -O2 and above:

 void test (unsigned b)
 {
   for (unsigned i = b; i >= 0; --i) {}
 }

The issue is that there are no useful location_t values in the CFG
by the time the analyzer sees it: two basic blocks with no
statements, connected by edges with UNKNOWN_LOCATION for their
"goto_locus" values.  The analyzer's attempts to get a location for the
loop fail with "UNKNOWN_LOCATION", and so it gives up on the warning.

Root cause is that the edge in question is created by gimple_split_edge
within the loop optimizer, and gimple_split_edge creates the new edge
with UNKNOWN_LOCATION.

This patch tweaks gimple_split_edge to copy edge_in->goto_locus's to the
new edge, so that the edge seen by the analyzer has a useful goto_locus
value, fixing the issue.

Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu.

Successful run of analyzer integration tests on x86_64-pc-linux-gnu,
which shows 8 new true positives from -Wanalyzer-infinite-loop with
the patch.

OK for trunk?

gcc/testsuite/ChangeLog:
	PR analyzer/115564
	* c-c++-common/analyzer/infinite-loop-pr115564.c: New test.

gcc/ChangeLog:
	PR analyzer/115564
	* tree-cfg.cc (gimple_split_edge): Propagate any source location
	from EDGE_IN to the new edge.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
---
 .../c-c++-common/analyzer/infinite-loop-pr115564.c        | 8 ++++++++
 gcc/tree-cfg.cc                                           | 3 +++
 2 files changed, 11 insertions(+)
 create mode 100644 gcc/testsuite/c-c++-common/analyzer/infinite-loop-pr115564.c

diff --git a/gcc/testsuite/c-c++-common/analyzer/infinite-loop-pr115564.c b/gcc/testsuite/c-c++-common/analyzer/infinite-loop-pr115564.c
new file mode 100644
index 000000000000..950d92dd1254
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/analyzer/infinite-loop-pr115564.c
@@ -0,0 +1,8 @@
+/* Verify that we detect the infinite loop below even at -O2.  */
+
+/* { dg-additional-options "-O2" } */
+
+void test (unsigned b)
+{
+  for (unsigned i = b; i >= 0; --i) {} /* { dg-warning "infinite loop" } */
+}
diff --git a/gcc/tree-cfg.cc b/gcc/tree-cfg.cc
index 7fb7b92966be..45c0eef6c095 100644
--- a/gcc/tree-cfg.cc
+++ b/gcc/tree-cfg.cc
@@ -3061,6 +3061,9 @@ gimple_split_edge (edge edge_in)
   /* set_phi_nodes sets the BB of the PHI nodes, so do it manually here.  */
   dest->il.gimple.phi_nodes = saved_phis;
 
+  /* Propagate any source location from EDGE_IN to the new edge.  */
+  new_edge->goto_locus = edge_in->goto_locus;
+
   return new_bb;
 }
 
-- 
2.26.3


             reply	other threads:[~2024-06-21 22:25 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-21 22:25 David Malcolm [this message]
2024-06-24 12:26 ` Richard Biener

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240621222541.1166986-1-dmalcolm@redhat.com \
    --to=dmalcolm@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).