Fixed a bug in Tokenizer::simplifyKnownVariables.
When we find constant variables, check if there's a usage of its reference in the code (for example: don't simplify 'f(&x)' to 'f(&100)').
This commit is contained in:
parent
bbc1747919
commit
f3029ce6bb
|
@ -5805,6 +5805,18 @@ bool Tokenizer::simplifyKnownVariables()
|
||||||
const Token * const vartok = (tok->next() && tok->next()->str() == "const") ? tok->tokAt(2) : tok->next();
|
const Token * const vartok = (tok->next() && tok->next()->str() == "const") ? tok->tokAt(2) : tok->next();
|
||||||
const Token * const valuetok = vartok->tokAt(2);
|
const Token * const valuetok = vartok->tokAt(2);
|
||||||
if (Token::Match(valuetok, "%bool%|%char%|%num%|%str% ;")) {
|
if (Token::Match(valuetok, "%bool%|%char%|%num%|%str% ;")) {
|
||||||
|
//check if there's not a reference usage inside the code
|
||||||
|
bool withreference = false;
|
||||||
|
for (const Token *tok2 = valuetok->tokAt(2); tok2; tok2 = tok2->next()) {
|
||||||
|
if (Token::Match(tok2,"(|[|,|{|=|return|%op% & %varid%", vartok->varId())) {
|
||||||
|
withreference = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//don't simplify 'f(&x)' to 'f(&100)'
|
||||||
|
if (withreference)
|
||||||
|
continue;
|
||||||
|
|
||||||
constantValues[vartok->varId()] = valuetok->str();
|
constantValues[vartok->varId()] = valuetok->str();
|
||||||
|
|
||||||
// remove statement
|
// remove statement
|
||||||
|
|
|
@ -2518,6 +2518,26 @@ private:
|
||||||
"}";
|
"}";
|
||||||
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
|
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
//don't simplify '&x'!
|
||||||
|
const char code[] = "const char * foo ( ) {\n"
|
||||||
|
"const char x1 = 'b' ;\n"
|
||||||
|
"f ( & x1 ) ;\n"
|
||||||
|
"const char x2 = 'b' ;\n"
|
||||||
|
"f ( y , & x2 ) ;\n"
|
||||||
|
"const char x3 = 'b' ;\n"
|
||||||
|
"t = & x3 ;\n"
|
||||||
|
"const char x4 = 'b' ;\n"
|
||||||
|
"t = y + & x4 ;\n"
|
||||||
|
"const char x5 = 'b' ;\n"
|
||||||
|
"z [ & x5 ] = y ;\n"
|
||||||
|
"const char x6 = 'b' ;\n"
|
||||||
|
"v = { & x6 } ;\n"
|
||||||
|
"const char x7 = 'b' ;\n"
|
||||||
|
"return & x7 ;\n"
|
||||||
|
"}";
|
||||||
|
ASSERT_EQUALS(code, tokenizeAndStringify(code, true));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void simplifyKnownVariablesIfEq1() {
|
void simplifyKnownVariablesIfEq1() {
|
||||||
|
|
Loading…
Reference in New Issue