Pointer Aliasing: Handle pointer aliasing through the Tokenizer::simplifyKnownVariables

This commit is contained in:
Daniel Marjamäki 2009-10-17 18:21:13 +02:00
parent 2e9f7755ee
commit 2ec1b676ea
2 changed files with 50 additions and 1 deletions

View File

@ -3167,12 +3167,16 @@ bool Tokenizer::simplifyKnownVariables()
else if (tok2->previous()->str() != "*" &&
(Token::Match(tok2, "%var% = %num% ;") ||
Token::Match(tok2, "%var% = %bool% ;")))
Token::Match(tok2, "%var% = %str% ;") ||
Token::Match(tok2, "%var% = %bool% ;") ||
Token::Match(tok2, "%var% = %var% ;")))
{
unsigned int varid = tok2->varId();
if (varid == 0)
continue;
const bool pointeralias(tok2->tokAt(2)->isName());
std::string value(tok2->strAt(2));
Token* bailOutFromLoop = 0;
int indentlevel3 = indentlevel; // indentlevel for tok3
@ -3189,6 +3193,9 @@ bool Tokenizer::simplifyKnownVariables()
break;
}
if (pointeralias && Token::Match(tok3, ("!!= " + value).c_str()))
break;
// Stop if something like 'while (--var)' is found
if (tok3->str() == "while" || tok3->str() == "do")
{

View File

@ -127,6 +127,8 @@ private:
TEST_CASE(simplifyTypedef5)
TEST_CASE(reverseArraySyntax)
TEST_CASE(simplify_numeric_condition);
TEST_CASE(pointeralias);
}
std::string tok(const char code[], bool simplify = true)
@ -1978,6 +1980,46 @@ private:
ASSERT_EQUALS("void f ( ) { { ; } }", tok(code));
}
}
void pointeralias()
{
{
const char code[] = "void f()\n"
"{\n"
" char buf[100];\n"
" char *p = buf;\n"
" x(p);\n"
"}\n";
const char expected[] = "void f ( ) "
"{ "
"char buf [ 100 ] ; "
"char * p ; p = buf ; "
"x ( buf ) ; "
"}";
ASSERT_EQUALS(expected, tok(code));
}
{
const char code[] = "void f(char *p1)\n"
"{\n"
" char *p = p1;\n"
" p1 = 0;"
" x(p);\n"
"}\n";
const char expected[] = "void f ( char * p1 ) "
"{ "
"char * p ; p = p1 ; "
"p1 = 0 ; "
"x ( p ) ; "
"}";
ASSERT_EQUALS(expected, tok(code));
}
}
};
REGISTER_TEST(TestSimplifyTokens)