forked from Kitware/KWStyle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kwsCheckFunctions.cxx
181 lines (159 loc) · 5.8 KB
/
kwsCheckFunctions.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
/*=========================================================================
Program: KWStyle - Kitware Style Checker
Module: kwsCheckFunctions.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 if ANY function implement in the given file is correct */
bool Parser::CheckFunctions(const char* regEx,unsigned long maxLength)
{
if(regEx)
{
m_TestsDone[FUNCTION_REGEX] = true;
m_TestsDescription[FUNCTION_REGEX] = "Functions should match regular expression: ";
m_TestsDescription[FUNCTION_REGEX] += regEx;
}
if(maxLength>0)
{
m_TestsDone[FUNCTION_LENGTH] = true;
m_TestsDescription[FUNCTION_LENGTH] = "Functions must not exceed: ";
char* temp = new char[10];
sprintf(temp,"%ld",maxLength);
m_TestsDescription[FUNCTION_LENGTH] += temp;
m_TestsDescription[FUNCTION_LENGTH] += " lines";
delete [] temp;
}
// First we need to find the parameters
bool hasError = false;
kwssys::RegularExpression regex(regEx);
// List all the function in the file
long int pos = static_cast<long int>(this->FindFunction(0));
while(pos != -1)
{
// We extract the name of the function
std::string functionName = "";
// Find the ) and the openning (
long int i=pos;
for(;i>0;i--)
{
if(m_BufferNoComment[i] == ')')
{
i = static_cast<long int>(this->FindOpeningChar(')','(',i,true));
i--;
break;
}
}
bool inWord = false;
for(;i>0;i--)
{
if(m_BufferNoComment[i] != ' ' && m_BufferNoComment[i] != '\t'
&& m_BufferNoComment[i] != '\r' && m_BufferNoComment[i] != '\n'
&& m_BufferNoComment[i] != '*' && m_BufferNoComment[i] != '&')
{
inWord = true;
functionName = m_BufferNoComment[i]+functionName;
}
else if(inWord)
{
break;
}
}
// Check that this is not a #define (tricky)
std::string functionLine = this->GetLine(this->GetLineNumber(i,true)-1);
if(functionLine.find("#define") == std::string::npos
&& functionLine.find("_attribute_") == std::string::npos
&& functionLine.find(" operator") == std::string::npos
&& functionLine.find("friend ") == std::string::npos
&& functionName.find("if") == std::string::npos
&& functionName.find("while") == std::string::npos
&& functionName.find("for") == std::string::npos
&& functionName.find("switch") == std::string::npos
&& functionName.find("main") == std::string::npos
&& functionName.find("~") == std::string::npos // skip destructor for now...
)
{
long int posf = static_cast<long int>(functionName.find("::",0));
long int posp = static_cast<long int>(functionName.find("(",posf));
if(posp != -1 && posf != -1 && posp>posf)
{
functionName = functionName.substr(posf+2,posp-posf-2);
}
else if(posf != -1)
{
functionName = functionName.substr(posf+2,functionName.size()-posf-2);
}
}
else
{
functionName = "";
}
//std::cout << "Function Name = " << functionName.c_str() << std::endl;
if(functionName.size() == 0)
{
long int bf = static_cast<long int>(m_BufferNoComment.find('{',pos));
long int pos2 = static_cast<long int>(this->FindClosingChar('{','}',bf,true));
pos = static_cast<long int>(this->FindFunction(pos2+1));
// we cannot go backward
/*if(pos2 > pos)
{
long int bf = m_BufferNoComment.find('{',pos2);
pos = this->FindFunction(bf);
}*/
continue;
}
else if(functionName.size()>0)
{
long int bf = static_cast<long int>(m_BufferNoComment.find('{',pos));
long int bfcomments = static_cast<long int>(GetPositionWithComments(bf));
long int bfl = static_cast<long int>(this->GetLineNumber(bfcomments));
long int pos2 = static_cast<long int>(this->FindClosingChar('{','}',bf,true));
long int poscomments = static_cast<long int>(GetPositionWithComments(pos2));
long int efl = this->GetLineNumber(poscomments);
pos = static_cast<long int>(this->FindFunction(pos2+1));
// we cannot go backward
if(pos2 > pos)
{
long int localbf = static_cast<long int>(m_BufferNoComment.find('{',pos2));
pos = static_cast<long int>(this->FindFunction(localbf));
}
if(!regex.find(functionName))
{
Error error;
error.line = bfl;
error.line2 = error.line;
error.number = FUNCTION_REGEX;
error.description = "function (" + functionName + ") doesn't match regular expression: " + regEx;
m_ErrorList.push_back(error);
hasError = true;
}
if(maxLength>0)
{
if((bfl>-1) && (efl>-1) && (efl-bfl>(long int)maxLength))
{
Error error;
error.line = bfl;
error.line2 = efl;
error.number = FUNCTION_LENGTH;
error.description = "function (" + functionName + ") has too many lines: ";
char* temp = new char[10];
sprintf(temp,"%ld",efl-bfl);
error.description += temp;
error.description += " (";
sprintf(temp,"%ld",maxLength);
error.description += temp;
error.description += ")";
m_ErrorList.push_back(error);
hasError = true;
delete [] temp;
}
}
}
}
return !hasError;
}
} // end namespace kws