| 1 |
/*========================================================================= |
| 2 |
|
| 3 |
Program: KWStyle - Kitware Style Checker |
| 4 |
Module: kwsCheckHeader.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 <string.h> // strlen |
| 16 |
|
| 17 |
namespace kws { |
| 18 |
|
| 19 |
/** Check header given a template filename |
| 20 |
* The template should have '<NA>\n' tag to avoid checking the rest of the line |
| 21 |
* or <NA> to skip a word. |
| 22 |
* The header should also be at the beginning of the file */ |
| 23 |
bool Parser::CheckHeader(const char* filename, bool considerSpaceEOL,bool useCVS) |
| 24 |
{ |
| 25 |
m_TestsDone[HEADER] = true; |
| 26 |
constexpr size_t length = 255; |
| 27 |
char* val = new char[length]; |
| 28 |
snprintf(val,length,"The header should respect the template"); |
| 29 |
m_TestsDescription[HEADER] = val; |
| 30 |
delete [] val; |
| 31 |
|
| 32 |
bool hasError = false; |
| 33 |
if(!filename) |
| 34 |
{ |
| 35 |
std::cout << "CheckHeader(): Please specify an header file or a directory containing headers" << std::endl; |
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
std::vector<std::string> fileNames; |
| 40 |
|
| 41 |
// if there is no space in the name (i.e on linux) we remove the |
| 42 |
// '"' if any. |
| 43 |
std::string headerFilename = filename; |
| 44 |
if(headerFilename.find(' ') == std::string::npos) |
| 45 |
{ |
| 46 |
headerFilename = ""; |
| 47 |
for(unsigned long j=0;j<strlen(filename);j++) |
| 48 |
{ |
| 49 |
if(filename[j] != '\"') |
| 50 |
{ |
| 51 |
headerFilename+=filename[j]; |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
// Remove quotes if any on linux |
| 57 |
#ifndef WIN32 |
| 58 |
if(headerFilename[0] == '"') |
| 59 |
{ |
| 60 |
headerFilename = headerFilename.substr(1,headerFilename.size()-2); |
| 61 |
} |
| 62 |
#endif |
| 63 |
|
| 64 |
// Check if we have a directory or header |
| 65 |
kwssys::Directory directory; |
| 66 |
if(directory.Load(headerFilename.c_str())) |
| 67 |
{ |
| 68 |
std::string dirname = headerFilename; |
| 69 |
if(dirname[dirname.size()-1] != '/') |
| 70 |
{ |
| 71 |
dirname += "/"; |
| 72 |
} |
| 73 |
|
| 74 |
for(unsigned int i=0;i<directory.GetNumberOfFiles();i++) |
| 75 |
{ |
| 76 |
std::string file = directory.GetFile(i); |
| 77 |
std::string fullpath = dirname+file; |
| 78 |
|
| 79 |
if(file!=".." && file!="." && file!="CVS"&& file!= ".svn") |
| 80 |
{ |
| 81 |
fileNames.push_back(dirname+file); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
else |
| 86 |
{ |
| 87 |
fileNames.push_back(headerFilename); |
| 88 |
} |
| 89 |
|
| 90 |
// Create a temporary vector of errors |
| 91 |
std::vector<ErrorVectorType> tempErrors; |
| 92 |
|
| 93 |
std::vector<std::string>::const_iterator itFilename = fileNames.begin(); |
| 94 |
while(itFilename != fileNames.end()) |
| 95 |
{ |
| 96 |
hasError = false; |
| 97 |
|
| 98 |
// Read the header file |
| 99 |
std::ifstream file; |
| 100 |
file.open((*itFilename).c_str(), std::ios::binary | std::ios::in); |
| 101 |
if(!file.is_open()) |
| 102 |
{ |
| 103 |
std::cout << "Cannot open header file: " << (*itFilename).c_str() << std::endl; |
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
file.seekg(0,std::ios::end); |
| 108 |
unsigned long fileSize = file.tellg(); |
| 109 |
file.seekg(0,std::ios::beg); |
| 110 |
|
| 111 |
char* buf = new char[fileSize+1]; |
| 112 |
file.read(buf,fileSize); |
| 113 |
buf[fileSize] = 0; |
| 114 |
std::string buffer(buf); |
| 115 |
buffer.resize(fileSize); |
| 116 |
file.close(); |
| 117 |
delete [] buf; |
| 118 |
|
| 119 |
this->ConvertBufferToWindowsFileType(buffer); |
| 120 |
|
| 121 |
ErrorVectorType tempErrorVector; |
| 122 |
|
| 123 |
// Check the file char by char |
| 124 |
std::string::const_iterator ith = buffer.begin(); |
| 125 |
std::string::const_iterator it = m_Buffer.begin(); |
| 126 |
|
| 127 |
unsigned int pos = 0; |
| 128 |
unsigned int posh = 0; |
| 129 |
long line = -1; |
| 130 |
|
| 131 |
|
| 132 |
while((ith != buffer.end()) && (it != m_Buffer.end())) |
| 133 |
{ |
| 134 |
// if we have cvs |
| 135 |
if((*ith == '$') && useCVS) |
| 136 |
{ |
| 137 |
ith++; |
| 138 |
posh++; |
| 139 |
it++; |
| 140 |
pos++; |
| 141 |
while(((*ith) != '$') && (ith != buffer.end())) |
| 142 |
{ |
| 143 |
ith++; |
| 144 |
posh++; |
| 145 |
} |
| 146 |
while(((*it) != '$') && (it != m_Buffer.end())) |
| 147 |
{ |
| 148 |
pos++; |
| 149 |
it++; |
| 150 |
} |
| 151 |
//continue; |
| 152 |
} |
| 153 |
|
| 154 |
if((*it) != (*ith)) |
| 155 |
{ |
| 156 |
// Check if we have a <NA> tag |
| 157 |
if((*ith) == '<') |
| 158 |
{ |
| 159 |
auto pos2 = static_cast<long int>(buffer.find("<NA>", posh)); |
| 160 |
auto pos3 = static_cast<long int>(buffer.find("<NA>\n", posh)); |
| 161 |
|
| 162 |
// We skip the line |
| 163 |
if(pos3 == static_cast<long int>(posh)) |
| 164 |
{ |
| 165 |
while(((*ith) != '\n') && (ith != buffer.end())) |
| 166 |
{ |
| 167 |
ith++; |
| 168 |
posh++; |
| 169 |
} |
| 170 |
while(((*it) != '\n') && (it != m_Buffer.end())) |
| 171 |
{ |
| 172 |
pos++; |
| 173 |
it++; |
| 174 |
} |
| 175 |
continue; |
| 176 |
} |
| 177 |
// if we have the tag we skip the word |
| 178 |
else if(pos2 == static_cast<long int>(posh)) |
| 179 |
{ |
| 180 |
while(((*ith) != ' ') && (ith != buffer.end())) |
| 181 |
{ |
| 182 |
ith++; |
| 183 |
posh++; |
| 184 |
} |
| 185 |
while(((*it) != ' ') && (it != m_Buffer.end())) |
| 186 |
{ |
| 187 |
pos++; |
| 188 |
it++; |
| 189 |
} |
| 190 |
continue; |
| 191 |
} |
| 192 |
} |
| 193 |
// if we should not check the spaces at the end of line |
| 194 |
else if( (!considerSpaceEOL) |
| 195 |
&& ((*ith == ' ') || (*it == ' ')) |
| 196 |
) |
| 197 |
{ |
| 198 |
// search if we are effectively at the end of the line |
| 199 |
bool isAtEnd = true; |
| 200 |
if(*it == ' ') |
| 201 |
{ |
| 202 |
std::string::const_iterator ittemp = it; |
| 203 |
while((ittemp != m_Buffer.end()) && ((*ittemp) != '\n')) |
| 204 |
{ |
| 205 |
if(*ittemp != ' ') |
| 206 |
{ |
| 207 |
isAtEnd = false; |
| 208 |
break; |
| 209 |
} |
| 210 |
ittemp++; |
| 211 |
} |
| 212 |
} |
| 213 |
else if(*ith == ' ') |
| 214 |
{ |
| 215 |
std::string::const_iterator ittemp = ith; |
| 216 |
while((ittemp != buffer.end()) && ((*ittemp) != '\n')) |
| 217 |
{ |
| 218 |
if(*ittemp != ' ') |
| 219 |
{ |
| 220 |
isAtEnd = false; |
| 221 |
break; |
| 222 |
} |
| 223 |
ittemp++; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
// If we are at the end we skip the line |
| 228 |
if(!isAtEnd) |
| 229 |
{ |
| 230 |
while((ith != buffer.end()) && ((*ith) != '\n')) |
| 231 |
{ |
| 232 |
ith++; |
| 233 |
posh++; |
| 234 |
} |
| 235 |
while((it != m_Buffer.end()) && ((*it) != '\n')) |
| 236 |
{ |
| 237 |
pos++; |
| 238 |
it++; |
| 239 |
} |
| 240 |
continue; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
// Report the error |
| 245 |
//hasError = true; |
| 246 |
|
| 247 |
// We report the wrong word and the line |
| 248 |
long l = this->GetLineNumber(pos); |
| 249 |
if(l != line) |
| 250 |
{ |
| 251 |
line = l; |
| 252 |
// Find the word |
| 253 |
auto poshw = static_cast<long int>(buffer.find(' ', posh)); |
| 254 |
auto poshw2 = static_cast<long int>(buffer.find('\n', posh)); |
| 255 |
std::string wordh = ""; |
| 256 |
if(poshw!= -1 && poshw < poshw2) |
| 257 |
{ |
| 258 |
wordh = buffer.substr(posh,poshw-posh); |
| 259 |
} |
| 260 |
else if (poshw2 != -1) |
| 261 |
{ |
| 262 |
wordh = buffer.substr(posh,poshw2-posh-1); |
| 263 |
} |
| 264 |
|
| 265 |
// Find the word |
| 266 |
auto posw = static_cast<long int>(m_Buffer.find(' ', pos)); |
| 267 |
auto posw2 = static_cast<long int>(m_Buffer.find('\n', pos)); |
| 268 |
std::string word = ""; |
| 269 |
if (posw != -1 && posw < posw2) { |
| 270 |
word = m_Buffer.substr(pos, posw - pos); |
| 271 |
} |
| 272 |
else if (poshw2 != -1) |
| 273 |
{ |
| 274 |
word = m_Buffer.substr(pos,posw2-pos-1); |
| 275 |
} |
| 276 |
|
| 277 |
if(word == wordh) |
| 278 |
{ |
| 279 |
wordh = "wrong ident"; |
| 280 |
} |
| 281 |
|
| 282 |
if(word == " ") |
| 283 |
{ |
| 284 |
word = "[space]"; |
| 285 |
} |
| 286 |
if(wordh == " ") |
| 287 |
{ |
| 288 |
wordh = "[space]"; |
| 289 |
} |
| 290 |
if(word == "\r") |
| 291 |
{ |
| 292 |
word = "[end of line]"; |
| 293 |
} |
| 294 |
if(wordh == "\r") |
| 295 |
{ |
| 296 |
wordh = "[end of line]"; |
| 297 |
} |
| 298 |
|
| 299 |
if(word[0] == 0) |
| 300 |
{ |
| 301 |
word = "[no char]"; |
| 302 |
} |
| 303 |
if(wordh[0] == 0) |
| 304 |
{ |
| 305 |
wordh = "[no char]"; |
| 306 |
} |
| 307 |
|
| 308 |
Error error; |
| 309 |
error.line = line; |
| 310 |
error.line2 = error.line; |
| 311 |
error.number = HEADER; |
| 312 |
error.description = "Header mismatch: "; |
| 313 |
error.description += word; |
| 314 |
error.description += " ("; |
| 315 |
error.description += wordh; |
| 316 |
error.description += ") : "; |
| 317 |
error.description += headerFilename.c_str(); |
| 318 |
tempErrorVector.push_back(error); |
| 319 |
hasError = true; |
| 320 |
|
| 321 |
// We skip that line |
| 322 |
while((ith != buffer.end()) && ((*ith) != '\n')) |
| 323 |
{ |
| 324 |
ith++; |
| 325 |
posh++; |
| 326 |
} |
| 327 |
while((it != m_Buffer.end()) && ((*it) != '\n')) |
| 328 |
{ |
| 329 |
pos++; |
| 330 |
it++; |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
if(ith != buffer.end()) |
| 336 |
{ |
| 337 |
posh++; |
| 338 |
ith++; |
| 339 |
} |
| 340 |
|
| 341 |
if(it != m_Buffer.end()) |
| 342 |
{ |
| 343 |
pos++; |
| 344 |
it++; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
if(it == m_Buffer.end()) |
| 349 |
{ |
| 350 |
Error error; |
| 351 |
error.line = 1; |
| 352 |
error.line2 = error.line; |
| 353 |
error.number = HEADER; |
| 354 |
error.description = "The header is incomplete"; |
| 355 |
tempErrorVector.push_back(error); |
| 356 |
hasError = true; |
| 357 |
} |
| 358 |
|
| 359 |
// If we don't have any errors we return the current errors |
| 360 |
if(!hasError) |
| 361 |
{ |
| 362 |
m_HeaderFilename = *itFilename; |
| 363 |
ErrorVectorType::const_iterator itErr = tempErrorVector.begin(); |
| 364 |
while(itErr != tempErrorVector.end()) |
| 365 |
{ |
| 366 |
m_ErrorList.push_back(*itErr); |
| 367 |
itErr++; |
| 368 |
} |
| 369 |
return true; |
| 370 |
} |
| 371 |
|
| 372 |
tempErrors.push_back(tempErrorVector); |
| 373 |
|
| 374 |
itFilename++; |
| 375 |
} // end loop of filenames |
| 376 |
|
| 377 |
// We check the less errors we can have |
| 378 |
unsigned int header=0; |
| 379 |
int minErrors = 9999999; |
| 380 |
unsigned int i=0; |
| 381 |
std::vector<ErrorVectorType>::const_iterator itErrV = tempErrors.begin(); |
| 382 |
while(itErrV != tempErrors.end()) |
| 383 |
{ |
| 384 |
if((int)(*itErrV).size() < minErrors) |
| 385 |
{ |
| 386 |
header = i; |
| 387 |
minErrors = static_cast<int>((*itErrV).size()); |
| 388 |
} |
| 389 |
i++; |
| 390 |
itErrV++; |
| 391 |
} |
| 392 |
|
| 393 |
ErrorVectorType::const_iterator itErr = tempErrors[header].begin(); |
| 394 |
while(itErr != tempErrors[header].end()) |
| 395 |
{ |
| 396 |
m_ErrorList.push_back(*itErr); |
| 397 |
itErr++; |
| 398 |
} |
| 399 |
|
| 400 |
m_HeaderFilename = fileNames[header]; |
| 401 |
return !hasError; |
| 402 |
} |
| 403 |
|
| 404 |
} // end namespace kws |