From 0f9aa35c79a0fe195d5076375b5794246cf44819 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Fri, 26 Mar 2021 13:26:15 -0400 Subject: [PATCH] analyzer: only call get_diagnostic_tree when it's needed impl_sm_context::get_diagnostic_tree could be expensive, and I find myself needing to put a breakpoint on it to debug PR analyzer/99771, so only call it if we're about to use the result. gcc/analyzer/ChangeLog: * sm-file.cc (fileptr_state_machine::on_stmt): Only call get_diagnostic_tree if the result will be used. * sm-malloc.cc (malloc_state_machine::on_stmt): Likewise. (malloc_state_machine::on_deallocator_call): Likewise. (malloc_state_machine::on_realloc_call): Likewise. (malloc_state_machine::on_realloc_call): Likewise. * sm-sensitive.cc (sensitive_state_machine::warn_for_any_exposure): Likewise. * sm-taint.cc (taint_state_machine::on_stmt): Likewise. --- gcc/analyzer/sm-file.cc | 2 +- gcc/analyzer/sm-malloc.cc | 10 +++++++--- gcc/analyzer/sm-sensitive.cc | 8 +++++--- gcc/analyzer/sm-taint.cc | 4 +++- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/gcc/analyzer/sm-file.cc b/gcc/analyzer/sm-file.cc index 7a81c8ff632..d64c313e31c 100644 --- a/gcc/analyzer/sm-file.cc +++ b/gcc/analyzer/sm-file.cc @@ -344,7 +344,6 @@ fileptr_state_machine::on_stmt (sm_context *sm_ctxt, if (is_named_call_p (callee_fndecl, "fclose", call, 1)) { tree arg = gimple_call_arg (call, 0); - tree diag_arg = sm_ctxt->get_diagnostic_tree (arg); sm_ctxt->on_transition (node, stmt, arg, m_start, m_closed); @@ -356,6 +355,7 @@ fileptr_state_machine::on_stmt (sm_context *sm_ctxt, if (sm_ctxt->get_state (stmt, arg) == m_closed) { + tree diag_arg = sm_ctxt->get_diagnostic_tree (arg); sm_ctxt->warn (node, stmt, arg, new double_fclose (*this, diag_arg)); sm_ctxt->set_next_state (stmt, arg, m_stop); diff --git a/gcc/analyzer/sm-malloc.cc b/gcc/analyzer/sm-malloc.cc index ef250c80915..ae03b068a88 100644 --- a/gcc/analyzer/sm-malloc.cc +++ b/gcc/analyzer/sm-malloc.cc @@ -1674,11 +1674,11 @@ malloc_state_machine::on_stmt (sm_context *sm_ctxt, if (TREE_CODE (op) == MEM_REF) { tree arg = TREE_OPERAND (op, 0); - tree diag_arg = sm_ctxt->get_diagnostic_tree (arg); state_t state = sm_ctxt->get_state (stmt, arg); if (unchecked_p (state)) { + tree diag_arg = sm_ctxt->get_diagnostic_tree (arg); sm_ctxt->warn (node, stmt, arg, new possible_null_deref (*this, diag_arg)); const allocation_state *astate = as_a_allocation_state (state); @@ -1686,12 +1686,14 @@ malloc_state_machine::on_stmt (sm_context *sm_ctxt, } else if (state == m_null) { + tree diag_arg = sm_ctxt->get_diagnostic_tree (arg); sm_ctxt->warn (node, stmt, arg, new null_deref (*this, diag_arg)); sm_ctxt->set_next_state (stmt, arg, m_stop); } else if (freed_p (state)) { + tree diag_arg = sm_ctxt->get_diagnostic_tree (arg); const allocation_state *astate = as_a_allocation_state (state); sm_ctxt->warn (node, stmt, arg, new use_after_free (*this, diag_arg, @@ -1738,7 +1740,6 @@ malloc_state_machine::on_deallocator_call (sm_context *sm_ctxt, if (argno >= gimple_call_num_args (call)) return; tree arg = gimple_call_arg (call, argno); - tree diag_arg = sm_ctxt->get_diagnostic_tree (arg); state_t state = sm_ctxt->get_state (call, arg); @@ -1752,6 +1753,7 @@ malloc_state_machine::on_deallocator_call (sm_context *sm_ctxt, if (!astate->m_deallocators->contains_p (d)) { /* Wrong allocator. */ + tree diag_arg = sm_ctxt->get_diagnostic_tree (arg); pending_diagnostic *pd = new mismatching_deallocation (*this, diag_arg, astate->m_deallocators, @@ -1766,6 +1768,7 @@ malloc_state_machine::on_deallocator_call (sm_context *sm_ctxt, else if (state == d->m_freed) { /* freed -> stop, with warning. */ + tree diag_arg = sm_ctxt->get_diagnostic_tree (arg); sm_ctxt->warn (node, call, arg, new double_free (*this, diag_arg, d->m_name)); sm_ctxt->set_next_state (call, arg, m_stop); @@ -1773,6 +1776,7 @@ malloc_state_machine::on_deallocator_call (sm_context *sm_ctxt, else if (state == m_non_heap) { /* non-heap -> stop, with warning. */ + tree diag_arg = sm_ctxt->get_diagnostic_tree (arg); sm_ctxt->warn (node, call, arg, new free_of_non_heap (*this, diag_arg, d->m_name)); @@ -1806,7 +1810,6 @@ malloc_state_machine::on_realloc_call (sm_context *sm_ctxt, const gcall *call) const { tree ptr = gimple_call_arg (call, 0); - tree diag_ptr = sm_ctxt->get_diagnostic_tree (ptr); state_t state = sm_ctxt->get_state (call, ptr); @@ -1818,6 +1821,7 @@ malloc_state_machine::on_realloc_call (sm_context *sm_ctxt, if (astate->m_deallocators != &m_free) { /* Wrong allocator. */ + tree diag_ptr = sm_ctxt->get_diagnostic_tree (ptr); pending_diagnostic *pd = new mismatching_deallocation (*this, diag_ptr, astate->m_deallocators, diff --git a/gcc/analyzer/sm-sensitive.cc b/gcc/analyzer/sm-sensitive.cc index 95172f08922..9703f7e7916 100644 --- a/gcc/analyzer/sm-sensitive.cc +++ b/gcc/analyzer/sm-sensitive.cc @@ -174,10 +174,12 @@ sensitive_state_machine::warn_for_any_exposure (sm_context *sm_ctxt, const gimple *stmt, tree arg) const { - tree diag_arg = sm_ctxt->get_diagnostic_tree (arg); if (sm_ctxt->get_state (stmt, arg) == m_sensitive) - sm_ctxt->warn (node, stmt, arg, - new exposure_through_output_file (*this, diag_arg)); + { + tree diag_arg = sm_ctxt->get_diagnostic_tree (arg); + sm_ctxt->warn (node, stmt, arg, + new exposure_through_output_file (*this, diag_arg)); + } } /* Implementation of state_machine::on_stmt vfunc for diff --git a/gcc/analyzer/sm-taint.cc b/gcc/analyzer/sm-taint.cc index 2b2792e5edb..e2460f9cf5c 100644 --- a/gcc/analyzer/sm-taint.cc +++ b/gcc/analyzer/sm-taint.cc @@ -227,7 +227,6 @@ taint_state_machine::on_stmt (sm_context *sm_ctxt, if (op == ARRAY_REF) { tree arg = TREE_OPERAND (rhs1, 1); - tree diag_arg = sm_ctxt->get_diagnostic_tree (arg); /* Unsigned types have an implicit lower bound. */ bool is_unsigned = false; @@ -239,6 +238,7 @@ taint_state_machine::on_stmt (sm_context *sm_ctxt, if (state == m_tainted) { /* Complain about missing bounds. */ + tree diag_arg = sm_ctxt->get_diagnostic_tree (arg); pending_diagnostic *d = new tainted_array_index (*this, diag_arg, is_unsigned @@ -249,6 +249,7 @@ taint_state_machine::on_stmt (sm_context *sm_ctxt, else if (state == m_has_lb) { /* Complain about missing upper bound. */ + tree diag_arg = sm_ctxt->get_diagnostic_tree (arg); sm_ctxt->warn (node, stmt, arg, new tainted_array_index (*this, diag_arg, BOUNDS_LOWER)); @@ -259,6 +260,7 @@ taint_state_machine::on_stmt (sm_context *sm_ctxt, /* Complain about missing lower bound. */ if (!is_unsigned) { + tree diag_arg = sm_ctxt->get_diagnostic_tree (arg); sm_ctxt->warn (node, stmt, arg, new tainted_array_index (*this, diag_arg, BOUNDS_UPPER));