util: Add a README file for the m5 utility.
[gem5.git] / util / style / style.py
1 #! /usr/bin/env python3
2 # Copyright (c) 2014, 2016 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
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.
13 #
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.
18 #
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.
29 #
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.
41
42 from abc import ABCMeta, abstractmethod
43 import difflib
44 import re
45 import sys
46
47 from .region import *
48
49 tabsize = 8
50 lead = re.compile(r'^([ \t]+)')
51 trail = re.compile(r'([ \t]+)$')
52 any_control = re.compile(r'\b(if|while|for)([ \t]*)\(')
53
54
55 class UserInterface(object, metaclass=ABCMeta):
56 def __init__(self, verbose=False):
57 self.verbose = verbose
58
59 def prompt(self, prompt, results, default):
60 while True:
61 result = self._prompt(prompt, results, default)
62 if result in results:
63 return result
64
65 @abstractmethod
66 def _prompt(self, prompt, results, default):
67 pass
68
69 @abstractmethod
70 def write(self, string):
71 pass
72
73 class StdioUI(UserInterface):
74 def _prompt(self, prompt, results, default):
75 return input(prompt) or default
76
77 def write(self, string):
78 sys.stdout.write(string)
79
80 def _re_ignore(expr):
81 """Helper function to create regular expression ignore file
82 matcher functions"""
83
84 rex = re.compile(expr)
85 def match_re(fname):
86 return rex.match(fname)
87 return match_re
88
89 # This list contains a list of functions that are called to determine
90 # if a file should be excluded from the style matching rules or
91 # not. The functions are called with the file name relative to the
92 # repository root (without a leading slash) as their argument. A file
93 # is excluded if any function in the list returns true.
94 style_ignores = [
95 # Ignore external projects as they are unlikely to follow the gem5
96 # coding convention.
97 _re_ignore("^ext/"),
98 # Ignore test data, as they are not code
99 _re_ignore("^tests/(?:quick|long)/"),
100 # Ignore RISC-V assembly tests as they are maintained in an external
101 # project that does not follow the gem5 coding convention
102 _re_ignore("tests/test-progs/asmtest/src/riscv/"),
103 # Ignore RISC-V assembly dump files
104 _re_ignore("tests/test-progs/asmtest/dump/riscv/")
105 ]
106
107 def check_ignores(fname):
108 """Check if a file name matches any of the ignore rules"""
109
110 for rule in style_ignores:
111 if rule(fname):
112 return True
113
114 return False
115
116
117 def normalized_len(line):
118 """Return a normalized line length with expanded tabs"""
119
120 count = 0
121 for c in line:
122 if c == '\t':
123 count += tabsize - count % tabsize
124 else:
125 count += 1
126
127 return count
128
129 def modified_regions(old, new, context=0):
130 regions = Regions()
131 m = difflib.SequenceMatcher(a=old, b=new, autojunk=False)
132 for group in m.get_grouped_opcodes(context):
133 first = group[0]
134 last = group[-1]
135
136 regions.extend(Region(first[3], last[4] + 1))
137
138 return regions