Skip to content

Commit f12c0c7

Browse files
IOBYTEDaniel Marjamäki
authored andcommitted
Tokenizer: add assert(_settings) to Tokenizer to insure the tokenizer always has settings. Ticket: danmar#2219
1 parent b414473 commit f12c0c7

25 files changed

Lines changed: 704 additions & 402 deletions

lib/preprocessor.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,7 +1115,8 @@ std::list<std::string> Preprocessor::getcfgs(const std::string &filedata, const
11151115

11161116
void Preprocessor::simplifyCondition(const std::map<std::string, std::string> &variables, std::string &condition, bool match)
11171117
{
1118-
Tokenizer tokenizer;
1118+
Settings settings;
1119+
Tokenizer tokenizer(&settings, NULL);
11191120
std::istringstream istr(("(" + condition + ")").c_str());
11201121
tokenizer.tokenize(istr, "", "", true);
11211122

@@ -1307,7 +1308,7 @@ std::string Preprocessor::getcode(const std::string &filedata, std::string cfg,
13071308

13081309
if (line.find("=") != std::string::npos)
13091310
{
1310-
Tokenizer tokenizer;
1311+
Tokenizer tokenizer(settings, NULL);
13111312
line.erase(0, sizeof("#pragma endasm"));
13121313
std::istringstream tempIstr(line.c_str());
13131314
tokenizer.tokenize(tempIstr, "");
@@ -1732,6 +1733,8 @@ static void getparams(const std::string &line,
17321733
class PreprocessorMacro
17331734
{
17341735
private:
1736+
Settings settings;
1737+
17351738
/** tokens of this macro */
17361739
Tokenizer tokenizer;
17371740

@@ -1824,6 +1827,8 @@ class PreprocessorMacro
18241827
PreprocessorMacro(const std::string &macro)
18251828
: _macro(macro), _prefix("__cppcheck__")
18261829
{
1830+
tokenizer.setSettings(&settings);
1831+
18271832
// Tokenize the macro to make it easier to handle
18281833
std::istringstream istr(macro.c_str());
18291834
tokenizer.createTokens(istr);

lib/tokenize.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ Tokenizer::Tokenizer()
5959
Tokenizer::Tokenizer(const Settings *settings, ErrorLogger *errorLogger)
6060
: _settings(settings), _errorLogger(errorLogger)
6161
{
62+
// make sure settings are specified
63+
assert(_settings);
64+
6265
_tokens = 0;
6366
_tokensBack = 0;
6467
_codeWithTemplates = false;
@@ -1812,6 +1815,9 @@ bool Tokenizer::tokenize(std::istream &code,
18121815
const std::string &configuration,
18131816
const bool preprocessorCondition)
18141817
{
1818+
// make sure settings specified
1819+
assert(_settings);
1820+
18151821
_configuration = configuration;
18161822

18171823
// The "_files" vector remembers what files have been tokenized..

lib/tokenize.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,11 @@ class Tokenizer
521521
return _codeWithTemplates;
522522
}
523523

524+
void setSettings(const Settings *settings)
525+
{
526+
_settings = settings;
527+
}
528+
524529
private:
525530
/** Disable copy constructor, no implementation */
526531
Tokenizer(const Tokenizer &);
@@ -531,7 +536,7 @@ class Tokenizer
531536
Token *_tokens, *_tokensBack;
532537
std::map<std::string, unsigned int> _typeSize;
533538
std::vector<std::string> _files;
534-
const Settings * const _settings;
539+
const Settings * _settings;
535540
ErrorLogger * const _errorLogger;
536541

537542
/** E.g. "A" for code where "#ifdef A" is true. This is used to

test/testautovariables.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,14 @@ class TestAutoVariables : public TestFixture
3737

3838
void check(const char code[])
3939
{
40-
// Tokenize..
40+
// Clear the error buffer..
41+
errout.str("");
42+
4143
Settings settings;
4244
settings.debugwarnings = true;
43-
Tokenizer tokenizer(&settings, 0);
45+
46+
// Tokenize..
47+
Tokenizer tokenizer(&settings, this);
4448
std::istringstream istr(code);
4549
tokenizer.tokenize(istr, "test.cpp");
4650
tokenizer.simplifyTokenList();
@@ -51,9 +55,6 @@ class TestAutoVariables : public TestFixture
5155
// Fill function list
5256
tokenizer.fillFunctionList();
5357

54-
// Clear the error buffer..
55-
errout.str("");
56-
5758
// Check auto variables
5859
CheckAutoVariables checkAutoVariables(&tokenizer, &settings, this);
5960
checkAutoVariables.autoVariables();

test/testbufferoverrun.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,15 @@ class TestBufferOverrun : public TestFixture
3838

3939
void check(const char code[], bool inconclusive = true)
4040
{
41+
// Clear the error buffer..
42+
errout.str("");
43+
44+
Settings settings;
45+
settings.inconclusive = inconclusive;
46+
settings._checkCodingStyle = true;
47+
4148
// Tokenize..
42-
Tokenizer tokenizer;
49+
Tokenizer tokenizer(&settings, this);
4350
std::istringstream istr(code);
4451
tokenizer.tokenize(istr, "test.cpp");
4552

@@ -49,13 +56,7 @@ class TestBufferOverrun : public TestFixture
4956
// Fill function list
5057
tokenizer.fillFunctionList();
5158

52-
// Clear the error buffer..
53-
errout.str("");
54-
5559
// Check for buffer overruns..
56-
Settings settings;
57-
settings.inconclusive = inconclusive;
58-
settings._checkCodingStyle = true;
5960
CheckBufferOverrun checkBufferOverrun(&tokenizer, &settings, this);
6061
checkBufferOverrun.bufferOverrun();
6162
checkBufferOverrun.negativeIndex();
@@ -302,14 +303,16 @@ class TestBufferOverrun : public TestFixture
302303

303304
void arrayInfo()
304305
{
306+
// Clear the error buffer..
307+
errout.str("");
308+
309+
Settings settings;
310+
305311
// Tokenize..
306-
Tokenizer tokenizer;
312+
Tokenizer tokenizer(&settings, this);
307313
std::istringstream istr("XY(1) const int a[2] = { 1, 2 };");
308314
tokenizer.tokenize(istr, "test.cpp");
309315

310-
// Clear the error buffer..
311-
errout.str("");
312-
313316
tokenizer.simplifySizeof();
314317

315318
CheckBufferOverrun::ArrayInfo ai;
@@ -2506,17 +2509,18 @@ class TestBufferOverrun : public TestFixture
25062509

25072510
void epcheck(const char code[])
25082511
{
2512+
// Clear the error buffer..
2513+
errout.str("");
2514+
2515+
Settings settings;
2516+
25092517
// Tokenize..
2510-
Tokenizer tokenizer;
2518+
Tokenizer tokenizer(&settings, this);
25112519
std::istringstream istr(code);
25122520
tokenizer.tokenize(istr, "test.cpp");
25132521
tokenizer.simplifyTokenList();
25142522

2515-
// Clear the error buffer..
2516-
errout.str("");
2517-
25182523
// Check for buffer overruns..
2519-
Settings settings;
25202524
CheckBufferOverrun checkBufferOverrun(&tokenizer, &settings, this);
25212525
checkBufferOverrun.executionPaths();
25222526
}

test/testcharvar.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,19 @@ class TestCharVar : public TestFixture
4545

4646
void check(const char code[])
4747
{
48+
// Clear the error buffer..
49+
errout.str("");
50+
51+
Settings settings;
52+
settings._checkCodingStyle = true;
53+
4854
// Tokenize..
49-
Tokenizer tokenizer;
55+
Tokenizer tokenizer(&settings, this);
5056
std::istringstream istr(code);
5157
tokenizer.tokenize(istr, "test.cpp");
5258
tokenizer.setVarId();
5359

54-
// Clear the error buffer..
55-
errout.str("");
56-
5760
// Check char variable usage..
58-
Settings settings;
59-
settings._checkCodingStyle = true;
6061
CheckOther checkOther(&tokenizer, &settings, this);
6162
checkOther.checkCharVariable();
6263
}

0 commit comments

Comments
 (0)