]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
analyzer: only call get_diagnostic_tree when it's needed
authorDavid Malcolm <dmalcolm@redhat.com>
Fri, 26 Mar 2021 17:26:15 +0000 (13:26 -0400)
committerDavid Malcolm <dmalcolm@redhat.com>
Tue, 30 Mar 2021 21:51:21 +0000 (17:51 -0400)
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
gcc/analyzer/sm-malloc.cc
gcc/analyzer/sm-sensitive.cc
gcc/analyzer/sm-taint.cc

index 7a81c8ff6322b76bc136ef5cf67b0e10b26fc5e6..d64c313e31cb2cd490da5cca379f942fc86a68ab 100644 (file)
@@ -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);
index ef250c80915d221b11ce80760e540554ccc77a19..ae03b068a889b836f5bd51b2189a53a4a8cc3b29 100644 (file)
@@ -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,
index 95172f089228ea82dbcd0dba2c0854e6140d4e0b..9703f7e791680e91feb454c3d2da639c8b1d2aa0 100644 (file)
@@ -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
index 2b2792e5edba6c32917ee31840ac28cd0b89c633..e2460f9cf5c0a275ae5f000991cb50090d62712d 100644 (file)
@@ -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));