Refactorized token.cpp:

- Removed redundant overload of Token::insertToken()
- Fixed leak in Token::deleteThis()
- Moved condition out of loop in Token::tokAt()
This commit is contained in:
PKEuS 2015-12-14 22:04:26 +01:00
parent 7d4c37430b
commit 18693a7fce
3 changed files with 15 additions and 52 deletions

View File

@ -220,6 +220,7 @@ void Token::deleteThis()
_variable = _next->_variable;
_type = _next->_type;
if (_next->_originalName) {
delete _originalName;
_originalName = _next->_originalName;
_next->_originalName = nullptr;
}
@ -241,6 +242,7 @@ void Token::deleteThis()
_variable = _previous->_variable;
_type = _previous->_type;
if (_previous->_originalName) {
delete _originalName;
_originalName = _previous->_originalName;
_previous->_originalName = nullptr;
}
@ -296,13 +298,13 @@ void Token::replace(Token *replaceThis, Token *start, Token *end)
const Token *Token::tokAt(int index) const
{
const Token *tok = this;
int num = std::abs(index);
while (num > 0 && tok) {
if (index > 0)
tok = tok->next();
else
tok = tok->previous();
--num;
while (index > 0 && tok) {
tok = tok->next();
--index;
}
while (index < 0 && tok) {
tok = tok->previous();
++index;
}
return tok;
}
@ -867,44 +869,6 @@ const Token *Token::findmatch(const Token *startTok, const char pattern[], const
return nullptr;
}
void Token::insertToken(const std::string &tokenStr, bool prepend)
{
//TODO: Find a solution for the first token on the list
if (prepend && !this->previous())
return;
Token *newToken;
if (_str.empty())
newToken = this;
else
newToken = new Token(tokensBack);
newToken->str(tokenStr);
newToken->_linenr = _linenr;
newToken->_fileIndex = _fileIndex;
newToken->_progressValue = _progressValue;
if (newToken != this) {
if (prepend) {
/*if (this->previous())*/ {
newToken->previous(this->previous());
newToken->previous()->next(newToken);
} /*else if (tokensFront?) {
*tokensFront? = newToken;
}*/
this->previous(newToken);
newToken->next(this);
} else {
if (this->next()) {
newToken->next(this->next());
newToken->next()->previous(newToken);
} else if (tokensBack) {
*tokensBack = newToken;
}
this->next(newToken);
newToken->previous(this);
}
}
}
void Token::insertToken(const std::string &tokenStr, const std::string &originalNameStr, bool prepend)
{
//TODO: Find a solution for the first token on the list

View File

@ -466,12 +466,11 @@ public:
* Insert new token after this token. This function will handle
* relations between next and previous token also.
* @param tokenStr String for the new token.
* @param originalNameStr String used for Token::originalName().
* @param prepend Insert the new token before this token when it's not
* the first one on the tokens list.
*/
void insertToken(const std::string &tokenStr, bool prepend=false);
void insertToken(const std::string &tokenStr, const std::string &originalNameStr, bool prepend=false);
void insertToken(const std::string &tokenStr, const std::string &originalNameStr=emptyString, bool prepend=false);
Token *previous() const {
return _previous;

View File

@ -5855,10 +5855,10 @@ void Tokenizer::simplifyStaticConst()
// Move the qualifier to the left-most position in the declaration
tok->deleteNext();
if (!leftTok) {
list.front()->insertToken(qualifiers[i], false);
list.front()->insertToken(qualifiers[i], emptyString, false);
list.front()->swapWithNext();
} else if (leftTok->next())
leftTok->next()->insertToken(qualifiers[i], true);
leftTok->next()->insertToken(qualifiers[i], emptyString, true);
else
leftTok->insertToken(qualifiers[i]);
}
@ -6796,7 +6796,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
tok3->deleteThis();
ret = true;
} else if (Token::Match(valueToken, "& %name% ;"))
tok3->insertToken("&", true);
tok3->insertToken("&", emptyString, true);
}
if (Token::simpleMatch(tok3, "= {")) {
@ -10256,7 +10256,7 @@ void Tokenizer::prepareTernaryOpForAST()
}
if (paranthesesNeeded && tok2 && tok2->str() == ":") {
tok->insertToken("(");
tok2->insertToken(")", true);
tok2->insertToken(")", emptyString, true);
Token::createMutualLinks(tok->next(), tok2->previous());
}
}