Tokenizer: fixed so that 'p=&x; if(p)' is simplified to 'p=&x;if(&x)'. Ticket: #2596

This commit is contained in:
Daniel Marjamäki 2011-02-20 18:18:27 +01:00
parent 7894d86132
commit 63c003f92e
4 changed files with 23 additions and 9 deletions

View File

@ -6308,7 +6308,7 @@ bool Tokenizer::simplifyKnownVariables()
if (tok2->str() == tok2->strAt(2)) if (tok2->str() == tok2->strAt(2))
continue; continue;
const bool pointeralias(tok2->tokAt(2)->isName() || tok2->tokAt(2)->str() == "&"); const Token * const valueToken = tok2->tokAt(2);
std::string value; std::string value;
unsigned int valueVarId = 0; unsigned int valueVarId = 0;
@ -6319,7 +6319,7 @@ bool Tokenizer::simplifyKnownVariables()
if (!simplifyKnownVariablesGetData(varid, &tok2, &tok3, value, valueVarId, valueIsPointer, floatvars.find(tok2->varId()) != floatvars.end())) if (!simplifyKnownVariablesGetData(varid, &tok2, &tok3, value, valueVarId, valueIsPointer, floatvars.find(tok2->varId()) != floatvars.end()))
continue; continue;
ret |= simplifyKnownVariablesSimplify(&tok2, tok3, varid, structname, value, valueVarId, valueIsPointer, pointeralias, indentlevel); ret |= simplifyKnownVariablesSimplify(&tok2, tok3, varid, structname, value, valueVarId, valueIsPointer, valueToken, indentlevel);
} }
} }
@ -6411,8 +6411,10 @@ bool Tokenizer::simplifyKnownVariablesGetData(unsigned int varid, Token **_tok2,
return true; return true;
} }
bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsigned int varid, const std::string &structname, std::string &value, unsigned int valueVarId, bool valueIsPointer, bool pointeralias, int indentlevel) bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsigned int varid, const std::string &structname, std::string &value, unsigned int valueVarId, bool valueIsPointer, const Token * const valueToken, int indentlevel)
{ {
const bool pointeralias(valueToken->isName() || Token::Match(valueToken, "& %var% ["));
bool ret = false; bool ret = false;
Token* bailOutFromLoop = 0; Token* bailOutFromLoop = 0;
@ -6591,6 +6593,11 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
tok3->deleteNext(); tok3->deleteNext();
tok3->deleteNext(); tok3->deleteNext();
} }
if (Token::Match(valueToken, "& %var% ;"))
{
tok3->insertToken("&");
tok3 = tok3->next();
}
tok3 = tok3->next(); tok3 = tok3->next();
tok3->str(value); tok3->str(value);
tok3->varId(valueVarId); tok3->varId(valueVarId);

View File

@ -320,7 +320,7 @@ public:
* utility function for simplifyKnownVariables. Perform simplification * utility function for simplifyKnownVariables. Perform simplification
* of a given variable * of a given variable
*/ */
bool simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsigned int varid, const std::string &structname, std::string &value, unsigned int valueVarId, bool valueIsPointer, bool pointeralias, int indentlevel); bool simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsigned int varid, const std::string &structname, std::string &value, unsigned int valueVarId, bool valueIsPointer, const Token * const valueToken, int indentlevel);
/** Replace a "goto" with the statements */ /** Replace a "goto" with the statements */
void simplifyGoto(); void simplifyGoto();

View File

@ -5799,11 +5799,7 @@ private:
"}\n"; "}\n";
const char expected[] = "int f ( ) " const char expected[] = "int f ( ) "
"{" "{"
" int i ;" " ; return 5 ; "
" int * p ;"
" p = & i ;"
" i = 5 ;"
" return 5 ; "
"}"; "}";
ASSERT_EQUALS(expected, tok(code)); ASSERT_EQUALS(expected, tok(code));
} }

View File

@ -127,6 +127,7 @@ private:
TEST_CASE(simplifyKnownVariables38); // ticket #2399 - simplify conditions TEST_CASE(simplifyKnownVariables38); // ticket #2399 - simplify conditions
TEST_CASE(simplifyKnownVariables39); TEST_CASE(simplifyKnownVariables39);
TEST_CASE(simplifyKnownVariables40); TEST_CASE(simplifyKnownVariables40);
TEST_CASE(simplifyKnownVariables41); // p=&x; if (p) ..
TEST_CASE(simplifyKnownVariablesBailOutAssign); TEST_CASE(simplifyKnownVariablesBailOutAssign);
TEST_CASE(simplifyKnownVariablesBailOutFor1); TEST_CASE(simplifyKnownVariablesBailOutFor1);
TEST_CASE(simplifyKnownVariablesBailOutFor2); TEST_CASE(simplifyKnownVariablesBailOutFor2);
@ -2020,6 +2021,16 @@ private:
ASSERT_EQUALS("void f ( ) {\n;\nchar c2 ; c2 = { 'a' } ;\n}", tokenizeAndStringify(code, true)); ASSERT_EQUALS("void f ( ) {\n;\nchar c2 ; c2 = { 'a' } ;\n}", tokenizeAndStringify(code, true));
} }
void simplifyKnownVariables41()
{
const char code[] = "void f() {\n"
" int x = 0;\n"
" const int *p; p = &x;\n"
" if (p) { return 0; }\n"
"}";
ASSERT_EQUALS("void f ( ) {\nint x ; x = 0 ;\nconst int * p ; p = & x ;\nif ( & x ) { return 0 ; }\n}", tokenizeAndStringify(code, true));
}
void simplifyKnownVariablesBailOutAssign() void simplifyKnownVariablesBailOutAssign()
{ {
const char code[] = "int foo() {\n" const char code[] = "int foo() {\n"