Line data Source code
1 : // Copyright (C) 2025-2026 Free Software Foundation, Inc.
2 :
3 : // This file is part of GCC.
4 :
5 : // GCC is free software; you can redistribute it and/or modify it under
6 : // the terms of the GNU General Public License as published by the Free
7 : // Software Foundation; either version 3, or (at your option) any later
8 : // version.
9 :
10 : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : // for more details.
14 :
15 : // You should have received a copy of the GNU General Public License
16 : // along with GCC; see the file COPYING3. If not see
17 : // <http://www.gnu.org/licenses/>.
18 :
19 : #include "rust-unused-context.h"
20 :
21 : namespace Rust {
22 : namespace Analysis {
23 :
24 : void
25 10 : UnusedContext::add_variable (HirId id)
26 :
27 : {
28 10 : used_vars.emplace (id);
29 10 : }
30 :
31 : bool
32 12 : UnusedContext::is_variable_used (HirId id) const
33 : {
34 12 : return used_vars.find (id) != used_vars.end ();
35 : }
36 :
37 : void
38 9 : UnusedContext::add_assign (HirId id_def, HirId id)
39 : {
40 9 : assigned_vars[id_def].push_back (id);
41 9 : }
42 :
43 : void
44 10 : UnusedContext::remove_assign (HirId id_def)
45 : {
46 10 : if (assigned_vars.find (id_def) != assigned_vars.end ())
47 2 : assigned_vars[id_def].pop_back ();
48 10 : }
49 :
50 : bool
51 9 : UnusedContext::is_variable_assigned (HirId id_def, HirId id)
52 : {
53 9 : auto assigned_vec = assigned_vars[id_def];
54 18 : return std::find (assigned_vec.begin (), assigned_vec.end (), id)
55 18 : != assigned_vec.end ();
56 9 : }
57 :
58 : void
59 5 : UnusedContext::add_mut (HirId id)
60 : {
61 5 : mutable_vars.emplace (id);
62 5 : }
63 :
64 : void
65 9 : UnusedContext::remove_mut (HirId id)
66 : {
67 9 : mutable_vars.erase (id);
68 9 : }
69 :
70 : bool
71 5 : UnusedContext::is_mut_used (HirId id) const
72 : {
73 5 : return mutable_vars.find (id) == mutable_vars.end ();
74 : }
75 :
76 : void
77 2 : UnusedContext::add_label (HirId id)
78 :
79 : {
80 2 : used_labels.emplace (id);
81 2 : }
82 :
83 : bool
84 4 : UnusedContext::is_label_used (HirId id) const
85 : {
86 4 : return used_labels.find (id) != used_labels.end ();
87 : }
88 :
89 : std::string
90 0 : UnusedContext::as_string () const
91 : {
92 0 : std::stringstream ss;
93 0 : ss << "UnusedContext: ";
94 0 : for (const auto &v : used_vars)
95 : {
96 0 : ss << "HirId: " << v << "\n";
97 : }
98 0 : return ss.str ();
99 0 : }
100 :
101 : } // namespace Analysis
102 : } // namespace Rust
|