diff --git a/lib/checkio.cpp b/lib/checkio.cpp index a3805dc7ac30..c7aacea79c4e 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -1358,7 +1358,7 @@ CheckIO::ArgumentInfo::ArgumentInfo(const Token * arg, const Settings *settings, top = top->astParent(); const ValueType *valuetype = top->argumentType(); if (valuetype && valuetype->type >= ValueType::Type::BOOL) { - typeToken = tempToken = new Token(); + typeToken = tempToken = new Token(const_cast(top)); if (valuetype->pointer && valuetype->constness & 1) { tempToken->str("const"); tempToken->insertToken("a"); @@ -1440,9 +1440,7 @@ CheckIO::ArgumentInfo::ArgumentInfo(const Token * arg, const Settings *settings, if (function->retType->classScope->enumType) typeToken = function->retType->classScope->enumType; else { - tempToken = new Token(); - tempToken->fileIndex(tok1->fileIndex()); - tempToken->linenr(tok1->linenr()); + tempToken = new Token(const_cast(tok1)); tempToken->str("int"); typeToken = tempToken; } @@ -1461,9 +1459,7 @@ CheckIO::ArgumentInfo::ArgumentInfo(const Token * arg, const Settings *settings, if (function->retType->classScope->enumType) typeToken = function->retType->classScope->enumType; else { - tempToken = new Token(); - tempToken->fileIndex(tok1->fileIndex()); - tempToken->linenr(tok1->linenr()); + tempToken = new Token(const_cast(tok1)); tempToken->str("int"); typeToken = tempToken; } @@ -1487,9 +1483,7 @@ CheckIO::ArgumentInfo::ArgumentInfo(const Token * arg, const Settings *settings, // check for some common well known functions else if (isCPP && ((Token::Match(tok1->previous(), "%var% . size|empty|c_str ( ) [,)]") && isStdContainer(tok1->previous())) || (Token::Match(tok1->previous(), "] . size|empty|c_str ( ) [,)]") && isStdContainer(tok1->previous()->link()->previous())))) { - tempToken = new Token(); - tempToken->fileIndex(tok1->fileIndex()); - tempToken->linenr(tok1->linenr()); + tempToken = new Token(const_cast(tok1)); if (tok1->next()->str() == "size") { // size_t is platform dependent if (settings->platform.sizeof_size_t == 8) { @@ -1548,9 +1542,7 @@ CheckIO::ArgumentInfo::ArgumentInfo(const Token * arg, const Settings *settings, if (variableInfo->type() && variableInfo->type()->classScope && variableInfo->type()->classScope->enumType) typeToken = variableInfo->type()->classScope->enumType; else { - tempToken = new Token(); - tempToken->fileIndex(tok1->fileIndex()); - tempToken->linenr(tok1->linenr()); + tempToken = new Token(const_cast(tok1)); tempToken->str("int"); typeToken = tempToken; } @@ -1588,9 +1580,7 @@ bool CheckIO::ArgumentInfo::isStdVectorOrString() return true; } if (variableInfo->isStlType(stl_string)) { - tempToken = new Token(); - tempToken->fileIndex(variableInfo->typeStartToken()->fileIndex()); - tempToken->linenr(variableInfo->typeStartToken()->linenr()); + tempToken = new Token(const_cast(variableInfo->typeStartToken())); if (variableInfo->typeStartToken()->strAt(2) == "string") tempToken->str("char"); else @@ -1608,9 +1598,7 @@ bool CheckIO::ArgumentInfo::isStdVectorOrString() return true; } if (Token::Match(nameTok, "std :: string|wstring")) { - tempToken = new Token(); - tempToken->fileIndex(variableInfo->typeStartToken()->fileIndex()); - tempToken->linenr(variableInfo->typeStartToken()->linenr()); + tempToken = new Token(const_cast(variableInfo->typeStartToken())); if (nameTok->strAt(2) == "string") tempToken->str("char"); else diff --git a/lib/token.cpp b/lib/token.cpp index 7e35af1e2d0a..63de2ac15943 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -63,6 +63,13 @@ Token::Token(TokensFrontBack *tokensFrontBack) : mImpl = new TokenImpl(); } +Token::Token(Token* tok) + : Token(tok->mTokensFrontBack) +{ + fileIndex(tok->fileIndex()); + linenr(tok->linenr()); +} + Token::~Token() { delete mImpl; diff --git a/lib/token.h b/lib/token.h index 0578a903a937..e9e32f45baeb 100644 --- a/lib/token.h +++ b/lib/token.h @@ -168,7 +168,8 @@ class CPPCHECKLIB Token { eNone }; - explicit Token(TokensFrontBack *tokensFrontBack = nullptr); + explicit Token(TokensFrontBack *tokensFrontBack); + explicit Token(Token *tok); ~Token(); ConstTokenRange until(const Token * t) const; diff --git a/test/testtoken.cpp b/test/testtoken.cpp index cb3f19b05c94..d1791686af1f 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -114,7 +114,7 @@ class TestToken : public TestFixture { } void nextprevious() const { - auto *token = new Token(); + auto *token = new Token(static_cast(nullptr)); token->str("1"); token->insertToken("2"); token->next()->insertToken("3"); @@ -147,49 +147,49 @@ class TestToken : public TestFixture { void multiCompare() const { // Test for found - Token one; + Token one(static_cast(nullptr)); one.str("one"); ASSERT_EQUALS(1, Token::multiCompare(&one, "one|two", 0)); - Token two; + Token two(static_cast(nullptr)); 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; + Token notfound(static_cast(nullptr)); 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; + Token s(static_cast(nullptr)); s.str("s"); ASSERT_EQUALS(static_cast(-1), static_cast(Token::multiCompare(&s, "verybig|two", 0))); - Token ne; + Token ne(static_cast(nullptr)); ne.str("ne"); ASSERT_EQUALS(static_cast(-1), static_cast(Token::multiCompare(&ne, "one|two", 0))); - Token a; + Token a(static_cast(nullptr)); a.str("a"); ASSERT_EQUALS(static_cast(-1), static_cast(Token::multiCompare(&a, "abc|def", 0))); - Token abcd; + Token abcd(static_cast(nullptr)); abcd.str("abcd"); ASSERT_EQUALS(static_cast(-1), static_cast(Token::multiCompare(&abcd, "abc|def", 0))); - Token def; + Token def(static_cast(nullptr)); def.str("default"); ASSERT_EQUALS(static_cast(-1), static_cast(Token::multiCompare(&def, "abc|def", 0))); // %op% - Token plus; + Token plus(static_cast(nullptr)); plus.str("+"); ASSERT_EQUALS(1, Token::multiCompare(&plus, "one|%op%", 0)); ASSERT_EQUALS(1, Token::multiCompare(&plus, "%op%|two", 0)); - Token x; + Token x(static_cast(nullptr)); x.str("x"); ASSERT_EQUALS(-1, Token::multiCompare(&x, "one|%op%", 0)); ASSERT_EQUALS(-1, Token::multiCompare(&x, "%op%|two", 0)); @@ -265,13 +265,13 @@ class TestToken : public TestFixture { } void multiCompare5() const { - Token tok; + Token tok(static_cast(nullptr)); tok.str("||"); ASSERT_EQUALS(true, Token::multiCompare(&tok, "+|%or%|%oror%", 0) >= 0); } void charTypes() const { - Token tok; + Token tok(static_cast(nullptr)); tok.str("'a'"); ASSERT_EQUALS(true, tok.isCChar()); @@ -323,7 +323,7 @@ class TestToken : public TestFixture { } void stringTypes() const { - Token tok; + Token tok(static_cast(nullptr)); tok.str("\"a\""); ASSERT_EQUALS(true, tok.isCChar()); @@ -367,7 +367,7 @@ class TestToken : public TestFixture { } void getStrLength() const { - Token tok; + Token tok(static_cast(nullptr)); tok.str("\"\""); ASSERT_EQUALS(0, Token::getStrLength(&tok)); @@ -395,7 +395,7 @@ class TestToken : public TestFixture { } void getStrSize() const { - Token tok; + Token tok(static_cast(nullptr)); const Settings settings; tok.str("\"\""); @@ -412,7 +412,7 @@ class TestToken : public TestFixture { } void strValue() const { - Token tok; + Token tok(static_cast(nullptr)); tok.str("\"\""); ASSERT_EQUALS("", tok.strValue()); @@ -443,7 +443,7 @@ class TestToken : public TestFixture { } void concatStr() const { - Token tok; + Token tok(static_cast(nullptr)); tok.str("\"\""); tok.concatStr("\"\""); @@ -538,7 +538,7 @@ class TestToken : public TestFixture { ASSERT_EQUALS(true, Token::Match(singleChar.tokens(), "[a|bc]")); ASSERT_EQUALS(false, Token::Match(singleChar.tokens(), "[d|ef]")); - Token multiChar; + Token multiChar(static_cast(nullptr)); multiChar.str("[ab"); ASSERT_EQUALS(false, Token::Match(&multiChar, "[ab|def]")); } @@ -781,7 +781,7 @@ 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; + Token tok(static_cast(nullptr)); tok.str(*test_op); ASSERT_EQUALS(true, tok.isArithmeticalOp()); } @@ -796,7 +796,7 @@ 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; + Token tok(static_cast(nullptr)); tok.str(*other_op); ASSERT_EQUALS_MSG(false, tok.isArithmeticalOp(), "Failing arithmetical operator: " + *other_op); } @@ -812,7 +812,7 @@ 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; + Token tok(static_cast(nullptr)); tok.str(*test_op); ASSERT_EQUALS(true, tok.isOp()); } @@ -823,7 +823,7 @@ 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; + Token tok(static_cast(nullptr)); tok.str(*other_op); ASSERT_EQUALS_MSG(false, tok.isOp(), "Failing normal operator: " + *other_op); } @@ -838,7 +838,7 @@ 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; + Token tok(static_cast(nullptr)); tok.str(*test_op); ASSERT_EQUALS(true, tok.isConstOp()); } @@ -850,7 +850,7 @@ 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; + Token tok(static_cast(nullptr)); tok.str(*other_op); ASSERT_EQUALS_MSG(false, tok.isConstOp(), "Failing normal operator: " + *other_op); } @@ -866,7 +866,7 @@ 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; + Token tok(static_cast(nullptr)); tok.str(*test_op); ASSERT_EQUALS(true, tok.isExtendedOp()); } @@ -874,7 +874,7 @@ 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; + Token tok(static_cast(nullptr)); tok.str(*other_op); ASSERT_EQUALS_MSG(false, tok.isExtendedOp(), "Failing assignment operator: " + *other_op); } @@ -883,7 +883,7 @@ 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; + Token tok(static_cast(nullptr)); tok.str(*test_op); ASSERT_EQUALS(true, tok.isAssignmentOp()); } @@ -898,7 +898,7 @@ 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; + Token tok(static_cast(nullptr)); tok.str(*other_op); ASSERT_EQUALS_MSG(false, tok.isAssignmentOp(), "Failing assignment operator: " + *other_op); } @@ -907,26 +907,26 @@ 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; + Token tok(static_cast(nullptr)); tok.str(*test_op); ASSERT_EQUALS(Token::eExtendedOp, tok.tokType()); } for (test_op = logicalOps.cbegin(); test_op != logicalOps.cend(); ++test_op) { - Token tok; + Token tok(static_cast(nullptr)); tok.str(*test_op); ASSERT_EQUALS(Token::eLogicalOp, tok.tokType()); } for (test_op = bitOps.cbegin(); test_op != bitOps.cend(); ++test_op) { - Token tok; + Token tok(static_cast(nullptr)); tok.str(*test_op); ASSERT_EQUALS(Token::eBitOp, tok.tokType()); } for (test_op = comparisonOps.cbegin(); test_op != comparisonOps.cend(); ++test_op) { - Token tok; + Token tok(static_cast(nullptr)); tok.str(*test_op); ASSERT_EQUALS(Token::eComparisonOp, tok.tokType()); } - Token tok; + Token tok(static_cast(nullptr)); tok.str("++"); ASSERT_EQUALS(Token::eIncDecOp, tok.tokType()); tok.str("--"); @@ -934,7 +934,7 @@ class TestToken : public TestFixture { } void literals() const { - Token tok; + Token tok(static_cast(nullptr)); tok.str("\"foo\""); ASSERT(tok.tokType() == Token::eString); @@ -965,13 +965,13 @@ 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; + Token tok(static_cast(nullptr)); tok.str(*test_op); ASSERT_EQUALS_MSG(true, tok.isStandardType(), "Failing standard type: " + *test_op); } // Negative test - Token tok; + Token tok(static_cast(nullptr)); tok.str("string"); ASSERT_EQUALS(false, tok.isStandardType()); @@ -987,7 +987,7 @@ class TestToken : public TestFixture { } void updateProperties() const { - Token tok; + Token tok(static_cast(nullptr)); tok.str("foobar"); ASSERT_EQUALS(true, tok.isName()); @@ -1000,39 +1000,39 @@ class TestToken : public TestFixture { } void isNameGuarantees1() const { - Token tok; + Token tok(static_cast(nullptr)); tok.str("Name"); ASSERT_EQUALS(true, tok.isName()); } void isNameGuarantees2() const { - Token tok; + Token tok(static_cast(nullptr)); tok.str("_name"); ASSERT_EQUALS(true, tok.isName()); } void isNameGuarantees3() const { - Token tok; + Token tok(static_cast(nullptr)); tok.str("_123"); ASSERT_EQUALS(true, tok.isName()); } void isNameGuarantees4() const { - Token tok; + Token tok(static_cast(nullptr)); tok.str("123456"); ASSERT_EQUALS(false, tok.isName()); ASSERT_EQUALS(true, tok.isNumber()); } void isNameGuarantees5() const { - Token tok; + Token tok(static_cast(nullptr)); tok.str("a123456"); ASSERT_EQUALS(true, tok.isName()); ASSERT_EQUALS(false, tok.isNumber()); } void isNameGuarantees6() const { - Token tok; + Token tok(static_cast(nullptr)); tok.str("$f"); ASSERT_EQUALS(true, tok.isName()); } @@ -1123,7 +1123,7 @@ class TestToken : public TestFixture { v2.valueType = ValueFlow::Value::ValueType::BUFFER_SIZE; v2.setKnown(); - Token token; + Token token(static_cast(nullptr)); ASSERT_EQUALS(true, token.addValue(v1)); ASSERT_EQUALS(true, token.addValue(v2)); ASSERT_EQUALS(false, token.hasKnownIntValue());