Tokenizer: Removed simplifyComparisonOrder

This commit is contained in:
Daniel Marjamäki 2014-01-17 17:25:56 +01:00
parent 0b4de97e2b
commit 4eb746d7cc
6 changed files with 23 additions and 60 deletions

View File

@ -999,12 +999,16 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
if (Token::simpleMatch(tok, "if (")) { if (Token::simpleMatch(tok, "if (")) {
if (alloctype == Fd) { if (alloctype == Fd) {
if (Token::Match(tok, "if ( 0|-1 <=|< %varid% )", varid) || if (Token::Match(tok, "if ( 0|-1 <=|< %varid% )", varid) ||
Token::Match(tok, "if ( %varid% != -1 )", varid)) { Token::Match(tok, "if ( %varid% >|>= 0|-1 )", varid) ||
Token::Match(tok, "if ( %varid% != -1 )", varid) ||
Token::Match(tok, "if ( -1 != %varid% )", varid)) {
addtoken(&rettail, tok, "if(var)"); addtoken(&rettail, tok, "if(var)");
tok = tok->next()->link(); tok = tok->next()->link();
continue; continue;
} else if (Token::Match(tok, "if ( %varid% == -1 )", varid) || } else if (Token::Match(tok, "if ( %varid% == -1 )", varid) ||
Token::Match(tok, "if ( %varid% < 0 )", varid)) { Token::Match(tok, "if ( -1 == %varid% )", varid) ||
Token::Match(tok, "if ( %varid% < 0 )", varid) ||
Token::Match(tok, "if ( 0 > %varid% )", varid)) {
addtoken(&rettail, tok, "if(!var)"); addtoken(&rettail, tok, "if(!var)");
tok = tok->next()->link(); tok = tok->next()->link();
continue; continue;
@ -1122,12 +1126,16 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
else if (alloctype == Fd && varid) { else if (alloctype == Fd && varid) {
if (Token::Match(tok, "while ( 0 <= %varid% )", varid) || if (Token::Match(tok, "while ( 0 <= %varid% )", varid) ||
Token::Match(tok, "while ( %varid% != -1 )", varid)) { Token::Match(tok, "while ( %varid% >= 0 )", varid) ||
Token::Match(tok, "while ( %varid% != -1 )", varid) ||
Token::Match(tok, "while ( -1 != %varid% )", varid)) {
addtoken(&rettail, tok, "while(var)"); addtoken(&rettail, tok, "while(var)");
tok = end; tok = end;
continue; continue;
} else if (Token::Match(tok, "while ( %varid% == -1 )", varid) || } else if (Token::Match(tok, "while ( %varid% == -1 )", varid) ||
Token::Match(tok, "while ( %varid% < 0 )", varid)) { Token::Match(tok, "while ( -1 == %varid% )", varid) ||
Token::Match(tok, "while ( %varid% < 0 )", varid) ||
Token::Match(tok, "while ( 0 > %varid% )", varid)) {
addtoken(&rettail, tok, "while(!var)"); addtoken(&rettail, tok, "while(!var)");
tok = end; tok = end;
continue; continue;

View File

@ -3302,9 +3302,6 @@ bool Tokenizer::simplifyTokenList1()
// simplify bit fields.. // simplify bit fields..
simplifyBitfields(); simplifyBitfields();
// Use "<" comparison instead of ">"
simplifyComparisonOrder();
// Simplify '(p == 0)' to '(!p)' // Simplify '(p == 0)' to '(!p)'
simplifyIfNot(); simplifyIfNot();
simplifyIfNotNull(); simplifyIfNotNull();
@ -3649,7 +3646,6 @@ bool Tokenizer::simplifyTokenList2()
simplifyIfNot(); simplifyIfNot();
simplifyIfNotNull(); simplifyIfNotNull();
simplifyIfSameInnerCondition(); simplifyIfSameInnerCondition();
simplifyComparisonOrder();
simplifyNestedStrcat(); simplifyNestedStrcat();
simplifyWhile0(); simplifyWhile0();
simplifyFuncInWhile(); simplifyFuncInWhile();
@ -9063,33 +9059,6 @@ void Tokenizer::simplifyStructInit()
} }
void Tokenizer::simplifyComparisonOrder()
{
// Use "<" comparison instead of ">"
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "[;(] %var%|%num% >|>= %num%|%var% [);]")) {
const Token *operand2 = tok->tokAt(3);
const std::string op1(tok->next()->str());
unsigned int var1 = tok->next()->varId();
tok->next()->str(operand2->str());
tok->next()->varId(operand2->varId());
tok->tokAt(3)->str(op1);
tok->tokAt(3)->varId(var1);
if (tok->strAt(2) == ">")
tok->tokAt(2)->str("<");
else
tok->tokAt(2)->str("<=");
} else if (Token::Match(tok, "( %num% ==|!= %var% )")) {
const std::string op1(tok->next()->str());
unsigned int var1 = tok->next()->varId();
tok->next()->str(tok->strAt(3));
tok->next()->varId(tok->tokAt(3)->varId());
tok->tokAt(3)->str(op1);
tok->tokAt(3)->varId(var1);
}
}
}
void Tokenizer::simplifyConst() void Tokenizer::simplifyConst()
{ {
for (Token *tok = list.front(); tok; tok = tok->next()) { for (Token *tok = list.front(); tok; tok = tok->next()) {

View File

@ -514,12 +514,6 @@ public:
*/ */
static std::string simplifyString(const std::string &source); static std::string simplifyString(const std::string &source);
/**
* Use "<" comparison instead of ">"
* Use "<=" comparison instead of ">="
*/
void simplifyComparisonOrder();
/** /**
* Change "int const x;" into "const int x;" * Change "int const x;" into "const int x;"
*/ */

View File

@ -210,6 +210,7 @@ private:
settings->inconclusive = inconclusive; settings->inconclusive = inconclusive;
settings->experimental = experimental; settings->experimental = experimental;
settings->standards.posix = posix; settings->standards.posix = posix;
settings->valueFlow = true;
if (posix) { if (posix) {
const char cfg[] = "<?xml version=\"1.0\"?>\n" const char cfg[] = "<?xml version=\"1.0\"?>\n"

View File

@ -848,7 +848,7 @@ private:
ASSERT_EQUALS("= p ;", tok("= (p);")); ASSERT_EQUALS("= p ;", tok("= (p);"));
ASSERT_EQUALS("if ( a < p ) { }", tok("if(a<(p)){}")); ASSERT_EQUALS("if ( a < p ) { }", tok("if(a<(p)){}"));
ASSERT_EQUALS("void f ( ) { int p ; if ( p == -1 ) { } }", tok("void f(){int p; if((p)==-1){}}")); ASSERT_EQUALS("void f ( ) { int p ; if ( p == -1 ) { } }", tok("void f(){int p; if((p)==-1){}}"));
ASSERT_EQUALS("void f ( ) { int p ; if ( p == -1 ) { } }", tok("void f(){int p; if(-1==(p)){}}")); ASSERT_EQUALS("void f ( ) { int p ; if ( -1 == p ) { } }", tok("void f(){int p; if(-1==(p)){}}"));
ASSERT_EQUALS("void f ( ) { int p ; if ( p ) { } }", tok("void f(){int p; if((p)){}}")); ASSERT_EQUALS("void f ( ) { int p ; if ( p ) { } }", tok("void f(){int p; if((p)){}}"));
ASSERT_EQUALS("return p ;", tok("return (p);")); ASSERT_EQUALS("return p ;", tok("return (p);"));
ASSERT_EQUALS("void f ( ) { int * p ; if ( ! * p ) { } }", tok("void f(){int *p; if (*(p) == 0) {}}")); ASSERT_EQUALS("void f ( ) { int * p ; if ( ! * p ) { } }", tok("void f(){int *p; if (*(p) == 0) {}}"));
@ -865,7 +865,7 @@ private:
ASSERT_EQUALS("return ( a + b ) * c ;", tok("return (a+b)*c;")); ASSERT_EQUALS("return ( a + b ) * c ;", tok("return (a+b)*c;"));
ASSERT_EQUALS("void f ( ) { int p ; if ( 2 * p == 0 ) { } }", tok("void f(){int p; if (2*p == 0) {}}")); ASSERT_EQUALS("void f ( ) { int p ; if ( 2 * p == 0 ) { } }", tok("void f(){int p; if (2*p == 0) {}}"));
ASSERT_EQUALS("void f ( ) { DIR * f ; f = opendir ( dirname ) ; if ( closedir ( f ) ) { } }", tok("void f(){DIR * f = opendir(dirname);if (closedir(f)){}}")); ASSERT_EQUALS("void f ( ) { DIR * f ; f = opendir ( dirname ) ; if ( closedir ( f ) ) { } }", tok("void f(){DIR * f = opendir(dirname);if (closedir(f)){}}"));
ASSERT_EQUALS("void foo ( int p ) { if ( 0 <= p ) { ; } }", tok("void foo(int p){if((p)>=0);}")); ASSERT_EQUALS("void foo ( int p ) { if ( p >= 0 ) { ; } }", tok("void foo(int p){if((p)>=0);}"));
} }
void declareVar() { void declareVar() {
@ -897,7 +897,7 @@ private:
" a = 10;\n" " a = 10;\n"
" a++;\n" " a++;\n"
"}\n"; "}\n";
ASSERT_EQUALS("void f ( int a ) { if ( 10 < a ) { a = 5 ; } else { a = 10 ; } a ++ ; }", tok(code)); ASSERT_EQUALS("void f ( int a ) { if ( a > 10 ) { a = 5 ; } else { a = 10 ; } a ++ ; }", tok(code));
} }
{ {
@ -909,7 +909,7 @@ private:
" a = 10;\n" " a = 10;\n"
" ++a;\n" " ++a;\n"
"}\n"; "}\n";
ASSERT_EQUALS("void f ( int a ) { if ( 10 < a ) { a = 5 ; } else { a = 10 ; } ++ a ; }", tok(code)); ASSERT_EQUALS("void f ( int a ) { if ( a > 10 ) { a = 5 ; } else { a = 10 ; } ++ a ; }", tok(code));
} }
} }
@ -2622,7 +2622,7 @@ private:
ASSERT_EQUALS("; a = b ( ) ; if ( ! ( a ) ) { ; }", simplifyIfAndWhileAssign(";if(!(a=b()));")); ASSERT_EQUALS("; a = b ( ) ; if ( ! ( a ) ) { ; }", simplifyIfAndWhileAssign(";if(!(a=b()));"));
ASSERT_EQUALS("; a . x = b ( ) ; if ( ! ( a . x ) ) { ; }", simplifyIfAndWhileAssign(";if(!(a->x=b()));")); ASSERT_EQUALS("; a . x = b ( ) ; if ( ! ( a . x ) ) { ; }", simplifyIfAndWhileAssign(";if(!(a->x=b()));"));
ASSERT_EQUALS("void f ( ) { A ( ) a = b ; if ( a ) { ; } }", simplifyIfAndWhileAssign("void f() { A() if(a=b); }")); ASSERT_EQUALS("void f ( ) { A ( ) a = b ; if ( a ) { ; } }", simplifyIfAndWhileAssign("void f() { A() if(a=b); }"));
ASSERT_EQUALS("void foo ( int a ) { a = b ( ) ; if ( 0 <= a ) { ; } }", tok("void foo(int a) {if((a=b())>=0);}")); ASSERT_EQUALS("void foo ( int a ) { a = b ( ) ; if ( a >= 0 ) { ; } }", tok("void foo(int a) {if((a=b())>=0);}"));
TODO_ASSERT_EQUALS("void foo ( A a ) { a . c = b ( ) ; if ( 0 <= a . c ) { ; } }", TODO_ASSERT_EQUALS("void foo ( A a ) { a . c = b ( ) ; if ( 0 <= a . c ) { ; } }",
"void foo ( A a ) { a . c = b ( ) ; if ( a . c >= 0 ) { ; } }", "void foo ( A a ) { a . c = b ( ) ; if ( a . c >= 0 ) { ; } }",
tok("void foo(A a) {if((a.c=b())>=0);}")); tok("void foo(A a) {if((a.c=b())>=0);}"));
@ -3610,7 +3610,7 @@ private:
" bar(); " " bar(); "
" } " " } "
"}"; "}";
ASSERT_EQUALS("void f ( ) { if ( 0 < k ) { goto label ; } " + *it + " ; { label : ; bar ( ) ; } }",tok(code)); ASSERT_EQUALS("void f ( ) { if ( k > 0 ) { goto label ; } " + *it + " ; { label : ; bar ( ) ; } }",tok(code));
} }
{ {

View File

@ -420,8 +420,6 @@ private:
TEST_CASE(removeExceptionSpecification5); TEST_CASE(removeExceptionSpecification5);
TEST_CASE(removeExceptionSpecification6); // #4617 TEST_CASE(removeExceptionSpecification6); // #4617
TEST_CASE(gt); // use "<" comparisons instead of ">"
TEST_CASE(simplifyString); TEST_CASE(simplifyString);
TEST_CASE(simplifyConst); TEST_CASE(simplifyConst);
TEST_CASE(switchCase); TEST_CASE(switchCase);
@ -1857,7 +1855,7 @@ private:
void simplifyKnownVariables19() { void simplifyKnownVariables19() {
const char code[] = "void f ( ) { int i=0; do { if (i>0) { a(); } i=b(); } while (i != 12); }"; const char code[] = "void f ( ) { int i=0; do { if (i>0) { a(); } i=b(); } while (i != 12); }";
ASSERT_EQUALS( ASSERT_EQUALS(
"void f ( ) { int i ; i = 0 ; do { if ( 0 < i ) { a ( ) ; } i = b ( ) ; } while ( i != 12 ) ; }", "void f ( ) { int i ; i = 0 ; do { if ( i > 0 ) { a ( ) ; } i = b ( ) ; } while ( i != 12 ) ; }",
simplifyKnownVariables(code)); simplifyKnownVariables(code));
} }
@ -2109,7 +2107,7 @@ private:
"{" "{"
" int i ; i = 2 ;" " int i ; i = 2 ;"
" if ( g ) { }" " if ( g ) { }"
" if ( 0 < 2 ) { } " " if ( 2 > 0 ) { } "
"}", "}",
simplifyKnownVariables(code)); simplifyKnownVariables(code));
} }
@ -2488,7 +2486,7 @@ private:
"}\n"; "}\n";
const char expected[] = "void f ( ) {\n" const char expected[] = "void f ( ) {\n"
"int x ; x = 10 ;\n" "int x ; x = 10 ;\n"
"do { cin >> x ; } while ( 5 < x ) ;\n" "do { cin >> x ; } while ( x > 5 ) ;\n"
"a [ x ] = 0 ;\n" "a [ x ] = 0 ;\n"
"}"; "}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
@ -2747,7 +2745,7 @@ private:
"}"; "}";
const char expected[] = "void f ( int sz ) {\n" const char expected[] = "void f ( int sz ) {\n"
"int i ;\n" "int i ;\n"
"for ( i = 0 ; ( i < sz ) && ( 3 < sz ) ; ++ i ) { }\n" "for ( i = 0 ; ( i < sz ) && ( sz > 3 ) ; ++ i ) { }\n"
"}"; "}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Settings::Unspecified, "test.c")); ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Settings::Unspecified, "test.c"));
} }
@ -6812,13 +6810,6 @@ private:
tokenizeAndStringify("void foo () noexcept(noexcept(true)) const { }")); tokenizeAndStringify("void foo () noexcept(noexcept(true)) const { }"));
} }
void gt() {
ASSERT_EQUALS("( i < 10 )", tokenizeAndStringify("(10>i)"));
ASSERT_EQUALS("; i < 10 ;", tokenizeAndStringify(";10>i;"));
ASSERT_EQUALS("void > ( ) ; void > ( )",
tokenizeAndStringify("void>(); void>()"));
}
void simplifyString() { void simplifyString() {
errout.str(""); errout.str("");