use '<' comparisons instead of '>' (#610)

This commit is contained in:
Daniel Marjamäki 2009-10-04 07:51:12 +02:00
parent 1a48f869c8
commit ee1bcb728c
4 changed files with 27 additions and 3 deletions

View File

@ -818,7 +818,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
{
if (alloctype == Fd)
{
if (Token::Match(tok, "if ( %varid% >= 0 )", varid) ||
if (Token::Match(tok, "if ( 0 <= %varid% )", varid) ||
Token::Match(tok, "if ( %varid% != -1 )", varid))
{
addtoken("if(var)");

View File

@ -572,6 +572,21 @@ bool Tokenizer::tokenize(std::istream &code, const char FileName[])
// replace "unsigned i" with "unsigned int i"
unsignedint();
// Use "<" comparison instead of ">"
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::Match(tok, "[;(] %any% >|>= %any% [);]"))
{
const std::string op1(tok->strAt(1));
tok->next()->str(tok->strAt(3));
tok->tokAt(3)->str(op1);
if (tok->tokAt(2)->str() == ">")
tok->tokAt(2)->str("<");
else
tok->tokAt(2)->str("<=");
}
}
/**
* @todo simplify "for"
* - move out start-statement "for (a;b;c);" => "{ a; for(;b;c); }"

View File

@ -441,7 +441,7 @@ private:
" a = 10;\n"
" a++;\n"
"}\n";
ASSERT_EQUALS("void f ( int a ) { if ( a > 10 ) { a = 5 ; } else { a = 10 ; } a ++ ; }", tok(code));
ASSERT_EQUALS("void f ( int a ) { if ( 10 < a ) { a = 5 ; } else { a = 10 ; } a ++ ; }", tok(code));
}
{
@ -453,7 +453,7 @@ private:
" a = 10;\n"
" ++a;\n"
"}\n";
ASSERT_EQUALS("void f ( int a ) { if ( a > 10 ) { a = 5 ; } else { a = 10 ; } ++ a ; }", tok(code));
ASSERT_EQUALS("void f ( int a ) { if ( 10 < a ) { a = 5 ; } else { a = 10 ; } ++ a ; }", tok(code));
}
}

View File

@ -157,6 +157,8 @@ private:
TEST_CASE(signed1);
TEST_CASE(removeExceptionSpecification);
TEST_CASE(gt); // use "<" comparisons instead of ">"
}
@ -2621,6 +2623,13 @@ private:
ASSERT_EQUALS(expected, tokenizeAndStringify(code));
}
void gt()
{
ASSERT_EQUALS("( i < 10 )", tokenizeAndStringify("(10>i)"));
ASSERT_EQUALS("; i < 10 ;", tokenizeAndStringify(";10>i;"));
}
};
REGISTER_TEST(TestTokenizer)