checkpatch.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2011 The Chromium OS Authors.
  3. #
  4. import collections
  5. import os
  6. import re
  7. import sys
  8. from patman import command
  9. from patman import gitutil
  10. from patman import terminal
  11. EMACS_PREFIX = r'(?:[0-9]{4}.*\.patch:[0-9]+: )?'
  12. TYPE_NAME = r'([A-Z_]+:)?'
  13. RE_ERROR = re.compile(r'ERROR:%s (.*)' % TYPE_NAME)
  14. RE_WARNING = re.compile(EMACS_PREFIX + r'WARNING:%s (.*)' % TYPE_NAME)
  15. RE_CHECK = re.compile(r'CHECK:%s (.*)' % TYPE_NAME)
  16. RE_FILE = re.compile(r'#(\d+): (FILE: ([^:]*):(\d+):)?')
  17. RE_NOTE = re.compile(r'NOTE: (.*)')
  18. def FindCheckPatch():
  19. top_level = gitutil.GetTopLevel()
  20. try_list = [
  21. os.getcwd(),
  22. os.path.join(os.getcwd(), '..', '..'),
  23. os.path.join(top_level, 'tools'),
  24. os.path.join(top_level, 'scripts'),
  25. '%s/bin' % os.getenv('HOME'),
  26. ]
  27. # Look in current dir
  28. for path in try_list:
  29. fname = os.path.join(path, 'checkpatch.pl')
  30. if os.path.isfile(fname):
  31. return fname
  32. # Look upwwards for a Chrome OS tree
  33. while not os.path.ismount(path):
  34. fname = os.path.join(path, 'src', 'third_party', 'kernel', 'files',
  35. 'scripts', 'checkpatch.pl')
  36. if os.path.isfile(fname):
  37. return fname
  38. path = os.path.dirname(path)
  39. sys.exit('Cannot find checkpatch.pl - please put it in your ' +
  40. '~/bin directory or use --no-check')
  41. def CheckPatchParseOneMessage(message):
  42. """Parse one checkpatch message
  43. Args:
  44. message: string to parse
  45. Returns:
  46. dict:
  47. 'type'; error or warning
  48. 'msg': text message
  49. 'file' : filename
  50. 'line': line number
  51. """
  52. if RE_NOTE.match(message):
  53. return {}
  54. item = {}
  55. err_match = RE_ERROR.match(message)
  56. warn_match = RE_WARNING.match(message)
  57. check_match = RE_CHECK.match(message)
  58. if err_match:
  59. item['cptype'] = err_match.group(1)
  60. item['msg'] = err_match.group(2)
  61. item['type'] = 'error'
  62. elif warn_match:
  63. item['cptype'] = warn_match.group(1)
  64. item['msg'] = warn_match.group(2)
  65. item['type'] = 'warning'
  66. elif check_match:
  67. item['cptype'] = check_match.group(1)
  68. item['msg'] = check_match.group(2)
  69. item['type'] = 'check'
  70. else:
  71. message_indent = ' '
  72. print('patman: failed to parse checkpatch message:\n%s' %
  73. (message_indent + message.replace('\n', '\n' + message_indent)),
  74. file=sys.stderr)
  75. return {}
  76. file_match = RE_FILE.search(message)
  77. # some messages have no file, catch those here
  78. no_file_match = any(s in message for s in [
  79. '\nSubject:', 'Missing Signed-off-by: line(s)',
  80. 'does MAINTAINERS need updating'
  81. ])
  82. if file_match:
  83. err_fname = file_match.group(3)
  84. if err_fname:
  85. item['file'] = err_fname
  86. item['line'] = int(file_match.group(4))
  87. else:
  88. item['file'] = '<patch>'
  89. item['line'] = int(file_match.group(1))
  90. elif no_file_match:
  91. item['file'] = '<patch>'
  92. else:
  93. message_indent = ' '
  94. print('patman: failed to find file / line information:\n%s' %
  95. (message_indent + message.replace('\n', '\n' + message_indent)),
  96. file=sys.stderr)
  97. return item
  98. def CheckPatchParse(checkpatch_output, verbose=False):
  99. """Parse checkpatch.pl output
  100. Args:
  101. checkpatch_output: string to parse
  102. verbose: True to print out every line of the checkpatch output as it is
  103. parsed
  104. Returns:
  105. namedtuple containing:
  106. ok: False=failure, True=ok
  107. problems: List of problems, each a dict:
  108. 'type'; error or warning
  109. 'msg': text message
  110. 'file' : filename
  111. 'line': line number
  112. errors: Number of errors
  113. warnings: Number of warnings
  114. checks: Number of checks
  115. lines: Number of lines
  116. stdout: checkpatch_output
  117. """
  118. fields = ['ok', 'problems', 'errors', 'warnings', 'checks', 'lines',
  119. 'stdout']
  120. result = collections.namedtuple('CheckPatchResult', fields)
  121. result.stdout = checkpatch_output
  122. result.ok = False
  123. result.errors, result.warnings, result.checks = 0, 0, 0
  124. result.lines = 0
  125. result.problems = []
  126. # total: 0 errors, 0 warnings, 159 lines checked
  127. # or:
  128. # total: 0 errors, 2 warnings, 7 checks, 473 lines checked
  129. emacs_stats = r'(?:[0-9]{4}.*\.patch )?'
  130. re_stats = re.compile(emacs_stats +
  131. r'total: (\d+) errors, (\d+) warnings, (\d+)')
  132. re_stats_full = re.compile(emacs_stats +
  133. r'total: (\d+) errors, (\d+) warnings, (\d+)'
  134. r' checks, (\d+)')
  135. re_ok = re.compile(r'.*has no obvious style problems')
  136. re_bad = re.compile(r'.*has style problems, please review')
  137. # A blank line indicates the end of a message
  138. for message in result.stdout.split('\n\n'):
  139. if verbose:
  140. print(message)
  141. # either find stats, the verdict, or delegate
  142. match = re_stats_full.match(message)
  143. if not match:
  144. match = re_stats.match(message)
  145. if match:
  146. result.errors = int(match.group(1))
  147. result.warnings = int(match.group(2))
  148. if len(match.groups()) == 4:
  149. result.checks = int(match.group(3))
  150. result.lines = int(match.group(4))
  151. else:
  152. result.lines = int(match.group(3))
  153. elif re_ok.match(message):
  154. result.ok = True
  155. elif re_bad.match(message):
  156. result.ok = False
  157. else:
  158. problem = CheckPatchParseOneMessage(message)
  159. if problem:
  160. result.problems.append(problem)
  161. return result
  162. def CheckPatch(fname, verbose=False, show_types=False):
  163. """Run checkpatch.pl on a file and parse the results.
  164. Args:
  165. fname: Filename to check
  166. verbose: True to print out every line of the checkpatch output as it is
  167. parsed
  168. show_types: Tell checkpatch to show the type (number) of each message
  169. Returns:
  170. namedtuple containing:
  171. ok: False=failure, True=ok
  172. problems: List of problems, each a dict:
  173. 'type'; error or warning
  174. 'msg': text message
  175. 'file' : filename
  176. 'line': line number
  177. errors: Number of errors
  178. warnings: Number of warnings
  179. checks: Number of checks
  180. lines: Number of lines
  181. stdout: Full output of checkpatch
  182. """
  183. chk = FindCheckPatch()
  184. args = [chk, '--no-tree']
  185. if show_types:
  186. args.append('--show-types')
  187. output = command.Output(*args, fname, raise_on_error=False)
  188. return CheckPatchParse(output, verbose)
  189. def GetWarningMsg(col, msg_type, fname, line, msg):
  190. '''Create a message for a given file/line
  191. Args:
  192. msg_type: Message type ('error' or 'warning')
  193. fname: Filename which reports the problem
  194. line: Line number where it was noticed
  195. msg: Message to report
  196. '''
  197. if msg_type == 'warning':
  198. msg_type = col.Color(col.YELLOW, msg_type)
  199. elif msg_type == 'error':
  200. msg_type = col.Color(col.RED, msg_type)
  201. elif msg_type == 'check':
  202. msg_type = col.Color(col.MAGENTA, msg_type)
  203. line_str = '' if line is None else '%d' % line
  204. return '%s:%s: %s: %s\n' % (fname, line_str, msg_type, msg)
  205. def CheckPatches(verbose, args):
  206. '''Run the checkpatch.pl script on each patch'''
  207. error_count, warning_count, check_count = 0, 0, 0
  208. col = terminal.Color()
  209. for fname in args:
  210. result = CheckPatch(fname, verbose)
  211. if not result.ok:
  212. error_count += result.errors
  213. warning_count += result.warnings
  214. check_count += result.checks
  215. print('%d errors, %d warnings, %d checks for %s:' % (result.errors,
  216. result.warnings, result.checks, col.Color(col.BLUE, fname)))
  217. if (len(result.problems) != result.errors + result.warnings +
  218. result.checks):
  219. print("Internal error: some problems lost")
  220. for item in result.problems:
  221. sys.stderr.write(
  222. GetWarningMsg(col, item.get('type', '<unknown>'),
  223. item.get('file', '<unknown>'),
  224. item.get('line', 0), item.get('msg', 'message')))
  225. print
  226. #print(stdout)
  227. if error_count or warning_count or check_count:
  228. str = 'checkpatch.pl found %d error(s), %d warning(s), %d checks(s)'
  229. color = col.GREEN
  230. if warning_count:
  231. color = col.YELLOW
  232. if error_count:
  233. color = col.RED
  234. print(col.Color(color, str % (error_count, warning_count, check_count)))
  235. return False
  236. return True