hgstyle.py (11402:ac9e1a3bed79) hgstyle.py (11403:e8949ea6961f)
1#! /usr/bin/env python
2# Copyright (c) 2014 ARM Limited
3# All rights reserved
4#
5# The license below extends only to copyright in the software and shall
6# not be construed as granting a license to any other intellectual
7# property including but not limited to intellectual property relating
8# to a hardware implementation of the functionality of the software

--- 28 unchanged lines hidden (view full) ---

37# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41#
42# Authors: Nathan Binkert
43# Steve Reinhardt
44
1#! /usr/bin/env python
2# Copyright (c) 2014 ARM Limited
3# All rights reserved
4#
5# The license below extends only to copyright in the software and shall
6# not be construed as granting a license to any other intellectual
7# property including but not limited to intellectual property relating
8# to a hardware implementation of the functionality of the software

--- 28 unchanged lines hidden (view full) ---

37# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41#
42# Authors: Nathan Binkert
43# Steve Reinhardt
44
45import heapq
46import os
47import re
48import sys
45import sys
46import os
47from os.path import join as joinpath
49
48
50from os.path import dirname, join as joinpath
51from itertools import count
52from mercurial import bdiff, mdiff, commands
53
54current_dir = dirname(__file__)
49current_dir = os.path.dirname(__file__)
55sys.path.insert(0, current_dir)
50sys.path.insert(0, current_dir)
56sys.path.insert(1, joinpath(dirname(current_dir), 'src', 'python'))
57
51
58from m5.util import neg_inf, pos_inf, Region, Regions
59import sort_includes
60from file_types import lang_type
52from style.verifiers import all_verifiers
53from style.validators import all_validators
54from style.file_types import lang_type
55from style.style import MercurialUI, check_ignores
56from style.region import *
61
57
62all_regions = Regions(Region(neg_inf, pos_inf))
58from mercurial import bdiff, mdiff, commands
63
59
64tabsize = 8
65lead = re.compile(r'^([ \t]+)')
66trail = re.compile(r'([ \t]+)$')
67any_control = re.compile(r'\b(if|while|for)([ \t]*)\(')
68
69format_types = set(('C', 'C++'))
70
71
72def re_ignore(expr):
73 """Helper function to create regular expression ignore file
74 matcher functions"""
75
76 rex = re.compile(expr)
77 def match_re(fname):
78 return rex.match(fname)
79 return match_re
80
81# This list contains a list of functions that are called to determine
82# if a file should be excluded from the style matching rules or
83# not. The functions are called with the file name relative to the
84# repository root (without a leading slash) as their argument. A file
85# is excluded if any function in the list returns true.
86style_ignores = [
87 # Ignore external projects as they are unlikely to follow the gem5
88 # coding convention.
89 re_ignore("^ext/"),
90]
91
92def check_ignores(fname):
93 """Check if a file name matches any of the ignore rules"""
94
95 for rule in style_ignores:
96 if rule(fname):
97 return True
98
99 return False
100
101
102def modified_regions(old_data, new_data):
103 regions = Regions()
104 beg = None
105 for pbeg, pend, fbeg, fend in bdiff.blocks(old_data, new_data):
106 if beg is not None and beg != fbeg:
107 regions.append(beg, fbeg)
108 beg = fend
109 return regions

--- 11 unchanged lines hidden (view full) ---

121 # only the lines that are new in both
122 mod_regions &= m2
123 else:
124 mod_regions = Regions()
125 mod_regions.append(0, len(lines))
126
127 return mod_regions
128
60def modified_regions(old_data, new_data):
61 regions = Regions()
62 beg = None
63 for pbeg, pend, fbeg, fend in bdiff.blocks(old_data, new_data):
64 if beg is not None and beg != fbeg:
65 regions.append(beg, fbeg)
66 beg = fend
67 return regions

--- 11 unchanged lines hidden (view full) ---

