2 # Copyright (c) 2014 ARM Limited
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.
14 # Copyright (c) 2006 The Regents of The University of Michigan
15 # Copyright (c) 2007,2011 The Hewlett-Packard Development Company
16 # Copyright (c) 2016 Advanced Micro Devices, Inc.
17 # All rights reserved.
19 # Redistribution and use in source and binary forms, with or without
20 # modification, are permitted provided that the following conditions are
21 # met: redistributions of source code must retain the above copyright
22 # notice, this list of conditions and the following disclaimer;
23 # redistributions in binary form must reproduce the above copyright
24 # notice, this list of conditions and the following disclaimer in the
25 # documentation and/or other materials provided with the distribution;
26 # neither the name of the copyright holders nor the names of its
27 # contributors may be used to endorse or promote products derived from
28 # this software without specific prior written permission.
30 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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.
42 # Authors: Nathan Binkert
50 from os
.path
import dirname
, join
as joinpath
51 from itertools
import count
52 from mercurial
import bdiff
, mdiff
, commands
54 current_dir
= dirname(__file__
)
55 sys
.path
.insert(0, current_dir
)
56 sys
.path
.insert(1, joinpath(dirname(current_dir
), 'src', 'python'))
58 from m5
.util
import neg_inf
, pos_inf
, Region
, Regions
60 from file_types
import lang_type
62 all_regions
= Regions(Region(neg_inf
, pos_inf
))
65 lead
= re
.compile(r
'^([ \t]+)')
66 trail
= re
.compile(r
'([ \t]+)$')
67 any_control
= re
.compile(r
'\b(if|while|for)([ \t]*)\(')
69 format_types
= set(('C', 'C++'))
73 """Helper function to create regular expression ignore file
76 rex
= re
.compile(expr
)
78 return rex
.match(fname
)
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.
87 # Ignore external projects as they are unlikely to follow the gem5
92 def check_ignores(fname
):
93 """Check if a file name matches any of the ignore rules"""
95 for rule
in style_ignores
:
102 def modified_regions(old_data
, new_data
):
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
)
111 def modregions(wctx
, fname
):
112 fctx
= wctx
.filectx(fname
)
113 pctx
= fctx
.parents()
115 file_data
= fctx
.data()
116 lines
= mdiff
.splitnewlines(file_data
)
117 if len(pctx
) in (1, 2):
118 mod_regions
= modified_regions(pctx
[0].data(), file_data
)
120 m2
= modified_regions(pctx
[1].data(), file_data
)
121 # only the lines that are new in both
124 mod_regions
= Regions()
125 mod_regions
.append(0, len(lines
))
129 class UserInterface(object):
130 def __init__(self
, verbose
=False):
131 self
.verbose
= verbose
133 def prompt(self
, prompt
, results
, default
):
135 result
= self
.do_prompt(prompt
, results
, default
)
136 if result
in results
:
139 class MercurialUI(UserInterface
):
140 def __init__(self
, ui
, *args
, **kwargs
):
141 super(MercurialUI
, self
).__init
__(*args
, **kwargs
)
144 def do_prompt(self
, prompt
, results
, default
):
145 return self
.ui
.prompt(prompt
, default
=default
)
147 def write(self
, string
):
148 self
.ui
.write(string
)
150 class StdioUI(UserInterface
):
151 def do_prompt(self
, prompt
, results
, default
):
152 return raw_input(prompt
) or default
154 def write(self
, string
):
155 sys
.stdout
.write(string
)
158 class Verifier(object):
159 """Base class for style verifier objects
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.)
169 def __init__(self
, ui
, repo
, opts
):
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)
183 def __getattr__(self
, attr
):
184 if attr
in ('prompt', 'write'):
185 return getattr(self
.ui
, attr
)
189 wctx
= repo
.workingctx()
191 from mercurial
import context
192 wctx
= context
.workingctx(repo
)
198 def open(self
, filename
, mode
):
199 filename
= self
.repo
.wjoin(filename
)
202 f
= file(filename
, mode
)
204 print 'could not open file %s: %s' % (filename
, msg
)
209 def skip(self
, filename
):
210 filename
= self
.repo
.wjoin(filename
)
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
217 if os
.path
.islink(filename
):
219 return lang_type(filename
) not in self
.languages
221 def check(self
, filename
, regions
=all_regions
):
222 """Check specified regions of file 'filename'.
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.
229 Returns a count of errors (0 if none), though actual non-zero
230 count value is not currently used anywhere.
233 f
= self
.open(filename
, 'r')
236 for num
,line
in enumerate(f
):
237 if num
not in regions
:
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))
244 self
.write(">>%s<<\n" % line
[:-1])
248 def fix(self
, filename
, regions
=all_regions
):
249 """Fix specified regions of file 'filename'.
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.
256 f
= self
.open(filename
, 'r+')
263 for i
,line
in enumerate(lines
):
265 line
= self
.fix_line(line
)
271 def apply(self
, filename
, regions
=all_regions
):
272 """Possibly apply to specified regions of file 'filename'.
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.
280 Returns True to abort, False otherwise.
282 if not (self
.opt_skip
or self
.skip(filename
)):
283 errors
= self
.check(filename
, regions
)
284 if errors
and not self
.opt_ignore
:
286 self
.fix(filename
, regions
)
288 result
= self
.ui
.prompt("(a)bort, (i)gnore, or (f)ix?",
291 self
.fix(filename
, regions
)
298 class Whitespace(Verifier
):
302 - No tabs used for indent
303 - No trailing whitespace
306 languages
= set(('C', 'C++', 'swig', 'python', 'asm', 'isa', 'scons'))
307 test_name
= 'whitespace'
310 def check_line(self
, line
):
311 match
= lead
.search(line
)
312 if match
and match
.group(1).find('\t') != -1:
315 match
= trail
.search(line
)
321 def fix_line(self
, line
):
322 if lead
.search(line
):
324 for i
,c
in enumerate(line
):
328 newline
+= ' ' * (tabsize
- len(newline
) % tabsize
)
335 return line
.rstrip() + '\n'
338 class ControlSpace(Verifier
):
339 """Check for exactly one space after if/while/for"""
341 languages
= set(('C', 'C++'))
342 test_name
= 'spacing after if/while/for'
345 def check_line(self
, line
):
346 match
= any_control
.search(line
)
347 return not (match
and match
.group(2) != " ")
349 def fix_line(self
, line
):
350 new_line
= any_control
.sub(r
'\1 (', line
)
354 class SortedIncludes(Verifier
):
355 """Check for proper sorting of include statements"""
357 languages
= sort_includes
.default_languages
358 test_name
= 'include file order'
361 def __init__(self
, *args
, **kwargs
):
362 super(SortedIncludes
, self
).__init
__(*args
, **kwargs
)
363 self
.sort_includes
= sort_includes
.SortIncludes()
365 def check(self
, filename
, regions
=all_regions
):
366 f
= self
.open(filename
, 'r')
368 lines
= [ l
.rstrip('\n') for l
in f
.xreadlines() ]
369 old
= ''.join(line
+ '\n' for line
in lines
)
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
)
379 mod
= modified_regions(old
, new
)
380 modified
= mod
& regions
383 self
.write("invalid sorting of includes in %s\n" % (filename
))
385 for start
, end
in modified
.regions
:
386 self
.write("bad region [%d, %d)\n" % (start
, end
))
391 def fix(self
, filename
, regions
=all_regions
):
392 f
= self
.open(filename
, 'r+')
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
)
403 for i
,line
in enumerate(sort_lines
):
410 tabs
= line
.count('\t')
417 count
+= tabsize
- count
% tabsize
423 class LineLength(Verifier
):
424 languages
= set(('C', 'C++', 'swig', 'python', 'asm', 'isa', 'scons'))
425 test_name
= 'line length'
428 def check_line(self
, line
):
429 return linelen(line
) <= 78
431 def fix(self
, filename
, regions
=all_regions
):
432 self
.write("Warning: cannot automatically fix overly long lines.\n")
435 class BoolCompare(Verifier
):
436 languages
= set(('C', 'C++', 'python'))
437 test_name
= 'boolean comparison'
438 opt_name
= 'boolcomp'
440 regex
= re
.compile(r
'\s*==\s*([Tt]rue|[Ff]alse)\b')
442 def check_line(self
, line
):
443 return self
.regex
.search(line
) == None
445 def fix_line(self
, line
):
446 match
= self
.regex
.search(line
)
448 if match
.group(1) in ('true', 'True'):
449 line
= self
.regex
.sub('', line
)
451 self
.write("Warning: cannot automatically fix "
452 "comparisons with false/False.\n")
456 # list of all verifier classes
465 class ValidationStats(object):
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
)
484 def __nonzero__(self
):
485 return self
.toolong
or self
.toolong80
or self
.leadtabs
or \
486 self
.trailwhite
or self
.badcontrol
or self
.cret
488 def validate(filename
, stats
, verbose
, exit_code
):
489 lang
= lang_type(filename
)
490 if lang
not in format_types
:
493 def msg(lineno
, line
, message
):
494 print '%s:%d>' % (filename
, lineno
+ 1), message
499 if exit_code
is not None:
503 f
= file(filename
, 'r')
506 print 'could not open file %s' % filename
510 for i
,line
in enumerate(f
):
511 line
= line
.rstrip('\n')
513 # no carriage returns
514 if line
.find('\r') != -1:
517 msg(i
, line
, 'carriage return found')
520 # lines max out at 79 chars
527 msg(i
, line
, 'line too long (%d chars)' % llen
)
530 # no tabs used to indent
531 match
= lead
.search(line
)
532 if match
and match
.group(1).find('\t') != -1:
535 msg(i
, line
, 'using tabs to indent')
538 # no trailing whitespace
539 if trail
.search(line
):
542 msg(i
, line
, 'trailing whitespace')
545 # for c++, exactly one space betwen if/while/for and (
547 match
= any_control
.search(line
)
548 if match
and match
.group(2) != " ":
549 stats
.badcontrol
+= 1
551 msg(i
, line
, 'improper spacing after %s' % match
.group(1))
555 def _modified_regions(repo
, patterns
, **kwargs
):
556 opt_all
= kwargs
.get('all', False)
557 opt_no_ignore
= kwargs
.get('no_ignore', False)
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.
563 from mercurial
import scmutil
564 m
= scmutil
.match(repo
[None], patterns
, kwargs
)
566 from mercurial
import cmdutil
567 m
= cmdutil
.match(repo
, patterns
, kwargs
)
569 modified
, added
, removed
, deleted
, unknown
, ignore
, clean
= \
570 repo
.status(match
=m
, clean
=opt_all
)
574 wctx
= repo
.workingctx()
576 from mercurial
import context
577 wctx
= context
.workingctx(repo
)
579 files
= [ (fn
, all_regions
) for fn
in added
] + \
580 [ (fn
, modregions(wctx
, fn
)) for fn
in modified
]
582 files
= [ (fn
, all_regions
) for fn
in added
+ modified
+ clean
]
584 for fname
, mod_regions
in files
:
585 if opt_no_ignore
or not check_ignores(fname
):
586 yield fname
, mod_regions
589 def do_check_style(hgui
, repo
, *pats
, **opts
):
590 """check files for proper m5 style guidelines
592 Without an argument, checks all modified and added files for gem5
593 coding style violations. A list of files can be specified to limit
594 the checker to a subset of the repository. The style rules are
595 normally applied on a diff of the repository state (i.e., added
596 files are checked in their entirety while only modifications of
597 modified files are checked).
599 The --all option can be specified to include clean files and check
600 modified files in their entirety.
602 The --fix-<check>, --ignore-<check>, and --skip-<check> options
603 can be used to control individual style checks:
605 --fix-<check> will perform the check and automatically attempt to
606 fix sny style error (printing a warning if unsuccessful)
608 --ignore-<check> will perform the check but ignore any errors
609 found (other than printing a message for each)
611 --skip-<check> will skip performing the check entirely
613 If none of these options are given, all checks will be performed
614 and the user will be prompted on how to handle each error.
616 --fix-all, --ignore-all, and --skip-all are equivalent to specifying
617 --fix-<check>, --ignore-<check>, or --skip-<check> for all checks,
618 respectively. However, option settings for specific checks take
619 precedence. Thus --skip-all --fix-white can be used to skip every
620 check other than whitespace errors, which will be checked and
623 The -v/--verbose flag will display the offending line(s) as well
627 ui
= MercurialUI(hgui
, verbose
=hgui
.verbose
)
629 # instantiate varifier objects
630 verifiers
= [v(ui
, repo
, opts
) for v
in all_verifiers
]
632 for fname
, mod_regions
in _modified_regions(repo
, pats
, **opts
):
633 for verifier
in verifiers
:
634 if verifier
.apply(fname
, mod_regions
):
639 def do_check_format(hgui
, repo
, *pats
, **opts
):
640 """check files for gem5 code formatting violations
642 Without an argument, checks all modified and added files for gem5
643 code formatting violations. A list of files can be specified to
644 limit the checker to a subset of the repository. The style rules
645 are normally applied on a diff of the repository state (i.e.,
646 added files are checked in their entirety while only modifications
647 of modified files are checked).
649 The --all option can be specified to include clean files and check
650 modified files in their entirety.
652 ui
= MercurialUI(hgui
, hgui
.verbose
)
655 for fname
, mod_regions
in _modified_regions(repo
, pats
, **opts
):
656 stats
= ValidationStats()
657 validate(joinpath(repo
.root
, fname
), stats
, verbose
, None)
661 result
= ui
.prompt("invalid formatting\n(i)gnore or (a)bort?",
668 def check_hook(hooktype
):
669 if hooktype
not in ('pretxncommit', 'pre-qrefresh'):
670 raise AttributeError, \
671 "This hook is not meant for %s" % hooktype
673 # This function provides a hook that is called before transaction
674 # commit and on qrefresh
675 def check_style(ui
, repo
, hooktype
, **kwargs
):
680 return do_check_style(ui
, repo
, **args
)
683 traceback
.print_exc()
686 def check_format(ui
, repo
, hooktype
, **kwargs
):
691 return do_check_format(ui
, repo
, **args
)
694 traceback
.print_exc()
698 from mercurial
.i18n
import _
703 _common_region_options
= [
705 _("include clean files and unmodified parts of modified files")),
706 ('', 'no-ignore', False, _("ignore the style ignore list")),
710 fix_opts
= [('f', 'fix-all', False, _("fix all style errors"))] + \
711 [('', 'fix-' + v
.opt_name
, False,
712 _('fix errors in ' + v
.test_name
)) for v
in all_verifiers
]
713 ignore_opts
= [('', 'ignore-all', False, _("ignore all style errors"))] + \
714 [('', 'ignore-' + v
.opt_name
, False,
715 _('ignore errors in ' + v
.test_name
)) for v
in all_verifiers
]
716 skip_opts
= [('', 'skip-all', False, _("skip all style error checks"))] + \
717 [('', 'skip-' + v
.opt_name
, False,
718 _('skip checking for ' + v
.test_name
)) for v
in all_verifiers
]
719 all_opts
= fix_opts
+ ignore_opts
+ skip_opts
724 do_check_style
, all_opts
+ _common_region_options
+ commands
.walkopts
,
725 _('hg m5style [-a] [FILE]...')),
728 ] + _common_region_options
+ commands
.walkopts
,
729 _('hg m5format [FILE]...')),
732 if __name__
== '__main__':
735 progname
= sys
.argv
[0]
736 if len(sys
.argv
) < 2:
737 sys
.exit('usage: %s <command> [<command args>]' % progname
)
739 fixwhite_usage
= '%s fixwhite [-t <tabsize> ] <path> [...] \n' % progname
740 chkformat_usage
= '%s chkformat <path> [...] \n' % progname
741 chkwhite_usage
= '%s chkwhite <path> [...] \n' % progname
743 command
= sys
.argv
[1]
744 if command
== 'fixwhite':
746 usage
= fixwhite_usage
747 elif command
== 'chkwhite':
749 usage
= chkwhite_usage
750 elif command
== 'chkformat':
752 usage
= chkformat_usage
754 sys
.exit(fixwhite_usage
+ chkwhite_usage
+ chkformat_usage
)
756 opts
, args
= getopt
.getopt(sys
.argv
[2:], flags
)
768 if command
== 'fixwhite':
769 for filename
in args
:
770 fixwhite(filename
, tabsize
)
771 elif command
== 'chkwhite':
772 for filename
in args
:
773 for line
,num
in checkwhite(filename
):
774 print 'invalid whitespace: %s:%d' % (filename
, num
)
776 print '>>%s<<' % line
[:-1]
777 elif command
== 'chkformat':
778 stats
= ValidationStats()
779 for filename
in args
:
780 validate(filename
, stats
=stats
, verbose
=verbose
, exit_code
=code
)
785 sys
.exit("command '%s' not found" % command
)