forked from danmar/cppcheck
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtoken.cpp
More file actions
592 lines (498 loc) · 13.2 KB
/
token.cpp
File metadata and controls
592 lines (498 loc) · 13.2 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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*
* 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/
*/
#include "token.h"
#include <cstdlib>
#include <cstring>
#include <string>
#include <iostream>
#include <cctype>
#include <sstream>
#include <map>
Token::Token() :
_str(""),
_isName(false),
_isNumber(false),
_isBoolean(false),
_varId(0),
_next(0),
_previous(0),
_link(0),
_fileIndex(0),
_linenr(0)
{
}
Token::~Token()
{
}
void Token::str(const std::string &s)
{
_str = s;
_isName = bool(_str[0] == '_' || std::isalpha(_str[0]));
_isNumber = bool(std::isdigit(_str[(_str[0] == '-') ? 1 : 0]) != 0);
if (_str == "true" || _str == "false")
_isBoolean = true;
else
_isBoolean = false;
_varId = 0;
}
void Token::str(const char s[])
{
str(std::string(s));
}
void Token::concatStr(std::string const& b)
{
_str.erase(_str.length() - 1);
_str.append(b.begin() + 1, b.end());
}
void Token::deleteNext()
{
Token *n = _next;
_next = n->next();
delete n;
if (_next)
_next->previous(this);
}
void Token::deleteThis()
{
if (_next)
{
_str = _next->_str;
_isName = _next->_isName;
_isNumber = _next->_isNumber;
_isBoolean = _next->_isBoolean;
_varId = _next->_varId;
_fileIndex = _next->_fileIndex;
_linenr = _next->_linenr;
_link = _next->_link;
deleteNext();
}
else if (_previous)
{
// This should never be used for tokens
// at the end of the list
str(";");
}
else
{
// We are the last token in the list, we can't delete
// ourselves, so just make us ;
str(";");
}
}
void Token::replace(Token *replaceThis, Token *start, Token *end)
{
// Fix the whole in the old location of start and end
if (start->previous())
start->previous()->next(end->next());
if (end->next())
end->next()->previous(start->previous());
// Move start and end to their new location
if (replaceThis->previous())
replaceThis->previous()->next(start);
if (replaceThis->next())
replaceThis->next()->previous(end);
start->previous(replaceThis->previous());
end->next(replaceThis->next());
// Delete old token, which is replaced
delete replaceThis;
}
const Token *Token::tokAt(int index) const
{
const Token *tok = this;
int num = abs(index);
while (num > 0 && tok)
{
if (index > 0)
tok = tok->next();
else
tok = tok->previous();
--num;
}
return tok;
}
Token *Token::tokAt(int index)
{
Token *tok = this;
int num = abs(index);
while (num > 0 && tok)
{
if (index > 0)
tok = tok->next();
else
tok = tok->previous();
--num;
}
return tok;
}
const char *Token::strAt(int index) const
{
const Token *tok = this->tokAt(index);
return tok ? tok->_str.c_str() : "";
}
int Token::multiCompare(const char *haystack, const char *needle)
{
bool emptyStringFound = false;
bool noMatch = false;
const char *needlePointer = needle;
for (; *haystack; ++haystack)
{
if (*haystack == '|')
{
if (noMatch)
{
// We didn't have a match at this round
noMatch = false;
}
else if (*needlePointer == 0)
{
// If needle and haystack are both at the end, we have a match.
return 1;
}
else if (needlePointer == needle)
{
// If needlePointer was not increased at all, we had a empty
// string in the haystack
emptyStringFound = true;
}
needlePointer = needle;
continue;
}
if (noMatch)
continue;
// If haystack and needle don't share the same character,
// find next '|' character.
if (*needlePointer != *haystack)
{
noMatch = true;
continue;
}
// All characters in haystack and needle have matched this far
++needlePointer;
}
if (!noMatch)
{
if (*needlePointer == 0)
{
// If both needle and haystack are at the end, then we have a match.
return 1;
}
else if (needlePointer == needle)
{
// Last string in haystack was empty string e.g. "one|two|"
return 0;
}
}
// If empty string was found earlier from the haystack
if (emptyStringFound)
return 0;
return -1;
}
bool Token::simpleMatch(const Token *tok, const char pattern[])
{
const char *current, *next;
current = pattern;
next = strchr(pattern, ' ');
if (!next)
next = pattern + strlen(pattern);
while (*current)
{
size_t length = static_cast<size_t>(next - current);
if (!tok || length != tok->_str.length() || strncmp(current, tok->_str.c_str(), length))
return false;
current = next;
if (*next)
{
next = strchr(++current, ' ');
if (!next)
next = current + strlen(current);
}
tok = tok->next();
}
return true;
}
bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
{
const char *p = pattern;
bool firstpattern = true;
while (*p)
{
// Skip spaces in pattern..
while (*p == ' ')
++p;
// Extract token from pattern..
char str[500];
char *s = str;
while (*p && *p != ' ')
{
*s = *p;
++s;
++p;
}
*s = 0;
// No token => Success!
if (str[0] == 0)
return true;
if (!tok)
{
// If we have no tokens, pattern "!!else" should return true
if (str[1] == '!' && str[0] == '!' && str[2] != '\0')
continue;
else
return false;
}
// If we are in the first token, we skip all initial !! patterns
if (firstpattern && !tok->previous() && tok->next() && str[1] == '!' && str[0] == '!' && str[2] != '\0')
continue;
firstpattern = false;
// Compare the first character of the string for optimization reasons
// before doing more detailed checks.
bool patternIdentified = false;
if (str[0] == '%')
{
// Any symbolname..
if (strcmp(str, "%var%") == 0)
{
if (!tok->isName())
return false;
patternIdentified = true;
}
// Type..
if (strcmp(str, "%type%") == 0)
{
if (!tok->isName())
return false;
if (tok->str() == "delete")
return false;
patternIdentified = true;
}
// Accept any token
else if (strcmp(str, "%any%") == 0)
{
patternIdentified = true;
}
else if (strcmp(str, "%varid%") == 0)
{
if (varid == 0)
{
std::cout << "\n###### If you see this, there is a bug ###### Token::Match() - varid was 0" << std::endl;
}
if (tok->varId() != varid)
return false;
patternIdentified = true;
}
else if (strcmp(str, "%num%") == 0)
{
if (!tok->isNumber())
return false;
patternIdentified = true;
}
else if (strcmp(str, "%bool%") == 0)
{
if (!tok->isBoolean())
return false;
patternIdentified = true;
}
else if (strcmp(str, "%str%") == 0)
{
if (tok->_str[0] != '\"')
return false;
patternIdentified = true;
}
}
if (patternIdentified)
{
// Pattern was identified already above.
}
// [.. => search for a one-character token..
else if (str[0] == '[' && strchr(str, ']') && tok->_str[1] == 0)
{
*strrchr(str, ']') = 0;
if (strchr(str + 1, tok->_str[0]) == 0)
return false;
}
// Parse multi options, such as void|int|char (accept token which is one of these 3)
else if (strchr(str, '|') && (str[0] != '|' || strlen(str) > 2))
{
int res = multiCompare(str, tok->_str.c_str());
if (res == 0)
{
// Empty alternative matches, use the same token on next round
continue;
}
else if (res == -1)
{
// No match
return false;
}
}
// Parse "not" options. Token can be anything except the given one
else if (str[1] == '!' && str[0] == '!' && str[2] != '\0')
{
if (strcmp(tok->str().c_str(), &(str[2])) == 0)
return false;
}
else if (str != tok->_str)
return false;
tok = tok->next();
}
// The end of the pattern has been reached and nothing wrong has been found
return true;
}
bool Token::isName() const
{
return _isName;
}
bool Token::isNumber() const
{
return _isNumber;
}
bool Token::isBoolean() const
{
return _isBoolean;
}
bool Token::isStandardType() const
{
bool ret = false;
const char *type[] = {"bool", "char", "short", "int", "long", "float", "double", 0};
for (int i = 0; type[i]; i++)
ret |= (_str == type[i]);
return ret;
}
//---------------------------------------------------------------------------
const Token *Token::findmatch(const Token *tok, const char pattern[], unsigned int varId)
{
for (; tok; tok = tok->next())
{
if (Token::Match(tok, pattern, varId))
return tok;
}
return 0;
}
unsigned int Token::varId() const
{
return _varId;
}
void Token::varId(unsigned int id)
{
_varId = id;
}
Token *Token::next() const
{
return _next;
}
void Token::next(Token *next)
{
_next = next;
}
Token *Token::previous() const
{
return _previous;
}
void Token::previous(Token *previous)
{
_previous = previous;
}
void Token::insertToken(const char str[])
{
Token *newToken = new Token;
newToken->str(str);
newToken->_linenr = _linenr;
newToken->_fileIndex = _fileIndex;
if (this->next())
{
newToken->next(this->next());
newToken->next()->previous(newToken);
}
this->next(newToken);
newToken->previous(this);
}
void Token::eraseTokens(Token *begin, const Token *end)
{
if (! begin)
return;
while (begin->next() && begin->next() != end)
{
begin->deleteNext();
}
}
unsigned int Token::fileIndex() const
{
return _fileIndex;
}
void Token::fileIndex(unsigned int fileIndex)
{
_fileIndex = fileIndex;
}
unsigned int Token::linenr() const
{
return _linenr;
}
void Token::linenr(unsigned int linenr)
{
_linenr = linenr;
}
void Token::link(Token *link)
{
_link = link;
}
Token *Token::link() const
{
return _link;
}
void Token::printOut(const char *title) const
{
std::cout << stringifyList(true, title) << std::endl;
}
std::string Token::stringifyList(const bool varid, const char *title) const
{
std::ostringstream ret;
if (title)
ret << "\n### " << title << " ###\n";
unsigned int linenr = 0;
int fileIndex = -1;
std::map<unsigned int, unsigned int> lineNumbers;
for (const Token *tok = this; tok; tok = tok->next())
{
bool fileChange = false;
if (static_cast<int>(tok->_fileIndex) != fileIndex)
{
if (fileIndex != -1)
{
lineNumbers[fileIndex] = tok->_fileIndex;
}
fileIndex = static_cast<int>(tok->_fileIndex);
ret << "\n\n##file " << fileIndex << "";
linenr = lineNumbers[fileIndex];
fileChange = true;
}
if (linenr != tok->linenr() || fileChange)
{
while (linenr < tok->linenr())
{
++linenr;
ret << "\n" << linenr << ":";
}
linenr = tok->linenr();
}
ret << " " << tok->str();
if (varid && tok->varId() > 0)
ret << "@" << tok->varId();
}
ret << "\n";
return ret.str();
}