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