shared: optimize nm_utils_error_set() for string literals

If there is only one argument, we can assume this is a plain string.

That is especially the case, because g_set_error() is G_GNUC_PRINTF()
and would warn if this would be a format string with missing parameters.

This is for convenience. Previously, one was compelled to explicitly
choose between nm_utils_error_set_literal() and nm_utils_error_set().
Now, it automatically chooses.

Note that there are a few things that won't work, like

  nm_utils_error_set (error, code, "bogus %u escape");

But that's good. You get a compiler warning (as you used to)
and it's clear in this case you really need
nm_utils_error_set_literal().
This commit is contained in:
Thomas Haller 2019-07-02 16:14:11 +02:00
parent 7f75a1b5f5
commit 6d30021fee

View file

@ -759,7 +759,13 @@ nm_utils_error_set_literal (GError **error, int error_code, const char *literal)
}
#define nm_utils_error_set(error, error_code, ...) \
g_set_error ((error), NM_UTILS_ERROR, error_code, __VA_ARGS__)
G_STMT_START { \
if (NM_NARG (__VA_ARGS__) == 1) { \
g_set_error_literal ((error), NM_UTILS_ERROR, (error_code), _NM_UTILS_MACRO_FIRST (__VA_ARGS__)); \
} else { \
g_set_error ((error), NM_UTILS_ERROR, (error_code), __VA_ARGS__); \
} \
} G_STMT_END
#define nm_utils_error_set_errno(error, errsv, fmt, ...) \
G_STMT_START { \