refactoring error messages

This commit is contained in:
Daniel Marjamäki 2009-03-21 21:33:27 +01:00
parent 262d182f26
commit 6fc66d3e2c
7 changed files with 73 additions and 59 deletions

View File

@ -72,15 +72,26 @@ protected:
/** report an error */
void reportError(const Token *tok, const std::string severity, const std::string id, const std::string msg)
{
ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = tok->linenr();
loc.file = _tokenizer->file(tok);
std::list<const Token *> callstack;
callstack.push_back(tok);
reportError(callstack, severity, id, msg);
}
/** report an error */
void reportError(const std::list<const Token *> callstack, const std::string severity, const std::string id, const std::string msg)
{
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
for (std::list<const Token *>::const_iterator it = callstack.begin(); it != callstack.end(); ++it)
{
ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = (*it)->linenr();
loc.file = _tokenizer->file(*it);
locationList.push_back(loc);
}
_errorLogger->reportErr(ErrorLogger::ErrorMessage(locationList, severity, msg, id));
}
};
#endif

View File

@ -189,22 +189,25 @@ void CheckAutoVariables::autoVariables()
else if (bindent > 0 && Token::Match(tok, "%var% = & %var%")) //Critical assignement
{
if (error_av(tok->tokAt(0), tok->tokAt(3)))
_errorLogger->genericError(_tokenizer,
tok,
reportError(tok,
"error",
"autoVariables",
"Wrong assignement of an auto-variable to an effective parameter of a function");
}
else if (bindent > 0 && Token::Match(tok, "%var% [ %any% ] = & %var%")) //Critical assignement
{
if (error_av(tok->tokAt(0), tok->tokAt(6)))
_errorLogger->genericError(_tokenizer,
tok,
reportError(tok,
"error",
"autoVariables",
"Wrong assignement of an auto-variable to an effective parameter of a function");
}
else if (bindent > 0 && Token::Match(tok, "return & %var%")) //Critical return
{
if (is_auto_var(tok->tokAt(2)))
_errorLogger->genericError(_tokenizer,
tok,
reportError(tok,
"error",
"autoVariables",
"Return of the address of an auto-variable");
}
}

View File

@ -47,9 +47,30 @@ CheckBufferOverrunClass instance;
void CheckBufferOverrunClass::arrayIndexOutOfBounds(const Token *tok)
{
_callStack.push_back(tok);
_errorLogger->arrayIndexOutOfBounds(_tokenizer, _callStack);
arrayIndexOutOfBounds();
_callStack.pop_back();
}
void CheckBufferOverrunClass::arrayIndexOutOfBounds()
{
reportError(_callStack, "all", "arrayIndexOutOfBounds", "Array index out of bounds");
}
void CheckBufferOverrunClass::bufferOverrun(const Token *tok)
{
reportError(tok, "all", "bufferOverrun", "Buffer overrun");
}
void CheckBufferOverrunClass::strncatUsage(const Token *tok)
{
reportError(tok, "all", "strncatUsage", "Dangerous usage of strncat, possible buffer overrun");
}
void CheckBufferOverrunClass::outOfBounds(const Token *tok, const std::string &what)
{
reportError(tok, "error", "outOfBounds", what + " is out of bounds");
}
//---------------------------------------------------------------------------
@ -148,7 +169,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, co
const char *num = tok->strAt(6);
if (std::atoi(num) > total_size)
{
_errorLogger->bufferOverrun(_tokenizer, tok);
bufferOverrun(tok);
}
}
}
@ -161,7 +182,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, co
const char *num = tok->strAt(varc + 6);
if (std::atoi(num) > total_size)
{
_errorLogger->bufferOverrun(_tokenizer, tok);
bufferOverrun(tok);
}
}
continue;
@ -220,7 +241,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, co
if (Token::Match(tok2, pattern.str().c_str()))
{
_errorLogger->bufferOverrun(_tokenizer, tok2);
bufferOverrun(tok2);
break;
}
@ -243,7 +264,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, co
}
if (len > 2 && len >= (int)size + 2)
{
_errorLogger->bufferOverrun(_tokenizer, tok);
bufferOverrun(tok);
}
continue;
}
@ -254,7 +275,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, co
{
int n = atoi(tok->strAt(6));
if (n == size)
_errorLogger->strncatUsage(_tokenizer, tok);
strncatUsage(tok);
}
@ -263,7 +284,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, co
{
int n = atoi(tok->strAt(6)) + atoi(tok->strAt(15));
if (n > size)
_errorLogger->strncatUsage(_tokenizer, tok->tokAt(9));
strncatUsage(tok->tokAt(9));
}
@ -288,7 +309,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, co
}
if (len > (int)size)
{
_errorLogger->bufferOverrun(_tokenizer, tok);
bufferOverrun(tok);
}
}
@ -297,13 +318,13 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, co
{
int n = std::atoi(tok->strAt(4));
if (n > size)
_errorLogger->outOfBounds(_tokenizer, tok->tokAt(4), "snprintf size");
outOfBounds(tok->tokAt(4), "snprintf size");
}
// cin..
if (varid > 0 && Token::Match(tok, "cin >> %varid% ;", varid))
{
_errorLogger->bufferOverrun(_tokenizer, tok);
bufferOverrun(tok);
}
// Function call..
@ -589,6 +610,3 @@ void CheckBufferOverrunClass::bufferOverrun()

