Tokenizer: remove redundant assignments and variables
This commit is contained in:
parent
e15a16eb15
commit
1a59e1bc68
|
@ -2796,7 +2796,9 @@ void Tokenizer::removeRedundantAssignment()
|
||||||
tok2 = tok2->tokAt(2);
|
tok2 = tok2->tokAt(2);
|
||||||
localvars.insert(tok2->varId());
|
localvars.insert(tok2->varId());
|
||||||
}
|
}
|
||||||
else if (tok2->varId() && !Token::Match(tok2->previous(), "[;{}] %var% = %var% ;"))
|
else if (tok2->varId() &&
|
||||||
|
!Token::Match(tok2->previous(), "[;{}] %var% = %var% ;") &&
|
||||||
|
!Token::Match(tok2->previous(), "[;{}] %var% = %num% ;"))
|
||||||
{
|
{
|
||||||
localvars.erase(tok2->varId());
|
localvars.erase(tok2->varId());
|
||||||
}
|
}
|
||||||
|
@ -2814,7 +2816,7 @@ void Tokenizer::removeRedundantAssignment()
|
||||||
{
|
{
|
||||||
Token::eraseTokens(tok2, tok2->tokAt(4));
|
Token::eraseTokens(tok2, tok2->tokAt(4));
|
||||||
}
|
}
|
||||||
else if (Token::Match(tok2, "[;{}] %var% = %var% ;") && localvars.find(tok2->next()->varId()) != localvars.end())
|
else if (Token::Match(tok2, "[;{}] %var% = %any% ;") && localvars.find(tok2->next()->varId()) != localvars.end())
|
||||||
{
|
{
|
||||||
Token::eraseTokens(tok2, tok2->tokAt(4));
|
Token::eraseTokens(tok2, tok2->tokAt(4));
|
||||||
}
|
}
|
||||||
|
@ -4515,7 +4517,7 @@ bool Tokenizer::simplifyRedundantParanthesis()
|
||||||
ret = true;
|
ret = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Token::Match(tok->previous(), "[(!*;}] ( %var% )") && tok->next()->varId() != 0)
|
if (Token::Match(tok->previous(), "[(!*;{}] ( %var% )") && tok->next()->varId() != 0)
|
||||||
{
|
{
|
||||||
// We have "( var )", remove the paranthesis
|
// We have "( var )", remove the paranthesis
|
||||||
tok->deleteThis();
|
tok->deleteThis();
|
||||||
|
|
|
@ -293,7 +293,7 @@ private:
|
||||||
|
|
||||||
{
|
{
|
||||||
const char code1[] = "void f() { int a; bool use = true; if( use ) a=0; else if( bb ) a=1; else if( cc ) a=33; else { gg = 0; } int c=1; }";
|
const char code1[] = "void f() { int a; bool use = true; if( use ) a=0; else if( bb ) a=1; else if( cc ) a=33; else { gg = 0; } int c=1; }";
|
||||||
const char code2[] = "void f ( ) { int a ; ; ; { a = 0 ; } int c ; c = 1 ; }";
|
const char code2[] = "void f ( ) { ; ; ; { ; } ; ; }";
|
||||||
ASSERT_EQUALS(code2, tok(code1));
|
ASSERT_EQUALS(code2, tok(code1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -485,7 +485,7 @@ private:
|
||||||
ASSERT_EQUALS("return p ;", tok("return (p);"));
|
ASSERT_EQUALS("return p ;", tok("return (p);"));
|
||||||
ASSERT_EQUALS("void f ( ) { int * p ; if ( ! * p ) { } }", tok("void f(){int *p; if (*(p) == 0) {}}"));
|
ASSERT_EQUALS("void f ( ) { int * p ; if ( ! * p ) { } }", tok("void f(){int *p; if (*(p) == 0) {}}"));
|
||||||
ASSERT_EQUALS("void f ( ) { int * p ; if ( ! * p ) { } }", tok("void f(){int *p; if (*p == 0) {}}"));
|
ASSERT_EQUALS("void f ( ) { int * p ; if ( ! * p ) { } }", tok("void f(){int *p; if (*p == 0) {}}"));
|
||||||
ASSERT_EQUALS("void f ( ) { int p ; p = 1 ; }", tok("void f(){int p; (p) = 1;}"));
|
ASSERT_EQUALS("void f ( int & p ) { p = 1 ; }", tok("void f(int &p) {(p) = 1;}"));
|
||||||
ASSERT_EQUALS("void f ( ) { int p [ 10 ] ; p [ 0 ] = 1 ; }", tok("void f(){int p[10]; (p)[0] = 1;}"));
|
ASSERT_EQUALS("void f ( ) { int p [ 10 ] ; p [ 0 ] = 1 ; }", tok("void f(){int p[10]; (p)[0] = 1;}"));
|
||||||
ASSERT_EQUALS("void f ( ) { int p ; if ( ! p ) { } }", tok("void f(){int p; if ((p) == 0) {}}"));
|
ASSERT_EQUALS("void f ( ) { int p ; if ( ! p ) { } }", tok("void f(){int p; if ((p) == 0) {}}"));
|
||||||
ASSERT_EQUALS("void f ( ) { int * p ; * p = 1 ; }", tok("void f(){int *p; *(p) = 1;}"));
|
ASSERT_EQUALS("void f ( ) { int * p ; * p = 1 ; }", tok("void f(){int *p; *(p) = 1;}"));
|
||||||
|
@ -542,28 +542,28 @@ private:
|
||||||
|
|
||||||
void removePostIncrement()
|
void removePostIncrement()
|
||||||
{
|
{
|
||||||
const char code[] = "void f()\n"
|
const char code[] = "void f(int &c)\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" unsigned int c = 0;\n"
|
" c = 0;\n"
|
||||||
" c++;\n"
|
" c++;\n"
|
||||||
" if (c>0) { c++; }\n"
|
" if (c>0) { c++; }\n"
|
||||||
" c++;\n"
|
" c++;\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
ASSERT_EQUALS("void f ( ) { int c ; c = 3 ; ; { ; } ; }", tok(code));
|
ASSERT_EQUALS("void f ( int & c ) { c = 3 ; ; { ; } ; }", tok(code));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void removePreIncrement()
|
void removePreIncrement()
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
const char code[] = "void f()\n"
|
const char code[] = "void f(int &c)\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" unsigned int c = 0;\n"
|
" c = 0;\n"
|
||||||
" ++c;\n"
|
" ++c;\n"
|
||||||
" if (c>0) { ++c; }\n"
|
" if (c>0) { ++c; }\n"
|
||||||
" ++c;\n"
|
" ++c;\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
ASSERT_EQUALS("void f ( ) { int c ; c = 3 ; ; { ; } ; }", tok(code));
|
ASSERT_EQUALS("void f ( int & c ) { c = 3 ; ; { ; } ; }", tok(code));
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
|
@ -752,22 +752,22 @@ private:
|
||||||
const char code[] = "void f()\n"
|
const char code[] = "void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" char* ptrs[2];\n"
|
" char* ptrs[2];\n"
|
||||||
" int a = sizeof( ptrs );\n"
|
" a = sizeof( ptrs );\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
oss << (sizeofFromTokenizer("*") * 2);
|
oss << (sizeofFromTokenizer("*") * 2);
|
||||||
ASSERT_EQUALS("void f ( ) { char * ptrs [ 2 ] ; int a ; a = " + oss.str() + " ; }", sizeof_(code));
|
ASSERT_EQUALS("void f ( ) { char * ptrs [ 2 ] ; a = " + oss.str() + " ; }", sizeof_(code));
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
const char code[] = "void f()\n"
|
const char code[] = "void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" char* ptrs[55];\n"
|
" char* ptrs[55];\n"
|
||||||
" int a = sizeof( ptrs );\n"
|
" a = sizeof( ptrs );\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
oss << (sizeofFromTokenizer("*") * 55);
|
oss << (sizeofFromTokenizer("*") * 55);
|
||||||
ASSERT_EQUALS("void f ( ) { char * ptrs [ 55 ] ; int a ; a = " + oss.str() + " ; }", sizeof_(code));
|
ASSERT_EQUALS("void f ( ) { char * ptrs [ 55 ] ; a = " + oss.str() + " ; }", sizeof_(code));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -775,11 +775,11 @@ private:
|
||||||
const char code[] = "void f()\n"
|
const char code[] = "void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" char* ptrs;\n"
|
" char* ptrs;\n"
|
||||||
" int a = sizeof( ptrs );\n"
|
" a = sizeof( ptrs );\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
oss << sizeofFromTokenizer("*");
|
oss << sizeofFromTokenizer("*");
|
||||||
ASSERT_EQUALS("void f ( ) { ; int a ; a = " + oss.str() + " ; }", sizeof_(code));
|
ASSERT_EQUALS("void f ( ) { ; a = " + oss.str() + " ; }", sizeof_(code));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1941,7 +1941,7 @@ private:
|
||||||
" bool x = false;\n"
|
" bool x = false;\n"
|
||||||
" int b = x ? 44 : 3;\n"
|
" int b = x ? 44 : 3;\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
ASSERT_EQUALS("void f ( ) { ; ; int b ; b = 3 ; }", tok(code));
|
ASSERT_EQUALS("void f ( ) { ; ; ; ; }", tok(code));
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
|
@ -3014,7 +3014,7 @@ private:
|
||||||
"}\n"
|
"}\n"
|
||||||
"}";
|
"}";
|
||||||
|
|
||||||
ASSERT_EQUALS("void f ( ) { int x ; x = 0 ; { g ( ) ; } }", tok(code));
|
ASSERT_EQUALS("void f ( ) { ; ; { g ( ) ; } }", tok(code));
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
|
@ -3027,7 +3027,7 @@ private:
|
||||||
"}\n"
|
"}\n"
|
||||||
"}";
|
"}";
|
||||||
|
|
||||||
ASSERT_EQUALS("void f ( ) { int x ; x = 1 ; }", tok(code));
|
ASSERT_EQUALS("void f ( ) { ; ; }", tok(code));
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
|
@ -3266,17 +3266,17 @@ private:
|
||||||
// ticket #1005 - int *p(0); => int *p = 0;
|
// ticket #1005 - int *p(0); => int *p = 0;
|
||||||
{
|
{
|
||||||
const char code[] = "void foo() { int *p(0); }";
|
const char code[] = "void foo() { int *p(0); }";
|
||||||
ASSERT_EQUALS("void foo ( ) { int * p ; p = 0 ; }", tok(code));
|
ASSERT_EQUALS("void foo ( ) { ; ; }", tok(code));
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
const char code[] = "void foo() { int p(0); }";
|
const char code[] = "void foo() { int p(0); }";
|
||||||
ASSERT_EQUALS("void foo ( ) { int p ; p = 0 ; }", tok(code));
|
ASSERT_EQUALS("void foo ( ) { ; ; }", tok(code));
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
const char code[] = "void a() { foo *p(0); }";
|
const char code[] = "void a() { foo *p(0); }";
|
||||||
ASSERT_EQUALS("void a ( ) { foo * p ; p = 0 ; }", tok(code));
|
ASSERT_EQUALS("void a ( ) { ; ; }", tok(code));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -2269,7 +2269,7 @@ private:
|
||||||
std::ostringstream ostr;
|
std::ostringstream ostr;
|
||||||
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
|
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
|
||||||
ostr << " " << tok->str();
|
ostr << " " << tok->str();
|
||||||
ASSERT_EQUALS(" void f ( ) { const int a = 45 ; { int b ; b = 45 ; } } void g ( ) { int a ; a = 2 ; }", ostr.str());
|
ASSERT_EQUALS(" void f ( ) { const int a = 45 ; { ; ; } } void g ( ) { ; ; }", ostr.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void simplify_constants2()
|
void simplify_constants2()
|
||||||
|
@ -2872,6 +2872,7 @@ private:
|
||||||
void removeRedundantAssignment()
|
void removeRedundantAssignment()
|
||||||
{
|
{
|
||||||
ASSERT_EQUALS("void f ( ) { ; int * q ; ; }", tokenizeAndStringify("void f() { int *p, *q; p = q; }", true));
|
ASSERT_EQUALS("void f ( ) { ; int * q ; ; }", tokenizeAndStringify("void f() { int *p, *q; p = q; }", true));
|
||||||
|
ASSERT_EQUALS("void f ( ) { ; ; int * q ; ; }", tokenizeAndStringify("void f() { int *p = 0, *q; p = q; }", true));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue