xref: /OK3568_Linux_fs/u-boot/tools/patman/checkpatch.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun# Copyright (c) 2011 The Chromium OS Authors.
2*4882a593Smuzhiyun#
3*4882a593Smuzhiyun# SPDX-License-Identifier:	GPL-2.0+
4*4882a593Smuzhiyun#
5*4882a593Smuzhiyun
6*4882a593Smuzhiyunimport collections
7*4882a593Smuzhiyunimport command
8*4882a593Smuzhiyunimport gitutil
9*4882a593Smuzhiyunimport os
10*4882a593Smuzhiyunimport re
11*4882a593Smuzhiyunimport sys
12*4882a593Smuzhiyunimport terminal
13*4882a593Smuzhiyun
14*4882a593Smuzhiyundef FindCheckPatch():
15*4882a593Smuzhiyun    top_level = gitutil.GetTopLevel()
16*4882a593Smuzhiyun    try_list = [
17*4882a593Smuzhiyun        os.getcwd(),
18*4882a593Smuzhiyun        os.path.join(os.getcwd(), '..', '..'),
19*4882a593Smuzhiyun        os.path.join(top_level, 'tools'),
20*4882a593Smuzhiyun        os.path.join(top_level, 'scripts'),
21*4882a593Smuzhiyun        '%s/bin' % os.getenv('HOME'),
22*4882a593Smuzhiyun        ]
23*4882a593Smuzhiyun    # Look in current dir
24*4882a593Smuzhiyun    for path in try_list:
25*4882a593Smuzhiyun        fname = os.path.join(path, 'checkpatch.pl')
26*4882a593Smuzhiyun        if os.path.isfile(fname):
27*4882a593Smuzhiyun            return fname
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun    # Look upwwards for a Chrome OS tree
30*4882a593Smuzhiyun    while not os.path.ismount(path):
31*4882a593Smuzhiyun        fname = os.path.join(path, 'src', 'third_party', 'kernel', 'files',
32*4882a593Smuzhiyun                'scripts', 'checkpatch.pl')
33*4882a593Smuzhiyun        if os.path.isfile(fname):
34*4882a593Smuzhiyun            return fname
35*4882a593Smuzhiyun        path = os.path.dirname(path)
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun    sys.exit('Cannot find checkpatch.pl - please put it in your ' +
38*4882a593Smuzhiyun             '~/bin directory or use --no-check')
39*4882a593Smuzhiyun
40*4882a593Smuzhiyundef CheckPatch(fname, verbose=False):
41*4882a593Smuzhiyun    """Run checkpatch.pl on a file.
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun    Returns:
44*4882a593Smuzhiyun        namedtuple containing:
45*4882a593Smuzhiyun            ok: False=failure, True=ok
46*4882a593Smuzhiyun            problems: List of problems, each a dict:
47*4882a593Smuzhiyun                'type'; error or warning
48*4882a593Smuzhiyun                'msg': text message
49*4882a593Smuzhiyun                'file' : filename
50*4882a593Smuzhiyun                'line': line number
51*4882a593Smuzhiyun            errors: Number of errors
52*4882a593Smuzhiyun            warnings: Number of warnings
53*4882a593Smuzhiyun            checks: Number of checks
54*4882a593Smuzhiyun            lines: Number of lines
55*4882a593Smuzhiyun            stdout: Full output of checkpatch
56*4882a593Smuzhiyun    """
57*4882a593Smuzhiyun    fields = ['ok', 'problems', 'errors', 'warnings', 'checks', 'lines',
58*4882a593Smuzhiyun              'stdout']
59*4882a593Smuzhiyun    result = collections.namedtuple('CheckPatchResult', fields)
60*4882a593Smuzhiyun    result.ok = False
61*4882a593Smuzhiyun    result.errors, result.warning, result.checks = 0, 0, 0
62*4882a593Smuzhiyun    result.lines = 0
63*4882a593Smuzhiyun    result.problems = []
64*4882a593Smuzhiyun    chk = FindCheckPatch()
65*4882a593Smuzhiyun    item = {}
66*4882a593Smuzhiyun    result.stdout = command.Output(chk, '--no-tree', fname,
67*4882a593Smuzhiyun                                   raise_on_error=False)
68*4882a593Smuzhiyun    #pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE)
69*4882a593Smuzhiyun    #stdout, stderr = pipe.communicate()
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun    # total: 0 errors, 0 warnings, 159 lines checked
72*4882a593Smuzhiyun    # or:
73*4882a593Smuzhiyun    # total: 0 errors, 2 warnings, 7 checks, 473 lines checked
74*4882a593Smuzhiyun    re_stats = re.compile('total: (\\d+) errors, (\d+) warnings, (\d+)')
75*4882a593Smuzhiyun    re_stats_full = re.compile('total: (\\d+) errors, (\d+) warnings, (\d+)'
76*4882a593Smuzhiyun                               ' checks, (\d+)')
77*4882a593Smuzhiyun    re_ok = re.compile('.*has no obvious style problems')
78*4882a593Smuzhiyun    re_bad = re.compile('.*has style problems, please review')
79*4882a593Smuzhiyun    re_error = re.compile('ERROR: (.*)')
80*4882a593Smuzhiyun    re_warning = re.compile('WARNING: (.*)')
81*4882a593Smuzhiyun    re_check = re.compile('CHECK: (.*)')
82*4882a593Smuzhiyun    re_file = re.compile('#\d+: FILE: ([^:]*):(\d+):')
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun    for line in result.stdout.splitlines():
85*4882a593Smuzhiyun        if verbose:
86*4882a593Smuzhiyun            print(line)
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun        # A blank line indicates the end of a message
89*4882a593Smuzhiyun        if not line and item:
90*4882a593Smuzhiyun            result.problems.append(item)
91*4882a593Smuzhiyun            item = {}
92*4882a593Smuzhiyun        match = re_stats_full.match(line)
93*4882a593Smuzhiyun        if not match:
94*4882a593Smuzhiyun            match = re_stats.match(line)
95*4882a593Smuzhiyun        if match:
96*4882a593Smuzhiyun            result.errors = int(match.group(1))
97*4882a593Smuzhiyun            result.warnings = int(match.group(2))
98*4882a593Smuzhiyun            if len(match.groups()) == 4:
99*4882a593Smuzhiyun                result.checks = int(match.group(3))
100*4882a593Smuzhiyun                result.lines = int(match.group(4))
101*4882a593Smuzhiyun            else:
102*4882a593Smuzhiyun                result.lines = int(match.group(3))
103*4882a593Smuzhiyun        elif re_ok.match(line):
104*4882a593Smuzhiyun            result.ok = True
105*4882a593Smuzhiyun        elif re_bad.match(line):
106*4882a593Smuzhiyun            result.ok = False
107*4882a593Smuzhiyun        err_match = re_error.match(line)
108*4882a593Smuzhiyun        warn_match = re_warning.match(line)
109*4882a593Smuzhiyun        file_match = re_file.match(line)
110*4882a593Smuzhiyun        check_match = re_check.match(line)
111*4882a593Smuzhiyun        if err_match:
112*4882a593Smuzhiyun            item['msg'] = err_match.group(1)
113*4882a593Smuzhiyun            item['type'] = 'error'
114*4882a593Smuzhiyun        elif warn_match:
115*4882a593Smuzhiyun            item['msg'] = warn_match.group(1)
116*4882a593Smuzhiyun            item['type'] = 'warning'
117*4882a593Smuzhiyun        elif check_match:
118*4882a593Smuzhiyun            item['msg'] = check_match.group(1)
119*4882a593Smuzhiyun            item['type'] = 'check'
120*4882a593Smuzhiyun        elif file_match:
121*4882a593Smuzhiyun            item['file'] = file_match.group(1)
122*4882a593Smuzhiyun            item['line'] = int(file_match.group(2))
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun    return result
125*4882a593Smuzhiyun
126*4882a593Smuzhiyundef GetWarningMsg(col, msg_type, fname, line, msg):
127*4882a593Smuzhiyun    '''Create a message for a given file/line
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun    Args:
130*4882a593Smuzhiyun        msg_type: Message type ('error' or 'warning')
131*4882a593Smuzhiyun        fname: Filename which reports the problem
132*4882a593Smuzhiyun        line: Line number where it was noticed
133*4882a593Smuzhiyun        msg: Message to report
134*4882a593Smuzhiyun    '''
135*4882a593Smuzhiyun    if msg_type == 'warning':
136*4882a593Smuzhiyun        msg_type = col.Color(col.YELLOW, msg_type)
137*4882a593Smuzhiyun    elif msg_type == 'error':
138*4882a593Smuzhiyun        msg_type = col.Color(col.RED, msg_type)
139*4882a593Smuzhiyun    elif msg_type == 'check':
140*4882a593Smuzhiyun        msg_type = col.Color(col.MAGENTA, msg_type)
141*4882a593Smuzhiyun    return '%s:%d: %s: %s\n' % (fname, line, msg_type, msg)
142*4882a593Smuzhiyun
143*4882a593Smuzhiyundef CheckPatches(verbose, args):
144*4882a593Smuzhiyun    '''Run the checkpatch.pl script on each patch'''
145*4882a593Smuzhiyun    error_count, warning_count, check_count = 0, 0, 0
146*4882a593Smuzhiyun    col = terminal.Color()
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun    for fname in args:
149*4882a593Smuzhiyun        result = CheckPatch(fname, verbose)
150*4882a593Smuzhiyun        if not result.ok:
151*4882a593Smuzhiyun            error_count += result.errors
152*4882a593Smuzhiyun            warning_count += result.warnings
153*4882a593Smuzhiyun            check_count += result.checks
154*4882a593Smuzhiyun            print('%d errors, %d warnings, %d checks for %s:' % (result.errors,
155*4882a593Smuzhiyun                    result.warnings, result.checks, col.Color(col.BLUE, fname)))
156*4882a593Smuzhiyun            if (len(result.problems) != result.errors + result.warnings +
157*4882a593Smuzhiyun                    result.checks):
158*4882a593Smuzhiyun                print("Internal error: some problems lost")
159*4882a593Smuzhiyun            for item in result.problems:
160*4882a593Smuzhiyun                sys.stderr.write(
161*4882a593Smuzhiyun                    GetWarningMsg(col, item.get('type', '<unknown>'),
162*4882a593Smuzhiyun                        item.get('file', '<unknown>'),
163*4882a593Smuzhiyun                        item.get('line', 0), item.get('msg', 'message')))
164*4882a593Smuzhiyun            print
165*4882a593Smuzhiyun            #print(stdout)
166*4882a593Smuzhiyun    if error_count or warning_count or check_count:
167*4882a593Smuzhiyun        str = 'checkpatch.pl found %d error(s), %d warning(s), %d checks(s)'
168*4882a593Smuzhiyun        color = col.GREEN
169*4882a593Smuzhiyun        if warning_count:
170*4882a593Smuzhiyun            color = col.YELLOW
171*4882a593Smuzhiyun        if error_count:
172*4882a593Smuzhiyun            color = col.RED
173*4882a593Smuzhiyun        print(col.Color(color, str % (error_count, warning_count, check_count)))
174*4882a593Smuzhiyun        return False
175*4882a593Smuzhiyun    return True
176