diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 352363f67..eb527394b 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2780,8 +2780,21 @@ void Tokenizer::simplifyTemplates() { tok->insertToken(","); tok = tok->next(); - tok->insertToken((*it)->strAt(1)); - tok = tok->next(); + const Token *from = (*it)->next(); + std::stack links; + while (from && (!links.empty() || (from->str() != "," && from->str() != ">"))) + { + tok->insertToken(from->str()); + tok = tok->next(); + if (Token::Match(tok, "(|[")) + links.push(tok); + else if (!links.empty() && Token::Match(tok, ")|]")) + { + Token::createMutualLinks(links.top(), tok); + links.pop(); + } + from = from->next(); + } ++it; } } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index ae11bd0ed..e07d202b0 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -264,6 +264,7 @@ private: TEST_CASE(removeattribute); TEST_CASE(cpp0xtemplate1); TEST_CASE(cpp0xtemplate2); + TEST_CASE(cpp0xtemplate3); TEST_CASE(cpp0xdefault); TEST_CASE(arraySize); @@ -4634,6 +4635,18 @@ private: "list < list < int >> ints ;", tokenizeAndStringify(code)); } + void cpp0xtemplate3() + { + // #2549 + const char *code = "template\n" + "struct S\n" + "{};\n" + "S s;\n"; + TODO_ASSERT_EQUALS(";\n\n\nS < int , ( int ) 0 > s ;", // wanted result + ";\n\n\nS < int , ( T ) 0 > s ;", // current result + tokenizeAndStringify(code)); + } + void cpp0xdefault() { {