Commit graph

48 commits

Author SHA1 Message Date
Thomas Haller
921af527f7
std-aux: cleanup NM_CMP_*() macros
- add code comments explaining some things.

- for NM_CMP_FIELD*() variants have a corresponding NM_CMP_DIRECT*()
  macro and use it (aside the "memcmp" variants, which don't translate
  directly).
2022-06-09 09:52:51 +02:00
Thomas Haller
0b7dc4137d
std-aux: include default std-aux headers by "nm-default-std.h" 2022-05-09 19:20:19 +02:00
Thomas Haller
80a19958dc
code-style: fix wrong indentation for code comments 2022-05-09 19:20:18 +02:00
Thomas Haller
532f3e34a8
glib-aux: drop nm_str_buf_init() for NM_STR_BUF_INIT()
NM_STR_BUF_INIT() and nm_str_buf_init() were pretty much redundant. Drop one of
them.

Usually our pattern is that we don't have functions that return structs.
But NM_STR_BUF_INIT() returns a struct, because it's convenient to use
with

  nm_auto_str_buf NMStrBuf strbuf = NM_STR_BUF_INIT(...);

So use that variant instead.
2022-05-09 19:18:30 +02:00
Thomas Haller
2c5bacd416
std-aux: add NM_UTILS_GET_NEXT_REALLOC_SIZE_488 define 2022-04-13 09:23:28 +02:00
Thomas Haller
f307e12080
std-aux: cast NM_IN_SET_SET() operands to "int" for "-Wbitwise-instead-of-logical" warning
Clang 14 has a new warning "-Wbitwise-instead-of-logical", and it warns
about our usage with NM_IN_SET_SE()/NM_IN_STRSET_SE(). It complains that we
are using '|' with boolean operands. Which is true (and intended), as we bitwise-or
the result of the '==' comparisons.

