Tokenizer: better handling for switch/break in the simplifyKnownVariables. Ticket: #2324

This commit is contained in:
Daniel Marjamäki 2010-12-18 11:14:31 +01:00
parent f6c00fc478
commit 8ffc2b0ac6
2 changed files with 44 additions and 1 deletions

View File

@ -6076,7 +6076,9 @@ bool Tokenizer::simplifyKnownVariables()
if (Token::Match(tok3, "; %type% : ;"))
break;
// Stop if return is found ..
// Stop if return or break is found ..
if (tok3->str() == "break")
break;
if (indentlevel3 == 1)
{
if (tok3->str() == "return")

View File

@ -127,6 +127,7 @@ private:
TEST_CASE(simplifyKnownVariablesBailOutFor3);
TEST_CASE(simplifyKnownVariablesBailOutMemberFunction);
TEST_CASE(simplifyKnownVariablesBailOutConditionalIncrement);
TEST_CASE(simplifyKnownVariablesBailOutSwitchBreak); // ticket #2324
TEST_CASE(varid1);
TEST_CASE(varid2);
@ -1951,6 +1952,46 @@ private:
ASSERT_EQUALS("", errout.str()); // no debug warnings
}
void simplifyKnownVariablesBailOutSwitchBreak()
{
// Ticket #2324
const char code[] = "int f(char *x) {\n"
" char *p;\n"
" char *q;\n"
"\n"
" switch (x & 0x3)\n"
" {\n"
" case 1:\n"
" p = x;\n"
" x = p;\n"
" break;\n"
" case 2:\n"
" q = x;\n" // x is not equal with p
" x = q;\n"
" break;\n"
" }\n"
"}\n";
const char expected[] = "int f ( char * x ) {\n"
"char * p ;\n"
"char * q ;\n"
"\n"
"switch ( x & 3 )\n"
"{\n"
"case 1 : ;\n"
"p = x ;\n"
"x = p ;\n"
"break ;\n"
"case 2 : ;\n"
"q = x ;\n"
"x = q ;\n"
"break ;\n"
"}\n"
"}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code,true));
}
std::string tokenizeDebugListing(const std::string &code, bool simplify = false)
{
errout.str("");