g_return_if_reached and g_critical patch



Here's my patch that adds g_return_if_reached, g_return_val_if_reached, and
g_critical as discussed in a previous message. The file enclosure is the
same as the patch in the body, but with the tabs correct (I am using a
mailer that turns tabs into spaces).

Besides adding the new macros/functions, I also removed trailing "." from
the g_return family messages to make them match the g_assert family messages
and added parentheses around the use of "val". I just realized that these
parentheses might not be necessary. I can't remember the reason that you
sometimes need parentheses around macro expressions (it just slipped my
mind) and I suspect it may not apply in this case.

Comments? Someone want to apply this? Or should I commit?

===================================================================
RCS file: /cvs/gnome/glib/glib.h,v
retrieving revision 1.180
diff -p -u -r1.180 glib.h
--- glib.h      2000/07/20 16:58:52     1.180
+++ glib.h      2000/07/24 22:09:02
@@ -426,6 +426,8 @@ extern "C" {
 
 #define g_return_if_fail(expr)
 #define g_return_val_if_fail(expr,val)
+#define g_return_if_reached() return
+#define g_return_val_if_reached(val) return (val)
 
 #else /* !G_DISABLE_CHECKS */
 
@@ -436,7 +438,7 @@ extern "C" {
        {                                                               \
         g_log (G_LOG_DOMAIN,                                           \
                G_LOG_LEVEL_CRITICAL,                                   \
-               "file %s: line %d (%s): assertion `%s' failed.",        \
+               "file %s: line %d (%s): assertion `%s' failed",         \
                __FILE__,                                               \
                __LINE__,                                               \
                __PRETTY_FUNCTION__,                                    \
@@ -449,14 +451,32 @@ extern "C" {
        {                                                               \
         g_log (G_LOG_DOMAIN,                                           \
                G_LOG_LEVEL_CRITICAL,                                   \
-               "file %s: line %d (%s): assertion `%s' failed.",        \
+               "file %s: line %d (%s): assertion `%s' failed",         \
                __FILE__,                                               \
                __LINE__,                                               \
                __PRETTY_FUNCTION__,                                    \
                #expr);                                                 \
-        return val;                                                    \
+        return (val);                                                  \
        };                              }G_STMT_END
 
+#define g_return_if_reached()          G_STMT_START{                   \
+     g_log (G_LOG_DOMAIN,                                              \
+           G_LOG_LEVEL_CRITICAL,                                       \
+           "file %s: line %d (%s): assertion `%s' failed",             \
+           __FILE__,                                                   \
+           __LINE__,                                                   \
+           __PRETTY_FUNCTION__);                                       \
+     return;                           }G_STMT_END
+
+#define g_return_val_if_reached(val)   G_STMT_START{                   \
+     g_log (G_LOG_DOMAIN,                                              \
+           G_LOG_LEVEL_CRITICAL,                                       \
+           "file %s: line %d (%s): should not be reached",             \
+           __FILE__,                                                   \
+           __LINE__,                                                   \
+           __PRETTY_FUNCTION__);                                       \
+     return (val);                     }G_STMT_END
+
 #else /* !__GNUC__ */
 
 #define g_return_if_fail(expr)         G_STMT_START{           \
@@ -464,7 +484,7 @@ extern "C" {
        {                                                       \
         g_log (G_LOG_DOMAIN,                                   \
                G_LOG_LEVEL_CRITICAL,                           \
-               "file %s: line %d: assertion `%s' failed.",     \
+               "file %s: line %d: assertion `%s' failed",      \
                __FILE__,                                       \
                __LINE__,                                       \
                #expr);                                         \
@@ -476,13 +496,29 @@ extern "C" {
        {                                                       \
         g_log (G_LOG_DOMAIN,                                   \
                G_LOG_LEVEL_CRITICAL,                           \
-               "file %s: line %d: assertion `%s' failed.",     \
+               "file %s: line %d: assertion `%s' failed",      \
                __FILE__,                                       \
                __LINE__,                                       \
                #expr);                                         \
-        return val;                                            \
+        return (val);                                          \
        };                              }G_STMT_END
 
+#define g_return_if_reached()          G_STMT_START{           \
+     g_log (G_LOG_DOMAIN,                                      \
+           G_LOG_LEVEL_CRITICAL,                               \
+           "file %s: line %d: assertion `%s' failed",          \
+           __FILE__,                                           \
+           __LINE__);                                          \
+     return;                           }G_STMT_END
+
+#define g_return_val_if_reached(val)   G_STMT_START{           \
+     g_log (G_LOG_DOMAIN,                                      \
+           G_LOG_LEVEL_CRITICAL,                               \
+           "file %s: line %d: should not be reached",          \
+           __FILE__,                                           \
+           __LINE__);                                          \
+     return (val);                     }G_STMT_END
+
 #endif /* !__GNUC__ */
 
 #endif /* !G_DISABLE_CHECKS */
@@ -1445,6 +1481,9 @@ GLogLevelFlags    g_log_set_always_fatal  (G
 #define        g_warning(format, args...)      g_log (G_LOG_DOMAIN, \
                                               G_LOG_LEVEL_WARNING, \
                                               format, ##args)
+#define        g_critical(format, args...)     g_log (G_LOG_DOMAIN, \
+                                              G_LOG_LEVEL_CRITICAL, \
+                                              format, ##args)
 #else  /* !__GNUC__ */
 static void
 g_error (const gchar *format,
@@ -1471,6 +1510,15 @@ g_warning (const gchar *format,
   va_list args;
   va_start (args, format);
   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
+  va_end (args);
+}
+static void
+g_critical (const gchar *format,
+           ...)
+{
+  va_list args;
+  va_start (args, format);
+  g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
   va_end (args);
 }
 #endif /* !__GNUC__ */
===================================================================


    -- Darin

Index: glib.h
===================================================================
RCS file: /cvs/gnome/glib/glib.h,v
retrieving revision 1.180
diff -p -u -r1.180 glib.h
--- glib.h	2000/07/20 16:58:52	1.180
+++ glib.h	2000/07/24 22:09:02
@@ -426,6 +426,8 @@ extern "C" {
 
 #define g_return_if_fail(expr)
 #define g_return_val_if_fail(expr,val)
+#define g_return_if_reached() return
+#define g_return_val_if_reached(val) return (val)
 
 #else /* !G_DISABLE_CHECKS */
 
@@ -436,7 +438,7 @@ extern "C" {
        {								\
 	 g_log (G_LOG_DOMAIN,						\
 		G_LOG_LEVEL_CRITICAL,					\
-		"file %s: line %d (%s): assertion `%s' failed.",	\
+		"file %s: line %d (%s): assertion `%s' failed",		\
 		__FILE__,						\
 		__LINE__,						\
 		__PRETTY_FUNCTION__,					\
@@ -449,14 +451,32 @@ extern "C" {
        {								\
 	 g_log (G_LOG_DOMAIN,						\
 		G_LOG_LEVEL_CRITICAL,					\
-		"file %s: line %d (%s): assertion `%s' failed.",	\
+		"file %s: line %d (%s): assertion `%s' failed",		\
 		__FILE__,						\
 		__LINE__,						\
 		__PRETTY_FUNCTION__,					\
 		#expr);							\
-	 return val;							\
+	 return (val);							\
        };				}G_STMT_END
 
+#define g_return_if_reached()		G_STMT_START{			\
+     g_log (G_LOG_DOMAIN,						\
+	    G_LOG_LEVEL_CRITICAL,					\
+	    "file %s: line %d (%s): assertion `%s' failed",		\
+	    __FILE__,							\
+	    __LINE__,							\
+	    __PRETTY_FUNCTION__);					\
+     return;				}G_STMT_END
+
+#define g_return_val_if_reached(val)	G_STMT_START{			\
+     g_log (G_LOG_DOMAIN,						\
+	    G_LOG_LEVEL_CRITICAL,					\
+	    "file %s: line %d (%s): should not be reached",		\
+	    __FILE__,							\
+	    __LINE__,							\
+	    __PRETTY_FUNCTION__);					\
+     return (val);			}G_STMT_END
+
 #else /* !__GNUC__ */
 
 #define g_return_if_fail(expr)		G_STMT_START{		\
@@ -464,7 +484,7 @@ extern "C" {
        {							\
 	 g_log (G_LOG_DOMAIN,					\
 		G_LOG_LEVEL_CRITICAL,				\
-		"file %s: line %d: assertion `%s' failed.",	\
+		"file %s: line %d: assertion `%s' failed",	\
 		__FILE__,					\
 		__LINE__,					\
 		#expr);						\
@@ -476,13 +496,29 @@ extern "C" {
        {							\
 	 g_log (G_LOG_DOMAIN,					\
 		G_LOG_LEVEL_CRITICAL,				\
-		"file %s: line %d: assertion `%s' failed.",	\
+		"file %s: line %d: assertion `%s' failed",	\
 		__FILE__,					\
 		__LINE__,					\
 		#expr);						\
-	 return val;						\
+	 return (val);						\
        };				}G_STMT_END
 
+#define g_return_if_reached()		G_STMT_START{		\
+     g_log (G_LOG_DOMAIN,					\
+	    G_LOG_LEVEL_CRITICAL,				\
+	    "file %s: line %d: assertion `%s' failed",		\
+	    __FILE__,						\
+	    __LINE__);						\
+     return;				}G_STMT_END
+
+#define g_return_val_if_reached(val)	G_STMT_START{		\
+     g_log (G_LOG_DOMAIN,					\
+	    G_LOG_LEVEL_CRITICAL,				\
+	    "file %s: line %d: should not be reached",		\
+	    __FILE__,						\
+	    __LINE__);						\
+     return (val);			}G_STMT_END
+
 #endif /* !__GNUC__ */
 
 #endif /* !G_DISABLE_CHECKS */
@@ -1445,6 +1481,9 @@ GLogLevelFlags	g_log_set_always_fatal	(G
 #define	g_warning(format, args...)	g_log (G_LOG_DOMAIN, \
 					       G_LOG_LEVEL_WARNING, \
 					       format, ##args)
+#define	g_critical(format, args...)	g_log (G_LOG_DOMAIN, \
+					       G_LOG_LEVEL_CRITICAL, \
+					       format, ##args)
 #else	/* !__GNUC__ */
 static void
 g_error (const gchar *format,
@@ -1471,6 +1510,15 @@ g_warning (const gchar *format,
   va_list args;
   va_start (args, format);
   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
+  va_end (args);
+}
+static void
+g_critical (const gchar *format,
+	    ...)
+{
+  va_list args;
+  va_start (args, format);
+  g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
   va_end (args);
 }
 #endif	/* !__GNUC__ */


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