Debian Codegraph @

kwsCheckDeclarationOrder.cxx

raw
1 /*=========================================================================
2
3 Program: KWStyle - Kitware Style Checker
4 Module: kwsCheckDeclarationOrder.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 /** Check the order of the declaration */
20 bool Parser::CheckDeclarationOrder(size_t posPublic, size_t posProtected, size_t posPrivate)
21 {
22 m_TestsDone[DECL_ORDER] = true;
23 constexpr size_t length = 255;
24 char* val = new char[length];
25 snprintf(val,length,"Declaration order should match ");
26 m_TestsDescription[DECL_ORDER] = val;
27
28 if(posPublic>posProtected && posProtected>posPrivate)
29 {
30 m_TestsDescription[DECL_ORDER] += "Private/Protected/Public";
31 }
32 else if(posPublic<posProtected && posProtected<posPrivate)
33 {
34 m_TestsDescription[DECL_ORDER] += "Public/Protected/Private";
35 }
36 else if(posPublic>posPrivate && posProtected<posPrivate)
37 {
38 m_TestsDescription[DECL_ORDER] += "Protected/Private/Public";
39 }
40 else if(posPublic<posPrivate && posProtected<posPrivate)
41 {
42 m_TestsDescription[DECL_ORDER] += "Protected/Public/Private";
43 }
44
45 delete [] val;
46
47 size_t publicFirst;
48 size_t publicLast;
49 this->FindPublicArea(publicFirst,publicLast);
50
51 size_t class_end = this->FindEndOfClass(publicFirst);
52 size_t class_beg = this->FindOpeningChar('}','{',class_end,true);
53
54 // Find the first public declaration
55 while(publicFirst!=std::string::npos && publicFirst!=MAX_CHAR && (publicFirst<class_beg || publicFirst>class_end))
56 {
57 this->FindPublicArea(publicFirst,publicLast,publicFirst+1);
58 class_end = this->FindEndOfClass(publicFirst);
59 class_beg = this->FindOpeningChar('}','{',class_end,true);
60 }
61
62 // Currently checking only one class per file (do a loop in the future)
63 size_t currentclass = class_beg;
64
65 size_t protectedFirst;
66 size_t protectedLast;
67 this->FindProtectedArea(protectedFirst,protectedLast);
68
69 class_end = this->FindEndOfClass(protectedFirst);
70 class_beg = this->FindOpeningChar('}','{',class_end,true);
71 while(protectedFirst!=std::string::npos && protectedFirst!=MAX_CHAR &&class_beg != currentclass)
72 {
73 this->FindProtectedArea(protectedFirst,protectedLast,protectedFirst+1);
74 class_end = this->FindEndOfClass(protectedFirst);
75 class_beg = this->FindOpeningChar('}','{',class_end,true);
76 }
77
78 size_t privateFirst;
79 size_t privateLast;
80 this->FindPrivateArea(privateFirst,privateLast);
81
82 class_end = this->FindEndOfClass(privateFirst);
83 class_beg = this->FindOpeningChar('}','{',class_end,true);
84
85 while(privateFirst!=std::string::npos && privateFirst!=MAX_CHAR && class_beg != currentclass)
86 {
87 this->FindPrivateArea(privateFirst,privateLast,privateFirst+1);
88 class_end = this->FindEndOfClass(privateFirst);
89 class_beg = this->FindOpeningChar('}','{',class_end,true);
90 }
91
92 bool hasError = false;
93
94 // public v.s protected
95 if( (posPublic > posProtected)
96 && (protectedFirst != MAX_CHAR)
97 && (publicFirst != MAX_CHAR)
98 && (protectedFirst > publicFirst)
99 )
100 {
101 Error error;
102 error.line = this->GetLineNumber(publicFirst,true);
103 error.line2 = this->GetLineNumber(publicFirst,true);
104 error.number = DECL_ORDER;
105 error.description = "Public defined before Protected";
106 m_ErrorList.push_back(error);
107 hasError = true;
108 }
109 else if(
110 (posPublic < posProtected)
111 && (protectedFirst != MAX_CHAR)
112 && (publicFirst != MAX_CHAR)
113 && (protectedFirst < publicFirst)
114 )
115 {
116 Error error;
117 error.line = this->GetLineNumber(protectedFirst,true);
118 error.line2 = this->GetLineNumber(protectedFirst,true);
119 error.number = DECL_ORDER;
120 error.description = "Protected defined before Public";
121 m_ErrorList.push_back(error);
122 hasError = true;
123 }
124
125 // protected v.s. private
126 if( (posPrivate > posProtected)
127 && (protectedFirst != MAX_CHAR)
128 && (privateFirst != MAX_CHAR)
129 && (protectedFirst > privateFirst)
130 )
131 {
132 Error error;
133 error.line = this->GetLineNumber(privateFirst,true);
134 error.line2 = this->GetLineNumber(privateFirst,true);
135 error.number = DECL_ORDER;
136 error.description = "Private defined before Protected";
137 m_ErrorList.push_back(error);
138 hasError = true;
139 }
140 else if(
141 (posPrivate > posProtected)
142 && (protectedFirst != MAX_CHAR)
143 && (privateFirst != MAX_CHAR)
144 && (protectedFirst > privateFirst)
145 )
146 {
147 Error error;
148 error.line = this->GetLineNumber(protectedFirst,true);
149 error.line2 = this->GetLineNumber(protectedFirst,true);
150 error.number = DECL_ORDER;
151 error.description = "Protected defined before Private";
152 m_ErrorList.push_back(error);
153 hasError = true;
154 }
155
156 // Public v.s. Private
157 if( (posPublic > posPrivate)
158 && (privateFirst != MAX_CHAR)
159 && (publicFirst != MAX_CHAR)
160 && (privateFirst > publicFirst)
161 )
162 {
163 Error error;
164 error.line = this->GetLineNumber(publicFirst,true);
165 error.line2 = this->GetLineNumber(publicFirst,true);
166 error.number = DECL_ORDER;
167 error.description = "Public defined before Private";
168 m_ErrorList.push_back(error);
169 hasError = true;
170 }
171 else if(
172 (posPublic > posPrivate)
173 && (privateFirst != MAX_CHAR)
174 && (publicFirst != MAX_CHAR)
175 && (privateFirst > publicFirst)
176 )
177 {
178 Error error;
179 error.line = this->GetLineNumber(privateFirst,true);
180 error.line2 = this->GetLineNumber(privateFirst,true);
181 error.number = DECL_ORDER;
182 error.description = "Private defined before Public";
183 m_ErrorList.push_back(error);
184 hasError = true;
185 }
186
187 return !hasError;
188 }
189
190 } // end namespace kws