diff --git a/lib/token.cpp b/lib/token.cpp index 82c0081dc6f..9116dd88191 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -57,11 +57,7 @@ namespace { const std::list TokenImpl::mEmptyValueList; -Token::Token() - : Token(static_cast(nullptr)) -{} - -Token::Token(TokensFrontBack *tokensFrontBack) : +Token::Token(TokensFrontBack &tokensFrontBack) : mTokensFrontBack(tokensFrontBack) { mImpl = new TokenImpl(); @@ -150,7 +146,7 @@ void Token::update_property_info() else if (std::isalpha((unsigned char)mStr[0]) || mStr[0] == '_' || mStr[0] == '$') { // Name if (mImpl->mVarId) tokType(eVariable); - else if (mTokensFrontBack && mTokensFrontBack->list.isKeyword(mStr)) + else if (mTokensFrontBack.list.isKeyword(mStr)) tokType(eKeyword); else if (baseKeywords.count(mStr) > 0) tokType(eKeyword); @@ -296,8 +292,8 @@ void Token::deleteNext(nonneg int count) if (mNext) mNext->previous(this); - else if (mTokensFrontBack) - mTokensFrontBack->back = this; + else + mTokensFrontBack.back = this; } void Token::deletePrevious(nonneg int count) @@ -316,8 +312,8 @@ void Token::deletePrevious(nonneg int count) if (mPrevious) mPrevious->next(this); - else if (mTokensFrontBack) - mTokensFrontBack->front = this; + else + mTokensFrontBack.front = this; } void Token::swapWithNext() @@ -400,10 +396,10 @@ void Token::replace(Token *replaceThis, Token *start, Token *end) start->previous(replaceThis->previous()); end->next(replaceThis->next()); - if (end->mTokensFrontBack && end->mTokensFrontBack->back == end) { + if (end->mTokensFrontBack.back == end) { while (end->next()) end = end->next(); - end->mTokensFrontBack->back = end; + end->mTokensFrontBack.back = end; } // Update mProgressValue, fileIndex and linenr @@ -1160,8 +1156,8 @@ Token* Token::insertToken(const std::string& tokenStr, const std::string& origin if (this->previous()) { newToken->previous(this->previous()); newToken->previous()->next(newToken); - } else if (mTokensFrontBack) { - mTokensFrontBack->front = newToken; + } else { + mTokensFrontBack.front = newToken; } this->previous(newToken); newToken->next(this); @@ -1169,8 +1165,8 @@ Token* Token::insertToken(const std::string& tokenStr, const std::string& origin if (this->next()) { newToken->next(this->next()); newToken->next()->previous(newToken); - } else if (mTokensFrontBack) { - mTokensFrontBack->back = newToken; + } else { + mTokensFrontBack.back = newToken; } this->next(newToken); newToken->previous(this); @@ -1820,7 +1816,7 @@ void Token::printValueFlow(bool xml, std::ostream &out) const else { if (fileIndex != tok->fileIndex()) { outs += "File "; - outs += tok->mTokensFrontBack->list.getFiles()[tok->fileIndex()]; + outs += tok->mTokensFrontBack.list.getFiles()[tok->fileIndex()]; outs += '\n'; line = 0; } @@ -2749,8 +2745,5 @@ const Token* findLambdaEndScope(const Token* tok) { bool Token::isCpp() const { - if (mTokensFrontBack) { - return mTokensFrontBack->list.isCPP(); - } - return true; // assume C++ by default + return mTokensFrontBack.list.isCPP(); } diff --git a/lib/token.h b/lib/token.h index a889416d1de..a4594ba0865 100644 --- a/lib/token.h +++ b/lib/token.h @@ -151,10 +151,7 @@ class CPPCHECKLIB Token { friend class TestToken; private: - // for usage in TestToken only - Token(); - - TokensFrontBack* mTokensFrontBack{}; + TokensFrontBack& mTokensFrontBack; public: Token(const Token &) = delete; @@ -171,7 +168,7 @@ class CPPCHECKLIB Token { eNone }; - explicit Token(TokensFrontBack *tokensFrontBack); + explicit Token(TokensFrontBack &tokensFrontBack); // for usage in CheckIO::ArgumentInfo only explicit Token(const Token *tok); ~Token(); diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index f2568cb8691..a0706491dac 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -147,7 +147,7 @@ void TokenList::addtoken(const std::string& str, const nonneg int lineno, const if (mTokensFrontBack.back) { mTokensFrontBack.back->insertToken(str); } else { - mTokensFrontBack.front = new Token(&mTokensFrontBack); + mTokensFrontBack.front = new Token(mTokensFrontBack); mTokensFrontBack.back = mTokensFrontBack.front; mTokensFrontBack.back->str(str); } @@ -165,7 +165,7 @@ void TokenList::addtoken(const std::string& str, const Token *locationTok) if (mTokensFrontBack.back) { mTokensFrontBack.back->insertToken(str); } else { - mTokensFrontBack.front = new Token(&mTokensFrontBack); + mTokensFrontBack.front = new Token(mTokensFrontBack); mTokensFrontBack.back = mTokensFrontBack.front; mTokensFrontBack.back->str(str); } @@ -183,7 +183,7 @@ void TokenList::addtoken(const Token * tok, const nonneg int lineno, const nonne if (mTokensFrontBack.back) { mTokensFrontBack.back->insertToken(tok->str(), tok->originalName()); } else { - mTokensFrontBack.front = new Token(&mTokensFrontBack); + mTokensFrontBack.front = new Token(mTokensFrontBack); mTokensFrontBack.back = mTokensFrontBack.front; mTokensFrontBack.back->str(tok->str()); if (!tok->originalName().empty()) @@ -204,7 +204,7 @@ void TokenList::addtoken(const Token *tok, const Token *locationTok) if (mTokensFrontBack.back) { mTokensFrontBack.back->insertToken(tok->str(), tok->originalName()); } else { - mTokensFrontBack.front = new Token(&mTokensFrontBack); + mTokensFrontBack.front = new Token(mTokensFrontBack); mTokensFrontBack.back = mTokensFrontBack.front; mTokensFrontBack.back->str(tok->str()); if (!tok->originalName().empty()) @@ -225,7 +225,7 @@ void TokenList::addtoken(const Token *tok) if (mTokensFrontBack.back) { mTokensFrontBack.back->insertToken(tok->str(), tok->originalName(), tok->getMacroName()); } else { - mTokensFrontBack.front = new Token(&mTokensFrontBack); + mTokensFrontBack.front = new Token(mTokensFrontBack); mTokensFrontBack.back = mTokensFrontBack.front; mTokensFrontBack.back->str(tok->str()); mTokensFrontBack.back->originalName(tok->originalName()); @@ -355,7 +355,7 @@ void TokenList::createTokens(simplecpp::TokenList&& tokenList) if (mTokensFrontBack.back) { mTokensFrontBack.back->insertToken(str); } else { - mTokensFrontBack.front = new Token(&mTokensFrontBack); + mTokensFrontBack.front = new Token(mTokensFrontBack); mTokensFrontBack.back = mTokensFrontBack.front; mTokensFrontBack.back->str(str); } diff --git a/test/testtoken.cpp b/test/testtoken.cpp index 19ba3df2340..8850d295e27 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -36,6 +36,8 @@ class TestToken : public TestFixture { TestToken() : TestFixture("TestToken") {} private: + const TokenList list{nullptr}; + std::vector arithmeticalOps; std::vector logicalOps; std::vector bitOps; @@ -114,7 +116,8 @@ class TestToken : public TestFixture { } void nextprevious() const { - auto *token = new Token(); + TokensFrontBack tokensFrontBack(list); + auto *token = new Token(tokensFrontBack); token->str("1"); token->insertToken("2"); token->next()->insertToken("3"); @@ -147,52 +150,83 @@ class TestToken : public TestFixture { void multiCompare() const { // Test for found - Token one; - one.str("one"); - ASSERT_EQUALS(1, Token::multiCompare(&one, "one|two", 0)); + { + TokensFrontBack tokensFrontBack(list); + Token one(tokensFrontBack); + one.str("one"); + ASSERT_EQUALS(1, Token::multiCompare(&one, "one|two", 0)); + } - Token two; - two.str("two"); - ASSERT_EQUALS(1, Token::multiCompare(&two, "one|two", 0)); - ASSERT_EQUALS(1, Token::multiCompare(&two, "verybig|two|", 0)); + { + TokensFrontBack tokensFrontBack(list); + Token two(tokensFrontBack); + two.str("two"); + ASSERT_EQUALS(1, Token::multiCompare(&two, "one|two", 0)); + ASSERT_EQUALS(1, Token::multiCompare(&two, "verybig|two|", 0)); + } // Test for empty string found - Token notfound; - notfound.str("notfound"); - ASSERT_EQUALS(0, Token::multiCompare(¬found, "one|two|", 0)); - - // Test for not found - ASSERT_EQUALS(static_cast(-1), static_cast(Token::multiCompare(¬found, "one|two", 0))); + { + TokensFrontBack tokensFrontBack(list); + Token notfound(tokensFrontBack); + notfound.str("notfound"); + ASSERT_EQUALS(0, Token::multiCompare(¬found, "one|two|", 0)); + + // Test for not found + ASSERT_EQUALS(static_cast(-1), static_cast(Token::multiCompare(¬found, "one|two", 0))); + } - Token s; - s.str("s"); - ASSERT_EQUALS(static_cast(-1), static_cast(Token::multiCompare(&s, "verybig|two", 0))); + { + TokensFrontBack tokensFrontBack(list); + Token s(tokensFrontBack); + s.str("s"); + ASSERT_EQUALS(static_cast(-1), static_cast(Token::multiCompare(&s, "verybig|two", 0))); + } - Token ne; - ne.str("ne"); - ASSERT_EQUALS(static_cast(-1), static_cast(Token::multiCompare(&ne, "one|two", 0))); + { + TokensFrontBack tokensFrontBack(list); + Token ne(tokensFrontBack); + ne.str("ne"); + ASSERT_EQUALS(static_cast(-1), static_cast(Token::multiCompare(&ne, "one|two", 0))); + } - Token a; - a.str("a"); - ASSERT_EQUALS(static_cast(-1), static_cast(Token::multiCompare(&a, "abc|def", 0))); + { + TokensFrontBack tokensFrontBack(list); + Token a(tokensFrontBack); + a.str("a"); + ASSERT_EQUALS(static_cast(-1), static_cast(Token::multiCompare(&a, "abc|def", 0))); + } - Token abcd; - abcd.str("abcd"); - ASSERT_EQUALS(static_cast(-1), static_cast(Token::multiCompare(&abcd, "abc|def", 0))); + { + TokensFrontBack tokensFrontBack(list); + Token abcd(tokensFrontBack); + abcd.str("abcd"); + ASSERT_EQUALS(static_cast(-1), static_cast(Token::multiCompare(&abcd, "abc|def", 0))); + } - Token def; - def.str("default"); - ASSERT_EQUALS(static_cast(-1), static_cast(Token::multiCompare(&def, "abc|def", 0))); + { + TokensFrontBack tokensFrontBack(list); + Token def(tokensFrontBack); + def.str("default"); + ASSERT_EQUALS(static_cast(-1), static_cast(Token::multiCompare(&def, "abc|def", 0))); + } // %op% - Token plus; - plus.str("+"); - ASSERT_EQUALS(1, Token::multiCompare(&plus, "one|%op%", 0)); - ASSERT_EQUALS(1, Token::multiCompare(&plus, "%op%|two", 0)); - Token x; - x.str("x"); - ASSERT_EQUALS(-1, Token::multiCompare(&x, "one|%op%", 0)); - ASSERT_EQUALS(-1, Token::multiCompare(&x, "%op%|two", 0)); + { + TokensFrontBack tokensFrontBack(list); + Token plus(tokensFrontBack); + plus.str("+"); + ASSERT_EQUALS(1, Token::multiCompare(&plus, "one|%op%", 0)); + ASSERT_EQUALS(1, Token::multiCompare(&plus, "%op%|two", 0)); + } + { + TokensFrontBack tokensFrontBack(list); + Token x(tokensFrontBack); + x.str("x"); + ASSERT_EQUALS(-1, Token::multiCompare(&x, "one|%op%", 0)); + ASSERT_EQUALS(-1, Token::multiCompare(&x, "%op%|two", 0)); + } + } void multiCompare2() const { // #3294 @@ -265,13 +299,15 @@ class TestToken : public TestFixture { } void multiCompare5() const { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str("||"); ASSERT_EQUALS(true, Token::multiCompare(&tok, "+|%or%|%oror%", 0) >= 0); } void charTypes() const { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str("'a'"); ASSERT_EQUALS(true, tok.isCChar()); @@ -323,7 +359,8 @@ class TestToken : public TestFixture { } void stringTypes() const { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str("\"a\""); ASSERT_EQUALS(true, tok.isCChar()); @@ -367,7 +404,8 @@ class TestToken : public TestFixture { } void getStrLength() const { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str("\"\""); ASSERT_EQUALS(0, Token::getStrLength(&tok)); @@ -395,7 +433,8 @@ class TestToken : public TestFixture { } void getStrSize() const { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); const Settings settings; tok.str("\"\""); @@ -412,7 +451,8 @@ class TestToken : public TestFixture { } void strValue() const { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str("\"\""); ASSERT_EQUALS("", tok.strValue()); @@ -443,7 +483,8 @@ class TestToken : public TestFixture { } void concatStr() const { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str("\"\""); tok.concatStr("\"\""); @@ -482,10 +523,9 @@ class TestToken : public TestFixture { } void deleteLast() const { - TokenList list(nullptr); TokensFrontBack listEnds(list); Token ** const tokensBack = &(listEnds.back); - Token tok(&listEnds); + Token tok(listEnds); tok.insertToken("aba"); ASSERT_EQUALS(true, *tokensBack == tok.next()); tok.deleteNext(); @@ -493,10 +533,9 @@ class TestToken : public TestFixture { } void deleteFirst() const { - TokenList list(nullptr); TokensFrontBack listEnds(list); Token ** const tokensFront = &(listEnds.front); - Token tok(&listEnds); + Token tok(listEnds); tok.insertToken("aba"); @@ -540,7 +579,8 @@ class TestToken : public TestFixture { ASSERT_EQUALS(true, Token::Match(singleChar.tokens(), "[a|bc]")); ASSERT_EQUALS(false, Token::Match(singleChar.tokens(), "[d|ef]")); - Token multiChar; + TokensFrontBack tokensFrontBack(list); + Token multiChar(tokensFrontBack); multiChar.str("[ab"); ASSERT_EQUALS(false, Token::Match(&multiChar, "[ab|def]")); } @@ -783,7 +823,8 @@ class TestToken : public TestFixture { void isArithmeticalOp() const { std::vector::const_iterator test_op, test_ops_end = arithmeticalOps.cend(); for (test_op = arithmeticalOps.cbegin(); test_op != test_ops_end; ++test_op) { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str(*test_op); ASSERT_EQUALS(true, tok.isArithmeticalOp()); } @@ -798,7 +839,8 @@ class TestToken : public TestFixture { std::vector::const_iterator other_op, other_ops_end = other_ops.cend(); for (other_op = other_ops.cbegin(); other_op != other_ops_end; ++other_op) { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str(*other_op); ASSERT_EQUALS_MSG(false, tok.isArithmeticalOp(), "Failing arithmetical operator: " + *other_op); } @@ -814,7 +856,8 @@ class TestToken : public TestFixture { std::vector::const_iterator test_op, test_ops_end = test_ops.cend(); for (test_op = test_ops.cbegin(); test_op != test_ops_end; ++test_op) { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str(*test_op); ASSERT_EQUALS(true, tok.isOp()); } @@ -825,7 +868,8 @@ class TestToken : public TestFixture { std::vector::const_iterator other_op, other_ops_end = other_ops.cend(); for (other_op = other_ops.cbegin(); other_op != other_ops_end; ++other_op) { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str(*other_op); ASSERT_EQUALS_MSG(false, tok.isOp(), "Failing normal operator: " + *other_op); } @@ -840,7 +884,8 @@ class TestToken : public TestFixture { std::vector::const_iterator test_op, test_ops_end = test_ops.cend(); for (test_op = test_ops.cbegin(); test_op != test_ops_end; ++test_op) { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str(*test_op); ASSERT_EQUALS(true, tok.isConstOp()); } @@ -852,7 +897,8 @@ class TestToken : public TestFixture { std::vector::const_iterator other_op, other_ops_end = other_ops.cend(); for (other_op = other_ops.cbegin(); other_op != other_ops_end; ++other_op) { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str(*other_op); ASSERT_EQUALS_MSG(false, tok.isConstOp(), "Failing normal operator: " + *other_op); } @@ -868,7 +914,8 @@ class TestToken : public TestFixture { std::vector::const_iterator test_op, test_ops_end = test_ops.cend(); for (test_op = test_ops.cbegin(); test_op != test_ops_end; ++test_op) { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str(*test_op); ASSERT_EQUALS(true, tok.isExtendedOp()); } @@ -876,7 +923,8 @@ class TestToken : public TestFixture { // Negative test against assignment operators std::vector::const_iterator other_op, other_ops_end = assignmentOps.cend(); for (other_op = assignmentOps.cbegin(); other_op != other_ops_end; ++other_op) { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str(*other_op); ASSERT_EQUALS_MSG(false, tok.isExtendedOp(), "Failing assignment operator: " + *other_op); } @@ -885,7 +933,8 @@ class TestToken : public TestFixture { void isAssignmentOp() const { std::vector::const_iterator test_op, test_ops_end = assignmentOps.cend(); for (test_op = assignmentOps.cbegin(); test_op != test_ops_end; ++test_op) { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str(*test_op); ASSERT_EQUALS(true, tok.isAssignmentOp()); } @@ -900,7 +949,8 @@ class TestToken : public TestFixture { std::vector::const_iterator other_op, other_ops_end = other_ops.cend(); for (other_op = other_ops.cbegin(); other_op != other_ops_end; ++other_op) { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str(*other_op); ASSERT_EQUALS_MSG(false, tok.isAssignmentOp(), "Failing assignment operator: " + *other_op); } @@ -909,26 +959,31 @@ class TestToken : public TestFixture { void operators() const { std::vector::const_iterator test_op; for (test_op = extendedOps.cbegin(); test_op != extendedOps.cend(); ++test_op) { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str(*test_op); ASSERT_EQUALS(Token::eExtendedOp, tok.tokType()); } for (test_op = logicalOps.cbegin(); test_op != logicalOps.cend(); ++test_op) { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str(*test_op); ASSERT_EQUALS(Token::eLogicalOp, tok.tokType()); } for (test_op = bitOps.cbegin(); test_op != bitOps.cend(); ++test_op) { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str(*test_op); ASSERT_EQUALS(Token::eBitOp, tok.tokType()); } for (test_op = comparisonOps.cbegin(); test_op != comparisonOps.cend(); ++test_op) { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str(*test_op); ASSERT_EQUALS(Token::eComparisonOp, tok.tokType()); } - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str("++"); ASSERT_EQUALS(Token::eIncDecOp, tok.tokType()); tok.str("--"); @@ -936,7 +991,8 @@ class TestToken : public TestFixture { } void literals() const { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str("\"foo\""); ASSERT(tok.tokType() == Token::eString); @@ -967,13 +1023,15 @@ class TestToken : public TestFixture { std::vector::const_iterator test_op, test_ops_end = standard_types.cend(); for (test_op = standard_types.cbegin(); test_op != test_ops_end; ++test_op) { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str(*test_op); ASSERT_EQUALS_MSG(true, tok.isStandardType(), "Failing standard type: " + *test_op); } // Negative test - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str("string"); ASSERT_EQUALS(false, tok.isStandardType()); @@ -989,7 +1047,8 @@ class TestToken : public TestFixture { } void updateProperties() const { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str("foobar"); ASSERT_EQUALS(true, tok.isName()); @@ -1002,39 +1061,45 @@ class TestToken : public TestFixture { } void isNameGuarantees1() const { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str("Name"); ASSERT_EQUALS(true, tok.isName()); } void isNameGuarantees2() const { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str("_name"); ASSERT_EQUALS(true, tok.isName()); } void isNameGuarantees3() const { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str("_123"); ASSERT_EQUALS(true, tok.isName()); } void isNameGuarantees4() const { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str("123456"); ASSERT_EQUALS(false, tok.isName()); ASSERT_EQUALS(true, tok.isNumber()); } void isNameGuarantees5() const { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str("a123456"); ASSERT_EQUALS(true, tok.isName()); ASSERT_EQUALS(false, tok.isNumber()); } void isNameGuarantees6() const { - Token tok; + TokensFrontBack tokensFrontBack(list); + Token tok(tokensFrontBack); tok.str("$f"); ASSERT_EQUALS(true, tok.isName()); } @@ -1125,7 +1190,8 @@ class TestToken : public TestFixture { v2.valueType = ValueFlow::Value::ValueType::BUFFER_SIZE; v2.setKnown(); - Token token; + TokensFrontBack tokensFrontBack(list); + Token token(tokensFrontBack); ASSERT_EQUALS(true, token.addValue(v1)); ASSERT_EQUALS(true, token.addValue(v2)); ASSERT_EQUALS(false, token.hasKnownIntValue());