1 // Copyright (C) 2020-2022 Free Software Foundation, Inc.
3 // This file is part of GCC.
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
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
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/>.
19 #include "rust-privacy-ctx.h"
26 insert_if_higher (ReachLevel new_level
,
27 std::unordered_map
<DefId
, ReachLevel
>::iterator
&existing
)
29 if (new_level
> existing
->second
)
30 existing
->second
= new_level
;
32 return existing
->second
;
36 PrivacyContext::update_reachability (const Analysis::NodeMapping
&mapping
,
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
);
44 reachability_map
.insert ({def_id
, reach
});
49 PrivacyContext::lookup_reachability (const Analysis::NodeMapping
&mapping
)
51 auto existing_reach
= reachability_map
.find (mapping
.get_defid ());
52 if (existing_reach
== reachability_map
.end ())
55 return &existing_reach
->second
;
57 } // namespace Privacy
63 update_reachability_test (void)
65 auto ctx
= Rust::Privacy::PrivacyContext ();
66 // Bogus values for the mappings
67 auto mapping
= Rust::Analysis::NodeMapping (15, 15, 15, 15);
70 = ctx
.update_reachability (mapping
, Rust::Privacy::ReachLevel::Unreachable
);
72 ASSERT_EQ (new_level
, Rust::Privacy::ReachLevel::Unreachable
);
74 ASSERT_TRUE (ctx
.lookup_reachability (mapping
));
75 ASSERT_EQ (*ctx
.lookup_reachability (mapping
),
76 Rust::Privacy::ReachLevel::Unreachable
);
79 = ctx
.update_reachability (mapping
, Rust::Privacy::ReachLevel::Reachable
);
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
);
88 rust_privacy_ctx_test (void)
90 update_reachability_test ();
92 } // namespace selftest