Added TODO test case for the simplifyKnownVariables to better simplify local variables

This commit is contained in:
Daniel Marjamäki 2011-06-23 17:03:14 +02:00
parent 2d1b7285d2
commit 40a14736b7
2 changed files with 24 additions and 8 deletions

View File

@ -6873,6 +6873,8 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
// Stop if unknown function call is seen
// If the variable is a global or a member variable it might be
// changed by the function call
// TODO: don't bail out if the variable is a local variable,
// then it can't be changed by the function call.
if (tok3->str() == ")" && tok3->link() &&
Token::Match(tok3->link()->tokAt(-2), "[;{}] %var% (") &&
!Token::Match(tok3->link()->previous(), "if|for|while|switch"))

View File

@ -2272,15 +2272,29 @@ private:
void simplifyKnownVariablesClassMember()
{
// Ticket #2815
const char code[] = "char *a;\n"
"void f(const char *s) {\n"
" a = NULL;\n"
" x();\n"
" memcpy(a, s, 10);\n" // <- don't simplify "a" here
"}\n";
{
const char code[] = "char *a;\n"
"void f(const char *s) {\n"
" a = NULL;\n"
" x();\n"
" memcpy(a, s, 10);\n" // <- don't simplify "a" here
"}\n";
const std::string s(tokenizeAndStringify(code, true));
ASSERT_EQUALS(true, s.find("memcpy ( a , s , 10 ) ;") != std::string::npos);
const std::string s(tokenizeAndStringify(code, true));
ASSERT_EQUALS(true, s.find("memcpy ( a , s , 10 ) ;") != std::string::npos);
}
// If the variable is local then perform simplification..
{
const char code[] = "void f(const char *s) {\n"
" char *a = NULL;\n"
" x();\n"
" memcpy(a, s, 10);\n" // <- simplify "a"
"}\n";
const std::string s(tokenizeAndStringify(code, true));
TODO_ASSERT_EQUALS(true, false, s.find("memcpy ( 0 , s , 10 ) ;") != std::string::npos);
}
}