Skip to content

Commit

Permalink
made Token::mTokensFrontBack a reference
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Feb 8, 2024
1 parent e98b98d commit 183b04b
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 107 deletions.
35 changes: 14 additions & 21 deletions lib/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ namespace {

const std::list<ValueFlow::Value> TokenImpl::mEmptyValueList;

Token::Token()
: Token(static_cast<TokensFrontBack*>(nullptr))
{}

Token::Token(TokensFrontBack *tokensFrontBack) :
Token::Token(TokensFrontBack &tokensFrontBack) :
mTokensFrontBack(tokensFrontBack)
{
mImpl = new TokenImpl();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1160,17 +1156,17 @@ 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);
} else {
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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
}
7 changes: 2 additions & 5 deletions lib/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
12 changes: 6 additions & 6 deletions lib/tokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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());
Expand Down Expand Up @@ -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);
}
Expand Down
Loading

0 comments on commit 183b04b

Please sign in to comment.