summarylogtreecommitdiffstats
path: root/non-fatal-assertions.h
blob: 9304765db9f5379c0850e819a088a79c5bb479ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/////////////////////////////////////////////////////////////////////////
// non-fatal-assertions.h    —    non-fatal   assertions   for    glib //
/////////////////////////////////////////////////////////////////////////
// Copyright (C) 2023 Fredrick R. Brennan
// <copypaste@kittens.ph>
// 
// This  library is free software; you can redistribute it and/or  modify
// it  under  the  terms  of the GNU Lesser  General  Public  License  as
// published  by the Free Software Foundation; either version 2.1 of  the
// License, or (at your option) any later version. 
// 
// This  library  is distributed in the hope that it will be useful,  but
// WITHOUT   ANY   WARRANTY;  without  even  the  implied   warranty   of
// MERCHANTABILITY  or  FITNESS  FOR A PARTICULAR PURPOSE.  See  the  GNU
// Lesser General Public License for more details. 
// 
// You  should  have  received a copy of the GNU  Lesser  General  Public
// License along with this library; if not, write to: 
// 
// The Free Software Foundation, Inc.
// № 51 Franklin Street, Fifth Floor
// Boston, MA 02110-1301 USA
/////////////////////////////////////////////////////////////////////////

#ifndef NON_FATAL_ASSERTIONS_H

#include <glib/gmacros.h>
#include <glib.h>

#ifndef NDEBUG

#ifdef NO_NON_FATAL_ASSERTIONS
#define g_assert_nonfatal(expr) G_STMT_START{ (void)0; }G_STMT_END
#endif /* NO_NON_FATAL_ASSERTIONS */

#ifndef g_assert_nonfatal
#define g_assert_nonfatal(expr)                                      \
    G_STMT_START {                                                   \
      if (G_LIKELY (expr))                                           \
        ;                                                            \
      else                                                           \
        g_critical ("file %s: line %d (%s): assertion failed: (%s)", \
            __FILE__,                                                \
            __LINE__,                                                \
            G_STRFUNC,                                               \
            #expr);                                                  \
    } G_STMT_END
#undef g_assert
#define g_assert(x) G_STMT_START{ g_assert_nonfatal(x); }G_STMT_END
#endif /* g_assert_nonfatal */

#ifndef g_assert_not_reached_nonfatal
#define g_assert_not_reached_nonfatal(expr)                             \
    G_STMT_START {                                                      \
      g_critical ("file %s: line %d (%s): assertion not reached: (%s)", \
          __FILE__,                                                     \
          __LINE__,                                                     \
          G_STRFUNC,                                                    \
          #expr);                                                       \
    } G_STMT_END
#undef g_assert_not_reached
#define g_assert_not_reached(x) G_STMT_START{ g_assert_not_reached_nonfatal(x); }G_STMT_END
#endif /* g_assert_not_reached_nonfatal */

#endif /* !NDEBUG */
#endif /* NON_FATAL_ASSERTIONS_H */