Debian Codegraph @

kwsCheckFunctions.cxx

raw
1 /*=========================================================================
2
3 Program: KWStyle - Kitware Style Checker
4 Module: kwsCheckFunctions.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 if ANY function implement in the given file is correct */
19 bool Parser::CheckFunctions(const char* regEx,unsigned long maxLength)
20 {
21 if(regEx)
22 {
23 m_TestsDone[FUNCTION_REGEX] = true;
24 m_TestsDescription[FUNCTION_REGEX] = "Functions should match regular expression: ";
25 m_TestsDescription[FUNCTION_REGEX] += regEx;
26 }
27
28 if(maxLength>0)
29 {
30 m_TestsDone[FUNCTION_LENGTH] = true;
31 m_TestsDescription[FUNCTION_LENGTH] = "Functions must not exceed: ";
32 constexpr size_t length = 22;
33 char* temp = new char[length];
34 snprintf(temp,length,"%ld",maxLength);
35 m_TestsDescription[FUNCTION_LENGTH] += temp;
36 m_TestsDescription[FUNCTION_LENGTH] += " lines";
37 delete [] temp;
38 }
39
40 // First we need to find the parameters
41 bool hasError = false;
42
43 kwssys::RegularExpression regex(regEx);
44
45 // List all the function in the file
46 auto pos = static_cast<long int>(this->FindFunction(0));
47 while(pos != -1)
48 {
49 // We extract the name of the function
50 std::string functionName = "";
51 // Find the ) and the openning (
52 long int i=pos;
53 for(;i>0;i--)
54 {
55 if(m_BufferNoComment[i] == ')')
56 {
57 i = static_cast<long int>(this->FindOpeningChar(')','(',i,true));
58 i--;
59 break;
60 }
61 }
62
63 bool inWord = false;
64 for(;i>0;i--)
65 {
66 if(m_BufferNoComment[i] != ' ' && m_BufferNoComment[i] != '\t'
67 && m_BufferNoComment[i] != '\r' && m_BufferNoComment[i] != '\n'
68 && m_BufferNoComment[i] != '*' && m_BufferNoComment[i] != '&')
69 {
70 inWord = true;
71 functionName = m_BufferNoComment[i]+functionName;
72 }
73 else if(inWord)
74 {
75 break;
76 }
77 }
78
79 // Check that this is not a #define (tricky)
80 std::string functionLine = this->GetLine(this->GetLineNumber(i,true)-1);
81
82 if(functionLine.find("#define") == std::string::npos
83 && functionLine.find("_attribute_") == std::string::npos
84 && functionLine.find(" operator") == std::string::npos
85 && functionLine.find("friend ") == std::string::npos
86 && functionName.find("if") == std::string::npos
87 && functionName.find("while") == std::string::npos
88 && functionName.find("for") == std::string::npos
89 && functionName.find("switch") == std::string::npos
90 && functionName.find("main") == std::string::npos
91 && functionName.find("~") == std::string::npos // skip destructor for now...
92 )
93 {
94 auto posf = static_cast<long int>(functionName.find("::", 0));
95 auto posp = static_cast<long int>(functionName.find("(", posf));
96 if(posp != -1 && posf != -1 && posp>posf)
97 {
98 functionName = functionName.substr(posf+2,posp-posf-2);
99 }
100 else if(posf != -1)
101 {
102 functionName = functionName.substr(posf+2,functionName.size()-posf-2);
103 }
104 }
105 else
106 {
107 functionName = "";
108 }
109
110 //std::cout << "Function Name = " << functionName.c_str() << std::endl;
111
112 if (functionName.empty()) {
113 auto bf = static_cast<long int>(m_BufferNoComment.find('{', pos));
114 long int pos2 =
115 static_cast<long int>(this->FindClosingChar('{', '}', bf, true));
116 pos = static_cast<long int>(this->FindFunction(pos2 + 1));
117
118 // we cannot go backward
119 /*if(pos2 > pos)
120 {
121 long int bf = m_BufferNoComment.find('{',pos2);
122 pos = this->FindFunction(bf);
123 }*/
124 continue;
125 } else if (!functionName.empty()) {
126 auto bf = static_cast<long int>(m_BufferNoComment.find('{', pos));
127 auto bfcomments = static_cast<long int>(GetPositionWithComments(bf));
128 auto bfl = static_cast<long int>(this->GetLineNumber(bfcomments));
129 long int pos2 =
130 static_cast<long int>(this->FindClosingChar('{', '}', bf, true));
131 auto poscomments = static_cast<long int>(GetPositionWithComments(pos2));
132 long int efl = this->GetLineNumber(poscomments);
133
134 pos = static_cast<long int>(this->FindFunction(pos2 + 1));
135
136 // we cannot go backward
137 if (pos2 > pos) {
138 auto localbf =
139 static_cast<long int>(m_BufferNoComment.find('{', pos2));
140 pos = static_cast<long int>(this->FindFunction(localbf));
141 }
142
143 if(!regex.find(functionName))
144 {
145 Error error;
146 error.line = bfl;
147 error.line2 = error.line;
148 error.number = FUNCTION_REGEX;
149 error.description = "function (" + functionName + ") doesn't match regular expression: " + regEx;
150 m_ErrorList.push_back(error);
151 hasError = true;
152 }
153
154 if(maxLength>0)
155 {
156 if((bfl>-1) && (efl>-1) && (efl-bfl>(long int)maxLength))
157 {
158 Error error;
159 error.line = bfl;
160 error.line2 = efl;
161 error.number = FUNCTION_LENGTH;
162 error.description = "function (" + functionName + ") has too many lines: ";
163 constexpr size_t length = 22;
164 char* temp = new char[length];
165 snprintf(temp,length,"%ld",efl-bfl);
166 error.description += temp;
167 error.description += " (";
168 snprintf(temp,length,"%ld",maxLength);
169 error.description += temp;
170 error.description += ")";
171 m_ErrorList.push_back(error);
172 hasError = true;
173 delete [] temp;
174 }
175 }
176 }
177 }
178
179 return !hasError;
180 }
181
182 } // end namespace kws