From dae530d8defd0d6895a18a3810df12c3567b801e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 8 May 2009 16:19:22 +0200 Subject: [PATCH] template: no usage -> no expansion (#292) --- src/tokenize.cpp | 3 ++- test/testsimplifytokens.cpp | 43 +++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 757baeef2..d6f9c7c1d 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -470,6 +470,7 @@ bool Tokenizer::tokenize(std::istream &code, const char FileName[]) // Handle templates.. simplifyTemplates(); + return true; } //--------------------------------------------------------------------------- @@ -528,7 +529,7 @@ void Tokenizer::simplifyTemplates() if (!tok) break; } - else if (Token::Match(tok, "%var% <")) + else if (Token::Match(tok->previous(), "[{};] %var% <")) { used.push_back(tok); } diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 043547ab3..7138cc4d9 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -85,6 +85,7 @@ private: TEST_CASE(template5); TEST_CASE(template6); TEST_CASE(template7); + TEST_CASE(template8); TEST_CASE(namespaces); @@ -653,6 +654,48 @@ private: } } + // Template definitions but no usage => no expansion + void template8() + { + const char code[] = "template class A;\n" + "template class B;\n" + "\n" + "typedef A x;\n" + "typedef B y;\n" + "\n" + "template class A {\n" + " void f() {\n" + " B a = B::g();\n" + " T b = 0;\n" + " if (b)\n" + " b = 0;\n" + " }\n" + "};\n" + "\n" + "template inline B h() { return B(); }\n"; + + const std::string expected(" template < typename T > class A ;" + " template < typename T > class B ;" + "" + " typedef A < int > x ;" + " typedef B < int > y ;" + "" + " template < typename T > class A {" + " void f ( ) {" + " B < T > a = B < T > :: g ( ) ;" + " T b ; b = 0 ;" + " }" + " } ;" + "" + " template < typename T > inline B < T > h ( ) { return B < T > ( ) ; }"); + + ASSERT_EQUALS(expected, sizeof_(code)); + } + + + + + void namespaces() { {