Tokenizer: remove removeRedundantAssignment from simplifyTokenList2

This commit is contained in:
Daniel Marjamäki 2022-06-10 12:35:05 +02:00
parent 676507fd72
commit 375880988c
3 changed files with 9 additions and 489 deletions

View File

@ -5304,8 +5304,6 @@ bool Tokenizer::simplifyTokenList2()
// Replace "&str[num]" => "(str + num)" // Replace "&str[num]" => "(str + num)"
simplifyOffsetPointerReference(); simplifyOffsetPointerReference();
removeRedundantAssignment();
simplifyRealloc(); simplifyRealloc();
// Change initialisation of variable to assignment // Change initialisation of variable to assignment
@ -5314,7 +5312,6 @@ bool Tokenizer::simplifyTokenList2()
// Simplify variable declarations // Simplify variable declarations
simplifyVarDecl(false); simplifyVarDecl(false);
simplifyErrNoInWhile();
simplifyIfAndWhileAssign(); simplifyIfAndWhileAssign();
simplifyRedundantParentheses(); simplifyRedundantParentheses();
simplifyNestedStrcat(); simplifyNestedStrcat();
@ -5367,8 +5364,6 @@ bool Tokenizer::simplifyTokenList2()
simplifyReturnStrncat(); simplifyReturnStrncat();
removeRedundantAssignment();
simplifyComma(); simplifyComma();
removeRedundantSemicolons(); removeRedundantSemicolons();
@ -5913,59 +5908,6 @@ void Tokenizer::addSemicolonAfterUnknownMacro()
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void Tokenizer::removeRedundantAssignment()
{
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (tok->str() == "{")
tok = tok->link();
const Token * const start = const_cast<Token *>(startOfExecutableScope(tok));
if (start) {
tok = start->previous();
// parse in this function..
std::set<int> localvars;
const Token * const end = tok->next()->link();
for (Token * tok2 = tok->next(); tok2 && tok2 != end; tok2 = tok2->next()) {
// skip local class or struct
if (Token::Match(tok2, "class|struct %type% {|:")) {
// skip to '{'
tok2 = tok2->tokAt(2);
while (tok2 && tok2->str() != "{")
tok2 = tok2->next();
if (tok2)
tok2 = tok2->link(); // skip local class or struct
else
return;
} else if (Token::Match(tok2, "[;{}] %type% * %name% ;") && tok2->next()->str() != "return") {
tok2 = tok2->tokAt(3);
localvars.insert(tok2->varId());
} else if (Token::Match(tok2, "[;{}] %type% %name% ;") && tok2->next()->isStandardType()) {
tok2 = tok2->tokAt(2);
localvars.insert(tok2->varId());
} else if (tok2->varId() &&
!Token::Match(tok2->previous(), "[;{}] %name% = %char%|%num%|%name% ;")) {
localvars.erase(tok2->varId());
}
}
localvars.erase(0);
if (!localvars.empty()) {
for (Token *tok2 = tok->next(); tok2 && tok2 != end;) {
if (Token::Match(tok2, "[;{}] %type% %name% ;") && localvars.find(tok2->tokAt(2)->varId()) != localvars.end()) {
tok2->deleteNext(3);
} else if ((Token::Match(tok2, "[;{}] %type% * %name% ;") &&
localvars.find(tok2->tokAt(3)->varId()) != localvars.end()) ||
(Token::Match(tok2, "[;{}] %name% = %any% ;") &&
localvars.find(tok2->next()->varId()) != localvars.end())) {
tok2->deleteNext(4);
} else
tok2 = tok2->next();
}
}
}
}
}
void Tokenizer::simplifyRealloc() void Tokenizer::simplifyRealloc()
{ {
for (Token *tok = list.front(); tok; tok = tok->next()) { for (Token *tok = list.front(); tok; tok = tok->next()) {
@ -10405,35 +10347,6 @@ void Tokenizer::simplifyFunctionTryCatch()
} }
} }
void Tokenizer::simplifyErrNoInWhile()
{
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (tok->str() != "errno")
continue;
Token *endpar = nullptr;
if (Token::Match(tok->previous(), "&& errno == EINTR ) { ;| }"))
endpar = tok->tokAt(3);
else if (Token::Match(tok->tokAt(-2), "&& ( errno == EINTR ) ) { ;| }"))
endpar = tok->tokAt(4);
else
continue;
if (Token::simpleMatch(endpar->link()->previous(), "while (")) {
Token *tok1 = tok->previous();
if (tok1->str() == "(")
tok1 = tok1->previous();
// erase "&& errno == EINTR"
tok1 = tok1->previous();
Token::eraseTokens(tok1, endpar);
// tok is invalid.. move to endpar
tok = endpar;
}
}
}
void Tokenizer::simplifyFuncInWhile() void Tokenizer::simplifyFuncInWhile()
{ {

View File

@ -269,9 +269,6 @@ public:
/** Remove unknown macro in variable declarations: PROGMEM char x; */ /** Remove unknown macro in variable declarations: PROGMEM char x; */
void removeMacroInVarDecl(); void removeMacroInVarDecl();
/** Remove redundant assignment */
void removeRedundantAssignment();
/** Simplifies some realloc usage like /** Simplifies some realloc usage like
* 'x = realloc (0, n);' => 'x = malloc(n);' * 'x = realloc (0, n);' => 'x = malloc(n);'
* 'x = realloc (y, 0);' => 'x = 0; free(y);' * 'x = realloc (y, 0);' => 'x = 0; free(y);'
@ -583,11 +580,6 @@ private:
*/ */
void simplifyWhile0(); void simplifyWhile0();
/**
* Simplify while(func() && errno==EINTR)
*/
void simplifyErrNoInWhile();
/** /**
* Simplify while(func(f)) * Simplify while(func(f))
*/ */

View File

@ -52,12 +52,6 @@ private:
settings_std.checkUnusedTemplates = true; settings_std.checkUnusedTemplates = true;
settings_windows.checkUnusedTemplates = true; settings_windows.checkUnusedTemplates = true;
// Make sure the Tokenizer::simplifyTokenList works.
// The order of the simplifications is important. So this test
// case shall make sure the simplifications are done in the
// correct order
TEST_CASE(simplifyTokenList1);
TEST_CASE(test1); // array access. replace "*(p+1)" => "p[1]" TEST_CASE(test1); // array access. replace "*(p+1)" => "p[1]"
TEST_CASE(cast); TEST_CASE(cast);
@ -156,7 +150,6 @@ private:
TEST_CASE(simplifyOperator2); TEST_CASE(simplifyOperator2);
TEST_CASE(simplifyArrayAccessSyntax); TEST_CASE(simplifyArrayAccessSyntax);
TEST_CASE(simplify_numeric_condition);
TEST_CASE(simplify_condition); TEST_CASE(simplify_condition);
TEST_CASE(pointeralias1); TEST_CASE(pointeralias1);
@ -172,18 +165,12 @@ private:
// remove "std::" on some standard functions // remove "std::" on some standard functions
TEST_CASE(removestd); TEST_CASE(removestd);
// Tokenizer::simplifyInitVar
TEST_CASE(simplifyInitVar);
// Tokenizer::simplifyReference // Tokenizer::simplifyReference
TEST_CASE(simplifyReference); TEST_CASE(simplifyReference);
// x = realloc(y,0); => free(y);x=0; // x = realloc(y,0); => free(y);x=0;
TEST_CASE(simplifyRealloc); TEST_CASE(simplifyRealloc);
// while(f() && errno==EINTR) { } => while (f()) { }
TEST_CASE(simplifyErrNoInWhile);
// while(fclose(f)); => r = fclose(f); while(r){r=fclose(f);} // while(fclose(f)); => r = fclose(f); while(r){r=fclose(f);}
TEST_CASE(simplifyFuncInWhile); TEST_CASE(simplifyFuncInWhile);
@ -257,7 +244,7 @@ private:
TEST_CASE(simplifyKnownVariables25); TEST_CASE(simplifyKnownVariables25);
TEST_CASE(simplifyKnownVariables27); TEST_CASE(simplifyKnownVariables27);
TEST_CASE(simplifyKnownVariables28); TEST_CASE(simplifyKnownVariables28);
TEST_CASE(simplifyKnownVariables29); // ticket #1811 // FIXME Does expression id handle these? TEST_CASE(simplifyKnownVariables29); // ticket #1811
TEST_CASE(simplifyKnownVariables30); TEST_CASE(simplifyKnownVariables30);
TEST_CASE(simplifyKnownVariables31); TEST_CASE(simplifyKnownVariables31);
TEST_CASE(simplifyKnownVariables32); // const TEST_CASE(simplifyKnownVariables32); // const
@ -265,10 +252,7 @@ private:
TEST_CASE(simplifyKnownVariables34); TEST_CASE(simplifyKnownVariables34);
TEST_CASE(simplifyKnownVariables35); // ticket #2353 - False positive: Division by zero 'if (x == 0) return 0; return 10 / x;' TEST_CASE(simplifyKnownVariables35); // ticket #2353 - False positive: Division by zero 'if (x == 0) return 0; return 10 / x;'
TEST_CASE(simplifyKnownVariables36); // ticket #2304 - known value for strcpy parameter TEST_CASE(simplifyKnownVariables36); // ticket #2304 - known value for strcpy parameter
TEST_CASE(simplifyKnownVariables37); // ticket #2398 - false positive caused by no simplification in for loop
TEST_CASE(simplifyKnownVariables38); // ticket #2399 - simplify conditions
TEST_CASE(simplifyKnownVariables39); TEST_CASE(simplifyKnownVariables39);
TEST_CASE(simplifyKnownVariables40);
TEST_CASE(simplifyKnownVariables41); // p=&x; if (p) .. TEST_CASE(simplifyKnownVariables41); // p=&x; if (p) ..
TEST_CASE(simplifyKnownVariables42); // ticket #2031 - known string value after strcpy TEST_CASE(simplifyKnownVariables42); // ticket #2031 - known string value after strcpy
TEST_CASE(simplifyKnownVariables43); TEST_CASE(simplifyKnownVariables43);
@ -280,7 +264,6 @@ private:
TEST_CASE(simplifyKnownVariables49); // #3691 - continue in switch TEST_CASE(simplifyKnownVariables49); // #3691 - continue in switch
TEST_CASE(simplifyKnownVariables50); // #4066 sprintf changes TEST_CASE(simplifyKnownVariables50); // #4066 sprintf changes
TEST_CASE(simplifyKnownVariables51); // #4409 hang TEST_CASE(simplifyKnownVariables51); // #4409 hang
TEST_CASE(simplifyKnownVariables52); // #4728 "= x %cop%"
TEST_CASE(simplifyKnownVariables53); // references TEST_CASE(simplifyKnownVariables53); // references
TEST_CASE(simplifyKnownVariables54); // #4913 'x' is not 0 after *--x=0; TEST_CASE(simplifyKnownVariables54); // #4913 'x' is not 0 after *--x=0;
TEST_CASE(simplifyKnownVariables55); // pointer alias TEST_CASE(simplifyKnownVariables55); // pointer alias
@ -301,11 +284,9 @@ private:
TEST_CASE(simplifyKnownVariablesBailOutMemberFunction); TEST_CASE(simplifyKnownVariablesBailOutMemberFunction);
TEST_CASE(simplifyKnownVariablesBailOutConditionalIncrement); TEST_CASE(simplifyKnownVariablesBailOutConditionalIncrement);
TEST_CASE(simplifyKnownVariablesBailOutSwitchBreak); // ticket #2324 TEST_CASE(simplifyKnownVariablesBailOutSwitchBreak); // ticket #2324
TEST_CASE(simplifyKnownVariablesFloat); // #2454 - float variable
TEST_CASE(simplifyKnownVariablesClassMember); // #2815 - value of class member may be changed by function call TEST_CASE(simplifyKnownVariablesClassMember); // #2815 - value of class member may be changed by function call
TEST_CASE(simplifyKnownVariablesFunctionCalls); // Function calls (don't assume pass by reference) TEST_CASE(simplifyKnownVariablesFunctionCalls); // Function calls (don't assume pass by reference)
TEST_CASE(simplifyKnownVariablesGlobalVars); TEST_CASE(simplifyKnownVariablesGlobalVars);
TEST_CASE(simplifyKnownVariablesReturn); // 3500 - return
TEST_CASE(simplifyKnownVariablesPointerAliasFunctionCall); // #7440 TEST_CASE(simplifyKnownVariablesPointerAliasFunctionCall); // #7440
TEST_CASE(simplifyKnownVariablesNamespace); // #10059 TEST_CASE(simplifyKnownVariablesNamespace); // #10059
@ -326,9 +307,6 @@ private:
TEST_CASE(simplifyCasts16); // #6278 TEST_CASE(simplifyCasts16); // #6278
TEST_CASE(simplifyCasts17); // #6110 - don't remove any parentheses in 'a(b)(c)' TEST_CASE(simplifyCasts17); // #6110 - don't remove any parentheses in 'a(b)(c)'
TEST_CASE(removeRedundantAssignment);
TEST_CASE(simplify_constants);
TEST_CASE(simplify_constants2); TEST_CASE(simplify_constants2);
TEST_CASE(simplify_constants3); TEST_CASE(simplify_constants3);
TEST_CASE(simplify_constants4); TEST_CASE(simplify_constants4);
@ -454,13 +432,6 @@ private:
return tokenizer.tokens()->stringifyList(true); return tokenizer.tokens()->stringifyList(true);
} }
void simplifyTokenList1() {
// #1717 : The simplifyErrNoInWhile needs to be used before simplifyIfAndWhileAssign..
ASSERT_EQUALS("{ x = f ( ) ; while ( x == -1 ) { x = f ( ) ; } }",
tok("{ while((x=f())==-1 && errno==EINTR){}}",true));
}
void test1() { void test1() {
// "&p[1]" => "p+1" // "&p[1]" => "p+1"
@ -1909,29 +1880,6 @@ private:
const char code2[] = " void f() { int a; bool use = true; { a=0;} int c=1; }"; const char code2[] = " void f() { int a; bool use = true; { a=0;} int c=1; }";
ASSERT_EQUALS(tok(code2), tok(code1)); ASSERT_EQUALS(tok(code2), tok(code1));
} }
{
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 ( ) { }";
ASSERT_EQUALS(code2, tok(code1));
}
{
const char code1[] = " void f() { if( aa ) { a=0; } else if( true ) a=1; else { a=2; } }";
const char code2[] = " void f ( ) { if ( aa ) { a = 0 ; } else { { a = 1 ; } } }";
ASSERT_EQUALS(tok(code2), tok(code1));
}
{
const char code1[] = " void f() { if( aa ) { a=0; } else if( false ) a=1; else { a=2; } }";
const char code2[] = " void f ( ) { if ( aa ) { a = 0 ; } else { { a = 2 ; } } }";
ASSERT_EQUALS(tok(code2), tok(code1));
}
{
const char code1[] = "static const int x=1; void f() { if(x) { a=0; } }";
ASSERT_EQUALS("void f ( ) { a = 0 ; }", tok(code1));
}
} }
void combine_strings() { void combine_strings() {
@ -2483,7 +2431,7 @@ private:
"}\n"; "}\n";
std::ostringstream oss; std::ostringstream oss;
oss << sizeofFromTokenizer("*"); oss << sizeofFromTokenizer("*");
ASSERT_EQUALS("void f ( ) { a = " + oss.str() + " ; }", tok(code)); ASSERT_EQUALS("void f ( ) { char * ptrs ; a = " + oss.str() + " ; }", tok(code));
} }
} }
@ -2658,7 +2606,7 @@ private:
const char expected[] = "void f ( ) " const char expected[] = "void f ( ) "
"{" "{"
"" " int * p ;"
" 4 ; " " 4 ; "
"}"; "}";
@ -3550,29 +3498,6 @@ private:
ASSERT_EQUALS("( abc . a ) ;", tok(code)); ASSERT_EQUALS("( abc . a ) ;", tok(code));
} }
{
const char code[] = "void f()\n"
"{\n"
" bool x = false;\n"
" int b = x ? 44 : 3;\n"
"}\n";
ASSERT_EQUALS("void f ( ) { }", tok(code));
}
{
const char code[] = "int vals[] = { 0x13, 1?0x01:0x00 };";
ASSERT_EQUALS("int vals [ 2 ] = { 0x13 , 0x01 } ;", tok(code));
}
{
const char code[] = "int vals[] = { 0x13, 0?0x01:0x00 };";
ASSERT_EQUALS("int vals [ 2 ] = { 0x13 , 0x00 } ;", tok(code));
}
{
const char code[] = "a = 1 ? 0 : ({ 0; });";
ASSERT_EQUALS("a = 0 ;", tok(code));
}
//GNU extension: "x ?: y" <-> "x ? x : y" //GNU extension: "x ?: y" <-> "x ? x : y"
{ {
@ -3589,7 +3514,6 @@ private:
ASSERT_EQUALS("; x = 4 ;", tok("; x = (false)?2:4;")); ASSERT_EQUALS("; x = 4 ;", tok("; x = (false)?2:4;"));
ASSERT_EQUALS("; x = * a ;", tok("; x = (true)?*a:*b;")); ASSERT_EQUALS("; x = * a ;", tok("; x = (true)?*a:*b;"));
ASSERT_EQUALS("; x = * b ;", tok("; x = (false)?*a:*b;")); ASSERT_EQUALS("; x = * b ;", tok("; x = (false)?*a:*b;"));
ASSERT_EQUALS("void f ( ) { return 1 ; }", tok("void f() { char *p=0; return (p==0)?1:2; }"));
} }
{ {
@ -3711,31 +3635,6 @@ private:
ASSERT_EQUALS("void foo ( int b ) { int a ; a = b ; bar ( a ) ; }", ASSERT_EQUALS("void foo ( int b ) { int a ; a = b ; bar ( a ) ; }",
tok("void foo ( int b ) { int a = 0 | b ; bar ( a ) ; }")); tok("void foo ( int b ) { int a = 0 | b ; bar ( a ) ; }"));
// ticket #3093
ASSERT_EQUALS("int f ( ) { return 15 ; }",
tok("int f() { int a = 10; int b = 5; return a + b; }"));
ASSERT_EQUALS("int f ( ) { return a ; }",
tok("int f() { return a * 1; }"));
ASSERT_EQUALS("int f ( int a ) { return 0 ; }",
tok("int f(int a) { return 0 * a; }"));
ASSERT_EQUALS("bool f ( int i ) { switch ( i ) { case 15 : ; return true ; } }",
tok("bool f(int i) { switch (i) { case 10 + 5: return true; } }"));
// ticket #3576 - False positives in boolean expressions
ASSERT_EQUALS("int foo ( ) { return 1 ; }",
tok("int foo ( ) { int i; int j; i = 1 || j; return i; }"));
ASSERT_EQUALS("int foo ( ) { return 0 ; }",
tok("int foo ( ) { int i; int j; i = 0 && j; return i; }")); // ticket #3576 - False positives in boolean expressions
// ticket #3723 - Simplify condition (0 && a < 123)
ASSERT_EQUALS("( 0 ) ;",
tok("( 0 && a < 123 );"));
ASSERT_EQUALS("( 0 ) ;",
tok("( 0 && a[123] );"));
// ticket #4931
ASSERT_EQUALS("dostuff ( 1 ) ;", tok("dostuff(9&&8);"));
} }
@ -4116,109 +4015,6 @@ private:
"1: int a@1 ; a@1 [ 13 ] ;\n", tokenizeDebugListing("int a; 13[a];")); "1: int a@1 ; a@1 [ 13 ] ;\n", tokenizeDebugListing("int a; 13[a];"));
} }
void simplify_numeric_condition() {
{
const char code[] =
"void f()\n"
"{\n"
"int x = 0;\n"
"if( !x || 0 )\n"
"{ g();\n"
"}\n"
"}";
ASSERT_EQUALS("void f ( ) { g ( ) ; }", tok(code));
}
{
const char code[] =
"void f()\n"
"{\n"
"int x = 1;\n"
"if( !x )\n"
"{ g();\n"
"}\n"
"}";
ASSERT_EQUALS("void f ( ) { }", tok(code));
}
{
const char code[] =
"void f()\n"
"{\n"
"bool x = true;\n"
"if( !x )\n"
"{ g();\n"
"}\n"
"}";
ASSERT_EQUALS("void f ( ) { }", tok(code));
}
{
const char code[] =
"void f()\n"
"{\n"
"bool x = false;\n"
"if( !x )\n"
"{ g();\n"
"}\n"
"}";
ASSERT_EQUALS("void f ( ) { g ( ) ; }", tok(code));
}
{
const char code[] = "void f()\n"
"{\n"
" if (5==5);\n"
"}\n";
ASSERT_EQUALS("void f ( ) { ; }", tok(code));
}
{
const char code[] = "void f()\n"
"{\n"
" if (4<5);\n"
"}\n";
ASSERT_EQUALS("void f ( ) { ; }", tok(code));
}
{
const char code[] = "void f()\n"
"{\n"
" if (5<5);\n"
"}\n";
ASSERT_EQUALS("void f ( ) { }", tok(code));
}
{
const char code[] = "void f()\n"
"{\n"
" if (13>12?true:false);\n"
"}\n";
ASSERT_EQUALS("void f ( ) { ; }", tok(code));
}
{
// #7849
const char code[] =
"void f() {\n"
"if (-1e-2 == -0.01) \n"
" g();\n"
"else\n"
" h();\n"
"}";
ASSERT_EQUALS("void f ( ) { if ( -1e-2 == -0.01 ) { g ( ) ; } else { h ( ) ; } }",
tok(code));
}
}
void simplify_condition() { void simplify_condition() {
{ {
const char code[] = const char code[] =
@ -4346,7 +4142,7 @@ private:
" int *p = &i;\n" " int *p = &i;\n"
" return *p;\n" " return *p;\n"
"}\n"; "}\n";
ASSERT_EQUALS("void f ( ) { int i ; return i ; }", tok(code)); ASSERT_EQUALS("void f ( ) { int i ; int * p ; return i ; }", tok(code));
} }
void pointeralias3() { void pointeralias3() {
@ -4375,10 +4171,7 @@ private:
" *p = 5;\n" " *p = 5;\n"
" return i;\n" " return i;\n"
"}\n"; "}\n";
const char expected[] = "int f ( ) " const char expected[] = "int f ( ) { int i ; int * p ; i = 5 ; return 5 ; }";
"{"
" return 5 ; "
"}";
ASSERT_EQUALS(expected, tok(code)); ASSERT_EQUALS(expected, tok(code));
} }
@ -4398,11 +4191,6 @@ private:
//ticket #3140 //ticket #3140
ASSERT_EQUALS("void f ( ) { int i ; for ( i = 0 ; i < 0 ; i ++ ) { } }", tok("void f() { int i; for (i = 0; i < 0; i++) { foo(); break; } }")); ASSERT_EQUALS("void f ( ) { int i ; for ( i = 0 ; i < 0 ; i ++ ) { } }", tok("void f() { int i; for (i = 0; i < 0; i++) { foo(); break; } }"));
ASSERT_EQUALS("void f ( ) { int i ; for ( i = 0 ; i < 0 ; i ++ ) { } }", tok("void f() { int i; for (i = 0; i < 0; i++) { foo(); continue; } }")); ASSERT_EQUALS("void f ( ) { int i ; for ( i = 0 ; i < 0 ; i ++ ) { } }", tok("void f() { int i; for (i = 0; i < 0; i++) { foo(); continue; } }"));
ASSERT_EQUALS("void f ( ) { }", tok("void f() { for (int i = 0; i < 0; i++) { a; } }"));
ASSERT_EQUALS("void f ( ) { }", tok("void f() { for (unsigned int i = 0; i < 0; i++) { a; } }"));
ASSERT_EQUALS("void f ( ) { }", tok("void f() { for (long long i = 0; i < 0; i++) { a; } }"));
ASSERT_EQUALS("void f ( ) { }", tok("void f() { for (signed long long i = 0; i < 0; i++) { a; } }"));
ASSERT_EQUALS("void f ( ) { }", tok("void f() { int n = 0; for (signed long long i = 0; i < n; i++) { a; } }"));
// #8059 // #8059
ASSERT_EQUALS("void f ( ) { int i ; for ( i = 0 ; i < 0 ; ++ i ) { } return i ; }", tok("void f() { int i; for (i=0;i<0;++i){ dostuff(); } return i; }")); ASSERT_EQUALS("void f ( ) { int i ; for ( i = 0 ; i < 0 ; ++ i ) { } return i ; }", tok("void f() { int i; for (i=0;i<0;++i){ dostuff(); } return i; }"));
} }
@ -4416,24 +4204,6 @@ private:
ASSERT_EQUALS("; malloc ( 10 ) ;", tok("; std::malloc(10);")); ASSERT_EQUALS("; malloc ( 10 ) ;", tok("; std::malloc(10);"));
} }
void simplifyInitVar() {
// ticket #1005 - int *p(0); => int *p = 0;
{
const char code[] = "void foo() { int *p(0); }";
ASSERT_EQUALS("void foo ( ) { }", tok(code));
}
{
const char code[] = "void foo() { int p(0); }";
ASSERT_EQUALS("void foo ( ) { }", tok(code));
}
{
const char code[] = "void a() { foo *p(0); }";
ASSERT_EQUALS("void a ( ) { }", tok(code));
}
}
void simplifyReference() { void simplifyReference() {
ASSERT_EQUALS("void f ( ) { int a ; a ++ ; }", ASSERT_EQUALS("void f ( ) { int a ; a ++ ; }",
tok("void f() { int a; int &b(a); b++; }")); tok("void f() { int a; int &b(a); b++; }"));
@ -4455,13 +4225,6 @@ private:
ASSERT_EQUALS("; p = malloc ( f ( 1 ) ) ;", tok("; p = realloc(0, f(1));")); ASSERT_EQUALS("; p = malloc ( f ( 1 ) ) ;", tok("; p = realloc(0, f(1));"));
} }
void simplifyErrNoInWhile() {
ASSERT_EQUALS("{ while ( f ( ) ) { } }",
tok("{ while (f() && errno == EINTR) { } }"));
ASSERT_EQUALS("{ while ( f ( ) ) { } }",
tok("{ while (f() && (errno == EINTR)) { } }"));
}
void simplifyFuncInWhile() { void simplifyFuncInWhile() {
ASSERT_EQUALS("{ " ASSERT_EQUALS("{ "
"int cppcheck:r1 = fclose ( f ) ; " "int cppcheck:r1 = fclose ( f ) ; "
@ -6117,42 +5880,6 @@ private:
ASSERT_EQUALS(expected2, tokenizeAndStringify(code2, true)); ASSERT_EQUALS(expected2, tokenizeAndStringify(code2, true));
} }
void simplifyKnownVariables37() {
// Ticket #2398 - no simplification in for loop
const char code[] = "void f() {\n"
" double x = 0;\n"
" for (int iter=0; iter<42; iter++) {\n"
" int EvaldF = 1;\n"
" if (EvaldF)\n"
" Eval (x);\n"
" }\n"
"}";
const char expected[] = "void f ( ) {\n"
"double x ; x = 0 ;\n"
"for ( int iter = 0 ; iter < 42 ; iter ++ ) {\n"
"\n"
"\n"
"Eval ( x ) ;\n"
"}\n"
"}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
}
void simplifyKnownVariables38() {
// Ticket #2399 - simplify conditions
const char code[] = "void f() {\n"
" int x = 0;\n"
" int y = 1;\n"
" if (x || y);\n"
"}";
const char expected[] = "void f ( ) {\n"
"\n"
"\n"
";\n"
"}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
}
void simplifyKnownVariables39() { void simplifyKnownVariables39() {
// Ticket #2296 - simplify pointer alias 'delete p;' // Ticket #2296 - simplify pointer alias 'delete p;'
{ {
@ -6161,7 +5888,7 @@ private:
" int *y = x;\n" " int *y = x;\n"
" delete y;\n" " delete y;\n"
"}"; "}";
ASSERT_EQUALS("void f ( ) {\nint * x ;\n\ndelete x ;\n}", tokenizeAndStringify(code, true)); ASSERT_EQUALS("void f ( ) {\nint * x ;\nint * y ; y = x ;\ndelete x ;\n}", tokenizeAndStringify(code, true));
} }
{ {
const char code[] = "void f() {\n" const char code[] = "void f() {\n"
@ -6169,19 +5896,10 @@ private:
" int *y = x;\n" " int *y = x;\n"
" delete [] y;\n" " delete [] y;\n"
"}"; "}";
ASSERT_EQUALS("void f ( ) {\nint * x ;\n\ndelete [ ] x ;\n}", tokenizeAndStringify(code, true)); ASSERT_EQUALS("void f ( ) {\nint * x ;\nint * y ; y = x ;\ndelete [ ] x ;\n}", tokenizeAndStringify(code, true));
} }
} }
void simplifyKnownVariables40() {
const char code[] = "void f() {\n"
" char c1 = 'a';\n"
" char c2 = { c1 };\n"
"}";
ASSERT_EQUALS("void f ( ) {\n\nchar c2 ; c2 = { 'a' } ;\n}", tokenizeAndStringify(code, true));
}
void simplifyKnownVariables41() { void simplifyKnownVariables41() {
const char code[] = "void f() {\n" const char code[] = "void f() {\n"
" int x = 0;\n" " int x = 0;\n"
@ -6329,15 +6047,6 @@ private:
"}"; "}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Settings::Native, "test.cpp")); ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Settings::Native, "test.cpp"));
} }
{
const char expected[] = "void f ( ) {\n"
"\n"
"cin >> 0 ;\n"
"return 0 ;\n"
"}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Settings::Native, "test.c"));
}
} }
void simplifyKnownVariables47() { void simplifyKnownVariables47() {
@ -6473,31 +6182,6 @@ private:
ASSERT_THROW(tokenizeAndStringify(code, true), InternalError); ASSERT_THROW(tokenizeAndStringify(code, true), InternalError);
} }
void simplifyKnownVariables52() { // #4728 "= x %op%"
ASSERT_EQUALS("void f ( ) { int y ; y = 34 + z ; }", tokenizeAndStringify("void f() { int x=34; int y=x+z; }", true));
ASSERT_EQUALS("void f ( ) { int y ; y = 34 - z ; }", tokenizeAndStringify("void f() { int x=34; int y=x-z; }", true));
ASSERT_EQUALS("void f ( ) { int y ; y = 34 * z ; }", tokenizeAndStringify("void f() { int x=34; int y=x*z; }", true));
ASSERT_EQUALS("void f ( ) { int y ; y = 34 / z ; }", tokenizeAndStringify("void f() { int x=34; int y=x/z; }", true));
ASSERT_EQUALS("void f ( ) { int y ; y = 34 % z ; }", tokenizeAndStringify("void f() { int x=34; int y=x%z; }", true));
ASSERT_EQUALS("void f ( ) { int y ; y = 34 & z ; }", tokenizeAndStringify("void f() { int x=34; int y=x&z; }", true));
ASSERT_EQUALS("void f ( ) { int y ; y = 34 | z ; }", tokenizeAndStringify("void f() { int x=34; int y=x|z; }", true));
ASSERT_EQUALS("void f ( ) { int y ; y = 34 ^ z ; }", tokenizeAndStringify("void f() { int x=34; int y=x^z; }", true));
ASSERT_EQUALS("void f ( ) { int y ; y = 34 << z ; }", tokenizeAndStringify("void f() { int x=34; int y=x<<z; }", true));
ASSERT_EQUALS("void f ( ) { int y ; y = 34 >> z ; }", tokenizeAndStringify("void f() { int x=34; int y=x>>z; }", true));
ASSERT_EQUALS("void f ( ) { int y ; y = 34 && z ; }", tokenizeAndStringify("void f() { int x=34; int y=x&&z; }", true));
ASSERT_EQUALS("void f ( ) { int y ; y = 34 || z ; }", tokenizeAndStringify("void f() { int x=34; int y=x||z; }", true));
ASSERT_EQUALS("void f ( ) { int y ; y = 34 > z ; }", tokenizeAndStringify("void f() { int x=34; int y=x>z; }", true));
ASSERT_EQUALS("void f ( ) { int y ; y = 34 >= z ; }", tokenizeAndStringify("void f() { int x=34; int y=x>=z; }", true));
ASSERT_EQUALS("void f ( ) { int y ; y = 34 < z ; }", tokenizeAndStringify("void f() { int x=34; int y=x<z; }", true));
ASSERT_EQUALS("void f ( ) { int y ; y = 34 <= z ; }", tokenizeAndStringify("void f() { int x=34; int y=x<=z; }", true));
ASSERT_EQUALS("void f ( ) { int y ; y = 34 == z ; }", tokenizeAndStringify("void f() { int x=34; int y=x==z; }", true));
ASSERT_EQUALS("void f ( ) { int y ; y = 34 != z ; }", tokenizeAndStringify("void f() { int x=34; int y=x!=z; }", true));
// #4007
ASSERT_EQUALS("void f ( ) { }", tokenizeAndStringify("void f() { char *p = 0; int result = p && (!*p); }", true));
ASSERT_EQUALS("void f ( ) { }", tokenizeAndStringify("void f() { Foo *p = 0; bool b = (p && (p->type() == 1)); }", true));
}
void simplifyKnownVariables53() { // references void simplifyKnownVariables53() { // references
ASSERT_EQUALS("void f ( ) { int x ; x = abc ( ) ; }", tokenizeAndStringify("void f() { int x; int &ref=x; ref=abc(); }", true)); ASSERT_EQUALS("void f ( ) { int x ; x = abc ( ) ; }", tokenizeAndStringify("void f() { int x; int &ref=x; ref=abc(); }", true));
ASSERT_EQUALS("void f ( ) { int * p ; p = abc ( ) ; }", tokenizeAndStringify("void f() { int *p; int *&ref=p; ref=abc(); }", true)); ASSERT_EQUALS("void f ( ) { int * p ; p = abc ( ) ; }", tokenizeAndStringify("void f() { int *p; int *&ref=p; ref=abc(); }", true));
@ -6508,9 +6192,9 @@ private:
} }
void simplifyKnownVariables55() { // pointer alias void simplifyKnownVariables55() { // pointer alias
ASSERT_EQUALS("void f ( ) { int a ; if ( a > 0 ) { } }", tokenizeAndStringify("void f() { int a; int *p=&a; if (*p>0) {} }", true)); ASSERT_EQUALS("void f ( ) { int a ; int * p ; if ( a > 0 ) { } }", tokenizeAndStringify("void f() { int a; int *p=&a; if (*p>0) {} }", true));
ASSERT_EQUALS("void f ( ) { int a ; struct AB ab ; ab . a = & a ; if ( a > 0 ) { } }", tokenizeAndStringify("void f() { int a; struct AB ab; ab.a = &a; if (*ab.a>0) {} }", true)); ASSERT_EQUALS("void f ( ) { int a ; struct AB ab ; ab . a = & a ; if ( a > 0 ) { } }", tokenizeAndStringify("void f() { int a; struct AB ab; ab.a = &a; if (*ab.a>0) {} }", true));
ASSERT_EQUALS("void f ( ) { int a ; if ( x > a ) { } }", tokenizeAndStringify("void f() { int a; int *p=&a; if (x>*p) {} }", true)); ASSERT_EQUALS("void f ( ) { int a ; int * p ; if ( x > a ) { } }", tokenizeAndStringify("void f() { int a; int *p=&a; if (x>*p) {} }", true));
} }
void simplifyKnownVariables56() { // ticket #5301 - >> void simplifyKnownVariables56() { // ticket #5301 - >>
@ -6744,49 +6428,7 @@ private:
ASSERT_EQUALS(expected, tokenizeAndStringify(code,true)); ASSERT_EQUALS(expected, tokenizeAndStringify(code,true));
} }
void simplifyKnownVariablesFloat() {
// Ticket #2454
const char code[] = "void f() {\n"
" float a = 40;\n"
" x(10 / a);\n"
"}\n";
const char expected[] = "void f ( ) {\n\nx ( 0.25 ) ;\n}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code,true));
// Ticket #4227
const char code2[] = "double f() {"
" double a = false;"
" return a;"
"}";
ASSERT_EQUALS("double f ( ) { return 0.0 ; }", tokenizeAndStringify(code2,true));
// Ticket #5485
const char code3[] = "void f() {"
" double a = 1e+007;\n"
" std::cout << a;\n"
"}";
ASSERT_EQUALS("void f ( ) {\nstd :: cout << 1e+007 ;\n}", tokenizeAndStringify(code3,true));
const char code4[] = "void f() {"
" double a = 1;\n"
" std::cout << a;\n"
"}";
ASSERT_EQUALS("void f ( ) {\nstd :: cout << 1.0 ;\n}", tokenizeAndStringify(code4,true));
}
void simplifyKnownVariablesFunctionCalls() { void simplifyKnownVariablesFunctionCalls() {
{
const char code[] = "void a(int x);" // <- x is passed by value
"void b() {"
" int x = 123;"
" a(x);" // <- replace with a(123);
"}";
const char expected[] = "void a ( int x ) ; void b ( ) { a ( 123 ) ; }";
ASSERT_EQUALS(expected, tokenizeAndStringify(code,true));
}
{ {
const char code[] = "void a(int &x);" // <- x is passed by reference const char code[] = "void a(int &x);" // <- x is passed by reference
"void b() {" "void b() {"
@ -6808,14 +6450,6 @@ private:
ASSERT_EQUALS("static int x ; void f ( ) { x = 123 ; while ( ! x ) { dostuff ( ) ; } }", tokenizeAndStringify(code,true)); ASSERT_EQUALS("static int x ; void f ( ) { x = 123 ; while ( ! x ) { dostuff ( ) ; } }", tokenizeAndStringify(code,true));
} }
void simplifyKnownVariablesReturn() {
const char code[] = "int a() {"
" int x = 123;"
" return (x);"
"}";
ASSERT_EQUALS("int a ( ) { return 123 ; }", tokenizeAndStringify(code,true));
}
void simplifyKnownVariablesPointerAliasFunctionCall() { // #7440 void simplifyKnownVariablesPointerAliasFunctionCall() { // #7440
const char code[] = "int main() {\n" const char code[] = "int main() {\n"
" char* data = new char[100];\n" " char* data = new char[100];\n"
@ -7022,25 +6656,6 @@ private:
} }
void removeRedundantAssignment() {
ASSERT_EQUALS("void f ( ) { }", tok("void f() { int *p, *q; p = q; }"));
ASSERT_EQUALS("void f ( ) { }", tok("void f() { int *p = 0, *q; p = q; }"));
ASSERT_EQUALS("int f ( int * x ) { return * x ; }", tok("int f(int *x) { return *x; }"));
}
void simplify_constants() {
const char code[] =
"void f() {\n"
"const int a = 45;\n"
"if( a )\n"
"{ int b = a; }\n"
"}\n"
"void g() {\n"
"int a = 2;\n"
"}";
ASSERT_EQUALS("void f ( ) { } void g ( ) { }", tok(code));
}
void simplify_constants2() { void simplify_constants2() {
const char code[] = const char code[] =
"void f( Foo &foo, Foo *foo2 ) {\n" "void f( Foo &foo, Foo *foo2 ) {\n"