Fixed #688 (False positive in error category when --all is used)

This commit is contained in:
Daniel Marjamäki 2009-09-19 08:23:10 +02:00
parent 1c1ffa63f3
commit 8f96c1992a
3 changed files with 27 additions and 24 deletions

View File

@ -281,11 +281,9 @@ void CheckMemoryLeak::memoryLeak(const Token *tok, const std::string &varname, A
alloctype == CheckMemoryLeak::Pipe ||
alloctype == CheckMemoryLeak::Fd ||
alloctype == CheckMemoryLeak::Dir)
resourceLeakError(tok, varname.c_str());
else if (all)
memleakallError(tok, varname.c_str());
resourceLeakError(tok, varname.c_str(), all);
else
memleakError(tok, varname.c_str());
memleakError(tok, varname.c_str(), all);
}
//---------------------------------------------------------------------------
@ -321,19 +319,14 @@ void CheckMemoryLeak::reportErr(const std::list<const Token *> &callstack, Sever
std::cout << errmsg.toXML() << std::endl;
}
void CheckMemoryLeak::memleakError(const Token *tok, const std::string &varname)
void CheckMemoryLeak::memleakError(const Token *tok, const std::string &varname, bool all)
{
reportErr(tok, Severity::error, "memleak", "Memory leak: " + varname);
reportErr(tok, all ? Severity::possibleError : Severity::error, "memleak", "Memory leak: " + varname);
}
void CheckMemoryLeak::memleakallError(const Token *tok, const std::string &varname)
void CheckMemoryLeak::resourceLeakError(const Token *tok, const std::string &varname, bool all)
{
reportErr(tok, Severity::possibleError, "memleakall", "Memory leak: " + varname);
}
void CheckMemoryLeak::resourceLeakError(const Token *tok, const std::string &varname)
{
reportErr(tok, Severity::error, "resourceLeak", "Resource leak: " + varname);
reportErr(tok, all ? Severity::possibleError : Severity::error, "resourceLeak", "Resource leak: " + varname);
}
void CheckMemoryLeak::deallocDeallocError(const Token *tok, const std::string &varname)

View File

@ -117,9 +117,8 @@ public:
bool isclass(const Tokenizer *_tokenizer, const Token *typestr) const;
void memleakError(const Token *tok, const std::string &varname);
void memleakallError(const Token *tok, const std::string &varname);
void resourceLeakError(const Token *tok, const std::string &varname);
void memleakError(const Token *tok, const std::string &varname, bool all);
void resourceLeakError(const Token *tok, const std::string &varname, bool all);
void deallocDeallocError(const Token *tok, const std::string &varname);
void deallocuseError(const Token *tok, const std::string &varname);
@ -252,9 +251,8 @@ private:
void getErrorMessages()
{
memleakError(0, "varname");
memleakallError(0, "varname");
resourceLeakError(0, "varname");
memleakError(0, "varname", false);
resourceLeakError(0, "varname", false);
deallocDeallocError(0, "varname");
deallocuseError(0, "varname");

View File

@ -396,7 +396,7 @@ private:
std::string simplifycode(const char code[], bool all = false) const
std::string simplifycode(const char code[], bool &all) const
{
// Tokenize..
Tokenizer tokenizer;
@ -432,8 +432,20 @@ private:
return ret.str();
}
std::string simplifycode(const char code[])
{
bool all = false;
const std::string str1 = simplifycode(code, all);
ASSERT_EQUALS(0, all ? 1 : 0);
all = true;
const std::string str2 = simplifycode(code, all);
ASSERT_EQUALS(all ? 1 : 0, (str1 != str2) ? 1 : 0);
return all ? (str1 + "\n" + str2) : str1;
}
// Test that the CheckMemoryLeaksInFunction::simplifycode works
void simplifycode()
@ -453,6 +465,8 @@ private:
ASSERT_EQUALS("; use ;", simplifycode("; if(var) use ;"));
ASSERT_EQUALS("; alloc ; dealloc ;\n; alloc ;", simplifycode("; alloc ; if(!var) { return ; } if { dealloc ; }"));
// "if ; .."
ASSERT_EQUALS("; if xxx ;", simplifycode("; if ; else xxx ;"));
ASSERT_EQUALS("; if(var) xxx ;", simplifycode("; if(!var) ; else xxx ;"));
@ -472,8 +486,7 @@ private:
ASSERT_EQUALS("; exit ;", simplifycode("; alloc ; do { } loop ; exit ;"));
// callfunc..
ASSERT_EQUALS("; callfunc ;", simplifycode(";callfunc;", false));
ASSERT_EQUALS(";", simplifycode(";callfunc;", true));
ASSERT_EQUALS("; callfunc ;\n;", simplifycode(";callfunc;"));
// exit..
ASSERT_EQUALS("; exit ;", simplifycode("; alloc; exit;"));
@ -481,8 +494,7 @@ private:
ASSERT_EQUALS("; alloc ;", simplifycode("; alloc ; if { use; exit; }"));
ASSERT_EQUALS("; alloc ;", simplifycode("; alloc ; if(!var) { exit; }"));
TODO_ASSERT_EQUALS(";", simplifycode("; alloc ; if(var) { exit; }"));
TODO_ASSERT_EQUALS(";", simplifycode("; alloc ; ifv { exit; }", false));
TODO_ASSERT_EQUALS("; alloc ;", simplifycode("; alloc ; ifv { exit; }", true));
TODO_ASSERT_EQUALS(";\n; alloc ;", simplifycode("; alloc ; ifv { exit; }"));
}