Debian Codegraph @

kwsCheckInternalVariables.cxx

raw
1 /*=========================================================================
2
3 Program: KWStyle - Kitware Style Checker
4 Module: kwsCheckInternalVariables.cxx
5
6 Copyright (c) Kitware, Inc. All rights reserved.
7 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
8
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notices for more information.
12
13 =========================================================================*/
14 #include "kwsParser.h"
15
16 namespace kws {
17
18 /** Check if the internal variables of the class are correct */
19 bool Parser::CheckInternalVariables(const char* regEx,bool alignment,bool checkProtected)
20 {
21 if(checkProtected)
22 {
23 m_TestsDone[IVAR_PUBLIC] = true;
24 m_TestsDescription[IVAR_PUBLIC] = "ivars should be in the protected or private section";
25 }
26
27 m_TestsDone[IVAR_REGEX] = true;
28 m_TestsDescription[IVAR_REGEX] = "ivars should match regular expression: ";
29 m_TestsDescription[IVAR_REGEX] += regEx;
30
31 if(alignment)
32 {
33 m_TestsDone[IVAR_ALIGN] = true;
34 m_TestsDescription[IVAR_ALIGN] = "ivars should be aligned with previous ivars ";
35 }
36
37 // First we need to find the parameters
38 // float myParam;
39 bool hasError = false;
40
41 kwssys::RegularExpression regex(regEx);
42
43 // We loop through the classes
44 size_t classPosBegin = this->GetClassPosition(0);
45 while(classPosBegin != std::string::npos)
46 {
47 // First we check in the public area
48 size_t publicFirst;
49 size_t publicLast;
50 this->FindPublicArea(publicFirst,publicLast,classPosBegin);
51
52 size_t previousline = 0;
53 size_t previouspos = 0;
54
55 size_t pos = publicFirst;
56 while(pos!= std::string::npos)
57 {
58 std::string var = this->FindInternalVariable(pos+1,publicLast,pos);
59 if (var.empty()) {
60 continue;
61 }
62
63 if(this->IsInStruct(pos) || this->IsInUnion(pos))
64 {
65 continue;
66 }
67
68 if(var.length() > 0)
69 {
70 if(checkProtected)
71 {
72 Error error;
73 error.line = this->GetLineNumber(pos,true);
74 error.line2 = error.line;
75 error.number = IVAR_PUBLIC;
76 error.description = "Encapsulation not preserved";
77 m_ErrorList.push_back(error);
78 hasError = true;
79 }
80
81 // Check the alignment if specified
82 if(alignment)
83 {
84 // Find the position in the line
85 auto posvar = static_cast<unsigned long>(
86 m_BufferNoComment.find(var, pos - var.size() - 2));
87 auto l = static_cast<unsigned long>(this->GetPositionInLine(posvar));
88 unsigned long line = this->GetLineNumber(pos,true);
89
90 // if the typedef is on a line close to the previous one we check
91 if(line-previousline<2)
92 {
93 if(l!=previouspos)
94 {
95 Error error;
96 error.line = this->GetLineNumber(pos,true);
97 error.line2 = error.line;
98 error.number = IVAR_ALIGN;
99 error.description = "Internal variable (" + var + ") is not aligned with the previous one";
100 m_ErrorList.push_back(error);
101 hasError = true;
102 }
103 }
104 else
105 {
106 previouspos = l;
107 }
108 previousline = line;
109 } // end alignement
110
111 if(!regex.find(var))
112 {
113 Error error;
114 error.line = this->GetLineNumber(pos,true);
115 error.line2 = error.line;
116 error.number = IVAR_REGEX;
117 error.description = "Internal variable (" + var + ") doesn't match regular expression (";
118 error.description += regEx;
119 error.description += ")";
120 m_ErrorList.push_back(error);
121 hasError = true;
122 }
123 }
124 }
125
126 // Second in the protected area
127 size_t protectedFirst;
128 size_t protectedLast;
129 this->FindProtectedArea(protectedFirst,protectedLast);
130 pos = protectedFirst;
131
132 previousline = 0;
133 previouspos = 0;
134
135 while(pos!=std::string::npos)
136 {
137 std::string var = this->FindInternalVariable(pos+1,protectedLast,pos);
138
139 if (var.empty()) {
140 continue;
141 }
142
143 if(this->IsInStruct(pos) || this->IsInUnion(pos))
144 {
145 continue;
146 }
147
148 if(var.length() > 0)
149 {
150 // Check the alignment if specified
151 if(alignment)
152 {
153 // Find the position in the line
154 size_t posvar = m_BufferNoComment.find(var,pos-var.size()-2);
155 size_t l = this->GetPositionInLine(posvar);
156 size_t line = this->GetLineNumber(pos,true);
157
158 // if the typedef is on a line close to the previous one we check
159 if(line-previousline<2)
160 {
161 if(l!=previouspos)
162 {
163 Error error;
164 error.line = this->GetLineNumber(pos,true);
165 error.line2 = error.line;
166 error.number = IVAR_ALIGN;
167 error.description = "Internal variable (" + var + ") is not aligned with the previous one";
168 m_ErrorList.push_back(error);
169 hasError = true;
170 }
171 }
172 else
173 {
174 previouspos = l;
175 }
176 previousline = line;
177 } // end alignement
178
179 if(!regex.find(var))
180 {
181 Error error;
182 error.line = this->GetLineNumber(pos,true);
183 error.line2 = error.line;
184 error.number = IVAR_REGEX;
185 error.description = "Internal variable (" + var + ") doesn't match regular expression (";
186 error.description += regEx;
187 error.description += ")";
188 m_ErrorList.push_back(error);
189 hasError = true;
190 }
191 }
192 }
193
194 // Third and last in the private area
195 size_t privateFirst;
196 size_t privateLast;
197 this->FindPrivateArea(privateFirst,privateLast);
198 pos = privateFirst;
199 previousline = 0;
200 previouspos = 0;
201 while(pos != std::string::npos)
202 {
203 std::string var = this->FindInternalVariable(pos+1,privateLast,pos);
204 if (var.empty()) {
205 continue;
206 }
207
208 if(this->IsInStruct(pos) || this->IsInUnion(pos))
209 {
210 continue;
211 }
212
213 if(var.length() > 0)
214 {
215 // Check the alignment if specified
216 if(alignment)
217 {
218 // Find the position in the line
219 size_t posvar = m_BufferNoComment.find(var,pos-var.size()-2);
220 size_t l = this->GetPositionInLine(posvar);
221 size_t line = this->GetLineNumber(pos,true);
222
223 // if the typedef is on a line close to the previous one we check
224 if(line-previousline<2)
225 {
226 if(l!=previouspos)
227 {
228 Error error;
229 error.line = this->GetLineNumber(pos,true);
230 error.line2 = error.line;
231 error.number = IVAR_ALIGN;
232 error.description = "Internal variable (" + var + ") is not aligned with the previous one";
233 m_ErrorList.push_back(error);
234 hasError = true;
235 }
236 }
237 else
238 {
239 previouspos = l;
240 }
241 previousline = line;
242 } // end alignement
243
244 if(!regex.find(var))
245 {
246 Error error;
247 error.line = this->GetLineNumber(pos,true);
248 error.line2 = error.line;
249 error.number = IVAR_REGEX;
250 error.description = "Internal variable (" + var + ") doesn't match regular expression (";
251 error.description += regEx;
252 error.description += ")";
253 m_ErrorList.push_back(error);
254 hasError = true;
255 }
256 }
257 }
258
259 classPosBegin = this->GetClassPosition(classPosBegin+1);
260
261 } // End loop class pos
262
263 return !hasError;
264 }
265
266 /** Find the first ivar in the source code */
267 std::string Parser::FindInternalVariable(size_t start, size_t end,size_t & pos)
268 {
269 size_t posSemicolon = m_BufferNoComment.find(";",start);
270 while(posSemicolon != std::string::npos && posSemicolon<end)
271 {
272 // We try to find the word before that
273 unsigned long i=static_cast<unsigned long>(posSemicolon)-1;
274 bool inWord = true;
275 bool first = false;
276 std::string ivar = "";
277 while(i != std::string::npos && inWord)
278 {
279 if((m_BufferNoComment[i] != ' ')
280 && (m_BufferNoComment[i] != '\t')
281 )
282 {
283 if((m_BufferNoComment[i] == '}')
284 || (m_BufferNoComment[i] == ')')
285 || (m_BufferNoComment[i] == ']')
286 || (m_BufferNoComment[i] == '\n')
287 )
288 {
289 inWord = false;
290 }
291 else
292 {
293 std::string store = ivar;
294 ivar = m_BufferNoComment[i];
295 ivar += store;
296 inWord = true;
297 first = true;
298 }
299 }
300 else // we have a space
301 {
302 if(first)
303 {
304 inWord = false;
305 }
306 }
307 i--;
308 }
309 pos = posSemicolon;
310 // We extract the complete definition.
311 // This means that we look for a '{' or '}' or '{' or ':'
312 // but not '::'
313 while(i != std::string::npos)
314 {
315 if(m_BufferNoComment[i] == ';'
316 || m_BufferNoComment[i] == '}'
317 )
318 {
319 break;
320 }
321 else if(m_BufferNoComment[i] == ':')
322 {
323 if((m_BufferNoComment[i-1] != ':') && (m_BufferNoComment[i+1] != ':'))
324 {
325 break;
326 }
327 }
328 i--;
329 }
330
331 bool isenum = false;
332 // If we have a '}' we check that this is not an enum definition
333 // or a typedef
334 if(m_BufferNoComment[i] == '}')
335 {
336
337 // Find the opening char
338 size_t j = this->FindOpeningChar('}','{',i,true);
339 // Find a semicolon before that
340 while(j>0)
341 {
342 if(m_BufferNoComment[j] == ';')
343 {
344 break;
345 }
346 j--;
347 }
348
349 if(m_BufferNoComment.substr(j+1,i-j-1).find("enum") != std::string::npos)
350 {
351 isenum = true;
352 }
353 else if(m_BufferNoComment.substr(j+1,i-j-1).find("typedef") != std::string::npos)
354 {
355 isenum = true;
356 }
357 }
358
359 std::string subphrase = "";
360 subphrase = m_BufferNoComment.substr(i+1,posSemicolon-i-1);
361
362 if( (subphrase.find("=") == std::string::npos)
363 && (subphrase.find("(") == std::string::npos)
364 && (subphrase.find("typedef") == std::string::npos)
365 && (subphrase.find("}") == std::string::npos)
366 && (subphrase.find("friend") == std::string::npos)
367 && (subphrase.find("class") == std::string::npos)
368 && (subphrase.find("return") == std::string::npos)
369 && (subphrase.find("extern") == std::string::npos)
370 && (subphrase.find("\"") == std::string::npos)
371 && (subphrase.find("<<") == std::string::npos)
372 && !isenum
373 )
374 {
375 // Check that we are not inside a function(){}
376 if(!this->IsInFunction(posSemicolon))
377 {
378 // We check if any * is present and strip
379 // the work
380 size_t posstar = ivar.find_last_of("*");
381 if(posstar != std::string::npos)
382 {
383 ivar = ivar.substr(posstar+1,ivar.size()-posstar-1);
384 }
385 return ivar;
386 }
387 }
388 posSemicolon = m_BufferNoComment.find(";",posSemicolon+1);
389 }
390
391 pos = std::string::npos;
392 return "";
393 }
394
395 } // end namespace kws