From 5c1a2db4342c5589eb6151c02a53577725997821 Mon Sep 17 00:00:00 2001 From: Ken-Patrick Lehrmann Date: Tue, 16 Jun 2020 19:05:55 +0200 Subject: [PATCH] Fix handling of c++ casts in template expansion Cast were not expanded properly: the `<` was not taken into account in typeindentlevel, so we would then miss a `>`, resulting in syntaxError. --- lib/templatesimplifier.cpp | 3 +++ test/testsimplifytemplate.cpp | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 51bfd36ad..a92c9a130 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -1986,6 +1986,9 @@ void TemplateSimplifier::expandTemplate( } else if (typeindentlevel > 0 && typetok->str() == ">" && brackets1.top()->str() == "<") { --typeindentlevel; brackets1.pop(); + } else if (Token::Match(typetok, "const_cast|dynamic_cast|reinterpret_cast|static_cast <")) { + brackets1.push(typetok->next()); + ++typeindentlevel; } else if (typetok->str() == "(") ++typeindentlevel; else if (typetok->str() == ")") diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index 77c268df6..eb520527c 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -256,6 +256,8 @@ private: TEST_CASE(template_variable_4); TEST_CASE(simplifyDecltype); + + TEST_CASE(castInExpansion); } std::string tok(const char code[], bool debugwarnings = false, Settings::PlatformType type = Settings::Native) { @@ -5129,6 +5131,21 @@ private: "class type { } ;"; ASSERT_EQUALS(expected, tok(code)); } + + void castInExpansion() { + const char code[] = "template class C { };\n" + "template class Base {};\n" + "template class Derived : private Base {};\n" + "typedef Derived(-1)> > C_;\n" + "class C3 { C_ c; };"; + const char expected[] = "template < int N > class C { } ; " + "class Base-1>> ; " + "class Derived-1>> ; " + "class C3 { Derived-1>> c ; } ; " + "class Derived-1>> : private Base-1>> { } ; " + "class Base-1>> { } ;"; + ASSERT_EQUALS(expected, tok(code)); + } }; REGISTER_TEST(TestSimplifyTemplate)