summaryrefslogtreecommitdiff
path: root/src/include/c.h
diff options
context:
space:
mode:
authorAndres Freund2015-03-11 13:19:54 +0000
committerAndres Freund2015-03-11 13:30:01 +0000
commitbbfd7edae5aa5ad5553d3c7e102f2e450d4380d4 (patch)
treed230d006ceb7bf350abd5c3c25a89b660a8d3193 /src/include/c.h
parent66ece312f99f384bd33e4342580e78b0eebf0e74 (diff)
Add macros wrapping all usage of gcc's __attribute__.
Until now __attribute__() was defined to be empty for all compilers but gcc. That's problematic because it prevents using it in other compilers; which is necessary e.g. for atomics portability. It's also just generally dubious to do so in a header as widely included as c.h. Instead add pg_attribute_format_arg, pg_attribute_printf, pg_attribute_noreturn macros which are implemented in the compilers that understand them. Also add pg_attribute_noreturn and pg_attribute_packed, but don't provide fallbacks, since they can affect functionality. This means that external code that, possibly unwittingly, relied on __attribute__ defined to be empty on !gcc compilers may now run into warnings or errors on those compilers. But there shouldn't be many occurances of that and it's hard to work around... Discussion: 54B58BA3.8040302@ohmu.fi Author: Oskari Saarenmaa, with some minor changes by me.
Diffstat (limited to 'src/include/c.h')
-rw-r--r--src/include/c.h49
1 files changed, 43 insertions, 6 deletions
diff --git a/src/include/c.h b/src/include/c.h
index ee615ee687f..a2d4a2c5c5d 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -156,10 +156,6 @@
#define dummyret char
#endif
-#ifndef __GNUC__
-#define __attribute__(_arg_)
-#endif
-
/* ----------------------------------------------------------------
* Section 2: bool, true, false, TRUE, FALSE, NULL
* ----------------------------------------------------------------
@@ -560,6 +556,47 @@ typedef NameData *Name;
/* we don't currently need wider versions of the other ALIGN macros */
#define MAXALIGN64(LEN) TYPEALIGN64(MAXIMUM_ALIGNOF, (LEN))
+/* ----------------
+ * Attribute macros
+ *
+ * GCC: https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
+ * GCC: https://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html
+ * Sunpro: https://docs.oracle.com/cd/E18659_01/html/821-1384/gjzke.html
+ * XLC: http://www-01.ibm.com/support/knowledgecenter/SSGH2K_11.1.0/com.ibm.xlc111.aix.doc/language_ref/function_attributes.html
+ * XLC: http://www-01.ibm.com/support/knowledgecenter/SSGH2K_11.1.0/com.ibm.xlc111.aix.doc/language_ref/type_attrib.html
+ * ----------------
+ */
+
+/* only GCC supports the unused attribute */
+#ifdef __GNUC__
+#define pg_attribute_unused __attribute__((unused))
+#else
+#define pg_attribute_unused
+#endif
+
+/* GCC and XLC support format attributes */
+#if defined(__GNUC__) || defined(__IBMC__)
+#define pg_attribute_format_arg(a) __attribute__((format_arg(a)))
+#define pg_attribute_printf(f,a) __attribute__((format(PG_PRINTF_ATTRIBUTE, f, a)))
+#else
+#define pg_attribute_format_arg(a)
+#define pg_attribute_printf(f,a)
+#endif
+
+/* GCC, Sunpro and XLC support aligned, packed and noreturn */
+#if defined(__GNUC__) || defined(__SUNPRO_C) || defined(__IBMC__)
+#define pg_attribute_aligned(a) __attribute__((aligned(a)))
+#define pg_attribute_noreturn __attribute__((noreturn))
+#define pg_attribute_packed __attribute__((packed))
+#else
+/*
+ * NB: aligned and packed are not defined as empty as they affect code
+ * functionality; they must be implemented by the compiler if they are to be
+ * used.
+ */
+#define pg_attribute_noreturn
+#endif
+
/* ----------------------------------------------------------------
* Section 6: assertions
* ----------------------------------------------------------------
@@ -906,7 +943,7 @@ typedef NameData *Name;
#ifdef USE_ASSERT_CHECKING
#define PG_USED_FOR_ASSERTS_ONLY
#else
-#define PG_USED_FOR_ASSERTS_ONLY __attribute__((unused))
+#define PG_USED_FOR_ASSERTS_ONLY pg_attribute_unused
#endif
@@ -973,7 +1010,7 @@ typedef NameData *Name;
extern int
snprintf(char *str, size_t count, const char *fmt,...)
/* This extension allows gcc to check the format string */
-__attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
+pg_attribute_printf(3, 4);
#endif
#if !HAVE_DECL_VSNPRINTF