diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 825c7a73f..6374b67d9 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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; } } } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index de3c4e1e3..5b25de9e9 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -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)); } }