From c5fd8ce31428a502c4fec337a8fc0e39dbc01c1c Mon Sep 17 00:00:00 2001 From: firewave Date: Wed, 27 Aug 2025 16:25:54 +0200 Subject: [PATCH] cleaned up some `Token::insertToken*()` calls --- lib/templatesimplifier.cpp | 14 +++++++------- lib/token.h | 8 ++++++-- lib/tokenize.cpp | 6 +++--- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 18cec14c171..492e5906cd8 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -1581,7 +1581,7 @@ void TemplateSimplifier::addNamespace(const TokenAndName &templateDeclaration, c } } else { if (insert) - mTokenList.back()->tokAt(offset)->insertToken(token, emptyString); + mTokenList.back()->tokAt(offset)->insertToken(token); else mTokenList.addtoken(token, tok->linenr(), tok->column(), tok->fileIndex()); } @@ -1592,10 +1592,10 @@ void TemplateSimplifier::addNamespace(const TokenAndName &templateDeclaration, c if (token != tokStart->str() || tok->strAt(-1) != "::") { if (insert) { if (!inTemplate) - mTokenList.back()->tokAt(offset)->insertToken(templateDeclaration.scope().substr(start), emptyString); + mTokenList.back()->tokAt(offset)->insertToken(templateDeclaration.scope().substr(start)); else mTokenList.back()->tokAt(offset)->str(mTokenList.back()->strAt(offset) + templateDeclaration.scope().substr(start)); - mTokenList.back()->tokAt(offset)->insertToken("::", emptyString); + mTokenList.back()->tokAt(offset)->insertToken("::"); } else { if (!inTemplate) mTokenList.addtoken(templateDeclaration.scope().substr(start), tok->linenr(), tok->column(), tok->fileIndex()); @@ -1762,7 +1762,7 @@ void TemplateSimplifier::expandTemplate( ++typeindentlevel; else if (typetok->str() == ")") --typeindentlevel; - dst->insertToken(typetok->str(), typetok->originalName(), typetok->getMacroName(), true); + dst->insertTokenBefore(typetok->str(), typetok->originalName(), typetok->getMacroName()); dst->previous()->linenr(start->linenr()); dst->previous()->column(start->column()); Token *previous = dst->previous(); @@ -1790,7 +1790,7 @@ void TemplateSimplifier::expandTemplate( } } if (pointerType && Token::simpleMatch(dst1, "const")) { - dst->insertToken("const", dst1->originalName(), dst1->getMacroName(), true); + dst->insertTokenBefore("const", dst1->originalName(), dst1->getMacroName()); dst->previous()->linenr(start->linenr()); dst->previous()->column(start->column()); dst1->deleteThis(); @@ -1842,7 +1842,7 @@ void TemplateSimplifier::expandTemplate( } // just copy the token if it wasn't instantiated if (start != closing) { - dst->insertToken(start->str(), start->originalName(), start->getMacroName(), true); + dst->insertTokenBefore(start->str(), start->originalName(), start->getMacroName()); dst->previous()->linenr(start->linenr()); dst->previous()->column(start->column()); dst->previous()->isSigned(start->isSigned()); @@ -1850,7 +1850,7 @@ void TemplateSimplifier::expandTemplate( dst->previous()->isLong(start->isLong()); } } else { - dst->insertToken(start->str(), start->originalName(), start->getMacroName(), true); + dst->insertTokenBefore(start->str(), start->originalName(), start->getMacroName()); dst->previous()->linenr(start->linenr()); dst->previous()->column(start->column()); dst->previous()->isSigned(start->isSigned()); diff --git a/lib/token.h b/lib/token.h index b23fb2785d4..9beec6de9ca 100644 --- a/lib/token.h +++ b/lib/token.h @@ -964,10 +964,12 @@ class CPPCHECKLIB Token { * 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. */ - RET_NONNULL Token* insertToken(const std::string& tokenStr, const std::string& originalNameStr = emptyString, const std::string& macroNameStr = emptyString, bool prepend = false); + RET_NONNULL Token* insertToken(const std::string& tokenStr, const std::string& originalNameStr = emptyString, const std::string& macroNameStr = emptyString) + { + return insertToken(tokenStr, originalNameStr, macroNameStr, false); + } RET_NONNULL Token* insertTokenBefore(const std::string& tokenStr, const std::string& originalNameStr = emptyString, const std::string& macroNameStr = emptyString) { @@ -1402,6 +1404,8 @@ class CPPCHECKLIB Token { */ static const char *chrInFirstWord(const char *str, char c); + RET_NONNULL Token* insertToken(const std::string& tokenStr, const std::string& originalNameStr, const std::string& macroNameStr, bool prepend); + std::string mStr; Token* mNext{}; diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index ea911b1acbe..e57462bcfd2 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2972,7 +2972,7 @@ bool Tokenizer::simplifyUsing() structEnd = structEnd->link(); // add ';' after end of struct - structEnd->insertToken(";", emptyString); + structEnd->insertToken(";"); // add name for anonymous struct if (!hasName) { @@ -2982,8 +2982,8 @@ bool Tokenizer::simplifyUsing() else newName = "Unnamed" + std::to_string(mUnnamedCount++); TokenList::copyTokens(structEnd->next(), tok, start); - structEnd->tokAt(5)->insertToken(newName, emptyString); - start->insertToken(newName, emptyString); + structEnd->tokAt(5)->insertToken(newName); + start->insertToken(newName); } else TokenList::copyTokens(structEnd->next(), tok, start->next());