Skip to contentSkip to conversation

amd-xgbe: fix clang -Wformat warnings

PATCH Applied Justin Stitt 3 participants 4 messages Jul 8, 2022 – Jul 12, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 amd-xgbe: fix clang -Wformat warnings

When building with Clang we encounter the following warning:

| drivers/net/ethernet/amd/xgbe/xgbe-dcb.c:234:42: error: format specifies
| type 'unsigned char' but the argument has type '__u16' (aka 'unsigned
| short') [-Werror,-Wformat] pfc->pfc_cap, pfc->pfc_en, pfc->mbc,
| pfc->delay);

pfc->pfc_cap , pfc->pfc_cn, pfc->mbc are all of type u8 while pfc->delay is of type u16. The correct format specifiers %hh[u|x] were used for the first three but not for pfc->delay, which is causing the warning above.

Variadic functions (printf-like) undergo default argument promotion. Documentation/core-api/printk-formats.rst specifically recommends using the promoted-to-type's format flag. In this case %d (or %x to maintain hex representation) should be used since both u8's and u16's are fully representable by an int.

Moreover, C11 6.3.1.1 states:
(https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf) If an int can represent all values of the original type ..., the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions.

---
For clarification, the first three parameters given to netif_dbg did NOT cause a -Wformat warning. I changed them simply to follow what the standard and documentation recommend.

 drivers/net/ethernet/amd/xgbe/xgbe-dcb.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/amd/xgbe/xgbe-dcb.c +1 -1
