diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index aee8a5555..6e442039f 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -708,7 +708,7 @@ bool TemplateSimplifier::getTemplateDeclarations() if (!Token::simpleMatch(tok, "template <")) continue; // ignore template template parameter - if (tok->strAt(-1) == "<") + if (tok->strAt(-1) == "<" || tok->strAt(-1) == ",") continue; // ignore nested template if (tok->strAt(-1) == ">") @@ -1606,7 +1606,7 @@ void TemplateSimplifier::expandTemplate( } } unsigned int typeindentlevel = 0; - while (!(typeindentlevel == 0 && Token::Match(end, ";|{|:"))) { + while (end && !(typeindentlevel == 0 && Token::Match(end, ";|{|:"))) { if (Token::Match(end, "<|(|{")) ++typeindentlevel; else if (Token::Match(end, ">|)|}")) diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index 110cb3765..7b0dbbbb2 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -188,6 +188,7 @@ private: TEST_CASE(template148); // syntax error TEST_CASE(template149); // unknown macro TEST_CASE(template150); // syntax error + TEST_CASE(template151); // crash TEST_CASE(template_specialization_1); // #7868 - template specialization template struct S> {..}; TEST_CASE(template_specialization_2); // #7868 - template specialization template struct S> {..}; TEST_CASE(template_enum); // #6299 Syntax error in complex enum declaration (including template) @@ -3571,6 +3572,53 @@ private: ASSERT_EQUALS(exp, tok(code)); } + void template151() { // crash + { + const char code[] = "class SimulationComponentGroupGenerator {\n" + " std::list> build() const;\n" + "};\n" + "template <\n" + " class obj_type,\n" + " template class allocator = std::allocator,\n" + " template class data_container = std::list>\n" + "class GenericConfigurationHandler {\n" + " data_container> m_target_configurations;\n" + "};\n" + "class TargetConfigurationHandler : public GenericConfigurationHandler { };"; + const char exp[] = "class SimulationComponentGroupGenerator { " + "std :: list < int , std :: allocator < int > > build ( ) const ; " + "} ; " + "class GenericConfigurationHandler ; " + "class TargetConfigurationHandler : public GenericConfigurationHandler { } ; " + "class GenericConfigurationHandler { " + "std :: list < int , std :: std :: allocator < int > > m_target_configurations ; " + "} ;"; + ASSERT_EQUALS(exp, tok(code)); + } + { + const char code[] = "std::list> a;\n" + "template class allocator = std::allocator> class b {};\n" + "class c : b {};"; + const char exp[] = "std :: list < std :: allocator < int > > a ; " + "class b ; " + "class c : b { } ; " + "class b { } ;"; + ASSERT_EQUALS(exp, tok(code)); + } + { + const char code[] = "template class a {};\n" + "template class a;\n" + "template class a = a> class b {};\n" + "class c : b {};"; + const char exp[] = "class a ; " + "class b ; " + "class c : b { } ; " + "class b { } ; " + "class a { } ;"; + ASSERT_EQUALS(exp, tok(code)); + } + } + void template_specialization_1() { // #7868 - template specialization template struct S> {..}; const char code[] = "template struct C {};\n" "template struct S {a};\n"