diff --git a/src/errormessage.h b/src/errormessage.h index 0c7fae1cd..f51e5ffc6 100644 --- a/src/errormessage.h +++ b/src/errormessage.h @@ -33,7 +33,7 @@ private: public: static std::string arrayIndexOutOfBounds(const Tokenizer *tokenizer, const Token *Location) { - return msg1(tokenizer, Location) + "Array index out of bounds"; + return msg1(tokenizer, Location) + std::string("(all) ") + "Array index out of bounds"; } static bool arrayIndexOutOfBounds(const Settings &s) { @@ -42,7 +42,7 @@ public: static std::string bufferOverrun(const Tokenizer *tokenizer, const Token *Location) { - return msg1(tokenizer, Location) + "Buffer overrun"; + return msg1(tokenizer, Location) + std::string("(all) ") + "Buffer overrun"; } static bool bufferOverrun(const Settings &s) { @@ -51,7 +51,7 @@ public: static std::string outOfBounds(const Tokenizer *tokenizer, const Token *Location, const std::string &what) { - return msg1(tokenizer, Location) + "" + what + " is out of bounds"; + return msg1(tokenizer, Location) + std::string("(always) ") + "" + what + " is out of bounds"; } static bool outOfBounds() { @@ -60,7 +60,7 @@ public: static std::string noConstructor(const Tokenizer *tokenizer, const Token *Location, const std::string &classname) { - return msg1(tokenizer, Location) + "The class '" + classname + "' has no constructor"; + return msg1(tokenizer, Location) + std::string("(style) ") + "The class '" + classname + "' has no constructor"; } static bool noConstructor(const Settings &s) { @@ -69,7 +69,7 @@ public: static std::string uninitVar(const Tokenizer *tokenizer, const Token *Location, const std::string &classname, const std::string &varname) { - return msg1(tokenizer, Location) + "Uninitialized member variable '" + classname + "::" + varname + "'"; + return msg1(tokenizer, Location) + std::string("(always) ") + "Uninitialized member variable '" + classname + "::" + varname + "'"; } static bool uninitVar() { @@ -78,7 +78,7 @@ public: static std::string unusedPrivateFunction(const Tokenizer *tokenizer, const Token *Location, const std::string &classname, const std::string &funcname) { - return msg1(tokenizer, Location) + "Unused private function '" + classname + "::" + funcname + "'"; + return msg1(tokenizer, Location) + std::string("(style) ") + "Unused private function '" + classname + "::" + funcname + "'"; } static bool unusedPrivateFunction(const Settings &s) { @@ -87,7 +87,7 @@ public: static std::string memsetClass(const Tokenizer *tokenizer, const Token *Location, const std::string &memfunc) { - return msg1(tokenizer, Location) + "Using '" + memfunc + "' on class"; + return msg1(tokenizer, Location) + std::string("(always) ") + "Using '" + memfunc + "' on class"; } static bool memsetClass() { @@ -96,7 +96,7 @@ public: static std::string memsetStruct(const Tokenizer *tokenizer, const Token *Location, const std::string &memfunc, const std::string &classname) { - return msg1(tokenizer, Location) + "Using '" + memfunc + "' on struct that contains a 'std::" + classname + "'"; + return msg1(tokenizer, Location) + std::string("(always) ") + "Using '" + memfunc + "' on struct that contains a 'std::" + classname + "'"; } static bool memsetStruct() { @@ -105,7 +105,7 @@ public: static std::string operatorEq(const Tokenizer *tokenizer, const Token *Location) { - return msg1(tokenizer, Location) + "'operator=' should return something"; + return msg1(tokenizer, Location) + std::string("(style) ") + "'operator=' should return something"; } static bool operatorEq(const Settings &s) { @@ -114,7 +114,7 @@ public: static std::string virtualDestructor(const Tokenizer *tokenizer, const Token *Location, const std::string &Base, const std::string &Derived) { - return msg1(tokenizer, Location) + "Class " + Base + " which is inherited by class " + Derived + " does not have a virtual destructor"; + return msg1(tokenizer, Location) + std::string("(always) ") + "Class " + Base + " which is inherited by class " + Derived + " does not have a virtual destructor"; } static bool virtualDestructor() { @@ -123,16 +123,16 @@ public: static std::string unusedFunction(const std::string &filename, const std::string &funcname) { - return "[" + filename + "]: The function '" + funcname + "' is never used"; + return std::string("(all style) ") + "[" + filename + "]: The function '" + funcname + "' is never used"; } static bool unusedFunction(const Settings &s) { - return s._showAll & s._checkCodingStyle; + return s._checkCodingStyle || s._showAll; } static std::string mismatchAllocDealloc(const Tokenizer *tokenizer, const Token *Location, const std::string &varname) { - return msg1(tokenizer, Location) + "Mismatching allocation and deallocation: " + varname + ""; + return msg1(tokenizer, Location) + std::string("(always) ") + "Mismatching allocation and deallocation: " + varname + ""; } static bool mismatchAllocDealloc() { @@ -141,7 +141,7 @@ public: static std::string memleak(const Tokenizer *tokenizer, const Token *Location, const std::string &varname) { - return msg1(tokenizer, Location) + "Memory leak: " + varname + ""; + return msg1(tokenizer, Location) + std::string("(always) ") + "Memory leak: " + varname + ""; } static bool memleak() { @@ -150,7 +150,7 @@ public: static std::string resourceLeak(const Tokenizer *tokenizer, const Token *Location, const std::string &varname) { - return msg1(tokenizer, Location) + "Resource leak: " + varname + ""; + return msg1(tokenizer, Location) + std::string("(always) ") + "Resource leak: " + varname + ""; } static bool resourceLeak() { @@ -159,7 +159,7 @@ public: static std::string deallocDealloc(const Tokenizer *tokenizer, const Token *Location, const std::string &varname) { - return msg1(tokenizer, Location) + "Deallocating a deallocated pointer: " + varname + ""; + return msg1(tokenizer, Location) + std::string("(always) ") + "Deallocating a deallocated pointer: " + varname + ""; } static bool deallocDealloc() { @@ -168,7 +168,7 @@ public: static std::string cstyleCast(const Tokenizer *tokenizer, const Token *Location) { - return msg1(tokenizer, Location) + "C-style pointer casting"; + return msg1(tokenizer, Location) + std::string("(style) ") + "C-style pointer casting"; } static bool cstyleCast(const Settings &s) { @@ -177,7 +177,7 @@ public: static std::string redundantIfDelete0(const Tokenizer *tokenizer, const Token *Location) { - return msg1(tokenizer, Location) + "Redundant condition. It is safe to deallocate a NULL pointer"; + return msg1(tokenizer, Location) + std::string("(style) ") + "Redundant condition. It is safe to deallocate a NULL pointer"; } static bool redundantIfDelete0(const Settings &s) { @@ -186,7 +186,7 @@ public: static std::string redundantIfRemove(const Tokenizer *tokenizer, const Token *Location) { - return msg1(tokenizer, Location) + "Redundant condition. The remove function in the STL will not do anything if element doesn't exist"; + return msg1(tokenizer, Location) + std::string("(style) ") + "Redundant condition. The remove function in the STL will not do anything if element doesn't exist"; } static bool redundantIfRemove(const Settings &s) { @@ -195,7 +195,7 @@ public: static std::string dangerousUsageStrtol(const Tokenizer *tokenizer, const Token *Location) { - return msg1(tokenizer, Location) + "Invalid radix in call to strtol or strtoul. Must be 0 or 2-36"; + return msg1(tokenizer, Location) + std::string("(always) ") + "Invalid radix in call to strtol or strtoul. Must be 0 or 2-36"; } static bool dangerousUsageStrtol() { @@ -204,7 +204,7 @@ public: static std::string ifNoAction(const Tokenizer *tokenizer, const Token *Location) { - return msg1(tokenizer, Location) + "Found redundant if condition - 'if (condition);'"; + return msg1(tokenizer, Location) + std::string("(style) ") + "Found redundant if condition - 'if (condition);'"; } static bool ifNoAction(const Settings &s) { @@ -213,7 +213,7 @@ public: static std::string sprintfOverlappingData(const Tokenizer *tokenizer, const Token *Location, const Settings &settings, const std::string &varname) { - return msg1(tokenizer, Location) + "Overlapping data buffer " + varname + "" + std::string(settings._verbose ? "\n -- If copying takes place between objects that overlap as a result of a\n call to sprintf() or snprintf(), the results are undefined.\n http://www.opengroup.org/onlinepubs/000095399/functions/printf.html" : ""); + return msg1(tokenizer, Location) + std::string("(always) ") + "Overlapping data buffer " + varname + "" + std::string(settings._verbose ? "\n -- If copying takes place between objects that overlap as a result of a\n call to sprintf() or snprintf(), the results are undefined.\n http://www.opengroup.org/onlinepubs/000095399/functions/printf.html" : ""); } static bool sprintfOverlappingData() { @@ -222,7 +222,7 @@ public: static std::string udivError(const Tokenizer *tokenizer, const Token *Location) { - return msg1(tokenizer, Location) + "Unsigned division. The result will be wrong."; + return msg1(tokenizer, Location) + std::string("(always) ") + "Unsigned division. The result will be wrong."; } static bool udivError() { @@ -231,16 +231,16 @@ public: static std::string udivWarning(const Tokenizer *tokenizer, const Token *Location) { - return msg1(tokenizer, Location) + "Warning: Division with signed and unsigned operators"; + return msg1(tokenizer, Location) + std::string("(all style) ") + "Warning: Division with signed and unsigned operators"; } static bool udivWarning(const Settings &s) { - return s._showAll & s._checkCodingStyle; + return s._checkCodingStyle || s._showAll; } static std::string unusedStructMember(const Tokenizer *tokenizer, const Token *Location, const std::string &structname, const std::string &varname) { - return msg1(tokenizer, Location) + "struct or union member '" + structname + "::" + varname + "' is never used"; + return msg1(tokenizer, Location) + std::string("(style) ") + "struct or union member '" + structname + "::" + varname + "' is never used"; } static bool unusedStructMember(const Settings &s) { @@ -249,7 +249,7 @@ public: static std::string passedByValue(const Tokenizer *tokenizer, const Token *Location, const std::string &parname) { - return msg1(tokenizer, Location) + "Function parameter '" + parname + "' is passed by value. It could be passed by reference instead."; + return msg1(tokenizer, Location) + std::string("(style) ") + "Function parameter '" + parname + "' is passed by value. It could be passed by reference instead."; } static bool passedByValue(const Settings &s) { @@ -258,7 +258,7 @@ public: static std::string constStatement(const Tokenizer *tokenizer, const Token *Location, const std::string &type) { - return msg1(tokenizer, Location) + "Redundant code: Found a statement that begins with " + type + " constant"; + return msg1(tokenizer, Location) + std::string("(style) ") + "Redundant code: Found a statement that begins with " + type + " constant"; } static bool constStatement(const Settings &s) { @@ -267,7 +267,7 @@ public: static std::string charArrayIndex(const Tokenizer *tokenizer, const Token *Location) { - return msg1(tokenizer, Location) + "Warning - using char variable as array index"; + return msg1(tokenizer, Location) + std::string("(style) ") + "Warning - using char variable as array index"; } static bool charArrayIndex(const Settings &s) { @@ -276,7 +276,7 @@ public: static std::string charBitOp(const Tokenizer *tokenizer, const Token *Location) { - return msg1(tokenizer, Location) + "Warning - using char variable in bit operation"; + return msg1(tokenizer, Location) + std::string("(style) ") + "Warning - using char variable in bit operation"; } static bool charBitOp(const Settings &s) { @@ -285,7 +285,7 @@ public: static std::string variableScope(const Tokenizer *tokenizer, const Token *Location, const std::string &varname) { - return msg1(tokenizer, Location) + "The scope of the variable " + varname + " can be limited"; + return msg1(tokenizer, Location) + std::string("(never) ") + "The scope of the variable " + varname + " can be limited"; } static bool variableScope() { @@ -294,7 +294,7 @@ public: static std::string conditionAlwaysTrueFalse(const Tokenizer *tokenizer, const Token *Location, const std::string &truefalse) { - return msg1(tokenizer, Location) + "Condition is always " + truefalse + ""; + return msg1(tokenizer, Location) + std::string("(style) ") + "Condition is always " + truefalse + ""; } static bool conditionAlwaysTrueFalse(const Settings &s) { @@ -303,7 +303,7 @@ public: static std::string strPlusChar(const Tokenizer *tokenizer, const Token *Location) { - return msg1(tokenizer, Location) + "Unusual pointer arithmetic"; + return msg1(tokenizer, Location) + std::string("(always) ") + "Unusual pointer arithmetic"; } static bool strPlusChar() { diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index 00441a152..1b035ec14 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -187,7 +187,7 @@ private: " int data[2];\n" " data[ sizeof(data[0]) ] = 0;\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:4]: Array index out of bounds\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:4]: (all) Array index out of bounds\n"), errout.str()); */ } @@ -202,7 +202,7 @@ private: " str[15] = 0;\n" " str[16] = 0;\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:5]: Array index out of bounds\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:5]: (all) Array index out of bounds\n"), errout.str()); } @@ -214,7 +214,7 @@ private: " str[15] = 0;\n" " str[16] = 0;\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:5]: Array index out of bounds\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:5]: (all) Array index out of bounds\n"), errout.str()); } @@ -226,7 +226,7 @@ private: " for (i = 0; i < 100; i++)\n" " sum += val[i];\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:5]: Buffer overrun\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:5]: (all) Buffer overrun\n"), errout.str()); } @@ -238,7 +238,7 @@ private: " int i[SIZE];\n" " i[SIZE] = 0;\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:5]: Array index out of bounds\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:5]: (all) Array index out of bounds\n"), errout.str()); } @@ -249,7 +249,7 @@ private: " int i[10];\n" " i[ sizeof(i) - 1 ] = 0;\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:4]: Array index out of bounds\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:4]: (all) Array index out of bounds\n"), errout.str()); } @@ -265,7 +265,7 @@ private: " struct ABC abc;\n" " abc.str[10] = 0;\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:9]: Array index out of bounds\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:9]: (all) Array index out of bounds\n"), errout.str()); } @@ -280,7 +280,7 @@ private: "{\n" " abc->str[10] = 0;\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:8]: Array index out of bounds\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:8]: (all) Array index out of bounds\n"), errout.str()); } @@ -298,7 +298,7 @@ private: " struct ABC abc;\n" " abc.str[SIZE] = 0;\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:11]: Array index out of bounds\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:11]: (all) Array index out of bounds\n"), errout.str()); } void array_index_9() @@ -313,7 +313,7 @@ private: " char str[5];\n" " memclr( str ); // ERROR\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:9] -> [test.cpp:3]: Array index out of bounds\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:9] -> [test.cpp:3]: (all) Array index out of bounds\n"), errout.str()); } @@ -333,7 +333,7 @@ private: "{\n" " memclr(abc->str);\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:13] -> [test.cpp:8]: Array index out of bounds\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:13] -> [test.cpp:8]: (all) Array index out of bounds\n"), errout.str()); } @@ -354,7 +354,7 @@ private: " abc->str[10] = 0;\n" " }\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:12]: Array index out of bounds\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:12]: (all) Array index out of bounds\n"), errout.str()); } @@ -372,7 +372,7 @@ private: " str[10] = 0;\n" "}\n"); std::string err(errout.str()); - ASSERT_EQUALS(std::string("[test.cpp:10]: Array index out of bounds\n"), err); + ASSERT_EQUALS(std::string("[test.cpp:10]: (all) Array index out of bounds\n"), err); } @@ -383,7 +383,7 @@ private: " char str[3];\n" " strcpy(str, \"abc\");\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:4]: Buffer overrun\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:4]: (all) Buffer overrun\n"), errout.str()); } @@ -398,7 +398,7 @@ private: "{\n" " strcpy( abc->str, \"abcdef\" );\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:8]: Buffer overrun\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:8]: (all) Buffer overrun\n"), errout.str()); } @@ -409,7 +409,7 @@ private: " char str[3];\n" " sprintf(str, \"%s\", \"abc\");\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:4]: Buffer overrun\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:4]: (all) Buffer overrun\n"), errout.str()); } void snprintf1() @@ -419,7 +419,7 @@ private: " char str[5];\n" " snprintf(str, 10, \"%s\", \"abc\");\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:4]: snprintf size is out of bounds\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:4]: (always) snprintf size is out of bounds\n"), errout.str()); } void snprintf2() diff --git a/test/testcharvar.cpp b/test/testcharvar.cpp index d65d52d6c..7f103acaf 100644 --- a/test/testcharvar.cpp +++ b/test/testcharvar.cpp @@ -72,7 +72,7 @@ private: " char ch = 0x80;\n" " buf[ch] = 0;\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:4]: Warning - using char variable as array index\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:4]: (style) Warning - using char variable as array index\n"), errout.str()); /* TODO check("void foo()\n" @@ -80,14 +80,14 @@ private: " signed char ch = 0x80;\n" " buf[ch] = 0;\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:4]: Warning - using char variable as array index\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:4]: (style) Warning - using char variable as array index\n"), errout.str()); */ check("void foo(char ch)\n" "{\n" " buf[ch] = 0;\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:3]: Warning - using char variable as array index\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:3]: (style) Warning - using char variable as array index\n"), errout.str()); } @@ -98,7 +98,7 @@ private: " char ch;\n" " result = a | ch;\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:4]: Warning - using char variable in bit operation\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:4]: (style) Warning - using char variable in bit operation\n"), errout.str()); } void bitop2() diff --git a/test/testclass.cpp b/test/testclass.cpp index cadab0cf6..61b4aadbc 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -84,7 +84,7 @@ private: checkVirtualDestructor("class Base { };\n" "class Derived : public Base { public: ~Derived() { (void)11; } };"); - ASSERT_EQUALS(std::string("[test.cpp:1]: Class Base which is inherited by class Derived does not have a virtual destructor\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:1]: (always) Class Base which is inherited by class Derived does not have a virtual destructor\n"), errout.str()); checkVirtualDestructor("class Base { };\n" "class Derived : Base { public: ~Derived() { (void)11; } };"); @@ -97,11 +97,11 @@ private: checkVirtualDestructor("class Base { public: ~Base(); };\n" "class Derived : public Base { public: ~Derived() { (void)11; } };"); - ASSERT_EQUALS(std::string("[test.cpp:1]: Class Base which is inherited by class Derived does not have a virtual destructor\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:1]: (always) Class Base which is inherited by class Derived does not have a virtual destructor\n"), errout.str()); checkVirtualDestructor("class Base { public: ~Base(); };\n" "class Derived : private Fred, public Base { public: ~Derived() { (void)11; } };"); - ASSERT_EQUALS(std::string("[test.cpp:1]: Class Base which is inherited by class Derived does not have a virtual destructor\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:1]: (always) Class Base which is inherited by class Derived does not have a virtual destructor\n"), errout.str()); } void virtualDestructor4() @@ -165,7 +165,7 @@ private: " ECODES _code;\n" "};\n"); - ASSERT_EQUALS("[test.cpp:10]: Uninitialized member variable 'Fred::_code'\n", errout.str()); + ASSERT_EQUALS("[test.cpp:10]: (always) Uninitialized member variable 'Fred::_code'\n", errout.str()); } void uninitVarStream() diff --git a/test/testconstructors.cpp b/test/testconstructors.cpp index c15304100..79ceb2d3e 100644 --- a/test/testconstructors.cpp +++ b/test/testconstructors.cpp @@ -84,7 +84,7 @@ private: " int i;\n" "};\n"); std::string actual(errout.str()); - std::string expected("[test.cpp:1]: The class 'Fred' has no constructor\n"); + std::string expected("[test.cpp:1]: (style) The class 'Fred' has no constructor\n"); ASSERT_EQUALS(expected, actual); } @@ -97,7 +97,7 @@ private: " Fred() { }\n" " int i;\n" "};\n"); - ASSERT_EQUALS(std::string("[test.cpp:4]: Uninitialized member variable 'Fred::i'\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:4]: (always) Uninitialized member variable 'Fred::i'\n"), errout.str()); } @@ -111,7 +111,7 @@ private: "};\n" "Fred::Fred()\n" "{ }\n"); - ASSERT_EQUALS(std::string("[test.cpp:7]: Uninitialized member variable 'Fred::i'\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:7]: (always) Uninitialized member variable 'Fred::i'\n"), errout.str()); } @@ -130,7 +130,7 @@ private: "{\n" " i = _i;\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:8]: Uninitialized member variable 'Fred::i'\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:8]: (always) Uninitialized member variable 'Fred::i'\n"), errout.str()); } @@ -197,7 +197,7 @@ private: " void operator=() { }\n" " int i;\n" "};\n"); - ASSERT_EQUALS(std::string("[test.cpp:5]: Uninitialized member variable 'Fred::i'\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:5]: (always) Uninitialized member variable 'Fred::i'\n"), errout.str()); } void initvar_operator_eq3() diff --git a/test/testdivision.cpp b/test/testdivision.cpp index 3b662c80f..7b57017eb 100644 --- a/test/testdivision.cpp +++ b/test/testdivision.cpp @@ -76,7 +76,7 @@ private: " unsigned int uvar = 2;\n" " return ivar / uvar;\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:5]: Warning: Division with signed and unsigned operators\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:5]: (all style) Warning: Division with signed and unsigned operators\n"), errout.str()); } void division2() @@ -87,7 +87,7 @@ private: " unsigned int uvar = 2;\n" " return uvar / ivar;\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:5]: Warning: Division with signed and unsigned operators\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:5]: (all style) Warning: Division with signed and unsigned operators\n"), errout.str()); } void division3() @@ -100,7 +100,7 @@ private: " u32 uvar = 2;\n" " return uvar / ivar;\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:7]: Warning: Division with signed and unsigned operators\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:7]: (all style) Warning: Division with signed and unsigned operators\n"), errout.str()); } void division4() @@ -136,7 +136,7 @@ private: " unsigned int val = 32;\n" " int i = val / -2;\n" ); - ASSERT_EQUALS(std::string("[test.cpp:4]: Unsigned division. The result will be wrong.\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:4]: (always) Unsigned division. The result will be wrong.\n"), errout.str()); } void division7() @@ -146,7 +146,7 @@ private: " unsigned int val = 32;\n" " int i = -96 / val;\n" ); - ASSERT_EQUALS(std::string("[test.cpp:4]: Unsigned division. The result will be wrong.\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:4]: (always) Unsigned division. The result will be wrong.\n"), errout.str()); } }; diff --git a/test/testincompletestatement.cpp b/test/testincompletestatement.cpp index b552fffa2..3027c9819 100644 --- a/test/testincompletestatement.cpp +++ b/test/testincompletestatement.cpp @@ -86,7 +86,7 @@ private: " ;\"abc\";\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:3]: Redundant code: Found a statement that begins with string constant\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:3]: (style) Redundant code: Found a statement that begins with string constant\n"), errout.str()); } }; diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 1df9eac34..285ab210e 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -185,7 +185,7 @@ private: "{\n" " int *a = new int[10];\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:4]: Memory leak: a\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:4]: (always) Memory leak: a\n"), errout.str()); } void simple2() @@ -216,7 +216,7 @@ private: " char *s = new char[100];\n" " return 0;\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:4]: Memory leak: s\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:4]: (always) Memory leak: s\n"), errout.str()); } @@ -353,7 +353,7 @@ private: " }\n" " free(str);\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:6]: Memory leak: str\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:6]: (always) Memory leak: str\n"), errout.str()); } @@ -379,7 +379,7 @@ private: " return;\n" " }\n" "}\n", true); - ASSERT_EQUALS(std::string("[test.cpp:9]: Memory leak: str\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:9]: (always) Memory leak: str\n"), errout.str()); } @@ -429,7 +429,7 @@ private: " }\n" " return NULL;\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:8]: Memory leak: s\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:8]: (always) Memory leak: s\n"), errout.str()); } @@ -489,7 +489,7 @@ private: " return;\n" " foo(p);\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:6]: Memory leak: p\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:6]: (always) Memory leak: p\n"), errout.str()); } void if2() @@ -591,7 +591,7 @@ private: " }\n" " free(c);\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:11]: Memory leak: c\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:11]: (always) Memory leak: c\n"), errout.str()); } void if9() @@ -661,7 +661,7 @@ private: " free(str);\n" " }\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:7]: Memory leak: str\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:7]: (always) Memory leak: str\n"), errout.str()); } @@ -676,7 +676,7 @@ private: " }\n" " free(str);\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:4]: Memory leak: str\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:4]: (always) Memory leak: str\n"), errout.str()); } @@ -692,7 +692,7 @@ private: " }\n" " free(str);\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:4]: Memory leak: str\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:4]: (always) Memory leak: str\n"), errout.str()); } @@ -721,7 +721,7 @@ private: " str = strdup(a[i]);\n" " }\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:8]: Memory leak: str\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:8]: (always) Memory leak: str\n"), errout.str()); } @@ -737,7 +737,7 @@ private: " free(str);\n" " }\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:7]: Memory leak: str\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:7]: (always) Memory leak: str\n"), errout.str()); } @@ -802,7 +802,7 @@ private: "\n" " return a;\n" "}\n", true); - ASSERT_EQUALS(std::string("[test.cpp:11]: Memory leak: a\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:11]: (always) Memory leak: a\n"), errout.str()); } @@ -821,7 +821,7 @@ private: " while (!str);\n" " return str;\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:5]: Memory leak: str\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:5]: (always) Memory leak: str\n"), errout.str()); } @@ -858,7 +858,7 @@ private: check(code.c_str(), false); ASSERT_EQUALS("", errout.str()); check(code.c_str(), true); - ASSERT_EQUALS("[test.cpp:12]: Memory leak: str\n", errout.str()); + ASSERT_EQUALS("[test.cpp:12]: (always) Memory leak: str\n", errout.str()); } void switch3() @@ -876,7 +876,7 @@ private: " }\n" " delete [] str;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:9]: Memory leak: str\n", errout.str()); + ASSERT_EQUALS("[test.cpp:9]: (always) Memory leak: str\n", errout.str()); } @@ -906,7 +906,7 @@ private: " delete abc;\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:6]: Memory leak: abc\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:6]: (always) Memory leak: abc\n"), errout.str()); } void ret3() @@ -916,7 +916,7 @@ private: " FILE *filep = fopen(\"myfile.txt\",\"w\");\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:4]: Resource leak: filep\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:4]: (always) Resource leak: filep\n"), errout.str()); } void ret4() @@ -926,7 +926,7 @@ private: " FILE *p = popen( \"ls -l\", \"r\");\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:4]: Resource leak: p\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:4]: (always) Resource leak: p\n"), errout.str()); } void ret5() @@ -949,7 +949,7 @@ private: " int *a = new int[10];\n" " free(a);\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:4]: Mismatching allocation and deallocation: a\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:4]: (always) Mismatching allocation and deallocation: a\n"), errout.str()); } @@ -993,7 +993,7 @@ private: " char *p = new char[100];\n" " foo(p);\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:8]: Memory leak: p\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:8]: (always) Memory leak: p\n"), errout.str()); } @@ -1026,7 +1026,7 @@ private: " foo(p);\n" "}\n"); std::string err(errout.str()); - ASSERT_EQUALS(std::string("[test.cpp:9] -> [test.cpp:3]: Mismatching allocation and deallocation: str\n"), err); + ASSERT_EQUALS(std::string("[test.cpp:9] -> [test.cpp:3]: (always) Mismatching allocation and deallocation: str\n"), err); } @@ -1043,7 +1043,7 @@ private: " foo(p);\n" "}\n"); std::string err(errout.str()); - ASSERT_EQUALS(std::string("[test.cpp:10]: Memory leak: p\n"), err); + ASSERT_EQUALS(std::string("[test.cpp:10]: (always) Memory leak: p\n"), err); } @@ -1062,7 +1062,7 @@ private: " foo(p);\n" "}\n"); std::string err(errout.str()); - ASSERT_EQUALS(std::string("[test.cpp:11]: Memory leak: p\n"), err); + ASSERT_EQUALS(std::string("[test.cpp:11]: (always) Memory leak: p\n"), err); } @@ -1206,7 +1206,7 @@ private: " delete [] str2;\n" "}\n", true); - ASSERT_EQUALS(std::string("[test.cpp:1]: Memory leak: Fred::str1\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:1]: (always) Memory leak: Fred::str1\n"), errout.str()); } @@ -1231,7 +1231,7 @@ private: " free(str1);\n" "}\n", true); - ASSERT_EQUALS(std::string("[test.cpp:17]: Mismatching allocation and deallocation: Fred::str1\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:17]: (always) Mismatching allocation and deallocation: Fred::str1\n"), errout.str()); } void class3() @@ -1308,7 +1308,7 @@ private: "{\n" " char *str = new char[100];\n" "}\n"); - ASSERT_EQUALS("[test.cpp:10]: Memory leak: str\n", errout.str()); + ASSERT_EQUALS("[test.cpp:10]: (always) Memory leak: str\n", errout.str()); } void class6() @@ -1381,7 +1381,7 @@ private: " delete [] str;\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:5]: Memory leak: str\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:5]: (always) Memory leak: str\n"), errout.str()); } void throw2() @@ -1448,7 +1448,7 @@ private: std::string err(errout.str()); - ASSERT_EQUALS(std::string("[test.cpp:12]: Memory leak: s2\n"), err); + ASSERT_EQUALS(std::string("[test.cpp:12]: (always) Memory leak: s2\n"), err); } @@ -1460,7 +1460,7 @@ private: " a = realloc(a, 100);\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:5]: Memory leak: a\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:5]: (always) Memory leak: a\n"), errout.str()); } void realloc2() @@ -1485,7 +1485,7 @@ private: " free(a);\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:3]: Memory leak: a\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:3]: (always) Memory leak: a\n"), errout.str()); check("void foo()\n" "{\n" @@ -1554,7 +1554,7 @@ private: " char *a = reinterpret_cast(malloc(10));\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:4]: Memory leak: a\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:4]: (always) Memory leak: a\n"), errout.str()); } void cast2() @@ -1592,7 +1592,7 @@ private: " delete abc;\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:5]: Memory leak: abc.a\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:5]: (always) Memory leak: abc.a\n"), errout.str()); } @@ -1674,7 +1674,7 @@ private: " free(str);\n" " free(str);\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:5]: Deallocating a deallocated pointer: str\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:5]: (always) Deallocating a deallocated pointer: str\n"), errout.str()); } void freefree2() diff --git a/test/testother.cpp b/test/testother.cpp index 93a90336f..c7fc79f20 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -87,14 +87,14 @@ private: " delete p;\n" " }\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:3]: Redundant condition. It is safe to deallocate a NULL pointer\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:3]: (style) Redundant condition. It is safe to deallocate a NULL pointer\n"), errout.str()); check("void foo()\n" "{\n" " if (p)\n" " delete p;\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:3]: Redundant condition. It is safe to deallocate a NULL pointer\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:3]: (style) Redundant condition. It is safe to deallocate a NULL pointer\n"), errout.str()); } void unreachable1() @@ -138,7 +138,7 @@ private: " char buf[100];\n" " sprintf(buf,\"%s\",buf);\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:4]: Overlapping data buffer buf\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:4]: (always) Overlapping data buffer buf\n"), errout.str()); } void sprintf2() @@ -205,7 +205,7 @@ private: "{\n" " const char *p = \"/usr\" + '/';\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:3]: Unusual pointer arithmetic\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:3]: (always) Unusual pointer arithmetic\n"), errout.str()); } void strPlusChar2() @@ -216,7 +216,7 @@ private: " char ch = '/';\n" " const char *p = \"/usr\" + ch;\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:4]: Unusual pointer arithmetic\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:4]: (always) Unusual pointer arithmetic\n"), errout.str()); } void strPlusChar3() diff --git a/test/testredundantif.cpp b/test/testredundantif.cpp index b3c5f1117..7ce033ffe 100644 --- a/test/testredundantif.cpp +++ b/test/testredundantif.cpp @@ -66,7 +66,7 @@ private: " if (haystack.find(needle) != haystack.end())\n" " haystack.remove(needle);" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:3]: Redundant condition. The remove function in the STL will not do anything if element doesn't exist\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:3]: (style) Redundant condition. The remove function in the STL will not do anything if element doesn't exist\n"), errout.str()); } void remove2() @@ -78,7 +78,7 @@ private: " haystack.remove(needle);\n" " }\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:3]: Redundant condition. The remove function in the STL will not do anything if element doesn't exist\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:3]: (style) Redundant condition. The remove function in the STL will not do anything if element doesn't exist\n"), errout.str()); } }; diff --git a/test/testunusedprivfunc.cpp b/test/testunusedprivfunc.cpp index f48a9da37..e118a3e02 100644 --- a/test/testunusedprivfunc.cpp +++ b/test/testunusedprivfunc.cpp @@ -78,7 +78,7 @@ private: "unsigned int Fred::f()\n" "{ }\n"); - ASSERT_EQUALS(std::string("[test.cpp:4]: Unused private function 'Fred::f'\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:4]: (style) Unused private function 'Fred::f'\n"), errout.str()); } @@ -108,7 +108,7 @@ private: "Fred::Fred()\n" "{}\n"); - ASSERT_EQUALS(std::string("[test.cpp:6]: Unused private function 'Fred::get'\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:6]: (style) Unused private function 'Fred::get'\n"), errout.str()); } diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index ebdd1c3f7..2f6b04a41 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -69,9 +69,9 @@ private: " int b;\n" " int c;\n" "};\n"); - ASSERT_EQUALS(std::string("[test.cpp:3]: struct or union member 'abc::a' is never used\n" - "[test.cpp:4]: struct or union member 'abc::b' is never used\n" - "[test.cpp:5]: struct or union member 'abc::c' is never used\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:3]: (style) struct or union member 'abc::a' is never used\n" + "[test.cpp:4]: (style) struct or union member 'abc::b' is never used\n" + "[test.cpp:5]: (style) struct or union member 'abc::c' is never used\n"), errout.str()); } void structmember2() diff --git a/tools/errmsg.cpp b/tools/errmsg.cpp index 8b2195dd5..743f05b3d 100644 --- a/tools/errmsg.cpp +++ b/tools/errmsg.cpp @@ -36,6 +36,8 @@ public: void generateDoc(std::ostream &ostr, Settings i) const; + std::string stringifySettings(bool text) const; + private: std::string _funcname; std::string _msg; @@ -209,6 +211,8 @@ std::string Message::msg(bool code) const return ret; } +static std::string stringifySeverity(); + void Message::generateCode(std::ostream &ostr) const { bool loc = bool(_msg.substr(0, 4) != "[%1]"); @@ -228,6 +232,7 @@ void Message::generateCode(std::ostream &ostr) const ostr << " return "; if (loc) ostr << "msg1(tokenizer, Location) + "; + ostr << " std::string(\"(" << stringifySettings(true) << ") \") + "; ostr << msg(true); if (_details.empty()) ostr << ";\n"; @@ -251,26 +256,7 @@ void Message::generateCode(std::ostream &ostr) const ostr << "const Settings &s"; ostr << ")" << std::endl; ostr << " {\n"; - ostr << " return "; - switch (_settings) - { - case always: - ostr << "true"; - break; - case all: - ostr << "s._showAll"; - break; - case style: - ostr << "s._checkCodingStyle"; - break; - case style_all: - ostr << "s._showAll & s._checkCodingStyle"; - break; - case never: - ostr << "false"; - break; - } - ostr << ";\n"; + ostr << " return " << stringifySettings(false) << ";\n"; ostr << " }\n\n"; } @@ -283,6 +269,23 @@ void Message::generateDoc(std::ostream &ostr, Message::Settings i) const } +std::string Message::stringifySettings(bool text) const +{ + switch (_settings) + { + case always: + return text ? "always" : "true"; + case all: + return text ? "all" : "s._showAll"; + case style: + return text ? "style" : "s._checkCodingStyle"; + case style_all: + return text ? "all style" : "s._checkCodingStyle || s._showAll"; + case never: + return text ? "never" : "false"; + } + return ""; +}