From 29d35ece8249f2d8a51437a5c008e6bf63da9874 Mon Sep 17 00:00:00 2001
From: Eric Li
Date: Mon, 25 Jul 2022 12:18:53 -0400
Subject: [PATCH] [clang][dataflow] Fix MapLattice::insert() to not drop return
value
Fix `MapLattice` API to return `std::pair`,
allowing users to detect when an element has been inserted without
performing a redundant map lookup.
Differential Revision: https://reviews.llvm.org/D130497
---
clang/include/clang/Analysis/FlowSensitive/MapLattice.h | 9 ++++++---
clang/unittests/Analysis/FlowSensitive/MapLatticeTest.cpp | 9 +++++++--
2 files changed, 13 insertions(+), 5 deletions(-)
diff --git a/clang/include/clang/Analysis/FlowSensitive/MapLattice.h b/clang/include/clang/Analysis/FlowSensitive/MapLattice.h
index 014cd60..16b0c97 100644
--- a/clang/include/clang/Analysis/FlowSensitive/MapLattice.h
+++ b/clang/include/clang/Analysis/FlowSensitive/MapLattice.h
@@ -54,10 +54,13 @@ public:
// The `bottom` element is the empty map.
static MapLattice bottom() { return MapLattice(); }
- void insert(const std::pair &P) { C.insert(P); }
+ std::pair
+ insert(const std::pair &P) {
+ return C.insert(P);
+ }
- void insert(std::pair &&P) {
- C.insert(std::move(P));
+ std::pair insert(std::pair &&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 d3436e8..f6fe81e 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 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))));
--
2.7.4