forked from vianuevm/cppStyle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
single_line_checks.py
334 lines (282 loc) · 12.5 KB
/
single_line_checks.py
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
from style_grader_functions import check_if_function, check_if_function_prototype
from pyparsing import Literal, Word, Optional, ParseException, Group, SkipTo, alphanums, LineStart, srange
import re
def check_function_def_above_main(self, code):
prototype = check_if_function_prototype(code)
function = check_if_function(code)
inside = Literal("int main")
if len(inside.searchString(code)):
return
elif function and not prototype and self.outside_main:
function_regex = re.compile("^\s*(\w+)\s+(\w+)")
match = function_regex.search(code)
function_name = match.group(2) if match else "NOT_FOUND"
self.add_error(label="DEFINITION_ABOVE_MAIN", data={'function': function_name})
def check_int_for_bool(self, code):
if check_if_function(code):
function_regex = re.compile("^\s*(\w+)\s+(\w+)")
match = function_regex.search(code)
if match:
self.current_function = (match.group(1), match.group(2))
current_function = getattr(self, "current_function", ("", ""))
return_regex = re.compile("\s*return\s+(\w+)")
match = return_regex.search(code)
if match and match.group(1).isdigit() and current_function[0] == "bool":
self.add_error(label="INT_FOR_BOOL")
# New operator spacing function
def check_operator_spacing(self, code):
# TODO: Temporary fix to ignore & and * operators in function params
if check_if_function(code) or check_if_function_prototype(code) or\
'#include' in code: return
# Check normal operators
# account for *=, %=, /=, +=, -=
indexes = []
indexes = findOccurences(code, '+') + \
findOccurences(code, '-') + \
findOccurences(code, '%') + \
findOccurences(code, '*') + \
findOccurences(code, '/') + \
findOccurences(code, '!') + \
findOccurences(code, '>') + \
findOccurences(code, '<') + \
findOccurences(code, '=') + \
findOccurences(code, '&') + \
findOccurences(code, '|')
indexes.sort() # Force compound operator indexes to be correctly ordered
skip_next = False
for operator_index in indexes:
if skip_next:
# skip second operator in compound/increment/decrement operator
skip_next = False
continue
if skip_operator(code, operator_index):
skip_next = True
elif is_compound_operator(code, operator_index):
# Always use front char in compound operators, therefore need to skip second char
skip_next = True
if not operator_helper(True, code, operator_index):
self.add_error(label='OPERATOR_SPACING', column=operator_index,
data={'operator': code[operator_index:operator_index + 2]})
else:
# TODO: Add code checking for unary + and - operators
if code[operator_index] == '!':
index = operator_index - 1
# Only check for spacing in front of ! (NOT) operator
if code[index]:
if code[index] not in [' ', '\r', '\n', '(']:
self.add_error(label='OPERATOR_SPACING', column=operator_index, data={'operator': code[operator_index]})
elif not operator_helper(False, code, operator_index):
self.add_error(label='OPERATOR_SPACING', column=operator_index, data={'operator': code[operator_index]})
def skip_operator(code, index):
# Don't worry about increment/decrement/pointer-arrow operators
return is_increment_decrement(code, index) or is_pointer_arrow(code, index)
def is_increment_decrement(code, index):
if code[index + 1]:
if code[index] in ['+', '-'] and code[index + 1] == code[index]:
return True
return False
def is_pointer_arrow(code, index):
if code[index + 1]:
if code[index] == '-' and code[index + 1] == '>':
return True
return False
def is_compound_operator(code, index):
if code[index + 1]:
# Check for >=, <=, >>, <<
if code[index] in ['>', '<']:
if code[index + 1] == '=' or code[index + 1] == code[index]:
return True
# Check for +=, -=, *=, /=, %=, ==, !=
if code[index] in ['*', '/', '+', '-', '!', '=', '%']:
if code[index + 1] == '=':
return True
# Check &&, ||, &=, |=
elif code[index] in ['&', '|']:
if code[index + 1] == code[index] or code[index + 1] == '=':
return True
return False
def operator_helper(compound, code, index):
correct_spacing = True
if compound:
if code[index + 2] and code[index + 2] != '\r' and code[index + 2] != '\n' and code[index + 2] != ' ':
correct_spacing = False
if code[index - 1] and code[index - 1] != ' ':
correct_spacing = False
else:
if code[index + 1] and code[index + 1] != '\r' and code[index + 1] != '\n' and code[index + 1] != ' ':
correct_spacing = False
if code[index - 1] and code[index - 1] != ' ':
correct_spacing = False
return correct_spacing
def findOccurences(s, ch):
return [i for i, letter in enumerate(s) if letter == ch]
def check_equals_true(self, code):
keyword = Literal("true") | Literal("false")
statement_parser = Group("==" + keyword) | Group(keyword + "==")
if len(statement_parser.searchString(code)):
self.add_error(label="EQUALS_TRUE")
def check_goto(self, code):
# Hacky but gets the job done for now - has holes though
q_goto = re.compile('\".*goto.*\"')
r_goto = re.compile('(?:\s+|^|\{)goto\s+')
if r_goto.search(code) and not q_goto.search(code):
self.add_error(label="GOTO")
def erase_string(code):
# remove contents of literal strings
code = code.replace("\\\"", "") # remove escaped quotes
results = re.findall(r'"(.*?)"', code)
for string in results:
quote_mark = "\""
code = code.replace(quote_mark + string + quote_mark, "\"\"")
# remove contents of literal chars
code = code.replace('\\\\', '') # replace escaped backslash
code = code.replace("\\'", "") # remove escaped single quote
results = re.findall(r"'(.*?)'", code)
for string in results:
single_quote_mark = "'"
code = code.replace(single_quote_mark + string + single_quote_mark, "''")
return code
def check_define_statement(self, code):
q_define = re.compile('\".*(?:\s+|^)#\s*define\s+.*\"')
r_define = re.compile('(?:\s+|^)#\s*define\s+')
if r_define.search(code) and not q_define.search(code):
words = code.split()
# They shouldn't be using __MY_HEADER_H__ because __-names are
# reserved, but we'll allow it anyways.
legal_endings = ["_H", "_H__"]
if not any(words[-1].endswith(i) for i in legal_endings):
self.add_error(label="DEFINE_STATEMENT")
def check_continue(self, code):
# Hacky but gets the job done for now - has holes though
q_continue = re.compile('\".*continue.*\"')
r_continue = re.compile('(?:\s+|^|\{)continue\s*;')
if r_continue.search(code) and not q_continue.search(code):
self.add_error(label="CONTINUE_STATEMENT")
def check_ternary_operator(self, code):
q_ternary = re.compile('\".*\?.*\"')
r_ternary = re.compile('\?')
if r_ternary.search(code) and not q_ternary.search(code):
self.add_error(label="TERNARY_OPERATOR")
def check_while_true(self, code):
statement_parser = Literal("while") + Literal("(") + Literal("true") + Literal(")")
if len(statement_parser.searchString(code)):
self.add_error(label="WHILE_TRUE")
def check_non_const_global(self, code):
inside = Literal("int main")
if len(inside.searchString(code)):
self.outside_main = False
if self.outside_main:
function = check_if_function(code)
variables = variables = re.compile("^(?:\w|_)+\s+(?:\w|_|\[|\])+\s*=\s*.+;")
keywords = re.compile("^\s*(?:using|class|struct)")
constants = re.compile("^\s*(?:static\s+)?const")
if not function and variables.search(code) and \
not keywords.search(code) and \
not constants.search(code):
self.add_error(label="NON_CONST_GLOBAL")
def check_main_syntax(self, code):
# Return value for main is optional in C++11
parser = Literal("int") + Literal("main") + Literal("(") + SkipTo(Literal(")")) + Literal(")")
if len(parser.searchString(code)):
main_prefix = Literal("int") + Literal("main") + Literal("(")
full_use = Literal("int") + "argc" + "," + Optional("const") + "char" + "*" + "argv" + "[" + "]" + ")"
# 3 options for main() syntax
if not len((main_prefix + Literal(")")).searchString(code)) and \
not len((main_prefix + Literal("void") + Literal(")")).searchString(code)) and \
not len((main_prefix + full_use).searchString(code)):
self.add_error(label="MAIN_SYNTAX")
def check_first_char(self, code):
# check if the first char is lower-case alpha or '_'
lowercase = re.compile("(?:^|\s+)(?:class|struct)\s+(?:[a-z]|_)\w+")
bad_naming = lowercase.search(code)
if bad_naming:
result = bad_naming.group(0).split()
self.add_error(label="FIRST_CHAR",
data={"keyword": result[0],
"expected": str(result[1]).capitalize(),
"found": str(result[1])})
def check_unnecessary_include(self, code):
grammar = Literal('#') + Literal('include') + Literal('<') + Word(alphanums)
try:
grammar.parseString(code)
begin = code.find("<")
end = code.find(">")
included_library = code[begin + 1:end]
if included_library not in self.includes:
self.add_error(label="UNNECESSARY_INCLUDE")
except ParseException:
return
def check_local_include(self, code):
grammar = Literal('#') + Literal('include') + Literal('"') + Word(alphanums)
try:
grammar.parseString(code)
begin = code.find('"')
included_file = code[begin + 1:]
end = included_file.find('"')
included_file = included_file[:end]
if included_file not in self.includes:
self.local_includes[self.current_file].append(included_file)
except ParseException:
return
def check_for_loop_semicolon_spacing(self, code):
# Match the semicolons and any whitespace around them.
for_loop_regex = re.compile(
r"""
\s*for\s*\(
(?P<code1>[^;]*?)
(?P<semicolon1>\s*;\s*)
(?P<code2>[^;]*?)
(?P<semicolon2>\s*;\s*)
(?P<code3>[^;]*?)
\)
""",
re.VERBOSE
)
match = for_loop_regex.search(code)
if not match:
return
self.for_loop_spacing_before = getattr(self, "for_loop_spacing_before", None)
self.for_loop_spacing_after = getattr(self, "for_loop_spacing_after", None)
semicolon1 = match.group("semicolon1")
semicolon2 = match.group("semicolon2")
code1 = match.group("code1")
code2 = match.group("code2")
code3 = match.group("code3")
def is_spacing_okay(semicolon, before_code, after_code):
spacing_before = semicolon.startswith(" ")
spacing_after = semicolon.endswith(" ")
def check_spacing(convention, actual):
if convention is None:
convention = actual
if convention != actual:
return convention, False
else:
return convention, True
if before_code or after_code:
if before_code:
self.for_loop_spacing_before, result = check_spacing(
self.for_loop_spacing_before,
spacing_before
)
if not result:
return False
if after_code:
self.for_loop_spacing_after, result = check_spacing(
self.for_loop_spacing_after,
spacing_after
)
if not result:
return False
else:
# This is a plain semicolon, so we can't infer anything about the
# spacing convention.
pass
return True
if not (
is_spacing_okay(semicolon1, code1, code2)
and is_spacing_okay(semicolon2, code2, code3)
):
self.add_error(
label="FOR_LOOP_SEMICOLON_SPACING",
data={"line": self.current_line_num}
)