Skip to content

Commit d46ea7b

Browse files
authored
avoid unnecessary copies with push_back() and push_front() (#4451)
1 parent 2e8d855 commit d46ea7b

19 files changed

Lines changed: 69 additions & 69 deletions

lib/astutils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,7 @@ static void followVariableExpressionError(const Token *tok1, const Token *tok2,
11351135
ErrorPathItem item = std::make_pair(tok2, "'" + tok1->str() + "' is assigned value '" + tok2->expressionString() + "' here.");
11361136
if (std::find(errors->begin(), errors->end(), item) != errors->end())
11371137
return;
1138-
errors->push_back(item);
1138+
errors->push_back(std::move(item));
11391139
}
11401140

11411141
std::vector<ReferenceToken> followAllReferences(const Token* tok,

lib/checkclass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3089,7 +3089,7 @@ Check::FileInfo *CheckClass::getFileInfo(const Tokenizer *tokenizer, const Setti
30893089
}
30903090
nameLoc.hash = std::hash<std::string> {}(def);
30913091

3092-
classDefinitions.push_back(nameLoc);
3092+
classDefinitions.push_back(std::move(nameLoc));
30933093
}
30943094

30953095
if (classDefinitions.empty())
@@ -3132,7 +3132,7 @@ Check::FileInfo * CheckClass::loadFileInfoFromXml(const tinyxml2::XMLElement *xm
31323132
nameLoc.lineNumber = std::atoi(line);
31333133
nameLoc.column = std::atoi(col);
31343134
nameLoc.hash = MathLib::toULongNumber(hash);
3135-
fileInfo->classDefinitions.push_back(nameLoc);
3135+
fileInfo->classDefinitions.push_back(std::move(nameLoc));
31363136
}
31373137
}
31383138
if (fileInfo->classDefinitions.empty()) {

lib/checkunusedfunctions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ void CheckUnusedFunctions::unusedFunctionError(ErrorLogger * const errorLogger,
346346
ErrorMessage::FileLocation fileLoc;
347347
fileLoc.setfile(filename);
348348
fileLoc.line = lineNumber;
349-
locationList.push_back(fileLoc);
349+
locationList.push_back(std::move(fileLoc));
350350
}
351351

352352
const ErrorMessage errmsg(locationList, emptyString, Severity::style, "$symbol:" + funcname + "\nThe function '$symbol' is never used.", "unusedFunction", CWE561, Certainty::normal);

lib/clangimport.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,9 +1607,9 @@ void clangimport::parseClangAstDump(Tokenizer *tokenizer, std::istream &f)
16071607
AstNodePtr newNode = std::make_shared<AstNode>(nodeType, ext, &data);
16081608
tree[level - 1]->children.push_back(newNode);
16091609
if (level >= tree.size())
1610-
tree.push_back(newNode);
1610+
tree.push_back(std::move(newNode));
16111611
else
1612-
tree[level] = newNode;
1612+
tree[level] = std::move(newNode);
16131613
}
16141614

16151615
if (!tree.empty())

