Fix #765 (divsion by zero not detected in std::cout stream)

http://sourceforge.net/apps/trac/cppcheck/ticket/765
This commit is contained in:
Reijo Tomperi 2009-10-06 09:14:59 +03:00
parent c0a46907c0
commit 0a7da96cb6
2 changed files with 34 additions and 2 deletions

View File

@ -3269,7 +3269,9 @@ bool Tokenizer::simplifyKnownVariables()
} }
// Variable is used in calculation.. // Variable is used in calculation..
if (Token::Match(tok3, "[=+-*/[] %varid% [+-*/;]]", varid)) if (Token::Match(tok3, "[=+-*/[] %varid% [+-*/;]]", varid) ||
Token::Match(tok3, "[=+-*/[] %varid% <<", varid) ||
Token::Match(tok3, "<< %varid% [+-*/;]]", varid))
{ {
tok3 = tok3->next(); tok3 = tok3->next();
tok3->str(value); tok3->str(value);
@ -3428,7 +3430,10 @@ bool Tokenizer::simplifyCalculations()
} }
// (1-2) // (1-2)
if (Token::Match(tok, "[[,(=<>+-*] %num% [+-*/] %num% [],);=<>+-*/]")) if (Token::Match(tok, "[[,(=<>+-*] %num% [+-*/] %num% [],);=<>+-*/]") ||
Token::Match(tok, "<< %num% [+-*/] %num% [],);=<>+-*/]") ||
Token::Match(tok, "[[,(=<>+-*] %num% [+-*/] %num% <<") ||
Token::Match(tok, "<< %num% [+-*/] %num% <<"))
{ {
tok = tok->next(); tok = tok->next();

View File

@ -81,6 +81,7 @@ private:
TEST_CASE(simplifyKnownVariables12); TEST_CASE(simplifyKnownVariables12);
TEST_CASE(simplifyKnownVariables13); TEST_CASE(simplifyKnownVariables13);
TEST_CASE(simplifyKnownVariables14); TEST_CASE(simplifyKnownVariables14);
TEST_CASE(simplifyKnownVariables15);
TEST_CASE(match1); TEST_CASE(match1);
@ -815,6 +816,32 @@ private:
ASSERT_EQUALS(code, simplifyKnownVariables(code)); ASSERT_EQUALS(code, simplifyKnownVariables(code));
} }
void simplifyKnownVariables15()
{
{
const char code[] = "int main()\n"
"{\n"
" int x=5;\n"
" std::cout << 10 / x << std::endl;\n"
"}\n";
ASSERT_EQUALS(
"int main ( ) { int x ; x = 5 ; std :: cout << 10 / 5 << std :: endl ; }",
simplifyKnownVariables(code));
}
{
const char code[] = "int main()\n"
"{\n"
" int x=5;\n"
" std::cout << x / ( x == 1 ) << std::endl;\n"
"}\n";
ASSERT_EQUALS(
"int main ( ) { int x ; x = 5 ; std :: cout << 5 / ( 5 == 1 ) << std :: endl ; }",
simplifyKnownVariables(code));
}
}
void match1() void match1()
{ {