Skip to content

Commit 76d1b9f

Browse files
authored
avoid unnecessary copies with emplace_back() (cppcheck-opensource#4450)
* avoid unnecessary copies with `emplace_back()` * cmdlineparser.cpp: suppress `accessMoved` selfcheck false positives
1 parent 5382051 commit 76d1b9f

12 files changed

Lines changed: 33 additions & 29 deletions

cli/cmdlineparser.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ static void addFilesToList(const std::string& fileList, std::vector<std::string>
6565
}
6666
if (files && *files) {
6767
std::string fileName;
68+
// cppcheck-suppress accessMoved - FP
6869
while (std::getline(*files, fileName)) { // next line
70+
// cppcheck-suppress accessMoved - FP
6971
if (!fileName.empty()) {
70-
pathNames.emplace_back(fileName);
72+
pathNames.emplace_back(std::move(fileName));
7173
}
7274
}
7375
}
@@ -78,6 +80,7 @@ static bool addIncludePathsToList(const std::string& fileList, std::list<std::st
7880
std::ifstream files(fileList);
7981
if (files) {
8082
std::string pathName;
83+
// cppcheck-suppress accessMoved - FP
8184
while (std::getline(files, pathName)) { // next line
8285
if (!pathName.empty()) {
8386
pathName = Path::removeQuotationMarks(pathName);
@@ -87,7 +90,7 @@ static bool addIncludePathsToList(const std::string& fileList, std::list<std::st
8790
if (!endsWith(pathName, '/'))
8891
pathName += '/';
8992

90-
pathNames->emplace_back(pathName);
93+
pathNames->emplace_back(std::move(pathName));
9194
}
9295
}
9396
return true;
@@ -200,7 +203,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
200203
if (!endsWith(path,'/'))
201204
path += '/';
202205

203-
mSettings->includePaths.emplace_back(path);
206+
mSettings->includePaths.emplace_back(std::move(path));
204207
}
205208

206209
// User undef
@@ -423,7 +426,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
423426
if (!endsWith(path, '/'))
424427
path += '/';
425428
}
426-
mIgnoredPaths.emplace_back(path);
429+
mIgnoredPaths.emplace_back(std::move(path));
427430
}
428431
}
429432

@@ -722,7 +725,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
722725
else if (std::strncmp(argv[i], "--rule=", 7) == 0) {
723726
Settings::Rule rule;
724727
rule.pattern = 7 + argv[i];
725-
mSettings->rules.emplace_back(rule);
728+
mSettings->rules.emplace_back(std::move(rule));
726729
}
727730

728731
// Rule file
@@ -760,7 +763,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
760763
}
761764

