checkpatch: externalize function-like type macros
PATCH v2 1/1 checkpatch: externalize function-like type macros
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.
---
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/[email protected]
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
MAINTAINERS +1 -0
@@ -6264,6 +6264,7 @@ CHECKPATCH M: Andy Whitcroft <[email protected]> M: Joe Perches <[email protected]> S: Maintained +F: scripts/*.checkpatch F: scripts/checkpatch.pl CHECKPATCH DOCUMENTATION
scripts/checkpatch.pl +7 -2
@@ -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) )
scripts/type_macros.checkpatch new file +6 -0
@@ -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
Joe Perches
maintainerQuoted 7 lines · HONG Yifan
Following
scripts/const_structs.checkpatchin commitbf1fa1dae68e("checkpatch: externalize the structs that should be const"), read additional function-like type macro names from a new companion filescripts/type_macros.checkpatchinto $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