Author: Eric Li Date: 2022-07-25T14:24:33-04:00 New Revision: 29d35ece8249f2d8a51437a5c008e6bf63da9874
URL: https://github.com/llvm/llvm-project/commit/29d35ece8249f2d8a51437a5c008e6bf63da9874 DIFF: https://github.com/llvm/llvm-project/commit/29d35ece8249f2d8a51437a5c008e6bf63da9874.diff LOG: [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 Added: Modified: clang/include/clang/Analysis/FlowSensitive/MapLattice.h clang/unittests/Analysis/FlowSensitive/MapLatticeTest.cpp Removed: ################################################################################ diff --git a/clang/include/clang/Analysis/FlowSensitive/MapLattice.h b/clang/include/clang/Analysis/FlowSensitive/MapLattice.h index 014cd60841ee7..16b0c978779a7 100644 --- a/clang/include/clang/Analysis/FlowSensitive/MapLattice.h +++ b/clang/include/clang/Analysis/FlowSensitive/MapLattice.h @@ -54,10 +54,13 @@ template <typename Key, typename ElementLattice> class MapLattice { // 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(); } diff --git a/clang/unittests/Analysis/FlowSensitive/MapLatticeTest.cpp b/clang/unittests/Analysis/FlowSensitive/MapLatticeTest.cpp index d3436e8f9496b..f6fe81e0dfc5f 100644 --- a/clang/unittests/Analysis/FlowSensitive/MapLatticeTest.cpp +++ b/clang/unittests/Analysis/FlowSensitive/MapLatticeTest.cpp @@ -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)))); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits