| 1 |
/*========================================================================= |
| 2 |
|
| 3 |
Program: KWStyle - Kitware Style Checker |
| 4 |
Module: kwsCheckIfNDefDefine.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 <cctype> |
| 16 |
|
| 17 |
namespace kws { |
| 18 |
|
| 19 |
|
| 20 |
/** Check if the #ifndef/#define is defined correctly for the class |
| 21 |
* match can contain [NameOfClass] and [Extension] */ |
| 22 |
bool Parser::CheckIfNDefDefine(const char* match, bool uppercaseTheDefinition) |
| 23 |
{ |
| 24 |
// Check only if we are not a .cxx or .cc or .c file |
| 25 |
if(m_Filename.find(".c") != std::string::npos) |
| 26 |
{ |
| 27 |
return false; |
| 28 |
} |
| 29 |
|
| 30 |
m_TestsDone[NDEFINE] = true; |
| 31 |
constexpr size_t length = 512; |
| 32 |
char* val = new char[length]; |
| 33 |
snprintf(val,length,"#ifndef/#define should match "); |
| 34 |
m_TestsDescription[NDEFINE] = val; |
| 35 |
// Replace < and > |
| 36 |
std::string l = match; |
| 37 |
auto inf = static_cast<long int>(l.find("<", 0)); |
| 38 |
while(inf != -1) |
| 39 |
{ |
| 40 |
l.replace(inf,1,"<"); |
| 41 |
inf = static_cast<long int>(l.find("<",0)); |
| 42 |
} |
| 43 |
|
| 44 |
auto sup = static_cast<long int>(l.find(">", 0)); |
| 45 |
while (sup != -1) { |
| 46 |
l.replace(sup, 1, ">"); |
| 47 |
sup = static_cast<long int>(l.find(">", 0)); |
| 48 |
} |
| 49 |
m_TestsDescription[NDEFINE] += l; |
| 50 |
delete [] val; |
| 51 |
|
| 52 |
bool hasError = false; |
| 53 |
bool notDefined = false; |
| 54 |
|
| 55 |
|
| 56 |
// We should have a #ifndef/#define on the same line and we go |
| 57 |
// through the file until we find a correct match. |
| 58 |
bool doesMatch = true; |
| 59 |
|
| 60 |
// Find the #ifndef word in the file |
| 61 |
auto pos = static_cast<long int>(m_BufferNoComment.find("#ifndef", 0)); |
| 62 |
if(pos == -1) |
| 63 |
{ |
| 64 |
notDefined = true; |
| 65 |
} |
| 66 |
else |
| 67 |
{ |
| 68 |
for(long int i=0;i<pos;i++) |
| 69 |
{ |
| 70 |
if((m_BufferNoComment[i] != ' ') |
| 71 |
&& (m_BufferNoComment[i] != '\r') |
| 72 |
&& (m_BufferNoComment[i] != '\n') |
| 73 |
) |
| 74 |
{ |
| 75 |
notDefined = true; |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
long int definepos = pos; |
| 81 |
|
| 82 |
if(notDefined) |
| 83 |
{ |
| 84 |
Error error; |
| 85 |
error.line = this->GetLineNumber(0,true); |
| 86 |
error.line2 = error.line; |
| 87 |
error.number = NDEFINE; |
| 88 |
error.description = "#ifndef not defined"; |
| 89 |
m_ErrorList.push_back(error); |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
while(!notDefined) |
| 94 |
{ |
| 95 |
doesMatch = true; |
| 96 |
|
| 97 |
// Find the word after #ifndef |
| 98 |
pos += 8; |
| 99 |
|
| 100 |
while(m_BufferNoComment[pos] == ' ') |
| 101 |
{ |
| 102 |
pos++; |
| 103 |
} |
| 104 |
long int begin = pos; |
| 105 |
while((m_BufferNoComment[pos] != ' ') |
| 106 |
&&(m_BufferNoComment[pos] != '\r') |
| 107 |
&&(m_BufferNoComment[pos] != '\n') |
| 108 |
) |
| 109 |
{ |
| 110 |
pos++; |
| 111 |
} |
| 112 |
long int end = pos; |
| 113 |
std::string ifndef = m_BufferNoComment.substr(begin,end-begin); |
| 114 |
|
| 115 |
// Find the word after #define |
| 116 |
auto posDef = static_cast<long int>(m_BufferNoComment.find("#define", end)); |
| 117 |
|
| 118 |
if(posDef == -1) |
| 119 |
{ |
| 120 |
Error error; |
| 121 |
error.line = this->GetLineNumber(end,true); |
| 122 |
error.line2 = error.line; |
| 123 |
error.number = NDEFINE; |
| 124 |
error.description = "#define not defined"; |
| 125 |
m_ErrorList.push_back(error); |
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
posDef += 7; |
| 130 |
while(m_BufferNoComment[posDef] == ' ') |
| 131 |
{ |
| 132 |
posDef++; |
| 133 |
} |
| 134 |
begin = posDef; |
| 135 |
while((m_BufferNoComment[posDef] != ' ') |
| 136 |
&&(m_BufferNoComment[posDef] != '\r') |
| 137 |
&&(m_BufferNoComment[posDef] != '\n') |
| 138 |
) |
| 139 |
{ |
| 140 |
posDef++; |
| 141 |
} |
| 142 |
end = posDef; |
| 143 |
std::string define = m_BufferNoComment.substr(begin,end-begin); |
| 144 |
|
| 145 |
if(ifndef != define) |
| 146 |
{ |
| 147 |
doesMatch = false; |
| 148 |
} |
| 149 |
|
| 150 |
if(doesMatch) |
| 151 |
{ |
| 152 |
if (m_Filename.empty()) { |
| 153 |
std::cout << "CheckIfNDefDefine() : m_Filename should be set" << std::endl; |
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
auto point = static_cast<long int>(m_Filename.find_last_of(".")); |
| 158 |
auto slash = static_cast<long int>(m_Filename.find_last_of("/")); |
| 159 |
|
| 160 |
std::string nameofclass = m_Filename.substr(slash+1,point-slash-1); |
| 161 |
std::string extension = m_Filename.substr(point+1,m_Filename.size()-point-1); |
| 162 |
|
| 163 |
// construct the string |
| 164 |
std::string toMatch = match; |
| 165 |
auto posnofc = static_cast<long int>(toMatch.find("[NameOfClass]")); |
| 166 |
if(posnofc != -1) |
| 167 |
{ |
| 168 |
toMatch.replace(posnofc,13,nameofclass); |
| 169 |
} |
| 170 |
posnofc = static_cast<long int>(toMatch.find("[Extension]")); |
| 171 |
if(posnofc != -1) |
| 172 |
{ |
| 173 |
toMatch.replace(posnofc,11,extension); |
| 174 |
} |
| 175 |
|
| 176 |
if(uppercaseTheDefinition) |
| 177 |
{ |
| 178 |
for(std::string::iterator it = toMatch.begin(); it != toMatch.end(); it++) |
| 179 |
*it = static_cast<char>(std::toupper(*it)); |
| 180 |
} |
| 181 |
|
| 182 |
if(ifndef != toMatch) |
| 183 |
{ |
| 184 |
Error error; |
| 185 |
error.line = this->GetLineNumber(definepos,true); |
| 186 |
error.line2 = this->GetLineNumber(end,true); |
| 187 |
error.number = NDEFINE; |
| 188 |
error.description = "#ifndef/#define does not match expression"; |
| 189 |
error.description = ifndef+" v.s. "+toMatch; |
| 190 |
m_ErrorList.push_back(error); |
| 191 |
return false; |
| 192 |
} |
| 193 |
else |
| 194 |
{ |
| 195 |
return true; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
pos = static_cast<long int>(m_BufferNoComment.find("#ifndef",pos+1)); |
| 200 |
|
| 201 |
if(pos == -1) |
| 202 |
{ |
| 203 |
notDefined = true; |
| 204 |
} |
| 205 |
/*else |
| 206 |
{ |
| 207 |
for(int i=0;i<pos;i++) |
| 208 |
{ |
| 209 |
if((m_BufferNoComment[i] != ' ') |
| 210 |
&& (m_BufferNoComment[i] != '\r') |
| 211 |
&& (m_BufferNoComment[i] != '\n') |
| 212 |
) |
| 213 |
{ |
| 214 |
std::cout << "Not defined!" << std::endl; |
| 215 |
notDefined = true; |
| 216 |
} |
| 217 |
} |
| 218 |
}*/ |
| 219 |
|
| 220 |
} // end looking for entire file |
| 221 |
|
| 222 |
|
| 223 |
Error error; |
| 224 |
error.line = this->GetLineNumber(definepos,true); |
| 225 |
error.line2 = error.line; |
| 226 |
error.number = NDEFINE; |
| 227 |
error.description = "#ifndef/#define does not match"; |
| 228 |
m_ErrorList.push_back(error); |
| 229 |
|
| 230 |
return !hasError; |
| 231 |
} |
| 232 |
|
| 233 |
} // end namespace kws |