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