From 1d4d407e805d6ae00aaef96abc303eea72c293c1 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 1 Oct 2024 10:57:36 +0200 Subject: [PATCH 01/11] Update testsimplifytypedef.cpp --- test/testsimplifytypedef.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index d0c7a21d336..d5bfda765c1 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -3353,8 +3353,8 @@ class TestSimplifyTypedef : public TestFixture { " struct S { enum E { E0 }; };\n" "}\n" "typedef N::S T;\n" - "enum class E { a = T::E0; };\n"; - ASSERT_EQUALS("namespace N { struct S { enum E { E0 } ; } ; } enum class E { a = N :: S :: E0 ; } ;", tok(code)); + "enum class E { a = T::E0 };\n"; + ASSERT_EQUALS("namespace N { struct S { enum E { E0 } ; } ; } enum class E { a = N :: S :: E0 } ;", tok(code)); } { // #11494 const char code[] = "typedef struct S {} KEY;\n" From 6e6114af6954243d06eb2d6e36aa3fa15708f8c8 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 1 Oct 2024 10:58:24 +0200 Subject: [PATCH 02/11] Update testsimplifytemplate.cpp --- test/testsimplifytemplate.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index 15458a516c8..e3f8e9d83c8 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -3585,7 +3585,7 @@ class TestSimplifyTemplate : public TestFixture { "using A2 = struct B2 { void f(T){} };\n" "A2 a2;\n" "template\n" - "using A3 = enum B3 {b = 0;};\n" + "using A3 = enum B3 {b = 0};\n" "A3 a3;"; const char exp[] = "template < int N > " "using A1 = struct B1 { static auto constexpr value = N ; } ; " @@ -3594,7 +3594,7 @@ class TestSimplifyTemplate : public TestFixture { "using A2 = struct B2 { void f ( T ) { } } ; " "A2 < bool > a2 ; " "template < class T > " - "using A3 = enum B3 { b = 0 ; } ; " + "using A3 = enum B3 { b = 0 } ; " "A3 < int > a3 ;"; ASSERT_EQUALS(exp, tok(code)); } From fc535f5491820ced0922b2d3e571959ebc5e81f0 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 1 Oct 2024 10:59:27 +0200 Subject: [PATCH 03/11] Update symboldatabase.h --- lib/symboldatabase.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/symboldatabase.h b/lib/symboldatabase.h index a594c84b2ac..bba22b78b3f 100644 --- a/lib/symboldatabase.h +++ b/lib/symboldatabase.h @@ -1409,6 +1409,9 @@ class CPPCHECKLIB SymbolDatabase { void clangSetVariables(const std::vector &variableList); void createSymbolDatabaseExprIds(); + /* returns the opening { if tok points to enum */ + static const Token* isEnumDefinition(const Token* tok); + private: friend class Scope; friend class Function; From bceb661e4e91b1d40835f1e5ea343c004865c88c Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 1 Oct 2024 11:00:55 +0200 Subject: [PATCH 04/11] Update symboldatabase.cpp --- lib/symboldatabase.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 2086af9c6a3..474c0bffceb 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -123,18 +123,18 @@ static bool isExecutableScope(const Token* tok) return false; } -static bool isEnumDefinition(const Token* tok) +const Token* SymbolDatabase::isEnumDefinition(const Token* tok) { - if (!Token::Match(tok, "enum class| %name% {|:")) - return false; + if (!Token::Match(tok, "enum class| %name%| {|:")) + return nullptr; while (!Token::Match(tok, "[{:]")) tok = tok->next(); if (tok->str() == "{") - return true; + return tok; tok = tok->next(); // skip ':' while (Token::Match(tok, "%name%|::")) tok = tok->next(); - return Token::simpleMatch(tok, "{"); + return Token::simpleMatch(tok, "{") ? tok : nullptr; } void SymbolDatabase::createSymbolDatabaseFindAllScopes() From 3c89685adefb00a72a7c8c184b2c0ba96732c9b4 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 1 Oct 2024 11:02:37 +0200 Subject: [PATCH 05/11] Update tokenize.cpp --- lib/tokenize.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 9102c750600..dc0ff10b401 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -8608,8 +8608,16 @@ void Tokenizer::findGarbageCode() const syntaxError(tok2, "Unexpected token '" + (tok2 ? tok2->str() : "") + "'"); } } - if (Token::Match(tok, "enum : %num%| {")) - syntaxError(tok->tokAt(2), "Unexpected token '" + tok->strAt(2) + "'"); + if (tok->str() == "enum") { + if (Token::Match(tok->next(), ": %num%| {")) + syntaxError(tok->tokAt(3), "Unexpected token '" + tok->strAt(3) + "'"); + if (const Token* start = SymbolDatabase::isEnumDefinition(tok)) { + for (const Token* tok2 = start->next(); tok2 && tok2 != start->link(); tok2 = tok2->next()) { + if (Token::Match(tok2, "[;{}]")) + syntaxError(tok2); + } + } + } } // Keywords in global scope From 134c6752f9e7360fc311027de7f2f5ee1e4d3eb8 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 1 Oct 2024 11:03:19 +0200 Subject: [PATCH 06/11] Add files via upload --- .../fuzz-crash/crash-66bd8440a699d270b323367978dab020dd1da3ff | 1 + 1 file changed, 1 insertion(+) create mode 100644 test/cli/fuzz-crash/crash-66bd8440a699d270b323367978dab020dd1da3ff diff --git a/test/cli/fuzz-crash/crash-66bd8440a699d270b323367978dab020dd1da3ff b/test/cli/fuzz-crash/crash-66bd8440a699d270b323367978dab020dd1da3ff new file mode 100644 index 00000000000..bdae8e1eded --- /dev/null +++ b/test/cli/fuzz-crash/crash-66bd8440a699d270b323367978dab020dd1da3ff @@ -0,0 +1 @@ +enum{C={;}=e}; \ No newline at end of file From 1e153f0bfe64c522fd1b16bc2fa3826dafc8e1cc Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 1 Oct 2024 11:16:08 +0200 Subject: [PATCH 07/11] Update testvarid.cpp --- test/testvarid.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/testvarid.cpp b/test/testvarid.cpp index 4e5c3984631..8182a6eded6 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -3838,11 +3838,11 @@ class TestVarID : public TestFixture { void varidenum1() { const char code[] = "const int eStart = 6;\n" "enum myEnum {\n" - " A = eStart;\n" + " A = eStart\n" "};\n"; const char expected[] = "1: const int eStart@1 = 6 ;\n" "2: enum myEnum {\n" - "3: A = eStart@1 ;\n" + "3: A = eStart@1\n" "4: } ;\n"; ASSERT_EQUALS(expected, tokenize(code)); } @@ -3850,11 +3850,11 @@ class TestVarID : public TestFixture { void varidenum2() { const char code[] = "const int eStart = 6;\n" "enum myEnum {\n" - " A = f(eStart);\n" + " A = f(eStart)\n" "};\n"; const char expected[] = "1: const int eStart@1 = 6 ;\n" "2: enum myEnum {\n" - "3: A = f ( eStart@1 ) ;\n" + "3: A = f ( eStart@1 )\n" "4: } ;\n"; ASSERT_EQUALS(expected, tokenize(code)); } @@ -3862,11 +3862,11 @@ class TestVarID : public TestFixture { void varidenum3() { const char code[] = "const int eStart = 6;\n" "enum myEnum {\n" - " A = f(eStart, x);\n" + " A = f(eStart, x)\n" "};\n"; const char expected[] = "1: const int eStart@1 = 6 ;\n" "2: enum myEnum {\n" - "3: A = f ( eStart@1 , x ) ;\n" + "3: A = f ( eStart@1 , x )\n" "4: } ;\n"; ASSERT_EQUALS(expected, tokenize(code)); } @@ -3874,11 +3874,11 @@ class TestVarID : public TestFixture { void varidenum4() { const char code[] = "const int eStart = 6;\n" "enum myEnum {\n" - " A = f(x, eStart);\n" + " A = f(x, eStart)\n" "};\n"; const char expected[] = "1: const int eStart@1 = 6 ;\n" "2: enum myEnum {\n" - "3: A = f ( x , eStart@1 ) ;\n" + "3: A = f ( x , eStart@1 )\n" "4: } ;\n"; ASSERT_EQUALS(expected, tokenize(code)); } @@ -3886,7 +3886,7 @@ class TestVarID : public TestFixture { void varidenum5() { const char code[] = "const int eStart = 6;\n" "enum myEnum {\n" - " A = f(x, eStart, y);\n" + " A = f(x, eStart, y)\n" "};\n"; const char expected[] = "1: const int eStart@1 = 6 ;\n" "2: enum myEnum {\n" @@ -3894,7 +3894,7 @@ class TestVarID : public TestFixture { "4: } ;\n"; const char current[] = "1: const int eStart@1 = 6 ;\n" "2: enum myEnum {\n" - "3: A = f ( x , eStart , y ) ;\n" + "3: A = f ( x , eStart , y )\n" "4: } ;\n"; TODO_ASSERT_EQUALS(expected, current, tokenize(code)); } From f95e51e297a66bfaf6ed50669f03c414c81828c7 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 1 Oct 2024 11:18:20 +0200 Subject: [PATCH 08/11] Update testvarid.cpp --- test/testvarid.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testvarid.cpp b/test/testvarid.cpp index 8182a6eded6..9a27621b2f9 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -3890,7 +3890,7 @@ class TestVarID : public TestFixture { "};\n"; const char expected[] = "1: const int eStart@1 = 6 ;\n" "2: enum myEnum {\n" - "3: A = f ( x , eStart@1 , y ) ;\n" + "3: A = f ( x , eStart@1 , y )\n" "4: } ;\n"; const char current[] = "1: const int eStart@1 = 6 ;\n" "2: enum myEnum {\n" From cbd2746fa581f9b41d89257d2fda73a4a9abf790 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 1 Oct 2024 11:20:59 +0200 Subject: [PATCH 09/11] Update tokenize.cpp --- lib/tokenize.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index dc0ff10b401..163e2a04a30 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -8610,7 +8610,7 @@ void Tokenizer::findGarbageCode() const } if (tok->str() == "enum") { if (Token::Match(tok->next(), ": %num%| {")) - syntaxError(tok->tokAt(3), "Unexpected token '" + tok->strAt(3) + "'"); + syntaxError(tok->tokAt(2), "Unexpected token '" + tok->strAt(2) + "'"); if (const Token* start = SymbolDatabase::isEnumDefinition(tok)) { for (const Token* tok2 = start->next(); tok2 && tok2 != start->link(); tok2 = tok2->next()) { if (Token::Match(tok2, "[;{}]")) From c7c56ebada2c1f5e24f2fa1d5d7265697d1746f2 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 1 Oct 2024 18:28:28 +0200 Subject: [PATCH 10/11] Update tokenize.cpp --- lib/tokenize.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 163e2a04a30..02d8f5318da 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -8613,7 +8613,7 @@ void Tokenizer::findGarbageCode() const syntaxError(tok->tokAt(2), "Unexpected token '" + tok->strAt(2) + "'"); if (const Token* start = SymbolDatabase::isEnumDefinition(tok)) { for (const Token* tok2 = start->next(); tok2 && tok2 != start->link(); tok2 = tok2->next()) { - if (Token::Match(tok2, "[;{}]")) + if (tok2->str() == ";") syntaxError(tok2); } } From d9a203040f498c41b98372f50d8c659c64399039 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 1 Oct 2024 22:34:21 +0200 Subject: [PATCH 11/11] Update testtokenize.cpp --- test/testtokenize.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 8c03bfd3136..e5f6fb22903 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -7132,6 +7132,7 @@ class TestTokenizer : public TestFixture { ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("enum : { };"), SYNTAX, "syntax error: Unexpected token '{'"); ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("enum : 3 { };"), SYNTAX, "syntax error: Unexpected token '3'"); + ASSERT_NO_THROW(tokenizeAndStringify("enum { E = int{} };")); ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("int a() { b((c)return 0) }"), SYNTAX, "syntax error"); ASSERT_THROW_EQUALS(tokenizeAndStringify("int f() { MACRO(x) return 0; }"),