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