79 # only the lines that are new in both
80 mod_regions &= m2
81 else:
82 mod_regions = Regions()
83 mod_regions.append(0, len(lines))
84
85 return mod_regions
86
129class UserInterface(object):
130 def __init__(self, verbose=False):
131 self.verbose = verbose
132
87
133 def prompt(self, prompt, results, default):
134 while True:
135 result = self.do_prompt(prompt, results, default)
136 if result in results:
137 return result
138
139class MercurialUI(UserInterface):
140 def __init__(self, ui, *args, **kwargs):
141 super(MercurialUI, self).__init__(*args, **kwargs)
142 self.ui = ui
143
144 def do_prompt(self, prompt, results, default):
145 return self.ui.prompt(prompt, default=default)
146
147 def write(self, string):
148 self.ui.write(string)
149
150class StdioUI(UserInterface):
151 def do_prompt(self, prompt, results, default):
152 return raw_input(prompt) or default
153
154 def write(self, string):
155 sys.stdout.write(string)
156
157
158class Verifier(object):
159 """Base class for style verifier objects
160
161 Subclasses must define these class attributes:
162 languages = set of strings identifying applicable languages
163 test_name = long descriptive name of test, will be used in
164 messages such as "error in <foo>" or "invalid <foo>"
165 opt_name = short name used to generate command-line options to
166 control the test (--fix-<foo>, --ignore-<foo>, etc.)
167 """
168
169 def __init__(self, ui, repo, opts):
170 self.ui = ui
171 self.repo = repo
172 # opt_name must be defined as a class attribute of derived classes.
173 # Check test-specific opts first as these have precedence.
174 self.opt_fix = opts.get('fix_' + self.opt_name, False)
175 self.opt_ignore = opts.get('ignore_' + self.opt_name, False)
176 self.opt_skip = opts.get('skip_' + self.opt_name, False)
177 # If no test-specific opts were set, then set based on "-all" opts.
178 if not (self.opt_fix or self.opt_ignore or self.opt_skip):
179 self.opt_fix = opts.get('fix_all', False)
180 self.opt_ignore = opts.get('ignore_all', False)
181 self.opt_skip = opts.get('skip_all', False)
182
183 def __getattr__(self, attr):
184 if attr in ('prompt', 'write'):
185 return getattr(self.ui, attr)
186
187 if attr == 'wctx':
188 try:
189 wctx = repo.workingctx()
190 except:
191 from mercurial import context
192 wctx = context.workingctx(repo)
193 self.wctx = wctx
194 return wctx
195
196 raise AttributeError
197
198 def open(self, filename, mode):
199 filename = self.repo.wjoin(filename)
200
201 try:
202 f = file(filename, mode)
203 except OSError, msg:
204 print 'could not open file %s: %s' % (filename, msg)
205 return None
206
207 return f
208
209 def skip(self, filename):
210 filename = self.repo.wjoin(filename)
211
212 # We never want to handle symlinks, so always skip them: If the location
213 # pointed to is a directory, skip it. If the location is a file inside
214 # the gem5 directory, it will be checked as a file, so symlink can be
215 # skipped. If the location is a file outside gem5, we don't want to
216 # check it anyway.
217 if os.path.islink(filename):
218 return True
219 return lang_type(filename) not in self.languages
220
221 def check(self, filename, regions=all_regions):
222 """Check specified regions of file 'filename'.
223
224 Line-by-line checks can simply provide a check_line() method
225 that returns True if the line is OK and False if it has an
226 error. Verifiers that need a multi-line view (like
227 SortedIncludes) must override this entire function.
228
229 Returns a count of errors (0 if none), though actual non-zero
230 count value is not currently used anywhere.
231 """
232
233 f = self.open(filename, 'r')
234
235 errors = 0
236 for num,line in enumerate(f):
237 if num not in regions:
238 continue
239 line = line.rstrip('\n')
240 if not self.check_line(line):
241 self.write("invalid %s in %s:%d\n" % \
242 (self.test_name, filename, num + 1))
243 if self.ui.verbose:
244 self.write(">>%s<<\n" % line[:-1])
245 errors += 1
246 return errors
247
248 def fix(self, filename, regions=all_regions):
249 """Fix specified regions of file 'filename'.
250
251 Line-by-line fixes can simply provide a fix_line() method that
252 returns the fixed line. Verifiers that need a multi-line view
253 (like SortedIncludes) must override this entire function.
254 """
255
256 f = self.open(filename, 'r+')
257
258 lines = list(f)
259
260 f.seek(0)
261 f.truncate()
262
263 for i,line in enumerate(lines):
264 if i in regions:
265 line = self.fix_line(line)
266
267 f.write(line)
268 f.close()
269
270
271 def apply(self, filename, regions=all_regions):
272 """Possibly apply to specified regions of file 'filename'.
273
274 Verifier is skipped if --skip-<test> option was provided or if
275 file is not of an applicable type. Otherwise file is checked
276 and error messages printed. Errors are fixed or ignored if
277 the corresponding --fix-<test> or --ignore-<test> options were
278 provided. If neither, the user is prompted for an action.
279
280 Returns True to abort, False otherwise.
281 """
282 if not (self.opt_skip or self.skip(filename)):
283 errors = self.check(filename, regions)
284 if errors and not self.opt_ignore:
285 if self.opt_fix:
286 self.fix(filename, regions)
287 else:
288 result = self.ui.prompt("(a)bort, (i)gnore, or (f)ix?",
289 'aif', 'a')
290 if result == 'f':
291 self.fix(filename, regions)
292 elif result == 'a':
293 return True # abort
294
295 return False
296
297
298class Whitespace(Verifier):
299 """Check whitespace.
300
301 Specifically:
302 - No tabs used for indent
303 - No trailing whitespace
304 """
305
306 languages = set(('C', 'C++', 'swig', 'python', 'asm', 'isa', 'scons'))
307 test_name = 'whitespace'
308 opt_name = 'white'
309
310 def check_line(self, line):
311 match = lead.search(line)
312 if match and match.group(1).find('\t') != -1:
313 return False
314
315 match = trail.search(line)
316 if match:
317 return False
318
319 return True
320
321 def fix_line(self, line):
322 if lead.search(line):
323 newline = ''
324 for i,c in enumerate(line):
325 if c == ' ':
326 newline += ' '
327 elif c == '\t':
328 newline += ' ' * (tabsize - len(newline) % tabsize)
329 else:
330 newline += line[i:]
331 break
332
333 line = newline
334
335 return line.rstrip() + '\n'
336
337
338class ControlSpace(Verifier):
339 """Check for exactly one space after if/while/for"""
340
341 languages = set(('C', 'C++'))
342 test_name = 'spacing after if/while/for'
343 opt_name = 'control'
344
345 def check_line(self, line):
346 match = any_control.search(line)
347 return not (match and match.group(2) != " ")
348
349 def fix_line(self, line):
350 new_line = any_control.sub(r'\1 (', line)
351 return new_line
352
353
354class SortedIncludes(Verifier):
355 """Check for proper sorting of include statements"""
356
357 languages = sort_includes.default_languages
358 test_name = 'include file order'
359 opt_name = 'include'
360
361 def __init__(self, *args, **kwargs):
362 super(SortedIncludes, self).__init__(*args, **kwargs)
363 self.sort_includes = sort_includes.SortIncludes()
364
365 def check(self, filename, regions=all_regions):
366 f = self.open(filename, 'r')
367
368 lines = [ l.rstrip('\n') for l in f.xreadlines() ]
369 old = ''.join(line + '\n' for line in lines)
370 f.close()
371
372 if len(lines) == 0:
373 return 0
374
375 language = lang_type(filename, lines[0])
376 sort_lines = list(self.sort_includes(lines, filename, language))
377 new = ''.join(line + '\n' for line in sort_lines)
378
379 mod = modified_regions(old, new)
380 modified = mod & regions
381
382 if modified:
383 self.write("invalid sorting of includes in %s\n" % (filename))
384 if self.ui.verbose:
385 for start, end in modified.regions:
386 self.write("bad region [%d, %d)\n" % (start, end))
387 return 1
388
389 return 0
390
391 def fix(self, filename, regions=all_regions):
392 f = self.open(filename, 'r+')
393
394 old = f.readlines()
395 lines = [ l.rstrip('\n') for l in old ]
396 language = lang_type(filename, lines[0])
397 sort_lines = list(self.sort_includes(lines, filename, language))
398 new = ''.join(line + '\n' for line in sort_lines)
399
400 f.seek(0)
401 f.truncate()
402
403 for i,line in enumerate(sort_lines):
404 f.write(line)
405 f.write('\n')
406 f.close()
407
408
409def linelen(line):
410 tabs = line.count('\t')
411 if not tabs:
412 return len(line)
413
414 count = 0
415 for c in line:
416 if c == '\t':
417 count += tabsize - count % tabsize
418 else:
419 count += 1
420
421 return count
422
423class LineLength(Verifier):
424 languages = set(('C', 'C++', 'swig', 'python', 'asm', 'isa', 'scons'))
425 test_name = 'line length'
426 opt_name = 'length'
427
428 def check_line(self, line):
429 return linelen(line) <= 78
430
431 def fix(self, filename, regions=all_regions):
432 self.write("Warning: cannot automatically fix overly long lines.\n")
433
434
435class BoolCompare(Verifier):
436 languages = set(('C', 'C++', 'python'))
437 test_name = 'boolean comparison'
438 opt_name = 'boolcomp'
439
440 regex = re.compile(r'\s*==\s*([Tt]rue|[Ff]alse)\b')
441
442 def check_line(self, line):
443 return self.regex.search(line) == None
444
445 def fix_line(self, line):
446 match = self.regex.search(line)
447 if match:
448 if match.group(1) in ('true', 'True'):
449 line = self.regex.sub('', line)
450 else:
451 self.write("Warning: cannot automatically fix "
452 "comparisons with false/False.\n")
453 return line
454
455
456# list of all verifier classes
457all_verifiers = [
458 Whitespace,
459 ControlSpace,
460 LineLength,
461 BoolCompare,
462 SortedIncludes
463]
464
465class ValidationStats(object):
466 def __init__(self):
467 self.toolong = 0
468 self.toolong80 = 0
469 self.leadtabs = 0
470 self.trailwhite = 0
471 self.badcontrol = 0
472 self.cret = 0
473
474 def dump(self):
475 print '''\
476%d violations of lines over 79 chars. %d of which are 80 chars exactly.
477%d cases of whitespace at the end of a line.
478%d cases of tabs to indent.
479%d bad parens after if/while/for.
480%d carriage returns found.
481''' % (self.toolong, self.toolong80, self.trailwhite, self.leadtabs,
482 self.badcontrol, self.cret)
483
484 def __nonzero__(self):
485 return self.toolong or self.toolong80 or self.leadtabs or \
486 self.trailwhite or self.badcontrol or self.cret
487
488def validate(filename, stats, verbose, exit_code):
88def validate(filename, verbose, exit_code):
489 lang = lang_type(filename)
89 lang = lang_type(filename)
490 if lang not in format_types:
90 if lang not in ('C', 'C++'):
491 return
492
91 return
92
493 def msg(lineno, line, message):
494 print '%s:%d>' % (filename, lineno + 1), message
495 if verbose > 2:
496 print line
497
498 def bad():
499 if exit_code is not None:
500 sys.exit(exit_code)
501
502 try:
503 f = file(filename, 'r')
504 except OSError:
505 if verbose > 0:
506 print 'could not open file %s' % filename
507 bad()
93 def bad():
94 if exit_code is not None:
95 sys.exit(exit_code)
96
97 try:
98 f = file(filename, 'r')
99 except OSError:
100 if verbose > 0:
101 print 'could not open file %s' % filename
102 bad()
508 return
103 return None
509
104
510 for i,line in enumerate(f):
105 vals = [ v(filename, verbose=(verbose > 1), language=lang)
106 for v in all_validators ]
107
108 for i, line in enumerate(f):
511 line = line.rstrip('\n')
109 line = line.rstrip('\n')
110 for v in vals:
111 v.validate_line(i, line)
512
112
513 # no carriage returns
514 if line.find('\r') != -1:
515 self.cret += 1
516 if verbose > 1:
517 msg(i, line, 'carriage return found')
518 bad()
519
113
520 # lines max out at 79 chars
521 llen = linelen(line)
522 if llen > 79:
523 stats.toolong += 1
524 if llen == 80:
525 stats.toolong80 += 1
526 if verbose > 1:
527 msg(i, line, 'line too long (%d chars)' % llen)
528 bad()
114 return vals
529
115
530 # no tabs used to indent
531 match = lead.search(line)
532 if match and match.group(1).find('\t') != -1:
533 stats.leadtabs += 1
534 if verbose > 1:
535 msg(i, line, 'using tabs to indent')
536 bad()
537
116
538 # no trailing whitespace
539 if trail.search(line):
540 stats.trailwhite +=1
541 if verbose > 1:
542 msg(i, line, 'trailing whitespace')
543 bad()
544
545 # for c++, exactly one space betwen if/while/for and (
546 if lang == 'C++':
547 match = any_control.search(line)
548 if match and match.group(2) != " ":
549 stats.badcontrol += 1
550 if verbose > 1:
551 msg(i, line, 'improper spacing after %s' % match.group(1))
552 bad()
553
554
555def _modified_regions(repo, patterns, **kwargs):
556 opt_all = kwargs.get('all', False)
557 opt_no_ignore = kwargs.get('no_ignore', False)
558
559 # Import the match (repository file name matching helper)
560 # function. Different versions of Mercurial keep it in different
561 # modules and implement them differently.
562 try:

