Skip to content

Commit a61f21d

Browse files
jokvadanmar
authored andcommitted
Accept nested templates in tokenizer-simplify (cppcheck-opensource#1070)
The following snippet triggerd the error: template<typename DerivedT> template<typename T> auto ComposableParserImpl<DerivedT>::operator|( T const &other ) const -> Parser { return Parser() | static_cast<DerivedT const &>( *this ) | other; } Whenever simplifyFunctionParameters was called on a templated class' templated member function (and probably any nested template), the tokenizer would recognise it as a syntax error, assuming that return type *must* come after a template<> token.
1 parent d47b772 commit a61f21d

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

lib/tokenize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8420,7 +8420,7 @@ const Token * Tokenizer::findGarbageCode() const
84208420
for (const Token *tok = tokens(); tok; tok = tok->next()) {
84218421
if (!Token::simpleMatch(tok, "template <"))
84228422
continue;
8423-
if (tok->previous() && !Token::Match(tok->previous(), "[:;{})]"))
8423+
if (tok->previous() && !Token::Match(tok->previous(), "[:;{})>]"))
84248424
return tok;
84258425
const Token * const tok1 = tok;
84268426
tok = tok->next()->findClosingBracket();

test/testtokenize.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ class TestTokenizer : public TestFixture {
220220
TEST_CASE(simplifyFunctionParameters1); // #3721
221221
TEST_CASE(simplifyFunctionParameters2); // #4430
222222
TEST_CASE(simplifyFunctionParameters3); // #4436
223+
TEST_CASE(simplifyFunctionParametersMultiTemplate);
223224
TEST_CASE(simplifyFunctionParametersErrors);
224225

225226
TEST_CASE(removeParentheses1); // Ticket #61
@@ -3116,6 +3117,12 @@ class TestTokenizer : public TestFixture {
31163117
ASSERT_EQUALS(code, tokenizeAndStringify(code));
31173118
}
31183119

3120+
void simplifyFunctionParametersMultiTemplate() {
3121+
const char code[] = "template < typename T1 > template < typename T2 > "
3122+
"void A < T1 > :: foo ( T2 ) { }";
3123+
ASSERT_EQUALS(code, tokenizeAndStringify(code));
3124+
}
3125+
31193126
void simplifyFunctionParametersErrors() {
31203127
//same parameters...
31213128
ASSERT_THROW(tokenizeAndStringify("void foo(x, x)\n"

0 commit comments

Comments
 (0)