Miscellaneous Macros

Miscellaneous Macros — specialized macros which are not used often

Functions

#define g_auto()
#define g_autoptr()
#define g_autolist()
#define g_autoslist()
#define g_autoqueue()
#define G_DEFINE_AUTOPTR_CLEANUP_FUNC()
#define G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC()
#define G_DEFINE_AUTO_CLEANUP_FREE_FUNC()
#define G_VA_COPY()
#define G_STRINGIFY()
#define G_PASTE()
#define G_STATIC_ASSERT()
#define G_STATIC_ASSERT_EXPR()
#define G_GNUC_CHECK_VERSION()
#define G_GNUC_ALLOC_SIZE()
#define G_GNUC_ALLOC_SIZE2()
#define G_GNUC_DEPRECATED_FOR()
#define G_GNUC_PRINTF()
#define G_GNUC_SCANF()
#define G_GNUC_STRFTIME()
#define G_GNUC_FORMAT()
#define G_DEPRECATED_FOR()
#define G_UNAVAILABLE()
#define G_LIKELY()
#define G_UNLIKELY()

Includes

#include <glib.h>

Description

These macros provide more specialized features which are not needed so often by application programmers.

Functions

g_auto()

#define             g_auto(TypeName)

Helper to declare a variable with automatic cleanup.

The variable is cleaned up in a way appropriate to its type when the variable goes out of scope. The type must support this. The way to clean up the type must have been defined using one of the macros G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC() or G_DEFINE_AUTO_CLEANUP_FREE_FUNC().

This feature is only supported on GCC and clang. This macro is not defined on other compilers and should not be used in programs that are intended to be portable to those compilers.

This is meant to be used with stack-allocated structures and non-pointer types. For the (more commonly used) pointer version, see g_autoptr().

This macro can be used to avoid having to do explicit cleanups of local variables when exiting functions. It often vastly simplifies handling of error conditions, removing the need for various tricks such as goto out or repeating of cleanup code. It is also helpful for non-error cases.

Consider the following example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
GVariant *
my_func(void)
{
  g_auto(GQueue) queue = G_QUEUE_INIT;
  g_auto(GVariantBuilder) builder;
  g_auto(GStrv) strv;

  g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
  strv = g_strsplit("a:b:c", ":", -1);

  ...

  if (error_condition)
    return NULL;

  ...

  return g_variant_builder_end (&builder);
}

You must initialize the variable in some way — either by use of an initialiser or by ensuring that an _init function will be called on it unconditionally before it goes out of scope.

Parameters

TypeName

a supported variable type

 

Since: 2.44


g_autoptr()

#define             g_autoptr(TypeName)

Helper to declare a pointer variable with automatic cleanup.

The variable is cleaned up in a way appropriate to its type when the variable goes out of scope. The type must support this. The way to clean up the type must have been defined using the macro G_DEFINE_AUTOPTR_CLEANUP_FUNC().

This feature is only supported on GCC and clang. This macro is not defined on other compilers and should not be used in programs that are intended to be portable to those compilers.

This is meant to be used to declare pointers to types with cleanup functions. The type of the variable is a pointer to TypeName . You must not add your own *.

This macro can be used to avoid having to do explicit cleanups of local variables when exiting functions. It often vastly simplifies handling of error conditions, removing the need for various tricks such as goto out or repeating of cleanup code. It is also helpful for non-error cases.

Consider the following example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
gboolean
check_exists(GVariant *dict)
{
  g_autoptr(GVariant) dirname, basename = NULL;
  g_autofree gchar *path = NULL;

  dirname = g_variant_lookup_value (dict, "dirname", G_VARIANT_TYPE_STRING);

  if (dirname == NULL)
    return FALSE;

  basename = g_variant_lookup_value (dict, "basename", G_VARIANT_TYPE_STRING);

  if (basename == NULL)
    return FALSE;

  path = g_build_filename (g_variant_get_string (dirname, NULL),
                           g_variant_get_string (basename, NULL),
                           NULL);

  return g_access (path, R_OK) == 0;
}

