Fixed ticket #496 (Tokenizer: simplify statements with "," better)
https://sourceforge.net/apps/trac/cppcheck/ticket/496
This commit is contained in:
parent
ea49b433ec
commit
a73346e889
|
@ -3232,6 +3232,75 @@ bool Tokenizer::simplifyComma()
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool inReturn = false;
|
||||
Token *startFrom = NULL; // next tokean after "; return"
|
||||
Token *endAt = NULL; // first ";" token after "; return"
|
||||
|
||||
// find "; return" pattern before comma
|
||||
for (Token *tok2 = tok; tok2; tok2 = tok2->previous()) {
|
||||
if (Token::Match(tok2, "[;{}]")) {
|
||||
break;
|
||||
|
||||
} else if (tok2->str() == "return" && Token::Match(tok2->previous(), "[;{}]")) {
|
||||
inReturn = true;
|
||||
startFrom = tok2->next();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// find token where return ends and also count commas
|
||||
if (inReturn) {
|
||||
size_t commaCounter = 0;
|
||||
size_t indentlevel = 0;
|
||||
|
||||
for (Token *tok2 = startFrom; tok2; tok2 = tok2->next()) {
|
||||
if (tok2->str() == ";") {
|
||||
endAt = tok2;
|
||||
break;
|
||||
|
||||
} else if (tok2->str() == "(") {
|
||||
++indentlevel;
|
||||
|
||||
} else if (tok2->str() == ")") {
|
||||
--indentlevel;
|
||||
|
||||
} else if (tok2->str() == "," && indentlevel == 0) {
|
||||
++commaCounter;
|
||||
}
|
||||
}
|
||||
|
||||
if (commaCounter) {
|
||||
indentlevel = 0;
|
||||
|
||||
// change tokens:
|
||||
// "; return a ( ) , b ( ) , c ;"
|
||||
// to
|
||||
// "; return a ( ) ; b ( ) ; c ;"
|
||||
for (Token *tok2 = startFrom; tok2 != endAt; tok2 = tok2->next()) {
|
||||
if (tok2->str() == "(") {
|
||||
++indentlevel;
|
||||
|
||||
} else if (tok2->str() == ")") {
|
||||
--indentlevel;
|
||||
|
||||
} else if (tok2->str() == "," && indentlevel == 0) {
|
||||
tok2->str(";");
|
||||
--commaCounter;
|
||||
if (commaCounter == 0) {
|
||||
tok2->insertToken("return");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// delete old "return"
|
||||
startFrom->previous()->deleteThis();
|
||||
|
||||
tok = endAt;
|
||||
ret = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
|
|
@ -168,6 +168,7 @@ public:
|
|||
* Simplify comma into a semicolon when possible
|
||||
* Example: "delete a, delete b" => "delete a; delete b;"
|
||||
* Example: "a = 0, b = 0;" => "a = 0; b = 0;"
|
||||
* Example: "return a(), b;" => "a(); return b;"
|
||||
* @return true if something is modified
|
||||
* false if nothing is done.
|
||||
*/
|
||||
|
|
|
@ -1051,6 +1051,27 @@ private:
|
|||
"}\n";
|
||||
ASSERT_EQUALS(" void foo ( ) { char * a ; char * b ; delete a ; delete b ; }", sizeof_(code));
|
||||
}
|
||||
|
||||
{
|
||||
const char code[] = "int f()\n"
|
||||
"{\n"
|
||||
" if (something)\n"
|
||||
" return a(2, c(3, 4)), b(3), 10;\n"
|
||||
" return a(), b(0, 0, 0), 10;\n"
|
||||
"}\n";
|
||||
ASSERT_EQUALS(" int f ( )"
|
||||
" {"
|
||||
" if ( something )"
|
||||
" {"
|
||||
" a ( 2 , c ( 3 , 4 ) ) ;"
|
||||
" b ( 3 ) ;"
|
||||
" return 10 ;"
|
||||
" }"
|
||||
" a ( ) ;"
|
||||
" b ( 0 , 0 , 0 ) ;"
|
||||
" return 10 ; "
|
||||
"}", sizeof_(code));
|
||||
}
|
||||
}
|
||||
|
||||
void remove_comma()
|
||||
|
|
Loading…
Reference in New Issue