avoid unnecessary copies with `emplace_back()` (#4450)

* avoid unnecessary copies with `emplace_back()`

* cmdlineparser.cpp: suppress `accessMoved` selfcheck false positives
This commit is contained in:
Oliver Stöneberg 2022-09-10 11:25:15 +02:00 committed by GitHub
parent 53820515c9
commit 76d1b9f31a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 33 additions and 29 deletions

View File

@ -65,9 +65,11 @@ static void addFilesToList(const std::string& fileList, std::vector<std::string>
} }
if (files && *files) { if (files && *files) {
std::string fileName; std::string fileName;
// cppcheck-suppress accessMoved - FP
while (std::getline(*files, fileName)) { // next line while (std::getline(*files, fileName)) { // next line
// cppcheck-suppress accessMoved - FP
if (!fileName.empty()) { 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::list<std::st
std::ifstream files(fileList); std::ifstream files(fileList);
if (files) { if (files) {
std::string pathName; std::string pathName;
// cppcheck-suppress accessMoved - FP
while (std::getline(files, pathName)) { // next line while (std::getline(files, pathName)) { // next line
if (!pathName.empty()) { if (!pathName.empty()) {
pathName = Path::removeQuotationMarks(pathName); pathName = Path::removeQuotationMarks(pathName);
@ -87,7 +90,7 @@ static bool addIncludePathsToList(const std::string& fileList, std::list<std::st
if (!endsWith(pathName, '/')) if (!endsWith(pathName, '/'))
pathName += '/'; pathName += '/';
pathNames->emplace_back(pathName); pathNames->emplace_back(std::move(pathName));
} }
} }
return true; return true;
@ -200,7 +203,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
if (!endsWith(path,'/')) if (!endsWith(path,'/'))
path += '/'; path += '/';
mSettings->includePaths.emplace_back(path); mSettings->includePaths.emplace_back(std::move(path));
} }
// User undef // User undef
@ -423,7 +426,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
if (!endsWith(path, '/')) if (!endsWith(path, '/'))
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) { else if (std::strncmp(argv[i], "--rule=", 7) == 0) {
Settings::Rule rule; Settings::Rule rule;
rule.pattern = 7 + argv[i]; rule.pattern = 7 + argv[i];
mSettings->rules.emplace_back(rule); mSettings->rules.emplace_back(std::move(rule));
} }
// Rule file // Rule file
@ -760,7 +763,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
} }
if (!rule.pattern.empty()) if (!rule.pattern.empty())
mSettings->rules.emplace_back(rule); mSettings->rules.emplace_back(std::move(rule));
} }
} else { } else {
printError("unable to load rule-file: " + std::string(12+argv[i])); printError("unable to load rule-file: " + std::string(12+argv[i]));

View File

@ -171,7 +171,7 @@ int ProcessExecutor::handleRead(int rpipe, unsigned int &result)
// Alert only about unique errors // Alert only about unique errors
std::string errmsg = msg.toString(mSettings.verbose); std::string errmsg = msg.toString(mSettings.verbose);
if (std::find(mErrorList.begin(), mErrorList.end(), errmsg) == mErrorList.end()) { 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) if (type == PipeWriter::REPORT_ERROR)
mErrorLogger.reportErr(msg); mErrorLogger.reportErr(msg);
else else

View File

@ -99,12 +99,13 @@ private:
// Alert only about unique errors // Alert only about unique errors
bool reportError = false; bool reportError = false;
const std::string errmsg = msg.toString(mThreadExecutor.mSettings.verbose);
{ {
std::string errmsg = msg.toString(mThreadExecutor.mSettings.verbose);
std::lock_guard<std::mutex> lg(mErrorSync); std::lock_guard<std::mutex> lg(mErrorSync);
if (std::find(mThreadExecutor.mErrorList.begin(), mThreadExecutor.mErrorList.end(), errmsg) == mThreadExecutor.mErrorList.end()) { 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; reportError = true;
} }
} }

View File

