Tokenizer: Removed Tokenizer::simplifyIfNotNull (#6072)
This commit is contained in:
parent
ffc707246b
commit
1b8252181d
|
@ -366,11 +366,18 @@ bool CheckCondition::isOppositeCond(bool isNot, const Token * const cond1, const
|
|||
if (!cond1 || !cond2)
|
||||
return false;
|
||||
|
||||
if (cond1->str() == "!")
|
||||
if (cond1->str() == "!") {
|
||||
if (cond2->str() == "!=") {
|
||||
if (cond2->astOperand1()->str() == "0")
|
||||
return isSameExpression(_tokenizer, cond1->astOperand1(), cond2->astOperand2(), constFunctions);
|
||||
if (cond2->astOperand2()->str() == "0")
|
||||
return isSameExpression(_tokenizer, cond1->astOperand1(), cond2->astOperand1(), constFunctions);
|
||||
}
|
||||
return isSameExpression(_tokenizer, cond1->astOperand1(), cond2, constFunctions);
|
||||
}
|
||||
|
||||
if (cond2->str() == "!")
|
||||
return isSameExpression(_tokenizer, cond1, cond2->astOperand1(), constFunctions);
|
||||
return isOppositeCond(isNot, cond2, cond1, constFunctions);
|
||||
|
||||
if (!cond1->isComparisonOp() || !cond2->isComparisonOp())
|
||||
return false;
|
||||
|
|
|
@ -3703,7 +3703,6 @@ bool Tokenizer::simplifyTokenList2()
|
|||
simplifyIfAndWhileAssign();
|
||||
simplifyRedundantParentheses();
|
||||
simplifyIfNot();
|
||||
simplifyIfNotNull();
|
||||
simplifyIfSameInnerCondition();
|
||||
simplifyNestedStrcat();
|
||||
simplifyFuncInWhile();
|
||||
|
@ -6069,63 +6068,6 @@ void Tokenizer::simplifyIfNot()
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
void Tokenizer::simplifyIfNotNull()
|
||||
{
|
||||
for (Token *tok = list.front(); tok; tok = tok->next()) {
|
||||
Token *deleteFrom = nullptr;
|
||||
|
||||
// Remove 'x = x != 0;'
|
||||
if (Token::simpleMatch(tok, "=")) {
|
||||
if (Token::Match(tok->tokAt(-2), "[;{}] %name%")) {
|
||||
const std::string& varname(tok->previous()->str());
|
||||
|
||||
if (Token::simpleMatch(tok->next(), (varname + " != 0 ;").c_str()) ||
|
||||
Token::simpleMatch(tok->next(), ("0 != " + varname + " ;").c_str())) {
|
||||
tok = tok->tokAt(-2);
|
||||
tok->deleteNext(6);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (Token::Match(tok, "==|!= ("))
|
||||
tok = tok->linkAt(1);
|
||||
|
||||
if (Token::Match(tok, "(|&&|%oror%")) {
|
||||
tok = tok->next();
|
||||
|
||||
if (!tok)
|
||||
break;
|
||||
|
||||
if (Token::Match(tok, "0 != %name%|(")) {
|
||||
deleteFrom = tok->previous();
|
||||
if (tok->tokAt(2))
|
||||
tok->tokAt(2)->isPointerCompare(true);
|
||||
}
|
||||
|
||||
else if (Token::Match(tok, "%name% != 0")) {
|
||||
deleteFrom = tok;
|
||||
tok->isPointerCompare(true);
|
||||
tok->isExpandedMacro(tok->isExpandedMacro() || tok->tokAt(2)->isExpandedMacro());
|
||||
}
|
||||
|
||||
else if (Token::Match(tok, "%name% .|:: %name% != 0")) {
|
||||
tok = tok->tokAt(2);
|
||||
deleteFrom = tok;
|
||||
tok->isPointerCompare(true);
|
||||
tok->isExpandedMacro(tok->isExpandedMacro() || tok->tokAt(2)->isExpandedMacro());
|
||||
}
|
||||
}
|
||||
|
||||
if (deleteFrom) {
|
||||
Token::eraseTokens(deleteFrom, deleteFrom->tokAt(3));
|
||||
tok = deleteFrom;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Tokenizer::simplifyIfSameInnerCondition()
|
||||
{
|
||||
// same inner condition
|
||||
|
|
|
@ -305,13 +305,6 @@ public:
|
|||
*/
|
||||
void simplifyIfNot();
|
||||
|
||||
/**
|
||||
* simplify if-not NULL
|
||||
* Example: "if(0!=x);" => "if(x);"
|
||||
* Special case: 'x = (0 != x);' is removed.
|
||||
*/
|
||||
void simplifyIfNotNull();
|
||||
|
||||
/** @brief simplify if (a) { if (a) */
|
||||
void simplifyIfSameInnerCondition();
|
||||
|
||||
|
|
|
@ -1389,7 +1389,7 @@ private:
|
|||
" int y = rand(), z = rand();\n"
|
||||
" if (y==0 || y!=0 && z);\n"
|
||||
"}", false);
|
||||
ASSERT_EQUALS("[test.cpp:3]: (style) Redundant condition: y. 'A && (!A || B)' is equivalent to 'A || B'\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:3]: (style) Redundant condition: y!=0. 'A && (!A || B)' is equivalent to 'A || B'\n", errout.str());
|
||||
|
||||
check("void f() {\n"
|
||||
" if (x>0 || (x<0 && y)) {}\n"
|
||||
|
|
|
@ -99,7 +99,6 @@ private:
|
|||
|
||||
// "if(0==x)" => "if(!x)"
|
||||
TEST_CASE(simplifyIfNot);
|
||||
TEST_CASE(simplifyIfNotNull);
|
||||
|
||||
TEST_CASE(combine_wstrings);
|
||||
|
||||
|
@ -1687,13 +1686,6 @@ private:
|
|||
ASSERT_EQUALS("if ( ! ( ! fclose ( fd ) ) ) { ; }", tok("if(!(fclose(fd) == 0));"));
|
||||
}
|
||||
|
||||
void simplifyIfNotNull() {
|
||||
const char code[] = "void f(int x) {\n"
|
||||
" x = (x != 0);\n"
|
||||
"}";
|
||||
ASSERT_EQUALS("void f ( int x ) { }", tok(code, true));
|
||||
}
|
||||
|
||||
void not1() {
|
||||
ASSERT_EQUALS("void f ( ) { if ( ! p ) { ; } }", tok("void f() { if (not p); }", "test.c", false));
|
||||
ASSERT_EQUALS("void f ( ) { if ( p && ! q ) { ; } }", tok("void f() { if (p && not q); }", "test.c", false));
|
||||
|
|
|
@ -127,8 +127,6 @@ private:
|
|||
TEST_CASE(forAddBraces1);
|
||||
TEST_CASE(forAddBraces2); // #5088
|
||||
|
||||
TEST_CASE(pointers_condition);
|
||||
|
||||
TEST_CASE(simplifyKnownVariables1);
|
||||
TEST_CASE(simplifyKnownVariables2);
|
||||
TEST_CASE(simplifyKnownVariables3);
|
||||
|
@ -1082,47 +1080,6 @@ private:
|
|||
ASSERT_EQUALS(";\n\nasm ( \"\"mov ax,bx\"\" ) ;", tokenizeAndStringify(";\n\n__asm__ volatile ( \"mov ax,bx\" );", true));
|
||||
}
|
||||
|
||||
|
||||
void pointers_condition() {
|
||||
ASSERT_EQUALS("( p )", tokenizeAndStringify("( p != NULL )", true));
|
||||
ASSERT_EQUALS("( p )", tokenizeAndStringify("( NULL != p )", true));
|
||||
ASSERT_EQUALS("( this . p )", tokenizeAndStringify("( this->p != NULL )", true));
|
||||
ASSERT_EQUALS("( this . p )", tokenizeAndStringify("( NULL != this->p )", true));
|
||||
ASSERT_EQUALS("( Foo :: p )", tokenizeAndStringify("( Foo::p != NULL )", true));
|
||||
ASSERT_EQUALS("( Foo :: p )", tokenizeAndStringify("( NULL != Foo::p )", true));
|
||||
|
||||
ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( p == NULL )", true));
|
||||
ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( NULL == p )", true));
|
||||
ASSERT_EQUALS("( ! this . p )", tokenizeAndStringify("( this->p == NULL )", true));
|
||||
ASSERT_EQUALS("( ! this . p )", tokenizeAndStringify("( NULL == this->p )", true));
|
||||
ASSERT_EQUALS("( ! Foo :: p )", tokenizeAndStringify("( Foo::p == NULL )", true));
|
||||
ASSERT_EQUALS("( ! Foo :: p )", tokenizeAndStringify("( NULL == Foo::p )", true));
|
||||
|
||||
ASSERT_EQUALS("( p1 || ! p2 )", tokenizeAndStringify("( p1 != NULL || p2 == NULL )", true));
|
||||
ASSERT_EQUALS("( p1 && ! p2 )", tokenizeAndStringify("( p1 != NULL && p2 == NULL )", true));
|
||||
ASSERT_EQUALS("a & & b", tokenizeAndStringify("a & &b", true));
|
||||
|
||||
ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( p == false )", true));
|
||||
ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( p == 0 )", true));
|
||||
ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( p == '\\0' )", true));
|
||||
ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( p == 0L )", true));
|
||||
ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( p == 0UL )", true));
|
||||
ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( p == 0ul )", true));
|
||||
ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( p == 0l )", true));
|
||||
|
||||
ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( false == p )", true));
|
||||
ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( 0 == p )", true));
|
||||
ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( '\\0' == p )", true));
|
||||
ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( 0L == p )", true));
|
||||
ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( 0UL == p )", true));
|
||||
ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( 0ul == p )", true));
|
||||
ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( 0l == p )", true));
|
||||
|
||||
// not pointer
|
||||
ASSERT_EQUALS("( x != ( y != 0 ) )", tokenizeAndStringify("( x != ( y != 0 ) )", false));
|
||||
}
|
||||
|
||||
|
||||
void ifAddBraces1() {
|
||||
const char code[] = "void f()\n"
|
||||
"{\n"
|
||||
|
|
Loading…
Reference in New Issue