Fixed #4237 (False positive: (error) Memory Leak: data (references and pointer aliases))

This commit is contained in:
Daniel Marjamäki 2013-07-30 11:30:14 +02:00
parent 66e406fe43
commit 807204e580
3 changed files with 32 additions and 3 deletions

View File

@ -6017,6 +6017,32 @@ bool Tokenizer::simplifyKnownVariables()
tok = tok->previous();
goback = false;
}
// Reference to variable
if (Token::Match(tok, "%type%|* & %var% = %var% ;")) {
Token *start = tok->previous();
while (Token::Match(start,"%type%|*|&"))
start = start->previous();
if (!Token::Match(start,"[;{}]"))
continue;
const Token *reftok = tok->tokAt(2);
const Token *vartok = reftok->tokAt(2);
int level = 0;
for (Token *tok2 = tok->tokAt(6); tok2; tok2 = tok2->next()) {
if (tok2->str() == "{") {
++level;
} else if (tok2->str() == "}") {
if (level <= 0)
break;
--level;
} else if (tok2->varId() == reftok->varId()) {
tok2->str(vartok->str());
tok2->varId(vartok->varId());
}
}
Token::eraseTokens(start, tok->tokAt(6));
tok = start;
}
if (tok->isName() && (Token::Match(tok, "static| const| static| %type% const| %var% = %any% ;") ||
Token::Match(tok, "static| const| static| %type% const| %var% ( %any% ) ;"))) {
bool isconst = false;

View File

@ -517,9 +517,6 @@ private:
// use reference
ASSERT_EQUALS(";;callfunc&use;", getcode("struct AB *ab; f(&ab);", "ab"));
// reference
ASSERT_EQUALS(";", getcode("char *p; char * & ref = p; p = malloc(10);", "p"));
// return..
ASSERT_EQUALS(";;return;", getcode("char *s; return;", "s"));
ASSERT_EQUALS(";;returnuse;", getcode("char *s; return s;", "s"));

View File

@ -175,6 +175,7 @@ private:
TEST_CASE(simplifyKnownVariables50); // #4066 sprintf changes
TEST_CASE(simplifyKnownVariables51); // #4409 hang
TEST_CASE(simplifyKnownVariables52); // #4728 "= x %cop%"
TEST_CASE(simplifyKnownVariables53); // references
TEST_CASE(simplifyKnownVariablesIfEq1); // if (a==5) => a is 5 in the block
TEST_CASE(simplifyKnownVariablesIfEq2); // if (a==5) { buf[a++] = 0; }
TEST_CASE(simplifyKnownVariablesBailOutAssign1);
@ -2722,6 +2723,11 @@ private:
ASSERT_EQUALS("void f ( ) { }", tokenizeAndStringify("void f() { Foo *p = 0; bool b = (p && (p->type() == 1)); }", true));
}
void simplifyKnownVariables53() { // references
ASSERT_EQUALS("void f ( ) { int x ; x = abc ( ) ; }", tokenizeAndStringify("void f() { int x; int &ref=x; ref=abc(); }", true));
ASSERT_EQUALS("void f ( ) { int * p ; p = abc ( ) ; }", tokenizeAndStringify("void f() { int *p; int *&ref=p; ref=abc(); }", true));
}
void simplifyKnownVariablesIfEq1() {
const char code[] = "void f(int x) {\n"
" if (x==5) {\n"