style.py (10691:65da28dee7cf) style.py (10692:ab81a0feab55)
1#! /usr/bin/env python
2# Copyright (c) 2014 ARM Limited
3# All rights reserved
4#
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

--- 338 unchanged lines hidden (view full) ---

347''' % (self.toolong, self.toolong80, self.trailwhite, self.leadtabs,
348 self.badcontrol, self.cret)
349
350 def __nonzero__(self):
351 return self.toolong or self.toolong80 or self.leadtabs or \
352 self.trailwhite or self.badcontrol or self.cret
353
354def validate(filename, stats, verbose, exit_code):
1#! /usr/bin/env python
2# Copyright (c) 2014 ARM Limited
3# All rights reserved
4#
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

--- 338 unchanged lines hidden (view full) ---

347''' % (self.toolong, self.toolong80, self.trailwhite, self.leadtabs,
348 self.badcontrol, self.cret)
349
350 def __nonzero__(self):
351 return self.toolong or self.toolong80 or self.leadtabs or \
352 self.trailwhite or self.badcontrol or self.cret
353
354def validate(filename, stats, verbose, exit_code):
355 if lang_type(filename) not in format_types:
355 lang = lang_type(filename)
356 if lang not in format_types:
356 return
357
358 def msg(lineno, line, message):
359 print '%s:%d>' % (filename, lineno + 1), message
360 if verbose > 2:
361 print line
362
363 def bad():

--- 39 unchanged lines hidden (view full) ---

403 # no trailing whitespace
404 if trail.search(line):
405 stats.trailwhite +=1
406 if verbose > 1:
407 msg(i, line, 'trailing whitespace')
408 bad()
409
410 # for c++, exactly one space betwen if/while/for and (
357 return
358
359 def msg(lineno, line, message):
360 print '%s:%d>' % (filename, lineno + 1), message
361 if verbose > 2:
362 print line
363
364 def bad():

--- 39 unchanged lines hidden (view full) ---

