1 /* Classes for analyzer diagnostics.
2 Copyright (C) 2019-2021 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)
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 #ifndef GCC_ANALYZER_PENDING_DIAGNOSTIC_H
22 #define GCC_ANALYZER_PENDING_DIAGNOSTIC_H
26 /* Various bundles of information used for generating more precise
27 messages for events within a diagnostic_path, for passing to the
28 various "describe_*" vfuncs of pending_diagnostic. See those
29 for more information. */
35 event_desc (bool colorize
) : m_colorize (colorize
) {}
37 label_text
formatted_print (const char *fmt
, ...) const
38 ATTRIBUTE_GCC_DIAG(2,3);
43 /* For use by pending_diagnostic::describe_state_change. */
45 struct state_change
: public event_desc
47 state_change (bool colorize
,
50 state_machine::state_t old_state
,
51 state_machine::state_t new_state
,
52 diagnostic_event_id_t event_id
,
53 const state_change_event
&event
)
54 : event_desc (colorize
),
55 m_expr (expr
), m_origin (origin
),
56 m_old_state (old_state
), m_new_state (new_state
),
57 m_event_id (event_id
), m_event (event
)
60 bool is_global_p () const { return m_expr
== NULL_TREE
; }
64 state_machine::state_t m_old_state
;
65 state_machine::state_t m_new_state
;
66 diagnostic_event_id_t m_event_id
;
67 const state_change_event
&m_event
;
70 /* For use by pending_diagnostic::describe_call_with_state. */
72 struct call_with_state
: public event_desc
74 call_with_state (bool colorize
,
75 tree caller_fndecl
, tree callee_fndecl
,
76 tree expr
, state_machine::state_t state
)
77 : event_desc (colorize
),
78 m_caller_fndecl (caller_fndecl
),
79 m_callee_fndecl (callee_fndecl
),
88 state_machine::state_t m_state
;
91 /* For use by pending_diagnostic::describe_return_of_state. */
93 struct return_of_state
: public event_desc
95 return_of_state (bool colorize
,
96 tree caller_fndecl
, tree callee_fndecl
,
97 state_machine::state_t state
)
98 : event_desc (colorize
),
99 m_caller_fndecl (caller_fndecl
),
100 m_callee_fndecl (callee_fndecl
),
105 tree m_caller_fndecl
;
106 tree m_callee_fndecl
;
107 state_machine::state_t m_state
;
110 /* For use by pending_diagnostic::describe_final_event. */
112 struct final_event
: public event_desc
114 final_event (bool colorize
,
115 tree expr
, state_machine::state_t state
)
116 : event_desc (colorize
),
117 m_expr (expr
), m_state (state
)
121 state_machine::state_t m_state
;
124 } /* end of namespace evdesc */
126 /* An abstract base class for capturing information about a diagnostic in
127 a form that is ready to emit at a later point (or be rejected).
128 Each kind of diagnostic will have a concrete subclass of
131 Normally, gcc diagnostics are emitted using va_list, which can't be
132 portably stored for later use, so we have to use an "emit" virtual
135 This class also supports comparison, so that multiple pending_diagnostic
136 instances can be de-duplicated.
138 As well as emitting a diagnostic, the class has various "precision of
139 wording" virtual functions, for generating descriptions for events
140 within a diagnostic_path. These are optional, but implementing these
141 allows for more precise wordings than the more generic
144 class pending_diagnostic
147 virtual ~pending_diagnostic () {}
149 /* Vfunc for emitting the diagnostic. The rich_location will have been
150 populated with a diagnostic_path.
151 Return true if a diagnostic is actually emitted. */
152 virtual bool emit (rich_location
*) = 0;
154 /* Hand-coded RTTI: get an ID for the subclass. */
155 virtual const char *get_kind () const = 0;
157 /* A vfunc for identifying "use of uninitialized value". */
158 virtual bool use_of_uninit_p () const { return false; }
160 /* Compare for equality with OTHER, which might be of a different
163 bool equal_p (const pending_diagnostic
&other
) const
165 /* Check for pointer equality on the IDs from get_kind. */
166 if (get_kind () != other
.get_kind ())
168 /* Call vfunc now we know they have the same ID: */
169 return subclass_equal_p (other
);
172 /* A vfunc for testing for equality, where we've already
173 checked they have the same ID. See pending_diagnostic_subclass
174 below for a convenience subclass for implementing this. */
175 virtual bool subclass_equal_p (const pending_diagnostic
&other
) const = 0;
177 /* Return true if T1 and T2 are "the same" for the purposes of
178 diagnostic deduplication. */
179 static bool same_tree_p (tree t1
, tree t2
);
181 /* A vfunc for fixing up locations (both the primary location for the
182 diagnostic, and for events in their paths), e.g. to avoid unwinding
183 inside specific macros. */
184 virtual location_t
fixup_location (location_t loc
) const
189 /* For greatest precision-of-wording, the various following "describe_*"
190 virtual functions give the pending diagnostic a way to describe events
191 in a diagnostic_path in terms that make sense for that diagnostic.
193 In each case, return a non-NULL label_text to give the event a custom
194 description; NULL otherwise (falling back on a more generic
197 /* Precision-of-wording vfunc for describing a critical state change
198 within the diagnostic_path.
200 For example, a double-free diagnostic might use the descriptions:
201 - "first 'free' happens here"
202 - "second 'free' happens here"
203 for the pertinent events, whereas a use-after-free might use the
206 - "use after free here"
207 Note how in both cases the first event is a "free": the best
208 description to use depends on the diagnostic. */
210 virtual label_text
describe_state_change (const evdesc::state_change
&)
212 /* Default no-op implementation. */
213 return label_text ();
216 /* Precision-of-wording vfunc for describing an interprocedural call
217 carrying critial state for the diagnostic, from caller to callee.
219 For example a double-free diagnostic might use:
220 - "passing freed pointer 'ptr' in call to 'deallocator' from 'test'"
221 to make it clearer how the freed value moves from caller to
224 virtual label_text
describe_call_with_state (const evdesc::call_with_state
&)
226 /* Default no-op implementation. */
227 return label_text ();
230 /* Precision-of-wording vfunc for describing an interprocedural return
231 within the diagnostic_path that carries critial state for the
232 diagnostic, from callee back to caller.
234 For example, a deref-of-unchecked-malloc diagnostic might use:
235 - "returning possibly-NULL pointer to 'make_obj' from 'allocator'"
236 to make it clearer how the unchecked value moves from callee
239 virtual label_text
describe_return_of_state (const evdesc::return_of_state
&)
241 /* Default no-op implementation. */
242 return label_text ();
245 /* Precision-of-wording vfunc for describing the final event within a
248 For example a double-free diagnostic might use:
249 - "second 'free' here; first 'free' was at (3)"
250 and a use-after-free might use
251 - "use after 'free' here; memory was freed at (2)". */
253 virtual label_text
describe_final_event (const evdesc::final_event
&)
255 /* Default no-op implementation. */
256 return label_text ();
259 /* End of precision-of-wording vfuncs. */
261 /* Vfunc for extending/overriding creation of the events for an
262 exploded_edge that corresponds to a superedge, allowing for custom
263 events to be created that are pertinent to a particular
264 pending_diagnostic subclass.
266 For example, the -Wanalyzer-stale-setjmp-buffer diagnostic adds a
267 custom event showing when the pertinent stack frame is popped
268 (and thus the point at which the jmp_buf becomes invalid). */
270 virtual bool maybe_add_custom_events_for_superedge (const exploded_edge
&,
276 /* Vfunc for determining that this pending_diagnostic supercedes OTHER,
277 and that OTHER should therefore not be emitted.
278 They have already been tested for being at the same stmt. */
281 supercedes_p (const pending_diagnostic
&other ATTRIBUTE_UNUSED
) const
287 /* A template to make it easier to make subclasses of pending_diagnostic.
289 This uses the curiously-recurring template pattern, to implement
290 pending_diagnostic::subclass_equal_p by casting and calling
293 This assumes that BASE_OTHER has already been checked to have
294 been of the same subclass (which pending_diagnostic::equal_p does). */
296 template <class Subclass
>
297 class pending_diagnostic_subclass
: public pending_diagnostic
300 bool subclass_equal_p (const pending_diagnostic
&base_other
) const
303 const Subclass
&other
= (const Subclass
&)base_other
;
304 return *(const Subclass
*)this == other
;
310 #endif /* GCC_ANALYZER_PENDING_DIAGNOSTIC_H */