Skip to content

Commit 6e10603

Browse files
committed
Ticket cppcheck-opensource#6023: Properly handle template'd default template parameter values.
1 parent efab840 commit 6e10603

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

lib/templatesimplifier.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,11 +600,16 @@ void TemplateSimplifier::useDefaultArgumentValues(const std::list<Token *> &temp
600600
for (std::size_t i = (templatepar - eq.size()); it != eq.end() && i < usedpar; ++i)
601601
++it;
602602
while (it != eq.end()) {
603+
int indentlevel = 0;
603604
tok->insertToken(",");
604605
tok = tok->next();
605606
const Token *from = (*it)->next();
606607
std::stack<Token *> links;
607-
while (from && (!links.empty() || (from->str() != "," && from->str() != ">"))) {
608+
while (from && (!links.empty() || (from->str() != "," && (indentlevel || from->str() != ">")))) {
609+
if (from->str() == "<")
610+
++indentlevel;
611+
else if (from->str() == ">")
612+
--indentlevel;
608613
tok->insertToken(from->str(), from->originalName());
609614
tok = tok->next();
610615
if (Token::Match(tok, "(|["))

test/testsimplifytokens.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ class TestSimplifyTokens : public TestFixture {
139139
TEST_CASE(template44); // #5297 - TemplateSimplifier::simplifyCalculations not eager enough
140140
TEST_CASE(template45); // #5814 - syntax error reported for valid code
141141
TEST_CASE(template46); // #5816 - syntax error reported for valid code
142+
TEST_CASE(template47); // #6023 - syntax error reported for valid code
142143
TEST_CASE(template_unhandled);
143144
TEST_CASE(template_default_parameter);
144145
TEST_CASE(template_default_type);
@@ -2424,6 +2425,12 @@ class TestSimplifyTokens : public TestFixture {
24242425
ASSERT_EQUALS("", errout.str());
24252426
}
24262427

2428+
void template47() { // #6023
2429+
tok("template <typename T1, typename T2 = T3<T1> > class C1 {}; "
2430+
"class C2 : public C1<C2> {};");
2431+
ASSERT_EQUALS("", errout.str());
2432+
}
2433+
24272434
void template_default_parameter() {
24282435
{
24292436
const char code[] = "template <class T, int n=3>\n"

0 commit comments

Comments
 (0)