Skip to content

Commit 16e4dac

Browse files
Fix #13680 syntax error: template parameter static_cast<uint16_t>(0U) (danmar#7362)
1 parent 7967d5f commit 16e4dac

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

lib/templatesimplifier.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ void TemplateSimplifier::getTemplateInstantiations()
983983
// parse backwards and add template instantiations
984984
// TODO
985985
for (; tok2 && tok2 != tok; tok2 = tok2->previous()) {
986-
if (Token::Match(tok2, ",|< %name% <") &&
986+
if (Token::Match(tok2, ",|< %name% <") && !tok2->next()->isKeyword() &&
987987
(tok2->strAt(3) == ">" || templateParameters(tok2->tokAt(2)))) {
988988
addInstantiation(tok2->next(), tok->scopeInfo()->name);
989989
} else if (Token::Match(tok2->next(), "class|struct"))
@@ -2622,6 +2622,13 @@ void TemplateSimplifier::simplifyTemplateArgs(Token *start, const Token *end, st
26222622
}
26232623

26242624
for (Token *tok = first->next(); tok && tok != end; tok = tok->next()) {
2625+
if (tok->isKeyword() && endsWith(tok->str(), "_cast")) {
2626+
Token* tok2 = tok->next()->findClosingBracket();
2627+
if (!Token::simpleMatch(tok2, "> ("))
2628+
syntaxError(tok);
2629+
tok = tok2->linkAt(1);
2630+
continue;
2631+
}
26252632
if (Token::Match(tok, "( %num%|%bool% )") &&
26262633
(tok->previous() && !tok->previous()->isName())) {
26272634
tok->deleteThis();

test/testsimplifytemplate.cpp

+24-5
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ class TestSimplifyTemplate : public TestFixture {
216216
TEST_CASE(template177);
217217
TEST_CASE(template178);
218218
TEST_CASE(template179);
219+
TEST_CASE(template180);
219220
TEST_CASE(template_specialization_1); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
220221
TEST_CASE(template_specialization_2); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
221222
TEST_CASE(template_specialization_3);
@@ -4574,6 +4575,24 @@ class TestSimplifyTemplate : public TestFixture {
45744575
ASSERT_EQUALS(exp, tok(code));
45754576
}
45764577

4578+
void template180() {
4579+
const char code[] = "template<int C>\n" // #13680
4580+
"int f() {\n"
4581+
" return dostuff(C);\n"
4582+
"}\n"
4583+
"void g() {\n"
4584+
" f<static_cast<int>(0U)>();\n"
4585+
"}\n";
4586+
const char exp[] = "int f<static_cast<int>(0U)> ( ) ; "
4587+
"void g ( ) { "
4588+
"f<static_cast<int>(0U)> ( ) ; "
4589+
"} "
4590+
"int f<static_cast<int>(0U)> ( ) { "
4591+
"return dostuff ( static_cast < int > ( 0U ) ) ; "
4592+
"}";
4593+
ASSERT_EQUALS(exp, tok(code));
4594+
}
4595+
45774596
void template_specialization_1() { // #7868 - template specialization template <typename T> struct S<C<T>> {..};
45784597
const char code[] = "template <typename T> struct C {};\n"
45794598
"template <typename T> struct S {a};\n"
@@ -6433,11 +6452,11 @@ class TestSimplifyTemplate : public TestFixture {
64336452
"typedef Derived<C<static_cast<int>(-1)> > C_;\n"
64346453
"class C3 { C_ c; };";
64356454
const char expected[] = "template < int N > class C { } ; "
6436-
"class Base<C<static_cast<int>-1>> ; "
6437-
"class Derived<C<static_cast<int>-1>> ; "
6438-
"class C3 { Derived<C<static_cast<int>-1>> c ; } ; "
6439-
"class Derived<C<static_cast<int>-1>> : private Base<C<static_cast<int>-1>> { } ; "
6440-
"class Base<C<static_cast<int>-1>> { } ;";
6455+
"class Base<C<static_cast<int>(-1)>> ; "
6456+
"class Derived<C<static_cast<int>(-1)>> ; "
6457+
"class C3 { Derived<C<static_cast<int>(-1)>> c ; } ; "
6458+
"class Derived<C<static_cast<int>(-1)>> : private Base<C<static_cast<int>(-1)>> { } ; "
6459+
"class Base<C<static_cast<int>(-1)>> { } ;";
64416460
ASSERT_EQUALS(expected, tok(code));
64426461
}
64436462

0 commit comments

Comments
 (0)