Message refactorization: checkuninitvar.cpp, checkunusedfunctions.cpp, checkunusedvar.cpp
This commit is contained in:
parent
d7b658a5aa
commit
24b98feadb
|
@ -1337,12 +1337,12 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer) const
|
||||||
|
|
||||||
void CheckUninitVar::uninitstringError(const Token *tok, const std::string &varname, bool strncpy_)
|
void CheckUninitVar::uninitstringError(const Token *tok, const std::string &varname, bool strncpy_)
|
||||||
{
|
{
|
||||||
reportError(tok, Severity::error, "uninitstring", "Dangerous usage of '" + varname + "'" + (strncpy_ ? " (strncpy doesn't always 0-terminate it)" : " (not 0-terminated)"));
|
reportError(tok, Severity::error, "uninitstring", "Dangerous usage of '" + varname + "'" + (strncpy_ ? " (strncpy doesn't always 0-terminate it)." : " (not 0-terminated)."));
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckUninitVar::uninitdataError(const Token *tok, const std::string &varname)
|
void CheckUninitVar::uninitdataError(const Token *tok, const std::string &varname)
|
||||||
{
|
{
|
||||||
reportError(tok, Severity::error, "uninitdata", "Data is allocated but not initialized: " + varname);
|
reportError(tok, Severity::error, "uninitdata", "Memory is allocated but not initialized: " + varname);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckUninitVar::uninitvarError(const Token *tok, const std::string &varname)
|
void CheckUninitVar::uninitvarError(const Token *tok, const std::string &varname)
|
||||||
|
|
|
@ -177,7 +177,7 @@ void CheckUnusedFunctions::unusedFunctionError(ErrorLogger * const errorLogger,
|
||||||
locationList.push_back(fileLoc);
|
locationList.push_back(fileLoc);
|
||||||
}
|
}
|
||||||
|
|
||||||
const ErrorLogger::ErrorMessage errmsg(locationList, Severity::style, "The function '" + funcname + "' is never used", "unusedFunction", false);
|
const ErrorLogger::ErrorMessage errmsg(locationList, Severity::style, "The function '" + funcname + "' is never used.", "unusedFunction", false);
|
||||||
if (errorLogger)
|
if (errorLogger)
|
||||||
errorLogger->reportErr(errmsg);
|
errorLogger->reportErr(errmsg);
|
||||||
else
|
else
|
||||||
|
|
|
@ -1026,17 +1026,17 @@ void CheckUnusedVar::unusedVariableError(const Token *tok, const std::string &va
|
||||||
|
|
||||||
void CheckUnusedVar::allocatedButUnusedVariableError(const Token *tok, const std::string &varname)
|
void CheckUnusedVar::allocatedButUnusedVariableError(const Token *tok, const std::string &varname)
|
||||||
{
|
{
|
||||||
reportError(tok, Severity::style, "unusedAllocatedMemory", "Variable '" + varname + "' is allocated memory that is never used");
|
reportError(tok, Severity::style, "unusedAllocatedMemory", "Variable '" + varname + "' is allocated memory that is never used.");
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckUnusedVar::unreadVariableError(const Token *tok, const std::string &varname)
|
void CheckUnusedVar::unreadVariableError(const Token *tok, const std::string &varname)
|
||||||
{
|
{
|
||||||
reportError(tok, Severity::style, "unreadVariable", "Variable '" + varname + "' is assigned a value that is never used");
|
reportError(tok, Severity::style, "unreadVariable", "Variable '" + varname + "' is assigned a value that is never used.");
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckUnusedVar::unassignedVariableError(const Token *tok, const std::string &varname)
|
void CheckUnusedVar::unassignedVariableError(const Token *tok, const std::string &varname)
|
||||||
{
|
{
|
||||||
reportError(tok, Severity::style, "unassignedVariable", "Variable '" + varname + "' is not assigned a value");
|
reportError(tok, Severity::style, "unassignedVariable", "Variable '" + varname + "' is not assigned a value.");
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
@ -1129,5 +1129,5 @@ void CheckUnusedVar::checkStructMemberUsage()
|
||||||
|
|
||||||
void CheckUnusedVar::unusedStructMemberError(const Token *tok, const std::string &structname, const std::string &varname)
|
void CheckUnusedVar::unusedStructMemberError(const Token *tok, const std::string &structname, const std::string &varname)
|
||||||
{
|
{
|
||||||
reportError(tok, Severity::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.");
|
||||||
}
|
}
|
||||||
|
|
|
@ -483,7 +483,7 @@ private:
|
||||||
" char *s = malloc(100);\n"
|
" char *s = malloc(100);\n"
|
||||||
" *s += 10;\n"
|
" *s += 10;\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (error) Data is allocated but not initialized: s\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: s\n", errout.str());
|
||||||
|
|
||||||
checkUninitVar("void f()\n"
|
checkUninitVar("void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
@ -1257,7 +1257,7 @@ private:
|
||||||
" strcpy(strMsg,buffer);\n"
|
" strcpy(strMsg,buffer);\n"
|
||||||
" free(buffer);\n"
|
" free(buffer);\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (error) Data is allocated but not initialized: buffer\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: buffer\n", errout.str());
|
||||||
|
|
||||||
// #3845
|
// #3845
|
||||||
checkUninitVar("int foo() {\n"
|
checkUninitVar("int foo() {\n"
|
||||||
|
@ -1292,42 +1292,42 @@ private:
|
||||||
" char *s = malloc(100);\n"
|
" char *s = malloc(100);\n"
|
||||||
" strcat(s, \"abc\");\n"
|
" strcat(s, \"abc\");\n"
|
||||||
"};\n");
|
"};\n");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (error) Data is allocated but not initialized: s\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: s\n", errout.str());
|
||||||
|
|
||||||
checkUninitVar("void f()\n"
|
checkUninitVar("void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" char *s = malloc(100);\n"
|
" char *s = malloc(100);\n"
|
||||||
" perror(s);\n"
|
" perror(s);\n"
|
||||||
"};");
|
"};");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (error) Data is allocated but not initialized: s\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: s\n", errout.str());
|
||||||
|
|
||||||
checkUninitVar("void f()\n"
|
checkUninitVar("void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" char *s1 = new char[10];\n"
|
" char *s1 = new char[10];\n"
|
||||||
" char *s2 = new char[strlen(s1)];\n"
|
" char *s2 = new char[strlen(s1)];\n"
|
||||||
"};\n");
|
"};\n");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (error) Data is allocated but not initialized: s1\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: s1\n", errout.str());
|
||||||
|
|
||||||
checkUninitVar("void f()\n"
|
checkUninitVar("void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" char *p = malloc(64);\n"
|
" char *p = malloc(64);\n"
|
||||||
" int x = p[0];\n"
|
" int x = p[0];\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (error) Data is allocated but not initialized: p\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: p\n", errout.str());
|
||||||
|
|
||||||
checkUninitVar("void f()\n"
|
checkUninitVar("void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" char *p = malloc(64);\n"
|
" char *p = malloc(64);\n"
|
||||||
" if (p[0]) { }\n"
|
" if (p[0]) { }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (error) Data is allocated but not initialized: p\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: p\n", errout.str());
|
||||||
|
|
||||||
checkUninitVar("void f()\n"
|
checkUninitVar("void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" char *p = malloc(64);\n"
|
" char *p = malloc(64);\n"
|
||||||
" return p[0];\n"
|
" return p[0];\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (error) Data is allocated but not initialized: p\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: p\n", errout.str());
|
||||||
|
|
||||||
checkUninitVar("void f()\n"
|
checkUninitVar("void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
@ -1407,7 +1407,7 @@ private:
|
||||||
" return;\n"
|
" return;\n"
|
||||||
" char c = *s;\n"
|
" char c = *s;\n"
|
||||||
"};\n");
|
"};\n");
|
||||||
ASSERT_EQUALS("[test.cpp:6]: (error) Data is allocated but not initialized: s\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:6]: (error) Memory is allocated but not initialized: s\n", errout.str());
|
||||||
|
|
||||||
// #3708 - false positive when using ptr typedef
|
// #3708 - false positive when using ptr typedef
|
||||||
checkUninitVar("void f() {\n"
|
checkUninitVar("void f() {\n"
|
||||||
|
@ -1572,7 +1572,7 @@ private:
|
||||||
" strncpy(a, s, 20);\n"
|
" strncpy(a, s, 20);\n"
|
||||||
" strncat(a, s, 20);\n"
|
" strncat(a, s, 20);\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:5]: (error) Dangerous usage of 'a' (strncpy doesn't always 0-terminate it)\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:5]: (error) Dangerous usage of 'a' (strncpy doesn't always 0-terminate it).\n", errout.str());
|
||||||
|
|
||||||
checkUninitVar("void f()\n"
|
checkUninitVar("void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
@ -1580,7 +1580,7 @@ private:
|
||||||
" strncpy(a, \"hello\", 3);\n"
|
" strncpy(a, \"hello\", 3);\n"
|
||||||
" strncat(a, \"world\", 20);\n"
|
" strncat(a, \"world\", 20);\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:5]: (error) Dangerous usage of 'a' (strncpy doesn't always 0-terminate it)\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:5]: (error) Dangerous usage of 'a' (strncpy doesn't always 0-terminate it).\n", errout.str());
|
||||||
|
|
||||||
checkUninitVar("void f()\n"
|
checkUninitVar("void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
@ -1623,7 +1623,7 @@ private:
|
||||||
" memset(a, 'a', 20);\n"
|
" memset(a, 'a', 20);\n"
|
||||||
" strcat(a, s);\n"
|
" strcat(a, s);\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (error) Dangerous usage of 'a' (not 0-terminated)\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (error) Dangerous usage of 'a' (not 0-terminated).\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string analyseFunctions(const char code[]) {
|
std::string analyseFunctions(const char code[]) {
|
||||||
|
|
|
@ -177,19 +177,19 @@ private:
|
||||||
void unusedError() {
|
void unusedError() {
|
||||||
check("void foo() {}\n"
|
check("void foo() {}\n"
|
||||||
"int main()\n");
|
"int main()\n");
|
||||||
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used.\n", errout.str());
|
||||||
|
|
||||||
check("void foo() const {}\n"
|
check("void foo() const {}\n"
|
||||||
"int main()\n");
|
"int main()\n");
|
||||||
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used.\n", errout.str());
|
||||||
|
|
||||||
check("void foo() const throw() {}\n"
|
check("void foo() const throw() {}\n"
|
||||||
"int main()\n");
|
"int main()\n");
|
||||||
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used.\n", errout.str());
|
||||||
|
|
||||||
check("void foo() throw() {}\n"
|
check("void foo() throw() {}\n"
|
||||||
"int main()\n");
|
"int main()\n");
|
||||||
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void unusedMain() {
|
void unusedMain() {
|
||||||
|
@ -217,7 +217,7 @@ private:
|
||||||
|
|
||||||
void returnRef() {
|
void returnRef() {
|
||||||
check("int& foo() {return x;}");
|
check("int& foo() {return x;}");
|
||||||
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void multipleFiles() {
|
void multipleFiles() {
|
||||||
|
@ -247,15 +247,15 @@ private:
|
||||||
// Check for unused functions..
|
// Check for unused functions..
|
||||||
c.check(this);
|
c.check(this);
|
||||||
|
|
||||||
ASSERT_EQUALS("[test1.cpp:1]: (style) The function 'f' is never used\n",errout.str());
|
ASSERT_EQUALS("[test1.cpp:1]: (style) The function 'f' is never used.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void lineNumber() {
|
void lineNumber() {
|
||||||
check("void foo() {}\n"
|
check("void foo() {}\n"
|
||||||
"void bar() {}\n"
|
"void bar() {}\n"
|
||||||
"int main()\n");
|
"int main()\n");
|
||||||
ASSERT_EQUALS("[test.cpp:2]: (style) The function 'bar' is never used\n"
|
ASSERT_EQUALS("[test.cpp:2]: (style) The function 'bar' is never used.\n"
|
||||||
"[test.cpp:1]: (style) The function 'foo' is never used\n", errout.str());
|
"[test.cpp:1]: (style) The function 'foo' is never used.\n", errout.str());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue