From eeb6db05f13694337e8b2d19bd8597b80e646a02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Thu, 29 Sep 2022 21:47:17 +0200 Subject: [PATCH] avoid some unnecessary object creations and copies (#4493) --- lib/checkunusedfunctions.cpp | 5 +---- lib/cppcheck.cpp | 8 ++------ lib/errorlogger.h | 2 +- lib/forwardanalyzer.cpp | 2 +- lib/library.cpp | 4 ++-- lib/library.h | 2 +- lib/symboldatabase.cpp | 2 +- lib/valueflow.cpp | 16 +++++++++------- 8 files changed, 18 insertions(+), 23 deletions(-) diff --git a/lib/checkunusedfunctions.cpp b/lib/checkunusedfunctions.cpp index 5d688d593..020d98c80 100644 --- a/lib/checkunusedfunctions.cpp +++ b/lib/checkunusedfunctions.cpp @@ -341,10 +341,7 @@ void CheckUnusedFunctions::unusedFunctionError(ErrorLogger * const errorLogger, { std::list locationList; if (!filename.empty()) { - ErrorMessage::FileLocation fileLoc; - fileLoc.setfile(filename); - fileLoc.line = lineNumber; - locationList.push_back(std::move(fileLoc)); + locationList.emplace_back(filename, lineNumber); } const ErrorMessage errmsg(locationList, emptyString, Severity::style, "$symbol:" + funcname + "\nThe function '$symbol' is never used.", "unusedFunction", CWE561, Certainty::normal); diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 3f4185fe6..a4acbb722 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -1476,9 +1476,7 @@ void CppCheck::tooManyConfigsError(const std::string &file, const int numberOfCo std::list loclist; if (!file.empty()) { - ErrorMessage::FileLocation location; - location.setfile(file); - loclist.push_back(std::move(location)); + loclist.emplace_back(file); } std::ostringstream msg; @@ -1514,9 +1512,7 @@ void CppCheck::purgedConfigurationMessage(const std::string &file, const std::st std::list loclist; if (!file.empty()) { - ErrorMessage::FileLocation location; - location.setfile(file); - loclist.push_back(std::move(location)); + loclist.emplace_back(file); } ErrorMessage errmsg(loclist, diff --git a/lib/errorlogger.h b/lib/errorlogger.h index 1982c409c..7a2e0a566 100644 --- a/lib/errorlogger.h +++ b/lib/errorlogger.h @@ -57,7 +57,7 @@ public: FileLocation() : fileIndex(0), line(0), column(0) {} - FileLocation(const std::string &file, int line, unsigned int column) + explicit FileLocation(const std::string &file, int line = 0, unsigned int column = 0) : fileIndex(0), line(line), column(column), mOrigFileName(file), mFileName(file) {} FileLocation(const std::string &file, std::string info, int line, unsigned int column) diff --git a/lib/forwardanalyzer.cpp b/lib/forwardanalyzer.cpp index 5435109c7..e61654415 100644 --- a/lib/forwardanalyzer.cpp +++ b/lib/forwardanalyzer.cpp @@ -301,7 +301,7 @@ struct ForwardTraversal { std::vector tryForkScope(Token* endBlock, bool isModified = false) { if (analyzer->updateScope(endBlock, isModified)) { ForwardTraversal ft = fork(); - return {ft}; + return {std::move(ft)}; } return std::vector {}; } diff --git a/lib/library.cpp b/lib/library.cpp index 62b26e006..1ab7e34f9 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -1645,9 +1645,9 @@ const Library::Container * getLibraryContainer(const Token * tok) return tok->valueType()->container; } -Library::TypeCheck Library::getTypeCheck(const std::string &check, const std::string &typeName) const +Library::TypeCheck Library::getTypeCheck(std::string check, std::string typeName) const { - auto it = mTypeChecks.find(std::pair(check, typeName)); + auto it = mTypeChecks.find(std::pair(std::move(check), std::move(typeName))); return it == mTypeChecks.end() ? TypeCheck::def : it->second; } diff --git a/lib/library.h b/lib/library.h index 9a5e87a58..1e9ad3325 100644 --- a/lib/library.h +++ b/lib/library.h @@ -555,7 +555,7 @@ public: /** Suppress/check a type */ enum class TypeCheck { def, check, suppress }; - TypeCheck getTypeCheck(const std::string &check, const std::string &typeName) const; + TypeCheck getTypeCheck(std::string check, std::string typeName) const; private: // load a xml node diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index d2564c9f7..8e1bbba1b 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -3034,7 +3034,7 @@ Function* SymbolDatabase::addGlobalFunction(Scope*& scope, const Token*& tok, co Function* SymbolDatabase::addGlobalFunctionDecl(Scope*& scope, const Token *tok, const Token *argStart, const Token* funcStart) { Function function(mTokenizer, tok, scope, funcStart, argStart); - scope->addFunction(function); + scope->addFunction(std::move(function)); return &scope->functionList.back(); } diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index e6d5759e4..3ec22a18a 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -6389,7 +6389,7 @@ struct SimpleConditionHandler : ConditionHandler { cond.false_values.emplace_back(tok, 0LL); cond.vartok = vartok; - return {cond}; + return {std::move(cond)}; } }; @@ -6574,8 +6574,8 @@ struct SymbolicConditionHandler : SimpleConditionHandler { setSymbolic(false_value, valuetok); Condition cond; - cond.true_values = {true_value}; - cond.false_values = {false_value}; + cond.true_values = {std::move(true_value)}; + cond.false_values = {std::move(false_value)}; cond.vartok = vartok; cond.inverted = inverted; result.push_back(std::move(cond)); @@ -7977,7 +7977,7 @@ struct IteratorConditionHandler : SimpleConditionHandler { cond.false_values = values; } - return {cond}; + return {std::move(cond)}; } }; @@ -8308,7 +8308,7 @@ struct ContainerConditionHandler : ConditionHandler { cond.false_values.emplace_back(std::move(value)); cond.vartok = vartok; cond.inverted = true; - return {cond}; + return {std::move(cond)}; } // String compare if (Token::Match(tok, "==|!=")) { @@ -8331,7 +8331,7 @@ struct ContainerConditionHandler : ConditionHandler { cond.true_values.emplace_back(std::move(value)); cond.vartok = vartok; cond.impossible = false; - return {cond}; + return {std::move(cond)}; } return {}; } @@ -8965,6 +8965,7 @@ const ValueFlow::Value* ValueFlow::findValue(const std::list& return ret; } +// TODO: returns a single value at most - no need for std::vector static std::vector isOutOfBoundsImpl(const ValueFlow::Value& size, const Token* indexTok, bool condition) @@ -8996,9 +8997,10 @@ static std::vector isOutOfBoundsImpl(const ValueFlow::Value& s return {}; value.intvalue = size.intvalue; value.bound = ValueFlow::Value::Bound::Lower; - return {value}; + return {std::move(value)}; } +// TODO: return single value at most - no need for std::vector std::vector ValueFlow::isOutOfBounds(const Value& size, const Token* indexTok, bool possible) { ValueFlow::Value inBoundsValue = inferCondition("<", indexTok, size.intvalue);