[committed] analyzer: add svalue::maybe_get_region

Message ID 20210716195419.74646-1-dmalcolm@redhat.com
State New
Headers show
Series
  • [committed] analyzer: add svalue::maybe_get_region
Related show

Commit Message

Lewis Hyatt via Gcc-patches July 16, 2021, 7:54 p.m.
Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu.
Pushed to trunk as 5932dd35eaa816e8d9b6406c6c433395ff5b6162.

gcc/analyzer/ChangeLog:
	* program-state.cc (program_state::detect_leaks): Simplify using
	svalue::maybe_get_region.
	* region-model-impl-calls.cc (region_model::impl_call_fgets): Likewise.
	(region_model::impl_call_fread): Likewise.
	(region_model::impl_call_free): Likewise.
	(region_model::impl_call_operator_delete): Likewise.
	* region-model.cc (selftest::test_stack_frames): Likewise.
	(selftest::test_state_merging): Likewise.
	* svalue.cc (svalue::maybe_get_region): New.
	* svalue.h (svalue::maybe_get_region): New decl.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>

---
 gcc/analyzer/program-state.cc           |  9 +++------
 gcc/analyzer/region-model-impl-calls.cc | 16 ++++------------
 gcc/analyzer/region-model.cc            |  5 ++---
 gcc/analyzer/svalue.cc                  | 12 ++++++++++++
 gcc/analyzer/svalue.h                   |  1 +
 5 files changed, 22 insertions(+), 21 deletions(-)

-- 
2.26.3

Patch

diff --git a/gcc/analyzer/program-state.cc b/gcc/analyzer/program-state.cc
index 23cfcb032c6..cc53aef552f 100644
--- a/gcc/analyzer/program-state.cc
+++ b/gcc/analyzer/program-state.cc
@@ -1285,12 +1285,9 @@  program_state::detect_leaks (const program_state &src_state,
 
   /* Purge dead heap-allocated regions from dynamic extents.  */
   for (const svalue *sval : dead_svals)
-    if (const region_svalue *region_sval = sval->dyn_cast_region_svalue ())
-      {
-	const region *reg = region_sval->get_pointee ();
-	if (reg->get_kind () == RK_HEAP_ALLOCATED)
-	  dest_state.m_region_model->unset_dynamic_extents (reg);
-      }
+    if (const region *reg = sval->maybe_get_region ())
+      if (reg->get_kind () == RK_HEAP_ALLOCATED)
+	dest_state.m_region_model->unset_dynamic_extents (reg);
 }
 
 #if CHECKING_P
diff --git a/gcc/analyzer/region-model-impl-calls.cc b/gcc/analyzer/region-model-impl-calls.cc
index 4be6550f07f..efb0fc83433 100644
--- a/gcc/analyzer/region-model-impl-calls.cc
+++ b/gcc/analyzer/region-model-impl-calls.cc
@@ -325,10 +325,8 @@  region_model::impl_call_fgets (const call_details &cd)
   /* Ideally we would bifurcate state here between the
      error vs no error cases.  */
   const svalue *ptr_sval = cd.get_arg_svalue (0);
