Merge branch 'mark_g_assert_as_noreturn_on_MSVC' into 'master'

Adding macros G_NORETURN and G_NORETURN_FUNCPTR

Closes #994

See merge request GNOME/glib!1078
This commit is contained in:
Philip Withnall 2020-11-25 11:52:23 +00:00
commit 3088fbae52
10 changed files with 93 additions and 8 deletions

View File

@ -469,6 +469,8 @@ G_ALIGNOF
<SUBSECTION>
G_CONST_RETURN
G_NORETURN
G_NORETURN_FUNCPTR
<SUBSECTION>
G_N_ELEMENTS

View File

@ -37,7 +37,7 @@ static const gchar *info = NULL;
static GCancellable *cancellable = NULL;
static gint return_value = 0;
static void G_GNUC_NORETURN
static G_NORETURN void
usage (void)
{
fprintf (stderr, "Usage: proxy [-s] (uri|host:port|ip:port|path|srv/protocol/domain)\n");

View File

@ -40,7 +40,7 @@ static gboolean synchronous = FALSE;
static guint connectable_count = 0;
static GResolverRecordType record_type = 0;
static void G_GNUC_NORETURN
static G_NORETURN void
usage (void)
{
fprintf (stderr, "Usage: resolver [-s] [hostname | IP | service/protocol/domain ] ...\n");

View File

@ -423,6 +423,12 @@
* 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.
*
* |[<!-- language="C" -->
@ -917,6 +923,76 @@
#define G_CONST_RETURN const GLIB_DEPRECATED_MACRO_IN_2_30_FOR(const)
#endif
/**
* G_NORETURN:
*
* Expands to the GNU C or MSVC `noreturn` function attribute depending on
* the compiler. It is used for declaring functions which never return.
* Enables optimization of the function, and avoids possible compiler warnings.
*
* Note that %G_NORETURN supersedes the previous %G_GNUC_NORETURN macro, which
* will eventually be deprecated. %G_NORETURN supports more platforms.
*
* Place the attribute before the function declaration as follows:
*
* |[<!-- language="C" -->
* G_NORETURN void g_abort (void);
* ]|
*
* Since: 2.68
*/
/* Note: We cant annotate this with GLIB_AVAILABLE_MACRO_IN_2_68 because its
* used within the GLib headers in function declarations which are always
* evaluated when a header is included. This results in warnings in third party
* code which includes glib.h, even if the third party code doesnt use the new
* macro itself. */
#if (3 <= __GNUC__ || (__GNUC__ == 2 && 8 <= __GNUC_MINOR__)) || (0x5110 <= __SUNPRO_C)
/* For compatibility with G_NORETURN_FUNCPTR on clang, use
__attribute__((__noreturn__)), not _Noreturn. */
# define G_NORETURN __attribute__ ((__noreturn__))
#elif 1200 <= _MSC_VER
/* Use MSVC specific syntax. */
# define G_NORETURN __declspec (noreturn)
/* Use ISO C++11 syntax when the compiler supports it. */
#elif (__cplusplus >= 201103 && !(__GNUC__ == 4 && __GNUC_MINOR__ == 7)) || (_MSC_VER >= 1900)
# define G_NORETURN [[noreturn]]
/* Use ISO C11 syntax when the compiler supports it. */
#elif __STDC_VERSION__ >= 201112 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)
# define G_NORETURN _Noreturn
#else
# define G_NORETURN /* empty */
#endif
/**
* G_NORETURN_FUNCPTR:
*
* Expands to the GNU C or MSVC `noreturn` function attribute depending on
* the compiler. It is used for declaring function pointers which never return.
* Enables optimization of the function, and avoids possible compiler warnings.
*
* Place the attribute before the function declaration as follows:
*
* |[<!-- language="C" -->
* G_NORETURN_FUNCPTR void (*funcptr) (void);
* ]|
*
* Note that if the function is not a function pointer, you can simply use
* the %G_NORETURN macro as follows:
*
* |[<!-- language="C" -->
* G_NORETURN void g_abort (void);
* ]|
*
* Since: 2.68
*/
#if (3 <= __GNUC__ || (__GNUC__ == 2 && 8 <= __GNUC_MINOR__)) || (0x5110 <= __SUNPRO_C)
# define G_NORETURN_FUNCPTR __attribute__ ((__noreturn__)) \
GLIB_AVAILABLE_MACRO_IN_2_68
#else
# define G_NORETURN_FUNCPTR /* empty */ \
GLIB_AVAILABLE_MACRO_IN_2_68
#endif
/*
* The G_LIKELY and G_UNLIKELY macros let the programmer give hints to
* the compiler about the expected result of an expression. Some compilers

View File

@ -283,11 +283,12 @@ void g_warn_message (const char *domain,
const char *func,
const char *warnexpr) G_ANALYZER_NORETURN;
GLIB_DEPRECATED
G_NORETURN
void g_assert_warning (const char *log_domain,
const char *file,
const int line,
const char *pretty_function,
const char *expression) G_GNUC_NORETURN;
const char *expression);
GLIB_AVAILABLE_IN_2_56
void g_log_structured_standard (const gchar *log_domain,
@ -399,7 +400,7 @@ void g_log_structured_standard (const gchar *log_domain,
format)
#endif
#else /* no varargs macros */
static void g_error (const gchar *format, ...) G_GNUC_NORETURN G_ANALYZER_NORETURN;
static G_NORETURN void g_error (const gchar *format, ...) G_ANALYZER_NORETURN;
static void g_critical (const gchar *format, ...) G_ANALYZER_NORETURN;
static inline void

View File

@ -1091,7 +1091,7 @@ g_option_context_get_help (GOptionContext *context,
return g_string_free (string, FALSE);
}
G_GNUC_NORETURN
G_NORETURN
static void
print_help (GOptionContext *context,
gboolean main_help,

View File

@ -1116,7 +1116,7 @@ write_all (gint fd, gconstpointer vbuf, gsize to_write)
/* This function is called between fork() and exec() and hence must be
* async-signal-safe (see signal-safety(7)). */
G_GNUC_NORETURN
G_NORETURN
static void
write_err_and_exit (gint fd, gint msg)
{

View File

@ -514,11 +514,12 @@ void g_assertion_message (const char *domain,
const char *func,
const char *message) G_ANALYZER_NORETURN;
GLIB_AVAILABLE_IN_ALL
G_NORETURN
void g_assertion_message_expr (const char *domain,
const char *file,
int line,
const char *func,
const char *expr) G_GNUC_NORETURN;
const char *expr);
GLIB_AVAILABLE_IN_ALL
void g_assertion_message_cmpstr (const char *domain,
const char *file,

View File

@ -434,7 +434,7 @@ g_bit_storage_impl (gulong number)
# define g_abort() abort ()
#else
GLIB_AVAILABLE_IN_2_50
void g_abort (void) G_GNUC_NORETURN G_ANALYZER_NORETURN;
G_NORETURN void g_abort (void) G_ANALYZER_NORETURN;
#endif
#endif

View File

@ -24,6 +24,11 @@
#pragma warning(disable:4101) /* unreferenced local variable */
#pragma warning(error:4150)
/* G_NORETURN */
#pragma warning(error:4646) /* function declared with __declspec(noreturn) has non-void return type */
#pragma warning(error:4715) /* 'function': not all control paths return a value */
#pragma warning(error:4098) /* 'void' function returning a value */
#pragma warning(disable:4244) /* No possible loss of data warnings */
#pragma warning(disable:4305) /* No truncation from int to char warnings */