Skip to content

Commit 246576f

Browse files
IOBYTEdanmar
authored andcommitted
Fixed #9178 (Assertion `brackets1.empty() == false' failed on valid C++ code) (#1905)
This fixes a nasty intrinsics related bug causing instantiations that shouldn't happen.
1 parent e979b06 commit 246576f

2 files changed

Lines changed: 56 additions & 13 deletions

File tree

‎lib/templatesimplifier.cpp‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,14 @@ TemplateSimplifier::TokenAndName::TokenAndName(Token *tok, const std::string &s,
109109
else if (!isAlias()) {
110110
if (isFunction())
111111
tok1 = tok1->link()->next();
112-
tok1 = Token::findmatch(tok1, "{|;");
112+
while (tok1 && !Token::Match(tok1, ";|{")) {
113+
if (tok1->str() == "<")
114+
tok1 = tok1->findClosingBracket();
115+
else if (Token::Match(tok1, "(|[") && tok1->link())
116+
tok1 = tok1->link();
117+
if (tok1)
118+
tok1 = tok1->next();
119+
}
113120
if (tok1)
114121
isForwardDeclaration(tok1->str() == ";");
115122
}
@@ -771,7 +778,15 @@ void TemplateSimplifier::getTemplateInstantiations()
771778
// #7914
772779
// Ignore template instantiations within template definitions: they will only be
773780
// handled if the definition is actually instantiated
774-
Token *tok2 = Token::findmatch(tok, "{|;");
781+
Token * tok2 = tok->next();
782+
while (tok2 && !Token::Match(tok2, ";|{")) {
783+
if (tok2->str() == "<")
784+
tok2 = tok2->findClosingBracket();
785+
else if (Token::Match(tok2, "(|[") && tok2->link())
786+
tok2 = tok2->link();
787+
if (tok2)
788+
tok2 = tok2->next();
789+
}
775790
if (tok2 && tok2->str() == "{")
776791
tok = tok2->link();
777792
else if (tok2 && tok2->str() == ";")

‎test/testsimplifytemplate.cpp‎

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ class TestSimplifyTemplate : public TestFixture {
153153
TEST_CASE(template113);
154154
TEST_CASE(template114); // #9155
155155
TEST_CASE(template115); // #9153
156+
TEST_CASE(template116); // #9178
156157
TEST_CASE(template_specialization_1); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
157158
TEST_CASE(template_specialization_2); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
158159
TEST_CASE(template_enum); // #6299 Syntax error in complex enum declaration (including template)
@@ -1845,15 +1846,34 @@ class TestSimplifyTemplate : public TestFixture {
18451846
}
18461847

18471848
void template84() { // #8880
1848-
const char code[] = "template <class b, int c, class>\n"
1849-
"auto d() -> typename a<decltype(b{})>::e {\n"
1850-
" d<int, c, int>();\n"
1851-
"}";
1852-
const char exp[] = "auto d<int,c,int> ( ) . a < int > :: e ; "
1853-
"auto d<int,c,int> ( ) . a < int > :: e { "
1854-
"d<int,c,int> ( ) ; "
1855-
"}";
1856-
ASSERT_EQUALS(exp, tok(code));
1849+
{
1850+
const char code[] = "template <class b, int c, class>\n"
1851+
"auto d() -> typename a<decltype(b{})>::e {\n"
1852+
" d<int, c, int>();\n"
1853+
"}";
1854+
const char exp[] = "template < class b , int c , class > "
1855+
"auto d ( ) . a < decltype ( b { } ) > :: e { "
1856+
"d < int , c , int > ( ) ; "
1857+
"}";
1858+
ASSERT_EQUALS(exp, tok(code));
1859+
}
1860+
{
1861+
const char code[] = "template <class b, int c, class>\n"
1862+
"auto d() -> typename a<decltype(b{})>::e {\n"
1863+
" d<int, c, int>();\n"
1864+
"}"
1865+
"void foo() { d<char, 1, int>(); }";
1866+
const char exp[] = "auto d<char,1,int> ( ) . a < char > :: e ; "
1867+
"auto d<int,1,int> ( ) . a < int > :: e ; "
1868+
"void foo ( ) { d<char,1,int> ( ) ; } "
1869+
"auto d<char,1,int> ( ) . a < char > :: e { "
1870+
"d<int,1,int> ( ) ; "
1871+
"} "
1872+
"auto d<int,1,int> ( ) . a < int > :: e { "
1873+
"d<int,1,int> ( ) ; "
1874+
"}";
1875+
ASSERT_EQUALS(exp, tok(code));
1876+
}
18571877
}
18581878

18591879
void template85() { // #8902 - crash
@@ -2704,8 +2724,8 @@ class TestSimplifyTemplate : public TestFixture {
27042724
"template <typename a> using e = typename c<std::is_final<a>{}, d<a>>::f;\n";
27052725
const char exp[] = "template < typename a , a > struct b ; "
27062726
"template < bool , typename > struct c ; "
2707-
"struct d<a> ; "
2708-
"template < typename a > using e = typename c < std :: is_final < a > { } , d<a> > :: f ; struct d<a> : b < bool , std :: is_empty < a > { } > { } ;";
2727+
"template < typename a > struct d : b < bool , std :: is_empty < a > { } > { } ; "
2728+
"template < typename a > using e = typename c < std :: is_final < a > { } , d < a > > :: f ;";
27092729
ASSERT_EQUALS(exp, tok(code));
27102730
}
27112731
}
@@ -2735,6 +2755,14 @@ class TestSimplifyTemplate : public TestFixture {
27352755
ASSERT_EQUALS(exp, tok(code));
27362756
}
27372757

2758+
void template116() { // #9178
2759+
const char code[] = "template <class, class a> auto b() -> decltype(a{}.template b<void(int, int)>);\n"
2760+
"template <class, class a> auto b() -> decltype(a{}.template b<void(int, int)>){}";
2761+
const char exp[] = "template < class , class a > auto b ( ) . decltype ( a { } . template b < void ( int , int ) > ) ; "
2762+
"template < class , class a > auto b ( ) . decltype ( a { } . template b < void ( int , int ) > ) { }";
2763+
ASSERT_EQUALS(exp, tok(code));
2764+
}
2765+
27382766
void template_specialization_1() { // #7868 - template specialization template <typename T> struct S<C<T>> {..};
27392767
const char code[] = "template <typename T> struct C {};\n"
27402768
"template <typename T> struct S {a};\n"

0 commit comments

Comments
 (0)