1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2011 The Chromium OS Authors.
10 from patman import command
11 from patman import gitutil
12 from patman import terminal
13 from patman import tools
16 top_level = gitutil.GetTopLevel()
19 os.path.join(os.getcwd(), '..', '..'),
20 os.path.join(top_level, 'tools'),
21 os.path.join(top_level, 'scripts'),
22 '%s/bin' % os.getenv('HOME'),
26 fname = os.path.join(path, 'checkpatch.pl')
27 if os.path.isfile(fname):
30 # Look upwwards for a Chrome OS tree
31 while not os.path.ismount(path):
32 fname = os.path.join(path, 'src', 'third_party', 'kernel', 'files',
33 'scripts', 'checkpatch.pl')
34 if os.path.isfile(fname):
36 path = os.path.dirname(path)
38 sys.exit('Cannot find checkpatch.pl - please put it in your ' +
39 '~/bin directory or use --no-check')
41 def CheckPatch(fname, verbose=False):
42 """Run checkpatch.pl on a file.
45 namedtuple containing:
46 ok: False=failure, True=ok
47 problems: List of problems, each a dict:
48 'type'; error or warning
52 errors: Number of errors
53 warnings: Number of warnings
54 checks: Number of checks
55 lines: Number of lines
56 stdout: Full output of checkpatch
58 fields = ['ok', 'problems', 'errors', 'warnings', 'checks', 'lines',
60 result = collections.namedtuple('CheckPatchResult', fields)
62 result.errors, result.warnings, result.checks = 0, 0, 0
65 chk = FindCheckPatch()
67 result.stdout = command.Output(chk, '--no-tree', fname,
69 #pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE)
70 #stdout, stderr = pipe.communicate()
72 # total: 0 errors, 0 warnings, 159 lines checked
74 # total: 0 errors, 2 warnings, 7 checks, 473 lines checked
75 emacs_prefix = '(?:[0-9]{4}.*\.patch:[0-9]+: )?'
76 emacs_stats = '(?:[0-9]{4}.*\.patch )?'
77 re_stats = re.compile(emacs_stats +
78 'total: (\\d+) errors, (\d+) warnings, (\d+)')
79 re_stats_full = re.compile(emacs_stats +
80 'total: (\\d+) errors, (\d+) warnings, (\d+)'
82 re_ok = re.compile('.*has no obvious style problems')
83 re_bad = re.compile('.*has style problems, please review')
84 re_error = re.compile('ERROR: (.*)')
85 re_warning = re.compile(emacs_prefix + 'WARNING:(?:[A-Z_]+:)? (.*)')
86 re_check = re.compile('CHECK: (.*)')
87 re_file = re.compile('#\d+: FILE: ([^:]*):(\d+):')
88 re_note = re.compile('NOTE: (.*)')
90 for line in result.stdout.splitlines():
94 # A blank line indicates the end of a message
97 result.problems.append(item)
100 if re_note.match(line):
102 # Skip lines which quote code
103 if line.startswith(indent):
105 # Skip code quotes and #<n>
106 if line.startswith('+') or line.startswith('#'):
108 match = re_stats_full.match(line)
110 match = re_stats.match(line)
112 result.errors = int(match.group(1))
113 result.warnings = int(match.group(2))
114 if len(match.groups()) == 4:
115 result.checks = int(match.group(3))
116 result.lines = int(match.group(4))
118 result.lines = int(match.group(3))
120 elif re_ok.match(line):
123 elif re_bad.match(line):
126 err_match = re_error.match(line)
127 warn_match = re_warning.match(line)
128 file_match = re_file.match(line)
129 check_match = re_check.match(line)
130 subject_match = line.startswith('Subject:')
132 item['msg'] = err_match.group(1)
133 item['type'] = 'error'
135 item['msg'] = warn_match.group(1)
136 item['type'] = 'warning'
138 item['msg'] = check_match.group(1)
139 item['type'] = 'check'
141 item['file'] = file_match.group(1)
142 item['line'] = int(file_match.group(2))
144 item['file'] = '<patch subject>'
147 print('bad line "%s", %d' % (line, len(line)))
151 def GetWarningMsg(col, msg_type, fname, line, msg):
152 '''Create a message for a given file/line
155 msg_type: Message type ('error' or 'warning')
156 fname: Filename which reports the problem
157 line: Line number where it was noticed
158 msg: Message to report
160 if msg_type == 'warning':
161 msg_type = col.Color(col.YELLOW, msg_type)
162 elif msg_type == 'error':
163 msg_type = col.Color(col.RED, msg_type)
164 elif msg_type == 'check':
165 msg_type = col.Color(col.MAGENTA, msg_type)
166 line_str = '' if line is None else '%d' % line
167 return '%s:%s: %s: %s\n' % (fname, line_str, msg_type, msg)
169 def CheckPatches(verbose, args):
170 '''Run the checkpatch.pl script on each patch'''
171 error_count, warning_count, check_count = 0, 0, 0
172 col = terminal.Color()
175 result = CheckPatch(fname, verbose)
177 error_count += result.errors
178 warning_count += result.warnings
179 check_count += result.checks
180 print('%d errors, %d warnings, %d checks for %s:' % (result.errors,
181 result.warnings, result.checks, col.Color(col.BLUE, fname)))
182 if (len(result.problems) != result.errors + result.warnings +
184 print("Internal error: some problems lost")
185 for item in result.problems:
187 GetWarningMsg(col, item.get('type', '<unknown>'),
188 item.get('file', '<unknown>'),
189 item.get('line', 0), item.get('msg', 'message')))
192 if error_count or warning_count or check_count:
193 str = 'checkpatch.pl found %d error(s), %d warning(s), %d checks(s)'
199 print(col.Color(color, str % (error_count, warning_count, check_count)))