Debian Codegraph @

kwsCheckVariables.cxx

raw
1 /*=========================================================================
2
3 Program: KWStyle - Kitware Style Checker
4 Module: kwsCheckVariables.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 variables implementation of the class are correct */
19 bool Parser::CheckVariables(const char* regEx)
20 {
21 m_TestsDone[VARS] = true;
22 m_TestsDescription[VARS] = "ivars implementation should match regular expression: ";
23 m_TestsDescription[VARS] += regEx;
24
25 // First we need to find the parameters
26 bool hasError = false;
27
28 kwssys::RegularExpression regex(regEx);
29
30 // We first read the .h if any
31 std::string headerfile = kwssys::SystemTools::GetFilenamePath(m_Filename.c_str());
32 headerfile += "/";
33 headerfile += kwssys::SystemTools::GetFilenameWithoutExtension(m_Filename.c_str());
34 headerfile += ".h";
35
36 if(!kwssys::SystemTools::FileExists(headerfile.c_str()))
37 {
38 return false;
39 }
40
41 // We open the file
42 std::ifstream file;
43 file.open(headerfile.c_str(), std::ios::binary | std::ios::in);
44 if(!file.is_open())
45 {
46 std::cout << "Cannot open file: " << headerfile.c_str() << std::endl;
47 return true;
48 }
49
50 file.seekg(0,std::ios::end);
51 size_t fileSize = file.tellg();
52 file.seekg(0,std::ios::beg);
53
54 char* buf = new char[fileSize+1];
55 file.read(buf,fileSize);
56 buf[fileSize] = 0;
57 std::string buffer(buf);
58 buffer.resize(fileSize);
59 delete [] buf;
60
61 file.close();
62
63 this->ConvertBufferToWindowsFileType(buffer);
64 buffer = this->RemoveComments(buffer.c_str());
65
66 // Construct the list of variables to check
67 using PairType = std::pair<std::string, long>;
68 std::vector<PairType> ivars;
69 size_t pos = 0;
70 while(pos != std::string::npos)
71 {
72 std::string var = this->FindVariable(buffer,pos+1,buffer.size(),pos);
73 if (!var.empty()) {
74 std::string correct = "";
75 for (char i : var) {
76 if (i != '*') {
77 correct += i;
78 }
79 }
80 PairType p;
81 p.first = correct;
82 p.second = this->GetLineNumber(pos,true)-1;
83 ivars.push_back(p);
84 }
85 }
86
87 // Do the checking
88 std::vector<PairType>::const_iterator it = ivars.begin();
89 while(it != ivars.end())
90 {
91 std::string v = (*it).first;
92 long int p =(*it).second;
93 size_t posVar = m_BufferNoComment.find(v);
94 while(posVar != std::string::npos)
95 {
96 // Extract the complete insert of the variable
97 if(!this->IsBetweenQuote(posVar)
98 &&(
99 m_BufferNoComment[posVar-1]=='.'
100 || m_BufferNoComment[posVar-1]=='>'
101 || m_BufferNoComment[posVar-1]=='\n'
102 || m_BufferNoComment[posVar-1]==' '
103 || m_BufferNoComment[posVar-1]=='('
104 || m_BufferNoComment[posVar-1]=='['
105 )
106 &&(
107 m_BufferNoComment[posVar+v.size()]=='.'
108 || m_BufferNoComment[posVar+v.size()]=='>'
109 || m_BufferNoComment[posVar+v.size()]=='\n'
110 || m_BufferNoComment[posVar+v.size()]==' '
111 || m_BufferNoComment[posVar+v.size()]=='('
112 || m_BufferNoComment[posVar+v.size()]=='['
113 || m_BufferNoComment[posVar+v.size()]==')'
114 || m_BufferNoComment[posVar+v.size()]==']'
115 )
116 )
117 {
118
119 size_t i = posVar-1;
120 while(i>0)
121 {
122 if(m_BufferNoComment[i]==' '
123 || m_BufferNoComment[i]=='('
124 || m_BufferNoComment[i]=='['
125 || m_BufferNoComment[i]=='\n'
126 || m_BufferNoComment[i]=='!'
127 || m_BufferNoComment[i]=='{'
128 || m_BufferNoComment[i]==';'
129 || m_BufferNoComment[i]=='<'
130 || m_BufferNoComment[i]=='*'
131 )
132 {
133 break;
134 }
135 i--;
136 }
137
138 std::string var = m_BufferNoComment.substr(i+1,posVar-i+v.size()-1);
139
140 bool showError = true;
141
142 // Check if this a macro
143 if(this->GetLineNumber(posVar,true) == p+1)
144 {
145 showError = false;
146 }
147 else
148 {
149 std::string line = this->GetLine(this->GetLineNumber(posVar,true)-1);
150 if(line.find("Macro") != std::string::npos)
151 {
152 showError = false;
153 }
154 }
155
156 // Check the regex
157 if(showError && !regex.find(var))
158 {
159 Error error;
160 error.line = this->GetLineNumber(posVar,true);
161 error.line2 = error.line;
162 error.number = VARS;
163 error.description = "variable (" + var + ") doesn't match regular expression";
164 m_ErrorList.push_back(error);
165 hasError = true;
166 }
167 }
168 posVar = m_BufferNoComment.find(v,posVar+1);
169 }
170 it++;
171 }
172 return !hasError;
173 }
174
175 /** Find the first ivar in the source code */
176 std::string Parser::FindVariable(std::string & buffer, size_t start, size_t end,size_t & pos)
177 {
178 size_t posSemicolon = buffer.find(";",start);
179 while(posSemicolon != std::string::npos && posSemicolon<end)
180 {
181 // We try to find the word before that
182 size_t i = posSemicolon-1;
183 bool inWord = true;
184 bool first = false;
185 std::string ivar = "";
186 while(i!=std::string::npos && inWord)
187 {
188 if(buffer[i] != ' ')
189 {
190 if((buffer[i] == '}')
191 || (buffer[i] == ')')
192 || (buffer[i] == ']')
193 || (buffer[i] == '\n')
194 )
195 {
196 inWord = false;
197 }
198 else
199 {
200 std::string store = ivar;
201 ivar = buffer[i];
202 ivar += store;
203 inWord = true;
204 first = true;
205 }
206 }
207 else // we have a space
208 {
209 if(first)
210 {
211 inWord = false;
212 }
213 }
214 i--;
215 }
216 pos = posSemicolon;
217 // We extract the complete definition.
218 // This means that we look for a '{' or '}' or '{' or ':'
219 // but not '::'
220 while(i != std::string::npos)
221 {
222 if(buffer[i] == ';')
223 {
224 break;
225 }
226 else if(buffer[i] == ':')
227 {
228 if((buffer[i-1] != ':') && (buffer[i+1] != ':'))
229 {
230 break;
231 }
232 }
233 i--;
234 }
235
236 std::string subphrase = "";
237
238 if(i != std::string::npos)
239 {
240 subphrase = buffer.substr(i+1,posSemicolon-i-1);
241 }
242
243 if( (subphrase.find("=") == std::string::npos)
244 && (subphrase.find("(") == std::string::npos)
245 && (subphrase.find("typedef") == std::string::npos)
246 && (subphrase.find("}") == std::string::npos)
247 && (subphrase.find("friend") == std::string::npos)
248 && (subphrase.find("class") == std::string::npos)
249 && (subphrase.find("return") == std::string::npos)
250 && (subphrase.find("\"") == std::string::npos)
251 && (subphrase.find("<<") == std::string::npos)
252 )
253 {
254 // Check that we are not inside a function(){}
255 if(!this->IsInFunction(posSemicolon,buffer.c_str())
256 && !this->IsInStruct(posSemicolon,buffer.c_str())
257 )
258 {
259 return ivar;
260 }
261 }
262
263 posSemicolon = buffer.find(";",posSemicolon+1);
264 }
265
266 pos = std::string::npos;
267 return "";
268 }
269
270 } // end namespace kws