diff options
| author | Don Hinton <hintonda@gmail.com> | 2019-04-05 13:59:24 +0000 |
|---|---|---|
| committer | Don Hinton <hintonda@gmail.com> | 2019-04-05 13:59:24 +0000 |
| commit | 629daef4baee7ed74d7de5ad644576271a7a6c15 (patch) | |
| tree | bb42a6860b13275963bf72995bd2ae377d76b574 /llvm | |
| parent | 50a8652785226b6ae9de0f51d2f2415268f42c50 (diff) | |
| download | bcm5719-llvm-629daef4baee7ed74d7de5ad644576271a7a6c15.tar.gz bcm5719-llvm-629daef4baee7ed74d7de5ad644576271a7a6c15.zip | |
[llvm] Add isa_and_nonnull
Summary:
Add new ``isa_and_nonnull<>`` operator that works just like
the ``isa<>`` operator, except that it allows for a null pointer as an
argument (which it then returns false).
Reviewers: lattner, aaron.ballman, greened
Reviewed By: lattner
Subscribers: hubert.reinterpretcast, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60291
llvm-svn: 357761
Diffstat (limited to 'llvm')
| -rw-r--r-- | llvm/docs/ProgrammersManual.rst | 6 | ||||
| -rw-r--r-- | llvm/include/llvm/Support/Casting.h | 10 | ||||
| -rw-r--r-- | llvm/unittests/Support/Casting.cpp | 6 |
3 files changed, 22 insertions, 0 deletions
diff --git a/llvm/docs/ProgrammersManual.rst b/llvm/docs/ProgrammersManual.rst index adb23bd5ac2..75b6239375a 100644 --- a/llvm/docs/ProgrammersManual.rst +++ b/llvm/docs/ProgrammersManual.rst @@ -164,6 +164,12 @@ rarely have to include this file directly). 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 diff --git a/llvm/include/llvm/Support/Casting.h b/llvm/include/llvm/Support/Casting.h index cddee3642fa..46bdedb04cf 100644 --- a/llvm/include/llvm/Support/Casting.h +++ b/llvm/include/llvm/Support/Casting.h @@ -143,6 +143,16 @@ template <class X, class Y> LLVM_NODISCARD inline bool isa(const Y &Val) { 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 //===----------------------------------------------------------------------===// diff --git a/llvm/unittests/Support/Casting.cpp b/llvm/unittests/Support/Casting.cpp index 368ca17ef7c..bcdaca94a3c 100644 --- a/llvm/unittests/Support/Casting.cpp +++ b/llvm/unittests/Support/Casting.cpp @@ -118,6 +118,12 @@ TEST(CastingTest, isa) { 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); |

