Tokenizer::simplifyInitVar : Refactorings and fixes related to ticket #1989
This commit is contained in:
parent
d45186d645
commit
83a8879f11
|
@ -1994,9 +1994,6 @@ bool Tokenizer::tokenize(std::istream &code, const char FileName[], const std::s
|
|||
|
||||
simplifyVariableMultipleAssign();
|
||||
|
||||
// Change initialisation of variable to assignment
|
||||
simplifyInitVar();
|
||||
|
||||
// Remove redundant parantheses
|
||||
simplifyRedundantParanthesis();
|
||||
|
||||
|
@ -2026,6 +2023,9 @@ bool Tokenizer::tokenize(std::istream &code, const char FileName[], const std::s
|
|||
//updateClassList();
|
||||
setVarId();
|
||||
|
||||
// Change initialisation of variable to assignment
|
||||
simplifyInitVar();
|
||||
|
||||
_tokens->assignProgressValues();
|
||||
|
||||
return validate();
|
||||
|
@ -5517,10 +5517,14 @@ void Tokenizer::simplifyInitVar()
|
|||
{
|
||||
for (Token *tok = _tokens; tok; tok = tok->next())
|
||||
{
|
||||
if (Token::Match(tok, "[{};] class|struct|union| %type% *| %var% ( &| %any% ) ;"))
|
||||
tok = initVar(tok->next());
|
||||
else if (tok == _tokens && Token::Match(tok, "class|struct|union| %type% *| %var% ( &| %any% ) ;"))
|
||||
tok = initVar(tok);
|
||||
if (Token::Match(tok, "{|}|;| class|struct|union| %type% *| %var% ( &| %any% ) ;") ||
|
||||
Token::Match(tok, "{|}|;| %type% *| %var% ( %type% ("))
|
||||
{
|
||||
if (Token::Match(tok, "[;{}]"))
|
||||
tok = initVar(tok->next());
|
||||
else
|
||||
tok = initVar(tok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5561,7 +5565,7 @@ Token * Tokenizer::initVar(Token * tok)
|
|||
// check initializer..
|
||||
if (tok->tokAt(2)->isStandardType() || tok->tokAt(2)->str() == "void")
|
||||
return tok;
|
||||
else if (!tok->tokAt(2)->isNumber() && tok->tokAt(2)->str() != "&" && tok->tokAt(2)->varId() == 0)
|
||||
else if (!tok->tokAt(2)->isNumber() && !Token::Match(tok->tokAt(2), "%type% (") && tok->tokAt(2)->str() != "&" && tok->tokAt(2)->varId() == 0)
|
||||
return tok;
|
||||
|
||||
// insert '; var ='
|
||||
|
@ -5570,13 +5574,14 @@ Token * Tokenizer::initVar(Token * tok)
|
|||
tok = tok->tokAt(2);
|
||||
tok->insertToken("=");
|
||||
|
||||
// remove parantheses..
|
||||
tok = tok->next();
|
||||
tok->deleteNext();
|
||||
tok = tok->next();
|
||||
if (tok->str() == "&")
|
||||
tok = tok->next();
|
||||
tok->deleteNext();
|
||||
// goto '('..
|
||||
tok = tok->tokAt(2);
|
||||
|
||||
// delete ')'
|
||||
tok->link()->deleteThis();
|
||||
|
||||
// delete this
|
||||
tok->deleteThis();
|
||||
|
||||
return tok;
|
||||
}
|
||||
|
|
|
@ -44,6 +44,8 @@ private:
|
|||
TEST_CASE(tokenize3);
|
||||
TEST_CASE(tokenize4);
|
||||
TEST_CASE(tokenize5);
|
||||
TEST_CASE(tokenize6);
|
||||
TEST_CASE(tokenize7);
|
||||
|
||||
// array access. replace "*(p+1)" => "p[1]"
|
||||
TEST_CASE(tokenize6);
|
||||
|
@ -369,6 +371,16 @@ private:
|
|||
ASSERT_EQUALS("; x = p [ n ] ;", tokenizeAndStringify("; x = * ( p + n ) ;", true));
|
||||
}
|
||||
|
||||
void tokenize7()
|
||||
{
|
||||
const std::string code = "void f() {\n"
|
||||
" int x1 = 1;\n"
|
||||
" int x2(x1);\n"
|
||||
"}\n";
|
||||
ASSERT_EQUALS("void f ( ) {\nint x1 ; x1 = 1 ;\nint x2 ; x2 = x1 ;\n}",
|
||||
tokenizeAndStringify(code.c_str(), false));
|
||||
}
|
||||
|
||||
void wrong_syntax()
|
||||
{
|
||||
{
|
||||
|
@ -4200,13 +4212,8 @@ private:
|
|||
|
||||
{
|
||||
const char code[] = "int i ; int p(i);";
|
||||
// this can't be simplified because i doesn't have a varid yet
|
||||
ASSERT_EQUALS("int i ; int p ( i ) ;", tokenizeAndStringify(code, false));
|
||||
checkSimplifyInitVar(code, false);
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
// this can be simplified because i shold have a varid
|
||||
ASSERT_EQUALS("int i ; int p ; p = i ;", tokenizeAndStringify(code, true));
|
||||
checkSimplifyInitVar(code, true);
|
||||
ASSERT_EQUALS("int i ; int p ; p = i ;", tokenizeAndStringify(code));
|
||||
checkSimplifyInitVar(code);
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
|
@ -4361,25 +4368,22 @@ private:
|
|||
|
||||
{
|
||||
const char code[] = "class A { } ; int foo(A);";
|
||||
// don't know what A is yet so leave it alone
|
||||
ASSERT_EQUALS("class A { } ; int foo ( A ) ;", tokenizeAndStringify(code, false));
|
||||
checkSimplifyInitVar(code, false);
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
// we know A is not a variable here so leave it alone
|
||||
ASSERT_EQUALS("class A { } ; int foo ( A ) ;", tokenizeAndStringify(code, true));
|
||||
checkSimplifyInitVar(code, true);
|
||||
ASSERT_EQUALS("class A { } ; int foo ( A ) ;", tokenizeAndStringify(code));
|
||||
checkSimplifyInitVar(code);
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
{
|
||||
const char code[] = "class A { } ; A a; int foo(a);";
|
||||
// don't know what a is yet so leave it alone
|
||||
ASSERT_EQUALS("class A { } ; A a ; int foo ( a ) ;", tokenizeAndStringify(code, false));
|
||||
checkSimplifyInitVar(code, false);
|
||||
ASSERT_EQUALS("class A { } ; A a ; int foo ; foo = a ;", tokenizeAndStringify(code, false));
|
||||
checkSimplifyInitVar(code);
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
// we know a is a variable here so simplify it
|
||||
ASSERT_EQUALS("class A { } ; A a ; int foo ; foo = a ;", tokenizeAndStringify(code, true));
|
||||
checkSimplifyInitVar(code, true);
|
||||
}
|
||||
|
||||
{
|
||||
const char code[] = "int x(f());";
|
||||
ASSERT_EQUALS("int x ; x = f ( ) ;", tokenizeAndStringify(code, false));
|
||||
checkSimplifyInitVar(code);
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -349,7 +349,7 @@ private:
|
|||
"{\n"
|
||||
" int i(0);\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'i' is assigned a value that is never used\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:3]: (style) Unused variable: i\n", errout.str());
|
||||
|
||||
// if a is undefined then Cppcheck can't determine if "int i(a)" is a
|
||||
// * variable declaration
|
||||
|
@ -365,7 +365,7 @@ private:
|
|||
" int j = 0;\n"
|
||||
" int i(j);\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:4]: (style) Unused variable: i\n", errout.str());
|
||||
|
||||
functionVariableUsage("void foo()\n"
|
||||
"{\n"
|
||||
|
@ -404,14 +404,14 @@ private:
|
|||
" int * j = 0;\n"
|
||||
" int * i(j);\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:4]: (style) Unused variable: i\n", errout.str());
|
||||
|
||||
functionVariableUsage("void foo()\n"
|
||||
"{\n"
|
||||
" int * j = 0;\n"
|
||||
" const int * i(j);\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:4]: (style) Unused variable: i\n", errout.str());
|
||||
|
||||
functionVariableUsage("void foo()\n"
|
||||
"{\n"
|
||||
|
|
Loading…
Reference in New Issue