Refactor and rename CHECK_WRONG_DATA

This commit is contained in:
Daniel Marjamäki 2017-06-05 18:41:15 +02:00
parent fc79941828
commit 150d2aa902
3 changed files with 23 additions and 14 deletions

View File

@ -41,6 +41,19 @@ void Check::reportError(const ErrorLogger::ErrorMessage &errmsg)
std::cout << errmsg.toXML(true, 1) << std::endl; 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 *> &Check::instances() std::list<Check *> &Check::instances()
{ {
#ifdef __SVR4 #ifdef __SVR4

View File

@ -31,22 +31,13 @@
#include <list> #include <list>
#include <string> #include <string>
/**
* 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 { namespace tinyxml2 {
class XMLElement; 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 /// @addtogroup Core
/// @{ /// @{
@ -187,6 +178,11 @@ protected:
return errorPath; 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: private:
const std::string _name; const std::string _name;

View File

@ -196,7 +196,7 @@ void CheckFunctions::checkIgnoredReturnValue()
continue; 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()); 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()) { for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
if (Token::Match(tok, "memset|wmemset (") && (numberOfArguments(tok)==3)) { if (Token::Match(tok, "memset|wmemset (") && (numberOfArguments(tok)==3)) {
const std::vector<const Token *> &arguments = getArguments(tok); const std::vector<const Token *> &arguments = getArguments(tok);
if (!CHECK_WRONG_DATA(arguments.size() == 3U, tok)) if (WRONG_DATA(arguments.size() == 3U, tok))
continue; continue;
const Token* lastParamTok = arguments[2]; const Token* lastParamTok = arguments[2];
if (lastParamTok->str() == "0") if (lastParamTok->str() == "0")