GCC Middle and Back End API Reference
diagnostic-state-graphs.h
Go to the documentation of this file.
1/* Extensions to diagnostics::digraphs to support state graphs.
2 Copyright (C) 2025 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21#ifndef GCC_DIAGNOSTIC_STATE_GRAPHS_H
22#define GCC_DIAGNOSTIC_STATE_GRAPHS_H
23
24#include "diagnostic-digraphs.h"
25#include "logical-location.h"
26
27/* diagnostics::digraphs provides support for directed graphs.
28
29 diagnostics::state_graphs provides a way to extend these graphs
30 for representing "state graphs" i.e. a representation of the state
31 of memory inside a program, for use e.g. by -fanalyzer.
32
33 Specifically, nodes represent memory regions, and we use property bags
34 in these nodes to stash extra properties (e.g. what kind of memory region
35 a node is e.g. stack vs heap). */
36
37class sarif_graph;
38namespace dot { class graph; }
39
40namespace diagnostics {
41namespace state_graphs {
42
43enum class node_kind
44{
45 // Memory regions
48 function, // code within a particular function
53
54 /* Dynamically-allocated buffer,
55 on heap or stack (depending on parent). */
57
59
60 field, // field within a struct or union
61 padding, // padding bits in a struct or union
62 element, // element within an array
63
64 other // anything else
65};
66
67extern const char *
69
77
78/* Prefixes to use in SARIF property bags. */
79#define STATE_GRAPH_PREFIX "gcc/diagnostic_state_graph/"
80#define STATE_NODE_PREFIX "gcc/diagnostic_state_node/"
81#define STATE_EDGE_PREFIX "gcc/diagnostic_state_edge/"
82
83/* A wrapper around a node that gets/sets attributes, using
84 the node's property bag for storage, so that the data roundtrips
85 through SARIF. */
86
88{
92
93 enum node_kind
94 get_node_kind () const;
95 void
97
98 // For node_kind::stack_frame, this will be the function
101 {
102 return m_node.get_logical_loc ();
103 }
104
105 // For node_kind::dynalloc_buffer
107 get_dynalloc_state () const;
108
109 void
111
112 const char *
113 get_dynamic_extents () const;
114
115 const char *
116 get_name () const { return get_attr ("name"); }
117 void
118 set_name (const char *name) const { set_attr ("name", name); }
119
120 const char *
121 get_type () const { return get_attr ("type"); }
122 void
123 set_type (const char *type) const { set_attr ("type", type); }
124
125 const char *
126 get_value () const { return get_attr ("value"); }
127
128 const char *
129 get_index () const { return get_attr ("index"); }
130
131 const char *
132 get_attr (const char *key) const
133 {
134 return m_node.get_attr (STATE_NODE_PREFIX, key);
135 }
136
137 void
138 set_attr (const char *key, const char *value) const
139 {
140 return m_node.set_attr (STATE_NODE_PREFIX, key, value);
141 }
142
143 void
144 set_json_attr (const char *key, std::unique_ptr<json::value> value) const;
145
147};
148
149extern std::unique_ptr<dot::graph>
151 const logical_location_manager &logical_loc_mgr);
152
153} // namespace state_graphs
154} // namespace diagnostics
155
156#endif /* ! GCC_DIAGNOSTIC_STATE_GRAPHS_H */
Definition diagnostic-digraphs.h:88
Definition diagnostic-digraphs.h:217
Definition logical-location.h:91
Definition diagnostic-format-sarif.h:155
#define STATE_NODE_PREFIX
Definition diagnostic-state-graphs.h:80
logical_location_manager::key logical_location
Definition logical-location.h:173
@ value
Definition logical-location.h:59
Definition diagnostic-state-graphs.h:41
const char * node_kind_to_str(enum node_kind)
Definition diagnostic-state-graphs.cc:56
node_dynalloc_state
Definition diagnostic-state-graphs.h:71
@ nonnull
Definition diagnostic-state-graphs.h:73
@ freed
Definition diagnostic-state-graphs.h:75
@ unchecked
Definition diagnostic-state-graphs.h:74
@ unknown
Definition diagnostic-state-graphs.h:72
std::unique_ptr< dot::graph > make_dot_graph(const diagnostics::digraphs::digraph &state_graph, const logical_location_manager &logical_loc_mgr)
Definition diagnostic-state-to-dot.cc:546
node_kind
Definition diagnostic-state-graphs.h:44
@ field
Definition diagnostic-state-graphs.h:60
@ globals
Definition diagnostic-state-graphs.h:46
@ heap_
Definition diagnostic-state-graphs.h:51
@ stack_frame
Definition diagnostic-state-graphs.h:50
@ other
Definition diagnostic-state-graphs.h:64
@ thread_local_
Definition diagnostic-state-graphs.h:52
@ dynalloc_buffer
Definition diagnostic-state-graphs.h:56
@ element
Definition diagnostic-state-graphs.h:62
@ padding
Definition diagnostic-state-graphs.h:61
@ code
Definition diagnostic-state-graphs.h:47
@ function
Definition diagnostic-state-graphs.h:48
@ variable
Definition diagnostic-state-graphs.h:58
@ stack
Definition diagnostic-state-graphs.h:49
Definition diagnostic-digraphs.h:35
Definition diagnostic-digraphs.h:33
const char * get_dynamic_extents() const
Definition diagnostic-state-graphs.cc:112
void set_node_kind(enum node_kind)
Definition diagnostic-state-graphs.cc:78
const char * get_value() const
Definition diagnostic-state-graphs.h:126
logical_location get_logical_loc() const
Definition diagnostic-state-graphs.h:100
void set_type(const char *type) const
Definition diagnostic-state-graphs.h:123
void set_json_attr(const char *key, std::unique_ptr< json::value > value) const
Definition diagnostic-state-graphs.cc:118
const char * get_type() const
Definition diagnostic-state-graphs.h:121
const char * get_attr(const char *key) const
Definition diagnostic-state-graphs.h:132
void set_name(const char *name) const
Definition diagnostic-state-graphs.h:118
enum node_kind get_node_kind() const
Definition diagnostic-state-graphs.cc:64
const char * get_name() const
Definition diagnostic-state-graphs.h:116
void set_attr(const char *key, const char *value) const
Definition diagnostic-state-graphs.h:138
state_node_ref(diagnostics::digraphs::node &node)
Definition diagnostic-state-graphs.h:89
diagnostics::digraphs::node & m_node
Definition diagnostic-state-graphs.h:146
const char * get_index() const
Definition diagnostic-state-graphs.h:129
void set_dynalloc_state(enum node_dynalloc_state) const
Definition diagnostic-state-graphs.cc:105
enum node_dynalloc_state get_dynalloc_state() const
Definition diagnostic-state-graphs.cc:91
Definition graphviz.h:160
Definition gengtype.h:252