Refactoring: Disable inconclusive checks. They can still be activated for debugging/testing purposes

This commit is contained in:
Daniel Marjamäki 2010-04-10 14:05:33 +02:00
parent 7763d25847
commit e9b4ea44a2
19 changed files with 94 additions and 104 deletions

View File

@ -64,7 +64,7 @@ void CheckBufferOverrun::arrayIndexOutOfBounds(int size, int index)
if (size <= 1 || _callStack.size() > 1) if (size <= 1 || _callStack.size() > 1)
{ {
severity = Severity::possibleError; severity = Severity::possibleError;
if (_settings->_showAll == false) if (_settings->inconclusive == false)
return; return;
} }
else else
@ -88,7 +88,7 @@ void CheckBufferOverrun::bufferOverrun(const Token *tok, const std::string &varn
if (_callStack.size() > 0) if (_callStack.size() > 0)
{ {
severity = Severity::possibleError; severity = Severity::possibleError;
if (_settings->_showAll == false) if (_settings->inconclusive == false)
return; return;
} }
else else
@ -109,7 +109,7 @@ void CheckBufferOverrun::bufferOverrun(const Token *tok, const std::string &varn
void CheckBufferOverrun::dangerousStdCin(const Token *tok) void CheckBufferOverrun::dangerousStdCin(const Token *tok)
{ {
if (_settings && _settings->_showAll == false) if (_settings && _settings->inconclusive == false)
return; return;
reportError(tok, Severity::possibleError, "dangerousStdCin", "Dangerous usage of std::cin, possible buffer overrun"); reportError(tok, Severity::possibleError, "dangerousStdCin", "Dangerous usage of std::cin, possible buffer overrun");
@ -117,7 +117,7 @@ void CheckBufferOverrun::dangerousStdCin(const Token *tok)
void CheckBufferOverrun::strncatUsage(const Token *tok) void CheckBufferOverrun::strncatUsage(const Token *tok)
{ {
if (_settings && _settings->_showAll == false) if (_settings && _settings->inconclusive == false)
return; return;
reportError(tok, Severity::possibleError, "strncatUsage", "Dangerous usage of strncat. Tip: the 3rd parameter means maximum number of characters to append"); reportError(tok, Severity::possibleError, "strncatUsage", "Dangerous usage of strncat. Tip: the 3rd parameter means maximum number of characters to append");
@ -130,7 +130,7 @@ void CheckBufferOverrun::outOfBounds(const Token *tok, const std::string &what)
void CheckBufferOverrun::sizeArgumentAsChar(const Token *tok) void CheckBufferOverrun::sizeArgumentAsChar(const Token *tok)
{ {
if (_settings && _settings->_showAll == false) if (_settings && _settings->inconclusive == false)
return; return;
reportError(tok, Severity::possibleError, "sizeArgumentAsChar", "The size argument is given as a char constant"); reportError(tok, Severity::possibleError, "sizeArgumentAsChar", "The size argument is given as a char constant");
@ -700,7 +700,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
if (Token::Match(tok, "%var% (")) if (Token::Match(tok, "%var% ("))
{ {
// Only perform this checking if showAll setting is enabled.. // Only perform this checking if showAll setting is enabled..
if (!_settings->_showAll) if (!_settings->inconclusive)
continue; continue;
unsigned int parlevel = 0, par = 0; unsigned int parlevel = 0, par = 0;

View File

@ -481,7 +481,7 @@ void CheckClass::constructors()
} }
} }
if (hasPrivateConstructor && !_settings->_showAll) if (hasPrivateConstructor && !_settings->inconclusive)
{ {
/** @todo Handle private constructors. Right now to avoid /** @todo Handle private constructors. Right now to avoid
* false positives we just bail out */ * false positives we just bail out */
@ -542,7 +542,7 @@ void CheckClass::checkConstructors(const Token *tok1, const std::string &funcnam
const std::string className = tok1->strAt(1); const std::string className = tok1->strAt(1);
// Check that all member variables are initialized.. // Check that all member variables are initialized..
bool withClasses = bool(_settings->_showAll && funcname == "operator ="); bool withClasses = bool(_settings->inconclusive && funcname == "operator =");
Var *varlist = getVarList(tok1, withClasses, isStruct); Var *varlist = getVarList(tok1, withClasses, isStruct);
int indentlevel = 0; int indentlevel = 0;

View File

@ -63,7 +63,7 @@ public:
checkClass.operatorEq(); checkClass.operatorEq();
checkClass.privateFunctions(); checkClass.privateFunctions();
checkClass.operatorEqRetRefThis(); checkClass.operatorEqRetRefThis();
if (settings->_showAll) if (settings->inconclusive)
{ {
checkClass.thisSubtraction(); checkClass.thisSubtraction();
checkClass.operatorEqToSelf(); checkClass.operatorEqToSelf();

View File

@ -603,7 +603,7 @@ const char * CheckMemoryLeakInFunction::call_func(const Token *tok, std::list<co
--parlevel; --parlevel;
if (parlevel < 1) if (parlevel < 1)
{ {
return (_settings && _settings->_showAll) ? 0 : "callfunc"; return (_settings && _settings->inconclusive) ? 0 : "callfunc";
} }
} }
@ -800,7 +800,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
{ {
if (isclass(_tokenizer, tok->tokAt(3))) if (isclass(_tokenizer, tok->tokAt(3)))
{ {
if (_settings->_showAll) if (_settings->inconclusive)
{ {
if (_settings->isAutoDealloc(tok->strAt(3))) if (_settings->isAutoDealloc(tok->strAt(3)))
@ -1380,7 +1380,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all)
} }
// Two "if alloc ;" after one another.. perhaps only one of them can be executed each time // Two "if alloc ;" after one another.. perhaps only one of them can be executed each time
else if (!_settings->_showAll && Token::Match(tok2, "[;{}] if alloc ; if alloc ;")) else if (!_settings->inconclusive && Token::Match(tok2, "[;{}] if alloc ; if alloc ;"))
{ {
Token::eraseTokens(tok2, tok2->tokAt(4)); Token::eraseTokens(tok2, tok2->tokAt(4));
done = false; done = false;
@ -1399,7 +1399,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all)
// Otherwise, only the "if" will be deleted // Otherwise, only the "if" will be deleted
else if (Token::Match(tok2, "[;{}] if assign|dealloc|use ; !!else")) else if (Token::Match(tok2, "[;{}] if assign|dealloc|use ; !!else"))
{ {
if (_settings->_showAll && tok2->tokAt(2)->str() != "assign" && !Token::simpleMatch(tok2->tokAt(2), "dealloc ; dealloc")) if (_settings->inconclusive && tok2->tokAt(2)->str() != "assign" && !Token::simpleMatch(tok2->tokAt(2), "dealloc ; dealloc"))
{ {
Token::eraseTokens(tok2, tok2->tokAt(3)); Token::eraseTokens(tok2, tok2->tokAt(3));
all = true; all = true;
@ -1471,7 +1471,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all)
} }
// Reducing if.. // Reducing if..
else if (_settings->_showAll) else if (_settings->inconclusive)
{ {
if (Token::Match(tok2, "[;{}] if { assign|dealloc|use ; return ; } !!else")) if (Token::Match(tok2, "[;{}] if { assign|dealloc|use ; return ; } !!else"))
{ {
@ -1616,7 +1616,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all)
} }
// Remove the "if break|continue ;" that follows "dealloc ; alloc ;" // Remove the "if break|continue ;" that follows "dealloc ; alloc ;"
if (! _settings->_showAll && Token::Match(tok2, "dealloc ; alloc ; if break|continue ;")) if (! _settings->inconclusive && Token::Match(tok2, "dealloc ; alloc ; if break|continue ;"))
{ {
tok2 = tok2->tokAt(3); tok2 = tok2->tokAt(3);
Token::eraseTokens(tok2, tok2->tokAt(3)); Token::eraseTokens(tok2, tok2->tokAt(3));
@ -1881,7 +1881,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all)
} }
// If "--all" is given, remove all "callfunc".. // If "--all" is given, remove all "callfunc"..
if (done && _settings->_showAll) if (done && _settings->inconclusive)
{ {
for (Token *tok2 = tok; tok2; tok2 = tok2->next()) for (Token *tok2 = tok; tok2; tok2 = tok2->next())
{ {
@ -2047,7 +2047,7 @@ void CheckMemoryLeakInFunction::checkScope(const Token *Tok1, const std::string
return; return;
} }
if ((result = findleak(tok, _settings->_showAll)) != NULL) if ((result = findleak(tok, _settings->inconclusive)) != NULL)
{ {
memoryLeak(result, varname, alloctype, all); memoryLeak(result, varname, alloctype, all);
} }
@ -2293,7 +2293,7 @@ void CheckMemoryLeakInClass::parseClass(const Token *tok1, std::vector<std::stri
if (_settings->isAutoDealloc(tok->str().c_str())) if (_settings->isAutoDealloc(tok->str().c_str()))
continue; continue;
if (_settings->_showAll || !isclass(_tokenizer, tok)) if (_settings->inconclusive || !isclass(_tokenizer, tok))
variable(classname.back(), tok->tokAt(2)); variable(classname.back(), tok->tokAt(2));
} }
} }
@ -2301,7 +2301,7 @@ void CheckMemoryLeakInClass::parseClass(const Token *tok1, std::vector<std::stri
void CheckMemoryLeakInClass::variable(const std::string &classname, const Token *tokVarname) void CheckMemoryLeakInClass::variable(const std::string &classname, const Token *tokVarname)
{ {
if (!_settings->_showAll) if (!_settings->inconclusive)
return; return;
const std::string varname = tokVarname->strAt(0); const std::string varname = tokVarname->strAt(0);

View File

@ -307,7 +307,7 @@ void CheckOther::invalidFunctionUsage()
void CheckOther::checkUnsignedDivision() void CheckOther::checkUnsignedDivision()
{ {
if (!_settings->_showAll || !_settings->_checkCodingStyle) if (!_settings->inconclusive || !_settings->_checkCodingStyle)
return; return;
// Check for "ivar / uvar" and "uvar / ivar" // Check for "ivar / uvar" and "uvar / ivar"
@ -327,16 +327,13 @@ void CheckOther::checkUnsignedDivision()
tok->tokAt(1)->varId() != 0 && tok->tokAt(1)->varId() != 0 &&
tok->tokAt(3)->varId() != 0) tok->tokAt(3)->varId() != 0)
{ {
if (ErrorLogger::udivWarning(*_settings)) char sign1 = varsign[tok->tokAt(1)->varId()];
{ char sign2 = varsign[tok->tokAt(3)->varId()];
char sign1 = varsign[tok->tokAt(1)->varId()];
char sign2 = varsign[tok->tokAt(3)->varId()];
if (sign1 && sign2 && sign1 != sign2) if (sign1 && sign2 && sign1 != sign2)
{ {
// One of the operands are signed, the other is unsigned.. // One of the operands are signed, the other is unsigned..
udivWarning(tok->next()); udivWarning(tok->next());
}
} }
} }
@ -2499,7 +2496,7 @@ void CheckOther::executionPaths()
{ {
// no writing if multiple threads are used (TODO: thread safe analysis?) // no writing if multiple threads are used (TODO: thread safe analysis?)
if (_settings->_jobs == 1) if (_settings->_jobs == 1)
CheckUninitVar::analyseFunctions(_tokenizer->tokens(), CheckUninitVar::uvarFunctions, _settings->_showAll); CheckUninitVar::analyseFunctions(_tokenizer->tokens(), CheckUninitVar::uvarFunctions, _settings->inconclusive);
CheckUninitVar c(this); CheckUninitVar c(this);
checkExecutionPaths(_tokenizer->tokens(), &c); checkExecutionPaths(_tokenizer->tokens(), &c);

View File

@ -73,7 +73,7 @@ public:
checkOther.checkConstantFunctionParameter(); checkOther.checkConstantFunctionParameter();
checkOther.checkIncompleteStatement(); checkOther.checkIncompleteStatement();
checkOther.unreachableCode(); checkOther.unreachableCode();
if (settings->_showAll) if (settings->inconclusive)
{ {
checkOther.postIncrement(); checkOther.postIncrement();
} }

View File

@ -55,10 +55,9 @@ public:
checkStl.stlBoundries(); checkStl.stlBoundries();
checkStl.if_find(); checkStl.if_find();
if (settings->_checkCodingStyle) if (settings->_checkCodingStyle && settings->inconclusive)
{ {
if (settings->_showAll) checkStl.size();
checkStl.size();
} }
} }

View File

@ -117,23 +117,6 @@ public:
*/ */
virtual void reportStatus(unsigned int index, unsigned int max) = 0; virtual void reportStatus(unsigned int index, unsigned int max) = 0;
static bool arrayIndexOutOfBounds(const Settings &s)
{
return s._showAll;
}
static bool bufferOverrun(const Settings &s)
{
return s._showAll;
}
static bool strncatUsage(const Settings &s)
{
return s._showAll;
}
static bool outOfBounds() static bool outOfBounds()
{ {
return true; return true;
@ -189,11 +172,6 @@ public:
reportErr(ErrorLogger::ErrorMessage(loc, "style", "The function '" + funcname + "' is never used", "unusedFunction")); reportErr(ErrorLogger::ErrorMessage(loc, "style", "The function '" + funcname + "' is never used", "unusedFunction"));
} }
static bool unusedFunction(const Settings &s)
{
return s._checkCodingStyle || s._showAll;
}
static bool mismatchAllocDealloc() static bool mismatchAllocDealloc()
{ {
@ -205,12 +183,6 @@ public:
return true; return true;
} }
static bool memleakall(const Settings &s)
{
return s._showAll;
}
static bool resourceLeak() static bool resourceLeak()
{ {
return true; return true;
@ -270,12 +242,6 @@ public:
} }
static bool udivWarning(const Settings &s)
{
return s._checkCodingStyle || s._showAll;
}
static bool unusedStructMember(const Settings &s) static bool unusedStructMember(const Settings &s)
{ {
return s._checkCodingStyle; return s._checkCodingStyle;

40
lib/settings.cpp Executable file → Normal file
View File

@ -24,9 +24,9 @@
#include <stdexcept> #include <stdexcept>
Settings::Settings() Settings::Settings()
: inconclusive(false)
{ {
_debug = false; _debug = false;
_showAll = false;
_checkCodingStyle = false; _checkCodingStyle = false;
_errorsOnly = false; _errorsOnly = false;
_inlineSuppressions = false; _inlineSuppressions = false;
@ -40,6 +40,40 @@ Settings::Settings()
_terminate = false; _terminate = false;
} }
Settings::Settings(const Settings &s)
: inconclusive(s.inconclusive)
{
*this = s;
}
// Constructor used in unit testing..
Settings::Settings(bool all)
: inconclusive(all)
{
Settings s;
*this = s; // This assigns all members except "inconclusive"
}
const Settings &Settings::operator=(const Settings & s)
{
if (&s == this)
return *this;
_debug = s._debug;
_checkCodingStyle = s._checkCodingStyle;
_errorsOnly = s._errorsOnly;
_inlineSuppressions = s._inlineSuppressions;
_verbose = s._verbose;
_force = s._force;
_xml = s._xml;
_jobs = s._jobs;
_exitCode = s._exitCode;
_showtime = s._showtime;
_append = s._append;
_terminate = s._terminate;
return *this;
}
Settings::~Settings() Settings::~Settings()
{ {
@ -140,11 +174,9 @@ void Settings::addEnabled(const std::string &str)
bool handled = false; bool handled = false;
if (str == "all") if (str == "all")
handled = _checkCodingStyle = _showAll = true; handled = _checkCodingStyle = true;
else if (str == "style") else if (str == "style")
handled = _checkCodingStyle = true; handled = _checkCodingStyle = true;
else if (str == "possibleError")
handled = _showAll = true;
std::set<std::string> id; std::set<std::string> id;
id.insert("exceptNew"); id.insert("exceptNew");

View File

@ -48,15 +48,26 @@ private:
/** @brief terminate checking */ /** @brief terminate checking */
bool _terminate; bool _terminate;
Settings(bool all);
public: public:
Settings(); Settings();
Settings(const Settings &s);
const Settings &operator=(const Settings &s);
virtual ~Settings(); virtual ~Settings();
/** @brief Return test settings where inconclusive is true */
static Settings testSettings()
{
return Settings(true);
}
/** @brief Is --debug given? */ /** @brief Is --debug given? */
bool _debug; bool _debug;
/** @brief Is --all given? */ /** @brief Inconclusive checks - for debugging of Cppcheck */
bool _showAll; const bool inconclusive;
/** @brief Is --style given? */ /** @brief Is --style given? */
bool _checkCodingStyle; bool _checkCodingStyle;

View File

@ -53,8 +53,7 @@ private:
errout.str(""); errout.str("");
// Check for buffer overruns.. // Check for buffer overruns..
Settings settings; Settings settings(showAll ? Settings::testSettings() : Settings());
settings._showAll = showAll;
settings._checkCodingStyle = true; settings._checkCodingStyle = true;
CheckBufferOverrun checkBufferOverrun(&tokenizer, &settings, this); CheckBufferOverrun checkBufferOverrun(&tokenizer, &settings, this);
checkBufferOverrun.bufferOverrun(); checkBufferOverrun.bufferOverrun();

View File

@ -417,9 +417,8 @@ private:
errout.str(""); errout.str("");
// Check.. // Check..
Settings settings; Settings settings(Settings::testSettings());
settings._checkCodingStyle = true; settings._checkCodingStyle = true;
settings._showAll = true;
CheckClass checkClass(&tokenizer, &settings, this); CheckClass checkClass(&tokenizer, &settings, this);
checkClass.operatorEqToSelf(); checkClass.operatorEqToSelf();
} }
@ -1392,8 +1391,7 @@ private:
errout.str(""); errout.str("");
// Check.. // Check..
Settings settings; Settings settings(Settings::testSettings());
settings._showAll = true;
CheckClass checkClass(&tokenizer, &settings, this); CheckClass checkClass(&tokenizer, &settings, this);
checkClass.constructors(); checkClass.constructors();
} }
@ -2108,9 +2106,8 @@ private:
errout.str(""); errout.str("");
// Check.. // Check..
Settings settings; Settings settings(Settings::testSettings());
settings._checkCodingStyle = true; settings._checkCodingStyle = true;
settings._showAll = true;
CheckClass checkClass(&tokenizer, &settings, this); CheckClass checkClass(&tokenizer, &settings, this);
checkClass.thisSubtraction(); checkClass.thisSubtraction();
} }

View File

@ -46,9 +46,8 @@ private:
errout.str(""); errout.str("");
// Check class constructors.. // Check class constructors..
Settings settings; Settings settings(showAll ? Settings::testSettings() : Settings());
settings._checkCodingStyle = true; settings._checkCodingStyle = true;
settings._showAll = showAll;
CheckClass checkClass(&tokenizer, &settings, this); CheckClass checkClass(&tokenizer, &settings, this);
checkClass.constructors(); checkClass.constructors();
} }

View File

@ -53,8 +53,7 @@ private:
errout.str(""); errout.str("");
// Check for dangerous functions.. // Check for dangerous functions..
Settings settings; Settings settings(Settings::testSettings());
settings._showAll = true;
CheckDangerousFunctions checkDangerousFunctions(&tokenizer, &settings, this); CheckDangerousFunctions checkDangerousFunctions(&tokenizer, &settings, this);
checkDangerousFunctions.dangerousFunctions(); checkDangerousFunctions.dangerousFunctions();
} }

View File

@ -46,8 +46,7 @@ private:
// Clear the error buffer.. // Clear the error buffer..
errout.str(""); errout.str("");
Settings settings; Settings settings(all ? Settings::testSettings() : Settings());
settings._showAll = all;
settings._checkCodingStyle = style; settings._checkCodingStyle = style;
// Check for unsigned divisions.. // Check for unsigned divisions..

View File

@ -47,8 +47,7 @@ private:
// Clear the error buffer.. // Clear the error buffer..
errout.str(""); errout.str("");
Settings settings; Settings settings(Settings::testSettings());
settings._showAll = true;
// Check for unused variables.. // Check for unused variables..
CheckOther checkOther(&tokenizer, &settings, this); CheckOther checkOther(&tokenizer, &settings, this);

View File

@ -212,9 +212,8 @@ private:
errout.str(""); errout.str("");
// Check for memory leaks.. // Check for memory leaks..
Settings settings; Settings settings(showAll ? Settings::testSettings() : Settings());
settings._debug = true; settings._debug = true;
settings._showAll = showAll;
tokenizer.fillFunctionList(); tokenizer.fillFunctionList();
CheckMemoryLeakInFunction checkMemoryLeak(&tokenizer, &settings, this); CheckMemoryLeakInFunction checkMemoryLeak(&tokenizer, &settings, this);
checkMemoryLeak.check(); checkMemoryLeak.check();
@ -601,8 +600,7 @@ private:
} }
} }
Settings settings; Settings settings(all ? Settings::testSettings() : Settings());
settings._showAll = all;
CheckMemoryLeakInFunction checkMemoryLeak(&tokenizer, &settings, NULL); CheckMemoryLeakInFunction checkMemoryLeak(&tokenizer, &settings, NULL);
all = false; all = false;
checkMemoryLeak.simplifycode(tokens, all); checkMemoryLeak.simplifycode(tokens, all);
@ -2143,9 +2141,8 @@ private:
errout.str(""); errout.str("");
// Check for memory leaks.. // Check for memory leaks..
Settings settings; Settings settings(Settings::testSettings());
settings._debug = true; settings._debug = true;
settings._showAll = true;
{ {
std::istringstream istr(_autoDealloc); std::istringstream istr(_autoDealloc);
@ -2715,9 +2712,8 @@ private:
errout.str(""); errout.str("");
// Check for memory leaks.. // Check for memory leaks..
Settings settings; Settings settings(showAll ? Settings::testSettings() : Settings());
settings._debug = true; settings._debug = true;
settings._showAll = showAll;
tokenizer.fillFunctionList(); tokenizer.fillFunctionList();
CheckMemoryLeakInClass checkMemoryLeak(&tokenizer, &settings, this); CheckMemoryLeakInClass checkMemoryLeak(&tokenizer, &settings, this);
checkMemoryLeak.check(); checkMemoryLeak.check();

View File

@ -3435,8 +3435,7 @@ private:
void checkSimplifyTypedef(const char code[]) void checkSimplifyTypedef(const char code[])
{ {
// Tokenize.. // Tokenize..
Settings settings; Settings settings(Settings::testSettings());
settings._showAll = true;
settings._checkCodingStyle = true; settings._checkCodingStyle = true;
Tokenizer tokenizer(&settings, this); Tokenizer tokenizer(&settings, this);
std::istringstream istr(code); std::istringstream istr(code);
@ -4046,8 +4045,7 @@ private:
void checkSimplifyEnum(const char code[]) void checkSimplifyEnum(const char code[])
{ {
// Tokenize.. // Tokenize..
Settings settings; Settings settings(Settings::testSettings());
settings._showAll = true;
settings._checkCodingStyle = true; settings._checkCodingStyle = true;
Tokenizer tokenizer(&settings, this); Tokenizer tokenizer(&settings, this);
std::istringstream istr(code); std::istringstream istr(code);

View File

@ -91,9 +91,8 @@ private:
errout.str(""); errout.str("");
// Check.. // Check..
Settings settings; Settings settings(Settings::testSettings());
settings._checkCodingStyle = true; settings._checkCodingStyle = true;
settings._showAll = true;
CheckStl checkStl; CheckStl checkStl;
checkStl.runSimplifiedChecks(&tokenizer, &settings, this); checkStl.runSimplifiedChecks(&tokenizer, &settings, this);
} }