lib/cppcheck.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ static bool reportClangErrors(std::istream &is, std::function<void(const ErrorMe
417417
Certainty::normal);
418418

419419
if (line.compare(pos3, 10, ": warning:") == 0) {
420-
warnings->push_back(errmsg);
420+
warnings->push_back(std::move(errmsg));
421421
continue;
422422
}
423423

@@ -909,13 +909,13 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
909909
std::list<ErrorMessage::FileLocation> locationList;
910910
if (e.token) {
911911
ErrorMessage::FileLocation loc(e.token, &tokenizer.list);
912-
locationList.push_back(loc);
912+
locationList.push_back(std::move(loc));
913913
} else {
914914
ErrorMessage::FileLocation loc2(filename, 0, 0);
915-
locationList.push_back(loc2);
915+
locationList.push_back(std::move(loc2));
916916
if (filename != tokenizer.list.getSourceFilePath()) {
917917
ErrorMessage::FileLocation loc(tokenizer.list.getSourceFilePath(), 0, 0);
918-
locationList.push_back(loc);
918+
locationList.push_back(std::move(loc));
919919
}
920920
}
921921
ErrorMessage errmsg(std::move(locationList),
@@ -1463,7 +1463,7 @@ void CppCheck::tooManyConfigsError(const std::string &file, const int numberOfCo
14631463
if (!file.empty()) {
14641464
ErrorMessage::FileLocation location;
14651465
location.setfile(file);
1466-
loclist.push_back(location);
1466+
loclist.push_back(std::move(location));
14671467
}
14681468

14691469
std::ostringstream msg;
@@ -1501,7 +1501,7 @@ void CppCheck::purgedConfigurationMessage(const std::string &file, const std::st
15011501
if (!file.empty()) {
15021502
ErrorMessage::FileLocation location;
15031503
location.setfile(file);
1504-
loclist.push_back(location);
1504+
loclist.push_back(std::move(location));
15051505
}
15061506

15071507
ErrorMessage errmsg(loclist,

lib/ctu.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,11 @@ void CTU::FileInfo::loadFromXml(const tinyxml2::XMLElement *xmlElement)
237237
if (std::strcmp(e->Name(), "function-call") == 0) {
238238
FunctionCall functionCall;
239239
if (functionCall.loadFromXml(e))
240-
functionCalls.push_back(functionCall);
240+
functionCalls.push_back(std::move(functionCall));
241241
} else if (std::strcmp(e->Name(), "nested-call") == 0) {
242242
NestedCall nestedCall;
243243
if (nestedCall.loadFromXml(e))
244-
nestedCalls.push_back(nestedCall);
244+
nestedCalls.push_back(std::move(nestedCall));
245245
}
246246
}
247247
}
@@ -273,7 +273,7 @@ std::list<CTU::FileInfo::UnsafeUsage> CTU::loadUnsafeUsageListFromXml(const tiny
273273
unsafeUsage.value = readAttrInt(e, ATTR_VALUE, &error);
274274

275275
if (!error)
276-
ret.push_back(unsafeUsage);
276+
ret.push_back(std::move(unsafeUsage));
277277
}
278278
return ret;
279279
}
@@ -353,9 +353,9 @@ CTU::FileInfo *CTU::getFileInfo(const Tokenizer *tokenizer)
353353
loc.line = i.first->linenr();
354354
loc.column = i.first->column();
355355
loc.setinfo(i.second);
356-
functionCall.callValuePath.push_back(loc);
356+
functionCall.callValuePath.push_back(std::move(loc));
357357
}
358-
fileInfo->functionCalls.push_back(functionCall);
358+
fileInfo->functionCalls.push_back(std::move(functionCall));
359359
}
360360
// array
361361
if (argtok->variable() && argtok->variable()->isArray() && argtok->variable()->dimensions().size() == 1) {
@@ -368,7 +368,7 @@ CTU::FileInfo *CTU::getFileInfo(const Tokenizer *tokenizer)
368368
functionCall.callArgumentExpression = argtok->expressionString();
369369
functionCall.callArgValue = argtok->variable()->dimension(0) * argtok->valueType()->typeSize(*tokenizer->getSettings());
370370
functionCall.warning = false;
371-
fileInfo->functionCalls.push_back(functionCall);
371+
fileInfo->functionCalls.push_back(std::move(functionCall));
372372
}
373373
// &var => buffer
374374
if (argtok->isUnaryOp("&") && argtok->astOperand1()->variable() && argtok->astOperand1()->valueType() && !argtok->astOperand1()->variable()->isArray()) {
@@ -381,7 +381,7 @@ CTU::FileInfo *CTU::getFileInfo(const Tokenizer *tokenizer)
381381
functionCall.callArgumentExpression = argtok->expressionString();
382382
functionCall.callArgValue = argtok->astOperand1()->valueType()->typeSize(*tokenizer->getSettings());
383383
functionCall.warning = false;
384-
fileInfo->functionCalls.push_back(functionCall);
384+
fileInfo->functionCalls.push_back(std::move(functionCall));
385385
}
386386
// pointer/reference to uninitialized data
387387
auto isAddressOfArg = [](const Token* argtok) -> const Token* {
@@ -414,7 +414,7 @@ CTU::FileInfo *CTU::getFileInfo(const Tokenizer *tokenizer)
414414
functionCall.callArgValue = 0;
415415
functionCall.callArgumentExpression = argtok->expressionString();
416416
functionCall.warning = false;
417-
fileInfo->functionCalls.push_back(functionCall);
417+
fileInfo->functionCalls.push_back(std::move(functionCall));
418418
continue;
419419
}
420420
}
@@ -428,7 +428,7 @@ CTU::FileInfo *CTU::getFileInfo(const Tokenizer *tokenizer)
428428
FileInfo::NestedCall nestedCall(tokenizer, scopeFunction, tok);
429429
nestedCall.myArgNr = argnr + 1;
430430
nestedCall.callArgNr = argnr2;
431-
fileInfo->nestedCalls.push_back(nestedCall);
431+
fileInfo->nestedCalls.push_back(std::move(nestedCall));
432432
}
433433
}
434434
}
@@ -580,12 +580,12 @@ std::list<ErrorMessage::FileLocation> CTU::FileInfo::getErrorPath(InvalidValueTy
580580

581581
ErrorMessage::FileLocation fileLoc(path[index]->location.fileName, path[index]->location.lineNumber, path[index]->location.column);
582582
fileLoc.setinfo("Calling function " + path[index]->callFunctionName + ", " + MathLib::toString(path[index]->callArgNr) + getOrdinalText(path[index]->callArgNr) + " argument is " + value1);
583-
locationList.push_back(fileLoc);
583+
locationList.push_back(std::move(fileLoc));
584584
}
585585

