- Fixed mispelled function name;

changed variable name inside simplifyFlowControl for consistency;
improved simplifyFlowControl to handle better this kind of code:
"return; { { } { label : ; ok ( ) ; } }"->"return ; { { label: ok ( ) ; } }".
This commit is contained in:
Edoardo Prezioso 2011-10-17 02:16:49 +02:00
parent 6f80c5ff64
commit c3caade3ca
3 changed files with 40 additions and 23 deletions

View File

@ -4161,7 +4161,7 @@ bool Tokenizer::simplifyTokenList()
modified |= simplifyConditions(); modified |= simplifyConditions();
modified |= simplifyFunctionReturn(); modified |= simplifyFunctionReturn();
modified |= simplifyKnownVariables(); modified |= simplifyKnownVariables();
modified |= removeReduntantConditions(); modified |= removeRedundantConditions();
modified |= simplifyRedundantParenthesis(); modified |= simplifyRedundantParenthesis();
modified |= simplifyQuestionMark(); modified |= simplifyQuestionMark();
modified |= simplifyCalculations(); modified |= simplifyCalculations();
@ -4280,7 +4280,7 @@ void Tokenizer::simplifyFlowControl()
{ {
unsigned int indentlevel = 0; unsigned int indentlevel = 0;
unsigned int indentcase = 0; unsigned int indentcase = 0;
unsigned int indentret = 0; unsigned int indentflow = 0;
unsigned int indentswitch = 0; unsigned int indentswitch = 0;
unsigned int indentlabel = 0; unsigned int indentlabel = 0;
unsigned int roundbraces = 0; unsigned int roundbraces = 0;
@ -4295,7 +4295,8 @@ void Tokenizer::simplifyFlowControl()
if (tok->str() == "{") { if (tok->str() == "{") {
++indentlevel; ++indentlevel;
if (indentret) { if (indentflow) {
indentlabel = 0;
unsigned int indentlevel1 = indentlevel; unsigned int indentlevel1 = indentlevel;
for (Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) { for (Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) {
if (tok2->str() == "{") if (tok2->str() == "{")
@ -4317,21 +4318,21 @@ void Tokenizer::simplifyFlowControl()
} else if (tok->str() == "}") { } else if (tok->str() == "}") {
if (!indentlevel) if (!indentlevel)
break; //too many closing parenthesis break; //too many closing parenthesis
if (indentret) { if (indentflow) {
if (!indentswitch || indentlevel > indentcase) { if (!indentswitch || indentlevel > indentcase) {
if (indentlevel > indentret && indentlevel > indentlabel) { if (indentlevel > indentflow && indentlevel > indentlabel) {
tok = tok->previous(); tok = tok->previous();
tok->deleteNext(); tok->deleteNext();
} }
} else { } else {
if (indentcase > indentret && indentlevel > indentlabel) { if (indentcase > indentflow && indentlevel > indentlabel) {
tok = tok->previous(); tok = tok->previous();
tok->deleteNext(); tok->deleteNext();
} }
} }
} }
if (indentlevel == indentret) { if (indentlevel == indentflow) {
indentret = 0; indentflow = 0;
} }
--indentlevel; --indentlevel;
if (indentlevel <= indentcase) { if (indentlevel <= indentcase) {
@ -4342,7 +4343,7 @@ void Tokenizer::simplifyFlowControl()
indentcase = indentlevel-1; indentcase = indentlevel-1;
} }
} }
} else if (!indentret) { } else if (!indentflow) {
if (tok->str() == "switch") { if (tok->str() == "switch") {
if (!indentlevel) if (!indentlevel)
break; break;
@ -4398,7 +4399,7 @@ void Tokenizer::simplifyFlowControl()
continue; continue;
if (Token::Match(tok,"continue|break ;")) { if (Token::Match(tok,"continue|break ;")) {
indentret = indentlevel; indentflow = indentlevel;
if (Token::Match(tok->tokAt(2),"continue|break ;")) { if (Token::Match(tok->tokAt(2),"continue|break ;")) {
tok = tok->tokAt(3); tok = tok->tokAt(3);
continue; continue;
@ -4417,29 +4418,29 @@ void Tokenizer::simplifyFlowControl()
} else if (tok2->str() == ";") { } else if (tok2->str() == ";") {
if (returnroundbraces) if (returnroundbraces)
break; //excessive opening parenthesis break; //excessive opening parenthesis
indentret = indentlevel; indentflow = indentlevel;
tok = tok2; tok = tok2;
break; break;
} else if (Token::Match(tok2, "[{}]")) } else if (Token::Match(tok2, "[{}]"))
break; //I think this is an error code... break; //I think this is an error code...
} }
if (!indentret) if (!indentflow)
break; break;
} }
} else if (indentret) { //there's already a "return;" declaration } else if (indentflow) { //there's already a "return;" declaration
if (!indentswitch || indentlevel > indentcase+1) { if (!indentswitch || indentlevel > indentcase+1) {
if (indentlevel >= indentret && (!Token::Match(tok, "%var% : ;") || Token::Match(tok, "case|default"))) { if (indentlevel >= indentflow && (!Token::Match(tok, "%var% : ;") || Token::Match(tok, "case|default"))) {
tok = tok->previous(); tok = tok->previous();
tok->deleteNext(); tok->deleteNext();
} else { } else {
indentret = 0; indentflow = 0;
} }
} else { } else {
if (!Token::Match(tok, "%var% : ;") && !Token::Match(tok, "case|default")) { if (!Token::Match(tok, "%var% : ;") && !Token::Match(tok, "case|default")) {
tok = tok->previous(); tok = tok->previous();
tok->deleteNext(); tok->deleteNext();
} else { } else {
indentret = 0; indentflow = 0;
tok = tok->previous(); tok = tok->previous();
} }
} }
@ -4448,7 +4449,7 @@ void Tokenizer::simplifyFlowControl()
} }
bool Tokenizer::removeReduntantConditions() bool Tokenizer::removeRedundantConditions()
{ {
// Return value for function. Set to true if there are any simplifications // Return value for function. Set to true if there are any simplifications
bool ret = false; bool ret = false;

View File

@ -367,7 +367,7 @@ public:
* @return true if something is modified * @return true if something is modified
* false if nothing is done. * false if nothing is done.
*/ */
bool removeReduntantConditions(); bool removeRedundantConditions();
/** /**
* Remove redundant for: * Remove redundant for:

View File

@ -163,6 +163,7 @@ private:
TEST_CASE(return3); TEST_CASE(return3);
TEST_CASE(return4); TEST_CASE(return4);
TEST_CASE(return5); TEST_CASE(return5);
TEST_CASE(return6);
TEST_CASE(break1); TEST_CASE(break1);
TEST_CASE(break2); TEST_CASE(break2);
@ -3086,9 +3087,9 @@ private:
void return2() { void return2() {
const char code[] = "void f(){ " const char code[] = "void f(){ "
"if (k>0) goto label; " " if (k>0) goto label; "
"return; " " return; "
"if (tnt) " " if (tnt) "
" { " " { "
" { " " { "
" check(); " " check(); "
@ -3102,6 +3103,21 @@ private:
} }
void return3() { void return3() {
const char code[] = "void foo () {"
" return;"
" {"
" boo();"
" while (n) { --n; }"
" {"
" label:"
" ok();"
" }"
" }"
"}";
ASSERT_EQUALS("void foo ( ) { return ; { { label : ; ok ( ) ; } } }", tok(code));
}
void return4() {
const char code[] = "int f() { " const char code[] = "int f() { "
"switch (x) { case 1: return 1; bar(); tack; { ticak(); return; } return; " "switch (x) { case 1: return 1; bar(); tack; { ticak(); return; } return; "
"case 2: return 2; { random(); } tack(); " "case 2: return 2; { random(); } tack(); "
@ -3110,7 +3126,7 @@ private:
ASSERT_EQUALS("int f ( ) { switch ( x ) { case 1 : ; return 1 ; case 2 : ; return 2 ; } return 3 ; }",tok(code)); ASSERT_EQUALS("int f ( ) { switch ( x ) { case 1 : ; return 1 ; case 2 : ; return 2 ; } return 3 ; }",tok(code));
} }
void return4() { void return5() {
const char code[] = "int f() {" const char code[] = "int f() {"
"switch (x) { case 1: return 1; bar(); tack; { ticak(); return; } return;" "switch (x) { case 1: return 1; bar(); tack; { ticak(); return; } return;"
"case 2: switch(y) { case 1: return 0; bar2(); foo(); case 2: return 7; }" "case 2: switch(y) { case 1: return 0; bar2(); foo(); case 2: return 7; }"
@ -3122,7 +3138,7 @@ private:
ASSERT_EQUALS(expected,tok(code)); ASSERT_EQUALS(expected,tok(code));
} }
void return5() { void return6() {
const char code[] = "void foo () {" const char code[] = "void foo () {"
" switch (i) { case 0: switch (j) { case 0: return -1; }" " switch (i) { case 0: switch (j) { case 0: return -1; }"
" case 1: switch (j) { case -1: return -1; }" " case 1: switch (j) { case -1: return -1; }"