This repository was archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNonConstGlobalChecker.h
236 lines (213 loc) · 5.36 KB
/
NonConstGlobalChecker.h
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
#ifndef NON_CONST_GLOBAL_CHECKER_H_
#define NON_CONST_GLOBAL_CHECKER_H_
#include <string>
#include <stack>
#include <algorithm>
#include "Token.h"
#ifdef DEBUG
#include <iostream>
#endif
struct GlobalsMachineData {
int firstBadLine;
int currLineNum;
std::string currWS;
int namespaceDepth;
int braceDepth;
std::vector<Lossy::Token> currNormalLine;
GlobalsMachineData()
: firstBadLine(-1),
currLineNum(1),
currWS("\n"),
namespaceDepth(0),
braceDepth (0) {}
int badLineNum() {
return firstBadLine;
}
void checkForNonConst() {
std::vector<Lossy::Token>::const_iterator begin = currNormalLine.cbegin();
std::vector<Lossy::Token>::const_iterator end = currNormalLine.cend();
if (std::find(begin, end, Lossy::CONST_T) == end
&& std::find(begin, end, Lossy::CONSTEXPR_T) == end
&& std::find(begin, end, Lossy::USING_T) == end
&& std::find(begin, end, Lossy::TYPEDEF_T) == end
&& !isAFunctionHeadLine(currNormalLine)) {
firstBadLine = currLineNum;
}
}
void stepWS(Lossy::Token t) {
if (t == Lossy::NEWLINE_T) {
currLineNum++;
}
}
};
#ifdef DEBUG
const char* nonConstCheckerStateToStr(int s) {
switch (s) {
case 0:
return "START_S";
case 1:
return "STRUCTURE_START_LINE_S";
case 2:
return "STRUCTURE_BODY_S";
case 3:
return "STRUCTURE_CLOSE_LINE_S";
case 4:
return "NORMAL_LINE_S";
case 5:
return "FUNC_BODY_S";
case 6:
return "BRACES_IN_NORMAL_LINE_S";
case 7:
return "NAMESPACE_START_LINE_S";
case 8:
return "PREPROC_LINE_S";
default:
return "??";
}
}
#endif
int getFirstNonConstGlobalLine(const std::vector<Lossy::Token>& tokens) {
GlobalsMachineData data;
enum State {
START_S,
STRUCTURE_START_LINE_S,
STRUCTURE_BODY_S,
STRUCTURE_CLOSE_LINE_S,
NORMAL_LINE_S,
FUNC_BODY_S,
BRACES_IN_NORMAL_LINE_S,
NAMESPACE_START_LINE_S,
PREPROC_LINE_S,
} currState = START_S;
for (Lossy::Token t : tokens) {
//printf("State:%s, Token:%s\n", nonConstCheckerStateToStr(currState), tokenToStr(t));
switch (currState) {
case START_S:
switch (t) {
case Lossy::SINGLE_SPACE_T:
case Lossy::TAB_T:
case Lossy::NEWLINE_T:
break;//just needed to stepws
case Lossy::CLASS_T:
case Lossy::STRUCT_T:
case Lossy::UNION_T:
case Lossy::ENUM_T:
currState = STRUCTURE_START_LINE_S;
break;
case Lossy::OCTOTHORPE_T:
currState = PREPROC_LINE_S;
break;
case Lossy::NAMESPACE_T:
currState = NAMESPACE_START_LINE_S;
break;
case Lossy::R_BRACE_T:
data.namespaceDepth--;
break;
case Lossy::EOF_T:
break; //do nothing
case Lossy::CASE_T:
case Lossy::DEFAULT_T:
case Lossy::PUBLIC_T:
case Lossy::PRIVATE_T:
case Lossy::PROTECTED_T:
case Lossy::SEMICOLON_T:
case Lossy::L_BRACE_T:
case Lossy::ELSE_T:
case Lossy::SWITCH_T:
case Lossy::CATCH_T:
case Lossy::IF_T:
case Lossy::WHILE_T:
case Lossy::FOR_T:
case Lossy::DO_T:
case Lossy::TRY_T:
case Lossy::L_PAREN_T:
case Lossy::R_PAREN_T:
case Lossy::SINGLE_COLON_T:
case Lossy::OTHER_STUFF_T:
default: //shouldn't happen, but same as above
data.currNormalLine.clear();
data.currNormalLine.push_back(t);
currState = NORMAL_LINE_S;
break;
}
break;
case STRUCTURE_START_LINE_S:
if (t == Lossy::L_BRACE_T) {
data.braceDepth = 1;
currState = STRUCTURE_BODY_S;
}
break;
case STRUCTURE_BODY_S:
if (t == Lossy::L_BRACE_T) {
data.braceDepth++;
}
else if (t == Lossy::R_BRACE_T) {
data.braceDepth--;
if (data.braceDepth == 0) {
currState = STRUCTURE_CLOSE_LINE_S;
}
}
break;
case STRUCTURE_CLOSE_LINE_S:
if (t == Lossy::SEMICOLON_T) {
currState = START_S;
}
break;
case NORMAL_LINE_S:
data.currNormalLine.push_back(t);
if (t == Lossy::SEMICOLON_T) {
currState = START_S;
data.checkForNonConst();
if (data.badLineNum() != -1) {
return data.badLineNum();
}
}
else if (t == Lossy::L_BRACE_T) {
data.braceDepth = 1;
if (isBraceInLineFunctionOpen(data.currNormalLine)) {
currState = FUNC_BODY_S;
}
else {
currState = BRACES_IN_NORMAL_LINE_S;
}
}
break;
case FUNC_BODY_S:
if (t == Lossy::L_BRACE_T) {
data.braceDepth++;
}
else if (t == Lossy::R_BRACE_T) {
data.braceDepth--;
if (data.braceDepth == 0) {
currState = START_S;
}
}
break;
case BRACES_IN_NORMAL_LINE_S:
if (t == Lossy::L_BRACE_T) {
data.braceDepth++;
}
else if (t == Lossy::R_BRACE_T) {
data.braceDepth--;
if (data.braceDepth == 0) {
currState = NORMAL_LINE_S;
}
}
break;
case NAMESPACE_START_LINE_S:
if (t == Lossy::L_BRACE_T) {
data.namespaceDepth++;
currState = START_S;
}
break;
case PREPROC_LINE_S:
if (t == Lossy::NEWLINE_T) {
currState = START_S;
}
break;
}
data.stepWS(t);
}
return data.badLineNum();
}
#endif