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.
44 from os
.path
import join
as joinpath
46 current_dir
= os
.path
.dirname(__file__
)
47 sys
.path
.insert(0, current_dir
)
49 from style
.verifiers
import all_verifiers
50 from style
.style
import MercurialUI
, check_ignores
51 from style
.region
import *
53 from mercurial
import bdiff
, mdiff
, commands
55 def modified_regions(old_data
, new_data
):
58 for pbeg
, pend
, fbeg
, fend
in bdiff
.blocks(old_data
, new_data
):
59 if beg
is not None and beg
!= fbeg
:
60 regions
.append(beg
, fbeg
)
64 def modregions(wctx
, fname
):
65 fctx
= wctx
.filectx(fname
)
68 file_data
= fctx
.data()
69 lines
= mdiff
.splitnewlines(file_data
)
70 if len(pctx
) in (1, 2):
71 mod_regions
= modified_regions(pctx
[0].data(), file_data
)
73 m2
= modified_regions(pctx
[1].data(), file_data
)
74 # only the lines that are new in both
77 mod_regions
= Regions()
78 mod_regions
.append(0, len(lines
))
83 def _modified_regions(repo
, patterns
, **kwargs
):
84 opt_all
= kwargs
.get('all', False)
85 opt_no_ignore
= kwargs
.get('no_ignore', False)
87 # Import the match (repository file name matching helper)
88 # function. Different versions of Mercurial keep it in different
89 # modules and implement them differently.
91 from mercurial
import scmutil
92 m
= scmutil
.match(repo
[None], patterns
, kwargs
)
94 from mercurial
import cmdutil
95 m
= cmdutil
.match(repo
, patterns
, kwargs
)
97 modified
, added
, removed
, deleted
, unknown
, ignore
, clean
= \
98 repo
.status(match
=m
, clean
=opt_all
)
102 wctx
= repo
.workingctx()
104 from mercurial
import context
105 wctx
= context
.workingctx(repo
)
107 files
= [ (fn
, all_regions
) for fn
in added
] + \
108 [ (fn
, modregions(wctx
, fn
)) for fn
in modified
]
110 files
= [ (fn
, all_regions
) for fn
in added
+ modified
+ clean
]
112 for fname
, mod_regions
in files
:
113 if opt_no_ignore
or not check_ignores(fname
):
114 yield fname
, mod_regions
117 def do_check_style(hgui
, repo
, *pats
, **opts
):
118 """check files for proper m5 style guidelines
120 Without an argument, checks all modified and added files for gem5
121 coding style violations. A list of files can be specified to limit
122 the checker to a subset of the repository. The style rules are
123 normally applied on a diff of the repository state (i.e., added
124 files are checked in their entirety while only modifications of
125 modified files are checked).
127 The --all option can be specified to include clean files and check
128 modified files in their entirety.
130 The --fix-<check>, --ignore-<check>, and --skip-<check> options
131 can be used to control individual style checks:
133 --fix-<check> will perform the check and automatically attempt to
134 fix sny style error (printing a warning if unsuccessful)
136 --ignore-<check> will perform the check but ignore any errors
137 found (other than printing a message for each)
139 --skip-<check> will skip performing the check entirely
141 If none of these options are given, all checks will be performed
142 and the user will be prompted on how to handle each error.
144 --fix-all, --ignore-all, and --skip-all are equivalent to specifying
145 --fix-<check>, --ignore-<check>, or --skip-<check> for all checks,
146 respectively. However, option settings for specific checks take
147 precedence. Thus --skip-all --fix-white can be used to skip every
148 check other than whitespace errors, which will be checked and
151 The -v/--verbose flag will display the offending line(s) as well
155 ui
= MercurialUI(hgui
, verbose
=hgui
.verbose
)
157 # instantiate varifier objects
158 verifiers
= [v(ui
, opts
, base
=repo
.root
) for v
in all_verifiers
]
160 for fname
, mod_regions
in _modified_regions(repo
, pats
, **opts
):
161 for verifier
in verifiers
:
162 if verifier
.apply(joinpath(repo
.root
, fname
), mod_regions
):
167 def check_hook(hooktype
):
168 if hooktype
not in ('pretxncommit', 'pre-qrefresh'):
169 raise AttributeError, \
170 "This hook is not meant for %s" % hooktype
172 # This function provides a hook that is called before transaction
173 # commit and on qrefresh
174 def check_style(ui
, repo
, hooktype
, **kwargs
):
179 return do_check_style(ui
, repo
, **args
)
182 traceback
.print_exc()
186 from mercurial
.i18n
import _
191 _common_region_options
= [
193 _("include clean files and unmodified parts of modified files")),
194 ('', 'no-ignore', False, _("ignore the style ignore list")),
198 fix_opts
= [('f', 'fix-all', False, _("fix all style errors"))] + \
199 [('', 'fix-' + v
.opt_name
, False,
200 _('fix errors in ' + v
.test_name
)) for v
in all_verifiers
]
201 ignore_opts
= [('', 'ignore-all', False, _("ignore all style errors"))] + \
202 [('', 'ignore-' + v
.opt_name
, False,
203 _('ignore errors in ' + v
.test_name
)) for v
in all_verifiers
]
204 skip_opts
= [('', 'skip-all', False, _("skip all style error checks"))] + \
205 [('', 'skip-' + v
.opt_name
, False,
206 _('skip checking for ' + v
.test_name
)) for v
in all_verifiers
]
207 all_opts
= fix_opts
+ ignore_opts
+ skip_opts
212 do_check_style
, all_opts
+ _common_region_options
+ commands
.walkopts
,
213 _('hg m5style [-a] [FILE]...')),
216 if __name__
== '__main__':
217 print >> sys
.stderr
, "This file cannot be used from the command line. Use"
218 print >> sys
.stderr
, "style.py instead."