Fixed #1445 (segfault in Tokenizer::simplifyRedundantParanthesis())

This commit is contained in:
Robert Reif 2010-02-25 07:24:32 +01:00 committed by Daniel Marjamäki
parent 01c5bdfc68
commit f9d444ed52
2 changed files with 42 additions and 9 deletions

View File

@ -5423,9 +5423,9 @@ void Tokenizer::simplifyEnum()
tok1 = tok1->next();
// Check for links and fix them up
if (tok1->str() == "(" || tok1->str() == "[")
if (tok1->str() == "(" || tok1->str() == "[" || tok1->str() == "{")
links.push(tok1);
else if (tok1->str() == ")" || tok1->str() == "]")
else if (tok1->str() == ")" || tok1->str() == "]" || tok1->str() == "}")
{
Token * link = links.top();
@ -5469,12 +5469,20 @@ void Tokenizer::simplifyEnum()
enumValueStart = tok1;
enumValueEnd = tok1;
int level = 0;
while (enumValueEnd->next() && (enumValueEnd->next()->str() != "}") &&
((enumValueEnd->next()->str() != ",") || level))
if (enumValueEnd->str() == "(" ||
enumValueEnd->str() == "[" ||
enumValueEnd->str() == "{")
level++;
while (enumValueEnd->next() &&
(!Token::Match(enumValueEnd->next(), "}|,") || level))
{
if (enumValueEnd->next()->str() == "(" || enumValueEnd->next()->str() == "[")
if (enumValueEnd->next()->str() == "(" ||
enumValueEnd->next()->str() == "[" ||
enumValueEnd->next()->str() == "{")
level++;
else if (enumValueEnd->next()->str() == ")" || enumValueEnd->next()->str() == "]")
else if (enumValueEnd->next()->str() == ")" ||
enumValueEnd->next()->str() == "]" ||
enumValueEnd->next()->str() == "}")
level--;
enumValueEnd = enumValueEnd->next();
@ -5547,7 +5555,7 @@ void Tokenizer::simplifyEnum()
{
std::stack<Token *> links;
tok2->str(enumValueStart->strAt(0));
if (tok2->str() == "(" || tok2->str() == "[")
if (tok2->str() == "(" || tok2->str() == "[" || tok2->str() == "{")
links.push(tok2);
Token * nextToken = enumValueStart->next();
for (; nextToken != enumValueEnd->next(); nextToken = nextToken->next())
@ -5556,9 +5564,9 @@ void Tokenizer::simplifyEnum()
tok2 = tok2->next();
// Check for links and fix them up
if (tok2->str() == "(" || tok2->str() == "[")
if (tok2->str() == "(" || tok2->str() == "[" || tok2->str() == "{")
links.push(tok2);
else if (tok2->str() == ")" || tok2->str() == "]")
else if (tok2->str() == ")" || tok2->str() == "]" || tok2->str() == "}")
{
Token * link = links.top();
@ -5582,6 +5590,16 @@ void Tokenizer::simplifyEnum()
}
}
// check for a variable definition: enum {} x;
if (end->next()->str() != ";")
{
Token * tok = end;
tok->insertToken(";");
tok = tok->next();
tok->insertToken("int");
}
if (enumType)
{
const std::string pattern(className.empty() ? "" : (className + " :: " + enumType->str()).c_str());

View File

@ -196,6 +196,7 @@ private:
TEST_CASE(enum7);
TEST_CASE(enum8);
TEST_CASE(enum9); // ticket 1404
TEST_CASE(enum10); // ticket 1445
// remove "std::" on some standard functions
TEST_CASE(removestd);
@ -3778,6 +3779,7 @@ private:
std::istringstream istr(code);
errout.str("");
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
}
void enum8()
@ -3838,6 +3840,19 @@ private:
ASSERT_EQUALS("", errout.str());
}
void enum10()
{
// ticket 1445
const char code[] = "enum {\n"
"SHELL_SIZE = sizeof(union { int i; char *cp; double d; }) - 1, \n"
"} e = SHELL_SIZE;";
const char expected[] = "; int e ; e = sizeof ( union { int i ; char * cp ; double d ; } ) - 1 ;";
ASSERT_EQUALS(expected, tok(code, false));
checkSimplifyEnum(code);
ASSERT_EQUALS("", errout.str());
}
void removestd()
{
ASSERT_EQUALS("; strcpy ( a , b ) ;", tok("; std::strcpy(a,b);"));