From ee1bcb728c0859f0e8b5c596195caf43f780e938 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 4 Oct 2009 07:51:12 +0200 Subject: [PATCH] use '<' comparisons instead of '>' (#610) --- src/checkmemoryleak.cpp | 2 +- src/tokenize.cpp | 15 +++++++++++++++ test/testsimplifytokens.cpp | 4 ++-- test/testtokenize.cpp | 9 +++++++++ 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index d116f0f39..c563d5897 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -818,7 +818,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list= 0 )", varid) || + if (Token::Match(tok, "if ( 0 <= %varid% )", varid) || Token::Match(tok, "if ( %varid% != -1 )", varid)) { addtoken("if(var)"); diff --git a/src/tokenize.cpp b/src/tokenize.cpp index a5cb41c1c..5203fc95d 100755 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -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); }" diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 8ecc1a57a..6d16ede19 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -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)); } } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 0e7b4d904..091906a78 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -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)