From 439b8c40518de48ab19072a2b291c630b9bea412 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 20 Feb 2009 17:27:57 +0000 Subject: [PATCH] simplify known variables: variable used as array index --- src/tokenize.cpp | 2 +- test/testtokenize.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 65ac098d1..003e7a3b4 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -1638,7 +1638,7 @@ bool Tokenizer::simplifyKnownVariables() } // Variable is used in calculation.. - if (Token::Match(tok3, "[=+-*/] %varid% [+-*/;]", varid)) + if (Token::Match(tok3, "[=+-*/[] %varid% [+-*/;]]", varid)) { tok3 = tok3->next(); tok3->str(tok2->strAt(2)); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index d0933f3ee..7ab9e29a4 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -83,6 +83,7 @@ private: TEST_CASE(simplifyKnownVariables3); TEST_CASE(simplifyKnownVariables4); TEST_CASE(simplifyKnownVariables5); + TEST_CASE(simplifyKnownVariables6); TEST_CASE(multiCompare); @@ -510,6 +511,29 @@ private: ASSERT_EQUALS(std::string(" void f ( ) { int a = 4 ; if ( a = 5 ) ; }"), ostr.str()); } + void simplifyKnownVariables6() + { + const char code[] = "void f()\n" + "{\n" + " char str[2];" + " int a = 4;\n" + " str[a] = 0;\n" + "}\n"; + + // tokenize.. + OurTokenizer tokenizer; + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + + tokenizer.setVarId(); + tokenizer.simplifyKnownVariables(); + + std::ostringstream ostr; + for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) + ostr << " " << tok->str(); + ASSERT_EQUALS(std::string(" void f ( ) { char str [ 2 ] ; int a = 4 ; str [ 4 ] = 0 ; }"), ostr.str()); + } + void multiCompare() { // Test for found