all: avoid "-Wunreachable-code-generic-assoc" warning with _Generic()

Clang 15 ([1], [2]) added

  Added the -Wunreachable-code-generic-assoc diagnostic flag (grouped
  under the -Wunreachable-code flag) which is enabled by default and warns
  the user about _Generic selection associations which are unreachable
  because the type specified is an array type or a qualified type.

This causes compiler warnings with various uses of _Generic():

  ../src/libnm-glib-aux/nm-shared-utils.h:2489:12: error: due to lvalue conversion of the controlling expression, association of type 'const char *const *const' will never be selected becaus
  e it is qualified [-Werror,-Wunreachable-code-generic-assoc]
      return nm_strv_find_first((const char *const *) strv->pdata, strv->len, str);
             ^
  ../src/libnm-glib-aux/nm-shared-utils.h:475:25: note: expanded from macro 'nm_strv_find_first'
      _nm_strv_find_first(NM_CAST_STRV_CC(list), (len), (needle))
                          ^
  ../src/libnm-glib-aux/nm-macros-internal.h:397:22: note: expanded from macro 'NM_CAST_STRV_CC'
                 const char *const*const: (const char *const*) (value), \
                       ^

Clang is correct.

[1] https://releases.llvm.org/15.0.0/tools/clang/docs/ReleaseNotes.html#improvements-to-clang-s-diagnostics
[2] https://reviews.llvm.org/D125259
This commit is contained in:
Thomas Haller 2022-10-11 11:07:25 +02:00
parent 8aa8d483f2
commit b28c6ca30e
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
3 changed files with 36 additions and 77 deletions

View file

@ -11,15 +11,12 @@
#if !_NM_CC_SUPPORT_GENERIC
#define _NM_DEVICE_CAST(self) ((NMDevice *) (self))
#elif !defined(_NMLOG_DEVICE_TYPE)
#define _NM_DEVICE_CAST(self) \
_Generic((self), NMDevice * : ((NMDevice *) (self)), NMDevice *const : ((NMDevice *) (self)))
#define _NM_DEVICE_CAST(self) _Generic((self), NMDevice * : ((NMDevice *) (self)))
#else
#define _NM_DEVICE_CAST(self) \
_Generic((self), \
_NMLOG_DEVICE_TYPE * : ((NMDevice *) (self)), \
_NMLOG_DEVICE_TYPE * const: ((NMDevice *) (self)), \
NMDevice * : ((NMDevice *) (self)), \
NMDevice * const: ((NMDevice *) (self)))
#define _NM_DEVICE_CAST(self) \
_Generic((self), _NMLOG_DEVICE_TYPE * \
: ((NMDevice *) (self)), NMDevice * \
: ((NMDevice *) (self)))
#endif
#undef _NMLOG_ENABLED

View file