You must initialise the variable in some way — either by use of an initialiser or by ensuring that it is assigned to unconditionally before it goes out of scope.

See also g_auto(), g_autofree() and g_steal_pointer().

Parameters

TypeName

a supported variable type

 

Since: 2.44


g_autolist()

#define             g_autolist(TypeName)

Helper to declare a list variable with automatic deep cleanup.

The list is deeply freed, in a way appropriate to the specified type, when the variable goes out of scope. The type must support this.

This feature is only supported on GCC and clang. This macro is not defined on other compilers and should not be used in programs that are intended to be portable to those compilers.

This is meant to be used to declare lists of a type with a cleanup function. The type of the variable is a GList *. You must not add your own *.

This macro can be used to avoid having to do explicit cleanups of local variables when exiting functions. It often vastly simplifies handling of error conditions, removing the need for various tricks such as goto out or repeating of cleanup code. It is also helpful for non-error cases.

See also g_autoslist(), g_autoptr() and g_steal_pointer().

Parameters

TypeName

a supported variable type

 

Since: 2.56


g_autoslist()

#define             g_autoslist(TypeName)

Helper to declare a singly linked list variable with automatic deep cleanup.

The list is deeply freed, in a way appropriate to the specified type, when the variable goes out of scope. The type must support this.

This feature is only supported on GCC and clang. This macro is not defined on other compilers and should not be used in programs that are intended to be portable to those compilers.

This is meant to be used to declare lists of a type with a cleanup function. The type of the variable is a GSList *. You must not add your own *.

This macro can be used to avoid having to do explicit cleanups of local variables when exiting functions. It often vastly simplifies handling of error conditions, removing the need for various tricks such as goto out or repeating of cleanup code. It is also helpful for non-error cases.

See also g_autolist(), g_autoptr() and g_steal_pointer().

Parameters

TypeName

a supported variable type

 

Since: 2.56


g_autoqueue()

#define             g_autoqueue(TypeName)

Helper to declare a double-ended queue variable with automatic deep cleanup.

The queue is deeply freed, in a way appropriate to the specified type, when the variable goes out of scope. The type must support this.

This feature is only supported on GCC and clang. This macro is not defined on other compilers and should not be used in programs that are intended to be portable to those compilers.

This is meant to be used to declare queues of a type with a cleanup function. The type of the variable is a GQueue *. You must not add your own *.

This macro can be used to avoid having to do explicit cleanups of local variables when exiting functions. It often vastly simplifies handling of error conditions, removing the need for various tricks such as goto out or repeating of cleanup code. It is also helpful for non-error cases.

See also g_autolist(), g_autoptr() and g_steal_pointer().

Parameters

TypeName

a supported variable type

 

Since: 2.62


G_DEFINE_AUTOPTR_CLEANUP_FUNC()

#define             G_DEFINE_AUTOPTR_CLEANUP_FUNC(TypeName, func)

Defines the appropriate cleanup function for a pointer type.

The function will not be called if the variable to be cleaned up contains NULL.

This will typically be the _free() or _unref() function for the given type.

With this definition, it will be possible to use g_autoptr() with TypeName .

1
G_DEFINE_AUTOPTR_CLEANUP_FUNC(GObject, g_object_unref)

This macro should be used unconditionally; it is a no-op on compilers where cleanup is not supported.

Parameters

TypeName

a type name to define a g_autoptr() cleanup function for

 

func

the cleanup function

 

Since: 2.44


G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC()

#define             G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TypeName, func)

Defines the appropriate cleanup function for a type.

This will typically be the _clear() function for the given type.

With this definition, it will be possible to use g_auto() with TypeName .

1
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(GQueue, g_queue_clear)

This macro should be used unconditionally; it is a no-op on compilers where cleanup is not supported.

Parameters

TypeName

a type name to define a g_auto() cleanup function for

 

func

the clear function

 

Since: 2.44


G_DEFINE_AUTO_CLEANUP_FREE_FUNC()

#define             G_DEFINE_AUTO_CLEANUP_FREE_FUNC(TypeName, func, none)

