diff --git a/lib/token.cpp b/lib/token.cpp index 313fb56c661..82c0081dc6f 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -150,7 +150,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 && mTokensFrontBack->list->isKeyword(mStr)) + else if (mTokensFrontBack && mTokensFrontBack->list.isKeyword(mStr)) tokType(eKeyword); else if (baseKeywords.count(mStr) > 0) tokType(eKeyword); @@ -1820,7 +1820,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 +2749,8 @@ const Token* findLambdaEndScope(const Token* tok) { bool Token::isCpp() const { - if (mTokensFrontBack && mTokensFrontBack->list) { - return mTokensFrontBack->list->isCPP(); + if (mTokensFrontBack) { + return mTokensFrontBack->list.isCPP(); } return true; // assume C++ by default } diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 4e0914d6757..f2568cb8691 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -46,10 +46,10 @@ static constexpr int AST_MAX_DEPTH = 150; -TokenList::TokenList(const Settings* settings) : - mSettings(settings) +TokenList::TokenList(const Settings* settings) + : mTokensFrontBack(*this) + , mSettings(settings) { - mTokensFrontBack.list = this; if (mSettings && (mSettings->enforcedLang != Standards::Language::None)) { mLang = mSettings->enforcedLang; } diff --git a/lib/tokenlist.h b/lib/tokenlist.h index 3b71d07d156..b359137cf33 100644 --- a/lib/tokenlist.h +++ b/lib/tokenlist.h @@ -44,9 +44,10 @@ namespace simplecpp { * @brief This struct stores pointers to the front and back tokens of the list this token is in. */ struct TokensFrontBack { + explicit TokensFrontBack(const TokenList& list) : list(list) {} Token *front{}; Token* back{}; - const TokenList* list{}; + const TokenList& list; }; class CPPCHECKLIB TokenList { diff --git a/test/testtoken.cpp b/test/testtoken.cpp index cb3f19b05c9..19ba3df2340 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -482,7 +482,8 @@ class TestToken : public TestFixture { } void deleteLast() const { - TokensFrontBack listEnds; + TokenList list(nullptr); + TokensFrontBack listEnds(list); Token ** const tokensBack = &(listEnds.back); Token tok(&listEnds); tok.insertToken("aba"); @@ -492,7 +493,8 @@ class TestToken : public TestFixture { } void deleteFirst() const { - TokensFrontBack listEnds; + TokenList list(nullptr); + TokensFrontBack listEnds(list); Token ** const tokensFront = &(listEnds.front); Token tok(&listEnds);