LCOV - code coverage report
Current view: top level - gcc/analyzer - pending-diagnostic.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 84.8 % 112 95
Test Date: 2026-11-14 16:39:50 Functions: 93.3 % 15 14
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Classes for analyzer diagnostics.
       2              :    Copyright (C) 2019-2026 Free Software Foundation, Inc.
       3              :    Contributed by David Malcolm <dmalcolm@redhat.com>.
       4              : 
       5              : This file is part of GCC.
       6              : 
       7              : GCC is free software; you can redistribute it and/or modify it
       8              : under the terms of the GNU General Public License as published by
       9              : the Free Software Foundation; either version 3, or (at your option)
      10              : any later version.
      11              : 
      12              : GCC is distributed in the hope that it will be useful, but
      13              : WITHOUT ANY WARRANTY; without even the implied warranty of
      14              : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15              : General Public License for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : #include "analyzer/common.h"
      22              : 
      23              : #include "diagnostics/event-id.h"
      24              : #include "diagnostics/logging.h"
      25              : #include "diagnostics/sarif-sink.h"
      26              : #include "cpplib.h"
      27              : #include "digraph.h"
      28              : #include "ordered-hash-map.h"
      29              : #include "cfg.h"
      30              : #include "gimple-iterator.h"
      31              : #include "cgraph.h"
      32              : 
      33              : #include "analyzer/analyzer-logging.h"
      34              : #include "analyzer/sm.h"
      35              : #include "analyzer/sm.h"
      36              : #include "analyzer/pending-diagnostic.h"
      37              : #include "analyzer/diagnostic-manager.h"
      38              : #include "analyzer/call-string.h"
      39              : #include "analyzer/program-point.h"
      40              : #include "analyzer/store.h"
      41              : #include "analyzer/region-model.h"
      42              : #include "analyzer/supergraph.h"
      43              : #include "analyzer/program-state.h"
      44              : #include "analyzer/exploded-graph.h"
      45              : #include "analyzer/checker-path.h"
      46              : 
      47              : #if ENABLE_ANALYZER
      48              : 
      49              : namespace ana {
      50              : 
      51              : /* struct interesting_t.  */
      52              : 
      53              : /* Mark the creation of REG as being interesting.  */
      54              : 
      55              : void
      56         2806 : interesting_t::add_region_creation (const region *reg)
      57              : {
      58         2806 :   gcc_assert (reg);
      59         2806 :   m_region_creation.safe_push (reg);
      60         2806 : }
      61              : 
      62              : /* Mark the value read from REG as being interesting.  */
      63              : 
      64              : void
      65         1202 : interesting_t::add_read_region (const region *reg, std::string debug_desc)
      66              : {
      67         1202 :   gcc_assert (reg);
      68         1202 :   m_read_regions.push_back (diagnostic_state (std::move (debug_desc), reg));
      69         1202 : }
      70              : 
      71              : void
      72            0 : interesting_t::dump_to_pp (pretty_printer *pp, bool simple) const
      73              : {
      74            0 :   pp_string (pp, "{ region creation: [");
      75            0 :   unsigned i;
      76            0 :   const region *reg;
      77            0 :   FOR_EACH_VEC_ELT (m_region_creation, i, reg)
      78              :     {
      79            0 :       if (i > 0)
      80            0 :         pp_string (pp, ", ");
      81            0 :       reg->dump_to_pp (pp, simple);
      82              :     }
      83            0 :   pp_string (pp, "], read regions: [");
      84            0 :   for (i = 0; i < m_read_regions.size (); ++i)
      85              :     {
      86            0 :       auto &ann = m_read_regions[i];
      87            0 :       if (i > 0)
      88            0 :         pp_string (pp, ", ");
      89            0 :       ann.dump_to_pp (pp);
      90              :     }
      91            0 :   pp_string (pp, "]}");
      92            0 : }
      93              : 
      94              : /* class diagnostic_emission_context.  */
      95              : 
      96              : /* Get the pending_diagnostic being emitted.  */
      97              : 
      98              : const pending_diagnostic &
      99         4331 : diagnostic_emission_context::get_pending_diagnostic () const
     100              : {
     101         4331 :   return *m_sd.m_d.get ();
     102              : }
     103              : 
     104              : /* Emit a warning, using the rich_location, metadata, and the
     105              :    pending_diagnostic's option.  */
     106              : 
     107              : bool
     108         4071 : diagnostic_emission_context::warn (const char *gmsgid, ...)
     109              : {
     110         4071 :   auto dc_logger = global_dc->get_logger ();
     111         4071 :   diagnostics::logging::log_function_params
     112         4071 :     (dc_logger, "ana::diagnostic_emission_context::warn")
     113         4071 :     .log_param_string ("gmsgid", gmsgid);
     114         4071 :   diagnostics::logging::auto_inc_depth depth_sentinel (dc_logger);
     115              : 
     116         4071 :   const pending_diagnostic &pd = get_pending_diagnostic ();
     117         4071 :   auto_diagnostic_group d;
     118         4071 :   va_list ap;
     119         4071 :   va_start (ap, gmsgid);
     120        12213 :   const bool result = emit_diagnostic_valist_meta (diagnostics::kind::warning,
     121         4071 :                                                    &m_rich_loc, &m_metadata,
     122         4071 :                                                    pd.get_controlling_option (),
     123              :                                                    gmsgid, &ap);
     124         4071 :   va_end (ap);
     125              : 
     126         4071 :   if (dc_logger)
     127            0 :     dc_logger->log_bool_return ("ana::diagnostic_emission_context::warn",
     128              :                                 result);
     129              : 
     130         8142 :   return result;
     131         4071 : }
     132              : 
     133              : /* Emit a note, using the rich_location and metadata (and the
     134              :    pending_diagnostic's option).  */
     135              : 
     136              : void
     137          260 : diagnostic_emission_context::inform (const char *gmsgid, ...)
     138              : {
     139          260 :   auto dc_logger = global_dc->get_logger ();
     140          260 :   diagnostics::logging::log_function_params
     141          260 :     (dc_logger, "ana::diagnostic_emission_context::inform")
     142          260 :     .log_param_string ("gmsgid", gmsgid);
     143          260 :   diagnostics::logging::auto_inc_depth depth_sentinel (dc_logger);
     144              : 
     145          260 :   const pending_diagnostic &pd = get_pending_diagnostic ();
     146          260 :   auto_diagnostic_group d;
     147          260 :   va_list ap;
     148          260 :   va_start (ap, gmsgid);
     149          780 :   emit_diagnostic_valist_meta (diagnostics::kind::note,
     150          260 :                                &m_rich_loc, &m_metadata,
     151          260 :                                pd.get_controlling_option (),
     152              :                                gmsgid, &ap);
     153          260 :   va_end (ap);
     154          260 : }
     155              : 
     156              : std::unique_ptr<json::object>
     157            1 : pending_diagnostic::to_json () const
     158              : {
     159              :   /* maybe_add_sarif_properties assumes a sarif_object, so use one here.  */
     160            1 :   auto obj = std::make_unique<diagnostics::sarif_object> ();
     161            1 :   obj->set_string ("kind", get_kind ());
     162            1 :   maybe_add_sarif_properties (*obj);
     163            1 :   return obj;
     164            1 : }
     165              : 
     166              : /* Return true if T1 and T2 are "the same" for the purposes of
     167              :    diagnostic deduplication.  */
     168              : 
     169              : bool
     170       144133 : pending_diagnostic::same_tree_p (tree t1, tree t2)
     171              : {
     172       144133 :   return simple_cst_equal (t1, t2) == 1;
     173              : }
     174              : 
     175              : /* Return true iff IDENT is STR.  */
     176              : 
     177              : static bool
     178        21196 : ht_ident_eq (ht_identifier ident, const char *str)
     179              : {
     180        21196 :   return (strlen (str) == ident.len
     181        21196 :           && 0 == strcmp (str, (const char *)ident.str));
     182              : }
     183              : 
     184              : /* Return true if we should show the expansion location rather than unwind
     185              :    within MACRO.  */
     186              : 
     187              : static bool
     188         4276 : fixup_location_in_macro_p (cpp_hashnode *macro)
     189              : {
     190         4276 :   ht_identifier ident = macro->ident;
     191              : 
     192              :   /* Don't unwind inside "alloca" macro, so that we don't suppress warnings
     193              :      from it (due to being in system headers).  */
     194         4276 :   if (ht_ident_eq (ident, "alloca"))
     195              :     return true;
     196              : 
     197              :   /* Don't unwind inside <stdarg.h> macros, so that we don't suppress warnings
     198              :      from them (due to being in system headers).  */
     199         4244 :   if (ht_ident_eq (ident, "va_start")
     200         4234 :       || ht_ident_eq (ident, "va_copy")
     201         4227 :       || ht_ident_eq (ident, "va_arg")
     202         8459 :       || ht_ident_eq (ident, "va_end"))
     203              :     return true;
     204              :   return false;
     205              : }
     206              : 
     207              : /* Base implementation of pending_diagnostic::fixup_location.
     208              :    Don't unwind inside macros for which fixup_location_in_macro_p is true.  */
     209              : 
     210              : location_t
     211        32846 : pending_diagnostic::fixup_location (location_t loc, bool) const
     212              : {
     213        32846 :   if (linemap_location_from_macro_expansion_p (line_table, loc))
     214              :     {
     215         4276 :       line_map *map
     216         4276 :         = const_cast <line_map *> (linemap_lookup (line_table, loc));
     217         4276 :       const line_map_macro *macro_map = linemap_check_macro (map);
     218         4276 :       if (fixup_location_in_macro_p (macro_map->macro))
     219           67 :         loc = linemap_resolve_location (line_table, loc,
     220              :                                         LRK_MACRO_EXPANSION_POINT, nullptr);
     221              :     }
     222        32846 :   return loc;
     223              : }
     224              : 
     225              : /* Base implementation of pending_diagnostic::add_function_entry_event.
     226              :    Add a function_entry_event to EMISSION_PATH.  */
     227              : 
     228              : void
     229         5352 : pending_diagnostic::add_function_entry_event (const exploded_edge &eedge,
     230              :                                               checker_path *emission_path,
     231              :                                               const state_transition_at_call *state_trans)
     232              : {
     233         5352 :   const exploded_node *dst_node = eedge.m_dest;
     234         5352 :   const program_point &dst_point = dst_node->get_point ();
     235         5352 :   const program_state &dst_state = dst_node->get_state ();
     236              : 
     237              :   /* If we have STATE_TRANS with a specific param, put the event on
     238              :      that parameter, otherwise put in on the function name.  */
     239         5352 :   auto loc_info {event_loc_info_for_function_entry (dst_point, state_trans)};
     240              : 
     241         5352 :   emission_path->add_event
     242         5352 :     (std::make_unique<function_entry_event> (loc_info,
     243              :                                              dst_state,
     244              :                                              state_trans));
     245         5352 : }
     246              : 
     247              : /* Base implementation of pending_diagnostic::add_call_event.
     248              :    Add a call_event to EMISSION_PATH.  */
     249              : 
     250              : void
     251         1363 : pending_diagnostic::add_call_event (const exploded_edge &eedge,
     252              :                                     const gcall &,
     253              :                                     checker_path &emission_path,
     254              :                                     const state_transition_at_call *state_trans)
     255              : {
     256         1363 :   emission_path.add_event
     257         1363 :     (std::make_unique<call_event> (eedge,
     258         1363 :                                    event_loc_info (eedge.m_src),
     259              :                                    state_trans));
     260         1363 : }
     261              : 
     262              : /* Base implementation of pending_diagnostic::add_region_creation_events.
     263              :    See the comment for class region_creation_event.  */
     264              : 
     265              : void
     266          602 : pending_diagnostic::add_region_creation_events (const region *reg,
     267              :                                                 tree capacity,
     268              :                                                 const event_loc_info &loc_info,
     269              :                                                 checker_path &emission_path)
     270              : {
     271          602 :   emission_path.add_event
     272          602 :     (std::make_unique<region_creation_event_memory_space>
     273          602 :        (reg->get_memory_space (),
     274              :         loc_info));
     275              : 
     276          602 :   if (capacity)
     277          349 :     emission_path.add_event
     278          349 :       (std::make_unique<region_creation_event_capacity> (capacity, loc_info));
     279          602 : }
     280              : 
     281              : /* Base implementation of pending_diagnostic::add_final_event.
     282              :    Add a warning_event to the end of EMISSION_PATH.  */
     283              : 
     284              : void
     285         4148 : pending_diagnostic::add_final_event (const state_machine *sm,
     286              :                                      const exploded_node *enode,
     287              :                                      const event_loc_info &loc_info,
     288              :                                      tree var, state_machine::state_t state,
     289              :                                      checker_path *emission_path)
     290              : {
     291         4148 :   emission_path->add_event
     292         4148 :     (std::make_unique<warning_event>
     293         4148 :      (loc_info,
     294              :       enode,
     295              :       sm, var, state,
     296         4148 :       get_final_state ()));
     297         4148 : }
     298              : 
     299              : } // namespace ana
     300              : 
     301              : #endif /* #if ENABLE_ANALYZER */
        

Generated by: LCOV version 2.5.0-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.