]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: compiler: define a __attribute__warning() macro
authorWilly Tarreau <w@1wt.eu>
Fri, 7 Apr 2023 12:54:36 +0000 (14:54 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 7 Apr 2023 16:14:28 +0000 (18:14 +0200)
__attribute__((deprecated)) is convenient to discourage from using
something deprecated, but gcc >= 4.3 provides __attribute__((warning(x)))
that allows to display a specific warning if something is used. This is
particularly convenient to give indications when some API parts need to
be adapted. Let's just define it as a macro that falls back to the older
deprecated attribute when not available.

It's supported on clang 14 as well but works differently and errors
out when redefined (while the main purpose precisely is to add such a
redefinition). Thus instead on clang we use deprecated(msg) which is
OK. See https://github.com/llvm/llvm-project/issues/56519

include/haproxy/compiler.h

index 3e00493768331f00385f8148fc623b337dd7d84c..86d4ddf5a7acdaf012e3a822939bdfcc4529c2da 100644 (file)
 #define __attribute__(x) __attribute__(x)
 #endif
 
+/* attribute(warning) was added in gcc 4.3 */
+#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
+#  define __has_attribute_warning 1
+#endif
+
+/* __attribute__warning(x) does __attribute__((warning(x))) if supported by the
+ * compiler, otherwise __attribute__((deprecated)). Clang supports it since v14
+ * but is a bit capricious in that it refuses a redefinition with a warning
+ * attribute that wasn't there the first time. However it's OK with deprecated(x)
+ * so better use this one. See: https://github.com/llvm/llvm-project/issues/56519
+ */
+#if defined(__clang__)
+#  define __attribute__warning(x) __attribute__((deprecated(x)))
+#elif __has_attribute(warning)
+#  define __attribute__warning(x) __attribute__((warning(x)))
+#else
+#  define __attribute__warning(x) __attribute__((deprecated))
+#endif
+
 /* By default, gcc does not inline large chunks of code, but we want it to
  * respect our choices.
  */