From 420d061fd999639eaf3e0be1a5db2a4394754804 Mon Sep 17 00:00:00 2001 From: Andy Holmes Date: Thu, 21 Mar 2024 19:03:10 -0700 Subject: [PATCH] test: silence an infinite-loop error in `g_error()` Due to some improvements to GCC's static analyzer, the intentional loop in `g_error()` throws an error in the CI. Add `V_GNUC_BEGIN_IGNORE_INFINITE_LOOP` (and matching `END`) macro to silence false positives from `g_error()`. --- src/libvalent/core/valent-debug.h.in | 35 ++++++++++++++++++++++++++++ src/libvalent/core/valent-object.c | 11 ++++++--- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/libvalent/core/valent-debug.h.in b/src/libvalent/core/valent-debug.h.in index 37dadceaa0..97179b7aa7 100644 --- a/src/libvalent/core/valent-debug.h.in +++ b/src/libvalent/core/valent-debug.h.in @@ -204,5 +204,40 @@ void valent_trace_mark (const char *strfunc, # define VALENT_JSON(_node, _ctx) G_STMT_START { } G_STMT_END #endif +/** + * V_GNUC_BEGIN_IGNORE_INFINITE_LOOP: (skip) + * + * Tells gcc (if it is a new enough version) to temporarily stop emitting + * warnings when functions employ an infinite loop. This is useful for functions + * that do so intentionally, such as [func@GLib.error]. + * + * Since: 1.0 + */ + +/** + * V_GNUC_END_IGNORE_INFINITE_LOOP: (skip) + * + * Undoes the effect of [func@Valent.V_GNUC_BEGIN_IGNORE_INFINITE_LOOP], telling gcc + * to begin outputting warnings again (assuming those warnings had been enabled + * to begin with). + * + * Since: 1.0 + */ + +#if G_GNUC_CHECK_VERSION(14, 0) + #define V_GNUC_BEGIN_IGNORE_INFINITE_LOOP \ + G_STMT_START { \ + _Pragma("GCC diagnostic push") \ + _Pragma("GCC diagnostic ignored \"-Wanalyzer-infinite-loop\"") \ + } G_STMT_END + #define V_GNUC_END_IGNORE_INFINITE_LOOP \ + G_STMT_START { \ + _Pragma("GCC diagnostic pop") \ + } G_STMT_END +#else + #define V_GNUC_BEGIN_IGNORE_INFINITE_LOOP G_STMT_START { } G_STMT_END + #define V_GNUC_END_IGNORE_INFINITE_LOOP G_STMT_START { } G_STMT_END +#endif + G_END_DECLS diff --git a/src/libvalent/core/valent-object.c b/src/libvalent/core/valent-object.c index 81b8197e29..a9c5348250 100644 --- a/src/libvalent/core/valent-object.c +++ b/src/libvalent/core/valent-object.c @@ -6,6 +6,7 @@ #include "config.h" +#include "valent-debug.h" #include "valent-macros.h" #include "valent-object.h" @@ -182,9 +183,13 @@ static void valent_object_constructed (GObject *object) { if G_UNLIKELY (G_OBJECT_GET_CLASS (object)->dispose != valent_object_dispose) - g_error ("%s overrides GObject.Object.dispose() instead of " - "Valent.Object.destroy(), which is not thread safe", - G_OBJECT_TYPE_NAME (object)); + { + V_GNUC_BEGIN_IGNORE_INFINITE_LOOP; + g_error ("%s overrides GObject.Object.dispose() instead of " + "Valent.Object.destroy(), which is not thread safe", + G_OBJECT_TYPE_NAME (object)); + V_GNUC_END_IGNORE_INFINITE_LOOP; + } G_OBJECT_CLASS (valent_object_parent_class)->constructed (object); }