From 55ae206ecc7571ebf7a00295a949285244823c08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 19 May 2017 17:29:16 +0200 Subject: [PATCH] ErrorPath: better output --- lib/check.h | 8 ++++---- lib/checkbufferoverrun.cpp | 2 +- lib/checknullpointer.cpp | 2 +- lib/checkother.cpp | 2 +- lib/errorlogger.cpp | 4 ++++ 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/check.h b/lib/check.h index fabda7173..a51bb5480 100644 --- a/lib/check.h +++ b/lib/check.h @@ -165,19 +165,19 @@ protected: reportError(errmsg); } - ErrorPath getErrorPath(const Token *errtok, const ValueFlow::Value *value) const { + ErrorPath getErrorPath(const Token *errtok, const ValueFlow::Value *value, const std::string &bug) const { ErrorPath errorPath; if (!value) { - errorPath.push_back(ErrorPathItem(errtok,"")); + errorPath.push_back(ErrorPathItem(errtok,bug)); } else if (_settings->verbose || _settings->outputFormat == "clang") { errorPath = value->errorPath; - errorPath.push_back(ErrorPathItem(errtok,"")); + errorPath.push_back(ErrorPathItem(errtok,bug)); } else { if (value->condition) errorPath.push_back(ErrorPathItem(value->condition, "condition '" + value->condition->expressionString() + "'")); //else if (!value->isKnown() || value->defaultArg) // errorPath = value->callstack; - errorPath.push_back(ErrorPathItem(errtok,"")); + errorPath.push_back(ErrorPathItem(errtok,bug)); } return errorPath; } diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 85b0ed751..595513a26 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -1780,7 +1780,7 @@ void CheckBufferOverrun::negativeIndexError(const Token *tok, MathLib::bigint in void CheckBufferOverrun::negativeIndexError(const Token *tok, const ValueFlow::Value &index) { - const ErrorPath errorPath = getErrorPath(tok, &index); + const ErrorPath errorPath = getErrorPath(tok, &index, "Negative array index"); std::ostringstream errmsg; if (index.condition) errmsg << ValueFlow::eitherTheConditionIsRedundant(index.condition) diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 9809d48b3..2c2c0bec2 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -473,7 +473,7 @@ void CheckNullPointer::nullPointerError(const Token *tok, const std::string &var if (!_settings->isEnabled(value, inconclusive)) return; - const ErrorPath errorPath = getErrorPath(tok,value); + const ErrorPath errorPath = getErrorPath(tok, value, "Null pointer dereference"); if (value->condition) { reportError(errorPath, Severity::warning, "nullPointerRedundantCheck", errmsgcond, CWE476, inconclusive || value->inconclusive); diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 25900433f..10efc0d18 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1656,7 +1656,7 @@ void CheckOther::zerodivError(const Token *tok, const ValueFlow::Value *value) return; } - const ErrorPath errorPath = getErrorPath(tok, value); + const ErrorPath errorPath = getErrorPath(tok, value, "Division by zero"); std::ostringstream errmsg; if (value->condition) diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index 088d727f4..cda17e3a0 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -425,6 +425,10 @@ std::string ErrorLogger::ErrorMessage::toString(bool verbose, const std::string << ": " << (verbose ? _verboseMessage : _shortMessage) << " [" << _id << ']'; + + if (_callStack.size() == 1U) + return text.str(); + for (std::list::const_iterator loc = _callStack.begin(); loc != _callStack.end(); ++loc) text << std::endl << loc->getfile()