2008-12-18 22:28:57 +01:00
|
|
|
/*
|
2009-01-21 21:04:20 +01:00
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
|
|
|
* Copyright (C) 2007-2009 Daniel Marjamäki, Reijo Tomperi, Nicolas Le Cam,
|
|
|
|
* Leandro Penz, Kimmo Varis
|
2008-12-18 22:28:57 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "testsuite.h"
|
2009-01-06 15:18:36 +01:00
|
|
|
#include "../src/tokenize.h"
|
2008-12-18 22:28:57 +01:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
extern std::ostringstream errout;
|
|
|
|
|
|
|
|
class TestSimplifyTokens : public TestFixture
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TestSimplifyTokens() : TestFixture("TestSimplifyTokens")
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void run()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
TEST_CASE(cast0);
|
|
|
|
TEST_CASE(sizeof1);
|
|
|
|
TEST_CASE(iftruefalse);
|
2009-02-08 09:52:03 +01:00
|
|
|
TEST_CASE(combine_strings);
|
2009-02-14 19:44:50 +01:00
|
|
|
TEST_CASE(double_plus);
|
2009-02-14 21:56:08 +01:00
|
|
|
TEST_CASE(redundant_plus);
|
2009-02-15 14:28:54 +01:00
|
|
|
TEST_CASE(parantheses1);
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string tok(const char code[])
|
|
|
|
{
|
|
|
|
std::istringstream istr(code);
|
|
|
|
Tokenizer tokenizer;
|
2009-01-05 16:49:57 +01:00
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
2008-12-21 16:10:44 +01:00
|
|
|
tokenizer.setVarId();
|
2008-12-18 22:28:57 +01:00
|
|
|
tokenizer.simplifyTokenList();
|
|
|
|
std::string ret;
|
2009-01-05 16:49:57 +01:00
|
|
|
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
ret += tok->str() + " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cast0()
|
|
|
|
{
|
|
|
|
const char code1[] = " if ( p == (char *)0 ) ";
|
|
|
|
const char code2[] = " if ( p == 0 ) ";
|
2009-01-05 16:49:57 +01:00
|
|
|
ASSERT_EQUALS(tok(code1), tok(code2));
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void sizeof1()
|
|
|
|
{
|
|
|
|
const char code1[] = " struct ABC *abc = malloc(sizeof(*abc)); ";
|
|
|
|
const char code2[] = " struct ABC *abc = malloc(100); ";
|
2009-01-27 21:47:00 +01:00
|
|
|
const char code3[] = " struct ABC *abc = malloc(sizeof *abc ); ";
|
2009-01-05 16:49:57 +01:00
|
|
|
ASSERT_EQUALS(tok(code1), tok(code2));
|
2009-01-27 21:47:00 +01:00
|
|
|
ASSERT_EQUALS(tok(code2), tok(code3));
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
2008-12-21 16:10:44 +01:00
|
|
|
|
|
|
|
void iftruefalse()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
const char code1[] = " void f() { int a; bool use = false; if( use ) { a=0; } else {a=1;} } ";
|
2008-12-21 22:04:58 +01:00
|
|
|
const char code2[] = " void f() { int a; bool use = false; {a=1;} } ";
|
2009-01-05 16:49:57 +01:00
|
|
|
ASSERT_EQUALS(tok(code2), tok(code1));
|
2008-12-21 16:10:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const char code1[] = " void f() { int a; bool use = true; if( use ) { a=0; } else {a=1;} } ";
|
2008-12-21 22:04:58 +01:00
|
|
|
const char code2[] = " void f() { int a; bool use = true; { a=0; } } ";
|
2009-01-05 16:49:57 +01:00
|
|
|
ASSERT_EQUALS(tok(code2), tok(code1));
|
2008-12-21 16:10:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const char code1[] = " void f() { int a; int use = 5; if( use ) { a=0; } else {a=1;} } ";
|
2008-12-21 22:04:58 +01:00
|
|
|
const char code2[] = " void f() { int a; int use = 5; { a=0; } } ";
|
2009-01-05 16:49:57 +01:00
|
|
|
ASSERT_EQUALS(tok(code2), tok(code1));
|
2008-12-21 16:10:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const char code1[] = " void f() { int a; int use = 0; if( use ) { a=0; } else {a=1;} } ";
|
2008-12-21 22:04:58 +01:00
|
|
|
const char code2[] = " void f() { int a; int use = 0; {a=1;} } ";
|
2009-01-05 16:49:57 +01:00
|
|
|
ASSERT_EQUALS(tok(code2), tok(code1));
|
2008-12-21 16:10:44 +01:00
|
|
|
}
|
2008-12-21 22:04:58 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
const char code1[] = " void f() { int a; bool use = false; if( use ) a=0; else a=1; int c=1; } ";
|
2008-12-23 09:11:33 +01:00
|
|
|
const char code2[] = " void f() { int a; bool use = false; { a=1; } int c=1; } ";
|
2009-01-05 16:49:57 +01:00
|
|
|
ASSERT_EQUALS(tok(code2), tok(code1));
|
2008-12-21 22:04:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const char code1[] = " void f() { int a; bool use = true; if( use ) a=0; else a=1; int c=1; } ";
|
2008-12-22 21:49:56 +01:00
|
|
|
const char code2[] = " void f() { int a; bool use = true; { a=0; } int c=1; } ";
|
2009-01-05 16:49:57 +01:00
|
|
|
ASSERT_EQUALS(tok(code2), tok(code1));
|
2008-12-21 22:04:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const char code1[] = " void f() { int a; bool use = false; if( use ) a=0; else if( bb ) a=1; int c=1; } ";
|
|
|
|
const char code2[] = " void f() { int a; bool use = false; if( bb ) a=1; int c=1; } ";
|
2009-01-05 16:49:57 +01:00
|
|
|
ASSERT_EQUALS(tok(code2), tok(code1));
|
2008-12-21 22:04:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const char code1[] = " void f() { int a; bool use = true; if( use ) a=0; else if( bb ) a=1; int c=1; } ";
|
2008-12-26 13:55:53 +01:00
|
|
|
const char code2[] = " void f() { int a; bool use = true; { a=0;} int c=1; } ";
|
2009-01-05 16:49:57 +01:00
|
|
|
ASSERT_EQUALS(tok(code2), tok(code1));
|
2008-12-21 22:04:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
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; } ";
|
2008-12-26 13:55:53 +01:00
|
|
|
const char code2[] = " void f() { int a; bool use = true; { a=0; }int c=1; } ";
|
2009-01-05 16:49:57 +01:00
|
|
|
ASSERT_EQUALS(tok(code2), tok(code1));
|
2008-12-26 13:55:53 +01:00
|
|
|
}
|
2008-12-26 16:42:04 +01:00
|
|
|
|
2008-12-26 15:43:16 +01:00
|
|
|
{
|
2008-12-26 16:42:04 +01:00
|
|
|
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; } } ";
|
2009-01-05 16:49:57 +01:00
|
|
|
ASSERT_EQUALS(tok(code2), tok(code1));
|
2008-12-26 15:43:16 +01:00
|
|
|
}
|
2008-12-26 16:46:59 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
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; } } ";
|
2009-01-05 16:49:57 +01:00
|
|
|
ASSERT_EQUALS(tok(code2), tok(code1));
|
2008-12-26 16:46:59 +01:00
|
|
|
}
|
2008-12-21 16:10:44 +01:00
|
|
|
}
|
2009-02-08 09:52:03 +01:00
|
|
|
|
|
|
|
void combine_strings()
|
|
|
|
{
|
|
|
|
const char code1[] = "void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
"const char *a =\n"
|
|
|
|
"{\n"
|
|
|
|
"\"hello \"\n"
|
|
|
|
"\"world\"\n"
|
|
|
|
"};\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
const char code2[] = "void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
"const char *a =\n"
|
|
|
|
"{\n"
|
|
|
|
"\"hello world\"\n"
|
|
|
|
"};\n"
|
|
|
|
"}\n";
|
|
|
|
ASSERT_EQUALS(tok(code2), tok(code1));
|
|
|
|
}
|
2009-02-12 21:32:59 +01:00
|
|
|
|
|
|
|
void double_plus()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
const char code1[] = "void foo( int a )\n"
|
|
|
|
"{\n"
|
|
|
|
"a++;\n"
|
|
|
|
"a--;\n"
|
|
|
|
"++a;\n"
|
|
|
|
"--a;\n"
|
|
|
|
"}\n";
|
|
|
|
ASSERT_EQUALS("void foo ( int a ) { a ++ ; a -- ; ++ a ; -- a ; } ", tok(code1));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const char code1[] = "void foo( int a )\n"
|
|
|
|
"{\n"
|
|
|
|
"a=a+a;\n"
|
|
|
|
"}\n";
|
|
|
|
ASSERT_EQUALS("void foo ( int a ) { a = a + a ; } ", tok(code1));
|
|
|
|
}
|
2009-02-12 21:49:05 +01:00
|
|
|
{
|
|
|
|
const char code1[] = "void foo( int a, int b )\n"
|
|
|
|
"{\n"
|
|
|
|
"a=a+++b;\n"
|
|
|
|
"}\n";
|
2009-02-14 19:44:50 +01:00
|
|
|
ASSERT_EQUALS("void foo ( int a , int b ) { a = a ++ + b ; } ", tok(code1));
|
2009-02-12 21:49:05 +01:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const char code1[] = "void foo( int a, int b )\n"
|
|
|
|
"{\n"
|
|
|
|
"a=a---b;\n"
|
|
|
|
"}\n";
|
2009-02-14 19:44:50 +01:00
|
|
|
ASSERT_EQUALS("void foo ( int a , int b ) { a = a -- - b ; } ", tok(code1));
|
2009-02-12 21:49:05 +01:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const char code1[] = "void foo( int a, int b )\n"
|
|
|
|
"{\n"
|
|
|
|
"a=a--+b;\n"
|
|
|
|
"}\n";
|
2009-02-14 19:44:50 +01:00
|
|
|
ASSERT_EQUALS("void foo ( int a , int b ) { a = a -- + b ; } ", tok(code1));
|
2009-02-12 21:49:05 +01:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const char code1[] = "void foo( int a, int b )\n"
|
|
|
|
"{\n"
|
|
|
|
"a=a++-b;\n"
|
|
|
|
"}\n";
|
2009-02-14 19:44:50 +01:00
|
|
|
ASSERT_EQUALS("void foo ( int a , int b ) { a = a ++ - b ; } ", tok(code1));
|
2009-02-12 21:49:05 +01:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const char code1[] = "void foo( int a, int b )\n"
|
|
|
|
"{\n"
|
|
|
|
"a=a+--b;\n"
|
|
|
|
"}\n";
|
2009-02-14 19:44:50 +01:00
|
|
|
ASSERT_EQUALS("void foo ( int a , int b ) { a = a + -- b ; } ", tok(code1));
|
2009-02-12 21:49:05 +01:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const char code1[] = "void foo( int a, int b )\n"
|
|
|
|
"{\n"
|
|
|
|
"a=a-++b;\n"
|
|
|
|
"}\n";
|
2009-02-14 19:44:50 +01:00
|
|
|
ASSERT_EQUALS("void foo ( int a , int b ) { a = a - ++ b ; } ", tok(code1));
|
2009-02-12 21:49:05 +01:00
|
|
|
}
|
2009-02-12 21:32:59 +01:00
|
|
|
}
|
2009-02-14 21:56:08 +01:00
|
|
|
|
|
|
|
void redundant_plus()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
const char code1[] = "void foo( int a, int b )\n"
|
|
|
|
"{\n"
|
|
|
|
"a=a + + b;\n"
|
|
|
|
"}\n";
|
|
|
|
ASSERT_EQUALS("void foo ( int a , int b ) { a = a + b ; } ", tok(code1));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const char code1[] = "void foo( int a, int b )\n"
|
|
|
|
"{\n"
|
|
|
|
"a=a + + + b;\n"
|
|
|
|
"}\n";
|
|
|
|
ASSERT_EQUALS("void foo ( int a , int b ) { a = a + b ; } ", tok(code1));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const char code1[] = "void foo( int a, int b )\n"
|
|
|
|
"{\n"
|
|
|
|
"a=a + - b;\n"
|
|
|
|
"}\n";
|
|
|
|
ASSERT_EQUALS("void foo ( int a , int b ) { a = a - b ; } ", tok(code1));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const char code1[] = "void foo( int a, int b )\n"
|
|
|
|
"{\n"
|
|
|
|
"a=a - + b;\n"
|
|
|
|
"}\n";
|
|
|
|
ASSERT_EQUALS("void foo ( int a , int b ) { a = a - b ; } ", tok(code1));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const char code1[] = "void foo( int a, int b )\n"
|
|
|
|
"{\n"
|
|
|
|
"a=a - - b;\n"
|
|
|
|
"}\n";
|
|
|
|
ASSERT_EQUALS("void foo ( int a , int b ) { a = a + b ; } ", tok(code1));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const char code1[] = "void foo( int a, int b )\n"
|
|
|
|
"{\n"
|
|
|
|
"a=a - + - b;\n"
|
|
|
|
"}\n";
|
|
|
|
ASSERT_EQUALS("void foo ( int a , int b ) { a = a + b ; } ", tok(code1));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const char code1[] = "void foo( int a, int b )\n"
|
|
|
|
"{\n"
|
|
|
|
"a=a - - - b;\n"
|
|
|
|
"}\n";
|
|
|
|
ASSERT_EQUALS("void foo ( int a , int b ) { a = a - b ; } ", tok(code1));
|
|
|
|
}
|
|
|
|
}
|
2009-02-15 14:28:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
void parantheses1()
|
|
|
|
{
|
|
|
|
const char code1[] = "<= (10+100);";
|
|
|
|
ASSERT_EQUALS("<= 110 ; ", tok(code1));
|
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
};
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
REGISTER_TEST(TestSimplifyTokens)
|