586586
ErrorMessage::FileLocation fileLoc2(unsafeUsage.location.fileName, unsafeUsage.location.lineNumber, unsafeUsage.location.column);
587587
fileLoc2.setinfo(replaceStr(info, "ARG", unsafeUsage.myArgumentName));
588-
locationList.push_back(fileLoc2);
588+
locationList.push_back(std::move(fileLoc2));
589589

590590
return locationList;
591591
}

lib/errorlogger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ bool ErrorMessage::deserialize(const std::string &data)
334334
if (substrings.size() == 5)
335335
loc.setinfo(substrings[4]);
336336

337-
callStack.push_back(loc);
337+
callStack.push_back(std::move(loc));
338338

339339
if (callStack.size() >= stackSize)
340340
break;

lib/importproject.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void ImportProject::FileSettings::setIncludePaths(const std::string &basepath, c
164164
if (s[0] == '/' || (s.size() > 1U && s.compare(1,2,":/") == 0)) {
165165
if (!endsWith(s,'/'))
166166
s += '/';
167-
includePaths.push_back(s);
167+
includePaths.push_back(std::move(s));
168168
continue;
169169
}
170170

@@ -312,7 +312,7 @@ void ImportProject::FileSettings::parseCommand(std::string command)
312312
if (i.size() > 1 && i[0] == '\"' && i.back() == '\"')
313313
i = unescape(i.substr(1, i.size() - 2));
314314
if (std::find(includePaths.begin(), includePaths.end(), i) == includePaths.end())
315-
includePaths.push_back(i);
315+
includePaths.push_back(std::move(i));
316316
} else if (F=='s' && fval.compare(0,2,"td") == 0) {
317317
++pos;
318318
const std::string stdval = readUntil(command, &pos, " ");
@@ -341,8 +341,8 @@ void ImportProject::FileSettings::parseCommand(std::string command)
341341
}
342342
} else if (F == 'i' && fval == "system") {
343343
++pos;
344-
const std::string isystem = readUntil(command, &pos, " ");
345-
systemIncludePaths.push_back(isystem);
344+
std::string isystem = readUntil(command, &pos, " ");
345+
systemIncludePaths.push_back(std::move(isystem));
346346
} else if (F=='m') {
347347
if (fval == "unicode") {
348348
defs += "UNICODE";
@@ -444,7 +444,7 @@ bool ImportProject::importCompileCommands(std::istream &istr)
444444
fs.parseCommand(command); // read settings; -D, -I, -U, -std, -m*, -f*
445445
std::map<std::string, std::string, cppcheck::stricmp> variables;
446446
fs.setIncludePaths(directory, fs.includePaths, variables);
447-
fileSettings.push_back(fs);
447+
fileSettings.push_back(std::move(fs));
448448
}
449449

450450
return true;
@@ -804,7 +804,7 @@ bool ImportProject::importVcxproj(const std::string &filename, std::map<std::str
804804
}
805805
fs.setDefines(fs.defines);
806806
fs.setIncludePaths(Path::getPathFromFilename(filename), toStringList(includePath + ';' + additionalIncludePaths), variables);
807-
fileSettings.push_back(fs);
807+
fileSettings.push_back(std::move(fs));
808808
}
809809
}
810810

