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
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):
@ -408,7 +409,7 @@ def validate(filename, stats, verbose, exit_code):
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
@ -417,6 +418,40 @@ def validate(filename, stats, verbose, exit_code):
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
@ -430,8 +465,6 @@ def do_check_style(hgui, repo, *pats, **opts):
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)
@ -440,8 +473,6 @@ def do_check_style(hgui, repo, *pats, **opts):
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):
@ -460,36 +491,9 @@ def do_check_style(hgui, repo, *pats, **opts):
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
@ -498,17 +502,27 @@ def do_check_style(hgui, repo, *pats, **opts):
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)
modified, added, removed, deleted, unknown, ignore, clean = repo.status()
verbose = 0
for fname, mod_regions in _modified_regions(repo, pats, **opts):
stats = ValidationStats()
for f in modified + added:
validate(joinpath(repo.root, f), stats, verbose, None)
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')
@ -550,20 +564,23 @@ except ImportError:
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]...')),
}