Fix #2849, False positive: memory leak (using question mark operator)

http://sourceforge.net/apps/trac/cppcheck/ticket/2849
Simplify: *d = b ? b : a; into if-else
Simplify { ; { ; } } into { ; }
Removed one memleak test which should be redundant now as question marks
should not get into memleak check.
This commit is contained in:
Reijo Tomperi 2011-06-30 22:04:26 +03:00
parent 6375e1d3dc
commit 89fd5bacf9
4 changed files with 77 additions and 11 deletions

View File

@ -4928,9 +4928,22 @@ void Tokenizer::removeRedundantSemicolons()
{ {
tok = tok->link(); tok = tok->link();
} }
while (Token::simpleMatch(tok, "; ;")) for (;;)
{ {
tok->deleteNext(); if (Token::simpleMatch(tok, "; ;"))
{
tok->deleteNext();
}
else if (Token::simpleMatch(tok, "; { ; }"))
{
tok->deleteNext();
tok->deleteNext();
tok->deleteNext();
}
else
{
break;
}
} }
} }
} }
@ -5270,14 +5283,31 @@ void Tokenizer::simplifyConditionOperator()
++parlevel; ++parlevel;
else if (tok->str() == ")") else if (tok->str() == ")")
--parlevel; --parlevel;
else if (parlevel == 0 && Token::Match(tok, "; %var% = %var% ? %var% : %var% ;")) else if (parlevel == 0 && Token::Match(tok, ";|{|} *| %any% = %any% ? %any% : %any% ;"))
{ {
const std::string var(tok->strAt(1)); std::string var(tok->strAt(1));
bool isPointer = false;
if (Token::simpleMatch(tok->next(), "*"))
{
tok = tok->next();
var += " " + tok->strAt(1);
isPointer = true;
}
const std::string condition(tok->strAt(3)); const std::string condition(tok->strAt(3));
const std::string value1(tok->strAt(5)); const std::string value1(tok->strAt(5));
const std::string value2(tok->strAt(7)); const std::string value2(tok->strAt(7));
Token::eraseTokens(tok, tok->tokAt(9)); if (isPointer)
{
tok = tok->previous();
Token::eraseTokens(tok, tok->tokAt(10));
}
else
{
Token::eraseTokens(tok, tok->tokAt(9));
}
std::string str("if ( " + condition + " ) { " + var + " = " + value1 + " ; } else { " + var + " = " + value2 + " ; }"); std::string str("if ( " + condition + " ) { " + var + " = " + value1 + " ; } else { " + var + " = " + value2 + " ; }");
std::string::size_type pos1 = 0; std::string::size_type pos1 = 0;
@ -5297,9 +5327,18 @@ void Tokenizer::simplifyConditionOperator()
tok = tok->next(); tok = tok->next();
} }
Token::createMutualLinks(tok->tokAt(-15), tok->tokAt(-13)); if (isPointer)
Token::createMutualLinks(tok->tokAt(-12), tok->tokAt(-7)); {
Token::createMutualLinks(tok->tokAt(-5), tok); Token::createMutualLinks(tok->tokAt(-17), tok->tokAt(-15));
Token::createMutualLinks(tok->tokAt(-14), tok->tokAt(-8));
Token::createMutualLinks(tok->tokAt(-6), tok);
}
else
{
Token::createMutualLinks(tok->tokAt(-15), tok->tokAt(-13));
Token::createMutualLinks(tok->tokAt(-12), tok->tokAt(-7));
Token::createMutualLinks(tok->tokAt(-5), tok);
}
} }
} }
} }

View File

@ -484,7 +484,6 @@ private:
ASSERT_EQUALS(";;use;;", getcode("char *s; x = {1,s};", "s")); ASSERT_EQUALS(";;use;;", getcode("char *s; x = {1,s};", "s"));
ASSERT_EQUALS(";{};;alloc;;use;", getcode("struct Foo { }; Foo *p; p = malloc(10); const Foo *q; q = p;", "p")); ASSERT_EQUALS(";{};;alloc;;use;", getcode("struct Foo { }; Foo *p; p = malloc(10); const Foo *q; q = p;", "p"));
ASSERT_EQUALS(";;alloc;use;", getcode("Fred *fred; p.setFred(fred = new Fred);", "fred")); ASSERT_EQUALS(";;alloc;use;", getcode("Fred *fred; p.setFred(fred = new Fred);", "fred"));
ASSERT_EQUALS(";;use;", getcode("char *s; *s2 = s ? s : p;", "s"));
// non-use.. // non-use..
ASSERT_EQUALS(";;", getcode("char *s; s = s + 1;", "s")); ASSERT_EQUALS(";;", getcode("char *s; s = s + 1;", "s"));

View File

@ -485,7 +485,7 @@ private:
{ {
const char code1[] = "void f() { int a; bool use = true; if( use ) a=0; else if( bb ) a=1; else if( cc ) a=33; else { gg = 0; } int c=1; }"; const char code1[] = "void f() { int a; bool use = true; if( use ) a=0; else if( bb ) a=1; else if( cc ) a=33; else { gg = 0; } int c=1; }";
const char code2[] = "void f ( ) { ; { ; } ; }"; const char code2[] = "void f ( ) { ; }";
ASSERT_EQUALS(code2, tok(code1)); ASSERT_EQUALS(code2, tok(code1));
} }
@ -2698,6 +2698,34 @@ private:
const char code[] = "= 1 ? 0 : ({ 0; });"; const char code[] = "= 1 ? 0 : ({ 0; });";
ASSERT_EQUALS("= 0 ;", tok(code)); ASSERT_EQUALS("= 0 ;", tok(code));
} }
{
const char code[] = "int f(int b, int d)\n"
"{\n"
" d = b ? b : 10;\n"
" return d;\n"
"}\n";
ASSERT_EQUALS("int f ( int b , int d ) { if ( b ) { d = b ; } else { d = 10 ; } return d ; }", tok(code));
}
{
const char code[] = "int f(int b, int *d)\n"
"{\n"
" *d = b ? b : 10;\n"
" return *d;\n"
"}\n";
ASSERT_EQUALS("int f ( int b , int * d ) { if ( b ) { * d = b ; } else { * d = 10 ; } return * d ; }", tok(code));
}
{
const char code[] = "int f(int b, int *d)\n"
"{\n"
" if(b) {b++;}"
" *d = b ? b : 10;\n"
" return *d;\n"
"}\n";
ASSERT_EQUALS("int f ( int b , int * d ) { if ( b ) { b ++ ; } if ( b ) { * d = b ; } else { * d = 10 ; } return * d ; }", tok(code));
}
} }
void calculations() void calculations()

View File

@ -2048,7 +2048,7 @@ private:
const char expected[] = "void f ( ) {\n" const char expected[] = "void f ( ) {\n"
";\n" ";\n"
"\n" "\n"
"{ ; }\n" "\n"
"}"; "}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
} }