style: Fix broken m5format command

The m5format command didn't actually work due to parameter handling
issues and missing language detection. This changeset fixes those
issues and cleans up some of the code to shared between the style
checker and the format checker.
This commit is contained in:
Andreas Sandberg 2015-02-11 10:23:34 -05:00
parent 267443fa22
commit 5a573762d0

View file

@ -352,7 +352,8 @@ class ValidationStats(object):
self.trailwhite or self.badcontrol or self.cret self.trailwhite or self.badcontrol or self.cret
def validate(filename, stats, verbose, exit_code): 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 return
def msg(lineno, line, message): def msg(lineno, line, message):
@ -408,7 +409,7 @@ def validate(filename, stats, verbose, exit_code):
bad() bad()
# for c++, exactly one space betwen if/while/for and ( # for c++, exactly one space betwen if/while/for and (
if cpp: if lang == 'C++':
match = any_control.search(line) match = any_control.search(line)
if match and not good_control.search(line): if match and not good_control.search(line):
stats.badcontrol += 1 stats.badcontrol += 1
@ -417,6 +418,40 @@ def validate(filename, stats, verbose, exit_code):
bad() 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): def do_check_style(hgui, repo, *pats, **opts):
"""check files for proper m5 style guidelines """check files for proper m5 style guidelines
@ -430,8 +465,6 @@ def do_check_style(hgui, repo, *pats, **opts):
The --all option can be specified to include clean files and check The --all option can be specified to include clean files and check
modified files in their entirety. modified files in their entirety.
""" """
from mercurial import mdiff, util
opt_fix_all = opts.get('fix_all', False) opt_fix_all = opts.get('fix_all', False)
if not opt_fix_all: if not opt_fix_all:
opt_fix_white = opts.get('fix_white', False) opt_fix_white = opts.get('fix_white', False)
@ -440,8 +473,6 @@ def do_check_style(hgui, repo, *pats, **opts):
opt_fix_white = True opt_fix_white = True
opt_fix_include = 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) ui = MercurialUI(hgui, verbose=hgui.verbose)
def prompt(name, func, regions=all_regions): def prompt(name, func, regions=all_regions):
@ -460,36 +491,9 @@ def do_check_style(hgui, repo, *pats, **opts):
prompt_white = prompt if not opt_fix_white else no_prompt prompt_white = prompt if not opt_fix_white else no_prompt
prompt_include = prompt if not opt_fix_include 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) whitespace = Whitespace(ui, repo)
sorted_includes = SortedIncludes(ui, repo) sorted_includes = SortedIncludes(ui, repo)
for fname, mod_regions in files: for fname, mod_regions in _modified_regions(repo, pats, **opts):
if not opt_no_ignore and check_ignores(fname):
continue
if whitespace.apply(fname, prompt_white, mod_regions): if whitespace.apply(fname, prompt_white, mod_regions):
return True return True
@ -498,22 +502,32 @@ def do_check_style(hgui, repo, *pats, **opts):
return False return False
def do_check_format(hgui, repo, **args): 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).
The --all option can be specified to include clean files and check
modified files in their entirety.
"""
ui = MercurialUI(hgui, hgui.verbose) ui = MercurialUI(hgui, hgui.verbose)
modified, added, removed, deleted, unknown, ignore, clean = repo.status()
verbose = 0 verbose = 0
stats = ValidationStats() for fname, mod_regions in _modified_regions(repo, pats, **opts):
for f in modified + added: stats = ValidationStats()
validate(joinpath(repo.root, f), stats, verbose, None) validate(joinpath(repo.root, fname), stats, verbose, None)
if stats:
if stats: print "%s:" % fname
stats.dump() stats.dump()
result = ui.prompt("invalid formatting\n(i)gnore or (a)bort?", result = ui.prompt("invalid formatting\n(i)gnore or (a)bort?",
'ai', 'a') 'ai', 'a')
if result == 'a': if result == 'a':
return True return True
return False return False
@ -550,20 +564,23 @@ except ImportError:
def _(arg): def _(arg):
return 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 = { cmdtable = {
'^m5style' : ( '^m5style' : (
do_check_style, [ do_check_style, [
('f', 'fix-all', False, _("automatically fix style issues")), ('f', 'fix-all', False, _("automatically fix style issues")),
('', 'fix-white', False, _("automatically fix white space issues")), ('', 'fix-white', False, _("automatically fix white space issues")),
('', 'fix-include', False, _("automatically fix include ordering")), ('', 'fix-include', False, _("automatically fix include ordering")),
('a', 'all', False, ] + _common_region_options + commands.walkopts,
_("include clean files and unmodified parts of modified files")),
('', 'no-ignore', False, _("ignore the style ignore list")),
] + commands.walkopts,
_('hg m5style [-a] [FILE]...')), _('hg m5style [-a] [FILE]...')),
'^m5format' : '^m5format' :
( do_check_format, ( do_check_format, [
[ ], ] + _common_region_options + commands.walkopts,
_('hg m5format [FILE]...')), _('hg m5format [FILE]...')),
} }