DPDK patches and discussions
 help / color / mirror / Atom feed
From: "Morten Brørup" <mb@smartsharesystems.com>
To: "Andre Muezerie" <andremue@linux.microsoft.com>
Cc: <dev@dpdk.org>, <stephen@networkplumber.org>,
	<bruce.richardson@intel.com>
Subject: RE: [PATCH v13 3/3] drivers/net: add diagnostics macros to make code portable
Date: Thu, 16 Jan 2025 10:08:07 +0100	[thread overview]
Message-ID: <98CBD80474FA8B44BF855DF32C47DC35E9F9BD@smartserver.smartshare.dk> (raw)
In-Reply-To: <1736992511-20462-4-git-send-email-andremue@linux.microsoft.com>

> From: Andre Muezerie [mailto:andremue@linux.microsoft.com]
> Sent: Thursday, 16 January 2025 02.55
> 
> 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.

Here is some food for thought and discussion...

> @@ -2083,7 +2075,7 @@ dpaa2_dev_loopback_rx(void *queue,
>  			if (unlikely((status & QBMAN_DQ_STAT_VALIDFRAME) ==
> 0))
>  				continue;
>  		}
> -		fd[num_rx] = (struct qbman_fd *)qbman_result_DQ_fd(dq_storage);
> +		fd[num_rx] = RTE_PTR_DROP_QUALIFIERS(qbman_result_DQ_fd(dq_storage));

I do not think this makes the code more readable; quite the opposite.
Before this, I could see which type the variable was being cast to.

How about a macro that resembles "traditional" type casting:

