Skip to content

Commit

Permalink
avoid some unnecessary copies [skip ci]
Browse files Browse the repository at this point in the history
this is mainly based on the warnings of the unfinished `performance-unnecessary-copy-on-last-use` clang-tidy check (see llvm/llvm-project#53489). it also includes some fixes spotted by review as well as some adjustments to the previous Coverity fixes
  • Loading branch information
firewave committed Feb 12, 2024
1 parent 4eecf7b commit d371a03
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 61 deletions.
14 changes: 7 additions & 7 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ static bool addIncludePathsToList(const std::string& fileList, std::list<std::st
// cppcheck-suppress accessMoved - FP
while (std::getline(files, pathName)) { // next line
if (!pathName.empty()) {
pathName = Path::removeQuotationMarks(pathName);
pathName = Path::fromNativeSeparators(pathName);
pathName = Path::removeQuotationMarks(std::move(pathName));
pathName = Path::fromNativeSeparators(std::move(pathName));

// If path doesn't end with / or \, add it
if (!endsWith(pathName, '/'))
Expand Down Expand Up @@ -433,8 +433,8 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
else {
path = 2 + argv[i];
}
path = Path::removeQuotationMarks(path);
path = Path::fromNativeSeparators(path);
path = Path::removeQuotationMarks(std::move(path));
path = Path::fromNativeSeparators(std::move(path));

// If path doesn't end with / or \, add it
if (!endsWith(path,'/'))
Expand Down Expand Up @@ -676,9 +676,9 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
}

if (!path.empty()) {
path = Path::removeQuotationMarks(path);
path = Path::fromNativeSeparators(path);
path = Path::simplifyPath(path);
path = Path::removeQuotationMarks(std::move(path));
path = Path::fromNativeSeparators(std::move(path));
path = Path::simplifyPath(std::move(path));

if (Path::isDirectory(path)) {
// If directory name doesn't end with / or \, add it
Expand Down
2 changes: 1 addition & 1 deletion gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,7 @@ void MainWindow::reAnalyzeSelected(const QStringList& files)
QDateTime saveCheckStartTime = mThread->getCheckStartTime();
mThread->check(checkSettings);
mUI->mResults->setCheckSettings(checkSettings);
mThread->setCheckStartTime(saveCheckStartTime);
mThread->setCheckStartTime(std::move(saveCheckStartTime));
}

void MainWindow::reAnalyze(bool all)
Expand Down
2 changes: 1 addition & 1 deletion gui/projectfiledialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ void ProjectFileDialog::saveToProjectFile(ProjectFile *projectFile) const
codingStandards << CODING_STANDARD_MISRA_CPP_2008;
if (mUI->mAutosar->isChecked())
codingStandards << CODING_STANDARD_AUTOSAR;
projectFile->setCodingStandards(codingStandards);
projectFile->setCodingStandards(std::move(codingStandards));
projectFile->setCertIntPrecision(mUI->mEditCertIntPrecision->text().toInt());
projectFile->setBughunting(mUI->mBughunting->isChecked());
projectFile->setClangAnalyzer(mUI->mToolClangAnalyzer->isChecked());
Expand Down
2 changes: 1 addition & 1 deletion gui/resultstree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ void ResultsTree::recheckSelectedFiles()
selectedItems<<fileNameWithCheckPath;
}
}
emit checkSelected(selectedItems);
emit checkSelected(std::move(selectedItems));
}

void ResultsTree::hideAllIdResult()
Expand Down
4 changes: 2 additions & 2 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3225,7 +3225,7 @@ bool isCPPCast(const Token* tok)
return tok && Token::simpleMatch(tok->previous(), "> (") && tok->astOperand2() && tok->astOperand1() && isCPPCastKeyword(tok->astOperand1());
}

