forked from mathieu/cppcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenutils.h
More file actions
413 lines (386 loc) · 10.3 KB
/
Copy pathtokenutils.h
File metadata and controls
413 lines (386 loc) · 10.3 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2009 Daniel Marjamäki and Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/
*/
#ifndef __tokenutils__
#define __tokenutils__
#include <list>
#include <iostream>
#include <stdio.h>
#include <sstream>
#include "token.h"
#include "tokenize.h"
#define GET_ALL_BY_NAME(T, LIST, PARAM, NAME) \
std::list<T*>::iterator declIt; \
std::list<T*>* outList=new std::list<T*>(); \
for (declIt = LIST.begin(); declIt != LIST.end(); declIt++) \
{ \
if ((*declIt)->PARAM == NAME) \
outList->push_back(*declIt); \
}
#define APPLY_TO_LIST(T, LIST,IT) \
std::list<T>::iterator IT; \
for (IT = LIST.begin(); IT != LIST.end(); IT++)
namespace TUtils
{
static const std::string TObjectId = "TObject";
static const std::string TParId = "TPar";
static const std::string TCallId = "TCall";
static const std::string TContextId = "TContext";
static const std::string TSwitchContextId = "TSwitchContext";
static const std::string TSwitchCaseId = "TSwitchCase";
struct TObject
{
std::string ObjectId;
TObject()
{
ObjectId = TObjectId;
}
std::string toString()
{
return ObjectId;
}
};
struct TPar : public TObject
{
std::string name;
bool isPointer;
bool isContent;
bool isArray;
TPar()
{
isPointer = false;
isContent = false;
isArray = false;
ObjectId = TParId;
}
void Print()
{
printf("[%s P%d C%d A%d] ", name.c_str(), isPointer, isContent, isArray);
}
};
struct TCall : public TObject
{
std::string name;
const Token *token;
int line;
std::list<TPar*> parametersList;
TCall()
{
line = -1;
token = NULL;
ObjectId = TCallId;
}
void Print()
{
printf("Call to %s at line %d ", name.c_str(), line);
std::list<TPar*>::iterator parIt;
for (parIt = parametersList.begin(); parIt != parametersList.end(); parIt++)
{
(*parIt)->Print();
}
printf("\n");
}
};
struct TAssignement
{
std::string left;
std::string right;
int line;
bool leftIsAddress;
bool leftIsContent;
bool rightIsAddress;
bool rightIsContent;
const Token *token;
TAssignement()
{
left = "";
right = "";
leftIsAddress = false;
leftIsContent = false;
rightIsAddress = false;
rightIsContent = false;
line = -1;
}
void Print()
{
printf("Assignement at line %d. Left [%s] A %d C %d Right [%s] A %d C %d\n",
line,
left.c_str(),
leftIsAddress,
leftIsContent,
right.c_str(),
rightIsAddress,
rightIsContent
);
}
};
struct TDeclaration
{
std::string name;
std::string type;
const Token *token;
bool isPointer;
bool isSigned;
bool isUnsigned;
bool isShort;
bool isLong;
bool isLongLong;
bool isStatic;
bool isConst;
bool isArray;
TDeclaration* alias;
int line;
TDeclaration()
{
std::string name = "";
std::string type = "";
int line;
isPointer = false;
isSigned = false;
isUnsigned = false;
isShort = false;
isLong = false;
isLongLong = false;
isStatic = false;
isConst = false;
isArray = false;
line = 0;
token = NULL;
alias = NULL;
}
TDeclaration* getRealDeclaration()
{
TDeclaration* decl = alias;
while (decl->alias)
{
decl = decl->alias;
}
return decl;
}
void Print()
{
std::string str = "";
if (alias)
str = alias->name;
printf("Var %s (type %s) declared at %d: P %d SIGNED %d UNSIGNED %d SHORT %d LONG %d LL %d STATIC %d CONST %d ARRAY %d ALIAS %s\n",
name.c_str(),
type.c_str(),
line,
isPointer,
isSigned,
isUnsigned,
isShort,
isLong,
isLongLong,
isStatic,
isConst,
isArray,
str.c_str());
}
};
struct TContext : public TObject
{
int beginsAt;
int endsAt;
TContext* parent;
bool isFirstContext;
std::list<TDeclaration*> declarationList;
std::list<TAssignement*> assignementList;
std::list<TCall*> callList;
std::list<TContext*> contextList;
std::list<TAssignement*>* getAllAssignementsWithLeft(std::string name)
{
GET_ALL_BY_NAME(TAssignement, assignementList, left, name)
return outList;
}
std::list<TAssignement*>* getAllAssignementsWithin(int begins,int ends)
{
std::list<TAssignement*>* res=new std::list<TAssignement*>();
APPLY_TO_LIST(TAssignement*,assignementList,assIt)
{
if (((*assIt)->line > begins) && ((*assIt)->line <= ends))
res->push_back((*assIt));
}
return res;
}
TDeclaration * getDeclaration(std::string name)
{
std::list<TDeclaration*>::iterator declIt;
for (declIt = declarationList.begin(); declIt != declarationList.end(); declIt++)
{
if ((*declIt)->name == name)
return *declIt;
}
if (!isFirstContext)
{
return parent->getDeclaration(name);
}
return NULL;
}
TCall* getFirstCall(std::string name)
{
std::list<TCall*>::iterator callIt;
for (callIt = callList.begin();callIt != callList.end();callIt++)
{
if ((*callIt)->name == name)
return *callIt;
}
return NULL;
}
std::list<TCall*> getAllCalls(std::string name)
{
std::list<TCall*> list;
std::list<TCall*>::iterator callIt;
for (callIt = callList.begin();callIt != callList.end();callIt++)
{
if ((*callIt)->name == name)
list.push_back(*callIt);
}
return list;
}
TContext()
{
beginsAt = -1;
endsAt = -1;
parent = NULL;
isFirstContext = true;
ObjectId = TContextId;
}
~TContext()
{
std::list<TDeclaration*>::iterator it;
for (it = declarationList.begin(); it != declarationList.end(); it++)
{
delete *it;
}
declarationList.clear();
std::list<TContext*>::iterator it2;
for (it2 = contextList.begin(); it2 != contextList.end(); it2++)
{
delete *it2;
}
contextList.clear();
}
virtual void Print()
{
printf("Context from line %d to line %d {\n", beginsAt, endsAt);
std::list<TDeclaration*>::iterator declIt;
printf("Declarations{\n");
for (declIt = declarationList.begin(); declIt != declarationList.end(); declIt++)
{
(*declIt)->Print();
}
printf("}\n");
printf("Assignements{\n");
std::list<TAssignement*>::iterator assIt;
for (assIt = assignementList.begin();assIt != assignementList.end();assIt++)
{
(*assIt)->Print();
}
printf("}\n");
printf("Calls{\n");
std::list<TCall*>::iterator callIt;
for (callIt = callList.begin();callIt != callList.end();callIt++)
{
(*callIt)->Print();
}
printf("}\n");
printf("Sub Context{\n");
std::list<TContext*>::iterator scIt;
for (scIt = contextList.begin(); scIt != contextList.end(); scIt++)
{
(*scIt)->Print();
}
printf("}\n");
printf("}\n");
}
};
struct TSwitchCase : public TObject
{
int beginsAt;
int endsAt;
bool hasBreak;
bool isDefault;
TSwitchCase()
{
beginsAt = -1;
endsAt = -1;
hasBreak = false;
isDefault = false;
ObjectId = TSwitchCaseId;
}
void Print()
{
printf("case B %d E %d HB %d ID %d\n", beginsAt, endsAt, hasBreak, isDefault);
}
};
struct TSwitchContext : public TContext
{
bool isSwitch;
std::list<TSwitchCase*> caseList;
TSwitchContext()
{
TContext();
isSwitch = true;
ObjectId = TSwitchContextId;
}
TSwitchCase* getCaseThatContainsLine(int line)
{
std::list<TSwitchCase*>::iterator cIt;
for (cIt = caseList.begin();cIt != caseList.end();cIt++)
{
if (line >= (*cIt)->beginsAt && line <= (*cIt)->endsAt)
return *cIt;
}
return NULL;
}
void Print()
{
printf("Switch ");
TContext::Print();
printf("Switch cases{\n");
std::list<TSwitchCase*>::iterator cIt;
for (cIt = caseList.begin();cIt != caseList.end();cIt++)
{
(*cIt)->Print();
}
printf("}");
}
};
void checkSingleContext(TContext* context);
class tokenutils
{
private:
std::list<TDeclaration*> _list;
bool first;
TContext* context;
static const std::string keywords[];
static const std::string operators[];
public:
TDeclaration* getDeclData(std::string varName);
const Token *createContext(const Token *tok, TContext* parent, TContext* current, bool first);
TContext* getContext();
tokenutils(const Tokenizer *tokenizer);
~tokenutils();
static bool isLanguageKeyword(const Token *tok);
static bool isLanguageOperator(const Token *tok);
static TAssignement* getAssignement(const Token *tok, int* shift);
static TDeclaration* getDeclaration(const Token *tok, int* shift);
static TCall* getCall(const Token *tok, int* shift);
};
}
#endif // __tokenutils__