From eebfea2b23d6ffa4f730271e29dce9354a73cd54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 7 Jan 2013 19:20:15 +0100 Subject: [PATCH] Fixed #4381 (Inline type declaration on statics causes warning) --- lib/checkunusedvar.cpp | 20 ++++++++++++++------ lib/tokenize.cpp | 14 +++++++++----- test/testsimplifytokens.cpp | 24 ++++++++++++------------ test/testtokenize.cpp | 13 ++++++++++--- 4 files changed, 45 insertions(+), 26 deletions(-) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 0c5bfdc7e..51affdde8 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -1133,12 +1133,6 @@ void CheckUnusedVar::checkStructMemberUsage() continue; if (Token::Match(tok, "struct|union %type% {")) { - structname.clear(); - if (tok->strAt(-1) == "extern") - continue; - if ((!tok->previous() || tok->previous()->str() == ";") && Token::Match(tok->linkAt(2), ("} ; " + tok->strAt(1) + " %var% ;").c_str())) - continue; - structname = tok->strAt(1); // Bail out if struct/union contain any functions @@ -1173,6 +1167,20 @@ void CheckUnusedVar::checkStructMemberUsage() } } + // bail out for extern/global struct + for (const Token *tok2 = Token::findmatch(tok, (structname + " %var%").c_str()); + tok2; + tok2 = Token::findmatch(tok2->next(), (structname + " %var%").c_str())) { + + const Variable *var = _tokenizer->getSymbolDatabase()->getVariableFromVarId(tok2->next()->varId()); + if (var && (var->isExtern() || (var->isGlobal() && !var->isStatic()))) { + structname.clear(); + break; + } + } + if (structname.empty()) + continue; + // Try to prevent false positives when struct members are not used directly. if (Token::findmatch(tok, (structname + " *").c_str())) structname.clear(); diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index ede9698fd..287275b5b 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -8333,7 +8333,9 @@ void Tokenizer::simplifyStructDecl() // check for named struct/union else if (Token::Match(tok, "class|struct|union %type% :|{")) { - Token *isStatic = tok->previous() && tok->previous()->str() == "static" ? tok->previous() : NULL; + Token *start = tok; + while (Token::Match(start->previous(), "%type%")) + start = start->previous(); Token *type = tok->next(); Token *next = tok->tokAt(2); @@ -8349,12 +8351,14 @@ void Tokenizer::simplifyStructDecl() if (Token::Match(tok->next(), "*|&| %type% ,|;|[|=")) { tok->insertToken(";"); tok = tok->next(); - if (isStatic) { - isStatic->deleteThis(); - tok->insertToken("static"); + while (!Token::Match(start, "struct|class|union")) { + tok->insertToken(start->str()); tok = tok->next(); + start->deleteThis(); } tok->insertToken(type->str()); + if (start->str() != "class") + tok->insertToken(start->str()); } tok = restart; @@ -9366,7 +9370,7 @@ void Tokenizer::simplifyMathExpressions() } } else if (Token::simpleMatch(tok->tokAt(2), "sinh (")) { Token *tok2 = tok->linkAt(3); - if (!Token::simpleMatch(tok2, ") , 2 ) - pow ( cosh (") ) + if (!Token::simpleMatch(tok2, ") , 2 ) - pow ( cosh (")) continue; Token *tok3 = tok2->tokAt(8); if (!Token::simpleMatch(tok3->link(), ") , 2 )")) diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 0ab88478e..3c7eabe6e 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -7391,37 +7391,37 @@ private: void simplifyStructDecl1() { { const char code[] = "struct ABC { } abc;"; - const char expected[] = "struct ABC { } ; ABC abc ;"; + const char expected[] = "struct ABC { } ; struct ABC abc ;"; ASSERT_EQUALS(expected, tok(code, false)); } { const char code[] = "struct ABC { } * pabc;"; - const char expected[] = "struct ABC { } ; ABC * pabc ;"; + const char expected[] = "struct ABC { } ; struct ABC * pabc ;"; ASSERT_EQUALS(expected, tok(code, false)); } { const char code[] = "struct ABC { } abc[4];"; - const char expected[] = "struct ABC { } ; ABC abc [ 4 ] ;"; + const char expected[] = "struct ABC { } ; struct ABC abc [ 4 ] ;"; ASSERT_EQUALS(expected, tok(code, false)); } { const char code[] = "struct ABC { } abc, def;"; - const char expected[] = "struct ABC { } ; ABC abc ; ABC def ;"; + const char expected[] = "struct ABC { } ; struct ABC abc ; struct ABC def ;"; ASSERT_EQUALS(expected, tok(code, false)); } { const char code[] = "struct ABC { } abc, * pabc;"; - const char expected[] = "struct ABC { } ; ABC abc ; ABC * pabc ;"; + const char expected[] = "struct ABC { } ; struct ABC abc ; struct ABC * pabc ;"; ASSERT_EQUALS(expected, tok(code, false)); } { const char code[] = "struct ABC { struct DEF {} def; } abc;"; - const char expected[] = "struct ABC { struct DEF { } ; DEF def ; } ; ABC abc ;"; + const char expected[] = "struct ABC { struct DEF { } ; struct DEF def ; } ; struct ABC abc ;"; ASSERT_EQUALS(expected, tok(code, false)); } @@ -7457,13 +7457,13 @@ private: { const char code[] = "struct { struct DEF {} def; } abc;"; - const char expected[] = "struct Anonymous0 { struct DEF { } ; DEF def ; } ; Anonymous0 abc ;"; + const char expected[] = "struct Anonymous0 { struct DEF { } ; struct DEF def ; } ; Anonymous0 abc ;"; ASSERT_EQUALS(expected, tok(code, false)); } { const char code[] = "struct ABC { struct {} def; } abc;"; - const char expected[] = "struct ABC { struct Anonymous0 { } ; Anonymous0 def ; } ; ABC abc ;"; + const char expected[] = "struct ABC { struct Anonymous0 { } ; Anonymous0 def ; } ; struct ABC abc ;"; ASSERT_EQUALS(expected, tok(code, false)); } @@ -7475,7 +7475,7 @@ private: { const char code[] = "union ABC { int i; float f; } abc;"; - const char expected[] = "union ABC { int i ; float f ; } ; ABC abc ;"; + const char expected[] = "union ABC { int i ; float f ; } ; union ABC abc ;"; ASSERT_EQUALS(expected, tok(code, false)); } @@ -7518,7 +7518,7 @@ private: // ticket 2464 { const char code[] = "static struct ABC { } abc ;"; - const char expected[] = "struct ABC { } ; static ABC abc ;"; + const char expected[] = "struct ABC { } ; static struct ABC abc ;"; ASSERT_EQUALS(expected, tok(code, false)); } @@ -7676,7 +7676,7 @@ private: "void foo ( ) { " "int i ; " "float & f = i ; " - "struct Fee { } ; Fee fee ; " + "struct Fee { } ; struct Fee fee ; " "} " "union { " "long long ll ; " @@ -7700,7 +7700,7 @@ private: void simplifyStructDecl6() { ASSERT_EQUALS("struct A { " "char integers [ X ] ; " - "} ; A arrays ; arrays = { { 0 } } ;", + "} ; struct A arrays ; arrays = { { 0 } } ;", tok("struct A {\n" " char integers[X];\n" "} arrays = {{0}};", false)); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 9fd3a67af..7d2bfe48c 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -322,6 +322,8 @@ private: TEST_CASE(simplify_null); TEST_CASE(simplifyMulAndParens); // Ticket #2784 + #3184 + TEST_CASE(simplifyStructDecl); + TEST_CASE(vardecl1); TEST_CASE(vardecl2); TEST_CASE(vardecl3); @@ -3151,8 +3153,8 @@ private: const std::string expected("\n\n##file 0\n" "1: struct S {\n" "2: struct T {\n" - "3: } ; T t@1 ;\n" - "4: } ; S s@2 ;\n"); + "3: } ; struct T t@1 ;\n" + "4: } ; struct S s@2 ;\n"); ASSERT_EQUALS(expected, actual); } @@ -3167,7 +3169,7 @@ private: const std::string expected("\n\n##file 0\n" "1: struct S {\n" "2: struct T {\n" - "3: } ; T t@1 ;\n" + "3: } ; struct T t@1 ;\n" "4: } ;\n"); ASSERT_EQUALS(expected, actual); @@ -5137,6 +5139,11 @@ private: ASSERT_EQUALS(expected, tokenizeAndStringify(code)); } + void simplifyStructDecl() { + const char code[] = "const struct A { int a; int b; } a;"; + ASSERT_EQUALS("struct A { int a ; int b ; } ; const struct A a ;", tokenizeAndStringify(code)); + } + void vardecl1() { const char code[] = "unsigned int a, b;";