refactoring: renamed the severity "all" to "possible error"

This commit is contained in:
Daniel Marjamäki 2009-07-13 10:16:31 +02:00
parent 11bc5195b8
commit 7d35447d80
15 changed files with 127 additions and 103 deletions

View File

@ -80,7 +80,7 @@ protected:
ErrorLogger * const _errorLogger; ErrorLogger * const _errorLogger;
/** report an error */ /** report an error */
void reportError(const Token *tok, const std::string &severity, const std::string &id, const std::string &msg) void reportError(const Token *tok, const Severity::e severity, const std::string &id, const std::string &msg)
{ {
std::list<const Token *> callstack; std::list<const Token *> callstack;
callstack.push_back(tok); callstack.push_back(tok);
@ -88,12 +88,12 @@ protected:
} }
/** report an error */ /** report an error */
void reportError(const std::list<const Token *> &callstack, const std::string &severity, const std::string &id, std::string msg) void reportError(const std::list<const Token *> &callstack, const Severity::e severity, const std::string &id, std::string msg)
{ {
// No errorLogger => just report the message to stdout // No errorLogger => just report the message to stdout
if (_errorLogger == NULL) if (_errorLogger == NULL)
{ {
std::cout << "(" << severity << ") " << msg << std::endl; std::cout << "(" << Severity::stringify(severity) << ") " << msg << std::endl;
return; return;
} }
@ -114,7 +114,7 @@ protected:
locationList.push_back(loc); locationList.push_back(loc);
} }
_errorLogger->reportErr(ErrorLogger::ErrorMessage(locationList, severity, msg, id)); _errorLogger->reportErr(ErrorLogger::ErrorMessage(locationList, Severity::stringify(severity), msg, id));
} }
private: private:

View File

@ -195,7 +195,7 @@ void CheckAutoVariables::autoVariables()
{ {
if (errorAv(tok->tokAt(1), tok->tokAt(4))) if (errorAv(tok->tokAt(1), tok->tokAt(4)))
reportError(tok, reportError(tok,
"error", Severity::error,
"autoVariables", "autoVariables",
"Wrong assignement of an auto-variable to an effective parameter of a function"); "Wrong assignement of an auto-variable to an effective parameter of a function");
} }
@ -203,7 +203,7 @@ void CheckAutoVariables::autoVariables()
{ {
if (errorAv(tok->tokAt(1), tok->tokAt(7))) if (errorAv(tok->tokAt(1), tok->tokAt(7)))
reportError(tok, reportError(tok,
"error", Severity::error,
"autoVariables", "autoVariables",
"Wrong assignement of an auto-variable to an effective parameter of a function"); "Wrong assignement of an auto-variable to an effective parameter of a function");
} }
@ -211,7 +211,7 @@ void CheckAutoVariables::autoVariables()
{ {
if (isAutoVar(tok->tokAt(2))) if (isAutoVar(tok->tokAt(2)))
reportError(tok, reportError(tok,
"error", Severity::error,
"autoVariables", "autoVariables",
"Return of the address of an auto-variable"); "Return of the address of an auto-variable");
} }
@ -285,7 +285,7 @@ void CheckAutoVariables::returnPointerToLocalArray()
void CheckAutoVariables::errorReturnPointerToLocalArray(const Token *tok) void CheckAutoVariables::errorReturnPointerToLocalArray(const Token *tok)
{ {
reportError(tok, "error", "returnLocalVariable", "Returning pointer to local array variable"); reportError(tok, Severity::error, "returnLocalVariable", "Returning pointer to local array variable");
} }

View File

@ -67,7 +67,7 @@ private:
void getErrorMessages() void getErrorMessages()
{ {
std::cout << "===auto variables===" << "\n"; std::cout << "===auto variables===" << "\n";
reportError(0, "error", "autoVariables", "Wrong assignement of an auto-variable to an effective parameter of a function"); reportError(0, Severity::error, "autoVariables", "Wrong assignement of an auto-variable to an effective parameter of a function");
errorReturnPointerToLocalArray(0); errorReturnPointerToLocalArray(0);
} }

View File

