Skip to content

Commit a9bd6cf

Browse files
committed
Fixed cppcheck-opensource#4857 (False Positive: function argument hides enumerator of same type)
1 parent b31c218 commit a9bd6cf

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

lib/tokenize.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7448,10 +7448,31 @@ void Tokenizer::simplifyEnum()
74487448

74497449
// are there shadow arguments?
74507450
if (Token::simpleMatch(tok2->previous(), ") {") || Token::simpleMatch(tok2->tokAt(-2), ") const {")) {
7451+
// Determine if this is a executable scope..
7452+
bool executableScope = false;
7453+
{
7454+
const Token *prev = tok2->previous()->link();
7455+
while (prev) {
7456+
if (prev->str() == "}")
7457+
prev = prev->link();
7458+
else if (prev->str() == "{") {
7459+
while ((prev = prev->previous()) && (prev->isName()));
7460+
if (!prev || prev->str() == ")")
7461+
break;
7462+
}
7463+
prev = prev->previous();
7464+
}
7465+
7466+
if (prev)
7467+
executableScope = true;
7468+
}
7469+
74517470
std::set<std::string> shadowArg;
74527471
for (const Token* arg = tok2; arg && arg->str() != "("; arg = arg->previous()) {
74537472
if (Token::Match(arg->previous(), "%type%|*|& %type% [,)]") &&
74547473
enumValues.find(arg->str()) != enumValues.end()) {
7474+
if (executableScope && Token::Match(arg->previous(), "[*&]"))
7475+
continue;
74557476
shadowArg.insert(arg->str());
74567477
if (inScope && _settings->isEnabled("style")) {
74577478
const EnumValue enumValue = enumValues.find(arg->str())->second;

test/testsimplifytokens.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7383,6 +7383,10 @@ class TestSimplifyTokens : public TestFixture {
73837383

73847384
const char code4[] = "enum { a, b }; void f() { int &a=x; }";
73857385
ASSERT_EQUALS("void f ( ) { int & a = x ; }", checkSimplifyEnum(code4));
7386+
7387+
// #4857 - not shadow variable
7388+
checkSimplifyEnum("enum { a,b }; void f() { if (x) { } else if ( x & a ) {} }");
7389+
ASSERT_EQUALS("", errout.str());
73867390
}
73877391

73887392
void enum38() { // #4463

0 commit comments

Comments
 (0)