Fixed #896 (False positive with --all when return value of open() is positive in a loop)

http://sourceforge.net/apps/trac/cppcheck/ticket/896
This commit is contained in:
Reijo Tomperi 2009-11-05 00:58:15 +02:00
parent 141b91ae08
commit de4ca924b5
3 changed files with 31 additions and 16 deletions

View File

@ -577,19 +577,7 @@ bool Tokenizer::tokenize(std::istream &code, const char FileName[])
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("<=");
}
}
simplifyComparisonOrder();
/**
* @todo simplify "for"
@ -2033,13 +2021,12 @@ void Tokenizer::simplifyTokenList()
}
simplifyFunctionParameters();
elseif();
simplifyIfAssign();
simplifyRedundantParanthesis();
simplifyIfNot();
simplifyIfNotNull();
simplifyIfAssign();
simplifyComparisonOrder();
simplifyNestedStrcat();
for (Token *tok = _tokens; tok; tok = tok->next())
@ -3136,6 +3123,7 @@ void Tokenizer::simplifyIfAssign()
tok2->insertToken(tok->strAt(2));
}
tok2->insertToken(tok->strAt(1));
tok2->next()->varId(tok->tokAt(1)->varId());
while (! braces.empty())
{
@ -4537,6 +4525,24 @@ std::string Tokenizer::simplifyString(const std::string &source)
return str;
}
void Tokenizer::simplifyComparisonOrder()
{
// 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("<=");
}
}
}
void Tokenizer::getErrorMessages()
{
syntaxError(0, ' ');

View File

@ -304,6 +304,12 @@ private:
*/
std::string simplifyString(const std::string &source);
/**
* Use "<" comparison instead of ">"
* Use "<=" comparison instead of ">="
*/
void simplifyComparisonOrder();
/**
* Remove exception specifications. This function calls itself recursively.
* @param tok First token in scope to cleanup

View File

@ -440,6 +440,7 @@ private:
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 ( ) { 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);}"));
}
void declareVar()
@ -1391,6 +1392,8 @@ private:
ASSERT_EQUALS("; a = b ( ) ; if ( ! ( a ) ) { ; }", simplifyIfAssign(";if(!(a=b()));"));
ASSERT_EQUALS("; a . x = b ( ) ; if ( ! ( a . x ) ) { ; }", simplifyIfAssign(";if(!(a->x=b()));"));
ASSERT_EQUALS("A ( ) a = b ; if ( a ) { ; }", simplifyIfAssign("A() if(a=b);"));
ASSERT_EQUALS("void foo ( int a ) { a = b ( ) ; if ( 0 <= a ) { ; } }", tok("void foo(int a) {if((a=b())>=0);}"));
TODO_ASSERT_EQUALS("void foo ( A a ) { a . c = b ( ) ; if ( 0 <= a . c ) { ; } }", tok("void foo(A a) {if((a.c=b())>=0);}"));
}