self.trailwhite or self.badcontrol or self.cret
def validate(filename, stats, verbose, exit_code):
- if lang_type(filename) not in format_types:
+ lang = lang_type(filename)
+ if lang not in format_types:
return
def msg(lineno, line, message):
bad()
# for c++, exactly one space betwen if/while/for and (
- if cpp:
+ if lang == 'C++':
match = any_control.search(line)
if match and not good_control.search(line):
stats.badcontrol += 1
bad()
+def _modified_regions(repo, patterns, **kwargs):
+ opt_all = kwargs.get('all', False)
+ opt_no_ignore = kwargs.get('no_ignore', False)
+
+ # Import the match (repository file name matching helper)
+ # function. Different versions of Mercurial keep it in different
+ # modules and implement them differently.
+ try:
+ from mercurial import scmutil
+ m = scmutil.match(repo[None], patterns, kwargs)
+ except ImportError:
+ from mercurial import cmdutil
+ m = cmdutil.match(repo, patterns, kwargs)
+
+ modified, added, removed, deleted, unknown, ignore, clean = \
+ repo.status(match=m, clean=opt_all)
+
+ if not opt_all:
+ try:
+ wctx = repo.workingctx()
+ except:
+ from mercurial import context
+ wctx = context.workingctx(repo)
+
+ files = [ (fn, all_regions) for fn in added ] + \
+ [ (fn, modregions(wctx, fn)) for fn in modified ]
+ else:
+ files = [ (fn, all_regions) for fn in added + modified + clean ]
+
+ for fname, mod_regions in files:
+ if opt_no_ignore or not check_ignores(fname):
+ yield fname, mod_regions
+
+
def do_check_style(hgui, repo, *pats, **opts):
"""check files for proper m5 style guidelines
The --all option can be specified to include clean files and check
modified files in their entirety.
"""
- from mercurial import mdiff, util
-
opt_fix_all = opts.get('fix_all', False)
if not opt_fix_all:
opt_fix_white = opts.get('fix_white', False)
opt_fix_white = True
opt_fix_include = True
- opt_all = opts.get('all', False)
- opt_no_ignore = opts.get('no_ignore', False)
ui = MercurialUI(hgui, verbose=hgui.verbose)
def prompt(name, func, regions=all_regions):
prompt_white = prompt if not opt_fix_white else no_prompt
prompt_include = prompt if not opt_fix_include else no_prompt
- # Import the match (repository file name matching helper)
- # function. Different versions of Mercurial keep it in different
- # modules and implement them differently.
- try:
- from mercurial import scmutil
- m = scmutil.match(repo[None], pats, opts)
- except ImportError:
- from mercurial import cmdutil
- m = cmdutil.match(repo, pats, opts)
-
- modified, added, removed, deleted, unknown, ignore, clean = \
- repo.status(match=m, clean=opt_all)
- if not opt_all:
- try:
- wctx = repo.workingctx()
- except:
- from mercurial import context
- wctx = context.workingctx(repo)
-
- files = [ (fn, all_regions) for fn in added ] + \
- [ (fn, modregions(wctx, fn)) for fn in modified ]
- else:
- files = [ (fn, all_regions) for fn in added + modified + clean ]
-
whitespace = Whitespace(ui, repo)
sorted_includes = SortedIncludes(ui, repo)
- for fname, mod_regions in files:
- if not opt_no_ignore and check_ignores(fname):
- continue
-
+ for fname, mod_regions in _modified_regions(repo, pats, **opts):
if whitespace.apply(fname, prompt_white, mod_regions):
return True
return False
-def do_check_format(hgui, repo, **args):
- ui = MercurialUI(hgui, hgui.verbose)
+def do_check_format(hgui, repo, *pats, **opts):
+ """check files for gem5 code formatting violations
+
+ Without an argument, checks all modified and added files for gem5
+ code formatting violations. A list of files can be specified to
+ limit the checker to a subset of the repository. The style rules
+ are normally applied on a diff of the repository state (i.e.,
+ added files are checked in their entirety while only modifications
+ of modified files are checked).
- modified, added, removed, deleted, unknown, ignore, clean = repo.status()
+ The --all option can be specified to include clean files and check
+ modified files in their entirety.
+ """
+ ui = MercurialUI(hgui, hgui.verbose)
verbose = 0
- stats = ValidationStats()
- for f in modified + added:
- validate(joinpath(repo.root, f), stats, verbose, None)
-
- if stats:
- stats.dump()
- result = ui.prompt("invalid formatting\n(i)gnore or (a)bort?",
- 'ai', 'a')
- if result == 'a':
- return True
+ for fname, mod_regions in _modified_regions(repo, pats, **opts):
+ stats = ValidationStats()
+ validate(joinpath(repo.root, fname), stats, verbose, None)
+ if stats:
+ print "%s:" % fname
+ stats.dump()
+ result = ui.prompt("invalid formatting\n(i)gnore or (a)bort?",
+ 'ai', 'a')
+ if result == 'a':
+ return True
return False
def _(arg):
return arg
+_common_region_options = [
+ ('a', 'all', False,
+ _("include clean files and unmodified parts of modified files")),
+ ('', 'no-ignore', False, _("ignore the style ignore list")),
+ ]
+
cmdtable = {
'^m5style' : (
do_check_style, [
('f', 'fix-all', False, _("automatically fix style issues")),
('', 'fix-white', False, _("automatically fix white space issues")),
('', 'fix-include', False, _("automatically fix include ordering")),
- ('a', 'all', False,
- _("include clean files and unmodified parts of modified files")),
- ('', 'no-ignore', False, _("ignore the style ignore list")),
- ] + commands.walkopts,
+ ] + _common_region_options + commands.walkopts,
_('hg m5style [-a] [FILE]...')),
'^m5format' :
- ( do_check_format,
- [ ],
+ ( do_check_format, [
+ ] + _common_region_options + commands.walkopts,
_('hg m5format [FILE]...')),
}