[clang][dataflow] Fix MapLattice::insert() to not drop return value

Fix `MapLattice` API to return `std::pair<iterator, bool>`,
allowing users to detect when an element has been inserted without
performing a redundant map lookup.

Differential Revision: https://reviews.llvm.org/D130497
This commit is contained in:
Eric Li 2022-07-25 12:18:53 -04:00
parent fb95b8dc35
commit 29d35ece82
2 changed files with 13 additions and 5 deletions

View File

@ -54,10 +54,13 @@ public:
// The `bottom` element is the empty map.
static MapLattice bottom() { return MapLattice(); }
void insert(const std::pair<const key_type, mapped_type> &P) { C.insert(P); }
std::pair<iterator, bool>
insert(const std::pair<const key_type, mapped_type> &P) {
return C.insert(P);
}
void insert(std::pair<const key_type, mapped_type> &&P) {
C.insert(std::move(P));
std::pair<iterator, bool> insert(std::pair<const key_type, mapped_type> &&P) {
return C.insert(std::move(P));
}
unsigned size() const { return C.size(); }

View File

@ -50,13 +50,18 @@ static constexpr int Key1 = 0;
static constexpr int Key2 = 1;
namespace {
using ::testing::_;
using ::testing::Pair;
using ::testing::UnorderedElementsAre;
TEST(MapLatticeTest, InsertWorks) {
MapLattice<int, BooleanLattice> Lattice;
Lattice.insert({Key1, BooleanLattice(false)});
Lattice.insert({Key2, BooleanLattice(false)});
EXPECT_THAT(Lattice.insert({Key1, BooleanLattice(false)}), Pair(_, true));
EXPECT_THAT(Lattice.insert({Key2, BooleanLattice(false)}), Pair(_, true));
// Insertion fails on collision.
EXPECT_THAT(Lattice.insert({Key1, BooleanLattice(false)}), Pair(_, false));
EXPECT_THAT(Lattice.insert({Key2, BooleanLattice(false)}), Pair(_, false));
EXPECT_THAT(Lattice, UnorderedElementsAre(Pair(Key1, BooleanLattice(false)),
Pair(Key2, BooleanLattice(false))));