Debian Codegraph @

kwsCheckComments.cxx

raw
1 /*=========================================================================
2
3 Program: KWStyle - Kitware Style Checker
4 Module: kwsCheckComments.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 the comments
19 * The comment definition should be set before CheckIndent() to get the correct indentation
20 * for the comments. */
21 bool Parser::CheckComments(const char* begin,const char* middle,const char* end,
22 bool allowEmptyLineBeforeClass,
23 bool checkWrongComment,
24 bool checkMissingComment)
25 {
26 bool hasError = false;
27
28 if(checkWrongComment)
29 {
30 m_TestsDone[WRONGCOMMENT] = true;
31 m_TestsDescription[WRONGCOMMENT] = "The comments are misspelled";
32
33 // Set the ivars for the indent checking
34 m_CommentBegin = begin;
35 m_CommentMiddle = middle;
36 m_CommentEnd = end;
37
38 // We check if we have duplicate code in the comments
39 std::vector<PairType>::const_iterator it = m_CommentPositions.begin();
40 while(it != m_CommentPositions.end())
41 {
42 std::string previousWord = "";
43 size_t i = (*it).first;
44 while((i<m_Buffer.size()) && i<(*it).second)
45 {
46 // we go to the next space
47 while((i<m_Buffer.size()) && ((m_Buffer[i] != ' ') && (m_Buffer[i] != '\r') && (m_Buffer[i] != '\r')) && i<(*it).second)
48 {
49 i++;
50 }
51 bool inWord = true;
52 bool first = false;
53 std::string word = "";
54
55 while((i<m_Buffer.size()) && i<(*it).second && inWord)
56 {
57 if(m_Buffer[i] != ' ' && m_Buffer[i] != '\r' && m_Buffer[i] != '\n')
58 {
59 word += m_Buffer[i];
60 inWord = true;
61 first = true;
62 }
63 else // we have a space
64 {
65 if(first)
66 {
67 inWord = false;
68 i--;
69 }
70 }
71 i++;
72 }
73
74 if (!word.empty()) {
75 if (word != previousWord) {
76 previousWord = word;
77 }
78 else if(previousWord.size() > 1 &&
79 previousWord[0] != '/'
80 && previousWord[0]<48 && previousWord[0]>57 // We check if the first word is not a number
81 ) // avoid single char and comment
82 {
83 Error error;
84 error.line = this->GetLineNumber(i,false);
85 error.line2 = error.line;
86 error.number = WRONGCOMMENT;
87 error.description = "Duplicate word: ";
88 error.description += previousWord;
89 m_ErrorList.push_back(error);
90 hasError = true;
91 }
92 }
93 }
94 it++;
95 } // end buffer loop
96 }
97
98 if(checkMissingComment)
99 {
100 // Check if there is a comment before each class
101 m_TestsDone[MISSINGCOMMENT] = true;
102 m_TestsDescription[MISSINGCOMMENT] = "The class should have previously defined comments starting with \\class";
103
104 if(allowEmptyLineBeforeClass)
105 {
106 m_TestsDescription[MISSINGCOMMENT] += " (allowing empty line)";
107 }
108 else
109 {
110 m_TestsDescription[MISSINGCOMMENT] += " (not allowing empty line)";
111 }
112
113 size_t pos = this->GetClassPosition(0);
114 while(pos != std::string::npos)
115 {
116 size_t poswithcom = this->GetPositionWithComments(pos);
117
118 // Find the last comment (remove spaces if any)
119 std::string commentEnd = "";
120 for (char j : m_CommentEnd) {
121 if (j != ' ') {
122 commentEnd += j;
123 }
124 }
125
126 size_t poscom = m_Buffer.find(commentEnd,0);
127 size_t poscomtemp = poscom;
128 while(poscomtemp!=std::string::npos && poscomtemp<poswithcom)
129 {
130 poscom = poscomtemp;
131 poscomtemp = m_Buffer.find(commentEnd,poscomtemp+1);
132 }
133
134 // if we don't have the comment
135 if((poscom == std::string::npos) || (poscom > poswithcom))
136 {
137 Error error;
138 error.line = this->GetLineNumber(poswithcom,false);
139 error.line2 = error.line;
140 error.number = MISSINGCOMMENT;
141 error.description = "Comment is missing for the class";
142 m_ErrorList.push_back(error);
143 hasError = true;
144 }
145 else
146 {
147 // We check if we have m_CommentEnd before an empty line
148 bool emptyLine = false;
149 bool gotchar = true;
150 for(size_t i=poscom;i<poswithcom;i++)
151 {
152 if(m_Buffer[i] == '\n')
153 {
154 if(!gotchar)
155 {
156 emptyLine = true;
157 break;
158 }
159 gotchar = false;
160 }
161
162 if( (m_Buffer[i] != '\n')
163 && (m_Buffer[i] != '\r')
164 && (m_Buffer[i] != ' '))
165 {
166 gotchar = true;
167 }
168 }
169
170 if(emptyLine)
171 {
172 if(!allowEmptyLineBeforeClass)
173 {
174 Error error;
175 error.line = this->GetLineNumber(poswithcom,false);
176 error.line2 = error.line;
177 error.number = MISSINGCOMMENT;
178 error.description = "Comment is missing for the class";
179 m_ErrorList.push_back(error);
180 hasError = true;
181 }
182 }
183 else // we check that the word \class exists
184 {
185 // Find the last
186 size_t poscombeg = m_Buffer.find(m_CommentBegin,0);
187 size_t poscombegt = poscombeg;
188 while(poscombegt!=std::string::npos && poscombegt<poscom)
189 {
190 poscombeg = poscombegt;
191 poscombegt = m_Buffer.find(m_CommentBegin,poscombegt+1);
192 }
193
194 if(poscombeg!=std::string::npos && (poscom-poscombeg>0))
195 {
196 std::string sub = m_Buffer.substr(poscombeg,poscom-poscombeg);
197 if(sub.find("\\class") == std::string::npos)
198 {
199 Error error;
200 error.line = this->GetLineNumber(poswithcom,false);
201 error.line2 = error.line;
202 error.number = MISSINGCOMMENT;
203 error.description = "comment doesn't have \\class";
204 m_ErrorList.push_back(error);
205 hasError = true;
206 }
207 }
208 }
209 }
210 pos = this->GetClassPosition(pos+1);
211 }
212 } // end check missing comments
213 return !hasError;
214 }
215
216 } // end namespace kws