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 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
|
/*=========================================================================
Program: KWStyle - Kitware Style Checker
Module: kwsCheckInternalVariables.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 internal variables of the class are correct */
bool Parser::CheckInternalVariables(const char* regEx,bool alignment,bool checkProtected)
{
if(checkProtected)
{
m_TestsDone[IVAR_PUBLIC] = true;
m_TestsDescription[IVAR_PUBLIC] = "ivars should be in the protected or private section";
}
m_TestsDone[IVAR_REGEX] = true;
m_TestsDescription[IVAR_REGEX] = "ivars should match regular expression: ";
m_TestsDescription[IVAR_REGEX] += regEx;
if(alignment)
{
m_TestsDone[IVAR_ALIGN] = true;
m_TestsDescription[IVAR_ALIGN] = "ivars should be aligned with previous ivars ";
}
// First we need to find the parameters
// float myParam;
bool hasError = false;
kwssys::RegularExpression regex(regEx);
// We loop through the classes
size_t classPosBegin = this->GetClassPosition(0);
while(classPosBegin != std::string::npos)
{
// First we check in the public area
size_t publicFirst;
size_t publicLast;
this->FindPublicArea(publicFirst,publicLast,classPosBegin);
size_t previousline = 0;
size_t previouspos = 0;
size_t pos = publicFirst;
while(pos!= std::string::npos)
{
std::string var = this->FindInternalVariable(pos+1,publicLast,pos);
if(var == "")
{
continue;
}
if(this->IsInStruct(pos) || this->IsInUnion(pos))
{
continue;
}
if(var.length() > 0)
{
if(checkProtected)
{
Error error;
error.line = this->GetLineNumber(pos,true);
error.line2 = error.line;
error.number = IVAR_PUBLIC;
error.description = "Encapsulation not preserved";
m_ErrorList.push_back(error);
hasError = true;
}
// Check the alignment if specified
if(alignment)
{
// Find the position in the line
unsigned long posvar = static_cast<unsigned long>(m_BufferNoComment.find(var,pos-var.size()-2));
unsigned long l = static_cast<unsigned long>(this->GetPositionInLine(posvar));
unsigned long line = this->GetLineNumber(pos,true);
// if the typedef is on a line close to the previous one we check
if(line-previousline<2)
{
if(l!=previouspos)
{
Error error;
error.line = this->GetLineNumber(pos,true);
error.line2 = error.line;
error.number = IVAR_ALIGN;
error.description = "Internal variable (" + var + ") is not aligned with the previous one";
m_ErrorList.push_back(error);
hasError = true;
}
}
else
{
previouspos = l;
}
previousline = line;
} // end alignement
if(!regex.find(var))
{
Error error;
error.line = this->GetLineNumber(pos,true);
error.line2 = error.line;
error.number = IVAR_REGEX;
error.description = "Internal variable (" + var + ") doesn't match regular expression (";
error.description += regEx;
error.description += ")";
m_ErrorList.push_back(error);
hasError = true;
}
}
}
// Second in the protected area
size_t protectedFirst;
size_t protectedLast;
this->FindProtectedArea(protectedFirst,protectedLast);
pos = protectedFirst;
previousline = 0;
previouspos = 0;
while(pos!=std::string::npos)
{
std::string var = this->FindInternalVariable(pos+1,protectedLast,pos);
if(var == "")
{
continue;
}
if(this->IsInStruct(pos) || this->IsInUnion(pos))
{
continue;
}
if(var.length() > 0)
{
// Check the alignment if specified
if(alignment)
{
// Find the position in the line
size_t posvar = m_BufferNoComment.find(var,pos-var.size()-2);
size_t l = this->GetPositionInLine(posvar);
size_t line = this->GetLineNumber(pos,true);
// if the typedef is on a line close to the previous one we check
if(line-previousline<2)
{
if(l!=previouspos)
{
Error error;
error.line = this->GetLineNumber(pos,true);
error.line2 = error.line;
error.number = IVAR_ALIGN;
error.description = "Internal variable (" + var + ") is not aligned with the previous one";
m_ErrorList.push_back(error);
hasError = true;
}
}
else
{
previouspos = l;
}
previousline = line;
} // end alignement
if(!regex.find(var))
{
Error error;
error.line = this->GetLineNumber(pos,true);
error.line2 = error.line;
error.number = IVAR_REGEX;
error.description = "Internal variable (" + var + ") doesn't match regular expression (";
error.description += regEx;
error.description += ")";
m_ErrorList.push_back(error);
hasError = true;
}
}
}
// Third and last in the private area
size_t privateFirst;
size_t privateLast;
this->FindPrivateArea(privateFirst,privateLast);
pos = privateFirst;
previousline = 0;
previouspos = 0;
while(pos != std::string::npos)
{
std::string var = this->FindInternalVariable(pos+1,privateLast,pos);
if(var == "")
{
continue;
}
if(this->IsInStruct(pos) || this->IsInUnion(pos))
{
continue;
}
if(var.length() > 0)
{
// Check the alignment if specified
if(alignment)
{
// Find the position in the line
size_t posvar = m_BufferNoComment.find(var,pos-var.size()-2);
size_t l = this->GetPositionInLine(posvar);
size_t line = this->GetLineNumber(pos,true);
// if the typedef is on a line close to the previous one we check
if(line-previousline<2)
{
if(l!=previouspos)
{
Error error;
error.line = this->GetLineNumber(pos,true);
error.line2 = error.line;
error.number = IVAR_ALIGN;
error.description = "Internal variable (" + var + ") is not aligned with the previous one";
m_ErrorList.push_back(error);
hasError = true;
}
}
else
{
previouspos = l;
}
previousline = line;
} // end alignement
if(!regex.find(var))
{
Error error;
error.line = this->GetLineNumber(pos,true);
error.line2 = error.line;
error.number = IVAR_REGEX;
error.description = "Internal variable (" + var + ") doesn't match regular expression (";
error.description += regEx;
error.description += ")";
m_ErrorList.push_back(error);
hasError = true;
}
}
}
classPosBegin = this->GetClassPosition(classPosBegin+1);
} // End loop class pos
return !hasError;
}
/** Find the first ivar in the source code */
std::string Parser::FindInternalVariable(size_t start, size_t end,size_t & pos)
{
size_t posSemicolon = m_BufferNoComment.find(";",start);
while(posSemicolon != std::string::npos && posSemicolon<end)
{
// We try to find the word before that
unsigned long i=static_cast<unsigned long>(posSemicolon)-1;
bool inWord = true;
bool first = false;
std::string ivar = "";
while(i != std::string::npos && inWord)
{
if((m_BufferNoComment[i] != ' ')
&& (m_BufferNoComment[i] != '\t')
)
{
if((m_BufferNoComment[i] == '}')
|| (m_BufferNoComment[i] == ')')
|| (m_BufferNoComment[i] == ']')
|| (m_BufferNoComment[i] == '\n')
)
{
inWord = false;
}
else
{
std::string store = ivar;
ivar = m_BufferNoComment[i];
ivar += store;
inWord = true;
first = true;
}
}
else // we have a space
{
if(first)
{
inWord = false;
}
}
i--;
}
pos = posSemicolon;
// We extract the complete definition.
// This means that we look for a '{' or '}' or '{' or ':'
// but not '::'
while(i != std::string::npos)
{
if(m_BufferNoComment[i] == ';'
|| m_BufferNoComment[i] == '}'
)
{
break;
}
else if(m_BufferNoComment[i] == ':')
{
if((m_BufferNoComment[i-1] != ':') && (m_BufferNoComment[i+1] != ':'))
{
break;
}
}
i--;
}
bool isenum = false;
// If we have a '}' we check that this is not an enum definition
// or a typedef
if(m_BufferNoComment[i] == '}')
{
// Find the opening char
size_t j = this->FindOpeningChar('}','{',i,true);
// Find a semicolon before that
while(j>0)
{
if(m_BufferNoComment[j] == ';')
{
break;
}
j--;
}
if(m_BufferNoComment.substr(j+1,i-j-1).find("enum") != std::string::npos)
{
isenum = true;
}
else if(m_BufferNoComment.substr(j+1,i-j-1).find("typedef") != std::string::npos)
{
isenum = true;
}
}
std::string subphrase = "";
subphrase = m_BufferNoComment.substr(i+1,posSemicolon-i-1);
if( (subphrase.find("=") == std::string::npos)
&& (subphrase.find("(") == std::string::npos)
&& (subphrase.find("typedef") == std::string::npos)
&& (subphrase.find("}") == std::string::npos)
&& (subphrase.find("friend") == std::string::npos)
&& (subphrase.find("class") == std::string::npos)
&& (subphrase.find("return") == std::string::npos)
&& (subphrase.find("extern") == std::string::npos)
&& (subphrase.find("\"") == std::string::npos)
&& (subphrase.find("<<") == std::string::npos)
&& !isenum
)
{
// Check that we are not inside a function(){}
if(!this->IsInFunction(posSemicolon))
{
// We check if any * is present and strip
// the work
size_t posstar = ivar.find_last_of("*");
if(posstar != std::string::npos)
{
ivar = ivar.substr(posstar+1,ivar.size()-posstar-1);
}
return ivar;
}
}
posSemicolon = m_BufferNoComment.find(";",posSemicolon+1);
}
pos = std::string::npos;
return "";
}
} // end namespace kws
|