404 # no trailing whitespace
405 if trail.search(line):
406 stats.trailwhite +=1
407 if verbose > 1:
408 msg(i, line, 'trailing whitespace')
409 bad()
410
411 # for c++, exactly one space betwen if/while/for and (
411 if cpp:
412 if lang == 'C++':
412 match = any_control.search(line)
413 if match and not good_control.search(line):
414 stats.badcontrol += 1
415 if verbose > 1:
416 msg(i, line, 'improper spacing after %s' % match.group(1))
417 bad()
418
419
413 match = any_control.search(line)
414 if match and not good_control.search(line):
415 stats.badcontrol += 1
416 if verbose > 1:
417 msg(i, line, 'improper spacing after %s' % match.group(1))
418 bad()
419
420
421def _modified_regions(repo, patterns, **kwargs):
422 opt_all = kwargs.get('all', False)
423 opt_no_ignore = kwargs.get('no_ignore', False)
424
425 # Import the match (repository file name matching helper)
426 # function. Different versions of Mercurial keep it in different
427 # modules and implement them differently.
428 try:
429 from mercurial import scmutil
430 m = scmutil.match(repo[None], patterns, kwargs)
431 except ImportError:
432 from mercurial import cmdutil
433 m = cmdutil.match(repo, patterns, kwargs)
434
435 modified, added, removed, deleted, unknown, ignore, clean = \
436 repo.status(match=m, clean=opt_all)
437
438 if not opt_all:
439 try:
440 wctx = repo.workingctx()
441 except:
442 from mercurial import context
443 wctx = context.workingctx(repo)
444
445 files = [ (fn, all_regions) for fn in added ] + \
446 [ (fn, modregions(wctx, fn)) for fn in modified ]
447 else:
448 files = [ (fn, all_regions) for fn in added + modified + clean ]
449
450 for fname, mod_regions in files:
451 if opt_no_ignore or not check_ignores(fname):
452 yield fname, mod_regions
453
454
420def do_check_style(hgui, repo, *pats, **opts):
421 """check files for proper m5 style guidelines
422
423 Without an argument, checks all modified and added files for gem5
424 coding style violations. A list of files can be specified to limit
425 the checker to a subset of the repository. The style rules are
426 normally applied on a diff of the repository state (i.e., added
427 files are checked in their entirety while only modifications of
428 modified files are checked).
429
430 The --all option can be specified to include clean files and check
431 modified files in their entirety.
432 """
455def do_check_style(hgui, repo, *pats, **opts):
456 """check files for proper m5 style guidelines
457
458 Without an argument, checks all modified and added files for gem5
459 coding style violations. A list of files can be specified to limit
460 the checker to a subset of the repository. The style rules are
461 normally applied on a diff of the repository state (i.e., added
462 files are checked in their entirety while only modifications of
463 modified files are checked).
464
465 The --all option can be specified to include clean files and check
466 modified files in their entirety.
467 """
433 from mercurial import mdiff, util
434
435 opt_fix_all = opts.get('fix_all', False)
436 if not opt_fix_all:
437 opt_fix_white = opts.get('fix_white', False)
438 opt_fix_include = opts.get('fix_include', False)
439 else:
440 opt_fix_white = True
441 opt_fix_include = True
442
468 opt_fix_all = opts.get('fix_all', False)
469 if not opt_fix_all:
470 opt_fix_white = opts.get('fix_white', False)
471 opt_fix_include = opts.get('fix_include', False)
472 else:
473 opt_fix_white = True
474 opt_fix_include = True
475
443 opt_all = opts.get('all', False)
444 opt_no_ignore = opts.get('no_ignore', False)
445 ui = MercurialUI(hgui, verbose=hgui.verbose)
446
447 def prompt(name, func, regions=all_regions):
448 result = ui.prompt("(a)bort, (i)gnore, or (f)ix?", 'aif', 'a')
449 if result == 'a':
450 return True
451 elif result == 'f':
452 func(name, regions)
453
454 return False
455
456 def no_prompt(name, func, regions=all_regions):
457 func(name, regions)
458 return False
459
460 prompt_white = prompt if not opt_fix_white else no_prompt
461 prompt_include = prompt if not opt_fix_include else no_prompt
462
476 ui = MercurialUI(hgui, verbose=hgui.verbose)
477
478 def prompt(name, func, regions=all_regions):
479 result = ui.prompt("(a)bort, (i)gnore, or (f)ix?", 'aif', 'a')
480 if result == 'a':
481 return True
482 elif result == 'f':
483 func(name, regions)
484
485 return False
486
487 def no_prompt(name, func, regions=all_regions):
488 func(name, regions)
489 return False
490
491 prompt_white = prompt if not opt_fix_white else no_prompt
492 prompt_include = prompt if not opt_fix_include else no_prompt
493
463 # Import the match (repository file name matching helper)
464 # function. Different versions of Mercurial keep it in different
465 # modules and implement them differently.
466 try:
467 from mercurial import scmutil
468 m = scmutil.match(repo[None], pats, opts)
469 except ImportError:
470 from mercurial import cmdutil
471 m = cmdutil.match(repo, pats, opts)
472
473 modified, added, removed, deleted, unknown, ignore, clean = \
474 repo.status(match=m, clean=opt_all)
475 if not opt_all:
476 try:
477 wctx = repo.workingctx()
478 except:
479 from mercurial import context
480 wctx = context.workingctx(repo)
481
482 files = [ (fn, all_regions) for fn in added ] + \
483 [ (fn, modregions(wctx, fn)) for fn in modified ]
484 else:
485 files = [ (fn, all_regions) for fn in added + modified + clean ]
486
487 whitespace = Whitespace(ui, repo)
488 sorted_includes = SortedIncludes(ui, repo)
494 whitespace = Whitespace(ui, repo)
495 sorted_includes = SortedIncludes(ui, repo)
489 for fname, mod_regions in files:
490 if not opt_no_ignore and check_ignores(fname):
491 continue
492
496 for fname, mod_regions in _modified_regions(repo, pats, **opts):
493 if whitespace.apply(fname, prompt_white, mod_regions):
494 return True
495
496 if sorted_includes.apply(fname, prompt_include, mod_regions):
497 return True
498
499 return False
500
497 if whitespace.apply(fname, prompt_white, mod_regions):
498 return True
499
500 if sorted_includes.apply(fname, prompt_include, mod_regions):
501 return True
502
503 return False
504
501def do_check_format(hgui, repo, **args):
502 ui = MercurialUI(hgui, hgui.verbose)
505def do_check_format(hgui, repo, *pats, **opts):
506 """check files for gem5 code formatting violations
503
507
504 modified, added, removed, deleted, unknown, ignore, clean = repo.status()
508 Without an argument, checks all modified and added files for gem5
509 code formatting violations. A list of files can be specified to
510 limit the checker to a subset of the repository. The style rules
511 are normally applied on a diff of the repository state (i.e.,
512 added files are checked in their entirety while only modifications
513 of modified files are checked).
505
514
515 The --all option can be specified to include clean files and check
516 modified files in their entirety.
517 """
518 ui = MercurialUI(hgui, hgui.verbose)
519
506 verbose = 0
520 verbose = 0
507 stats = ValidationStats()
508 for f in modified + added:
509 validate(joinpath(repo.root, f), stats, verbose, None)
521 for fname, mod_regions in _modified_regions(repo, pats, **opts):
522 stats = ValidationStats()
523 validate(joinpath(repo.root, fname), stats, verbose, None)
524 if stats:
525 print "%s:" % fname
526 stats.dump()
527 result = ui.prompt("invalid formatting\n(i)gnore or (a)bort?",
528 'ai', 'a')
529 if result == 'a':
530 return True
510
531
511 if stats:
512 stats.dump()
513 result = ui.prompt("invalid formatting\n(i)gnore or (a)bort?",
514 'ai', 'a')
515 if result == 'a':
516 return True
517
518 return False
519
520def check_hook(hooktype):
521 if hooktype not in ('pretxncommit', 'pre-qrefresh'):
522 raise AttributeError, \
523 "This hook is not meant for %s" % hooktype
524
525def check_style(ui, repo, hooktype, **kwargs):

