Fixed #4115 (Incorrect uninitialized variable error message with realloc macro)

This commit is contained in:
Daniel Marjamäki 2012-09-22 18:41:33 +02:00
parent 37695d44f3
commit 2e3a7db4eb
3 changed files with 44 additions and 0 deletions

View File

@ -1979,6 +1979,9 @@ bool Tokenizer::tokenize(std::istream &code,
// f(x=g()) => x=g(); f(x)
simplifyAssignmentInFunctionCall();
// x = ({ 123; }); => { x = 123; }
simplifyAssignmentBlock();
simplifyVariableMultipleAssign();
// Remove redundant parentheses
@ -8560,6 +8563,36 @@ void Tokenizer::simplifyAssignmentInFunctionCall()
}
}
void Tokenizer::simplifyAssignmentBlock()
{
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "[;{}] %var% = ( {")) {
// goto the "} )"
unsigned int indentlevel = 0;
Token *tok2 = tok;
while (NULL != (tok2 = tok2->next())) {
if (tok2->str() == "(" || tok2->str() == "{")
++indentlevel;
else if (tok2->str() == ")" || tok2->str() == "}") {
if (indentlevel <= 2)
break;
--indentlevel;
}
}
if (indentlevel == 2 && Token::simpleMatch(tok2, "} )")) {
tok2 = tok2->tokAt(-3);
if (Token::Match(tok2, "[;{}] %any% ;") && (tok2->next()->isName() || tok2->next()->isNumber())) {
tok2->insertToken("=");
tok2->insertToken(tok->next()->str());
tok2->next()->varId(tok->next()->varId());
tok->deleteNext(3);
tok2->tokAt(5)->deleteNext();
}
}
}
}
}
// Remove __asm..
void Tokenizer::simplifyAsm()
{

View File

@ -159,6 +159,9 @@ public:
*/
void simplifyAssignmentInFunctionCall();
/** Simplify assignment where rhs is a block : "x=({123;});" => "{x=123;}" */
void simplifyAssignmentBlock();
/**
* Simplify constant calculations such as "1+2" => "3"
* @return true if modifications to token-list are done.

View File

@ -418,6 +418,9 @@ private:
// "x += .." => "x = x + .."
TEST_CASE(simplifyCompoundAssignment);
// x = ({ 123; }); => { x = 123; }
TEST_CASE(simplifyAssignmentBlock);
// Tokenize C#
TEST_CASE(cs);
@ -6708,6 +6711,11 @@ private:
ASSERT_EQUALS("; x = g ( ) ; f ( x ) ;", tokenizeAndStringify(";f(x=g());"));
}
void simplifyAssignmentBlock() {
ASSERT_EQUALS("; { x = 123 ; } ;", tokenizeAndStringify(";x=({123;});"));
ASSERT_EQUALS("; { x = y ; } ;", tokenizeAndStringify(";x=({y;});"));
}
void cs() {
bool simplify = false;
bool expand = true;