@ -52,27 +52,27 @@ void CheckBufferOverrunClass::arrayIndexOutOfBounds(const Token *tok)
void CheckBufferOverrunClass::arrayIndexOutOfBounds() void CheckBufferOverrunClass::arrayIndexOutOfBounds()
{ {
reportError(_callStack, "all", "arrayIndexOutOfBounds", "Array index out of bounds"); reportError(_callStack, Severity::possibleError, "arrayIndexOutOfBounds", "Array index out of bounds");
} }
void CheckBufferOverrunClass::bufferOverrun(const Token *tok) void CheckBufferOverrunClass::bufferOverrun(const Token *tok)
{ {
reportError(tok, "all", "bufferOverrun", "Buffer overrun"); reportError(tok, Severity::possibleError, "bufferOverrun", "Buffer overrun");
} }
void CheckBufferOverrunClass::strncatUsage(const Token *tok) void CheckBufferOverrunClass::strncatUsage(const Token *tok)
{ {
reportError(tok, "all", "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");
} }
void CheckBufferOverrunClass::outOfBounds(const Token *tok, const std::string &what) void CheckBufferOverrunClass::outOfBounds(const Token *tok, const std::string &what)
{ {
reportError(tok, "error", "outOfBounds", what + " is out of bounds"); reportError(tok, Severity::error, "outOfBounds", what + " is out of bounds");
} }
void CheckBufferOverrunClass::sizeArgumentAsChar(const Token *tok) void CheckBufferOverrunClass::sizeArgumentAsChar(const Token *tok)
{ {
reportError(tok, "all", "sizeArgumentAsChar", "The size argument is given as a char constant"); reportError(tok, Severity::possibleError, "sizeArgumentAsChar", "The size argument is given as a char constant");
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -774,42 +774,42 @@ void CheckClass::virtualDestructor()
void CheckClass::noConstructorError(const Token *tok, const std::string &classname) void CheckClass::noConstructorError(const Token *tok, const std::string &classname)
{ {
reportError(tok, "style", "noConstructor", "The class '" + classname + "' has no constructor"); reportError(tok, Severity::style, "noConstructor", "The class '" + classname + "' has no constructor");
} }
void CheckClass::uninitVarError(const Token *tok, const std::string &classname, const std::string &varname) void CheckClass::uninitVarError(const Token *tok, const std::string &classname, const std::string &varname)
{ {
reportError(tok, "style", "uninitVar", "Member variable not initialized in the constructor '" + classname + "::" + varname + "'"); reportError(tok, Severity::style, "uninitVar", "Member variable not initialized in the constructor '" + classname + "::" + varname + "'");
} }
void CheckClass::operatorEqVarError(const Token *tok, const std::string &classname, const std::string &varname) void CheckClass::operatorEqVarError(const Token *tok, const std::string &classname, const std::string &varname)
{ {
reportError(tok, "all style", "operatorEqVarError", "Member variable '" + classname + "::" + varname + "' is not assigned a value in '" + classname + "::operator=" + "'"); reportError(tok, Severity::possibleStyle, "operatorEqVarError", "Member variable '" + classname + "::" + varname + "' is not assigned a value in '" + classname + "::operator=" + "'");
} }
void CheckClass::unusedPrivateFunctionError(const Token *tok, const std::string &classname, const std::string &funcname) void CheckClass::unusedPrivateFunctionError(const Token *tok, const std::string &classname, const std::string &funcname)
{ {
reportError(tok, "style", "unusedPrivateFunction", "Unused private function '" + classname + "::" + funcname + "'"); reportError(tok, Severity::style, "unusedPrivateFunction", "Unused private function '" + classname + "::" + funcname + "'");
} }
void CheckClass::memsetClassError(const Token *tok, const std::string &memfunc) void CheckClass::memsetClassError(const Token *tok, const std::string &memfunc)
{ {
reportError(tok, "error", "memsetClass", "Using '" + memfunc + "' on class"); reportError(tok, Severity::error, "memsetClass", "Using '" + memfunc + "' on class");
} }
void CheckClass::memsetStructError(const Token *tok, const std::string &memfunc, const std::string &classname) void CheckClass::memsetStructError(const Token *tok, const std::string &memfunc, const std::string &classname)
{ {
reportError(tok, "error", "memsetStruct", "Using '" + memfunc + "' on struct that contains a 'std::" + classname + "'"); reportError(tok, Severity::error, "memsetStruct", "Using '" + memfunc + "' on struct that contains a 'std::" + classname + "'");
} }
void CheckClass::operatorEqReturnError(const Token *tok) void CheckClass::operatorEqReturnError(const Token *tok)
{ {
reportError(tok, "style", "operatorEq", "'operator=' should return something"); reportError(tok, Severity::style, "operatorEq", "'operator=' should return something");
} }
void CheckClass::virtualDestructorError(const Token *tok, const std::string &Base, const std::string &Derived) void CheckClass::virtualDestructorError(const Token *tok, const std::string &Base, const std::string &Derived)
{ {
reportError(tok, "error", "virtualDestructor", "Class " + Base + " which is inherited by class " + Derived + " does not have a virtual destructor"); reportError(tok, Severity::error, "virtualDestructor", "Class " + Base + " which is inherited by class " + Derived + " does not have a virtual destructor");
} }

View File

@ -54,15 +54,15 @@ void CheckDangerousFunctionsClass::dangerousFunctions()
void CheckDangerousFunctionsClass::dangerousFunctionmktemp(const Token *tok) void CheckDangerousFunctionsClass::dangerousFunctionmktemp(const Token *tok)
{ {
reportError(tok, "style", "dangerousFunctionmktemp", "Found 'mktemp'. You should use 'mkstemp' instead"); reportError(tok, Severity::style, "dangerousFunctionmktemp", "Found 'mktemp'. You should use 'mkstemp' instead");
} }
void CheckDangerousFunctionsClass::dangerousFunctiongets(const Token *tok) void CheckDangerousFunctionsClass::dangerousFunctiongets(const Token *tok)
{ {
reportError(tok, "style", "dangerousFunctiongets", "Found 'gets'. You should use 'fgets' instead"); reportError(tok, Severity::style, "dangerousFunctiongets", "Found 'gets'. You should use 'fgets' instead");
} }
void CheckDangerousFunctionsClass::dangerousFunctionscanf(const Token *tok) void CheckDangerousFunctionsClass::dangerousFunctionscanf(const Token *tok)
{ {
reportError(tok, "style", "dangerousFunctionscanf", "Found 'scanf'. You should use 'fgets' instead"); reportError(tok, Severity::style, "dangerousFunctionscanf", "Found 'scanf'. You should use 'fgets' instead");
} }

View File

@ -226,37 +226,37 @@ void CheckMemoryLeak::memoryLeak(const Token *tok, const char varname[], AllocTy
void CheckMemoryLeak::memleakError(const Token *tok, const std::string &varname) void CheckMemoryLeak::memleakError(const Token *tok, const std::string &varname)
{ {
error(tok, "error", "memleak", "Memory leak: " + varname); error(tok, Severity::error, "memleak", "Memory leak: " + varname);
} }
void CheckMemoryLeak::memleakallError(const Token *tok, const std::string &varname) void CheckMemoryLeak::memleakallError(const Token *tok, const std::string &varname)
{ {
error(tok, "all", "memleakall", "Memory leak: " + varname); error(tok, Severity::possibleError, "memleakall", "Memory leak: " + varname);
} }
void CheckMemoryLeak::resourceLeakError(const Token *tok, const std::string &varname) void CheckMemoryLeak::resourceLeakError(const Token *tok, const std::string &varname)
{ {
error(tok, "error", "resourceLeak", "Resource leak: " + varname); error(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)
{ {
error(tok, "error", "deallocDealloc", "Deallocating a deallocated pointer: " + varname); error(tok, Severity::error, "deallocDealloc", "Deallocating a deallocated pointer: " + varname);
} }
void CheckMemoryLeak::deallocuseError(const Token *tok, const std::string &varname) void CheckMemoryLeak::deallocuseError(const Token *tok, const std::string &varname)
{ {
error(tok, "error", "deallocuse", "Using '" + varname + "' after it is deallocated / released"); error(tok, Severity::error, "deallocuse", "Using '" + varname + "' after it is deallocated / released");
} }
void CheckMemoryLeak::mismatchSizeError(const Token *tok, const std::string &sz) void CheckMemoryLeak::mismatchSizeError(const Token *tok, const std::string &sz)
{ {
error(tok, "error", "mismatchSize", "The given size " + sz + " is mismatching"); error(tok, Severity::error, "mismatchSize", "The given size " + sz + " is mismatching");
} }
void CheckMemoryLeak::mismatchAllocDealloc(const std::list<const Token *> &callstack, const std::string &varname) void CheckMemoryLeak::mismatchAllocDealloc(const std::list<const Token *> &callstack, const std::string &varname)
{ {
error(callstack, "error", "mismatchAllocDealloc", "Mismatching allocation and deallocation: " + varname); error(callstack, Severity::error, "mismatchAllocDealloc", "Mismatching allocation and deallocation: " + varname);
} }
CheckMemoryLeak::AllocType CheckMemoryLeak::functionReturnType(const Token *tok) const CheckMemoryLeak::AllocType CheckMemoryLeak::functionReturnType(const Token *tok) const

View File

@ -65,8 +65,8 @@ public:
void mismatchAllocDealloc(const std::list<const Token *> &callstack, const std::string &varname); void mismatchAllocDealloc(const std::list<const Token *> &callstack, const std::string &varname);
// error message // error message
virtual void error(const Token *tok, const std::string &severity, const std::string &id, const std::string &msg) = 0; virtual void error(const Token *tok, const Severity::e severity, const std::string &id, const std::string &msg) = 0;
virtual void error(const std::list<const Token *> &callstack, const std::string &severity, const std::string &id, const std::string &msg) = 0; virtual void error(const std::list<const Token *> &callstack, const Severity::e severity, const std::string &id, const std::string &msg) = 0;
/** What type of allocated memory does the given function return? */ /** What type of allocated memory does the given function return? */
AllocType functionReturnType(const Token *tok) const; AllocType functionReturnType(const Token *tok) const;
@ -141,12 +141,12 @@ private:
void checkScope(const Token *Tok1, const char varname[], bool classmember, unsigned int sz); void checkScope(const Token *Tok1, const char varname[], bool classmember, unsigned int sz);
void error(const Token *tok, const std::string &severity, const std::string &id, const std::string &msg) void error(const Token *tok, const Severity::e severity, const std::string &id, const std::string &msg)
{ {
reportError(tok, severity, id, msg); reportError(tok, severity, id, msg);
} }
void error(const std::list<const Token *> &callstack, const std::string &severity, const std::string &id, const std::string &msg) void error(const std::list<const Token *> &callstack, const Severity::e severity, const std::string &id, const std::string &msg)
{ {
reportError(callstack, severity, id, msg); reportError(callstack, severity, id, msg);
} }
@ -197,12 +197,12 @@ private:
void parseClass(const Token *tok1, std::vector<const char *> &classname); void parseClass(const Token *tok1, std::vector<const char *> &classname);
void variable(const char classname[], const Token *tokVarname); void variable(const char classname[], const Token *tokVarname);
void error(const Token *tok, const std::string &severity, const std::string &id, const std::string &msg) void error(const Token *tok, const Severity::e severity, const std::string &id, const std::string &msg)
{ {
reportError(tok, severity, id, msg); reportError(tok, severity, id, msg);
} }
void error(const std::list<const Token *> &callstack, const std::string &severity, const std::string &id, const std::string &msg) void error(const std::list<const Token *> &callstack, const Severity::e severity, const std::string &id, const std::string &msg)
{ {
reportError(callstack, severity, id, msg); reportError(callstack, severity, id, msg);
} }
@ -251,12 +251,12 @@ public:
private: private:
void error(const Token *tok, const std::string &severity, const std::string &id, const std::string &msg) void error(const Token *tok, const Severity::e severity, const std::string &id, const std::string &msg)
{ {
reportError(tok, severity, id, msg); reportError(tok, severity, id, msg);
} }
void error(const std::list<const Token *> &callstack, const std::string &severity, const std::string &id, const std::string &msg) void error(const std::list<const Token *> &callstack, const Severity::e severity, const std::string &id, const std::string &msg)
{ {
reportError(callstack, severity, id, msg); reportError(callstack, severity, id, msg);
} }

View File

@ -965,90 +965,90 @@ void CheckOther::checkZeroDivision()
void CheckOther::cstyleCastError(const Token *tok) void CheckOther::cstyleCastError(const Token *tok)
{ {
reportError(tok, "style", "cstyleCast", "C-style pointer casting"); reportError(tok, Severity::style, "cstyleCast", "C-style pointer casting");
} }
void CheckOther::redundantIfDelete0Error(const Token *tok) void CheckOther::redundantIfDelete0Error(const Token *tok)
{ {
reportError(tok, "style", "redundantIfDelete0", "Redundant condition. It is safe to deallocate a NULL pointer"); reportError(tok, Severity::style, "redundantIfDelete0", "Redundant condition. It is safe to deallocate a NULL pointer");
} }
void CheckOther::redundantIfRemoveError(const Token *tok) void CheckOther::redundantIfRemoveError(const Token *tok)
{ {
reportError(tok, "style", "redundantIfRemove", "Redundant condition. The remove function in the STL will not do anything if element doesn't exist"); reportError(tok, Severity::style, "redundantIfRemove", "Redundant condition. The remove function in the STL will not do anything if element doesn't exist");
} }
void CheckOther::dangerousUsageStrtolError(const Token *tok) void CheckOther::dangerousUsageStrtolError(const Token *tok)
{ {
reportError(tok, "error", "dangerousUsageStrtol", "Invalid radix in call to strtol or strtoul. Must be 0 or 2-36"); reportError(tok, Severity::error, "dangerousUsageStrtol", "Invalid radix in call to strtol or strtoul. Must be 0 or 2-36");
} }
void CheckOther::ifNoActionError(const Token *tok) void CheckOther::ifNoActionError(const Token *tok)
{ {
reportError(tok, "style", "ifNoAction", "Found redundant if condition - 'if (condition);'"); reportError(tok, Severity::style, "ifNoAction", "Found redundant if condition - 'if (condition);'");
} }
void CheckOther::sprintfOverlappingDataError(const Token *tok, const std::string &varname) void CheckOther::sprintfOverlappingDataError(const Token *tok, const std::string &varname)
{ {
reportError(tok, "error", "sprintfOverlappingData", "Overlapping data buffer " + varname + "\nWhen using sprintf the same buffer must not be used both for output and input. The behaviour is undefined when that happens.\nFor example: 'sprintf(str,\"<%s>\",str);'"); reportError(tok, Severity::error, "sprintfOverlappingData", "Overlapping data buffer " + varname + "\nWhen using sprintf the same buffer must not be used both for output and input. The behaviour is undefined when that happens.\nFor example: 'sprintf(str,\"<%s>\",str);'");
} }
void CheckOther::udivError(const Token *tok) void CheckOther::udivError(const Token *tok)
{ {
reportError(tok, "error", "udivError", "Unsigned division. The result will be wrong."); reportError(tok, Severity::error, "udivError", "Unsigned division. The result will be wrong.");
} }
void CheckOther::udivWarning(const Token *tok) void CheckOther::udivWarning(const Token *tok)
{ {
reportError(tok, "all style", "udivWarning", "Warning: Division with signed and unsigned operators"); reportError(tok, Severity::possibleStyle, "udivWarning", "Warning: Division with signed and unsigned operators");
} }
void CheckOther::unusedStructMemberError(const Token *tok, const std::string &structname, const std::string &varname) void CheckOther::unusedStructMemberError(const Token *tok, const std::string &structname, const std::string &varname)
{ {
reportError(tok, "style", "unusedStructMember", "struct or union member '" + structname + "::" + varname + "' is never used"); reportError(tok, Severity::style, "unusedStructMember", "struct or union member '" + structname + "::" + varname + "' is never used");
} }
void CheckOther::passedByValueError(const Token *tok, const std::string &parname) void CheckOther::passedByValueError(const Token *tok, const std::string &parname)
{ {
reportError(tok, "style", "passedByValue", "Function parameter '" + parname + "' is passed by value. It could be passed by reference instead."); reportError(tok, Severity::style, "passedByValue", "Function parameter '" + parname + "' is passed by value. It could be passed by reference instead.");
} }
void CheckOther::constStatementError(const Token *tok, const std::string &type) void CheckOther::constStatementError(const Token *tok, const std::string &type)
{ {
reportError(tok, "style", "constStatement", "Redundant code: Found a statement that begins with " + type + " constant"); reportError(tok, Severity::style, "constStatement", "Redundant code: Found a statement that begins with " + type + " constant");
} }
void CheckOther::charArrayIndexError(const Token *tok) void CheckOther::charArrayIndexError(const Token *tok)
{ {
reportError(tok, "style", "charArrayIndex", "Warning - using char variable as array index"); reportError(tok, Severity::style, "charArrayIndex", "Warning - using char variable as array index");
} }
void CheckOther::charBitOpError(const Token *tok) void CheckOther::charBitOpError(const Token *tok)
{ {
reportError(tok, "style", "charBitOp", "Warning - using char variable in bit operation"); reportError(tok, Severity::style, "charBitOp", "Warning - using char variable in bit operation");
} }
void CheckOther::variableScopeError(const Token *tok, const std::string &varname) void CheckOther::variableScopeError(const Token *tok, const std::string &varname)
{ {
reportError(tok, "style", "variableScope", "The scope of the variable " + varname + " can be limited"); reportError(tok, Severity::style, "variableScope", "The scope of the variable " + varname + " can be limited");
} }
void CheckOther::conditionAlwaysTrueFalse(const Token *tok, const std::string &truefalse) void CheckOther::conditionAlwaysTrueFalse(const Token *tok, const std::string &truefalse)
{ {
reportError(tok, "style", "conditionAlwaysTrueFalse", "Condition is always " + truefalse); reportError(tok, Severity::style, "conditionAlwaysTrueFalse", "Condition is always " + truefalse);
} }
void CheckOther::strPlusChar(const Token *tok) void CheckOther::strPlusChar(const Token *tok)
{ {
reportError(tok, "error", "strPlusChar", "Unusual pointer arithmetic"); reportError(tok, Severity::error, "strPlusChar", "Unusual pointer arithmetic");
} }
void CheckOther::nullPointerError(const Token *tok) void CheckOther::nullPointerError(const Token *tok)
{ {
reportError(tok, "error", "nullPointer", "Possible null pointer dereference"); reportError(tok, Severity::error, "nullPointer", "Possible null pointer dereference");
} }
void CheckOther::zerodivError(const Token *tok) void CheckOther::zerodivError(const Token *tok)
{ {
reportError(tok, "error", "zerodiv", "Division by zero"); reportError(tok, Severity::error, "zerodiv", "Division by zero");
} }

View File

@ -31,13 +31,13 @@ CheckStl instance;
// Error message for bad iterator usage.. // Error message for bad iterator usage..
void CheckStl::iteratorsError(const Token *tok, const std::string &container1, const std::string &container2) void CheckStl::iteratorsError(const Token *tok, const std::string &container1, const std::string &container2)
{ {
reportError(tok, "error", "iterators", "Same iterator is used with both " + container1 + " and " + container2); reportError(tok, Severity::error, "iterators", "Same iterator is used with both " + container1 + " and " + container2);
} }
// Error message used when dereferencing an iterator that has been erased.. // Error message used when dereferencing an iterator that has been erased..
void CheckStl::dereferenceErasedError(const Token *tok, const std::string &itername) void CheckStl::dereferenceErasedError(const Token *tok, const std::string &itername)
{ {
reportError(tok, "error", "eraseDereference", "Dereferenced iterator '" + itername + "' has been erased"); reportError(tok, Severity::error, "eraseDereference", "Dereferenced iterator '" + itername + "' has been erased");
} }
void CheckStl::iterators() void CheckStl::iterators()
@ -131,7 +131,7 @@ void CheckStl::stlOutOfBounds()
// Error message for bad iterator usage.. // Error message for bad iterator usage..
void CheckStl::stlOutOfBoundsError(const Token *tok, const std::string &num, const std::string &var) void CheckStl::stlOutOfBoundsError(const Token *tok, const std::string &num, const std::string &var)
{ {
reportError(tok, "error", "stlOutOfBounds", "When " + num + "==" + var + ".size(), " + var + "[" + num + "] is out of bounds"); reportError(tok, Severity::error, "stlOutOfBounds", "When " + num + "==" + var + ".size(), " + var + "[" + num + "] is out of bounds");
} }
@ -210,7 +210,7 @@ void CheckStl::eraseCheckLoop(const Token *it)
// Error message for bad iterator usage.. // Error message for bad iterator usage..
void CheckStl::eraseError(const Token *tok) void CheckStl::eraseError(const Token *tok)
{ {
reportError(tok, "error", "erase", "Dangerous usage of erase\nAfter erase has been used the iterator may be invalid so dereferencing it or comparing it with other iterator is invalid."); reportError(tok, Severity::error, "erase", "Dangerous usage of erase\nAfter erase has been used the iterator may be invalid so dereferencing it or comparing it with other iterator is invalid.");
} }
@ -317,14 +317,14 @@ void CheckStl::pushback()
// Error message for bad iterator usage.. // Error message for bad iterator usage..
void CheckStl::pushbackError(const Token *tok, const std::string &iterator_name) void CheckStl::pushbackError(const Token *tok, const std::string &iterator_name)
{ {
reportError(tok, "error", "pushback", "After push_back or push_front, the iterator '" + iterator_name + "' may be invalid"); reportError(tok, Severity::error, "pushback", "After push_back or push_front, the iterator '" + iterator_name + "' may be invalid");
} }
// Error message for bad iterator usage.. // Error message for bad iterator usage..
void CheckStl::invalidPointerError(const Token *tok, const std::string &pointer_name) void CheckStl::invalidPointerError(const Token *tok, const std::string &pointer_name)
{ {
reportError(tok, "error", "pushback", "Invalid pointer '" + pointer_name + "' after push_back / push_front"); reportError(tok, Severity::error, "pushback", "Invalid pointer '" + pointer_name + "' after push_back / push_front");
} }
@ -374,5 +374,5 @@ void CheckStl::stlBoundries()
// Error message for bad boundry usage.. // Error message for bad boundry usage..
void CheckStl::stlBoundriesError(const Token *tok) void CheckStl::stlBoundriesError(const Token *tok)
{ {
reportError(tok, "error", "stlBoundries", "STL range check should be using != and not < since the order of the pointers isn't guaranteed"); reportError(tok, Severity::error, "stlBoundries", "STL range check should be using != and not < since the order of the pointers isn't guaranteed");
} }

View File

@ -334,4 +334,28 @@ private:
void _writemsg(const Tokenizer *tokenizer, const Token *tok, const char severity[], const std::string &msg, const std::string &id); void _writemsg(const Tokenizer *tokenizer, const Token *tok, const char severity[], const std::string &msg, const std::string &id);
void _writemsg(const Tokenizer *tokenizer, const std::list<const Token *> &callstack, const char severity[], const std::string &msg, const std::string &id); void _writemsg(const Tokenizer *tokenizer, const std::list<const Token *> &callstack, const char severity[], const std::string &msg, const std::string &id);
}; };
/** enum class for severity */
class Severity
{
public:
enum e { error, style, possibleError, possibleStyle };
static std::string stringify(e severity)
{
switch (severity)
{
case error:
return "error";
case style:
return "style";
case possibleError:
return "possible error";
case possibleStyle:
return "possible style";
};
return "???";
}
};
#endif #endif

View File

@ -200,7 +200,7 @@ private:
" int data[2];\n" " int data[2];\n"
" data[ sizeof(data[0]) ] = 0;\n" " data[ sizeof(data[0]) ] = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (all) Array index out of bounds\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (possible error) Array index out of bounds\n", errout.str());
} }
void sizeof3() void sizeof3()
@ -223,7 +223,7 @@ private:
" str[15] = 0;\n" " str[15] = 0;\n"
" str[16] = 0;\n" " str[16] = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:5]: (all) Array index out of bounds\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (possible error) Array index out of bounds\n", errout.str());
} }
@ -235,7 +235,7 @@ private:
" str[15] = 0;\n" " str[15] = 0;\n"
" str[16] = 0;\n" " str[16] = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:5]: (all) Array index out of bounds\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (possible error) Array index out of bounds\n", errout.str());
} }
@ -248,7 +248,7 @@ private:
" for (i = 0; i < 100; i++)\n" " for (i = 0; i < 100; i++)\n"
" sum += val[i];\n" " sum += val[i];\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:5]: (all) Buffer overrun\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (possible error) Buffer overrun\n", errout.str());
} }
{ {
@ -258,7 +258,7 @@ private:
" for (i = 1; i < 100; i++)\n" " for (i = 1; i < 100; i++)\n"
" sum += val[i];\n" " sum += val[i];\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:5]: (all) Buffer overrun\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (possible error) Buffer overrun\n", errout.str());
} }
@ -269,7 +269,7 @@ private:
" for (i = a; i < 100; i++)\n" " for (i = a; i < 100; i++)\n"
" sum += val[i];\n" " sum += val[i];\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:5]: (all) Buffer overrun\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (possible error) Buffer overrun\n", errout.str());
} }
} }
@ -282,7 +282,7 @@ private:
" int i[SIZE];\n" " int i[SIZE];\n"
" i[SIZE] = 0;\n" " i[SIZE] = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:5]: (all) Array index out of bounds\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (possible error) Array index out of bounds\n", errout.str());
} }
@ -293,7 +293,7 @@ private:
" int i[10];\n" " int i[10];\n"
" i[ sizeof(i) - 1 ] = 0;\n" " i[ sizeof(i) - 1 ] = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (all) Array index out of bounds\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (possible error) Array index out of bounds\n", errout.str());
} }
@ -309,7 +309,7 @@ private:
" struct ABC abc;\n" " struct ABC abc;\n"
" abc.str[10] = 0;\n" " abc.str[10] = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:9]: (all) Array index out of bounds\n", errout.str()); ASSERT_EQUALS("[test.cpp:9]: (possible error) Array index out of bounds\n", errout.str());
} }
@ -324,7 +324,7 @@ private:
"{\n" "{\n"
" abc->str[10] = 0;\n" " abc->str[10] = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:8]: (all) Array index out of bounds\n", errout.str()); ASSERT_EQUALS("[test.cpp:8]: (possible error) Array index out of bounds\n", errout.str());
} }
@ -342,7 +342,7 @@ private:
" struct ABC abc;\n" " struct ABC abc;\n"
" abc.str[SIZE] = 0;\n" " abc.str[SIZE] = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:11]: (all) Array index out of bounds\n", errout.str()); ASSERT_EQUALS("[test.cpp:11]: (possible error) Array index out of bounds\n", errout.str());
} }
void array_index_9() void array_index_9()
@ -357,7 +357,7 @@ private:
" char str[5];\n" " char str[5];\n"
" memclr( str ); // ERROR\n" " memclr( str ); // ERROR\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:3]: (all) Array index out of bounds\n", errout.str()); ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:3]: (possible error) Array index out of bounds\n", errout.str());
} }
@ -377,7 +377,7 @@ private:
"{\n" "{\n"
" memclr(abc->str);\n" " memclr(abc->str);\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:13] -> [test.cpp:8]: (all) Array index out of bounds\n", errout.str()); ASSERT_EQUALS("[test.cpp:13] -> [test.cpp:8]: (possible error) Array index out of bounds\n", errout.str());
} }
@ -398,7 +398,7 @@ private:
" abc->str[10] = 0;\n" " abc->str[10] = 0;\n"
" }\n" " }\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:12]: (all) Array index out of bounds\n", errout.str()); ASSERT_EQUALS("[test.cpp:12]: (possible error) Array index out of bounds\n", errout.str());
} }
@ -415,7 +415,7 @@ private:
"{\n" "{\n"
" str[10] = 0;\n" " str[10] = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:10]: (all) Array index out of bounds\n", errout.str()); ASSERT_EQUALS("[test.cpp:10]: (possible error) Array index out of bounds\n", errout.str());
} }
void array_index_13() void array_index_13()
@ -439,7 +439,7 @@ private:
" char str[3];\n" " char str[3];\n"
" strcpy(str, \"abc\");\n" " strcpy(str, \"abc\");\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (all) Buffer overrun\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (possible error) Buffer overrun\n", errout.str());
} }
@ -454,7 +454,7 @@ private:
"{\n" "{\n"
" strcpy( abc->str, \"abcdef\" );\n" " strcpy( abc->str, \"abcdef\" );\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:8]: (all) Buffer overrun\n", errout.str()); ASSERT_EQUALS("[test.cpp:8]: (possible error) Buffer overrun\n", errout.str());
} }
@ -468,7 +468,7 @@ private:
" for (i = 0; i <= 10; ++i)\n" " for (i = 0; i <= 10; ++i)\n"
" a[i] = 0;\n" " a[i] = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:7]: (all) Buffer overrun\n", errout.str()); ASSERT_EQUALS("[test.cpp:7]: (possible error) Buffer overrun\n", errout.str());
} }
@ -480,7 +480,7 @@ private:
" for (int i = 0; i < 8; ++i)\n" " for (int i = 0; i < 8; ++i)\n"
" p[i] = 0;\n" " p[i] = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:5]: (all) Buffer overrun\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (possible error) Buffer overrun\n", errout.str());
// No false positive // No false positive
check("void foo(int x, int y)\n" check("void foo(int x, int y)\n"
@ -501,7 +501,7 @@ private:
" char str[3];\n" " char str[3];\n"
" sprintf(str, \"%s\", \"abc\");\n" " sprintf(str, \"%s\", \"abc\");\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (all) Buffer overrun\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (possible error) Buffer overrun\n", errout.str());
} }
void snprintf1() void snprintf1()
@ -555,7 +555,7 @@ private:
" strncpy(str, a, 10);\n" " strncpy(str, a, 10);\n"
" strncat(str, b, 10);\n" " strncat(str, b, 10);\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:5]: (all) Dangerous usage of strncat. Tip: the 3rd parameter means maximum number of characters to append\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (possible error) Dangerous usage of strncat. Tip: the 3rd parameter means maximum number of characters to append\n", errout.str());
} }
void strncat2() void strncat2()
@ -565,7 +565,7 @@ private:
" char str[5];\n" " char str[5];\n"
" strncat(str, a, 5);\n" " strncat(str, a, 5);\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (all) Dangerous usage of strncat. Tip: the 3rd parameter means maximum number of characters to append\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (possible error) Dangerous usage of strncat. Tip: the 3rd parameter means maximum number of characters to append\n", errout.str());
} }
@ -577,7 +577,7 @@ private:
" char str[10];\n" " char str[10];\n"
" cin >> str;\n" " cin >> str;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (all) Buffer overrun\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (possible error) Buffer overrun\n", errout.str());
} }
@ -619,7 +619,7 @@ private:
"{\n" "{\n"
" str[3] = 0;\n" " str[3] = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:5]: (all) Array index out of bounds\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (possible error) Array index out of bounds\n", errout.str());
} }
@ -631,14 +631,14 @@ private:
" char *s = new char[10];\n" " char *s = new char[10];\n"
" s[10] = 0;\n" " s[10] = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (all) Array index out of bounds\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (possible error) Array index out of bounds\n", errout.str());
check("void foo()\n" check("void foo()\n"
"{\n" "{\n"
" char *s = malloc(10);\n" " char *s = malloc(10);\n"
" s[10] = 0;\n" " s[10] = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (all) Array index out of bounds\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (possible error) Array index out of bounds\n", errout.str());
} }
@ -649,7 +649,7 @@ private:
" char s[10];\n" " char s[10];\n"
" memset(s, 5, '*');\n" " memset(s, 5, '*');\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (all) The size argument is given as a char constant\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (possible error) The size argument is given as a char constant\n", errout.str());
} }
}; };

