scripts/macro_checker.py
Source file repositories/reference/linux-study-clean/scripts/macro_checker.py
File Facts
- System
- Linux kernel
- Corpus path
scripts/macro_checker.py- Extension
.py- Size
- 4198 bytes
- Lines
- 131
- Domain
- Support Tooling And Documentation
- Bucket
- scripts
- Inferred role
- Support Tooling And Documentation: scripts
- Status
- atlas-only
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
Dependency Surface
- No C-style include directives detected by the generator.
Detected Declarations
- No top-level syscall, struct, function, initcall, or export declaration detected by the generator.
Annotated Snippet
#!/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
parser = argparse.ArgumentParser()
parser.add_argument("path", type=str, help="The file or dir path that needs check")
parser.add_argument("-v", "--verbose", action="store_true",
help="Check conditional macros, but may lead to more false positives")
args = parser.parse_args()
macro_pattern = r"#define\s+(\w+)\(([^)]*)\)"
# below vars were used to reduce false positives
fp_patterns = [r"\s*do\s*\{\s*\}\s*while\s*\(\s*0\s*\)",
r"\(?0\)?", r"\(?1\)?"]
correct_macros = []
cond_compile_mark = "#if"
cond_compile_end = "#endif"
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()]
macro_def = macro_def.strip()
if not macro_def:
return
# used to reduce false positives, like #define endfor_nexthops(rt) }
if len(macro_def) == 1:
return
for fp_pattern in fp_patterns:
if (re.match(fp_pattern, macro_def)):
return
for arg in arguments:
# used to reduce false positives
if "..." in arg:
return
for arg in arguments:
if not arg in macro_def and report == False:
return
# if there is a correct macro with the same name, do not report it.
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
Annotation
- Atlas domain: Support Tooling And Documentation / scripts.
- Implementation status: atlas-only.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.