Deleted Added
sdiff udiff text old ( 10692:ab81a0feab55 ) new ( 11319:7ca84595249c )
full compact
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
9# licensed hereunder. You may use the software subject to the license
10# terms below provided that you ensure that this notice is replicated
11# unmodified and in its entirety in all distributions of the software,
12# modified or unmodified, in source code or in binary form.
13#
14# Copyright (c) 2006 The Regents of The University of Michigan
15# Copyright (c) 2007,2011 The Hewlett-Packard Development Company
16# All rights reserved.
17#
18# Redistribution and use in source and binary forms, with or without
19# modification, are permitted provided that the following conditions are
20# met: redistributions of source code must retain the above copyright
21# notice, this list of conditions and the following disclaimer;
22# redistributions in binary form must reproduce the above copyright
23# notice, this list of conditions and the following disclaimer in the

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

34# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40#
41# Authors: Nathan Binkert
42
43import heapq
44import os
45import re
46import sys
47
48from os.path import dirname, join as joinpath
49from itertools import count

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

57import sort_includes
58from file_types import lang_type
59
60all_regions = Regions(Region(neg_inf, pos_inf))
61
62tabsize = 8
63lead = re.compile(r'^([ \t]+)')
64trail = re.compile(r'([ \t]+)$')
65any_control = re.compile(r'\b(if|while|for)[ \t]*[(]')
66good_control = re.compile(r'\b(if|while|for) [(]')
67
68format_types = set(('C', 'C++'))
69
70
71def re_ignore(expr):
72 """Helper function to create regular expression ignore file
73 matcher functions"""
74

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

148
149class StdioUI(UserInterface):
150 def do_prompt(self, prompt, results, default):
151 return raw_input(prompt) or default
152
153 def write(self, string):
154 sys.stdout.write(string)
155
156class Verifier(object):
157 def __init__(self, ui, repo):
158 self.ui = ui
159 self.repo = repo
160
161 def __getattr__(self, attr):
162 if attr in ('prompt', 'write'):
163 return getattr(self.ui, attr)
164
165 if attr == 'wctx':
166 try:
167 wctx = repo.workingctx()

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

192 # the gem5 directory, it will be checked as a file, so symlink can be
193 # skipped. If the location is a file outside gem5, we don't want to
194 # check it anyway.
195 if os.path.islink(filename):
196 return True
197 return lang_type(filename) not in self.languages
198
199 def check(self, filename, regions=all_regions):
200 f = self.open(filename, 'r')
201
202 errors = 0
203 for num,line in enumerate(f):
204 if num not in regions:
205 continue
206 if not self.check_line(line):
207 self.write("invalid %s in %s:%d\n" % \
208 (self.test_name, filename, num + 1))
209 if self.ui.verbose:
210 self.write(">>%s<<\n" % line[-1])
211 errors += 1
212 return errors
213
214 def fix(self, filename, regions=all_regions):
215 f = self.open(filename, 'r+')
216
217 lines = list(f)
218
219 f.seek(0)
220 f.truncate()
221
222 for i,line in enumerate(lines):
223 if i in regions:
224 line = self.fix_line(line)
225
226 f.write(line)
227 f.close()
228
229 def apply(self, filename, prompt, regions=all_regions):
230 if not self.skip(filename):
231 errors = self.check(filename, regions)
232 if errors:
233 if prompt(filename, self.fix, regions):
234 return True
235 return False
236
237
238class Whitespace(Verifier):
239 languages = set(('C', 'C++', 'swig', 'python', 'asm', 'isa', 'scons'))
240 test_name = 'whitespace'
241 def check_line(self, line):
242 match = lead.search(line)
243 if match and match.group(1).find('\t') != -1:
244 return False
245
246 match = trail.search(line)
247 if match:
248 return False

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

260 else:
261 newline += line[i:]
262 break
263
264 line = newline
265
266 return line.rstrip() + '\n'
267
268class SortedIncludes(Verifier):
269 languages = sort_includes.default_languages
270 def __init__(self, *args, **kwargs):
271 super(SortedIncludes, self).__init__(*args, **kwargs)
272 self.sort_includes = sort_includes.SortIncludes()
273
274 def check(self, filename, regions=all_regions):
275 f = self.open(filename, 'r')
276
277 lines = [ l.rstrip('\n') for l in f.xreadlines() ]

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