-  if (const region_svalue *ptr_to_region_sval
-      = ptr_sval->dyn_cast_region_svalue ())
+  if (const region *reg = ptr_sval->maybe_get_region ())
     {
-      const region *reg = ptr_to_region_sval->get_pointee ();
       const region *base_reg = reg->get_base_region ();
       const svalue *new_sval = cd.get_or_create_conjured_svalue (base_reg);
       purge_state_involving (new_sval, cd.get_ctxt ());
@@ -342,10 +340,8 @@  void
 region_model::impl_call_fread (const call_details &cd)
 {
   const svalue *ptr_sval = cd.get_arg_svalue (0);
-  if (const region_svalue *ptr_to_region_sval
-      = ptr_sval->dyn_cast_region_svalue ())
+  if (const region *reg = ptr_sval->maybe_get_region ())
     {
-      const region *reg = ptr_to_region_sval->get_pointee ();
       const region *base_reg = reg->get_base_region ();
       const svalue *new_sval = cd.get_or_create_conjured_svalue (base_reg);
       purge_state_involving (new_sval, cd.get_ctxt ());
@@ -372,12 +368,10 @@  void
 region_model::impl_call_free (const call_details &cd)
 {
   const svalue *ptr_sval = cd.get_arg_svalue (0);
-  if (const region_svalue *ptr_to_region_sval
-      = ptr_sval->dyn_cast_region_svalue ())
+  if (const region *freed_reg = ptr_sval->maybe_get_region ())
     {
       /* If the ptr points to an underlying heap region, delete it,
 	 poisoning pointers.  */
-      const region *freed_reg = ptr_to_region_sval->get_pointee ();
       unbind_region_and_descendents (freed_reg, POISON_KIND_FREED);
       m_dynamic_extents.remove (freed_reg);
     }
@@ -472,12 +466,10 @@  bool
 region_model::impl_call_operator_delete (const call_details &cd)
 {
   const svalue *ptr_sval = cd.get_arg_svalue (0);
-  if (const region_svalue *ptr_to_region_sval
-      = ptr_sval->dyn_cast_region_svalue ())
+  if (const region *freed_reg = ptr_sval->maybe_get_region ())
     {
       /* If the ptr points to an underlying heap region, delete it,
 	 poisoning pointers.  */
-      const region *freed_reg = ptr_to_region_sval->get_pointee ();
       unbind_region_and_descendents (freed_reg, POISON_KIND_FREED);
     }
   return false;
diff --git a/gcc/analyzer/region-model.cc b/gcc/analyzer/region-model.cc
index 3fe2cce229b..190c8524f90 100644
--- a/gcc/analyzer/region-model.cc
+++ b/gcc/analyzer/region-model.cc
@@ -4541,7 +4541,7 @@  test_stack_frames ()
      renumbering.  */
   const svalue *new_q_sval = model.get_rvalue (q, &ctxt);
   ASSERT_EQ (new_q_sval->get_kind (), SK_REGION);
-  ASSERT_EQ (new_q_sval->dyn_cast_region_svalue ()->get_pointee (),
+  ASSERT_EQ (new_q_sval->maybe_get_region (),
 	     model.get_lvalue (p, &ctxt));
 
   /* Verify that top of stack has been updated.  */
@@ -5070,8 +5070,7 @@  test_state_merging ()
     model0.set_value (q_in_first_frame, sval_ptr, NULL);
 
     /* Verify that it's pointing at the newer frame.  */
-    const region *reg_pointee
-      = sval_ptr->dyn_cast_region_svalue ()->get_pointee ();
+    const region *reg_pointee = sval_ptr->maybe_get_region ();
     ASSERT_EQ (reg_pointee->get_parent_region (), reg_2nd_frame);
 
     model0.canonicalize ();
diff --git a/gcc/analyzer/svalue.cc b/gcc/analyzer/svalue.cc
index 22da769f095..fa9a862bdb5 100644
--- a/gcc/analyzer/svalue.cc
+++ b/gcc/analyzer/svalue.cc
@@ -111,6 +111,18 @@  svalue::maybe_get_constant () const
     return NULL_TREE;
 }
 
+/* If this svalue is a region_svalue, return the region it points to.
+   Otherwise return NULL.  */
+
+const region *
+svalue::maybe_get_region () const
+{
+  if (const region_svalue *region_sval = dyn_cast_region_svalue ())
+    return region_sval->get_pointee ();
+  else
+    return NULL;
+}
+
 /* If this svalue is a cast (i.e a unaryop NOP_EXPR or VIEW_CONVERT_EXPR),
    return the underlying svalue.
    Otherwise return NULL.  */
diff --git a/gcc/analyzer/svalue.h b/gcc/analyzer/svalue.h
index 20d7cf8f751..15198899cd5 100644
--- a/gcc/analyzer/svalue.h
+++ b/gcc/analyzer/svalue.h
@@ -126,6 +126,7 @@  public:
   dyn_cast_conjured_svalue () const { return NULL; }
 
   tree maybe_get_constant () const;
+  const region *maybe_get_region () const;
   const svalue *maybe_undo_cast () const;
   const svalue *unwrap_any_unmergeable () const;