From fbccb3ae556e36138771d72f316528214d72317c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 10 Oct 2017 15:49:15 +0200 Subject: [PATCH] Add errorpath for accessMoved message --- lib/checkother.cpp | 19 +++++++++++++------ lib/checkother.h | 5 ++--- lib/valueflow.cpp | 5 +++++ 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 43af8f16d..f7bd81b22 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2658,7 +2658,7 @@ void CheckOther::checkAccessOfMovedVariable() } } if (accessOfMoved || (inconclusive && reportInconclusive)) - accessMovedError(tok, tok->str(), movedValue->moveKind, inconclusive || movedValue->isInconclusive()); + accessMovedError(tok, tok->str(), movedValue, inconclusive || movedValue->isInconclusive()); } } } @@ -2676,11 +2676,17 @@ bool CheckOther::isMovedParameterAllowedForInconclusiveFunction(const Token * to return true; } -void CheckOther::accessMovedError(const Token *tok, const std::string &varname, ValueFlow::Value::MoveKind moveKind, bool inconclusive) +void CheckOther::accessMovedError(const Token *tok, const std::string &varname, const ValueFlow::Value *value, bool inconclusive) { + if (!tok) { + reportError(tok, Severity::warning, "accessMoved", "Access of moved variable v.", CWE672, false); + reportError(tok, Severity::warning, "accessForwarded", "Access of forwarded variable v.", CWE672, false); + return; + } + const char * errorId = nullptr; - const char * kindString = nullptr; - switch (moveKind) { + std::string kindString; + switch (value->moveKind) { case ValueFlow::Value::MovedVariable: errorId = "accessMoved"; kindString = "moved"; @@ -2692,8 +2698,9 @@ void CheckOther::accessMovedError(const Token *tok, const std::string &varname, default: return; } - const std::string errmsg(std::string("Access of ") + kindString + " variable " + varname + "."); - reportError(tok, Severity::warning, errorId, errmsg, CWE672, inconclusive); + const std::string errmsg("Access of " + kindString + " variable " + varname + "."); + const ErrorPath errorPath = getErrorPath(tok, value, errmsg); + reportError(errorPath, Severity::warning, errorId, errmsg, CWE672, inconclusive); } diff --git a/lib/checkother.h b/lib/checkother.h index dcb2dbcf3..85ede5e93 100644 --- a/lib/checkother.h +++ b/lib/checkother.h @@ -257,7 +257,7 @@ private: void unusedLabelError(const Token* tok, bool inSwitch); void unknownEvaluationOrder(const Token* tok); static bool isMovedParameterAllowedForInconclusiveFunction(const Token * tok); - void accessMovedError(const Token *tok, const std::string &varname, ValueFlow::Value::MoveKind moveKind, bool inconclusive); + void accessMovedError(const Token *tok, const std::string &varname, const ValueFlow::Value *value, bool inconclusive); void funcArgNamesDifferent(const std::string & functionName, size_t index, const Token* declaration, const Token* definition); void funcArgOrderDifferent(const std::string & functionName, const Token * declaration, const Token * definition, const std::vector & declarations, const std::vector & definitions); @@ -313,8 +313,7 @@ private: c.unusedLabelError(nullptr, true); c.unusedLabelError(nullptr, false); c.unknownEvaluationOrder(nullptr); - c.accessMovedError(nullptr, "v", ValueFlow::Value::MovedVariable, false); - c.accessMovedError(nullptr, "v", ValueFlow::Value::ForwardedVariable, false); + c.accessMovedError(nullptr, "v", nullptr, false); c.funcArgNamesDifferent("function", 1, nullptr, nullptr); std::vector nullvec; diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index de872319e..79d4a6dc9 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -2122,6 +2122,7 @@ static void valueFlowAfterMove(TokenList *tokenlist, SymbolDatabase* symboldatab ValueFlow::Value value; value.valueType = ValueFlow::Value::MOVED; value.moveKind = ValueFlow::Value::NonMovedVariable; + value.errorPath.push_back(ErrorPathItem(tok, "Calling " + tok->next()->expressionString() + " makes " + tok->str() + " 'non-moved'")); value.setKnown(); std::list values; values.push_back(value); @@ -2158,6 +2159,10 @@ static void valueFlowAfterMove(TokenList *tokenlist, SymbolDatabase* symboldatab ValueFlow::Value value; value.valueType = ValueFlow::Value::MOVED; value.moveKind = moveKind; + if (moveKind == ValueFlow::Value::MovedVariable) + value.errorPath.push_back(ErrorPathItem(tok, "Calling std::move(" + varTok->str() + ")")); + else // if (moveKind == ValueFlow::Value::ForwardedVariable) + value.errorPath.push_back(ErrorPathItem(tok, "Calling std::forward(" + varTok->str() + ")")); value.setKnown(); std::list values; values.push_back(value);