Fixed #4381 (Inline type declaration on statics causes warning)

This commit is contained in:
Daniel Marjamäki 2013-01-07 19:20:15 +01:00
parent f3c3b7c910
commit eebfea2b23
4 changed files with 45 additions and 26 deletions

View File

@ -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();

View File

@ -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;

View File

@ -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));

View File

@ -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;";