From 8d2c4453ada6ea5f2c4f60fcf54abd03f9ec82c9 Mon Sep 17 00:00:00 2001 From: Alexander Mai Date: Sat, 29 Aug 2015 19:00:09 +0200 Subject: [PATCH] Small optimizations for C code in Tokenizer. Add some regression tests for recently fixed results --- lib/tokenize.cpp | 10 ++++++---- test/testbufferoverrun.cpp | 22 ++++++++++++++++++++++ test/testuninitvar.cpp | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index e5ed0de59..17bfa6d77 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3916,6 +3916,8 @@ void Tokenizer::removeMacrosInGlobalScope() void Tokenizer::removeMacroInClassDef() { + if (!isCPP()) + return; for (Token *tok = list.front(); tok; tok = tok->next()) { if (Token::Match(tok, "class|struct %name% %name% {|:") && (tok->next()->isUpperCaseName() || tok->tokAt(2)->isUpperCaseName())) { @@ -6567,7 +6569,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign } // Delete pointer alias - if (pointeralias && (tok3->str() == "delete") && tok3->next() && + if (isCPP() && pointeralias && (tok3->str() == "delete") && tok3->next() && (Token::Match(tok3->next(), "%varid% ;", varid) || Token::Match(tok3->next(), "[ ] %varid%", varid))) { tok3 = (tok3->next()->str() == "[") ? tok3->tokAt(3) : tok3->next(); @@ -8457,7 +8459,7 @@ void Tokenizer::simplifyComma() // We must not accept just any keyword, e.g. accepting int // would cause function parameters to corrupt. - if (tok->strAt(1) == "delete") { + if (isCPP() && tok->strAt(1) == "delete") { // Handle "delete a, delete b;" tok->str(";"); } @@ -8472,8 +8474,8 @@ void Tokenizer::simplifyComma() if (tok2->str() == "=") { // Handle "a = 0, b = 0;" replace = true; - } else if (Token::Match(tok2, "delete %name%") || - Token::Match(tok2, "delete [ ] %name%")) { + } else if (isCPP() && (Token::Match(tok2, "delete %name%") || + Token::Match(tok2, "delete [ ] %name%"))) { // Handle "delete a, a = 0;" replace = true; } else if (Token::Match(tok2, "[?:;,{}()]")) { diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index 2dfd148d2..edcf6dcbd 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -1590,6 +1590,28 @@ private: " a[0][0] = 0;\n" "}"); ASSERT_EQUALS("", errout.str()); + + check("void draw_quad(float z) {\n" + " int i;\n" + " float (*vertices)[2][4];\n" + " vertices[0][0][0] = z;\n" + " vertices[0][0][1] = z;\n" + " vertices[1][0][0] = z;\n" + " vertices[1][0][1] = z;\n" + " vertices[2][0][0] = z;\n" + " vertices[2][0][1] = z;\n" + " vertices[3][0][0] = z;\n" + " vertices[3][0][1] = z;\n" + " for (i = 0; i < 4; i++) {\n" + " vertices[i][0][2] = z;\n" + " vertices[i][0][3] = 1.0;\n" + " vertices[i][1][0] = 2.0;\n" + " vertices[i][1][1] = 3.0;\n" + " vertices[i][1][2] = 4.0;\n" + " vertices[i][1][3] = 5.0;\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void array_index_switch_in_for() { diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index d42ec93c4..1c7f2ab65 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -66,6 +66,7 @@ private: TEST_CASE(uninitvar_funcptr); // #6404 TEST_CASE(uninitvar_operator); // #6680 TEST_CASE(uninitvar_ternaryexpression); // #4683 + TEST_CASE(uninitvar_pointertoarray); TEST_CASE(trac_4871); TEST_CASE(syntax_error); // Ticket #5073 @@ -1430,6 +1431,40 @@ private: ASSERT_EQUALS("", errout.str()); } + void uninitvar_pointertoarray() { + checkUninitVar("void draw_quad(float z) {\n" + " int i;\n" + " float (*vertices)[2][4];\n" + " vertices[0][0][0] = z;\n" + " vertices[0][0][1] = z;\n" + " vertices[1][0][0] = z;\n" + " vertices[1][0][1] = z;\n" + " vertices[2][0][0] = z;\n" + " vertices[2][0][1] = z;\n" + " vertices[3][0][0] = z;\n" + " vertices[3][0][1] = z;\n" + " for (i = 0; i < 4; i++) {\n" + " vertices[i][0][2] = z;\n" + " vertices[i][0][3] = 1.0;\n" + " vertices[i][1][0] = 2.0;\n" + " vertices[i][1][1] = 3.0;\n" + " vertices[i][1][2] = 4.0;\n" + " vertices[i][1][3] = 5.0;\n" + " }\n" + "}\n"); + // kind of regression test - there are more lines which access vertices!? + ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: vertices\n" + "[test.cpp:5]: (error) Uninitialized variable: vertices\n" + "[test.cpp:6]: (error) Uninitialized variable: vertices\n" + "[test.cpp:7]: (error) Uninitialized variable: vertices\n" + "[test.cpp:8]: (error) Uninitialized variable: vertices\n" + "[test.cpp:9]: (error) Uninitialized variable: vertices\n" + "[test.cpp:10]: (error) Uninitialized variable: vertices\n" + "[test.cpp:11]: (error) Uninitialized variable: vertices\n" + "[test.cpp:18]: (error) Uninitialized variable: vertices\n", + errout.str()); + } + // alloc.. void uninitvar_alloc() { checkUninitVar("void f() {\n"