Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions lib/templatesimplifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,14 @@ TemplateSimplifier::TokenAndName::TokenAndName(Token *tok, const std::string &s,
else if (!isAlias()) {
if (isFunction())
tok1 = tok1->link()->next();
tok1 = Token::findmatch(tok1, "{|;");
while (tok1 && !Token::Match(tok1, ";|{")) {
if (tok1->str() == "<")
tok1 = tok1->findClosingBracket();
else if (Token::Match(tok1, "(|[") && tok1->link())
tok1 = tok1->link();
if (tok1)
tok1 = tok1->next();
}
if (tok1)
isForwardDeclaration(tok1->str() == ";");
}
Expand Down Expand Up @@ -771,7 +778,15 @@ void TemplateSimplifier::getTemplateInstantiations()
// #7914
// Ignore template instantiations within template definitions: they will only be
// handled if the definition is actually instantiated
Token *tok2 = Token::findmatch(tok, "{|;");
Token * tok2 = tok->next();
while (tok2 && !Token::Match(tok2, ";|{")) {
if (tok2->str() == "<")
tok2 = tok2->findClosingBracket();
else if (Token::Match(tok2, "(|[") && tok2->link())
tok2 = tok2->link();
if (tok2)
tok2 = tok2->next();
}
if (tok2 && tok2->str() == "{")
tok = tok2->link();
else if (tok2 && tok2->str() == ";")
Expand Down
50 changes: 39 additions & 11 deletions test/testsimplifytemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class TestSimplifyTemplate : public TestFixture {
TEST_CASE(template113);
TEST_CASE(template114); // #9155
TEST_CASE(template115); // #9153
TEST_CASE(template116); // #9178
TEST_CASE(template_specialization_1); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
TEST_CASE(template_specialization_2); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
TEST_CASE(template_enum); // #6299 Syntax error in complex enum declaration (including template)
Expand Down Expand Up @@ -1845,15 +1846,34 @@ class TestSimplifyTemplate : public TestFixture {
}

void template84() { // #8880
const char code[] = "template <class b, int c, class>\n"
"auto d() -> typename a<decltype(b{})>::e {\n"
" d<int, c, int>();\n"
"}";
const char exp[] = "auto d<int,c,int> ( ) . a < int > :: e ; "
"auto d<int,c,int> ( ) . a < int > :: e { "
"d<int,c,int> ( ) ; "
"}";
ASSERT_EQUALS(exp, tok(code));
{
const char code[] = "template <class b, int c, class>\n"
"auto d() -> typename a<decltype(b{})>::e {\n"
" d<int, c, int>();\n"
"}";
const char exp[] = "template < class b , int c , class > "
"auto d ( ) . a < decltype ( b { } ) > :: e { "
"d < int , c , int > ( ) ; "
"}";
ASSERT_EQUALS(exp, tok(code));
}
{
const char code[] = "template <class b, int c, class>\n"
"auto d() -> typename a<decltype(b{})>::e {\n"
" d<int, c, int>();\n"
"}"
"void foo() { d<char, 1, int>(); }";
const char exp[] = "auto d<char,1,int> ( ) . a < char > :: e ; "
"auto d<int,1,int> ( ) . a < int > :: e ; "
"void foo ( ) { d<char,1,int> ( ) ; } "
"auto d<char,1,int> ( ) . a < char > :: e { "
"d<int,1,int> ( ) ; "
"} "
"auto d<int,1,int> ( ) . a < int > :: e { "
"d<int,1,int> ( ) ; "
"}";
ASSERT_EQUALS(exp, tok(code));
}
}

void template85() { // #8902 - crash
Expand Down Expand Up @@ -2704,8 +2724,8 @@ class TestSimplifyTemplate : public TestFixture {
"template <typename a> using e = typename c<std::is_final<a>{}, d<a>>::f;\n";
const char exp[] = "template < typename a , a > struct b ; "
"template < bool , typename > struct c ; "
"struct d<a> ; "
"template < typename a > using e = typename c < std :: is_final < a > { } , d<a> > :: f ; struct d<a> : b < bool , std :: is_empty < a > { } > { } ;";
"template < typename a > struct d : b < bool , std :: is_empty < a > { } > { } ; "
"template < typename a > using e = typename c < std :: is_final < a > { } , d < a > > :: f ;";
ASSERT_EQUALS(exp, tok(code));
}
}
Expand Down Expand Up @@ -2735,6 +2755,14 @@ class TestSimplifyTemplate : public TestFixture {
ASSERT_EQUALS(exp, tok(code));
}

void template116() { // #9178
const char code[] = "template <class, class a> auto b() -> decltype(a{}.template b<void(int, int)>);\n"
"template <class, class a> auto b() -> decltype(a{}.template b<void(int, int)>){}";
const char exp[] = "template < class , class a > auto b ( ) . decltype ( a { } . template b < void ( int , int ) > ) ; "
"template < class , class a > auto b ( ) . decltype ( a { } . template b < void ( int , int ) > ) { }";
ASSERT_EQUALS(exp, tok(code));
}

void template_specialization_1() { // #7868 - template specialization template <typename T> struct S<C<T>> {..};
const char code[] = "template <typename T> struct C {};\n"
"template <typename T> struct S {a};\n"
Expand Down