aboutsummaryrefslogtreecommitdiff
path: root/gcc/diagnostics/state-graphs.cc
blob: 5941c4138214558f8168176199424f7da630b5fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* Extensions to diagnostics::digraphs to support state graphs.
   Copyright (C) 2025 Free Software Foundation, Inc.
   Contributed by David Malcolm <dmalcolm@redhat.com>.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

GCC is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#define INCLUDE_ALGORITHM
#define INCLUDE_MAP
#define INCLUDE_SET
#define INCLUDE_STRING
#define INCLUDE_VECTOR
#include "config.h"
#include "system.h"
#include "coretypes.h"

#include "diagnostics/state-graphs.h"
#include "selftest.h"

using namespace diagnostics::state_graphs;

const char * const node_kind_strs[] = {
  "globals",
  "code",
  "function",
  "stack",
  "stack-frame",
  "heap",
  "thread-local",
  "dynalloc-buffer",
  "variable",
  "field",
  "padding",
  "element",
  "other",
};

const char *
diagnostics::state_graphs::node_kind_to_str (enum node_kind k)
{
  return node_kind_strs[static_cast<int> (k)];
}

// struct state_node_ref

enum node_kind
state_node_ref::get_node_kind () const
{
  const char *value = get_attr ("kind");
  if (!value)
    return node_kind::other;

  for (size_t i = 0; i < ARRAY_SIZE (node_kind_strs); ++i)
    if (!strcmp (node_kind_strs[i], value))
      return static_cast<enum node_kind> (i);

  return node_kind::other;
}

void
state_node_ref::set_node_kind (enum node_kind k)
{
  set_attr ("kind", node_kind_to_str (k));
}

const char * const dynalloc_state_strs[] = {
  "unknown",
  "nonnull",
  "unchecked",
  "freed"
};

enum node_dynalloc_state
state_node_ref::get_dynalloc_state () const
{
  const char *value = get_attr ("dynalloc-state");
  if (!value)
    return node_dynalloc_state::unknown;

  for (size_t i = 0; i < ARRAY_SIZE (dynalloc_state_strs); ++i)
    if (!strcmp (dynalloc_state_strs[i], value))
      return static_cast<enum node_dynalloc_state> (i);
  
  return node_dynalloc_state::unknown;
}

void
state_node_ref::set_dynalloc_state (enum node_dynalloc_state s) const
{
  set_attr ("dynalloc-state",
	    dynalloc_state_strs[static_cast <size_t> (s)]);
}

const char *
state_node_ref::get_dynamic_extents () const
{
  return m_node.get_attr (STATE_NODE_PREFIX, "dynamic-extents");
}

void
state_node_ref::set_json_attr (const char *key,
			       std::unique_ptr<json::value> value) const
{
  m_node.set_json_attr (STATE_NODE_PREFIX, key, std::move (value));
}

#if CHECKING_P

namespace diagnostics {
namespace selftest {

static void
test_node_attrs ()
{
  digraphs::digraph g;
  digraphs::node n (g, "a");
  state_node_ref node_ref (n);

  ASSERT_EQ (node_ref.get_node_kind (), node_kind::other);
  node_ref.set_node_kind (node_kind::stack);
  ASSERT_EQ (node_ref.get_node_kind (), node_kind::stack);

  ASSERT_EQ (node_ref.get_dynalloc_state (), node_dynalloc_state::unknown);
  node_ref.set_dynalloc_state (node_dynalloc_state::freed);
  ASSERT_EQ (node_ref.get_dynalloc_state (), node_dynalloc_state::freed);

  ASSERT_EQ (node_ref.get_type (), nullptr);
  node_ref.set_type ("const char *");
  ASSERT_STREQ (node_ref.get_type (), "const char *");
}

/* Run all of the selftests within this file.  */

void
state_graphs_cc_tests ()
{
  test_node_attrs ();
}

} // namespace diagnostics::selftest
} // namespace diagnostics

#endif /* CHECKING_P */