@ -96,20 +96,20 @@ std::string Check::getMessageId(const ValueFlow::Value &value, const char id[])
return 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; ErrorPath errorPath;
if (!value) { if (!value) {
errorPath.emplace_back(errtok, bug); errorPath.emplace_back(errtok, std::move(bug));
} else if (mSettings->verbose || mSettings->xml || !mSettings->templateLocation.empty()) { } else if (mSettings->verbose || mSettings->xml || !mSettings->templateLocation.empty()) {
errorPath = value->errorPath; errorPath = value->errorPath;
errorPath.emplace_back(errtok, bug); errorPath.emplace_back(errtok, std::move(bug));
} else { } else {
if (value->condition) if (value->condition)
errorPath.emplace_back(value->condition, "condition '" + value->condition->expressionString() + "'"); errorPath.emplace_back(value->condition, "condition '" + value->condition->expressionString() + "'");
//else if (!value->isKnown() || value->defaultArg) //else if (!value->isKnown() || value->defaultArg)
// errorPath = value->callstack; // errorPath = value->callstack;
errorPath.emplace_back(errtok, bug); errorPath.emplace_back(errtok, std::move(bug));
} }
return errorPath; return errorPath;
} }

View File

@ -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); 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 * Use WRONG_DATA in checkers when you check for wrong data. That

View File

@ -1388,18 +1388,18 @@ void CppCheck::executeAddons(const std::vector<std::string>& files)
ErrorMessage errmsg; ErrorMessage errmsg;
if (obj.count("file") > 0) { if (obj.count("file") > 0) {
const std::string fileName = obj["file"].get<std::string>(); std::string fileName = obj["file"].get<std::string>();
const int64_t lineNumber = obj["linenr"].get<int64_t>(); const int64_t lineNumber = obj["linenr"].get<int64_t>();
const int64_t column = obj["column"].get<int64_t>(); const int64_t column = obj["column"].get<int64_t>();
errmsg.callStack.emplace_back(fileName, lineNumber, column); errmsg.callStack.emplace_back(std::move(fileName), lineNumber, column);
} else if (obj.count("loc") > 0) { } else if (obj.count("loc") > 0) {
for (const picojson::value &locvalue: obj["loc"].get<picojson::array>()) { for (const picojson::value &locvalue: obj["loc"].get<picojson::array>()) {
picojson::object loc = locvalue.get<picojson::object>(); picojson::object loc = locvalue.get<picojson::object>();
const std::string fileName = loc["file"].get<std::string>(); std::string fileName = loc["file"].get<std::string>();
const int64_t lineNumber = loc["linenr"].get<int64_t>(); const int64_t lineNumber = loc["linenr"].get<int64_t>();
const int64_t column = loc["column"].get<int64_t>(); const int64_t column = loc["column"].get<int64_t>();
const std::string info = loc["info"].get<std::string>(); const std::string info = loc["info"].get<std::string>();
errmsg.callStack.emplace_back(fileName, info, lineNumber, column); errmsg.callStack.emplace_back(std::move(fileName), info, lineNumber, column);
} }
} }

View File

@ -521,7 +521,7 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc)
struct Container::RangeItemRecordTypeItem member; struct Container::RangeItemRecordTypeItem member;
member.name = memberName ? memberName : ""; member.name = memberName ? memberName : "";
member.templateParameter = memberTemplateParameter ? std::atoi(memberTemplateParameter) : -1; member.templateParameter = memberTemplateParameter ? std::atoi(memberTemplateParameter) : -1;
container.rangeItemRecordType.emplace_back(member); container.rangeItemRecordType.emplace_back(std::move(member));
} }
} else } else
unknown_elements.insert(containerNodeName); unknown_elements.insert(containerNodeName);

View File

