lib: add post condition assertions for current thread error after user functions
[babeltrace.git] / src / lib / assert-post.h
index 94bdf4ef0191029278105665434c6cf08bdb2e68..b06eb7425fb03da0bf64382420cf6652c5331396 100644 (file)
@@ -43,6 +43,7 @@
 #include <stdlib.h>
 #include <inttypes.h>
 #include "common/macros.h"
+#include "common/common.h"
 
 /*
  * Prints the details of an unsatisfied postcondition without
                        BT_ASSERT_POST_MSG("Babeltrace 2 library postcondition not satisfied; error is:"); \
                        BT_ASSERT_POST_MSG(_fmt, ##__VA_ARGS__);        \
                        BT_ASSERT_POST_MSG("Aborting...");              \
-                       abort();                                        \
+                       bt_common_abort();                                      \
                }                                                       \
        } while (0)
 
+/*
+ * Asserts that if there's an error on the current thread, an error status code
+ * was returned.  Puts back the error in place (if there is one) such that if
+ * the assertion hits, it will be possible to inspect it with a debugger.
+ */
+#define BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(_status)                    \
+       do {                                                                    \
+               const struct bt_error *err = bt_current_thread_take_error();    \
+               if (err) {                                                      \
+                       bt_current_thread_move_error(err);                      \
+               }                                                               \
+               BT_ASSERT_POST(_status < 0 || !err,                             \
+                       "Current thread has an error, but user function "       \
+                       "returned a non-error status: status=%s",               \
+                       bt_common_func_status_string(_status));                 \
+       } while (0)
+
+/*
+ * Asserts that the current thread has no error.
+ */
+#define BT_ASSERT_POST_NO_ERROR() \
+       BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(0)
+
 /*
  * Marks a function as being only used within a BT_ASSERT_POST()
  * context.
 # define BT_ASSERT_POST_DEV(_cond, _fmt, ...)                          \
        BT_ASSERT_POST((_cond), _fmt, ##__VA_ARGS__)
 
+/* Developer mode version of BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(). */
+# define BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(_status) \
+       BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(_status)
+
 /* Developer mode version of `BT_ASSERT_POST_FUNC`. */
 # define BT_ASSERT_POST_DEV_FUNC BT_ASSERT_POST_FUNC
 #else
 # define BT_ASSERT_POST_DEV_MSG(_fmt, ...)
 # define BT_ASSERT_POST_DEV(_cond, _fmt, ...)  ((void) sizeof((void) (_cond), 0))
+# define BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(_status) \
+       ((void) sizeof((void) (_status), 0))
 # define BT_ASSERT_POST_DEV_FUNC       __attribute__((unused))
 #endif /* BT_DEV_MODE */
 
This page took 0.025351 seconds and 4 git commands to generate.