| 1 |
/*========================================================================= |
| 2 |
|
| 3 |
Program: KWStyle - Kitware Style Checker |
| 4 |
Module: kwsCheckNamespace.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 |
/** Find the name space |
| 20 |
* using namespace is prohibited for this parser |
| 21 |
* We look if the closing bracket is at the end of the file, if yes we accept otherwise we reject*/ |
| 22 |
bool Parser::CheckNamespace(const char* name,bool doNotCheckMain) |
| 23 |
{ |
| 24 |
// If there is a 'int main' keyword in the file we do not check |
| 25 |
// Or if there is not keyword class |
| 26 |
if(doNotCheckMain) |
| 27 |
{ |
| 28 |
if( (m_BufferNoComment.find("main ",0) != std::string::npos) |
| 29 |
|| (m_BufferNoComment.find("class ",0) == std::string::npos) |
| 30 |
) |
| 31 |
{ |
| 32 |
return false; |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
m_TestsDone[NAMESPACE] = true; |
| 37 |
constexpr size_t length = 255; |
| 38 |
char* val = new char[length]; |
| 39 |
snprintf(val,length,"The first namespace of the file should match %s",name); |
| 40 |
m_TestsDescription[NAMESPACE] = val; |
| 41 |
delete [] val; |
| 42 |
|
| 43 |
std::string nameSpace = ""; |
| 44 |
|
| 45 |
size_t pos = m_BufferNoComment.find("namespace",0); |
| 46 |
|
| 47 |
if(pos == std::string::npos) |
| 48 |
{ |
| 49 |
Error error; |
| 50 |
error.line = this->GetLineNumber(0,true); |
| 51 |
error.line2 = error.line; |
| 52 |
error.number = NAMESPACE; |
| 53 |
error.description = "namespace not defined"; |
| 54 |
m_ErrorList.push_back(error); |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
// check that the word is not using namespace |
| 60 |
bool ok = true; |
| 61 |
if(this->FindPreviousWord(pos) == "using") |
| 62 |
{ |
| 63 |
ok = false; |
| 64 |
} |
| 65 |
|
| 66 |
if(ok) |
| 67 |
{ |
| 68 |
std::string names = name; |
| 69 |
// extract the namespace |
| 70 |
std::string nspace = this->FindNextWord(pos); |
| 71 |
if(nspace != names) |
| 72 |
{ |
| 73 |
Error error; |
| 74 |
error.line = this->GetLineNumber(pos,true); |
| 75 |
error.line2 = error.line; |
| 76 |
error.number = NAMESPACE; |
| 77 |
error.description = "namespace is wrong"; |
| 78 |
m_ErrorList.push_back(error); |
| 79 |
return false; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/* |
| 84 |
// Now search if we have other namespaces |
| 85 |
while(pos!= std::string::npos && ok) |
| 86 |
{ |
| 87 |
pos = buffer.find("namespace",pos+1); |
| 88 |
// check that the word is not using namespace |
| 89 |
use = buffer.find("using",pos-20); |
| 90 |
|
| 91 |
ok = true; |
| 92 |
|
| 93 |
if((use!=-1 && pos-use<20) || (pos == -1)) |
| 94 |
{ |
| 95 |
ok = false; |
| 96 |
} |
| 97 |
|
| 98 |
if(ok) |
| 99 |
{ |
| 100 |
// extract the namespace |
| 101 |
long int bracket = buffer.find("{",pos); |
| 102 |
long int closing = FindClosingBracket(buffer,bracket); |
| 103 |
|
| 104 |
// Find the number of } after closing |
| 105 |
int num = 0; |
| 106 |
int close = closing+1; |
| 107 |
while(buffer.find("}",close) != -1) |
| 108 |
{ |
| 109 |
num++; |
| 110 |
close=buffer.find("}",close); |
| 111 |
close++; |
| 112 |
} |
| 113 |
|
| 114 |
if(bracket != -1 && bracket-pos-10<10 && num==nNameSpace) |
| 115 |
{ |
| 116 |
std::string name = buffer.substr(pos+10,bracket-pos-10); |
| 117 |
RemoveCtrlN(name); |
| 118 |
RemoveChar(name,' '); |
| 119 |
RemoveCtrlN(name); |
| 120 |
nameSpace += "::"; |
| 121 |
nameSpace += name; |
| 122 |
nNameSpace++; |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
*/ |
| 127 |
|
| 128 |
return true; |
| 129 |
} |
| 130 |
|
| 131 |
} // end namespace kws |