/
tokenize.cpp
4357 lines (3775 loc) · 127 KB
/
tokenize.cpp
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 "tokenize.h"
#include "token.h"
#include "filelister.h"
#include "mathlib.h"
#include "settings.h"
#include "errorlogger.h"
#include "check.h"
#include <locale>
#include <fstream>
#include <string>
#include <cstring>
#include <iostream>
#include <sstream>
#include <list>
#include <cassert>
#include <algorithm>
#include <cctype>
#include <stack>
#include <stdexcept> // for std::runtime_error
//---------------------------------------------------------------------------
Tokenizer::Tokenizer()
: _settings(0), _errorLogger(0)
{
_tokens = 0;
_tokensBack = 0;
}
Tokenizer::Tokenizer(const Settings *settings, ErrorLogger *errorLogger)
: _settings(settings), _errorLogger(errorLogger)
{
_tokens = 0;
_tokensBack = 0;
}
Tokenizer::~Tokenizer()
{
deallocateTokens();
}
//---------------------------------------------------------------------------
// Helper functions..
//---------------------------------------------------------------------------
const Token *Tokenizer::tokens() const
{
return _tokens;
}
const std::vector<std::string> *Tokenizer::getFiles() const
{
return &_files;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// addtoken
// add a token. Used by 'Tokenizer'
//---------------------------------------------------------------------------
void Tokenizer::addtoken(const char str[], const unsigned int lineno, const unsigned int fileno)
{
if (str[0] == 0)
return;
// Replace hexadecimal value with decimal
std::ostringstream str2;
if (strncmp(str, "0x", 2) == 0)
{
str2 << std::strtoul(str + 2, NULL, 16);
}
else
{
str2 << str;
}
if (_tokensBack)
{
_tokensBack->insertToken(str2.str().c_str());
_tokensBack = _tokensBack->next();
}
else
{
_tokens = new Token;
_tokensBack = _tokens;
_tokensBack->str(str2.str());
}
_tokensBack->linenr(lineno);
_tokensBack->fileIndex(fileno);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// SizeOfType - gives the size of a type
//---------------------------------------------------------------------------
unsigned int Tokenizer::sizeOfType(const Token *type) const
{
if (!type || !type->strAt(0))
return 0;
if (type->str()[0] == '"')
return static_cast<unsigned int>(Token::getStrLength(type) + 1);
std::map<std::string, unsigned int>::const_iterator it = _typeSize.find(type->strAt(0));
if (it == _typeSize.end())
return 0;
return it->second;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// InsertTokens - Copy and insert tokens
//---------------------------------------------------------------------------
void Tokenizer::insertTokens(Token *dest, const Token *src, unsigned int n)
{
while (n > 0)
{
dest->insertToken(src->str().c_str());
dest = dest->next();
dest->fileIndex(src->fileIndex());
dest->linenr(src->linenr());
dest->varId(src->varId());
src = src->next();
--n;
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Tokenize - tokenizes a given file.
//---------------------------------------------------------------------------
void Tokenizer::createTokens(std::istream &code)
{
// line number in parsed code
unsigned int lineno = 1;
// The current token being parsed
std::string CurrentToken;
// lineNumbers holds line numbers for files in fileIndexes
// every time an include file is complitely parsed, last item in the vector
// is removed and lineno is set to point to that value.
std::vector<unsigned int> lineNumbers;
// fileIndexes holds index for _files vector about currently parsed files
// every time an include file is complitely parsed, last item in the vector
// is removed and FileIndex is set to point to that value.
std::vector<unsigned int> fileIndexes;
// FileIndex. What file in the _files vector is read now?
unsigned int FileIndex = 0;
// Read one byte at a time from code and create tokens
for (char ch = (char)code.get(); code.good(); ch = (char)code.get())
{
// We are not handling UTF and stuff like that. Code is supposed to plain simple text.
if (ch < 0)
continue;
// char/string..
if (ch == '\'' || ch == '\"')
{
std::string line;
// read char
bool special = false;
char c = ch;
do
{
// Append token..
line += c;
if (c == '\n')
++lineno;
// Special sequence '\.'
if (special)
special = false;
else
special = (c == '\\');
// Get next character
c = (char)code.get();
}
while (code.good() && (special || c != ch));
line += ch;
// Handle #file "file.h"
if (CurrentToken == "#file")
{
// Extract the filename
line = line.substr(1, line.length() - 2);
// Has this file been tokenized already?
++lineno;
bool foundOurfile = false;
fileIndexes.push_back(FileIndex);
for (unsigned int i = 0; i < _files.size(); i++)
{
if (FileLister::sameFileName(_files[i].c_str(), line.c_str()))
{
// Use this index
foundOurfile = true;
FileIndex = i;
}
}
if (!foundOurfile)
{
// The "_files" vector remembers what files have been tokenized..
_files.push_back(FileLister::simplifyPath(line.c_str()));
FileIndex = static_cast<unsigned int>(_files.size() - 1);
}
lineNumbers.push_back(lineno);
lineno = 0;
}
else
{
// Add previous token
addtoken(CurrentToken.c_str(), lineno, FileIndex);
// Add content of the string
addtoken(line.c_str(), lineno, FileIndex);
}
CurrentToken.clear();
continue;
}
if (strchr("+-*/%&|^?!=<>[](){};:,.~\n ", ch))
{
if (ch == '.' &&
CurrentToken.length() > 0 &&
std::isdigit(CurrentToken[0]))
{
// Don't separate doubles "5.4"
}
else if (strchr("+-", ch) &&
CurrentToken.length() > 0 &&
std::isdigit(CurrentToken[0]) &&
CurrentToken[CurrentToken.length()-1] == 'e')
{
// Don't separate doubles "4.2e+10"
}
else
{
if (CurrentToken == "#file")
{
// Handle this where strings are handled
continue;
}
else if (CurrentToken == "#endfile")
{
if (lineNumbers.empty() || fileIndexes.empty())
{
std::cerr << "####### Preprocessor bug! #######\n";
std::exit(0);
}
lineno = lineNumbers.back();
lineNumbers.pop_back();
FileIndex = fileIndexes.back();
fileIndexes.pop_back();
CurrentToken.clear();
continue;
}
// If token contains # characters, split it up
std::string temp;
for (std::string::size_type i = 0; i < CurrentToken.length(); ++i)
{
if (CurrentToken[i] == '#' && CurrentToken.length() + 1 > i && CurrentToken[i+1] == '#')
{
addtoken(temp.c_str(), lineno, FileIndex);
temp.clear();
addtoken("##", lineno, FileIndex);
++i;
}
else
temp += CurrentToken[i];
}
addtoken(temp.c_str(), lineno, FileIndex);
CurrentToken.clear();
if (ch == '\n')
{
++lineno;
continue;
}
else if (ch == ' ')
{
continue;
}
CurrentToken += ch;
// Add "++", "--" or ">>" token
if ((ch == '+' || ch == '-' || ch == '>') && (code.peek() == ch))
CurrentToken += (char)code.get();
addtoken(CurrentToken.c_str(), lineno, FileIndex);
CurrentToken.clear();
continue;
}
}
CurrentToken += ch;
}
addtoken(CurrentToken.c_str(), lineno, FileIndex);
}
void Tokenizer::simplifyTypedef()
{
std::string className;
int classLevel = 0;
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::Match(tok, "class %any%"))
{
className = tok->next()->str();
classLevel = 0;
continue;
}
else if (tok->str() == "}")
{
--classLevel;
if (classLevel < 0)
className = "";
continue;
}
else if (tok->str() == "{")
{
++classLevel;
continue;
}
else if (tok->str() != "typedef")
continue;
const char *type1 = 0;
const char *type2 = 0;
const char *typeName = 0;
bool pointer = false;
if (Token::Match(tok->next(), "%type% %type% ;") ||
Token::Match(tok->next(), "%type% %type% *| %type% ;"))
{
if (tok->tokAt(3)->str() == ";")
{
type1 = tok->strAt(1);
type2 = 0;
typeName = tok->strAt(2);
tok = tok->tokAt(3);
}
else
{
pointer = (tok->tokAt(3)->str() == "*");
type1 = tok->strAt(1);
type2 = tok->strAt(2);
if (pointer)
{
typeName = tok->strAt(4);
tok = tok->tokAt(5);
}
else
{
typeName = tok->strAt(3);
tok = tok->tokAt(4);
}
}
const std::string pattern = className + " :: " + typeName;
int level = 0;
bool inScope = true;
bool exitThisScope = false;
int exitScope = 0;
bool simplifyType = false;
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
{
if (tok2->str() == "}")
{
--level;
if (level < 0)
inScope = false;
if (exitThisScope)
{
if (level < exitScope)
exitThisScope = false;
}
}
else if (tok2->str() == "{")
++level;
else if (Token::Match(tok2, pattern.c_str()))
{
tok2->deleteNext();
tok2->deleteNext();
simplifyType = true;
}
else if (inScope && !exitThisScope && tok2->str() == typeName)
{
if (Token::Match(tok2->tokAt(-2), "!!typedef") &&
Token::Match(tok2->tokAt(-3), "!!typedef"))
{
simplifyType = true;
}
else
{
// Typedef with the same name.
exitThisScope = true;
exitScope = level;
}
}
if (simplifyType)
{
tok2->str(type1);
if (type2)
{
tok2->insertToken(type2);
tok2 = tok2->next();
}
if (pointer)
{
tok2->insertToken("*");
tok2 = tok2->next();
}
simplifyType = false;
}
}
}
}
}
bool Tokenizer::tokenize(std::istream &code, const char FileName[])
{
// The "_files" vector remembers what files have been tokenized..
_files.push_back(FileLister::simplifyPath(FileName));
createTokens(code);
if (!createLinks())
{
// Source has syntax errors, can't proceed
return false;
}
simplifyDoWhileAddBraces();
simplifyIfAddBraces();
// Combine "- %num%" ..
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::Match(tok, "[(+-*/=,] - %num%") && tok->strAt(2)[0] != '-')
{
tok->next()->str(std::string("-") + tok->strAt(2));
tok->next()->deleteNext();
}
if (Token::Match(tok, "return - %num%") && tok->strAt(2)[0] != '-')
{
tok->next()->str(std::string("-") + tok->strAt(2));
tok->next()->deleteNext();
}
}
// Combine tokens..
for (Token *tok = _tokens; tok && tok->next(); tok = tok->next())
{
static const char * const combineWithNext[][3] =
{
{ "<", "<", "<<" },
{ "&", "&", "&&" },
{ "|", "|", "||" },
{ "+", "=", "+=" },
{ "-", "=", "-=" },
{ "*", "=", "*=" },
{ "/", "=", "/=" },
{ "&", "=", "&=" },
{ "|", "=", "|=" },
{ "=", "=", "==" },
{ "!", "=", "!=" },
{ "<", "=", "<=" },
{ ">", "=", ">=" },
{ ":", ":", "::" },
{ "-", ">", "." }, // Replace "->" with "."
{ "private", ":", "private:" },
{ "protected", ":", "protected:" },
{ "public", ":", "public:" },
{ "__published", ":", "__published:" }
};
for (unsigned ui = 0; ui < sizeof(combineWithNext) / sizeof(combineWithNext[0]); ui++)
{
if (tok->str() == combineWithNext[ui][0] && tok->next()->str() == combineWithNext[ui][1])
{
tok->str(combineWithNext[ui][2]);
tok->deleteNext();
}
}
}
// typedef..
simplifyTypedef();
// Remove __asm..
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::Match(tok->next(), "__asm|_asm|asm {") &&
tok->tokAt(2)->link() &&
tok->tokAt(2)->link()->next())
{
Token::eraseTokens(tok, tok->tokAt(2)->link()->next());
}
}
// Remove "volatile" and "mutable"
while (_tokens && (_tokens->str() == "volatile" || _tokens->str() == "mutable"))
{
_tokens->deleteThis();
}
for (Token *tok = _tokens; tok; tok = tok->next())
{
while (tok->next() && (tok->next()->str() == "volatile" || tok->next()->str() == "mutable"))
{
tok->deleteNext();
}
}
// replace "unsigned i" with "unsigned int i"
unsignedint();
// Use "<" comparison instead of ">"
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::Match(tok, "[;(] %any% >|>= %any% [);]"))
{
const std::string op1(tok->strAt(1));
tok->next()->str(tok->strAt(3));
tok->tokAt(3)->str(op1);
if (tok->tokAt(2)->str() == ">")
tok->tokAt(2)->str("<");
else
tok->tokAt(2)->str("<=");
}
}
/**
* @todo simplify "for"
* - move out start-statement "for (a;b;c);" => "{ a; for(;b;c); }"
* - try to change "for" loop to a "while" loop instead
*/
// Split up variable declarations.
simplifyVarDecl();
// Handle templates..
simplifyTemplates();
// Simplify the operator "?:"
simplifyConditionOperator();
// remove exception specifications..
removeExceptionSpecifications(_tokens);
setVarId();
return true;
}
//---------------------------------------------------------------------------
void Tokenizer::simplifyTemplates()
{
// Remove "typename" unless used in template arguments..
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (tok->str() == "typename")
tok->deleteThis();
if (Token::simpleMatch(tok, "template <"))
{
while (tok && tok->str() != ">")
tok = tok->next();
if (!tok)
break;
}
}
// Locate templates..
std::list<Token *> templates;
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::simpleMatch(tok, "template <"))
{
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
{
// Just a declaration => ignore this
if (tok2->str() == ";")
break;
// Implementation => add to "templates"
if (tok2->str() == "{")
{
templates.push_back(tok);
break;
}
}
}
}
if (templates.empty())
return;
// Locate possible instantiations of templates..
std::list<Token *> used;
for (Token *tok = _tokens; tok; tok = tok->next())
{
// template definition.. skip it
if (Token::simpleMatch(tok, "template <"))
{
unsigned int indentlevel = 0;
for (; tok; tok = tok->next())
{
if (tok->str() == "{")
{
++indentlevel;
}
else if (tok->str() == "}")
{
if (indentlevel <= 1)
break;
--indentlevel;
}
else if (indentlevel == 0 && tok->str() == ";")
{
break;
}
}
if (!tok)
break;
}
else if (Token::Match(tok->previous(), "[{};=] %var% <"))
{
used.push_back(tok);
}
}
if (used.empty())
return;
// Template arguments with default values
for (std::list<Token *>::iterator iter1 = templates.begin(); iter1 != templates.end(); ++iter1)
{
std::list<Token *> eq;
unsigned int templatepar = 1;
std::string classname;
for (Token *tok = *iter1; tok; tok = tok->next())
{
if (tok->str() == ">")
{
if (Token::Match(tok, "> class %var%"))
classname = tok->strAt(2);
break;
}
if (tok->str() == ",")
++templatepar;
else if (tok->str() == "=")
eq.push_back(tok);
}
if (eq.empty() || classname.empty())
continue;
for (std::list<Token *>::iterator iter2 = used.begin(); iter2 != used.end(); ++iter2)
{
Token *tok = *iter2;
if (!Token::Match(tok, (classname + " < %any%").c_str()))
continue;
// count the parameters..
unsigned int usedpar = 1;
for (tok = tok->tokAt(3); tok; tok = tok->tokAt(2))
{
if (tok->str() == ">")
break;
if (tok->str() == ",")
++usedpar;
else
break;
}
if (tok && tok->str() == ">")
{
tok = tok->previous();
std::list<Token *>::const_iterator it = eq.begin();
for (unsigned int i = (templatepar - eq.size()); it != eq.end() && i < usedpar; ++i)
++it;
while (it != eq.end())
{
tok->insertToken(",");
tok = tok->next();
tok->insertToken((*it)->strAt(1));
tok = tok->next();
++it;
}
}
}
for (std::list<Token *>::iterator it = eq.begin(); it != eq.end(); ++it)
{
(*it)->deleteThis();
(*it)->deleteThis();
}
}
// expand templates
for (std::list<Token *>::iterator iter1 = templates.begin(); iter1 != templates.end(); ++iter1)
{
Token *tok = *iter1;
std::vector<std::string> type;
for (tok = tok->tokAt(2); tok && tok->str() != ">"; tok = tok->next())
{
if (Token::Match(tok, "%var% ,|>"))
type.push_back(tok->str());
}
// bail out if the end of the file was reached
if (!tok)
break;
// if this is a template function, get the position of the function name
unsigned int pos = 0;
if (Token::Match(tok, "> %type% *| %var% ("))
pos = 2;
else if (Token::Match(tok, "> %type% %type% *| %var% ("))
pos = 3;
if (pos > 0 && tok->tokAt(pos)->str() == "*")
++pos;
// name of template function/class..
const std::string name(tok->strAt(pos > 0 ? pos : 2));
const bool isfunc(pos > 0);
// locate template usage..
std::string s(name + " <");
for (unsigned int i = 0; i < type.size(); ++i)
{
if (i > 0)
s += ",";
s += " %any% ";
}
const std::string pattern(s + "> ");
for (std::list<Token *>::iterator iter2 = used.begin(); iter2 != used.end(); ++iter2)
{
Token *tok2 = *iter2;
if (tok2->str() != name)
continue;
if (!Token::Match(tok2, (pattern + (isfunc ? "(" : "%var%")).c_str()))
continue;
// New type..
std::vector<std::string> types2;
s = "";
for (const Token *tok3 = tok2->tokAt(2); tok3->str() != ">"; tok3 = tok3->next())
{
if (tok3->str() != ",")
types2.push_back(tok3->str());
s += tok3->str();
}
const std::string type2(s);
// New classname/funcname..
const std::string name2(name + "<" + type2 + ">");
// Copy template..
int _indentlevel = 0;
int _parlevel = 0;
for (const Token *tok3 = _tokens; tok3; tok3 = tok3->next())
{
if (tok3->str() == "{")
++_indentlevel;
else if (tok3->str() == "}")
--_indentlevel;
else if (tok3->str() == "(")
++_parlevel;
else if (tok3->str() == ")")
--_parlevel;
// Start of template..
if (tok3 == tok)
{
tok3 = tok3->next();
}
// member function implemented outside class definition
else if (_indentlevel == 0 && _parlevel == 0 && Token::Match(tok3, (pattern + " :: %var% (").c_str()))
{
addtoken(name2.c_str(), tok3->linenr(), tok3->fileIndex());
while (tok3->str() != "::")
tok3 = tok3->next();
}
// not part of template.. go on to next token
else
continue;
int indentlevel = 0;
std::stack<Token *> braces; // holds "{" tokens
std::stack<Token *> brackets; // holds "(" tokens
for (; tok3; tok3 = tok3->next())
{
if (tok3->str() == "{")
++indentlevel;
else if (tok3->str() == "}")
{
if (indentlevel <= 1)
{
// there is a bug if indentlevel is 0
// the "}" token should only be added if indentlevel is 1 but I add it always intentionally
// if indentlevel ever becomes 0, cppcheck will write:
// ### Error: Invalid number of character {
addtoken("}", tok3->linenr(), tok3->fileIndex());
Token::createMutualLinks(braces.top(), _tokensBack);
braces.pop();
break;
}
--indentlevel;
}
{
// search for this token in the type vector
unsigned int itype = 0;
while (itype < type.size() && type[itype] != tok3->str())
++itype;
// replace type with given type..
if (itype < type.size())
addtoken(types2[itype].c_str(), tok3->linenr(), tok3->fileIndex());
// replace name..
else if (tok3->str() == name)
addtoken(name2.c_str(), tok3->linenr(), tok3->fileIndex());
// copy
else
{
addtoken(tok3->str().c_str(), tok3->linenr(), tok3->fileIndex());
// link() newly tokens manually
if (tok3->str() == "{")
{
braces.push(_tokensBack);
}
else if (tok3->str() == "}")
{
assert(braces.empty() == false);
Token::createMutualLinks(braces.top(), _tokensBack);
braces.pop();
}
else if (tok3->str() == "(")
{
brackets.push(_tokensBack);
}
else if (tok3->str() == ")")
{
assert(brackets.empty() == false);
Token::createMutualLinks(brackets.top(), _tokensBack);
brackets.pop();
}
}
}
}
assert(braces.empty());
assert(brackets.empty());
}
// Replace all these template usages..
s = name + " < " + type2 + " >";
for (std::string::size_type pos = s.find(","); pos != std::string::npos; pos = s.find(",", pos + 2))
{
s.insert(pos + 1, " ");
s.insert(pos, " ");
}
for (Token *tok4 = tok2; tok4; tok4 = tok4->next())
{
if (Token::simpleMatch(tok4, s.c_str()))
{
tok4->str(name2);
while (tok4->next()->str() != ">")
tok4->deleteNext();
tok4->deleteNext();
}
}
}
}
}
//---------------------------------------------------------------------------
void Tokenizer::updateClassList()
{
const char pattern_class[] = "class %var% [{:]";
_classInfoList.clear();
// Locate class
const Token *tok1 = tokens();
while ((tok1 = Token::findmatch(tok1, pattern_class)) != 0)
{
const char *className;
className = tok1->strAt(1);
tok1 = tok1->next();
ClassInfo::MemberType memberType = ClassInfo::PRIVATE;
int indentlevel = 0;
for (const Token *tok = tok1; tok; tok = tok->next())
{
// Indentation
if (tok->str() == "{")
{
++indentlevel;
continue;
}
else if (tok->str() == "}")
{
--indentlevel;
if (indentlevel <= 0)
break;
continue;
}
// Parse class contents (indentlevel == 1)..
if (indentlevel == 1)
{
if (tok->str() == "private:")
memberType = ClassInfo::PRIVATE;
else if (tok->str() == "protected:")
memberType = ClassInfo::PROTECTED;
else if (tok->str() == "public:")
memberType = ClassInfo::PUBLIC;