Fixed #1067 (Tokenizer: K&R style function that returns function pointer)

This commit is contained in:
Daniel Marjamäki 2009-12-30 15:12:38 +01:00
parent 06ee643c6a
commit a7ab47bb98
2 changed files with 27 additions and 59 deletions

View File

@ -3026,17 +3026,26 @@ void Tokenizer::simplifyCasts()
void Tokenizer::simplifyFunctionParameters()
{
int indentlevel = 0;
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (tok->str() == "{")
++indentlevel;
{
tok = tok->link();
if (!tok)
break;
continue;
}
else if (tok->str() == "}")
--indentlevel;
if (tok->str() == "(")
{
tok = tok->link();
if (!tok)
break;
continue;
}
// Find the function e.g. foo( x ) or foo( x, y )
else if (indentlevel == 0 && Token::Match(tok, "%var% ( %var% [,)]"))
if (Token::Match(tok, "%var% ( %var% [,)]"))
{
// We have found old style function, now we need to change it
@ -3103,6 +3112,8 @@ void Tokenizer::simplifyFunctionParameters()
}
}
tok = tok ? tok->link() : 0;
if (tok == NULL)
{
break;
@ -3112,8 +3123,6 @@ void Tokenizer::simplifyFunctionParameters()
{
continue;
}
++indentlevel;
}
}
}

View File

@ -1918,62 +1918,21 @@ private:
void simplify_function_parameters()
{
{
const char code[] = "void f(x) int x;\n"
"{\n"
"}\n";
ASSERT_EQUALS("void f ( int x ) { }", tokenizeAndStringify("void f(x) int x; { }", true));
ASSERT_EQUALS("void f ( int x , char y ) { }", tokenizeAndStringify("void f(x,y) int x; char y; { }", true));
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(" void f ( int x ) { }", ostr.str());
}
{
const char code[] = "void f(x,y) int x; char y;\n"
"{\n"
"}\n";
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(" void f ( int x , char y ) { }", ostr.str());
}
// #1067 - Not simplified. Feel free to fix so it is simplified correctly but this syntax is obsolete.
ASSERT_EQUALS("int ( * d ( a , b , c ) ) ( ) int a ; int b ; int c ; { }", tokenizeAndStringify("int (*d(a,b,c))()int a,b,c; { }", true));
{
// This is not a function but the pattern is similar..
const char code[] = "void foo()\n"
"{\n"
" if (x)\n"
" int x;\n"
" { }\n"
"}\n";
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(" void foo ( ) { if ( x ) { int x ; } { } }", ostr.str());
const char code[] = "void foo()"
"{"
" if (x)"
" int x;"
" { }"
"}";
ASSERT_EQUALS("void foo ( ) { if ( x ) { int x ; } { } }", tokenizeAndStringify(code, true));
}
}