| 1 |
/*========================================================================= |
| 2 |
|
| 3 |
Program: KWStyle - Kitware Style Checker |
| 4 |
Module: kwsParser.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 |
#include <stdlib.h> // atoi |
| 17 |
#include <string.h> |
| 18 |
|
| 19 |
#include "kwsPushBoostWarnings.h" |
| 20 |
#include <boost/xpressive/xpressive.hpp> |
| 21 |
#include "kwsPopBoostWarnings.h" |
| 22 |
|
| 23 |
using namespace boost::xpressive; |
| 24 |
|
| 25 |
namespace kws { |
| 26 |
|
| 27 |
/** Constructor */ |
| 28 |
Parser::Parser() |
| 29 |
{ |
| 30 |
m_HeaderFilename = ""; |
| 31 |
m_Filename = ""; |
| 32 |
m_FixFile = false; |
| 33 |
m_FixedPositions.clear(); |
| 34 |
|
| 35 |
for(unsigned int i=0;i<NUMBER_ERRORS;i++) |
| 36 |
{ |
| 37 |
m_TestsDone[i] = false; |
| 38 |
m_TestsDescription[i] += "NA"; |
| 39 |
} |
| 40 |
|
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
/** To be able to use std::sort we provide the < operator */ |
| 45 |
bool Parser::operator<(const Parser& a) const |
| 46 |
{ |
| 47 |
return !m_Filename.compare(a.m_Filename); |
| 48 |
} |
| 49 |
|
| 50 |
/** Given the name of the check to perform and the default value perform the check */ |
| 51 |
bool Parser::Check(const char* name, const char* value) |
| 52 |
{ |
| 53 |
if(!strcmp(name,"LineLength")) |
| 54 |
{ |
| 55 |
this->CheckLineLength(std::stoul(value)); |
| 56 |
return true; |
| 57 |
} |
| 58 |
else if(m_Positions.empty()) |
| 59 |
{ |
| 60 |
this->CheckLineLength(0,false,false); // run CheckLineLength without errors to fill m_Positions |
| 61 |
} |
| 62 |
if(!strcmp(name,"DeclarationOrder")) |
| 63 |
{ |
| 64 |
this->CheckDeclarationOrder(std::stoul(&value[0]),std::stoul(&value[2]),std::stoul(&value[4])); |
| 65 |
return true; |
| 66 |
} |
| 67 |
if(!strcmp(name,"Typedefs")) |
| 68 |
{ |
| 69 |
bool alignment = true; // check alignment by default |
| 70 |
std::string val = value; |
| 71 |
size_t pos = val.find(",",0); |
| 72 |
if(pos != std::string::npos) |
| 73 |
{ |
| 74 |
std::string v1 = val.substr(0,pos); |
| 75 |
|
| 76 |
// Check the alignment |
| 77 |
size_t pos1 = val.find(",",pos+1); |
| 78 |
|
| 79 |
std::string v2 = ""; |
| 80 |
if(pos1 == std::string::npos) |
| 81 |
{ |
| 82 |
v2 = val.substr(pos+1,val.size()-pos-1); |
| 83 |
} |
| 84 |
else |
| 85 |
{ |
| 86 |
v2 = val.substr(pos+1,pos1-pos-1); |
| 87 |
} |
| 88 |
|
| 89 |
if(!strcmp(v2.c_str(),"false") || !strcmp(v2.c_str(),"0")) |
| 90 |
{ |
| 91 |
alignment = false; |
| 92 |
} |
| 93 |
val = v1; |
| 94 |
} |
| 95 |
|
| 96 |
this->CheckTypedefs(val.c_str(),alignment); |
| 97 |
return true; |
| 98 |
} |
| 99 |
if(!strcmp(name,"InternalVariables")) |
| 100 |
{ |
| 101 |
bool alignment = true; // check alignment by default |
| 102 |
bool checkProtected = false; // check protected by default |
| 103 |
std::string val = value; |
| 104 |
size_t pos = val.find(",",0); |
| 105 |
if(pos != std::string::npos) |
| 106 |
{ |
| 107 |
std::string v1 = val.substr(0,pos); |
| 108 |
|
| 109 |
// Check the alignment |
| 110 |
size_t pos1 = val.find(",",pos+1); |
| 111 |
|
| 112 |
std::string v2 = ""; |
| 113 |
if(pos1 == std::string::npos) |
| 114 |
{ |
| 115 |
v2 = val.substr(pos+1,val.size()-pos-1); |
| 116 |
} |
| 117 |
else |
| 118 |
{ |
| 119 |
v2 = val.substr(pos+1,pos1-pos-1); |
| 120 |
} |
| 121 |
|
| 122 |
if(!strcmp(v2.c_str(),"false") || !strcmp(v2.c_str(),"0")) |
| 123 |
{ |
| 124 |
alignment = false; |
| 125 |
} |
| 126 |
|
| 127 |
if(pos1 != std::string::npos) |
| 128 |
{ |
| 129 |
// Check the protected |
| 130 |
pos = val.find(",",pos1+1); |
| 131 |
std::string v3 = ""; |
| 132 |
if(pos == std::string::npos) |
| 133 |
{ |
| 134 |
v3 = val.substr(pos1+1,val.size()-pos1-1); |
| 135 |
} |
| 136 |
else |
| 137 |
{ |
| 138 |
v3 = val.substr(pos1+1,pos-pos1-1); |
| 139 |
} |
| 140 |
|
| 141 |
if(!strcmp(v3.c_str(),"true") || !strcmp(v3.c_str(),"1")) |
| 142 |
{ |
| 143 |
checkProtected = true; |
| 144 |
} |
| 145 |
} |
| 146 |
val = v1; |
| 147 |
} |
| 148 |
|
| 149 |
this->CheckInternalVariables(val.c_str(),alignment,checkProtected); |
| 150 |
|
| 151 |
return true; |
| 152 |
} |
| 153 |
if(!strcmp(name,"Variables")) |
| 154 |
{ |
| 155 |
this->CheckVariables(value); |
| 156 |
return true; |
| 157 |
} |
| 158 |
if(!strcmp(name,"Struct")) |
| 159 |
{ |
| 160 |
this->CheckStruct(value); |
| 161 |
return true; |
| 162 |
} |
| 163 |
if(!strcmp(name,"MemberFunctions")) |
| 164 |
{ |
| 165 |
std::string val = value; |
| 166 |
std::string v1 = value; |
| 167 |
std::string v2 = "0"; |
| 168 |
size_t pos = val.find(",",0); |
| 169 |
if(pos != std::string::npos) |
| 170 |
{ |
| 171 |
v1 = val.substr(0,pos); |
| 172 |
v2 = val.substr(pos+1,val.size()-pos-1); |
| 173 |
} |
| 174 |
this->CheckMemberFunctions(v1.c_str(),std::stoul(v2.c_str())); |
| 175 |
return true; |
| 176 |
} |
| 177 |
if(!strcmp(name,"Functions")) |
| 178 |
{ |
| 179 |
std::string val = value; |
| 180 |
std::string v1 = value; |
| 181 |
std::string v2 = "0"; |
| 182 |
size_t pos = val.find(",",0); |
| 183 |
if(pos != std::string::npos) |
| 184 |
{ |
| 185 |
v1 = val.substr(0,pos); |
| 186 |
v2 = val.substr(pos+1,val.size()-pos-1); |
| 187 |
} |
| 188 |
this->CheckFunctions(v1.c_str(),std::stoul(v2.c_str())); |
| 189 |
return true; |
| 190 |
} |
| 191 |
if(!strcmp(name,"SemicolonSpace")) |
| 192 |
{ |
| 193 |
this->CheckSemicolonSpace(std::stoul(value)); |
| 194 |
return true; |
| 195 |
} |
| 196 |
if(!strcmp(name,"EndOfFileNewLine")) |
| 197 |
{ |
| 198 |
this->CheckEndOfFileNewLine(); |
| 199 |
return true; |
| 200 |
} |
| 201 |
if(!strcmp(name,"Tabs")) |
| 202 |
{ |
| 203 |
this->CheckTabs(); |
| 204 |
return true; |
| 205 |
} |
| 206 |
if(!strcmp(name,"Spaces")) |
| 207 |
{ |
| 208 |
this->CheckExtraSpaces(std::stoul(value)); |
| 209 |
return true; |
| 210 |
} |
| 211 |
if(!strcmp(name,"StatementPerLine")) |
| 212 |
{ |
| 213 |
std::string val = value; |
| 214 |
std::string v1 = value; |
| 215 |
size_t pos = val.find(",",0); |
| 216 |
bool checkInlineFunctions = true; |
| 217 |
if(pos != std::string::npos) |
| 218 |
{ |
| 219 |
v1 = val.substr(0,pos); |
| 220 |
std::string v2 = val.substr(pos+1,val.size()-pos-1); |
| 221 |
if(v2 == "0") |
| 222 |
{ |
| 223 |
checkInlineFunctions = false; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
this->CheckStatementPerLine(std::stoul(v1.c_str()),checkInlineFunctions); |
| 228 |
return true; |
| 229 |
} |
| 230 |
if(!strcmp(name,"BadCharacters")) |
| 231 |
{ |
| 232 |
this->CheckBadCharacters(!strcmp(value,"true")); |
| 233 |
return true; |
| 234 |
} |
| 235 |
if(!strcmp(name,"VariablePerLine")) |
| 236 |
{ |
| 237 |
this->CheckVariablePerLine(std::stoul(value)); |
| 238 |
return true; |
| 239 |
} |
| 240 |
if(!strcmp(name,"Comments")) |
| 241 |
{ |
| 242 |
std::string val = value; |
| 243 |
size_t pos = val.find(",",0); |
| 244 |
if(pos == std::string::npos) |
| 245 |
{ |
| 246 |
std::cout << "Comments not defined correctly" << std::endl; |
| 247 |
return false; |
| 248 |
} |
| 249 |
std::string v1 = val.substr(0,pos); |
| 250 |
auto pos1 = val.find(",", pos + 1); |
| 251 |
if(pos1 == std::string::npos) |
| 252 |
{ |
| 253 |
std::cout << "Comments not defined correctly" << std::endl; |
| 254 |
return false; |
| 255 |
} |
| 256 |
std::string v2 = val.substr(pos+1,pos1-pos-1); |
| 257 |
pos = val.find(",",pos1+1); |
| 258 |
if(pos == std::string::npos) |
| 259 |
{ |
| 260 |
std::cout << "Comments not defined correctly" << std::endl; |
| 261 |
return false; |
| 262 |
} |
| 263 |
std::string v3 = val.substr(pos1+1,pos-pos1-1); |
| 264 |
|
| 265 |
// Check if we allow empty lines before /class |
| 266 |
pos1 = val.find(",",pos+1); |
| 267 |
std::string v4 = ""; |
| 268 |
if(pos1 == std::string::npos) |
| 269 |
{ |
| 270 |
v4 = val.substr(pos+1,val.length()-pos-1); |
| 271 |
} |
| 272 |
else |
| 273 |
{ |
| 274 |
v4 = val.substr(pos+1,pos1-pos-1); |
| 275 |
} |
| 276 |
|
| 277 |
bool allowEmptyLine = !strcmp(v4.c_str(),"true"); |
| 278 |
|
| 279 |
// Check if we should check the comments misspeling |
| 280 |
pos = val.find(",",pos1+1); |
| 281 |
std::string v5 = ""; |
| 282 |
if(pos == std::string::npos) |
| 283 |
{ |
| 284 |
v5 = val.substr(pos1+1,val.length()-pos1-1); |
| 285 |
} |
| 286 |
else |
| 287 |
{ |
| 288 |
v5 = val.substr(pos1+1,pos-pos1-1); |
| 289 |
} |
| 290 |
|
| 291 |
bool checkWrongComment = true; |
| 292 |
if(!strcmp(v5.c_str(),"false")) |
| 293 |
{ |
| 294 |
checkWrongComment = false; |
| 295 |
} |
| 296 |
|
| 297 |
// Check if we should check the missing comments |
| 298 |
pos1 = val.find(",",pos+1); |
| 299 |
std::string v6 = ""; |
| 300 |
if(pos == std::string::npos) |
| 301 |
{ |
| 302 |
v6 = val.substr(pos+1,val.length()-pos-1); |
| 303 |
} |
| 304 |
else |
| 305 |
{ |
| 306 |
v6 = val.substr(pos+1,pos1-pos-1); |
| 307 |
} |
| 308 |
|
| 309 |
bool checkMissingComment = true; |
| 310 |
if(!strcmp(v6.c_str(),"false")) |
| 311 |
{ |
| 312 |
checkMissingComment = false; |
| 313 |
} |
| 314 |
|
| 315 |
this->CheckComments(v1.c_str(),v2.c_str(),v3.c_str(),allowEmptyLine,checkWrongComment,checkMissingComment); |
| 316 |
|
| 317 |
return true; |
| 318 |
} |
| 319 |
// should be before CheckIndent |
| 320 |
else if(!strcmp(name,"Header")) |
| 321 |
{ |
| 322 |
std::string val = value; |
| 323 |
size_t pos = val.find(",",0); |
| 324 |
if(pos == std::string::npos) |
| 325 |
{ |
| 326 |
std::cout << "Header not defined correctly" << std::endl; |
| 327 |
return false; |
| 328 |
} |
| 329 |
|
| 330 |
std::string v1 = ""; |
| 331 |
if(pos>0) |
| 332 |
{ |
| 333 |
v1 = val.substr(0,pos); |
| 334 |
} |
| 335 |
|
| 336 |
auto pos1 = val.find(",", pos + 1); |
| 337 |
if (pos1 == std::string::npos) { |
| 338 |
std::cout << "Header not defined correctly" << std::endl; |
| 339 |
return false; |
| 340 |
} |
| 341 |
|
| 342 |
std::string v2 = val.substr(pos+1,pos1-pos-1); |
| 343 |
std::string v3 = val.substr(pos1+1,val.length()-pos1-1); |
| 344 |
|
| 345 |
bool spaceEndOfLine = false; |
| 346 |
bool useCVS = false; |
| 347 |
|
| 348 |
if(!strcmp(v2.c_str(),"true")) |
| 349 |
{ |
| 350 |
spaceEndOfLine = true; |
| 351 |
} |
| 352 |
if(!strcmp(v3.c_str(),"true")) |
| 353 |
{ |
| 354 |
useCVS = true; |
| 355 |
} |
| 356 |
|
| 357 |
if (!v1.empty()) { |
| 358 |
this->CheckHeader(v1.c_str(), spaceEndOfLine, useCVS); |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
else if(!strcmp(name,"Indent")) |
| 363 |
{ |
| 364 |
std::string val = value; |
| 365 |
size_t pos = val.find(",",0); |
| 366 |
if(pos == std::string::npos) |
| 367 |
{ |
| 368 |
std::cout << "Indent not defined correctly" << std::endl; |
| 369 |
return false; |
| 370 |
} |
| 371 |
std::string v1 = val.substr(0,pos); |
| 372 |
auto pos1 = val.find(",", pos + 1); |
| 373 |
if(pos1 == std::string::npos) |
| 374 |
{ |
| 375 |
std::cout << "Indent not defined correctly" << std::endl; |
| 376 |
return false; |
| 377 |
} |
| 378 |
std::string v2 = val.substr(pos+1,pos1-pos-1); |
| 379 |
pos = val.find(",",pos1+1); |
| 380 |
if(pos == std::string::npos) |
| 381 |
{ |
| 382 |
std::cout << "Indent not defined correctly" << std::endl; |
| 383 |
return false; |
| 384 |
} |
| 385 |
std::string v3 = val.substr(pos1+1,pos-pos1-1); |
| 386 |
std::string v4 = val.substr(pos+1,val.length()-pos-1); |
| 387 |
bool header = false; |
| 388 |
if(!strcmp(v3.c_str(),"true")) |
| 389 |
{ |
| 390 |
header = true; |
| 391 |
} |
| 392 |
bool blockline = false; |
| 393 |
if(!strcmp(v4.c_str(),"true")) |
| 394 |
{ |
| 395 |
blockline = true; |
| 396 |
} |
| 397 |
|
| 398 |
IndentType itype = kws::SPACE; |
| 399 |
|
| 400 |
if(!strcmp(v1.c_str(),"TAB")) |
| 401 |
{ |
| 402 |
itype = kws::TAB; |
| 403 |
} |
| 404 |
this->CheckIndent(itype,std::stoul(v2.c_str()),header,blockline); |
| 405 |
} |
| 406 |
|
| 407 |
else if(!strcmp(name,"Namespace")) |
| 408 |
{ |
| 409 |
this->CheckNamespace(value); |
| 410 |
} |
| 411 |
else if(!strcmp(name,"NameOfClass")) |
| 412 |
{ |
| 413 |
std::string val = value; |
| 414 |
size_t pos = val.find(",",0); |
| 415 |
if(pos == std::string::npos) |
| 416 |
{ |
| 417 |
std::cout << "NameOfClass not defined correctly" << std::endl; |
| 418 |
return false; |
| 419 |
} |
| 420 |
std::string v1 = val.substr(0,pos); |
| 421 |
std::string v2 = val.substr(pos+1,val.length()-pos-1); |
| 422 |
this->CheckNameOfClass(v1.c_str(),v2.c_str()); |
| 423 |
} |
| 424 |
else if(!strcmp(name,"IfNDefDefine")) |
| 425 |
{ |
| 426 |
std::string val = value; |
| 427 |
std::string v1 = value; |
| 428 |
bool uppercaseTheDefinition = false; |
| 429 |
const size_t pos = val.find(",", 0); |
| 430 |
if(pos != std::string::npos) |
| 431 |
{ |
| 432 |
v1 = val.substr(0, pos); |
| 433 |
std::string v2 = val.substr(pos+1); |
| 434 |
uppercaseTheDefinition = !v2.compare("true") || !v2.compare("1"); |
| 435 |
} |
| 436 |
this->CheckIfNDefDefine(v1.c_str(), uppercaseTheDefinition); |
| 437 |
} |
| 438 |
else if(!strcmp(name,"EmptyLines")) |
| 439 |
{ |
| 440 |
this->CheckEmptyLines(std::stoul(value)); |
| 441 |
} |
| 442 |
else if(!strcmp(name,"Template")) |
| 443 |
{ |
| 444 |
this->CheckTemplate(value); |
| 445 |
} |
| 446 |
else if(!strcmp(name,"Operator")) |
| 447 |
{ |
| 448 |
std::string val = value; |
| 449 |
const size_t pos = val.find(",",0); |
| 450 |
if(pos == std::string::npos) |
| 451 |
{ |
| 452 |
std::cout << "Operator not defined correctly" << std::endl; |
| 453 |
return false; |
| 454 |
} |
| 455 |
std::string v1 = val.substr(0,pos); |
| 456 |
std::string v2 = val.substr(pos+1,val.length()-pos-1); |
| 457 |
this->CheckOperator(std::stoul(v1),std::stoul(v2)); |
| 458 |
} |
| 459 |
else if(!strcmp(name,"Comma")) |
| 460 |
{ |
| 461 |
std::string val = value; |
| 462 |
const size_t pos = val.find(",",0); |
| 463 |
if(pos == std::string::npos) |
| 464 |
{ |
| 465 |
std::cout << "Comma not defined correctly" << std::endl; |
| 466 |
return false; |
| 467 |
} |
| 468 |
std::string v1 = val.substr(0,pos); |
| 469 |
std::string v2 = val.substr(pos+1,val.length()-pos-1); |
| 470 |
this->CheckComma(std::stoul(v1),std::stoul(v2)); |
| 471 |
} |
| 472 |
else if(!strcmp(name,"Parenthesis")) |
| 473 |
{ |
| 474 |
this->CheckParenthesis(std::stoul(value)); |
| 475 |
} |
| 476 |
else if(!strcmp(name,"IfWhileForUntil")) |
| 477 |
{ |
| 478 |
this->CheckIfWhileForUntil(std::stoul(value)); |
| 479 |
} |
| 480 |
else if(!strcmp(name,"BlackList")) |
| 481 |
{ |
| 482 |
this->CheckBlackList(value); |
| 483 |
} |
| 484 |
else if(!strcmp(name, "UsingDirectives")) |
| 485 |
{ |
| 486 |
bool forbidUsingDirectives = true; |
| 487 |
if( !strcmp(value, "0") || !strcmp(value, "false")) |
| 488 |
forbidUsingDirectives = false; |
| 489 |
this->CheckUsingDirectives(forbidUsingDirectives); |
| 490 |
} |
| 491 |
else if(!strcmp(name, "RelativePathInInclude")) |
| 492 |
{ |
| 493 |
bool forbidRelativePaths = true; |
| 494 |
if( !strcmp(value, "0") || !strcmp(value, "false")) |
| 495 |
forbidRelativePaths = false; |
| 496 |
this->CheckRelativePathInInclude(forbidRelativePaths); |
| 497 |
} |
| 498 |
return false; |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
/** Return if a test has been performed */ |
| 503 |
bool Parser::HasBeenPerformed(unsigned int test) const |
| 504 |
{ |
| 505 |
return m_TestsDone[test]; |
| 506 |
} |
| 507 |
|
| 508 |
/** Return the test description given the erro number) */ |
| 509 |
std::string Parser::GetTestDescription(size_t test) const |
| 510 |
{ |
| 511 |
return m_TestsDescription[test]; |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
/** Return the last errors as a string */ |
| 516 |
std::string Parser::GetLastErrors() |
| 517 |
{ |
| 518 |
std::string output = ""; |
| 519 |
std::vector<Error>::const_iterator it = m_ErrorList.begin(); |
| 520 |
while(it != m_ErrorList.end()) |
| 521 |
{ |
| 522 |
output += "Error #"; |
| 523 |
constexpr size_t length = 10; |
| 524 |
char* val = new char[length]; |
| 525 |
snprintf(val,length,"%ld",(*it).number); |
| 526 |
output += val; |
| 527 |
delete [] val; |
| 528 |
output += " ("; |
| 529 |
val = new char[length]; |
| 530 |
snprintf(val,length,"%ld",(*it).line); |
| 531 |
output += val; |
| 532 |
delete [] val; |
| 533 |
output += ") "; |
| 534 |
output += (*it).description; |
| 535 |
output += "\n"; |
| 536 |
it++; |
| 537 |
} |
| 538 |
return output; |
| 539 |
} |
| 540 |
|
| 541 |
/** Return the error tag as string given the error number */ |
| 542 |
std::string Parser::GetErrorTag(unsigned long number) const |
| 543 |
{ |
| 544 |
return ErrorTag[number]; |
| 545 |
} |
| 546 |
|
| 547 |
/** Return the last warnings as a string */ |
| 548 |
std::string Parser::GetLastWarnings() |
| 549 |
{ |
| 550 |
std::string output = ""; |
| 551 |
std::vector<Warning>::const_iterator it = m_WarningList.begin(); |
| 552 |
while(it != m_WarningList.end()) |
| 553 |
{ |
| 554 |
output += "Warning #"; |
| 555 |
constexpr size_t length = 10; |
| 556 |
char* val = new char[length]; |
| 557 |
snprintf(val,length,"%ld",(*it).number); |
| 558 |
output += val; |
| 559 |
delete [] val; |
| 560 |
output += " ("; |
| 561 |
val = new char[length]; |
| 562 |
snprintf(val,length,"%ld",(*it).line); |
| 563 |
output += val; |
| 564 |
delete [] val; |
| 565 |
output += ") "; |
| 566 |
output += (*it).description; |
| 567 |
output += "\n"; |
| 568 |
it++; |
| 569 |
} |
| 570 |
return output; |
| 571 |
} |
| 572 |
|
| 573 |
void Parser::ConvertBufferToWindowsFileType(std::string & buffer) |
| 574 |
{ |
| 575 |
// replace logical newlines with windows newlines |
| 576 |
sregex unixNewline = sregex::compile("\r?\n|\r"); |
| 577 |
std::string windowsNewline("\r\n"); |
| 578 |
buffer = regex_replace(buffer, unixNewline, windowsNewline); |
| 579 |
} |
| 580 |
|
| 581 |
/** Return the number of lines */ |
| 582 |
unsigned long Parser::GetNumberOfLines() const |
| 583 |
{ |
| 584 |
unsigned long lines = 0; |
| 585 |
auto pos = m_Buffer.find("\n", 0); |
| 586 |
while(pos != std::string::npos) |
| 587 |
{ |
| 588 |
lines++; |
| 589 |
pos = m_Buffer.find("\n",pos+1); |
| 590 |
} |
| 591 |
lines++; // the last line doesn't have any \n |
| 592 |
return lines; |
| 593 |
} |
| 594 |
|
| 595 |
/** Return the line */ |
| 596 |
std::string Parser::GetLine(unsigned long i) const |
| 597 |
{ |
| 598 |
unsigned long lines = 0; |
| 599 |
size_t prec = 0; |
| 600 |
auto pos = m_Buffer.find("\n", 0); |
| 601 |
while(pos != std::string::npos) |
| 602 |
{ |
| 603 |
if(lines == i) |
| 604 |
{ |
| 605 |
if(pos-prec-1>0) |
| 606 |
{ |
| 607 |
return m_Buffer.substr(prec,pos-prec-1); |
| 608 |
} |
| 609 |
} |
| 610 |
lines++; |
| 611 |
prec = pos; |
| 612 |
pos = m_Buffer.find("\n",pos+1); |
| 613 |
} |
| 614 |
|
| 615 |
if(lines == i) |
| 616 |
{ |
| 617 |
return m_Buffer.substr(prec,m_Buffer.size()-prec); |
| 618 |
} |
| 619 |
|
| 620 |
return ""; |
| 621 |
} |
| 622 |
|
| 623 |
/** Find the previous word given a position */ |
| 624 |
std::string Parser::FindPreviousWord(size_t pos,bool withComments,std::string buffer) const |
| 625 |
{ |
| 626 |
std::string stream = buffer; |
| 627 |
if (buffer.empty()) { |
| 628 |
stream = m_BufferNoComment; |
| 629 |
if(withComments) |
| 630 |
{ |
| 631 |
stream = m_Buffer; |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
size_t i=pos; |
| 636 |
|
| 637 |
while(stream[i] != ' ' && stream[i] != '\r' && i>0) |
| 638 |
{ |
| 639 |
i--; |
| 640 |
} |
| 641 |
|
| 642 |
if(stream[i] == '\r' && i>1) |
| 643 |
{ |
| 644 |
i--; |
| 645 |
} |
| 646 |
|
| 647 |
bool inWord = true; |
| 648 |
bool first = false; |
| 649 |
std::string ivar = ""; |
| 650 |
while(i != std::string::npos && inWord) |
| 651 |
{ |
| 652 |
if(stream[i] != ' ' && stream[i] != '\n' && stream[i] != '\r') |
| 653 |
{ |
| 654 |
std::string store = ivar; |
| 655 |
ivar = stream[i]; |
| 656 |
ivar += store; |
| 657 |
inWord = true; |
| 658 |
first = true; |
| 659 |
} |
| 660 |
else // we have a space |
| 661 |
{ |
| 662 |
if(stream[i] == '\r') |
| 663 |
{ |
| 664 |
// do nothing |
| 665 |
} |
| 666 |
else if(first) |
| 667 |
{ |
| 668 |
inWord = false; |
| 669 |
} |
| 670 |
} |
| 671 |
i--; |
| 672 |
} |
| 673 |
return ivar; |
| 674 |
} |
| 675 |
|
| 676 |
/** Find the next word given a position */ |
| 677 |
std::string Parser::FindNextWord(size_t pos) const |
| 678 |
{ |
| 679 |
size_t i=pos; |
| 680 |
|
| 681 |
// we go to the next space |
| 682 |
while(m_BufferNoComment[i] != ' ' && i<m_BufferNoComment.size()) |
| 683 |
{ |
| 684 |
i++; |
| 685 |
} |
| 686 |
|
| 687 |
bool inWord = true; |
| 688 |
bool first = false; |
| 689 |
std::string ivar = ""; |
| 690 |
while(i<m_BufferNoComment.size() && inWord) |
| 691 |
{ |
| 692 |
if(m_BufferNoComment[i] != ' ' && m_BufferNoComment[i] != '\r') |
| 693 |
{ |
| 694 |
ivar += m_BufferNoComment[i]; |
| 695 |
inWord = true; |
| 696 |
first = true; |
| 697 |
} |
| 698 |
else // we have a space |
| 699 |
{ |
| 700 |
if(first) |
| 701 |
{ |
| 702 |
inWord = false; |
| 703 |
} |
| 704 |
} |
| 705 |
i++; |
| 706 |
} |
| 707 |
return ivar; |
| 708 |
} |
| 709 |
|
| 710 |
/** Return the position in the line given the position in the text */ |
| 711 |
size_t Parser::GetPositionInLine(size_t pos) |
| 712 |
{ |
| 713 |
size_t begin = pos; |
| 714 |
while(begin != std::string::npos && m_BufferNoComment[begin]!='\n') |
| 715 |
{ |
| 716 |
begin--; |
| 717 |
} |
| 718 |
return pos-begin; |
| 719 |
} |
| 720 |
|
| 721 |
/** Given the position without comments return the position with the comments */ |
| 722 |
size_t Parser::GetPositionWithComments(size_t pos) const |
| 723 |
{ |
| 724 |
auto it = m_CommentPositions.begin(); |
| 725 |
while(it != m_CommentPositions.end()) |
| 726 |
{ |
| 727 |
if((pos>=(*it).first)) |
| 728 |
{ |
| 729 |
pos += ((*it).second-(*it).first); |
| 730 |
} |
| 731 |
else |
| 732 |
{ |
| 733 |
break; |
| 734 |
} |
| 735 |
it++; |
| 736 |
} |
| 737 |
return pos; |
| 738 |
} |
| 739 |
|
| 740 |
/** Given the position with comments return the position without the comments */ |
| 741 |
size_t Parser::GetPositionWithoutComments(size_t pos) const |
| 742 |
{ |
| 743 |
size_t pos2 = pos; |
| 744 |
auto it = m_CommentPositions.begin(); |
| 745 |
while(it != m_CommentPositions.end()) |
| 746 |
{ |
| 747 |
if((pos>=(*it).first)) |
| 748 |
{ |
| 749 |
pos2 -= ((*it).second-(*it).first); |
| 750 |
} |
| 751 |
else |
| 752 |
{ |
| 753 |
break; |
| 754 |
} |
| 755 |
it++; |
| 756 |
} |
| 757 |
return pos2; |
| 758 |
} |
| 759 |
|
| 760 |
/** Return if the dept of the current class |
| 761 |
* This function works on m_BufferNoComments! */ |
| 762 |
int Parser::IsInClass(size_t position) const |
| 763 |
{ |
| 764 |
int inClass = 0; |
| 765 |
size_t classPos = this->GetClassPosition(0); |
| 766 |
while(classPos!=std::string::npos) |
| 767 |
{ |
| 768 |
size_t i=classPos; |
| 769 |
while(i!=std::string::npos && i<m_BufferNoComment.size()) |
| 770 |
{ |
| 771 |
if(m_BufferNoComment[i] == '{') |
| 772 |
{ |
| 773 |
break; |
| 774 |
} |
| 775 |
i++; |
| 776 |
} |
| 777 |
size_t classEnd = this->FindClosingChar('{','}',i+1,true); |
| 778 |
|
| 779 |
if(position>classPos && position<classEnd) |
| 780 |
{ |
| 781 |
inClass++; |
| 782 |
} |
| 783 |
classPos = this->GetClassPosition(classPos+1); |
| 784 |
} |
| 785 |
|
| 786 |
return inClass; |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
/** Return the line number in the source code given the character position */ |
| 791 |
long int Parser::GetLineNumber(size_t pos,bool withoutComments) const |
| 792 |
{ |
| 793 |
|
| 794 |
// if we have comments we add them to the list |
| 795 |
if(withoutComments) |
| 796 |
{ |
| 797 |
pos = this->GetPositionWithComments(pos); |
| 798 |
} |
| 799 |
|
| 800 |
unsigned int i=0; |
| 801 |
auto it = m_Positions.begin(); |
| 802 |
while(it != m_Positions.end()) |
| 803 |
{ |
| 804 |
if(pos<=(*it)) |
| 805 |
{ |
| 806 |
return i; |
| 807 |
} |
| 808 |
i++; |
| 809 |
it++; |
| 810 |
} |
| 811 |
return 0; |
| 812 |
} |
| 813 |
|
| 814 |
/** Find public area in source code. */ |
| 815 |
void Parser::FindPublicArea(size_t &before, size_t &after, size_t startPos) const |
| 816 |
{ |
| 817 |
before = 0; |
| 818 |
after = 0; |
| 819 |
|
| 820 |
// First look if public is before protected and private |
| 821 |
size_t pub = this->FindArea("public", startPos); |
| 822 |
size_t priv = this->FindArea("private", startPos); |
| 823 |
size_t protect = this->FindArea("protected", startPos); |
| 824 |
|
| 825 |
if(pub == std::string::npos) |
| 826 |
{ |
| 827 |
// ok we don't have any public area |
| 828 |
before = std::string::npos; |
| 829 |
after = std::string::npos; |
| 830 |
return; |
| 831 |
pub = MAX_CHAR; |
| 832 |
} |
| 833 |
|
| 834 |
if(priv == std::string::npos) |
| 835 |
{ |
| 836 |
priv = MAX_CHAR; |
| 837 |
} |
| 838 |
|
| 839 |
if(protect == std::string::npos) |
| 840 |
{ |
| 841 |
protect = MAX_CHAR; |
| 842 |
} |
| 843 |
|
| 844 |
if(pub>priv || pub>protect) |
| 845 |
{ |
| 846 |
if(pub>=priv && pub<=protect) |
| 847 |
{ |
| 848 |
before = priv; |
| 849 |
after = protect; |
| 850 |
} |
| 851 |
else if(pub<=priv && pub>=protect) |
| 852 |
{ |
| 853 |
before = pub; |
| 854 |
after = priv; |
| 855 |
} |
| 856 |
else |
| 857 |
{ |
| 858 |
before = pub; |
| 859 |
size_t eoc = this->FindEndOfClass(pub); |
| 860 |
if(eoc != std::string::npos) |
| 861 |
{ |
| 862 |
after = eoc; |
| 863 |
} |
| 864 |
else |
| 865 |
{ |
| 866 |
after = pub; // end of class |
| 867 |
} |
| 868 |
} |
| 869 |
} |
| 870 |
else |
| 871 |
{ |
| 872 |
before = pub; |
| 873 |
|
| 874 |
if(priv<protect) |
| 875 |
{ |
| 876 |
after = priv; |
| 877 |
} |
| 878 |
else |
| 879 |
{ |
| 880 |
after = protect; |
| 881 |
} |
| 882 |
} |
| 883 |
|
| 884 |
// If there is nothing after we point to the end of the class |
| 885 |
if(after == MAX_CHAR) |
| 886 |
{ |
| 887 |
size_t classpos = this->GetClassPosition(startPos); |
| 888 |
if(classpos != std::string::npos) |
| 889 |
{ |
| 890 |
size_t posBrace = m_BufferNoComment.find("{",classpos); |
| 891 |
if(posBrace != std::string::npos) |
| 892 |
{ |
| 893 |
size_t end = this->FindClosingChar('{','}',posBrace,true); |
| 894 |
if(end != std::string::npos) |
| 895 |
{ |
| 896 |
after = end; |
| 897 |
} |
| 898 |
} |
| 899 |
} |
| 900 |
} |
| 901 |
} |
| 902 |
|
| 903 |
/** Find the correct area given its name |
| 904 |
* This looks for public: or public : */ |
| 905 |
size_t Parser::FindArea(const char* name,size_t startPos) const |
| 906 |
{ |
| 907 |
size_t pos = m_BufferNoComment.find(name, startPos); |
| 908 |
while(pos != std::string::npos) |
| 909 |
{ |
| 910 |
if(m_BufferNoComment[pos+strlen(name)]==':' || this->FindNextWord(pos) == ":") |
| 911 |
{ |
| 912 |
return pos; |
| 913 |
} |
| 914 |
pos = m_BufferNoComment.find(name, pos+1); |
| 915 |
} |
| 916 |
return std::string::npos; |
| 917 |
} |
| 918 |
|
| 919 |
|
| 920 |
/** Find protected area in source code. */ |
| 921 |
void Parser::FindProtectedArea(size_t &before, size_t &after, size_t startPos) const |
| 922 |
{ |
| 923 |
before = 0; |
| 924 |
after = 0; |
| 925 |
|
| 926 |
// First look if public is before protected and private |
| 927 |
size_t pub = this->FindArea("public", startPos); |
| 928 |
size_t priv = this->FindArea("private", startPos); |
| 929 |
size_t protect = this->FindArea("protected", startPos); |
| 930 |
|
| 931 |
if(priv == std::string::npos) |
| 932 |
{ |
| 933 |
priv = MAX_CHAR; |
| 934 |
} |
| 935 |
|
| 936 |
if(protect == std::string::npos) |
| 937 |
{ |
| 938 |
protect = MAX_CHAR; |
| 939 |
} |
| 940 |
|
| 941 |
if(pub == std::string::npos) |
| 942 |
{ |
| 943 |
pub = MAX_CHAR; |
| 944 |
} |
| 945 |
|
| 946 |
if(protect>priv || protect>pub) |
| 947 |
{ |
| 948 |
if(protect>=priv && protect<=pub) |
| 949 |
{ |
| 950 |
before = protect; |
| 951 |
after = pub; |
| 952 |
} |
| 953 |
else if(protect<=priv && protect>=pub) |
| 954 |
{ |
| 955 |
before = protect; |
| 956 |
after = priv; |
| 957 |
} |
| 958 |
else |
| 959 |
{ |
| 960 |
before = protect; |
| 961 |
size_t eoc = this->FindEndOfClass(protect); |
| 962 |
if(eoc != std::string::npos) |
| 963 |
{ |
| 964 |
after = eoc; |
| 965 |
} |
| 966 |
else |
| 967 |
{ |
| 968 |
after = protect; // end of class |
| 969 |
} |
| 970 |
} |
| 971 |
} |
| 972 |
else |
| 973 |
{ |
| 974 |
before = protect; |
| 975 |
if(priv<pub) |
| 976 |
{ |
| 977 |
after = priv; |
| 978 |
} |
| 979 |
else |
| 980 |
{ |
| 981 |
after = pub; |
| 982 |
} |
| 983 |
} |
| 984 |
|
| 985 |
|
| 986 |
// If there is nothing after we point to the end of the class |
| 987 |
if(after == MAX_CHAR) |
| 988 |
{ |
| 989 |
size_t classpos = this->GetClassPosition(startPos); |
| 990 |
if(classpos != std::string::npos) |
| 991 |
{ |
| 992 |
size_t posBrace = m_BufferNoComment.find("{",classpos); |
| 993 |
if(posBrace != std::string::npos) |
| 994 |
{ |
| 995 |
size_t end = this->FindClosingChar('{','}',posBrace,true); |
| 996 |
if(end != std::string::npos) |
| 997 |
{ |
| 998 |
after = end; |
| 999 |
} |
| 1000 |
} |
| 1001 |
} |
| 1002 |
} |
| 1003 |
} |
| 1004 |
|
| 1005 |
/** Find the end of the class */ |
| 1006 |
size_t Parser::FindEndOfClass(size_t position) const |
| 1007 |
{ |
| 1008 |
if(position == std::string::npos) |
| 1009 |
{ |
| 1010 |
position = 0; |
| 1011 |
} |
| 1012 |
|
| 1013 |
// Try to find the end of the class |
| 1014 |
size_t endclass = m_BufferNoComment.find("}",position); |
| 1015 |
while(endclass != std::string::npos) |
| 1016 |
{ |
| 1017 |
bool isClass = true; |
| 1018 |
// if the next char is not a semicolon this cannot be a class |
| 1019 |
for(size_t i=endclass+1;i<m_BufferNoComment.size();i++) |
| 1020 |
{ |
| 1021 |
if( (m_BufferNoComment[i] != ' ') && (m_BufferNoComment[i] != '\r') |
| 1022 |
&& (m_BufferNoComment[i] != '\n') && (m_BufferNoComment[i] != ';') |
| 1023 |
) |
| 1024 |
{ |
| 1025 |
isClass = false; |
| 1026 |
break; |
| 1027 |
} |
| 1028 |
if(m_BufferNoComment[i] == ';') |
| 1029 |
{ |
| 1030 |
break; |
| 1031 |
} |
| 1032 |
} |
| 1033 |
|
| 1034 |
if(isClass) |
| 1035 |
{ |
| 1036 |
size_t openingChar = this->FindOpeningChar('}','{',endclass,true); |
| 1037 |
// check if we have the class name somewhere |
| 1038 |
size_t classPos = m_BufferNoComment.find("class",0); |
| 1039 |
while(classPos != std::string::npos && classPos<position) |
| 1040 |
{ |
| 1041 |
if(classPos != std::string::npos && openingChar!= std::string::npos) |
| 1042 |
{ |
| 1043 |
for(size_t i=classPos;i<openingChar+1;i++) |
| 1044 |
{ |
| 1045 |
if(m_BufferNoComment[i] == '{') |
| 1046 |
{ |
| 1047 |
classPos = i; |
| 1048 |
break; |
| 1049 |
} |
| 1050 |
} |
| 1051 |
if(openingChar == classPos) |
| 1052 |
{ |
| 1053 |
return endclass; |
| 1054 |
} |
| 1055 |
} |
| 1056 |
classPos = m_BufferNoComment.find("class",classPos+1); |
| 1057 |
} |
| 1058 |
} |
| 1059 |
endclass = m_BufferNoComment.find("}",endclass+1); |
| 1060 |
} |
| 1061 |
return std::string::npos; |
| 1062 |
} |
| 1063 |
|
| 1064 |
|
| 1065 |
/** Find private area in source code. */ |
| 1066 |
void Parser::FindPrivateArea(size_t &before, size_t &after, size_t startPos) const |
| 1067 |
{ |
| 1068 |
before = 0; |
| 1069 |
after = 0; |
| 1070 |
|
| 1071 |
// First look if public is before protected and private |
| 1072 |
size_t pub = this->FindArea("public", startPos); |
| 1073 |
size_t priv = this->FindArea("private", startPos); |
| 1074 |
size_t protect = this->FindArea("protected", startPos); |
| 1075 |
|
| 1076 |
if(priv == std::string::npos) |
| 1077 |
{ |
| 1078 |
priv = MAX_CHAR; |
| 1079 |
} |
| 1080 |
|
| 1081 |
if(pub == std::string::npos) |
| 1082 |
{ |
| 1083 |
pub = MAX_CHAR; |
| 1084 |
} |
| 1085 |
|
| 1086 |
if(protect == std::string::npos) |
| 1087 |
{ |
| 1088 |
protect = MAX_CHAR; |
| 1089 |
} |
| 1090 |
|
| 1091 |
if(priv>pub || priv>protect) |
| 1092 |
{ |
| 1093 |
if(priv>=pub && priv<=protect) |
| 1094 |
{ |
| 1095 |
before = priv; |
| 1096 |
after = protect; |
| 1097 |
} |
| 1098 |
else if(priv<=pub && priv>=protect) |
| 1099 |
{ |
| 1100 |
before = priv; |
| 1101 |
after = pub; |
| 1102 |
} |
| 1103 |
else |
| 1104 |
{ |
| 1105 |
before = priv; |
| 1106 |
size_t eoc = this->FindEndOfClass(priv); |
| 1107 |
if(eoc != std::string::npos) |
| 1108 |
{ |
| 1109 |
after = eoc; |
| 1110 |
} |
| 1111 |
else |
| 1112 |
{ |
| 1113 |
after = priv; // end of class |
| 1114 |
} |
| 1115 |
} |
| 1116 |
} |
| 1117 |
else |
| 1118 |
{ |
| 1119 |
before = priv; |
| 1120 |
if(pub<protect) |
| 1121 |
{ |
| 1122 |
after = pub; |
| 1123 |
} |
| 1124 |
else |
| 1125 |
{ |
| 1126 |
after = protect; |
| 1127 |
} |
| 1128 |
} |
| 1129 |
|
| 1130 |
// If there is nothing after we point to the end of the class |
| 1131 |
if(after == MAX_CHAR) |
| 1132 |
{ |
| 1133 |
size_t classpos = this->GetClassPosition(startPos); |
| 1134 |
if(classpos != std::string::npos) |
| 1135 |
{ |
| 1136 |
size_t posBrace = m_BufferNoComment.find("{",classpos); |
| 1137 |
if(posBrace != std::string::npos) |
| 1138 |
{ |
| 1139 |
size_t end = this->FindClosingChar('{','}',posBrace,true); |
| 1140 |
if(end != std::string::npos) |
| 1141 |
{ |
| 1142 |
after = end; |
| 1143 |
} |
| 1144 |
} |
| 1145 |
} |
| 1146 |
} |
| 1147 |
} |
| 1148 |
|
| 1149 |
/** Return true if the position pos is inside a function |
| 1150 |
* This function works on the m_BufferNoComment */ |
| 1151 |
bool Parser::IsInFunction(size_t pos,const char* buffer) const |
| 1152 |
{ |
| 1153 |
std::string buf = m_BufferNoComment; |
| 1154 |
|
| 1155 |
if(buffer) |
| 1156 |
{ |
| 1157 |
buf = buffer; |
| 1158 |
} |
| 1159 |
|
| 1160 |
if((pos == std::string::npos) || pos > buf.size()-1) |
| 1161 |
{ |
| 1162 |
return false; |
| 1163 |
} |
| 1164 |
|
| 1165 |
// a function is defined as: |
| 1166 |
// function() {}; and if() {} is considered as a function |
| 1167 |
// here. |
| 1168 |
size_t close = 1; |
| 1169 |
bool check = false; |
| 1170 |
|
| 1171 |
// We go backwards |
| 1172 |
size_t i = pos; |
| 1173 |
while(i != std::string::npos) |
| 1174 |
{ |
| 1175 |
if(buf[i] == '}') |
| 1176 |
{ |
| 1177 |
close++; |
| 1178 |
} |
| 1179 |
else if(buf[i] == '{') |
| 1180 |
{ |
| 1181 |
check = true; |
| 1182 |
close--; |
| 1183 |
} |
| 1184 |
else if(buf[i] == ')') |
| 1185 |
{ |
| 1186 |
if(check && close==0) |
| 1187 |
{ |
| 1188 |
return true; |
| 1189 |
} |
| 1190 |
} |
| 1191 |
else if(buf[i] == ' ' |
| 1192 |
|| buf[i] == '\r' |
| 1193 |
|| buf[i] == '\n' |
| 1194 |
) |
| 1195 |
{ |
| 1196 |
|
| 1197 |
} |
| 1198 |
else |
| 1199 |
{ |
| 1200 |
//check = false; |
| 1201 |
} |
| 1202 |
i--; |
| 1203 |
} |
| 1204 |
|
| 1205 |
return false; |
| 1206 |
} |
| 1207 |
|
| 1208 |
|
| 1209 |
/** Return the position of the last character |
| 1210 |
* of the function name/definition */ |
| 1211 |
size_t Parser::FindFunction(size_t pos,const char* buffer) const |
| 1212 |
{ |
| 1213 |
std::string buf = m_BufferNoComment; |
| 1214 |
|
| 1215 |
if(buffer) |
| 1216 |
{ |
| 1217 |
buf = buffer; |
| 1218 |
} |
| 1219 |
|
| 1220 |
if((pos == std::string::npos) || pos > buf.size()-1) |
| 1221 |
{ |
| 1222 |
return std::string::npos; |
| 1223 |
} |
| 1224 |
|
| 1225 |
// a function is defined as: |
| 1226 |
// function() [const] {}; and if() {} is considered as a function |
| 1227 |
// here. |
| 1228 |
|
| 1229 |
// We go backwards |
| 1230 |
size_t end = buf.find('}',pos); |
| 1231 |
while(end != std::string::npos) |
| 1232 |
{ |
| 1233 |
size_t beg = this->FindOpeningChar('}','{',end,true); |
| 1234 |
if(beg == std::string::npos) |
| 1235 |
{ |
| 1236 |
break; |
| 1237 |
} |
| 1238 |
|
| 1239 |
// check that before the beg we have |
| 1240 |
bool nospecialchar=true; |
| 1241 |
size_t i = beg-1; |
| 1242 |
while(i != std::string::npos) |
| 1243 |
{ |
| 1244 |
if(buf[i] == ')') |
| 1245 |
{ |
| 1246 |
break; |
| 1247 |
} |
| 1248 |
else if( |
| 1249 |
buf[i] != ' ' && buf[i] != '\r' |
| 1250 |
&& buf[i] != '\n' |
| 1251 |
) |
| 1252 |
{ |
| 1253 |
nospecialchar = false; |
| 1254 |
} |
| 1255 |
--i; |
| 1256 |
} |
| 1257 |
|
| 1258 |
if(nospecialchar) |
| 1259 |
{ |
| 1260 |
return beg; |
| 1261 |
} |
| 1262 |
if(i != std::string::npos)// check if we have a const |
| 1263 |
{ |
| 1264 |
if(buf.substr(i,beg-i).find("]") == std::string::npos && |
| 1265 |
buf.substr(i,beg-i).find("const") != std::string::npos |
| 1266 |
) |
| 1267 |
{ |
| 1268 |
return beg; |
| 1269 |
} |
| 1270 |
} |
| 1271 |
end = buf.find('}',end+1); |
| 1272 |
} |
| 1273 |
|
| 1274 |
return std::string::npos; |
| 1275 |
} |
| 1276 |
|
| 1277 |
/** Return true if the position pos is inside a struct |
| 1278 |
* This function works on the m_BufferNoComment */ |
| 1279 |
bool Parser::IsInStruct(size_t pos,const char* buffer) const |
| 1280 |
{ |
| 1281 |
std::string buf = m_BufferNoComment; |
| 1282 |
if(buffer) |
| 1283 |
{ |
| 1284 |
buf = buffer; |
| 1285 |
} |
| 1286 |
|
| 1287 |
if((pos == std::string::npos) || pos > buf.size()-1) |
| 1288 |
{ |
| 1289 |
return false; |
| 1290 |
} |
| 1291 |
|
| 1292 |
size_t b = buf.find("struct",0); |
| 1293 |
while(b!=std::string::npos) |
| 1294 |
{ |
| 1295 |
// The next character should be either a space or a { |
| 1296 |
if(b+6==buf.size()) |
| 1297 |
{ |
| 1298 |
return false; |
| 1299 |
} |
| 1300 |
|
| 1301 |
if(buf[b+6]!=' ' && buf[b+6]!='{') |
| 1302 |
{ |
| 1303 |
return false; |
| 1304 |
} |
| 1305 |
|
| 1306 |
while(b<buf.size()) |
| 1307 |
{ |
| 1308 |
if(buf[b] == '{') |
| 1309 |
{ |
| 1310 |
break; |
| 1311 |
} |
| 1312 |
b++; |
| 1313 |
} |
| 1314 |
|
| 1315 |
size_t c=this->FindClosingChar('{','}',b,true); |
| 1316 |
if(pos<c && pos>b) |
| 1317 |
{ |
| 1318 |
return true; |
| 1319 |
} |
| 1320 |
b = buf.find("struct",b+1); |
| 1321 |
} |
| 1322 |
return false; |
| 1323 |
} |
| 1324 |
|
| 1325 |
|
| 1326 |
/** Return true if the position pos is inside a union |
| 1327 |
* This function works on the m_BufferNoComment */ |
| 1328 |
bool Parser::IsInUnion(size_t pos,const char* buffer) const |
| 1329 |
{ |
| 1330 |
std::string buf = m_BufferNoComment; |
| 1331 |
if(buffer) |
| 1332 |
{ |
| 1333 |
buf = buffer; |
| 1334 |
} |
| 1335 |
|
| 1336 |
if((pos == std::string::npos) || pos > buf.size()-1) |
| 1337 |
{ |
| 1338 |
return false; |
| 1339 |
} |
| 1340 |
|
| 1341 |
size_t b = buf.find("union",0); |
| 1342 |
while(b!=std::string::npos) |
| 1343 |
{ |
| 1344 |
while(b<buf.size()) |
| 1345 |
{ |
| 1346 |
if(buf[b] == '{') |
| 1347 |
{ |
| 1348 |
break; |
| 1349 |
} |
| 1350 |
b++; |
| 1351 |
} |
| 1352 |
|
| 1353 |
size_t c=this->FindClosingChar('{','}',b,true); |
| 1354 |
if(pos<c && pos>b) |
| 1355 |
{ |
| 1356 |
return true; |
| 1357 |
} |
| 1358 |
b = buf.find("union",b+1); |
| 1359 |
} |
| 1360 |
return false; |
| 1361 |
} |
| 1362 |
|
| 1363 |
/** return true if the position pos is inside a comment */ |
| 1364 |
bool Parser::IsInComments(size_t pos) const |
| 1365 |
{ |
| 1366 |
if ((pos == std::string::npos) || (m_CommentBegin.empty()) || |
| 1367 |
(m_CommentEnd.empty())) { |
| 1368 |
return false; |
| 1369 |
} |
| 1370 |
|
| 1371 |
size_t b0 = m_Buffer.find(m_CommentBegin,0); |
| 1372 |
|
| 1373 |
if(b0 == std::string::npos) |
| 1374 |
{ |
| 1375 |
return false; |
| 1376 |
} |
| 1377 |
|
| 1378 |
size_t b1 = m_Buffer.find(m_CommentEnd,b0); |
| 1379 |
|
| 1380 |
while(b0 != std::string::npos && b1 != std::string::npos && b1>b0) |
| 1381 |
{ |
| 1382 |
if(pos>=b0 && pos<=(b1+m_CommentEnd.size())) |
| 1383 |
{ |
| 1384 |
return true; |
| 1385 |
} |
| 1386 |
b0 = m_Buffer.find(m_CommentBegin,b0+1); |
| 1387 |
b1 = m_Buffer.find(m_CommentEnd,b0); |
| 1388 |
} |
| 1389 |
|
| 1390 |
return false; |
| 1391 |
} |
| 1392 |
|
| 1393 |
/** Return true is the position is in any comments */ |
| 1394 |
bool Parser::IsInAnyComments(size_t pos) const |
| 1395 |
{ |
| 1396 |
//Check // |
| 1397 |
size_t posslash = m_Buffer.find("//",0); |
| 1398 |
size_t posend = std::string::npos; |
| 1399 |
if(posslash != std::string::npos) |
| 1400 |
{ |
| 1401 |
posend = m_Buffer.find("\n",posslash); |
| 1402 |
} |
| 1403 |
|
| 1404 |
while(posend != std::string::npos) |
| 1405 |
{ |
| 1406 |
if(pos<=posend && pos>=posslash) |
| 1407 |
{ |
| 1408 |
return true; |
| 1409 |
} |
| 1410 |
posslash = m_Buffer.find("//",posend+1); |
| 1411 |
if(posslash != std::string::npos) |
| 1412 |
{ |
| 1413 |
posend = m_Buffer.find("\n",posslash); |
| 1414 |
} |
| 1415 |
else |
| 1416 |
{ |
| 1417 |
posend = std::string::npos; |
| 1418 |
} |
| 1419 |
} |
| 1420 |
|
| 1421 |
// Check the /* */ |
| 1422 |
size_t posstart = m_Buffer.find("/*",0); |
| 1423 |
posend = std::string::npos; |
| 1424 |
if(posstart != std::string::npos) |
| 1425 |
{ |
| 1426 |
posend = m_Buffer.find("*/",posstart); |
| 1427 |
} |
| 1428 |
|
| 1429 |
while(posend != std::string::npos) |
| 1430 |
{ |
| 1431 |
if(pos<=posend && pos>=posstart) |
| 1432 |
{ |
| 1433 |
return true; |
| 1434 |
} |
| 1435 |
posstart = m_Buffer.find("/*",posend+1); |
| 1436 |
if(posstart != std::string::npos) |
| 1437 |
{ |
| 1438 |
posend = m_Buffer.find("*/",posstart); |
| 1439 |
} |
| 1440 |
else |
| 1441 |
{ |
| 1442 |
posend = std::string::npos; |
| 1443 |
} |
| 1444 |
} |
| 1445 |
return false; |
| 1446 |
} |
| 1447 |
|
| 1448 |
/** Return true if it's a valid quote */ |
| 1449 |
bool Parser::IsValidQuote(std::string & stream,size_t pos) const |
| 1450 |
{ |
| 1451 |
// We need to count the number of \ if it's an odd number |
| 1452 |
// then this is not a valid " |
| 1453 |
int n = 0; |
| 1454 |
size_t i = pos; |
| 1455 |
if(pos != std::string::npos) |
| 1456 |
{ |
| 1457 |
i = pos-1; |
| 1458 |
} |
| 1459 |
|
| 1460 |
while(i != std::string::npos && stream[i]=='\\') |
| 1461 |
{ |
| 1462 |
n++; |
| 1463 |
i--; |
| 1464 |
} |
| 1465 |
|
| 1466 |
return n % 2 == 0; |
| 1467 |
} |
| 1468 |
|
| 1469 |
/** return true if the position pos is between ' ' */ |
| 1470 |
bool Parser::IsBetweenSingleQuote(size_t pos,bool withComments,std::string buffer) const |
| 1471 |
{ |
| 1472 |
std::string stream = buffer; |
| 1473 |
|
| 1474 |
if(pos == std::string::npos) |
| 1475 |
{ |
| 1476 |
return false; |
| 1477 |
} |
| 1478 |
|
| 1479 |
if(pos == 0) |
| 1480 |
{ |
| 1481 |
return false; |
| 1482 |
} |
| 1483 |
|
| 1484 |
if (buffer.empty()) { |
| 1485 |
stream = m_BufferNoComment; |
| 1486 |
if (withComments) { |
| 1487 |
stream = m_Buffer; |
| 1488 |
} |
| 1489 |
} |
| 1490 |
|
| 1491 |
if(pos == 1 && stream[0] != '\'') |
| 1492 |
{ |
| 1493 |
return false; |
| 1494 |
} |
| 1495 |
|
| 1496 |
if(pos == stream.length()-1) |
| 1497 |
{ |
| 1498 |
return false; |
| 1499 |
} |
| 1500 |
|
| 1501 |
return ( ( stream[pos-1] == '\'' || ( stream[pos-1] == '\\' && |
| 1502 |
stream[pos-2] == '\'' ) ) && stream[pos+1] == '\'' ); |
| 1503 |
} |
| 1504 |
|
| 1505 |
/** return true if the position pos is between " " */ |
| 1506 |
bool Parser::IsBetweenDoubleQuote(size_t pos,bool withComments,std::string buffer) const |
| 1507 |
{ |
| 1508 |
return this->IsBetweenQuoteChar(pos,withComments,buffer,'"'); |
| 1509 |
} |
| 1510 |
|
| 1511 |
/** return true if the position pos is between " " or ' ' */ |
| 1512 |
bool Parser::IsBetweenQuote(size_t pos,bool withComments,std::string buffer) const |
| 1513 |
{ |
| 1514 |
return ( this->IsBetweenSingleQuote(pos,withComments,buffer) || |
| 1515 |
this->IsBetweenDoubleQuote(pos,withComments,buffer) ); |
| 1516 |
} |
| 1517 |
|
| 1518 |
/** return true if the position pos is between the selected quoteChar (should be ' or ") */ |
| 1519 |
bool Parser::IsBetweenQuoteChar(size_t pos,bool withComments,std::string buffer, char quoteChar) const |
| 1520 |
{ |
| 1521 |
std::string stream = buffer; |
| 1522 |
|
| 1523 |
if (buffer.empty()) { |
| 1524 |
stream = m_BufferNoComment; |
| 1525 |
if(withComments) |
| 1526 |
{ |
| 1527 |
stream = m_Buffer; |
| 1528 |
} |
| 1529 |
} |
| 1530 |
|
| 1531 |
if(pos == std::string::npos) |
| 1532 |
{ |
| 1533 |
return false; |
| 1534 |
} |
| 1535 |
|
| 1536 |
// We don't want to check for \" otherwise it fails |
| 1537 |
size_t b0 = stream.find(quoteChar,0); |
| 1538 |
while((b0!=std::string::npos) && !this->IsValidQuote(stream,b0)) |
| 1539 |
{ |
| 1540 |
b0 = stream.find(quoteChar,b0+1); |
| 1541 |
} |
| 1542 |
|
| 1543 |
size_t b1 = stream.find(quoteChar,b0+1); |
| 1544 |
while((b1!=std::string::npos) && !this->IsValidQuote(stream,b1)) |
| 1545 |
{ |
| 1546 |
b1 = stream.find(quoteChar,b1+1); |
| 1547 |
} |
| 1548 |
|
| 1549 |
while(b0 != std::string::npos && b1 != std::string::npos && b1>b0) |
| 1550 |
{ |
| 1551 |
if(pos>b0 && pos<b1) |
| 1552 |
{ |
| 1553 |
return true; |
| 1554 |
} |
| 1555 |
b0 = stream.find(quoteChar,b1+1); |
| 1556 |
while((b0!=std::string::npos) && !this->IsValidQuote(stream,b0)) |
| 1557 |
{ |
| 1558 |
b0 = stream.find(quoteChar,b0+1); |
| 1559 |
} |
| 1560 |
|
| 1561 |
b1 = stream.find(quoteChar,b0+1); |
| 1562 |
while((b1!=std::string::npos) && !this->IsValidQuote(stream,b1)) |
| 1563 |
{ |
| 1564 |
b1 = stream.find(quoteChar,b1+1); |
| 1565 |
} |
| 1566 |
} |
| 1567 |
return false; |
| 1568 |
} |
| 1569 |
|
| 1570 |
/** return true if the position pos is between 'begin' and 'end' */ |
| 1571 |
bool Parser::IsBetweenCharsFast(const char begin, const char end ,size_t pos,bool withComments,std::string buffer) const |
| 1572 |
{ |
| 1573 |
std::string stream = buffer; |
| 1574 |
if (buffer.empty()) { |
| 1575 |
stream = m_BufferNoComment; |
| 1576 |
if(withComments) |
| 1577 |
{ |
| 1578 |
stream = m_Buffer; |
| 1579 |
} |
| 1580 |
} |
| 1581 |
|
| 1582 |
if(pos == std::string::npos) |
| 1583 |
{ |
| 1584 |
return false; |
| 1585 |
} |
| 1586 |
|
| 1587 |
size_t b0 = stream.find(begin,0); |
| 1588 |
size_t b1 = stream.find(end,b0); |
| 1589 |
|
| 1590 |
while(b0 != std::string::npos && b1 != std::string::npos && b1>b0) |
| 1591 |
{ |
| 1592 |
if(pos>b0 && pos<b1) |
| 1593 |
{ |
| 1594 |
return true; |
| 1595 |
} |
| 1596 |
b0 = stream.find(begin,b0+1); |
| 1597 |
b1 = stream.find(end,b0); |
| 1598 |
} |
| 1599 |
return false; |
| 1600 |
} |
| 1601 |
|
| 1602 |
/** return true if the position pos is between 'begin' and 'end' */ |
| 1603 |
bool Parser::IsBetweenChars(const char begin, const char end ,size_t pos, |
| 1604 |
bool withComments,std::string buffer) const |
| 1605 |
{ |
| 1606 |
std::string stream = buffer; |
| 1607 |
if (buffer.empty()) { |
| 1608 |
stream = m_BufferNoComment; |
| 1609 |
if(withComments) |
| 1610 |
{ |
| 1611 |
stream = m_Buffer; |
| 1612 |
} |
| 1613 |
} |
| 1614 |
|
| 1615 |
if(pos == std::string::npos) |
| 1616 |
{ |
| 1617 |
return false; |
| 1618 |
} |
| 1619 |
|
| 1620 |
size_t b0 = stream.find(begin,0); |
| 1621 |
size_t b1 = this->FindClosingChar(begin,end,b0,!withComments,buffer); |
| 1622 |
|
| 1623 |
while(b0 != std::string::npos && b1 != std::string::npos && b1>b0) |
| 1624 |
{ |
| 1625 |
if(pos>b0 && pos<b1) |
| 1626 |
{ |
| 1627 |
return true; |
| 1628 |
} |
| 1629 |
b0 = stream.find(begin,b0+1); |
| 1630 |
b1 = this->FindClosingChar(begin,end,b0,!withComments,buffer); |
| 1631 |
} |
| 1632 |
return false; |
| 1633 |
} |
| 1634 |
|
| 1635 |
/** Return true if the class has a template */ |
| 1636 |
size_t Parser::IsTemplated(const std::string & buffer, size_t classnamepos) const |
| 1637 |
{ |
| 1638 |
size_t templatepos = buffer.find("template",0); |
| 1639 |
size_t returnval = templatepos; |
| 1640 |
|
| 1641 |
while(templatepos!=std::string::npos && templatepos<classnamepos) |
| 1642 |
{ |
| 1643 |
// find the last > that corresponding to the template |
| 1644 |
size_t p0 = buffer.find("<",templatepos); |
| 1645 |
size_t pos = std::string::npos; |
| 1646 |
if(p0!=std::string::npos) |
| 1647 |
{ |
| 1648 |
size_t i=p0+1; |
| 1649 |
unsigned int n=1; |
| 1650 |
|
| 1651 |
while(i<classnamepos && n>0) |
| 1652 |
{ |
| 1653 |
if(buffer[i] == '<') |
| 1654 |
{ |
| 1655 |
n++; |
| 1656 |
} |
| 1657 |
else if(buffer[i] == '>') |
| 1658 |
{ |
| 1659 |
n--; |
| 1660 |
} |
| 1661 |
if(n==0) |
| 1662 |
{ |
| 1663 |
pos =i; |
| 1664 |
break; |
| 1665 |
} |
| 1666 |
i++; |
| 1667 |
} |
| 1668 |
} |
| 1669 |
|
| 1670 |
if(pos<classnamepos && pos!=std::string::npos) |
| 1671 |
{ |
| 1672 |
bool ok = true; |
| 1673 |
// Check if between the last > and classnamepos we don't have words |
| 1674 |
for(size_t i=pos+1;i<classnamepos;i++) |
| 1675 |
{ |
| 1676 |
if(buffer.c_str()[i] != 13 && buffer.c_str()[i] != '\n' && buffer.c_str()[i]!= '\t') |
| 1677 |
{ |
| 1678 |
ok = false; |
| 1679 |
} |
| 1680 |
} |
| 1681 |
if(ok) |
| 1682 |
{ |
| 1683 |
return templatepos; |
| 1684 |
} |
| 1685 |
/*else |
| 1686 |
{ |
| 1687 |
std::cout << "ERROR in Parser::IsTemplated, Cannot find proper template parameters" << std::endl; |
| 1688 |
system("PAUSE"); |
| 1689 |
}*/ |
| 1690 |
} |
| 1691 |
returnval = templatepos; |
| 1692 |
templatepos = buffer.find("template",templatepos+1); |
| 1693 |
} |
| 1694 |
|
| 1695 |
return returnval; |
| 1696 |
} |
| 1697 |
|
| 1698 |
/** Remove the comments */ |
| 1699 |
std::string Parser::RemoveComments(const char* buffer) |
| 1700 |
{ |
| 1701 |
if(!buffer) |
| 1702 |
{ |
| 1703 |
m_CommentPositions.clear(); |
| 1704 |
} |
| 1705 |
size_t cc; |
| 1706 |
const char* inch; |
| 1707 |
if(buffer) |
| 1708 |
{ |
| 1709 |
inch = buffer; |
| 1710 |
} |
| 1711 |
else |
| 1712 |
{ |
| 1713 |
inch = m_Buffer.c_str(); |
| 1714 |
} |
| 1715 |
|
| 1716 |
std::vector<char> outBuffer; |
| 1717 |
size_t inStrSize = m_Buffer.size(); |
| 1718 |
if(buffer) |
| 1719 |
{ |
| 1720 |
std::string tempbuf = buffer; |
| 1721 |
inStrSize = tempbuf.size(); |
| 1722 |
} |
| 1723 |
// Reserve enough space for all files. The size will be at least the size of |
| 1724 |
// the file. |
| 1725 |
outBuffer.reserve(inStrSize+1); |
| 1726 |
|
| 1727 |
bool inString = false; |
| 1728 |
bool inComment = false; |
| 1729 |
bool copyChar; |
| 1730 |
bool cppComment = false; |
| 1731 |
|
| 1732 |
size_t beginOfComment = 0; |
| 1733 |
size_t endOfComment = 0; |
| 1734 |
|
| 1735 |
for ( cc = 0; cc < inStrSize; ++ cc ) |
| 1736 |
{ |
| 1737 |
copyChar = true; |
| 1738 |
|
| 1739 |
// it is important to know if we are in a string---if we are then |
| 1740 |
// we don't care about comment-ish symbols |
| 1741 |
// |
| 1742 |
// we are starting or ending a string if we find a " without a |
| 1743 |
// preceeding '\' |
| 1744 |
bool isStringBarrier = (*inch == '"') && (cc == 0 || *(inch-1) != '\\'); |
| 1745 |
inString = isStringBarrier ? !inString : inString; |
| 1746 |
|
| 1747 |
if (!inString) |
| 1748 |
{ |
| 1749 |
if ( !inComment ) |
| 1750 |
{ |
| 1751 |
if ( *inch == '/' ) |
| 1752 |
{ |
| 1753 |
if ( cc < inStrSize-1 ) |
| 1754 |
{ |
| 1755 |
if ( *(inch+1) == '*' ) |
| 1756 |
{ |
| 1757 |
inComment = true; |
| 1758 |
beginOfComment = cc; |
| 1759 |
} |
| 1760 |
else if ( *(inch+1) == '/' ) |
| 1761 |
{ |
| 1762 |
inComment = true; |
| 1763 |
beginOfComment = cc; |
| 1764 |
cppComment = true; |
| 1765 |
} |
| 1766 |
} |
| 1767 |
} |
| 1768 |
} |
| 1769 |
else |
| 1770 |
{ |
| 1771 |
if ( cppComment && *inch == '\n' ) |
| 1772 |
{ |
| 1773 |
endOfComment = cc; |
| 1774 |
inComment = false; |
| 1775 |
copyChar = true; |
| 1776 |
cppComment = false; |
| 1777 |
PairType pair(beginOfComment, endOfComment); |
| 1778 |
if(!buffer) |
| 1779 |
{ |
| 1780 |
m_CommentPositions.push_back(pair); |
| 1781 |
} |
| 1782 |
} |
| 1783 |
if ( *inch == '/' ) |
| 1784 |
{ |
| 1785 |
if ( cc != std::string::npos && *(inch-1) == '*' ) |
| 1786 |
{ |
| 1787 |
endOfComment = cc+1; |
| 1788 |
inComment = false; |
| 1789 |
copyChar = false; |
| 1790 |
PairType pair(beginOfComment, endOfComment); |
| 1791 |
if(!buffer) |
| 1792 |
{ |
| 1793 |
m_CommentPositions.push_back(pair); |
| 1794 |
} |
| 1795 |
} |
| 1796 |
} |
| 1797 |
} |
| 1798 |
} |
| 1799 |
if ( inComment ) |
| 1800 |
{ |
| 1801 |
copyChar = false; |
| 1802 |
} |
| 1803 |
if ( copyChar ) |
| 1804 |
{ |
| 1805 |
outBuffer.push_back(*inch); |
| 1806 |
} |
| 1807 |
inch++; |
| 1808 |
} |
| 1809 |
outBuffer.push_back(0); |
| 1810 |
if(buffer) |
| 1811 |
{ |
| 1812 |
return &*outBuffer.begin(); |
| 1813 |
} |
| 1814 |
else |
| 1815 |
{ |
| 1816 |
m_BufferNoComment = &*outBuffer.begin(); |
| 1817 |
} |
| 1818 |
|
| 1819 |
return ""; |
| 1820 |
} |
| 1821 |
|
| 1822 |
/** Find the constructor in the file */ |
| 1823 |
size_t Parser::FindConstructor(const std::string & buffer, const std::string & className, |
| 1824 |
bool headerfile, size_t startPos) const |
| 1825 |
{ |
| 1826 |
std::string constructor = ""; |
| 1827 |
|
| 1828 |
if(headerfile) |
| 1829 |
{ |
| 1830 |
constructor = " "; |
| 1831 |
} |
| 1832 |
else |
| 1833 |
{ |
| 1834 |
constructor = ":"; |
| 1835 |
} |
| 1836 |
constructor += className; |
| 1837 |
size_t pos = buffer.find(constructor.c_str(),startPos); |
| 1838 |
size_t pos2 = buffer.find("(",pos); |
| 1839 |
size_t pos3 = buffer.find(")",pos2); |
| 1840 |
|
| 1841 |
// if it's not the destructor ... |
| 1842 |
/*std::string dest = "~"; |
| 1843 |
dest += className; |
| 1844 |
long destructor = buffer.find(dest.c_str(),0);*/ |
| 1845 |
while(pos != std::string::npos && pos2 != std::string::npos && pos3 != std::string::npos) |
| 1846 |
{ |
| 1847 |
//if(pos != destructor+1) // we have the constructor |
| 1848 |
{ |
| 1849 |
// we look if we have only spaces or \n between pos pos2 and pos3 |
| 1850 |
std::string val = buffer.substr(pos+className.size()+1,pos2-pos-className.size()-1); |
| 1851 |
bool ok = true; |
| 1852 |
for (char i : val) { |
| 1853 |
if (i != ' ' || i != '\n') { |
| 1854 |
ok =false; |
| 1855 |
} |
| 1856 |
} |
| 1857 |
|
| 1858 |
bool ok2 = false; |
| 1859 |
if(ok) |
| 1860 |
{ |
| 1861 |
ok2 = true; |
| 1862 |
val = buffer.substr(pos2+1,pos3-pos2-1); |
| 1863 |
for (char i : val) { |
| 1864 |
if (i != ' ' || i != '\n') { |
| 1865 |
ok2 =false; |
| 1866 |
} |
| 1867 |
} |
| 1868 |
} |
| 1869 |
|
| 1870 |
// if ok2 then we check if the constructor is implemented |
| 1871 |
if(ok2) |
| 1872 |
{ |
| 1873 |
size_t pos4 = buffer.find(";",pos3); |
| 1874 |
size_t pos5 = buffer.find("{",pos3); |
| 1875 |
|
| 1876 |
if(pos5<pos4) |
| 1877 |
{ |
| 1878 |
return pos5; |
| 1879 |
} |
| 1880 |
return std::string::npos; |
| 1881 |
} |
| 1882 |
} |
| 1883 |
std::string localconstructor = ""; |
| 1884 |
if(headerfile) |
| 1885 |
{ |
| 1886 |
localconstructor = " "; |
| 1887 |
} |
| 1888 |
else |
| 1889 |
{ |
| 1890 |
localconstructor = ":"; |
| 1891 |
} |
| 1892 |
localconstructor += className; |
| 1893 |
pos = buffer.find(localconstructor.c_str(),pos+1); |
| 1894 |
pos2 = buffer.find("(",pos); |
| 1895 |
pos3 = buffer.find(")",pos2); |
| 1896 |
} |
| 1897 |
|
| 1898 |
return std::string::npos; |
| 1899 |
} |
| 1900 |
|
| 1901 |
/** Find the closing char given the position of the opening char */ |
| 1902 |
size_t Parser::FindClosingChar(char openChar, char closeChar, |
| 1903 |
size_t pos,bool noComment,std::string buffer) const |
| 1904 |
{ |
| 1905 |
std::string stream = buffer; |
| 1906 |
if (buffer.empty()) { |
| 1907 |
stream = m_Buffer.c_str(); |
| 1908 |
if(noComment) |
| 1909 |
{ |
| 1910 |
stream = m_BufferNoComment.c_str(); |
| 1911 |
} |
| 1912 |
} |
| 1913 |
|
| 1914 |
size_t open = 1; |
| 1915 |
for(size_t i=pos+1;i<stream.length();i++) |
| 1916 |
{ |
| 1917 |
if(stream[i] == openChar || stream[i] == closeChar) |
| 1918 |
{ |
| 1919 |
bool skip = false; |
| 1920 |
// We want to check that we are not in the #if/#else/#endif thing |
| 1921 |
auto itLS = m_IfElseEndifList.begin(); |
| 1922 |
while(itLS != m_IfElseEndifList.end()) |
| 1923 |
{ |
| 1924 |
size_t j = i; |
| 1925 |
if(noComment) |
| 1926 |
{ |
| 1927 |
j = this->GetPositionWithoutComments(i); |
| 1928 |
} |
| 1929 |
if(j>(*itLS).first && j<(*itLS).second) |
| 1930 |
{ |
| 1931 |
skip = true; |
| 1932 |
break; |
| 1933 |
} |
| 1934 |
itLS++; |
| 1935 |
} |
| 1936 |
|
| 1937 |
if(!skip) |
| 1938 |
{ |
| 1939 |
if(stream[i] == openChar) |
| 1940 |
{ |
| 1941 |
open++; |
| 1942 |
} |
| 1943 |
else if(stream[i] == closeChar) |
| 1944 |
{ |
| 1945 |
open--; |
| 1946 |
} |
| 1947 |
} |
| 1948 |
} |
| 1949 |
if(open == 0) |
| 1950 |
{ |
| 1951 |
return i; |
| 1952 |
} |
| 1953 |
} |
| 1954 |
return std::string::npos; // closing char not found |
| 1955 |
} |
| 1956 |
|
| 1957 |
|
| 1958 |
/** Compute the list of #if/#else/#endif |
| 1959 |
* In the case we have #if/#else/#endif we want to ignore the #else section |
| 1960 |
* if we have some '{' not closed |
| 1961 |
* #if |
| 1962 |
* { |
| 1963 |
* #else |
| 1964 |
* { |
| 1965 |
* #endif */ |
| 1966 |
void Parser::ComputeIfElseEndifList() |
| 1967 |
{ |
| 1968 |
m_IfElseEndifList.clear(); |
| 1969 |
size_t posSharpElse = m_BufferNoComment.find("#else",0); |
| 1970 |
while(posSharpElse != std::string::npos) |
| 1971 |
{ |
| 1972 |
// Find the corresponding #endif |
| 1973 |
size_t posSharpEndif = m_BufferNoComment.find("#endif",posSharpElse); |
| 1974 |
while(posSharpEndif != std::string::npos) |
| 1975 |
{ |
| 1976 |
// Look if we have any #if between the #else and #endif |
| 1977 |
// Count the number of #if and #endif |
| 1978 |
int nIf = 0; |
| 1979 |
size_t posIf = m_BufferNoComment.find("#if",posSharpElse); |
| 1980 |
while(posIf<posSharpEndif && posIf!=std::string::npos) |
| 1981 |
{ |
| 1982 |
posIf = m_BufferNoComment.find("#if",posIf+1); |
| 1983 |
nIf++; |
| 1984 |
} |
| 1985 |
|
| 1986 |
int nEndIf = 0; |
| 1987 |
size_t posEndif = m_BufferNoComment.find("#endif",posSharpElse); |
| 1988 |
while(posEndif<posSharpEndif && posEndif!=std::string::npos) |
| 1989 |
{ |
| 1990 |
posEndif = m_BufferNoComment.find("#endif",posEndif+1); |
| 1991 |
nEndIf++; |
| 1992 |
} |
| 1993 |
|
| 1994 |
if(nEndIf == nIf) |
| 1995 |
{ |
| 1996 |
break; |
| 1997 |
} |
| 1998 |
|
| 1999 |
// Then continue to search*/ |
| 2000 |
posSharpEndif = m_BufferNoComment.find("#endif",posSharpEndif+1); |
| 2001 |
} |
| 2002 |
|
| 2003 |
if(posSharpEndif != std::string::npos) |
| 2004 |
{ |
| 2005 |
// Search for the corresponding if |
| 2006 |
size_t posSharpIf = m_BufferNoComment.find("#if",0); |
| 2007 |
while(posSharpIf != std::string::npos) |
| 2008 |
{ |
| 2009 |
size_t posSharpIf2 = m_BufferNoComment.find("#if",posSharpIf+1); |
| 2010 |
if(posSharpIf2>posSharpElse || posSharpIf2==std::string::npos) |
| 2011 |
{ |
| 2012 |
break; |
| 2013 |
} |
| 2014 |
posSharpIf = posSharpIf2; |
| 2015 |
} |
| 2016 |
|
| 2017 |
if(posSharpIf != std::string::npos) |
| 2018 |
{ |
| 2019 |
// We check if the total number of '{' is equal to the total number of '}' |
| 2020 |
// in the #if/#else/#endif section |
| 2021 |
int nOpen = 0; |
| 2022 |
int nClose = 0; |
| 2023 |
|
| 2024 |
size_t posOpen = m_BufferNoComment.find("{",posSharpIf); |
| 2025 |
while(posOpen != std::string::npos && posOpen<posSharpEndif) |
| 2026 |
{ |
| 2027 |
nOpen++; |
| 2028 |
posOpen = m_BufferNoComment.find("{",posOpen+1); |
| 2029 |
} |
| 2030 |
|
| 2031 |
size_t posClose = m_BufferNoComment.find("}",posSharpIf); |
| 2032 |
while(posClose != std::string::npos && posClose<posSharpEndif) |
| 2033 |
{ |
| 2034 |
nClose++; |
| 2035 |
posClose = m_BufferNoComment.find("}",posClose+1); |
| 2036 |
} |
| 2037 |
|
| 2038 |
if(nOpen != nClose) |
| 2039 |
{ |
| 2040 |
IfElseEndifPairType p; |
| 2041 |
p.first = this->GetPositionWithComments(posSharpElse-1); |
| 2042 |
p.second = this->GetPositionWithComments(posSharpEndif); |
| 2043 |
m_IfElseEndifList.push_back(p); |
| 2044 |
} |
| 2045 |
} |
| 2046 |
} |
| 2047 |
posSharpElse = m_BufferNoComment.find("#else",posSharpElse+1); |
| 2048 |
} |
| 2049 |
} |
| 2050 |
|
| 2051 |
/** Find the opening char given the position of the closing char */ |
| 2052 |
size_t Parser::FindOpeningChar(char closeChar, char openChar,size_t pos,bool noComment) const |
| 2053 |
{ |
| 2054 |
if(pos == std::string::npos) |
| 2055 |
{ |
| 2056 |
return std::string::npos; |
| 2057 |
} |
| 2058 |
|
| 2059 |
std::string stream = m_Buffer.c_str(); |
| 2060 |
if(noComment) |
| 2061 |
{ |
| 2062 |
stream = m_BufferNoComment.c_str(); |
| 2063 |
} |
| 2064 |
|
| 2065 |
long int close = 1; |
| 2066 |
for(size_t i=pos-1;i!=std::string::npos;i--) |
| 2067 |
{ |
| 2068 |
if(stream[i] == closeChar) |
| 2069 |
{ |
| 2070 |
close++; |
| 2071 |
} |
| 2072 |
else if(stream[i] == openChar) |
| 2073 |
{ |
| 2074 |
close--; |
| 2075 |
} |
| 2076 |
else |
| 2077 |
{ |
| 2078 |
continue; |
| 2079 |
} |
| 2080 |
if(close == 0) |
| 2081 |
{ |
| 2082 |
return i; |
| 2083 |
} |
| 2084 |
} |
| 2085 |
return std::string::npos; // opening char not found |
| 2086 |
} |
| 2087 |
|
| 2088 |
/** Generate the fixed file */ |
| 2089 |
void Parser::GenerateFixedFile() |
| 2090 |
{ |
| 2091 |
if(!this->m_FixFile) |
| 2092 |
{ |
| 2093 |
return; |
| 2094 |
} |
| 2095 |
|
| 2096 |
if (m_ErrorList.empty()) { |
| 2097 |
std::cout << "No error. Not generating corrected file." << std::endl; |
| 2098 |
return; |
| 2099 |
} |
| 2100 |
|
| 2101 |
std::string filename = kwssys::SystemTools::GetFilenameWithoutExtension(m_Filename.c_str()); |
| 2102 |
filename += ".fixed"; |
| 2103 |
filename += kwssys::SystemTools::GetFilenameExtension(m_Filename.c_str()); |
| 2104 |
|
| 2105 |
std::cout << "Generating corrected file: " << filename.c_str() << std::endl; |
| 2106 |
|
| 2107 |
std::ofstream file; |
| 2108 |
file.open(filename.c_str(), std::ios::binary | std::ios::out); |
| 2109 |
if(!file.is_open()) |
| 2110 |
{ |
| 2111 |
std::cout << "Cannot open file for writing: " << std::endl; |
| 2112 |
return; |
| 2113 |
} |
| 2114 |
|
| 2115 |
file << this->m_FixedBuffer; |
| 2116 |
|
| 2117 |
file.close(); |
| 2118 |
} |
| 2119 |
|
| 2120 |
/** Functions to deal with the fixed buffer */ |
| 2121 |
void Parser::ReplaceCharInFixedBuffer(size_t pos,size_t size,const char* replacingString) |
| 2122 |
{ |
| 2123 |
std::vector<PairType>::const_iterator it = m_FixedPositions.begin(); |
| 2124 |
while(it != m_FixedPositions.end()) |
| 2125 |
{ |
| 2126 |
if((*it).first<pos) |
| 2127 |
{ |
| 2128 |
pos += (*it).second; |
| 2129 |
} |
| 2130 |
it++; |
| 2131 |
} |
| 2132 |
m_FixedBuffer.replace(pos,size,replacingString); |
| 2133 |
|
| 2134 |
// Keep track of the current position history |
| 2135 |
size_t sizeNewChar = strlen(replacingString); |
| 2136 |
if(sizeNewChar != size) |
| 2137 |
{ |
| 2138 |
PairType p; |
| 2139 |
p.first = pos; |
| 2140 |
p.second = sizeNewChar-size; |
| 2141 |
m_FixedPositions.push_back(p); |
| 2142 |
} |
| 2143 |
} |
| 2144 |
|
| 2145 |
} // end namespace kws |