Defines the appropriate cleanup function for a type.

With this definition, it will be possible to use g_auto() with TypeName .

This function will be rarely used. It is used with pointer-based typedefs and non-pointer types where the value of the variable represents a resource that must be freed. Two examples are GStrv and file descriptors.

none specifies the "none" value for the type in question. It is probably something like NULL or -1. If the variable is found to contain this value then the free function will not be called.

1
G_DEFINE_AUTO_CLEANUP_FREE_FUNC(GStrv, g_strfreev, NULL)

This macro should be used unconditionally; it is a no-op on compilers where cleanup is not supported.

Parameters

TypeName

a type name to define a g_auto() cleanup function for

 

func

the free function

 

none

the "none" value for the type

 

Since: 2.44


G_VA_COPY()

#define             G_VA_COPY(ap1,ap2)

Portable way to copy va_list variables.

In order to use this function, you must include string.h yourself, because this macro may use memmove() and GLib does not include string.h for you.

Parameters

ap1

the va_list variable to place a copy of ap2 in

 

ap2

a va_list

 

G_STRINGIFY()

#define G_STRINGIFY(macro_or_string) G_STRINGIFY_ARG (macro_or_string)

Accepts a macro or a string and converts it into a string after preprocessor argument expansion. For example, the following code:

1
2
#define AGE 27
const gchar *greeting = G_STRINGIFY (AGE) " today!";

is transformed by the preprocessor into (code equivalent to):

1
const gchar *greeting = "27 today!";

Parameters

macro_or_string

a macro or a string

 

G_PASTE()

#define G_PASTE(identifier1,identifier2)      G_PASTE_ARGS (identifier1, identifier2)

Yields a new preprocessor pasted identifier identifier1identifier2 from its expanded arguments identifier1 and identifier2 . For example, the following code:

1
2
3
4
#define GET(traveller,method) G_PASTE(traveller_get_, method) (traveller)
const gchar *name = GET (traveller, name);
const gchar *quest = GET (traveller, quest);
GdkColor *favourite = GET (traveller, favourite_colour);

is transformed by the preprocessor into:

1
2
3
const gchar *name = traveller_get_name (traveller);
const gchar *quest = traveller_get_quest (traveller);
GdkColor *favourite = traveller_get_favourite_colour (traveller);

Parameters

identifier1

an identifier

 

identifier2

an identifier

 

Since: 2.20


G_STATIC_ASSERT()

#define G_STATIC_ASSERT(expr) _Static_assert (expr, "Expression evaluates to false")

The G_STATIC_ASSERT() macro lets the programmer check a condition at compile time, the condition needs to be compile time computable. The macro can be used in any place where a typedef is valid.

A typedef is generally allowed in exactly the same places that a variable declaration is allowed. For this reason, you should not use G_STATIC_ASSERT() in the middle of blocks of code.

The macro should only be used once per source code line.

Parameters

expr

a constant expression

 

Since: 2.20


G_STATIC_ASSERT_EXPR()

#define G_STATIC_ASSERT_EXPR(expr) ((void) sizeof (char[(expr) ? 1 : -1]))

The G_STATIC_ASSERT_EXPR() macro lets the programmer check a condition at compile time. The condition needs to be compile time computable.

Unlike G_STATIC_ASSERT(), this macro evaluates to an expression and, as such, can be used in the middle of other expressions. Its value should be ignored. This can be accomplished by placing it as the first argument of a comma expression.

1
2
#define ADD_ONE_TO_INT(x) \
  (G_STATIC_ASSERT_EXPR(sizeof (x) == sizeof (int)), ((x) + 1))

Parameters

expr

a constant expression

 

Since: 2.30


G_GNUC_CHECK_VERSION()

#define             G_GNUC_CHECK_VERSION(major, minor)

Expands to a check for a compiler with __GNUC__ defined and a version greater than or equal to the major and minor numbers provided. For example, the following would only match on compilers such as GCC 4.8 or newer.

1
2
#if G_GNUC_CHECK_VERSION(4, 8)
#endif

