From 4503fbf6610381c8fa8ee7743846a094bae08b25 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 4 Apr 2024 16:02:10 +0200 Subject: [PATCH] Fix #12588 FP unusedFunction with forward-declared enum (#6232) --- lib/tokenize.cpp | 4 ++-- test/testsimplifytokens.cpp | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index f34ef22d5b8..4dcd5b6fad5 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -8873,9 +8873,9 @@ void Tokenizer::simplifyStructDecl() const Token * const type = tok->next(); Token *next = tok->tokAt(2); - while (next && next->str() != "{") + while (next && !Token::Match(next, "[{;]")) next = next->next(); - if (!next) + if (!next || next->str() == ";") continue; Token* after = next->link(); if (!after) diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 2a7a85874d7..3a14888eef4 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -89,6 +89,7 @@ class TestSimplifyTokens : public TestFixture { TEST_CASE(simplifyStructDecl6); // ticket #3732 TEST_CASE(simplifyStructDecl7); // ticket #476 (static anonymous struct array) TEST_CASE(simplifyStructDecl8); // ticket #7698 + TEST_CASE(simplifyStructDecl9); // register int var; => int var; // inline int foo() {} => int foo() {} @@ -1249,6 +1250,28 @@ class TestSimplifyTokens : public TestFixture { ASSERT_EQUALS("enum class J : short { x , y , z } ; enum J j ; j = x ;", tok("enum class J : short { x, y, z } j{x};")); } + void simplifyStructDecl9() { + const char* code = "enum E : int;\n" // #12588 + "void f() {}\n" + "namespace {\n" + " struct S {\n" + " explicit S(int i) : m(i) {}\n" + " int m;\n" + " };\n" + "}\n" + "void g() { S s(0); }\n"; + const char* exp = "enum E : int ; " + "void f ( ) { } " + "namespace { " + "struct S { " + "explicit S ( int i ) : m ( i ) { } " + "int m ; " + "} ; " + "} " + "void g ( ) { S s ( 0 ) ; }"; + ASSERT_EQUALS(exp, tok(code)); + } + void removeUnwantedKeywords() { ASSERT_EQUALS("int var ;", tok("register int var ;")); ASSERT_EQUALS("short var ;", tok("register short int var ;"));