]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
autogen.sh, lib/: Replace must_be_array() by -Werror=sizeof-pointer-div
authorAlejandro Colomar <alx@kernel.org>
Wed, 21 May 2025 12:03:06 +0000 (14:03 +0200)
committerSerge Hallyn <serge@hallyn.com>
Tue, 3 Jun 2025 14:34:55 +0000 (09:34 -0500)
The error works as well as the magic macro, and we get cleaner code.
Plus, very soon we'll get the countof() operator from GCC 16 and
Clang 21, which doesn't even need this diagnostic to work safely.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
autogen.sh
lib/must_be.h
lib/sizeof.h

index 3c7c37acf873352b925e2ccf71314b5f36b8aa22..bf67df0c7f221c87ca5975daa304340b01de913e 100755 (executable)
@@ -10,6 +10,7 @@ CFLAGS="$CFLAGS -Werror=implicit-int"
 CFLAGS="$CFLAGS -Werror=incompatible-pointer-types"
 CFLAGS="$CFLAGS -Werror=int-conversion"
 CFLAGS="$CFLAGS -Werror=sign-compare"
+CFLAGS="$CFLAGS -Werror=sizeof-pointer-div"
 CFLAGS="$CFLAGS -Wno-expansion-to-defined"
 CFLAGS="$CFLAGS -Wno-unknown-attributes"
 CFLAGS="$CFLAGS -Wno-unknown-warning-option"
index 2c866e7baef43b5f7a1bf4ce60a13c73f9af5a63..ff9b150e55629e177c147e18b7c47dc654b1484a 100644 (file)
 
 #include <config.h>
 
-#include <assert.h>
-
-
-/*
- * SYNOPSIS
- *     int must_be(bool e);
- *
- * ARGUMENTS
- *     e       Expression to be asserted.
- *
- * DESCRIPTION
- *     This macro fails compilation if 'e' is false.  If 'e' is true,
- *     it returns (int) 0, so it doesn't affect the expression in which
- *     it is contained.
- *
- *     This macro is similar to static_assert(3).  While
- *     static_assert(3) can only be used where a statement is allowed,
- *     this must_be() macro can be used wherever an expression is
- *     allowed.
- *
- * RETURN VALUE
- *     0
- *
- * ERRORS
- *     If 'e' is false, the compilation will fail, as when using
- *     static_assert(3).
- *
- * EXAMPLES
- *     #define must_be_array(a)  must_be(is_array(a))
- *
- *     #define countof(a)  (sizeof(a) / sizeof(*(a)) + must_be_array(a))
- *
- *     int foo[42];
- *     int bar[countof(foo)];
- */
-
-
-#define must_be(e)                                                            \
-(                                                                             \
-       0 * (int) sizeof(                                                     \
-               struct {                                                      \
-                       static_assert(e, "");                                 \
-                       int ISO_C_forbids_a_struct_with_no_members_;          \
-               }                                                             \
-       )                                                                     \
-)
-
-
-/*
- * SYNOPSIS
- *     int must_be_array(a);
- *
- * ARGUMENTS
- *     a       Array.
- *
- * DESCRIPTION
- *     This macro fails compilation if 'a' is not an array.  It is
- *     useful in macros that accept an array as a parameter, where this
- *     macro can validate the macro argument.  It prevent passing a
- *     pointer to such macros, which would otherwise produce silent
- *     bugs.
- *
- * RETURN VALUE
- *     0
- *
- * ERRORS
- *     If 'a' is not an array, the compilation will fail.
- *
- * EXAMPLES
- *     int a[10];
- *     int *p;
- *
- *     must_be_array(a);  // Ok
- *     must_be_array(p);  // Compile-time error
- *
- * SEE ALSO
- *     must_be()
- */
-
 
 #define is_same_type(a, b)                                                    \
 (                                                                             \
 )
 
 
-#define is_array(a)                                                           \
-(                                                                             \
-       !is_same_typeof(a, &(a)[0])                                           \
-)
-
-
-#define must_be_array(a)                                                      \
-(                                                                             \
-       must_be(is_array(a))                                                  \
-)
-
-
 #endif  // include guard
index af7cf4b9bd3bee39864874d203afad0a880e0058..dca4b36ccc270fa8803a9c039fcca702ad3ae0f1 100644 (file)
 #endif
 #include <sys/types.h>
 
-#include "must_be.h"
-
 
 #define ssizeof(x)           ((ssize_t) sizeof(x))
 #define memberof(T, member)  ((T){}.member)
 #define WIDTHOF(x)           (sizeof(x) * CHAR_BIT)
 
 #if !defined(countof)
-# define countof(a)          (sizeof(a) / sizeof((a)[0]) + must_be_array(a))
+# define countof(a)          (sizeof(a) / sizeof((a)[0]))
 #endif
 
 #define SIZEOF_ARRAY(a)      (countof(a) * sizeof((a)[0]))