Parameters

major

major version to check against

 

minor

minor version to check against

 

Since: 2.42


G_GNUC_ALLOC_SIZE()

#define G_GNUC_ALLOC_SIZE(x) __attribute__((__alloc_size__(x)))

Expands to the GNU C alloc_size function attribute if the compiler is a new enough gcc. This attribute tells the compiler that the function returns a pointer to memory of a size that is specified by the xth function parameter.

Place the attribute after the function declaration, just before the semicolon.

1
gpointer g_malloc (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);

See the GNU C documentation for more details.

Parameters

x

the index of the argument specifying the allocation size

 

Since: 2.18


G_GNUC_ALLOC_SIZE2()

#define G_GNUC_ALLOC_SIZE2(x,y) __attribute__((__alloc_size__(x,y)))

Expands to the GNU C alloc_size function attribute if the compiler is a new enough gcc. This attribute tells the compiler that the function returns a pointer to memory of a size that is specified by the product of two function parameters.

Place the attribute after the function declaration, just before the semicolon.

1
2
gpointer g_malloc_n (gsize n_blocks,
                     gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1, 2);

See the GNU C documentation for more details.

Parameters

x

the index of the argument specifying one factor of the allocation size

 

y

the index of the argument specifying the second factor of the allocation size

 

Since: 2.18


G_GNUC_DEPRECATED_FOR()

#define             G_GNUC_DEPRECATED_FOR(f)

Like G_GNUC_DEPRECATED, but names the intended replacement for the deprecated symbol if the version of gcc in use is new enough to support custom deprecation messages.

Place the attribute after the declaration, just before the semicolon.

1
int my_mistake (void) G_GNUC_DEPRECATED_FOR(my_replacement);

See the GNU C documentation for more details.

