[glib/mcatanzaro/g_assert_cpp: 175/175] Add separate definitions of g_assert_[non]null() for C++



commit 4a0ce6b4589c3ced0fba37c7917038d27747192b
Author: Michael Catanzaro <mcatanzaro igalia com>
Date:   Wed Jan 2 16:15:17 2019 -0600

    Add separate definitions of g_assert_[non]null() for C++
    
    In C++ we can use nullptr to ensure g_assert_[non]null() is only called
    with pointers. This will introduce build failures in tests that would
    have previously compiled, but only in C++, and only for code that
    misused these macros. Code using the macros properly will be fine.
    
    This change caught a couple bugs in WebKit's API tests, where I had
    accidentally used these functions improperly. E.g. this is now a build
    failure in C++:
    
    g_assert_null(webkit_context_menu_get_n_items(menu)); /* Oops! */
    
    Either I wanted to use cmpuint there, or I wanted to use
    webkit_context_menu_get_items() to receive a GList* instead.
    
    Another example that will no longer build in C++:
    
    g_assert_null(0); /* Contrived, but 0 is not a pointer! */

 glib/gtestutils.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
---
diff --git a/glib/gtestutils.h b/glib/gtestutils.h
index 4aa6fa168..effc88237 100644
--- a/glib/gtestutils.h
+++ b/glib/gtestutils.h
@@ -124,6 +124,19 @@ typedef void (*GTestFixtureFunc) (gpointer      fixture,
                                                g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, 
G_STRFUNC, \
                                                                     "'" #expr "' should be FALSE"); \
                                         } G_STMT_END
+
+/* Use nullptr in C++ to catch misuse of these macros. */
+#if defined(__cplusplus) && __cplusplus >= 201100L
+#define g_assert_null(expr)             G_STMT_START { if G_LIKELY ((expr) == nullptr) ; else \
+                                               g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, 
G_STRFUNC, \
+                                                                    "'" #expr "' should be nullptr"); \
+                                        } G_STMT_END
+#define g_assert_nonnull(expr)          G_STMT_START { \
+                                             if G_LIKELY ((expr) != nullptr) ; else \
+                                               g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, 
G_STRFUNC, \
+                                                                    "'" #expr "' should not be nullptr"); \
+                                        } G_STMT_END
+#else /* not C++ */
 #define g_assert_null(expr)             G_STMT_START { if G_LIKELY ((expr) == NULL) ; else \
                                                g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, 
G_STRFUNC, \
                                                                     "'" #expr "' should be NULL"); \
@@ -133,6 +146,8 @@ typedef void (*GTestFixtureFunc) (gpointer      fixture,
                                                g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, 
G_STRFUNC, \
                                                                     "'" #expr "' should not be NULL"); \
                                         } G_STMT_END
+#endif
+
 #ifdef G_DISABLE_ASSERT
 #define g_assert_not_reached()          G_STMT_START { (void) 0; } G_STMT_END
 #define g_assert(expr)                  G_STMT_START { (void) 0; } G_STMT_END


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]