309 f.seek(0)
310 f.truncate()
311
312 for i,line in enumerate(sort_lines):
313 f.write(line)
314 f.write('\n')
315 f.close()
316
317def linelen(line):
318 tabs = line.count('\t')
319 if not tabs:
320 return len(line)
321
322 count = 0
323 for c in line:
324 if c == '\t':

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

406 stats.trailwhite +=1
407 if verbose > 1:
408 msg(i, line, 'trailing whitespace')
409 bad()
410
411 # for c++, exactly one space betwen if/while/for and (
412 if lang == 'C++':
413 match = any_control.search(line)
414 if match and not good_control.search(line):
415 stats.badcontrol += 1
416 if verbose > 1:
417 msg(i, line, 'improper spacing after %s' % match.group(1))
418 bad()
419
420
421def _modified_regions(repo, patterns, **kwargs):
422 opt_all = kwargs.get('all', False)

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

459 coding style violations. A list of files can be specified to limit
460 the checker to a subset of the repository. The style rules are
461 normally applied on a diff of the repository state (i.e., added
462 files are checked in their entirety while only modifications of
463 modified files are checked).
464
465 The --all option can be specified to include clean files and check
466 modified files in their entirety.
467 """
468 opt_fix_all = opts.get('fix_all', False)
469 if not opt_fix_all:
470 opt_fix_white = opts.get('fix_white', False)
471 opt_fix_include = opts.get('fix_include', False)
472 else:
473 opt_fix_white = True
474 opt_fix_include = True
475
476 ui = MercurialUI(hgui, verbose=hgui.verbose)
477
478 def prompt(name, func, regions=all_regions):
479 result = ui.prompt("(a)bort, (i)gnore, or (f)ix?", 'aif', 'a')
480 if result == 'a':
481 return True
482 elif result == 'f':
483 func(name, regions)
484
485 return False
486
487 def no_prompt(name, func, regions=all_regions):
488 func(name, regions)
489 return False
490
491 prompt_white = prompt if not opt_fix_white else no_prompt
492 prompt_include = prompt if not opt_fix_include else no_prompt
493
494 whitespace = Whitespace(ui, repo)
495 sorted_includes = SortedIncludes(ui, repo)
496 for fname, mod_regions in _modified_regions(repo, pats, **opts):
497 if whitespace.apply(fname, prompt_white, mod_regions):
498 return True
499
500 if sorted_includes.apply(fname, prompt_include, mod_regions):
501 return True
502
503 return False
504
505def do_check_format(hgui, repo, *pats, **opts):
506 """check files for gem5 code formatting violations
507
508 Without an argument, checks all modified and added files for gem5
509 code formatting violations. A list of files can be specified to
510 limit the checker to a subset of the repository. The style rules

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

531
532 return False
533
534def check_hook(hooktype):
535 if hooktype not in ('pretxncommit', 'pre-qrefresh'):
536 raise AttributeError, \
537 "This hook is not meant for %s" % hooktype
538
539def check_style(ui, repo, hooktype, **kwargs):
540 check_hook(hooktype)
541 args = {}
542
543 try:
544 return do_check_style(ui, repo, **args)
545 except Exception, e:
546 import traceback

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

565 return arg
566
567_common_region_options = [
568 ('a', 'all', False,
569 _("include clean files and unmodified parts of modified files")),
570 ('', 'no-ignore', False, _("ignore the style ignore list")),
571 ]
572
573cmdtable = {
574 '^m5style' : (
575 do_check_style, [
576 ('f', 'fix-all', False, _("automatically fix style issues")),
577 ('', 'fix-white', False, _("automatically fix white space issues")),
578 ('', 'fix-include', False, _("automatically fix include ordering")),
579 ] + _common_region_options + commands.walkopts,
580 _('hg m5style [-a] [FILE]...')),
581 '^m5format' :
582 ( do_check_format, [
583 ] + _common_region_options + commands.walkopts,
584 _('hg m5format [FILE]...')),
585}
586
587if __name__ == '__main__':

--- 53 unchanged lines hidden ---