Fixed #970 (Tokenizer: Incorrect comparison order simplification)

Revert changes from 11cb7b4710 commit
for lib/tokenize.cpp (fixed it by another way).

Teach unsignedint() to expand "unsigned" in casts.

http://sourceforge.net/apps/trac/cppcheck/ticket/970
This commit is contained in:
Slava Semushin 2009-11-18 00:03:23 +06:00
parent 7126524afc
commit 42c5025000
2 changed files with 19 additions and 4 deletions

View File

@ -1853,9 +1853,6 @@ bool Tokenizer::simplifyTokenList()
// Convert e.g. atol("0") into 0
simplifyMathFunctions();
// simplify casts before removing "unsigned" keywords (to fix #961)
simplifyCasts();
// Remove unwanted keywords
static const char * const unwantedWords[] = { "unsigned", "unlikely", "likely" };
for (Token *tok = _tokens; tok; tok = tok->next())
@ -2007,6 +2004,7 @@ bool Tokenizer::simplifyTokenList()
}
simplifyLogicalOperators();
simplifyCasts();
// Simplify simple calculations..
while (simplifyCalculations())
@ -3093,7 +3091,8 @@ void Tokenizer::unsignedint()
}
// A variable declaration where the "int" is left out?
else if (!Token::Match(tok, "unsigned|signed %var% [;,=)]"))
else if (!Token::Match(tok, "unsigned|signed %var% [;,=)]") &&
!Token::Match(tok->previous(), "( unsigned|signed )"))
continue;
// Previous token should either be a symbol or one of "{};(,"

View File

@ -48,6 +48,7 @@ private:
TEST_CASE(removeCast1);
TEST_CASE(removeCast2);
TEST_CASE(removeCast3);
TEST_CASE(removeCast4);
TEST_CASE(inlineasm);
@ -157,6 +158,7 @@ private:
// unsigned i; => unsigned int i;
TEST_CASE(unsigned1);
TEST_CASE(unsigned2);
TEST_CASE(testUpdateClassList);
TEST_CASE(createLinks);
TEST_CASE(signed1);
@ -291,6 +293,13 @@ private:
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
}
void removeCast4()
{
// ticket #970
const char code[] = "if (a >= (unsigned)(b)) {}";
const char expected[] ="if ( a >= ( int ) b ) { }";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
}
void inlineasm()
{
@ -2513,6 +2522,13 @@ private:
}
void unsigned2()
{
const char code[] = "i = (unsigned)j;";
const char expected[] = "i = ( unsigned int ) j ;";
ASSERT_EQUALS(expected, tokenizeAndStringify(code));
}
void tokenizeAndUpdateClassList(const char code[])
{
// tokenize..