tech-toolchain archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

New feature-testing macros in <sys/cdefs.h>



Hello,

Clang ships with fine-grained useful macros for checking
whether a feature is supported. Instead of redefining them
 in the other places, I propose to put them in a central
place in <sys/cdefs.h>.

GCC 5.0 ships with __has_include, for now it's skipped
as GCC 5.0 isn't released.

Regards,

diff --git a/sys/cdefs.h b/sys/cdefs.h
index 02ea4b3..f850655 100644
--- a/sys/cdefs.h
+++ b/sys/cdefs.h
@@ -55,6 +55,41 @@
 #define	__GNUC_PREREQ__(x, y)	0
 #endif
 
+/*
+ * Macro to test if we're using a compiler with specific:
+ * - features,
+ * - extensions,
+ * - attributes,
+ * - builtins.
+ *
+ * Usage:
+ *
+ *	#if __has_builtin(__builtin_popcount)
+ *		return __builtin_popcount(x);
+ *	#else
+ *		int count = 0;
+ *		for (; x != 0; x &= x - 1)
+ *			count++;
+ *		return count;
+ *	#endif
+ */
+
+#ifndef __has_feature
+#define __has_feature(x) 0
+#endif
+
+#ifndef __has_extension
+#define __has_extension(x) 0
+#endif
+
+#ifndef __has_attribute
+#define __has_attribute(x) 0
+#endif
+
+#ifndef __has_builtin
+#define __has_builtin(x) 0
+#endif
+
 #include <machine/cdefs.h>
 #ifdef __ELF__
 #include <sys/cdefs_elf.h>


Home | Main Index | Thread Index | Old Index