style.py revision 13540:da30e62884ee
12SN/A#! /usr/bin/env python2.7 212109SRekai.GonzalezAlberquilla@arm.com# 39920Syasuko.eckert@amd.com# Copyright (c) 2016 ARM Limited 48733Sgeoffrey.blake@arm.com# All rights reserved 58733Sgeoffrey.blake@arm.com# 68733Sgeoffrey.blake@arm.com# The license below extends only to copyright in the software and shall 78733Sgeoffrey.blake@arm.com# not be construed as granting a license to any other intellectual 88733Sgeoffrey.blake@arm.com# property including but not limited to intellectual property relating 98733Sgeoffrey.blake@arm.com# to a hardware implementation of the functionality of the software 108733Sgeoffrey.blake@arm.com# licensed hereunder. You may use the software subject to the license 118733Sgeoffrey.blake@arm.com# terms below provided that you ensure that this notice is replicated 128733Sgeoffrey.blake@arm.com# unmodified and in its entirety in all distributions of the software, 138733Sgeoffrey.blake@arm.com# modified or unmodified, in source code or in binary form. 148733Sgeoffrey.blake@arm.com# 152190SN/A# Redistribution and use in source and binary forms, with or without 162SN/A# modification, are permitted provided that the following conditions are 172SN/A# met: redistributions of source code must retain the above copyright 182SN/A# notice, this list of conditions and the following disclaimer; 192SN/A# redistributions in binary form must reproduce the above copyright 202SN/A# notice, this list of conditions and the following disclaimer in the 212SN/A# documentation and/or other materials provided with the distribution; 222SN/A# neither the name of the copyright holders nor the names of its 232SN/A# contributors may be used to endorse or promote products derived from 242SN/A# this software without specific prior written permission. 252SN/A# 262SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 272SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 282SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 292SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 302SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 312SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 322SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 332SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 342SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 352SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 362SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 372SN/A# 382SN/A# Authors: Andreas Sandberg 392SN/A 402665SN/Aimport os 412665SN/Aimport sys 422SN/A 432SN/Afrom style.file_types import lang_type 442680Sktlim@umich.eduimport style.verifiers 452680Sktlim@umich.edufrom style.region import all_regions 462SN/A 478229Snate@binkert.orgfrom style.style import StdioUI 487680Sgblack@eecs.umich.edufrom style import repo 497680Sgblack@eecs.umich.edu 506329Sgblack@eecs.umich.eduverifier_names = dict([ 513453Sgblack@eecs.umich.edu (c.__name__, c) for c in style.verifiers.all_verifiers ]) 526216Snate@binkert.org 536658Snate@binkert.orgdef verify(filename, regions=all_regions, verbose=False, verifiers=None, 5412104Snathanael.premillieu@arm.com auto_fix=False): 552SN/A ui = StdioUI() 562190SN/A opts = { 572190SN/A "fix_all" : auto_fix, 583453Sgblack@eecs.umich.edu } 593453Sgblack@eecs.umich.edu base = os.path.join(os.path.dirname(__file__), "..") 609020Sgblack@eecs.umich.edu if verifiers is None: 613453Sgblack@eecs.umich.edu verifiers = style.verifiers.all_verifiers 622190SN/A 6312406Sgabeblack@google.com if verbose: 648887Sgeoffrey.blake@arm.com print "Verifying %s[%s]..." % (filename, regions) 657680Sgblack@eecs.umich.edu for verifier in [ v(ui, opts, base=base) for v in verifiers ]: 662313SN/A if verbose: 678706Sandreas.hansson@arm.com print "Applying %s (%s)" % ( 688706Sandreas.hansson@arm.com verifier.test_name, verifier.__class__.__name__) 698706Sandreas.hansson@arm.com if verifier.apply(filename, regions=regions): 702190SN/A return False 712190SN/A return True 723548Sgblack@eecs.umich.edu 733548Sgblack@eecs.umich.edudef detect_repo(): 743548Sgblack@eecs.umich.edu repo_classes = repo.detect_repo() 758902Sandreas.hansson@arm.com if not repo_classes: 768902Sandreas.hansson@arm.com print >> sys.stderr, "Error: Failed to detect repository type, no " \ 772SN/A "known repository type found." 782680Sktlim@umich.edu sys.exit(1) 792680Sktlim@umich.edu elif len(repo_classes) > 1: 802680Sktlim@umich.edu print >> sys.stderr, "Error: Detected multiple repository types." 812680Sktlim@umich.edu sys.exit(1) 822680Sktlim@umich.edu else: 832680Sktlim@umich.edu return repo_classes[0]() 842680Sktlim@umich.edu 852680Sktlim@umich.edurepo_types = { 862680Sktlim@umich.edu "auto" : detect_repo, 872680Sktlim@umich.edu "none" : lambda : None, 882680Sktlim@umich.edu "git" : repo.GitRepo, 892682Sktlim@umich.edu "hg" : repo.MercurialRepo, 902680Sktlim@umich.edu} 912680Sktlim@umich.edu 922680Sktlim@umich.eduif __name__ == '__main__': 932680Sktlim@umich.edu import argparse 942680Sktlim@umich.edu 952SN/A parser = argparse.ArgumentParser( 962107SN/A description="Check a file for gem5 style violations", 972107SN/A epilog="""If no files are specified, the style checker tries to 989920Syasuko.eckert@amd.com determine the list of modified and added files from the version 9912109SRekai.GonzalezAlberquilla@arm.com control system and checks those.""" 10012109SRekai.GonzalezAlberquilla@arm.com ) 1012SN/A 1026029Ssteve.reinhardt@amd.com parser.add_argument("--verbose", "-v", action="count", 103246SN/A help="Produce verbose output") 104246SN/A 105246SN/A parser.add_argument("--fix", "-f", action="store_true", 106246SN/A help="Automatically fix style violations.") 107246SN/A 108246SN/A parser.add_argument("--modifications", "-m", action="store_true", 109246SN/A help="""Apply the style checker to modified regions 1102190SN/A instead of whole files""") 111246SN/A 112246SN/A parser.add_argument("--repo-type", choices=repo_types, default="auto", 113246SN/A help="Repository type to use to detect changes") 114246SN/A 115246SN/A parser.add_argument("--checker", "-c", choices=verifier_names, default=[], 116246SN/A action="append", 117246SN/A help="""Style checkers to run. Can be specified 1182SN/A multiple times.""") 1192680Sktlim@umich.edu 1202423SN/A parser.add_argument("files", metavar="FILE", nargs="*", 1212190SN/A type=str, 122180SN/A help="Source file(s) to inspect") 12310110Sandreas.hansson@arm.com 1242190SN/A args = parser.parse_args() 12510190Sakash.bagdia@arm.com 12610190Sakash.bagdia@arm.com repo = repo_types[args.repo_type]() 12710110Sandreas.hansson@arm.com 1285715Shsul@eecs.umich.edu verifiers = [ verifier_names[name] for name in args.checker ] \ 1295715Shsul@eecs.umich.edu if args.checker else None 1305714Shsul@eecs.umich.edu 13110110Sandreas.hansson@arm.com files = args.files 1325714Shsul@eecs.umich.edu if not files and repo: 1335714Shsul@eecs.umich.edu added, modified = repo.staged_files() 1345714Shsul@eecs.umich.edu files = [ repo.file_path(f) for f in added + modified ] 13512406Sgabeblack@google.com 1362190SN/A for filename in files: 13712406Sgabeblack@google.com if args.modifications and repo and repo.in_repo(filename): 1382521SN/A regions = repo.modified_regions(filename) 1398887Sgeoffrey.blake@arm.com else: 1408733Sgeoffrey.blake@arm.com regions = all_regions 1419020Sgblack@eecs.umich.edu 1428541Sgblack@eecs.umich.edu if not verify(filename, regions=regions, 1434997Sgblack@eecs.umich.edu verbose=args.verbose, 1444997Sgblack@eecs.umich.edu verifiers=verifiers, 1453548Sgblack@eecs.umich.edu auto_fix=args.fix): 1462654SN/A sys.exit(1) 1478852Sandreas.hansson@arm.com