From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 2CD23460D7; Tue, 21 Jan 2025 23:36:22 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 231CA402F2; Tue, 21 Jan 2025 23:36:18 +0100 (CET) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by mails.dpdk.org (Postfix) with ESMTP id 96537402CB for ; Tue, 21 Jan 2025 23:36:15 +0100 (CET) Received: by linux.microsoft.com (Postfix, from userid 1213) id 9BB07205A9EF; Tue, 21 Jan 2025 14:36:14 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 9BB07205A9EF DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1737498974; bh=GcKf1Ofb7plql+EDBQllWvu6buZHEFyl+L1UaBG7Kg4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tAMOUl88327PtNwzW9ffOYqdU51u/t30Eo2yrRd7fk2pmtsd54GN4NVcX1eRcLHvB MS2Dqv3kNUegADLLpBmBKspBP5QVKGP2nyVrPEmd4EuE1OfYr21Xpboog7eujQ328e Pdxm9py8cpmuRR23V+fUoW2P8eM3m/8xddw40RDc= From: Andre Muezerie To: andremue@linux.microsoft.com Cc: dev@dpdk.org, stephen@networkplumber.org, bruce.richardson@intel.com Subject: [PATCH v16 1/3] eal: add diagnostics macros to make code portable Date: Tue, 21 Jan 2025 14:36:08 -0800 Message-Id: <1737498970-4631-2-git-send-email-andremue@linux.microsoft.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1737498970-4631-1-git-send-email-andremue@linux.microsoft.com> References: <1735263196-2809-1-git-send-email-andremue@linux.microsoft.com> <1737498970-4631-1-git-send-email-andremue@linux.microsoft.com> X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org 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 --- lib/eal/include/rte_common.h | 48 ++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/lib/eal/include/rte_common.h b/lib/eal/include/rte_common.h index 40592f71b1..9983eb586d 100644 --- a/lib/eal/include/rte_common.h +++ b/lib/eal/include/rte_common.h @@ -156,6 +156,54 @@ typedef uint16_t unaligned_uint16_t; #define RTE_DEPRECATED(x) #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 + +/** + * Macro to disable compiler warnings about removing 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 + +/** + * Workaround to discard qualifiers (such as const, volatile, restrict) from a pointer, + * without the compiler emitting a warning. + */ +#define RTE_PTR_UNQUAL(X) ((void *)(uintptr_t)(X)) + +/** + * Workaround to cast a pointer to a specific type, + * without the compiler emitting a warning about discarding qualifiers. + * + * @warning + * When casting a pointer to point to a larger type, the resulting pointer may + * be misaligned, which results in undefined behavior. + * E.g.: + * + * struct s { + * uint16_t a; + * uint8_t b; + * uint8_t c; + * uint8_t d; + * } v; + * uint16_t * p = RTE_CAST_PTR(uint16_t *, &v.c); // "p" is not 16 bit aligned! + */ +#define RTE_CAST_PTR(type, ptr) ((type)(uintptr_t)(ptr)) + /** * Mark a function or variable to a weak reference. */ -- 2.47.2.vfs.0.1