Message ID | 20240723091154.52458-1-sunjunchao2870@gmail.com (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | scripts: add macro_checker script to check unused parameters in macros | expand |
On Tue, 23 Jul 2024 05:11:54 -0400 Julian Sun <sunjunchao2870@gmail.com> wrote: > Hi, > > Recently, I saw a patch[1] on the ext4 mailing list regarding > the correction of a macro definition error. Jan mentioned > that "The bug in the macro is a really nasty trap...". > Because existing compilers are unable to detect > unused parameters in macro definitions. This inspired me > to write a script to check for unused parameters in > macro definitions and to run it. Seems a useful contribution thanks. And a nice changelog! > scripts/macro_checker.py | 101 +++++++++++++++++++++++++++++++++++++++ Makes me wonder who will run this, and why. Perhaps a few people will run ls and wonder "hey, what's that". But many people who might have been interested in running this simply won't know about it. "make help | grep check" shows we have a few ad-hoc integrations but I wonder if we would benefit from a top-level `make static-checks' target?
Andrew Morton <akpm@linux-foundation.org> 于2024年7月23日周二 18:09写道: > > On Tue, 23 Jul 2024 05:11:54 -0400 Julian Sun <sunjunchao2870@gmail.com> wrote: > > > Hi, > > > > Recently, I saw a patch[1] on the ext4 mailing list regarding > > the correction of a macro definition error. Jan mentioned > > that "The bug in the macro is a really nasty trap...". > > Because existing compilers are unable to detect > > unused parameters in macro definitions. This inspired me > > to write a script to check for unused parameters in > > macro definitions and to run it. > > > > Seems a useful contribution thanks. And a nice changelog! Thanks for your review and kind words. > > > scripts/macro_checker.py | 101 +++++++++++++++++++++++++++++++++++++++ > > > > Makes me wonder who will run this, and why. Perhaps a few people will > > run ls and wonder "hey, what's that". But many people who might have > > been interested in running this simply won't know about it. Yeah. For example, I am not familiar with these current checking tools... > > > > "make help | grep check" shows we have a few ad-hoc integrations but I > > wonder if we would benefit from a top-level `make static-checks' > > target? I have another idea. I asked some of my friends, they are familiar with scripts/checkpatch.pl and usually use it to check patches before submitting them. Therefore, can we integrate some checking tools like includecheck and macro_checker into checkpatch? This way, when contributors use this tool, it will automatically check the modified source files. The benefit is that contributors can benefit from these tools without knowing their existence, and over time, all source files will be checked. Best regards,
On Tue, 23 Jul 2024 23:03:53 -0400 Julian Sun <sunjunchao2870@gmail.com> wrote: > > > "make help | grep check" shows we have a few ad-hoc integrations but I > > > wonder if we would benefit from a top-level `make static-checks' > > > target? > I have another idea. I asked some of my friends, they are familiar > with scripts/checkpatch.pl and usually use it to check patches before > submitting them. Therefore, can we integrate some checking tools like > includecheck and macro_checker into checkpatch? Well, checkpatch is for checking patches - it will check whole files but isn't typically used that way as far as I know. A higher-level script which bundles the various static checking tools would be the way to go.
> > Makes me wonder who will run this, and why. I suspect once it's there we could convince folks like the 0-day robot maintainers or Jakub for nipa [1] to run it and at least flag newly reported issues. [1] https://github.com/linux-netdev/nipa Or maybe run it with W=1 like we run kernel-doc then (cmd_checkdoc and "$(call cmd,checkdoc)")? johannes
Hi! On Tue 23-07-24 05:11:54, Julian Sun wrote: > Recently, I saw a patch[1] on the ext4 mailing list regarding > the correction of a macro definition error. Jan mentioned > that "The bug in the macro is a really nasty trap...". > Because existing compilers are unable to detect > unused parameters in macro definitions. This inspired me > to write a script to check for unused parameters in > macro definitions and to run it. > > Surprisingly, the script uncovered numerous issues across > various subsystems, including filesystems, drivers, and sound etc. > > Some of these issues involved parameters that were accepted > but never used, for example: > #define XFS_DAENTER_DBS(mp,w) \ > (XFS_DA_NODE_MAXDEPTH + (((w) == XFS_DATA_FORK) ? 2 : 0)) > where mp was unused. > > While others are actual bugs. > For example: > #define HAL_SEQ_WCSS_UMAC_CE0_SRC_REG(x) \ > (ab->hw_params.regs->hal_seq_wcss_umac_ce0_src_reg) > #define HAL_SEQ_WCSS_UMAC_CE0_DST_REG(x) \ > (ab->hw_params.regs->hal_seq_wcss_umac_ce0_dst_reg) > #define HAL_SEQ_WCSS_UMAC_CE1_SRC_REG(x) \ > (ab->hw_params.regs->hal_seq_wcss_umac_ce1_src_reg) > #define HAL_SEQ_WCSS_UMAC_CE1_DST_REG(x) \ > (ab->hw_params.regs->hal_seq_wcss_umac_ce1_dst_reg) > where x was entirely unused, and instead, a local variable ab was used. > > I have submitted patches[2-5] to fix some of these issues, > but due to the large number, many still remain unaddressed. > I believe that the kernel and matainers would benefit from > this script to check for unused parameters in macro definitions. > > It should be noted that it may cause some false positives > in conditional compilation scenarios, such as > #ifdef DEBUG > static int debug(arg) {}; > #else > #define debug(arg) > #endif > So the caller needs to manually verify whether it is a true > issue. But this should be fine, because Maintainers should only > need to review their own subsystems, which typically results > in only a few reports. Useful script! Thanks! I think you could significantly reduce these false positives by checking whether the macro definition ends up being empty, 0, or "do { } while (0)" and in those cases don't issue a warning about unused arguments because it is pretty much guaranteed the author meant it this way in these cases. You seem to be already detecting the last pattern so adding the first two should be easy. Honza > > [1]: https://patchwork.ozlabs.org/project/linux-ext4/patch/1717652596-58760-1-git-send-email-carrionbent@linux.alibaba.com/ > [2]: https://lore.kernel.org/linux-xfs/20240721112701.212342-1-sunjunchao2870@gmail.com/ > [3]: https://lore.kernel.org/linux-bcachefs/20240721123943.246705-1-sunjunchao2870@gmail.com/ > [4]: https://sourceforge.net/p/linux-f2fs/mailman/message/58797811/ > [5]: https://sourceforge.net/p/linux-f2fs/mailman/message/58797812/ > > Signed-off-by: Julian Sun <sunjunchao2870@gmail.com> > --- > scripts/macro_checker.py | 101 +++++++++++++++++++++++++++++++++++++++ > 1 file changed, 101 insertions(+) > create mode 100755 scripts/macro_checker.py > > diff --git a/scripts/macro_checker.py b/scripts/macro_checker.py > new file mode 100755 > index 000000000000..cd10c9c10d31 > --- /dev/null > +++ b/scripts/macro_checker.py > @@ -0,0 +1,101 @@ > +#!/usr/bin/python3 > +# SPDX-License-Identifier: GPL-2.0 > +# Author: Julian Sun <sunjunchao2870@gmail.com> > + > +""" Find macro definitions with unused parameters. """ > + > +import argparse > +import os > +import re > + > +macro_pattern = r"#define\s+(\w+)\(([^)]*)\)" > +# below two vars were used to reduce false positives > +do_while0_pattern = r"\s*do\s*\{\s*\}\s*while\s*\(\s*0\s*\)" > +correct_macros = [] > + > +def check_macro(macro_line, report): > + match = re.match(macro_pattern, macro_line) > + if match: > + macro_def = re.sub(macro_pattern, '', macro_line) > + identifier = match.group(1) > + content = match.group(2) > + arguments = [item.strip() for item in content.split(',') if item.strip()] > + > + if (re.match(do_while0_pattern, macro_def)): > + return > + > + for arg in arguments: > + # used to reduce false positives > + if "..." in arg: > + continue > + if not arg in macro_def and report == False: > + return > + if not arg in macro_def and identifier not in correct_macros: > + print(f"Argument {arg} is not used in function-line macro {identifier}") > + return > + > + correct_macros.append(identifier) > + > + > +# remove comment and whitespace > +def macro_strip(macro): > + comment_pattern1 = r"\/\/*" > + comment_pattern2 = r"\/\**\*\/" > + > + macro = macro.strip() > + macro = re.sub(comment_pattern1, '', macro) > + macro = re.sub(comment_pattern2, '', macro) > + > + return macro > + > +def file_check_macro(file_path, report): > + # only check .c and .h file > + if not file_path.endswith(".c") and not file_path.endswith(".h"): > + return > + > + with open(file_path, "r") as f: > + while True: > + line = f.readline() > + if not line: > + return > + > + macro = re.match(macro_pattern, line) > + if macro: > + macro = macro_strip(macro.string) > + while macro[-1] == '\\': > + macro = macro[0:-1] > + macro = macro.strip() > + macro += f.readline() > + macro = macro_strip(macro) > + check_macro(macro, report) > + > +def get_correct_macros(path): > + file_check_macro(path, False) > + > +def dir_check_macro(dir_path): > + > + for dentry in os.listdir(dir_path): > + path = os.path.join(dir_path, dentry) > + if os.path.isdir(path): > + dir_check_macro(path) > + elif os.path.isfile(path): > + get_correct_macros(path) > + file_check_macro(path, True) > + > + > +def main(): > + parser = argparse.ArgumentParser() > + > + parser.add_argument("path", type=str, help="The file or dir path that needs check") > + args = parser.parse_args() > + > + if os.path.isfile(args.path): > + get_correct_macros(args.path) > + file_check_macro(args.path, True) > + elif os.path.isdir(args.path): > + dir_check_macro(args.path) > + else: > + print(f"{args.path} doesn't exit or is neither a file nor a dir") > + > +if __name__ == "__main__": > + main() > \ No newline at end of file > -- > 2.39.2 >
On Tue, 2024-07-23 at 05:11 -0400, Julian Sun wrote: > Hi, > > Recently, I saw a patch[1] on the ext4 mailing list regarding > the correction of a macro definition error. Jan mentioned > that "The bug in the macro is a really nasty trap...". > Because existing compilers are unable to detect > unused parameters in macro definitions. This inspired me > to write a script to check for unused parameters in > macro definitions and to run it. > checkpatch has a similar test: https://lkml.kernel.org/r/20240507032757.146386-3-21cnbao@gmail.com $ git log --format=email -1 b1be5844c1a0124a49a30a20a189d0a53aa10578 From b1be5844c1a0124a49a30a20a189d0a53aa10578 Mon Sep 17 00:00:00 2001 From: Xining Xu <mac.xxn@outlook.com> Date: Tue, 7 May 2024 15:27:57 +1200 Subject: [PATCH] scripts: checkpatch: check unused parameters for function-like macro If function-like macros do not utilize a parameter, it might result in a build warning. In our coding style guidelines, we advocate for utilizing static inline functions to replace such macros. This patch verifies compliance with the new rule. For a macro such as the one below, #define test(a) do { } while (0) The test result is as follows. WARNING: Argument 'a' is not used in function-like macro #21: FILE: mm/init-mm.c:20: +#define test(a) do { } while (0) total: 0 errors, 1 warnings, 8 lines checked Link: https://lkml.kernel.org/r/20240507032757.146386-3-21cnbao@gmail.com
Joe Perches <joe@perches.com> 于2024年7月24日周三 09:30写道: > > On Tue, 2024-07-23 at 05:11 -0400, Julian Sun wrote: > > Hi, > > > > Recently, I saw a patch[1] on the ext4 mailing list regarding > > the correction of a macro definition error. Jan mentioned > > that "The bug in the macro is a really nasty trap...". > > Because existing compilers are unable to detect > > unused parameters in macro definitions. This inspired me > > to write a script to check for unused parameters in > > macro definitions and to run it. > > > > checkpatch has a similar test: > > https://lkml.kernel.org/r/20240507032757.146386-3-21cnbao@gmail.com > > $ git log --format=email -1 b1be5844c1a0124a49a30a20a189d0a53aa10578 > From b1be5844c1a0124a49a30a20a189d0a53aa10578 Mon Sep 17 00:00:00 2001 > From: Xining Xu <mac.xxn@outlook.com> > Date: Tue, 7 May 2024 15:27:57 +1200 > Subject: [PATCH] scripts: checkpatch: check unused parameters for > function-like macro > > If function-like macros do not utilize a parameter, it might result in a > build warning. In our coding style guidelines, we advocate for utilizing > static inline functions to replace such macros. This patch verifies > compliance with the new rule. > > For a macro such as the one below, > > #define test(a) do { } while (0) > > The test result is as follows. > > WARNING: Argument 'a' is not used in function-like macro > #21: FILE: mm/init-mm.c:20: > +#define test(a) do { } while (0) > > total: 0 errors, 1 warnings, 8 lines checked > > > > Link: https://lkml.kernel.org/r/20240507032757.146386-3-21cnbao@gmail.com Yeah, I noticted the test. The difference between checkpatch and macro_checker is that checkpatch only checks the patch files, instead of the entire source files, which results in the inability to check all macros in source files. > Thanks,
Jan Kara <jack@suse.cz> 于2024年7月24日周三 06:00写道: > > Hi! > > On Tue 23-07-24 05:11:54, Julian Sun wrote: > > Recently, I saw a patch[1] on the ext4 mailing list regarding > > the correction of a macro definition error. Jan mentioned > > that "The bug in the macro is a really nasty trap...". > > Because existing compilers are unable to detect > > unused parameters in macro definitions. This inspired me > > to write a script to check for unused parameters in > > macro definitions and to run it. > > > > Surprisingly, the script uncovered numerous issues across > > various subsystems, including filesystems, drivers, and sound etc. > > > > Some of these issues involved parameters that were accepted > > but never used, for example: > > #define XFS_DAENTER_DBS(mp,w) \ > > (XFS_DA_NODE_MAXDEPTH + (((w) == XFS_DATA_FORK) ? 2 : 0)) > > where mp was unused. > > > > While others are actual bugs. > > For example: > > #define HAL_SEQ_WCSS_UMAC_CE0_SRC_REG(x) \ > > (ab->hw_params.regs->hal_seq_wcss_umac_ce0_src_reg) > > #define HAL_SEQ_WCSS_UMAC_CE0_DST_REG(x) \ > > (ab->hw_params.regs->hal_seq_wcss_umac_ce0_dst_reg) > > #define HAL_SEQ_WCSS_UMAC_CE1_SRC_REG(x) \ > > (ab->hw_params.regs->hal_seq_wcss_umac_ce1_src_reg) > > #define HAL_SEQ_WCSS_UMAC_CE1_DST_REG(x) \ > > (ab->hw_params.regs->hal_seq_wcss_umac_ce1_dst_reg) > > where x was entirely unused, and instead, a local variable ab was used. > > > > I have submitted patches[2-5] to fix some of these issues, > > but due to the large number, many still remain unaddressed. > > I believe that the kernel and matainers would benefit from > > this script to check for unused parameters in macro definitions. > > > > It should be noted that it may cause some false positives > > in conditional compilation scenarios, such as > > #ifdef DEBUG > > static int debug(arg) {}; > > #else > > #define debug(arg) > > #endif > > So the caller needs to manually verify whether it is a true > > issue. But this should be fine, because Maintainers should only > > need to review their own subsystems, which typically results > > in only a few reports. > > Useful script! Thanks! > > > > I think you could significantly reduce these false positives by checking > > whether the macro definition ends up being empty, 0, or "do { } while (0)" > > and in those cases don't issue a warning about unused arguments because it > > is pretty much guaranteed the author meant it this way in these cases. You > > seem to be already detecting the last pattern so adding the first two > > should be easy. Great suggestion! I will refine this script and send patch v2. Thanks for you suggestion, Jan. > > Honza > > > > > [1]: https://patchwork.ozlabs.org/project/linux-ext4/patch/1717652596-58760-1-git-send-email-carrionbent@linux.alibaba.com/ > > [2]: https://lore.kernel.org/linux-xfs/20240721112701.212342-1-sunjunchao2870@gmail.com/ > > [3]: https://lore.kernel.org/linux-bcachefs/20240721123943.246705-1-sunjunchao2870@gmail.com/ > > [4]: https://sourceforge.net/p/linux-f2fs/mailman/message/58797811/ > > [5]: https://sourceforge.net/p/linux-f2fs/mailman/message/58797812/ > > > > Signed-off-by: Julian Sun <sunjunchao2870@gmail.com> > > --- > > scripts/macro_checker.py | 101 +++++++++++++++++++++++++++++++++++++++ > > 1 file changed, 101 insertions(+) > > create mode 100755 scripts/macro_checker.py > > > > diff --git a/scripts/macro_checker.py b/scripts/macro_checker.py > > new file mode 100755 > > index 000000000000..cd10c9c10d31 > > --- /dev/null > > +++ b/scripts/macro_checker.py > > @@ -0,0 +1,101 @@ > > +#!/usr/bin/python3 > > +# SPDX-License-Identifier: GPL-2.0 > > +# Author: Julian Sun <sunjunchao2870@gmail.com> > > + > > +""" Find macro definitions with unused parameters. """ > > + > > +import argparse > > +import os > > +import re > > + > > +macro_pattern = r"#define\s+(\w+)\(([^)]*)\)" > > +# below two vars were used to reduce false positives > > +do_while0_pattern = r"\s*do\s*\{\s*\}\s*while\s*\(\s*0\s*\)" > > +correct_macros = [] > > + > > +def check_macro(macro_line, report): > > + match = re.match(macro_pattern, macro_line) > > + if match: > > + macro_def = re.sub(macro_pattern, '', macro_line) > > + identifier = match.group(1) > > + content = match.group(2) > > + arguments = [item.strip() for item in content.split(',') if item.strip()] > > + > > + if (re.match(do_while0_pattern, macro_def)): > > + return > > + > > + for arg in arguments: > > + # used to reduce false positives > > + if "..." in arg: > > + continue > > + if not arg in macro_def and report == False: > > + return > > + if not arg in macro_def and identifier not in correct_macros: > > + print(f"Argument {arg} is not used in function-line macro {identifier}") > > + return > > + > > + correct_macros.append(identifier) > > + > > + > > +# remove comment and whitespace > > +def macro_strip(macro): > > + comment_pattern1 = r"\/\/*" > > + comment_pattern2 = r"\/\**\*\/" > > + > > + macro = macro.strip() > > + macro = re.sub(comment_pattern1, '', macro) > > + macro = re.sub(comment_pattern2, '', macro) > > + > > + return macro > > + > > +def file_check_macro(file_path, report): > > + # only check .c and .h file > > + if not file_path.endswith(".c") and not file_path.endswith(".h"): > > + return > > + > > + with open(file_path, "r") as f: > > + while True: > > + line = f.readline() > > + if not line: > > + return > > + > > + macro = re.match(macro_pattern, line) > > + if macro: > > + macro = macro_strip(macro.string) > > + while macro[-1] == '\\': > > + macro = macro[0:-1] > > + macro = macro.strip() > > + macro += f.readline() > > + macro = macro_strip(macro) > > + check_macro(macro, report) > > + > > +def get_correct_macros(path): > > + file_check_macro(path, False) > > + > > +def dir_check_macro(dir_path): > > + > > + for dentry in os.listdir(dir_path): > > + path = os.path.join(dir_path, dentry) > > + if os.path.isdir(path): > > + dir_check_macro(path) > > + elif os.path.isfile(path): > > + get_correct_macros(path) > > + file_check_macro(path, True) > > + > > + > > +def main(): > > + parser = argparse.ArgumentParser() > > + > > + parser.add_argument("path", type=str, help="The file or dir path that needs check") > > + args = parser.parse_args() > > + > > + if os.path.isfile(args.path): > > + get_correct_macros(args.path) > > + file_check_macro(args.path, True) > > + elif os.path.isdir(args.path): > > + dir_check_macro(args.path) > > + else: > > + print(f"{args.path} doesn't exit or is neither a file nor a dir") > > + > > +if __name__ == "__main__": > > + main() > > \ No newline at end of file > > -- > > 2.39.2 > > > -- > Jan Kara <jack@suse.com> > SUSE Labs, CR Thanks,
Hi, I noticed that you have already merged this patch into the mm-nonmm-unstable branch. If I want to continue refining this script, should I send a new v2 version or make modifications based on the current version? Andrew Morton <akpm@linux-foundation.org> 于2024年7月23日周二 18:09写道: > > On Tue, 23 Jul 2024 05:11:54 -0400 Julian Sun <sunjunchao2870@gmail.com> wrote: > > > Hi, > > > > Recently, I saw a patch[1] on the ext4 mailing list regarding > > the correction of a macro definition error. Jan mentioned > > that "The bug in the macro is a really nasty trap...". > > Because existing compilers are unable to detect > > unused parameters in macro definitions. This inspired me > > to write a script to check for unused parameters in > > macro definitions and to run it. > > Seems a useful contribution thanks. And a nice changelog! > > > scripts/macro_checker.py | 101 +++++++++++++++++++++++++++++++++++++++ > > Makes me wonder who will run this, and why. Perhaps a few people will > run ls and wonder "hey, what's that". But many people who might have > been interested in running this simply won't know about it. > > "make help | grep check" shows we have a few ad-hoc integrations but I > wonder if we would benefit from a top-level `make static-checks' > target? Thanks,
On Wed, 24 Jul 2024 22:30:49 -0400 Julian Sun <sunjunchao2870@gmail.com> wrote: > I noticed that you have already merged this patch into the > mm-nonmm-unstable branch. Yup. If a patch looks desirable (and reasonably close to ready) I'll grab it to give it some exposure and testing while it's under development, To help things along and to hopefully arrive at a batter end result. > If I want to continue refining this script, > should I send a new v2 version or make modifications based on the > current version? Either is OK - whatever makes most sense for the reviewers. Reissuing a large patch series for a single line change is counterproductive ;)
Andrew Morton <akpm@linux-foundation.org> 于2024年7月24日周三 22:43写道: > > On Wed, 24 Jul 2024 22:30:49 -0400 Julian Sun <sunjunchao2870@gmail.com> wrote: > > > I noticed that you have already merged this patch into the > > mm-nonmm-unstable branch. > > Yup. If a patch looks desirable (and reasonably close to ready) I'll > grab it to give it some exposure and testing while it's under > development, To help things along and to hopefully arrive at a batter > end result. > > > If I want to continue refining this script, > > should I send a new v2 version or make modifications based on the > > current version? > > > > Either is OK - whatever makes most sense for the reviewers. Reissuing > > a large patch series for a single line change is counterproductive ;) Thanks for your clarification. I will send a new patch based on the current version. > Thanks,
On Wed, 2024-07-24 at 22:09 -0400, Julian Sun wrote: > Joe Perches <joe@perches.com> 于2024年7月24日周三 09:30写道: > > > > On Tue, 2024-07-23 at 05:11 -0400, Julian Sun wrote: > > > Hi, > > > > > > Recently, I saw a patch[1] on the ext4 mailing list regarding > > > the correction of a macro definition error. Jan mentioned > > > that "The bug in the macro is a really nasty trap...". > > > Because existing compilers are unable to detect > > > unused parameters in macro definitions. This inspired me > > > to write a script to check for unused parameters in > > > macro definitions and to run it. > > > > > > > checkpatch has a similar test: > > > > https://lkml.kernel.org/r/20240507032757.146386-3-21cnbao@gmail.com > > > > $ git log --format=email -1 b1be5844c1a0124a49a30a20a189d0a53aa10578 > > From b1be5844c1a0124a49a30a20a189d0a53aa10578 Mon Sep 17 00:00:00 2001 > > From: Xining Xu <mac.xxn@outlook.com> > > Date: Tue, 7 May 2024 15:27:57 +1200 > > Subject: [PATCH] scripts: checkpatch: check unused parameters for > > function-like macro > > > > If function-like macros do not utilize a parameter, it might result in a > > build warning. In our coding style guidelines, we advocate for utilizing > > static inline functions to replace such macros. This patch verifies > > compliance with the new rule. > > > > For a macro such as the one below, > > > > #define test(a) do { } while (0) > > > > The test result is as follows. > > > > WARNING: Argument 'a' is not used in function-like macro > > #21: FILE: mm/init-mm.c:20: > > +#define test(a) do { } while (0) > > > > total: 0 errors, 1 warnings, 8 lines checked > > > > > > > Link: https://lkml.kernel.org/r/20240507032757.146386-3-21cnbao@gmail.com > Yeah, I noticted the test. The difference between checkpatch and > macro_checker is that checkpatch only checks the patch files, instead > of the entire source files, which results in the inability to check > all macros in source files. Another possibility: $ git ls-files -- '*.[ch]' | \ xargs ./scripts/checkpatch -f --terse --no-summary --types=MACRO_ARG_UNUSED Though I agree the addition of a test for "do {} while (0)" and no content would be also be useful for unused macro args tests. --- scripts/checkpatch.pl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 39032224d504f..285d29b3e9010 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -6060,7 +6060,9 @@ sub process { } # check if this is an unused argument - if ($define_stmt !~ /\b$arg\b/) { + if ($define_stmt !~ /\b$arg\b/ && + $define_stmt !~ /^$/ && + $define_stmt !~ /^do\s*\{\s*\}\s*while\s*\(\s*0\s*\)$/) { WARN("MACRO_ARG_UNUSED", "Argument '$arg' is not used in function-like macro\n" . "$herectx"); }
On Wed, Jul 24, 2024 at 11:05:43PM -0400, Julian Sun wrote: > Andrew Morton <akpm@linux-foundation.org> 于2024年7月24日周三 22:43写道: > > > > On Wed, 24 Jul 2024 22:30:49 -0400 Julian Sun <sunjunchao2870@gmail.com> wrote: > > > > > I noticed that you have already merged this patch into the > > > mm-nonmm-unstable branch. > > > > Yup. If a patch looks desirable (and reasonably close to ready) I'll > > grab it to give it some exposure and testing while it's under > > development, To help things along and to hopefully arrive at a batter > > end result. > > > > > If I want to continue refining this script, > > > should I send a new v2 version or make modifications based on the > > > current version? > > > > > > > Either is OK - whatever makes most sense for the reviewers. Reissuing > > > a large patch series for a single line change is counterproductive ;) > Thanks for your clarification. I will send a new patch based on the > current version. Hi Julian, can you please Cc linux-kbuild in v2? Kind regards, Nicolas
Nicolas Schier <n.schier@avm.de> 于2024年7月25日周四 00:48写道: > > On Wed, Jul 24, 2024 at 11:05:43PM -0400, Julian Sun wrote: > > Andrew Morton <akpm@linux-foundation.org> 于2024年7月24日周三 22:43写道: > > > > > > On Wed, 24 Jul 2024 22:30:49 -0400 Julian Sun <sunjunchao2870@gmail.com> wrote: > > > > > > > I noticed that you have already merged this patch into the > > > > mm-nonmm-unstable branch. > > > > > > Yup. If a patch looks desirable (and reasonably close to ready) I'll > > > grab it to give it some exposure and testing while it's under > > > development, To help things along and to hopefully arrive at a batter > > > end result. > > > > > > > If I want to continue refining this script, > > > > should I send a new v2 version or make modifications based on the > > > > current version? > > > > > > > > > > Either is OK - whatever makes most sense for the reviewers. Reissuing > > > > a large patch series for a single line change is counterproductive ;) > > Thanks for your clarification. I will send a new patch based on the > > current version. > > > > Hi Julian, > > > > can you please Cc linux-kbuild in v2? Sure, sorry for missing that. I have sent another patch to reduce false positives and CCed linux-kbuild. > > Kind regards, > Nicolas Thanks,
diff --git a/scripts/macro_checker.py b/scripts/macro_checker.py new file mode 100755 index 000000000000..cd10c9c10d31 --- /dev/null +++ b/scripts/macro_checker.py @@ -0,0 +1,101 @@ +#!/usr/bin/python3 +# SPDX-License-Identifier: GPL-2.0 +# Author: Julian Sun <sunjunchao2870@gmail.com> + +""" Find macro definitions with unused parameters. """ + +import argparse +import os +import re + +macro_pattern = r"#define\s+(\w+)\(([^)]*)\)" +# below two vars were used to reduce false positives +do_while0_pattern = r"\s*do\s*\{\s*\}\s*while\s*\(\s*0\s*\)" +correct_macros = [] + +def check_macro(macro_line, report): + match = re.match(macro_pattern, macro_line) + if match: + macro_def = re.sub(macro_pattern, '', macro_line) + identifier = match.group(1) + content = match.group(2) + arguments = [item.strip() for item in content.split(',') if item.strip()] + + if (re.match(do_while0_pattern, macro_def)): + return + + for arg in arguments: + # used to reduce false positives + if "..." in arg: + continue + if not arg in macro_def and report == False: + return + if not arg in macro_def and identifier not in correct_macros: + print(f"Argument {arg} is not used in function-line macro {identifier}") + return + + correct_macros.append(identifier) + + +# remove comment and whitespace +def macro_strip(macro): + comment_pattern1 = r"\/\/*" + comment_pattern2 = r"\/\**\*\/" + + macro = macro.strip() + macro = re.sub(comment_pattern1, '', macro) + macro = re.sub(comment_pattern2, '', macro) + + return macro + +def file_check_macro(file_path, report): + # only check .c and .h file + if not file_path.endswith(".c") and not file_path.endswith(".h"): + return + + with open(file_path, "r") as f: + while True: + line = f.readline() + if not line: + return + + macro = re.match(macro_pattern, line) + if macro: + macro = macro_strip(macro.string) + while macro[-1] == '\\': + macro = macro[0:-1] + macro = macro.strip() + macro += f.readline() + macro = macro_strip(macro) + check_macro(macro, report) + +def get_correct_macros(path): + file_check_macro(path, False) + +def dir_check_macro(dir_path): + + for dentry in os.listdir(dir_path): + path = os.path.join(dir_path, dentry) + if os.path.isdir(path): + dir_check_macro(path) + elif os.path.isfile(path): + get_correct_macros(path) + file_check_macro(path, True) + + +def main(): + parser = argparse.ArgumentParser() + + parser.add_argument("path", type=str, help="The file or dir path that needs check") + args = parser.parse_args() + + if os.path.isfile(args.path): + get_correct_macros(args.path) + file_check_macro(args.path, True) + elif os.path.isdir(args.path): + dir_check_macro(args.path) + else: + print(f"{args.path} doesn't exit or is neither a file nor a dir") + +if __name__ == "__main__": + main() \ No newline at end of file
Hi, Recently, I saw a patch[1] on the ext4 mailing list regarding the correction of a macro definition error. Jan mentioned that "The bug in the macro is a really nasty trap...". Because existing compilers are unable to detect unused parameters in macro definitions. This inspired me to write a script to check for unused parameters in macro definitions and to run it. Surprisingly, the script uncovered numerous issues across various subsystems, including filesystems, drivers, and sound etc. Some of these issues involved parameters that were accepted but never used, for example: #define XFS_DAENTER_DBS(mp,w) \ (XFS_DA_NODE_MAXDEPTH + (((w) == XFS_DATA_FORK) ? 2 : 0)) where mp was unused. While others are actual bugs. For example: #define HAL_SEQ_WCSS_UMAC_CE0_SRC_REG(x) \ (ab->hw_params.regs->hal_seq_wcss_umac_ce0_src_reg) #define HAL_SEQ_WCSS_UMAC_CE0_DST_REG(x) \ (ab->hw_params.regs->hal_seq_wcss_umac_ce0_dst_reg) #define HAL_SEQ_WCSS_UMAC_CE1_SRC_REG(x) \ (ab->hw_params.regs->hal_seq_wcss_umac_ce1_src_reg) #define HAL_SEQ_WCSS_UMAC_CE1_DST_REG(x) \ (ab->hw_params.regs->hal_seq_wcss_umac_ce1_dst_reg) where x was entirely unused, and instead, a local variable ab was used. I have submitted patches[2-5] to fix some of these issues, but due to the large number, many still remain unaddressed. I believe that the kernel and matainers would benefit from this script to check for unused parameters in macro definitions. It should be noted that it may cause some false positives in conditional compilation scenarios, such as #ifdef DEBUG static int debug(arg) {}; #else #define debug(arg) #endif So the caller needs to manually verify whether it is a true issue. But this should be fine, because Maintainers should only need to review their own subsystems, which typically results in only a few reports. [1]: https://patchwork.ozlabs.org/project/linux-ext4/patch/1717652596-58760-1-git-send-email-carrionbent@linux.alibaba.com/ [2]: https://lore.kernel.org/linux-xfs/20240721112701.212342-1-sunjunchao2870@gmail.com/ [3]: https://lore.kernel.org/linux-bcachefs/20240721123943.246705-1-sunjunchao2870@gmail.com/ [4]: https://sourceforge.net/p/linux-f2fs/mailman/message/58797811/ [5]: https://sourceforge.net/p/linux-f2fs/mailman/message/58797812/ Signed-off-by: Julian Sun <sunjunchao2870@gmail.com> --- scripts/macro_checker.py | 101 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100755 scripts/macro_checker.py