From 004dcf834b4fe898168a09088c9cdd4dffb16dbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 20 Nov 2011 21:50:26 +0100 Subject: [PATCH] Fixed #3281 (Tokenizer::simplifyKnownVariables : static constant value is not used) --- lib/tokenize.cpp | 2 +- test/testtokenize.cpp | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 2cf75d4a6..774ec66de 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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") diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 3a482503d..2dd4918dc 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -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"