Skip to content

Commit 9f32d89

Browse files
authored
Tokenizer: removed some friend class declarations / cleaned up member access (#7958)
1 parent 08cdf36 commit 9f32d89

File tree

6 files changed

+64
-20
lines changed

6 files changed

+64
-20
lines changed

lib/templatesimplifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ bool TemplateSimplifier::TokenAndName::isAliasToken(const Token *tok) const
275275

276276
TemplateSimplifier::TemplateSimplifier(Tokenizer &tokenizer)
277277
: mTokenizer(tokenizer), mTokenList(mTokenizer.list), mSettings(mTokenizer.getSettings()),
278-
mErrorLogger(mTokenizer.mErrorLogger)
278+
mErrorLogger(mTokenizer.getErrorLogger())
279279
{}
280280

281281
void TemplateSimplifier::checkComplicatedSyntaxErrorsInTemplates()

lib/tokenize.h

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ enum class Severity : std::uint8_t;
4848
class CPPCHECKLIB Tokenizer {
4949

5050
friend class SymbolDatabase;
51-
friend class TemplateSimplifier;
52-
53-
friend class TestSimplifyTemplate;
54-
friend class TestSimplifyTypedef;
55-
friend class TestTokenizer;
5651

5752
public:
5853
Tokenizer(TokenList tokenList, ErrorLogger &errorLogger);
@@ -116,6 +111,7 @@ class CPPCHECKLIB Tokenizer {
116111
void removeExtraTemplateKeywords();
117112

118113

114+
protected:
119115
/** Split up template right angle brackets.
120116
* foo < bar < >> => foo < bar < > >
121117
*/
@@ -193,6 +189,7 @@ class CPPCHECKLIB Tokenizer {
193189
*/
194190
void simplifyVariableMultipleAssign();
195191

192+
protected:
196193
/**
197194
* Simplify the 'C Alternative Tokens'
198195
* Examples:
@@ -202,6 +199,7 @@ class CPPCHECKLIB Tokenizer {
202199
*/
203200
bool simplifyCAlternativeTokens();
204201

202+
private:
205203
/** Add braces to an if-block, for-block, etc.
206204
* @return true if no syntax errors
207205
*/
@@ -231,7 +229,9 @@ class CPPCHECKLIB Tokenizer {
231229
* typedef A mytype;
232230
* A c;
233231
*/
232+
protected:
234233
void simplifyTypedef();
234+
private:
235235
void simplifyTypedefCpp();
236236
/**
237237
* Move typedef token to the left og the expression
@@ -244,7 +244,9 @@ class CPPCHECKLIB Tokenizer {
244244

245245
/**
246246
*/
247+
public:
247248
bool simplifyUsing();
249+
private:
248250
void simplifyUsingError(const Token* usingStart, const Token* usingEnd);
249251

250252
/** Simplify useless C++ empty namespaces, like: 'namespace %name% { }'*/
@@ -305,10 +307,12 @@ class CPPCHECKLIB Tokenizer {
305307

306308
void fillTypeSizes();
307309

310+
protected:
308311
void combineOperators();
309312

310313
void combineStringAndCharLiterals();
311314

315+
private:
312316
void concatenateNegativeNumberAndAnyPositive();
313317

314318
void simplifyExternC();
@@ -325,6 +329,7 @@ class CPPCHECKLIB Tokenizer {
325329

326330
void findComplicatedSyntaxErrorsInTemplates();
327331

332+
protected:
328333
/**
329334
* Modify strings in the token list by replacing hex and oct
330335
* values. E.g. "\x61" -> "a" and "\000" -> "\0"
@@ -352,6 +357,7 @@ class CPPCHECKLIB Tokenizer {
352357
*/
353358
NORETURN void cppcheckError(const Token *tok) const;
354359

360+
protected:
355361
/**
356362
* Setup links for tokens so that one can call Token::link().
357363
*/
@@ -362,6 +368,7 @@ class CPPCHECKLIB Tokenizer {
362368
*/
363369
void createLinks2();
364370

371+
private:
365372
/**
366373
* Set isCast() for C++ casts
367374
*/
@@ -375,6 +382,7 @@ class CPPCHECKLIB Tokenizer {
375382
/** Syntax error. Unmatched character. */
376383
NORETURN void unmatchedToken(const Token *tok) const;
377384

385+
private:
378386
/** Syntax error. C++ code in C file. */
379387
NORETURN void syntaxErrorC(const Token *tok, const std::string &what) const;
380388

@@ -383,8 +391,6 @@ class CPPCHECKLIB Tokenizer {
383391

384392
void unhandledCharLiteral(const Token *tok, const std::string& msg) const;
385393

386-
private:
387-
388394
/** Report that there is an unhandled "class x y {" code */
389395
void unhandled_macro_class_x_y(const Token *tok, const std::string& type, const std::string& x, const std::string& y, const std::string& bracket) const;
390396

@@ -395,12 +401,14 @@ class CPPCHECKLIB Tokenizer {
395401
*/
396402
void validateC() const;
397403

404+
protected:
398405
/**
399406
* assert that tokens are ok - used during debugging for example
400407
* to catch problems in simplifyTokenList1/2.
401408
*/
402409
void validate() const;
403410

411+
private:
404412
/** Detect unknown macros and throw unknownMacro */
405413
void reportUnknownMacros() const;
406414

@@ -433,9 +441,11 @@ class CPPCHECKLIB Tokenizer {
433441
*/
434442
void simplifyCppcheckAttribute();
435443

444+
protected:
436445
/** Simplify c++20 spaceship operator */
437446
void simplifySpaceshipOperator();
438447

448+
private:
439449
/**
440450
* Remove keywords "volatile", "inline", "register", and "restrict"
441451
*/
@@ -522,11 +532,13 @@ class CPPCHECKLIB Tokenizer {
522532
*/
523533
void simplifyCoroutines();
524534

535+
protected:
525536
/**
526537
* Prepare ternary operators with parentheses so that the AST can be created
527538
* */
528539
void prepareTernaryOpForAST();
529540

541+
private:
530542
/**
531543
* report error message
532544
*/
@@ -561,8 +573,10 @@ class CPPCHECKLIB Tokenizer {
561573

562574
void dump(std::ostream &out) const;
563575

576+
private:
564577
Token *deleteInvalidTypedef(Token *typeDef);
565578

579+
public:
566580
/**
567581
* Get variable count.
568582
* @return number of variables
@@ -644,8 +658,10 @@ class CPPCHECKLIB Tokenizer {
644658
/** Symbol database that all checks etc can use */
645659
SymbolDatabase* mSymbolDatabase{};
646660

661+
protected:
647662
TemplateSimplifier * const mTemplateSimplifier;
648663

664+
private:
649665
std::set<nonneg int> mTemplateVarIdUsage;
650666

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

test/helpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ struct LibraryHelper
231231
};
232232

233233
class SimpleTokenizer2 : public Tokenizer {
234+
friend class TestSimplifyTypedef; // TODO: get rid of this
234235
public:
235236
template<size_t size>
236237
SimpleTokenizer2(const Settings &settings, ErrorLogger &errorlogger, const char (&code)[size], const std::string& file0)

test/testsimplifytemplate.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,15 @@ class TestSimplifyTemplate : public TestFixture {
319319
TEST_CASE(dumpTemplateArgFrom);
320320
}
321321

322+
class TokenizerTest : public Tokenizer
323+
{
324+
friend class TestSimplifyTemplate;
325+
public:
326+
TokenizerTest(TokenList tokenList, ErrorLogger &errorLogger)
327+
: Tokenizer(std::move(tokenList), errorLogger)
328+
{}
329+
};
330+
322331
struct CheckOptions
323332
{
324333
bool debugwarnings = false;
@@ -5461,7 +5470,7 @@ class TestSimplifyTemplate : public TestFixture {
54615470
tokenlist.appendFileIfNew("test.cpp");
54625471
if (!tokenlist.createTokensFromString(data))
54635472
return false;
5464-
Tokenizer tokenizer(std::move(tokenlist), *this);
5473+
TokenizerTest tokenizer(std::move(tokenlist), *this);
54655474
tokenizer.createLinks();
54665475
tokenizer.splitTemplateRightAngleBrackets(false);
54675476

@@ -5531,7 +5540,7 @@ class TestSimplifyTemplate : public TestFixture {
55315540
tokenlist.appendFileIfNew("test.cpp");
55325541
if (!tokenlist.createTokensFromString(data))
55335542
return false;
5534-
Tokenizer tokenizer(std::move(tokenlist), *this);
5543+
TokenizerTest tokenizer(std::move(tokenlist), *this);
55355544
tokenizer.createLinks();
55365545
tokenizer.splitTemplateRightAngleBrackets(false);
55375546

@@ -5602,7 +5611,7 @@ class TestSimplifyTemplate : public TestFixture {
56025611
TokenList tokenlist{settings, Standards::Language::CPP};
56035612
if (!TokenListHelper::createTokensFromString(tokenlist, data, "test.cpp"))
56045613
return false;
5605-
Tokenizer tokenizer(std::move(tokenlist), *this);
5614+
TokenizerTest tokenizer(std::move(tokenlist), *this);
56065615
tokenizer.createLinks();
56075616
tokenizer.splitTemplateRightAngleBrackets(false);
56085617

@@ -5633,7 +5642,7 @@ class TestSimplifyTemplate : public TestFixture {
56335642

56345643
if (!TokenListHelper::createTokensFromString(tokenlist, data, "test.cpp"))
56355644
return false;
5636-
Tokenizer tokenizer(std::move(tokenlist), *this);
5645+
TokenizerTest tokenizer(std::move(tokenlist), *this);
56375646
tokenizer.createLinks();
56385647
tokenizer.splitTemplateRightAngleBrackets(false);
56395648

test/testsimplifytypedef.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,15 @@ class TestSimplifyTypedef : public TestFixture {
257257
TEST_CASE(typedefInfo3);
258258
}
259259

260+
class TokenizerTest : public Tokenizer
261+
{
262+
friend class TestSimplifyTypedef;
263+
public:
264+
TokenizerTest(TokenList tokenList, ErrorLogger &errorLogger)
265+
: Tokenizer(std::move(tokenList), errorLogger)
266+
{}
267+
};
268+
260269
struct TokOptions
261270
{
262271
bool simplify = true;
@@ -280,7 +289,7 @@ class TestSimplifyTypedef : public TestFixture {
280289
TokenList tokenlist{settings1, Standards::Language::CPP};
281290
if (!tokenlist.createTokensFromString(data))
282291
return "";
283-
Tokenizer tokenizer(std::move(tokenlist), *this);
292+
TokenizerTest tokenizer(std::move(tokenlist), *this);
284293
tokenizer.createLinks();
285294
tokenizer.simplifyTypedef();
286295

@@ -315,7 +324,7 @@ class TestSimplifyTypedef : public TestFixture {
315324

316325
if (!TokenListHelper::createTokensFromString(tokenlist, data, "file.c"))
317326
return "";
318-
Tokenizer tokenizer(std::move(tokenlist), *this);
327+
TokenizerTest tokenizer(std::move(tokenlist), *this);
319328
tokenizer.createLinks();
320329
tokenizer.simplifyTypedef();
321330
try {
@@ -331,7 +340,7 @@ class TestSimplifyTypedef : public TestFixture {
331340
TokenList tokenlist{settings1, Standards::Language::C};
332341
if (!TokenListHelper::createTokensFromString(tokenlist, code, "file.c"))
333342
return {};
334-
Tokenizer tokenizer(std::move(tokenlist), *this);
343+
TokenizerTest tokenizer(std::move(tokenlist), *this);
335344
tokenizer.createLinks();
336345
tokenizer.simplifyTypedef();
337346
try {
@@ -4457,7 +4466,7 @@ class TestSimplifyTypedef : public TestFixture {
44574466

44584467
TokenList tokenlist{settings1, Standards::Language::C};
44594468
ASSERT(TokenListHelper::createTokensFromString(tokenlist, code, "file.c"));
4460-
Tokenizer tokenizer(std::move(tokenlist), *this);
4469+
TokenizerTest tokenizer(std::move(tokenlist), *this);
44614470
tokenizer.createLinks();
44624471
tokenizer.simplifyTypedef();
44634472

@@ -4499,7 +4508,7 @@ class TestSimplifyTypedef : public TestFixture {
44994508

45004509
TokenList tokenlist{settings1, Standards::Language::C};
45014510
ASSERT(TokenListHelper::createTokensFromString(tokenlist, code, "file.c"));
4502-
Tokenizer tokenizer(std::move(tokenlist), *this);
4511+
TokenizerTest tokenizer(std::move(tokenlist), *this);
45034512
tokenizer.createLinks();
45044513
tokenizer.simplifyTypedef();
45054514

@@ -4517,7 +4526,7 @@ class TestSimplifyTypedef : public TestFixture {
45174526

45184527
TokenList tokenlist{settings1, Standards::Language::C};
45194528
ASSERT(TokenListHelper::createTokensFromString(tokenlist, code, "file.c"));
4520-
Tokenizer tokenizer(std::move(tokenlist), *this);
4529+
TokenizerTest tokenizer(std::move(tokenlist), *this);
45214530
tokenizer.createLinks();
45224531
tokenizer.simplifyTypedef();
45234532

test/testtokenize.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,15 @@ class TestTokenizer : public TestFixture {
508508
TEST_CASE(simplifyRedundantParentheses);
509509
}
510510

511+
class TokenizerTest : public Tokenizer
512+
{
513+
friend class TestTokenizer;
514+
public:
515+
TokenizerTest(TokenList tokenList, ErrorLogger &errorLogger)
516+
: Tokenizer(std::move(tokenList), errorLogger)
517+
{}
518+
};
519+
511520
struct TokenizeOptions
512521
{
513522
bool expand = true;
@@ -3782,7 +3791,7 @@ class TestTokenizer : public TestFixture {
37823791

37833792
void simplifyString() {
37843793
TokenList tokenlist{settings0, Standards::Language::CPP};
3785-
Tokenizer tokenizer(std::move(tokenlist), *this);
3794+
TokenizerTest tokenizer(std::move(tokenlist), *this);
37863795
ASSERT_EQUALS("\"abc\"", tokenizer.simplifyString("\"abc\""));
37873796
ASSERT_EQUALS("\"\n\"", tokenizer.simplifyString("\"\\xa\""));
37883797
ASSERT_EQUALS("\"3\"", tokenizer.simplifyString("\"\\x33\""));
@@ -6349,7 +6358,7 @@ class TestTokenizer : public TestFixture {
63496358
if (!tokenlist.createTokensFromString(data))
63506359
return "ERROR";
63516360

6352-
Tokenizer tokenizer(std::move(tokenlist), *this);
6361+
TokenizerTest tokenizer(std::move(tokenlist), *this);
63536362
tokenizer.combineStringAndCharLiterals();
63546363
tokenizer.combineOperators();
63556364
tokenizer.simplifySpaceshipOperator();

0 commit comments

Comments
 (0)