This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH 06/13] Installed header hygiene (BZ#20366): Macros used in #if without checking whether they are defined.
sysdeps/unix/sysv/linux/bits/socket.h wants to know whether __flexarr
will produce a real flexible array member -- specifically, one that
doesn't alter sizeof(the structure containing it). Rather than make
its approximation to that condition any more complicated, I added a
new macro to sys/cdefs.h, __flexarr_is_fake, which reveals exactly
what it wants to know. I also took the opportunity to flatten the
rather messy conditional nest defining __flexarr.
It seems to me that _LIBC should not appear in installed headers, but
avoiding that for argp specifically would require more surgery than
feels appropriate for this patch set. It's possible that
"#ifdef _LIBC" would be sufficient, but I wanted to be conservative.
* argp/argp.h: Check whether _LIBC is defined before expanding it.
* posix/glob.h: Check whether __USE_XOPEN2K8 is defined instead
of expanding it.
* misc/sys/cdefs.h: Tidy up conditional nest defining __flexarr.
Define __flexarr_is_fake when the compiler does not support
flexible array members.
* sysdeps/unix/sysv/linux/bits/socket.h: Use __flexarr_is_fake
in definitions of struct cmsghdr and CMSG_DATA.
---
argp/argp.h | 4 ++--
misc/sys/cdefs.h | 27 +++++++++++++++------------
posix/glob.h | 2 +-
sysdeps/unix/sysv/linux/bits/socket.h | 4 ++--
4 files changed, 20 insertions(+), 17 deletions(-)
diff --git a/argp/argp.h b/argp/argp.h
index 7cb5a69..5066776 100644
--- a/argp/argp.h
+++ b/argp/argp.h
@@ -511,7 +511,7 @@ extern void *__argp_input (const struct argp *__restrict __argp,
#ifdef __USE_EXTERN_INLINES
-# if !_LIBC
+# if !(defined _LIBC && _LIBC)
# define __argp_usage argp_usage
# define __argp_state_help argp_state_help
# define __option_is_short _option_is_short
@@ -546,7 +546,7 @@ __NTH (__option_is_end (const struct argp_option *__opt))
return !__opt->key && !__opt->name && !__opt->doc && !__opt->group;
}
-# if !_LIBC
+# if !(defined _LIBC && _LIBC)
# undef __argp_usage
# undef __argp_state_help
# undef __option_is_short
diff --git a/misc/sys/cdefs.h b/misc/sys/cdefs.h
index 6e9b840..a6ba860 100644
--- a/misc/sys/cdefs.h
+++ b/misc/sys/cdefs.h
@@ -153,21 +153,24 @@
# define __errordecl(name, msg) extern void name (void)
#endif
-/* Support for flexible arrays. */
-#if __GNUC_PREREQ (2,97)
-/* GCC 2.97 supports C99 flexible array members. */
+/* Support for flexible arrays.
+ Headers that should use flexible arrays only if they're "real"
+ (e.g. only if they won't affect sizeof()) should test
+ #ifndef __flexarr_is_fake. */
+#if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
# define __flexarr []
+#elif __GNUC_PREREQ (2,97)
+/* GCC 2.97 supports C99 flexible array members as an extension,
+ even when in C89 mode or compiling C++ (any version). */
+# define __flexarr []
+#elif defined __GNUC__
+/* Pre-2.97 GCC did not support C99 flexible arrays but did have
+ an equivalent extension with slightly different notation. */
+# define __flexarr [0]
#else
-# ifdef __GNUC__
-# define __flexarr [0]
-# else
-# if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
-# define __flexarr []
-# else
/* Some other non-C99 compiler. Approximate with [1]. */
-# define __flexarr [1]
-# endif
-# endif
+# define __flexarr [1]
+# define __flexarr_is_fake 1
#endif
diff --git a/posix/glob.h b/posix/glob.h
index e4548f6..ae70fa7 100644
--- a/posix/glob.h
+++ b/posix/glob.h
@@ -25,7 +25,7 @@ __BEGIN_DECLS
/* We need `size_t' for the following definitions. */
#ifndef __size_t
typedef __SIZE_TYPE__ __size_t;
-# if defined __USE_XOPEN || __USE_XOPEN2K8
+# if defined __USE_XOPEN || defined __USE_XOPEN2K8
typedef __SIZE_TYPE__ size_t;
# endif
#else
diff --git a/sysdeps/unix/sysv/linux/bits/socket.h b/sysdeps/unix/sysv/linux/bits/socket.h
index 2266047..3b57101 100644
--- a/sysdeps/unix/sysv/linux/bits/socket.h
+++ b/sysdeps/unix/sysv/linux/bits/socket.h
@@ -273,13 +273,13 @@ struct cmsghdr
with this. */
int cmsg_level; /* Originating protocol. */
int cmsg_type; /* Protocol specific type. */
-#if (!defined __STRICT_ANSI__ && __GNUC__ >= 2) || __STDC_VERSION__ >= 199901L
+#ifndef __flexarr_is_fake
__extension__ unsigned char __cmsg_data __flexarr; /* Ancillary data. */
#endif
};
/* Ancillary data object manipulation macros. */
-#if (!defined __STRICT_ANSI__ && __GNUC__ >= 2) || __STDC_VERSION__ >= 199901L
+#ifndef __flexarr_is_fake
# define CMSG_DATA(cmsg) ((cmsg)->__cmsg_data)
#else
# define CMSG_DATA(cmsg) ((unsigned char *) ((struct cmsghdr *) (cmsg) + 1))
--
2.9.3