Skip to content

Commit 040730e

Browse files
authored
removed usage of default parameters from Token::insertToken*() (#7813)
this avoids the unnecessary creation of `std::string` objects
1 parent 8378b97 commit 040730e

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

lib/token.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,18 +1061,14 @@ void Token::function(const Function *f)
10611061
tokType(eName);
10621062
}
10631063

1064-
Token* Token::insertToken(const std::string& tokenStr, const std::string& originalNameStr, const std::string& macroNameStr, bool prepend)
1064+
Token* Token::insertToken(const std::string& tokenStr, bool prepend)
10651065
{
10661066
Token *newToken;
10671067
if (mStr.empty())
10681068
newToken = this;
10691069
else
10701070
newToken = new Token(mList, mTokensFrontBack);
10711071
newToken->str(tokenStr);
1072-
if (!originalNameStr.empty())
1073-
newToken->originalName(originalNameStr);
1074-
if (!macroNameStr.empty())
1075-
newToken->setMacroName(macroNameStr);
10761072

10771073
if (newToken != this) {
10781074
newToken->mImpl->mLineNumber = mImpl->mLineNumber;
@@ -1198,6 +1194,22 @@ Token* Token::insertToken(const std::string& tokenStr, const std::string& origin
11981194
return newToken;
11991195
}
12001196

1197+
Token* Token::insertToken(const std::string& tokenStr, const std::string& originalNameStr, bool prepend)
1198+
{
1199+
Token* const newToken = insertToken(tokenStr, prepend);
1200+
if (!originalNameStr.empty())
1201+
newToken->originalName(originalNameStr);
1202+
return newToken;
1203+
}
1204+
1205+
Token* Token::insertToken(const std::string& tokenStr, const std::string& originalNameStr, const std::string& macroNameStr, bool prepend)
1206+
{
1207+
Token* const newToken = insertToken(tokenStr, originalNameStr, prepend);
1208+
if (!macroNameStr.empty())
1209+
newToken->setMacroName(macroNameStr);
1210+
return newToken;
1211+
}
1212+
12011213
void Token::eraseTokens(Token *begin, const Token *end)
12021214
{
12031215
if (!begin || begin == end)

lib/token.h

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -959,19 +959,46 @@ class CPPCHECKLIB Token {
959959
*/
960960
static void eraseTokens(Token *begin, const Token *end);
961961

962+
/**
963+
* Insert new token after this token. This function will handle
964+
* relations between next and previous token also.
965+
* @param tokenStr String for the new token.
966+
*/
967+
RET_NONNULL Token* insertToken(const std::string& tokenStr)
968+
{
969+
return insertToken(tokenStr, false);
970+
}
962971
/**
963972
* Insert new token after this token. This function will handle
964973
* relations between next and previous token also.
965974
* @param tokenStr String for the new token.
966975
* @param originalNameStr String used for Token::originalName().
967-
* the first one on the tokens list.
968976
*/
969-
RET_NONNULL Token* insertToken(const std::string& tokenStr, const std::string& originalNameStr = emptyString, const std::string& macroNameStr = emptyString)
977+
RET_NONNULL Token* insertToken(const std::string& tokenStr, const std::string& originalNameStr)
978+
{
979+
return insertToken(tokenStr, originalNameStr, false);
980+
}
981+
/**
982+
* Insert new token after this token. This function will handle
983+
* relations between next and previous token also.
984+
* @param tokenStr String for the new token.
985+
* @param originalNameStr String used for Token::originalName().
986+
* @param macroNameStr String used for Token::getMacroName().
987+
*/
988+
RET_NONNULL Token* insertToken(const std::string& tokenStr, const std::string& originalNameStr, const std::string& macroNameStr)
970989
{
971990
return insertToken(tokenStr, originalNameStr, macroNameStr, false);
972991
}
973992

974-
RET_NONNULL Token* insertTokenBefore(const std::string& tokenStr, const std::string& originalNameStr = emptyString, const std::string& macroNameStr = emptyString)
993+
RET_NONNULL Token* insertTokenBefore(const std::string& tokenStr)
994+
{
995+
return insertToken(tokenStr, true);
996+
}
997+
RET_NONNULL Token* insertTokenBefore(const std::string& tokenStr, const std::string& originalNameStr)
998+
{
999+
return insertToken(tokenStr, originalNameStr, true);
1000+
}
1001+
RET_NONNULL Token* insertTokenBefore(const std::string& tokenStr, const std::string& originalNameStr, const std::string& macroNameStr)
9751002
{
9761003
return insertToken(tokenStr, originalNameStr, macroNameStr, true);
9771004
}
@@ -1404,6 +1431,8 @@ class CPPCHECKLIB Token {
14041431
*/
14051432
static const char *chrInFirstWord(const char *str, char c);
14061433

1434+
RET_NONNULL Token* insertToken(const std::string& tokenStr, bool prepend);
1435+
RET_NONNULL Token* insertToken(const std::string& tokenStr, const std::string& originalNameStr, bool prepend);
14071436
RET_NONNULL Token* insertToken(const std::string& tokenStr, const std::string& originalNameStr, const std::string& macroNameStr, bool prepend);
14081437

14091438
std::string mStr;

0 commit comments

Comments
 (0)