Fixed #780 (Tokenizer: replace typedef for pointers to struct)

http://sourceforge.net/apps/trac/cppcheck/ticket/780
This commit is contained in:
Slava Semushin 2009-10-03 22:02:23 +07:00
parent 803748a654
commit b0d8b6ca8b
2 changed files with 42 additions and 3 deletions

View File

@ -372,9 +372,10 @@ void Tokenizer::simplifyTypedef()
const char *type1 = 0;
const char *type2 = 0;
const char *typeName = 0;
bool pointer = false;
if (Token::Match(tok->next(), "%type% %type% ;") ||
Token::Match(tok->next(), "%type% %type% %type% ;"))
Token::Match(tok->next(), "%type% %type% *| %type% ;"))
{
if (tok->tokAt(3)->str() == ";")
{
@ -385,10 +386,22 @@ void Tokenizer::simplifyTypedef()
}
else
{
pointer = (tok->tokAt(3)->str() == "*");
type1 = tok->strAt(1);
type2 = tok->strAt(2);
typeName = tok->strAt(3);
tok = tok->tokAt(4);
if (pointer)
{
typeName = tok->strAt(4);
tok = tok->tokAt(5);
}
else
{
typeName = tok->strAt(3);
tok = tok->tokAt(4);
}
}
const std::string pattern = className + " :: " + typeName;
@ -443,6 +456,11 @@ void Tokenizer::simplifyTypedef()
tok2->insertToken(type2);
tok2 = tok2->next();
}
if (pointer)
{
tok2->insertToken("*");
tok2 = tok2->next();
}
simplifyType = false;
}

View File

@ -123,6 +123,7 @@ private:
TEST_CASE(simplifyTypedef2)
TEST_CASE(simplifyTypedef3)
TEST_CASE(simplifyTypedef4)
TEST_CASE(simplifyTypedef5)
}
std::string tok(const char code[], bool simplify = true)
@ -1754,6 +1755,26 @@ private:
ASSERT_EQUALS(expected, tok(code, false));
}
void simplifyTypedef5()
{
// ticket #780
const char code[] =
"typedef struct yy_buffer_state *YY_BUFFER_STATE;\n"
"void f()\n"
"{\n"
" YY_BUFFER_STATE state;\n"
"}\n";
const char expected[] =
"typedef struct yy_buffer_state * YY_BUFFER_STATE ; "
"void f ( ) "
"{ "
"struct yy_buffer_state * state ; "
"}";
ASSERT_EQUALS(expected, tok(code, false));
}
};
REGISTER_TEST(TestSimplifyTokens)