From 8ffc2b0ac6567fd35691a87b2fd5a86fcbd08bab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 18 Dec 2010 11:14:31 +0100 Subject: [PATCH] Tokenizer: better handling for switch/break in the simplifyKnownVariables. Ticket: #2324 --- lib/tokenize.cpp | 4 +++- test/testtokenize.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index c06cc5fb4..7c7ed8840 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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") diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 1f985ff50..31494532c 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -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("");