Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/templatesimplifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ bool TemplateSimplifier::TokenAndName::isAliasToken(const Token *tok) const

TemplateSimplifier::TemplateSimplifier(Tokenizer &tokenizer)
: mTokenizer(tokenizer), mTokenList(mTokenizer.list), mSettings(mTokenizer.getSettings()),
mErrorLogger(mTokenizer.mErrorLogger)
mErrorLogger(mTokenizer.getErrorLogger())
{}

void TemplateSimplifier::checkComplicatedSyntaxErrorsInTemplates()
Expand Down
30 changes: 23 additions & 7 deletions lib/tokenize.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ enum class Severity : std::uint8_t;
class CPPCHECKLIB Tokenizer {

friend class SymbolDatabase;
friend class TemplateSimplifier;

friend class TestSimplifyTemplate;
friend class TestSimplifyTypedef;
friend class TestTokenizer;

public:
Tokenizer(TokenList tokenList, ErrorLogger &errorLogger);
Expand Down Expand Up @@ -116,6 +111,7 @@ class CPPCHECKLIB Tokenizer {
void removeExtraTemplateKeywords();


protected:
/** Split up template right angle brackets.
* foo < bar < >> => foo < bar < > >
*/
Expand Down Expand Up @@ -193,6 +189,7 @@ class CPPCHECKLIB Tokenizer {
*/
void simplifyVariableMultipleAssign();

protected:
/**
* Simplify the 'C Alternative Tokens'
* Examples:
Expand All @@ -202,6 +199,7 @@ class CPPCHECKLIB Tokenizer {
*/
bool simplifyCAlternativeTokens();

private:
/** Add braces to an if-block, for-block, etc.
* @return true if no syntax errors
*/
Expand Down Expand Up @@ -231,7 +229,9 @@ class CPPCHECKLIB Tokenizer {
* typedef A mytype;
* A c;
*/
protected:
void simplifyTypedef();
private:
void simplifyTypedefCpp();
/**
* Move typedef token to the left og the expression
Expand All @@ -244,7 +244,9 @@ class CPPCHECKLIB Tokenizer {

/**
*/
public:
bool simplifyUsing();
private:
void simplifyUsingError(const Token* usingStart, const Token* usingEnd);

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

void fillTypeSizes();

protected:
void combineOperators();

void combineStringAndCharLiterals();

private:
void concatenateNegativeNumberAndAnyPositive();

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

void findComplicatedSyntaxErrorsInTemplates();

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

protected:
/**
* Setup links for tokens so that one can call Token::link().
*/
Expand All @@ -362,6 +368,7 @@ class CPPCHECKLIB Tokenizer {
*/
void createLinks2();

private:
/**
* Set isCast() for C++ casts
*/
Expand All @@ -375,6 +382,7 @@ class CPPCHECKLIB Tokenizer {
/** Syntax error. Unmatched character. */
NORETURN void unmatchedToken(const Token *tok) const;

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

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

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

private:

/** Report that there is an unhandled "class x y {" code */
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;

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

protected:
/**
* assert that tokens are ok - used during debugging for example
* to catch problems in simplifyTokenList1/2.
*/
void validate() const;

private:
/** Detect unknown macros and throw unknownMacro */
void reportUnknownMacros() const;

Expand Down Expand Up @@ -433,9 +441,11 @@ class CPPCHECKLIB Tokenizer {
*/
void simplifyCppcheckAttribute();

protected:
/** Simplify c++20 spaceship operator */
void simplifySpaceshipOperator();

private:
/**
* Remove keywords "volatile", "inline", "register", and "restrict"
*/
Expand Down Expand Up @@ -522,11 +532,13 @@ class CPPCHECKLIB Tokenizer {
*/
void simplifyCoroutines();

protected:
/**
* Prepare ternary operators with parentheses so that the AST can be created
* */
void prepareTernaryOpForAST();

private:
/**
* report error message
*/
Expand Down Expand Up @@ -561,8 +573,10 @@ class CPPCHECKLIB Tokenizer {

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

private:
Token *deleteInvalidTypedef(Token *typeDef);

public:
/**
* Get variable count.
* @return number of variables
Expand Down Expand Up @@ -644,8 +658,10 @@ class CPPCHECKLIB Tokenizer {
/** Symbol database that all checks etc can use */
SymbolDatabase* mSymbolDatabase{};

protected:
TemplateSimplifier * const mTemplateSimplifier;

private:
std::set<nonneg int> mTemplateVarIdUsage;

/** E.g. "A" for code where "#ifdef A" is true. This is used to
Expand Down
1 change: 1 addition & 0 deletions test/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ struct LibraryHelper
};

class SimpleTokenizer2 : public Tokenizer {
friend class TestSimplifyTypedef; // TODO: get rid of this
public:
template<size_t size>
SimpleTokenizer2(const Settings &settings, ErrorLogger &errorlogger, const char (&code)[size], const std::string& file0)
Expand Down
17 changes: 13 additions & 4 deletions test/testsimplifytemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,15 @@ class TestSimplifyTemplate : public TestFixture {
TEST_CASE(dumpTemplateArgFrom);
}

class TokenizerTest : public Tokenizer
{
friend class TestSimplifyTemplate;
public:
TokenizerTest(TokenList tokenList, ErrorLogger &errorLogger)
: Tokenizer(std::move(tokenList), errorLogger)
{}
};

struct CheckOptions
{
bool debugwarnings = false;
Expand Down Expand Up @@ -5461,7 +5470,7 @@ class TestSimplifyTemplate : public TestFixture {
tokenlist.appendFileIfNew("test.cpp");
if (!tokenlist.createTokensFromString(data))
return false;
Tokenizer tokenizer(std::move(tokenlist), *this);
TokenizerTest tokenizer(std::move(tokenlist), *this);
tokenizer.createLinks();
tokenizer.splitTemplateRightAngleBrackets(false);

Expand Down Expand Up @@ -5531,7 +5540,7 @@ class TestSimplifyTemplate : public TestFixture {
tokenlist.appendFileIfNew("test.cpp");
if (!tokenlist.createTokensFromString(data))
return false;
Tokenizer tokenizer(std::move(tokenlist), *this);
TokenizerTest tokenizer(std::move(tokenlist), *this);
tokenizer.createLinks();
tokenizer.splitTemplateRightAngleBrackets(false);

Expand Down Expand Up @@ -5602,7 +5611,7 @@ class TestSimplifyTemplate : public TestFixture {
TokenList tokenlist{settings, Standards::Language::CPP};
if (!TokenListHelper::createTokensFromString(tokenlist, data, "test.cpp"))
return false;
Tokenizer tokenizer(std::move(tokenlist), *this);
TokenizerTest tokenizer(std::move(tokenlist), *this);
tokenizer.createLinks();
tokenizer.splitTemplateRightAngleBrackets(false);

Expand Down Expand Up @@ -5633,7 +5642,7 @@ class TestSimplifyTemplate : public TestFixture {

if (!TokenListHelper::createTokensFromString(tokenlist, data, "test.cpp"))
return false;
Tokenizer tokenizer(std::move(tokenlist), *this);
TokenizerTest tokenizer(std::move(tokenlist), *this);
tokenizer.createLinks();
tokenizer.splitTemplateRightAngleBrackets(false);

Expand Down
21 changes: 15 additions & 6 deletions test/testsimplifytypedef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,15 @@ class TestSimplifyTypedef : public TestFixture {
TEST_CASE(typedefInfo3);
}

class TokenizerTest : public Tokenizer
{
friend class TestSimplifyTypedef;
public:
TokenizerTest(TokenList tokenList, ErrorLogger &errorLogger)
: Tokenizer(std::move(tokenList), errorLogger)
{}
};

struct TokOptions
{
bool simplify = true;
Expand All @@ -280,7 +289,7 @@ class TestSimplifyTypedef : public TestFixture {
TokenList tokenlist{settings1, Standards::Language::CPP};
if (!tokenlist.createTokensFromString(data))
return "";
Tokenizer tokenizer(std::move(tokenlist), *this);
TokenizerTest tokenizer(std::move(tokenlist), *this);
tokenizer.createLinks();
tokenizer.simplifyTypedef();

Expand Down Expand Up @@ -315,7 +324,7 @@ class TestSimplifyTypedef : public TestFixture {

if (!TokenListHelper::createTokensFromString(tokenlist, data, "file.c"))
return "";
Tokenizer tokenizer(std::move(tokenlist), *this);
TokenizerTest tokenizer(std::move(tokenlist), *this);
tokenizer.createLinks();
tokenizer.simplifyTypedef();
try {
Expand All @@ -331,7 +340,7 @@ class TestSimplifyTypedef : public TestFixture {
TokenList tokenlist{settings1, Standards::Language::C};
if (!TokenListHelper::createTokensFromString(tokenlist, code, "file.c"))
return {};
Tokenizer tokenizer(std::move(tokenlist), *this);
TokenizerTest tokenizer(std::move(tokenlist), *this);
tokenizer.createLinks();
tokenizer.simplifyTypedef();
try {
Expand Down Expand Up @@ -4457,7 +4466,7 @@ class TestSimplifyTypedef : public TestFixture {

TokenList tokenlist{settings1, Standards::Language::C};
ASSERT(TokenListHelper::createTokensFromString(tokenlist, code, "file.c"));
Tokenizer tokenizer(std::move(tokenlist), *this);
TokenizerTest tokenizer(std::move(tokenlist), *this);
tokenizer.createLinks();
tokenizer.simplifyTypedef();

Expand Down Expand Up @@ -4499,7 +4508,7 @@ class TestSimplifyTypedef : public TestFixture {

TokenList tokenlist{settings1, Standards::Language::C};
ASSERT(TokenListHelper::createTokensFromString(tokenlist, code, "file.c"));
Tokenizer tokenizer(std::move(tokenlist), *this);
TokenizerTest tokenizer(std::move(tokenlist), *this);
tokenizer.createLinks();
tokenizer.simplifyTypedef();

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

TokenList tokenlist{settings1, Standards::Language::C};
ASSERT(TokenListHelper::createTokensFromString(tokenlist, code, "file.c"));
Tokenizer tokenizer(std::move(tokenlist), *this);
TokenizerTest tokenizer(std::move(tokenlist), *this);
tokenizer.createLinks();
tokenizer.simplifyTypedef();

Expand Down
13 changes: 11 additions & 2 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,15 @@ class TestTokenizer : public TestFixture {
TEST_CASE(simplifyRedundantParentheses);
}

class TokenizerTest : public Tokenizer
{
friend class TestTokenizer;
public:
TokenizerTest(TokenList tokenList, ErrorLogger &errorLogger)
: Tokenizer(std::move(tokenList), errorLogger)
{}
};

struct TokenizeOptions
{
bool expand = true;
Expand Down Expand Up @@ -3782,7 +3791,7 @@ class TestTokenizer : public TestFixture {

void simplifyString() {
TokenList tokenlist{settings0, Standards::Language::CPP};
Tokenizer tokenizer(std::move(tokenlist), *this);
TokenizerTest tokenizer(std::move(tokenlist), *this);
ASSERT_EQUALS("\"abc\"", tokenizer.simplifyString("\"abc\""));
ASSERT_EQUALS("\"\n\"", tokenizer.simplifyString("\"\\xa\""));
ASSERT_EQUALS("\"3\"", tokenizer.simplifyString("\"\\x33\""));
Expand Down Expand Up @@ -6349,7 +6358,7 @@ class TestTokenizer : public TestFixture {
if (!tokenlist.createTokensFromString(data))
return "ERROR";

Tokenizer tokenizer(std::move(tokenlist), *this);
TokenizerTest tokenizer(std::move(tokenlist), *this);
tokenizer.combineStringAndCharLiterals();
tokenizer.combineOperators();
tokenizer.simplifySpaceshipOperator();
Expand Down