Small optimizations for C code in Tokenizer. Add some regression tests for recently fixed results

This commit is contained in:
Alexander Mai 2015-08-29 19:00:09 +02:00
parent f5d131671c
commit 8d2c4453ad
3 changed files with 63 additions and 4 deletions

View File

@ -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, "[?:;,{}()]")) {

View File

@ -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() {

View File

@ -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"