From 7da9ff74d7bfa736ce0bd5addfa76d6bda891785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Tue, 2 Sep 2025 19:21:59 +0200 Subject: [PATCH 1/5] add test --- test/testtokenize.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index af34625ca18..d0ba5db5600 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -218,6 +218,7 @@ class TestTokenizer : public TestFixture { TEST_CASE(vardecl29); // #9282 TEST_CASE(vardecl30); TEST_CASE(vardecl31); // function pointer init + TEST_CASE(vardecl32); TEST_CASE(vardecl_stl_1); TEST_CASE(vardecl_stl_2); TEST_CASE(vardecl_stl_3); @@ -2766,6 +2767,13 @@ class TestTokenizer : public TestFixture { } } + void vardecl32() { + { + const char code[] = "static enum { E } f() { return E; }"; + ASSERT_EQUALS("enum Anonymous0 { E } ; static enum Anonymous0 f ( ) { return E ; }", tokenizeAndStringify(code)); + } + } + void volatile_variables() { { const char code[] = "volatile int a=0;\n" From 4af3a041ad871a6090e0ad7aa4ef5fe3d991a335 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Tue, 2 Sep 2025 19:02:37 +0200 Subject: [PATCH 2/5] fix #14106 --- lib/tokenize.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index ea911b1acbe..675b496b1f2 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -9209,7 +9209,8 @@ void Tokenizer::simplifyStructDecl() } // check for initialization - if (Token::Match(after, "%any% (|{")) { + bool isFuncDecl = Token::Match(after, "%name% (") && Token::simpleMatch(after->linkAt(1), ") {"); + if (Token::Match(after, "%any% (|{") && !isFuncDecl) { after->insertToken("="); after = after->next(); const bool isEnum = start->str() == "enum"; From 03bd6719f9f334cd76175ca98cf1baeb4aaccf3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Fri, 5 Sep 2025 10:41:01 +0200 Subject: [PATCH 3/5] add tests involving pointers --- test/testtokenize.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index d0ba5db5600..c0c3eea724b 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -219,6 +219,8 @@ class TestTokenizer : public TestFixture { TEST_CASE(vardecl30); TEST_CASE(vardecl31); // function pointer init TEST_CASE(vardecl32); + TEST_CASE(vardecl33); + TEST_CASE(vardecl34); TEST_CASE(vardecl_stl_1); TEST_CASE(vardecl_stl_2); TEST_CASE(vardecl_stl_3); @@ -2771,6 +2773,18 @@ class TestTokenizer : public TestFixture { { const char code[] = "static enum { E } f() { return E; }"; ASSERT_EQUALS("enum Anonymous0 { E } ; static enum Anonymous0 f ( ) { return E ; }", tokenizeAndStringify(code)); + + void vardecl33() { + { + const char code[] = "static enum { E } *f() { return NULL; }"; + ASSERT_EQUALS("enum Anonymous0 { E } ; static enum Anonymous0 * f ( ) { return NULL ; }", tokenizeAndStringify(code, true, Platform::Type::Native, false)); + } + } + + void vardecl34() { + { + const char code[] = "static enum { E } const *f() { return NULL; }"; + ASSERT_EQUALS("enum Anonymous0 { E } ; static enum Anonymous0 const * f ( ) { return NULL ; }", tokenizeAndStringify(code, true, Platform::Type::Native, false)); } } From aeea64153e7810b1431059f3d666652c25cbc607 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Fri, 5 Sep 2025 10:41:25 +0200 Subject: [PATCH 4/5] enforce c language in test --- test/testtokenize.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index c0c3eea724b..97073f0cee8 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -2772,7 +2772,9 @@ class TestTokenizer : public TestFixture { void vardecl32() { { const char code[] = "static enum { E } f() { return E; }"; - ASSERT_EQUALS("enum Anonymous0 { E } ; static enum Anonymous0 f ( ) { return E ; }", tokenizeAndStringify(code)); + ASSERT_EQUALS("enum Anonymous0 { E } ; static enum Anonymous0 f ( ) { return E ; }", tokenizeAndStringify(code, true, Platform::Type::Native, false)); + } + } void vardecl33() { { From 14fc7cacaa54c5937d4a9a5737879cc67bf518fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Fri, 5 Sep 2025 12:40:44 +0200 Subject: [PATCH 5/5] handle pointers in isAnonymousEnum --- lib/tokenize.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 675b496b1f2..e73fb34673c 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -9108,7 +9108,7 @@ static bool isAnonymousEnum(const Token* tok) while (Token::Match(end, "%name%|::")) end = end->next(); } - return end && Token::Match(end->link(), "} (| %type%| )| [,;[({=]"); + return end && Token::Match(end->link(), "} (| %type%| )| [*,;[({=]"); } void Tokenizer::simplifyStructDecl()