~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/scripts/macro_checker.py

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /scripts/macro_checker.py (Architecture mips) and /scripts/macro_checker.py (Architecture alpha)


  1 #!/usr/bin/python3                                  1 #!/usr/bin/python3
  2 # SPDX-License-Identifier: GPL-2.0                  2 # SPDX-License-Identifier: GPL-2.0
  3 # Author: Julian Sun <sunjunchao2870@gmail.com>      3 # Author: Julian Sun <sunjunchao2870@gmail.com>
  4                                                     4 
  5 """ Find macro definitions with unused paramet      5 """ Find macro definitions with unused parameters. """
  6                                                     6 
  7 import argparse                                     7 import argparse
  8 import os                                           8 import os
  9 import re                                           9 import re
 10                                                    10 
 11 parser = argparse.ArgumentParser()                 11 parser = argparse.ArgumentParser()
 12                                                    12 
 13 parser.add_argument("path", type=str, help="Th     13 parser.add_argument("path", type=str, help="The file or dir path that needs check")
 14 parser.add_argument("-v", "--verbose", action=     14 parser.add_argument("-v", "--verbose", action="store_true",
 15                     help="Check conditional ma     15                     help="Check conditional macros, but may lead to more false positives")
 16 args = parser.parse_args()                         16 args = parser.parse_args()
 17                                                    17 
 18 macro_pattern = r"#define\s+(\w+)\(([^)]*)\)"      18 macro_pattern = r"#define\s+(\w+)\(([^)]*)\)"
 19 # below vars were used to reduce false positiv     19 # below vars were used to reduce false positives
 20 fp_patterns = [r"\s*do\s*\{\s*\}\s*while\s*\(\     20 fp_patterns = [r"\s*do\s*\{\s*\}\s*while\s*\(\s*0\s*\)",
 21                r"\(?0\)?", r"\(?1\)?"]             21                r"\(?0\)?", r"\(?1\)?"]
 22 correct_macros = []                                22 correct_macros = []
 23 cond_compile_mark = "#if"                          23 cond_compile_mark = "#if"
 24 cond_compile_end = "#endif"                        24 cond_compile_end = "#endif"
 25                                                    25 
 26 def check_macro(macro_line, report):               26 def check_macro(macro_line, report):
 27     match = re.match(macro_pattern, macro_line     27     match = re.match(macro_pattern, macro_line)
 28     if match:                                      28     if match:
 29         macro_def = re.sub(macro_pattern, '',      29         macro_def = re.sub(macro_pattern, '', macro_line)
 30         identifier = match.group(1)                30         identifier = match.group(1)
 31         content = match.group(2)                   31         content = match.group(2)
 32         arguments = [item.strip() for item in      32         arguments = [item.strip() for item in content.split(',') if item.strip()]
 33                                                    33 
 34         macro_def = macro_def.strip()              34         macro_def = macro_def.strip()
 35         if not macro_def:                          35         if not macro_def:
 36             return                                 36             return
 37         # used to reduce false positives, like     37         # used to reduce false positives, like #define endfor_nexthops(rt) }
 38         if len(macro_def) == 1:                    38         if len(macro_def) == 1:
 39             return                                 39             return
 40                                                    40 
 41         for fp_pattern in fp_patterns:             41         for fp_pattern in fp_patterns:
 42             if (re.match(fp_pattern, macro_def     42             if (re.match(fp_pattern, macro_def)):
 43                 return                             43                 return
 44                                                    44 
 45         for arg in arguments:                      45         for arg in arguments:
 46             # used to reduce false positives       46             # used to reduce false positives
 47             if "..." in arg:                       47             if "..." in arg:
 48                 return                             48                 return
 49         for arg in arguments:                      49         for arg in arguments:
 50             if not arg in macro_def and report     50             if not arg in macro_def and report == False:
 51                 return                             51                 return
 52             # if there is a correct macro with     52             # if there is a correct macro with the same name, do not report it.
 53             if not arg in macro_def and identi     53             if not arg in macro_def and identifier not in correct_macros:
 54                 print(f"Argument {arg} is not      54                 print(f"Argument {arg} is not used in function-line macro {identifier}")
 55                 return                             55                 return
 56                                                    56 
 57         correct_macros.append(identifier)          57         correct_macros.append(identifier)
 58                                                    58 
 59                                                    59 
 60 # remove comment and whitespace                    60 # remove comment and whitespace
 61 def macro_strip(macro):                            61 def macro_strip(macro):
 62     comment_pattern1 = r"\/\/*"                    62     comment_pattern1 = r"\/\/*"
 63     comment_pattern2 = r"\/\**\*\/"                63     comment_pattern2 = r"\/\**\*\/"
 64                                                    64 
 65     macro = macro.strip()                          65     macro = macro.strip()
 66     macro = re.sub(comment_pattern1, '', macro     66     macro = re.sub(comment_pattern1, '', macro)
 67     macro = re.sub(comment_pattern2, '', macro     67     macro = re.sub(comment_pattern2, '', macro)
 68                                                    68 
 69     return macro                                   69     return macro
 70                                                    70 
 71 def file_check_macro(file_path, report):           71 def file_check_macro(file_path, report):
 72     # number of conditional compiling              72     # number of conditional compiling
 73     cond_compile = 0                               73     cond_compile = 0
 74     # only check .c and .h file                    74     # only check .c and .h file
 75     if not file_path.endswith(".c") and not fi     75     if not file_path.endswith(".c") and not file_path.endswith(".h"):
 76         return                                     76         return
 77                                                    77 
 78     with open(file_path, "r") as f:                78     with open(file_path, "r") as f:
 79         while True:                                79         while True:
 80             line = f.readline()                    80             line = f.readline()
 81             if not line:                           81             if not line:
 82                 break                              82                 break
 83             line = line.strip()                    83             line = line.strip()
 84             if line.startswith(cond_compile_ma     84             if line.startswith(cond_compile_mark):
 85                 cond_compile += 1                  85                 cond_compile += 1
 86                 continue                           86                 continue
 87             if line.startswith(cond_compile_en     87             if line.startswith(cond_compile_end):
 88                 cond_compile -= 1                  88                 cond_compile -= 1
 89                 continue                           89                 continue
 90                                                    90 
 91             macro = re.match(macro_pattern, li     91             macro = re.match(macro_pattern, line)
 92             if macro:                              92             if macro:
 93                 macro = macro_strip(macro.stri     93                 macro = macro_strip(macro.string)
 94                 while macro[-1] == '\\':           94                 while macro[-1] == '\\':
 95                     macro = macro[0:-1]            95                     macro = macro[0:-1]
 96                     macro = macro.strip()          96                     macro = macro.strip()
 97                     macro += f.readline()          97                     macro += f.readline()
 98                     macro = macro_strip(macro)     98                     macro = macro_strip(macro)
 99                 if not args.verbose:               99                 if not args.verbose:
100                     if file_path.endswith(".c"    100                     if file_path.endswith(".c")  and cond_compile != 0:
101                         continue                  101                         continue
102                     # 1 is for #ifdef xxx at t    102                     # 1 is for #ifdef xxx at the beginning of the header file
103                     if file_path.endswith(".h"    103                     if file_path.endswith(".h") and cond_compile != 1:
104                         continue                  104                         continue
105                 check_macro(macro, report)        105                 check_macro(macro, report)
106                                                   106 
107 def get_correct_macros(path):                     107 def get_correct_macros(path):
108     file_check_macro(path, False)                 108     file_check_macro(path, False)
109                                                   109 
110 def dir_check_macro(dir_path):                    110 def dir_check_macro(dir_path):
111                                                   111 
112     for dentry in os.listdir(dir_path):           112     for dentry in os.listdir(dir_path):
113         path = os.path.join(dir_path, dentry)     113         path = os.path.join(dir_path, dentry)
114         if os.path.isdir(path):                   114         if os.path.isdir(path):
115             dir_check_macro(path)                 115             dir_check_macro(path)
116         elif os.path.isfile(path):                116         elif os.path.isfile(path):
117             get_correct_macros(path)              117             get_correct_macros(path)
118             file_check_macro(path, True)          118             file_check_macro(path, True)
119                                                   119 
120                                                   120 
121 def main():                                       121 def main():
122     if os.path.isfile(args.path):                 122     if os.path.isfile(args.path):
123         get_correct_macros(args.path)             123         get_correct_macros(args.path)
124         file_check_macro(args.path, True)         124         file_check_macro(args.path, True)
125     elif os.path.isdir(args.path):                125     elif os.path.isdir(args.path):
126         dir_check_macro(args.path)                126         dir_check_macro(args.path)
127     else:                                         127     else:
128         print(f"{args.path} doesn't exit or is    128         print(f"{args.path} doesn't exit or is neither a file nor a dir")
129                                                   129 
130 if __name__ == "__main__":                        130 if __name__ == "__main__":
131     main()                                        131     main()
                                                      

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php