[v12,1/3] lib/eal: add diagnostics macros to make code portable

Message ID 1736915238-779-2-git-send-email-andremue@linux.microsoft.com (mailing list archive)
State Superseded
Delegated to: Thomas Monjalon
Headers
Series add diagnostics macros to make code portable |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Andre Muezerie Jan. 15, 2025, 4:27 a.m. UTC
It was a common pattern to have "GCC diagnostic ignored" pragmas
sprinkled over the code and only activate these pragmas for certain
compilers (gcc and clang). Clang supports GCC's pragma for
compatibility with existing source code, so #pragma GCC diagnostic
and #pragma clang diagnostic are synonyms for Clang
(https://clang.llvm.org/docs/UsersManual.html).

Now that effort is being made to make the code compatible with MSVC
these expressions would become more complex. It makes sense to hide
this complexity behind macros. This makes maintenance easier as these
macros are defined in a single place. As a plus the code becomes
more readable as well.

Signed-off-by: Andre Muezerie <andremue@linux.microsoft.com>
---
 lib/eal/include/rte_common.h | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
  

Comments

Morten Brørup Jan. 15, 2025, 9:05 a.m. UTC | #1
> +/*
> + * Macro to ignore whenever a pointer is cast so as to remove a type
> + * qualifier from the target type.
> + */

This description could be better, something like the push/pop description:
"Macro to disable compiler warnings about ..."

> +#if !defined __INTEL_COMPILER && !defined RTE_TOOLCHAIN_MSVC

Prefer defined(NAME) over defined NAME.
Same for the description of push/pop below.
(I see both in rte_common.h, so perhaps it's just my personal preference.)

> +#define __rte_diagnostic_ignored_wcast_qual \
> +		_Pragma("GCC diagnostic ignored \"-Wcast-qual\"")
> +#else
> +#define __rte_diagnostic_ignored_wcast_qual
> +#endif
> +
> +/*
> + * Macros to cause the compiler to remember the state of the diagnostics as of
> + * each push, and restore to that point at each pop.
> + */
> +#if !defined __INTEL_COMPILER && !defined RTE_TOOLCHAIN_MSVC
> +#define __rte_diagnostic_push _Pragma("GCC diagnostic push")
> +#define __rte_diagnostic_pop  _Pragma("GCC diagnostic pop")
> +#else
> +#define __rte_diagnostic_push
> +#define __rte_diagnostic_pop
> +#endif


> +#define RTE_IGNORE_CAST_QUAL(X) \
> +	((uintptr_t)(X))

A description of this macro is missing.
Rather than assign a name that refers to the name of compiler's warning, could you come up with a name that describes what the macro does to X, i.e. discards qualifiers.
And if the macro is exclusively for pointers, perhaps it should have PTR somewhere in its name.

And do we really need this macro? Can't RTE_CAST_FIELD() be used instead?

Or can we make the macro more like RTE_CAST_FIELD()?
Perhaps RTE_CAST(var, type)?

Or maybe, inspired by RTE_PTR_ADD():
#define RTE_PTR(var) ((void*)((uintptr_t)(ptr)))
#define RTE_CONST_PTR(var) ((const void*)((uintptr_t)(ptr)))
  

Patch

diff --git a/lib/eal/include/rte_common.h b/lib/eal/include/rte_common.h
index 4d299f2b36..2142dd968d 100644
--- a/lib/eal/include/rte_common.h
+++ b/lib/eal/include/rte_common.h
@@ -137,6 +137,32 @@  typedef uint16_t unaligned_uint16_t;
 #define RTE_DEPRECATED(x)
 #endif
 
+/*
+ * Macro to ignore whenever a pointer is cast so as to remove a type
+ * qualifier from the target type.
+ */
+#if !defined __INTEL_COMPILER && !defined RTE_TOOLCHAIN_MSVC
+#define __rte_diagnostic_ignored_wcast_qual \
+		_Pragma("GCC diagnostic ignored \"-Wcast-qual\"")
+#else
+#define __rte_diagnostic_ignored_wcast_qual
+#endif
+
+/*
+ * Macros to cause the compiler to remember the state of the diagnostics as of
+ * each push, and restore to that point at each pop.
+ */
+#if !defined __INTEL_COMPILER && !defined RTE_TOOLCHAIN_MSVC
+#define __rte_diagnostic_push _Pragma("GCC diagnostic push")
+#define __rte_diagnostic_pop  _Pragma("GCC diagnostic pop")
+#else
+#define __rte_diagnostic_push
+#define __rte_diagnostic_pop
+#endif
+
+#define RTE_IGNORE_CAST_QUAL(X) \
+	((uintptr_t)(X))
+
 /**
  * Mark a function or variable to a weak reference.
  */