Add safe_is_a

Message ID 20230421135135.B805413456@imap2.suse-dmz.suse.de
State Repeat Merge
Headers
Series Add safe_is_a |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Richard Biener April 21, 2023, 1:51 p.m. UTC
  The following adds safe_is_a, an is_a check handling nullptr
gracefully.

Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.

	* is-a.h (safe_is_a): New.
---
 gcc/is-a.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)
  

Patch

diff --git a/gcc/is-a.h b/gcc/is-a.h
index b5355242655..0a697cf002a 100644
--- a/gcc/is-a.h
+++ b/gcc/is-a.h
@@ -232,6 +232,19 @@  is_a (U *p)
   return is_a_helper<T>::test (p);
 }
 
+/* Similar to is_a<>, but where the pointer can be NULL, even if
+   is_a_helper<T> doesn't check for NULL.  */
+
+template <typename T, typename U>
+inline bool
+safe_is_a (U *p)
+{
+  if (p)
+    return is_a_helper <T>::test (p);
+  else
+    return false;
+}
+
 /* A generic conversion from a base type U to a derived type T.  See the
    discussion above for when to use this function.  */