Deleted Added
sdiff udiff text old ( 11402:ac9e1a3bed79 ) new ( 11403:e8949ea6961f )
full compact
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

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

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.
41#
42# Authors: Nathan Binkert
43# Steve Reinhardt
44
45import sys
46import os
47from os.path import join as joinpath
48
49current_dir = os.path.dirname(__file__)
50sys.path.insert(0, current_dir)
51
52from style.verifiers import all_verifiers
53from style.validators import all_validators
54from style.file_types import lang_type
55from style.style import MercurialUI, check_ignores
56from style.region import *
57
58from mercurial import bdiff, mdiff, commands
59
60def modified_regions(old_data, new_data):
61 regions = Regions()
62 beg = None
63 for pbeg, pend, fbeg, fend in bdiff.blocks(old_data, new_data):
64 if beg is not None and beg != fbeg:
65 regions.append(beg, fbeg)
66 beg = fend
67 return regions

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

79 # only the lines that are new in both
80 mod_regions &= m2
81 else:
82 mod_regions = Regions()
83 mod_regions.append(0, len(lines))
84
85 return mod_regions
86
87
88def validate(filename, verbose, exit_code):
89 lang = lang_type(filename)
90 if lang not in ('C', 'C++'):
91 return
92
93 def bad():
94 if exit_code is not None:
95 sys.exit(exit_code)
96
97 try:
98 f = file(filename, 'r')
99 except OSError:
100 if verbose > 0:
101 print 'could not open file %s' % filename
102 bad()
103 return None
104
105 vals = [ v(filename, verbose=(verbose > 1), language=lang)
106 for v in all_validators ]
107
108 for i, line in enumerate(f):
109 line = line.rstrip('\n')
110 for v in vals:
111 v.validate_line(i, line)
112
113
114 return vals
115
116
117def _modified_regions(repo, patterns, **kwargs):
118 opt_all = kwargs.get('all', False)
119 opt_no_ignore = kwargs.get('no_ignore', False)
120
121 # Import the match (repository file name matching helper)
122 # function. Different versions of Mercurial keep it in different
123 # modules and implement them differently.
124 try:

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

184
185 The -v/--verbose flag will display the offending line(s) as well
186 as their location.
187 """
188
189 ui = MercurialUI(hgui, verbose=hgui.verbose)
190
191 # instantiate varifier objects
192 verifiers = [v(ui, opts, base=repo.root) for v in all_verifiers]
193
194 for fname, mod_regions in _modified_regions(repo, pats, **opts):
195 for verifier in verifiers:
196 if verifier.apply(joinpath(repo.root, fname), mod_regions):
197 return True
198
199 return False
200
201def do_check_format(hgui, repo, *pats, **opts):
202 """check files for gem5 code formatting violations
203
204 Without an argument, checks all modified and added files for gem5

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

210
211 The --all option can be specified to include clean files and check
212 modified files in their entirety.
213 """
214 ui = MercurialUI(hgui, hgui.verbose)
215
216 verbose = 0
217 for fname, mod_regions in _modified_regions(repo, pats, **opts):
218 vals = validate(joinpath(repo.root, fname), verbose, None)
219 if vals is None:
220 return True
221 elif any([not v for v in vals]):
222 print "%s:" % fname
223 for v in vals:
224 v.dump()
225 result = ui.prompt("invalid formatting\n(i)gnore or (a)bort?",
226 'ai', 'a')
227 if result == 'a':
228 return True
229
230 return False
231
232def check_hook(hooktype):

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

303 help="Produce verbose output")
304
305 parser.add_argument("file", metavar="FILE", nargs="+",
306 type=str,
307 help="Source file to inspect")
308
309 args = parser.parse_args()
310
311 for filename in args.file:
312 vals = validate(filename, verbose=args.verbose,
313 exit_code=1)
314
315 if args.verbose > 0 and vals is not None:
316 for v in vals:
317 v.dump()