From cc2c13902bdb393a8dac6ceff1e1eb57896a01de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Tue, 29 Sep 2026 14:09:43 +0200 Subject: [PATCH 1/4] test --- test/testsimplifyusing.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/testsimplifyusing.cpp b/test/testsimplifyusing.cpp index e3c714803cb..555be0dc939 100644 --- a/test/testsimplifyusing.cpp +++ b/test/testsimplifyusing.cpp @@ -79,6 +79,10 @@ class TestSimplifyUsing : public TestFixture { TEST_CASE(simplifyUsing39); TEST_CASE(simplifyUsing40); TEST_CASE(simplifyUsing41); + TEST_CASE(simplifyUsing42); + TEST_CASE(simplifyUsing43); + TEST_CASE(simplifyUsing44); + TEST_CASE(simplifyUsing45); TEST_CASE(simplifyUsing8970); TEST_CASE(simplifyUsing8971); @@ -956,6 +960,34 @@ class TestSimplifyUsing : public TestFixture { ASSERT_EQUALS(expected, tok(code)); } + void simplifyUsing42() { + const char code[] = "using A = bool;\n" + "template A func() {}\n"; + const char expected[] = "template < typename T > bool func ( ) { }"; + ASSERT_EQUALS(expected, tok(code)); + } + + void simplifyUsing43() { + const char code[] = "using A = bool;\n" + "template A func() {}\n"; + const char expected[] = "template < typename A > A func ( ) { }"; + ASSERT_EQUALS(expected, tok(code)); + } + + void simplifyUsing44() { + const char code[] = "using A = bool;\n" + "template class C { A x; }\n"; + const char expected[] = "template < typename T > class C { bool x ; }"; + ASSERT_EQUALS(expected, tok(code)); + } + + void simplifyUsing45() { + const char code[] = "using A = bool;\n" + "template class C { A x; }\n"; + const char expected[] = "template < typename A > class C { A x ; }"; + ASSERT_EQUALS(expected, tok(code)); + } + void simplifyUsing8970() { const char code[] = "using V = std::vector;\n" "struct A {\n" From f805e9e773ec28ae77232c99d37d33ac6559ac8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Tue, 29 Sep 2026 14:01:36 +0200 Subject: [PATCH 2/4] fix --- lib/tokenize.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index d67356ad086..4cff68fc509 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3201,8 +3201,15 @@ bool Tokenizer::simplifyUsing() continue; } - // skip template definitions if (Token::Match(tok1, "template < !!>")) { + Token *paramsEnd = tok1->next()->findClosingBracket(); + bool shadowed = !paramsEnd; + for (const Token *param = tok1->next(); !shadowed && param != paramsEnd; param = param->next()) + shadowed = param->str() == nameToken->str(); + if (!shadowed) { + tok1 = paramsEnd; + continue; + } Token *declEndToken = TemplateSimplifier::findTemplateDeclarationEnd(tok1); if (declEndToken) tok1 = declEndToken; From eec21f9b0d5d4a3c190842c9b43f6d93f0801be8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Tue, 29 Sep 2026 15:07:44 +0200 Subject: [PATCH 3/4] combine test cases --- test/testsimplifyusing.cpp | 52 ++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/test/testsimplifyusing.cpp b/test/testsimplifyusing.cpp index 555be0dc939..a6acdf2a26d 100644 --- a/test/testsimplifyusing.cpp +++ b/test/testsimplifyusing.cpp @@ -80,9 +80,6 @@ class TestSimplifyUsing : public TestFixture { TEST_CASE(simplifyUsing40); TEST_CASE(simplifyUsing41); TEST_CASE(simplifyUsing42); - TEST_CASE(simplifyUsing43); - TEST_CASE(simplifyUsing44); - TEST_CASE(simplifyUsing45); TEST_CASE(simplifyUsing8970); TEST_CASE(simplifyUsing8971); @@ -961,31 +958,30 @@ class TestSimplifyUsing : public TestFixture { } void simplifyUsing42() { - const char code[] = "using A = bool;\n" - "template A func() {}\n"; - const char expected[] = "template < typename T > bool func ( ) { }"; - ASSERT_EQUALS(expected, tok(code)); - } - - void simplifyUsing43() { - const char code[] = "using A = bool;\n" - "template A func() {}\n"; - const char expected[] = "template < typename A > A func ( ) { }"; - ASSERT_EQUALS(expected, tok(code)); - } - - void simplifyUsing44() { - const char code[] = "using A = bool;\n" - "template class C { A x; }\n"; - const char expected[] = "template < typename T > class C { bool x ; }"; - ASSERT_EQUALS(expected, tok(code)); - } - - void simplifyUsing45() { - const char code[] = "using A = bool;\n" - "template class C { A x; }\n"; - const char expected[] = "template < typename A > class C { A x ; }"; - ASSERT_EQUALS(expected, tok(code)); + { + const char code[] = "using A = bool;\n" + "template A func() {}\n"; + const char expected[] = "template < typename T > bool func ( ) { }"; + ASSERT_EQUALS(expected, tok(code)); + } + { + const char code[] = "using A = bool;\n" + "template A func() {}\n"; + const char expected[] = "template < typename A > A func ( ) { }"; + ASSERT_EQUALS(expected, tok(code)); + } + { + const char code[] = "using A = bool;\n" + "template class C { A x; }\n"; + const char expected[] = "template < typename T > class C { bool x ; }"; + ASSERT_EQUALS(expected, tok(code)); + } + { + const char code[] = "using A = bool;\n" + "template class C { A x; }\n"; + const char expected[] = "template < typename A > class C { A x ; }"; + ASSERT_EQUALS(expected, tok(code)); + } } void simplifyUsing8970() { From 316e2bf798a3c22284c476076dead1783c9ab3b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Tue, 29 Sep 2026 15:10:36 +0200 Subject: [PATCH 4/4] additional test case --- test/testsimplifyusing.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/testsimplifyusing.cpp b/test/testsimplifyusing.cpp index a6acdf2a26d..239407f5c2f 100644 --- a/test/testsimplifyusing.cpp +++ b/test/testsimplifyusing.cpp @@ -982,6 +982,13 @@ class TestSimplifyUsing : public TestFixture { const char expected[] = "template < typename A > class C { A x ; }"; ASSERT_EQUALS(expected, tok(code)); } + { + const char code[] = "using A = bool;\n" + "template class C { using A = T; A x; }\n" + "C c;\n"; + const char expected[] = "class C ; C c ; class C { int x ; }"; + ASSERT_EQUALS(expected, tok(code)); + } } void simplifyUsing8970() {