-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathRULE_4_1_E_align_conditions.py
122 lines (105 loc) · 3.02 KB
/
RULE_4_1_E_align_conditions.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
"""
Align condition list in 'if' and 'while' clause if they are splitted in multiple lines.
All conditions should be aligned in the same column with the first condition.
== Violation ==
if (a == b &&
a == c) <== Violation
== Good ==
if (a == b &&
a == c) <== OK!
"""
from nsiqcppstyle_reporter import *
from nsiqcppstyle_rulehelper import *
from nsiqcppstyle_rulemanager import *
from nsiqunittest.nsiqcppstyle_unittestbase import *
def RunRule(lexer, contextStack):
t = lexer.GetCurToken()
if t.type in ("IF", "WHILE"):
t2 = lexer.GetNextTokenSkipWhiteSpaceAndCommentAndPreprocess()
if t2 is not None and t2.type == "LPAREN":
rparen = lexer.GetNextMatchingToken(True)
firstElement = lexer.PeekNextTokenSkipWhiteSpaceAndCommentAndPreprocess()
firstElementLineNo = firstElement.lineno
firstElementColumn = GetRealColumn(firstElement)
while True:
t3 = lexer.GetNextTokenSkipWhiteSpaceAndCommentAndPreprocess()
if t3 is None or t3 == rparen:
break
if firstElementLineNo != t3.lineno:
firstElementLineNo = t3.lineno
if firstElementColumn != GetRealColumn(t3):
nsiqcppstyle_reporter.Error(
t3,
__name__,
"Incorrect align on condition list '%s'. It should be aligned in column %d. "
% (t3.value, firstElementColumn),
)
ruleManager.AddFunctionScopeRule(RunRule)
##########################################################################
# Unit Test
##########################################################################
class testRule(nct):
def setUpRule(self):
ruleManager.AddFunctionScopeRule(RunRule)
def test1(self):
self.Analyze(
"test/thisFile.c",
"""
void function(int k, int j, int pp)
{
if (AA == D &&
kK = 22) {
}
}
""",
)
self.ExpectError(__name__)
def test2(self):
self.Analyze(
"test/thisFile.c",
"""
void function(int k, int j, int pp)
{
if (AA == D &&
kK = 22) {
}
}
""",
)
self.ExpectSuccess(__name__)
def test3(self):
self.Analyze(
"test/thisFile.c",
"""
void function(int k, int j, int pp)
{
while (AA == D &&
kK = 22) {
}
}
""",
)
self.ExpectError(__name__)
def test4(self):
self.Analyze(
"test/thisFile.c",
"""
while (AA == D &&
kK = 22) {
}
""",
)
self.ExpectSuccess(__name__)
def test5(self):
self.Analyze(
"test/thisFile.c",
"""
void F() {
while (AA == D &&
kK = 22
) {
}
}
""",
)
self.ExpectSuccess(__name__)