Silence -Wunused-but-set-variable error with clang
[babeltrace.git] / src / common / macros.h
index 23d4b310d573ab49c0f001d900fef04cf678b307..c42cdf5a52f358d1e5c8d13a09689a99bbe8e73d 100644 (file)
 #define BT_HIDDEN __attribute__((visibility("hidden")))
 #endif
 
+/*
+ * Yield `ref`'s value while setting `ref` to NULL.
+ *
+ * This can be used to give a strong reference to a callee:
+ *
+ *   add_foo_to_list(list, BT_MOVE_REF(foo));
+ *
+ * or in a simple assignment:
+ *
+ *   my_struct->foo = BT_MOVE_REF(foo);
+ *
+ * When moving a reference in a function call, the reference is given to the
+ * callee even if that function call fails, so make sure the called function
+ * is written accordingly.
+ */
+
+#define BT_MOVE_REF(ref)               \
+       ({                              \
+               typeof(ref) _ref = ref; \
+               ref = NULL;             \
+               _ref;                   \
+       })
+
+#if defined __clang__
+#  if __has_warning("-Wunused-but-set-variable")
+#    define BT_DIAG_IGNORE_UNUSED_BUT_SET_VARIABLE \
+       _Pragma("GCC diagnostic ignored \"-Wunused-but-set-variable\"")
+#  endif
+#endif
+
+#if !defined BT_DIAG_IGNORE_UNUSED_BUT_SET_VARIABLE
+#  define BT_DIAG_IGNORE_UNUSED_BUT_SET_VARIABLE
+#endif
+
 #endif
This page took 0.023108 seconds and 4 git commands to generate.