From 76d1b9f31a0455be5aed2928fd4a7e291c30e24e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Sat, 10 Sep 2022 11:25:15 +0200 Subject: [PATCH] avoid unnecessary copies with `emplace_back()` (#4450) * avoid unnecessary copies with `emplace_back()` * cmdlineparser.cpp: suppress `accessMoved` selfcheck false positives --- cli/cmdlineparser.cpp | 15 +++++++++------ cli/processexecutor.cpp | 2 +- cli/threadexecutor.cpp | 5 +++-- lib/check.cpp | 8 ++++---- lib/check.h | 2 +- lib/cppcheck.cpp | 8 ++++---- lib/library.cpp | 2 +- lib/preprocessor.cpp | 4 ++-- lib/symboldatabase.cpp | 2 +- lib/templatesimplifier.cpp | 6 +++--- lib/tokenize.cpp | 2 +- lib/valueflow.cpp | 6 +++--- 12 files changed, 33 insertions(+), 29 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index 3987ebca0..b16af080c 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -65,9 +65,11 @@ static void addFilesToList(const std::string& fileList, std::vector } if (files && *files) { std::string fileName; + // cppcheck-suppress accessMoved - FP while (std::getline(*files, fileName)) { // next line + // cppcheck-suppress accessMoved - FP if (!fileName.empty()) { - pathNames.emplace_back(fileName); + pathNames.emplace_back(std::move(fileName)); } } } @@ -78,6 +80,7 @@ static bool addIncludePathsToList(const std::string& fileList, std::listemplace_back(pathName); + pathNames->emplace_back(std::move(pathName)); } } return true; @@ -200,7 +203,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[]) if (!endsWith(path,'/')) path += '/'; - mSettings->includePaths.emplace_back(path); + mSettings->includePaths.emplace_back(std::move(path)); } // User undef @@ -423,7 +426,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[]) if (!endsWith(path, '/')) path += '/'; } - mIgnoredPaths.emplace_back(path); + mIgnoredPaths.emplace_back(std::move(path)); } } @@ -722,7 +725,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[]) else if (std::strncmp(argv[i], "--rule=", 7) == 0) { Settings::Rule rule; rule.pattern = 7 + argv[i]; - mSettings->rules.emplace_back(rule); + mSettings->rules.emplace_back(std::move(rule)); } // Rule file @@ -760,7 +763,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[]) } if (!rule.pattern.empty()) - mSettings->rules.emplace_back(rule); + mSettings->rules.emplace_back(std::move(rule)); } } else { printError("unable to load rule-file: " + std::string(12+argv[i])); diff --git a/cli/processexecutor.cpp b/cli/processexecutor.cpp index 2719513a1..ed11ee731 100644 --- a/cli/processexecutor.cpp +++ b/cli/processexecutor.cpp @@ -171,7 +171,7 @@ int ProcessExecutor::handleRead(int rpipe, unsigned int &result) // Alert only about unique errors std::string errmsg = msg.toString(mSettings.verbose); if (std::find(mErrorList.begin(), mErrorList.end(), errmsg) == mErrorList.end()) { - mErrorList.emplace_back(errmsg); + mErrorList.emplace_back(std::move(errmsg)); if (type == PipeWriter::REPORT_ERROR) mErrorLogger.reportErr(msg); else diff --git a/cli/threadexecutor.cpp b/cli/threadexecutor.cpp index ab5777450..aef500217 100644 --- a/cli/threadexecutor.cpp +++ b/cli/threadexecutor.cpp @@ -99,12 +99,13 @@ private: // Alert only about unique errors bool reportError = false; - const std::string errmsg = msg.toString(mThreadExecutor.mSettings.verbose); { + std::string errmsg = msg.toString(mThreadExecutor.mSettings.verbose); + std::lock_guard lg(mErrorSync); if (std::find(mThreadExecutor.mErrorList.begin(), mThreadExecutor.mErrorList.end(), errmsg) == mThreadExecutor.mErrorList.end()) { - mThreadExecutor.mErrorList.emplace_back(errmsg); + mThreadExecutor.mErrorList.emplace_back(std::move(errmsg)); reportError = true; } } diff --git a/lib/check.cpp b/lib/check.cpp index 7579152a1..af899c4f8 100644 --- a/lib/check.cpp +++ b/lib/check.cpp @@ -96,20 +96,20 @@ std::string Check::getMessageId(const ValueFlow::Value &value, const char id[]) return id; } -ErrorPath Check::getErrorPath(const Token* errtok, const ValueFlow::Value* value, const std::string& bug) const +ErrorPath Check::getErrorPath(const Token* errtok, const ValueFlow::Value* value, std::string bug) const { ErrorPath errorPath; if (!value) { - errorPath.emplace_back(errtok, bug); + errorPath.emplace_back(errtok, std::move(bug)); } else if (mSettings->verbose || mSettings->xml || !mSettings->templateLocation.empty()) { errorPath = value->errorPath; - errorPath.emplace_back(errtok, bug); + errorPath.emplace_back(errtok, std::move(bug)); } else { if (value->condition) errorPath.emplace_back(value->condition, "condition '" + value->condition->expressionString() + "'"); //else if (!value->isKnown() || value->defaultArg) // errorPath = value->callstack; - errorPath.emplace_back(errtok, bug); + errorPath.emplace_back(errtok, std::move(bug)); } return errorPath; } diff --git a/lib/check.h b/lib/check.h index f110d0086..43c91f1d2 100644 --- a/lib/check.h +++ b/lib/check.h @@ -154,7 +154,7 @@ protected: void reportError(const ErrorPath &errorPath, Severity::SeverityType severity, const char id[], const std::string &msg, const CWE &cwe, Certainty::CertaintyLevel certainty); - ErrorPath getErrorPath(const Token* errtok, const ValueFlow::Value* value, const std::string& bug) const; + ErrorPath getErrorPath(const Token* errtok, const ValueFlow::Value* value, std::string bug) const; /** * Use WRONG_DATA in checkers when you check for wrong data. That diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index e6696bd99..9aafd11e5 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -1388,18 +1388,18 @@ void CppCheck::executeAddons(const std::vector& files) ErrorMessage errmsg; if (obj.count("file") > 0) { - const std::string fileName = obj["file"].get(); + std::string fileName = obj["file"].get(); const int64_t lineNumber = obj["linenr"].get(); const int64_t column = obj["column"].get(); - errmsg.callStack.emplace_back(fileName, lineNumber, column); + errmsg.callStack.emplace_back(std::move(fileName), lineNumber, column); } else if (obj.count("loc") > 0) { for (const picojson::value &locvalue: obj["loc"].get()) { picojson::object loc = locvalue.get(); - const std::string fileName = loc["file"].get(); + std::string fileName = loc["file"].get(); const int64_t lineNumber = loc["linenr"].get(); const int64_t column = loc["column"].get(); const std::string info = loc["info"].get(); - errmsg.callStack.emplace_back(fileName, info, lineNumber, column); + errmsg.callStack.emplace_back(std::move(fileName), info, lineNumber, column); } } diff --git a/lib/library.cpp b/lib/library.cpp index 6e17d702a..16e0655b2 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -521,7 +521,7 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc) struct Container::RangeItemRecordTypeItem member; member.name = memberName ? memberName : ""; member.templateParameter = memberTemplateParameter ? std::atoi(memberTemplateParameter) : -1; - container.rangeItemRecordType.emplace_back(member); + container.rangeItemRecordType.emplace_back(std::move(member)); } } else unknown_elements.insert(containerNodeName); diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index d9d47546e..adc704fde 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -109,7 +109,7 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std: std::vector suppressions = Suppressions::parseMultiSuppressComment(comment, &errmsg); if (!errmsg.empty()) - bad->emplace_back(tok->location, errmsg); + bad->emplace_back(tok->location, std::move(errmsg)); for (const Suppressions::Suppression &s : suppressions) { if (!s.errorId.empty()) @@ -126,7 +126,7 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std: inlineSuppressions.push_back(std::move(s)); if (!errmsg.empty()) - bad->emplace_back(tok->location, errmsg); + bad->emplace_back(tok->location, std::move(errmsg)); } return true; diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 2ff258f8c..75029ccf6 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -7334,7 +7334,7 @@ void ValueType::setDebugPath(const Token* tok, SourceLocation ctx, SourceLocatio return; std::string s = Path::stripDirectoryPart(file) + ":" + MathLib::toString(ctx.line()) + ": " + ctx.function_name() + " => " + local.function_name(); - debugPath.emplace_back(tok, s); + debugPath.emplace_back(tok, std::move(s)); } ValueType::MatchResult ValueType::matchParameter(const ValueType *call, const ValueType *func) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 226cc1cd2..3b12a742b 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -704,7 +704,7 @@ void TemplateSimplifier::addInstantiation(Token *token, const std::string &scope instantiation); if (it == mTemplateInstantiations.end()) - mTemplateInstantiations.emplace_back(instantiation); + mTemplateInstantiations.emplace_back(std::move(instantiation)); } static void getFunctionArguments(const Token *nameToken, std::vector &args) @@ -2207,9 +2207,9 @@ void TemplateSimplifier::expandTemplate( } if (copy) - newInstantiations.emplace_back(mTokenList.back(), scope); + newInstantiations.emplace_back(mTokenList.back(), std::move(scope)); else if (!inTemplateDefinition) - newInstantiations.emplace_back(tok3, scope); + newInstantiations.emplace_back(tok3, std::move(scope)); } // link() newly tokens manually diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 8c3191a03..b64962752 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -9496,7 +9496,7 @@ void Tokenizer::printUnknownTypes() const } } - unknowns.emplace_back(name, nameTok); + unknowns.emplace_back(std::move(name), nameTok); } if (!unknowns.empty()) { diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 2616c47d5..ea11ce9cb 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -169,7 +169,7 @@ static void setSourceLocation(ValueFlow::Value& v, return; std::string s = Path::stripDirectoryPart(file) + ":" + MathLib::toString(ctx.line()) + ": " + ctx.function_name() + " => " + local.function_name() + ": " + debugString(v); - v.debugPath.emplace_back(tok, s); + v.debugPath.emplace_back(tok, std::move(s)); } static void changeKnownToPossible(std::list &values, int indirect=-1) @@ -5391,8 +5391,8 @@ static void valueFlowForwardAssign(Token* const tok, else if (value.bound == ValueFlow::Value::Bound::Upper) valueKind = "greater than "; } - const std::string info = "Assignment '" + tok->astParent()->expressionString() + "', assigned value is " + valueKind + value.infoString(); - value.errorPath.emplace_back(tok, info); + std::string info = "Assignment '" + tok->astParent()->expressionString() + "', assigned value is " + valueKind + value.infoString(); + value.errorPath.emplace_back(tok, std::move(info)); } }