diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 77379b73f..19ac61473 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2134,7 +2134,7 @@ void Tokenizer::simplifyTemplates() { if (tok->str() == ">") { - if (Token::Match(tok, "> class %var%")) + if (Token::Match(tok, "> class|struct %var%")) classname = tok->strAt(2); break; } @@ -2213,7 +2213,7 @@ void Tokenizer::simplifyTemplates() // get the position of the template name unsigned int namepos = 0; - if (Token::Match(tok, "> class %type% {|:")) + if (Token::Match(tok, "> class|struct %type% {|:")) namepos = 2; else if (Token::Match(tok, "> %type% *|&| %type% (")) namepos = 2; diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 8a683aeb4..6788fedf4 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -104,6 +104,7 @@ private: TEST_CASE(template18); TEST_CASE(template19); TEST_CASE(template20); + TEST_CASE(template21); TEST_CASE(template_unhandled); TEST_CASE(template_default_parameter); TEST_CASE(template_default_type); @@ -1776,6 +1777,69 @@ private: ASSERT_EQUALS(expected, sizeof_(code)); } + void template21() + { + { + const char code[] = "template struct Fred { T a; };\n" + "Fred fred;"; + + const std::string expected("; ; " + "Fred fred ; " + "struct Fred { int a ; }"); + + ASSERT_EQUALS(expected, sizeof_(code)); + } + + { + const char code[] = "template struct Fred { T data[sz]; };\n" + "Fred fred;"; + + const std::string expected("; ; " + "Fred fred ; " + "struct Fred { float data [ 4 ] ; }"); + + ASSERT_EQUALS(expected, sizeof_(code)); + } + + { + const char code[] = "template struct Fred { Fred(); };\n" + "Fred fred;"; + + const std::string expected("; ; " + "Fred fred ; " + "struct Fred { Fred ( ) ; }"); + + ASSERT_EQUALS(expected, sizeof_(code)); + } + + { + const char code[] = "template struct Fred { };\n" + "template Fred::Fred() { }\n" + "Fred fred;"; + + const std::string expected("; ; " + "; " + "Fred fred ; " + "struct Fred { } " + "Fred :: Fred ( ) { }"); + + ASSERT_EQUALS(expected, sizeof_(code)); + } + + { + const char code[] = "template struct Fred { };\n" + "Fred fred1;\n" + "Fred fred2;"; + + const std::string expected("; ;" + " Fred fred1 ;" + " Fred fred2 ;" + " struct Fred { }"); + + ASSERT_EQUALS(expected, sizeof_(code)); + } + } + void template_unhandled() { // An unhandled template usage should be simplified..