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 r12-2374] analyzer: add svalue::maybe_get_region Date: Fri, 16 Jul 2021 19:46:09 +0000 (GMT) [thread overview] Message-ID: <20210716194609.E4A8F385E007@sourceware.org> (raw) https://gcc.gnu.org/g:5932dd35eaa816e8d9b6406c6c433395ff5b6162 commit r12-2374-g5932dd35eaa816e8d9b6406c6c433395ff5b6162 Author: David Malcolm <dmalcolm@redhat.com> Date: Fri Jul 16 15:45:33 2021 -0400 analyzer: add svalue::maybe_get_region 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> Diff: --- 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(-) 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;
reply other threads:[~2021-07-16 19:46 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=20210716194609.E4A8F385E007@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: linkBe 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).