Fixed #1685 (segmentation fault of cppcheck)

This commit is contained in:
Robert Reif 2010-05-18 07:11:23 +02:00 committed by Daniel Marjamäki
parent c52d7fbb22
commit 0d7474042a
2 changed files with 32 additions and 9 deletions

View File

@ -1073,8 +1073,16 @@ void Tokenizer::simplifyTypedef()
if (functionPtr || functionRef || function)
{
tok2->insertToken("(");
tok2 = tok2->next();
// don't add parenthesis around function names because it
// confuses other simplifications
bool needParen = true;
if (function && tok2->next()->str() != "*")
needParen = false;
if (needParen)
{
tok2->insertToken("(");
tok2 = tok2->next();
}
Token *tok3 = tok2;
if (functionNamespace)
{
@ -1091,7 +1099,7 @@ void Tokenizer::simplifyTypedef()
if (!inCast)
{
if (tok2->next()->str() != ")" && tok2->next()->str() != ",")
if (tok2->next() && tok2->next()->str() != ")" && tok2->next()->str() != ",")
{
if (Token::Match(tok2->next(), "( * %type% ) ("))
tok2 = tok2->tokAt(5)->link();
@ -1099,7 +1107,7 @@ void Tokenizer::simplifyTypedef()
{
if (tok2->next()->str() == "(")
tok2 = tok2->next()->link();
else if (!Token::Match(tok2->next(), "[|>"))
else if (!Token::Match(tok2->next(), "[|>|;"))
{
tok2 = tok2->next();
@ -1119,9 +1127,12 @@ void Tokenizer::simplifyTypedef()
}
}
tok2->insertToken(")");
tok2 = tok2->next();
Token::createMutualLinks(tok2, tok3);
if (needParen)
{
tok2->insertToken(")");
tok2 = tok2->next();
Token::createMutualLinks(tok2, tok3);
}
tok2->insertToken("(");
tok2 = tok2->next();
tok3 = tok2;

View File

@ -192,7 +192,8 @@ private:
TEST_CASE(simplifyTypedef47);
TEST_CASE(simplifyTypedef48); // ticket #1673
TEST_CASE(simplifyTypedefFunction);
TEST_CASE(simplifyTypedefFunction1);
TEST_CASE(simplifyTypedefFunction2); // ticket #1685
TEST_CASE(reverseArraySyntax)
TEST_CASE(simplify_numeric_condition)
@ -3988,7 +3989,7 @@ private:
ASSERT_EQUALS(expected, sizeof_(code));
}
void simplifyTypedefFunction()
void simplifyTypedefFunction1()
{
{
const char code[] = "typedef void (my_func)(arg_class*);\n"
@ -4021,6 +4022,17 @@ private:
}
}
void simplifyTypedefFunction2() // ticket #1685
{
const char code[] = "typedef void voidfn (int);\n"
"voidfn xxx;";
// The expected result..
const std::string expected("; "
"void xxx ( int ) ;");
ASSERT_EQUALS(expected, sizeof_(code));
}
void reverseArraySyntax()
{
ASSERT_EQUALS("a [ 13 ]", tok("13[a]"));