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() void Tokenizer::simplifyFunctionParameters()
{ {
int indentlevel = 0;
for (Token *tok = _tokens; tok; tok = tok->next()) for (Token *tok = _tokens; tok; tok = tok->next())
{ {
if (tok->str() == "{") if (tok->str() == "{")
++indentlevel; {
tok = tok->link();
if (!tok)
break;
continue;
}
else if (tok->str() == "}") if (tok->str() == "(")
--indentlevel; {
tok = tok->link();
if (!tok)
break;
continue;
}
// Find the function e.g. foo( x ) or foo( x, y ) // 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 // 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) if (tok == NULL)
{ {
break; break;
@ -3112,8 +3123,6 @@ void Tokenizer::simplifyFunctionParameters()
{ {
continue; continue;
} }
++indentlevel;
} }
} }
} }

View File

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