Skip to content

Commit 18693a7

Browse files
committed
Refactorized token.cpp:
- Removed redundant overload of Token::insertToken() - Fixed leak in Token::deleteThis() - Moved condition out of loop in Token::tokAt()
1 parent 7d4c374 commit 18693a7

3 files changed

Lines changed: 15 additions & 52 deletions

File tree

lib/token.cpp

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ void Token::deleteThis()
220220
_variable = _next->_variable;
221221
_type = _next->_type;
222222
if (_next->_originalName) {
223+
delete _originalName;
223224
_originalName = _next->_originalName;
224225
_next->_originalName = nullptr;
225226
}
@@ -241,6 +242,7 @@ void Token::deleteThis()
241242
_variable = _previous->_variable;
242243
_type = _previous->_type;
243244
if (_previous->_originalName) {
245+
delete _originalName;
244246
_originalName = _previous->_originalName;
245247
_previous->_originalName = nullptr;
246248
}
@@ -296,13 +298,13 @@ void Token::replace(Token *replaceThis, Token *start, Token *end)
296298
const Token *Token::tokAt(int index) const
297299
{
298300
const Token *tok = this;
299-
int num = std::abs(index);
300-
while (num > 0 && tok) {
301-
if (index > 0)
302-
tok = tok->next();
303-
else
304-
tok = tok->previous();
305-
--num;
301+
while (index > 0 && tok) {
302+
tok = tok->next();
303+
--index;
304+
}
305+
while (index < 0 && tok) {
306+
tok = tok->previous();
307+
++index;
306308
}
307309
return tok;
308310
}
@@ -867,44 +869,6 @@ const Token *Token::findmatch(const Token *startTok, const char pattern[], const
867869
return nullptr;
868870
}
869871

870-
void Token::insertToken(const std::string &tokenStr, bool prepend)
871-
{
872-
//TODO: Find a solution for the first token on the list
873-
if (prepend && !this->previous())
874-
return;
875-
Token *newToken;
876-
if (_str.empty())
877-
newToken = this;
878-
else
879-
newToken = new Token(tokensBack);
880-
newToken->str(tokenStr);
881-
newToken->_linenr = _linenr;
882-
newToken->_fileIndex = _fileIndex;
883-
newToken->_progressValue = _progressValue;
884-
885-
if (newToken != this) {
886-
if (prepend) {
887-
/*if (this->previous())*/ {
888-
newToken->previous(this->previous());
889-
newToken->previous()->next(newToken);
890-
} /*else if (tokensFront?) {
891-
*tokensFront? = newToken;
892-
}*/
893-
this->previous(newToken);
894-
newToken->next(this);
895-
} else {
896-
if (this->next()) {
897-
newToken->next(this->next());
898-
newToken->next()->previous(newToken);
899-
} else if (tokensBack) {
900-
*tokensBack = newToken;
901-
}
902-
this->next(newToken);
903-
newToken->previous(this);
904-
}
905-
}
906-
}
907-
908872
void Token::insertToken(const std::string &tokenStr, const std::string &originalNameStr, bool prepend)
909873
{
910874
//TODO: Find a solution for the first token on the list

lib/token.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,12 +466,11 @@ class CPPCHECKLIB Token {
466466
* Insert new token after this token. This function will handle
467467
* relations between next and previous token also.
468468
* @param tokenStr String for the new token.
469+
* @param originalNameStr String used for Token::originalName().
469470
* @param prepend Insert the new token before this token when it's not
470471
* the first one on the tokens list.
471472
*/
472-
void insertToken(const std::string &tokenStr, bool prepend=false);
473-
474-
void insertToken(const std::string &tokenStr, const std::string &originalNameStr, bool prepend=false);
473+
void insertToken(const std::string &tokenStr, const std::string &originalNameStr=emptyString, bool prepend=false);
475474

476475
Token *previous() const {
477476
return _previous;

lib/tokenize.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5855,10 +5855,10 @@ void Tokenizer::simplifyStaticConst()
58555855
// Move the qualifier to the left-most position in the declaration
58565856
tok->deleteNext();
58575857
if (!leftTok) {
5858-
list.front()->insertToken(qualifiers[i], false);
5858+
list.front()->insertToken(qualifiers[i], emptyString, false);
58595859
list.front()->swapWithNext();
58605860
} else if (leftTok->next())
5861-
leftTok->next()->insertToken(qualifiers[i], true);
5861+
leftTok->next()->insertToken(qualifiers[i], emptyString, true);
58625862
else
58635863
leftTok->insertToken(qualifiers[i]);
58645864
}
@@ -6796,7 +6796,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
67966796
tok3->deleteThis();
67976797
ret = true;
67986798
} else if (Token::Match(valueToken, "& %name% ;"))
6799-
tok3->insertToken("&", true);
6799+
tok3->insertToken("&", emptyString, true);
68006800
}
68016801

68026802
if (Token::simpleMatch(tok3, "= {")) {
@@ -10256,7 +10256,7 @@ void Tokenizer::prepareTernaryOpForAST()
1025610256
}
1025710257
if (paranthesesNeeded && tok2 && tok2->str() == ":") {
1025810258
tok->insertToken("(");
10259-
tok2->insertToken(")", true);
10259+
tok2->insertToken(")", emptyString, true);
1026010260
Token::createMutualLinks(tok->next(), tok2->previous());
1026110261
}
1026210262
}

0 commit comments

Comments
 (0)