bool isConstVarExpression(const Token *tok, std::function<bool(const Token*)> skipPredicate)
bool isConstVarExpression(const Token *tok, const std::function<bool(const Token*)>& skipPredicate)
{
if (!tok)
return false;
Expand Down Expand Up @@ -3255,7 +3255,7 @@ bool isConstVarExpression(const Token *tok, std::function<bool(const Token*)> sk
if (Token::Match(tok, "%cop%|[|.")) {
if (tok->astOperand1() && !isConstVarExpression(tok->astOperand1(), skipPredicate))
return false;
if (tok->astOperand2() && !isConstVarExpression(tok->astOperand2(), std::move(skipPredicate)))
if (tok->astOperand2() && !isConstVarExpression(tok->astOperand2(), skipPredicate))
return false;
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/astutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ bool isLikelyStreamRead(bool cpp, const Token *op);

bool isCPPCast(const Token* tok);

bool isConstVarExpression(const Token* tok, std::function<bool(const Token*)> skipPredicate = nullptr);
bool isConstVarExpression(const Token* tok, const std::function<bool(const Token*)>& skipPredicate = nullptr);

bool isLeafDot(const Token* tok);

Expand Down
2 changes: 1 addition & 1 deletion lib/checkbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ static std::string arrayIndexMessage(const Token* tok,
auto add_dim = [](const std::string &s, const Dimension &dim) {
return s + "[" + std::to_string(dim.num) + "]";
};
const std::string array = std::accumulate(dimensions.cbegin(), dimensions.cend(), tok->astOperand1()->expressionString(), add_dim);
const std::string array = std::accumulate(dimensions.cbegin(), dimensions.cend(), tok->astOperand1()->expressionString(), std::move(add_dim));

std::ostringstream errmsg;
if (condition)
Expand Down
4 changes: 2 additions & 2 deletions lib/forwardanalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ namespace {
}

template<class T, class F, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
Progress traverseTok(T* tok, F f, bool traverseUnknown, T** out = nullptr) {
Progress traverseTok(T* tok, const F &f, bool traverseUnknown, T** out = nullptr) {
if (Token::Match(tok, "asm|goto"))
return Break(Analyzer::Terminate::Bail);
if (Token::Match(tok, "setjmp|longjmp (")) {
Expand Down Expand Up @@ -166,7 +166,7 @@ namespace {
}

template<class T, class F, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
Progress traverseRecursive(T* tok, F f, bool traverseUnknown, unsigned int recursion=0) {
Progress traverseRecursive(T* tok, const F &f, bool traverseUnknown, unsigned int recursion=0) {
if (!tok)
return Progress::Continue;
if (recursion > 10000)
Expand Down
2 changes: 1 addition & 1 deletion lib/importproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ static bool simplifyPathWithVariables(std::string &s, std::map<std::string, std:
}
if (s.find("$(") != std::string::npos)
return false;
s = Path::simplifyPath(Path::fromNativeSeparators(s));
s = Path::simplifyPath(Path::fromNativeSeparators(std::move(s)));
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/pathanalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ PathAnalysis::Progress PathAnalysis::forwardRange(const Token* startToken, const
if (Token::Match(tok, "asm|goto|break|continue"))
return Progress::Break;
if (Token::Match(tok, "return|throw")) {
forwardRecursive(tok, info, f);
forwardRecursive(tok, std::move(info), f);
return Progress::Break;
// Evaluate RHS of assignment before LHS
}
Expand Down
8 changes: 4 additions & 4 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6469,7 +6469,7 @@ static const Token* parsedecl(const Token* type,
bool isCpp,
SourceLocation loc = SourceLocation::current());

void SymbolDatabase::setValueType(Token* tok, const Variable& var, SourceLocation loc)
void SymbolDatabase::setValueType(Token* tok, const Variable& var, const SourceLocation &loc)
{
ValueType valuetype;
if (mSettings.debugnormal || mSettings.debugwarnings)
Expand Down Expand Up @@ -6502,7 +6502,7 @@ void SymbolDatabase::setValueType(Token* tok, const Variable& var, SourceLocatio

static ValueType::Type getEnumType(const Scope* scope, const Platform& platform);

void SymbolDatabase::setValueType(Token* tok, const Enumerator& enumerator, SourceLocation loc)
void SymbolDatabase::setValueType(Token* tok, const Enumerator& enumerator, const SourceLocation &loc)
{
ValueType valuetype;
if (mSettings.debugnormal || mSettings.debugwarnings)
Expand Down Expand Up @@ -6551,7 +6551,7 @@ static bool isContainerYieldPointer(Library::Container::Yield yield)
return yield == Library::Container::Yield::BUFFER || yield == Library::Container::Yield::BUFFER_NT;
}

void SymbolDatabase::setValueType(Token* tok, const ValueType& valuetype, SourceLocation loc)
void SymbolDatabase::setValueType(Token* tok, const ValueType& valuetype, const SourceLocation &loc)
{
auto* valuetypePtr = new ValueType(valuetype);
if (mSettings.debugnormal || mSettings.debugwarnings)
Expand Down Expand Up @@ -8138,7 +8138,7 @@ std::string ValueType::str() const
return ret.substr(1);
}

void ValueType::setDebugPath(const Token* tok, SourceLocation ctx, SourceLocation local)
void ValueType::setDebugPath(const Token* tok, SourceLocation ctx, const SourceLocation &local)
{
std::string file = ctx.file_name();
if (file.empty())
Expand Down
8 changes: 4 additions & 4 deletions lib/symboldatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -1312,7 +1312,7 @@ class CPPCHECKLIB ValueType {
std::string str() const;
std::string dump() const;

void setDebugPath(const Token* tok, SourceLocation ctx, SourceLocation local = SourceLocation::current());
void setDebugPath(const Token* tok, SourceLocation ctx, const SourceLocation &local = SourceLocation::current());
};


Expand Down Expand Up @@ -1463,9 +1463,9 @@ class CPPCHECKLIB SymbolDatabase {

const Enumerator * findEnumerator(const Token * tok, std::set<std::string>& tokensThatAreNotEnumeratorValues) const;

void setValueType(Token* tok, const ValueType& valuetype, SourceLocation loc = SourceLocation::current());
void setValueType(Token* tok, const Variable& var, SourceLocation loc = SourceLocation::current());
void setValueType(Token* tok, const Enumerator& enumerator, SourceLocation loc = SourceLocation::current());
void setValueType(Token* tok, const ValueType& valuetype, const SourceLocation &loc = SourceLocation::current());
void setValueType(Token* tok, const Variable& var, const SourceLocation &loc = SourceLocation::current());
void setValueType(Token* tok, const Enumerator& enumerator, const SourceLocation &loc = SourceLocation::current());

Tokenizer& mTokenizer;
const Settings &mSettings;
Expand Down
18 changes: 9 additions & 9 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ static void setTokenValue(Token* tok,
}

else if (parent->str() == ":") {
setTokenValue(parent,value,settings);
setTokenValue(parent,std::move(value),settings);
}

else if (parent->str() == "?" && tok->str() == ":" && tok == parent->astOperand2() && parent->astOperand1()) {
Expand Down Expand Up @@ -807,7 +807,7 @@ static void setTokenValue(Token* tok,
if (ret)
return;

ValueFlow::Value v(value);
ValueFlow::Value v(std::move(value));
v.conditional = true;
v.changeKnownToPossible();

Expand Down Expand Up @@ -1053,20 +1053,20 @@ static void setTokenValue(Token* tok,
}

else if (Token::Match(parent, ":: %name%") && parent->astOperand2() == tok) {
setTokenValue(parent, value, settings);
setTokenValue(parent, std::move(value), settings);
}
// Calling std::size or std::empty on an array
else if (value.isTokValue() && Token::simpleMatch(value.tokvalue, "{") && tok->variable() &&
tok->variable()->isArray() && Token::Match(parent->previous(), "%name% (") && astIsRHS(tok)) {
std::vector<const Token*> args = getArguments(value.tokvalue);
if (const Library::Function* f = settings.library.getFunction(parent->previous())) {
if (f->containerYield == Library::Container::Yield::SIZE) {
ValueFlow::Value v(value);
ValueFlow::Value v(std::move(value));
v.valueType = ValueFlow::Value::ValueType::INT;
v.intvalue = args.size();
setTokenValue(parent, std::move(v), settings);
} else if (f->containerYield == Library::Container::Yield::EMPTY) {
ValueFlow::Value v(value);
ValueFlow::Value v(std::move(value));
v.intvalue = args.empty();
v.valueType = ValueFlow::Value::ValueType::INT;
setTokenValue(parent, std::move(v), settings);
Expand Down Expand Up @@ -2956,13 +2956,13 @@ struct ValueFlowAnalyzer : Analyzer {
if (tok->hasKnownIntValue())
return {static_cast<int>(tok->values().front().intvalue)};
std::vector<MathLib::bigint> result;
ProgramMemory pm = getProgramMemory();
if (Token::Match(tok, "&&|%oror%")) {
if (conditionIsTrue(tok, pm, &getSettings()))
if (conditionIsTrue(tok, getProgramMemory(), &getSettings()))
result.push_back(1);
if (conditionIsFalse(tok, std::move(pm), &getSettings()))
if (conditionIsFalse(tok, getProgramMemory(), &getSettings()))
result.push_back(0);
} else {
ProgramMemory pm = getProgramMemory();
MathLib::bigint out = 0;
bool error = false;
execute(tok, pm, &out, &error, &getSettings());
Expand Down Expand Up @@ -4009,7 +4009,7 @@ static void valueFlowForwardLifetime(Token * tok, TokenList &tokenlist, ErrorLog
const Token* parentLifetime =
getParentLifetime(tokenlist.isCPP(), parent->astOperand1()->astOperand2(), &settings.library);
if (parentLifetime && parentLifetime->exprId() > 0) {
valueFlowForward(nextExpression, endOfVarScope, parentLifetime, values, tokenlist, errorLogger, settings);
valueFlowForward(nextExpression, endOfVarScope, parentLifetime, std::move(values), tokenlist, errorLogger, settings);
}
}
}
Expand Down
Loading

0 comments on commit d371a03

Please sign in to comment.