index 895d35639129..c68ace804e37 100644
@@ -230,7 +230,7 @@ static int xgbe_dcb_ieee_setpfc(struct net_device *netdev,
 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
 
 	netif_dbg(pdata, drv, netdev,
-		  "cap=%hhu, en=%#hhx, mbc=%hhu, delay=%hhu\n",
+		  "cap=%d, en=%#x, mbc=%d, delay=%d\n",
 		  pfc->pfc_cap, pfc->pfc_en, pfc->mbc, pfc->delay);
 
 	/* Check PFC for supported number of traffic classes */
Signature · 1 line
2.37.0.rc0.161.g10f37bed90-goog
Reviewed-by Tested-by on v1 1/1
#1 lore raw
Quoted 18 lines · Justin Stitt

When building with Clang we encounter the following warning:

| drivers/net/ethernet/amd/xgbe/xgbe-dcb.c:234:42: error: format specifies
| type 'unsigned char' but the argument has type '__u16' (aka 'unsigned
| short') [-Werror,-Wformat] pfc->pfc_cap, pfc->pfc_en, pfc->mbc,
| pfc->delay);

pfc->pfc_cap , pfc->pfc_cn, pfc->mbc are all of type u8 while pfc->delay is of type u16. The correct format specifiers %hh[u|x] were used for the first three but not for pfc->delay, which is causing the warning above.

Variadic functions (printf-like) undergo default argument promotion. Documentation/core-api/printk-formats.rst specifically recommends using the promoted-to-type's format flag. In this case %d (or %x to maintain hex representation) should be used since both u8's and u16's are fully representable by an int.

Quoted 8 lines

Moreover, C11 6.3.1.1 states:
(https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf) If an int can represent all values of the original type ..., the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions.

Thanks for the patch, this fixes an instance of -Wformat I observe for x86_64 allmodconfig. I thought you had already fixed this file up?
https://lore.kernel.org/llvm/[email protected]/
which was merged. I'm guessing that was from a defconfig, while this was from an allmodconfig?
Seems like there's a bunch of config ifdef'ery around the definition of netif_dbg.

Either way, thanks for the patches and

Quoted 24 lines

---
For clarification, the first three parameters given to netif_dbg did NOT cause a -Wformat warning. I changed them simply to follow what the standard and documentation recommend.

 drivers/net/ethernet/amd/xgbe/xgbe-dcb.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/amd/xgbe/xgbe-dcb.c +1 -2
index 895d35639129..c68ace804e37 100644
@@ -230,7 +230,7 @@ static int xgbe_dcb_ieee_setpfc(struct net_device *netdev,
        struct xgbe_prv_data *pdata = netdev_priv(netdev);
 
        netif_dbg(pdata, drv, netdev,
-                 "cap=%hhu, en=%#hhx, mbc=%hhu, delay=%hhu\n",
+                 "cap=%d, en=%#x, mbc=%d, delay=%d\n",
                  pfc->pfc_cap, pfc->pfc_en, pfc->mbc, pfc->delay);
 
        /* Check PFC for supported number of traffic classes */
--

2.37.0.rc0.161.g10f37bed90-goog

Signature · 2 lines
Thanks,
~Nick Desaulniers
#2 lore raw
Quoted 29 lines · Nick Desaulniers
Quoted 18 lines · Justin Stitt

When building with Clang we encounter the following warning:

| drivers/net/ethernet/amd/xgbe/xgbe-dcb.c:234:42: error: format specifies
| type 'unsigned char' but the argument has type '__u16' (aka 'unsigned
| short') [-Werror,-Wformat] pfc->pfc_cap, pfc->pfc_en, pfc->mbc,
| pfc->delay);

pfc->pfc_cap , pfc->pfc_cn, pfc->mbc are all of type u8 while pfc->delay is of type u16. The correct format specifiers %hh[u|x] were used for the first three but not for pfc->delay, which is causing the warning above.

Variadic functions (printf-like) undergo default argument promotion. Documentation/core-api/printk-formats.rst specifically recommends using the promoted-to-type's format flag. In this case %d (or %x to maintain hex representation) should be used since both u8's and u16's are fully representable by an int.

Quoted 8 lines

Moreover, C11 6.3.1.1 states:
(https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf) If an int can represent all values of the original type ..., the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions.

Quoted 7 lines

Thanks for the patch, this fixes an instance of -Wformat I observe for x86_64 allmodconfig. I thought you had already fixed this file up?
https://lore.kernel.org/llvm/[email protected]/
which was merged. I'm guessing that was from a defconfig, while this was from an allmodconfig?
Seems like there's a bunch of config ifdef'ery around the definition of netif_dbg.

Definitely different across configs but ALSO files are ever-so-slightly different:
amd/xgbe/xgbe-drv.c _v.s_ amd/xgbe/xgbe-dcb.c

Quoted 35 lines

Either way, thanks for the patches and

Quoted 22 lines

---
For clarification, the first three parameters given to netif_dbg did NOT cause a -Wformat warning. I changed them simply to follow what the standard and documentation recommend.

 drivers/net/ethernet/amd/xgbe/xgbe-dcb.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/amd/xgbe/xgbe-dcb.c +1 -2
index 895d35639129..c68ace804e37 100644
@@ -230,7 +230,7 @@ static int xgbe_dcb_ieee_setpfc(struct net_device *netdev,
        struct xgbe_prv_data *pdata = netdev_priv(netdev);
 
        netif_dbg(pdata, drv, netdev,
-                 "cap=%hhu, en=%#hhx, mbc=%hhu, delay=%hhu\n",
+                 "cap=%d, en=%#x, mbc=%d, delay=%d\n",
                  pfc->pfc_cap, pfc->pfc_en, pfc->mbc, pfc->delay);
 
        /* Check PFC for supported number of traffic classes */
--
Quoted 2 lines

2.37.0.rc0.161.g10f37bed90-goog

--
Thanks,
~Nick Desaulniers

#3 lore raw
In reply to Justin Stitt
This patch was applied to netdev/net-next.git (master) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (master) by Jakub Kicinski <[email protected]>:

Quoted 6 lines · On Fri, 8 Jul 2022 16:26:53 -0700 you wrote:

When building with Clang we encounter the following warning:

| drivers/net/ethernet/amd/xgbe/xgbe-dcb.c:234:42: error: format specifies
| type 'unsigned char' but the argument has type '__u16' (aka 'unsigned
| short') [-Werror,-Wformat] pfc->pfc_cap, pfc->pfc_en, pfc->mbc,
| pfc->delay);
Quoted 6 lines

pfc->pfc_cap , pfc->pfc_cn, pfc->mbc are all of type u8 while pfc->delay is of type u16. The correct format specifiers %hh[u|x] were used for the first three but not for pfc->delay, which is causing the warning above.

[...]

Here is the summary with links:
- amd-xgbe: fix clang -Wformat warnings

    https://git.kernel.org/netdev/net-next/c/2afe46474ba3

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html