From a9bd6cf3dfef118ae4b117841ef89ca21808a586 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 16 Jun 2013 08:13:41 +0200 Subject: [PATCH] Fixed #4857 (False Positive: function argument hides enumerator of same type) --- lib/tokenize.cpp | 21 +++++++++++++++++++++ test/testsimplifytokens.cpp | 4 ++++ 2 files changed, 25 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index f238c56ee..54fa82ced 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7448,10 +7448,31 @@ void Tokenizer::simplifyEnum() // are there shadow arguments? if (Token::simpleMatch(tok2->previous(), ") {") || Token::simpleMatch(tok2->tokAt(-2), ") const {")) { + // Determine if this is a executable scope.. + bool executableScope = false; + { + const Token *prev = tok2->previous()->link(); + while (prev) { + if (prev->str() == "}") + prev = prev->link(); + else if (prev->str() == "{") { + while ((prev = prev->previous()) && (prev->isName())); + if (!prev || prev->str() == ")") + break; + } + prev = prev->previous(); + } + + if (prev) + executableScope = true; + } + std::set shadowArg; for (const Token* arg = tok2; arg && arg->str() != "("; arg = arg->previous()) { if (Token::Match(arg->previous(), "%type%|*|& %type% [,)]") && enumValues.find(arg->str()) != enumValues.end()) { + if (executableScope && Token::Match(arg->previous(), "[*&]")) + continue; shadowArg.insert(arg->str()); if (inScope && _settings->isEnabled("style")) { const EnumValue enumValue = enumValues.find(arg->str())->second; diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 933ee8b71..d9caaf3fe 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -7383,6 +7383,10 @@ private: const char code4[] = "enum { a, b }; void f() { int &a=x; }"; ASSERT_EQUALS("void f ( ) { int & a = x ; }", checkSimplifyEnum(code4)); + + // #4857 - not shadow variable + checkSimplifyEnum("enum { a,b }; void f() { if (x) { } else if ( x & a ) {} }"); + ASSERT_EQUALS("", errout.str()); } void enum38() { // #4463