View File

@ -216,7 +216,7 @@ private:
" void operator=(const Fred &fred) { }\n" " void operator=(const Fred &fred) { }\n"
" int i;\n" " int i;\n"
"};\n"); "};\n");
ASSERT_EQUALS("[test.cpp:5]: (all style) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (possible style) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='\n", errout.str());
} }
void initvar_operator_eq3() void initvar_operator_eq3()
@ -382,7 +382,7 @@ private:
"\n" "\n"
"void Fred::operator=(const Fred &f)\n" "void Fred::operator=(const Fred &f)\n"
"{ }", true); "{ }", true);
ASSERT_EQUALS("[test.cpp:13]: (all style) Member variable 'Fred::ints' is not assigned a value in 'Fred::operator='\n", errout.str()); ASSERT_EQUALS("[test.cpp:13]: (possible style) Member variable 'Fred::ints' is not assigned a value in 'Fred::operator='\n", errout.str());
} }
}; };

View File

@ -74,7 +74,7 @@ private:
" unsigned int uvar = 2;\n" " unsigned int uvar = 2;\n"
" return ivar / uvar;\n" " return ivar / uvar;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:5]: (all style) Warning: Division with signed and unsigned operators\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (possible style) Warning: Division with signed and unsigned operators\n", errout.str());
} }
void division2() void division2()
@ -85,7 +85,7 @@ private:
" unsigned int uvar = 2;\n" " unsigned int uvar = 2;\n"
" return uvar / ivar;\n" " return uvar / ivar;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:5]: (all style) Warning: Division with signed and unsigned operators\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (possible style) Warning: Division with signed and unsigned operators\n", errout.str());
} }
void division3() void division3()
@ -98,7 +98,7 @@ private:
" u32 uvar = 2;\n" " u32 uvar = 2;\n"
" return uvar / ivar;\n" " return uvar / ivar;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:7]: (all style) Warning: Division with signed and unsigned operators\n", errout.str()); ASSERT_EQUALS("[test.cpp:7]: (possible style) Warning: Division with signed and unsigned operators\n", errout.str());
} }
void division4() void division4()

