Skip to content

Commit 297f2c7

Browse files
committed
Fixed false positives variableHidingTypedef (cppcheck-opensource#5624, cppcheck-opensource#6507)
1 parent ee58587 commit 297f2c7

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

lib/tokenize.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token
269269
if (Token::simpleMatch(end, ") {")) { // function parameter ?
270270
// look backwards
271271
if (Token::Match(tok->previous(), "%type%") &&
272-
!Token::Match(tok->previous(), "return|new|const")) {
272+
!Token::Match(tok->previous(), "return|new|const|struct")) {
273273
duplicateTypedefError(*tokPtr, name, "function parameter");
274274
// duplicate definition so skip entire function
275275
*tokPtr = end->next()->link();
@@ -351,8 +351,10 @@ bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token
351351
tok = tok->previous();
352352
}
353353

354-
duplicateTypedefError(*tokPtr, name, "variable");
355-
return true;
354+
if ((*tokPtr)->strAt(1) != "(" || !Token::Match((*tokPtr)->linkAt(1), ") .|(|[")) {
355+
duplicateTypedefError(*tokPtr, name, "variable");
356+
return true;
357+
}
356358
}
357359
}
358360
}

test/testsimplifytypedef.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ class TestSimplifyTypedef : public TestFixture {
152152
TEST_CASE(simplifyTypedef113); // ticket #7030
153153
TEST_CASE(simplifyTypedef114); // ticket #7058 - skip "struct", AB::..
154154
TEST_CASE(simplifyTypedef115); // ticket #6998
155+
TEST_CASE(simplifyTypedef116); // ticket #5624
156+
TEST_CASE(simplifyTypedef117); // ticket #6507
155157

156158
TEST_CASE(simplifyTypedefFunction1);
157159
TEST_CASE(simplifyTypedefFunction2); // ticket #1685
@@ -2433,6 +2435,35 @@ class TestSimplifyTypedef : public TestFixture {
24332435
ASSERT_EQUALS("", errout.str());
24342436
}
24352437

2438+
void simplifyTypedef116() { // #5624
2439+
const char code[] = "void fn() {\n"
2440+
" typedef std::vector<CharacterConversion> CharacterToConversion;\n"
2441+
" CharacterToConversion c2c;\n"
2442+
" for (CharacterToConversion::iterator it = c2c.begin(); it != c2c.end(); ++it) {}\n"
2443+
" CharacterToConversion().swap(c2c);\n"
2444+
"}";
2445+
const char expected[] = "void fn ( ) { "
2446+
"std :: vector < CharacterConversion > c2c ; "
2447+
"for ( std :: vector < CharacterConversion > :: iterator it = c2c . begin ( ) ; it != c2c . end ( ) ; ++ it ) { } "
2448+
"std :: vector < CharacterConversion > ( ) . swap ( c2c ) ; "
2449+
"}";
2450+
ASSERT_EQUALS(expected, tok(code, false));
2451+
ASSERT_EQUALS("", errout.str());
2452+
}
2453+
2454+
void simplifyTypedef117() { // #6507
2455+
const char code[] = "typedef struct bstr {} bstr;\n"
2456+
"struct bstr bstr0(const char *s) {\n"
2457+
" return (struct bstr) { (unsigned char *)s, s ? strlen(s) : 0 };\n"
2458+
"}";
2459+
const char expected[] = "struct bstr { } ; "
2460+
"struct bstr bstr0 ( const char * s ) { "
2461+
"return ( struct bstr ) { ( unsigned char * ) s , s ? strlen ( s ) : 0 } ; "
2462+
"}";
2463+
ASSERT_EQUALS(expected, tok(code, false));
2464+
ASSERT_EQUALS("", errout.str());
2465+
}
2466+
24362467
void simplifyTypedefFunction1() {
24372468
{
24382469
const char code[] = "typedef void (*my_func)();\n"

0 commit comments

Comments
 (0)