@@ -1071,7 +1071,7 @@ bool ImportProject::importBcb6Prj(const std::string &projectFilename)
10711071
fs.setIncludePaths(projectDir, toStringList(includePath), variables);
10721072
fs.setDefines(cppMode ? cppDefines : defines);
10731073
fs.filename = Path::simplifyPath(Path::isAbsolute(c) ? c : projectDir + c);
1074-
fileSettings.push_back(fs);
1074+
fileSettings.push_back(std::move(fs));
10751075
}
10761076

10771077
return true;
@@ -1180,7 +1180,7 @@ bool ImportProject::importCppcheckGuiProject(std::istream &istr, Settings *setti
11801180
s.lineNumber = child->IntAttribute("lineNumber", Suppressions::Suppression::NO_LINE);
11811181
s.symbolName = read(child->Attribute("symbolName"), "");
11821182
std::istringstream(read(child->Attribute("hash"), "0")) >> s.hash;
1183-
suppressions.push_back(s);
1183+
suppressions.push_back(std::move(s));
11841184
}
11851185
} else if (strcmp(node->Name(), CppcheckXml::VSConfigurationElementName) == 0)
11861186
guiProject.checkVsConfigs = readXmlStringList(node, emptyString, CppcheckXml::VSConfigurationName, nullptr);

lib/infer.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,21 +321,21 @@ std::vector<ValueFlow::Value> infer(const ValuePtr<InferModel>& model,
321321
ValueFlow::Value value(diff.getScalar());
322322
addToErrorPath(value, refs);
323323
setValueKind(value, refs);
324-
result.push_back(value);
324+
result.push_back(std::move(value));
325325
} else {
326326
if (!diff.minvalue.empty()) {
327327
ValueFlow::Value value(diff.minvalue.front() - 1);
328328
value.setImpossible();
329329
value.bound = ValueFlow::Value::Bound::Upper;
330330
addToErrorPath(value, diff.minRef);
331-
result.push_back(value);
331+
result.push_back(std::move(value));
332332
}
333333
if (!diff.maxvalue.empty()) {
334334
ValueFlow::Value value(diff.maxvalue.front() + 1);
335335
value.setImpossible();
336336
value.bound = ValueFlow::Value::Bound::Lower;
337337
addToErrorPath(value, diff.maxRef);
338-
result.push_back(value);
338+
result.push_back(std::move(value));
339339
}
340340
}
341341
} else if ((op == "!=" || op == "==") && lhs.isScalarOrEmpty() && rhs.isScalarOrEmpty()) {
@@ -344,7 +344,7 @@ std::vector<ValueFlow::Value> infer(const ValuePtr<InferModel>& model,
344344
ValueFlow::Value value(calculate(op, lhs.getScalar(), rhs.getScalar()));
345345
addToErrorPath(value, refs);
346346
setValueKind(value, refs);
347-
result.push_back(value);
347+
result.push_back(std::move(value));
348348
} else {
349349
std::vector<const ValueFlow::Value*> refs;
350350
if (lhs.isScalar() && inferNotEqual(rhsValues, lhs.getScalar()))
@@ -355,7 +355,7 @@ std::vector<ValueFlow::Value> infer(const ValuePtr<InferModel>& model,
355355
ValueFlow::Value value(op == "!=");
356356
addToErrorPath(value, refs);
357357
setValueKind(value, refs);
358-
result.push_back(value);
358+
result.push_back(std::move(value));
359359
}
360360
}
361361
} else {
@@ -365,7 +365,7 @@ std::vector<ValueFlow::Value> infer(const ValuePtr<InferModel>& model,
365365
ValueFlow::Value value(r.front());
366366
addToErrorPath(value, refs);
367367
setValueKind(value, refs);
368-
result.push_back(value);
368+
result.push_back(std::move(value));
369369
}
370370
}
371371

