Debian Codegraph @

kwsCheckVariablePerLine.cxx

raw
1 /*=========================================================================
2
3 Program: KWStyle - Kitware Style Checker
4 Module: kwsCheckVariablePerLine.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 the number of statements per line */
20 bool Parser::CheckVariablePerLine(unsigned long max)
21 {
22 m_TestsDone[VARIABLEPERLINE] = true;
23 constexpr size_t length = 255;
24 char* val = new char[length];
25 snprintf(val,length,"Variables per line = %ld max",max);
26 m_TestsDescription[VARIABLEPERLINE] = val;
27 delete [] val;
28
29 // For the moment only standard types are defined.
30 // We might be able to do more with finding typedefs
31 #define ntypes 12
32 const char* types[ntypes] = {"int","unsigned int",
33 "char","unsigned char",
34 "short","unsigned short",
35 "long","unsigned long",
36 "float","double","void","long int"};
37
38 bool hasError = false;
39 for (auto &type : types) {
40 std::string typeToFind = type;
41 typeToFind += " ";
42 size_t posType = m_BufferNoComment.find(typeToFind,0);
43 while(posType != std::string::npos)
44 {
45 // Check that this is the first word
46 bool firstWord = false;
47 auto pos = static_cast<long int>(posType);
48 pos--;
49 while((pos>0) && (m_BufferNoComment[pos]==' '))
50 {
51 pos--;
52 }
53 if((pos<=0) || (m_BufferNoComment[pos]=='\n'))
54 {
55 firstWord = true;
56 }
57
58 if(firstWord)
59 {
60 std::string line = this->GetLine(this->GetLineNumber(posType,true)-1);
61
62 // Check if we have any comments
63 int poscom = static_cast<int>(line.find("//",0));
64 if(poscom != -1)
65 {
66 line = line.substr(0,poscom);
67 }
68 poscom = static_cast<int>(line.find("/*",0));
69 while(poscom != -1)
70 {
71 int poscomend = static_cast<int>(line.find("*/",0));
72 if(poscomend == -1)
73 {
74 line = line.substr(0,poscom);
75 break;
76 }
77
78 std::string line_temp = line.substr(0,poscom);
79 line = line_temp+line.substr(poscomend+2,line.size()-poscomend-2);
80 poscom = static_cast<int>(line.find("/*",poscom+1));
81 }
82
83 // If we have any '(' in the line we stop
84 if( !this->IsBetweenChars( '(', ')', posType, false ) &&
85 !this->IsBetweenCharsFast( '(', ')', posType, false ) &&
86 !this->IsBetweenChars( '<', '>', posType, false ) &&
87 !this->IsBetweenCharsFast( '<', '>', posType, false ))
88 {
89 // This is a very simple check we count the number of comas
90 unsigned int vars = 1;
91 pos = static_cast<long int>(line.find(',',0));
92 while(pos!=-1)
93 {
94 posType = m_BufferNoComment.find(',', posType+1);
95 if( !this->IsBetweenChars( '(', ')', posType, false ) &&
96 !this->IsBetweenCharsFast( '(', ')', posType, false ) &&
97 !this->IsBetweenChars( '<', '>', posType, false ) &&
98 !this->IsBetweenCharsFast( '<', '>', posType, false ))
99 {
100 // Check that we are not initializing an array
101 bool betweenBraces = false;
102 long int openCurly = pos-1;
103 while(openCurly>0)
104 {
105 // Ok we have the opening
106 if(line[openCurly] == '{')
107 {
108 long int posClosing = static_cast<long int>(this->FindClosingChar('{','}',openCurly,false,line));
109 if(posClosing == -1
110 || pos<posClosing)
111 {
112 betweenBraces = true;
113 }
114 break;
115 }
116 openCurly--;
117 }
118 // Check if we are not at the end of the line
119 bool endofline = true;
120 long eof = pos+1;
121 while(eof < (int)line.size())
122 {
123 if(line[eof] != ' ' && line[eof] != '\n'
124 && line[eof] != '\r' && line[eof] != '\t')
125 {
126 endofline = false;
127 break;
128 }
129 eof++;
130 }
131
132 if(!betweenBraces && !endofline)
133 {
134 vars++;
135 }
136 }
137 pos = static_cast<long int>(line.find(',',pos+1));
138 }
139
140 if(vars > max)
141 {
142 // len(str(2**64)) == 20 + 1 for +/- + 1 for '\0'
143 char localval[22]={0}; //Make an extra large buffer to avoid compiler overflow warnings
144 Error error;
145 error.line = this->GetLineNumber(posType,true);
146 error.line2 = error.line;
147 error.number = VARIABLEPERLINE;
148 error.description = "Number of variable per line exceed: ";
149 snprintf(localval,sizeof(localval),"%d",vars);
150 error.description += localval;
151 error.description += " (max=";
152 localval[0]='\0';
153 snprintf(localval,sizeof(localval),"%ld",max);
154 error.description += localval;
155 error.description += ")";
156 m_ErrorList.push_back(error);
157 hasError = true;
158 }
159 }
160 }// end firstWord
161 posType = m_BufferNoComment.find(typeToFind,posType+1);
162 }
163 }
164
165 return !hasError;
166 }
167
168 } // end namespace kws