[PATCH 1/5] api: don't use intermediate variables for deprecation warnings

Aleksander Morgado aleksander at aleksander.es
Sun Aug 20 20:50:08 UTC 2017


On Sun, Aug 20, 2017 at 7:09 PM, Ben Chan <benchan at chromium.org> wrote:
>
>
> On Sun, Aug 20, 2017 at 9:37 AM, Aleksander Morgado
> <aleksander at aleksander.es> wrote:
>>
>> Using an intermediate constant variable breaks compilation with C
>> compilers, as these variables cannot be used as initializers.
>>
>
> Aleksander, do you have the compiler version and the code snippet that
> causes the compiler error?
>

Here's an example:

/* test.c */
#include <stdio.h>
#include <ModemManager.h>

typedef struct {
  MMModemBand  band;
  char        *str;
} BandName;

static const BandName names[] = {
  { .band = MM_MODEM_BAND_EUTRAN_XLII, "e-utran 43" },
};

int main(int argc, char **argv)
{
  guint i;

  for (i = 0; i < G_N_ELEMENTS (names); i++)
    printf ("band '%s' has id %u\n", names[i].str, names[i].band);
  return 0;
}

Compilation fails with "error: initializer element is not constant":
$ gcc -o test `pkg-config --cflags --libs ModemManager glib-2.0` test.c
test.c:12:2: warning: ‘MM_DEPRECATED_MODEM_BAND_EUTRAN_XLII’ is
deprecated: Use 'MM_MODEM_BAND_EUTRAN_42' instead
[-Wdeprecated-declarations]
  { .band = MM_MODEM_BAND_EUTRAN_XLII, "e-utran 43" },
  ^
In file included from /usr/include/ModemManager/ModemManager.h:41:0,
                 from test.c:4:
/usr/include/ModemManager/ModemManager-compat.h:441:18: note: declared here
 static const int MM_DEPRECATED_MODEM_BAND_EUTRAN_XLII =
MM_MODEM_BAND_EUTRAN_42;
                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/ModemManager/ModemManager-compat.h:442:35: error:
initializer element is not constant
 #define MM_MODEM_BAND_EUTRAN_XLII MM_DEPRECATED_MODEM_BAND_EUTRAN_XLII
                                   ^
/usr/include/ModemManager/ModemManager-compat.h:442:35: note: in
definition of macro ‘MM_MODEM_BAND_EUTRAN_XLII’
 #define MM_MODEM_BAND_EUTRAN_XLII MM_DEPRECATED_MODEM_BAND_EUTRAN_XLII
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/ModemManager/ModemManager-compat.h:442:35: note: (near
initialization for ‘names[0].band’)
 #define MM_MODEM_BAND_EUTRAN_XLII MM_DEPRECATED_MODEM_BAND_EUTRAN_XLII
                                   ^
/usr/include/ModemManager/ModemManager-compat.h:442:35: note: in
definition of macro ‘MM_MODEM_BAND_EUTRAN_XLII’
 #define MM_MODEM_BAND_EUTRAN_XLII MM_DEPRECATED_MODEM_BAND_EUTRAN_XLII
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

$ gcc --version
gcc (GCC) 7.1.1 20170630

For what I could gather browsing; using constant variables as
initializers is valid C++, but not C. The approach I took for the
patch is what the GTK+ did when deprecating GtkStock macros (e.g.
https://github.com/GNOME/gtk/blob/7173df1dd1a8f45c47b75d30bfbfaa67f33d8e9b/gtk/deprecated/gtkstock.h#L102)

-- 
Aleksander
https://aleksander.es


More information about the ModemManager-devel mailing list