-
Notifications
You must be signed in to change notification settings - Fork 0
/
kwsCheckIfNDefDefine.cxx
227 lines (193 loc) · 5.42 KB
/
kwsCheckIfNDefDefine.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*=========================================================================
Program: KWStyle - Kitware Style Checker
Module: kwsCheckIfNDefDefine.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 the #ifndef/#define is defined correctly for the class
* match can contain [NameOfClass] and [Extension] */
bool Parser::CheckIfNDefDefine(const char* match)
{
// Check only if we are not a .cxx or .cc or .c file
if(m_Filename.find(".c") != std::string::npos)
{
return false;
}
m_TestsDone[NDEFINE] = true;
char* val = new char[512];
sprintf(val,"#ifndef/#define should match ");
m_TestsDescription[NDEFINE] = val;
// Replace < and >
std::string l = match;
long int inf = l.find("<",0);
while(inf != -1)
{
l.replace(inf,1,"<");
inf = l.find("<",0);
}
long int sup = l.find(">",0);
while(sup != -1)
{
l.replace(sup,1,">");
sup = l.find(">",0);
}
m_TestsDescription[NDEFINE] += l;
delete [] val;
bool hasError = false;
bool notDefined = false;
// We should have a #ifndef/#define on the same line and we go
// through the file until we find a correct match.
bool doesMatch = true;
// Find the #ifndef word in the file
long int pos = m_BufferNoComment.find("#ifndef",0);
if(pos == -1)
{
notDefined = true;
}
else
{
for(long int i=0;i<pos;i++)
{
if((m_BufferNoComment[i] != ' ')
&& (m_BufferNoComment[i] != '\r')
&& (m_BufferNoComment[i] != '\n')
)
{
notDefined = true;
}
}
}
long int definepos = pos;
if(notDefined)
{
Error error;
error.line = this->GetLineNumber(0,true);
error.line2 = error.line;
error.number = NDEFINE;
error.description = "#ifndef not defined";
m_ErrorList.push_back(error);
return false;
}
while(!notDefined)
{
doesMatch = true;
// Find the word after #ifndef
pos += 8;
while(m_BufferNoComment[pos] == ' ')
{
pos++;
}
long int begin = pos;
while((m_BufferNoComment[pos] != ' ')
&&(m_BufferNoComment[pos] != '\r')
&&(m_BufferNoComment[pos] != '\n')
)
{
pos++;
}
long int end = pos;
std::string ifndef = m_BufferNoComment.substr(begin,end-begin);
// Find the word after #define
long int posDef = m_BufferNoComment.find("#define",end);
if(posDef == -1)
{
Error error;
error.line = this->GetLineNumber(end,true);
error.line2 = error.line;
error.number = NDEFINE;
error.description = "#define not defined";
m_ErrorList.push_back(error);
return false;
}
posDef += 7;
while(m_BufferNoComment[posDef] == ' ')
{
posDef++;
}
begin = posDef;
while((m_BufferNoComment[posDef] != ' ')
&&(m_BufferNoComment[posDef] != '\r')
&&(m_BufferNoComment[posDef] != '\n')
)
{
posDef++;
}
end = posDef;
std::string define = m_BufferNoComment.substr(begin,end-begin);
if(ifndef != define)
{
doesMatch = false;
}
if(doesMatch)
{
if(m_Filename == "")
{
std::cout << "CheckIfNDefDefine() : m_Filename shoud be set" << std::endl;
return false;
}
long int point = m_Filename.find_last_of(".");
long int slash = m_Filename.find_last_of("/");
std::string nameofclass = m_Filename.substr(slash+1,point-slash-1);
std::string extension = m_Filename.substr(point+1,m_Filename.size()-point-1);
// construct the string
std::string toMatch = match;
long int posnofc = toMatch.find("[NameOfClass]");
if(posnofc != -1)
{
toMatch.replace(posnofc,13,nameofclass);
}
posnofc = toMatch.find("[Extension]");
if(posnofc != -1)
{
toMatch.replace(posnofc,11,extension);
}
if(ifndef != toMatch)
{
Error error;
error.line = this->GetLineNumber(definepos,true);
error.line2 = this->GetLineNumber(end,true);
error.number = NDEFINE;
error.description = "#ifndef/#define does not match expression";
error.description = ifndef+" v.s. "+toMatch;
m_ErrorList.push_back(error);
return false;
}
else
{
return true;
}
}
pos = m_BufferNoComment.find("#ifndef",pos+1);
if(pos == -1)
{
notDefined = true;
}
/*else
{
for(int i=0;i<pos;i++)
{
if((m_BufferNoComment[i] != ' ')
&& (m_BufferNoComment[i] != '\r')
&& (m_BufferNoComment[i] != '\n')
)
{
std::cout << "Not defined!" << std::endl;
notDefined = true;
}
}
}*/
} // end looking for entire file
Error error;
error.line = this->GetLineNumber(definepos,true);
error.line2 = error.line;
error.number = NDEFINE;
error.description = "#ifndef/#define does not match";
m_ErrorList.push_back(error);
return !hasError;
}
} // end namespace kws