changeset ab81a0feab55 in /z/repo/gem5 details: http://repo.gem5.org/gem5?cmd=changeset;node=ab81a0feab55 description: 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. diffstat: util/style.py | 123 +++++++++++++++++++++++++++++++++------------------------ 1 files changed, 70 insertions(+), 53 deletions(-) diffs (195 lines): diff -r 65da28dee7cf -r ab81a0feab55 util/style.py --- a/util/style.py Wed Feb 11 10:23:33 2015 -0500 +++ b/util/style.py Wed Feb 11 10:23:34 2015 -0500 @@ -352,7 +352,8 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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,22 +502,32 @@ 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 - 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 @@ -550,20 +564,23 @@ 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]...')), } _______________________________________________ gem5-dev mailing list gem5-dev@gem5.org http://m5sim.org/mailman/listinfo/gem5-dev