--- 19 unchanged lines hidden (view full) ---

545 return True
546
547try:
548 from mercurial.i18n import _
549except ImportError:
550 def _(arg):
551 return arg
552
532 return False
533
534def check_hook(hooktype):
535 if hooktype not in ('pretxncommit', 'pre-qrefresh'):
536 raise AttributeError, \
537 "This hook is not meant for %s" % hooktype
538
539def check_style(ui, repo, hooktype, **kwargs):

--- 19 unchanged lines hidden (view full) ---

559 return True
560
561try:
562 from mercurial.i18n import _
563except ImportError:
564 def _(arg):
565 return arg
566
567_common_region_options = [
568 ('a', 'all', False,
569 _("include clean files and unmodified parts of modified files")),
570 ('', 'no-ignore', False, _("ignore the style ignore list")),
571 ]
572
553cmdtable = {
554 '^m5style' : (
555 do_check_style, [
556 ('f', 'fix-all', False, _("automatically fix style issues")),
557 ('', 'fix-white', False, _("automatically fix white space issues")),
558 ('', 'fix-include', False, _("automatically fix include ordering")),
573cmdtable = {
574 '^m5style' : (
575 do_check_style, [
576 ('f', 'fix-all', False, _("automatically fix style issues")),
577 ('', 'fix-white', False, _("automatically fix white space issues")),
578 ('', 'fix-include', False, _("automatically fix include ordering")),
559 ('a', 'all', False,
560 _("include clean files and unmodified parts of modified files")),
561 ('', 'no-ignore', False, _("ignore the style ignore list")),
562 ] + commands.walkopts,
579 ] + _common_region_options + commands.walkopts,
563 _('hg m5style [-a] [FILE]...')),
564 '^m5format' :
580 _('hg m5style [-a] [FILE]...')),
581 '^m5format' :
565 ( do_check_format,
566 [ ],
582 ( do_check_format, [
583 ] + _common_region_options + commands.walkopts,
567 _('hg m5format [FILE]...')),
568}
569
570if __name__ == '__main__':
571 import getopt
572
573 progname = sys.argv[0]
574 if len(sys.argv) < 2:

--- 49 unchanged lines hidden ---
584 _('hg m5format [FILE]...')),
585}
586
587if __name__ == '__main__':
588 import getopt
589
590 progname = sys.argv[0]
591 if len(sys.argv) < 2:

--- 49 unchanged lines hidden ---