Skip to content

Commit 129055d

Browse files
authored
avoid some unnecessary copies around ErrorMessage construction (danmar#4665)
1 parent 1968e84 commit 129055d

16 files changed

+105
-113
lines changed

gui/checkthread.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ void CheckThread::parseClangErrors(const QString &tool, const QString &file0, QS
440440
const std::string f0 = file0.toStdString();
441441
const std::string msg = e.message.toStdString();
442442
const std::string id = e.errorId.toStdString();
443-
ErrorMessage errmsg(callstack, f0, e.severity, msg, id, Certainty::normal);
443+
ErrorMessage errmsg(std::move(callstack), f0, e.severity, msg, id, Certainty::normal);
444444
mResult.reportErr(errmsg);
445445
}
446446
}

lib/check.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ void Check::reportError(const std::list<const Token *> &callstack, Severity seve
7171
writeToErrorList(errmsg);
7272
}
7373

74-
void Check::reportError(const ErrorPath &errorPath, Severity severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty)
74+
void Check::reportError(ErrorPath errorPath, Severity severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty)
7575
{
7676
// TODO: report debug warning when error is for a disabled severity
77-
const ErrorMessage errmsg(errorPath, mTokenizer ? &mTokenizer->list : nullptr, severity, id, msg, cwe, certainty);
77+
const ErrorMessage errmsg(std::move(errorPath), mTokenizer ? &mTokenizer->list : nullptr, severity, id, msg, cwe, certainty);
7878
if (mErrorLogger)
7979
mErrorLogger->reportErr(errmsg);
8080
else

lib/check.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,10 @@ class CPPCHECKLIB Check {
142142
reportError(callstack, severity, id, msg, cwe, certainty);
143143
}
144144

145-
/** report an error */
146-
void reportError(const std::list<const Token *> &callstack, Severity severity, const std::string &id, const std::string &msg) {
147-
reportError(callstack, severity, id, msg, CWE(0U), Certainty::normal);
148-
}
149-
150145
/** report an error */
151146
void reportError(const std::list<const Token *> &callstack, Severity severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty certainty);
152147

153-
void reportError(const ErrorPath &errorPath, Severity severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty);
148+
void reportError(ErrorPath errorPath, Severity severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty);
154149

155150
/** log checker */
156151
void logChecker(const char id[]);

lib/checkautovariables.cpp

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

701701
void CheckAutoVariables::errorInvalidLifetime(const Token *tok, const ValueFlow::Value* val)
@@ -704,7 +704,7 @@ void CheckAutoVariables::errorInvalidLifetime(const Token *tok, const ValueFlow:
704704
ErrorPath errorPath = val ? val->errorPath : ErrorPath();
705705
std::string msg = "Using " + lifetimeMessage(tok, val, errorPath);
706706
errorPath.emplace_back(tok, "");
707-
reportError(errorPath, Severity::error, "invalidLifetime", msg + " that is out of scope.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal);
707+
reportError(std::move(errorPath), Severity::error, "invalidLifetime", msg + " that is out of scope.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal);
708708
}
709709

710710
void CheckAutoVariables::errorDanglingTemporaryLifetime(const Token* tok, const ValueFlow::Value* val, const Token* tempTok)
@@ -714,7 +714,7 @@ void CheckAutoVariables::errorDanglingTemporaryLifetime(const Token* tok, const
714714
std::string msg = "Using " + lifetimeMessage(tok, val, errorPath);
715715
errorPath.emplace_back(tempTok, "Temporary created here.");
716716
errorPath.emplace_back(tok, "");
717-
reportError(errorPath,
717+
reportError(std::move(errorPath),
718718
Severity::error,
719719
"danglingTemporaryLifetime",
720720
msg + " that is a temporary.",
@@ -730,21 +730,21 @@ void CheckAutoVariables::errorDanglngLifetime(const Token *tok, const ValueFlow:
730730
std::string msg = isStatic ? "Static" : "Non-local";
731731
msg += " variable '" + tokName + "' will use " + lifetimeMessage(tok, val, errorPath);
732732
errorPath.emplace_back(tok, "");
733-
reportError(errorPath, Severity::error, "danglingLifetime", msg + ".", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal);
733+
reportError(std::move(errorPath), Severity::error, "danglingLifetime", msg + ".", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal);
734734
}
735735

736736
void CheckAutoVariables::errorDanglingTempReference(const Token* tok, ErrorPath errorPath, bool inconclusive)
737737
{
738738
errorPath.emplace_back(tok, "");
739739
reportError(
740-
errorPath, Severity::error, "danglingTempReference", "Using reference to dangling temporary.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal);
740+
std::move(errorPath), Severity::error, "danglingTempReference", "Using reference to dangling temporary.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal);
741741
}
742742

743743
void CheckAutoVariables::errorReturnReference(const Token* tok, ErrorPath errorPath, bool inconclusive)
744744
{
745745
errorPath.emplace_back(tok, "");
746746
reportError(
747-
errorPath, Severity::error, "returnReference", "Reference to local variable returned.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal);
747+
std::move(errorPath), Severity::error, "returnReference", "Reference to local variable returned.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal);
748748
}
749749

750750
void CheckAutoVariables::errorDanglingReference(const Token *tok, const Variable *var, ErrorPath errorPath)
@@ -753,14 +753,14 @@ void CheckAutoVariables::errorDanglingReference(const Token *tok, const Variable
753753
std::string varName = var ? var->name() : "y";
754754
std::string msg = "Non-local reference variable '" + tokName + "' to local variable '" + varName + "'";
755755
errorPath.emplace_back(tok, "");
756-
reportError(errorPath, Severity::error, "danglingReference", msg, CWE562, Certainty::normal);
756+
reportError(std::move(errorPath), Severity::error, "danglingReference", msg, CWE562, Certainty::normal);
757757
}
758758

759759
void CheckAutoVariables::errorReturnTempReference(const Token* tok, ErrorPath errorPath, bool inconclusive)
760760
{
761761
errorPath.emplace_back(tok, "");
762762
reportError(
763-
errorPath, Severity::error, "returnTempReference", "Reference to temporary returned.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal);
763+
std::move(errorPath), Severity::error, "returnTempReference", "Reference to temporary returned.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal);
764764
}
765765

766766
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
@@ -875,7 +875,7 @@ void CheckBufferOverrun::argumentSizeError(const Token *tok, const std::string &
875875
errorPath.emplace_back(paramVar->nameToken(), "Passing buffer '" + paramVar->name() + "' to function that is declared here");
876876
errorPath.emplace_back(tok, "");
877877

878-
reportError(errorPath, Severity::warning, "argumentSize",
878+
reportError(std::move(errorPath), Severity::warning, "argumentSize",
879879
"$symbol:" + functionName + '\n' +
880880
"Buffer '" + paramExpression + "' is too small, the function '" + functionName + "' expects a bigger buffer in " + strParamNum + " argument", CWE_ARGUMENT_SIZE, Certainty::normal);
881881
}
@@ -1014,7 +1014,7 @@ bool CheckBufferOverrun::analyseWholeProgram1(const std::map<std::string, std::l
10141014
{
10151015
const CTU::FileInfo::FunctionCall *functionCall = nullptr;
10161016

1017-
const std::list<ErrorMessage::FileLocation> &locationList =
1017+
std::list<ErrorMessage::FileLocation> locationList =
10181018
CTU::FileInfo::getErrorPath(CTU::FileInfo::InvalidValueType::bufferOverflow,
10191019
unsafeUsage,
10201020
callsMap,
@@ -1042,7 +1042,7 @@ bool CheckBufferOverrun::analyseWholeProgram1(const std::map<std::string, std::l
10421042
cwe = CWE_POINTER_ARITHMETIC_OVERFLOW;
10431043
}
10441044

1045-
const ErrorMessage errorMessage(locationList,
1045+
const ErrorMessage errorMessage(std::move(locationList),
10461046
file0,
10471047
Severity::error,
10481048
errmsg,
@@ -1138,7 +1138,7 @@ void CheckBufferOverrun::objectIndexError(const Token *tok, const ValueFlow::Val
11381138
}
11391139
errorPath.emplace_back(tok, "");
11401140
std::string verb = known ? "is" : "might be";
1141-
reportError(errorPath,
1141+
reportError(std::move(errorPath),
11421142
known ? Severity::error : Severity::warning,
11431143
"objectIndex",
11441144
"The address of variable '" + name + "' " + verb + " accessed at non-zero index.",
@@ -1204,9 +1204,9 @@ void CheckBufferOverrun::negativeArraySizeError(const Token* tok)
12041204
void CheckBufferOverrun::negativeMemoryAllocationSizeError(const Token* tok, const ValueFlow::Value* value)
12051205
{
12061206
const std::string msg = "Memory allocation size is negative.";
1207-
const ErrorPath errorPath = getErrorPath(tok, value, msg);
1207+
ErrorPath errorPath = getErrorPath(tok, value, msg);
12081208
const bool inconclusive = value != nullptr && !value->isKnown();
1209-
reportError(errorPath, inconclusive ? Severity::warning : Severity::error, "negativeMemoryAllocationSize",
1209+
reportError(std::move(errorPath), inconclusive ? Severity::warning : Severity::error, "negativeMemoryAllocationSize",
12101210
msg, CWE131, inconclusive ? Certainty::inconclusive : Certainty::normal);
12111211
}
12121212

lib/checkclass.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2971,7 +2971,7 @@ void CheckClass::virtualFunctionCallInConstructorError(
29712971
}
29722972
}
29732973

2974-
reportError(errorPath, Severity::style, "virtualCallInConstructor",
2974+
reportError(std::move(errorPath), Severity::style, "virtualCallInConstructor",
29752975
"Virtual function '" + funcname + "' is called from " + scopeFunctionTypeName + " '" + constructorName + "' at line " + std::to_string(lineNumber) + ". Dynamic binding is not used.", CWE(0U), Certainty::normal);
29762976
}
29772977

@@ -2989,7 +2989,7 @@ void CheckClass::pureVirtualFunctionCallInConstructorError(
29892989
if (!errorPath.empty())
29902990
errorPath.back().second = purefuncname + " is a pure virtual function without body";
29912991

2992-
reportError(errorPath, Severity::warning, "pureVirtualCall",
2992+
reportError(std::move(errorPath), Severity::warning, "pureVirtualCall",
29932993
"$symbol:" + purefuncname +"\n"
29942994
"Call of pure virtual function '$symbol' in " + scopeFunctionTypeName + ".\n"
29952995
"Call of pure virtual function '$symbol' in " + scopeFunctionTypeName + ". The call will fail during runtime.", CWE(0U), Certainty::normal);
@@ -3120,7 +3120,7 @@ void CheckClass::duplInheritedMembersError(const Token *tok1, const Token* tok2,
31203120
const std::string message = "The " + std::string(derivedIsStruct ? "struct" : "class") + " '" + derivedName +
31213121
"' defines member " + member + " with name '" + memberName + "' also defined in its parent " +
31223122
std::string(baseIsStruct ? "struct" : "class") + " '" + baseName + "'.";
3123-
reportError(errorPath, Severity::warning, "duplInheritedMember", symbols + '\n' + message, CWE398, Certainty::normal);
3123+
reportError(std::move(errorPath), Severity::warning, "duplInheritedMember", symbols + '\n' + message, CWE398, Certainty::normal);
31243124
}
31253125

31263126

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

3233-
reportError(errorPath, Severity::style, "missingOverride",
3233+
reportError(std::move(errorPath), Severity::style, "missingOverride",
32343234
"$symbol:" + functionName + "\n"
32353235
"The " + funcType + " '$symbol' overrides a " + funcType + " in a base class but is not marked with a 'override' specifier.",
32363236
CWE(0U) /* Unknown CWE! */,
@@ -3254,7 +3254,7 @@ void CheckClass::uselessOverrideError(const Function *funcInBase, const Function
32543254
}
32553255
else
32563256
errStr += "just delegates back to the base class.";
3257-
reportError(errorPath, Severity::style, "uselessOverride",
3257+
reportError(std::move(errorPath), Severity::style, "uselessOverride",
32583258
"$symbol:" + functionName +
32593259
errStr,
32603260
CWE(0U) /* Unknown CWE! */,
@@ -3525,10 +3525,10 @@ bool CheckClass::checkThisUseAfterFreeRecursive(const Scope *classScope, const F
35253525
void CheckClass::thisUseAfterFree(const Token *self, const Token *free, const Token *use)
35263526
{
35273527
std::string selfPointer = self ? self->str() : "ptr";
3528-
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") };
3528+
ErrorPath errorPath = { ErrorPathItem(self, "Assuming '" + selfPointer + "' is used as 'this'"), ErrorPathItem(free, "Delete '" + selfPointer + "', invalidating 'this'"), ErrorPathItem(use, "Call method when 'this' is invalid") };
35293529
const std::string usestr = use ? use->str() : "x";
35303530
const std::string usemsg = use && use->function() ? ("Calling method '" + usestr + "()'") : ("Using member '" + usestr + "'");
3531-
reportError(errorPath, Severity::warning, "thisUseAfterFree",
3531+
reportError(std::move(errorPath), Severity::warning, "thisUseAfterFree",
35323532
"$symbol:" + selfPointer + "\n" +
35333533
usemsg + " when 'this' might be invalid",
35343534
CWE(0), Certainty::normal);

lib/checkcondition.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ void CheckCondition::duplicateConditionError(const Token *tok1, const Token *tok
522522

523523
std::string msg = "The if condition is the same as the previous if condition";
524524

525-
reportError(errorPath, Severity::style, "duplicateCondition", msg, CWE398, Certainty::normal);
525+
reportError(std::move(errorPath), Severity::style, "duplicateCondition", msg, CWE398, Certainty::normal);
526526
}
527527

528528
void CheckCondition::multiCondition()
@@ -590,7 +590,7 @@ void CheckCondition::oppositeElseIfConditionError(const Token *ifCond, const Tok
590590
errorPath.emplace_back(ifCond, "first condition");
591591
errorPath.emplace_back(elseIfCond, "else if condition is opposite to first condition");
592592

593-
reportError(errorPath, Severity::style, "multiCondition", errmsg.str(), CWE398, Certainty::normal);
593+
reportError(std::move(errorPath), Severity::style, "multiCondition", errmsg.str(), CWE398, Certainty::normal);
594594
}
595595

596596
//---------------------------------------------------------------------------
@@ -867,7 +867,7 @@ void CheckCondition::oppositeInnerConditionError(const Token *tok1, const Token*
867867

868868
const std::string msg("Opposite inner '" + innerSmt + "' condition leads to a dead code block.\n"
869869
"Opposite inner '" + innerSmt + "' condition leads to a dead code block (outer condition is '" + s1 + "' and inner condition is '" + s2 + "').");
870-
reportError(errorPath, Severity::warning, "oppositeInnerCondition", msg, CWE398, Certainty::normal);
870+
reportError(std::move(errorPath), Severity::warning, "oppositeInnerCondition", msg, CWE398, Certainty::normal);
871871
}
872872

873873
void CheckCondition::identicalInnerConditionError(const Token *tok1, const Token* tok2, ErrorPath errorPath)
@@ -882,7 +882,7 @@ void CheckCondition::identicalInnerConditionError(const Token *tok1, const Token
882882

883883
const std::string msg("Identical inner '" + innerSmt + "' condition is always true.\n"
884884
"Identical inner '" + innerSmt + "' condition is always true (outer condition is '" + s1 + "' and inner condition is '" + s2 + "').");
885-
reportError(errorPath, Severity::warning, "identicalInnerCondition", msg, CWE398, Certainty::normal);
885+
reportError(std::move(errorPath), Severity::warning, "identicalInnerCondition", msg, CWE398, Certainty::normal);
886886
}
887887

888888
void CheckCondition::identicalConditionAfterEarlyExitError(const Token *cond1, const Token* cond2, ErrorPath errorPath)
@@ -898,7 +898,7 @@ void CheckCondition::identicalConditionAfterEarlyExitError(const Token *cond1, c
898898
errorPath.emplace_back(cond1, "If condition '" + cond + "' is true, the function will return/exit");
899899
errorPath.emplace_back(cond2, (isReturnValue ? "Returning identical expression '" : "Testing identical condition '") + cond + "'");
900900

901-
reportError(errorPath,
901+
reportError(std::move(errorPath),
902902
Severity::warning,
903903
"identicalConditionAfterEarlyExit",
904904
isReturnValue
@@ -1353,12 +1353,12 @@ void CheckCondition::incorrectLogicOperatorError(const Token *tok, const std::st
13531353
return;
13541354
errors.emplace_back(tok, "");
13551355
if (always)
1356-
reportError(errors, Severity::warning, "incorrectLogicOperator",
1356+
reportError(std::move(errors), Severity::warning, "incorrectLogicOperator",
13571357
"Logical disjunction always evaluates to true: " + condition + ".\n"
13581358
"Logical disjunction always evaluates to true: " + condition + ". "
13591359
"Are these conditions necessary? Did you intend to use && instead? Are the numbers correct? Are you comparing the correct variables?", CWE571, inconclusive ? Certainty::inconclusive : Certainty::normal);
13601360
else
1361-
reportError(errors, Severity::warning, "incorrectLogicOperator",
1361+
reportError(std::move(errors), Severity::warning, "incorrectLogicOperator",
13621362
"Logical conjunction always evaluates to false: " + condition + ".\n"
13631363
"Logical conjunction always evaluates to false: " + condition + ". "
13641364
"Are these conditions necessary? Did you intend to use || instead? Are the numbers correct? Are you comparing the correct variables?", CWE570, inconclusive ? Certainty::inconclusive : Certainty::normal);
@@ -1647,8 +1647,8 @@ void CheckCondition::alwaysTrueFalseError(const Token* tok, const Token* conditi
16471647
const std::string expr = tok ? tok->expressionString() : std::string("x");
16481648
const std::string conditionStr = (Token::simpleMatch(condition, "return") ? "Return value" : "Condition");
16491649
const std::string errmsg = conditionStr + " '" + expr + "' is always " + bool_to_string(alwaysTrue);
1650-
const ErrorPath errorPath = getErrorPath(tok, value, errmsg);
1651-
reportError(errorPath,
1650+
ErrorPath errorPath = getErrorPath(tok, value, errmsg);
1651+
reportError(std::move(errorPath),
16521652
Severity::style,
16531653
"knownConditionTrueFalse",
16541654
errmsg,
@@ -1880,7 +1880,7 @@ void CheckCondition::duplicateConditionalAssignError(const Token *condTok, const
18801880
}
18811881

18821882
reportError(
1883-
errors, Severity::style, "duplicateConditionalAssign", msg, CWE398, Certainty::normal);
1883+
std::move(errors), Severity::style, "duplicateConditionalAssign", msg, CWE398, Certainty::normal);
18841884
}
18851885

18861886

0 commit comments

Comments
 (0)