Fixed #4476 (Tokenizer: wrong simplification of static anonymous-type struct array declaration)

This commit is contained in:
Daniel Marjamäki 2013-01-10 18:24:01 +01:00
parent fdcfbd5ff9
commit 97b928b2bc
2 changed files with 33 additions and 28 deletions

View File

@ -8320,6 +8320,16 @@ void Tokenizer::simplifyStructDecl()
std::stack<bool> skip; // true = in function, false = not in function
skip.push(false);
// Add names for anonymous structs
for (Token *tok = list.front(); tok; tok = tok->next()) {
// check for anonymous struct/union
if (Token::Match(tok, "struct|union {")) {
if (Token::Match(tok->next()->link(), "} *|&| %type% ,|;|[")) {
tok->insertToken("Anonymous" + MathLib::longToString(count++));
}
}
}
for (Token *tok = list.front(); tok; tok = tok->next()) {
Token *restart;
@ -8373,21 +8383,8 @@ void Tokenizer::simplifyStructDecl()
restart = tok->next();
tok = tok->next()->link();
// check for named type
if (Token::Match(tok->next(), "*|&| %type% ,|;|[")) {
std::string name;
name = "Anonymous" + MathLib::longToString(count++);
tok1->insertToken(name);
tok->insertToken(";");
tok = tok->next();
tok->insertToken(name);
}
// unnamed anonymous struct/union so possibly remove it
else if (tok->next() && tok->next()->str() == ";") {
if (tok->next() && tok->next()->str() == ";") {
if (tok1->str() == "union" && inFunction) {
// Try to create references in the union..
Token *tok2 = tok1->tokAt(2);

View File

@ -389,6 +389,7 @@ private:
TEST_CASE(simplifyStructDecl4);
TEST_CASE(simplifyStructDecl5); // ticket #3533 (segmentation fault)
TEST_CASE(simplifyStructDecl6); // ticket #3732
TEST_CASE(simplifyStructDecl7); // ticket #476 (static anonymous struct array)
// register int var; => int var;
// inline int foo() {} => int foo() {}
@ -7427,49 +7428,49 @@ private:
{
const char code[] = "struct { } abc;";
const char expected[] = "struct Anonymous0 { } ; Anonymous0 abc ;";
const char expected[] = "struct Anonymous0 { } ; struct Anonymous0 abc ;";
ASSERT_EQUALS(expected, tok(code, false));
}
{
const char code[] = "struct { } * pabc;";
const char expected[] = "struct Anonymous0 { } ; Anonymous0 * pabc ;";
const char expected[] = "struct Anonymous0 { } ; struct Anonymous0 * pabc ;";
ASSERT_EQUALS(expected, tok(code, false));
}
{
const char code[] = "struct { } abc[4];";
const char expected[] = "struct Anonymous0 { } ; Anonymous0 abc [ 4 ] ;";
const char expected[] = "struct Anonymous0 { } ; struct Anonymous0 abc [ 4 ] ;";
ASSERT_EQUALS(expected, tok(code, false));
}
{
const char code[] = "struct { } abc, def;";
const char expected[] = "struct Anonymous0 { } ; Anonymous0 abc ; Anonymous0 def ;";
const char expected[] = "struct Anonymous0 { } ; struct Anonymous0 abc ; struct Anonymous0 def ;";
ASSERT_EQUALS(expected, tok(code, false));
}
{
const char code[] = "struct { } abc, * pabc;";
const char expected[] = "struct Anonymous0 { } ; Anonymous0 abc ; Anonymous0 * pabc ;";
const char expected[] = "struct Anonymous0 { } ; struct Anonymous0 abc ; struct Anonymous0 * pabc ;";
ASSERT_EQUALS(expected, tok(code, false));
}
{
const char code[] = "struct { struct DEF {} def; } abc;";
const char expected[] = "struct Anonymous0 { struct DEF { } ; struct DEF def ; } ; Anonymous0 abc ;";
const char expected[] = "struct Anonymous0 { struct DEF { } ; struct DEF def ; } ; struct 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 ; } ; struct ABC abc ;";
const char expected[] = "struct ABC { struct Anonymous0 { } ; struct Anonymous0 def ; } ; struct ABC abc ;";
ASSERT_EQUALS(expected, tok(code, false));
}
{
const char code[] = "struct { struct {} def; } abc;";
const char expected[] = "struct Anonymous0 { struct Anonymous1 { } ; Anonymous1 def ; } ; Anonymous0 abc ;";
const char expected[] = "struct Anonymous0 { struct Anonymous1 { } ; struct Anonymous1 def ; } ; struct Anonymous0 abc ;";
ASSERT_EQUALS(expected, tok(code, false));
}
@ -7481,13 +7482,13 @@ private:
{
const char code[] = "struct ABC { struct {} def; };";
const char expected[] = "struct ABC { struct Anonymous0 { } ; Anonymous0 def ; } ;";
const char expected[] = "struct ABC { struct Anonymous0 { } ; struct Anonymous0 def ; } ;";
ASSERT_EQUALS(expected, tok(code, false));
}
{
const char code[] = "struct ABC : public XYZ { struct {} def; };";
const char expected[] = "struct ABC : public XYZ { struct Anonymous0 { } ; Anonymous0 def ; } ;";
const char expected[] = "struct ABC : public XYZ { struct Anonymous0 { } ; struct Anonymous0 def ; } ;";
ASSERT_EQUALS(expected, tok(code, false));
}
@ -7605,13 +7606,13 @@ private:
{
const char code[] = "struct { class DEF {} def; } abc;";
const char expected[] = "struct Anonymous0 { class DEF { } ; DEF def ; } ; Anonymous0 abc ;";
const char expected[] = "struct Anonymous0 { class DEF { } ; DEF def ; } ; struct Anonymous0 abc ;";
ASSERT_EQUALS(expected, tok(code, false));
}
{
const char code[] = "class ABC { struct {} def; } abc;";
const char expected[] = "class ABC { struct Anonymous0 { } ; Anonymous0 def ; } ; ABC abc ;";
const char expected[] = "class ABC { struct Anonymous0 { } ; struct Anonymous0 def ; } ; ABC abc ;";
ASSERT_EQUALS(expected, tok(code, false));
}
@ -7623,13 +7624,13 @@ private:
{
const char code[] = "class ABC { struct {} def; };";
const char expected[] = "class ABC { struct Anonymous0 { } ; Anonymous0 def ; } ;";
const char expected[] = "class ABC { struct Anonymous0 { } ; struct Anonymous0 def ; } ;";
ASSERT_EQUALS(expected, tok(code, false));
}
{
const char code[] = "class ABC : public XYZ { struct {} def; };";
const char expected[] = "class ABC : public XYZ { struct Anonymous0 { } ; Anonymous0 def ; } ;";
const char expected[] = "class ABC : public XYZ { struct Anonymous0 { } ; struct Anonymous0 def ; } ;";
ASSERT_EQUALS(expected, tok(code, false));
}
@ -7706,6 +7707,13 @@ private:
"} arrays = {{0}};", false));
}
void simplifyStructDecl7() {
ASSERT_EQUALS("struct Anonymous0 { char x ; } ; struct Anonymous0 a [ 2 ] ;",
tok("struct { char x; } a[2];", false));
ASSERT_EQUALS("struct Anonymous0 { char x ; } ; static struct Anonymous0 a [ 2 ] ;",
tok("static struct { char x; } a[2];", false));
}
void removeUnwantedKeywords() {
ASSERT_EQUALS("int var ;", tok("register int var ;", true));
ASSERT_EQUALS("short var ;", tok("register short int var ;", true));