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::Pipe ||
alloctype == CheckMemoryLeak::Fd || alloctype == CheckMemoryLeak::Fd ||
alloctype == CheckMemoryLeak::Dir) alloctype == CheckMemoryLeak::Dir)
resourceLeakError(tok, varname.c_str()); resourceLeakError(tok, varname.c_str(), all);
else if (all)
memleakallError(tok, varname.c_str());
else 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; 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); reportErr(tok, all ? Severity::possibleError : Severity::error, "resourceLeak", "Resource leak: " + varname);
}
void CheckMemoryLeak::resourceLeakError(const Token *tok, const std::string &varname)
{
reportErr(tok, Severity::error, "resourceLeak", "Resource leak: " + varname);
} }
void CheckMemoryLeak::deallocDeallocError(const Token *tok, const std::string &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; bool isclass(const Tokenizer *_tokenizer, const Token *typestr) const;
void memleakError(const Token *tok, const std::string &varname); void memleakError(const Token *tok, const std::string &varname, bool all);
void memleakallError(const Token *tok, const std::string &varname); void resourceLeakError(const Token *tok, const std::string &varname, bool all);
void resourceLeakError(const Token *tok, const std::string &varname);
void deallocDeallocError(const Token *tok, const std::string &varname); void deallocDeallocError(const Token *tok, const std::string &varname);
void deallocuseError(const Token *tok, const std::string &varname); void deallocuseError(const Token *tok, const std::string &varname);
@ -252,9 +251,8 @@ private:
void getErrorMessages() void getErrorMessages()
{ {
memleakError(0, "varname"); memleakError(0, "varname", false);
memleakallError(0, "varname"); resourceLeakError(0, "varname", false);
resourceLeakError(0, "varname");
deallocDeallocError(0, "varname"); deallocDeallocError(0, "varname");
deallocuseError(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.. // Tokenize..
Tokenizer tokenizer; Tokenizer tokenizer;
@ -432,8 +432,20 @@ private:
return ret.str(); 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 // Test that the CheckMemoryLeaksInFunction::simplifycode works
void simplifycode() void simplifycode()
@ -453,6 +465,8 @@ private:
ASSERT_EQUALS("; use ;", simplifycode("; if(var) use ;")); ASSERT_EQUALS("; use ;", simplifycode("; if(var) use ;"));
ASSERT_EQUALS("; alloc ; dealloc ;\n; alloc ;", simplifycode("; alloc ; if(!var) { return ; } if { dealloc ; }"));
// "if ; .." // "if ; .."
ASSERT_EQUALS("; if xxx ;", simplifycode("; if ; else xxx ;")); ASSERT_EQUALS("; if xxx ;", simplifycode("; if ; else xxx ;"));
ASSERT_EQUALS("; if(var) xxx ;", simplifycode("; if(!var) ; 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 ;")); ASSERT_EQUALS("; exit ;", simplifycode("; alloc ; do { } loop ; exit ;"));
// callfunc.. // callfunc..
ASSERT_EQUALS("; callfunc ;", simplifycode(";callfunc;", false)); ASSERT_EQUALS("; callfunc ;\n;", simplifycode(";callfunc;"));
ASSERT_EQUALS(";", simplifycode(";callfunc;", true));
// exit.. // exit..
ASSERT_EQUALS("; exit ;", simplifycode("; alloc; 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 { use; exit; }"));
ASSERT_EQUALS("; alloc ;", simplifycode("; alloc ; if(!var) { exit; }")); ASSERT_EQUALS("; alloc ;", simplifycode("; alloc ; if(!var) { exit; }"));
TODO_ASSERT_EQUALS(";", simplifycode("; alloc ; if(var) { exit; }")); TODO_ASSERT_EQUALS(";", simplifycode("; alloc ; if(var) { exit; }"));
TODO_ASSERT_EQUALS(";", simplifycode("; alloc ; ifv { exit; }", false)); TODO_ASSERT_EQUALS(";\n; alloc ;", simplifycode("; alloc ; ifv { exit; }"));
TODO_ASSERT_EQUALS("; alloc ;", simplifycode("; alloc ; ifv { exit; }", true));
} }