1 #! /usr/bin/env python2.7
2 # Copyright (c) 2014 ARM Limited
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
9 # licensed hereunder. You may use the software subject to the license
10 # terms below provided that you ensure that this notice is replicated
11 # unmodified and in its entirety in all distributions of the software,
12 # modified or unmodified, in source code or in binary form.
14 # Copyright (c) 2006 The Regents of The University of Michigan
15 # Copyright (c) 2007,2011 The Hewlett-Packard Development Company
16 # Copyright (c) 2016 Advanced Micro Devices, Inc.
17 # All rights reserved.
19 # Redistribution and use in source and binary forms, with or without
20 # modification, are permitted provided that the following conditions are
21 # met: redistributions of source code must retain the above copyright
22 # notice, this list of conditions and the following disclaimer;
23 # redistributions in binary form must reproduce the above copyright
24 # notice, this list of conditions and the following disclaimer in the
25 # documentation and/or other materials provided with the distribution;
26 # neither the name of the copyright holders nor the names of its
27 # contributors may be used to endorse or promote products derived from
28 # this software without specific prior written permission.
30 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 # Authors: Nathan Binkert
47 from os
.path
import join
as joinpath
49 current_dir
= os
.path
.dirname(__file__
)
50 sys
.path
.insert(0, current_dir
)
52 from style
.verifiers
import all_verifiers
53 from style
.style
import MercurialUI
, check_ignores
54 from style
.region
import *
56 from mercurial
import bdiff
, mdiff
, commands
58 def modified_regions(old_data
, new_data
):
61 for pbeg
, pend
, fbeg
, fend
in bdiff
.blocks(old_data
, new_data
):
62 if beg
is not None and beg
!= fbeg
:
63 regions
.append(beg
, fbeg
)
67 def modregions(wctx
, fname
):
68 fctx
= wctx
.filectx(fname
)
71 file_data
= fctx
.data()
72 lines
= mdiff
.splitnewlines(file_data
)
73 if len(pctx
) in (1, 2):
74 mod_regions
= modified_regions(pctx
[0].data(), file_data
)
76 m2
= modified_regions(pctx
[1].data(), file_data
)
77 # only the lines that are new in both
80 mod_regions
= Regions()
81 mod_regions
.append(0, len(lines
))
86 def _modified_regions(repo
, patterns
, **kwargs
):
87 opt_all
= kwargs
.get('all', False)
88 opt_no_ignore
= kwargs
.get('no_ignore', False)
90 # Import the match (repository file name matching helper)
91 # function. Different versions of Mercurial keep it in different
92 # modules and implement them differently.
94 from mercurial
import scmutil
95 m
= scmutil
.match(repo
[None], patterns
, kwargs
)
97 from mercurial
import cmdutil
98 m
= cmdutil
.match(repo
, patterns
, kwargs
)
100 modified
, added
, removed
, deleted
, unknown
, ignore
, clean
= \
101 repo
.status(match
=m
, clean
=opt_all
)
105 wctx
= repo
.workingctx()
107 from mercurial
import context
108 wctx
= context
.workingctx(repo
)
110 files
= [ (fn
, all_regions
) for fn
in added
] + \
111 [ (fn
, modregions(wctx
, fn
)) for fn
in modified
]
113 files
= [ (fn
, all_regions
) for fn
in added
+ modified
+ clean
]
115 for fname
, mod_regions
in files
:
116 if opt_no_ignore
or not check_ignores(fname
):
117 yield fname
, mod_regions
120 def do_check_style(hgui
, repo
, *pats
, **opts
):
121 """check files for proper m5 style guidelines
123 Without an argument, checks all modified and added files for gem5
124 coding style violations. A list of files can be specified to limit
125 the checker to a subset of the repository. The style rules are
126 normally applied on a diff of the repository state (i.e., added
127 files are checked in their entirety while only modifications of
128 modified files are checked).
130 The --all option can be specified to include clean files and check
131 modified files in their entirety.
133 The --fix-<check>, --ignore-<check>, and --skip-<check> options
134 can be used to control individual style checks:
136 --fix-<check> will perform the check and automatically attempt to
137 fix sny style error (printing a warning if unsuccessful)
139 --ignore-<check> will perform the check but ignore any errors
140 found (other than printing a message for each)
142 --skip-<check> will skip performing the check entirely
144 If none of these options are given, all checks will be performed
145 and the user will be prompted on how to handle each error.
147 --fix-all, --ignore-all, and --skip-all are equivalent to specifying
148 --fix-<check>, --ignore-<check>, or --skip-<check> for all checks,
149 respectively. However, option settings for specific checks take
150 precedence. Thus --skip-all --fix-white can be used to skip every
151 check other than whitespace errors, which will be checked and
154 The -v/--verbose flag will display the offending line(s) as well
158 ui
= MercurialUI(hgui
, verbose
=hgui
.verbose
)
160 # instantiate varifier objects
161 verifiers
= [v(ui
, opts
, base
=repo
.root
) for v
in all_verifiers
]
163 for fname
, mod_regions
in _modified_regions(repo
, pats
, **opts
):
164 for verifier
in verifiers
:
165 if verifier
.apply(joinpath(repo
.root
, fname
), mod_regions
):
170 def check_hook(hooktype
):
171 if hooktype
not in ('pretxncommit', 'pre-qrefresh'):
172 raise AttributeError, \
173 "This hook is not meant for %s" % hooktype
175 # This function provides a hook that is called before transaction
176 # commit and on qrefresh
177 def check_style(ui
, repo
, hooktype
, **kwargs
):
182 return do_check_style(ui
, repo
, **args
)
185 traceback
.print_exc()
189 from mercurial
.i18n
import _
194 _common_region_options
= [
196 _("include clean files and unmodified parts of modified files")),
197 ('', 'no-ignore', False, _("ignore the style ignore list")),
201 fix_opts
= [('f', 'fix-all', False, _("fix all style errors"))] + \
202 [('', 'fix-' + v
.opt_name
, False,
203 _('fix errors in ' + v
.test_name
)) for v
in all_verifiers
]
204 ignore_opts
= [('', 'ignore-all', False, _("ignore all style errors"))] + \
205 [('', 'ignore-' + v
.opt_name
, False,
206 _('ignore errors in ' + v
.test_name
)) for v
in all_verifiers
]
207 skip_opts
= [('', 'skip-all', False, _("skip all style error checks"))] + \
208 [('', 'skip-' + v
.opt_name
, False,
209 _('skip checking for ' + v
.test_name
)) for v
in all_verifiers
]
210 all_opts
= fix_opts
+ ignore_opts
+ skip_opts
215 do_check_style
, all_opts
+ _common_region_options
+ commands
.walkopts
,
216 _('hg m5style [-a] [FILE]...')),
219 if __name__
== '__main__':
220 print >> sys
.stderr
, "This file cannot be used from the command line. Use"
221 print >> sys
.stderr
, "style.py instead."