-
Notifications
You must be signed in to change notification settings - Fork 30
/
kwsCheckSemicolonSpace.cxx
126 lines (113 loc) · 3.72 KB
/
kwsCheckSemicolonSpace.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
/*=========================================================================
Program: KWStyle - Kitware Style Checker
Module: kwsCheckSemicolonSpace.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 number of space between the end of the declaration
* and the semicolon */
bool Parser::CheckSemicolonSpace(unsigned long max)
{
m_TestsDone[SEMICOLON_SPACE] = true;
constexpr size_t length = 255;
char* val = new char[length];
snprintf(val,length,"Semicolons = %ld max spaces",max);
m_TestsDescription[SEMICOLON_SPACE] = val;
delete [] val;
bool hasError = false;
auto posSemicolon = static_cast<long int>(m_BufferNoComment.find(";", 0));
while(posSemicolon != -1)
{
// We try to find the word before that
long i=posSemicolon-1;
unsigned long space = 0;
while(i>=0)
{
if(m_BufferNoComment[i] == ' ')
{
if(!IsBetweenQuote(i))
{
space++;
}
}
else
{
if(m_BufferNoComment[i] != '\n' && space > max)
{
Error error;
error.line = this->GetLineNumber(posSemicolon,true);
error.line2 = error.line;
error.number = SEMICOLON_SPACE;
error.description = "Number of spaces before semicolon exceed: ";
constexpr size_t length = 10;
char* localval = new char[length];
snprintf(localval,length,"%ld",space);
error.description += localval;
error.description += " (max=";
delete [] localval;
localval = new char[length];
snprintf(localval,length,"%ld",max);
error.description += localval;
error.description += ")";
delete [] localval;
m_ErrorList.push_back(error);
hasError = true;
if(m_FixFile)
{
this->ReplaceCharInFixedBuffer(this->GetPositionWithComments(posSemicolon-space),space,"");
}
} // space > max
break;
}
i--;
}
posSemicolon = static_cast<long int>(m_BufferNoComment.find(";",posSemicolon+1));
}
// We also check if we have two or more semicolons following each other
posSemicolon = static_cast<long int>(m_BufferNoComment.find(";",0));
while(posSemicolon != -1)
{
// We look forward
unsigned long i=posSemicolon+1;
while(i<m_BufferNoComment.size())
{
if(m_BufferNoComment[i] == ' ' || m_BufferNoComment[i] == '\r' || m_BufferNoComment[i] == '\n')
{
// this is normal
}
else if(m_BufferNoComment[i] == ';')
{
// if we have for ( ;; ) we don't report
// or ";;"
if(!this->IsBetweenCharsFast('(',')',i)
&& !this->IsBetweenQuote(i))
{
Error error;
error.line = this->GetLineNumber(i,true);
error.line2 = error.line;
error.number = SEMICOLON_SPACE;
error.description = "Too many semicolons";
m_ErrorList.push_back(error);
hasError = true;
if(m_FixFile)
{
this->ReplaceCharInFixedBuffer(this->GetPositionWithComments(i),1,"");
}
}
}
else
{
break;
}
i++;
}
posSemicolon = static_cast<long int>(m_BufferNoComment.find(";",i+1));
}
return !hasError;
}
} // end namespace kws