arm: use condition code registers for ARM ISA
[gem5.git] / util / chkformat
1 #!/usr/bin/env python
2 # Copyright (c) 2006 The Regents of The University of Michigan
3 # All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met: redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer;
9 # redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution;
12 # neither the name of the copyright holders nor the names of its
13 # contributors may be used to endorse or promote products derived from
14 # this software without specific prior written permission.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #
28 # Authors: Nathan Binkert
29
30 from getopt import getopt, GetoptError
31
32 import re
33 import sys
34
35 tabsize = 8
36
37 lead = re.compile(r'^([ \t])+')
38 trail = re.compile(r'[ \t]+$')
39 any_control = re.compile(r'\b(if|while|for)[ \t]*[(]')
40 good_control = re.compile(r'\b(if|while|for) [(]')
41
42 def linelen(line):
43 tabs = line.count('\t')
44 if not tabs:
45 return len(line)
46
47 count = 0
48 for c in line:
49 if c == '\t':
50 count += tabsize - count % tabsize
51 else:
52 count += 1
53
54 return count
55
56 toolong = 0
57 toolong80 = 0
58 leadtabs = 0
59 trailwhite = 0
60 badcontrol = 0
61 cret = 0
62
63 def validate(filename, verbose, code):
64 global toolong, toolong80, leadtabs, trailwhite, badcontrol, cret
65
66 def msg(lineno, line, message):
67 print '%s:%d>' % (filename, lineno + 1), message
68 if verbose > 2:
69 print line
70
71 def bad():
72 if code is not None:
73 sys.exit(code)
74
75 cpp = filename.endswith('.cc') or filename.endswith('.hh')
76 py = filename.endswith('.py')
77
78 if py + cpp != 1:
79 raise AttributeError, \
80 "I don't know how to deal with the file %s" % filename
81
82 try:
83 f = file(filename, 'r')
84 except OSError:
85 if verbose > 0:
86 print 'could not open file %s' % filename
87 bad()
88 return
89
90 for i,line in enumerate(f):
91 line = line.rstrip('\n')
92
93 # no carriage returns
94 if line.find('\r') != -1:
95 cret += 1
96 if verbose > 1:
97 msg(i, line, 'carriage return found')
98 bad()
99
100 # lines max out at 79 chars
101 llen = linelen(line)
102 if llen > 79:
103 toolong += 1
104 if llen == 80:
105 toolong80 += 1
106 if verbose > 1:
107 msg(i, line, 'line too long (%d chars)' % llen)
108 bad()
109
110 # no tabs used to indent
111 match = lead.search(line)
112 if match and match.group(1).find('\t') != -1:
113 leadtabs += 1
114 if verbose > 1:
115 msg(i, line, 'using tabs to indent')
116 bad()
117
118 # no trailing whitespace
119 if trail.search(line):
120 trailwhite +=1
121 if verbose > 1:
122 msg(i, line, 'trailing whitespace')
123 bad()
124
125 # for c++, exactly one space betwen if/while/for and (
126 if cpp:
127 match = any_control.search(line)
128 if match and not good_control.search(line):
129 badcontrol += 1
130 if verbose > 1:
131 msg(i, line, 'improper spacing after %s' % match.group(1))
132 bad()
133
134 if __name__ == '__main__':
135 progname = sys.argv[0]
136
137 def usage(code=None):
138 print >>sys.stderr, '''%s [-n] [-q] [-v] <filenames>''' % progname
139 if code is not None:
140 sys.exit(code)
141
142 try:
143 opts, args = getopt(sys.argv[1:], '-nv')
144 except GetoptError:
145 usage(2)
146
147 code = 1
148 verbose = 1
149 for opt,arg in opts:
150 if opt == '-n':
151 code = None
152 if opt == '-q':
153 verbose -= 1
154 if opt == '-v':
155 verbose += 1
156
157 for filename in args:
158 validate(filename, verbose=verbose, code=code)
159
160 if verbose > 0:
161 print '''\
162 %d violations of lines over 79 chars. %d of which are 80 chars exactly.
163 %d cases of whitespace at the end of a line.
164 %d cases of tabs to indent.
165 %d bad parens after if/while/for.
166 %d carriage returns found.
167 ''' % (toolong, toolong80, trailwhite, leadtabs, badcontrol, cret)
168