Paul Eggert wrote on 2025-02-07:
> +     * lib/intprops.h (INT_PROMOTE): New macro.
> +     * tests/test-intprops.c: Test it.

The test is void on compilers other than GCC and clang. This patch adds
checks for all compilers, and also clarifies what INT_PROMOTE does on
floats and pointers.


2025-02-09  Bruno Haible  <br...@clisp.org>

        intprops tests: Strengthen INT_PROMOTE tests.
        * lib/intprops.h (INT_PROMOTE): Refine comment.
        * tests/test-intprops.c: Check the size and sign of INT_PROMOTE (x) on
        all compilers.
        (main): Check that INT_PROMOTE is a no-op on floats.

diff --git a/lib/intprops.h b/lib/intprops.h
index 83efe39910..2f9fa0a022 100644
--- a/lib/intprops.h
+++ b/lib/intprops.h
@@ -39,7 +39,7 @@
    then 'switch (INT_PROMOTE (E))' pacifies gcc -Wswitch-enum if some
    enum values are deliberately omitted from the switch's cases.
    Here, unary + is safer than a cast or inline function, as unary +
-   does only integer promotions.  */
+   does only integer promotions and is disallowed on pointers.  */
 #define INT_PROMOTE(e) (+ (e))
 
 
diff --git a/tests/test-intprops.c b/tests/test-intprops.c
index 026c615aba..6c06e47ab3 100644
--- a/tests/test-intprops.c
+++ b/tests/test-intprops.c
@@ -69,6 +69,11 @@
          || defined __clang__))
 int check_INT_PROMOTE = _Generic (INT_PROMOTE ((short int) 0), int: 0);
 #endif
+/* For other compilers, check the size and sign of INT_PROMOTE (x).  */
+int check_INT_PROMOTE_size
+    [2 * (sizeof (INT_PROMOTE ((short int) 0)) == sizeof (int)) - 1];
+int check_INT_PROMOTE_sign
+    [2 * (INT_PROMOTE ((short int) -1) < 0) - 1];
 
 int int_minus_2 = -2;
 int int_1 = 1;
@@ -79,6 +84,9 @@ main (void)
   /* Use VERIFY for tests that must be integer constant expressions,
      ASSERT otherwise.  */
 
+  /* Check that INT_PROMOTE is a no-op on floats.  */
+  ASSERT (INT_PROMOTE (2.71828) > 2);
+
 #ifndef TEST_STDCKDINT
   /* TYPE_IS_INTEGER.  */
   ASSERT (TYPE_IS_INTEGER (bool));




Reply via email to