public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: David Malcolm <dmalcolm@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-4398] analyzer: fix folding of '(PTR + 0) => PTR' [PR105784]
Date: Wed, 30 Nov 2022 01:03:13 +0000 (GMT)	[thread overview]
Message-ID: <20221130010313.146043858D37@sourceware.org> (raw)

https://gcc.gnu.org/g:3a32fb2eaa761aac13ffe5424748d5839038ef66

commit r13-4398-g3a32fb2eaa761aac13ffe5424748d5839038ef66
Author: David Malcolm <dmalcolm@redhat.com>
Date:   Tue Nov 29 19:56:27 2022 -0500

    analyzer: fix folding of '(PTR + 0) => PTR' [PR105784]
    
    gcc/analyzer/ChangeLog:
            PR analyzer/105784
            * region-model-manager.cc
            (region_model_manager::maybe_fold_binop): For POINTER_PLUS_EXPR,
            PLUS_EXPR and MINUS_EXPR, eliminate requirement that the final
            type matches that of arg0 in favor of a cast.
    
    gcc/testsuite/ChangeLog:
            PR analyzer/105784
            * gcc.dg/analyzer/torture/fold-ptr-arith-pr105784.c: New test.
    
    Signed-off-by: David Malcolm <dmalcolm@redhat.com>

Diff:
---
 gcc/analyzer/region-model-manager.cc               |  8 ++--
 .../analyzer/torture/fold-ptr-arith-pr105784.c     | 43 ++++++++++++++++++++++
 2 files changed, 47 insertions(+), 4 deletions(-)

diff --git a/gcc/analyzer/region-model-manager.cc b/gcc/analyzer/region-model-manager.cc
index d9a7ae91a35..ae63c664ae5 100644
--- a/gcc/analyzer/region-model-manager.cc
+++ b/gcc/analyzer/region-model-manager.cc
@@ -613,13 +613,13 @@ region_model_manager::maybe_fold_binop (tree type, enum tree_code op,
     case POINTER_PLUS_EXPR:
     case PLUS_EXPR:
       /* (VAL + 0) -> VAL.  */
-      if (cst1 && zerop (cst1) && type == arg0->get_type ())
-	return arg0;
+      if (cst1 && zerop (cst1))
+	return get_or_create_cast (type, arg0);
       break;
     case MINUS_EXPR:
       /* (VAL - 0) -> VAL.  */
-      if (cst1 && zerop (cst1) && type == arg0->get_type ())
-	return arg0;
+      if (cst1 && zerop (cst1))
+	return get_or_create_cast (type, arg0);
       break;
     case MULT_EXPR:
       /* (VAL * 0).  */
diff --git a/gcc/testsuite/gcc.dg/analyzer/torture/fold-ptr-arith-pr105784.c b/gcc/testsuite/gcc.dg/analyzer/torture/fold-ptr-arith-pr105784.c
new file mode 100644
index 00000000000..5e5a2bf79a5
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/analyzer/torture/fold-ptr-arith-pr105784.c
@@ -0,0 +1,43 @@
+/* { dg-skip-if "" { *-*-* } { "-fno-fat-lto-objects" } { "" } } */
+
+#include "../analyzer-decls.h"
+
+extern _Bool quit_flag;
+extern void char_charset (int);
+
+static void
+__analyzer_ccl_driver (int *source, int src_size)
+{
+  int *src = source, *src_end = src + src_size;
+  int i = 0;
+
+  while (!quit_flag)
+    {
+      if (src < src_end)
+	{
+	  __analyzer_dump_path (); /* { dg-message "path" } */
+	  i = *src++; /* { dg-bogus "uninit" } */
+	}
+      char_charset (i);
+    }
+}
+
+void
+Fccl_execute_on_string (char *str, long str_bytes)
+{
+  while (1)
+    {
+      char *p = str;
+      char *endp = str + str_bytes;
+      int source[1024];
+      int src_size = 0;
+
+      while (src_size < 1024 && p < endp)
+	{
+	  __analyzer_dump_path (); /* { dg-message "path" } */
+	  source[src_size++] = *p++;
+	}
+
+      __analyzer_ccl_driver (source, src_size);
+    }
+}

                 reply	other threads:[~2022-11-30  1:03 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20221130010313.146043858D37@sourceware.org \
    --to=dmalcolm@gcc.gnu.org \
    --cc=gcc-cvs@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).