113540Sandrea.mondelli@ucf.edu#! /usr/bin/env python2.7
211409Sandreas.sandberg@arm.com#
311409Sandreas.sandberg@arm.com# Copyright (c) 2016 ARM Limited
411409Sandreas.sandberg@arm.com# All rights reserved
511409Sandreas.sandberg@arm.com#
611409Sandreas.sandberg@arm.com# The license below extends only to copyright in the software and shall
711409Sandreas.sandberg@arm.com# not be construed as granting a license to any other intellectual
811409Sandreas.sandberg@arm.com# property including but not limited to intellectual property relating
911409Sandreas.sandberg@arm.com# to a hardware implementation of the functionality of the software
1011409Sandreas.sandberg@arm.com# licensed hereunder.  You may use the software subject to the license
1111409Sandreas.sandberg@arm.com# terms below provided that you ensure that this notice is replicated
1211409Sandreas.sandberg@arm.com# unmodified and in its entirety in all distributions of the software,
1311409Sandreas.sandberg@arm.com# modified or unmodified, in source code or in binary form.
1411409Sandreas.sandberg@arm.com#
1511409Sandreas.sandberg@arm.com# Redistribution and use in source and binary forms, with or without
1611409Sandreas.sandberg@arm.com# modification, are permitted provided that the following conditions are
1711409Sandreas.sandberg@arm.com# met: redistributions of source code must retain the above copyright
1811409Sandreas.sandberg@arm.com# notice, this list of conditions and the following disclaimer;
1911409Sandreas.sandberg@arm.com# redistributions in binary form must reproduce the above copyright
2011409Sandreas.sandberg@arm.com# notice, this list of conditions and the following disclaimer in the
2111409Sandreas.sandberg@arm.com# documentation and/or other materials provided with the distribution;
2211409Sandreas.sandberg@arm.com# neither the name of the copyright holders nor the names of its
2311409Sandreas.sandberg@arm.com# contributors may be used to endorse or promote products derived from
2411409Sandreas.sandberg@arm.com# this software without specific prior written permission.
2511409Sandreas.sandberg@arm.com#
2611409Sandreas.sandberg@arm.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2711409Sandreas.sandberg@arm.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2811409Sandreas.sandberg@arm.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2911409Sandreas.sandberg@arm.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3011409Sandreas.sandberg@arm.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3111409Sandreas.sandberg@arm.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3211409Sandreas.sandberg@arm.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3311409Sandreas.sandberg@arm.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3411409Sandreas.sandberg@arm.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3511409Sandreas.sandberg@arm.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3611409Sandreas.sandberg@arm.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3711409Sandreas.sandberg@arm.com#
3811409Sandreas.sandberg@arm.com# Authors: Andreas Sandberg
3911409Sandreas.sandberg@arm.com
4011409Sandreas.sandberg@arm.comimport os
4111409Sandreas.sandberg@arm.comimport sys
4211409Sandreas.sandberg@arm.com
4311409Sandreas.sandberg@arm.comfrom style.file_types import lang_type
4411717Sandreas.sandberg@arm.comimport style.verifiers
4511409Sandreas.sandberg@arm.comfrom style.region import all_regions
4611409Sandreas.sandberg@arm.com
4711409Sandreas.sandberg@arm.comfrom style.style import StdioUI
4811409Sandreas.sandberg@arm.comfrom style import repo
4911409Sandreas.sandberg@arm.com
5011717Sandreas.sandberg@arm.comverifier_names = dict([
5111717Sandreas.sandberg@arm.com    (c.__name__, c) for c in style.verifiers.all_verifiers ])
5211717Sandreas.sandberg@arm.com
5311717Sandreas.sandberg@arm.comdef verify(filename, regions=all_regions, verbose=False, verifiers=None,
5411717Sandreas.sandberg@arm.com           auto_fix=False):
5511409Sandreas.sandberg@arm.com    ui = StdioUI()
5611717Sandreas.sandberg@arm.com    opts = {
5711717Sandreas.sandberg@arm.com        "fix_all" : auto_fix,
5811717Sandreas.sandberg@arm.com    }
5911409Sandreas.sandberg@arm.com    base = os.path.join(os.path.dirname(__file__), "..")
6011717Sandreas.sandberg@arm.com    if verifiers is None:
6111717Sandreas.sandberg@arm.com        verifiers = style.verifiers.all_verifiers
6211409Sandreas.sandberg@arm.com
6311409Sandreas.sandberg@arm.com    if verbose:
6411409Sandreas.sandberg@arm.com        print "Verifying %s[%s]..." % (filename, regions)
6511717Sandreas.sandberg@arm.com    for verifier in [ v(ui, opts, base=base) for v in verifiers ]:
6611409Sandreas.sandberg@arm.com        if verbose:
6711409Sandreas.sandberg@arm.com            print "Applying %s (%s)" % (
6811409Sandreas.sandberg@arm.com                verifier.test_name, verifier.__class__.__name__)
6911409Sandreas.sandberg@arm.com        if verifier.apply(filename, regions=regions):
7011409Sandreas.sandberg@arm.com            return False
7111409Sandreas.sandberg@arm.com    return True
7211409Sandreas.sandberg@arm.com
7311409Sandreas.sandberg@arm.comdef detect_repo():
7411409Sandreas.sandberg@arm.com    repo_classes = repo.detect_repo()
7511409Sandreas.sandberg@arm.com    if not repo_classes:
7611409Sandreas.sandberg@arm.com        print >> sys.stderr, "Error: Failed to detect repository type, no " \
7711409Sandreas.sandberg@arm.com            "known repository type found."
7811409Sandreas.sandberg@arm.com        sys.exit(1)
7911409Sandreas.sandberg@arm.com    elif len(repo_classes) > 1:
8011409Sandreas.sandberg@arm.com        print >> sys.stderr, "Error: Detected multiple repository types."
8111409Sandreas.sandberg@arm.com        sys.exit(1)
8211409Sandreas.sandberg@arm.com    else:
8311409Sandreas.sandberg@arm.com        return repo_classes[0]()
8411409Sandreas.sandberg@arm.com
8511409Sandreas.sandberg@arm.comrepo_types = {
8611409Sandreas.sandberg@arm.com    "auto" : detect_repo,
8711409Sandreas.sandberg@arm.com    "none" : lambda : None,
8811409Sandreas.sandberg@arm.com    "git" : repo.GitRepo,
8911409Sandreas.sandberg@arm.com    "hg" : repo.MercurialRepo,
9011409Sandreas.sandberg@arm.com}
9111409Sandreas.sandberg@arm.com
9211409Sandreas.sandberg@arm.comif __name__ == '__main__':
9311409Sandreas.sandberg@arm.com    import argparse
9411409Sandreas.sandberg@arm.com
9511409Sandreas.sandberg@arm.com    parser = argparse.ArgumentParser(
9611409Sandreas.sandberg@arm.com        description="Check a file for gem5 style violations",
9711409Sandreas.sandberg@arm.com        epilog="""If no files are specified, the style checker tries to
9811409Sandreas.sandberg@arm.com        determine the list of modified and added files from the version
9911409Sandreas.sandberg@arm.com        control system and checks those."""
10011409Sandreas.sandberg@arm.com    )
10111409Sandreas.sandberg@arm.com
10211409Sandreas.sandberg@arm.com    parser.add_argument("--verbose", "-v", action="count",
10311409Sandreas.sandberg@arm.com                        help="Produce verbose output")
10411409Sandreas.sandberg@arm.com
10511717Sandreas.sandberg@arm.com    parser.add_argument("--fix", "-f", action="store_true",
10611717Sandreas.sandberg@arm.com                        help="Automatically fix style violations.")
10711717Sandreas.sandberg@arm.com
10811409Sandreas.sandberg@arm.com    parser.add_argument("--modifications", "-m", action="store_true",
10911409Sandreas.sandberg@arm.com                        help="""Apply the style checker to modified regions
11011409Sandreas.sandberg@arm.com                        instead of whole files""")
11111409Sandreas.sandberg@arm.com
11211409Sandreas.sandberg@arm.com    parser.add_argument("--repo-type", choices=repo_types, default="auto",
11311409Sandreas.sandberg@arm.com                        help="Repository type to use to detect changes")
11411409Sandreas.sandberg@arm.com
11511717Sandreas.sandberg@arm.com    parser.add_argument("--checker", "-c", choices=verifier_names, default=[],
11611717Sandreas.sandberg@arm.com                        action="append",
11711717Sandreas.sandberg@arm.com                        help="""Style checkers to run. Can be specified
11811717Sandreas.sandberg@arm.com                        multiple times.""")
11911717Sandreas.sandberg@arm.com
12011409Sandreas.sandberg@arm.com    parser.add_argument("files", metavar="FILE", nargs="*",
12111409Sandreas.sandberg@arm.com                        type=str,
12211409Sandreas.sandberg@arm.com                        help="Source file(s) to inspect")
12311409Sandreas.sandberg@arm.com
12411409Sandreas.sandberg@arm.com    args = parser.parse_args()
12511409Sandreas.sandberg@arm.com
12611409Sandreas.sandberg@arm.com    repo = repo_types[args.repo_type]()
12711409Sandreas.sandberg@arm.com
12811717Sandreas.sandberg@arm.com    verifiers = [ verifier_names[name] for name in args.checker ] \
12911717Sandreas.sandberg@arm.com                if args.checker else None
13011717Sandreas.sandberg@arm.com
13111409Sandreas.sandberg@arm.com    files = args.files
13211409Sandreas.sandberg@arm.com    if not files and repo:
13311409Sandreas.sandberg@arm.com        added, modified = repo.staged_files()
13411409Sandreas.sandberg@arm.com        files = [ repo.file_path(f) for f in added + modified ]
13511409Sandreas.sandberg@arm.com
13611409Sandreas.sandberg@arm.com    for filename in files:
13711409Sandreas.sandberg@arm.com        if args.modifications and repo and repo.in_repo(filename):
13811409Sandreas.sandberg@arm.com            regions = repo.modified_regions(filename)
13911409Sandreas.sandberg@arm.com        else:
14011409Sandreas.sandberg@arm.com            regions = all_regions
14111409Sandreas.sandberg@arm.com
14211409Sandreas.sandberg@arm.com        if not verify(filename, regions=regions,
14311717Sandreas.sandberg@arm.com                      verbose=args.verbose,
14411717Sandreas.sandberg@arm.com                      verifiers=verifiers,
14511717Sandreas.sandberg@arm.com                      auto_fix=args.fix):
14611409Sandreas.sandberg@arm.com            sys.exit(1)
147