1 /* Classes for representing locations within the program.
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_PROGRAM_POINT_H
22 #define GCC_ANALYZER_PROGRAM_POINT_H
28 /* An enum for distinguishing the various kinds of program_point. */
31 /* A "fake" node which has edges to all entrypoints. */
38 /* Special values used for hash_map: */
45 extern const char *point_kind_to_string (enum point_kind pk
);
50 format (bool newlines
) : m_newlines (newlines
) {}
52 void spacer (pretty_printer
*pp
) const
63 /* A class for representing a location within the program, without
64 interprocedural information.
66 This represents a fine-grained location within the supergraph (or
67 within one of its nodes). */
72 function_point (const supernode
*supernode
,
73 const superedge
*from_edge
,
75 enum point_kind kind
);
77 void print (pretty_printer
*pp
, const format
&f
) const;
78 void print_source_line (pretty_printer
*pp
) const;
81 hashval_t
hash () const;
82 bool operator== (const function_point
&other
) const
84 return (m_supernode
== other
.m_supernode
85 && m_from_edge
== other
.m_from_edge
86 && m_stmt_idx
== other
.m_stmt_idx
87 && m_kind
== other
.m_kind
);
92 const supernode
*get_supernode () const { return m_supernode
; }
93 function
*get_function () const;
94 const gimple
*get_stmt () const;
95 location_t
get_location () const;
96 enum point_kind
get_kind () const { return m_kind
; }
97 const superedge
*get_from_edge () const
101 unsigned get_stmt_idx () const
103 gcc_assert (m_kind
== PK_BEFORE_STMT
);
107 /* Factory functions for making various kinds of program_point. */
109 static function_point
from_function_entry (const supergraph
&sg
,
112 static function_point
before_supernode (const supernode
*supernode
,
113 const superedge
*from_edge
);
115 static function_point
before_stmt (const supernode
*supernode
,
118 return function_point (supernode
, NULL
, stmt_idx
, PK_BEFORE_STMT
);
121 static function_point
after_supernode (const supernode
*supernode
)
123 return function_point (supernode
, NULL
, 0, PK_AFTER_SUPERNODE
);
126 /* Support for hash_map. */
128 static function_point
empty ()
130 return function_point (NULL
, NULL
, 0, PK_EMPTY
);
132 static function_point
deleted ()
134 return function_point (NULL
, NULL
, 0, PK_DELETED
);
137 static int cmp_within_supernode_1 (const function_point
&point_a
,
138 const function_point
&point_b
);
139 static int cmp_within_supernode (const function_point
&point_a
,
140 const function_point
&point_b
);
141 static int cmp (const function_point
&point_a
,
142 const function_point
&point_b
);
143 static int cmp_ptr (const void *p1
, const void *p2
);
145 /* For before_stmt, go to next stmt. */
149 const supernode
*m_supernode
;
151 /* For PK_BEFORE_SUPERNODE, and only for CFG edges. */
152 const superedge
*m_from_edge
;
154 /* Only for PK_BEFORE_STMT. */
157 enum point_kind m_kind
;
160 /* A class for representing a location within the program, including
161 interprocedural information.
163 This represents a fine-grained location within the supergraph (or
164 within one of its nodes), along with a call string giving the
165 interprocedural context. */
170 program_point (const function_point
&fn_point
,
171 const call_string
&call_string
)
172 : m_function_point (fn_point
),
173 m_call_string (call_string
)
177 void print (pretty_printer
*pp
, const format
&f
) const;
178 void print_source_line (pretty_printer
*pp
) const;
181 json::object
*to_json () const;
183 hashval_t
hash () const;
184 bool operator== (const program_point
&other
) const
186 return (m_function_point
== other
.m_function_point
187 && m_call_string
== other
.m_call_string
);
189 bool operator!= (const program_point
&other
) const
191 return !(*this == other
);
196 const function_point
&get_function_point () const { return m_function_point
; }
197 const call_string
&get_call_string () const { return m_call_string
; }
199 const supernode
*get_supernode () const
201 return m_function_point
.get_supernode ();
203 function
*get_function () const
205 return m_function_point
.get_function ();
207 function
*get_function_at_depth (unsigned depth
) const;
208 tree
get_fndecl () const
210 gcc_assert (get_kind () != PK_ORIGIN
);
211 return get_function ()->decl
;
213 const gimple
*get_stmt () const
215 return m_function_point
.get_stmt ();
217 location_t
get_location () const
219 return m_function_point
.get_location ();
221 enum point_kind
get_kind () const
223 return m_function_point
.get_kind ();
225 const superedge
*get_from_edge () const
227 return m_function_point
.get_from_edge ();
229 unsigned get_stmt_idx () const
231 return m_function_point
.get_stmt_idx ();
234 /* Get the number of frames we expect at this program point.
235 This will be one more than the length of the call_string
236 (which stores the parent callsites), apart from the origin
237 node, which doesn't have any frames. */
238 int get_stack_depth () const
240 if (get_kind () == PK_ORIGIN
)
242 return m_call_string
.length () + 1;
245 /* Factory functions for making various kinds of program_point. */
246 static program_point
origin ()
248 return program_point (function_point (NULL
, NULL
,
253 static program_point
from_function_entry (const supergraph
&sg
,
256 return program_point (function_point::from_function_entry (sg
, fun
),
260 static program_point
before_supernode (const supernode
*supernode
,
261 const superedge
*from_edge
,
262 const call_string
&call_string
)
264 return program_point (function_point::before_supernode (supernode
,
269 static program_point
before_stmt (const supernode
*supernode
,
271 const call_string
&call_string
)
273 return program_point (function_point::before_stmt (supernode
, stmt_idx
),
277 static program_point
after_supernode (const supernode
*supernode
,
278 const call_string
&call_string
)
280 return program_point (function_point::after_supernode (supernode
),
284 /* Support for hash_map. */
286 static program_point
empty ()
288 return program_point (function_point::empty (), call_string ());
290 static program_point
deleted ()
292 return program_point (function_point::deleted (), call_string ());
295 bool on_edge (exploded_graph
&eg
, const superedge
*succ
);
297 void validate () const;
299 /* For before_stmt, go to next stmt. */
300 void next_stmt () { m_function_point
.next_stmt (); }
302 program_point
get_next () const;
305 function_point m_function_point
;
306 call_string m_call_string
;
311 #endif /* GCC_ANALYZER_PROGRAM_POINT_H */