View File

@ -68,6 +68,10 @@ private:
std::list<const Token *> _callStack;
void arrayIndexOutOfBounds(const Token *tok);
void arrayIndexOutOfBounds();
void bufferOverrun(const Token *tok);
void strncatUsage(const Token *tok);
void outOfBounds(const Token *tok, const std::string &what);
};
//---------------------------------------------------------------------------

View File

@ -454,7 +454,7 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
if (alloc != Many && dealloctype != No && dealloctype != Many && dealloctype != alloc)
{
callstack.push_back(tok);
_errorLogger->mismatchAllocDealloc(_tokenizer, callstack, varname);
mismatchAllocDealloc(callstack, varname);
callstack.pop_back();
}
@ -501,7 +501,7 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
if (dealloc != Many && alloctype != No && alloctype != Many && alloctype != dealloc)
{
callstack.push_back(tok);
_errorLogger->mismatchAllocDealloc(_tokenizer, callstack, varname);
mismatchAllocDealloc(callstack, varname);
callstack.pop_back();
}
dealloctype = dealloc;
@ -1499,7 +1499,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_Variable(const char clas
if (alloc != Many && Dealloc != No && Dealloc != Many && Dealloc != alloc)
{
callstack.push_back(tok);
_errorLogger->mismatchAllocDealloc(_tokenizer, callstack, (std::string(classname) + "::" + varname).c_str());
mismatchAllocDealloc(callstack, (std::string(classname) + "::" + varname).c_str());
callstack.pop_back();
}
@ -1529,7 +1529,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_Variable(const char clas
if (dealloc != Many && Alloc != No && Alloc != Many && Alloc != dealloc)
{
callstack.push_back(tok);
_errorLogger->mismatchAllocDealloc(_tokenizer, callstack, (std::string(classname) + "::" + varname).c_str());
mismatchAllocDealloc(callstack, (std::string(classname) + "::" + varname).c_str());
callstack.pop_back();
}
@ -1648,3 +1648,7 @@ void CheckMemoryLeakClass::mismatchSizeError(const Token *tok, const std::string
reportError(tok, "error", "mismatchSize", "The given size " + sz + " is mismatching");
}
void CheckMemoryLeakClass::mismatchAllocDealloc(const std::list<const Token *> &callstack, const std::string &varname)
{
reportError(callstack, "error", "mismatchAllocDealloc", "Mismatching allocation and deallocation: " + varname);
}

View File

@ -131,7 +131,7 @@ private:
void deallocDeallocError(const Token *tok, const std::string &varname);
void deallocuseError(const Token *tok, const std::string &varname);
void mismatchSizeError(const Token *tok, const std::string &sz);
void mismatchAllocDealloc(const std::list<const Token *> &callstack, const std::string &varname);
// Experimental functionality..
protected:

View File

@ -95,46 +95,23 @@ public:
*/
virtual void reportStatus(unsigned int index, unsigned int max) = 0;
void genericError(const Tokenizer *tokenizer, const Token *Location, const std::string &msg)
{
_writemsg(tokenizer, Location, "error", "" + msg + "", "genericError");
}
static bool genericError()
{
return true;
}
void arrayIndexOutOfBounds(const Tokenizer *tokenizer, const std::list<const Token *> &Location)
{
_writemsg(tokenizer, Location, "all", "Array index out of bounds", "arrayIndexOutOfBounds");
}
static bool arrayIndexOutOfBounds(const Settings &s)
{
return s._showAll;
}
void bufferOverrun(const Tokenizer *tokenizer, const Token *Location)
{
_writemsg(tokenizer, Location, "all", "Buffer overrun", "bufferOverrun");
}
static bool bufferOverrun(const Settings &s)
{
return s._showAll;
}
void strncatUsage(const Tokenizer *tokenizer, const Token *Location)
{
_writemsg(tokenizer, Location, "all", "Dangerous usage of strncat, possible buffer overrun", "strncatUsage");
}
static bool strncatUsage(const Settings &s)
{
return s._showAll;
}
void outOfBounds(const Tokenizer *tokenizer, const Token *Location, const std::string &what)
{
_writemsg(tokenizer, Location, "error", "" + what + " is out of bounds", "outOfBounds");
}
static bool outOfBounds()
{
return true;
@ -189,10 +166,7 @@ public:
return s._checkCodingStyle || s._showAll;
}
void mismatchAllocDealloc(const Tokenizer *tokenizer, const std::list<const Token *> &Location, const std::string &varname)
{
_writemsg(tokenizer, Location, "error", "Mismatching allocation and deallocation: " + varname + "", "mismatchAllocDealloc");
}
static bool mismatchAllocDealloc()
{
return true;