| 1 |
/*========================================================================= |
| 2 |
|
| 3 |
Program: KWStyle - Kitware Style Checker |
| 4 |
Module: kwsCheckMemberFunctions.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 member function implementation of the class are correct */ |
| 19 |
bool Parser::CheckMemberFunctions(const char* regEx,unsigned long maxLength) |
| 20 |
{ |
| 21 |
m_TestsDone[MEMBERFUNCTION_REGEX] = true; |
| 22 |
m_TestsDescription[MEMBERFUNCTION_REGEX] = "Member functions should match regular expression: "; |
| 23 |
m_TestsDescription[MEMBERFUNCTION_REGEX] += regEx; |
| 24 |
|
| 25 |
if(maxLength>0) |
| 26 |
{ |
| 27 |
m_TestsDone[MEMBERFUNCTION_LENGTH] = true; |
| 28 |
m_TestsDescription[MEMBERFUNCTION_LENGTH] = "Member functions must not exceed: "; |
| 29 |
m_TestsDescription[MEMBERFUNCTION_LENGTH] += std::to_string(maxLength); |
| 30 |
m_TestsDescription[MEMBERFUNCTION_LENGTH] += " lines"; |
| 31 |
} |
| 32 |
|
| 33 |
// First we need to find the parameters |
| 34 |
bool hasError = false; |
| 35 |
|
| 36 |
kwssys::RegularExpression regex(regEx); |
| 37 |
|
| 38 |
// Construct the list of classnames to check |
| 39 |
std::vector<std::string> classes; |
| 40 |
|
| 41 |
// We first read the .h if any and not a .h file |
| 42 |
if(kwssys::SystemTools::GetFilenameExtension(m_Filename.c_str()) != ".h") |
| 43 |
{ |
| 44 |
std::string headerfile = kwssys::SystemTools::GetFilenamePath(m_Filename.c_str()); |
| 45 |
//headerfile += "/"; |
| 46 |
headerfile += kwssys::SystemTools::GetFilenameWithoutExtension(m_Filename.c_str()); |
| 47 |
headerfile += ".h"; |
| 48 |
|
| 49 |
if(kwssys::SystemTools::FileExists(headerfile.c_str())) |
| 50 |
{ |
| 51 |
// We open the file |
| 52 |
std::ifstream file; |
| 53 |
file.open(headerfile.c_str(), std::ios::binary | std::ios::in); |
| 54 |
if(!file.is_open()) |
| 55 |
{ |
| 56 |
std::cout << "Cannot open file: " << headerfile.c_str() << std::endl; |
| 57 |
return true; |
| 58 |
} |
| 59 |
|
| 60 |
file.seekg(0,std::ios::end); |
| 61 |
unsigned long fileSize = file.tellg(); |
| 62 |
file.seekg(0,std::ios::beg); |
| 63 |
|
| 64 |
char* buf = new char[fileSize+1]; |
| 65 |
file.read(buf,fileSize); |
| 66 |
buf[fileSize] = 0; |
| 67 |
std::string buffer(buf); |
| 68 |
buffer.resize(fileSize); |
| 69 |
delete [] buf; |
| 70 |
|
| 71 |
file.close(); |
| 72 |
|
| 73 |
this->ConvertBufferToWindowsFileType(buffer); |
| 74 |
buffer = this->RemoveComments(buffer.c_str()); |
| 75 |
|
| 76 |
size_t classpos = this->GetClassPosition(0,buffer); |
| 77 |
while(classpos!=std::string::npos) |
| 78 |
{ |
| 79 |
std::string classname = this->FindPreviousWord(classpos,false,buffer); |
| 80 |
if(buffer[classpos-1] != ' ' |
| 81 |
&& m_BufferNoComment[classpos-1] != '\n') |
| 82 |
{ |
| 83 |
classname = ""; |
| 84 |
size_t i=classpos-1; |
| 85 |
while(i!=std::string::npos && buffer[i] != ' ') |
| 86 |
{ |
| 87 |
classname = buffer[i]+classname; |
| 88 |
i--; |
| 89 |
} |
| 90 |
} |
| 91 |
classes.push_back(classname); |
| 92 |
classpos = this->GetClassPosition(classpos+1,buffer); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
// Check the current file for any classes |
| 98 |
// And check the name of the current files |
| 99 |
size_t classpos = this->GetClassPosition(0); |
| 100 |
while(classpos!=std::string::npos) |
| 101 |
{ |
| 102 |
size_t current; |
| 103 |
std::string memberFunction = |
| 104 |
this->FindMemberFunction(m_BufferNoComment,classpos, |
| 105 |
this->FindEndOfClass(classpos+1),current); |
| 106 |
|
| 107 |
std::string classname = this->FindPreviousWord(classpos); |
| 108 |
|
| 109 |
if(m_BufferNoComment[classpos-1] != ' ' |
| 110 |
&& m_BufferNoComment[classpos-1] != '\n' |
| 111 |
) |
| 112 |
{ |
| 113 |
classname = ""; |
| 114 |
size_t i=classpos-1; |
| 115 |
while(i!=std::string::npos && m_BufferNoComment[i] != ' ') |
| 116 |
{ |
| 117 |
classname = m_BufferNoComment[i]+classname; |
| 118 |
i--; |
| 119 |
} |
| 120 |
} |
| 121 |
std::string destructor = "~"; |
| 122 |
destructor += classname; |
| 123 |
|
| 124 |
while(current!=std::string::npos) |
| 125 |
{ |
| 126 |
// if the member function is a constructor or destructor we ignore |
| 127 |
if(memberFunction != classname |
| 128 |
&& memberFunction != destructor |
| 129 |
) |
| 130 |
{ |
| 131 |
if(!regex.find(memberFunction)) |
| 132 |
{ |
| 133 |
Error error; |
| 134 |
error.line = this->GetLineNumber(current,true); |
| 135 |
error.line2 = error.line; |
| 136 |
error.number = MEMBERFUNCTION_REGEX; |
| 137 |
error.description = "member function (" + memberFunction + ") doesn't match regular expression"; |
| 138 |
m_ErrorList.push_back(error); |
| 139 |
hasError = true; |
| 140 |
} |
| 141 |
|
| 142 |
// Check the size of the current memberFunction |
| 143 |
if(maxLength>0 && current!=std::string::npos) |
| 144 |
{ |
| 145 |
size_t open = m_BufferNoComment.find("{",current); |
| 146 |
size_t semicolon = m_BufferNoComment.find(";",current); |
| 147 |
if(semicolon>open) |
| 148 |
{ |
| 149 |
size_t close = this->FindClosingChar('{','}',open,true); |
| 150 |
size_t lopen = this->GetLineNumber(open); |
| 151 |
size_t lclose = this->GetLineNumber(close); |
| 152 |
if((open!=std::string::npos) && (close!=std::string::npos) && (lclose-lopen>maxLength)) |
| 153 |
{ |
| 154 |
Error error; |
| 155 |
error.line = this->GetLineNumber(current,true); |
| 156 |
error.line2 = error.line; |
| 157 |
error.number = MEMBERFUNCTION_LENGTH; |
| 158 |
error.description = "function (" + memberFunction + ") has too many lines: "; |
| 159 |
constexpr size_t length = 10; |
| 160 |
char* temp = new char[length]; |
| 161 |
snprintf(temp,length,"%zd",lclose-lopen); |
| 162 |
error.description += temp; |
| 163 |
error.description += " ("; |
| 164 |
snprintf(temp,length,"%ld",maxLength); |
| 165 |
error.description += temp; |
| 166 |
error.description += ")"; |
| 167 |
m_ErrorList.push_back(error); |
| 168 |
hasError = true; |
| 169 |
delete [] temp; |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
memberFunction = this->FindMemberFunction(m_BufferNoComment,current+1,this->FindEndOfClass(classpos+1),current); |
| 175 |
} |
| 176 |
classes.push_back(classname); |
| 177 |
classpos = this->GetClassPosition(classpos+1); |
| 178 |
} |
| 179 |
|
| 180 |
// Do the checking for the implementation file |
| 181 |
std::vector<std::string>::const_iterator it = classes.begin(); |
| 182 |
while(it != classes.end()) |
| 183 |
{ |
| 184 |
std::string classname = (*it); |
| 185 |
|
| 186 |
// Search all the classname:: |
| 187 |
size_t pos = m_BufferNoComment.find(classname,0); |
| 188 |
while(pos != std::string::npos) |
| 189 |
{ |
| 190 |
// look for :: |
| 191 |
size_t pos2 = m_BufferNoComment.find("::",pos); |
| 192 |
bool valid = false; |
| 193 |
if(pos2 != std::string::npos) |
| 194 |
{ |
| 195 |
valid = true; |
| 196 |
for(size_t i=pos+classname.size();i<pos2;i++) |
| 197 |
{ |
| 198 |
if(m_BufferNoComment[i] != ' ' && m_BufferNoComment[i] != '\n' && m_BufferNoComment[i] != '\r') |
| 199 |
{ |
| 200 |
valid = false; |
| 201 |
break; |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
// if we have a match we look for the name |
| 207 |
if(valid) |
| 208 |
{ |
| 209 |
size_t i=pos2+2; |
| 210 |
while(i<m_BufferNoComment.size() && |
| 211 |
(m_BufferNoComment[i] == ' ' |
| 212 |
|| m_BufferNoComment[i] == '\n' |
| 213 |
|| m_BufferNoComment[i] == '\r')) |
| 214 |
{ |
| 215 |
i++; |
| 216 |
} |
| 217 |
|
| 218 |
size_t pos3 = m_BufferNoComment.find("(",i); |
| 219 |
if(pos3 != std::string::npos) |
| 220 |
{ |
| 221 |
std::string functionName = m_BufferNoComment.substr(i,pos3-i); |
| 222 |
std::string destructor = "~"; |
| 223 |
destructor += classname; |
| 224 |
|
| 225 |
std::string functionLine = this->GetLine(this->GetLineNumber(i,true)-1); |
| 226 |
|
| 227 |
// if the member function is a constructor or destructor we ignore |
| 228 |
if(functionName != classname |
| 229 |
&& functionName != destructor |
| 230 |
&& functionLine.find("typedef ") == std::string::npos |
| 231 |
) |
| 232 |
{ |
| 233 |
if(!regex.find(functionName)) |
| 234 |
{ |
| 235 |
Error error; |
| 236 |
error.line = this->GetLineNumber(i,true); |
| 237 |
error.line2 = error.line; |
| 238 |
error.number = MEMBERFUNCTION_REGEX; |
| 239 |
error.description = "function (" + functionName + ") doesn't match regular expression"; |
| 240 |
m_ErrorList.push_back(error); |
| 241 |
hasError = true; |
| 242 |
} |
| 243 |
|
| 244 |
if(maxLength>0) |
| 245 |
{ |
| 246 |
size_t open = m_BufferNoComment.find("{",i); |
| 247 |
size_t close = this->FindClosingChar('{','}',open,true); |
| 248 |
size_t lopen = this->GetLineNumber(open); |
| 249 |
size_t lclose = this->GetLineNumber(close); |
| 250 |
if((open!=std::string::npos) && (close!=std::string::npos) |
| 251 |
&& (lclose-lopen>maxLength)) |
| 252 |
{ |
| 253 |
Error error; |
| 254 |
error.line = this->GetLineNumber(pos3,true); |
| 255 |
error.line2 = error.line; |
| 256 |
error.number = MEMBERFUNCTION_LENGTH; |
| 257 |
error.description = "function (" + functionName + ") has too many lines: "; |
| 258 |
constexpr size_t length = 10; |
| 259 |
char* temp = new char[length]; |
| 260 |
snprintf(temp,length,"%zd",lclose-lopen); |
| 261 |
error.description += temp; |
| 262 |
error.description += " ("; |
| 263 |
snprintf(temp,length,"%ld",maxLength); |
| 264 |
error.description += temp; |
| 265 |
error.description += ")"; |
| 266 |
m_ErrorList.push_back(error); |
| 267 |
hasError = true; |
| 268 |
delete [] temp; |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
pos = m_BufferNoComment.find(classname,pos+1); |
| 275 |
} |
| 276 |
it++; |
| 277 |
} |
| 278 |
|
| 279 |
return !hasError; |
| 280 |
} |
| 281 |
|
| 282 |
/** Find the first member function in the source code |
| 283 |
* Member functions are defined as int myfunction(); */ |
| 284 |
std::string Parser::FindMemberFunction(std::string & buffer, size_t start, size_t end,size_t& pos) |
| 285 |
{ |
| 286 |
size_t posSemicolon = buffer.find(";",start); |
| 287 |
while(posSemicolon != std::string::npos && posSemicolon<end) |
| 288 |
{ |
| 289 |
// We check that we don't have the keyword __attribute__ |
| 290 |
std::string line = this->GetLine(this->GetLineNumber(posSemicolon,true)-1); |
| 291 |
if((line.find("_attribute_") != std::string::npos) |
| 292 |
|| (line.find(" operator") != std::string::npos) |
| 293 |
) |
| 294 |
{ |
| 295 |
posSemicolon = buffer.find(";",posSemicolon+1); |
| 296 |
continue; |
| 297 |
} |
| 298 |
|
| 299 |
// We try to find a ( |
| 300 |
size_t i=posSemicolon-1; |
| 301 |
bool inFunction = true; |
| 302 |
while(i != std::string::npos && i>=start && inFunction) |
| 303 |
{ |
| 304 |
if(buffer[i] == ')') |
| 305 |
{ |
| 306 |
size_t close = this->FindClosingChar(')','(',i,true); |
| 307 |
if(close>0 && !this->IsInFunction(close,buffer.c_str())) |
| 308 |
{ |
| 309 |
i = close; |
| 310 |
inFunction = false; |
| 311 |
break; |
| 312 |
} |
| 313 |
} |
| 314 |
i--; |
| 315 |
} |
| 316 |
|
| 317 |
pos = i-1; |
| 318 |
|
| 319 |
// If we have a match we extract the word |
| 320 |
if(pos != std::string::npos && pos>start && pos<end) |
| 321 |
{ |
| 322 |
std::string functionName = ""; |
| 323 |
bool inWord = false; |
| 324 |
size_t index=pos; |
| 325 |
for(;index!=std::string::npos && index>start;index--) |
| 326 |
{ |
| 327 |
if(buffer[index] != ' ' && buffer[index] != '\t' |
| 328 |
&& buffer[index] != '\r' && buffer[index] != '\n' && buffer[index] != '*' && buffer[index] != '&') |
| 329 |
{ |
| 330 |
inWord = true; |
| 331 |
functionName = buffer[index]+functionName; |
| 332 |
} |
| 333 |
else if(inWord) |
| 334 |
{ |
| 335 |
break; |
| 336 |
} |
| 337 |
} |
| 338 |
// Check that this is not a #define (tricky) |
| 339 |
std::string functionLine = this->GetLine(this->GetLineNumber(i,true)-1); |
| 340 |
if(functionLine.find("#define") == std::string::npos |
| 341 |
&& functionLine.find("_attribute_") == std::string::npos |
| 342 |
&& functionLine.find(" operator") == std::string::npos |
| 343 |
&& functionLine.find("friend ") == std::string::npos) |
| 344 |
{ |
| 345 |
// If we have a class definition: Test():Base |
| 346 |
// we return the correct |
| 347 |
size_t posf = functionName.find("(",0); |
| 348 |
if(posf != std::string::npos) |
| 349 |
{ |
| 350 |
functionName = functionName.substr(0,posf); |
| 351 |
} |
| 352 |
posf = functionName.find(":",0); |
| 353 |
if(posf != std::string::npos) |
| 354 |
{ |
| 355 |
functionName = functionName.substr(0,posf); |
| 356 |
} |
| 357 |
|
| 358 |
// If the function name is void we skip because it means |
| 359 |
// this is a member variable function (see bug 5086) |
| 360 |
if(functionName != "void") |
| 361 |
{ |
| 362 |
return functionName; |
| 363 |
} |
| 364 |
} |
| 365 |
} |
| 366 |
posSemicolon = buffer.find(";",posSemicolon+1); |
| 367 |
} |
| 368 |
|
| 369 |
pos = std::string::npos; |
| 370 |
return ""; |
| 371 |
} |
| 372 |
|
| 373 |
} // end namespace kws |