diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 899e92ab1..f3c36234c 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7018,12 +7018,18 @@ bool Tokenizer::duplicateDefinition(Token ** tokPtr, const Token * name) const } } } else { - // look backwards - if (Token::Match(tok->previous(), "enum|,") || - (Token::Match(tok->previous(), "%type%") && - tok->previous()->str() != "return")) { + if (Token::Match(tok->previous(), "enum|,")) { duplicateEnumError(*tokPtr, name, "Variable"); return true; + } else if (Token::Match(tok->previous(), "%type%")) { + // look backwards + const Token *back = tok; + while (back && back->isName()) + back = back->previous(); + if (!back || Token::Match(back, "[(,;{}] !!return")) { + duplicateEnumError(*tokPtr, name, "Variable"); + return true; + } } } } diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index e18629841..1b88e2fab 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -354,6 +354,7 @@ private: TEST_CASE(enum33); // ticket #4015 (segmentation fault) TEST_CASE(enum34); // ticket #4141 (division by zero) TEST_CASE(enumscope1); // ticket #3949 + TEST_CASE(duplicateDefinition); // ticket #3565 // remove "std::" on some standard functions TEST_CASE(removestd); @@ -7176,6 +7177,15 @@ private: ASSERT_EQUALS("void foo ( ) { } void bar ( ) { int a ; a = A ; }", checkSimplifyEnum(code)); } + void duplicateDefinition() { // #3565 - wrongly detects duplicate definition + const Settings settings; + Tokenizer tokenizer(&settings, NULL); + std::istringstream istr("x ; return a not_eq x;"); + tokenizer.tokenize(istr, "test.c"); + Token *tok = (Token *)(tokenizer.tokens()->tokAt(5)); + ASSERT_EQUALS(false, tokenizer.duplicateDefinition(&tok, tokenizer.tokens())); + } + void removestd() { ASSERT_EQUALS("; strcpy ( a , b ) ;", tok("; std::strcpy(a,b);")); ASSERT_EQUALS("; strcat ( a , b ) ;", tok("; std::strcat(a,b);"));