/**
 * Workaround to discard qualifiers (such as const, volatile, restrict) from a pointer,
 * without the compiler emitting a warning.
 *
 * @warning
 * Although this macro can be abused for casting a pointer to point to a different type,
 * alignment may be incorrect when casting to point to a larger type. 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))


Writing the above warning lead me down another path...
Can we somehow use __typeof_unqual__?
It is available in both GCC [1] and MSVC [2].

[1]: https://gcc.gnu.org/onlinedocs/gcc/Typeof.html
[2]: https://learn.microsoft.com/en-us/cpp/c-language/typeof-unqual-c?view=msvc-170


We are making a workaround, and should take care to not endorse overusing it.
Especially for other purposes than intended.

Unfortunately, I think some of the type casts don't just remove qualifiers, but does exactly what my warning above describes: Casts a pointer to completely different type.
If the new type is a larger type, the pointer's alignment becomes invalid, and if the compiler considers alignment a "qualifier", -Wcast-qual emits a warning about it.


Backtracking a bit...
If the macro is intended to remove qualifiers, and not to cast to a different type, RTE_PTR_DROP_QUALIFIERS(ptr) might be better than RTE_CAST_PTR(type, ptr).
For brevity and to resemble the C23 keyword typeof_unqual, it could be named RTE_PTR_UNQUAL instead of RTE_PTR_DROP_QUALIFIERS.



  parent reply	other threads:[~2025-01-16  9:08 UTC|newest]

Thread overview: 67+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-27  1:33 [PATCH 0/3] " Andre Muezerie
2024-12-27  1:33 ` [PATCH 1/3] lib/eal: " Andre Muezerie
2024-12-27  1:33 ` [PATCH 2/3] drivers/common: " Andre Muezerie
2024-12-27 17:57   ` Stephen Hemminger
2024-12-27 19:43     ` Andre Muezerie
2024-12-27  1:33 ` [PATCH 3/3] drivers/net: " Andre Muezerie
2024-12-28  0:45 ` [PATCH v2 0/3] " Andre Muezerie
2024-12-28  0:45   ` [PATCH v2 1/3] lib/eal: " Andre Muezerie
2024-12-28  0:45   ` [PATCH v2 2/3] drivers/common: " Andre Muezerie
2024-12-28  0:45   ` [PATCH v2 3/3] drivers/net: " Andre Muezerie
2024-12-28  3:18 ` [PATCH v3 0/3] " Andre Muezerie
2024-12-28  3:18   ` [PATCH v3 1/3] lib/eal: " Andre Muezerie
2024-12-28  3:18   ` [PATCH v3 2/3] drivers/common: " Andre Muezerie
2024-12-28  3:18   ` [PATCH v3 3/3] drivers/net: " Andre Muezerie
2024-12-30 15:59 ` [PATCH v4 0/3] " Andre Muezerie
2024-12-30 15:59   ` [PATCH v4 1/3] lib/eal: " Andre Muezerie
2024-12-30 15:59   ` [PATCH v4 2/3] drivers/common: " Andre Muezerie
2024-12-30 15:59   ` [PATCH v4 3/3] drivers/net: " Andre Muezerie
2024-12-30 17:44   ` [PATCH v4 0/3] " Stephen Hemminger
2024-12-31 18:55 ` [PATCH v5 " Andre Muezerie
2024-12-31 18:55   ` [PATCH v5 1/3] lib/eal: " Andre Muezerie
2024-12-31 18:55   ` [PATCH v5 2/3] drivers/common: " Andre Muezerie
2024-12-31 18:55   ` [PATCH v5 3/3] drivers/net: " Andre Muezerie
2024-12-31 20:15 ` [PATCH v6 0/3] " Andre Muezerie
2024-12-31 20:15   ` [PATCH v6 1/3] lib/eal: " Andre Muezerie
2024-12-31 20:15   ` [PATCH v6 2/3] drivers/common: " Andre Muezerie
2024-12-31 20:15   ` [PATCH v6 3/3] drivers/net: " Andre Muezerie
2024-12-31 22:30 ` [PATCH v7 0/3] " Andre Muezerie
2024-12-31 22:30   ` [PATCH v7 1/3] lib/eal: " Andre Muezerie
2024-12-31 22:30   ` [PATCH v7 2/3] drivers/common: " Andre Muezerie
2024-12-31 22:30   ` [PATCH v7 3/3] drivers/net: " Andre Muezerie
2025-01-01  0:48 ` [PATCH v8 0/3] " Andre Muezerie
2025-01-01  0:48   ` [PATCH v8 1/3] lib/eal: " Andre Muezerie
2025-01-01  0:48   ` [PATCH v8 2/3] drivers/common: " Andre Muezerie
2025-01-01  0:48   ` [PATCH v8 3/3] drivers/net: " Andre Muezerie
2025-01-01  3:36 ` [PATCH v9 0/3] " Andre Muezerie
2025-01-01  3:36   ` [PATCH v9 1/3] lib/eal: " Andre Muezerie
2025-01-01  3:36   ` [PATCH v9 2/3] drivers/common: " Andre Muezerie
2025-01-01  3:36   ` [PATCH v9 3/3] drivers/net: " Andre Muezerie
2025-01-03  0:12 ` [PATCH v10 0/3] " Andre Muezerie
2025-01-03  0:12   ` [PATCH v10 1/3] lib/eal: " Andre Muezerie
2025-01-03  0:12   ` [PATCH v10 2/3] drivers/common: " Andre Muezerie
2025-01-03  0:12   ` [PATCH v10 3/3] drivers/net: " Andre Muezerie
2025-01-03 15:36 ` [PATCH v11 0/3] " Andre Muezerie
2025-01-03 15:36   ` [PATCH v11 1/3] lib/eal: " Andre Muezerie
2025-01-03 15:36   ` [PATCH v11 2/3] drivers/common: " Andre Muezerie
2025-01-03 15:36   ` [PATCH v11 3/3] drivers/net: " Andre Muezerie
2025-01-03 19:24   ` [PATCH v11 0/3] " Stephen Hemminger
2025-01-03 21:26     ` Andre Muezerie
2025-01-06 11:00       ` Bruce Richardson
2025-01-08  2:46         ` Andre Muezerie
2025-01-08  9:20           ` Bruce Richardson
2025-01-14 19:20             ` Andre Muezerie
2025-01-15 11:11               ` Bruce Richardson
2025-01-15  4:27 ` [PATCH v12 " Andre Muezerie
2025-01-15  4:27   ` [PATCH v12 1/3] lib/eal: " Andre Muezerie
2025-01-15  9:05     ` Morten Brørup
2025-01-15  4:27   ` [PATCH v12 2/3] drivers/common: " Andre Muezerie
2025-01-15 11:13     ` Bruce Richardson
2025-01-15  4:27   ` [PATCH v12 3/3] drivers/net: " Andre Muezerie
2025-01-16  1:55 ` [PATCH v13 0/3] " Andre Muezerie
2025-01-16  1:55   ` [PATCH v13 1/3] eal: " Andre Muezerie
2025-01-16  1:55   ` [PATCH v13 2/3] drivers/common: " Andre Muezerie
2025-01-16  1:55   ` [PATCH v13 3/3] drivers/net: " Andre Muezerie
2025-01-16  8:57     ` Bruce Richardson
2025-01-16  9:08     ` Morten Brørup [this message]
2025-01-16  8:58   ` [PATCH v13 0/3] " Bruce Richardson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=98CBD80474FA8B44BF855DF32C47DC35E9F9BD@smartserver.smartshare.dk \
    --to=mb@smartsharesystems.com \
    --cc=andremue@linux.microsoft.com \
    --cc=bruce.richardson@intel.com \
    --cc=dev@dpdk.org \
    --cc=stephen@networkplumber.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).