From c4763b901063bf83feb96328c24fe14a4d352641 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 12 Jun 2013 06:45:26 +0200 Subject: [PATCH] Fixed #4463 (false positive: (style) Variable 'CHDERR_NOT_OPEN' hides enumerator with same name) --- lib/tokenize.cpp | 4 ++-- test/testsimplifytokens.cpp | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index d260eb93b..6d981396d 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7142,7 +7142,7 @@ bool Tokenizer::duplicateDefinition(Token ** tokPtr, const Token * name) const const Token *back = tok; while (back && back->isName()) back = back->previous(); - if (!back || Token::Match(back, "[(,;{}] !!return")) { + if (!back || (Token::Match(back, "[(,;{}]") && !Token::Match(back->next(),"return|throw"))) { duplicateEnumError(*tokPtr, name, "Variable"); return true; } @@ -7461,7 +7461,7 @@ void Tokenizer::simplifyEnum() tok3 = tok3->link(); // skip inner scopes else if (tok3->isName() && enumValues.find(tok3->str()) != enumValues.end()) { const Token *prev = tok3->previous(); - if ((prev->isName() && !Token::Match(prev, "return|case")) || + if ((prev->isName() && !Token::Match(prev, "return|case|throw")) || prev->str() == "*" || Token::Match(prev, "& %type% =")) { // variable declaration? diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index b6664aad0..7ffd8335b 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -361,6 +361,7 @@ private: TEST_CASE(enum35); // ticket #3953 (avoid simplification of type) TEST_CASE(enum36); // ticket #4378 TEST_CASE(enum37); // ticket #4280 (shadow variable) + TEST_CASE(enum38); // ticket #4463 (when throwing enum id, don't warn about shadow variable) TEST_CASE(enumscope1); // ticket #3949 TEST_CASE(duplicateDefinition); // ticket #3565 @@ -7362,6 +7363,12 @@ private: ASSERT_EQUALS("void f ( ) { int & a = x ; }", checkSimplifyEnum(code4)); } + void enum38() { // #4463 + const char code[] = "enum { a,b }; void f() { throw a; }"; + checkSimplifyEnum(code); + ASSERT_EQUALS("", errout.str()); + } + 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; }";