From 0d7474042a5ad71478ff49b413c980efa75b0663 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Tue, 18 May 2010 07:11:23 +0200 Subject: [PATCH] Fixed #1685 (segmentation fault of cppcheck) --- lib/tokenize.cpp | 25 ++++++++++++++++++------- test/testsimplifytokens.cpp | 16 ++++++++++++++-- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 541bc7f0e..6e3464f7d 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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; diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 3ee4cc7fc..b4385816a 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -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]"));