| 1 |
/*========================================================================= |
| 2 |
|
| 3 |
Program: KWStyle - Kitware Style Checker |
| 4 |
Module: kwsCheckStruct.cxx |
| 5 |
|
| 6 |
Copyright (c) Kitware, Inc. All rights reserved. |
| 7 |
See Copyright.txt or http://www.kitware.com/Copyright.htm for details. |
| 8 |
|
| 9 |
This software is distributed WITHOUT ANY WARRANTY; without even |
| 10 |
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. See the above copyright notices for more information. |
| 12 |
|
| 13 |
=========================================================================*/ |
| 14 |
#include "kwsParser.h" |
| 15 |
|
| 16 |
namespace kws { |
| 17 |
|
| 18 |
|
| 19 |
/** Check if the struct variables of the class are correct */ |
| 20 |
bool Parser::CheckStruct(const char* regEx,bool alignment) |
| 21 |
{ |
| 22 |
m_TestsDone[SVAR_REGEX] = true; |
| 23 |
m_TestsDescription[SVAR_REGEX] = "struct variables should match regular expression: "; |
| 24 |
m_TestsDescription[SVAR_REGEX] += regEx; |
| 25 |
|
| 26 |
if(alignment) |
| 27 |
{ |
| 28 |
m_TestsDone[SVAR_ALIGN] = true; |
| 29 |
m_TestsDescription[SVAR_ALIGN] = "struct variables should be aligned with previous vars "; |
| 30 |
} |
| 31 |
|
| 32 |
// First we need to find the parameters |
| 33 |
// float myParam; |
| 34 |
bool hasError = false; |
| 35 |
|
| 36 |
kwssys::RegularExpression regex(regEx); |
| 37 |
|
| 38 |
// find the struct |
| 39 |
size_t posStruct = m_BufferNoComment.find("struct",0); |
| 40 |
|
| 41 |
while(posStruct != std::string::npos) |
| 42 |
{ |
| 43 |
size_t begin = posStruct; |
| 44 |
while(posStruct<m_BufferNoComment.size()) |
| 45 |
{ |
| 46 |
if(m_BufferNoComment[posStruct] == '{') |
| 47 |
{ |
| 48 |
break; |
| 49 |
} |
| 50 |
posStruct++; |
| 51 |
} |
| 52 |
size_t end = this->FindClosingChar('{','}',posStruct,true); |
| 53 |
|
| 54 |
size_t previousline = 0; |
| 55 |
size_t previouspos = 0; |
| 56 |
|
| 57 |
size_t pos = begin; |
| 58 |
while(pos!= std::string::npos) |
| 59 |
{ |
| 60 |
std::string var = this->FindInternalVariable(pos+1,end+1,pos); |
| 61 |
if (var.empty()) { |
| 62 |
continue; |
| 63 |
} |
| 64 |
|
| 65 |
if(var.length() > 0) |
| 66 |
{ |
| 67 |
// Check the alignment if specified |
| 68 |
if(alignment) |
| 69 |
{ |
| 70 |
// Find the position in the line |
| 71 |
size_t posvar = m_BufferNoComment.find(var,pos-var.size()-2); |
| 72 |
size_t l = this->GetPositionInLine(posvar); |
| 73 |
size_t line = this->GetLineNumber(pos,true); |
| 74 |
|
| 75 |
// if the typedef is on a line close to the previous one we check |
| 76 |
if(line-previousline<2) |
| 77 |
{ |
| 78 |
if(l!=previouspos) |
| 79 |
{ |
| 80 |
Error error; |
| 81 |
error.line = this->GetLineNumber(pos,true); |
| 82 |
error.line2 = error.line; |
| 83 |
error.number = SVAR_ALIGN; |
| 84 |
error.description = "Struct variable (" + var + ") is not aligned with the previous one"; |
| 85 |
m_ErrorList.push_back(error); |
| 86 |
hasError = true; |
| 87 |
} |
| 88 |
} |
| 89 |
else |
| 90 |
{ |
| 91 |
previouspos = l; |
| 92 |
} |
| 93 |
previousline = line; |
| 94 |
} // end alignement |
| 95 |
|
| 96 |
if(!regex.find(var)) |
| 97 |
{ |
| 98 |
Error error; |
| 99 |
error.line = this->GetLineNumber(pos,true); |
| 100 |
error.line2 = error.line; |
| 101 |
error.number = SVAR_REGEX; |
| 102 |
error.description = "Struct variable (" + var + ") doesn't match regular expression"; |
| 103 |
m_ErrorList.push_back(error); |
| 104 |
hasError = true; |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
posStruct = m_BufferNoComment.find("struct",posStruct+1); |
| 109 |
} // end struct loop |
| 110 |
|
| 111 |
return !hasError; |
| 112 |
} |
| 113 |
|
| 114 |
} // end namespace kws |