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

View File

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

View File

@ -163,6 +163,7 @@ private:
TEST_CASE(return3);
TEST_CASE(return4);
TEST_CASE(return5);
TEST_CASE(return6);
TEST_CASE(break1);
TEST_CASE(break2);
@ -3086,9 +3087,9 @@ private:
void return2() {
const char code[] = "void f(){ "
"if (k>0) goto label; "
"return; "
"if (tnt) "
" if (k>0) goto label; "
" return; "
" if (tnt) "
" { "
" { "
" check(); "
@ -3102,6 +3103,21 @@ private:
}
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() { "
"switch (x) { case 1: return 1; bar(); tack; { ticak(); return; } return; "
"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));
}
void return4() {
void return5() {
const char code[] = "int f() {"
"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; }"
@ -3122,7 +3138,7 @@ private:
ASSERT_EQUALS(expected,tok(code));
}
void return5() {
void return6() {
const char code[] = "void foo () {"
" switch (i) { case 0: switch (j) { case 0: return -1; }"
" case 1: switch (j) { case -1: return -1; }"