From 7b63da49646cfcb62bcfbe1e87d7fb7fe2fbad12 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Thu, 17 Mar 2011 20:00:49 -0400 Subject: [PATCH] fix #2651 (Segmentation fault (typedef)) --- lib/tokenize.cpp | 18 +++++++++++++++--- test/testsimplifytokens.cpp | 10 ++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 2c5f56d1e..a529e3b53 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1235,15 +1235,24 @@ void Tokenizer::simplifyTypedef() // function: typedef ... ( .... type )( ... ); // typedef ... (( .... type )( ... )); + // typedef ... ( * ( .... type )( ... )); else if ((tok->tokAt(offset)->str() == "(" && Token::Match(tok->tokAt(offset)->link()->previous(), "%type% ) (") && Token::Match(tok->tokAt(offset)->link()->next()->link(), ") const|volatile|;")) || (Token::simpleMatch(tok->tokAt(offset), "( (") && Token::Match(tok->tokAt(offset + 1)->link()->previous(), "%type% ) (") && - Token::Match(tok->tokAt(offset + 1)->link()->next()->link(), ") const|volatile| )"))) + Token::Match(tok->tokAt(offset + 1)->link()->next()->link(), ") const|volatile| ) ;|,")) || + (Token::simpleMatch(tok->tokAt(offset), "( * (") && + Token::Match(tok->tokAt(offset + 2)->link()->previous(), "%type% ) (") && + Token::Match(tok->tokAt(offset + 2)->link()->next()->link(), ") const|volatile| ) ;|,"))) { if (tok->strAt(offset + 1) == "(") offset++; + else if (Token::simpleMatch(tok->tokAt(offset), "( * (")) + { + pointers.push_back("*"); + offset += 2; + } if (tok->tokAt(offset)->link()->strAt(-2) == "*") functionPtr = true; @@ -1289,7 +1298,9 @@ void Tokenizer::simplifyTypedef() } // pointer to function returning pointer to function - else if (Token::Match(tok->tokAt(offset), "( * ( * %type% ) (")) + else if (Token::Match(tok->tokAt(offset), "( * ( * %type% ) (") && + Token::Match(tok->tokAt(offset + 6)->link(), ") ) (") && + Token::Match(tok->tokAt(offset + 6)->link()->tokAt(2)->link(), ") ;|,")) { functionPtrRetFuncPtr = true; @@ -1305,7 +1316,8 @@ void Tokenizer::simplifyTypedef() // function returning pointer to function else if (Token::Match(tok->tokAt(offset), "( * %type% (") && - Token::simpleMatch(tok->tokAt(offset + 3)->link(), ") ) (")) + Token::simpleMatch(tok->tokAt(offset + 3)->link(), ") ) (") && + Token::Match(tok->tokAt(offset + 3)->link()->tokAt(2)->link(), ") ;|,")) { functionRetFuncPtr = true; diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 20de55940..98b28ac53 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -250,6 +250,7 @@ private: TEST_CASE(simplifyTypedef84); // ticket #2630 TEST_CASE(simplifyTypedef85); // ticket #2651 TEST_CASE(simplifyTypedef86); // ticket #2581 + TEST_CASE(simplifyTypedef87); // ticket #2651 TEST_CASE(simplifyTypedefFunction1); TEST_CASE(simplifyTypedefFunction2); // ticket #1685 @@ -5060,6 +5061,15 @@ private: ASSERT_EQUALS("", errout.str()); } + void simplifyTypedef87() // ticket #2651 + { + const char code[] = "typedef FOO (*(*BAR)(void, int, const int, int*));\n"; + const char expected[] = ";"; + checkSimplifyTypedef(code); + ASSERT_EQUALS(expected, sizeof_(code)); + ASSERT_EQUALS("", errout.str()); + } + void simplifyTypedefFunction1() { {