Uninitialized variables: Better testing of function handling

This commit is contained in:
Daniel Marjamäki 2012-12-21 19:32:56 +01:00
parent dc6aa92f3b
commit 2050cd71db
2 changed files with 52 additions and 28 deletions

View File

@ -1315,7 +1315,7 @@ bool CheckUninitVar::isVariableUsage(const Scope* scope, const Token *vartok, bo
return true;
if (pointer && Token::Match(argStart, "%type% * %var% [,)]"))
return true;
if (!(pointer && address) && Token::Match(argStart, "const %type% * %var% [,)]"))
if ((pointer || address) && Token::Match(argStart, "const %type% * %var% [,)]"))
return true;
}
}

View File

@ -2387,52 +2387,76 @@ private:
// Handling of function calls
void uninitvar2_func() {
checkUninitVar2("void a(char c);\n"
// non-pointer variable
checkUninitVar2("void a(char c);\n" // value => error
"void b() {\n"
" char c;\n"
" a(c);\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str());
checkUninitVar2("void a(const char *s);\n"
checkUninitVar2("void a(const char c);\n" // const value => error
"void b() {\n"
" char c;\n"
" a(c);\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str());
checkUninitVar2("void a(char *c);\n" // address => no error
"void b() {\n"
" char c;\n"
" a(&c);\n"
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVar2("void a(const char *c);\n" // const address => error
"void b() {\n"
" char c;\n"
" a(&c);\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str());
checkUninitVar2("void a(char *s);\n"
// pointer variable
checkUninitVar2("void a(char c);\n" // value => error
"void b() {\n"
" char *c;\n"
" a(*c);\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str());
checkUninitVar2("void a(const char c);\n" // const value => error
"void b() {\n"
" char c;\n"
" a(*c);\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str());
checkUninitVar2("void a(char *c);\n" // address => error
"void b() {\n"
" char *c;\n"
" a(c);\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str());
checkUninitVar2("void a(const char *c);\n" // const address => error
"void b() {\n"
" char *c;\n"
" a(c);\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str());
checkUninitVar2("void a(char **c);\n" // address of pointer => no error
"void b() {\n"
" char *c;\n"
" a(&c);\n"
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVar2("void a(const ABC *abc);\n"
checkUninitVar2("void a(const char **c);\n" // const address of pointer => no error
"void b() {\n"
" ABC abc;\n"
" a(&abc);\n"
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVar2("void a(const ABC *abc);\n"
"void b() {\n"
" ABC abc;\n"
" a(&abc);\n"
"}", "test.c");
ASSERT_EQUALS("[test.c:4]: (error) Uninitialized variable: abc\n", errout.str());
checkUninitVar2("void a(char *p);\n"
"void b() {\n"
" char *s;\n"
" a(s);\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: s\n", errout.str());
checkUninitVar2("void a(const char **p);\n"
"void b() {\n"
" const char *s;\n"
" a(&s);\n"
" const char *c;\n"
" a(&c);\n"
"}");
ASSERT_EQUALS("", errout.str());
}