From 297f2c78bdafdd6433cfa4c5ebf4690c99857851 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Mon, 26 Oct 2015 10:06:52 +0100 Subject: [PATCH] Fixed false positives variableHidingTypedef (#5624, #6507) --- lib/tokenize.cpp | 8 +++++--- test/testsimplifytypedef.cpp | 31 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index c76ffd6f6..70e63a924 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -269,7 +269,7 @@ bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token if (Token::simpleMatch(end, ") {")) { // function parameter ? // look backwards if (Token::Match(tok->previous(), "%type%") && - !Token::Match(tok->previous(), "return|new|const")) { + !Token::Match(tok->previous(), "return|new|const|struct")) { duplicateTypedefError(*tokPtr, name, "function parameter"); // duplicate definition so skip entire function *tokPtr = end->next()->link(); @@ -351,8 +351,10 @@ bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token tok = tok->previous(); } - duplicateTypedefError(*tokPtr, name, "variable"); - return true; + if ((*tokPtr)->strAt(1) != "(" || !Token::Match((*tokPtr)->linkAt(1), ") .|(|[")) { + duplicateTypedefError(*tokPtr, name, "variable"); + return true; + } } } } diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index c0445a1a7..a7f6b7ba6 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -152,6 +152,8 @@ private: TEST_CASE(simplifyTypedef113); // ticket #7030 TEST_CASE(simplifyTypedef114); // ticket #7058 - skip "struct", AB::.. TEST_CASE(simplifyTypedef115); // ticket #6998 + TEST_CASE(simplifyTypedef116); // ticket #5624 + TEST_CASE(simplifyTypedef117); // ticket #6507 TEST_CASE(simplifyTypedefFunction1); TEST_CASE(simplifyTypedefFunction2); // ticket #1685 @@ -2433,6 +2435,35 @@ private: ASSERT_EQUALS("", errout.str()); } + void simplifyTypedef116() { // #5624 + const char code[] = "void fn() {\n" + " typedef std::vector CharacterToConversion;\n" + " CharacterToConversion c2c;\n" + " for (CharacterToConversion::iterator it = c2c.begin(); it != c2c.end(); ++it) {}\n" + " CharacterToConversion().swap(c2c);\n" + "}"; + const char expected[] = "void fn ( ) { " + "std :: vector < CharacterConversion > c2c ; " + "for ( std :: vector < CharacterConversion > :: iterator it = c2c . begin ( ) ; it != c2c . end ( ) ; ++ it ) { } " + "std :: vector < CharacterConversion > ( ) . swap ( c2c ) ; " + "}"; + ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef117() { // #6507 + const char code[] = "typedef struct bstr {} bstr;\n" + "struct bstr bstr0(const char *s) {\n" + " return (struct bstr) { (unsigned char *)s, s ? strlen(s) : 0 };\n" + "}"; + const char expected[] = "struct bstr { } ; " + "struct bstr bstr0 ( const char * s ) { " + "return ( struct bstr ) { ( unsigned char * ) s , s ? strlen ( s ) : 0 } ; " + "}"; + ASSERT_EQUALS(expected, tok(code, false)); + ASSERT_EQUALS("", errout.str()); + } + void simplifyTypedefFunction1() { { const char code[] = "typedef void (*my_func)();\n"