View File

@ -607,7 +607,7 @@ private:
" return;\n" " return;\n"
" }\n" " }\n"
"}\n", true); "}\n", true);
ASSERT_EQUALS("[test.cpp:9]: (all) Memory leak: str\n", errout.str()); ASSERT_EQUALS("[test.cpp:9]: (possible error) Memory leak: str\n", errout.str());
} }
@ -1128,7 +1128,7 @@ private:
check(code.c_str(), false); check(code.c_str(), false);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check(code.c_str(), true); check(code.c_str(), true);
ASSERT_EQUALS("[test.cpp:12]: (all) Memory leak: str\n", errout.str()); ASSERT_EQUALS("[test.cpp:12]: (possible error) Memory leak: str\n", errout.str());
} }
void switch3() void switch3()
@ -1923,7 +1923,7 @@ private:
"{\n" "{\n"
" Fred *f = new Fred;\n" " Fred *f = new Fred;\n"
"}\n", true); "}\n", true);
ASSERT_EQUALS("[test.cpp:4]: (all) Memory leak: f\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (possible error) Memory leak: f\n", errout.str());
} }
@ -2519,7 +2519,7 @@ private:
" delete [] str2;\n" " delete [] str2;\n"
"}\n", true); "}\n", true);
ASSERT_EQUALS("[test.cpp:4]: (all) Memory leak: Fred::str1\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (possible error) Memory leak: Fred::str1\n", errout.str());
} }
@ -2691,7 +2691,7 @@ private:
" int * p;\n" " int * p;\n"
" A() { p = new int; }\n" " A() { p = new int; }\n"
"};\n", true); "};\n", true);
ASSERT_EQUALS("[test.cpp:4]: (all) Memory leak: A::p\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (possible error) Memory leak: A::p\n", errout.str());
} }
void class11() void class11()
@ -2704,7 +2704,7 @@ private:
"};\n" "};\n"
"A::A() : p(new int[10])\n" "A::A() : p(new int[10])\n"
"{ }", true); "{ }", true);
ASSERT_EQUALS("[test.cpp:4]: (all) Memory leak: A::p\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (possible error) Memory leak: A::p\n", errout.str());
} }