Debian Codegraph @

kwsCheckOperator.cxx

raw
1 /*=========================================================================
2
3 Program: KWStyle - Kitware Style Checker
4 Module: kwsCheckOperator.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 <string.h> // strlen
16
17 namespace kws {
18
19 /** Check the number of spaces before and after the operator */
20 bool Parser::CheckOperator(unsigned int before, unsigned int after,
21 unsigned long maxSize,
22 bool doNotCheckInsideParenthesis)
23 {
24 m_TestsDone[OPERATOR] = true;
25 m_TestsDescription[OPERATOR] = "Number of spaces for the operators should be: before=";
26 constexpr size_t length = 10;
27 char* val = new char[length];
28 snprintf(val,length,"%d",before);
29 m_TestsDescription[OPERATOR] += val;
30 snprintf(val,length,", after=%d",after);
31 m_TestsDescription[OPERATOR] += val;
32 delete [] val;
33
34 bool hasErrors = false;
35
36 if(!this->FindOperator("==",before,after,maxSize,doNotCheckInsideParenthesis))
37 {
38 hasErrors = true;
39 }
40
41 if(!this->FindOperator("!=",before,after,maxSize,doNotCheckInsideParenthesis))
42 {
43 hasErrors = true;
44 }
45
46 if(!this->FindOperator("-=",before,after,maxSize,doNotCheckInsideParenthesis))
47 {
48 hasErrors = true;
49 }
50
51 if(!this->FindOperator("/=",before,after,maxSize,doNotCheckInsideParenthesis))
52 {
53 hasErrors = true;
54 }
55
56 if(!this->FindOperator("+=",before,after,maxSize,doNotCheckInsideParenthesis))
57 {
58 hasErrors = true;
59 }
60
61 if(!this->FindOperator("*=",before,after,maxSize,doNotCheckInsideParenthesis))
62 {
63 hasErrors = true;
64 }
65
66 // We check the = operator but we want to make sure that it is not part
67 // of the previous defined operator
68 bool doNotCheck = false;
69 size_t operatorPos = m_BufferNoComment.find("=",0);
70
71 if( (doNotCheckInsideParenthesis && this->IsBetweenCharsFast('(',')',operatorPos,false))
72 || (operatorPos == m_BufferNoComment.find("!=",0)+1)
73 || (operatorPos == m_BufferNoComment.find("*=",0)+1)
74 || (operatorPos == m_BufferNoComment.find("-=",0)+1)
75 || (operatorPos == m_BufferNoComment.find("+=",0)+1)
76 || (operatorPos == m_BufferNoComment.find("==",0)+1)
77 || (operatorPos == m_BufferNoComment.find("operator",0)+9)
78 )
79 {
80 doNotCheck = true;
81 }
82
83 while(operatorPos != std::string::npos)
84 {
85 if(!doNotCheck)
86 {
87 // Check number of space before
88 unsigned int bef = 0;
89 long int i = static_cast<long int>(operatorPos)-1;
90 while((i>0) && (m_BufferNoComment[i] == ' '))
91 {
92 bef++;
93 i--;
94 }
95
96 // Check number of space after
97 unsigned int aft = 0;
98 i = static_cast<long int>(operatorPos)+1;
99 while((i<(long int)m_BufferNoComment.size()) && (m_BufferNoComment[i] == ' '))
100 {
101 aft++;
102 i++;
103 }
104
105 if(bef != before || aft != after)
106 {
107 Error error;
108 error.line = this->GetLineNumber(operatorPos,true);
109 error.line2 = error.line;
110 error.number = OPERATOR;
111 error.description = "Spaces around operator are wrong";
112 //m_ErrorList.push_back(error);
113 hasErrors = true;
114 }
115 }
116
117 doNotCheck = false;
118 size_t tmpoperatorPos = m_BufferNoComment.find("=",operatorPos+1);
119
120 if( (doNotCheckInsideParenthesis && this->IsBetweenCharsFast('(',')',tmpoperatorPos,false))
121 || (tmpoperatorPos == m_BufferNoComment.find("!=",operatorPos+1)+1)
122 || (tmpoperatorPos == m_BufferNoComment.find("*=",operatorPos+1)+1)
123 || (tmpoperatorPos == m_BufferNoComment.find("-=",operatorPos+1)+1)
124 || (tmpoperatorPos == m_BufferNoComment.find("+=",operatorPos+1)+1)
125 || (tmpoperatorPos == m_BufferNoComment.find("==",operatorPos+1)+1)
126 || (tmpoperatorPos == m_BufferNoComment.find("operator",operatorPos+1)+8)
127 )
128 {
129 doNotCheck = true;
130 }
131
132 operatorPos = tmpoperatorPos;
133
134 }
135
136
137 return !hasErrors;
138 }
139
140 /** Check the operator */
141 bool Parser::FindOperator(const char* op,unsigned int before,
142 unsigned int after,
143 unsigned long maxSize,
144 bool doNotCheckInsideParenthesis
145 )
146 {
147 bool hasErrors = false;
148 long int pos = 0;
149 size_t operatorPos = m_BufferNoComment.find(op,pos);
150 while(operatorPos != std::string::npos)
151 {
152 bool showError=true;
153 // Check number of space before
154 unsigned int bef = 0;
155 long int i = static_cast<long int>(operatorPos)-1;
156 while((i>0) && ((m_BufferNoComment[i] == ' ')
157 || (m_BufferNoComment[i] == '\r')
158 || (m_BufferNoComment[i] == '\n'))
159 )
160 {
161 if((m_BufferNoComment[i] != '\r')
162 && (m_BufferNoComment[i] != '\n'))
163 {
164 bef++;
165 }
166 else
167 {
168 // check if the sum of the two lines is higher than the maximum length
169 std::string currentLine = this->GetLine(this->GetLineNumber(i,true)-1);
170 std::string previousLine = this->GetLine(this->GetLineNumber(i,true));
171 auto sum = static_cast<unsigned long>(currentLine.size() +
172 previousLine.size());
173 if(sum>maxSize)
174 {
175 showError = false;
176 break;
177 }
178 }
179 i--;
180 }
181
182 // Check number of space after
183 unsigned int aft = 0;
184 i = static_cast<long int>(operatorPos+strlen(op));
185 while((i<(long int)m_BufferNoComment.size())
186 && ((m_BufferNoComment[i] == ' ')
187 || (m_BufferNoComment[i] == '\r')
188 || (m_BufferNoComment[i] == '\n')
189 )
190 )
191 {
192 if((m_BufferNoComment[i] == '\r')
193 || (m_BufferNoComment[i] == '\n'))
194 {
195 // check if the sum of the two lines is higher than the maximum length
196 std::string currentLine = this->GetLine(this->GetLineNumber(i,true)-1);
197 std::string nextLine = this->GetLine(this->GetLineNumber(i,true));
198 auto sum =
199 static_cast<unsigned long>(currentLine.size() + nextLine.size());
200 if(sum>maxSize)
201 {
202 showError = false;
203 break;
204 }
205 }
206 else
207 {
208 aft++;
209 }
210 i++;
211 }
212
213 if(showError && (bef != before || aft != after))
214 {
215 // we make sure that the keyword operator is not defined right before
216 if(operatorPos != m_BufferNoComment.find("operator",operatorPos-11)+8
217 && (doNotCheckInsideParenthesis && !this->IsBetweenCharsFast('(',')',operatorPos,false)
218 && !this->IsBetweenQuote(operatorPos,false)
219 )
220 )
221 {
222 Error error;
223 error.line = this->GetLineNumber(operatorPos,true);
224 error.line2 = error.line;
225 error.number = OPERATOR;
226 error.description = "Spaces around operator are wrong: ";
227 error.description += "before = ";
228 constexpr size_t length = 10;
229 char* val = new char[length];
230 snprintf(val,length,"%d",bef);
231 error.description += val;
232 error.description += " v.s ";
233 snprintf(val,length,"%d",before);
234 error.description += val;
235 error.description += " ,after = ";
236 snprintf(val,length,"%d",aft);
237 error.description += val;
238 error.description += " v.s ";
239 snprintf(val,length,"%d",after);
240 error.description += val;
241 m_ErrorList.push_back(error);
242 delete [] val;
243 hasErrors = true;
244 }
245 }
246 pos = static_cast<long int>(operatorPos)+1;
247 operatorPos = m_BufferNoComment.find(op,operatorPos+1);
248 }
249 return !hasErrors;
250 }
251
252
253 } // end namespace kws