From 963108d9578cd65b45ad546b573a1d828b668cf1 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Tue, 11 May 2010 21:41:33 +0200 Subject: [PATCH] Fixed #1671 (simplifyTypedef: support for more typedefs) --- lib/tokenize.cpp | 32 ++++++++++++++++++++++++++++++++ test/testsimplifytokens.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 52fca414a..f77f631a4 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -732,6 +732,7 @@ void Tokenizer::simplifyTypedef() bool function = false; bool functionPtr = false; bool functionRef = false; + Token *functionNamespace = 0; if (Token::Match(tok->next(), "::") || Token::Match(tok->next(), "%type%")) @@ -794,6 +795,9 @@ void Tokenizer::simplifyTypedef() return; } + while (Token::Match(typeEnd->next(), "const|volatile")) + typeEnd = typeEnd->next(); + tok = typeEnd; offset = 1; } @@ -878,6 +882,27 @@ void Tokenizer::simplifyTypedef() continue; } } + else if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "( %var% :: *|&| %type% ) (")) + { + functionNamespace = tok->tokAt(offset + 1); + functionPtr = tok->tokAt(offset + 3)->str() == "*"; + functionRef = tok->tokAt(offset + 3)->str() == "&"; + function = tok->tokAt(offset + 4)->str() == ")"; + if (!function) + offset++; + if (tok->tokAt(offset + 5)->link()->next()) + { + typeName = tok->tokAt(offset + 3); + argStart = tok->tokAt(offset + 5); + argEnd = tok->tokAt(offset + 5)->link(); + tok = argEnd->next(); + } + else + { + // internal error + continue; + } + } else if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "( %type% (")) { function = true; @@ -1051,6 +1076,13 @@ void Tokenizer::simplifyTypedef() tok2->insertToken("("); tok2 = tok2->next(); Token *tok3 = tok2; + if (functionNamespace) + { + tok2->insertToken(functionNamespace->str()); + tok2 = tok2->next(); + tok2->insertToken("::"); + tok2 = tok2->next(); + } if (functionPtr) tok2->insertToken("*"); else if (functionRef) diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 327249ccd..75a77dfd5 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -185,6 +185,7 @@ private: TEST_CASE(simplifyTypedef44); TEST_CASE(simplifyTypedef45); // ticket #1613 TEST_CASE(simplifyTypedef46); // ticket #1615 + TEST_CASE(simplifyTypedef47); TEST_CASE(simplifyTypedefFunction); @@ -3911,6 +3912,29 @@ private: ASSERT_EQUALS("", errout.str()); } + void simplifyTypedef47() + { + { + const char code[] = "typedef std::pair const I;\n" + "I i;"; + + // The expected result.. + const std::string expected("; " + "std :: pair < int , int > const i ;"); + ASSERT_EQUALS(expected, sizeof_(code)); + } + + { + const char code[] = "typedef void (X:: *F)();\n" + "F f;"; + + // The expected result.. + const std::string expected("; " + "void ( X :: * f ) ( ) ;"); + ASSERT_EQUALS(expected, sizeof_(code)); + } + } + void simplifyTypedefFunction() { {