From 62a34cf72f1bb3beec5bcdfdbece61786bb35a90 Mon Sep 17 00:00:00 2001 From: chrchr Date: Mon, 9 Jan 2023 16:11:53 +0100 Subject: [PATCH 1/4] No implicit int in C++ mode --- lib/tokenlist.cpp | 2 +- test/testtokenize.cpp | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 18ba26fa61b..9315d33da29 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -1957,7 +1957,7 @@ void TokenList::simplifyStdType() { for (Token *tok = front(); tok; tok = tok->next()) { - if (Token::Match(tok, "const|extern *|&|%name%") && (!tok->previous() || Token::Match(tok->previous(), "[;{}]"))) { + if (isC() && Token::Match(tok, "const|extern *|&|%name%") && (!tok->previous() || Token::Match(tok->previous(), "[;{}]"))) { if (Token::Match(tok->next(), "%name% !!;")) continue; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index ec6f324647a..d4c0961e023 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -223,6 +223,7 @@ class TestTokenizer : public TestFixture { TEST_CASE(vardecl27); // #7850 - crash on valid C code TEST_CASE(vardecl28); TEST_CASE(vardecl29); // #9282 + TEST_CASE(vardecl30); TEST_CASE(vardecl_stl_1); TEST_CASE(vardecl_stl_2); TEST_CASE(vardecl_stl_3); @@ -2518,6 +2519,15 @@ class TestTokenizer : public TestFixture { tokenizeAndStringify(code)); } + void vardecl30() { + const char code[] = "struct D {} const d;"; + ASSERT_EQUALS("struct D { } ; struct D const d ;", + tokenizeAndStringify(code, true, Settings::Native, "test.cpp")); + TODO_ASSERT_EQUALS("struct D { } ; struct D const d ;", + "struct D { } const int d ;", + tokenizeAndStringify(code, true, Settings::Native, "test.c")); + } + void volatile_variables() { { const char code[] = "volatile int a=0;\n" From e2e3593f9414fa81e5c8f6c30216bbd45dac23f9 Mon Sep 17 00:00:00 2001 From: chrchr Date: Mon, 9 Jan 2023 16:20:23 +0100 Subject: [PATCH 2/4] Fix test cases --- test/testtokenize.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index d4c0961e023..1e9a11223a6 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -2586,15 +2586,15 @@ class TestTokenizer : public TestFixture { } void implicitIntConst() { - ASSERT_EQUALS("const int x ;", tokenizeAndStringify("const x;")); - ASSERT_EQUALS("const int * x ;", tokenizeAndStringify("const *x;")); - ASSERT_EQUALS("const int * f ( ) ;", tokenizeAndStringify("const *f();")); + ASSERT_EQUALS("const int x ;", tokenizeAndStringify("const x;", true, Settings::Native, "test.c")); + ASSERT_EQUALS("const int * x ;", tokenizeAndStringify("const *x;", true, Settings::Native, "test.c")); + ASSERT_EQUALS("const int * f ( ) ;", tokenizeAndStringify("const *f();", true, Settings::Native, "test.c")); } void implicitIntExtern() { - ASSERT_EQUALS("extern int x ;", tokenizeAndStringify("extern x;")); - ASSERT_EQUALS("extern int * x ;", tokenizeAndStringify("extern *x;")); - ASSERT_EQUALS("const int * f ( ) ;", tokenizeAndStringify("const *f();")); + ASSERT_EQUALS("extern int x ;", tokenizeAndStringify("extern x;", true, Settings::Native, "test.c")); + ASSERT_EQUALS("extern int * x ;", tokenizeAndStringify("extern *x;", true, Settings::Native, "test.c")); + ASSERT_EQUALS("const int * f ( ) ;", tokenizeAndStringify("const *f();", true, Settings::Native, "test.c"); } /** From fbd9ed49fffc0742a99bc64197b72e262de71d86 Mon Sep 17 00:00:00 2001 From: chrchr Date: Mon, 9 Jan 2023 17:00:04 +0100 Subject: [PATCH 3/4] Missing ) --- test/testtokenize.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 1e9a11223a6..4c1cf600bc6 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -2594,7 +2594,7 @@ class TestTokenizer : public TestFixture { void implicitIntExtern() { ASSERT_EQUALS("extern int x ;", tokenizeAndStringify("extern x;", true, Settings::Native, "test.c")); ASSERT_EQUALS("extern int * x ;", tokenizeAndStringify("extern *x;", true, Settings::Native, "test.c")); - ASSERT_EQUALS("const int * f ( ) ;", tokenizeAndStringify("const *f();", true, Settings::Native, "test.c"); + ASSERT_EQUALS("const int * f ( ) ;", tokenizeAndStringify("const *f();", true, Settings::Native, "test.c")); } /** From 72c18941cdd5eef21dd275a98dd88e6bd66a96ed Mon Sep 17 00:00:00 2001 From: chrchr Date: Mon, 9 Jan 2023 21:03:23 +0100 Subject: [PATCH 4/4] Fix TODO --- lib/tokenlist.cpp | 14 ++++++++++++++ test/testtokenize.cpp | 5 ++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 9315d33da29..a74d2499bd5 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -1955,11 +1955,25 @@ void TokenList::simplifyPlatformTypes() void TokenList::simplifyStdType() { + auto isVarDeclC = [](const Token* tok) -> bool { + if (!Token::simpleMatch(tok, "}")) + return false; + tok = tok->link()->previous(); + while (Token::Match(tok, "%name%")) { + if (Token::Match(tok, "struct|union|enum")) + return true; + tok = tok->previous(); + } + return false; + }; + for (Token *tok = front(); tok; tok = tok->next()) { if (isC() && Token::Match(tok, "const|extern *|&|%name%") && (!tok->previous() || Token::Match(tok->previous(), "[;{}]"))) { if (Token::Match(tok->next(), "%name% !!;")) continue; + if (isVarDeclC(tok->previous())) + continue; tok->insertToken("int"); tok->next()->isImplicitInt(true); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 4c1cf600bc6..8fd61378cc8 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -2523,9 +2523,8 @@ class TestTokenizer : public TestFixture { const char code[] = "struct D {} const d;"; ASSERT_EQUALS("struct D { } ; struct D const d ;", tokenizeAndStringify(code, true, Settings::Native, "test.cpp")); - TODO_ASSERT_EQUALS("struct D { } ; struct D const d ;", - "struct D { } const int d ;", - tokenizeAndStringify(code, true, Settings::Native, "test.c")); + ASSERT_EQUALS("struct D { } ; struct D const d ;", + tokenizeAndStringify(code, true, Settings::Native, "test.c")); } void volatile_variables() {