--- 59 unchanged lines hidden (view full) ---

622
623 The -v/--verbose flag will display the offending line(s) as well
624 as their location.
625 """
626
627 ui = MercurialUI(hgui, verbose=hgui.verbose)
628
629 # instantiate varifier objects
117def _modified_regions(repo, patterns, **kwargs):
118 opt_all = kwargs.get('all', False)
119 opt_no_ignore = kwargs.get('no_ignore', False)
120
121 # Import the match (repository file name matching helper)
122 # function. Different versions of Mercurial keep it in different
123 # modules and implement them differently.
124 try:

--- 59 unchanged lines hidden (view full) ---

184
185 The -v/--verbose flag will display the offending line(s) as well
186 as their location.
187 """
188
189 ui = MercurialUI(hgui, verbose=hgui.verbose)
190
191 # instantiate varifier objects
630 verifiers = [v(ui, repo, opts) for v in all_verifiers]
192 verifiers = [v(ui, opts, base=repo.root) for v in all_verifiers]
631
632 for fname, mod_regions in _modified_regions(repo, pats, **opts):
633 for verifier in verifiers:
193
194 for fname, mod_regions in _modified_regions(repo, pats, **opts):
195 for verifier in verifiers:
634 if verifier.apply(fname, mod_regions):
196 if verifier.apply(joinpath(repo.root, fname), mod_regions):
635 return True
636
637 return False
638
639def do_check_format(hgui, repo, *pats, **opts):
640 """check files for gem5 code formatting violations
641
642 Without an argument, checks all modified and added files for gem5

--- 5 unchanged lines hidden (view full) ---

648
649 The --all option can be specified to include clean files and check
650 modified files in their entirety.
651 """
652 ui = MercurialUI(hgui, hgui.verbose)
653
654 verbose = 0
655 for fname, mod_regions in _modified_regions(repo, pats, **opts):
197 return True
198
199 return False
200
201def do_check_format(hgui, repo, *pats, **opts):
202 """check files for gem5 code formatting violations
203
204 Without an argument, checks all modified and added files for gem5

--- 5 unchanged lines hidden (view full) ---

210
211 The --all option can be specified to include clean files and check
212 modified files in their entirety.
213 """
214 ui = MercurialUI(hgui, hgui.verbose)
215
216 verbose = 0
217 for fname, mod_regions in _modified_regions(repo, pats, **opts):
656 stats = ValidationStats()
657 validate(joinpath(repo.root, fname), stats, verbose, None)
658 if stats:
218 vals = validate(joinpath(repo.root, fname), verbose, None)
219 if vals is None:
220 return True
221 elif any([not v for v in vals]):
659 print "%s:" % fname
222 print "%s:" % fname
660 stats.dump()
223 for v in vals:
224 v.dump()
661 result = ui.prompt("invalid formatting\n(i)gnore or (a)bort?",
662 'ai', 'a')
663 if result == 'a':
664 return True
665
666 return False
667
668def check_hook(hooktype):

--- 70 unchanged lines hidden (view full) ---

739 help="Produce verbose output")
740
741 parser.add_argument("file", metavar="FILE", nargs="+",
742 type=str,
743 help="Source file to inspect")
744
745 args = parser.parse_args()
746
225 result = ui.prompt("invalid formatting\n(i)gnore or (a)bort?",
226 'ai', 'a')
227 if result == 'a':
228 return True
229
230 return False
231
232def check_hook(hooktype):

--- 70 unchanged lines hidden (view full) ---

303 help="Produce verbose output")
304
305 parser.add_argument("file", metavar="FILE", nargs="+",
306 type=str,
307 help="Source file to inspect")
308
309 args = parser.parse_args()
310
747 stats = ValidationStats()
748 for filename in args.file:
311 for filename in args.file:
749 validate(filename, stats=stats, verbose=args.verbose, exit_code=1)
312 vals = validate(filename, verbose=args.verbose,
313 exit_code=1)
750
314
751 if args.verbose > 0:
752 stats.dump()
315 if args.verbose > 0 and vals is not None:
316 for v in vals:
317 v.dump()