li.zhe.hua created this revision. li.zhe.hua added a reviewer: ymandel. Herald added subscribers: martong, tschuett, xazax.hun. Herald added a reviewer: NoQ. Herald added a project: All. li.zhe.hua requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
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. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D130497 Files: clang/include/clang/Analysis/FlowSensitive/MapLattice.h clang/unittests/Analysis/FlowSensitive/MapLatticeTest.cpp Index: clang/unittests/Analysis/FlowSensitive/MapLatticeTest.cpp =================================================================== --- clang/unittests/Analysis/FlowSensitive/MapLatticeTest.cpp +++ clang/unittests/Analysis/FlowSensitive/MapLatticeTest.cpp @@ -50,13 +50,18 @@ 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)))); Index: clang/include/clang/Analysis/FlowSensitive/MapLattice.h =================================================================== --- clang/include/clang/Analysis/FlowSensitive/MapLattice.h +++ clang/include/clang/Analysis/FlowSensitive/MapLattice.h @@ -54,10 +54,13 @@ // 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(); }
Index: clang/unittests/Analysis/FlowSensitive/MapLatticeTest.cpp =================================================================== --- clang/unittests/Analysis/FlowSensitive/MapLatticeTest.cpp +++ clang/unittests/Analysis/FlowSensitive/MapLatticeTest.cpp @@ -50,13 +50,18 @@ 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)))); Index: clang/include/clang/Analysis/FlowSensitive/MapLattice.h =================================================================== --- clang/include/clang/Analysis/FlowSensitive/MapLattice.h +++ clang/include/clang/Analysis/FlowSensitive/MapLattice.h @@ -54,10 +54,13 @@ // 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(); }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits