[PATCH] checkpatch: Fix MACRO_ARG_REUSE false positives for offsetof and sizeof_field

From: Alexander Duyck
Date: Thu Aug 12 2021 - 22:00:08 EST


From: Alexander Duyck <alexanderduyck@xxxxxx>

The MACRO_ARG_REUSE check was generating false positives when run against
macros that made use of offsetof and sizeof_field. This was due to the fact
that the type variable was not being checked for and as a result they were
not being stripped from the final use count for the macro argument.

Reproduction steps below:
  # ./scripts/checkpatch.pl --strict --no-tree -types MACRO_ARG_REUSE \
-f drivers/net/ethernet/intel/igb/igb_ethtool.c
CHECK: Macro argument reuse '_stat' - possible side-effects?
  #27: FILE: drivers/net/ethernet/intel/igb/igb_ethtool.c:27:
  +#define IGB_STAT(_name, _stat) { \
  + .stat_string = _name, \
  + .sizeof_stat = sizeof_field(struct igb_adapter, _stat), \
  + .stat_offset = offsetof(struct igb_adapter, _stat) \
  +}

  CHECK: Macro argument reuse '_net_stat' - possible side-effects?
  #77: FILE: drivers/net/ethernet/intel/igb/igb_ethtool.c:77:
  +#define IGB_NETDEV_STAT(_net_stat) { \
  + .stat_string = __stringify(_net_stat), \
  + .sizeof_stat = sizeof_field(struct rtnl_link_stats64, _net_stat), \
  + .stat_offset = offsetof(struct rtnl_link_stats64, _net_stat) \
  +}

  total: 0 errors, 0 warnings, 2 checks, 3498 lines checked

Address that by adding the type to the offset and sizeof_field checks so
that they will properly strip both the macro and the two arguments. With
this change the result is 0 checks as the two argument macros correctly
strip the type and argument.

Signed-off-by: Alexander Duyck <alexanderduyck@xxxxxx>
---
scripts/checkpatch.pl | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 461d4221e4a4..cda33bb71ad6 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -5833,7 +5833,7 @@ sub process {
next if ($arg =~ /\.\.\./);
next if ($arg =~ /^type$/i);
my $tmp_stmt = $define_stmt;
- $tmp_stmt =~ s/\b(__must_be_array|offsetof|sizeof|sizeof_field|__stringify|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
+ $tmp_stmt =~ s/\b(__must_be_array|offsetof\s*\(\s*$Type\s*,|\#+|sizeof|sizeof_field\s*\(\s*$Type\s*,|\#+|__stringify|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
$tmp_stmt =~ s/\#+\s*$arg\b//g;
$tmp_stmt =~ s/\b$arg\s*\#\#//g;
my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;