-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeCheck.py
More file actions
293 lines (217 loc) · 9.64 KB
/
Copy pathCodeCheck.py
File metadata and controls
293 lines (217 loc) · 9.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
from termcolor import colored
class CodeCheck():
def __init__(self, f_dir, f_name):
self.f_dir = f_dir
self.f_name = f_name
self.linter_out = ""
self.constants = []
@staticmethod
def rm_comment(line):
# ignore comments
comment_bgn = line.find("--")
if comment_bgn != -1:
line = line[:comment_bgn].strip()
return line.strip()
def check_file_name(self, lines):
info = "Consider renaming file (entity.vhd) or entity "
entity_keys = ["entity ", " is"]
for j, line in enumerate(lines):
i = j+1
if all(k in line.lower() for k in entity_keys):
# name
line = line.strip()
name = line[len("entity"):-2].strip()
if line.lower().find(self.f_name[:-4]) == -1:
self.linter_out += str(i) + ", " + info + ": entity " + name +" in "+ self.f_name +"\n"
def check_line_length(self, line, i, max_line_length):
info = "Line too long"
line_length = len(line)
if line_length > max_line_length:
hint = " (" + str(line_length) + "/" + str(max_line_length) + ")"
self.linter_out += str(i) + ", " + info + hint + ": " + line.strip() + "\n"
def check_statements_per_line(self, line, i):
info = "Lines must not contain multiple statements"
# ignore comments
line_ic = self.rm_comment(line)
chk = line_ic.find(";", 0)
if line_ic.find(";", chk+1) > chk:
self.linter_out += str(i) + ", " + info + ": " + line_ic + "\n"
def check_tabs(self, line, i):
info = "Tab found (\\t)"
chk = line.lower().find("\t", 0)
if chk != -1:
self.linter_out += str(i) + ", " + info + ": " + line.strip() + "\n"
def check_constant_names(self, line, i):
info = "Constant name should be upper case"
self.constants = []
constant_keys = ["constant ", ":", ";"]
if all(k in line for k in constant_keys):
bgn = line.find("constant") + len("constant")
end = line.find(":")
constant_name = line[bgn:end]
self.constants.append(constant_name.strip())
upper_case = True
for _, name in enumerate(constant_name):
if name.isalpha() and name.islower():
upper_case = False
if not upper_case:
self.linter_out += str(i) + ", " + info + ": " + constant_name + "\n"
def check_lower_case(self, line, i):
info = "Upper case only in constant names or comments"
exceptions = [" N", " T", "N_", "T_"]
exceptions.extend(self.constants)
excep_name = False
for ex in exceptions:
ex_chk = line.find(ex)
if ex_chk != -1:
excep_name = True
break
# ignore comments
line_ic = self.rm_comment(line)
lower_case = True
for _, char in enumerate(line_ic):
if char.isalpha() and char.isupper():
lower_case = False
if not lower_case and not excep_name:
self.linter_out += str(i) + ", " + info + ": " + line_ic + "\n"
def check_signal_names(self, line, i, max_name_length):
info = "Signal name too long"
line_ic = self.rm_comment(line)
name_bgn = line_ic.lower().find("signal ")
chk_multi = line.find(",")#
if name_bgn != -1 and chk_multi == -1:
name_end = line_ic.find(":")
signal_name = line_ic[name_bgn+len("signal "):name_end-1]
if len(signal_name.strip()) > max_name_length:
hint = " ("+str(len(signal_name)) + "/" + str(max_name_length)+")"
self.linter_out += str(i) + ", " + info + hint + ": " + signal_name + "\n"
def check_var_names(self, line, i, max_name_length):
info = "Variable name too long"
line_ic = self.rm_comment(line)
name_bgn = line_ic.lower().find("variable ")
chk_multi = line.find(",")#
if name_bgn != -1 and chk_multi == -1:
name_end = line_ic.find(":")
var_name = line_ic[name_bgn+len("variable "):name_end-1]
if len(var_name.strip()) > max_name_length:
hint = " ("+str(len(var_name)) + "/" + str(max_name_length)+")"
self.linter_out += str(i) + ", " + info + hint + ": " + var_name + "\n"
def check_pkg_name(self, line, i):
info = "Bad naming style for package (_pkg)"
pack_keys = ["package ", " is"]
if all(k in line for k in pack_keys):
if line.lower().find("_pkg ") == -1:
self.linter_out += str(i) + ", " + info + ": " + line.strip() + "\n"
def check_arc_name(self, line, i):
info = "Bad naming style for architecture (_arc)"
arch_keys = ["architecture ", " of ", " is"]
if all(k in line for k in arch_keys):
if line.lower().find("_arc ") == -1:
self.linter_out += str(i) + ", " + info + ": " + line.strip() + "\n"
def check_port_order(self, lines):
info = "Wrong port order"
i = 1
entity_dec_keys = ["entity ", " is"]
entity_lines = []
entity_found = False
for line in lines:
if all(k in line for k in entity_dec_keys):
entity_found = True
j = i
if entity_found:
entity_lines.append(line.strip())
if line.lower().find("end") != -1:
break
i += 1
#in_pos = False
out_pos = False
for line in entity_lines:
in_chk = line.find("in ")
#if in_chk != -1:
# in_pos = True
out_chk = line.find("out ")
if out_chk != -1:
out_pos = True
if in_chk != -1 and out_pos:
self.linter_out += str(j) + ", " + info + ": " + line + "\n"
j += 1
def check_spaces_in_ports(self, lines):
info = "Add space before"
i = 1
entity_dec_keys = ["entity ", " is"]
entity_lines = []
entity_found = False
for line in lines:
if all(k in line for k in entity_dec_keys):
entity_found = True
j = i
if entity_found:
entity_lines.append(line.strip())
if line.lower().find("end") != -1:
break
i += 1
for line in entity_lines:
if line.find("in ") != -1 and line.find(": in") == -1:
self.linter_out += str(j) + ", " + info + " in port : " + line + "\n"
if line.find("out ") != -1 and line.find(": out") == -1:
self.linter_out += str(j) + ", " + info + " out port : " + line + "\n"
if line.find("inout ") != -1 and line.find(": inout") == -1:
self.linter_out += str(j) + ", " + info + " inout port : " + line + "\n"
j += 1
def check_msb_to_lsb(self, line, i):
info = "Vector range should be MSB to LSB"
if line.lower().find("std_logic_vector") != -1:
chk_to = line.lower().find(" to ")
chk_downto = line.lower().find(" downto ")
if chk_downto == -1 and chk_to != -1:
self.linter_out += str(i) + ", " + info + ": " + line.strip() + "\n"
def check_user_def_types(self, line, i):
info = "Self-defined type not in library file"
type_keys = ["type ", " is"]
if all(k in line for k in type_keys):
if self.f_name.find("_pkg") == -1 and line.lower().find("fsm") == -1:
self.linter_out += str(i) + ", " + info + ": " + line.strip() + "\n"
def check_time_units(self, line, i):
info = "Add space before time unit"
time_keys = ["ns;", "ms;", "ps;"]
for key in time_keys:
chk = line.lower().find(key, 0)
if chk != -1 and line[chk-1].isnumeric() and not line[chk+len(key)-1].isalpha():
self.linter_out += str(i) + ", " + info + ": " + line.strip() + "\n"
def trailing_whitespace(self, line, i):
info = "Trailing whitespace"
line = line.lstrip()
chk = line.find(" ", 0)
chk_e1 = line.find(":", 0)
chk_e2 = line.find("=>", 0)
if chk != -1 and (chk_e1 == -1 and chk_e2 == -1):
self.linter_out += str(i) + ", " + info + ": " + line.strip() + "\n"
def check_comments(self, line, i):
info = "Ugly comment"
key = ["xxx", "to do", "todo", "delete", "check", "?"]
chk = line.find("--")
if chk != -1:
comment = line[chk:].strip()
if any(k in comment.lower() for k in key):
self.linter_out += str(i) + ", " + info + ": "+ comment + "\n"
def find_reports(self, line, i):
info = "Debugging code"
chk = line.lower().find("report", 0)
if chk != -1:
self.linter_out += str(i) + ", " + info + ": " + line.strip() + "\n"
def check_semicolons(self, line, i):
info = "Remove space before semicolon"
chk = line.lower().find(" ;", 0)
if chk != -1:
self.linter_out += str(i) + ", " + info + ": " + line.strip() + "\n"
def print2console(self, os, color):
if os == "win":
os.system('color')
print(colored("*************** " + self.f_name, color))
print(self.linter_out)
def print2file(self, f_out_dir, f_out_name):
file_content = "\n*************** " + self.f_name + "\n" + self.linter_out
f_out_path = f_out_dir + f_out_name
f_cc = open(f_out_path, "a")
f_cc.write(file_content)
f_cc.close()