Fixed #3281 (Tokenizer::simplifyKnownVariables : static constant value is not used)

This commit is contained in:
Daniel Marjamäki 2011-11-20 21:50:26 +01:00
parent 9a8c48b36e
commit 004dcf834b
2 changed files with 16 additions and 1 deletions

View File

@ -6477,7 +6477,7 @@ bool Tokenizer::simplifyKnownVariables()
Token *tok1 = tok;
// start of statement
if (tok != _tokens && !Token::Match(tok->previous(),"[;{}]"))
if (tok != _tokens && !Token::Match(tok->previous(),";|{|}|private:|protected:|public:"))
continue;
// skip "const" and "static"
while (tok->str() == "const" || tok->str() == "static")

View File

@ -136,6 +136,7 @@ private:
TEST_CASE(simplifyKnownVariables42); // ticket #2031 - known string value after strcpy
TEST_CASE(simplifyKnownVariables43);
TEST_CASE(simplifyKnownVariables44); // ticket #3117 - don't simplify static variables
TEST_CASE(simplifyKnownVariables45); // ticket #3281 - static constant variable not simplified
TEST_CASE(simplifyKnownVariablesBailOutAssign1);
TEST_CASE(simplifyKnownVariablesBailOutAssign2);
TEST_CASE(simplifyKnownVariablesBailOutFor1);
@ -2147,6 +2148,20 @@ private:
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
}
void simplifyKnownVariables45() {
const char code[] = "class Fred {\n"
"private:\n"
" const static int NUM = 2;\n"
" int array[NUM];\n"
"}";
const char expected[] = "class Fred {\n"
"private:\n"
";\n"
"int array [ 2 ] ;\n"
"}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
}
void simplifyKnownVariablesBailOutAssign1() {
const char code[] = "int foo() {\n"
" int i; i = 0;\n"