mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] checkpatch: externalize function-like type macros
@ 2026-11-18 19:39 HONG Yifan
  2026-11-18 20:17 ` Joe Perches
  0 siblings, 1 reply; 2+ messages in thread
From: HONG Yifan @ 2026-11-18 19:39 UTC (permalink / raw)
  To: Andrew Morton, Andy Whitcroft, Joe Perches
  Cc: HONG Yifan, kernel-team, linux-kernel

tools/testing/selftests/kselftest_harness.h defines FIXTURE_DATA() and
FIXTURE_VARIANT() to expand to struct type specifiers
("struct _test_data_##datatype_name" and
"struct _fixture_variant_##fixture_name").

Because checkpatch.pl does not recognize these function-like macros in
$NonptrType or $NonptrTypeWithAttr, annotate_values() parses them as
function calls and tags a following '*' as a binary multiplication
operator instead of a pointer declarator.  This triggers false-positive
errors across kselftests whenever helper functions take pointers to
fixture data or variants (over 150 sites in tools/testing/selftests/),
for example on commit d2b82fa39fda ("selftests/mm: add a GUP selftest"):

  ERROR:SPACING: need consistent spacing around '*' (ctx:WxV)
  #319: FILE: tools/testing/selftests/mm/gup.c:191:
  +		FIXTURE_DATA(gup_test) *self,

Following scripts/const_structs.checkpatch in commit bf1fa1dae68e
("checkpatch: externalize the structs that should be const"), read
additional function-like type macro names from a new companion file
scripts/type_macros.checkpatch into $type_macros (named to mirror
$const_structs/$conststructsfile and initialized with the built-in
typeof and __typeof__ keywords) and use $type_macros in $NonptrType and
$NonptrTypeWithAttr.

Seed scripts/type_macros.checkpatch with FIXTURE_DATA and
FIXTURE_VARIANT, and list scripts/*.checkpatch under CHECKPATCH in
MAINTAINERS.  Valid pointer declarations and casts no longer emit false
SPACING errors, while malformed pointer placement (such as
"FIXTURE_DATA(foo)* p" or "(FIXTURE_DATA(foo)*)p") is flagged by
POINTER_LOCATION.  Keep typeof and __typeof__ built-in as C/GCC
language operators so $type_macros is never empty and typeof() keeps
working when scripts/type_macros.checkpatch is missing or empty.

Signed-off-by: HONG Yifan <elsk@google.com>
---
Changes in v2:
- Externalize FIXTURE_DATA and FIXTURE_VARIANT into
  scripts/type_macros.checkpatch instead of hardcoding them in
  scripts/checkpatch.pl, matching scripts/const_structs.checkpatch from
  commit bf1fa1dae68e ("checkpatch: externalize the structs that should
  be const").
- Add scripts/*.checkpatch under CHECKPATCH in MAINTAINERS.
- Link to v1: https://lore.kernel.org/r/20261117220522.904290-1-elsk@google.com

 MAINTAINERS                    | 1 +
 scripts/checkpatch.pl          | 9 +++++++--
 scripts/type_macros.checkpatch | 6 ++++++
 3 files changed, 14 insertions(+), 2 deletions(-)
 create mode 100644 scripts/type_macros.checkpatch

diff --git a/MAINTAINERS b/MAINTAINERS
index d738990161bd..b0dca8edb154 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -6264,6 +6264,7 @@ CHECKPATCH
 M:	Andy Whitcroft <apw@canonical.com>
 M:	Joe Perches <joe@perches.com>
 S:	Maintained
+F:	scripts/*.checkpatch
 F:	scripts/checkpatch.pl

 CHECKPATCH DOCUMENTATION
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index b8b64f720191..7500e1cf447f 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -71,6 +71,7 @@ my $codespell = 0;
 my $codespellfile = "/usr/share/codespell/dictionary.txt";
 my $user_codespellfile = "";
 my $conststructsfile = "$D/const_structs.checkpatch";
+my $typemacrosfile = "$D/type_macros.checkpatch";
 my $docsfile = "$D/../Documentation/dev-tools/checkpatch.rst";
 my $typedefsfile;
 my $color = "auto";
@@ -1053,6 +1054,10 @@ if (defined($typedefsfile)) {
 	$typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
 }

+my $type_macros = "typeof|__typeof__";
+read_words(\$type_macros, $typemacrosfile)
+    or warn "No additional type macros will be considered - file '$typemacrosfile': $!\n";
+
 sub build_types {
 	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
 	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
@@ -1066,7 +1071,7 @@ sub build_types {
 	$NonptrType	= qr{
 			(?:$Modifier\s+|const\s+)*
 			(?:
-				(?:typeof|__typeof__)\s*\([^\)]*\)|
+				(?:$type_macros)\s*\([^\)]*\)|
 				(?:$typeTypedefs\b)|
 				(?:${all}\b)
 			)
@@ -1082,7 +1087,7 @@ sub build_types {
 	$NonptrTypeWithAttr	= qr{
 			(?:$Modifier\s+|const\s+)*
 			(?:
-				(?:typeof|__typeof__)\s*\([^\)]*\)|
+				(?:$type_macros)\s*\([^\)]*\)|
 				(?:$typeTypedefs\b)|
 				(?:${allWithAttr}\b)
 			)
diff --git a/scripts/type_macros.checkpatch b/scripts/type_macros.checkpatch
new file mode 100644
index 000000000000..c910a09e2ebc
--- /dev/null
+++ b/scripts/type_macros.checkpatch
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Function-like macros that expand to type specifiers (for example,
+# struct types) and take parenthesized arguments.
+FIXTURE_DATA
+FIXTURE_VARIANT
--
2.56.1.708.g77570a6a1d-goog

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] checkpatch: externalize function-like type macros
  2026-11-18 19:39 [PATCH v2] checkpatch: externalize function-like type macros HONG Yifan
@ 2026-11-18 20:17 ` Joe Perches
  0 siblings, 0 replies; 2+ messages in thread
From: Joe Perches @ 2026-11-18 20:17 UTC (permalink / raw)
  To: HONG Yifan; +Cc: Andrew Morton, Andy Whitcroft, kernel-team, linux-kernel

On 2026-11-18 11:39, HONG Yifan wrote:

> Following scripts/const_structs.checkpatch in commit bf1fa1dae68e
> ("checkpatch: externalize the structs that should be const"), read
> additional function-like type macro names from a new companion file
> scripts/type_macros.checkpatch into $type_macros (named to mirror
> $const_structs/$conststructsfile and initialized with the built-in
> typeof and __typeof__ keywords) and use $type_macros in $NonptrType and
> $NonptrTypeWithAttr.

Yes like this.

I'll test it before i ack it tomorrow.

Thanks,  Joe

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-18 20:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18 19:39 [PATCH v2] checkpatch: externalize function-like type macros HONG Yifan
2026-11-18 20:17 ` Joe Perches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®