From 99ff04f617b28f9894254efca3a4747213593361 Mon Sep 17 00:00:00 2001 From: Ken-Patrick Lehrmann Date: Sat, 6 Jun 2020 17:51:15 +0200 Subject: [PATCH] 9757: skip template parameters when computing scope (#2670) The template parameter is confusing simplifyUsing: it does not compute properly the scope, and we end up replace "type" in "to_string" with "void", then later "void" is removed and we have an internal error. --- lib/tokenize.cpp | 8 +++++++- test/testsimplifyusing.cpp | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index dc770111e..deabefcd4 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1712,7 +1712,7 @@ namespace { return; while (tok->str() == "}" && !scopeInfo->empty() && tok == scopeInfo->back().bodyEnd) scopeInfo->pop_back(); - if (!Token::Match(tok, "namespace|class|struct|union %name% {|:|::")) { + if (!Token::Match(tok, "namespace|class|struct|union %name% {|:|::|<")) { // check for using namespace if (Token::Match(tok, "using namespace %name% ;|::")) { const Token * tok1 = tok->tokAt(2); @@ -1778,6 +1778,12 @@ namespace { while (tok && !Token::Match(tok, ";|{")) tok = tok->next(); } + // skip template parameters + if (tok && tok->str() == "<") { + tok = tok->findClosingBracket(); + if (tok) + tok = tok->next(); + } if (tok && tok->str() == "{") { scopeInfo->emplace_back(classname,tok->link()); } diff --git a/test/testsimplifyusing.cpp b/test/testsimplifyusing.cpp index b7988a9e3..8d71d43b7 100644 --- a/test/testsimplifyusing.cpp +++ b/test/testsimplifyusing.cpp @@ -69,6 +69,7 @@ private: TEST_CASE(simplifyUsing9385); TEST_CASE(simplifyUsing9388); TEST_CASE(simplifyUsing9518); + TEST_CASE(simplifyUsing9757); } std::string tok(const char code[], bool simplify = true, Settings::PlatformType type = Settings::Native, bool debugwarnings = true) { @@ -627,6 +628,21 @@ private: ASSERT_EQUALS(exp, tok(code, false)); } + void simplifyUsing9757() { + const char code[] = "enum class Type_t { Nil = 0 };\n" + "template class MappedType { };\n" + "template<> class MappedType { using type = void; };\n" + "std::string to_string (Example::Type_t type) {\n" + " switch (type) {}\n" + "}"; + const char exp[] = "enum class Type_t { Nil = 0 } ; " + "class MappedType ; " + "template < Type_t type > class MappedType { } ; " + "class MappedType { } ; " + "std :: string to_string ( Example :: Type_t type ) { " + "switch ( type ) { } }"; + ASSERT_EQUALS(exp, tok(code, false)); + } }; REGISTER_TEST(TestSimplifyUsing)