From a09c221014d8847e684aaed51d97d1f52df62732 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Thu, 11 Nov 2010 06:40:49 +0100 Subject: [PATCH] typedefs: handle __typeof__ --- lib/tokenize.cpp | 43 +++++++++++++++++++++++++++++++++++++ test/testsimplifytokens.cpp | 10 +++++++++ 2 files changed, 53 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index daa5a2391..988cd2b70 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -818,6 +818,7 @@ void Tokenizer::simplifyTypedef() bool ptrToArray = false; bool refToArray = false; bool ptrMember = false; + bool typeOf = false; Token *namespaceStart = 0; Token *namespaceEnd = 0; @@ -959,6 +960,18 @@ void Tokenizer::simplifyTypedef() continue; } } + + // typeof + else if (Token::simpleMatch(tok->tokAt(offset - 1), "__typeof__ (") && + Token::Match(tok->tokAt(offset)->link(), ") %type% ;")) + { + argStart = tok->tokAt(offset); + argEnd = tok->tokAt(offset)->link(); + typeName = tok->tokAt(offset)->link()->next(); + tok = typeName->next(); + typeOf = true; + } + else if (Token::Match(tok->tokAt(offset), "( *|&| const|volatile| const|volatile| %type% ) (")) { functionPtr = tok->tokAt(offset + 1)->str() == "*"; @@ -1592,6 +1605,36 @@ void Tokenizer::simplifyTypedef() tok2 = tok2->next(); Token::createMutualLinks(tok2, tok3); } + else if (typeOf) + { + tok2->insertToken("("); + tok2 = tok2->next(); + Token *tok3 = tok2; + Token *nextArgTok; + std::stack argLinks; + for (nextArgTok = argStart->next(); nextArgTok != argEnd; nextArgTok = nextArgTok->next()) + { + tok2->insertToken(nextArgTok->strAt(0)); + tok2 = tok2->next(); + + // Check for links and fix them up + if (tok2->str() == "(" || tok2->str() == "[") + argLinks.push(tok2); + if (tok2->str() == ")" || tok2->str() == "]") + { + Token * link = argLinks.top(); + + tok2->link(link); + link->link(tok2); + + argLinks.pop(); + } + } + tok2->insertToken(")"); + tok2 = tok2->next(); + Token::createMutualLinks(tok2, tok3); + + } else if (tok2->tokAt(2) && tok2->tokAt(2)->str() == "[") { while (tok2->tokAt(2) && tok2->tokAt(2)->str() == "[") diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index a7a0f2c84..6ea0a4d89 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -219,6 +219,7 @@ private: TEST_CASE(simplifyTypedef61); // ticket #2074 and 2075 TEST_CASE(simplifyTypedef62); // ticket #2082 TEST_CASE(simplifyTypedef63); // ticket #2175 'typedef float x[3];' + TEST_CASE(simplifyTypedef64); TEST_CASE(simplifyTypedefFunction1); TEST_CASE(simplifyTypedefFunction2); // ticket #1685 @@ -4528,6 +4529,15 @@ private: ASSERT_EQUALS("", errout.str()); } + void simplifyTypedef64() + { + const char code[] = "typedef __typeof__(__type1() + __type2()) __type;" + "__type t;\n"; + const std::string actual(sizeof_(code)); + ASSERT_EQUALS("; __typeof__ ( __type1 ( ) + __type2 ( ) ) t ;", actual); + ASSERT_EQUALS("", errout.str()); + } + void simplifyTypedefFunction1() { {