Skip to content

Commit b04d026

Browse files
committed
avoid some unnecessary copies around ErrorMessage construction
1 parent 3136a50 commit b04d026

29 files changed

Lines changed: 171 additions & 182 deletions

cli/cppcheckexecutor.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,7 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck)
385385
cppcheck.tooManyConfigsError(emptyString,0U);
386386

387387
if (settings.checks.isEnabled(Checks::missingInclude) && (Preprocessor::missingIncludeFlag || Preprocessor::missingSystemIncludeFlag)) {
388-
const std::list<ErrorMessage::FileLocation> callStack;
389-
ErrorMessage msg(callStack,
388+
ErrorMessage msg({},
390389
emptyString,
391390
Severity::information,
392391
"Cppcheck cannot find all the include files (use --check-config for details)\n"

gui/checkthread.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ void CheckThread::parseClangErrors(const QString &tool, const QString &file0, QS
404404
const std::string f0 = file0.toStdString();
405405
const std::string msg = e.message.toStdString();
406406
const std::string id = e.errorId.toStdString();
407-
ErrorMessage errmsg(callstack, f0, e.severity, msg, id, Certainty::normal);
407+
ErrorMessage errmsg(std::move(callstack), f0, e.severity, msg, id, Certainty::normal);
408408
mResult.reportErr(errmsg);
409409
}
410410
}

lib/check.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,18 @@ void Check::reportError(const ErrorMessage &errmsg)
5050
}
5151

5252

53-
void Check::reportError(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty::CertaintyLevel certainty)
53+
void Check::reportError(std::list<const Token *> callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty::CertaintyLevel certainty)
5454
{
55-
const ErrorMessage errmsg(callstack, mTokenizer ? &mTokenizer->list : nullptr, severity, id, msg, cwe, certainty);
55+
const ErrorMessage errmsg(std::move(callstack), mTokenizer ? &mTokenizer->list : nullptr, severity, id, msg, cwe, certainty);
5656
if (mErrorLogger)
5757
mErrorLogger->reportErr(errmsg);
5858
else
5959
reportError(errmsg);
6060
}
6161

62-
void Check::reportError(const ErrorPath &errorPath, Severity::SeverityType severity, const char id[], const std::string &msg, const CWE &cwe, Certainty::CertaintyLevel certainty)
62+
void Check::reportError(ErrorPath errorPath, Severity::SeverityType severity, const char id[], const std::string &msg, const CWE &cwe, Certainty::CertaintyLevel certainty)
6363
{
64-
const ErrorMessage errmsg(errorPath, mTokenizer ? &mTokenizer->list : nullptr, severity, id, msg, cwe, certainty);
64+
const ErrorMessage errmsg(std::move(errorPath), mTokenizer ? &mTokenizer->list : nullptr, severity, id, msg, cwe, certainty);
6565
if (mErrorLogger)
6666
mErrorLogger->reportErr(errmsg);
6767
else

lib/check.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,19 +141,19 @@ class CPPCHECKLIB Check {
141141

142142
/** report an error */
143143
void reportError(const Token *tok, const Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty::CertaintyLevel certainty) {
144-
const std::list<const Token *> callstack(1, tok);
145-
reportError(callstack, severity, id, msg, cwe, certainty);
144+
std::list<const Token *> callstack(1, tok);
145+
reportError(std::move(callstack), severity, id, msg, cwe, certainty);
146146
}
147147

148148
/** report an error */
149-
void reportError(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg) {
150-
reportError(callstack, severity, id, msg, CWE(0U), Certainty::normal);
149+
void reportError(std::list<const Token *> callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg) {
150+
reportError(std::move(callstack), severity, id, msg, CWE(0U), Certainty::normal);
151151
}
152152

153153
/** report an error */
154-
void reportError(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty::CertaintyLevel certainty);
154+
void reportError(std::list<const Token *> callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty::CertaintyLevel certainty);
155155

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

158158
ErrorPath getErrorPath(const Token* errtok, const ValueFlow::Value* value, std::string bug) const;
159159

lib/checkautovariables.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ void CheckAutoVariables::errorReturnDanglingLifetime(const Token *tok, const Val
682682
ErrorPath errorPath = val ? val->errorPath : ErrorPath();
683683
std::string msg = "Returning " + lifetimeMessage(tok, val, errorPath);
684684
errorPath.emplace_back(tok, "");
685-
reportError(errorPath, Severity::error, "returnDanglingLifetime", msg + " that will be invalid when returning.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal);
685+
reportError(std::move(errorPath), Severity::error, "returnDanglingLifetime", msg + " that will be invalid when returning.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal);
686686
}
687687

688688
void CheckAutoVariables::errorInvalidLifetime(const Token *tok, const ValueFlow::Value* val)
@@ -691,7 +691,7 @@ void CheckAutoVariables::errorInvalidLifetime(const Token *tok, const ValueFlow:
691691
ErrorPath errorPath = val ? val->errorPath : ErrorPath();
692692
std::string msg = "Using " + lifetimeMessage(tok, val, errorPath);
693693
errorPath.emplace_back(tok, "");
694-
reportError(errorPath, Severity::error, "invalidLifetime", msg + " that is out of scope.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal);
694+
reportError(std::move(errorPath), Severity::error, "invalidLifetime", msg + " that is out of scope.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal);
695695
}
696696

697697
void CheckAutoVariables::errorDanglingTemporaryLifetime(const Token* tok, const ValueFlow::Value* val, const Token* tempTok)
@@ -701,7 +701,7 @@ void CheckAutoVariables::errorDanglingTemporaryLifetime(const Token* tok, const
701701
std::string msg = "Using " + lifetimeMessage(tok, val, errorPath);
702702
errorPath.emplace_back(tempTok, "Temporary created here.");
703703
errorPath.emplace_back(tok, "");
704-
reportError(errorPath,
704+
reportError(std::move(errorPath),
705705
Severity::error,
706706
"danglingTemporaryLifetime",
707707
msg + " that is a temporary.",
@@ -716,21 +716,21 @@ void CheckAutoVariables::errorDanglngLifetime(const Token *tok, const ValueFlow:
716716
std::string tokName = tok ? tok->expressionString() : "x";
717717
std::string msg = "Non-local variable '" + tokName + "' will use " + lifetimeMessage(tok, val, errorPath);
718718
errorPath.emplace_back(tok, "");
719-
reportError(errorPath, Severity::error, "danglingLifetime", msg + ".", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal);
719+
reportError(std::move(errorPath), Severity::error, "danglingLifetime", msg + ".", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal);
720720
}
721721

722722
void CheckAutoVariables::errorDanglingTempReference(const Token* tok, ErrorPath errorPath, bool inconclusive)
723723
{
724724
errorPath.emplace_back(tok, "");
725725
reportError(
726-
errorPath, Severity::error, "danglingTempReference", "Using reference to dangling temporary.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal);
726+
std::move(errorPath), Severity::error, "danglingTempReference", "Using reference to dangling temporary.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal);
727727
}
728728

729729
void CheckAutoVariables::errorReturnReference(const Token* tok, ErrorPath errorPath, bool inconclusive)
730730
{
731731
errorPath.emplace_back(tok, "");
732732
reportError(
733-
errorPath, Severity::error, "returnReference", "Reference to local variable returned.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal);
733+
std::move(errorPath), Severity::error, "returnReference", "Reference to local variable returned.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal);
734734
}
735735

736736
void CheckAutoVariables::errorDanglingReference(const Token *tok, const Variable *var, ErrorPath errorPath)
@@ -739,14 +739,14 @@ void CheckAutoVariables::errorDanglingReference(const Token *tok, const Variable
739739
std::string varName = var ? var->name() : "y";
740740
std::string msg = "Non-local reference variable '" + tokName + "' to local variable '" + varName + "'";
741741
errorPath.emplace_back(tok, "");
742-
reportError(errorPath, Severity::error, "danglingReference", msg, CWE562, Certainty::normal);
742+
reportError(std::move(errorPath), Severity::error, "danglingReference", msg, CWE562, Certainty::normal);
743743
}
744744

745745
void CheckAutoVariables::errorReturnTempReference(const Token* tok, ErrorPath errorPath, bool inconclusive)
746746
{
747747
errorPath.emplace_back(tok, "");
748748
reportError(
749-
errorPath, Severity::error, "returnTempReference", "Reference to temporary returned.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal);
749+
std::move(errorPath), Severity::error, "returnTempReference", "Reference to temporary returned.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal);
750750
}
751751

752752
void CheckAutoVariables::errorInvalidDeallocation(const Token *tok, const ValueFlow::Value *val)

lib/checkbufferoverrun.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ void CheckBufferOverrun::argumentSizeError(const Token *tok, const std::string &
861861
errorPath.emplace_back(paramVar->nameToken(), "Passing buffer '" + paramVar->name() + "' to function that is declared here");
862862
errorPath.emplace_back(tok, "");
863863

864-
reportError(errorPath, Severity::warning, "argumentSize",
864+
reportError(std::move(errorPath), Severity::warning, "argumentSize",
865865
"$symbol:" + functionName + '\n' +
866866
"Buffer '" + paramExpression + "' is too small, the function '" + functionName + "' expects a bigger buffer in " + strParamNum + " argument", CWE_ARGUMENT_SIZE, Certainty::normal);
867867
}
@@ -975,7 +975,7 @@ bool CheckBufferOverrun::analyseWholeProgram1(const std::map<std::string, std::l
975975
{
976976
const CTU::FileInfo::FunctionCall *functionCall = nullptr;
977977

978-
const std::list<ErrorMessage::FileLocation> &locationList =
978+
std::list<ErrorMessage::FileLocation> locationList =
979979
CTU::FileInfo::getErrorPath(CTU::FileInfo::InvalidValueType::bufferOverflow,
980980
unsafeUsage,
981981
callsMap,
@@ -1002,7 +1002,7 @@ bool CheckBufferOverrun::analyseWholeProgram1(const std::map<std::string, std::l
10021002
cwe = CWE_POINTER_ARITHMETIC_OVERFLOW;
10031003
}
10041004

1005-
const ErrorMessage errorMessage(locationList,
1005+
const ErrorMessage errorMessage(std::move(locationList),
10061006
emptyString,
10071007
Severity::error,
10081008
errmsg,
@@ -1097,7 +1097,7 @@ void CheckBufferOverrun::objectIndexError(const Token *tok, const ValueFlow::Val
10971097
}
10981098
errorPath.emplace_back(tok, "");
10991099
std::string verb = known ? "is" : "might be";
1100-
reportError(errorPath,
1100+
reportError(std::move(errorPath),
11011101
known ? Severity::error : Severity::warning,
11021102
"objectIndex",
11031103
"The address of local variable '" + name + "' " + verb + " accessed at non-zero index.",
@@ -1164,8 +1164,8 @@ void CheckBufferOverrun::negativeArraySizeError(const Token* tok)
11641164
void CheckBufferOverrun::negativeMemoryAllocationSizeError(const Token* tok, const ValueFlow::Value* value)
11651165
{
11661166
const std::string msg = "Memory allocation size is negative.";
1167-
const ErrorPath errorPath = getErrorPath(tok, value, msg);
1167+
ErrorPath errorPath = getErrorPath(tok, value, msg);
11681168
const bool inconclusive = value != nullptr && !value->isKnown();
1169-
reportError(errorPath, inconclusive ? Severity::warning : Severity::error, "negativeMemoryAllocationSize",
1169+
reportError(std::move(errorPath), inconclusive ? Severity::warning : Severity::error, "negativeMemoryAllocationSize",
11701170
msg, CWE131, inconclusive ? Certainty::inconclusive : Certainty::safe);
11711171
}

lib/checkclass.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2755,7 +2755,7 @@ void CheckClass::virtualFunctionCallInConstructorError(
27552755
}
27562756
}
27572757

2758-
reportError(errorPath, Severity::style, "virtualCallInConstructor",
2758+
reportError(std::move(errorPath), Severity::style, "virtualCallInConstructor",
27592759
"Virtual function '" + funcname + "' is called from " + scopeFunctionTypeName + " '" + constructorName + "' at line " + MathLib::toString(lineNumber) + ". Dynamic binding is not used.", CWE(0U), Certainty::normal);
27602760
}
27612761

@@ -2773,7 +2773,7 @@ void CheckClass::pureVirtualFunctionCallInConstructorError(
27732773
if (!errorPath.empty())
27742774
errorPath.back().second = purefuncname + " is a pure virtual function without body";
27752775

2776-
reportError(errorPath, Severity::warning, "pureVirtualCall",
2776+
reportError(std::move(errorPath), Severity::warning, "pureVirtualCall",
27772777
"$symbol:" + purefuncname +"\n"
27782778
"Call of pure virtual function '$symbol' in " + scopeFunctionTypeName + ".\n"
27792779
"Call of pure virtual function '$symbol' in " + scopeFunctionTypeName + ". The call will fail during runtime.", CWE(0U), Certainty::normal);
@@ -2834,7 +2834,7 @@ void CheckClass::duplInheritedMembersError(const Token *tok1, const Token* tok2,
28342834
const std::string message = "The " + std::string(derivedIsStruct ? "struct" : "class") + " '" + derivedName +
28352835
"' defines member variable with name '" + variableName + "' also defined in its parent " +
28362836
std::string(baseIsStruct ? "struct" : "class") + " '" + baseName + "'.";
2837-
reportError(errorPath, Severity::warning, "duplInheritedMember", symbols + '\n' + message, CWE398, Certainty::normal);
2837+
reportError(std::move(errorPath), Severity::warning, "duplInheritedMember", symbols + '\n' + message, CWE398, Certainty::normal);
28382838
}
28392839

28402840

@@ -2945,7 +2945,7 @@ void CheckClass::overrideError(const Function *funcInBase, const Function *funcI
29452945
errorPath.emplace_back(funcInDerived->tokenDef, char(std::toupper(funcType[0])) + funcType.substr(1) + " in derived class");
29462946
}
29472947

2948-
reportError(errorPath, Severity::style, "missingOverride",
2948+
reportError(std::move(errorPath), Severity::style, "missingOverride",
29492949
"$symbol:" + functionName + "\n"
29502950
"The " + funcType + " '$symbol' overrides a " + funcType + " in a base class but is not marked with a 'override' specifier.",
29512951
CWE(0U) /* Unknown CWE! */,
@@ -3041,10 +3041,10 @@ bool CheckClass::checkThisUseAfterFreeRecursive(const Scope *classScope, const F
30413041
void CheckClass::thisUseAfterFree(const Token *self, const Token *free, const Token *use)
30423042
{
30433043
std::string selfPointer = self ? self->str() : "ptr";
3044-
const ErrorPath errorPath = { ErrorPathItem(self, "Assuming '" + selfPointer + "' is used as 'this'"), ErrorPathItem(free, "Delete '" + selfPointer + "', invalidating 'this'"), ErrorPathItem(use, "Call method when 'this' is invalid") };
3044+
ErrorPath errorPath = { ErrorPathItem(self, "Assuming '" + selfPointer + "' is used as 'this'"), ErrorPathItem(free, "Delete '" + selfPointer + "', invalidating 'this'"), ErrorPathItem(use, "Call method when 'this' is invalid") };
30453045
const std::string usestr = use ? use->str() : "x";
30463046
const std::string usemsg = use && use->function() ? ("Calling method '" + usestr + "()'") : ("Using member '" + usestr + "'");
3047-
reportError(errorPath, Severity::warning, "thisUseAfterFree",
3047+
reportError(std::move(errorPath), Severity::warning, "thisUseAfterFree",
30483048
"$symbol:" + selfPointer + "\n" +
30493049
usemsg + " when 'this' might be invalid",
30503050
CWE(0), Certainty::normal);
@@ -3218,7 +3218,7 @@ bool CheckClass::analyseWholeProgram(const CTU::FileInfo *ctu, const std::list<C
32183218
locationList.emplace_back(nameLoc.fileName, nameLoc.lineNumber, nameLoc.column);
32193219
locationList.emplace_back(it->second.fileName, it->second.lineNumber, it->second.column);
32203220

3221-
const ErrorMessage errmsg(locationList,
3221+
const ErrorMessage errmsg(std::move(locationList),
32223222
emptyString,
32233223
Severity::error,
32243224
"$symbol:" + nameLoc.className +

0 commit comments

Comments
 (0)