Fixed #3909: Handle constructor syntax initialization.

This commit is contained in:
PKEuS 2012-06-24 09:57:17 -07:00
parent 25b24d149f
commit f5c42660de
2 changed files with 18 additions and 3 deletions

View File

@ -105,9 +105,12 @@ void CheckIO::checkFileUsage()
if (!var || !var->varId() || !Token::Match(var->typeStartToken(), "FILE *")) if (!var || !var->varId() || !Token::Match(var->typeStartToken(), "FILE *"))
continue; continue;
if (var->isLocal()) if (var->isLocal()) {
if (var->nameToken()->strAt(1) == "(") // initialize by calling "ctor"
filepointers.insert(std::make_pair(var->varId(), Filepointer(UNKNOWN)));
else
filepointers.insert(std::make_pair(var->varId(), Filepointer(CLOSED))); filepointers.insert(std::make_pair(var->varId(), Filepointer(CLOSED)));
else { } else {
filepointers.insert(std::make_pair(var->varId(), Filepointer(UNKNOWN))); filepointers.insert(std::make_pair(var->varId(), Filepointer(UNKNOWN)));
// TODO: If all fopen calls we find open the file in the same type, we can set Filepointer::mode // TODO: If all fopen calls we find open the file in the same type, we can set Filepointer::mode
} }

View File

@ -265,6 +265,18 @@ private:
" fwrite(buffer, 5, 6, f);\n" " fwrite(buffer, 5, 6, f);\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("void foo() {\n"
" FILE* f;\n"
" fwrite(buffer, 5, 6, f);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Used file that is not opened.\n", errout.str());
check("void foo() {\n"
" FILE* f(stdout);\n"
" fwrite(buffer, 5, 6, f);\n"
"}");
ASSERT_EQUALS("", errout.str());
} }
void fileIOwithoutPositioning() { void fileIOwithoutPositioning() {