d: Merge upstream dmd 56589f0f4, druntime 651389b5, phobos 1516ecad9.
[official-gcc.git] / gcc / analyzer / pending-diagnostic.cc
blobeff050f675774bf454d742ff1bbb6a8f65d96231
1 /* Classes for analyzer diagnostics.
2 Copyright (C) 2019-2022 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tree.h"
25 #include "intl.h"
26 #include "diagnostic.h"
27 #include "function.h"
28 #include "json.h"
29 #include "analyzer/analyzer.h"
30 #include "diagnostic-event-id.h"
31 #include "analyzer/analyzer-logging.h"
32 #include "analyzer/sm.h"
33 #include "diagnostic-event-id.h"
34 #include "analyzer/sm.h"
35 #include "analyzer/pending-diagnostic.h"
36 #include "analyzer/diagnostic-manager.h"
37 #include "selftest.h"
38 #include "tristate.h"
39 #include "analyzer/call-string.h"
40 #include "analyzer/program-point.h"
41 #include "analyzer/store.h"
42 #include "analyzer/region-model.h"
43 #include "cpplib.h"
44 #include "digraph.h"
45 #include "ordered-hash-map.h"
46 #include "cfg.h"
47 #include "basic-block.h"
48 #include "gimple.h"
49 #include "gimple-iterator.h"
50 #include "cgraph.h"
51 #include "analyzer/supergraph.h"
52 #include "analyzer/program-state.h"
53 #include "alloc-pool.h"
54 #include "fibonacci_heap.h"
55 #include "shortest-paths.h"
56 #include "sbitmap.h"
57 #include "analyzer/exploded-graph.h"
58 #include "diagnostic-path.h"
59 #include "analyzer/checker-path.h"
61 #if ENABLE_ANALYZER
63 namespace ana {
65 /* struct interesting_t. */
67 /* Mark the creation of REG as being interesting. */
69 void
70 interesting_t::add_region_creation (const region *reg)
72 gcc_assert (reg);
73 m_region_creation.safe_push (reg);
76 void
77 interesting_t::dump_to_pp (pretty_printer *pp, bool simple) const
79 pp_string (pp, "{ region creation: [");
80 unsigned i;
81 const region *reg;
82 FOR_EACH_VEC_ELT (m_region_creation, i, reg)
84 if (i > 0)
85 pp_string (pp, ", ");
86 reg->dump_to_pp (pp, simple);
88 pp_string (pp, "]}");
91 /* Generate a label_text by printing FMT.
93 Use a clone of the global_dc for formatting callbacks.
95 Use this evdesc::event_desc's m_colorize flag to control colorization
96 (so that e.g. we can disable it for JSON output). */
98 label_text
99 evdesc::event_desc::formatted_print (const char *fmt, ...) const
101 pretty_printer *pp = global_dc->printer->clone ();
103 pp_show_color (pp) = m_colorize;
105 text_info ti;
106 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
107 va_list ap;
108 va_start (ap, fmt);
109 ti.format_spec = _(fmt);
110 ti.args_ptr = &ap;
111 ti.err_no = 0;
112 ti.x_data = NULL;
113 ti.m_richloc = &rich_loc;
114 pp_format (pp, &ti);
115 pp_output_formatted_text (pp);
116 va_end (ap);
118 label_text result = label_text::take (xstrdup (pp_formatted_text (pp)));
119 delete pp;
120 return result;
123 /* Return true if T1 and T2 are "the same" for the purposes of
124 diagnostic deduplication. */
126 bool
127 pending_diagnostic::same_tree_p (tree t1, tree t2)
129 return simple_cst_equal (t1, t2) == 1;
132 /* Return true iff IDENT is STR. */
134 static bool
135 ht_ident_eq (ht_identifier ident, const char *str)
137 return (strlen (str) == ident.len
138 && 0 == strcmp (str, (const char *)ident.str));
141 /* Return true if we should show the expansion location rather than unwind
142 within MACRO. */
144 static bool
145 fixup_location_in_macro_p (cpp_hashnode *macro)
147 ht_identifier ident = macro->ident;
148 /* Don't unwind inside <stdarg.h> macros, so that we don't suppress warnings
149 from them (due to being in system headers). */
150 if (ht_ident_eq (ident, "va_start")
151 || ht_ident_eq (ident, "va_copy")
152 || ht_ident_eq (ident, "va_arg")
153 || ht_ident_eq (ident, "va_end"))
154 return true;
155 return false;
158 /* Base implementation of pending_diagnostic::fixup_location.
159 Don't unwind inside macros for which fixup_location_in_macro_p is true. */
161 location_t
162 pending_diagnostic::fixup_location (location_t loc) const
164 if (linemap_location_from_macro_expansion_p (line_table, loc))
166 line_map *map
167 = const_cast <line_map *> (linemap_lookup (line_table, loc));
168 const line_map_macro *macro_map = linemap_check_macro (map);
169 if (fixup_location_in_macro_p (macro_map->macro))
170 loc = linemap_resolve_location (line_table, loc,
171 LRK_MACRO_EXPANSION_POINT, NULL);
173 return loc;
176 /* Base implementation of pending_diagnostic::add_call_event.
177 Add a call_event to EMISSION_PATH. */
179 void
180 pending_diagnostic::add_call_event (const exploded_edge &eedge,
181 checker_path *emission_path)
183 const exploded_node *src_node = eedge.m_src;
184 const program_point &src_point = src_node->get_point ();
185 const int src_stack_depth = src_point.get_stack_depth ();
186 const gimple *last_stmt = src_point.get_supernode ()->get_last_stmt ();
187 emission_path->add_event
188 (new call_event (eedge,
189 (last_stmt
190 ? last_stmt->location
191 : UNKNOWN_LOCATION),
192 src_point.get_fndecl (),
193 src_stack_depth));
196 } // namespace ana
198 #endif /* #if ENABLE_ANALYZER */