Skip to content

Commit

Permalink
gtk.cfg: Add more definitions for assert macros (#5830)
Browse files Browse the repository at this point in the history
In particular, the missing definition of `g_assert_nonnull()` can cause
false positives because it's not recognized as `assert(expr != NULL)` by
cppcheck.
  • Loading branch information
ptomato authored Jan 5, 2024
1 parent 6ef3224 commit 8ca93c9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cfg/gtk.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,14 @@
<define name="g_assert(expr)" value="assert(expr)"/>
<define name="g_assert_not_reached()" value="assert(NULL)"/>
<define name="g_assert_true(expr)" value="g_assert(expr)"/>
<define name="g_assert_false(expr)" value="g_assert(!(expr))"/>
<define name="g_assert_cmpstr(s1, cmp, s2)" value="g_assert_true(g_strcmp0 ((s1), (s2)) cmp 0)"/>
<define name="g_assert_cmpint(n1, cmp, n2)" value="g_assert_true((n1) cmp (n2))"/>
<define name="g_assert_cmpuint(n1, cmp, n2)" value="g_assert_true((n1) cmp (n2))"/>
<define name="g_assert_cmphex(n1, cmp, n2)" value="g_assert_true((n1) cmp (n2))"/>
<define name="g_assert_cmpfloat(n1, cmp, n2)" value="g_assert_true((n1) cmp (n2))"/>
<define name="g_assert_null(expr)" value="g_assert_true((expr) == NULL)"/>
<define name="g_assert_nonnull(expr)" value="g_assert_true((expr) != NULL)"/>
<define name="g_signal_connect(instance, detailed_signal, c_handler, data)" value="g_signal_connect_data ((instance), (detailed_signal), (c_handler), (data), NULL, (GConnectFlags) 0)"/>
<define name="g_signal_connect_after(instance, detailed_signal, c_handler, data)" value="g_signal_connect_data ((instance), (detailed_signal), (c_handler), (data), NULL, G_CONNECT_AFTER)"/>
<define name="g_signal_connect_swapped(instance, detailed_signal, c_handler, data)" value="g_signal_connect_data ((instance), (detailed_signal), (c_handler), (data), NULL, G_CONNECT_SWAPPED)"/>
Expand Down
21 changes: 20 additions & 1 deletion test/cfg/gtk.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Test library configuration for gtk.cfg
//
// Usage:
// $ cppcheck --check-library --library=gtk --enable=style,information --inconclusive --error-exitcode=1 --disable=missingInclude --inline-suppr --library=gtk test/cfg/gtk.cpp
// $ cppcheck --check-library --enable=style,information --inconclusive --error-exitcode=1 --disable=missingInclude --inline-suppr --library=gtk test/cfg/gtk.c
// =>
// No warnings about bad library configuration, unmatched suppressions, etc. exitcode=0
//
Expand Down Expand Up @@ -230,6 +230,25 @@ void g_assert_test()
g_assert(a = 5);
}

void g_assert_true_false_test()
{
gboolean t = TRUE;
gboolean f = FALSE;
g_assert_true(t);
// cppcheck-suppress checkLibraryNoReturn
g_assert_false(f);
}

void g_assert_null_nonnull_test()
{
char * gpt = g_malloc(1);
g_assert_nonnull(gpt);
gpt[0] = 0;
g_free(gpt);
// cppcheck-suppress checkLibraryNoReturn
g_assert_null(NULL);
}

void g_print_test()
{
// cppcheck-suppress invalidPrintfArgType_uint
Expand Down

0 comments on commit 8ca93c9

Please sign in to comment.