Work around the warning by casting the operands to "int". Note that
in C, the comparison operators have already a type "int", so this cast
should not result in any changes in the compiled code.

    ../src/libnm-core-impl/tests/test-general.c:9415:17: error: use of bitwise '|' with boolean operands [-Werror,-Wbitwise-instead-of-logical]
        _ASSERT(2, !NM_IN_SET_SE(-1, G(1), G(2)));
        ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ../src/libnm-std-aux/nm-std-aux.h:800:30: note: expanded from macro 'NM_IN_SET_SE'
    #define NM_IN_SET_SE(x, ...) _NM_IN_SET(|, typeof(x), x, __VA_ARGS__)
                                 ^
    ../src/libnm-std-aux/nm-std-aux.h:789:39: note: expanded from macro '_NM_IN_SET'
            !!(NM_VA_ARGS_FOREACH(, , op, _NM_IN_SET_OP, __VA_ARGS__)); \
                                          ^
    ../src/libnm-std-aux/nm-std-aux.h:772:20: note: expanded from macro 'NM_VA_ARGS_FOREACH'
                       op,                                                   \
                       ^
    note: (skipping 7 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
    ../src/libnm-glib-aux/nm-macros-internal.h:1603:47: note: expanded from macro '_G_BOOLEAN_EXPR'
    #define _G_BOOLEAN_EXPR(expr) NM_BOOLEAN_EXPR(expr)
                                  ~~~~~~~~~~~~~~~~^~~~~
    ../src/libnm-std-aux/nm-std-aux.h:167:62: note: expanded from macro 'NM_BOOLEAN_EXPR'
    #define NM_BOOLEAN_EXPR(expr) _NM_BOOLEAN_EXPR_IMPL(NM_UNIQ, expr)
                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
    ../src/libnm-std-aux/nm-std-aux.h:161:13: note: expanded from macro '_NM_BOOLEAN_EXPR_IMPL'
            if (expr)                      \
                ^~~~
    ../src/libnm-core-impl/tests/test-general.c:9415:17: note: cast one or both operands to int to silence this warning
    ../src/libnm-std-aux/nm-std-aux.h:800:30: note: expanded from macro 'NM_IN_SET_SE'
    #define NM_IN_SET_SE(x, ...) _NM_IN_SET(|, typeof(x), x, __VA_ARGS__)
                                 ^
    ../src/libnm-std-aux/nm-std-aux.h:789:39: note: expanded from macro '_NM_IN_SET'
            !!(NM_VA_ARGS_FOREACH(, , op, _NM_IN_SET_OP, __VA_ARGS__)); \
                                          ^
2022-03-16 00:57:17 +01:00
Thomas Haller
71f53d4069
std-aux: add code comment for NM_STR_HAS_PREFIX()/NM_STR_HAS_SUFFIX() 2022-03-09 09:10:45 +01:00
Thomas Haller
615221a99c format: reformat source tree with clang-format 13.0
We use clang-format for automatic formatting of our source files.
Since clang-format is actively maintained software, the actual
formatting depends on the used version of clang-format. That is
unfortunate and painful, but really unavoidable unless clang-format
would be strictly bug-compatible.

So the version that we must use is from the current Fedora release, which
is also tested by our gitlab-ci. Previously, we were using Fedora 34 with
clang-tools-extra-12.0.1-1.fc34.x86_64.

As Fedora 35 comes along, we need to update our formatting as Fedora 35
comes with version "13.0.0~rc1-1.fc35".
An alternative would be to freeze on version 12, but that has different
problems (like, it's cumbersome to rebuild clang 12 on Fedora 35 and it
would be cumbersome for our developers which are on Fedora 35 to use a
clang that they cannot easily install).

The (differently painful) solution is to reformat from time to time, as we
switch to a new Fedora (and thus clang) version.
Usually we would expect that such a reformatting brings minor changes.
But this time, the changes are huge. That is mentioned in the release
notes [1] as

  Makes PointerAligment: Right working with AlignConsecutiveDeclarations. (Fixes https://llvm.org/PR27353)

[1] https://releases.llvm.org/13.0.0/tools/clang/docs/ReleaseNotes.html#clang-format
2021-11-29 09:31:09 +00:00
Harald van Dijk
58bfcffd59 core: support linking with LLD 13
LLD 13 adds -z start-stop-gc and makes it the default, resulting in:

    CCLD     src/core/NetworkManager-all-sym
  ld.lld: error: undefined symbol: __stop_connection_defaults
  >>> referenced by nm-config.c:0 (src/core/nm-config.c:0)
  >>>               libNetworkManager_la-nm-config.o:(read_config) in archive src/core/.libs/libNetworkManager.a
  >>> referenced by nm-config-data.c:1598 (src/core/nm-config-data.c:1598)
  >>>               libNetworkManager_la-nm-config-data.o:(nm_config_data_get_connection_default) in archive src/core/.libs/libNetworkManager.a
  >>> referenced by nm-config-data.c:0 (src/core/nm-config-data.c:0)
  >>>               libNetworkManager_la-nm-config-data.o:(nm_config_data_get_connection_default) in archive src/core/.libs/libNetworkManager.a

  ld.lld: error: undefined symbol: __start_connection_defaults
  >>> referenced by nm-config.c:0 (src/core/nm-config.c:0)
  >>>               libNetworkManager_la-nm-config.o:(read_config) in archive src/core/.libs/libNetworkManager.a
  >>> referenced by nm-config.c:0 (src/core/nm-config.c:0)
  >>>               libNetworkManager_la-nm-config.o:(read_config) in archive src/core/.libs/libNetworkManager.a
  >>> referenced by nm-config.c:0 (src/core/nm-config.c:0)
  >>>               libNetworkManager_la-nm-config.o:(read_config) in archive src/core/.libs/libNetworkManager.a
  >>> referenced 2 more times
  clang: error: linker command failed with exit code 1 (use -v to see invocation)

Add __attribute__((__retain__)) to prevent GC of the connection
defaults.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1008
2021-11-02 12:42:51 +01:00
Thomas Haller
1c85bc5ead
std-aux: work around "-Wunused-but-set-variable" warning with clang in nm_auto()
We use the cleanup attribute heavily. It's useful for deferring
deallocation. For example, we have code like:

  gs_unref_object NMBluezManager *self_keep_alive = g_object_ref(self);

where we don't use the variable otherwise, except for owning (and
freeing) the reference. This already lead to a compiler warning about
unused variable, which we would workaround with

  _nm_unused gs_unref_object NMBluezManager *self_keep_alive = g_object_ref(self);

With clang 13.0.0~rc1-1.fc35, this got worse. Now for example also

    static inline void
    nm_strvarray_set_strv(GArray **array, const char *const *strv)
    {
        gs_unref_array GArray *array_old = NULL;

        array_old = g_steal_pointer(array);

        if (!strv || !strv[0])
            return;

        nm_strvarray_ensure(array);
        for (; strv[0]; strv++)
            nm_strvarray_add(*array, strv[0]);
    }

leads to a warning

    ./src/libnm-glib-aux/nm-shared-utils.h:3078:28: error: variable array_old set but not used [-Werror,-Wunused-but-set-variable]
        gs_unref_array GArray *array_old = NULL;
                               ^

This is really annoying. We don't want to plaster our code with _nm_unused,
because that might hide actual issues. But we also want to keep using this
pattern and need to avoid the warning.

A problem is also that GCC usually does not warn about truly unused
variables with cleanup attribute. Clang was very useful here to flag
such variables. But now clang warns about cases which are no bugs, which
is a problem. So this does loose some useful warnings. On the other hand,
a truly unused variable (with cleanup attribute) is ugly, but not an actual
problem.

Now, with clang 13, automatically mark nm_auto() variables as _nm_unused
as workaround.
2021-10-08 15:56:10 +02:00
Thomas Haller
a3ca768fbc
std-aux: minor cleanup in nm_steal_int() to compare number explicitly against zero 2021-10-06 17:55:11 +02:00
Thomas Haller
f3def63fed
std-aux: add nm_clear_fd() helper 2021-09-28 15:58:29 +02:00
Thomas Haller
37047aba36
std-aux: add nm_assert_addr_family_or_unspec() and nm_utils_addr_family_other() helpers 2021-09-28 12:56:39 +02:00
Thomas Haller
2a07043489
std-aux: add "libnm-std-aux/nm-linux-compat.h" header to avoid build errors
We have a copy of a few linux user space headers in `src/linux-headers`.
The idea is that we want to use recent kernel API, and not depend on the
kernel UAPI headers installed on the build system (and not need to
workaround that).

However, we may not be able to simply compile them, because they too
have dependencies. For example,

  ../src/linux-headers/ethtool.h:1389:2: error: implicit declaration of function '__KERNEL_DIV_ROUND_UP' [-Werror=implicit-function-declaration]
    __u32 queue_mask[__KERNEL_DIV_ROUND_UP(MAX_NUM_QUEUE, 32)];
    ^

As workaround, don't include headers from "linux-headers" directly,
but only include the new "libnm-std-aux/nm-linux-compat.h" adapter
header, which tries to solve these incompatibilities.

Fixes: 34d48d2596 ('platform: clear all BASE types when setting advertised modes for ethernet autoneg')
2021-09-08 15:27:17 +02:00
Thomas Haller
5e54cb5845
std-aux: let nm_assert() macros return a TRUE value
For most purposes, this makes no difference. But it allows
to write something like:

  if (   ptr
      && nm_assert(check_ptr(ptr))
      && other_check(ptr))
      ...
2021-08-31 16:34:02 +02:00
Thomas Haller
10e0c4261e
format: reformat code with clang-format-12.0.1-1.fc34
The formatting produced by clang-format depends on the version of the
tool. The version that we use is the one of the current Fedora release.

Fedora 34 recently updated clang (and clang-tools-extra) from version
12.0.0 to 12.0.1. This brings some changes.

Update the formatting.
2021-08-30 13:14:00 +02:00
Thomas Haller
ea49b50651
all: add some README.md files describing the purpose of our sources 2021-08-19 17:51:11 +02:00
Thomas Haller
1d789ca44b
std-aux: add XXX() macro 2021-08-11 14:17:24 +02:00
Thomas Haller
f9fa3fbf9f
std-aux: add nm_utils_addr_family_to_str() helper
Like nm_utils_addr_family_to_char(), but gives a different treatment to
AF_UNSPEC to return "" instead of 'X'. As such, it also needs to
return a string and not a char.
2021-08-05 14:59:09 +02:00
Thomas Haller
7459a8c67a
std-aux: use unique temporary variable in NM_IS_IPv4()
NM_IS_IPv4() should work well, also inside other macros and not
be concerned about shadowing another variable.
2021-08-05 14:59:01 +02:00
Thomas Haller
17f0acfdb3
std-aux: add _nm_nil macro
This is only useful to hack the clang-formatting.
2021-08-02 10:01:03 +02:00
Thomas Haller
3a6eb5920e
all: use _NM_ENSURE_POINTER() macro 2021-07-30 11:03:29 +02:00
Thomas Haller
4484363df0
std-aux: add _NM_ENSURE_POINTER() macro 2021-07-30 11:02:33 +02:00
Thomas Haller
387d5ded93
std-aux: move _NM_ENSURE_TYPE(), _NM_ENSURE_TYPE_CONST(), NM_STRUCT_OFFSET_ENSURE_TYPE() 2021-07-30 10:51:02 +02:00
Thomas Haller
194e29d0dc
std-aux: move nm_offsetof_end() to std-aux and add nm_offsetof()
In std-aux, we cannot use G_STRUCT_OFFSET(). I guess, we could use
offsetof() directly. Instead, add an nm_offsetof() macro. This requires
<stddef.h>.
2021-07-30 10:51:01 +02:00
Thomas Haller
91a36478f6
std-aux: move _NM_CC_SUPPORT_AUTO_TYPE, _NM_CC_SUPPORT_GENERIC to "nm-std-aux.h" 2021-07-30 10:50:56 +02:00
Thomas Haller
4547b4a893
std-aux: implement NM_PTRARRAY_LEN() macro via static function
We use NM_PTRARRAY_LEN(), and I find it a bit ugly that a macro does so
much. Maybe, it's better to have it as a function.

But the macro currently lives in "libnm-std-aux/nm-std-aux.h", which
is header-only. To add it to a C source file, we would have to move
it to another header, but "libnm-std-aux/nm-std-aux.h" is nice because
it gets included by default already.

Keep it in "libnm-std-aux/nm-std-aux.h", but implement it as an inline
function.

The macro now only does (as before) some type checking shenanigans to ensure
that the argument is a pointer to pointers.

In practice, there is probably very little difference compared to
the macro before, likely the code will anyway be inlined.
2021-07-28 23:32:47 +02:00
Thomas Haller
4e109bacab
clang-format: use "IndentPPDirectives:None" instead of "BeforeHash"
Subjectively, I think this looks better.
2021-07-09 08:49:06 +02:00
Thomas Haller
f9f453994b
glib-aux: use NM_AUTO_PROTECT_ERRNO() in nm_auto_close and nm_auto_fclose 2021-07-01 11:04:22 +02:00
Thomas Haller
2b55408cc7
std-aux/glib-aux: move NM_AUTO_PROTECT_ERRNO() to libnm-std-aux 2021-07-01 11:04:22 +02:00
Thomas Haller
2dc5b63508
std-aux: add _nm_deprecated() macro 2021-06-28 13:31:32 +02:00
Thomas Haller
1e29a7e66b
std-aux: rework NM_STATIC_ASSERT_EXPR() macro
Replace NM_STATIC_ASSERT_EXPR() by NM_STATIC_ASSERT_EXPR_1() and
NM_STATIC_ASSERT_EXPR_VOID(). NM_STATIC_ASSERT_EXPR_VOID() can be
used as an expression that returns void (that is, a simple statement).
On the other hand, NM_STATIC_ASSERT_EXPR_1() itself retuns
a compile time constant of int value 1. The latter is useful, because
we can use the 1 to combine static assertions in expressions that
are themself compile time constants, like

  #define STATIC_CHECK_AND_GET(cond, value) \
      (NM_STATIC_ASSERT_EXPR_1(cond) ? (value) : (value))

This is itself a compile time constant if value is a compile
time constant. Also, it does the compile time check that "cond"
is true.
2021-06-23 13:09:24 +02:00
Thomas Haller
c6559b04c2
std-aux/glib-aux: minor cleanup of nm_ip_addr_*()/nm_utils_addr_family_*()
- let nm_ip_addr_is_null() work for unaligned addresses.

- drop redundant nm_assert_addr_family().

- move code around.
2021-06-22 09:47:44 +02:00
Thomas Haller
ae14caf05d
std-aux: add NM_ENSURE_IS_TYPE() macro 2021-06-10 11:08:57 +02:00
Thomas Haller
cf1b7d43df
std-aux: add _nm_warn_unused_result define 2021-06-10 10:36:58 +02:00
Thomas Haller
0ed95698fd
std-aux: implement NM_IN_SET()/NM_IN_STRSET() via NM_VA_ARGS_FOREACH() 2021-06-08 08:24:11 +02:00
Thomas Haller
34a521e2d6
std-aux: add NM_VA_ARGS_JOIN() helper macro
We have variadic macros like NM_UTILS_ENUM2STR() that create a switch
statement. Their implementation relies on the way how __VA_ARGS__
gets expanded to a comma separated list. But that implementation is
not great. Let's instead add (and later use) NM_VA_ARGS_JOIN() which
can join variadic arguments by a configurable separator.
2021-06-08 08:24:11 +02:00
Thomas Haller
dadaba9ab6
std-aux: add NM_VA_ARGS_FOREACH() helper macro 2021-06-08 08:24:10 +02:00
Thomas Haller
9823ae75e5
std-aux: add _NM_MACRO_IDENTITY(), _NM_MACRO_CALL2(), _NM_MACRO_SELECT_FIRST()
_NM_MACRO_CALL2() is needed, because we cannot call _NM_MACRO_CALL() inside
_NM_MACRO_CALL().
2021-06-08 08:24:10 +02:00
Thomas Haller
2c3abc7bcb
std-aux,glib-aux: move NM_NARG() and _NM_MACRO_CALL() to lib-std-aux 2021-06-08 08:24:10 +02:00
Thomas Haller
89a10ddf89
std-aux: add NM_BIT() macro 2021-06-08 08:24:10 +02:00
Thomas Haller
21321ac736
clang-format: reformat code with clang 12
The format depends on the version of the tool. Now that Fedora 34 is
released, update to clang 12 (clang-tools-extra-12.0.0-0.3.rc1.fc34.x86_64).
2021-05-04 13:56:26 +02:00
Thomas Haller
5de552893d
glib-aux: fix compile error for g_steal_pointer() macro
Some compiler versions don't like to dereference and opaque
pointer for typeof:

gcc 8.3.1-5.1.el8 on RHEL:

    In file included from ./src/libnm-glib-aux/nm-macros-internal.h:41,
                     from ./src/libnm-glib-aux/nm-default-glib.h:68,
                     from ./src/libnm-glib-aux/nm-default-glib-i18n-lib.h:13,
                     from ./src/libnm-core-impl/nm-default-libnm-core.h:11,
                     from src/libnm-core-impl/nm-setting-8021x.c:7:
    src/libnm-core-impl/nm-setting-8021x.c: In function '_nm_setting_802_1x_cert_value_to_bytes':
    ./src/libnm-glib-aux/nm-glib.h:417:16: error: dereferencing pointer to incomplete type 'GBytes' {aka 'struct _GBytes'}
             typeof(**_pp) *const         _p            = *_pp; \
                    ^~~~~
    src/libnm-core-impl/nm-setting-8021x.c:361:12: note: in expansion of macro 'g_steal_pointer'
         return g_steal_pointer(&bytes);
                ^~~~~~~~~~~~~~~
    ./src/libnm-glib-aux/nm-glib.h:417:54: error: initialization of 'int * const' from incompatible pointer type 'GBytes *' {aka 'struct _GBytes *'} [-Werror=incompatible-pointer-types]
             typeof(**_pp) *const         _p            = *_pp; \
                                                          ^
    src/libnm-core-impl/nm-setting-8021x.c:361:12: note: in expansion of macro 'g_steal_pointer'
         return g_steal_pointer(&bytes);
                ^~~~~~~~~~~~~~~
    ./src/libnm-glib-aux/nm-glib.h:415:6: error: returning 'int * const' from a function with incompatible return type 'GBytes *' {aka 'struct _GBytes *'} [-Werror=incompatible-pointer-types]
         ({                                                     \
         ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
             typeof(*(pp)) *const         _pp           = (pp); \
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
             typeof(**_pp) *const         _p            = *_pp; \
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
             _nm_unused const void *const _p_type_check = _p;   \
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                                                \
                                                                ~
             *_pp = NULL;                                       \
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
             _p;                                                \
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         })
         ~~
    src/libnm-core-impl/nm-setting-8021x.c:361:12: note: in expansion of macro 'g_steal_pointer'
         return g_steal_pointer(&bytes);
                ^~~~~~~~~~~~~~~

Fixes: 5bc511203e ('all: make nm_steal_pointer() and g_steal_pointer() more typesafe using typeof()')
2021-04-14 15:24:47 +02:00
Thomas Haller
5bc511203e
all: make nm_steal_pointer() and g_steal_pointer() more typesafe using typeof()
The previous code would always cast the argument to "void *", and thus
loose some type information.

For example,

  gulong variable = 0;

  g_steal_pointer(&variable);

would compile, when it shouldn't.

In general, we try to avoid redefining glib macros and headers. But we
really want those extra compile time checks that we can get, so let's do
it.
2021-04-13 15:01:38 +02:00
Thomas Haller
6107a94085
std-aux: add NM_UTILS_GET_NEXT_REALLOC_SIZE_232 define 2021-04-12 16:46:00 +02:00
Thomas Haller
db773fd54e
dispatcher: set G_LOG_DOMAIN to "nm-dispatcher"
Originally, we would define G_LOG_DOMAIN via CFLAGS arguments.
Since commit 341b6e0704 ('all: change G_LOG_DOMAIN to "nm"') we would
instead set it in source and uniformly define it as "nm".

The reasons are that most parts of our source should not use g_log() directly,
and there is an aim to avoid special CFLAGS to simplify the build setup.

However, dispatcher indeed uses g_log() for logging, so the value there
is important.

Fix that, but this time by setting the define in source not via
CFLAGS.

Fixes: 341b6e0704 ('all: change G_LOG_DOMAIN to "nm"')
2021-03-18 16:54:00 +01:00
Thomas Haller
635d84b896
build: remove "shared/" from include search path
There is nothing left there.
2021-02-24 12:49:06 +01:00
Thomas Haller
a8c34b9dcf
build: move "shared/nm-std-aux" to "src/libnm-std-aux" 2021-02-24 12:48:24 +01:00