From 8954cc09847c561d35943784149c31f6c120247e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 3 Jun 2013 19:18:51 +0200 Subject: [PATCH] Fixed #4280 (Tokenizer::simplifyEnum: don't simplify shadow variables) --- lib/tokenize.cpp | 16 ++++++++++++++++ test/testsimplifytokens.cpp | 6 ++++++ 2 files changed, 22 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index db50b8685..78d2e433e 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7432,6 +7432,22 @@ void Tokenizer::simplifyEnum() // Create a copy of the shadow ids for the inner scope if (!shadowId.empty()) shadowId.push(shadowId.top()); + + if (Token::simpleMatch(tok2->previous(), ") {") || Token::simpleMatch(tok2->tokAt(-2), ") const {")) { + std::set shadowArg; + for (const Token* arg = tok2; arg && arg->str() != "("; arg = arg->previous()) { + if (Token::Match(arg, "%type% [,)]") && enumValues.find(arg->str()) != enumValues.end()) { + shadowArg.insert(arg->str()); + } + } + if (!shadowArg.empty()) { + if (shadowId.empty()) + shadowId.push(shadowArg); + else + shadowId.top().insert(shadowArg.begin(), shadowArg.end()); + } + } + } // Function head } else if (Token::Match(tok2, "%var% (")) { diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 6779e5693..b1d6f7ce2 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -360,6 +360,7 @@ private: TEST_CASE(enum34); // ticket #4141 (division by zero) TEST_CASE(enum35); // ticket #3953 (avoid simplification of type) TEST_CASE(enum36); // ticket #4378 + TEST_CASE(enum37); // ticket #4280 (shadow variable) TEST_CASE(enumscope1); // ticket #3949 TEST_CASE(duplicateDefinition); // ticket #3565 @@ -7333,6 +7334,11 @@ private: ASSERT_EQUALS("struct X { X ( int ) { int y ; y = ( int ) 1 ; } } ;", checkSimplifyEnum(code)); } + void enum37() { // #4280 + const char code[] = "enum { a, b }; void f(int a) { return a + 1; }"; + ASSERT_EQUALS("void f ( int a ) { return a + 1 ; }", checkSimplifyEnum(code)); + } + void enumscope1() { // #3949 - don't simplify enum from one function in another function const char code[] = "void foo() { enum { A = 0, B = 1 }; }\n" "void bar() { int a = A; }";