@ -109,7 +109,7 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std:
std::vector<Suppressions::Suppression> suppressions = Suppressions::parseMultiSuppressComment(comment, &errmsg); std::vector<Suppressions::Suppression> suppressions = Suppressions::parseMultiSuppressComment(comment, &errmsg);
if (!errmsg.empty()) if (!errmsg.empty())
bad->emplace_back(tok->location, errmsg); bad->emplace_back(tok->location, std::move(errmsg));
for (const Suppressions::Suppression &s : suppressions) { for (const Suppressions::Suppression &s : suppressions) {
if (!s.errorId.empty()) if (!s.errorId.empty())
@ -126,7 +126,7 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std:
inlineSuppressions.push_back(std::move(s)); inlineSuppressions.push_back(std::move(s));
if (!errmsg.empty()) if (!errmsg.empty())
bad->emplace_back(tok->location, errmsg); bad->emplace_back(tok->location, std::move(errmsg));
} }
return true; return true;

View File

@ -7334,7 +7334,7 @@ void ValueType::setDebugPath(const Token* tok, SourceLocation ctx, SourceLocatio
return; return;
std::string s = Path::stripDirectoryPart(file) + ":" + MathLib::toString(ctx.line()) + ": " + ctx.function_name() + std::string s = Path::stripDirectoryPart(file) + ":" + MathLib::toString(ctx.line()) + ": " + ctx.function_name() +
" => " + local.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) ValueType::MatchResult ValueType::matchParameter(const ValueType *call, const ValueType *func)

View File

@ -704,7 +704,7 @@ void TemplateSimplifier::addInstantiation(Token *token, const std::string &scope
instantiation); instantiation);
if (it == mTemplateInstantiations.end()) if (it == mTemplateInstantiations.end())
mTemplateInstantiations.emplace_back(instantiation); mTemplateInstantiations.emplace_back(std::move(instantiation));
} }
static void getFunctionArguments(const Token *nameToken, std::vector<const Token *> &args) static void getFunctionArguments(const Token *nameToken, std::vector<const Token *> &args)
@ -2207,9 +2207,9 @@ void TemplateSimplifier::expandTemplate(
} }
if (copy) if (copy)
newInstantiations.emplace_back(mTokenList.back(), scope); newInstantiations.emplace_back(mTokenList.back(), std::move(scope));
else if (!inTemplateDefinition) else if (!inTemplateDefinition)
newInstantiations.emplace_back(tok3, scope); newInstantiations.emplace_back(tok3, std::move(scope));
} }
// link() newly tokens manually // link() newly tokens manually

View File

@ -9496,7 +9496,7 @@ void Tokenizer::printUnknownTypes() const
} }
} }
unknowns.emplace_back(name, nameTok); unknowns.emplace_back(std::move(name), nameTok);
} }
if (!unknowns.empty()) { if (!unknowns.empty()) {

View File

@ -169,7 +169,7 @@ static void setSourceLocation(ValueFlow::Value& v,
return; return;
std::string s = Path::stripDirectoryPart(file) + ":" + MathLib::toString(ctx.line()) + ": " + ctx.function_name() + std::string s = Path::stripDirectoryPart(file) + ":" + MathLib::toString(ctx.line()) + ": " + ctx.function_name() +
" => " + local.function_name() + ": " + debugString(v); " => " + local.function_name() + ": " + debugString(v);
v.debugPath.emplace_back(tok, s); v.debugPath.emplace_back(tok, std::move(s));
} }
static void changeKnownToPossible(std::list<ValueFlow::Value> &values, int indirect=-1) static void changeKnownToPossible(std::list<ValueFlow::Value> &values, int indirect=-1)
@ -5391,8 +5391,8 @@ static void valueFlowForwardAssign(Token* const tok,
else if (value.bound == ValueFlow::Value::Bound::Upper) else if (value.bound == ValueFlow::Value::Bound::Upper)
valueKind = "greater than "; valueKind = "greater than ";
} }
const std::string info = "Assignment '" + tok->astParent()->expressionString() + "', assigned value is " + valueKind + value.infoString(); std::string info = "Assignment '" + tok->astParent()->expressionString() + "', assigned value is " + valueKind + value.infoString();
value.errorPath.emplace_back(tok, info); value.errorPath.emplace_back(tok, std::move(info));
} }
} }