| 1 |
/*========================================================================= |
| 2 |
|
| 3 |
Program: KWStyle - Kitware Style Checker |
| 4 |
Module: kwsCheckTypedefs.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 |
/** Check if the typedefs of the class are correct */ |
| 19 |
bool Parser::CheckTypedefs(const char* regEx, bool alignment,unsigned int maxLength) |
| 20 |
{ |
| 21 |
if(alignment) |
| 22 |
{ |
| 23 |
m_TestsDone[TYPEDEF_ALIGN] = true; |
| 24 |
constexpr size_t length = 255; |
| 25 |
char* val = new char[length]; |
| 26 |
snprintf(val,length,"Typedefs should be aligned"); |
| 27 |
m_TestsDescription[TYPEDEF_ALIGN] = val; |
| 28 |
delete [] val; |
| 29 |
} |
| 30 |
|
| 31 |
m_TestsDone[TYPEDEF_REGEX] = true; |
| 32 |
m_TestsDescription[TYPEDEF_REGEX] = "Typedefs should match regular expression: "; |
| 33 |
m_TestsDescription[TYPEDEF_REGEX] += regEx; |
| 34 |
|
| 35 |
// First we need to find the typedefs |
| 36 |
// typedef type MyTypeDef; |
| 37 |
bool hasError = false; |
| 38 |
|
| 39 |
kwssys::RegularExpression regex(regEx); |
| 40 |
|
| 41 |
size_t previousline = 0; |
| 42 |
size_t previouspos = 0; |
| 43 |
size_t pos = 0; |
| 44 |
while(pos!= std::string::npos) |
| 45 |
{ |
| 46 |
size_t beg = 0; |
| 47 |
size_t typedefpos = 0; |
| 48 |
std::string var = this->FindTypedef(pos+1,m_BufferNoComment.size(),pos,beg,typedefpos); |
| 49 |
|
| 50 |
if (var.empty()) { |
| 51 |
continue; |
| 52 |
} |
| 53 |
// Check the alignment if specified |
| 54 |
if(alignment) |
| 55 |
{ |
| 56 |
// Find the position in the line |
| 57 |
size_t l = this->GetPositionInLine(beg); |
| 58 |
size_t line = this->GetLineNumber(beg,true); |
| 59 |
size_t typdefline = this->GetLineNumber(typedefpos,true); |
| 60 |
|
| 61 |
// if the typedef is on a line close to the previous one we check |
| 62 |
if(typdefline-previousline<2) |
| 63 |
{ |
| 64 |
if(l!=previouspos) |
| 65 |
{ |
| 66 |
bool reportError = true; |
| 67 |
// We check that the previous line is not ending with a semicolon |
| 68 |
// and that the sum of the two lines is more than maxLength |
| 69 |
std::string previousLine = this->GetLine(this->GetLineNumber(beg,true)-2); |
| 70 |
std::string currentLine = this->GetLine(this->GetLineNumber(beg,true)-1); |
| 71 |
if( (previousLine[previousLine.size()-1] != ';') |
| 72 |
&& (previousLine.size()+currentLine.size()>maxLength) |
| 73 |
) |
| 74 |
{ |
| 75 |
reportError = false; |
| 76 |
} |
| 77 |
|
| 78 |
// Check if the alignement is possible due to the length of the line |
| 79 |
size_t size = currentLine.size()-l; |
| 80 |
if(previouspos+size>maxLength) |
| 81 |
{ |
| 82 |
previouspos = l; |
| 83 |
reportError = false; |
| 84 |
} |
| 85 |
|
| 86 |
// This one is a bit tricky. Check the minimum |
| 87 |
// typedef igstk::VascularNetworkObject VascularNetworkObjectType; |
| 88 |
// typedef VascularNetworkObjectType::VesselObjectType VesselObjectType; |
| 89 |
// First find the optimal position (one space) for the current line |
| 90 |
size_t optimalCurrentLinePos = l; |
| 91 |
while(optimalCurrentLinePos>1 && currentLine[optimalCurrentLinePos-1] == ' ') |
| 92 |
{ |
| 93 |
optimalCurrentLinePos--; |
| 94 |
} |
| 95 |
optimalCurrentLinePos++; |
| 96 |
// Second find the size of the previous typedefs; |
| 97 |
size_t sizeTypedef = previousLine.size()-previouspos; |
| 98 |
// if the size is too big we don't report |
| 99 |
if(optimalCurrentLinePos+sizeTypedef>maxLength) |
| 100 |
{ |
| 101 |
previouspos = l; |
| 102 |
reportError = false; |
| 103 |
} |
| 104 |
|
| 105 |
if(reportError) |
| 106 |
{ |
| 107 |
Error error; |
| 108 |
error.line = this->GetLineNumber(beg,true); |
| 109 |
error.line2 = error.line; |
| 110 |
error.number = TYPEDEF_ALIGN; |
| 111 |
error.description = "Type definition (" + var + ") is not aligned with the previous one: "; |
| 112 |
constexpr size_t length = 10; |
| 113 |
char* localvar = new char[length]; |
| 114 |
snprintf(localvar,length,"%zd",l); |
| 115 |
error.description += localvar; |
| 116 |
error.description += " v.s. "; |
| 117 |
snprintf(localvar,length,"%zd",previouspos); |
| 118 |
error.description += localvar; |
| 119 |
delete [] localvar; |
| 120 |
m_ErrorList.push_back(error); |
| 121 |
hasError = true; |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
else |
| 126 |
{ |
| 127 |
previouspos = l; |
| 128 |
} |
| 129 |
previousline = line; |
| 130 |
} |
| 131 |
|
| 132 |
if(!regex.find(var)) |
| 133 |
{ |
| 134 |
Error error; |
| 135 |
error.line = this->GetLineNumber(pos,true); |
| 136 |
error.line2 = error.line; |
| 137 |
error.number = TYPEDEF_REGEX; |
| 138 |
error.description = "Type definition (" + var + ") doesn't match regular expression"; |
| 139 |
m_ErrorList.push_back(error); |
| 140 |
hasError = true; |
| 141 |
} |
| 142 |
} |
| 143 |
return !hasError; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
/** Find a typedef in the source code */ |
| 148 |
std::string Parser::FindTypedef(size_t start, size_t end,size_t & pos,size_t & beg,size_t & typdefpos) |
| 149 |
{ |
| 150 |
size_t posTypedef = m_BufferNoComment.find("typedef",start); |
| 151 |
if(posTypedef == std::string::npos) |
| 152 |
{ |
| 153 |
pos = std::string::npos; |
| 154 |
return ""; |
| 155 |
} |
| 156 |
|
| 157 |
typdefpos = posTypedef; |
| 158 |
size_t posSemicolon = m_BufferNoComment.find(";",posTypedef); |
| 159 |
|
| 160 |
// Check if we have any () in the subword then we don't check the typdef |
| 161 |
std::string sub = m_BufferNoComment.substr(posTypedef,posSemicolon-posTypedef); |
| 162 |
if((sub.find("(",0) != std::string::npos) |
| 163 |
|| (sub.find(")",0) != std::string::npos) |
| 164 |
|| (sub.find("{",0) != std::string::npos) |
| 165 |
|| (sub.find("}",0) != std::string::npos) |
| 166 |
) |
| 167 |
{ |
| 168 |
pos = posSemicolon; |
| 169 |
return ""; |
| 170 |
} |
| 171 |
|
| 172 |
std::string typedefname = ""; |
| 173 |
if(posSemicolon != std::string::npos && posSemicolon<end) |
| 174 |
{ |
| 175 |
// We try to find the word before that |
| 176 |
size_t i=posSemicolon-1; |
| 177 |
bool inWord = true; |
| 178 |
bool first = false; |
| 179 |
while(inWord) |
| 180 |
{ |
| 181 |
if(m_BufferNoComment[i] != ' ') |
| 182 |
{ |
| 183 |
std::string store = typedefname; |
| 184 |
typedefname = m_BufferNoComment[i]; |
| 185 |
typedefname += store; |
| 186 |
beg = i; |
| 187 |
inWord = true; |
| 188 |
first = true; |
| 189 |
} |
| 190 |
else // we have a space |
| 191 |
{ |
| 192 |
if(first) |
| 193 |
{ |
| 194 |
inWord = false; |
| 195 |
} |
| 196 |
} |
| 197 |
i--; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
pos = posSemicolon; |
| 202 |
return typedefname; |
| 203 |
} |
| 204 |
|
| 205 |
} // end namespace kws |