forked from Kitware/KWStyle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kwsCheckMemberFunctions.cxx
371 lines (334 loc) · 12.1 KB
/
kwsCheckMemberFunctions.cxx
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*=========================================================================
Program: KWStyle - Kitware Style Checker
Module: kwsCheckMemberFunctions.cxx
Copyright (c) Kitware, Inc. All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "kwsParser.h"
namespace kws {
/** Check if the member function implementation of the class are correct */
bool Parser::CheckMemberFunctions(const char* regEx,unsigned long maxLength)
{
m_TestsDone[MEMBERFUNCTION_REGEX] = true;
m_TestsDescription[MEMBERFUNCTION_REGEX] = "Member functions should match regular expression: ";
m_TestsDescription[MEMBERFUNCTION_REGEX] += regEx;
if(maxLength>0)
{
m_TestsDone[MEMBERFUNCTION_LENGTH] = true;
m_TestsDescription[MEMBERFUNCTION_LENGTH] = "Member functions must not exceed: ";
m_TestsDescription[MEMBERFUNCTION_LENGTH] += maxLength;
m_TestsDescription[MEMBERFUNCTION_LENGTH] += " lines";
}
// First we need to find the parameters
bool hasError = false;
kwssys::RegularExpression regex(regEx);
// Construct the list of classnames to check
std::vector<std::string> classes;
// We first read the .h if any and not a .h file
if(kwssys::SystemTools::GetFilenameExtension(m_Filename.c_str()) != ".h")
{
std::string headerfile = kwssys::SystemTools::GetFilenamePath(m_Filename.c_str());
//headerfile += "/";
headerfile += kwssys::SystemTools::GetFilenameWithoutExtension(m_Filename.c_str());
headerfile += ".h";
if(kwssys::SystemTools::FileExists(headerfile.c_str()))
{
// We open the file
std::ifstream file;
file.open(headerfile.c_str(), std::ios::binary | std::ios::in);
if(!file.is_open())
{
std::cout << "Cannot open file: " << headerfile.c_str() << std::endl;
return 1;
}
file.seekg(0,std::ios::end);
unsigned long fileSize = file.tellg();
file.seekg(0,std::ios::beg);
char* buf = new char[fileSize+1];
file.read(buf,fileSize);
buf[fileSize] = 0;
std::string buffer(buf);
buffer.resize(fileSize);
delete [] buf;
file.close();
this->ConvertBufferToWindowsFileType(buffer);
buffer = this->RemoveComments(buffer.c_str());
size_t classpos = this->GetClassPosition(0,buffer);
while(classpos!=std::string::npos)
{
std::string classname = this->FindPreviousWord(classpos,false,buffer);
if(buffer[classpos-1] != ' '
&& m_BufferNoComment[classpos-1] != '\n')
{
classname = "";
size_t i=classpos-1;
while(i!=std::string::npos && buffer[i] != ' ')
{
classname = buffer[i]+classname;
i--;
}
}
classes.push_back(classname);
classpos = this->GetClassPosition(classpos+1,buffer);
}
}
}
// Check the current file for any classes
// And check the name of the current files
size_t classpos = this->GetClassPosition(0);
while(classpos!=std::string::npos)
{
size_t current;
std::string memberFunction =
this->FindMemberFunction(m_BufferNoComment,classpos,
this->FindEndOfClass(classpos+1),current);
std::string classname = this->FindPreviousWord(classpos);
if(m_BufferNoComment[classpos-1] != ' '
&& m_BufferNoComment[classpos-1] != '\n'
)
{
classname = "";
size_t i=classpos-1;
while(i!=std::string::npos && m_BufferNoComment[i] != ' ')
{
classname = m_BufferNoComment[i]+classname;
i--;
}
}
std::string destructor = "~";
destructor += classname;
while(current!=std::string::npos)
{
// if the member function is a constructor or destructor we ignore
if(memberFunction != classname
&& memberFunction != destructor
)
{
if(!regex.find(memberFunction))
{
Error error;
error.line = this->GetLineNumber(current,true);
error.line2 = error.line;
error.number = MEMBERFUNCTION_REGEX;
error.description = "member function (" + memberFunction + ") doesn't match regular expression";
m_ErrorList.push_back(error);
hasError = true;
}
// Check the size of the current memberFunction
if(maxLength>0 && current!=std::string::npos)
{
size_t open = m_BufferNoComment.find("{",current);
size_t semicolon = m_BufferNoComment.find(";",current);
if(semicolon>open)
{
size_t close = this->FindClosingChar('{','}',open,true);
size_t lopen = this->GetLineNumber(open);
size_t lclose = this->GetLineNumber(close);
if((open!=std::string::npos) && (close!=std::string::npos) && (lclose-lopen>maxLength))
{
Error error;
error.line = this->GetLineNumber(current,true);
error.line2 = error.line;
error.number = MEMBERFUNCTION_LENGTH;
error.description = "function (" + memberFunction + ") has too many lines: ";
char* temp = new char[10];
sprintf(temp,"%zd",lclose-lopen);
error.description += temp;
error.description += " (";
sprintf(temp,"%ld",maxLength);
error.description += temp;
error.description += ")";
m_ErrorList.push_back(error);
hasError = true;
delete [] temp;
}
}
}
}
memberFunction = this->FindMemberFunction(m_BufferNoComment,current+1,this->FindEndOfClass(classpos+1),current);
}
classes.push_back(classname);
classpos = this->GetClassPosition(classpos+1);
}
// Do the checking for the implementation file
std::vector<std::string>::const_iterator it = classes.begin();
while(it != classes.end())
{
std::string classname = (*it);
// Search all the classname::
size_t pos = m_BufferNoComment.find(classname,0);
while(pos != std::string::npos)
{
// look for ::
size_t pos2 = m_BufferNoComment.find("::",pos);
bool valid = false;
if(pos2 != std::string::npos)
{
valid = true;
for(size_t i=pos+classname.size();i<pos2;i++)
{
if(m_BufferNoComment[i] != ' ' && m_BufferNoComment[i] != '\n' && m_BufferNoComment[i] != '\r')
{
valid = false;
break;
}
}
}
// if we have a match we look for the name
if(valid)
{
size_t i=pos2+2;
while(i<m_BufferNoComment.size() &&
(m_BufferNoComment[i] == ' '
|| m_BufferNoComment[i] == '\n'
|| m_BufferNoComment[i] == '\r'))
{
i++;
}
size_t pos3 = m_BufferNoComment.find("(",i);
if(pos3 != std::string::npos)
{
std::string functionName = m_BufferNoComment.substr(i,pos3-i);
std::string destructor = "~";
destructor += classname;
std::string functionLine = this->GetLine(this->GetLineNumber(i,true)-1);
// if the member function is a constructor or destructor we ignore
if(functionName != classname
&& functionName != destructor
&& functionLine.find("typedef ") == std::string::npos
)
{
if(!regex.find(functionName))
{
Error error;
error.line = this->GetLineNumber(i,true);
error.line2 = error.line;
error.number = MEMBERFUNCTION_REGEX;
error.description = "function (" + functionName + ") doesn't match regular expression";
m_ErrorList.push_back(error);
hasError = true;
}
if(maxLength>0)
{
size_t open = m_BufferNoComment.find("{",i);
size_t close = this->FindClosingChar('{','}',open,true);
size_t lopen = this->GetLineNumber(open);
size_t lclose = this->GetLineNumber(close);
if((open!=std::string::npos) && (close!=std::string::npos)
&& (lclose-lopen>maxLength))
{
Error error;
error.line = this->GetLineNumber(pos3,true);
error.line2 = error.line;
error.number = MEMBERFUNCTION_LENGTH;
error.description = "function (" + functionName + ") has too many lines: ";
char* temp = new char[10];
sprintf(temp,"%zd",lclose-lopen);
error.description += temp;
error.description += " (";
sprintf(temp,"%ld",maxLength);
error.description += temp;
error.description += ")";
m_ErrorList.push_back(error);
hasError = true;
delete [] temp;
}
}
}
}
}
pos = m_BufferNoComment.find(classname,pos+1);
}
it++;
}
return !hasError;
}
/** Find the first member function in the source code
* Member functions are defined as int myfunction(); */
std::string Parser::FindMemberFunction(std::string & buffer, size_t start, size_t end,size_t& pos)
{
size_t posSemicolon = buffer.find(";",start);
while(posSemicolon != std::string::npos && posSemicolon<end)
{
// We check that we don't have the keyword __attribute__
std::string line = this->GetLine(this->GetLineNumber(posSemicolon,true)-1);
if((line.find("_attribute_") != std::string::npos)
|| (line.find(" operator") != std::string::npos)
)
{
posSemicolon = buffer.find(";",posSemicolon+1);
continue;
}
// We try to find a (
size_t i=posSemicolon-1;
bool inFunction = true;
while(i != std::string::npos && i>=start && inFunction)
{
if(buffer[i] == ')')
{
size_t close = this->FindClosingChar(')','(',i,true);
if(close>0 && !this->IsInFunction(close,buffer.c_str()))
{
i = close;
inFunction = false;
break;
}
}
i--;
}
pos = i-1;
// If we have a match we extract the word
if(pos != std::string::npos && pos>start && pos<end)
{
std::string functionName = "";
bool inWord = false;
size_t index=pos;
for(;index!=std::string::npos && index>start;index--)
{
if(buffer[index] != ' ' && buffer[index] != '\t'
&& buffer[index] != '\r' && buffer[index] != '\n' && buffer[index] != '*' && buffer[index] != '&')
{
inWord = true;
functionName = buffer[index]+functionName;
}
else if(inWord)
{
break;
}
}
// Check that this is not a #define (tricky)
std::string functionLine = this->GetLine(this->GetLineNumber(i,true)-1);
if(functionLine.find("#define") == std::string::npos
&& functionLine.find("_attribute_") == std::string::npos
&& functionLine.find(" operator") == std::string::npos
&& functionLine.find("friend ") == std::string::npos)
{
// If we have a class definition: Test():Base
// we return the correct
size_t posf = functionName.find("(",0);
if(posf != std::string::npos)
{
functionName = functionName.substr(0,posf);
}
posf = functionName.find(":",0);
if(posf != std::string::npos)
{
functionName = functionName.substr(0,posf);
}
// If the function name is void we skip because it means
// this is a member variable function (see bug 5086)
if(functionName != "void")
{
return functionName;
}
}
}
posSemicolon = buffer.find(";",posSemicolon+1);
}
pos = std::string::npos;
return "";
}
} // end namespace kws