[PATCH] D60291: [llvm] Add isa_and_nonnull

Phabricator via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 5 06:58:07 PDT 2019


This revision was automatically updated to reflect the committed changes.
Closed by commit rL357761: [llvm] Add isa_and_nonnull (authored by dhinton, committed by ).
Herald added a subscriber: kristina.

Changed prior to commit:
  https://reviews.llvm.org/D60291?vs=193803&id=193864#toc

Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D60291/new/

https://reviews.llvm.org/D60291

Files:
  llvm/trunk/docs/ProgrammersManual.rst
  llvm/trunk/include/llvm/Support/Casting.h
  llvm/trunk/unittests/Support/Casting.cpp


Index: llvm/trunk/unittests/Support/Casting.cpp
===================================================================
--- llvm/trunk/unittests/Support/Casting.cpp
+++ llvm/trunk/unittests/Support/Casting.cpp
@@ -118,6 +118,12 @@
   EXPECT_TRUE(isa<foo>(B4));
 }
 
+TEST(CastingTest, isa_and_nonnull) {
+  EXPECT_TRUE(isa_and_nonnull<foo>(B2));
+  EXPECT_TRUE(isa_and_nonnull<foo>(B4));
+  EXPECT_FALSE(isa_and_nonnull<foo>(fub()));
+}
+
 TEST(CastingTest, cast) {
   foo &F1 = cast<foo>(B1);
   EXPECT_NE(&F1, null_foo);
Index: llvm/trunk/docs/ProgrammersManual.rst
===================================================================
--- llvm/trunk/docs/ProgrammersManual.rst
+++ llvm/trunk/docs/ProgrammersManual.rst
@@ -164,6 +164,12 @@
   efficient to use the ``InstVisitor`` class to dispatch over the instruction
   type directly.
 
+``isa_and_nonnull<>``:
+  The ``isa_and_nonnull<>`` operator works just like the ``isa<>`` operator,
+  except that it allows for a null pointer as an argument (which it then
+  returns false).  This can sometimes be useful, allowing you to combine several
+  null checks into one.
+
 ``cast_or_null<>``:
   The ``cast_or_null<>`` operator works just like the ``cast<>`` operator,
   except that it allows for a null pointer as an argument (which it then
Index: llvm/trunk/include/llvm/Support/Casting.h
===================================================================
--- llvm/trunk/include/llvm/Support/Casting.h
+++ llvm/trunk/include/llvm/Support/Casting.h
@@ -143,6 +143,16 @@
                        typename simplify_type<const Y>::SimpleType>::doit(Val);
 }
 
+// isa_and_nonnull<X> - Functionally identical to isa, except that a null value
+// is accepted.
+//
+template <class X, class Y>
+LLVM_NODISCARD inline bool isa_and_nonnull(const Y &Val) {
+  if (!Val)
+    return false;
+  return isa<X>(Val);
+}
+
 //===----------------------------------------------------------------------===//
 //                          cast<x> Support Templates
 //===----------------------------------------------------------------------===//


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D60291.193864.patch
Type: text/x-patch
Size: 2078 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190405/962983e5/attachment.bin>


More information about the llvm-commits mailing list