762765
if (!rule.pattern.empty())
763-
mSettings->rules.emplace_back(rule);
766+
mSettings->rules.emplace_back(std::move(rule));
764767
}
765768
} else {
766769
printError("unable to load rule-file: " + std::string(12+argv[i]));

cli/processexecutor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ int ProcessExecutor::handleRead(int rpipe, unsigned int &result)
171171
// Alert only about unique errors
172172
std::string errmsg = msg.toString(mSettings.verbose);
173173
if (std::find(mErrorList.begin(), mErrorList.end(), errmsg) == mErrorList.end()) {
174-
mErrorList.emplace_back(errmsg);
174+
mErrorList.emplace_back(std::move(errmsg));
175175
if (type == PipeWriter::REPORT_ERROR)
176176
mErrorLogger.reportErr(msg);
177177
else

cli/threadexecutor.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,13 @@ class ThreadExecutor::SyncLogForwarder : public ErrorLogger
9999

100100
// Alert only about unique errors
101101
bool reportError = false;
102-
const std::string errmsg = msg.toString(mThreadExecutor.mSettings.verbose);
103102

104103
{
104+
std::string errmsg = msg.toString(mThreadExecutor.mSettings.verbose);
105+
105106
std::lock_guard<std::mutex> lg(mErrorSync);
106107
if (std::find(mThreadExecutor.mErrorList.begin(), mThreadExecutor.mErrorList.end(), errmsg) == mThreadExecutor.mErrorList.end()) {
107-
mThreadExecutor.mErrorList.emplace_back(errmsg);
108+
mThreadExecutor.mErrorList.emplace_back(std::move(errmsg));
108109
reportError = true;
109110
}
110111
}

lib/check.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,20 @@ std::string Check::getMessageId(const ValueFlow::Value &value, const char id[])
9696
return id;
9797
}
9898

99-
ErrorPath Check::getErrorPath(const Token* errtok, const ValueFlow::Value* value, const std::string& bug) const
99+
ErrorPath Check::getErrorPath(const Token* errtok, const ValueFlow::Value* value, std::string bug) const
100100
{
101101
ErrorPath errorPath;
102102
if (!value) {
103-
errorPath.emplace_back(errtok, bug);
103+
errorPath.emplace_back(errtok, std::move(bug));
104104
} else if (mSettings->verbose || mSettings->xml || !mSettings->templateLocation.empty()) {
105105
errorPath = value->errorPath;
106-
errorPath.emplace_back(errtok, bug);
106+
errorPath.emplace_back(errtok, std::move(bug));
107107
} else {
108108
if (value->condition)
109109
errorPath.emplace_back(value->condition, "condition '" + value->condition->expressionString() + "'");
110110
//else if (!value->isKnown() || value->defaultArg)
111111
// errorPath = value->callstack;
112-
errorPath.emplace_back(errtok, bug);
112+
errorPath.emplace_back(errtok, std::move(bug));
113113
}
114114
return errorPath;
115115
}

lib/check.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class CPPCHECKLIB Check {
154154

155155
void reportError(const ErrorPath &errorPath, Severity::SeverityType severity, const char id[], const std::string &msg, const CWE &cwe, Certainty::CertaintyLevel certainty);
156156

157-
ErrorPath getErrorPath(const Token* errtok, const ValueFlow::Value* value, const std::string& bug) const;
157+
ErrorPath getErrorPath(const Token* errtok, const ValueFlow::Value* value, std::string bug) const;
158158

159159
/**
160160
* Use WRONG_DATA in checkers when you check for wrong data. That

lib/cppcheck.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,18 +1388,18 @@ void CppCheck::executeAddons(const std::vector<std::string>& files)
13881388
ErrorMessage errmsg;
13891389

13901390
if (obj.count("file") > 0) {
1391-
const std::string fileName = obj["file"].get<std::string>();
1391+
std::string fileName = obj["file"].get<std::string>();
13921392
const int64_t lineNumber = obj["linenr"].get<int64_t>();
13931393
const int64_t column = obj["column"].get<int64_t>();
1394-
errmsg.callStack.emplace_back(fileName, lineNumber, column);
1394+
errmsg.callStack.emplace_back(std::move(fileName), lineNumber, column);
13951395
} else if (obj.count("loc") > 0) {
13961396
for (const picojson::value &locvalue: obj["loc"].get<picojson::array>()) {
13971397
picojson::object loc = locvalue.get<picojson::object>();
1398-
const std::string fileName = loc["file"].get<std::string>();
1398+
std::string fileName = loc["file"].get<std::string>();
13991399
const int64_t lineNumber = loc["linenr"].get<int64_t>();
14001400
const int64_t column = loc["column"].get<int64_t>();
14011401
const std::string info = loc["info"].get<std::string>();
1402-
errmsg.callStack.emplace_back(fileName, info, lineNumber, column);
1402+
errmsg.callStack.emplace_back(std::move(fileName), info, lineNumber, column);
14031403
}
14041404
}
14051405

lib/library.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc)
521521
struct Container::RangeItemRecordTypeItem member;
522522
member.name = memberName ? memberName : "";
523523
member.templateParameter = memberTemplateParameter ? std::atoi(memberTemplateParameter) : -1;
524-
container.rangeItemRecordType.emplace_back(member);
524+
container.rangeItemRecordType.emplace_back(std::move(member));
525525
}
526526
} else
527527
unknown_elements.insert(containerNodeName);

lib/preprocessor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std:
109109
std::vector<Suppressions::Suppression> suppressions = Suppressions::parseMultiSuppressComment(comment, &errmsg);
110110

111111
if (!errmsg.empty())
112-
bad->emplace_back(tok->location, errmsg);
112+
bad->emplace_back(tok->location, std::move(errmsg));
113113

114114
for (const Suppressions::Suppression &s : suppressions) {
115115
if (!s.errorId.empty())
@@ -126,7 +126,7 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std:
126126
inlineSuppressions.push_back(std::move(s));
127127

128128
if (!errmsg.empty())
129-
bad->emplace_back(tok->location, errmsg);
129+
bad->emplace_back(tok->location, std::move(errmsg));
130130
}
131131

132132
return true;

lib/symboldatabase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7334,7 +7334,7 @@ void ValueType::setDebugPath(const Token* tok, SourceLocation ctx, SourceLocatio
73347334
return;
73357335
std::string s = Path::stripDirectoryPart(file) + ":" + MathLib::toString(ctx.line()) + ": " + ctx.function_name() +
73367336
" => " + local.function_name();
7337-
debugPath.emplace_back(tok, s);
7337+
debugPath.emplace_back(tok, std::move(s));
73387338
}
73397339

73407340
ValueType::MatchResult ValueType::matchParameter(const ValueType *call, const ValueType *func)

lib/templatesimplifier.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ void TemplateSimplifier::addInstantiation(Token *token, const std::string &scope
704704
instantiation);
705705

706706
if (it == mTemplateInstantiations.end())
707-
mTemplateInstantiations.emplace_back(instantiation);
707+
mTemplateInstantiations.emplace_back(std::move(instantiation));
708708
}
709709

710710
static void getFunctionArguments(const Token *nameToken, std::vector<const Token *> &args)
@@ -2207,9 +2207,9 @@ void TemplateSimplifier::expandTemplate(
22072207
}
22082208

22092209
if (copy)
2210-
newInstantiations.emplace_back(mTokenList.back(), scope);
2210+
newInstantiations.emplace_back(mTokenList.back(), std::move(scope));
22112211
else if (!inTemplateDefinition)
2212-
newInstantiations.emplace_back(tok3, scope);
2212+
newInstantiations.emplace_back(tok3, std::move(scope));
22132213
}
22142214

22152215
// link() newly tokens manually

0 commit comments

Comments
 (0)