Properly handle return/break/continue in CheckIO::checkFileUsage() (#4466)

This commit is contained in:
PKEuS 2013-02-15 08:30:43 -08:00
parent 017b4a8a7f
commit e2655da1ec
2 changed files with 21 additions and 1 deletions

View File

@ -140,6 +140,13 @@ void CheckIO::checkFileUsage()
i->second.lastOperation = Filepointer::UNKNOWN_OP;
}
}
} else if (tok->str() == "return" || tok->str() == "continue" || tok->str() == "break") { // Reset upon return, continue or break
for (std::map<unsigned int, Filepointer>::iterator i = filepointers.begin(); i != filepointers.end(); ++i) {
i->second.mode_indent = 0;
i->second.mode = UNKNOWN;
i->second.op_indent = 0;
i->second.lastOperation = Filepointer::UNKNOWN_OP;
}
} else if (tok->varId() && Token::Match(tok, "%var% =") && (tok->strAt(2) != "fopen" && tok->strAt(2) != "freopen" && tok->strAt(2) != "tmpfile")) {
std::map<unsigned int, Filepointer>::iterator i = filepointers.find(tok->varId());
if (i != filepointers.end()) {
@ -187,7 +194,7 @@ void CheckIO::checkFileUsage()
if (tok->str() == "ungetc" && fileTok)
fileTok = fileTok->nextArgument();
operation = Filepointer::UNIMPORTANT;
} else if (!Token::Match(tok, "if|for|while|catch|return")) {
} else if (!Token::Match(tok, "if|for|while|catch|switch")) {
const Token* const end2 = tok->linkAt(1);
for (const Token* tok2 = tok->tokAt(2); tok2 != end2; tok2 = tok2->next()) {
if (tok2->varId() && filepointers.find(tok2->varId()) != filepointers.end()) {

View File

@ -324,6 +324,19 @@ private:
" fprintf(fp, \"Here's the output.\\n\");\n"
"}");
ASSERT_EQUALS("[test.cpp:11]: (error) Used file that is not opened.\n", errout.str());
// #4466
check("void chdcd_parse_nero(FILE *infile) {\n"
" switch (mode) {\n"
" case 0x0300:\n"
" fclose(infile);\n"
" return;\n"
" case 0x0500:\n"
" fclose(infile);\n"
" return;\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void fileIOwithoutPositioning() {