Skip to contentSkip to conversation

scripts: add macro_checker script to check unused parameters in macros

PATCH Applied Julian Sun 6 participants 15 messages Jul 23, 2024 – Jul 25, 2024

Subsystems (2)
lore
Share & tools
lore

Julian Sun

author
#0 lore raw

PATCH 1/1 scripts: add macro_checker script to check unused parameters in macros

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/[email protected]/
[2]: https://lore.kernel.org/linux-xfs/[email protected]/
[3]: https://lore.kernel.org/linux-bcachefs/[email protected]/
[4]: https://sourceforge.net/p/linux-f2fs/mailman/message/58797811/
[5]: https://sourceforge.net/p/linux-f2fs/mailman/message/58797812/

---
 scripts/macro_checker.py | 101 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 101 insertions(+)
 create mode 100755 scripts/macro_checker.py
scripts/macro_checker.py new file +101 -0
new file mode 100755
index 000000000000..cd10c9c10d31
@@ -0,0 +1,101 @@
+#!/usr/bin/python3
+# SPDX-License-Identifier: GPL-2.0
+# Author: Julian Sun <[email protected]>
+
+""" 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
Signature · 1 line
2.39.2
#1 lore raw
Quoted 9 lines · On Tue, 23 Jul 2024 05:11:54 -0400 Julian Sun <[email protected]> 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!

Quoted 1 line
 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?

author
#2 lore raw
Quoted 14 lines · Andrew Morton <[email protected]> 于2024年7月23日周二 18:09写道:
Quoted 9 lines · On Tue, 23 Jul 2024 05:11:54 -0400 Julian Sun <[email protected]> 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.

Quoted 1 line
Quoted 1 line

Seems a useful contribution thanks. And a nice changelog!

Thanks for your review and kind words.

Quoted 7 lines
Quoted 7 lines
 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...

Quoted 5 lines
Quoted 5 lines

"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,

Signature · 1 line
Julian Sun <[email protected]>
#3 lore raw
Quoted 7 lines · On Tue, 23 Jul 2024 23:03:53 -0400 Julian Sun <[email protected]> wrote:
Quoted 3 lines
Quoted 3 lines

"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.

#4 lore raw
In reply to Andrew Morton
Quoted 2 lines

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

author
#5 lore raw
In reply to Andrew Morton

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?

Quoted 17 lines · Andrew Morton <[email protected]> 于2024年7月23日周二 18:09写道:
Quoted 9 lines · On Tue, 23 Jul 2024 05:11:54 -0400 Julian Sun <[email protected]> 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!

Quoted 1 line
 scripts/macro_checker.py | 101 +++++++++++++++++++++++++++++++++++++++
Quoted 7 lines

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,

Signature · 1 line
Julian Sun <[email protected]>
#6 lore raw
Quoted 2 lines · On Wed, 24 Jul 2024 22:30:49 -0400 Julian Sun <[email protected]> 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.

Quoted 3 lines

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 ;)

author
#7 lore raw
Quoted 18 lines · Andrew Morton <[email protected]> 于2024年7月24日周三 22:43写道:
Quoted 2 lines · On Wed, 24 Jul 2024 22:30:49 -0400 Julian Sun <[email protected]> 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.

Quoted 7 lines

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.

Quoted 1 line

Thanks,

Signature · 1 line
Julian Sun <[email protected]>
#8 lore raw
Quoted 17 lines · Julian Sun
Quoted 16 lines · Andrew Morton <[email protected]> 于2024年7月24日周三 22:43写道:
Quoted 2 lines · On Wed, 24 Jul 2024 22:30:49 -0400 Julian Sun <[email protected]> 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.

Quoted 3 lines

If I want to continue refining this script,
should I send a new v2 version or make modifications based on the current version?

Quoted 4 lines
Quoted 2 lines
Quoted 2 lines

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

author
#9 lore raw
Quoted 21 lines · Nicolas Schier <[email protected]> 于2024年7月25日周四 00:48写道:
Quoted 19 lines · Julian Sun
Quoted 18 lines · Andrew Morton <[email protected]> 于2024年7月24日周三 22:43写道:
Quoted 2 lines · On Wed, 24 Jul 2024 22:30:49 -0400 Julian Sun <[email protected]> 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.

Quoted 7 lines

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 ;)

Quoted 7 lines
Quoted 7 lines

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.

Quoted 3 lines

Kind regards,
Nicolas

Thanks,

Signature · 1 line
Julian Sun <[email protected]>
reviewer
#10 lore raw
In reply to Julian Sun

Hi!

Quoted 41 lines · Julian Sun

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
Quoted 4 lines

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

Quoted 124 lines

[1]: https://patchwork.ozlabs.org/project/linux-ext4/patch/[email protected]/
[2]: https://lore.kernel.org/linux-xfs/[email protected]/
[3]: https://lore.kernel.org/linux-bcachefs/[email protected]/
[4]: https://sourceforge.net/p/linux-f2fs/mailman/message/58797811/
[5]: https://sourceforge.net/p/linux-f2fs/mailman/message/58797812/

---
 scripts/macro_checker.py | 101 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 101 insertions(+)
 create mode 100755 scripts/macro_checker.py
scripts/macro_checker.py new file +101 -0
new file mode 100755
index 000000000000..cd10c9c10d31
@@ -0,0 +1,101 @@
+#!/usr/bin/python3
+# SPDX-License-Identifier: GPL-2.0
+# Author: Julian Sun <[email protected]>
+
+""" 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
Signature · 1 line
2.39.2
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author
#11 lore raw
Quoted 50 lines · Jan Kara <[email protected]> 于2024年7月24日周三 06:00写道:

Hi!

Quoted 41 lines · Julian Sun

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
Quoted 4 lines

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.

Quoted 9 lines

Useful script! Thanks!

Quoted 6 lines

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.

Quoted 122 lines

Honza

Quoted 119 lines

[1]: https://patchwork.ozlabs.org/project/linux-ext4/patch/[email protected]/
[2]: https://lore.kernel.org/linux-xfs/[email protected]/
[3]: https://lore.kernel.org/linux-bcachefs/[email protected]/
[4]: https://sourceforge.net/p/linux-f2fs/mailman/message/58797811/
[5]: https://sourceforge.net/p/linux-f2fs/mailman/message/58797812/

---
 scripts/macro_checker.py | 101 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 101 insertions(+)
 create mode 100755 scripts/macro_checker.py
scripts/macro_checker.py new file +100 -0
new file mode 100755
index 000000000000..cd10c9c10d31
@@ -0,0 +1,101 @@
+#!/usr/bin/python3
+# SPDX-License-Identifier: GPL-2.0
+# Author: Julian Sun <[email protected]>
+
+""" 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__":
Quoted 8 lines
Quoted 5 lines
scripts/macro_checker.py new file +1 -1
+    main()
\ No newline at end of file
--

2.39.2

--
Jan Kara <[email protected]>
SUSE Labs, CR

Thanks,

Signature · 1 line
Julian Sun <[email protected]>
maintainer
#12 lore raw
In reply to Julian Sun
Quoted 10 lines · Julian Sun

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/[email protected]

$ git log --format=email -1 b1be5844c1a0124a49a30a20a189d0a53aa10578
From b1be5844c1a0124a49a30a20a189d0a53aa10578 Mon Sep 17 00:00:00 2001
From: Xining Xu <[email protected]>
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

author
#13 lore raw
Quoted 33 lines · Joe Perches <[email protected]> 于2024年7月24日周三 09:30写道:
Quoted 10 lines · Julian Sun

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/[email protected]

$ git log --format=email -1 b1be5844c1a0124a49a30a20a189d0a53aa10578
From b1be5844c1a0124a49a30a20a189d0a53aa10578 Mon Sep 17 00:00:00 2001
From: Xining Xu <[email protected]>
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)

Quoted 10 lines

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

Quoted 1 line

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.

Quoted 1 line

Thanks,

Signature · 1 line
Julian Sun <[email protected]>
maintainer
#14 lore raw
Quoted 40 lines · Julian Sun
Quoted 39 lines · Joe Perches <[email protected]> 于2024年7月24日周三 09:30写道:
Quoted 10 lines · Julian Sun

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/[email protected]

$ git log --format=email -1 b1be5844c1a0124a49a30a20a189d0a53aa10578
From b1be5844c1a0124a49a30a20a189d0a53aa10578 Mon Sep 17 00:00:00 2001
From: Xining Xu <[email protected]>
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)

Quoted 8 lines
Quoted 4 lines

total: 0 errors, 1 warnings, 8 lines checked

Quoted 1 line

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(-)
scripts/checkpatch.pl +3 -1
index 39032224d504f..285d29b3e9010 100755
@@ -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");
 				}