lib/preprocessor.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std:
123123
return false;
124124

125125
if (!s.errorId.empty())
126-
inlineSuppressions.push_back(s);
126+
inlineSuppressions.push_back(std::move(s));
127127

128128
if (!errmsg.empty())
129129
bad->emplace_back(tok->location, errmsg);
@@ -231,7 +231,7 @@ void Preprocessor::setDirectives(const simplecpp::TokenList &tokens)
231231
else
232232
directive.str += tok2->str();
233233
}
234-
mDirectives.push_back(directive);
234+
mDirectives.push_back(std::move(directive));
235235
}
236236
}
237237
}
@@ -474,7 +474,7 @@ static void getConfigs(const simplecpp::TokenList &tokens, std::set<std::string>
474474
std::string config = readcondition(cmdtok, defined, undefined);
475475
if (isUndefined(config,undefined))
476476
config.clear();
477-
configs_if.push_back(config);
477+
configs_if.push_back(std::move(config));
478478
ret.insert(cfg(configs_if, userDefines));
479479
} else if (!configs_ifndef.empty()) {
480480
configs_if.push_back(configs_ifndef.back());
@@ -588,7 +588,7 @@ static void splitcfg(const std::string &cfg, std::list<std::string> &defines, co
588588
std::string def = (defineEndPos == std::string::npos) ? cfg.substr(defineStartPos) : cfg.substr(defineStartPos, defineEndPos - defineStartPos);
589589
if (!defaultValue.empty() && def.find('=') == std::string::npos)
590590
def += '=' + defaultValue;
591-
defines.push_back(def);
591+
defines.push_back(std::move(def));
592592
if (defineEndPos == std::string::npos)
593593
break;
594594
defineStartPos = defineEndPos + 1U;
@@ -615,7 +615,7 @@ static simplecpp::DUI createDUI(const Settings &mSettings, const std::string &cf
615615
} else {
616616
s[s.find(')')+1] = '=';
617617
}
618-
dui.defines.push_back(s);
618+
dui.defines.push_back(std::move(s));
619619
}
620620

621621
dui.undefined = mSettings.userUndefs; // -U
@@ -825,8 +825,8 @@ void Preprocessor::error(const std::string &filename, unsigned int linenr, const
825825
if (mSettings.relativePaths)
826826
file = Path::getRelativePath(file, mSettings.basePaths);
827827

828-
const ErrorMessage::FileLocation loc(file, linenr, 0);
829-
locationList.push_back(loc);
828+
ErrorMessage::FileLocation loc(file, linenr, 0);
829+
locationList.push_back(std::move(loc));
830830
}
831831
mErrorLogger->reportErr(ErrorMessage(locationList,
832832
mFile0,
@@ -865,7 +865,7 @@ void Preprocessor::missingInclude(const std::string &filename, unsigned int line
865865
ErrorMessage::FileLocation loc;
866866
loc.line = linenr;
867867
loc.setfile(Path::toNativeSeparators(filename));
868-
locationList.push_back(loc);
868+
locationList.push_back(std::move(loc));
869869
}
870870
ErrorMessage errmsg(locationList, mFile0, Severity::information,
871871
(headerType==SystemHeader) ?

0 commit comments

Comments
 (0)