| 1 |
/*======================================================================== |
| 2 |
|
| 3 |
Program: KWStyle - Kitware Style Checker |
| 4 |
Module: kwsStyle.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 |
#include "kwsGenerator.h" |
| 16 |
#include "metaCommand.h" |
| 17 |
#include <iostream> |
| 18 |
#include <fstream> |
| 19 |
#include <kwssys/Directory.hxx> |
| 20 |
#include <kwssys/SystemTools.hxx> |
| 21 |
#include <kwssys/Glob.hxx> |
| 22 |
#include <cmath> |
| 23 |
#include <string.h> |
| 24 |
#include <sstream> |
| 25 |
#include "kwsXMLReader.h" |
| 26 |
#include <vector> |
| 27 |
#include "KWStyleConfigure.h" |
| 28 |
|
| 29 |
#if defined(WIN32) && !defined(__CYGWIN__) |
| 30 |
#include <direct.h> // mkdir needs it |
| 31 |
#endif |
| 32 |
|
| 33 |
struct kwsFeature{ |
| 34 |
std::string name; |
| 35 |
std::string value; |
| 36 |
std::string filename; |
| 37 |
bool enable; |
| 38 |
}; |
| 39 |
|
| 40 |
// List of features to check |
| 41 |
static std::vector<kwsFeature> features; |
| 42 |
static std::vector<kwsFeature> overwriteFeatures; |
| 43 |
|
| 44 |
static void AddFeature(const char* name,const char* value,bool enable) |
| 45 |
{ |
| 46 |
kwsFeature f; |
| 47 |
f.name = name; |
| 48 |
f.value = value; |
| 49 |
f.enable = enable; |
| 50 |
f.filename = ""; |
| 51 |
features.push_back(f); |
| 52 |
} |
| 53 |
|
| 54 |
static void ChangeFeature(const char* name,const char* value) |
| 55 |
{ |
| 56 |
auto it = features.begin(); |
| 57 |
while(it != features.end()) |
| 58 |
{ |
| 59 |
if(!strcmp((*it).name.c_str(),name)) |
| 60 |
{ |
| 61 |
(*it).value = value; |
| 62 |
return; |
| 63 |
} |
| 64 |
++it; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
static void DisableFeature(const char* name) |
| 69 |
{ |
| 70 |
auto it = features.begin(); |
| 71 |
while(it != features.end()) |
| 72 |
{ |
| 73 |
if(!strcmp((*it).name.c_str(),name)) |
| 74 |
{ |
| 75 |
(*it).enable = false; |
| 76 |
return; |
| 77 |
} |
| 78 |
++it; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
/** Remove files */ |
| 83 |
static void RemoveFile(const char* regEx,std::vector<std::string> & filenames) |
| 84 |
{ |
| 85 |
kwssys::RegularExpression regex(regEx); |
| 86 |
auto it = filenames.begin(); |
| 87 |
|
| 88 |
while(it != filenames.end()) |
| 89 |
{ |
| 90 |
if(regex.find((*it).c_str())) |
| 91 |
{ |
| 92 |
it = filenames.erase(it); |
| 93 |
} |
| 94 |
else |
| 95 |
{ |
| 96 |
++it; |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
int main(int argc, const char **argv) |
| 102 |
{ |
| 103 |
double time0 = kwssys::SystemTools::GetTime(); |
| 104 |
|
| 105 |
MetaCommand command; |
| 106 |
command.SetVersion(KWSTYLE_VERSION_STRING); |
| 107 |
|
| 108 |
// Check if -cvs is defined and put MetaCommand in quiet mode |
| 109 |
/*for(int j=0;j<argc;j++) |
| 110 |
{ |
| 111 |
if(!strcmp(argv[j],"-cvs")) |
| 112 |
{ |
| 113 |
command.SetVerbose(false); |
| 114 |
} |
| 115 |
}*/ |
| 116 |
|
| 117 |
command.SetOption("directory","d",false,"Specify a directory"); |
| 118 |
command.SetOption("recursive","R",false,"Associated with -d recurse through directories"); |
| 119 |
command.SetOption("verbose","v",false,"Display errors"); |
| 120 |
command.SetOption("quiteverbose","qv",false,"Display less information"); |
| 121 |
command.SetOption("lesshtml","lesshtml",false,"Display less HTML"); |
| 122 |
|
| 123 |
command.SetOption("vim","vim",false,"Generate errors as VIM format"); |
| 124 |
command.SetOption("msvc","msvc",false,"Generate errors as MSVC format"); |
| 125 |
command.SetOption("gcc","gcc",false,"Generate errors as GCC format"); |
| 126 |
|
| 127 |
command.SetOption("html","html",false,"Generate the HTML report"); |
| 128 |
command.AddOptionField("html","filename",MetaCommand::STRING,false); |
| 129 |
command.SetOption("cvs","cvs",false,"Using KWStyle as a cvs precommit script"); |
| 130 |
command.SetOption("exporthtml","exporthtml",false,"Export the HTML report online"); |
| 131 |
command.SetOption("xml","xml",false,"Read a XML configure file"); |
| 132 |
command.AddOptionField("xml","filename",MetaCommand::STRING,false); |
| 133 |
command.SetOption("exportxml","exportxml",false,"Write output the report as a simple XML"); |
| 134 |
command.AddOptionField("exportxml","filename",MetaCommand::STRING,false); |
| 135 |
|
| 136 |
command.SetOption("overwrite","o",false,"Overwrite rules file"); |
| 137 |
command.AddOptionField("overwrite","filename",MetaCommand::STRING,false); |
| 138 |
|
| 139 |
command.SetOption("dirfile","D",false,"Specify a file listing all the directories"); |
| 140 |
command.SetOption("basedirectory","B",false,"Specify the base directory of the file"); |
| 141 |
command.AddOptionField("basedirectory","filename",MetaCommand::STRING,false); |
| 142 |
|
| 143 |
command.SetOption("blacklist","b",false,"Specify a black list of words"); |
| 144 |
command.AddOptionField("blacklist","filename",MetaCommand::STRING,false); |
| 145 |
command.SetOption("dart","dart",false,"Write out files to be send to the dart server"); |
| 146 |
command.AddOptionField("dart","filename",MetaCommand::STRING,true); |
| 147 |
command.AddOptionField("dart","maxerror",MetaCommand::INT,false,"-1"); |
| 148 |
command.AddOptionField("dart","group",MetaCommand::INT,false,"0"); |
| 149 |
|
| 150 |
|
| 151 |
command.SetOption("kwsurl","kwsurl",false,"Specify the base url of the KWStyle HTML report"); |
| 152 |
command.AddOptionField("kwsurl","url",MetaCommand::STRING,true); |
| 153 |
|
| 154 |
command.SetOption("fixFile","fix",false,"Write out a fixed version of the parsed file"); |
| 155 |
|
| 156 |
command.AddField("infile","input filename",MetaCommand::STRING,true); |
| 157 |
|
| 158 |
// Parsing |
| 159 |
if(!command.Parse(argc,argv)) |
| 160 |
{ |
| 161 |
return 1; |
| 162 |
} |
| 163 |
|
| 164 |
// Add the features |
| 165 |
AddFeature("Header","dummyheader.h,false,true",true); // should be first |
| 166 |
AddFeature("LineLength","80",true); |
| 167 |
AddFeature("DeclarationOrder","0,1,2",true); |
| 168 |
AddFeature("Typedefs","[A-Z],true",true); |
| 169 |
AddFeature("InternalVariables","m_[A-Z],true",true); |
| 170 |
AddFeature("Variables","this->[A-Z]",true); |
| 171 |
AddFeature("Struct","[a-z]",true); |
| 172 |
AddFeature("SemicolonSpace","0",true); |
| 173 |
AddFeature("EndOfFileNewLine","",true); |
| 174 |
AddFeature("Tabs","",true); |
| 175 |
AddFeature("Spaces","3",true); |
| 176 |
AddFeature("Comments","/**, *, */,true,true,true",true); |
| 177 |
AddFeature("Indent","kws::SPACE,2,true,true",true); |
| 178 |
AddFeature("Namespace","itk",true); |
| 179 |
AddFeature("NameOfClass","[NameOfClass],itk",true); |
| 180 |
AddFeature("IfNDefDefine","__[NameOfClass]_[Extension]",true); |
| 181 |
AddFeature("EmptyLines","2",true); |
| 182 |
AddFeature("Template","T",true); |
| 183 |
AddFeature("Operator","1,1",true); |
| 184 |
AddFeature("StatementPerLine","1",true); |
| 185 |
AddFeature("VariablePerLine","1",true); |
| 186 |
AddFeature("BadCharacters","true",true); |
| 187 |
AddFeature("MemberFunctions","*",true); |
| 188 |
AddFeature("Functions","*",true); |
| 189 |
AddFeature("UsingDirectives", "true", true); |
| 190 |
AddFeature("RelativePathInInclude", "true", true); |
| 191 |
AddFeature("IfWhileForUntil","",true); |
| 192 |
AddFeature("Comma","",true); |
| 193 |
AddFeature("Parenthesis","",true); |
| 194 |
|
| 195 |
std::string xmlFile = "KWStyle.xml"; |
| 196 |
// If we should look the definition from the xml file |
| 197 |
if(command.GetOptionWasSet("xml")) |
| 198 |
{ |
| 199 |
xmlFile = command.GetValueAsString("xml","filename"); |
| 200 |
} |
| 201 |
|
| 202 |
// Initialize the configuration parameters |
| 203 |
std::string blacklist = ""; |
| 204 |
std::string overwrite = ""; |
| 205 |
std::string recursive = ""; |
| 206 |
std::string verbose = ""; |
| 207 |
std::string htmlDirectory = ""; |
| 208 |
std::string lessHTML = ""; |
| 209 |
std::string filesToCheck = ""; |
| 210 |
std::string directoryToCheck = ""; |
| 211 |
std::string fileToCheck = ""; |
| 212 |
std::string fixedFile = ""; |
| 213 |
|
| 214 |
if (!xmlFile.empty()) { |
| 215 |
kws::XMLReader reader; |
| 216 |
if(reader.Open(xmlFile.c_str())) |
| 217 |
{ |
| 218 |
auto it = features.begin(); |
| 219 |
while(it != features.end()) |
| 220 |
{ |
| 221 |
std::string val = reader.GetValue((*it).name.c_str()); |
| 222 |
if(!val.empty()) |
| 223 |
{ |
| 224 |
ChangeFeature((*it).name.c_str(),val.c_str()); |
| 225 |
} |
| 226 |
else |
| 227 |
{ |
| 228 |
DisableFeature((*it).name.c_str()); |
| 229 |
} |
| 230 |
++it; |
| 231 |
} |
| 232 |
|
| 233 |
// Read the current configuration |
| 234 |
blacklist = reader.GetValue("BlackList"); |
| 235 |
overwrite = reader.GetValue("OverwriteFile"); |
| 236 |
verbose = reader.GetValue("Verbose"); |
| 237 |
if(!verbose.empty() && verbose[0] != '1') |
| 238 |
{ |
| 239 |
verbose = ""; |
| 240 |
} |
| 241 |
|
| 242 |
recursive = reader.GetValue("Recursive"); |
| 243 |
if(!recursive.empty() && recursive[0] != '1') |
| 244 |
{ |
| 245 |
recursive = ""; |
| 246 |
} |
| 247 |
htmlDirectory = reader.GetValue("HTMLDirectory"); |
| 248 |
lessHTML = reader.GetValue("LessHTML"); |
| 249 |
filesToCheck = reader.GetValue("Files"); |
| 250 |
directoryToCheck = reader.GetValue("Directory"); |
| 251 |
fileToCheck = reader.GetValue("File"); |
| 252 |
|
| 253 |
reader.Close(); |
| 254 |
} |
| 255 |
else |
| 256 |
{ |
| 257 |
std::cout << "Cannot open configuration file: " << xmlFile.c_str() << std::endl; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
if(command.GetOptionWasSet("blacklist")) |
| 262 |
{ |
| 263 |
blacklist = command.GetValueAsString("blacklist","filename"); |
| 264 |
} |
| 265 |
if(!blacklist.empty()) |
| 266 |
{ |
| 267 |
AddFeature("BlackList",blacklist.c_str(),true); |
| 268 |
} |
| 269 |
|
| 270 |
// If we should look at some overwritten rules |
| 271 |
if(command.GetOptionWasSet("overwrite")) |
| 272 |
{ |
| 273 |
overwrite = command.GetValueAsString("overwrite","filename"); |
| 274 |
} |
| 275 |
|
| 276 |
if(!overwrite.empty()) |
| 277 |
{ |
| 278 |
// Read the file |
| 279 |
std::ifstream file; |
| 280 |
file.open(overwrite.c_str(), std::ios::binary | std::ios::in); |
| 281 |
if(!file.is_open()) |
| 282 |
{ |
| 283 |
std::cout << "Cannot open overwrite file: " << overwrite.c_str() << std::endl; |
| 284 |
return 0; |
| 285 |
} |
| 286 |
file.seekg(0,std::ios::end); |
| 287 |
std::streamsize fileSize = file.tellg(); |
| 288 |
file.seekg(0,std::ios::beg); |
| 289 |
|
| 290 |
char* buf = new char[fileSize+1]; |
| 291 |
file.read(buf, fileSize); |
| 292 |
buf[fileSize] = 0; |
| 293 |
std::string buffer(buf); |
| 294 |
buffer.resize(static_cast<size_t>(fileSize)); |
| 295 |
delete [] buf; |
| 296 |
|
| 297 |
size_t start = 0; |
| 298 |
auto pos = buffer.find("\n", start); |
| 299 |
do |
| 300 |
{ |
| 301 |
std::string line = ""; |
| 302 |
|
| 303 |
if(pos == std::string::npos) |
| 304 |
{ |
| 305 |
line = buffer.substr(start,buffer.length()- start); |
| 306 |
pos = static_cast<size_t>(fileSize); // we stop |
| 307 |
} |
| 308 |
else |
| 309 |
{ |
| 310 |
line = buffer.substr(start,pos-start); |
| 311 |
start = pos+1; |
| 312 |
} |
| 313 |
if(line.size() < 2) |
| 314 |
{ |
| 315 |
break; |
| 316 |
} |
| 317 |
|
| 318 |
auto p = line.find(" "); |
| 319 |
if (p != std::string::npos) { |
| 320 |
kwsFeature f; |
| 321 |
f.filename = line.substr(0, p); |
| 322 |
size_t p1 = p; |
| 323 |
p = line.find(" ",p + 1); |
| 324 |
if (p != std::string::npos) { |
| 325 |
f.name = line.substr(p1 + 1,p - p1 - 1); |
| 326 |
} |
| 327 |
p1 = p; |
| 328 |
p = line.find(" ",p+1); |
| 329 |
std::string enablestring = line.substr(p1+1,p-p1-1); |
| 330 |
f.enable = enablestring.find("Enable") != std::string::npos; |
| 331 |
|
| 332 |
p1 = p; |
| 333 |
p = line.find("\n",p+1); |
| 334 |
|
| 335 |
if(p != std::string::npos) |
| 336 |
{ |
| 337 |
f.value = line.substr( p1+1,p-p1-1); |
| 338 |
} |
| 339 |
|
| 340 |
overwriteFeatures.push_back(f); |
| 341 |
} |
| 342 |
|
| 343 |
if(static_cast<std::streamsize>(pos) != fileSize) |
| 344 |
{ |
| 345 |
pos = buffer.find("\n",start); |
| 346 |
} |
| 347 |
} while(static_cast<std::streamsize>(pos) < fileSize); |
| 348 |
|
| 349 |
file.close(); |
| 350 |
} |
| 351 |
|
| 352 |
std::string inputFilename = command.GetValueAsString("infile"); |
| 353 |
|
| 354 |
if (!fileToCheck.empty()) { |
| 355 |
inputFilename = fileToCheck; |
| 356 |
} |
| 357 |
|
| 358 |
if(command.GetOptionWasSet("recursive")) |
| 359 |
{ |
| 360 |
recursive = "1"; |
| 361 |
} |
| 362 |
// if the -D command is used |
| 363 |
if(command.GetOptionWasSet("dirfile")) |
| 364 |
{ |
| 365 |
filesToCheck = inputFilename; |
| 366 |
} |
| 367 |
|
| 368 |
if(command.GetValueAsString("directory").length() > 0) |
| 369 |
{ |
| 370 |
directoryToCheck = inputFilename; |
| 371 |
if(directoryToCheck[directoryToCheck.size()-1] != '/') |
| 372 |
{ |
| 373 |
directoryToCheck += '/'; |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
std::vector<std::string> filenames; |
| 378 |
std::vector<kws::Parser> m_Parsers; |
| 379 |
|
| 380 |
// if the -d command is used |
| 381 |
if (!directoryToCheck.empty()) { |
| 382 |
kwssys::Glob glob; |
| 383 |
if (!recursive.empty()) { |
| 384 |
glob.RecurseOn(); |
| 385 |
} |
| 386 |
std::string globoption = directoryToCheck.c_str(); |
| 387 |
|
| 388 |
if(kwssys::SystemTools::FileExists(directoryToCheck.c_str())) |
| 389 |
{ |
| 390 |
globoption += "*.*"; |
| 391 |
} |
| 392 |
|
| 393 |
glob.FindFiles(globoption.c_str()); |
| 394 |
filenames = glob.GetFiles(); |
| 395 |
} else if (!filesToCheck.empty()) { |
| 396 |
// Read the file |
| 397 |
std::ifstream file; |
| 398 |
file.open(filesToCheck.c_str(), std::ios::binary | std::ios::in); |
| 399 |
if(!file.is_open()) |
| 400 |
{ |
| 401 |
std::cout << "Cannot open description files: " << filesToCheck.c_str() << std::endl; |
| 402 |
return 0; |
| 403 |
} |
| 404 |
file.seekg(0,std::ios::end); |
| 405 |
std::streamsize fileSize = file.tellg(); |
| 406 |
file.seekg(0,std::ios::beg); |
| 407 |
|
| 408 |
char* buf = new char[fileSize+1]; |
| 409 |
file.read(buf,fileSize); |
| 410 |
buf[fileSize] = 0; |
| 411 |
std::string buffer(buf); |
| 412 |
buffer.resize(static_cast<size_t>(fileSize)); |
| 413 |
delete [] buf; |
| 414 |
|
| 415 |
size_t start = 0; |
| 416 |
size_t pos = buffer.find("\n",start); |
| 417 |
size_t posr = buffer.find("\r",start); |
| 418 |
if(posr == std::string::npos) |
| 419 |
{ |
| 420 |
posr = pos; |
| 421 |
} |
| 422 |
|
| 423 |
// Define a parser to find if we are between quotes |
| 424 |
kws::Parser helperParser; |
| 425 |
|
| 426 |
do |
| 427 |
{ |
| 428 |
std::string dirname = ""; |
| 429 |
|
| 430 |
if(pos == std::string::npos) |
| 431 |
{ |
| 432 |
dirname = buffer.substr(start,buffer.length()- start); |
| 433 |
pos = static_cast<size_t>(fileSize); // we stop |
| 434 |
} |
| 435 |
else |
| 436 |
{ |
| 437 |
dirname = buffer.substr( start ,posr - start); |
| 438 |
start = pos+1; |
| 439 |
} |
| 440 |
if(dirname.size() < 2) |
| 441 |
{ |
| 442 |
break; |
| 443 |
} |
| 444 |
|
| 445 |
auto space = dirname.find(" "); |
| 446 |
while (space != std::string::npos && |
| 447 |
helperParser.IsBetweenQuote(space, false, dirname)) { |
| 448 |
space = dirname.find(" ",space + 1); |
| 449 |
} |
| 450 |
|
| 451 |
// if we should remove the file |
| 452 |
if(dirname.find("-f",0) != std::string::npos) |
| 453 |
{ |
| 454 |
std::string fileToRemove = dirname.substr(3,dirname.size()-3); |
| 455 |
RemoveFile(fileToRemove.c_str(),filenames); |
| 456 |
} |
| 457 |
else if(kwssys::SystemTools::FileExists(dirname.c_str())) |
| 458 |
{ |
| 459 |
filenames.push_back(dirname); |
| 460 |
} |
| 461 |
else // we add a directory |
| 462 |
{ |
| 463 |
kwssys::Glob glob; |
| 464 |
if(dirname.find("[R]",0) != std::string::npos) |
| 465 |
{ |
| 466 |
glob.RecurseOn(); |
| 467 |
} |
| 468 |
|
| 469 |
auto localspace = dirname.find(" "); |
| 470 |
while (localspace != std::string::npos && |
| 471 |
helperParser.IsBetweenQuote(localspace, false, dirname)) { |
| 472 |
localspace = |
| 473 |
dirname.find(" ", localspace + 1); |
| 474 |
} |
| 475 |
|
| 476 |
if(localspace != std::string::npos) |
| 477 |
{ |
| 478 |
dirname = dirname.substr(0,localspace); |
| 479 |
} |
| 480 |
|
| 481 |
// Remove quotes if any |
| 482 |
if(!dirname.empty() && dirname[0] == '"') |
| 483 |
{ |
| 484 |
dirname = dirname.substr(1,dirname.size()-2); |
| 485 |
} |
| 486 |
|
| 487 |
std::string globoption = dirname.c_str(); |
| 488 |
glob.FindFiles(globoption.c_str()); |
| 489 |
std::vector<std::string> globfiles = glob.GetFiles(); |
| 490 |
std::vector<std::string>::const_iterator itglob = globfiles.begin(); |
| 491 |
while(itglob != globfiles.end()) |
| 492 |
{ |
| 493 |
filenames.push_back(*itglob); |
| 494 |
++itglob; |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
if(static_cast<std::streamsize>(pos) != fileSize) |
| 499 |
{ |
| 500 |
pos = buffer.find("\n", start); |
| 501 |
posr = buffer.find("\r", start); |
| 502 |
if(posr == std::string::npos) |
| 503 |
{ |
| 504 |
posr = pos; |
| 505 |
} |
| 506 |
} |
| 507 |
} while(static_cast<std::streamsize>(pos) < fileSize); |
| 508 |
|
| 509 |
file.close(); |
| 510 |
} else { |
| 511 |
filenames.push_back(inputFilename); |
| 512 |
} |
| 513 |
|
| 514 |
// sort the filenames |
| 515 |
std::sort(filenames.begin(), filenames.end()); |
| 516 |
|
| 517 |
|
| 518 |
// if the -cvs command is used |
| 519 |
// WARNING this option should be last because its position is used |
| 520 |
// to determine the filenames. |
| 521 |
/*if(command.GetOptionWasSet("cvs")) |
| 522 |
{ |
| 523 |
filenames.clear(); |
| 524 |
int cvspos = 0; |
| 525 |
int i=0; |
| 526 |
// Look for the position of the cvs command |
| 527 |
for(i=0;i<argc;i++) |
| 528 |
{ |
| 529 |
if(!strcmp(argv[i],"-cvs")) |
| 530 |
{ |
| 531 |
cvspos = i; |
| 532 |
break; |
| 533 |
} |
| 534 |
} |
| 535 |
cvspos++; |
| 536 |
std::string cvsdir = argv[cvspos]; |
| 537 |
cvspos++; |
| 538 |
for(i=cvspos;i<argc;i++) |
| 539 |
{ |
| 540 |
std::string filename = argv[i]; |
| 541 |
if((filename.substr(filename.size()-4,4) == ".hxx") |
| 542 |
|| (filename.substr(filename.size()-4,4) == ".txx") |
| 543 |
|| (filename.substr(filename.size()-4,4) == ".cxx") |
| 544 |
|| (filename.substr(filename.size()-2,2) == ".h") |
| 545 |
) |
| 546 |
{ |
| 547 |
filenames.push_back(filename); |
| 548 |
} |
| 549 |
} |
| 550 |
}*/ |
| 551 |
|
| 552 |
std::vector<std::string>::const_iterator it = filenames.begin(); |
| 553 |
|
| 554 |
size_t nerrors = 0; |
| 555 |
|
| 556 |
while(it != filenames.end()) |
| 557 |
{ |
| 558 |
if(!command.GetOptionWasSet("quiteverbose") && |
| 559 |
!command.GetOptionWasSet("exporthtml") && |
| 560 |
!command.GetOptionWasSet("vim") && !command.GetOptionWasSet("msvc") |
| 561 |
&& !command.GetOptionWasSet("gcc") |
| 562 |
) |
| 563 |
{ |
| 564 |
std::cout << "Processing " << (*it).c_str() << std::endl; |
| 565 |
} |
| 566 |
|
| 567 |
// We open the file |
| 568 |
std::ifstream file; |
| 569 |
file.open((*it).c_str(), std::ios::binary | std::ios::in); |
| 570 |
if(!file.is_open()) |
| 571 |
{ |
| 572 |
std::cout << "Cannot open file: " << (*it).c_str() << std::endl; |
| 573 |
// When removing a file with CVS we should return 0 otherwise it fails |
| 574 |
if(command.GetOptionWasSet("cvs")) |
| 575 |
{ |
| 576 |
return 0; |
| 577 |
} |
| 578 |
return 1; |
| 579 |
} |
| 580 |
|
| 581 |
file.seekg(0,std::ios::end); |
| 582 |
std::streamsize fileSize = file.tellg(); |
| 583 |
file.seekg(0,std::ios::beg); |
| 584 |
|
| 585 |
char* buf = new char[fileSize+1]; |
| 586 |
file.read(buf, fileSize); |
| 587 |
buf[fileSize] = 0; |
| 588 |
std::string buffer(buf); |
| 589 |
buffer.resize( static_cast<size_t>(fileSize)); |
| 590 |
delete [] buf; |
| 591 |
|
| 592 |
file.close(); |
| 593 |
|
| 594 |
kws::Parser parser; |
| 595 |
parser.SetFilename((*it).c_str()); |
| 596 |
parser.SetBuffer(buffer); |
| 597 |
|
| 598 |
// Ask the parser to generate a corrected version of the file |
| 599 |
if(command.GetOptionWasSet("fixFile")) |
| 600 |
{ |
| 601 |
parser.SetFixFile(true); |
| 602 |
} |
| 603 |
|
| 604 |
auto itf = features.begin(); |
| 605 |
while (itf != features.end()) { |
| 606 |
// Check if we have a match in the list of overwriteFeatures |
| 607 |
bool checked = false; |
| 608 |
auto itof = overwriteFeatures.begin(); |
| 609 |
while (itof != overwriteFeatures.end()) { |
| 610 |
// Allow for for regex expression within overwrite files |
| 611 |
kwssys::RegularExpression regex((*itof).filename.c_str()); |
| 612 |
|
| 613 |
if (regex.find((*it).c_str()) && |
| 614 |
(!strcmp((*itof).name.c_str(), (*itf).name.c_str()))) { |
| 615 |
if ((*itof).enable) { |
| 616 |
parser.Check((*itof).name.c_str(), (*itof).value.c_str()); |
| 617 |
} |
| 618 |
|
| 619 |
if((*itof).name == "LineLength" && !((*itof).enable)) |
| 620 |
{ |
| 621 |
std::cout << "Cannot disable LineLength check. It computes internal parameters" << std::endl; |
| 622 |
} |
| 623 |
else |
| 624 |
{ |
| 625 |
checked = true; |
| 626 |
} |
| 627 |
} |
| 628 |
++itof; |
| 629 |
} |
| 630 |
|
| 631 |
if(!checked && (*itf).enable) |
| 632 |
{ |
| 633 |
parser.Check((*itf).name.c_str(),(*itf).value.c_str()); |
| 634 |
} |
| 635 |
++itf; |
| 636 |
} |
| 637 |
|
| 638 |
if(command.GetOptionWasSet("verbose")) |
| 639 |
{ |
| 640 |
verbose = "1"; |
| 641 |
} |
| 642 |
|
| 643 |
// If we should display the error |
| 644 |
if(!verbose.empty() |
| 645 |
&& !command.GetOptionWasSet("msvc") |
| 646 |
&& !command.GetOptionWasSet("vim") |
| 647 |
&& !command.GetOptionWasSet("gcc") |
| 648 |
) |
| 649 |
{ |
| 650 |
std::cout << parser.GetLastErrors().c_str() << std::endl; |
| 651 |
std::cout << parser.GetLastWarnings().c_str() << std::endl; |
| 652 |
} |
| 653 |
|
| 654 |
if(command.GetOptionWasSet("vim")) |
| 655 |
{ |
| 656 |
// Format the string for vim |
| 657 |
const kws::Parser::ErrorVectorType errors = parser.GetErrors(); |
| 658 |
auto eit = errors.begin(); |
| 659 |
while(eit != errors.end()) |
| 660 |
{ |
| 661 |
std::cout << (*it).c_str() << ":" << (*eit).line << ":" |
| 662 |
<< (*eit).description << std::endl; |
| 663 |
++eit; |
| 664 |
} |
| 665 |
} |
| 666 |
else if(command.GetOptionWasSet("msvc")) |
| 667 |
{ |
| 668 |
// Format the string for Visual Studio |
| 669 |
const kws::Parser::ErrorVectorType errors = parser.GetErrors(); |
| 670 |
auto eit = errors.begin(); |
| 671 |
while(eit != errors.end()) |
| 672 |
{ |
| 673 |
std::cout << (*it).c_str() << "(" << (*eit).line << ") : error " << (*eit).number << ":" |
| 674 |
<< (*eit).description << std::endl; |
| 675 |
++eit; |
| 676 |
} |
| 677 |
const kws::Parser::WarningVectorType warnings = parser.GetWarnings(); |
| 678 |
auto wit = warnings.begin(); |
| 679 |
while(wit != warnings.end()) |
| 680 |
{ |
| 681 |
std::cout << (*it).c_str() << "(" << (*wit).line << ") : warning " << (*wit).number << ":" |
| 682 |
<< (*wit).description << std::endl; |
| 683 |
++wit; |
| 684 |
} |
| 685 |
} |
| 686 |
else if(command.GetOptionWasSet("gcc")) |
| 687 |
{ |
| 688 |
// Format the string for Visual Studio |
| 689 |
const kws::Parser::ErrorVectorType errors = parser.GetErrors(); |
| 690 |
auto eit = errors.begin(); |
| 691 |
while(eit != errors.end()) |
| 692 |
{ |
| 693 |
std::cout << (*it).c_str() << ":" << (*eit).line << ": error: " |
| 694 |
<< (*eit).description << std::endl; |
| 695 |
++eit; |
| 696 |
} |
| 697 |
const kws::Parser::WarningVectorType warnings = parser.GetWarnings(); |
| 698 |
auto wit = warnings.begin(); |
| 699 |
while(wit != warnings.end()) |
| 700 |
{ |
| 701 |
std::cout << (*it).c_str() << ":" << (*wit).line << ": warning: " |
| 702 |
<< (*wit).description << std::endl; |
| 703 |
++wit; |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
if(command.GetOptionWasSet("cvs")) |
| 708 |
{ |
| 709 |
std::cout << parser.GetLastErrors().c_str() << std::endl; |
| 710 |
if (!parser.GetErrors().empty()) { |
| 711 |
return 1; |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
nerrors += parser.GetErrors().size(); |
| 716 |
|
| 717 |
parser.GenerateFixedFile(); |
| 718 |
|
| 719 |
m_Parsers.push_back(parser); |
| 720 |
++it; |
| 721 |
} // end filenames |
| 722 |
|
| 723 |
if(command.GetOptionWasSet("html")) |
| 724 |
{ |
| 725 |
htmlDirectory = command.GetValueAsString("html","filename"); |
| 726 |
} |
| 727 |
|
| 728 |
// If we should generate the HTML file |
| 729 |
if(!htmlDirectory.empty()) |
| 730 |
{ |
| 731 |
kws::Generator generator; |
| 732 |
generator.SetParser(&m_Parsers); |
| 733 |
|
| 734 |
if(command.GetOptionWasSet("xml")) |
| 735 |
{ |
| 736 |
std::string xml = command.GetValueAsString("xml","filename"); |
| 737 |
generator.ReadConfigurationFile(xml.c_str()); |
| 738 |
} |
| 739 |
|
| 740 |
bool showNoErrors = false; |
| 741 |
if (!lessHTML.empty()) { |
| 742 |
if(lessHTML[0] == '1') |
| 743 |
{ |
| 744 |
showNoErrors = true; |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
if(command.GetOptionWasSet("lesshtml")) |
| 749 |
{ |
| 750 |
showNoErrors = true; |
| 751 |
} |
| 752 |
generator.GenerateHTML(htmlDirectory.c_str(), !showNoErrors); |
| 753 |
} |
| 754 |
|
| 755 |
// If we should export the html report |
| 756 |
if(command.GetOptionWasSet("exporthtml")) |
| 757 |
{ |
| 758 |
kws::Generator generator; |
| 759 |
if(command.GetOptionWasSet("xml")) |
| 760 |
{ |
| 761 |
std::string xml = command.GetValueAsString("xml", "filename"); |
| 762 |
generator.ReadConfigurationFile(xml.c_str()); |
| 763 |
} |
| 764 |
generator.SetParser(&m_Parsers); |
| 765 |
generator.ExportHTML(std::cout); |
| 766 |
} |
| 767 |
|
| 768 |
if(command.GetOptionWasSet("exportxml")) |
| 769 |
{ |
| 770 |
const std::string outputxmlFile = command.GetValueAsString("exportxml","filename"); |
| 771 |
kws::Generator generator; |
| 772 |
generator.SetParser(&m_Parsers); |
| 773 |
generator.ExportXML(outputxmlFile.c_str()); |
| 774 |
} |
| 775 |
|
| 776 |
// If we should generate dart files |
| 777 |
if(command.GetOptionWasSet("dart")) |
| 778 |
{ |
| 779 |
std::string dart = command.GetValueAsString("dart", "filename"); |
| 780 |
int maxerror = command.GetValueAsInt("dart", "maxerror"); |
| 781 |
bool grouperrors = command.GetValueAsBool("dart", "group"); |
| 782 |
kws::Generator generator; |
| 783 |
if(command.GetOptionWasSet("xml")) |
| 784 |
{ |
| 785 |
std::string xml = command.GetValueAsString("xml", "filename"); |
| 786 |
generator.ReadConfigurationFile(xml.c_str()); |
| 787 |
} |
| 788 |
generator.SetParser(&m_Parsers); |
| 789 |
|
| 790 |
std::string url = ""; |
| 791 |
if(command.GetOptionWasSet("kwsurl")) |
| 792 |
{ |
| 793 |
url = command.GetValueAsString("kwsurl","url"); |
| 794 |
} |
| 795 |
|
| 796 |
std::string baseDirectory = ""; |
| 797 |
|
| 798 |
if(command.GetOptionWasSet("basedirectory")) |
| 799 |
{ |
| 800 |
baseDirectory = command.GetValueAsString("basedirectory","filename"); |
| 801 |
} |
| 802 |
generator.GenerateDart(dart.c_str(),maxerror,grouperrors,url,time0,baseDirectory.c_str()); |
| 803 |
} |
| 804 |
|
| 805 |
// If we have errors we return false |
| 806 |
if(nerrors > 0) |
| 807 |
{ |
| 808 |
return EXIT_FAILURE; |
| 809 |
} |
| 810 |
|
| 811 |
return EXIT_SUCCESS; |
| 812 |
} |