template: no usage -> no expansion (#292)

This commit is contained in:
Daniel Marjamäki 2009-05-08 16:19:22 +02:00
parent 80a0e183c5
commit dae530d8de
2 changed files with 45 additions and 1 deletions

View File

@ -470,6 +470,7 @@ bool Tokenizer::tokenize(std::istream &code, const char FileName[])
// Handle templates.. // Handle templates..
simplifyTemplates(); simplifyTemplates();
return true; return true;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -528,7 +529,7 @@ void Tokenizer::simplifyTemplates()
if (!tok) if (!tok)
break; break;
} }
else if (Token::Match(tok, "%var% <")) else if (Token::Match(tok->previous(), "[{};] %var% <"))
{ {
used.push_back(tok); used.push_back(tok);
} }

View File

@ -85,6 +85,7 @@ private:
TEST_CASE(template5); TEST_CASE(template5);
TEST_CASE(template6); TEST_CASE(template6);
TEST_CASE(template7); TEST_CASE(template7);
TEST_CASE(template8);
TEST_CASE(namespaces); TEST_CASE(namespaces);
@ -653,6 +654,48 @@ private:
} }
} }
// Template definitions but no usage => no expansion
void template8()
{
const char code[] = "template<typename T> class A;\n"
"template<typename T> class B;\n"
"\n"
"typedef A<int> x;\n"
"typedef B<int> y;\n"
"\n"
"template<typename T> class A {\n"
" void f() {\n"
" B<T> a = B<T>::g();\n"
" T b = 0;\n"
" if (b)\n"
" b = 0;\n"
" }\n"
"};\n"
"\n"
"template<typename T> inline B<T> h() { return B<T>(); }\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() void namespaces()
{ {
{ {