Fixed #1248 (tokenize typedef of array)

This commit is contained in:
Robert Reif 2010-01-11 17:09:04 +01:00 committed by Daniel Marjamäki
parent 8be54b592d
commit 94d5e32aa1
2 changed files with 83 additions and 1 deletions

View File

@ -449,11 +449,14 @@ void Tokenizer::simplifyTypedef()
bool pointer = false; bool pointer = false;
Token *start = 0; Token *start = 0;
Token *end = 0; Token *end = 0;
Token *num = 0;
if (Token::Match(tok->next(), "%type% <") || if (Token::Match(tok->next(), "%type% <") ||
Token::Match(tok->next(), "%type% :: %type% <") || Token::Match(tok->next(), "%type% :: %type% <") ||
Token::Match(tok->next(), "%type% *| %type% ;") || Token::Match(tok->next(), "%type% *| %type% ;") ||
Token::Match(tok->next(), "%type% %type% *| %type% ;")) Token::Match(tok->next(), "%type% %type% *| %type% ;") ||
Token::Match(tok->next(), "%type% *| %type% [ %num% ]") ||
Token::Match(tok->next(), "%type% %type% *| %type% [ %num% ]"))
{ {
if ((tok->tokAt(2)->str() == "<") || if ((tok->tokAt(2)->str() == "<") ||
(tok->tokAt(4) && (tok->tokAt(4)->str() == "<"))) (tok->tokAt(4) && (tok->tokAt(4)->str() == "<")))
@ -487,6 +490,48 @@ void Tokenizer::simplifyTypedef()
else else
continue; continue;
} }
else if ((tok->tokAt(3) && tok->tokAt(3)->str() == "[") ||
(tok->tokAt(4) && tok->tokAt(4)->str() == "[") ||
(tok->tokAt(5) && tok->tokAt(5)->str() == "["))
{
type1 = tok->strAt(1);
if ((tok->tokAt(4) && tok->tokAt(4)->str() == "[" && tok->tokAt(2)->str() != "*") ||
(tok->tokAt(5) && tok->tokAt(5)->str() == "["))
{
type2 = tok->strAt(2);
pointer = (tok->tokAt(3)->str() == "*");
if (pointer)
{
typeName = tok->strAt(4);
num = tok->tokAt(6);
tok = tok->tokAt(8);
}
else
{
typeName = tok->strAt(3);
num = tok->tokAt(5);
tok = tok->tokAt(7);
}
}
else
{
pointer = (tok->tokAt(2)->str() == "*");
if (pointer)
{
typeName = tok->strAt(3);
num = tok->tokAt(5);
tok = tok->tokAt(7);
}
else
{
typeName = tok->strAt(2);
num = tok->tokAt(4);
tok = tok->tokAt(6);
}
}
}
else if (tok->tokAt(3)->str() == ";") else if (tok->tokAt(3)->str() == ";")
{ {
type1 = tok->strAt(1); type1 = tok->strAt(1);
@ -612,6 +657,17 @@ void Tokenizer::simplifyTypedef()
tok2->insertToken("*"); tok2->insertToken("*");
tok2 = tok2->next(); tok2 = tok2->next();
} }
if (num)
{
tok2 = tok2->next();
tok2->insertToken("[");
tok2 = tok2->next();
tok2->insertToken(num->strAt(0));
tok2 = tok2->next();
tok2->insertToken("]");
tok2 = tok2->next();
}
} }
simplifyType = false; simplifyType = false;

View File

@ -148,6 +148,7 @@ private:
TEST_CASE(simplifyTypedef11); TEST_CASE(simplifyTypedef11);
TEST_CASE(simplifyTypedef12); TEST_CASE(simplifyTypedef12);
TEST_CASE(simplifyTypedef13); TEST_CASE(simplifyTypedef13);
TEST_CASE(simplifyTypedef14);
TEST_CASE(reverseArraySyntax) TEST_CASE(reverseArraySyntax)
TEST_CASE(simplify_numeric_condition) TEST_CASE(simplify_numeric_condition)
@ -2435,6 +2436,31 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void simplifyTypedef14()
{
{
const char code[] = "typedef char frame[10];\n"
"frame f;";
const char expected[] =
"typedef char frame [ 10 ] ; "
"char f [ 10 ] ;";
ASSERT_EQUALS(expected, tok(code, false));
}
{
const char code[] = "typedef unsigned char frame[10];\n"
"frame f;";
const char expected[] =
"typedef unsigned char frame [ 10 ] ; "
"unsigned char f [ 10 ] ;";
ASSERT_EQUALS(expected, tok(code, false));
}
}
void reverseArraySyntax() void reverseArraySyntax()
{ {
ASSERT_EQUALS("a [ 13 ]", tok("13[a]")); ASSERT_EQUALS("a [ 13 ]", tok("13[a]"));