From 7c3ae68e4d4657ed923c4541e94a455a9fd06914 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 19 Oct 2023 10:51:16 +0200 Subject: [PATCH] Fix #11893 FP constStatement unused variable (#5567) --- lib/templatesimplifier.cpp | 8 ++++---- test/testsimplifytemplate.cpp | 31 ++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index b18db08c905..9a07623f51f 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -2246,13 +2246,13 @@ void TemplateSimplifier::expandTemplate( assert(brackets.empty() == false); assert(brackets.top()->str() == "{"); Token::createMutualLinks(brackets.top(), mTokenList.back()); - if (tok3->strAt(1) == ";") { - const Token * tokSemicolon = tok3->next(); - mTokenList.addtoken(tokSemicolon, tokSemicolon->linenr(), tokSemicolon->column(), tokSemicolon->fileIndex()); - } brackets.pop(); if (brackets.empty() && !Token::Match(tok3, "} >|,|{")) { inTemplateDefinition = false; + if (isClass && tok3->strAt(1) == ";") { + const Token* tokSemicolon = tok3->next(); + mTokenList.addtoken(tokSemicolon, tokSemicolon->linenr(), tokSemicolon->column(), tokSemicolon->fileIndex()); + } break; } } else if (tok3->str() == ")") { diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index 752d5d85ec7..aa4db7d84aa 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -216,6 +216,7 @@ class TestSimplifyTemplate : public TestFixture { TEST_CASE(template175); // #10908 TEST_CASE(template176); // #11146 TEST_CASE(template177); + TEST_CASE(template178); TEST_CASE(template_specialization_1); // #7868 - template specialization template struct S> {..}; TEST_CASE(template_specialization_2); // #7868 - template specialization template struct S> {..}; TEST_CASE(template_enum); // #6299 Syntax error in complex enum declaration (including template) @@ -4518,6 +4519,34 @@ class TestSimplifyTemplate : public TestFixture { ASSERT_EQUALS(exp, tok(code)); } + void template178() { + const char code[] = "template\n" // #11893 + "void g() {\n" + " for (T i = 0; i < T{ 3 }; ++i) {}\n" + "}\n" + "void f() {\n" + " g();\n" + "}"; + const char exp[] = "void g ( ) ; void f ( ) { g ( ) ; } void g ( ) { for ( int i = 0 ; i < int { 3 } ; ++ i ) { } }"; + ASSERT_EQUALS(exp, tok(code)); + + const char code2[] = "template\n" + "struct S {\n" + " T c;\n" + " template\n" + " void g() {\n" + " for (U i = 0; i < U{ 3 }; ++i) {}\n" + " }\n" + "};\n" + "void f() {\n" + " S s{};\n" + " s.g();\n" + "}"; + const char exp2[] = "struct S ; void f ( ) { S s { } ; s . g ( ) ; } struct S { char c ; void g ( ) ; } ; " + "void S :: g ( ) { for ( int i = 0 ; i < int { 3 } ; ++ i ) { } }"; + ASSERT_EQUALS(exp2, tok(code2)); + } + void template_specialization_1() { // #7868 - template specialization template struct S> {..}; const char code[] = "template struct C {};\n" "template struct S {a};\n" @@ -5229,7 +5258,7 @@ class TestSimplifyTemplate : public TestFixture { void template_array_type() { ASSERT_EQUALS("void foo ( int [ ] x ) ; " "void bar ( ) { int [ 3 ] y ; foo ( y ) ; } " - "void foo ( int [ ] x ) { } ;", + "void foo ( int [ ] x ) { }", tok("template void foo(T x) {};\n" "void bar() {\n" " int[3] y;\n"