From 51beadd81c090ea3bd20ca7c15bf3b2fe17cb7e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 6 May 2009 21:03:11 +0200 Subject: [PATCH] templates: instantiations in a template class (#280) --- src/tokenize.cpp | 28 +++++++++++++++++++++++++++- test/testsimplifytokens.cpp | 15 +++++---------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 29793c52f..1a65688d0 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -483,8 +483,34 @@ void Tokenizer::simplifyTemplates() std::list used; for (Token *tok = _tokens; tok; tok = tok->next()) { - if (Token::Match(tok, "%var% <") && tok->str() != "template") + // template definition.. skip it + if (Token::simpleMatch(tok, "template <")) + { + unsigned int indentlevel = 0; + for (; tok; tok = tok->next()) + { + if (tok->str() == "{") + { + ++indentlevel; + } + else if (tok->str() == "}") + { + if (indentlevel <= 1) + break; + --indentlevel; + } + else if (indentlevel == 0 && tok->str() == ";") + { + break; + } + } + if (!tok) + break; + } + else if (Token::Match(tok, "%var% <")) + { used.push_back(tok); + } } if (used.empty()) return; diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 6de413d10..043547ab3 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -579,7 +579,7 @@ private: void template7() { - // Typedef + // A template class that is not used => no simplification { const char code[] = "template \n" "class ABC\n" @@ -587,21 +587,16 @@ private: "public:\n" " typedef ABC m;\n" "\n" - "};\n" - "\n" - "int main()\n" - "{}\n"; + "};\n"; const std::string expected(" template < class T > " "class ABC " "{ " "public: " - "typedef ABC m ; " - "} ; " - "int main ( )" - " { }"); + "typedef ABC < T > m ; " + "} ;"); - TODO_ASSERT_EQUALS(expected, sizeof_(code)); + ASSERT_EQUALS(expected, sizeof_(code)); } {