[PATCH] D143976: [ADT] Add lookupOrTrap method to DenseMap

Xiangxi Guo (Ryan) via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 13 20:37:55 PST 2023


StrongerXi created this revision.
StrongerXi added reviewers: lattner, Mogball, bzcheeseman.
Herald added a project: All.
StrongerXi requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Make it easier for users when they want to use validated
DenseMap::lookup as a composable C++ expression. For instance:

  c++
  // instead of
  if (auto val = map.lookup(key, "..."))
     return val;
  assert("...");
  
  // we can write
  return map.lookupOrTrap(key, "...");

Note that the error message is optional.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D143976

Files:
  llvm/include/llvm/ADT/DenseMap.h
  llvm/unittests/ADT/DenseMapTest.cpp


Index: llvm/unittests/ADT/DenseMapTest.cpp
===================================================================
--- llvm/unittests/ADT/DenseMapTest.cpp
+++ llvm/unittests/ADT/DenseMapTest.cpp
@@ -125,6 +125,13 @@
   EXPECT_TRUE(this->Map.find(this->getKey()) == this->Map.end());
   EXPECT_EQ(typename TypeParam::mapped_type(),
             this->Map.lookup(this->getKey()));
+
+  // LookupOrTrap tests
+  EXPECT_DEATH({ this->Map.lookupOrTrap(this->getKey()); },
+               "\\[DenseMap::lookupOrTrap\\] key not found;");
+  EXPECT_DEATH(
+      { this->Map.lookupOrTrap(this->getKey(), "clarification message 1"); },
+      "\\[DenseMap::lookupOrTrap\\] key not found; clarification message 1");
 }
 
 // Constant map tests
@@ -156,6 +163,13 @@
   EXPECT_TRUE(this->Map.find(this->getKey()) == this->Map.begin());
   EXPECT_EQ(this->getValue(), this->Map.lookup(this->getKey()));
   EXPECT_EQ(this->getValue(), this->Map[this->getKey()]);
+
+  // LookupOrTrap tests
+  EXPECT_DEATH({ this->Map.lookupOrTrap(this->getKey(1)); },
+               "\\[DenseMap::lookupOrTrap\\] key not found;");
+  EXPECT_DEATH(
+      { this->Map.lookupOrTrap(this->getKey(1), "clarification message 2"); },
+      "\\[DenseMap::lookupOrTrap\\] key not found; clarification message 2");
 }
 
 // Test clear() method
Index: llvm/include/llvm/ADT/DenseMap.h
===================================================================
--- llvm/include/llvm/ADT/DenseMap.h
+++ llvm/include/llvm/ADT/DenseMap.h
@@ -16,8 +16,10 @@
 
 #include "llvm/ADT/DenseMapInfo.h"
 #include "llvm/ADT/EpochTracker.h"
+#include "llvm/ADT/Twine.h"
 #include "llvm/Support/AlignOf.h"
 #include "llvm/Support/Compiler.h"
+#include "llvm/Support/Debug.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/MemAlloc.h"
 #include "llvm/Support/ReverseIteration.h"
@@ -201,6 +203,16 @@
     return ValueT();
   }
 
+  /// lookupOrTrap - Return the entry for the specified key, or abort with given
+  /// message if no such entry exists.
+  ValueT lookupOrTrap(const_arg_type_t<KeyT> Val, const Twine &Msg = "") const {
+    auto Iter = this->find(std::move(Val));
+    if (Iter != this->end())
+      return Iter->second;
+    dbgs() << "[DenseMap::lookupOrTrap] key not found; " << Msg << "\n";
+    std::abort();
+  }
+
   // Inserts key,value pair into the map if the key isn't already in the map.
   // If the key is already in the map, it returns false and doesn't update the
   // value.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143976.497190.patch
Type: text/x-patch
Size: 2458 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230214/aef0542e/attachment.bin>


More information about the llvm-commits mailing list