improved check: checkCastIntToCharAndBack. The check now handles (cin.get() != EOF) patterns.

This commit is contained in:
Ettl Martin 2013-03-04 14:25:35 +01:00
parent dc65667cec
commit c9b519bcd1
2 changed files with 79 additions and 0 deletions

View File

@ -68,6 +68,19 @@ void CheckOther::checkCastIntToCharAndBack()
checkCastIntToCharAndBackError(tok, tok->strAt(2));
}
}
} else if (Token::Match(tok, "EOF %comp% ( %var% = std :: cin . get (") || Token::Match(tok, "EOF %comp% ( %var% = cin . get (")) {
tok = tok->tokAt(3);
if (tok && tok->varId()) {
const Variable *var = tok->variable();
if (var && var->typeEndToken()->str() == "char" && !var->typeEndToken()->isSigned()) {
checkCastIntToCharAndBackError(tok, "cin.get");
}
}
} else if (Token::Match(tok, "%var% = std :: cin . get (") || Token::Match(tok, "%var% = cin . get (")) {
const Variable *var = tok->variable();
if (var && var->typeEndToken()->str() == "char" && !var->typeEndToken()->isSigned()) {
vars[tok->varId()] = "cin.get";
}
}
if (Token::Match(tok, "%var% %comp% EOF")) {
if (vars.find(tok->varId()) != vars.end()) {

View File

@ -7265,6 +7265,72 @@ private:
"} while (EOF != i)"
"}\n");
ASSERT_EQUALS("", errout.str());
// cin.get()
check("void f(){\n"
" char ch;\n"
" while ((ch = std::cin.get()) != EOF) {\n"
" std::cout << ch;\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Storing cin.get() return value in char variable and then comparing with EOF.\n", errout.str());
check("void f(){\n"
" char ch;\n"
" while ((ch = cin.get()) != EOF) {\n"
" std::cout << ch;\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Storing cin.get() return value in char variable and then comparing with EOF.\n", errout.str());
check("void f(){\n"
" int i;\n"
" while ((i = cin.get()) != EOF) {\n"
" std::cout << i;\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(){\n"
" int i;\n"
" while ((i = std::cin.get()) != EOF) {\n"
" std::cout << i;\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(){\n"
" char ch;\n"
" while ( EOF != (ch = std::cin.get()) ) {\n"
" std::cout << ch;\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Storing cin.get() return value in char variable and then comparing with EOF.\n", errout.str());
check("void f(){\n"
" char ch;\n"
" while ( EOF != (ch = cin.get()) ) {\n"
" std::cout << ch;\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Storing cin.get() return value in char variable and then comparing with EOF.\n", errout.str());
check("void f(){\n"
" int i;\n"
" while ( EOF != (i = cin.get()) ) {\n"
" std::cout << i;\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(){\n"
" int i;\n"
" while ( EOF != (i = std::cin.get()) ) {\n"
" std::cout << i;\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
};