Debian Codegraph @

kwsCheckNameOfClass.cxx

raw
1 /*=========================================================================
2
3 Program: KWStyle - Kitware Style Checker
4 Module: kwsCheckNameOfClass.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 if the name of the class
20 * The class name is the one that is not between anything and does not have ;
21 * before the } */
22 bool Parser::CheckNameOfClass(const char* name,const char* prefix)
23 {
24 m_TestsDone[NAMEOFCLASS] = true;
25 m_TestsDescription[NAMEOFCLASS] = "The name of the class should match ";
26 if(prefix)
27 {
28 m_TestsDescription[NAMEOFCLASS] += prefix;
29 }
30 m_TestsDescription[NAMEOFCLASS] += name;
31
32 bool gotMatch = false;
33 bool gotAtLeastOne = false;
34
35 long int classpos = static_cast<long int>(this->GetClassPosition(0));
36 std::string nameOfClass = "";
37 while(classpos!=-1)
38 {
39 nameOfClass = this->FindPreviousWord(classpos);
40
41 if (m_Filename.empty()) {
42 std::cout << "CheckNameOfClass() : m_Filename should be set" << std::endl;
43 return false;
44 }
45
46 auto point = static_cast<long int>(m_Filename.find_last_of("."));
47 auto slash = static_cast<long int>(m_Filename.find_last_of("/"));
48
49 if(slash == -1)
50 {
51 slash = 0;
52 }
53
54 std::string nameofclass = m_Filename.substr(slash+1,point-slash-1);
55 std::string extension = m_Filename.substr(point+1,m_Filename.size()-point-1);
56
57 // construct the string
58 std::string toMatch = name;
59 auto p = static_cast<long int>(toMatch.find("[NameOfClass]"));
60 if(p != -1)
61 {
62 toMatch.replace(p,13,nameofclass);
63 }
64 p = static_cast<long int>(toMatch.find("[Extension]"));
65 if(p != -1)
66 {
67 toMatch.replace(p,11,extension);
68 }
69
70 if(prefix)
71 {
72 nameOfClass = prefix+nameOfClass;
73 }
74
75 gotAtLeastOne = true;
76 if(nameOfClass == toMatch)
77 {
78 gotMatch = true;
79 break;
80 }
81 classpos = static_cast<long int>(this->GetClassPosition(classpos+1));
82 }
83
84 if(!gotMatch && gotAtLeastOne && (classpos!=-1))
85 {
86 Error error;
87 error.line = this->GetLineNumber(classpos,true);
88 error.line2 = error.line;
89 error.number = NAMEOFCLASS;
90 error.description = "classname is not defined correctly";
91 m_ErrorList.push_back(error);
92 return false;
93 }
94
95 return true;
96 }
97
98
99 /** Returns the position of a class within the file.
100 * This function checks that we are in the class definition and not somewhere else
101 * The return position is a position right after the name of the class (meaning before a : or a {
102 * Returns std::string::npos if not found
103 */
104 size_t Parser::GetClassPosition(size_t position,std::string buffer) const
105 {
106 if(position == std::string::npos)
107 {
108 position = 0;
109 }
110
111 if (buffer.empty()) {
112 buffer = m_BufferNoComment;
113 }
114
115 size_t pos = buffer.find("class",position);
116
117 std::string nameOfClass = "";
118 while(pos!=std::string::npos)
119 {
120 if(!this->IsBetweenCharsFast('<','>',pos,false,buffer))
121 {
122 bool valid = true;
123 // We check that the word class is alone
124 if(pos>1)
125 {
126 if(buffer[pos-1] != ' ' && buffer[pos-1] != '/' && buffer[pos-1] != '\n')
127 {
128 valid = false;
129 }
130 }
131 if(pos<buffer.size()-2)
132 {
133 if(buffer[pos+5] != ' ' && buffer[pos+5] != '/' && buffer[pos+5] != '\r')
134 {
135 valid = false;
136 }
137 }
138
139 size_t i = pos+4;
140 // We should get a { before a ;
141 size_t brac = buffer.find('{',pos);
142 size_t sem = buffer.find(';',pos);
143
144 if(sem<=brac)
145 {
146 valid = false;
147 }
148 else
149 {
150 while((buffer[i] != '{')
151 && (i<buffer.size())
152 )
153 {
154 if(buffer[i] == ';')
155 {
156 valid = false;
157 break;
158 }
159 else if(buffer[i] == ':')
160 {
161 break;
162 }
163 i++;
164 }
165 }
166
167 if(valid)
168 {
169 return i;
170 }
171 }
172 pos = buffer.find("class",pos+1);
173 }
174
175 return std::string::npos;
176 }
177
178 } // end namespace kws