Skip to content

Commit 99ff04f

Browse files
authored
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.
1 parent 44ff22f commit 99ff04f

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

lib/tokenize.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1712,7 +1712,7 @@ namespace {
17121712
return;
17131713
while (tok->str() == "}" && !scopeInfo->empty() && tok == scopeInfo->back().bodyEnd)
17141714
scopeInfo->pop_back();
1715-
if (!Token::Match(tok, "namespace|class|struct|union %name% {|:|::")) {
1715+
if (!Token::Match(tok, "namespace|class|struct|union %name% {|:|::|<")) {
17161716
// check for using namespace
17171717
if (Token::Match(tok, "using namespace %name% ;|::")) {
17181718
const Token * tok1 = tok->tokAt(2);
@@ -1778,6 +1778,12 @@ namespace {
17781778
while (tok && !Token::Match(tok, ";|{"))
17791779
tok = tok->next();
17801780
}
1781+
// skip template parameters
1782+
if (tok && tok->str() == "<") {
1783+
tok = tok->findClosingBracket();
1784+
if (tok)
1785+
tok = tok->next();
1786+
}
17811787
if (tok && tok->str() == "{") {
17821788
scopeInfo->emplace_back(classname,tok->link());
17831789
}

test/testsimplifyusing.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class TestSimplifyUsing : public TestFixture {
6969
TEST_CASE(simplifyUsing9385);
7070
TEST_CASE(simplifyUsing9388);
7171
TEST_CASE(simplifyUsing9518);
72+
TEST_CASE(simplifyUsing9757);
7273
}
7374

7475
std::string tok(const char code[], bool simplify = true, Settings::PlatformType type = Settings::Native, bool debugwarnings = true) {
@@ -627,6 +628,21 @@ class TestSimplifyUsing : public TestFixture {
627628
ASSERT_EQUALS(exp, tok(code, false));
628629
}
629630

631+
void simplifyUsing9757() {
632+
const char code[] = "enum class Type_t { Nil = 0 };\n"
633+
"template<Type_t type> class MappedType { };\n"
634+
"template<> class MappedType<Type_t::Nil> { using type = void; };\n"
635+
"std::string to_string (Example::Type_t type) {\n"
636+
" switch (type) {}\n"
637+
"}";
638+
const char exp[] = "enum class Type_t { Nil = 0 } ; "
639+
"class MappedType<Type_t::Nil> ; "
640+
"template < Type_t type > class MappedType { } ; "
641+
"class MappedType<Type_t::Nil> { } ; "
642+
"std :: string to_string ( Example :: Type_t type ) { "
643+
"switch ( type ) { } }";
644+
ASSERT_EQUALS(exp, tok(code, false));
645+
}
630646
};
631647

632648
REGISTER_TEST(TestSimplifyUsing)

0 commit comments

Comments
 (0)