-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathRULE_4_2_A_A_space_around_operator.py
335 lines (290 loc) · 8.39 KB
/
RULE_4_2_A_A_space_around_operator.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
335
"""
Provide the space before and after operators.
There are variation to provide spaces.
In the binary operator.
Spaces should be provided before and after the operator.
For example, +, /, %....
In the unary operator, Spaces should be provided before or after the operator.
However, when it's used in the (AA++), [--BB], {--KK}. It's OK not to provide spaces.
In the some operators(",", ";"), the spaces should be provided after the operator.
== Violation ==
for (a;b;c) <== Violation. It should be (a; b; c)
Hello(a,b,c) <== Violation. It should be (a, b, c)
int k = 2+3; <== Violation. It should be 2 + 3
c+++c; <== Violation. It should be c++ + c
== Good ==
int k = (2 + 3); <== OK. It's ok not to provide spaces before and after [, (, {
int k = -2; <== OK. Minus can be used to indicate minus value.
Therefore This rule doesn't care about it.
for (a; b; c) {} <== OK
Hello(a, b, c); <== OK
tt[c++] <== OK. This rule doesn't care about the unary operator is used in the [ ( [
"""
from nsiqcppstyle_reporter import *
from nsiqcppstyle_rulehelper import *
from nsiqcppstyle_rulemanager import *
from nsiqunittest.nsiqcppstyle_unittestbase import *
operator = (
"PLUS",
"DIVIDE",
"MODULO",
"OR",
"LSHIFT",
"LOR",
"LAND",
"LE",
"GE",
"EQ",
"EQUALS",
"TIMESEQUAL",
"DIVEQUAL",
"MODEQUAL",
"PLUSEQUAL",
"MINUSEQUAL",
"LSHIFTEQUAL",
"RSHIFTEQUAL",
"ANDEQUAL",
"XOREQUAL",
"OREQUAL",
"SPACESHIP",
)
nextoperator = (
"SEMI",
"COMMA",
)
unaryoperator = ("PLUSPLUS", "MINUSMINUS")
def RunRule(lexer, contextStack):
t = lexer.GetCurToken()
if t.type in operator:
t2 = lexer.PeekNextToken()
t3 = lexer.PeekPrevToken()
t4 = lexer.PeekPrevTokenSkipWhiteSpaceAndCommentAndPreprocess()
if t2 is not None and t3 is not None and (t4 is None or t4.type != "FUNCTION"):
if t.pp is True and t.type == "DIVIDE":
return
if t2.type not in ["SPACE", "LINEFEED", "PREPROCESSORNEXT"] or t3.type not in ["SPACE", "LINEFEED"]:
t3 = lexer.GetPrevTokenSkipWhiteSpaceAndComment()
if t3 is not None and t3.type != "OPERATOR" and not Match(r"^\w*#include", t.line):
nsiqcppstyle_reporter.Error(t, __name__, f"Provide spaces b/w operator '{t.value}'")
elif t.type in nextoperator:
t2 = lexer.PeekNextToken()
if (
t2 is not None
and t2.type not in ["SPACE", "LINEFEED", "PREPROCESSORNEXT"]
and not Match(r"^\w*#include", t.line)
):
nsiqcppstyle_reporter.Error(t, __name__, f"Provide spaces after operator '{t.value}'")
elif t.type in unaryoperator:
t2 = lexer.PeekPrevToken()
t3 = lexer.PeekNextToken()
t4 = lexer.PeekPrevTokenSkipWhiteSpaceAndCommentAndPreprocess()
if Match(r"^\w*#include", t.line):
return
if (
t3 is not None
and t3.type == "ID"
and t2.type
not in [
"COMMA",
"OPERATOR",
"SPACE",
"LINEFEED",
"LBRACE",
"LPAREN",
"LBRACKET",
]
and t3.type not in ["SEMI", "SPACE", "LINEFEED", "RBRACE", "RPAREN", "RBRACKET"]
):
nsiqcppstyle_reporter.Error(t, __name__, f"Provide spaces before operator '{t.value}'")
if (
t2 is not None
and t2.type == "ID"
and t3.type
not in [
"COMMA",
"OPERATOR",
"SPACE",
"LINEFEED",
"RBRACE",
"RPAREN",
"RBRACKET",
]
and t3.type not in ["SEMI", "SPACE", "LINEFEED", "RBRACE", "RPAREN", "RBRACKET"]
):
nsiqcppstyle_reporter.Error(t, __name__, f"Provide spaces after operator '{t.value}'")
ruleManager.AddRule(RunRule)
ruleManager.AddPreprocessRule(RunRule)
##########################################################################
# Unit Test
##########################################################################
class testRule(nct):
def setUpRule(self):
ruleManager.AddRule(RunRule)
ruleManager.AddPreprocessRule(RunRule)
def test1(self):
self.Analyze(
"test/thisFile.c",
"""
int *a;
void operator=(EWE) {
HELLO = ewe << 3;
TEST <= 3;
TEST < 3;
TEST | C;
TEST & C;
A != 3;
t = a++;
}
""",
)
self.ExpectSuccess(__name__)
def test2(self):
self.Analyze(
"test/thisFile.c",
"""
(DD +ww);
""",
)
self.ExpectError(__name__)
def test3(self):
self.Analyze(
"test/thisFile.c",
"""
HELLO = ewe <<3;
""",
)
self.ExpectError(__name__)
def test4(self):
self.Analyze(
"test/thisFile.c",
"""
HELLo = TET ||B;
""",
)
self.ExpectError(__name__)
def test5(self):
self.Analyze("test/thisFile.c", "#define KK(dsd) TET ||B;")
self.ExpectError(__name__)
def test6(self):
self.Analyze("test/thisFile.c", "k = &b;")
self.ExpectSuccess(__name__)
def test7(self):
self.Analyze("test/thisFile.c", "k=b;")
self.ExpectError(__name__)
def test8(self):
self.Analyze("test/thisFile.c", "k|= b;")
self.ExpectError(__name__)
def test9(self):
self.Analyze("test/thisFile.c", "k++c;")
self.ExpectError(__name__)
def test10(self):
self.Analyze("test/thisFile.c", "#include <h/ds>")
self.ExpectSuccess(__name__)
def test11(self):
self.Analyze("test/thisFile.c", "hash ^= hash << 4;")
self.ExpectSuccess(__name__)
def test12(self):
self.Analyze(
"test/thisFile.c",
"""
#define KK() ewee;\\
hash ^= hash << 4;
""",
)
self.ExpectSuccess(__name__)
def test13(self):
self.Analyze(
"test/thisFile.c",
"""
#define KK() ewee;\\
hash ^= hash<<4;
""",
)
self.ExpectError(__name__)
def test14(self):
self.Analyze(
"test/thisFile.c",
"""
#include <magic++.h>
""",
)
self.ExpectSuccess(__name__)
def test15(self):
self.Analyze(
"test/thisFile.c",
"""
m_mTabCommand.SetAt(nId++, p##TabName##TabCommand);
""",
)
self.ExpectSuccess(__name__)
def test16(self):
self.Analyze(
"test/thisFile.c",
"""
m_mTabCommand.SetAt(++nId, p##TabName##TabCommand);
m_mTabCommand.SetAt(nId++dd);
""",
)
self.ExpectError(__name__)
def test17(self):
self.Analyze(
"test/thisFile.c",
"""
string k = "k=b %s";
""",
)
self.ExpectSuccess(__name__)
def test18(self):
self.Analyze(
"test/thisFile.c",
"""
sprintf(l_szConfigPath, ""
"print%log");
""",
)
self.ExpectSuccess(__name__)
def test19(self):
self.Analyze(
"test/thisFile.c",
r"""
sprintf(l_szConfigPath, "\\"
"print"
wewewe
wewe);
wewe
"ewewe"
""",
)
self.ExpectSuccess(__name__)
def testSpaceshipOperatorOK(self):
self.Analyze(
"test/thisFile.c",
"""
const bool isEq = std::is_eq(a <=> b);
""",
)
self.ExpectSuccess(__name__)
def testSpaceshipOperatorKOLeft(self):
self.Analyze(
"test/thisFile.c",
"""
const bool isEq = std::is_eq(a<=> b);
""",
)
self.ExpectError(__name__)
def testSpaceshipOperatorKORight(self):
self.Analyze(
"test/thisFile.c",
"""
const bool isEq = std::is_eq(a <=>b);
""",
)
self.ExpectError(__name__)
def testSpaceshipOperatorKOBoth(self):
self.Analyze(
"test/thisFile.c",
"""
const bool isEq = std::is_eq(a<=>b);
""",
)
self.ExpectError(__name__)