Note that if f is a macro, it will be expanded in the warning message. You can enclose it in quotes to prevent this. (The quotes will show up in the warning, but it's better than showing the macro expansion.)

Parameters

f

the intended replacement for the deprecated symbol, such as the name of a function

 

Since: 2.26


G_GNUC_PRINTF()

#define             G_GNUC_PRINTF( format_idx, arg_idx )

Expands to the GNU C format function attribute if the compiler is gcc. This is used for declaring functions which take a variable number of arguments, with the same syntax as printf(). It allows the compiler to type-check the arguments passed to the function.

Place the attribute after the function declaration, just before the semicolon.

See the GNU C documentation for more details.

1
2
3
4
gint g_snprintf (gchar  *string,
                 gulong       n,
                 gchar const *format,
                 ...) G_GNUC_PRINTF (3, 4);

Parameters

format_idx

the index of the argument corresponding to the format string (the arguments are numbered from 1)

 

arg_idx

the index of the first of the format arguments, or 0 if there are no format arguments

 

G_GNUC_SCANF()

#define             G_GNUC_SCANF( format_idx, arg_idx )

Expands to the GNU C format function attribute if the compiler is gcc. This is used for declaring functions which take a variable number of arguments, with the same syntax as scanf(). It allows the compiler to type-check the arguments passed to the function.

1
2
3
4
5
6
int my_scanf (MyStream *stream,
              const char *format,
              ...) G_GNUC_SCANF (2, 3);
int my_vscanf (MyStream *stream,
               const char *format,
               va_list ap) G_GNUC_SCANF (2, 0);

See the GNU C documentation for details.

Parameters

format_idx

the index of the argument corresponding to the format string (the arguments are numbered from 1)

 

arg_idx

the index of the first of the format arguments, or 0 if there are no format arguments

 

G_GNUC_STRFTIME()

#define             G_GNUC_STRFTIME( format_idx )

Expands to the GNU C strftime format function attribute if the compiler is gcc. This is used for declaring functions which take a format argument which is passed to strftime() or an API implementing its formats. It allows the compiler check the format passed to the function.

1
2
3
gsize my_strftime (MyBuffer *buffer,
                   const char *format,
                   const struct tm *tm) G_GNUC_STRFTIME (2);

See the GNU C documentation for details.

Parameters

format_idx

the index of the argument corresponding to the format string (the arguments are numbered from 1)

 

Since: 2.60


G_GNUC_FORMAT()

#define             G_GNUC_FORMAT( arg_idx )

Expands to the GNU C format_arg function attribute if the compiler is gcc. This function attribute specifies that a function takes a format string for a printf(), scanf(), strftime() or strfmon() style function and modifies it, so that the result can be passed to a printf(), scanf(), strftime() or strfmon() style function (with the remaining arguments to the format function the same as they would have been for the unmodified string).

Place the attribute after the function declaration, just before the semicolon.

See the GNU C documentation for more details.

1
gchar *g_dgettext (gchar *domain_name, gchar *msgid) G_GNUC_FORMAT (2);

Parameters

arg_idx

the index of the argument

 

G_DEPRECATED_FOR()

#define G_DEPRECATED_FOR(f) __attribute__((__deprecated__("Use '" #f "' instead")))

This macro is similar to G_GNUC_DEPRECATED_FOR, and can be used to mark functions declarations as deprecated. Unlike G_GNUC_DEPRECATED_FOR, it is meant to be portable across different compilers and must be placed before the function declaration.

1
2
G_DEPRECATED_FOR(my_replacement)
int my_mistake (void);

Parameters

f

the name of the function that this function was deprecated for

 

Since: 2.32


G_UNAVAILABLE()

#define G_UNAVAILABLE(maj,min) __attribute__((deprecated("Not available before " #maj "." #min)))

This macro can be used to mark a function declaration as unavailable. It must be placed before the function declaration. Use of a function that has been annotated with this macros will produce a compiler warning.

Parameters

maj

the major version that introduced the symbol

 

min

the minor version that introduced the symbol

 

Since: 2.32


G_LIKELY()

#define G_LIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 1))

Hints the compiler that the expression is likely to evaluate to a true value. The compiler may use this information for optimizations.

1
2
if (G_LIKELY (random () != 1))
  g_print ("not one");

Parameters

expr

the expression

 

Returns

the value of expr

Since: 2.2


G_UNLIKELY()

#define G_UNLIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 0))

Hints the compiler that the expression is unlikely to evaluate to a true value. The compiler may use this information for optimizations.

1
2
if (G_UNLIKELY (random () == 1))
  g_print ("a random one");

Parameters

expr

the expression

 

Returns

the value of expr

Since: 2.2

Types and Values

G_INLINE_FUNC

#  define G_INLINE_FUNC extern GLIB_DEPRECATED_MACRO_IN_2_48_FOR(static inline)

G_INLINE_FUNC has been deprecated since version 2.48 and should not be used in newly-written code.

Use "static inline" instead

This macro used to be used to conditionally define inline functions in a compatible way before this feature was supported in all compilers. These days, GLib requires inlining support from the compiler, so your GLib-using programs can safely assume that the "inline" keyword works properly.

Never use this macro anymore. Just say "static inline".


g_autofree

#define             g_autofree

Macro to add an attribute to pointer variable to ensure automatic cleanup using g_free().

This macro differs from g_autoptr() in that it is an attribute supplied before the type name, rather than wrapping the type definition. Instead of using a type-specific lookup, this macro always calls g_free() directly.

This means it's useful for any type that is returned from g_malloc().

Otherwise, this macro has similar constraints as g_autoptr(): only supported on GCC and clang, the variable must be initialized, etc.

1
2
3
4
5
6
7
8
9
10
11
12
gboolean
operate_on_malloc_buf (void)
{
  g_autofree guint8* membuf = NULL;

  membuf = g_malloc (8192);

  // Some computation on membuf

  // membuf will be automatically freed here
  return TRUE;
}

Since: 2.44


G_STMT_START

#define G_STMT_START  do

Used within multi-statement macros so that they can be used in places where only one statement is expected by the compiler.


G_STMT_END

#define             G_STMT_END

Used within multi-statement macros so that they can be used in places where only one statement is expected by the compiler.


G_BEGIN_DECLS

#define G_BEGIN_DECLS  extern "C" {

Used (along with G_END_DECLS) to bracket header files. If the compiler in use is a C++ compiler, adds extern "C" around the header.


G_END_DECLS

#define G_END_DECLS    }

Used (along with G_BEGIN_DECLS) to bracket header files. If the compiler in use is a C++ compiler, adds extern "C" around the header.


G_GNUC_EXTENSION

#define G_GNUC_EXTENSION __extension__

Expands to __extension__ when gcc is used as the compiler. This simply tells gcc not to warn about the following non-standard code when compiling with the -pedantic option.


G_GNUC_CONST

#define             G_GNUC_CONST

Expands to the GNU C const function attribute if the compiler is gcc. Declaring a function as const enables better optimization of calls to the function. A const function doesn't examine any values except its parameters, and has no effects except its return value.

Place the attribute after the declaration, just before the semicolon.

1
gchar g_ascii_tolower (gchar c) G_GNUC_CONST;

See the GNU C documentation for more details.

A function that has pointer arguments and examines the data pointed to must not be declared const. Likewise, a function that calls a non-const function usually must not be const. It doesn't make sense for a const function to return void.


G_GNUC_PURE

#define G_GNUC_PURE __attribute__((__pure__))

Expands to the GNU C pure function attribute if the compiler is gcc. Declaring a function as pure enables better optimization of calls to the function. A pure function has no effects except its return value and the return value depends only on the parameters and/or global variables.

Place the attribute after the declaration, just before the semicolon.

1
gboolean g_type_check_value (const GValue *value) G_GNUC_PURE;

See the GNU C documentation for more details.


G_GNUC_MALLOC

#define G_GNUC_MALLOC __attribute__((__malloc__))

Expands to the GNU C malloc function attribute if the compiler is gcc. Declaring a function as malloc enables better optimization of the function, but must only be done if the allocation behaviour of the function is fully understood, otherwise miscompilation can result.

A function can have the malloc attribute if it returns a pointer which is guaranteed to not alias with any other pointer valid when the function returns, and moreover no pointers to valid objects occur in any storage addressed by the returned pointer.

In practice, this means that G_GNUC_MALLOC can be used with any function which returns unallocated or zeroed-out memory, but not with functions which return initialised structures containing other pointers, or with functions that reallocate memory. This definition changed in GLib 2.58 to match the stricter definition introduced around GCC 5.

Place the attribute after the declaration, just before the semicolon.

1
gpointer g_malloc (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);

See the GNU C documentation for more details.

Since: 2.6


G_GNUC_DEPRECATED

#define G_GNUC_DEPRECATED __attribute__((__deprecated__))

Expands to the GNU C deprecated attribute if the compiler is gcc. It can be used to mark typedefs, variables and functions as deprecated. When called with the -Wdeprecated-declarations option, gcc will generate warnings when deprecated interfaces are used.

Place the attribute after the declaration, just before the semicolon.

1
int my_mistake (void) G_GNUC_DEPRECATED;

See the GNU C documentation for more details.

Since: 2.2


G_GNUC_BEGIN_IGNORE_DEPRECATIONS

#define             G_GNUC_BEGIN_IGNORE_DEPRECATIONS

Tells gcc (if it is a new enough version) to temporarily stop emitting warnings when functions marked with G_GNUC_DEPRECATED or G_GNUC_DEPRECATED_FOR are called. This is useful for when you have one deprecated function calling another one, or when you still have regression tests for deprecated functions.

Use G_GNUC_END_IGNORE_DEPRECATIONS to begin warning again. (If you are not compiling with -Wdeprecated-declarations then neither macro has any effect.)

This macro can be used either inside or outside of a function body, but must appear on a line by itself. Both this macro and the corresponding G_GNUC_END_IGNORE_DEPRECATIONS are considered statements, so they should not be used around branching or loop conditions; for instance, this use is invalid:

1
2
3
4
5
6
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
if (check == some_deprecated_function ())
G_GNUC_END_IGNORE_DEPRECATIONS
  {
    do_something ();
  }

and you should move the deprecated section outside the condition

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Solution A
some_data_t *res;

G_GNUC_BEGIN_IGNORE_DEPRECATIONS
res = some_deprecated_function ();
G_GNUC_END_IGNORE_DEPRECATIONS

if (check == res)
  {
    do_something ();
  }

// Solution B
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
if (check == some_deprecated_function ())
  {
    do_something ();
  }
G_GNUC_END_IGNORE_DEPRECATIONS

1
2
3
4
5
6
7
static void
test_deprecated_function (void)
{
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS
  g_assert_cmpint (my_mistake (), ==, 42);
  G_GNUC_END_IGNORE_DEPRECATIONS
}

Since: 2.32


G_GNUC_END_IGNORE_DEPRECATIONS

#define             G_GNUC_END_IGNORE_DEPRECATIONS

Undoes the effect of G_GNUC_BEGIN_IGNORE_DEPRECATIONS, telling gcc to begin outputting warnings again (assuming those warnings had been enabled to begin with).

This macro can be used either inside or outside of a function body, but must appear on a line by itself.

Since: 2.32


G_GNUC_NORETURN

#define             G_GNUC_NORETURN

Expands to the GNU C noreturn function attribute if the compiler is gcc. It is used for declaring functions which never return. It enables optimization of the function, and avoids possible compiler warnings.

Since 2.68, it is recommended that code uses G_NORETURN instead of G_GNUC_NORETURN, as that works on more platforms and compilers (in particular, MSVC and C++11) than G_GNUC_NORETURN, which works with GCC and Clang only. G_GNUC_NORETURN continues to work, so has not been deprecated yet.

Place the attribute after the declaration, just before the semicolon.

1
void g_abort (void) G_GNUC_NORETURN;

See the GNU C documentation for more details.


G_GNUC_FALLTHROUGH

#define G_GNUC_FALLTHROUGH __attribute__((fallthrough))

Expands to the GNU C fallthrough statement attribute if the compiler supports it. This allows declaring case statement to explicitly fall through in switch statements. To enable this feature, use -Wimplicit-fallthrough during compilation.

Put the attribute right before the case statement you want to fall through to.

1
2
3
4
5
6
7
8
9
switch (foo)
  {
    case 1:
      g_message ("it's 1");
      G_GNUC_FALLTHROUGH;
    case 2:
      g_message ("it's either 1 or 2");
      break;
  }

See the GNU C documentation for more details.

Since: 2.60


G_GNUC_UNUSED

#define             G_GNUC_UNUSED

Expands to the GNU C unused function attribute if the compiler is gcc. It is used for declaring functions and arguments which may never be used. It avoids possible compiler warnings.

For functions, place the attribute after the declaration, just before the semicolon. For arguments, place the attribute at the beginning of the argument declaration.

1
2
void my_unused_function (G_GNUC_UNUSED gint unused_argument,
                         gint other_argument) G_GNUC_UNUSED;

See the GNU C documentation for more details.


G_GNUC_NULL_TERMINATED

#define G_GNUC_NULL_TERMINATED __attribute__((__sentinel__))

Expands to the GNU C sentinel function attribute if the compiler is gcc. This function attribute only applies to variadic functions and instructs the compiler to check that the argument list is terminated with an explicit NULL.

Place the attribute after the declaration, just before the semicolon.

1
2
gchar *g_strconcat (const gchar *string1,
                    ...) G_GNUC_NULL_TERMINATED;

See the GNU C documentation for more details.

Since: 2.8


G_GNUC_WARN_UNUSED_RESULT

#define G_GNUC_WARN_UNUSED_RESULT __attribute__((warn_unused_result))

Expands to the GNU C warn_unused_result function attribute if the compiler is gcc. This function attribute makes the compiler emit a warning if the result of a function call is ignored.

Place the attribute after the declaration, just before the semicolon.

1
2
GList *g_list_append (GList *list,
                      gpointer data) G_GNUC_WARN_UNUSED_RESULT;

See the GNU C documentation for more details.

Since: 2.10


G_GNUC_FUNCTION

#define G_GNUC_FUNCTION         __FUNCTION__ GLIB_DEPRECATED_MACRO_IN_2_26_FOR(G_STRFUNC)

G_GNUC_FUNCTION has been deprecated since version 2.16 and should not be used in newly-written code.

Use G_STRFUNC() instead

Expands to "" on all modern compilers, and to __FUNCTION__ on gcc version 2.x. Don't use it.


G_GNUC_PRETTY_FUNCTION

#define G_GNUC_PRETTY_FUNCTION  __PRETTY_FUNCTION__ GLIB_DEPRECATED_MACRO_IN_2_26_FOR(G_STRFUNC)

G_GNUC_PRETTY_FUNCTION has been deprecated since version 2.16 and should not be used in newly-written code.

Use G_STRFUNC() instead

Expands to "" on all modern compilers, and to __PRETTY_FUNCTION__ on gcc version 2.x. Don't use it.


G_GNUC_NO_INLINE

#define G_GNUC_NO_INLINE __attribute__((noinline))

Expands to the GNU C noinline function attribute if the compiler is gcc. If the compiler is not gcc, this macro expands to nothing.

Declaring a function as noinline prevents the function from being considered for inlining.

The attribute may be placed before the declaration or definition, right before the static keyword.

1
2
3
4
5
6
G_GNUC_NO_INLINE
static int
do_not_inline_this (void)
{
  ...
}

See the GNU C documentation for more details.

Since: 2.58


G_GNUC_NO_INSTRUMENT

#define             G_GNUC_NO_INSTRUMENT

Expands to the GNU C no_instrument_function function attribute if the compiler is gcc. Functions with this attribute will not be instrumented for profiling, when the compiler is called with the -finstrument-functions option.

Place the attribute after the declaration, just before the semicolon.

1
int do_uninteresting_things (void) G_GNUC_NO_INSTRUMENT;

See the GNU C documentation for more details.


G_HAVE_GNUC_VISIBILITY

#define G_HAVE_GNUC_VISIBILITY 1

Defined to 1 if gcc-style visibility handling is supported.


G_GNUC_INTERNAL

#define G_GNUC_INTERNAL __attribute__((visibility("hidden")))

This attribute can be used for marking library functions as being used internally to the library only, which may allow the compiler to handle function calls more efficiently. Note that static functions do not need to be marked as internal in this way. See the GNU C documentation for details.

When using a compiler that supports the GNU C hidden visibility attribute, this macro expands to __attribute__((visibility("hidden"))). When using the Sun Studio compiler, it expands to __hidden.

Note that for portability, the attribute should be placed before the function declaration. While GCC allows the macro after the declaration, Sun Studio does not.

1
2
3
4
5
G_GNUC_INTERNAL
void _g_log_fallback_handler (const gchar    *log_domain,
                              GLogLevelFlags  log_level,
                              const gchar    *message,
                              gpointer        unused_data);

Since: 2.6


G_GNUC_MAY_ALIAS

#define G_GNUC_MAY_ALIAS __attribute__((may_alias))

Expands to the GNU C may_alias type attribute if the compiler is gcc. Types with this attribute will not be subjected to type-based alias analysis, but are assumed to alias with any other type, just like char.

See the GNU C documentation for details.

Since: 2.14


G_DEPRECATED

#define G_DEPRECATED __attribute__((__deprecated__))

This macro is similar to G_GNUC_DEPRECATED, and can be used to mark functions declarations as deprecated. Unlike G_GNUC_DEPRECATED, it is meant to be portable across different compilers and must be placed before the function declaration.

1
2
G_DEPRECATED
int my_mistake (void);

Since: 2.32


G_STRLOC

#define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__) ":" __PRETTY_FUNCTION__ "()"

Expands to a string identifying the current code position.


G_STRFUNC

#define G_STRFUNC     ((const char*) (__PRETTY_FUNCTION__))

Expands to a string identifying the current function.

Since: 2.4