Skip to content

Commit d214684

Browse files
committed
Refactorizations:
- Replace several push_back-calls by emplace_back - Replace some x = x.substr(0, y) calls by x.erase(y)
1 parent 8aa71d6 commit d214684

19 files changed

Lines changed: 122 additions & 124 deletions

cli/threadexecutor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ unsigned int ThreadExecutor::check()
294294
oss << "Internal error: Child process crashed with signal " << WTERMSIG(stat);
295295

296296
std::list<ErrorLogger::ErrorMessage::FileLocation> locations;
297-
locations.push_back(ErrorLogger::ErrorMessage::FileLocation(childname, 0));
297+
locations.emplace_back(childname, 0);
298298
const ErrorLogger::ErrorMessage errmsg(locations,
299299
emptyString,
300300
Severity::error,

lib/analyzerinfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static bool skipAnalysis(const std::string &analyzerInfoFile, unsigned long long
9090

9191
for (const tinyxml2::XMLElement *e = rootNode->FirstChildElement(); e; e = e->NextSiblingElement()) {
9292
if (std::strcmp(e->Name(), "error") == 0)
93-
errors->push_back(ErrorLogger::ErrorMessage(e));
93+
errors->emplace_back(e);
9494
}
9595

9696
return true;

lib/check.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,16 @@ class CPPCHECKLIB Check {
162162
ErrorPath getErrorPath(const Token *errtok, const ValueFlow::Value *value, const std::string &bug) const {
163163
ErrorPath errorPath;
164164
if (!value) {
165-
errorPath.push_back(ErrorPathItem(errtok,bug));
165+
errorPath.emplace_back(errtok,bug);
166166
} else if (_settings->verbose || _settings->xml || _settings->outputFormat == "daca2") {
167167
errorPath = value->errorPath;
168-
errorPath.push_back(ErrorPathItem(errtok,bug));
168+
errorPath.emplace_back(errtok,bug);
169169
} else {
170170
if (value->condition)
171-
errorPath.push_back(ErrorPathItem(value->condition, "condition '" + value->condition->expressionString() + "'"));
171+
errorPath.emplace_back(value->condition, "condition '" + value->condition->expressionString() + "'");
172172
//else if (!value->isKnown() || value->defaultArg)
173173
// errorPath = value->callstack;
174-
errorPath.push_back(ErrorPathItem(errtok,bug));
174+
errorPath.emplace_back(errtok,bug);
175175
}
176176
return errorPath;
177177
}

lib/checkbufferoverrun.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,14 @@ void CheckBufferOverrun::arrayIndexOutOfBoundsError(const Token *tok, const Arra
103103
std::string nr;
104104
if (index.size() > 1U)
105105
nr = "(" + MathLib::toString(i + 1) + getOrdinalText(i + 1) + " array index) ";
106-
errorPath.push_back(ErrorPathItem(it->first, nr + info));
106+
errorPath.emplace_back(it->first, nr + info);
107107
}
108108
}
109-
errorPath.push_back(ErrorPathItem(tok,"Array index out of bounds"));
109+
errorPath.emplace_back(tok,"Array index out of bounds");
110110
} else {
111-
errorPath.push_back(ErrorPathItem(tok, "Array index out of bounds"));
111+
errorPath.emplace_back(tok, "Array index out of bounds");
112112
if (condition)
113-
errorPath.push_back(ErrorPathItem(condition, "Assuming that condition '" + condition->expressionString() + "' is not redundant"));
113+
errorPath.emplace_back(condition, "Assuming that condition '" + condition->expressionString() + "' is not redundant");
114114
}
115115

116116
if (condition != nullptr) {

lib/checkclass.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,13 +2115,13 @@ void CheckClass::initializerListOrder()
21152115
if (Token::Match(tok, "%name% (|{")) {
21162116
const Variable *var = scope->getVariable(tok->str());
21172117
if (var)
2118-
vars.push_back(VarInfo(var, tok));
2118+
vars.emplace_back(var, tok);
21192119

21202120
if (Token::Match(tok->tokAt(2), "%name% =")) {
21212121
var = scope->getVariable(tok->strAt(2));
21222122

21232123
if (var)
2124-
vars.push_back(VarInfo(var, tok->tokAt(2)));
2124+
vars.emplace_back(var, tok->tokAt(2));
21252125
}
21262126
tok = tok->next()->link()->next();
21272127
} else
@@ -2303,7 +2303,7 @@ void CheckClass::virtualFunctionCallInConstructorError(
23032303
ErrorPath errorPath;
23042304
int lineNumber = 1;
23052305
for (std::list<const Token *>::const_iterator it = tokStack.begin(); it != tokStack.end(); ++it)
2306-
errorPath.push_back(ErrorPathItem(*it, "Calling " + (*it)->str()));
2306+
errorPath.emplace_back(*it, "Calling " + (*it)->str());
23072307
if (!errorPath.empty()) {
23082308
lineNumber = errorPath.front().first->linenr();
23092309
errorPath.back().second = funcname + " is a virtual method";
@@ -2337,7 +2337,7 @@ void CheckClass::pureVirtualFunctionCallInConstructorError(
23372337

23382338
ErrorPath errorPath;
23392339
for (std::list<const Token *>::const_iterator it = tokStack.begin(); it != tokStack.end(); ++it)
2340-
errorPath.push_back(ErrorPathItem(*it, "Calling " + (*it)->str()));
2340+
errorPath.emplace_back(*it, "Calling " + (*it)->str());
23412341
if (!errorPath.empty())
23422342
errorPath.back().second = purefuncname + " is a pure virtual method without body";
23432343

lib/checkother.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,9 @@ void CheckOther::invalidPointerCast()
354354
if (toType->isIntegral() && fromType->isIntegral())
355355
continue;
356356
std::string toStr = toType->isIntegral() ? "integer *" : toType->str();
357-
toStr = toStr.substr(0, toStr.size()-2);
357+
toStr.erase(toStr.size()-2);
358358
std::string fromStr = fromType->isIntegral() ? "integer *" : fromType->str();
359-
fromStr = fromStr.substr(0, fromStr.size() - 2);
359+
fromStr.erase(fromStr.size() - 2);
360360

361361
invalidPointerCastError(tok, fromStr, toStr, toType->type == ValueType::Type::CHAR);
362362
}
@@ -1829,7 +1829,7 @@ void CheckOther::checkInvalidFree()
18291829

18301830
// Keep track of which variables were assigned addresses to newly-allocated memory
18311831
if (Token::Match(tok, "%var% = malloc|g_malloc|new")) {
1832-
allocatedVariables.insert(std::make_pair(tok->varId(), false));
1832+
allocatedVariables.emplace(tok->varId(), false);
18331833
}
18341834

18351835
// If a previously-allocated pointer is incremented or decremented, any subsequent

lib/checkstl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,16 +1069,16 @@ void CheckStl::string_c_str()
10691069
for (std::list<Scope>::const_iterator scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) {
10701070
for (std::list<Function>::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
10711071
if (c_strFuncParam.erase(func->tokenDef->str()) != 0) { // Check if function with this name was already found
1072-
c_strFuncParam.insert(std::make_pair(func->tokenDef->str(), 0)); // Disable, because there are overloads. TODO: Handle overloads
1072+
c_strFuncParam.emplace(func->tokenDef->str(), 0); // Disable, because there are overloads. TODO: Handle overloads
10731073
continue;
10741074
}
10751075

10761076
unsigned int numpar = 0;
1077-
c_strFuncParam.insert(std::make_pair(func->tokenDef->str(), numpar)); // Insert function as dummy, to indicate that there is at least one function with that name
1077+
c_strFuncParam.emplace(func->tokenDef->str(), numpar); // Insert function as dummy, to indicate that there is at least one function with that name
10781078
for (std::list<Variable>::const_iterator var = func->argumentList.cbegin(); var != func->argumentList.cend(); ++var) {
10791079
numpar++;
10801080
if (var->isStlStringType() && (!var->isReference() || var->isConst()))
1081-
c_strFuncParam.insert(std::make_pair(func->tokenDef->str(), numpar));
1081+
c_strFuncParam.emplace(func->tokenDef->str(), numpar);
10821082
}
10831083
}
10841084
}

lib/checkunusedfunctions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi
6868
if (tokenizer.isCPP() && func->retDef->str() == "template")
6969
continue;
7070

71-
_functionDecl.push_back(FunctionDecl(func));
71+
_functionDecl.emplace_back(func);
7272

7373
FunctionUsage &usage = _functions[func->name()];
7474

lib/checkunusedvar.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ void Variables::addVar(const Variable *var,
247247
{
248248
if (var->declarationId() > 0) {
249249
_varAddedInScope.back().insert(var->declarationId());
250-
_varUsage.insert(std::make_pair(var->declarationId(), VariableUsage(var, type, false, write_, false)));
250+
_varUsage.emplace(var->declarationId(), VariableUsage(var, type, false, write_, false));
251251
}
252252
}
253253

@@ -382,8 +382,8 @@ Variables::VariableUsage *Variables::find(unsigned int varid)
382382

383383
void Variables::enterScope()
384384
{
385-
_varAddedInScope.push_back(std::set<unsigned int>());
386-
_varReadInScope.push_back(std::set<unsigned int>());
385+
_varAddedInScope.emplace_back();
386+
_varReadInScope.emplace_back();
387387
}
388388

389389
void Variables::leaveScope(bool insideLoop)

lib/errorlogger.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ ErrorLogger::ErrorMessage::ErrorMessage(const std::list<const Token*>& callstack
8989
if (!(*it))
9090
continue;
9191

92-
_callStack.push_back(ErrorLogger::ErrorMessage::FileLocation(*it, list));
92+
_callStack.emplace_back(*it, list);
9393
}
9494

9595
if (list && !list->getFiles().empty())
@@ -108,7 +108,7 @@ ErrorLogger::ErrorMessage::ErrorMessage(const std::list<const Token*>& callstack
108108
if (!(*it))
109109
continue;
110110

111-
_callStack.push_back(ErrorLogger::ErrorMessage::FileLocation(*it, list));
111+
_callStack.emplace_back(*it, list);
112112
}
113113

114114
if (list && !list->getFiles().empty())
@@ -127,7 +127,7 @@ ErrorLogger::ErrorMessage::ErrorMessage(const ErrorPath &errorPath, const TokenL
127127

128128
// --errorlist can provide null values here
129129
if (tok)
130-
_callStack.push_back(ErrorLogger::ErrorMessage::FileLocation(tok, info, tokenList));
130+
_callStack.emplace_back(tok, info, tokenList);
131131
}
132132

133133
if (tokenList && !tokenList->getFiles().empty())
@@ -170,7 +170,7 @@ ErrorLogger::ErrorMessage::ErrorMessage(const tinyxml2::XMLElement * const errms
170170
const char *file = strfile ? strfile : unknown;
171171
const char *info = strinfo ? strinfo : "";
172172
const int line = strline ? std::atoi(strline) : 0;
173-
_callStack.push_back(ErrorLogger::ErrorMessage::FileLocation(file, info, line));
173+
_callStack.emplace_back(file, info, line);
174174
}
175175
}
176176
}
@@ -537,7 +537,7 @@ void ErrorLogger::reportUnmatchedSuppressions(const std::list<Suppressions::Supp
537537

538538
std::list<ErrorLogger::ErrorMessage::FileLocation> callStack;
539539
if (!i->fileName.empty())
540-
callStack.push_back(ErrorLogger::ErrorMessage::FileLocation(i->fileName, i->lineNumber));
540+
callStack.emplace_back(i->fileName, i->lineNumber);
541541
reportErr(ErrorLogger::ErrorMessage(callStack, emptyString, Severity::information, "Unmatched suppression: " + i->errorId, "unmatchedSuppression", false));
542542
}
543543
}

0 commit comments

Comments
 (0)