2 # SPDX-License-Identifier: GPL-2.0
5 """ Find macro definitions with unused parameters. """
11 parser = argparse.ArgumentParser()
13 parser.add_argument("path", type=str, help="The file or dir path that needs check")
14 parser.add_argument("-v", "--verbose", action="store_true",
15 help="Check conditional macros, but may lead to more false positives")
16 args = parser.parse_args()
18 macro_pattern = r"#define\s+(\w+)\(([^)]*)\)"
19 # below vars were used to reduce false positives
20 fp_patterns = [r"\s*do\s*\{\s*\}\s*while\s*\(\s*0\s*\)",
21 r"\(?0\)?", r"\(?1\)?"]
23 cond_compile_mark = "#if"
24 cond_compile_end = "#endif"
26 def check_macro(macro_line, report):
27 match = re.match(macro_pattern, macro_line)
29 macro_def = re.sub(macro_pattern, '', macro_line)
30 identifier = match.group(1)
31 content = match.group(2)
32 arguments = [item.strip() for item in content.split(',') if item.strip()]
34 macro_def = macro_def.strip()
37 # used to reduce false positives, like #define endfor_nexthops(rt) }
38 if len(macro_def) == 1:
41 for fp_pattern in fp_patterns:
42 if (re.match(fp_pattern, macro_def)):
46 # used to reduce false positives
50 if not arg in macro_def and report == False:
52 # if there is a correct macro with the same name, do not report it.
53 if not arg in macro_def and identifier not in correct_macros:
54 print(f"Argument {arg} is not used in function-line macro {identifier}")
57 correct_macros.append(identifier)
60 # remove comment and whitespace
61 def macro_strip(macro):
62 comment_pattern1 = r"\/\/*"
63 comment_pattern2 = r"\/\**\*\/"
66 macro = re.sub(comment_pattern1, '', macro)
67 macro = re.sub(comment_pattern2, '', macro)
71 def file_check_macro(file_path, report):
72 # number of conditional compiling
74 # only check .c and .h file
75 if not file_path.endswith(".c") and not file_path.endswith(".h"):
78 with open(file_path, "r") as f:
84 if line.startswith(cond_compile_mark):
87 if line.startswith(cond_compile_end):
91 macro = re.match(macro_pattern, line)
93 macro = macro_strip(macro.string)
94 while macro[-1] == '\\':
98 macro = macro_strip(macro)
100 if file_path.endswith(".c") and cond_compile != 0:
102 # 1 is for #ifdef xxx at the beginning of the header file
103 if file_path.endswith(".h") and cond_compile != 1:
105 check_macro(macro, report)
107 def get_correct_macros(path):
108 file_check_macro(path, False)
110 def dir_check_macro(dir_path):
112 for dentry in os.listdir(dir_path):
113 path = os.path.join(dir_path, dentry)
114 if os.path.isdir(path):
115 dir_check_macro(path)
116 elif os.path.isfile(path):
117 get_correct_macros(path)
118 file_check_macro(path, True)
122 if os.path.isfile(args.path):
123 get_correct_macros(args.path)
124 file_check_macro(args.path, True)
125 elif os.path.isdir(args.path):
126 dir_check_macro(args.path)
128 print(f"{args.path} doesn't exit or is neither a file nor a dir")
130 if __name__ == "__main__":