Debian Codegraph @

kwsCheckSemicolonSpace.cxx

raw
1 /*=========================================================================
2
3 Program: KWStyle - Kitware Style Checker
4 Module: kwsCheckSemicolonSpace.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 space between the end of the declaration
20 * and the semicolon */
21 bool Parser::CheckSemicolonSpace(unsigned long max)
22 {
23 m_TestsDone[SEMICOLON_SPACE] = true;
24 constexpr size_t length = 255;
25 char* val = new char[length];
26 snprintf(val,length,"Semicolons = %ld max spaces",max);
27 m_TestsDescription[SEMICOLON_SPACE] = val;
28 delete [] val;
29
30 bool hasError = false;
31 auto posSemicolon = static_cast<long int>(m_BufferNoComment.find(";", 0));
32 while(posSemicolon != -1)
33 {
34 // We try to find the word before that
35 long i=posSemicolon-1;
36 unsigned long space = 0;
37 while(i>=0)
38 {
39 if(m_BufferNoComment[i] == ' ')
40 {
41 if(!IsBetweenQuote(i))
42 {
43 space++;
44 }
45 }
46 else
47 {
48 if(m_BufferNoComment[i] != '\n' && space > max)
49 {
50 Error error;
51 error.line = this->GetLineNumber(posSemicolon,true);
52 error.line2 = error.line;
53 error.number = SEMICOLON_SPACE;
54 error.description = "Number of spaces before semicolon exceed: ";
55 constexpr size_t length = 10;
56 char* localval = new char[length];
57 snprintf(localval,length,"%ld",space);
58 error.description += localval;
59 error.description += " (max=";
60 delete [] localval;
61 localval = new char[length];
62 snprintf(localval,length,"%ld",max);
63 error.description += localval;
64 error.description += ")";
65 delete [] localval;
66 m_ErrorList.push_back(error);
67 hasError = true;
68
69 if(m_FixFile)
70 {
71 this->ReplaceCharInFixedBuffer(this->GetPositionWithComments(posSemicolon-space),space,"");
72 }
73 } // space > max
74 break;
75 }
76 i--;
77 }
78 posSemicolon = static_cast<long int>(m_BufferNoComment.find(";",posSemicolon+1));
79 }
80
81 // We also check if we have two or more semicolons following each other
82 posSemicolon = static_cast<long int>(m_BufferNoComment.find(";",0));
83 while(posSemicolon != -1)
84 {
85 // We look forward
86 unsigned long i=posSemicolon+1;
87 while(i<m_BufferNoComment.size())
88 {
89 if(m_BufferNoComment[i] == ' ' || m_BufferNoComment[i] == '\r' || m_BufferNoComment[i] == '\n')
90 {
91 // this is normal
92 }
93 else if(m_BufferNoComment[i] == ';')
94 {
95 // if we have for ( ;; ) we don't report
96 // or ";;"
97 if(!this->IsBetweenCharsFast('(',')',i)
98 && !this->IsBetweenQuote(i))
99 {
100 Error error;
101 error.line = this->GetLineNumber(i,true);
102 error.line2 = error.line;
103 error.number = SEMICOLON_SPACE;
104 error.description = "Too many semicolons";
105 m_ErrorList.push_back(error);
106 hasError = true;
107
108 if(m_FixFile)
109 {
110 this->ReplaceCharInFixedBuffer(this->GetPositionWithComments(i),1,"");
111 }
112 }
113 }
114 else
115 {
116 break;
117 }
118 i++;
119 }
120 posSemicolon = static_cast<long int>(m_BufferNoComment.find(";",i+1));
121 }
122
123 return !hasError;
124 }
125
126 } // end namespace kws