Line data Source code
1 : // Copyright (C) 2020-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-privacy-ctx.h"
20 : #include "selftest.h"
21 :
22 : namespace Rust {
23 : namespace Privacy {
24 :
25 : static ReachLevel
26 4077 : insert_if_higher (ReachLevel new_level,
27 : std::unordered_map<DefId, ReachLevel>::iterator &existing)
28 : {
29 0 : if (new_level > existing->second)
30 65 : existing->second = new_level;
31 :
32 4077 : return existing->second;
33 : }
34 :
35 : ReachLevel
36 24613 : PrivacyContext::update_reachability (const Analysis::NodeMapping &mapping,
37 : ReachLevel reach)
38 : {
39 24613 : auto def_id = mapping.get_defid ();
40 24613 : auto existing_reach = reachability_map.find (def_id);
41 24613 : if (existing_reach != reachability_map.end ())
42 4142 : return insert_if_higher (reach, existing_reach);
43 :
44 20536 : reachability_map.insert ({def_id, reach});
45 20536 : return reach;
46 : }
47 :
48 : const ReachLevel *
49 4 : PrivacyContext::lookup_reachability (const Analysis::NodeMapping &mapping)
50 : {
51 4 : auto existing_reach = reachability_map.find (mapping.get_defid ());
52 4 : if (existing_reach == reachability_map.end ())
53 : return nullptr;
54 :
55 4 : return &existing_reach->second;
56 : }
57 : } // namespace Privacy
58 : } // namespace Rust
59 :
60 : #if CHECKING_P
61 : namespace selftest {
62 : static void
63 1 : update_reachability_test (void)
64 : {
65 1 : auto ctx = Rust::Privacy::PrivacyContext ();
66 : // Bogus values for the mappings
67 1 : auto mapping = Rust::Analysis::NodeMapping (15, 15, 15, 15);
68 :
69 1 : auto new_level
70 1 : = ctx.update_reachability (mapping, Rust::Privacy::ReachLevel::Unreachable);
71 :
72 1 : ASSERT_EQ (new_level, Rust::Privacy::ReachLevel::Unreachable);
73 :
74 1 : ASSERT_TRUE (ctx.lookup_reachability (mapping));
75 1 : ASSERT_EQ (*ctx.lookup_reachability (mapping),
76 : Rust::Privacy::ReachLevel::Unreachable);
77 :
78 1 : new_level
79 1 : = ctx.update_reachability (mapping, Rust::Privacy::ReachLevel::Reachable);
80 :
81 1 : ASSERT_EQ (new_level, Rust::Privacy::ReachLevel::Reachable);
82 1 : ASSERT_TRUE (ctx.lookup_reachability (mapping));
83 1 : ASSERT_EQ (*ctx.lookup_reachability (mapping),
84 : Rust::Privacy::ReachLevel::Reachable);
85 1 : }
86 :
87 : void
88 1 : rust_privacy_ctx_test (void)
89 : {
90 1 : update_reachability_test ();
91 1 : }
92 : } // namespace selftest
93 : #endif // !CHECKING_P
|