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
2009-05-30 07:48:12 +02:00
* Copyright ( C ) 2007 - 2009 Daniel Marjamäki and Cppcheck team .
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
2009-09-27 17:08:31 +02:00
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
2008-12-18 22:28:57 +01:00
*/
# include "testsuite.h"
2009-08-04 21:32:14 +02:00
# define private public
2009-10-25 12:49:06 +01:00
# include "tokenize.h"
2009-08-04 21:32:14 +02:00
# undef private
2009-10-25 12:49:06 +01:00
# include "token.h"
2008-12-18 22:28:57 +01:00
# include <sstream>
extern std : : ostringstream errout ;
2009-02-17 20:18:26 +01:00
2008-12-18 22:28:57 +01:00
class TestSimplifyTokens : public TestFixture
{
public :
TestSimplifyTokens ( ) : TestFixture ( " TestSimplifyTokens " )
{ }
private :
void run ( )
{
2009-08-28 12:57:29 +02:00
TEST_CASE ( cast ) ;
2009-01-05 16:49:57 +01:00
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 ) ;
2009-02-27 07:07:38 +01:00
TEST_CASE ( paranthesesVar ) ; // Remove redundant parantheses around variable .. "( %var% )"
2009-02-28 21:40:37 +01:00
TEST_CASE ( declareVar ) ;
2009-02-17 20:18:26 +01:00
2009-06-10 19:36:00 +02:00
TEST_CASE ( declareArray ) ;
2009-05-25 14:21:58 +02:00
TEST_CASE ( dontRemoveIncrement ) ;
2009-05-25 08:26:11 +02:00
TEST_CASE ( removePostIncrement ) ;
2009-05-25 08:31:20 +02:00
TEST_CASE ( removePreIncrement ) ;
2009-05-25 08:26:11 +02:00
2009-02-17 20:18:26 +01:00
TEST_CASE ( elseif1 ) ;
2009-02-28 10:09:55 +01:00
TEST_CASE ( sizeof1 ) ;
TEST_CASE ( sizeof2 ) ;
TEST_CASE ( sizeof3 ) ;
TEST_CASE ( sizeof4 ) ;
TEST_CASE ( sizeof5 ) ;
TEST_CASE ( sizeof6 ) ;
2009-03-29 16:36:34 +02:00
TEST_CASE ( sizeof7 ) ;
2009-05-31 21:33:44 +02:00
TEST_CASE ( sizeof8 ) ;
2009-09-06 13:28:25 +02:00
TEST_CASE ( sizeof9 ) ;
2009-10-10 14:26:03 +02:00
TEST_CASE ( sizeof10 ) ;
2009-10-18 18:06:32 +02:00
TEST_CASE ( sizeof11 ) ;
2009-10-21 21:08:00 +02:00
TEST_CASE ( sizeof12 ) ;
2009-10-22 23:00:27 +02:00
TEST_CASE ( sizeof13 ) ;
2009-11-13 20:07:44 +01:00
TEST_CASE ( sizeof14 ) ;
2009-03-05 20:32:02 +01:00
TEST_CASE ( casting ) ;
2009-03-11 19:14:45 +01:00
2009-11-07 08:25:39 +01:00
TEST_CASE ( strlen1 ) ;
2009-03-11 18:50:24 +01:00
TEST_CASE ( template1 ) ;
2009-03-12 22:17:42 +01:00
TEST_CASE ( template2 ) ;
2009-03-14 21:26:32 +01:00
TEST_CASE ( template3 ) ;
2009-03-15 17:07:05 +01:00
TEST_CASE ( template4 ) ;
2009-03-15 20:03:29 +01:00
TEST_CASE ( template5 ) ;
2009-05-05 20:16:57 +02:00
TEST_CASE ( template6 ) ;
2009-05-05 22:09:15 +02:00
TEST_CASE ( template7 ) ;
2009-05-08 16:19:22 +02:00
TEST_CASE ( template8 ) ;
2009-05-09 08:02:59 +02:00
TEST_CASE ( template9 ) ;
2009-06-14 11:28:25 +02:00
TEST_CASE ( template10 ) ;
2009-06-14 14:57:47 +02:00
TEST_CASE ( template11 ) ;
2009-07-22 13:29:42 +02:00
TEST_CASE ( template12 ) ;
2009-09-14 21:49:41 +02:00
TEST_CASE ( template13 ) ;
2009-10-31 12:34:52 +01:00
TEST_CASE ( template14 ) ;
2009-11-01 15:56:00 +01:00
TEST_CASE ( template15 ) ;
2009-11-01 19:03:52 +01:00
TEST_CASE ( template16 ) ;
2009-09-02 22:54:50 +02:00
TEST_CASE ( template_default_parameter ) ;
2009-09-03 22:19:44 +02:00
TEST_CASE ( template_typename ) ;
2009-03-14 21:26:32 +01:00
2009-03-13 00:07:05 +01:00
TEST_CASE ( namespaces ) ;
2009-03-23 18:20:56 +01:00
// Assignment in condition..
TEST_CASE ( ifassign1 ) ;
2009-06-20 13:58:30 +02:00
TEST_CASE ( whileAssign ) ;
2009-03-24 18:23:21 +01:00
// "if(0==x)" => "if(!x)"
TEST_CASE ( ifnot ) ;
2009-04-05 21:21:38 +02:00
TEST_CASE ( combine_wstrings ) ;
2009-05-31 10:42:27 +02:00
// Simplify "not" to "!" (#345)
TEST_CASE ( not1 ) ;
2009-08-29 14:26:01 +02:00
// Simplify "and" to "&&" (#620)
TEST_CASE ( and1 ) ;
2009-06-10 23:12:26 +02:00
TEST_CASE ( comma_keyword ) ;
2009-07-04 17:06:25 +02:00
TEST_CASE ( remove_comma ) ;
2009-07-14 12:06:38 +02:00
// Simplify "?:"
TEST_CASE ( conditionOperator ) ;
2009-08-02 10:45:33 +02:00
// Simplify calculations
TEST_CASE ( calculations ) ;
2009-08-24 23:10:12 +02:00
// Simplify goto..
TEST_CASE ( goto1 ) ;
2009-09-05 18:46:27 +02:00
// Simplify nested strcat() calls
TEST_CASE ( strcat1 ) ;
2009-09-05 21:21:25 +02:00
// Syntax error
TEST_CASE ( argumentsWithSameName )
2009-09-12 22:54:47 +02:00
TEST_CASE ( simplifyAtol )
2009-09-27 16:04:10 +02:00
TEST_CASE ( simplifyHexInString )
2009-09-30 09:11:09 +02:00
TEST_CASE ( simplifyTypedef )
TEST_CASE ( simplifyTypedef2 )
TEST_CASE ( simplifyTypedef3 )
2009-09-30 13:35:00 +02:00
TEST_CASE ( simplifyTypedef4 )
2009-10-03 17:02:23 +02:00
TEST_CASE ( simplifyTypedef5 )
2009-10-04 22:33:41 +02:00
TEST_CASE ( reverseArraySyntax )
2009-10-06 11:40:50 +02:00
TEST_CASE ( simplify_numeric_condition ) ;
2009-10-17 18:21:13 +02:00
TEST_CASE ( pointeralias ) ;
2009-11-05 21:30:05 +01:00
TEST_CASE ( reduceConstness ) ;
2008-12-18 22:28:57 +01:00
}
2009-09-30 13:35:00 +02:00
std : : string tok ( const char code [ ] , bool simplify = true )
2008-12-18 22:28:57 +01:00
{
std : : istringstream istr ( code ) ;
Tokenizer tokenizer ;
2009-01-05 16:49:57 +01:00
tokenizer . tokenize ( istr , " test.cpp " ) ;
2009-09-30 13:35:00 +02:00
if ( simplify )
tokenizer . simplifyTokenList ( ) ;
2009-09-30 20:42:14 +02:00
tokenizer . validate ( ) ;
2008-12-18 22:28:57 +01:00
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
{
2009-07-14 11:37:26 +02:00
if ( tok ! = tokenizer . tokens ( ) )
ret + = " " ;
ret + = tok - > str ( ) ;
2008-12-18 22:28:57 +01:00
}
return ret ;
}
2009-08-28 12:57:29 +02:00
void cast ( )
2008-12-18 22:28:57 +01:00
{
2009-08-28 17:05:25 +02:00
ASSERT_EQUALS ( " if ( ! p ) " , tok ( " if (p == (char *)0) " ) ) ;
2009-08-28 12:57:29 +02:00
ASSERT_EQUALS ( " return str ; " , tok ( " return (char *)str; " ) ) ;
2008-12-18 22:28:57 +01:00
2009-08-28 12:57:29 +02:00
{
const char code [ ] = " static void crash() \n "
" { \n "
" goto err_exit; \n "
" err_exit: \n "
" (void)foo(); \n "
" } \n " ;
const char expected [ ] = " static void crash ( ) "
2009-08-28 17:05:25 +02:00
" { foo ( ) ; return ; foo ( ) ; } " ;
2009-08-28 12:57:29 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2009-10-04 23:12:12 +02:00
ASSERT_EQUALS ( " if ( * a ) " , tok ( " if ((char)*a) " ) ) ;
ASSERT_EQUALS ( " if ( & a ) " , tok ( " if ((int)&a) " ) ) ;
ASSERT_EQUALS ( " if ( * a ) " , tok ( " if ((unsigned int)(unsigned char)*a) " ) ) ;
2009-10-29 20:54:51 +01:00
ASSERT_EQUALS ( " class A { A operator * ( int ) ; } ; " , tok ( " class A { A operator *(int); }; " ) ) ;
ASSERT_EQUALS ( " class A { A operator * ( int ) const ; } ; " , tok ( " class A { A operator *(int) const; }; " ) ) ;
2009-03-09 19:47:21 +01:00
}
2009-08-28 12:57:29 +02:00
2008-12-21 16:10:44 +01:00
void iftruefalse ( )
{
{
2009-07-14 11:37:26 +02:00
const char code1 [ ] = " void f() { int a; bool use = false; if( use ) { a=0; } else {a=1;} } " ;
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
}
{
2009-07-14 11:37:26 +02:00
const char code1 [ ] = " void f() { int a; bool use = true; if( use ) { a=0; } else {a=1;} } " ;
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
}
{
2009-07-14 11:37:26 +02:00
const char code1 [ ] = " void f() { int a; int use = 5; if( use ) { a=0; } else {a=1;} } " ;
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
}
{
2009-07-14 11:37:26 +02:00
const char code1 [ ] = " void f() { int a; int use = 0; if( use ) { a=0; } else {a=1;} } " ;
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
{
2009-07-14 11:37:26 +02:00
const char code1 [ ] = " void f() { int a; bool use = false; if( use ) a=0; else a=1; int c=1; } " ;
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
}
{
2009-07-14 11:37:26 +02:00
const char code1 [ ] = " void f() { int a; bool use = true; if( use ) a=0; else a=1; int c=1; } " ;
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
}
{
2009-07-14 11:37:26 +02: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 ; use = false ; { if ( bb ) { a = 1 ; } } int c ; c = 1 ; } " ;
2009-01-05 16:49:57 +01:00
ASSERT_EQUALS ( tok ( code2 ) , tok ( code1 ) ) ;
2008-12-21 22:04:58 +01:00
}
{
2009-07-14 11:37:26 +02:00
const char code1 [ ] = " void f() { int a; bool use = true; if( use ) a=0; else if( bb ) a=1; int c=1; } " ;
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
}
{
2009-07-14 11:37:26 +02: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; } " ;
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
{
2009-07-14 11:37:26 +02: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
{
2009-07-14 11:37:26 +02: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 "
2009-04-05 21:21:38 +02:00
" } \n " ;
ASSERT_EQUALS ( tok ( code2 ) , tok ( code1 ) ) ;
}
void combine_wstrings ( )
{
const char code1 [ ] = " void foo() \n "
" { \n "
" const wchar_t *a = \n "
" { \n "
" L \" hello \" \n "
" L \" world \" \n "
" }; \n "
" } \n " ;
const char code2 [ ] = " void foo() \n "
" { \n "
" const wchar_t *a = \n "
" { \n "
" \" hello world \" \n "
" }; \n "
2009-02-08 09:52:03 +01:00
" } \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 " ;
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " void foo ( int a ) { a ++ ; a -- ; ++ a ; -- a ; } " , tok ( code1 ) ) ;
2009-02-12 21:32:59 +01:00
}
{
const char code1 [ ] = " void foo( int a ) \n "
" { \n "
" a=a+a; \n "
" } \n " ;
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " void foo ( int a ) { a = a + a ; } " , tok ( code1 ) ) ;
2009-02-12 21:32:59 +01:00
}
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-07-14 11:37:26 +02: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-07-14 11:37:26 +02: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-07-14 11:37:26 +02: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-07-14 11:37:26 +02: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-07-14 11:37:26 +02: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-07-14 11:37:26 +02: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 " ;
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " void foo ( int a , int b ) { a = a + b ; } " , tok ( code1 ) ) ;
2009-02-14 21:56:08 +01:00
}
{
const char code1 [ ] = " void foo( int a, int b ) \n "
" { \n "
" a=a + + + b; \n "
" } \n " ;
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " void foo ( int a , int b ) { a = a + b ; } " , tok ( code1 ) ) ;
2009-02-14 21:56:08 +01:00
}
{
const char code1 [ ] = " void foo( int a, int b ) \n "
" { \n "
" a=a + - b; \n "
" } \n " ;
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " void foo ( int a , int b ) { a = a - b ; } " , tok ( code1 ) ) ;
2009-02-14 21:56:08 +01:00
}
{
const char code1 [ ] = " void foo( int a, int b ) \n "
" { \n "
" a=a - + b; \n "
" } \n " ;
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " void foo ( int a , int b ) { a = a - b ; } " , tok ( code1 ) ) ;
2009-02-14 21:56:08 +01:00
}
{
const char code1 [ ] = " void foo( int a, int b ) \n "
" { \n "
" a=a - - b; \n "
" } \n " ;
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " void foo ( int a , int b ) { a = a + b ; } " , tok ( code1 ) ) ;
2009-02-14 21:56:08 +01:00
}
{
const char code1 [ ] = " void foo( int a, int b ) \n "
" { \n "
" a=a - + - b; \n "
" } \n " ;
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " void foo ( int a , int b ) { a = a + b ; } " , tok ( code1 ) ) ;
2009-02-14 21:56:08 +01:00
}
{
const char code1 [ ] = " void foo( int a, int b ) \n "
" { \n "
" a=a - - - b; \n "
" } \n " ;
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " void foo ( int a , int b ) { a = a - b ; } " , tok ( code1 ) ) ;
2009-02-14 21:56:08 +01:00
}
}
2009-02-15 14:28:54 +01:00
void parantheses1 ( )
{
2009-10-05 16:34:38 +02:00
ASSERT_EQUALS ( " <= 110 ; " , tok ( " <= (10+100); " ) ) ;
2009-02-15 14:28:54 +01:00
}
2009-02-17 20:18:26 +01:00
2009-02-27 07:07:38 +01:00
void paranthesesVar ( )
2009-02-25 20:55:24 +01:00
{
2009-02-27 07:07:38 +01:00
// remove parantheses..
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " = p ; " , tok ( " = (p); " ) ) ;
ASSERT_EQUALS ( " if ( a < p ) { } " , tok ( " if(a<(p)){} " ) ) ;
ASSERT_EQUALS ( " void f ( ) { int p ; if ( p == -1 ) { } } " , tok ( " void f(){int p; if((p)==-1){}} " ) ) ;
ASSERT_EQUALS ( " void f ( ) { int p ; if ( -1 == p ) { } } " , tok ( " void f(){int p; if(-1==(p)){}} " ) ) ;
ASSERT_EQUALS ( " void f ( ) { int p ; if ( p ) { } } " , tok ( " void f(){int p; if((p)){}} " ) ) ;
2009-07-24 21:55:35 +02:00
ASSERT_EQUALS ( " return p ; " , tok ( " return (p); " ) ) ;
2009-10-05 13:10:09 +02:00
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) {}} " ) ) ;
2009-10-05 13:22:35 +02:00
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 ; 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;} " ) ) ;
2009-10-05 13:30:05 +02:00
ASSERT_EQUALS ( " void f ( ) { int p ; if ( p ) { } p = 1 ; } " , tok ( " void f(){int p; if ( p ) { } (p) = 1;} " ) ) ;
2009-02-27 07:07:38 +01:00
// keep parantheses..
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " = a ; " , tok ( " = (char)a; " ) ) ;
ASSERT_EQUALS ( " cast < char * > ( p ) " , tok ( " cast<char *>(p) " ) ) ;
2009-07-24 21:55:35 +02:00
ASSERT_EQUALS ( " return ( a + b ) * c ; " , tok ( " return (a+b)*c; " ) ) ;
2009-10-05 13:10:09 +02:00
ASSERT_EQUALS ( " void f ( ) { int p ; if ( 2 * p == 0 ) { } } " , tok ( " void f(){int p; if (2*p == 0) {}} " ) ) ;
ASSERT_EQUALS ( " void f ( ) { DIR * f ; f = opendir ( dirname ) ; if ( closedir ( f ) ) { } } " , tok ( " void f(){DIR * f = opendir(dirname);if (closedir(f)){}} " ) ) ;
2009-11-04 23:58:15 +01:00
ASSERT_EQUALS ( " void foo ( int p ) { if ( 0 <= p ) { ; } } " , tok ( " void foo(int p){if((p)>=0);} " ) ) ;
2009-02-25 20:55:24 +01:00
}
2009-02-28 21:40:37 +01:00
void declareVar ( )
{
2009-07-14 11:37:26 +02:00
const char code [ ] = " void f ( ) { char str [ 100 ] = \" 100 \" ; } " ;
2009-02-28 21:40:37 +01:00
ASSERT_EQUALS ( code , tok ( code ) ) ;
}
2009-02-17 20:18:26 +01:00
2009-06-10 19:36:00 +02:00
void declareArray ( )
{
2009-07-14 11:37:26 +02:00
const char code [ ] = " void f ( ) { char str [ ] = \" 100 \" ; } " ;
const char expected [ ] = " void f ( ) { char * str ; str = \" 100 \" ; } " ;
2009-06-10 19:36:00 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2009-05-25 08:26:11 +02:00
2009-05-25 14:21:58 +02:00
void dontRemoveIncrement ( )
{
{
const char code [ ] = " void f(int a) \n "
" { \n "
" if (a > 10) \n "
" a = 5; \n "
" else \n "
" a = 10; \n "
" a++; \n "
" } \n " ;
2009-10-04 07:51:12 +02:00
ASSERT_EQUALS ( " void f ( int a ) { if ( 10 < a ) { a = 5 ; } else { a = 10 ; } a ++ ; } " , tok ( code ) ) ;
2009-05-25 14:21:58 +02:00
}
{
const char code [ ] = " void f(int a) \n "
" { \n "
" if (a > 10) \n "
" a = 5; \n "
" else \n "
" a = 10; \n "
" ++a; \n "
" } \n " ;
2009-10-04 07:51:12 +02:00
ASSERT_EQUALS ( " void f ( int a ) { if ( 10 < a ) { a = 5 ; } else { a = 10 ; } ++ a ; } " , tok ( code ) ) ;
2009-05-25 14:21:58 +02:00
}
}
2009-05-25 08:26:11 +02:00
void removePostIncrement ( )
{
const char code [ ] = " void f() \n "
" { \n "
" unsigned int c = 0; \n "
" c++; \n "
" if (c>0) { c++; } \n "
" c++; \n "
" } \n " ;
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " void f ( ) { int c ; c = 3 ; ; { ; } ; } " , tok ( code ) ) ;
2009-05-25 08:26:11 +02:00
}
2009-05-25 08:31:20 +02:00
void removePreIncrement ( )
{
const char code [ ] = " void f() \n "
" { \n "
" unsigned int c = 0; \n "
" ++c; \n "
" if (c>0) { ++c; } \n "
" ++c; \n "
" } \n " ;
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " void f ( ) { int c ; c = 3 ; ; { ; } ; } " , tok ( code ) ) ;
2009-05-25 08:31:20 +02:00
}
2009-02-17 20:18:26 +01:00
std : : string elseif ( const char code [ ] )
{
std : : istringstream istr ( code ) ;
2009-08-04 21:32:14 +02:00
Tokenizer tokenizer ;
tokenizer . createTokens ( istr ) ;
tokenizer . elseif ( ) ;
2009-02-17 20:18:26 +01:00
return tokenizer . tokens ( ) - > stringifyList ( false ) ;
}
void elseif1 ( )
{
const char code [ ] = " else if(ab) { cd } else { ef }gh " ;
2009-03-03 21:17:23 +01:00
ASSERT_EQUALS ( " \n \n ##file 0 \n 1: else { if ( ab ) { cd } else { ef } } gh \n " , elseif ( code ) ) ;
2009-02-17 20:18:26 +01:00
}
2009-02-28 10:09:55 +01:00
// Simplify 'sizeof'..
std : : string sizeof_ ( const char code [ ] )
{
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . setVarId ( ) ;
tokenizer . simplifyTokenList ( ) ;
std : : ostringstream ostr ;
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) )
2009-09-13 12:59:59 +02:00
{
if ( tok - > previous ( ) )
{
ostr < < " " ;
}
ostr < < tok - > str ( ) ;
}
2009-02-28 10:09:55 +01:00
return ostr . str ( ) ;
}
2009-10-27 19:23:45 +01:00
unsigned int sizeofFromTokenizer ( const char type [ ] )
{
Tokenizer tokenizer ;
std : : istringstream istr ( " " ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyTokenList ( ) ;
Token tok ;
tok . str ( type ) ;
return tokenizer . sizeOfType ( & tok ) ;
}
2009-02-28 10:09:55 +01:00
void sizeof1 ( )
{
2009-10-21 21:08:00 +02:00
ASSERT_EQUALS ( " struct ABC * abc ; abc = malloc ( 100 ) ; " , tok ( " struct ABC *abc = malloc(sizeof(*abc)); " ) ) ;
ASSERT_EQUALS ( " struct ABC * abc ; abc = malloc ( 100 ) ; " , tok ( " struct ABC *abc = malloc(sizeof *abc ); " ) ) ;
2009-02-28 10:09:55 +01:00
}
void sizeof2 ( )
{
const char code [ ] = " void foo() \n "
" { \n "
" int i[4]; \n "
" sizeof(i); \n "
" sizeof(*i); \n "
" } \n " ;
2009-09-13 12:59:59 +02:00
ASSERT_EQUALS ( " void foo ( ) { int i [ 4 ] ; 16 ; 4 ; } " , sizeof_ ( code ) ) ;
2009-02-28 10:09:55 +01:00
}
void sizeof3 ( )
{
const char code [ ] = " static int i[4]; \n "
" void f() \n "
" { \n "
" int i[10]; \n "
" sizeof(i); \n "
" } \n " ;
2009-09-13 12:59:59 +02:00
ASSERT_EQUALS ( " static int i [ 4 ] ; void f ( ) { int i [ 10 ] ; 40 ; } " , sizeof_ ( code ) ) ;
2009-02-28 10:09:55 +01:00
}
void sizeof4 ( )
{
2009-09-20 22:38:32 +02:00
{
const char code [ ] = " int i[10]; \n "
" sizeof(i[0]); \n " ;
ASSERT_EQUALS ( " int i [ 10 ] ; 4 ; " , sizeof_ ( code ) ) ;
}
{
const char code [ ] = " int i[10]; \n "
" sizeof i[0]; \n " ;
ASSERT_EQUALS ( " int i [ 10 ] ; 4 ; " , sizeof_ ( code ) ) ;
}
2009-02-28 10:09:55 +01:00
}
void sizeof5 ( )
{
const char code [ ] =
2009-10-21 21:08:00 +02:00
" const char * names[2]; "
" for (int i = 0; i < sizeof(names[0]); i++) "
2009-02-28 10:09:55 +01:00
" {} " ;
2009-10-27 19:23:45 +01:00
std : : ostringstream expected ;
expected < < " const char * names [ 2 ] ; for ( int i = 0 ; i < " < < sizeofFromTokenizer ( " * " ) < < " ; i ++ ) { } " ;
ASSERT_EQUALS ( expected . str ( ) , sizeof_ ( code ) ) ;
2009-02-28 10:09:55 +01:00
}
void sizeof6 ( )
{
const char code [ ] = " ;int i; \n "
" sizeof(i); \n " ;
std : : ostringstream expected ;
2009-09-13 12:59:59 +02:00
expected < < " ; int i ; " < < sizeof ( int ) < < " ; " ;
2009-02-28 10:09:55 +01:00
ASSERT_EQUALS ( expected . str ( ) , sizeof_ ( code ) ) ;
}
2009-03-29 16:36:34 +02:00
void sizeof7 ( )
{
const char code [ ] = " ;INT32 i[10]; \n "
" sizeof(i[0]); \n " ;
2009-09-13 12:59:59 +02:00
ASSERT_EQUALS ( " ; INT32 i [ 10 ] ; sizeof ( i [ 0 ] ) ; " , sizeof_ ( code ) ) ;
2009-03-29 16:36:34 +02:00
}
2009-05-31 21:33:44 +02:00
void sizeof8 ( )
{
{
const char code [ ] = " void f() \n "
" { \n "
" char* ptrs[2]; \n "
" int a = sizeof( ptrs ); \n "
" } \n " ;
std : : ostringstream oss ;
2009-10-27 19:23:45 +01:00
oss < < ( sizeofFromTokenizer ( " * " ) * 2 ) ;
2009-09-13 12:59:59 +02:00
ASSERT_EQUALS ( " void f ( ) { char * ptrs [ 2 ] ; int a ; a = " + oss . str ( ) + " ; } " , sizeof_ ( code ) ) ;
2009-05-31 21:33:44 +02:00
}
{
const char code [ ] = " void f() \n "
" { \n "
" char* ptrs[55]; \n "
" int a = sizeof( ptrs ); \n "
" } \n " ;
std : : ostringstream oss ;
2009-10-27 19:23:45 +01:00
oss < < ( sizeofFromTokenizer ( " * " ) * 55 ) ;
2009-09-13 12:59:59 +02:00
ASSERT_EQUALS ( " void f ( ) { char * ptrs [ 55 ] ; int a ; a = " + oss . str ( ) + " ; } " , sizeof_ ( code ) ) ;
2009-05-31 21:33:44 +02:00
}
{
const char code [ ] = " void f() \n "
" { \n "
" char* ptrs; \n "
" int a = sizeof( ptrs ); \n "
" } \n " ;
std : : ostringstream oss ;
2009-10-27 19:23:45 +01:00
oss < < sizeofFromTokenizer ( " * " ) ;
2009-09-13 12:59:59 +02:00
ASSERT_EQUALS ( " void f ( ) { char * ptrs ; int a ; a = " + oss . str ( ) + " ; } " , sizeof_ ( code ) ) ;
2009-05-31 21:33:44 +02:00
}
}
2009-09-06 13:28:25 +02:00
void sizeof9 ( )
{
// ticket #487
{
2009-10-07 09:54:34 +02:00
const char code [ ] = " ; const char *str = \" 1 \" ; sizeof(str); " ;
2009-09-06 13:28:25 +02:00
std : : ostringstream expected ;
2009-10-27 19:23:45 +01:00
expected < < " ; const char * str ; str = \" 1 \" ; " < < sizeofFromTokenizer ( " * " ) < < " ; " ;
2009-09-06 13:28:25 +02:00
2009-10-07 09:54:34 +02:00
ASSERT_EQUALS ( expected . str ( ) , sizeof_ ( code ) ) ;
2009-09-06 13:28:25 +02:00
}
{
2009-10-07 09:54:34 +02:00
const char code [ ] = " ; const char str[] = \" 1 \" ; sizeof(str); " ;
2009-09-06 13:28:25 +02:00
std : : ostringstream expected ;
2009-10-27 19:23:45 +01:00
expected < < " ; const char * str ; str = \" 1 \" ; " < < sizeofFromTokenizer ( " char " ) * 2 < < " ; " ;
2009-09-06 13:28:25 +02:00
2009-10-07 10:08:53 +02:00
ASSERT_EQUALS ( expected . str ( ) , sizeof_ ( code ) ) ;
2009-09-06 13:28:25 +02:00
}
{
2009-10-07 09:54:34 +02:00
const char code [ ] = " ; const char str[] = {'1'}; sizeof(str); " ;
2009-09-06 13:28:25 +02:00
const char str [ ] = { ' 1 ' } ;
std : : ostringstream expected ;
2009-10-07 09:54:34 +02:00
expected < < " ; const char * str ; str = { '1' } ; " < < sizeof ( str ) < < " ; " ;
2009-09-06 13:28:25 +02:00
TODO_ASSERT_EQUALS ( expected . str ( ) , sizeof_ ( code ) ) ;
}
2009-09-22 20:50:00 +02:00
// ticket #716 - sizeof string
{
std : : ostringstream expected ;
expected < < " ; " < < ( sizeof " 123 " ) ;
ASSERT_EQUALS ( expected . str ( ) , sizeof_ ( " ; sizeof \" 123 \" " ) ) ;
ASSERT_EQUALS ( expected . str ( ) , sizeof_ ( " ; sizeof( \" 123 \" ) " ) ) ;
}
2009-09-23 17:59:26 +02:00
2009-10-07 11:05:44 +02:00
{
const char code [ ] = " void f(char *a,char *b, char *c) "
" {g(sizeof(a),sizeof(b),sizeof(c));} " ;
std : : ostringstream expected ;
expected < < " void f ( char * a , char * b , char * c ) { g ( " < <
2009-10-27 19:23:45 +01:00
sizeofFromTokenizer ( " * " ) < < " , " < < sizeofFromTokenizer ( " * " ) < < " , " < < sizeofFromTokenizer ( " * " ) < < " ) ; } " ;
2009-10-07 11:05:44 +02:00
ASSERT_EQUALS ( expected . str ( ) , sizeof_ ( code ) ) ;
}
{
const char code [ ] = " void f(char a,char b, char c) "
" {g(sizeof(a),sizeof(b),sizeof(c));} " ;
std : : ostringstream expected ;
expected < < " void f ( char a , char b , char c ) { g ( " < <
2009-10-27 19:23:45 +01:00
sizeofFromTokenizer ( " char " ) < < " , " < < sizeofFromTokenizer ( " char " ) < < " , " < < sizeofFromTokenizer ( " char " ) < < " ) ; } " ;
2009-10-07 11:05:44 +02:00
ASSERT_EQUALS ( expected . str ( ) , sizeof_ ( code ) ) ;
}
{
const char code [ ] = " void f(const char *a,const char *b, const char *c) "
" {g(sizeof(a),sizeof(b),sizeof(c));} " ;
std : : ostringstream expected ;
expected < < " void f ( const char * a , const char * b , const char * c ) { g ( " < <
2009-10-27 19:23:45 +01:00
sizeofFromTokenizer ( " * " ) < < " , " < < sizeofFromTokenizer ( " * " ) < < " , " < < sizeofFromTokenizer ( " * " ) < < " ) ; } " ;
2009-10-07 11:05:44 +02:00
ASSERT_EQUALS ( expected . str ( ) , sizeof_ ( code ) ) ;
}
{
const char code [ ] = " void f(char a[10],char b[10], char c[10]) "
" {g(sizeof(a),sizeof(b),sizeof(c));} " ;
std : : ostringstream expected ;
expected < < " void f ( char a [ 10 ] , char b [ 10 ] , char c [ 10 ] ) { g ( " < <
2009-10-27 19:23:45 +01:00
sizeofFromTokenizer ( " * " ) < < " , " < < sizeofFromTokenizer ( " * " ) < < " , " < < sizeofFromTokenizer ( " * " ) < < " ) ; } " ;
2009-10-07 11:05:44 +02:00
ASSERT_EQUALS ( expected . str ( ) , sizeof_ ( code ) ) ;
}
{
const char code [ ] = " void f(const char a[10],const char b[10], const char c[10]) "
" {g(sizeof(a),sizeof(b),sizeof(c));} " ;
std : : ostringstream expected ;
expected < < " void f ( const char a [ 10 ] , "
" const char b [ 10 ] , "
" const char c [ 10 ] ) { g ( " < <
2009-10-27 19:23:45 +01:00
sizeofFromTokenizer ( " * " ) < < " , " < < sizeofFromTokenizer ( " * " ) < < " , " < < sizeofFromTokenizer ( " * " ) < < " ) ; } " ;
2009-10-07 11:05:44 +02:00
ASSERT_EQUALS ( expected . str ( ) , sizeof_ ( code ) ) ;
}
{
const char code [ ] = " void f(const char *a[10],const char *b[10], const char *c[10]) "
" {g(sizeof(a),sizeof(b),sizeof(c));} " ;
std : : ostringstream expected ;
expected < < " void f ( const char * a [ 10 ] , "
" const char * b [ 10 ] , "
" const char * c [ 10 ] ) { g ( " < <
2009-10-27 19:23:45 +01:00
sizeofFromTokenizer ( " * " ) < < " , " < < sizeofFromTokenizer ( " * " ) < < " , " < < sizeofFromTokenizer ( " * " ) < < " ) ; } " ;
2009-10-07 11:05:44 +02:00
ASSERT_EQUALS ( expected . str ( ) , sizeof_ ( code ) ) ;
}
{
const char code [ ] = " void f(char *a[10],char *b[10], char *c[10]) "
" {g(sizeof(a),sizeof(b),sizeof(c));} " ;
std : : ostringstream expected ;
expected < < " void f ( char * a [ 10 ] , char * b [ 10 ] , char * c [ 10 ] ) { g ( " < <
2009-10-27 19:23:45 +01:00
sizeofFromTokenizer ( " * " ) < < " , " < < sizeofFromTokenizer ( " * " ) < < " , " < < sizeofFromTokenizer ( " * " ) < < " ) ; } " ;
2009-10-07 11:05:44 +02:00
ASSERT_EQUALS ( expected . str ( ) , sizeof_ ( code ) ) ;
}
2009-09-23 17:59:26 +02:00
{
std : : ostringstream expected ;
expected < < " ; " < < sizeof ( " \" quote \" " ) ;
ASSERT_EQUALS ( expected . str ( ) , sizeof_ ( " ; sizeof( \" \\ \" quote \\ \" \" ) " ) ) ;
}
2009-10-07 09:54:34 +02:00
{
std : : ostringstream expected ;
2009-10-27 19:23:45 +01:00
expected < < " void f ( ) { char str [ 100 ] = \" 100 \" ; " < < sizeofFromTokenizer ( " char " ) * 100 < < " } " ;
2009-10-07 09:54:34 +02:00
ASSERT_EQUALS ( expected . str ( ) , tok ( " void f ( ) { char str [ 100 ] = \" 100 \" ; sizeof ( str ) } " ) ) ;
}
2009-09-06 13:28:25 +02:00
}
2009-10-10 14:26:03 +02:00
void sizeof10 ( )
{
// ticket #809
const char code [ ] = " int m ; "
" compat_ulong_t um ; "
" size_t size ; size = sizeof ( m ) / sizeof ( um ) ; " ;
ASSERT_EQUALS ( code , tok ( code ) ) ;
}
2009-10-18 18:06:32 +02:00
void sizeof11 ( )
{
// ticket #827
const char code [ ] = " void f() \n "
" { \n "
" char buf2[4]; \n "
" sizeof buf2; \n "
" } \n "
" \n "
" void g() \n "
" { \n "
" struct A a[2]; \n "
" char buf[32]; \n "
" sizeof buf; \n "
" } " ;
const char expected [ ] = " void f ( ) "
" { "
" char buf2 [ 4 ] ; "
" 4 ; "
" } "
" "
" void g ( ) "
" { "
" struct A a [ 2 ] ; "
" char buf [ 32 ] ; "
" 32 ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2009-10-21 21:08:00 +02:00
void sizeof12 ( )
{
// ticket #827
const char code [ ] = " void f() \n "
" { \n "
" int *p; \n "
" (sizeof *p); \n "
" } " ;
const char expected [ ] = " void f ( ) "
" { "
" int * p ; "
" 4 ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2009-10-22 23:00:27 +02:00
void sizeof13 ( )
{
// ticket #851
const char code [ ] = " int main() \n "
" { \n "
" char *a; \n "
" a = malloc(sizeof(*a)); \n "
" } \n "
" \n "
" struct B \n "
" { \n "
" char * b[2]; \n "
" }; " ;
const char expected [ ] = " int main ( ) "
" { "
" char * a ; "
" a = malloc ( 1 ) ; "
" } "
" struct B "
" { "
" char * b [ 2 ] ; "
" } ; " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2009-11-13 20:07:44 +01:00
void sizeof14 ( )
{
// ticket #954
const char code [ ] = " void f() \n "
" { \n "
" A **a; \n "
" int aa = sizeof *(*a)->b; \n "
" } \n " ;
const char expected [ ] = " void f ( ) "
" { "
" A * * a ; "
" int aa ; aa = sizeof ( * ( * a ) . b ) ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2009-03-05 20:32:02 +01:00
void casting ( )
{
2009-06-19 19:25:56 +02:00
{
const char code [ ] = " void f() \n "
" { \n "
" for (int i = 0; i < static_cast<int>(3); ++i) {} \n "
" } \n " ;
2009-09-13 12:59:59 +02:00
const std : : string expected ( " void f ( ) { for ( int i = 0 ; i < 3 ; ++ i ) { } } " ) ;
2009-03-05 20:32:02 +01:00
2009-06-19 19:25:56 +02:00
ASSERT_EQUALS ( expected , sizeof_ ( code ) ) ;
}
{
const char code [ ] = " void f() \n "
" { \n "
" p = const_cast<char *> qtu (); \n "
" } \n " ;
2009-03-05 20:32:02 +01:00
2009-09-13 12:59:59 +02:00
const std : : string expected ( " void f ( ) { p = const_cast < char * > qtu ( ) ; } " ) ;
2009-06-19 19:25:56 +02:00
ASSERT_EQUALS ( expected , sizeof_ ( code ) ) ;
}
2009-03-11 18:50:24 +01:00
2009-09-13 12:31:32 +02:00
{
// ticket #645
const char code [ ] = " void f() \n "
" { \n "
" return dynamic_cast<Foo *>((bar())); \n "
" } \n " ;
2009-09-13 12:59:59 +02:00
const std : : string expected ( " void f ( ) { return bar ( ) ; } " ) ;
2009-09-13 12:31:32 +02:00
ASSERT_EQUALS ( expected , sizeof_ ( code ) ) ;
}
}
2009-03-11 18:50:24 +01:00
2009-11-07 08:25:39 +01:00
void strlen1 ( )
{
ASSERT_EQUALS ( " 4 " , tok ( " strlen( \" abcd \" ) " ) ) ;
{
const char code [ ] = " void f() \n "
" { \n "
" const char *s = \" abcd \" ; \n "
" strlen(s); \n "
" } \n " ;
const char expected [ ] = " void f ( ) "
" { "
" const char * s ; "
" s = \" abcd \" ; "
" 4 ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
{
const char code [ ] = " void f() \n "
" { \n "
" const char s [ ] = \" abcd \" ; \n "
" strlen(s); \n "
" } \n " ;
const char expected [ ] = " void f ( ) "
" { "
" const char * s ; "
" s = \" abcd \" ; "
" 4 ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
}
2009-03-11 18:50:24 +01:00
void template1 ( )
{
const char code [ ] = " template <classname T> void f(T val) { T a; } \n "
" f<int>(10); " ;
2009-11-01 19:03:52 +01:00
const std : : string expected ( " ; f<int> ( 10 ) ; "
2009-03-11 18:50:24 +01:00
" void f<int> ( int val ) { int a ; } " ) ;
2009-03-11 19:14:45 +01:00
2009-03-11 18:50:24 +01:00
ASSERT_EQUALS ( expected , sizeof_ ( code ) ) ;
2009-03-05 20:32:02 +01:00
}
2009-03-12 22:17:42 +01:00
void template2 ( )
{
const char code [ ] = " template <classname T> class Fred { T a; }; \n "
" Fred<int> fred; " ;
2009-11-01 19:03:52 +01:00
const std : : string expected ( " ; ; "
2009-03-12 22:17:42 +01:00
" Fred<int> fred ; "
" class Fred<int> { int a ; } " ) ;
ASSERT_EQUALS ( expected , sizeof_ ( code ) ) ;
}
2009-03-13 00:07:05 +01:00
2009-03-14 21:26:32 +01:00
void template3 ( )
{
const char code [ ] = " template <classname T, int sz> class Fred { T data[sz]; }; \n "
" Fred<float,4> fred; " ;
2009-11-01 19:03:52 +01:00
const std : : string expected ( " ; ; "
2009-03-14 21:26:32 +01:00
" Fred<float,4> fred ; "
" class Fred<float,4> { float data [ 4 ] ; } " ) ;
ASSERT_EQUALS ( expected , sizeof_ ( code ) ) ;
}
2009-03-15 17:07:05 +01:00
void template4 ( )
{
const char code [ ] = " template <classname T> class Fred { Fred(); }; \n "
" Fred<float> fred; " ;
2009-11-01 19:03:52 +01:00
const std : : string expected ( " ; ; "
2009-03-15 17:07:05 +01:00
" Fred<float> fred ; "
" class Fred<float> { Fred<float> ( ) ; } " ) ;
ASSERT_EQUALS ( expected , sizeof_ ( code ) ) ;
}
2009-03-15 20:03:29 +01:00
void template5 ( )
{
const char code [ ] = " template <classname T> class Fred { }; \n "
" template <classname T> Fred<T>::Fred() { } \n "
" Fred<float> fred; " ;
2009-11-01 19:03:52 +01:00
const std : : string expected ( " ; ; "
" ; "
2009-03-15 20:03:29 +01:00
" Fred<float> fred ; "
" class Fred<float> { } "
" Fred<float> :: Fred<float> ( ) { } " ) ;
ASSERT_EQUALS ( expected , sizeof_ ( code ) ) ;
}
2009-05-05 20:16:57 +02:00
void template6 ( )
{
const char code [ ] = " template <classname T> class Fred { }; \n "
" Fred<float> fred1; \n "
" Fred<float> fred2; " ;
2009-11-01 19:03:52 +01:00
const std : : string expected ( " ; ; "
2009-05-05 20:16:57 +02:00
" Fred<float> fred1 ; "
" Fred<float> fred2 ; "
" class Fred<float> { } " ) ;
ASSERT_EQUALS ( expected , sizeof_ ( code ) ) ;
}
2009-05-05 22:09:15 +02:00
void template7 ( )
{
2009-05-06 21:03:11 +02:00
// A template class that is not used => no simplification
2009-05-05 22:09:15 +02:00
{
const char code [ ] = " template <class T> \n "
" class ABC \n "
" { \n "
" public: \n "
" typedef ABC<T> m; \n "
" \n "
2009-05-06 21:03:11 +02:00
" }; \n " ;
2009-05-05 22:09:15 +02:00
2009-11-01 19:03:52 +01:00
const std : : string expected ( " ; ; " ) ;
2009-05-05 22:09:15 +02:00
2009-05-06 21:03:11 +02:00
ASSERT_EQUALS ( expected , sizeof_ ( code ) ) ;
2009-05-05 22:09:15 +02:00
}
{
const char code [ ] = " template <typename T> class ABC { \n "
" public: \n "
" typedef std::vector<T> type; \n "
" }; \n "
" int main() { \n "
" ABC<int>::type v; \n "
" v.push_back(4); \n "
" return 0; \n "
" } \n " ;
2009-11-01 19:03:52 +01:00
const std : : string expected ( " ; ; "
2009-05-05 22:09:15 +02:00
" int main ( ) { "
" std :: vector < int > v ; "
" v . push_back ( 4 ) ; "
" return 0 ; "
" } " ) ;
TODO_ASSERT_EQUALS ( expected , sizeof_ ( code ) ) ;
}
{
const char code [ ] = " template <typename T> class ABC { \n "
" public: \n "
" typedef std::vector<T> type; \n "
" void f() \n "
" { \n "
" ABC<int>::type v; \n "
" v.push_back(4); \n "
" } \n "
2009-07-08 09:38:59 +02:00
" }; \n " ;
2009-05-05 22:09:15 +02:00
2009-11-01 19:03:52 +01:00
const std : : string expected ( " ; ; " ) ;
2009-05-05 22:09:15 +02:00
2009-11-01 19:03:52 +01:00
ASSERT_EQUALS ( expected , sizeof_ ( code ) ) ;
2009-05-05 22:09:15 +02:00
}
}
2009-03-14 21:26:32 +01:00
2009-05-08 16:19:22 +02:00
// Template definitions but no usage => no expansion
void template8 ( )
{
const char code [ ] = " template<typename T> class A; \n "
" template<typename T> class B; \n "
" \n "
" typedef A<int> x; \n "
" typedef B<int> y; \n "
" \n "
" template<typename T> class A { \n "
" void f() { \n "
" B<T> a = B<T>::g(); \n "
" T b = 0; \n "
" if (b) \n "
" b = 0; \n "
" } \n "
" }; \n "
" \n "
" template<typename T> inline B<T> h() { return B<T>(); } \n " ;
2009-11-01 21:00:43 +01:00
ASSERT_EQUALS ( " ; ; typedef A < int > x ; typedef B < int > y ; ; ; ; " , sizeof_ ( code ) ) ;
2009-11-01 20:49:43 +01:00
ASSERT_EQUALS ( " class A { ; } ; " , sizeof_ ( " class A{ template<typename T> int foo(T d);}; " ) ) ;
2009-05-08 16:19:22 +02:00
}
2009-05-09 08:02:59 +02:00
void template9 ( )
{
const char code [ ] = " template < typename T > class A { } ; \n "
" \n "
" void f ( ) { \n "
" A<int> a ; \n "
" } \n "
" \n "
" template < typename T > \n "
" class B { \n "
" void g ( ) { \n "
" A < T > b = A < T > :: h ( ) ; \n "
" } \n "
" } ; \n " ;
// The expected result..
2009-11-01 19:03:52 +01:00
std : : string expected ( " ; ; void f ( ) { A<int> a ; } ; ; class A<int> { } " ) ;
2009-05-09 08:02:59 +02:00
ASSERT_EQUALS ( expected , sizeof_ ( code ) ) ;
}
2009-06-14 11:28:25 +02:00
void template10 ( )
{
const char code [ ] = " template <int ui, typename T> T * foo() \n "
" { return new T[ui]; } \n "
" \n "
" void f ( ) \n "
" { \n "
" foo<3,int>(); \n "
" } \n " ;
// The expected result..
2009-11-01 19:03:52 +01:00
const std : : string expected ( " ; "
" void f ( ) "
" { "
" foo<3,int> ( ) ; "
" } "
" int * foo<3,int> ( ) { return new int [ 3 ] ; } " ) ;
2009-06-14 11:28:25 +02:00
ASSERT_EQUALS ( expected , sizeof_ ( code ) ) ;
}
2009-05-08 16:19:22 +02:00
2009-06-14 14:57:47 +02:00
void template11 ( )
{
const char code [ ] = " template <int ui, typename T> T * foo() \n "
" { return new T[ui]; } \n "
" \n "
" void f ( ) \n "
" { \n "
" char * p = foo<3,char>(); \n "
" } \n " ;
// The expected result..
2009-11-01 19:03:52 +01:00
const std : : string expected ( " ; "
" void f ( ) "
" { "
" char * p ; p = foo<3,char> ( ) ; "
" } "
" char * foo<3,char> ( ) { return new char [ 3 ] ; } " ) ;
2009-06-14 14:57:47 +02:00
ASSERT_EQUALS ( expected , sizeof_ ( code ) ) ;
}
2009-07-22 13:29:42 +02:00
void template12 ( )
{
const char code [ ] = " template <int x, int y, int z> \n "
" class A : public B<x, y, (x - y) ? ((y < z) ? 1 : -1) : 0> \n "
" { }; \n "
" \n "
" void f() \n "
" { \n "
" A<12,12,11> a; \n "
" } \n " ;
// The expected result..
2009-11-01 19:03:52 +01:00
const std : : string expected ( " ; ; "
" void f ( ) "
" { "
" A<12,12,11> a ; "
" } "
" class A<12,12,11> : public B < 12 , 12 , 0 > "
" { } " ) ;
2009-07-22 13:29:42 +02:00
ASSERT_EQUALS ( expected , sizeof_ ( code ) ) ;
}
2009-09-14 21:49:41 +02:00
void template13 ( )
{
const char code [ ] = " class BB {}; \n "
" \n "
" template <class T> \n "
" class AA \n "
" { \n "
" public: \n "
" static AA<T> create(T* newObject); \n "
" }; \n "
" \n "
" class CC { public: CC(AA<BB>, int) {} }; \n "
" \n "
" class XX { \n "
" AA<CC> y; \n "
" public: \n "
" XX(); \n "
" }; \n "
" \n "
" XX::XX(): \n "
" y(AA<CC>::create(new CC(AA<BB>(), 0))) \n "
" {} \n " ;
// Just run it and check that there are not assertions.
sizeof_ ( code ) ;
}
2009-10-31 12:34:52 +01:00
void template14 ( )
{
const char code [ ] = " template <> void foo<int *>() \n "
" { x(); } \n "
" \n "
" int main() \n "
" { \n "
" foo<int*>(); \n "
" } \n " ;
// The expected result..
const std : : string expected ( " void foo<int*> ( ) "
" { x ( ) ; } "
" int main ( ) "
" { foo<int*> ( ) ; } " ) ;
ASSERT_EQUALS ( expected , sizeof_ ( code ) ) ;
2009-11-01 15:56:00 +01:00
}
void template15 ( )
{
const char code [ ] = " template <unsigned int i> void a() \n "
" { \n "
" a<i-1>(); \n "
" } \n "
" \n "
" template <> void a<0>() \n "
" { } \n "
" \n "
" int main() \n "
" { \n "
" a<2>(); \n "
" return 0; \n "
" } \n " ;
// The expected result..
2009-11-01 19:03:52 +01:00
const std : : string expected ( " ; "
2009-11-01 15:56:00 +01:00
" void a<0> ( ) { } "
" int main ( ) "
" { a<2> ( ) ; return 0 ; } "
" void a<2> ( ) { a<1> ( ) ; } "
" void a<1> ( ) { a<0> ( ) ; } " ) ;
ASSERT_EQUALS ( expected , sizeof_ ( code ) ) ;
2009-10-31 12:34:52 +01:00
}
2009-11-01 19:03:52 +01:00
void template16 ( )
{
const char code [ ] = " template <unsigned int i> void a() \n "
" { } \n "
" \n "
" template <unsigned int i> void b() \n "
" { a<i>(); } \n "
" \n "
" int main() \n "
" { \n "
" b<2>(); \n "
" return 0; \n "
" } \n " ;
// The expected result..
const std : : string expected ( " ; "
" ; "
" int main ( ) { b<2> ( ) ; return 0 ; } "
" void b<2> ( ) { a<2> ( ) ; } "
" void a<2> ( ) { } " ) ;
ASSERT_EQUALS ( expected , sizeof_ ( code ) ) ;
}
2009-09-02 22:54:50 +02:00
void template_default_parameter ( )
{
2009-09-06 08:22:45 +02:00
{
const char code [ ] = " template <class T, int n=3> \n "
" class A \n "
" { T ar[n]; }; \n "
" \n "
" void f() \n "
" { \n "
" A<int,2> a1; \n "
" A<int> a2; \n "
" } \n " ;
2009-05-08 16:19:22 +02:00
2009-09-06 08:22:45 +02:00
// The expected result..
2009-11-01 19:03:52 +01:00
const std : : string expected ( " ; ; "
" void f ( ) "
" { "
2009-09-06 08:22:45 +02:00
" A<int,2> a1 ; "
2009-11-01 19:03:52 +01:00
" A<int,3> a2 ; "
" } "
" class A<int,2> "
" { int ar [ 2 ] ; } "
" class A<int,3> "
" { int ar [ 3 ] ; } " ) ;
2009-09-06 08:22:45 +02:00
ASSERT_EQUALS ( expected , sizeof_ ( code ) ) ;
}
{
const char code [ ] = " template <class T, int n1=3, int n2=2> \n "
" class A \n "
" { T ar[n1+n2]; }; \n "
" \n "
" void f() \n "
" { \n "
" A<int> a1; \n "
" A<int,3> a2; \n "
" } \n " ;
// The expected result..
2009-11-01 19:03:52 +01:00
const std : : string expected ( " ; ; "
" void f ( ) "
" { "
2009-09-06 08:22:45 +02:00
" A<int,3,2> a1 ; "
2009-11-01 19:03:52 +01:00
" A<int,3,2> a2 ; "
" } "
" class A<int,3,2> "
" { int ar [ 5 ] ; } " ) ;
2009-09-06 08:22:45 +02:00
ASSERT_EQUALS ( expected , sizeof_ ( code ) ) ;
}
2009-09-09 22:57:28 +02:00
{
const char code [ ] = " template <class T, int n=3> \n "
" class A \n "
" { T ar[n]; }; \n "
" \n "
" void f() \n "
" { \n "
" A<int,(int)2> a1; \n "
" A<int> a2; \n "
" } \n " ;
// The expected result..
2009-09-13 12:59:59 +02:00
const std : : string expected ( " template < class T , int n > "
2009-09-09 22:57:28 +02:00
" class A "
" { T ar [ n ] ; } ; "
" void f ( ) "
" { "
" A<int,(int)2> a1 ; "
" A<int,3> a2 ; "
" } "
" class A<int,2> "
" { int ar [ 2 ] ; } "
" class A<int,3> "
" { int ar [ 3 ] ; } " ) ;
TODO_ASSERT_EQUALS ( expected , sizeof_ ( code ) ) ;
}
2009-09-02 22:54:50 +02:00
}
2009-05-08 16:19:22 +02:00
2009-09-03 22:19:44 +02:00
void template_typename ( )
{
const char code [ ] = " template <class T> \n "
" void foo(typename T::t *) \n "
" { } " ;
// The expected result..
2009-11-01 19:03:52 +01:00
const std : : string expected ( " ; " ) ;
2009-09-03 22:19:44 +02:00
ASSERT_EQUALS ( expected , sizeof_ ( code ) ) ;
}
2009-05-08 16:19:22 +02:00
2009-03-13 00:07:05 +01:00
void namespaces ( )
{
{
const char code [ ] = " using namespace std; namespace a{ namespace b{ void f(){} } } " ;
2009-09-13 12:59:59 +02:00
const std : : string expected ( " using namespace std ; void f ( ) { } " ) ;
2009-03-13 00:07:05 +01:00
ASSERT_EQUALS ( expected , sizeof_ ( code ) ) ;
}
{
const char code [ ] = " namespace b{ void f(){} } " ;
2009-09-13 12:59:59 +02:00
const std : : string expected ( " void f ( ) { } " ) ;
2009-03-13 00:07:05 +01:00
ASSERT_EQUALS ( expected , sizeof_ ( code ) ) ;
}
2009-03-30 23:59:33 +02:00
{
const char code [ ] = " int a; namespace b{ } " ;
2009-09-13 12:59:59 +02:00
const std : : string expected ( " int a ; " ) ;
2009-03-30 23:59:33 +02:00
ASSERT_EQUALS ( expected , sizeof_ ( code ) ) ;
}
2009-03-13 00:07:05 +01:00
}
2009-03-23 18:20:56 +01:00
std : : string simplifyIfAssign ( const char code [ ] )
{
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyIfAssign ( ) ;
std : : ostringstream ostr ;
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) )
ostr < < ( tok - > previous ( ) ? " " : " " ) < < tok - > str ( ) ;
return ostr . str ( ) ;
}
void ifassign1 ( )
{
2009-09-30 20:42:14 +02:00
ASSERT_EQUALS ( " ; a = b ; if ( a ) { ; } " , simplifyIfAssign ( " ;if(a=b); " ) ) ;
ASSERT_EQUALS ( " ; a = b ( ) ; if ( ( a ) ) { ; } " , simplifyIfAssign ( " ;if((a=b())); " ) ) ;
ASSERT_EQUALS ( " ; a = b ( ) ; if ( ! ( a ) ) { ; } " , simplifyIfAssign ( " ;if(!(a=b())); " ) ) ;
ASSERT_EQUALS ( " ; a . x = b ( ) ; if ( ! ( a . x ) ) { ; } " , simplifyIfAssign ( " ;if(!(a->x=b())); " ) ) ;
2009-10-06 20:03:15 +02:00
ASSERT_EQUALS ( " A ( ) a = b ; if ( a ) { ; } " , simplifyIfAssign ( " A() if(a=b); " ) ) ;
2009-11-04 23:58:15 +01:00
ASSERT_EQUALS ( " void foo ( int a ) { a = b ( ) ; if ( 0 <= a ) { ; } } " , tok ( " void foo(int a) {if((a=b())>=0);} " ) ) ;
TODO_ASSERT_EQUALS ( " void foo ( A a ) { a . c = b ( ) ; if ( 0 <= a . c ) { ; } } " , tok ( " void foo(A a) {if((a.c=b())>=0);} " ) ) ;
2009-03-24 18:23:21 +01:00
}
2009-06-20 13:58:30 +02:00
void whileAssign ( )
{
ASSERT_EQUALS ( " ; a = b ; while ( a ) { b = 0 ; a = b ; } " , simplifyIfAssign ( " ;while(a=b) { b = 0; } " ) ) ;
2009-08-05 19:45:05 +02:00
ASSERT_EQUALS ( " ; a . b = c ; while ( a . b ) { c = 0 ; a . b = c ; } " , simplifyIfAssign ( " ;while(a.b=c) { c=0; } " ) ) ;
2009-11-12 20:04:11 +01:00
ASSERT_EQUALS ( " struct hfs_bnode * node ; "
" struct hfs_btree * tree ; "
" node = tree . node_hash [ i ++ ] ; "
" while ( node ) { node = tree . node_hash [ i ++ ] ; } " ,
tok ( " struct hfs_bnode *node; "
" struct hfs_btree *tree; "
" while ((node = tree->node_hash[i++])) { } " ) ) ;
2009-06-20 13:58:30 +02:00
}
2009-03-24 18:23:21 +01:00
std : : string simplifyIfNot ( const char code [ ] )
{
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyIfNot ( ) ;
std : : ostringstream ostr ;
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) )
ostr < < ( tok - > previous ( ) ? " " : " " ) < < tok - > str ( ) ;
return ostr . str ( ) ;
}
void ifnot ( )
{
ASSERT_EQUALS ( " if ( ! x ) " , simplifyIfNot ( " if(0==x) " ) ) ;
2009-07-18 10:18:46 +02:00
ASSERT_EQUALS ( " if ( ! x ) " , simplifyIfNot ( " if(x==0) " ) ) ;
2009-03-24 18:23:21 +01:00
ASSERT_EQUALS ( " if ( ! ( a = b ) ) " , simplifyIfNot ( " if(0==(a=b)) " ) ) ;
2009-05-01 12:07:10 +02:00
ASSERT_EQUALS ( " if ( ! x ) " , simplifyIfNot ( " if(x==0) " ) ) ;
ASSERT_EQUALS ( " if ( ! a && b ( ) ) " , simplifyIfNot ( " if( 0 == a && b() ) " ) ) ;
ASSERT_EQUALS ( " if ( b ( ) && ! a ) " , simplifyIfNot ( " if( b() && 0 == a ) " ) ) ;
ASSERT_EQUALS ( " if ( ! ( a = b ) ) " , simplifyIfNot ( " if((a=b)==0) " ) ) ;
2009-07-18 10:18:46 +02:00
ASSERT_EQUALS ( " if ( ! x . y ) " , simplifyIfNot ( " if(x.y==0) " ) ) ;
2009-07-29 23:37:01 +02:00
ASSERT_EQUALS ( " if ( ( ! x ) ) " , simplifyIfNot ( " if((x==0)) " ) ) ;
ASSERT_EQUALS ( " if ( ( ! x ) && ! y ) " , simplifyIfNot ( " if((x==0) && y==0) " ) ) ;
2009-08-20 21:37:05 +02:00
ASSERT_EQUALS ( " if ( ! ( ! fclose ( fd ) ) ) " , simplifyIfNot ( " if(!(fclose(fd) == 0)) " ) ) ;
2009-03-23 18:20:56 +01:00
}
2009-05-31 10:42:27 +02:00
2009-08-29 15:29:19 +02:00
std : : string simplifyLogicalOperators ( const char code [ ] )
2009-05-31 10:42:27 +02:00
{
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
2009-08-29 15:29:19 +02:00
tokenizer . simplifyLogicalOperators ( ) ;
2009-05-31 10:42:27 +02:00
std : : ostringstream ostr ;
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) )
ostr < < ( tok - > previous ( ) ? " " : " " ) < < tok - > str ( ) ;
return ostr . str ( ) ;
}
void not1 ( )
{
2009-08-29 15:29:19 +02:00
ASSERT_EQUALS ( " if ( ! p ) " , simplifyLogicalOperators ( " if (not p) " ) ) ;
ASSERT_EQUALS ( " if ( p && ! q ) " , simplifyLogicalOperators ( " if (p && not q) " ) ) ;
ASSERT_EQUALS ( " void foo ( not i ) " , simplifyLogicalOperators ( " void foo ( not i ) " ) ) ;
2009-05-31 10:42:27 +02:00
}
2009-08-29 14:26:01 +02:00
void and1 ( )
{
2009-09-30 20:42:14 +02:00
ASSERT_EQUALS ( " if ( p && q ) { ; } " ,
2009-08-29 22:33:12 +02:00
simplifyLogicalOperators ( " if (p and q) ; " ) ) ;
2009-08-29 14:26:01 +02:00
2009-09-30 20:42:14 +02:00
ASSERT_EQUALS ( " if ( foo ( ) && q ) { ; } " ,
2009-08-29 22:33:12 +02:00
simplifyLogicalOperators ( " if (foo() and q) ; " ) ) ;
2009-08-29 15:29:19 +02:00
2009-09-30 20:42:14 +02:00
ASSERT_EQUALS ( " if ( foo ( ) && bar ( ) ) { ; } " ,
2009-08-29 22:33:12 +02:00
simplifyLogicalOperators ( " if (foo() and bar()) ; " ) ) ;
2009-08-29 15:29:19 +02:00
2009-09-30 20:42:14 +02:00
ASSERT_EQUALS ( " if ( p && bar ( ) ) { ; } " ,
2009-08-29 22:33:12 +02:00
simplifyLogicalOperators ( " if (p and bar()) ; " ) ) ;
2009-08-29 14:26:01 +02:00
}
2009-06-10 23:12:26 +02:00
void comma_keyword ( )
{
2009-06-18 23:00:16 +02:00
{
const char code [ ] = " void foo() \n "
" { \n "
" char *a, *b; \n "
" delete a, delete b; \n "
" } \n " ;
2009-09-13 12:59:59 +02:00
ASSERT_EQUALS ( " void foo ( ) { char * a ; char * b ; delete a ; delete b ; } " , sizeof_ ( code ) ) ;
2009-06-18 23:00:16 +02:00
}
2009-08-06 23:31:39 +02:00
{
const char code [ ] = " void foo() \n "
" { \n "
" struct A *a, *b; \n "
" } \n " ;
2009-09-13 12:59:59 +02:00
ASSERT_EQUALS ( " void foo ( ) { struct A * a ; struct A * b ; } " , sizeof_ ( code ) ) ;
2009-08-06 23:31:39 +02:00
}
{
const char code [ ] = " void foo() \n "
" { \n "
" struct A **a, **b; \n "
" } \n " ;
2009-09-13 12:59:59 +02:00
ASSERT_EQUALS ( " void foo ( ) { struct A * * a ; struct A * * b ; } " , sizeof_ ( code ) ) ;
2009-08-06 23:31:39 +02:00
}
2009-06-18 23:00:16 +02:00
{
const char code [ ] = " void foo() \n "
" { \n "
" char *a, *b; \n "
" delete a, b; \n "
" } \n " ;
2009-09-13 12:59:59 +02:00
ASSERT_EQUALS ( " void foo ( ) { char * a ; char * b ; delete a ; delete b ; } " , sizeof_ ( code ) ) ;
2009-06-18 23:00:16 +02:00
}
2009-07-26 13:00:11 +02:00
2009-08-06 22:11:29 +02:00
{
const char code [ ] = " void foo() \n "
" { \n "
" char **a, **b, **c; \n "
" } \n " ;
2009-09-13 12:59:59 +02:00
ASSERT_EQUALS ( " void foo ( ) { char * * a ; char * * b ; char * * c ; } " , sizeof_ ( code ) ) ;
2009-08-06 22:11:29 +02:00
}
2009-07-26 13:00:11 +02:00
{
const char code [ ] = " int f() \n "
" { \n "
" if (something) \n "
" return a(2, c(3, 4)), b(3), 10; \n "
" return a(), b(0, 0, 0), 10; \n "
" } \n " ;
2009-09-13 12:59:59 +02:00
ASSERT_EQUALS ( " int f ( ) "
2009-07-26 13:00:11 +02:00
" { "
" if ( something ) "
" { "
2009-07-26 22:13:08 +02:00
" a ( 2 , c ( 3 , 4 ) ) ; "
" b ( 3 ) ; "
" return 10 ; "
2009-07-26 13:00:11 +02:00
" } "
" a ( ) ; "
" b ( 0 , 0 , 0 ) ; "
" return 10 ; "
" } " , sizeof_ ( code ) ) ;
}
2009-07-26 23:08:18 +02:00
{
const char code [ ] = " void foo() \n "
" { \n "
" delete [] a, a = 0; \n "
" } \n " ;
2009-09-13 12:59:59 +02:00
ASSERT_EQUALS ( " void foo ( ) { delete [ ] a ; a = 0 ; } " , sizeof_ ( code ) ) ;
2009-07-27 21:13:08 +02:00
}
{
const char code [ ] = " void foo() \n "
" { \n "
" delete a, a = 0; \n "
" } \n " ;
2009-09-13 12:59:59 +02:00
ASSERT_EQUALS ( " void foo ( ) { delete a ; a = 0 ; } " , sizeof_ ( code ) ) ;
2009-07-26 23:08:18 +02:00
}
2009-07-28 18:40:15 +02:00
{
const char code [ ] = " void f() \n "
" { \n "
" for(int a,b; a < 10; a = a + 1, b = b + 1); \n "
" } \n " ;
2009-09-13 12:59:59 +02:00
ASSERT_EQUALS ( " void f ( ) { for ( int a , b ; a < 10 ; a = a + 1 , b = b + 1 ) { ; } } " , sizeof_ ( code ) ) ;
2009-07-28 18:40:15 +02:00
}
2009-08-09 10:16:37 +02:00
2009-08-29 11:42:42 +02:00
{
2009-09-13 12:59:59 +02:00
const char code [ ] = " typedef enum { a = 0 , b = 1 , c = 2 } abc ; " ;
2009-08-29 11:42:42 +02:00
ASSERT_EQUALS ( code , sizeof_ ( code ) ) ;
}
2009-08-09 10:16:37 +02:00
{
const char code [ ] = " void f() \n "
" { \n "
" char buf[BUFSIZ], **p; \n "
" char *ptrs[BUFSIZ], **pp; \n "
" } \n " ;
2009-09-13 12:59:59 +02:00
ASSERT_EQUALS ( " void f ( ) { char buf [ BUFSIZ ] ; char * * p ; char * ptrs [ BUFSIZ ] ; char * * pp ; } " , sizeof_ ( code ) ) ;
2009-08-09 10:16:37 +02:00
}
2009-06-10 23:12:26 +02:00
}
2009-05-31 10:42:27 +02:00
2009-07-04 17:06:25 +02:00
void remove_comma ( )
{
{
const char code [ ] = " void f() \n "
" { \n "
" int a,b; \n "
" if( a ) \n "
" a=0, \n "
" b=0; \n "
" } \n " ;
2009-09-13 12:59:59 +02:00
ASSERT_EQUALS ( " void f ( ) { int a ; int b ; if ( a ) { a = 0 ; b = 0 ; } } " , sizeof_ ( code ) ) ;
2009-07-04 17:06:25 +02:00
}
{
const char code [ ] = " void f() \n "
" { \n "
" A a,b; \n "
" if( a.f ) \n "
" a.f=b.f, \n "
" a.g=b.g; \n "
" } \n " ;
2009-09-13 12:59:59 +02:00
ASSERT_EQUALS ( " void f ( ) { A a ; A b ; if ( a . f ) { a . f = b . f ; a . g = b . g ; } } " , sizeof_ ( code ) ) ;
2009-07-04 17:06:25 +02:00
}
2009-09-12 08:12:00 +02:00
// keep the comma in template specifiers..
{
const char code [ ] = " void f() \n "
" { \n "
" int a = b<T<char,3>, int>(); \n "
" } \n " ;
2009-09-13 12:59:59 +02:00
ASSERT_EQUALS ( " void f ( ) { int a ; a = b < T < char , 3 > , int > ( ) ; } " , sizeof_ ( code ) ) ;
2009-09-12 08:12:00 +02:00
}
2009-07-04 17:06:25 +02:00
}
2009-07-14 12:06:38 +02:00
void conditionOperator ( )
{
2009-07-22 13:29:42 +02:00
{
const char code [ ] = " ; x = a ? b : c; " ;
ASSERT_EQUALS ( " ; if ( a ) { x = b ; } else { x = c ; } " , tok ( code ) ) ;
}
{
const char code [ ] = " (0?(false?1:2):3) " ;
ASSERT_EQUALS ( " ( 3 ) " , tok ( code ) ) ;
}
{
const char code [ ] = " (1?(false?1:2):3) " ;
2009-07-23 10:12:02 +02:00
ASSERT_EQUALS ( " ( 2 ) " , tok ( code ) ) ;
}
2009-10-05 16:34:38 +02:00
{
const char code [ ] = " int a = (1?0:1 == 1?0:1); " ;
ASSERT_EQUALS ( " int a ; a = 0 ; " , tok ( code ) ) ;
}
{
const char code [ ] = " (1?0:foo()) " ;
ASSERT_EQUALS ( " ( 0 ) " , tok ( code ) ) ;
}
2009-07-23 10:12:02 +02:00
{
const char code [ ] = " ( true ? a ( ) : b ( ) ) " ;
2009-10-05 16:34:38 +02:00
ASSERT_EQUALS ( " ( a ( ) ) " , tok ( code ) ) ;
2009-07-23 10:12:02 +02:00
}
{
const char code [ ] = " ( true ? abc . a : abc . b ) " ;
2009-10-05 16:34:38 +02:00
ASSERT_EQUALS ( " ( abc . a ) " , tok ( code ) ) ;
2009-07-22 13:29:42 +02:00
}
2009-10-20 22:58:46 +02:00
{
const char code [ ] = " void f() \n "
" { \n "
" bool x = false; \n "
" int b = x ? 44 : 3; \n "
" } \n " ;
ASSERT_EQUALS ( " void f ( ) { bool x ; x = false ; int b ; b = 3 ; } " , tok ( code ) ) ;
}
2009-07-14 12:06:38 +02:00
}
2009-08-02 10:45:33 +02:00
void calculations ( )
{
{
const char code [ ] = " a[i+8+2] " ;
ASSERT_EQUALS ( " a [ i + 10 ] " , tok ( code ) ) ;
}
{
const char code [ ] = " a[8+2+i] " ;
ASSERT_EQUALS ( " a [ 10 + i ] " , tok ( code ) ) ;
}
{
const char code [ ] = " a[i + 2 * (2 * 4)] " ;
ASSERT_EQUALS ( " a [ i + 16 ] " , tok ( code ) ) ;
}
{
const char code [ ] = " a[i + 100 - 90] " ;
ASSERT_EQUALS ( " a [ i + 10 ] " , tok ( code ) ) ;
}
{
const char code [ ] = " a[1+1+1+1+1+1+1+1+1+1-2+5-3] " ;
ASSERT_EQUALS ( " a [ 10 ] " , tok ( code ) ) ;
}
{
const char code [ ] = " a[10+10-10-10] " ;
ASSERT_EQUALS ( " a [ 0 ] " , tok ( code ) ) ;
}
2009-08-28 12:13:46 +02:00
ASSERT_EQUALS ( " x = 1 + 2 * y ; " , tok ( " x=1+2*y; " ) ) ;
ASSERT_EQUALS ( " x = 7 ; " , tok ( " x=1+2*3; " ) ) ;
2009-09-15 22:38:54 +02:00
ASSERT_EQUALS ( " x = 47185 ; " , tok ( " x=(65536*72/100); " ) ) ;
2009-08-02 10:45:33 +02:00
}
2009-08-24 23:10:12 +02:00
void goto1 ( )
{
{
const char code [ ] = " void foo() \n "
" { \n "
" if (a()) \n "
" { \n "
" goto out; \n "
" } \n "
" b(); \n "
" out: \n "
" c(); \n "
" } " ;
const char expect [ ] = " void foo ( ) "
" { "
" if ( a ( ) ) "
" { "
" c ( ) ; "
" return ; "
" } "
" b ( ) ; "
" c ( ) ; "
" } " ;
ASSERT_EQUALS ( expect , tok ( code ) ) ;
}
{
const char code [ ] = " void foo() \n "
" { \n "
" if (a()) \n "
" goto out; \n "
" b(); \n "
" out: \n "
" if (c()) \n "
" d(); \n "
" } " ;
const char expect [ ] = " void foo ( ) "
" { "
" if ( a ( ) ) "
" { "
" if ( c ( ) ) "
2009-08-28 17:05:25 +02:00
" { d ( ) ; } "
2009-08-24 23:10:12 +02:00
" return ; "
" } "
" b ( ) ; "
" if ( c ( ) ) "
2009-08-28 17:05:25 +02:00
" { d ( ) ; } "
2009-08-24 23:10:12 +02:00
" } " ;
ASSERT_EQUALS ( expect , tok ( code ) ) ;
}
2009-08-26 18:46:56 +02:00
2009-09-30 13:30:53 +02:00
{
const char code [ ] = " void foo() \n "
" { \n "
" if (a()) \n "
" goto out; \n "
" b(); \n "
" out: \n "
" if (c()) \n "
" { \n "
" d(); \n "
" } \n "
" } " ;
const char expect [ ] = " void foo ( ) "
" { "
" if ( a ( ) ) "
" { "
" if ( c ( ) ) "
" { d ( ) ; } "
" return ; "
" } "
" b ( ) ; "
" if ( c ( ) ) "
" { d ( ) ; } "
" } " ;
ASSERT_EQUALS ( expect , tok ( code ) ) ;
}
2009-10-01 09:25:32 +02:00
{
const char code [ ] = " void foo(int x) \n "
" { \n "
" if (a()) \n "
" goto out; \n "
" b(); \n "
" out: \n "
" if (x) \n "
" { \n "
" x++; b[0]=x; \n "
" } \n "
" } " ;
const char expect [ ] = " void foo ( int x ) "
" { "
" if ( a ( ) ) "
" { "
" if ( x ) "
" { x ++ ; b [ 0 ] = x ; } "
" return ; "
" } "
" b ( ) ; "
" if ( x ) "
" { x ++ ; b [ 0 ] = x ; } "
" } " ;
ASSERT_EQUALS ( expect , tok ( code ) ) ;
}
2009-08-26 18:46:56 +02:00
{
const char code [ ] = " class NoLabels { bool varOne : 1 ; bool varTwo : 1 ; } ; " ;
ASSERT_EQUALS ( code , tok ( code ) ) ;
}
2009-08-26 18:54:28 +02:00
{
const char code [ ] = " void foo ( ) { int var ; var = x < y ? y : z ; } ; " ;
ASSERT_EQUALS ( code , tok ( code ) ) ;
}
2009-08-24 23:10:12 +02:00
}
2009-09-05 18:46:27 +02:00
void strcat1 ( )
{
const char code [ ] = " ; strcat(strcat(strcat(strcat(strcat(strcat(dst, \" this \" ), \" \" ), \" is \" ), \" a \" ), \" test \" ), \" . \" ); " ;
const char expect [ ] = " ; "
" strcat ( dst , \" this \" ) ; "
" strcat ( dst , \" \" ) ; "
" strcat ( dst , \" is \" ) ; "
" strcat ( dst , \" a \" ) ; "
" strcat ( dst , \" test \" ) ; "
" strcat ( dst , \" . \" ) ; " ;
ASSERT_EQUALS ( expect , tok ( code ) ) ;
}
2009-09-05 21:21:25 +02:00
void argumentsWithSameName ( )
{
// This code has syntax error, two variables can not have the same name
2009-09-05 22:41:39 +02:00
{
const char code [ ] = " void foo(x, x) \n "
" int x; \n "
" int x; \n "
" {} \n " ;
ASSERT_EQUALS ( " void foo ( x , x ) int x ; int x ; { } " , tok ( code ) ) ;
}
{
const char code [ ] = " void foo(x, y) \n "
" int x; \n "
" int x; \n "
" {} \n " ;
ASSERT_EQUALS ( " void foo ( int x , y ) int x ; { } " , tok ( code ) ) ;
}
2009-09-05 21:21:25 +02:00
}
2009-09-12 22:54:47 +02:00
void simplifyAtol ( )
{
ASSERT_EQUALS ( " a = std :: atol ( x ) ; " , tok ( " a = std::atol(x); " ) ) ;
ASSERT_EQUALS ( " a = atol ( \" text \" ) ; " , tok ( " a = atol( \" text \" ); " ) ) ;
ASSERT_EQUALS ( " a = 0 ; " , tok ( " a = std::atol( \" 0 \" ); " ) ) ;
ASSERT_EQUALS ( " a = 10 ; " , tok ( " a = atol( \" 0xa \" ); " ) ) ;
}
2009-09-27 16:04:10 +02:00
void simplifyHexInString ( )
{
ASSERT_EQUALS ( " \" a \" " , tok ( " \" \\ x61 \" " ) ) ;
ASSERT_EQUALS ( " \" a \" " , tok ( " \" \\ 141 \" " ) ) ;
ASSERT_EQUALS ( " \" \\ 0 \" " , tok ( " \" \\ x00 \" " ) ) ;
ASSERT_EQUALS ( " \" \\ 0 \" " , tok ( " \" \\ 000 \" " ) ) ;
ASSERT_EQUALS ( " \" \\ nhello \" " , tok ( " \" \\ nhello \" " ) ) ;
ASSERT_EQUALS ( " \" aaa \" " , tok ( " \" \\ x61 \\ x61 \\ x61 \" " ) ) ;
ASSERT_EQUALS ( " \" aaa \" " , tok ( " \" \\ 141 \\ 141 \\ 141 \" " ) ) ;
ASSERT_EQUALS ( " \" \\ \\ x61 \" " , tok ( " \" \\ \\ x61 \" " ) ) ;
// These tests can fail, if other characters are handled
// more correctly. But fow now all non null characters should
// become 'a'
ASSERT_EQUALS ( " \" a \" " , tok ( " \" \\ x62 \" " ) ) ;
ASSERT_EQUALS ( " \" a \" " , tok ( " \" \\ 177 \" " ) ) ;
}
2009-09-30 09:11:09 +02:00
void simplifyTypedef ( )
{
const char code [ ] = " class A \n "
" { \n "
" public: \n "
" typedef wchar_t duplicate; \n "
" void foo() {} \n "
" }; \n "
" typedef A duplicate; \n "
" int main() \n "
" { \n "
" duplicate a; \n "
" a.foo(); \n "
" A::duplicate c = 0; \n "
" } \n " ;
const std : : string expected =
" class A "
" { "
" public: "
" typedef wchar_t duplicate ; "
" void foo ( ) { } "
" } ; "
" typedef A duplicate ; "
" int main ( ) "
" { "
" A a ; "
" a . foo ( ) ; "
2009-09-30 13:35:00 +02:00
" wchar_t c ; c = 0 ; "
2009-09-30 09:11:09 +02:00
" } " ;
2009-09-30 13:35:00 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2009-09-30 09:11:09 +02:00
}
void simplifyTypedef2 ( )
{
const char code [ ] = " class A; \n "
" typedef A duplicate; \n "
" class A \n "
" { \n "
" public: \n "
" typedef wchar_t duplicate; \n "
" duplicate foo() { wchar_t b; return b; } \n "
" }; \n " ;
const std : : string expected =
" class A ; "
" typedef A duplicate ; "
" class A "
" { "
" public: "
" typedef wchar_t duplicate ; "
" wchar_t foo ( ) { wchar_t b ; return b ; } "
" } ; " ;
2009-09-30 13:35:00 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2009-09-30 09:11:09 +02:00
}
void simplifyTypedef3 ( )
{
const char code [ ] = " class A {}; \n "
" typedef A duplicate; \n "
" wchar_t foo() \n "
" { \n "
" typedef wchar_t duplicate; \n "
" duplicate b; \n "
" return b; \n "
" } \n "
" int main() \n "
" { \n "
" duplicate b; \n "
" } \n " ;
const std : : string expected =
" class A { } ; "
" typedef A duplicate ; "
" wchar_t foo ( ) "
" { "
" typedef wchar_t duplicate ; "
" wchar_t b ; "
" return b ; "
" } "
" int main ( ) "
" { "
" A b ; "
" } " ;
2009-09-30 13:35:00 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2009-09-30 09:11:09 +02:00
}
2009-09-30 13:35:00 +02:00
void simplifyTypedef4 ( )
{
const char code [ ] = " typedef int s32; \n "
" typedef unsigned int u32; \n "
" void f() \n "
" { \n "
" s32 ivar = -2; \n "
" u32 uvar = 2; \n "
" return uvar / ivar; \n "
" } \n " ;
const std : : string expected =
" typedef int s32 ; "
" typedef unsigned int u32 ; "
" void f ( ) "
" { "
" int ivar ; ivar = -2 ; "
" unsigned int uvar ; uvar = 2 ; "
" return uvar / ivar ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2009-10-03 17:02:23 +02:00
void simplifyTypedef5 ( )
{
// ticket #780
const char code [ ] =
" typedef struct yy_buffer_state *YY_BUFFER_STATE; \n "
" void f() \n "
" { \n "
" YY_BUFFER_STATE state; \n "
" } \n " ;
const char expected [ ] =
" typedef struct yy_buffer_state * YY_BUFFER_STATE ; "
" void f ( ) "
" { "
" struct yy_buffer_state * state ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2009-10-04 22:33:41 +02:00
void reverseArraySyntax ( )
{
ASSERT_EQUALS ( " a [ 13 ] " , tok ( " 13[a] " ) ) ;
}
2009-10-06 11:40:50 +02:00
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 ( ) { int x ; x = 0 ; { g ( ) ; } } " , tok ( code ) ) ;
}
{
const char code [ ] =
" void f() \n "
" { \n "
" int x = 1; \n "
" if( !x ) \n "
" { g(); \n "
" } \n "
" } " ;
ASSERT_EQUALS ( " void f ( ) { int x ; x = 1 ; } " , tok ( code ) ) ;
}
{
const char code [ ] =
" void f() \n "
" { \n "
" bool x = true; \n "
" if( !x ) \n "
" { g(); \n "
" } \n "
" } " ;
ASSERT_EQUALS ( " void f ( ) { bool x ; x = true ; } " , tok ( code ) ) ;
}
{
const char code [ ] =
" void f() \n "
" { \n "
" bool x = false; \n "
" if( !x ) \n "
" { g(); \n "
" } \n "
" } " ;
ASSERT_EQUALS ( " void f ( ) { bool x ; x = false ; { 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 " ;
2009-10-06 11:44:53 +02:00
ASSERT_EQUALS ( " void f ( ) { { ; } } " , tok ( code ) ) ;
2009-10-06 11:40:50 +02:00
}
}
2009-10-17 18:21:13 +02:00
void pointeralias ( )
{
{
const char code [ ] = " void f() \n "
" { \n "
" char buf[100]; \n "
" char *p = buf; \n "
" x(p); \n "
" } \n " ;
const char expected [ ] = " void f ( ) "
" { "
" char buf [ 100 ] ; "
" char * p ; p = buf ; "
" x ( buf ) ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
{
const char code [ ] = " void f(char *p1) \n "
" { \n "
" char *p = p1; \n "
" p1 = 0; "
" x(p); \n "
" } \n " ;
const char expected [ ] = " void f ( char * p1 ) "
" { "
" char * p ; p = p1 ; "
" p1 = 0 ; "
" x ( p ) ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2009-11-12 22:16:24 +01:00
{
const char code [ ] = " void foo(Result* ptr) \n "
" { \n "
" Result* obj = ptr; \n "
" ++obj->total; \n "
" } \n " ;
const char expected [ ] = " void foo ( Result * ptr ) "
" { "
" Result * obj ; obj = ptr ; "
" ++ obj . total ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2009-10-17 18:21:13 +02:00
}
2009-11-05 21:30:05 +01:00
void reduceConstness ( )
{
ASSERT_EQUALS ( " char * p ; " , tok ( " char * const p; " ) ) ;
}
2008-12-18 22:28:57 +01:00
} ;
2009-01-05 16:49:57 +01:00
REGISTER_TEST ( TestSimplifyTokens )