Debian Codegraph @

kwsCheckParenthesis.cxx

raw
1 /*=========================================================================
2
3 Program: KWStyle - Kitware Style Checker
4 Module: kwsCheckParenthesis.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 after ( and before ) */
20 bool Parser::CheckParenthesis( unsigned int space )
21 {
22 m_TestsDone[PARENTHESIS] = true;
23 m_TestsDescription[PARENTHESIS] =
24 "Number of spaces after ( and before ) should be = ";
25 constexpr size_t length = 200;
26 char* val = new char[length];
27 snprintf(val,length,"%d", space);
28 m_TestsDescription[PARENTHESIS] += val;
29 delete [] val;
30
31 bool hasErrors = false;
32
33 long int prevOperatorPos = 0;
34 size_t operatorPos = m_BufferNoComment.find("(", prevOperatorPos);
35 while(operatorPos != std::string::npos)
36 {
37 // Check number of space after
38 unsigned int aft = 0;
39 auto i = static_cast<long int>(operatorPos + 1);
40 while( i<(long int)m_BufferNoComment.size()
41 && m_BufferNoComment[i] == ' ' )
42 {
43 aft++;
44 i++;
45 }
46
47 if( i<(long int)m_BufferNoComment.size()
48 && ( m_BufferNoComment[i] == '\n' ||
49 m_BufferNoComment[i] == '\r' ) )
50 {
51 aft = space;
52 }
53
54 if( aft != space &&
55 !( i < (long int)m_BufferNoComment.size() && aft == 0 &&
56 ( m_BufferNoComment[i] == ')' ||
57 m_BufferNoComment[i] == '(' ) ) &&
58 !this->IsBetweenChars( '(', ')', operatorPos, false ) &&
59 !this->IsBetweenQuote(operatorPos,false) )
60 {
61 Error error;
62 error.line = this->GetLineNumber(operatorPos,true);
63 error.line2 = error.line;
64 error.number = PARENTHESIS;
65 error.description = "Spaces after ( = ";
66 constexpr size_t length = 200;
67 char* errorVal = new char[length];
68 snprintf(errorVal,length,"%d",aft);
69 error.description += errorVal;
70 error.description += " v.s ";
71 snprintf(errorVal,length,"%d",space);
72 error.description += errorVal;
73 m_ErrorList.push_back(error);
74 delete [] errorVal;
75 hasErrors = true;
76 }
77
78 prevOperatorPos = static_cast<long int>(operatorPos)+1;
79 if( prevOperatorPos < (long int)( m_BufferNoComment.size() ) - 1 )
80 {
81 operatorPos = m_BufferNoComment.find("(", prevOperatorPos);
82 }
83 else
84 {
85 operatorPos = std::string::npos;
86 }
87 }
88
89 prevOperatorPos = 0;
90 operatorPos = m_BufferNoComment.find(")", prevOperatorPos);
91 while(operatorPos != std::string::npos)
92 {
93 // Check number of space before
94 unsigned int bfr = 0;
95 auto i = static_cast<long int>(operatorPos);
96 while( i>0 && m_BufferNoComment[i-1] == ' ' )
97 {
98 ++bfr;
99 --i;
100 }
101
102 if( bfr != space &&
103 !( i > 0 && bfr == 0 &&
104 ( m_BufferNoComment[i-1] == '(' ||
105 m_BufferNoComment[i-1] == ')' ) ) &&
106 !this->IsBetweenChars( '(', ')', operatorPos, false ) &&
107 !this->IsBetweenQuote(operatorPos,false) )
108 {
109 Error error;
110 error.line = this->GetLineNumber(operatorPos,true);
111 error.line2 = error.line;
112 error.number = PARENTHESIS;
113 error.description = "Spaces before ) = ";
114 constexpr size_t length = 200;
115 char* errorVal = new char[length];
116 snprintf(errorVal,length,"%d",bfr);
117 error.description += errorVal;
118 error.description += " v.s ";
119 snprintf(errorVal,length,"%d",space);
120 error.description += errorVal;
121 m_ErrorList.push_back(error);
122 delete [] errorVal;
123 hasErrors = true;
124 }
125
126 prevOperatorPos = static_cast<long int>(operatorPos)+1;
127 if( prevOperatorPos < (long int)( m_BufferNoComment.size() ) - 1 )
128 {
129 operatorPos = m_BufferNoComment.find(")", prevOperatorPos);
130 }
131 else
132 {
133 operatorPos = std::string::npos;
134 }
135
136 }
137
138 return !hasErrors;
139 }
140
141
142 } // end namespace kws