@ -236,68 +236,40 @@ NM_G_ERROR_MSG(GError *error)
#if _NM_CC_SUPPORT_GENERIC
#define _NM_CONSTCAST_FULL_1(type, obj_expr, obj) \
(_Generic ((obj_expr), \
const void *const: ((const type *) (obj)), \
const void * : ((const type *) (obj)), \
void *const: (( type *) (obj)), \
void * : (( type *) (obj)), \
const type *const: ((const type *) (obj)), \
const type * : ((const type *) (obj)), \
type *const: (( type *) (obj)), \
type * : (( type *) (obj))))
const void *: ((const type *) (obj)), \
void *: (( type *) (obj)), \
const type *: ((const type *) (obj)), \
type *: (( type *) (obj))))
#define _NM_CONSTCAST_FULL_2(type, obj_expr, obj, alias_type2) \
(_Generic ((obj_expr), \
const void *const: ((const type *) (obj)), \
const void * : ((const type *) (obj)), \
void *const: (( type *) (obj)), \
void * : (( type *) (obj)), \
const alias_type2 *const: ((const type *) (obj)), \
const alias_type2 * : ((const type *) (obj)), \
alias_type2 *const: (( type *) (obj)), \
alias_type2 * : (( type *) (obj)), \
const type *const: ((const type *) (obj)), \
const type * : ((const type *) (obj)), \
type *const: (( type *) (obj)), \
type * : (( type *) (obj))))
const void *: ((const type *) (obj)), \
void *: (( type *) (obj)), \
const alias_type2 *: ((const type *) (obj)), \
alias_type2 *: (( type *) (obj)), \
const type *: ((const type *) (obj)), \
type *: (( type *) (obj))))
#define _NM_CONSTCAST_FULL_3(type, obj_expr, obj, alias_type2, alias_type3) \
(_Generic ((obj_expr), \
const void *const: ((const type *) (obj)), \
const void * : ((const type *) (obj)), \
void *const: (( type *) (obj)), \
void * : (( type *) (obj)), \
const alias_type2 *const: ((const type *) (obj)), \
const alias_type2 * : ((const type *) (obj)), \
alias_type2 *const: (( type *) (obj)), \
alias_type2 * : (( type *) (obj)), \
const alias_type3 *const: ((const type *) (obj)), \
const alias_type3 * : ((const type *) (obj)), \
alias_type3 *const: (( type *) (obj)), \
alias_type3 * : (( type *) (obj)), \
const type *const: ((const type *) (obj)), \
const type * : ((const type *) (obj)), \
type *const: (( type *) (obj)), \
type * : (( type *) (obj))))
const void *: ((const type *) (obj)), \
void *: (( type *) (obj)), \
const alias_type2 *: ((const type *) (obj)), \
alias_type2 *: (( type *) (obj)), \
const alias_type3 *: ((const type *) (obj)), \
alias_type3 *: (( type *) (obj)), \
const type *: ((const type *) (obj)), \
type *: (( type *) (obj))))
#define _NM_CONSTCAST_FULL_4(type, obj_expr, obj, alias_type2, alias_type3, alias_type4) \
(_Generic ((obj_expr), \
const void *const: ((const type *) (obj)), \
const void * : ((const type *) (obj)), \
void *const: (( type *) (obj)), \
void * : (( type *) (obj)), \
const alias_type2 *const: ((const type *) (obj)), \
const alias_type2 * : ((const type *) (obj)), \
alias_type2 *const: (( type *) (obj)), \
alias_type2 * : (( type *) (obj)), \
const alias_type3 *const: ((const type *) (obj)), \
const alias_type3 * : ((const type *) (obj)), \
alias_type3 *const: (( type *) (obj)), \
alias_type3 * : (( type *) (obj)), \
const alias_type4 *const: ((const type *) (obj)), \
const alias_type4 * : ((const type *) (obj)), \
alias_type4 *const: (( type *) (obj)), \
alias_type4 * : (( type *) (obj)), \
const type *const: ((const type *) (obj)), \
const type * : ((const type *) (obj)), \
type *const: (( type *) (obj)), \
type * : (( type *) (obj))))
const void *: ((const type *) (obj)), \
void *: (( type *) (obj)), \
const alias_type2 *: ((const type *) (obj)), \
alias_type2 *: (( type *) (obj)), \
const alias_type3 *: ((const type *) (obj)), \
alias_type3 *: (( type *) (obj)), \
const alias_type4 *: ((const type *) (obj)), \
alias_type4 *: (( type *) (obj)), \
const type *: ((const type *) (obj)), \
type *: (( type *) (obj))))
#define _NM_CONSTCAST_FULL_x(type, obj_expr, obj, n, ...) \
(_NM_CONSTCAST_FULL_##n(type, obj_expr, obj, ##__VA_ARGS__))
#define _NM_CONSTCAST_FULL_y(type, obj_expr, obj, n, ...) \
@ -393,13 +365,7 @@ NM_G_ERROR_MSG(GError *error)
char *const*: (const char *const*) (value), \
char * *: (const char *const*) (value), \
const void *: (const char *const*) (value), \
void *: (const char *const*) (value), \
const char *const*const: (const char *const*) (value), \
const char * *const: (const char *const*) (value), \
char *const*const: (const char *const*) (value), \
char * *const: (const char *const*) (value), \
const void *const: (const char *const*) (value), \
void *const: (const char *const*) (value)))
void *: (const char *const*) (value)))
#else
#define NM_CAST_STRV_MC(value) ((const char **) (value))
#define NM_CAST_STRV_CC(value) ((const char *const *) (value))

View file

@ -283,12 +283,8 @@ typedef uint64_t _nm_bitwise nm_be64_t;
* It's useful to check the let the compiler ensure that @value is
* of a certain type. */
#define _NM_ENSURE_TYPE(type, value) (_Generic((value), type : (value)))
#define _NM_ENSURE_TYPE_CONST(type, value) \
(_Generic((value), const type \
: ((const type)(value)), const type const \
: ((const type)(value)), type \
: ((const type)(value)), type const \
: ((const type)(value))))
#define _NM_ENSURE_TYPE_CONST(type, value) \
(_Generic((value), const type : ((const type)(value)), type : ((const type)(value))))
#else
#define _NM_ENSURE_TYPE(type, value) (value)
#define _NM_ENSURE_TYPE_CONST(type, value) ((const type)(value))