From ca3d0a9319f83d43d8f2ee8766264db6fcbcff03 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Wed, 13 Jul 2022 23:20:47 +0200 Subject: [PATCH 1/3] Fix #9877 FP unreadVariable (incomplete code; missing macro definition) --- lib/tokenize.cpp | 29 ----------------------------- lib/tokenize.h | 3 --- test/testtokenize.cpp | 14 -------------- test/testunusedvar.cpp | 9 +++++++++ 4 files changed, 9 insertions(+), 46 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 2d79460d7bd..3aa0a54911a 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5649,35 +5649,6 @@ void Tokenizer::removeMacroInClassDef() //--------------------------------------------------------------------------- -void Tokenizer::removeMacroInVarDecl() -{ - for (Token *tok = list.front(); tok; tok = tok->next()) { - if (Token::Match(tok, "[;{}] %name% (") && tok->next()->isUpperCaseName()) { - // goto ')' parentheses - const Token *tok2 = tok; - int parlevel = 0; - while (tok2) { - if (tok2->str() == "(") - ++parlevel; - else if (tok2->str() == ")") { - if (--parlevel <= 0) - break; - } - tok2 = tok2->next(); - } - tok2 = tok2 ? tok2->next() : nullptr; - - // check if this is a variable declaration.. - const Token *tok3 = tok2; - while (tok3 && tok3->isUpperCaseName()) - tok3 = tok3->next(); - if (tok3 && (tok3->isStandardType() || Token::Match(tok3,"const|static|struct|union|class"))) - Token::eraseTokens(tok,tok2); - } - } -} -//--------------------------------------------------------------------------- - void Tokenizer::addSemicolonAfterUnknownMacro() { if (!isCPP()) diff --git a/lib/tokenize.h b/lib/tokenize.h index e9636409886..cfa62c380b4 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -204,9 +204,6 @@ class CPPCHECKLIB Tokenizer { */ void removeMacroInClassDef(); - /** Remove unknown macro in variable declarations: PROGMEM char x; */ - void removeMacroInVarDecl(); - /** Add parentheses for sizeof: sizeof x => sizeof(x) */ void sizeofAddParentheses(); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 66801f73d8e..28dffe35c01 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -358,7 +358,6 @@ class TestTokenizer : public TestFixture { // Some simple cleanups of unhandled macros in the global scope TEST_CASE(removeMacrosInGlobalScope); - TEST_CASE(removeMacroInVarDecl); TEST_CASE(addSemicolonAfterUnknownMacro); @@ -5318,19 +5317,6 @@ class TestTokenizer : public TestFixture { InternalError); } - void removeMacroInVarDecl() { // #4304 - // only remove macros with parentheses (those hurt most) - ASSERT_EQUALS("void f ( ) { PROGMEM int x ; }", tokenizeAndStringify("void f() { PROGMEM int x ; }")); - ASSERT_EQUALS("void f ( ) { int x ; }", tokenizeAndStringify("void f() { SECTION(\".data.ro\") int x ; }")); - - // various variable declarations - ASSERT_EQUALS("void f ( ) { CONST int x ; }", tokenizeAndStringify("void f() { SECTION(\".data.ro\") CONST int x ; }")); - ASSERT_EQUALS("void f ( ) { char a [ 4 ] ; }", tokenizeAndStringify("void f() { SECTION(\".data.ro\") char a[4]; }")); - ASSERT_EQUALS("void f ( ) { const char a [ 4 ] ; }", tokenizeAndStringify("void f() { SECTION(\".data.ro\") const char a[4]; }")); - ASSERT_EQUALS("void f ( ) { struct ABC abc ; }", tokenizeAndStringify("void f() { SECTION(\".data.ro\") struct ABC abc; }")); - ASSERT_EQUALS("void f ( ) { CONST struct ABC abc ; }", tokenizeAndStringify("void f() { SECTION(\".data.ro\") CONST struct ABC abc; }")); - } - void addSemicolonAfterUnknownMacro() { // #6975 ASSERT_EQUALS("void f ( ) { MACRO ( ) ; try { } }", tokenizeAndStringify("void f() { MACRO() try {} }")); diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 8ff2097f159..9d874d10bc0 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -3217,6 +3217,15 @@ class TestUnusedVar : public TestFixture { " delete [] piArray;\n" "}"); ASSERT_EQUALS("", errout.str()); + + functionVariableUsage("std::vector g();\n" // #9877 + "int f() {\n" + " const std::vector x = get();\n" + " MACRO(2U, x.size())\n" + " int i = 0;\n" + " return i;\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void localvar44() { // #4020 - FP From 9fe9b6243e539d1d87fb57639744ba8c9d0f120c Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Wed, 13 Jul 2022 23:24:56 +0200 Subject: [PATCH 2/3] Remove --- lib/tokenize.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 3aa0a54911a..ff1b0a6728b 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4742,9 +4742,6 @@ bool Tokenizer::simplifyTokenList1(const char FileName[]) // Is there C++ code in C file? validateC(); - // remove MACRO in variable declaration: MACRO int x; - removeMacroInVarDecl(); - // Combine strings and character literals, e.g. L"string", L'c', "string1" "string2" combineStringAndCharLiterals(); From ad113e7ce61e9dfe9147c54989c7a1d93bdd0a1f Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Fri, 15 Jul 2022 23:48:39 +0200 Subject: [PATCH 3/3] Simplify test --- test/testunusedvar.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 9d874d10bc0..2108a2973c8 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -3218,8 +3218,7 @@ class TestUnusedVar : public TestFixture { "}"); ASSERT_EQUALS("", errout.str()); - functionVariableUsage("std::vector g();\n" // #9877 - "int f() {\n" + functionVariableUsage("int f() {\n" // #9877 " const std::vector x = get();\n" " MACRO(2U, x.size())\n" " int i = 0;\n"