From 150d2aa9028072ab3e883b6f4e3cd2f392c24566 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 5 Jun 2017 18:41:15 +0200 Subject: [PATCH] Refactor and rename CHECK_WRONG_DATA --- lib/check.cpp | 13 +++++++++++++ lib/check.h | 20 ++++++++------------ lib/checkfunctions.cpp | 4 ++-- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/lib/check.cpp b/lib/check.cpp index b10da7948..77cb21fde 100644 --- a/lib/check.cpp +++ b/lib/check.cpp @@ -41,6 +41,19 @@ void Check::reportError(const ErrorLogger::ErrorMessage &errmsg) std::cout << errmsg.toXML(true, 1) << std::endl; } +bool Check::wrongData(const Token *tok, bool condition, const char *str) +{ +#if defined(DACA2) || defined(UNSTABLE) + if (condition) { + reportError(tok, Severity::debug, "DacaWrongData", "Wrong data detected by condition " + std::string(str)); + } +#else + (void)tok; + (void)str; +#endif + return condition; +} + std::list &Check::instances() { #ifdef __SVR4 diff --git a/lib/check.h b/lib/check.h index 4de3a16b4..feade5e59 100644 --- a/lib/check.h +++ b/lib/check.h @@ -31,22 +31,13 @@ #include #include -/** - * When -DDACA2 is used, Cppcheck will print error messages when wrong - * data is seen. Intended to be used in Daca2 - * - * Use CHECK_WRONG_DATA in checkers when you check for wrong data. - */ -#if defined(DACA2) || defined(UNSTABLE) -#define CHECK_WRONG_DATA(COND, TOK) ({ if(!(COND)) reportError(TOK,Severity::debug,"DacaWrongData","Wrong data detected, " #COND); (COND);}) -#else -#define CHECK_WRONG_DATA(COND, TOK) (COND) -#endif - namespace tinyxml2 { class XMLElement; } +/** Use WRONG_DATA in checkers to mark conditions that check that data is correct */ +#define WRONG_DATA(COND, TOK) (wrongData((TOK), (COND), #COND)) + /// @addtogroup Core /// @{ @@ -187,6 +178,11 @@ protected: return errorPath; } + /** + * Use WRONG_DATA in checkers when you check for wrong data. That + * will call this method + */ + bool wrongData(const Token *tok, bool condition, const char *str); private: const std::string _name; diff --git a/lib/checkfunctions.cpp b/lib/checkfunctions.cpp index 9d9005a59..d3f60d55b 100644 --- a/lib/checkfunctions.cpp +++ b/lib/checkfunctions.cpp @@ -196,7 +196,7 @@ void CheckFunctions::checkIgnoredReturnValue() continue; } - if ((!tok->function() || !Token::Match(tok->function()->retDef, "void %name%")) && _settings->library.isUseRetVal(tok) && CHECK_WRONG_DATA(tok->next()->astOperand1(), tok)) + if ((!tok->function() || !Token::Match(tok->function()->retDef, "void %name%")) && _settings->library.isUseRetVal(tok) && !WRONG_DATA(tok->next()->astOperand1(), tok)) ignoredReturnValueError(tok, tok->next()->astOperand1()->expressionString()); } } @@ -323,7 +323,7 @@ void CheckFunctions::memsetZeroBytes() for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) { if (Token::Match(tok, "memset|wmemset (") && (numberOfArguments(tok)==3)) { const std::vector &arguments = getArguments(tok); - if (!CHECK_WRONG_DATA(arguments.size() == 3U, tok)) + if (WRONG_DATA(arguments.size() == 3U, tok)) continue; const Token* lastParamTok = arguments[2]; if (lastParamTok->str() == "0")