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
*/
2009-07-05 22:16:43 +02:00
// The preprocessor that Cppcheck uses is a bit special. Instead of generating
2008-12-18 22:28:57 +01:00
// the code for a known configuration, it generates the code for each configuration.
# include <cstring>
# 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
extern std : : ostringstream errout ;
class TestTokenizer : public TestFixture
{
public :
TestTokenizer ( ) : TestFixture ( " TestTokenizer " )
{ }
private :
void run ( )
{
2009-11-12 18:53:26 +01:00
TEST_CASE ( tokenize1 ) ;
2009-11-22 09:06:39 +01:00
TEST_CASE ( tokenize2 ) ;
2009-12-12 19:58:19 +01:00
TEST_CASE ( tokenize3 ) ;
2010-01-29 19:34:43 +01:00
TEST_CASE ( tokenize4 ) ;
2009-11-12 20:04:11 +01:00
2010-01-21 20:05:32 +01:00
// don't freak out when the syntax is wrong
TEST_CASE ( wrong_syntax ) ;
2009-03-17 20:50:06 +01:00
TEST_CASE ( minus ) ;
2009-01-05 16:49:57 +01:00
TEST_CASE ( longtok ) ;
2008-12-18 22:28:57 +01:00
2009-02-04 20:40:48 +01:00
TEST_CASE ( removeCast1 ) ;
2009-05-27 20:06:19 +02:00
TEST_CASE ( removeCast2 ) ;
2009-11-14 11:59:05 +01:00
TEST_CASE ( removeCast3 ) ;
2009-11-17 19:03:23 +01:00
TEST_CASE ( removeCast4 ) ;
2009-02-04 20:40:48 +01:00
2009-01-05 16:49:57 +01:00
TEST_CASE ( inlineasm ) ;
2008-12-18 22:28:57 +01:00
2009-01-05 16:49:57 +01:00
TEST_CASE ( dupfuncname ) ;
2008-12-18 22:28:57 +01:00
2009-01-05 16:49:57 +01:00
TEST_CASE ( const_and_volatile_functions ) ;
2008-12-18 22:28:57 +01:00
2009-01-05 16:49:57 +01:00
TEST_CASE ( ifAddBraces1 ) ;
TEST_CASE ( ifAddBraces2 ) ;
TEST_CASE ( ifAddBraces3 ) ;
TEST_CASE ( ifAddBraces4 ) ;
2009-03-15 13:44:57 +01:00
TEST_CASE ( ifAddBraces5 ) ;
2009-05-09 22:12:14 +02:00
TEST_CASE ( ifAddBraces6 ) ;
2009-06-22 22:54:11 +02:00
TEST_CASE ( ifAddBraces7 ) ;
2009-11-20 20:38:30 +01:00
TEST_CASE ( ifAddBraces9 ) ;
2008-12-22 10:20:46 +01:00
2009-06-20 13:20:51 +02:00
TEST_CASE ( whileAddBraces ) ;
2009-08-22 12:42:19 +02:00
TEST_CASE ( doWhileAddBraces ) ;
2009-06-20 13:20:51 +02:00
2009-07-25 13:11:29 +02:00
TEST_CASE ( pointers_condition ) ;
2008-12-18 22:28:57 +01:00
2009-01-05 16:49:57 +01:00
TEST_CASE ( simplifyKnownVariables1 ) ;
TEST_CASE ( simplifyKnownVariables2 ) ;
TEST_CASE ( simplifyKnownVariables3 ) ;
TEST_CASE ( simplifyKnownVariables4 ) ;
TEST_CASE ( simplifyKnownVariables5 ) ;
2009-02-20 18:27:57 +01:00
TEST_CASE ( simplifyKnownVariables6 ) ;
2009-02-24 17:11:37 +01:00
TEST_CASE ( simplifyKnownVariables7 ) ;
2009-02-27 19:25:47 +01:00
TEST_CASE ( simplifyKnownVariables8 ) ;
2009-03-25 07:10:17 +01:00
TEST_CASE ( simplifyKnownVariables9 ) ;
2009-05-01 20:31:07 +02:00
TEST_CASE ( simplifyKnownVariables10 ) ;
2009-06-19 18:03:09 +02:00
TEST_CASE ( simplifyKnownVariables11 ) ;
2009-08-01 15:57:54 +02:00
TEST_CASE ( simplifyKnownVariables12 ) ;
2009-08-22 10:23:55 +02:00
TEST_CASE ( simplifyKnownVariables13 ) ;
2009-09-28 17:15:31 +02:00
TEST_CASE ( simplifyKnownVariables14 ) ;
2009-10-06 08:14:59 +02:00
TEST_CASE ( simplifyKnownVariables15 ) ;
2009-10-10 10:13:56 +02:00
TEST_CASE ( simplifyKnownVariables16 ) ;
2009-11-02 23:07:15 +01:00
TEST_CASE ( simplifyKnownVariables17 ) ;
2010-01-15 22:46:47 +01:00
TEST_CASE ( simplifyKnownVariables18 ) ;
2010-01-24 19:27:39 +01:00
TEST_CASE ( simplifyKnownVariables19 ) ;
2008-12-18 22:28:57 +01:00
2009-01-05 16:49:57 +01:00
TEST_CASE ( match1 ) ;
2008-12-18 22:28:57 +01:00
2009-01-05 16:49:57 +01:00
TEST_CASE ( match2 ) ;
2008-12-18 22:28:57 +01:00
2009-01-05 16:49:57 +01:00
TEST_CASE ( varid1 ) ;
TEST_CASE ( varid2 ) ;
2009-02-02 18:35:46 +01:00
TEST_CASE ( varid3 ) ;
2009-02-15 15:02:57 +01:00
TEST_CASE ( varid4 ) ;
2009-02-16 18:41:33 +01:00
TEST_CASE ( varid5 ) ;
2009-03-25 18:15:49 +01:00
TEST_CASE ( varid6 ) ;
2009-03-01 17:37:02 +01:00
TEST_CASE ( varid7 ) ;
2009-02-28 21:21:48 +01:00
TEST_CASE ( varidReturn ) ;
2009-03-25 18:10:03 +01:00
TEST_CASE ( varid8 ) ;
2009-03-28 21:07:33 +01:00
TEST_CASE ( varid9 ) ;
2009-05-31 18:46:32 +02:00
TEST_CASE ( varid10 ) ;
2009-08-11 20:58:49 +02:00
TEST_CASE ( varid11 ) ;
2009-09-22 17:49:13 +02:00
TEST_CASE ( varid12 ) ;
2009-12-30 20:15:44 +01:00
TEST_CASE ( varid13 ) ;
2010-02-05 23:24:57 +01:00
TEST_CASE ( varid14 ) ;
2009-04-25 16:55:00 +02:00
TEST_CASE ( varidStl ) ;
2009-04-29 21:22:56 +02:00
TEST_CASE ( varid_delete ) ;
2009-05-27 22:34:08 +02:00
TEST_CASE ( varid_functions ) ;
2009-07-18 12:37:51 +02:00
TEST_CASE ( varid_reference_to_containers ) ;
2009-08-09 10:41:31 +02:00
TEST_CASE ( varid_in_class ) ;
2009-05-03 13:50:26 +02:00
TEST_CASE ( varidclass1 ) ;
TEST_CASE ( varidclass2 ) ;
TEST_CASE ( varidclass3 ) ;
TEST_CASE ( varidclass4 ) ;
2009-01-19 19:40:24 +01:00
2009-01-19 19:51:27 +01:00
TEST_CASE ( file1 ) ;
2009-01-20 23:25:49 +01:00
TEST_CASE ( file2 ) ;
2009-02-15 12:42:04 +01:00
TEST_CASE ( file3 ) ;
2009-01-20 18:26:16 +01:00
TEST_CASE ( doublesharp ) ;
2009-01-24 08:56:47 +01:00
2009-02-13 14:33:12 +01:00
TEST_CASE ( macrodoublesharp ) ;
2009-01-26 23:26:50 +01:00
TEST_CASE ( simplify_function_parameters ) ;
2009-01-25 20:39:05 +01:00
2009-05-28 19:37:39 +02:00
TEST_CASE ( removeParantheses1 ) ; // Ticket #61
TEST_CASE ( removeParantheses2 ) ;
2009-05-28 22:03:36 +02:00
TEST_CASE ( removeParantheses3 ) ;
2009-06-12 15:04:58 +02:00
TEST_CASE ( removeParantheses4 ) ; // Ticket #390
2009-06-12 16:14:01 +02:00
TEST_CASE ( removeParantheses5 ) ; // Ticket #392
2009-11-15 09:28:08 +01:00
TEST_CASE ( removeParantheses6 ) ;
2009-02-02 07:21:48 +01:00
2009-02-08 10:51:45 +01:00
TEST_CASE ( tokenize_double ) ;
2009-02-08 11:25:33 +01:00
TEST_CASE ( tokenize_strings ) ;
2009-02-12 20:26:42 +01:00
TEST_CASE ( simplify_constants ) ;
2009-02-14 22:33:28 +01:00
TEST_CASE ( simplify_constants2 ) ;
2009-03-13 22:38:42 +01:00
TEST_CASE ( findClassFunction1 ) ;
2010-01-02 17:29:55 +01:00
TEST_CASE ( findClassFunction2 ) ;
2009-03-16 19:03:23 +01:00
2009-03-18 20:32:05 +01:00
TEST_CASE ( vardecl1 ) ;
TEST_CASE ( vardecl2 ) ;
2009-06-14 14:57:47 +02:00
TEST_CASE ( vardecl3 ) ;
2009-06-14 19:32:34 +02:00
TEST_CASE ( vardecl4 ) ;
2009-10-02 16:28:30 +02:00
TEST_CASE ( vardec_static ) ;
2009-08-08 12:33:07 +02:00
TEST_CASE ( vardecl6 ) ;
2009-08-23 08:26:16 +02:00
TEST_CASE ( vardecl7 ) ;
2009-09-20 12:28:15 +02:00
TEST_CASE ( vardecl8 ) ;
2009-09-20 13:28:56 +02:00
TEST_CASE ( vardecl9 ) ;
2009-09-26 12:02:13 +02:00
TEST_CASE ( vardecl10 ) ;
2009-07-28 21:46:33 +02:00
TEST_CASE ( vardecl_stl ) ;
2009-12-05 21:15:14 +01:00
TEST_CASE ( vardecl_template ) ;
2009-04-20 20:38:05 +02:00
TEST_CASE ( volatile_variables ) ;
2009-05-07 22:17:29 +02:00
TEST_CASE ( syntax_error ) ;
2009-05-27 20:49:29 +02:00
TEST_CASE ( removeKeywords ) ;
2009-06-06 10:40:48 +02:00
// unsigned i; => unsigned int i;
TEST_CASE ( unsigned1 ) ;
2009-11-17 19:03:23 +01:00
TEST_CASE ( unsigned2 ) ;
2009-08-12 22:50:03 +02:00
TEST_CASE ( testUpdateClassList ) ;
2009-09-20 22:13:06 +02:00
TEST_CASE ( createLinks ) ;
2009-09-28 20:25:05 +02:00
TEST_CASE ( signed1 ) ;
2009-10-01 19:45:48 +02:00
2010-02-02 19:56:41 +01:00
TEST_CASE ( removeExceptionSpecification1 ) ;
TEST_CASE ( removeExceptionSpecification2 ) ;
2009-10-04 07:51:12 +02:00
TEST_CASE ( gt ) ; // use "<" comparisons instead of ">"
2009-10-09 21:11:29 +02:00
TEST_CASE ( simplifyString ) ;
2009-11-12 22:49:39 +01:00
TEST_CASE ( simplifyConst ) ;
2009-12-06 23:09:56 +01:00
TEST_CASE ( switchCase ) ;
2010-01-20 21:19:06 +01:00
TEST_CASE ( functionpointer ) ;
2010-01-31 09:33:57 +01:00
TEST_CASE ( removeRedundantAssignment ) ;
2010-02-03 20:01:56 +01:00
TEST_CASE ( removedeclspec ) ;
2008-12-18 22:28:57 +01:00
}
2009-01-03 21:29:20 +01:00
bool cmptok ( const char * expected [ ] , const Token * actual )
2008-12-18 22:28:57 +01:00
{
unsigned int i = 0 ;
for ( ; expected [ i ] & & actual ; + + i , actual = actual - > next ( ) )
{
2009-05-03 20:10:59 +02:00
if ( strcmp ( expected [ i ] , actual - > str ( ) . c_str ( ) ) ! = 0 )
2008-12-18 22:28:57 +01:00
return false ;
}
return ( expected [ i ] = = NULL & & actual = = NULL ) ;
}
2009-05-27 21:16:54 +02:00
std : : string tokenizeAndStringify ( const char code [ ] , bool simplify = false )
2009-01-18 09:52:20 +01:00
{
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
2009-05-27 20:49:29 +02:00
if ( simplify )
tokenizer . simplifyTokenList ( ) ;
2009-01-18 09:52:20 +01:00
std : : ostringstream ostr ;
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) )
{
ostr < < tok - > str ( ) ;
// Append newlines
2009-01-18 12:13:25 +01:00
if ( tok - > next ( ) )
2009-01-18 09:52:20 +01:00
{
2009-01-18 12:13:25 +01:00
if ( tok - > linenr ( ) ! = tok - > next ( ) - > linenr ( ) )
2009-01-18 09:52:20 +01:00
{
2009-07-13 15:51:35 +02:00
for ( unsigned int i = tok - > linenr ( ) ; i < tok - > next ( ) - > linenr ( ) ; + + i )
2009-01-18 09:52:20 +01:00
ostr < < " \n " ;
}
else
{
ostr < < " " ;
}
}
}
return ostr . str ( ) ;
}
2009-11-12 18:53:26 +01:00
void tokenize1 ( )
{
const std : : string code ( " void f ( ) \n "
" { if ( p . y ( ) > yof ) { } } " ) ;
ASSERT_EQUALS ( code , tokenizeAndStringify ( code . c_str ( ) ) ) ;
}
2009-11-22 09:06:39 +01:00
void tokenize2 ( )
{
const std : : string code ( " { sizeof a, sizeof b } " ) ;
ASSERT_EQUALS ( " { sizeof a , sizeof b } " , tokenizeAndStringify ( code . c_str ( ) ) ) ;
}
2009-12-12 19:58:19 +01:00
void tokenize3 ( )
{
errout . str ( " " ) ;
const std : : string code ( " void foo() \n "
" { \n "
" int i; \n "
" ABC(for(i=0;i<10;i++) x()); \n "
" } " ) ;
ASSERT_EQUALS ( " void foo ( ) \n "
" { \n "
" int i ; \n "
" ABC ( for ( i = 0 ; i < 10 ; i ++ ) x ( ) ) ; \n "
" } " , tokenizeAndStringify ( code . c_str ( ) ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2010-01-29 19:34:43 +01:00
void tokenize4 ( )
{
errout . str ( " " ) ;
const std : : string code ( " class foo \n "
" { \n "
" public: \n "
" const int i; \n "
" } " ) ;
ASSERT_EQUALS ( " class foo \n "
" { \n "
" public: \n "
" const int i ; \n "
" } " , tokenizeAndStringify ( code . c_str ( ) ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2010-01-21 20:05:32 +01:00
void wrong_syntax ( )
{
errout . str ( " " ) ;
const std : : string code ( " TR(kvmpio, PROTO(int rw), ARGS(rw), TP_(aa->rw;)) " ) ;
ASSERT_EQUALS ( " TR ( kvmpio , PROTO ( int rw ) , ARGS ( rw ) , TP_ ( aa . rw ; ) ) " , tokenizeAndStringify ( code . c_str ( ) , true ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2009-03-17 20:50:06 +01:00
void minus ( )
{
ASSERT_EQUALS ( " i = -12 " , tokenizeAndStringify ( " i = -12 " ) ) ;
ASSERT_EQUALS ( " 1 - 2 " , tokenizeAndStringify ( " 1-2 " ) ) ;
ASSERT_EQUALS ( " foo ( -1 ) - 2 " , tokenizeAndStringify ( " foo(-1)-2 " ) ) ;
2009-03-18 19:48:06 +01:00
ASSERT_EQUALS ( " int f ( ) { return -2 ; } " , tokenizeAndStringify ( " int f(){return -2;} " ) ) ;
2009-03-17 20:50:06 +01:00
}
2009-01-18 09:52:20 +01:00
2008-12-18 22:28:57 +01:00
void longtok ( )
{
2009-01-05 16:49:57 +01:00
std : : string filedata ( 10000 , ' a ' ) ;
2008-12-18 22:28:57 +01:00
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( filedata ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
// Expected result..
2009-05-01 22:41:55 +02:00
ASSERT_EQUALS ( std : : string ( 10000 , ' a ' ) , tokenizer . tokens ( ) - > str ( ) ) ;
2008-12-18 22:28:57 +01:00
}
2009-02-04 20:40:48 +01:00
// Dont remove "(int *)"..
void removeCast1 ( )
{
const char code [ ] = " int *f(int *); " ;
// tokenize..
2009-08-04 21:32:14 +02:00
Tokenizer tokenizer ;
2009-02-04 20:40:48 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyCasts ( ) ;
std : : ostringstream ostr ;
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) )
ostr < < " " < < tok - > str ( ) ;
2009-05-31 21:48:55 +02:00
ASSERT_EQUALS ( " int * f ( int * ) ; " , ostr . str ( ) ) ;
2009-02-04 20:40:48 +01:00
}
2009-05-27 20:06:19 +02:00
// remove static_cast..
void removeCast2 ( )
{
const char code [ ] = " t = (static_cast<std::vector<int> *>(&p)); \n " ;
// tokenize..
2009-08-04 21:32:14 +02:00
Tokenizer tokenizer ;
2009-05-27 20:06:19 +02:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyCasts ( ) ;
std : : ostringstream ostr ;
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) )
ostr < < " " < < tok - > str ( ) ;
2009-05-31 21:48:55 +02:00
ASSERT_EQUALS ( " t = ( & p ) ; " , ostr . str ( ) ) ;
2009-05-27 20:06:19 +02:00
}
2009-11-14 11:59:05 +01:00
void removeCast3 ( )
{
// ticket #961
const char code [ ] = " assert (iplen >= (unsigned) ipv4->ip_hl * 4 + 20); " ;
const char expected [ ] = " assert ( iplen >= ipv4 . ip_hl * 4 + 20 ) ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2009-11-17 19:03:23 +01:00
void removeCast4 ( )
{
// ticket #970
const char code [ ] = " if (a >= (unsigned)(b)) {} " ;
2009-11-17 21:15:25 +01:00
const char expected [ ] = " if ( a >= ( int ) b ) { } " ;
2009-11-17 19:03:23 +01:00
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2009-02-04 20:40:48 +01:00
2008-12-18 22:28:57 +01:00
void inlineasm ( )
{
2009-12-30 08:24:27 +01:00
ASSERT_EQUALS ( " ; asm ( ) ; " , tokenizeAndStringify ( " ;asm { mov ax,bx }; " ) ) ;
ASSERT_EQUALS ( " ; asm ( ) ; " , tokenizeAndStringify ( " ;_asm { mov ax,bx }; " ) ) ;
ASSERT_EQUALS ( " ; asm ( ) ; " , tokenizeAndStringify ( " ;__asm { mov ax,bx }; " ) ) ;
ASSERT_EQUALS ( " ; asm ( ) ; " , tokenizeAndStringify ( " ;__asm__ __volatile__ ( \" mov ax,bx \" ); " ) ) ;
2008-12-18 22:28:57 +01:00
}
void dupfuncname ( )
{
const char code [ ] = " void a() \n "
" { } \n "
" void a(int i) \n "
" { } \n "
" void b() \n "
" { } \n " ;
// tokenize..
2009-08-04 21:32:14 +02:00
Tokenizer tokenizer ;
2008-12-18 22:28:57 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . fillFunctionList ( ) ;
2009-08-04 21:32:14 +02:00
ASSERT_EQUALS ( 1 , static_cast < unsigned int > ( tokenizer . _functionList . size ( ) ) ) ;
ASSERT_EQUALS ( " b " , tokenizer . _functionList [ 0 ] - > str ( ) ) ;
2008-12-18 22:28:57 +01:00
}
void const_and_volatile_functions ( )
{
const char code [ ] = " class B \n \
{ \ n \
public : \ n \
void a ( ) ; \ n \
void b ( ) const ; \ n \
void c ( ) volatile ; \ n \
} ; \ n \
\ n \
void B : : a ( ) \ n \
{ } \ n \
\ n \
void B : : b ( ) const \ n \
{ } \ n \
\ n \
void B : : c ( ) volatile \ n \
{ } \ n " ;
// tokenize..
2009-08-04 21:32:14 +02:00
Tokenizer tokenizer ;
2008-12-18 22:28:57 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . fillFunctionList ( ) ;
2009-08-04 21:32:14 +02:00
ASSERT_EQUALS ( 3 , static_cast < unsigned int > ( tokenizer . _functionList . size ( ) ) ) ;
if ( tokenizer . _functionList . size ( ) = = 3 )
2008-12-18 22:28:57 +01:00
{
2009-08-04 21:32:14 +02:00
ASSERT_EQUALS ( " a " , tokenizer . _functionList [ 0 ] - > str ( ) ) ;
ASSERT_EQUALS ( " b " , tokenizer . _functionList [ 1 ] - > str ( ) ) ;
ASSERT_EQUALS ( " c " , tokenizer . _functionList [ 2 ] - > str ( ) ) ;
2008-12-18 22:28:57 +01:00
}
}
2009-07-25 13:11:29 +02:00
void pointers_condition ( )
{
const char code [ ] = " void f() \n "
" { \n "
" if (p != NULL); \n "
" if (NULL != p); \n "
" if (this->p != NULL); \n "
" if (NULL != this->p); \n "
" if (Foo::p != NULL); \n "
" if (NULL != Foo::p); \n "
" while (p != NULL); \n "
" while (NULL != p); \n "
" while (this->p != NULL); \n "
" while (NULL != this->p); \n "
" while (Foo::p != NULL); \n "
" while (NULL != Foo::p); \n "
" if (p == NULL); \n "
" if (NULL == p); \n "
" if (this->p == NULL); \n "
" if (NULL == this->p); \n "
" if (Foo::p == NULL); \n "
" if (NULL == Foo::p); \n "
" while (p == NULL); \n "
" while (NULL == p); \n "
" while (this->p == NULL); \n "
" while (NULL == this->p); \n "
" while (Foo::p == NULL); \n "
" while (NULL == Foo::p); \n "
" if (p1 != NULL || p2 == NULL) { ; } \n "
" if (p1 != NULL && p2 == NULL) { ; } \n "
2009-09-14 20:42:56 +02:00
" if (p == ' \\ 0'); \n "
2009-09-22 23:23:11 +02:00
" if (p == 0L); \n "
2009-09-23 21:44:52 +02:00
" if (p == 0UL); \n "
" if (p == 0ul); \n "
" if (p == 0l); \n "
2009-07-25 13:11:29 +02:00
" } \n " ;
ASSERT_EQUALS ( " void f ( ) \n "
" { \n "
" if ( p ) { ; } \n "
" if ( p ) { ; } \n "
" if ( this . p ) { ; } \n "
" if ( this . p ) { ; } \n "
" if ( Foo :: p ) { ; } \n "
" if ( Foo :: p ) { ; } \n "
" while ( p ) { ; } \n "
" while ( p ) { ; } \n "
" while ( this . p ) { ; } \n "
" while ( this . p ) { ; } \n "
" while ( Foo :: p ) { ; } \n "
" while ( Foo :: p ) { ; } \n "
" if ( ! p ) { ; } \n "
" if ( ! p ) { ; } \n "
" if ( ! this . p ) { ; } \n "
" if ( ! this . p ) { ; } \n "
" if ( ! Foo :: p ) { ; } \n "
" if ( ! Foo :: p ) { ; } \n "
" while ( ! p ) { ; } \n "
" while ( ! p ) { ; } \n "
" while ( ! this . p ) { ; } \n "
" while ( ! this . p ) { ; } \n "
" while ( ! Foo :: p ) { ; } \n "
" while ( ! Foo :: p ) { ; } \n "
" if ( p1 || ! p2 ) { ; } \n "
" if ( p1 && ! p2 ) { ; } \n "
2009-09-14 20:42:56 +02:00
" if ( ! p ) { ; } \n "
2009-09-22 23:23:11 +02:00
" if ( ! p ) { ; } \n "
2009-09-23 21:44:52 +02:00
" if ( ! p ) { ; } \n "
" if ( ! p ) { ; } \n "
" if ( ! p ) { ; } \n "
2009-07-25 13:11:29 +02:00
" } " , tokenizeAndStringify ( code , true ) ) ;
}
2008-12-22 19:05:22 +01:00
void ifAddBraces1 ( )
2008-12-22 10:20:46 +01:00
{
2008-12-22 19:05:22 +01:00
const char code [ ] = " void f() \n "
" { \n "
" if (a); \n "
2008-12-23 09:11:33 +01:00
" else ; \n "
2008-12-22 19:05:22 +01:00
" } \n " ;
2009-06-20 13:20:51 +02:00
ASSERT_EQUALS ( " void f ( ) \n "
" { \n "
" if ( a ) { ; } \n "
" else { ; } \n "
" } " , tokenizeAndStringify ( code , true ) ) ;
2008-12-22 19:05:22 +01:00
}
2008-12-22 10:20:46 +01:00
2008-12-22 19:05:22 +01:00
void ifAddBraces2 ( )
{
const char code [ ] = " void f() \n "
" { \n "
" if (a) if (b) { } \n "
" } \n " ;
2009-06-20 13:20:51 +02:00
ASSERT_EQUALS ( " void f ( ) \n "
" { \n "
" if ( a ) { if ( b ) { } } \n "
" } " , tokenizeAndStringify ( code , true ) ) ;
2008-12-22 19:05:22 +01:00
}
2008-12-22 10:20:46 +01:00
2008-12-22 19:05:22 +01:00
void ifAddBraces3 ( )
{
const char code [ ] = " void f() \n "
" { \n "
" if (a) for (;;) { } \n "
" } \n " ;
2009-06-20 13:20:51 +02:00
ASSERT_EQUALS ( " void f ( ) \n "
" { \n "
" if ( a ) { for ( ; ; ) { } } \n "
" } " , tokenizeAndStringify ( code , true ) ) ;
2008-12-22 10:20:46 +01:00
}
2008-12-22 19:32:04 +01:00
void ifAddBraces4 ( )
{
const char code [ ] = " char * foo () \n "
" { \n "
" char *str = malloc(10); \n "
" if (somecondition) \n "
" for ( ; ; ) \n "
" { } \n "
" return str; \n "
" } \n " ;
2009-06-20 13:20:51 +02:00
ASSERT_EQUALS ( " char * foo ( ) \n "
" { \n "
" char * str ; str = malloc ( 10 ) ; \n "
" if ( somecondition ) { \n "
" for ( ; ; ) \n "
" { } } \n "
" return str ; \n "
" } " , tokenizeAndStringify ( code , true ) ) ;
2008-12-22 19:32:04 +01:00
}
2009-03-15 13:44:57 +01:00
void ifAddBraces5 ( )
{
const char code [ ] = " void f() \n "
" { \n "
" for(int i = 0; i < 2; i++) \n "
" if(true) \n "
" return; \n "
" \n "
" return; \n "
" } \n " ;
2009-06-20 13:20:51 +02:00
ASSERT_EQUALS ( " void f ( ) \n "
" { \n "
" for ( int i = 0 ; i < 2 ; i ++ ) { \n "
" { \n "
" return ; } } \n \n "
" return ; \n "
" } " , tokenizeAndStringify ( code , true ) ) ;
2009-03-15 13:44:57 +01:00
}
2009-05-09 22:12:14 +02:00
void ifAddBraces6 ( )
{
const char code [ ] = " if() " ;
2009-06-20 13:20:51 +02:00
ASSERT_EQUALS ( " if ( ) " , tokenizeAndStringify ( code , true ) ) ;
}
2009-05-09 22:12:14 +02:00
2009-06-22 22:54:11 +02:00
void ifAddBraces7 ( )
{
const char code [ ] = " void f() \n "
" { \n "
" int a; \n "
" if( a ) \n "
" ({a=4;}),({a=5;}); \n "
" } \n " ;
ASSERT_EQUALS ( " void f ( ) \n "
" { \n "
" int a ; \n "
" if ( a ) { \n "
" ( { a = 4 ; } ) , ( { a = 5 ; } ) ; } \n "
" } " , tokenizeAndStringify ( code , true ) ) ;
}
2009-11-20 20:38:30 +01:00
void ifAddBraces9 ( )
{
// ticket #990
const char code [ ] =
" { "
" for (int k=0; k<VectorSize; k++) "
" LOG_OUT(ID_Vector[k]) "
" } " ;
const char expected [ ] =
" { "
" for ( int k = 0 ; k < VectorSize ; k ++ ) { "
" LOG_OUT ( ID_Vector [ k ] ) "
" } "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2009-06-20 13:20:51 +02:00
void whileAddBraces ( )
{
{
const char code [ ] = " ;while(a); " ;
ASSERT_EQUALS ( " ; while ( a ) { ; } " , tokenizeAndStringify ( code , true ) ) ;
}
}
2009-05-09 22:12:14 +02:00
2009-08-22 12:42:19 +02:00
void doWhileAddBraces ( )
{
2009-12-03 23:06:03 +01:00
{
const char code [ ] = " do ; while (0); " ;
2009-12-28 08:37:34 +01:00
const char result [ ] = " do { ; } while ( 0 ) ; " ;
2009-12-03 23:06:03 +01:00
2009-12-28 08:37:34 +01:00
ASSERT_EQUALS ( result , tokenizeAndStringify ( code , false ) ) ;
2009-12-03 23:06:03 +01:00
}
{
const char code [ ] = " UNKNOWN_MACRO ( do ) ; while ( a -- ) ; " ;
const char result [ ] = " UNKNOWN_MACRO ( do ) ; while ( a -- ) { ; } " ;
ASSERT_EQUALS ( result , tokenizeAndStringify ( code , true ) ) ;
}
{
const char code [ ] = " UNKNOWN_MACRO ( do , foo ) ; while ( a -- ) ; " ;
const char result [ ] = " UNKNOWN_MACRO ( do , foo ) ; while ( a -- ) { ; } " ;
ASSERT_EQUALS ( result , tokenizeAndStringify ( code , true ) ) ;
}
{
const char code [ ] = " void foo ( int c , int d ) { \n "
" do \n "
" if ( c ) { \n "
" while ( c ) { c -- ; } \n "
" } \n "
" while ( -- d > 0 ) ; \n "
" return 0 ; \n "
" } \n " ;
const char result [ ] = " void foo ( int c , int d ) { \n "
" do { \n "
" if ( c ) { \n "
" while ( c ) { c -- ; } \n "
" } } \n "
" while ( -- d > 0 ) ; \n "
" return 0 ; \n "
" } " ;
ASSERT_EQUALS ( result , tokenizeAndStringify ( code , true ) ) ;
}
2009-05-09 22:12:14 +02:00
2009-12-03 23:06:03 +01:00
{
const char code [ ] = " void foo ( int c , int d ) { \n "
" do \n "
" do c -- ; while ( c ) ; \n "
" while ( -- d > 0 ) ; \n "
" return 0 ; \n "
" } \n " ;
const char result [ ] = " void foo ( int c , int d ) { \n "
" do { \n "
" do { c -- ; } while ( c ) ; } \n "
" while ( -- d > 0 ) ; \n "
" return 0 ; \n "
" } " ;
ASSERT_EQUALS ( result , tokenizeAndStringify ( code , true ) ) ;
}
2009-08-22 12:42:19 +02:00
}
2009-03-15 13:44:57 +01:00
2009-09-19 08:55:41 +02:00
std : : string simplifyKnownVariables ( const char code [ ] )
{
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . setVarId ( ) ;
tokenizer . simplifyKnownVariables ( ) ;
std : : ostringstream ostr ;
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) )
{
if ( tok - > previous ( ) )
ostr < < " " ;
ostr < < tok - > str ( ) ;
}
return ostr . str ( ) ;
}
2008-12-22 19:08:32 +01:00
void simplifyKnownVariables1 ( )
2008-12-18 22:28:57 +01:00
{
2009-06-03 21:02:16 +02:00
{
const char code [ ] = " void f() \n "
" { \n "
" int a = 10; \n "
" if (a); \n "
" } \n " ;
2008-12-18 22:28:57 +01:00
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2009-09-30 20:42:14 +02:00
" void f ( ) { int a ; a = 10 ; if ( 10 ) { ; } } " ,
2009-09-19 13:21:50 +02:00
simplifyKnownVariables ( code ) ) ;
2009-06-03 21:02:16 +02:00
}
{
const char code [ ] = " void f() \n "
" { \n "
" int a = 10; \n "
" if (!a); \n "
" } \n " ;
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2009-09-30 20:42:14 +02:00
" void f ( ) { int a ; a = 10 ; if ( ! 10 ) { ; } } " ,
2009-09-19 13:21:50 +02:00
simplifyKnownVariables ( code ) ) ;
2009-06-03 21:02:16 +02:00
}
2008-12-22 19:08:32 +01:00
}
2008-12-18 22:28:57 +01:00
2008-12-22 19:08:32 +01:00
void simplifyKnownVariables2 ( )
{
const char code [ ] = " void f() \n "
" { \n "
" int a = 10; \n "
" a = g(); \n "
" if (a); \n "
" } \n " ;
2008-12-18 22:28:57 +01:00
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2009-09-30 20:42:14 +02:00
" void f ( ) { int a ; a = 10 ; a = g ( ) ; if ( a ) { ; } } " ,
2009-09-19 13:21:50 +02:00
simplifyKnownVariables ( code ) ) ;
2008-12-22 19:08:32 +01:00
}
2008-12-18 22:28:57 +01:00
2008-12-22 19:08:32 +01:00
void simplifyKnownVariables3 ( )
{
const char code [ ] = " void f() \n "
" { \n "
" int a = 4; \n "
" while(true){ \n "
" break; \n "
" a = 10; \n "
" } \n "
" if (a); \n "
" } \n " ;
2008-12-18 22:28:57 +01:00
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2009-09-30 20:42:14 +02:00
" void f ( ) { int a ; a = 4 ; while ( true ) { break ; a = 10 ; } if ( a ) { ; } } " ,
2009-09-19 13:21:50 +02:00
simplifyKnownVariables ( code ) ) ;
2008-12-22 19:08:32 +01:00
}
2008-12-18 22:28:57 +01:00
2008-12-22 19:08:32 +01:00
void simplifyKnownVariables4 ( )
{
const char code [ ] = " void f() \n "
" { \n "
" int a = 4; \n "
" if ( g(a)); \n "
" } \n " ;
2008-12-18 22:28:57 +01:00
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2009-09-30 20:42:14 +02:00
" void f ( ) { int a ; a = 4 ; if ( g ( 4 ) ) { ; } } " ,
2009-09-19 13:21:50 +02:00
simplifyKnownVariables ( code ) ) ;
2008-12-22 19:08:32 +01:00
}
2008-12-18 22:28:57 +01:00
2008-12-22 19:08:32 +01:00
void simplifyKnownVariables5 ( )
{
const char code [ ] = " void f() \n "
" { \n "
" int a = 4; \n "
" if ( a = 5 ); \n "
" } \n " ;
2008-12-18 22:28:57 +01:00
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2009-09-30 20:42:14 +02:00
" void f ( ) { int a ; a = 4 ; if ( a = 5 ) { ; } } " ,
2009-09-19 13:21:50 +02:00
simplifyKnownVariables ( code ) ) ;
2008-12-18 22:28:57 +01:00
}
2009-02-20 18:27:57 +01:00
void simplifyKnownVariables6 ( )
{
const char code [ ] = " void f() \n "
" { \n "
" char str[2]; "
" int a = 4; \n "
" str[a] = 0; \n "
" } \n " ;
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2009-09-19 13:21:50 +02:00
" void f ( ) { char str [ 2 ] ; int a ; a = 4 ; str [ 4 ] = 0 ; } " ,
simplifyKnownVariables ( code ) ) ;
2009-02-20 18:27:57 +01:00
}
2009-02-24 17:11:37 +01:00
void simplifyKnownVariables7 ( )
{
const char code [ ] = " void foo() \n "
" { \n "
" int i = 22; \n "
" abc[i++] = 1; \n "
" abc[++i] = 2; \n "
" } \n " ;
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2009-09-19 13:21:50 +02:00
" void foo ( ) { int i ; i = 24 ; abc [ 22 ] = 1 ; abc [ 24 ] = 2 ; } " ,
simplifyKnownVariables ( code ) ) ;
2009-02-24 17:11:37 +01:00
}
2009-02-27 19:25:47 +01:00
void simplifyKnownVariables8 ( )
{
const char code [ ] = " void foo() \n "
" { \n "
" int i = 22; \n "
" i++; \n "
" abc[i] = 0; \n "
" } \n " ;
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2009-09-19 13:21:50 +02:00
" void foo ( ) { int i ; i = 23 ; ; abc [ 23 ] = 0 ; } " ,
simplifyKnownVariables ( code ) ) ;
2009-03-25 07:10:17 +01:00
}
void simplifyKnownVariables9 ( )
{
const char code [ ] = " void foo() \n "
" { \n "
" int a = 1, b = 2; \n "
" if (a < b) \n "
" ; \n "
" } \n " ;
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2009-09-30 20:42:14 +02:00
" void foo ( ) { int a ; a = 1 ; int b ; b = 2 ; if ( 1 < 2 ) { ; } } " ,
2009-09-19 13:21:50 +02:00
simplifyKnownVariables ( code ) ) ;
2009-02-27 19:25:47 +01:00
}
2009-05-01 20:31:07 +02:00
void simplifyKnownVariables10 ( )
{
{
const char code [ ] = " void f() \n "
" { \n "
" bool b=false; \n "
" \n "
" { \n "
" b = true; \n "
" } \n "
" \n "
" if( b ) \n "
" { \n "
" a(); \n "
" } \n "
" } \n " ;
2009-09-19 08:55:41 +02:00
TODO_ASSERT_EQUALS (
2009-09-19 13:21:50 +02:00
" void f ( ) { bool b ; b = false ; { b = true ; } if ( true ) { a ( ) ; } } " ,
simplifyKnownVariables ( code ) ) ;
2009-05-01 20:31:07 +02:00
}
2009-02-24 17:11:37 +01:00
2009-05-01 20:31:07 +02:00
{
const char code [ ] = " void f() \n "
" { \n "
" bool b=false; \n "
" { b = false; } \n "
" { \n "
" b = true; \n "
" } \n "
" \n "
" if( b ) \n "
" { \n "
" a(); \n "
" } \n "
" } \n " ;
2009-09-19 08:55:41 +02:00
TODO_ASSERT_EQUALS (
2009-09-19 13:21:50 +02:00
" void f ( ) { bool b ; b = false ; { b = false ; } { b = true ; } if ( true ) { a ( ) ; } } " ,
simplifyKnownVariables ( code ) ) ;
2009-05-01 20:31:07 +02:00
}
2009-05-03 20:57:27 +02:00
{
const char code [ ] = " void f() \n "
" { \n "
" int b=0; \n "
" b = 1; \n "
" for( int i = 0; i < 10; i++ ) "
" { \n "
" } \n "
" \n "
" a(b); \n "
" } \n " ;
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2009-09-19 13:21:50 +02:00
" void f ( ) { int b ; b = 0 ; b = 1 ; for ( int i = 0 ; i < 10 ; i ++ ) { } a ( 1 ) ; } " ,
simplifyKnownVariables ( code ) ) ;
2009-05-03 20:57:27 +02:00
}
2009-05-01 20:31:07 +02:00
}
2009-02-24 17:11:37 +01:00
2009-06-19 18:03:09 +02:00
void simplifyKnownVariables11 ( )
{
const char code [ ] = " const int foo = 0; \n "
" int main() \n "
" { \n "
" int foo=0; \n "
" } \n " ;
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2009-09-19 13:21:50 +02:00
" const int foo = 0 ; int main ( ) { int foo ; foo = 0 ; } " ,
simplifyKnownVariables ( code ) ) ;
2009-06-19 18:03:09 +02:00
}
2009-08-01 15:57:54 +02:00
void simplifyKnownVariables12 ( )
{
const char code [ ] = " ENTER_NAMESPACE(project_namespace) \n "
" const double pi = 3.14; \n "
" int main(){} \n " ;
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2009-09-19 13:21:50 +02:00
" ENTER_NAMESPACE ( project_namespace ) const double pi = 3.14 ; int main ( ) { } " ,
simplifyKnownVariables ( code ) ) ;
2009-08-01 15:57:54 +02:00
}
2009-08-22 10:23:55 +02:00
void simplifyKnownVariables13 ( )
{
const char code [ ] = " void f() \n "
" { \n "
" int i = 10; \n "
" while(--i) {} \n "
" } \n " ;
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2009-09-19 13:21:50 +02:00
" void f ( ) { int i ; i = 10 ; while ( -- i ) { } } " ,
simplifyKnownVariables ( code ) ) ;
2009-09-28 17:15:31 +02:00
}
void simplifyKnownVariables14 ( )
{
// ticket #753
const char code [ ] = " void f ( ) { int n ; n = 1 ; do { ++ n ; } while ( n < 10 ) ; } " ;
ASSERT_EQUALS ( code , simplifyKnownVariables ( code ) ) ;
2009-08-22 10:23:55 +02:00
}
2009-10-06 08:14:59 +02:00
void simplifyKnownVariables15 ( )
{
{
const char code [ ] = " int main() \n "
" { \n "
" int x=5; \n "
" std::cout << 10 / x << std::endl; \n "
" } \n " ;
ASSERT_EQUALS (
" int main ( ) { int x ; x = 5 ; std :: cout << 10 / 5 << std :: endl ; } " ,
simplifyKnownVariables ( code ) ) ;
}
{
const char code [ ] = " int main() \n "
" { \n "
" int x=5; \n "
" std::cout << x / ( x == 1 ) << std::endl; \n "
" } \n " ;
ASSERT_EQUALS (
" int main ( ) { int x ; x = 5 ; std :: cout << 5 / ( 5 == 1 ) << std :: endl ; } " ,
simplifyKnownVariables ( code ) ) ;
}
}
2009-08-22 10:23:55 +02:00
2009-10-10 10:13:56 +02:00
void simplifyKnownVariables16 ( )
{
// ticket #807 - segmentation fault when macro isn't found
const char code [ ] = " void f ( ) { int n = 1; DISPATCH(while); } " ;
simplifyKnownVariables ( code ) ;
}
2009-11-02 23:07:15 +01:00
void simplifyKnownVariables17 ( )
{
// ticket #807 - segmentation fault when macro isn't found
const char code [ ] = " void f ( ) { char *s = malloc(100);mp_ptr p = s; p++; } " ;
ASSERT_EQUALS (
" void f ( ) { char * s ; s = malloc ( 100 ) ; mp_ptr p ; p = s ; p ++ ; } " ,
simplifyKnownVariables ( code ) ) ;
}
2010-01-15 22:46:47 +01:00
void simplifyKnownVariables18 ( )
{
const char code [ ] = " void f ( ) { char *s = malloc(100);mp_ptr p = s; ++p; } " ;
ASSERT_EQUALS (
" void f ( ) { char * s ; s = malloc ( 100 ) ; mp_ptr p ; p = s ; ++ p ; } " ,
simplifyKnownVariables ( code ) ) ;
}
2010-01-24 19:27:39 +01:00
void simplifyKnownVariables19 ( )
{
const char code [ ] = " void f ( ) { int i=0; do { if (i>0) { a(); } i=b(); } while (i != 12); } " ;
ASSERT_EQUALS (
" void f ( ) { int i ; i = 0 ; do { if ( 0 < i ) { a ( ) ; } i = b ( ) ; } while ( i != 12 ) ; } " ,
simplifyKnownVariables ( code ) ) ;
}
2008-12-18 22:28:57 +01:00
void match1 ( )
{
// Match "%var% | %var%"
{
const std : : string code ( " abc|def " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
// Match..
2009-01-05 16:49:57 +01:00
ASSERT_EQUALS ( true , Token : : Match ( tokenizer . tokens ( ) , " %var% | %var% " ) ) ;
2008-12-18 22:28:57 +01:00
}
// Match "%var% || %var%"
{
const std : : string code ( " abc||def " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
// Match..
2009-01-05 16:49:57 +01:00
ASSERT_EQUALS ( true , Token : : Match ( tokenizer . tokens ( ) , " %var% || %var% " ) ) ;
2008-12-18 22:28:57 +01:00
}
}
void match2 ( )
{
{
const std : : string code ( " " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
// Match..
2009-01-05 16:49:57 +01:00
ASSERT_EQUALS ( true , Token : : Match ( tokenizer . tokens ( ) , " !!else " ) ) ;
2008-12-18 22:28:57 +01:00
}
2009-01-10 01:33:48 +01:00
{
const std : : string code ( " " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
// Match..
ASSERT_EQUALS ( false , Token : : Match ( tokenizer . tokens ( ) , " !!else something " ) ) ;
}
2009-01-10 22:13:10 +01:00
{
const std : : string code ( " if ; " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
// Match..
ASSERT_EQUALS ( true , Token : : Match ( tokenizer . tokens ( ) , " !!return if " ) ) ;
}
2008-12-18 22:28:57 +01:00
{
const std : : string code ( " if ; " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
// Match..
2009-01-05 16:49:57 +01:00
ASSERT_EQUALS ( true , Token : : Match ( tokenizer . tokens ( ) , " if ; !!else " ) ) ;
2008-12-18 22:28:57 +01:00
}
{
const std : : string code ( " if ; something " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
// Match..
2009-01-05 16:49:57 +01:00
ASSERT_EQUALS ( true , Token : : Match ( tokenizer . tokens ( ) , " if ; !!else " ) ) ;
2008-12-18 22:28:57 +01:00
}
{
const std : : string code ( " else " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
// Match..
2009-01-05 16:49:57 +01:00
ASSERT_EQUALS ( false , Token : : Match ( tokenizer . tokens ( ) , " !!else " ) ) ;
2008-12-18 22:28:57 +01:00
}
{
const std : : string code ( " if ; else " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
// Match..
2009-01-05 16:49:57 +01:00
ASSERT_EQUALS ( false , Token : : Match ( tokenizer . tokens ( ) , " if ; !!else " ) ) ;
2008-12-18 22:28:57 +01:00
}
}
void varid1 ( )
{
2009-09-30 13:40:10 +02:00
{
const std : : string code ( " static int i = 1; \n "
" void f() \n "
" { \n "
" int i = 2; \n "
" for (int i = 0; i < 10; ++i) \n "
" i = 3; \n "
" i = 4; \n "
" } \n " ) ;
2008-12-18 22:28:57 +01:00
2009-09-30 13:40:10 +02:00
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
// result..
const std : : string actual ( tokenizer . tokens ( ) - > stringifyList ( true ) ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: static int i@1 = 1 ; \n "
" 2: void f ( ) \n "
" 3: { \n "
" 4: int i@2 ; i@2 = 2 ; \n "
2009-09-30 20:42:14 +02:00
" 5: for ( int i@3 = 0 ; i@3 < 10 ; ++ i@3 ) { \n "
" 6: i@3 = 3 ; } \n "
2009-09-30 13:40:10 +02:00
" 7: i@2 = 4 ; \n "
" 8: } \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
2008-12-18 22:28:57 +01:00
2009-09-30 13:40:10 +02:00
{
const std : : string code ( " static int i = 1; \n "
" void f() \n "
" { \n "
" int i = 2; \n "
" for (int i = 0; i < 10; ++i) \n "
" { \n "
" i = 3; \n "
" } \n "
" i = 4; \n "
" } \n " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
// result..
const std : : string actual ( tokenizer . tokens ( ) - > stringifyList ( true ) ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: static int i@1 = 1 ; \n "
" 2: void f ( ) \n "
" 3: { \n "
" 4: int i@2 ; i@2 = 2 ; \n "
" 5: for ( int i@3 = 0 ; i@3 < 10 ; ++ i@3 ) \n "
" 6: { \n "
" 7: i@3 = 3 ; \n "
" 8: } \n "
" 9: i@2 = 4 ; \n "
" 10: } \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
2008-12-18 22:28:57 +01:00
}
void varid2 ( )
{
const std : : string code ( " void f() \n "
" { \n "
" struct ABC abc; \n "
" abc.a = 3; \n "
" i = abc.a; \n "
2009-01-05 16:49:57 +01:00
" } \n " ) ;
2008-12-18 22:28:57 +01:00
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
2009-02-13 07:25:29 +01:00
// result..
const std : : string actual ( tokenizer . tokens ( ) - > stringifyList ( true ) ) ;
2009-03-03 21:17:23 +01:00
const std : : string expected ( " \n \n ##file 0 \n "
2009-02-13 07:25:29 +01:00
" 1: void f ( ) \n "
" 2: { \n "
" 3: struct ABC abc@1 ; \n "
" 4: abc@1 . a@2 = 3 ; \n "
" 5: i = abc@1 . a@2 ; \n "
" 6: } \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
2008-12-18 22:28:57 +01:00
}
2009-01-19 19:40:24 +01:00
2009-02-02 18:27:34 +01:00
void varid3 ( )
{
const std : : string code ( " static char str[4]; \n "
" void f() \n "
" { \n "
" char str[10]; \n "
" str[0] = 0; \n "
" } \n " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
2009-02-13 07:25:29 +01:00
// result..
const std : : string actual ( tokenizer . tokens ( ) - > stringifyList ( true ) ) ;
2009-03-03 21:17:23 +01:00
const std : : string expected ( " \n \n ##file 0 \n "
2009-02-13 07:25:29 +01:00
" 1: static char str@1 [ 4 ] ; \n "
" 2: void f ( ) \n "
" 3: { \n "
" 4: char str@2 [ 10 ] ; \n "
" 5: str@2 [ 0 ] = 0 ; \n "
" 6: } \n " ) ;
2009-02-13 17:23:02 +01:00
ASSERT_EQUALS ( expected , actual ) ;
}
void varid4 ( )
{
const std : : string code ( " void f(const unsigned int a[]) \n "
" { \n "
" int i = *(a+10); \n "
" } \n " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyTokenList ( ) ;
// result..
const std : : string actual ( tokenizer . tokens ( ) - > stringifyList ( true ) ) ;
2009-03-03 21:17:23 +01:00
const std : : string expected ( " \n \n ##file 0 \n "
2009-02-13 17:23:02 +01:00
" 1: void f ( const int a@1 [ ] ) \n "
" 2: { \n "
" 3: int i@2 ; i@2 = a@1 [ 10 ] ; \n "
" 4: } \n " ) ;
2009-02-13 18:25:23 +01:00
ASSERT_EQUALS ( expected , actual ) ;
}
void varid5 ( )
{
const std : : string code ( " void f() \n "
" { \n "
" int a,b; \n "
" } \n " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyTokenList ( ) ;
// result..
const std : : string actual ( tokenizer . tokens ( ) - > stringifyList ( true ) ) ;
2009-03-03 21:17:23 +01:00
const std : : string expected ( " \n \n ##file 0 \n "
2009-02-13 18:25:23 +01:00
" 1: void f ( ) \n "
" 2: { \n "
2010-01-31 09:33:57 +01:00
" 3: ; ; \n "
2009-02-13 18:25:23 +01:00
" 4: } \n " ) ;
2009-02-13 07:25:29 +01:00
ASSERT_EQUALS ( expected , actual ) ;
2009-02-02 18:27:34 +01:00
}
2009-01-19 19:40:24 +01:00
2009-02-16 21:46:24 +01:00
void varid6 ( )
{
const std : : string code ( " int f(int a, int b) \n "
" { \n "
" return a+b; \n "
" } \n " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyTokenList ( ) ;
// result..
const std : : string actual ( tokenizer . tokens ( ) - > stringifyList ( true ) ) ;
2009-03-03 21:17:23 +01:00
const std : : string expected ( " \n \n ##file 0 \n "
2009-03-25 18:15:49 +01:00
" 1: int f ( int a@1 , int b@2 ) \n "
2009-02-16 21:46:24 +01:00
" 2: { \n "
" 3: return a@1 + b@2 ; \n "
" 4: } \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
2009-03-01 17:37:02 +01:00
void varid7 ( )
{
const std : : string code ( " void func() \n "
" { \n "
2009-03-15 22:09:27 +01:00
" char a[256] = \" test \" ; \n "
" { \n "
" char b[256] = \" test \" ; \n "
" } \n "
2009-03-01 17:37:02 +01:00
" } \n " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
// result..
const std : : string actual ( tokenizer . tokens ( ) - > stringifyList ( true ) ) ;
2009-03-03 21:17:23 +01:00
const std : : string expected ( " \n \n ##file 0 \n "
2009-03-01 17:37:02 +01:00
" 1: void func ( ) \n "
" 2: { \n "
" 3: char a@1 [ 256 ] = \" test \" ; \n "
" 4: { \n "
" 5: char b@2 [ 256 ] = \" test \" ; \n "
" 6: } \n "
" 7: } \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
2009-02-28 21:21:48 +01:00
void varidReturn ( )
{
const std : : string code ( " int f() \n "
" { \n "
" int a; \n "
" return a; \n "
" } \n " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyTokenList ( ) ;
// result..
const std : : string actual ( tokenizer . tokens ( ) - > stringifyList ( true ) ) ;
2009-03-03 21:17:23 +01:00
const std : : string expected ( " \n \n ##file 0 \n "
2009-02-28 21:21:48 +01:00
" 1: int f ( ) \n "
" 2: { \n "
" 3: int a@1 ; \n "
" 4: return a@1 ; \n "
" 5: } \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
2009-03-25 18:10:03 +01:00
void varid8 ( )
{
const std : : string code ( " void func() \n "
" { \n "
" std::string str( \" test \" ); \n "
" str.clear(); \n "
" } \n " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
// result..
const std : : string actual ( tokenizer . tokens ( ) - > stringifyList ( true ) ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: void func ( ) \n "
" 2: { \n "
" 3: std :: string str@1 ( \" test \" ) ; \n "
2009-05-27 22:34:08 +02:00
" 4: str@1 . clear ( ) ; \n "
2009-03-25 18:10:03 +01:00
" 5: } \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
2009-02-16 21:46:24 +01:00
2009-03-28 21:07:33 +01:00
void varid9 ( )
{
const std : : string code ( " typedef int INT32; \n " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
// result..
const std : : string actual ( tokenizer . tokens ( ) - > stringifyList ( true ) ) ;
const std : : string expected ( " \n \n ##file 0 \n "
2010-01-16 09:18:21 +01:00
" 1: ; \n " ) ;
2009-03-28 21:07:33 +01:00
ASSERT_EQUALS ( expected , actual ) ;
2009-04-25 16:55:00 +02:00
}
2009-05-31 18:46:32 +02:00
void varid10 ( )
{
const std : : string code ( " void foo() \n "
" { \n "
" int abc; \n "
" struct abc abc1; \n "
" } " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
// result..
const std : : string actual ( tokenizer . tokens ( ) - > stringifyList ( true ) ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: void foo ( ) \n "
" 2: { \n "
" 3: int abc@1 ; \n "
" 4: struct abc abc1@2 ; \n "
" 5: } \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
2009-08-11 20:58:49 +02:00
void varid11 ( )
{
const std : : string code ( " class Foo; \n " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
// result..
const std : : string actual ( tokenizer . tokens ( ) - > stringifyList ( true ) ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: class Foo ; \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
2009-09-22 17:49:13 +02:00
void varid12 ( )
{
const std : : string code ( " static void a() \n "
" { \n "
" class Foo *foo; \n "
" } \n " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
// result..
const std : : string actual ( tokenizer . tokens ( ) - > stringifyList ( true ) ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: static void a ( ) \n "
" 2: { \n "
" 3: class Foo * foo@1 ; \n "
" 4: } \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
2009-12-30 20:15:44 +01:00
void varid13 ( )
{
const std : : string code ( " void f() \n "
" { \n "
" int a; int b; \n "
" a = a; \n "
" } \n " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyTokenList ( ) ;
// result..
const std : : string actual ( tokenizer . tokens ( ) - > stringifyList ( true ) ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: void f ( ) \n "
" 2: { \n "
2010-01-31 09:33:57 +01:00
" 3: int a@1 ; ; \n "
2009-12-30 20:15:44 +01:00
" 4: a@1 = a@1 ; \n "
" 5: } \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
2010-02-05 23:24:57 +01:00
void varid14 ( )
{
// Overloaded operator*
const std : : string code ( " void foo() \n "
" { \n "
" A a; \n "
" B b; \n "
" b * a; \n "
" } " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
// result..
const std : : string actual ( tokenizer . tokens ( ) - > stringifyList ( true ) ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: void foo ( ) \n "
" 2: { \n "
" 3: A a@1 ; \n "
" 4: B b@2 ; \n "
2010-02-05 23:30:47 +01:00
" 5: b@2 * a@1 ; \n "
" 6: } \n " ) ;
2010-02-05 23:24:57 +01:00
TODO_ASSERT_EQUALS ( expected , actual ) ;
}
2009-04-25 16:55:00 +02:00
void varidStl ( )
{
const std : : string code ( " list<int> ints; \n "
2009-07-28 19:54:13 +02:00
" list<int>::iterator it; \n "
2009-07-30 19:25:26 +02:00
" std::vector<std::string> dirs; \n "
2009-07-30 19:40:41 +02:00
" std::map<int, int> coords; \n "
" std::tr1::unordered_map<int, int> xy; \n "
" std::list<boost::wave::token_id> tokens; \n "
2009-07-31 19:39:55 +02:00
" static std::vector<CvsProcess*> ex1; \n "
" extern std::vector<CvsProcess*> ex2; \n "
2009-08-01 18:17:35 +02:00
" std::map<int, 1> m; \n "
2009-07-31 14:01:50 +02:00
) ;
2009-04-25 16:55:00 +02:00
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . setVarId ( ) ;
// result..
const std : : string actual ( tokenizer . tokens ( ) - > stringifyList ( true ) ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: list < int > ints@1 ; \n "
2009-07-28 19:54:13 +02:00
" 2: list < int > :: iterator it@2 ; \n "
2009-07-30 19:25:26 +02:00
" 3: std :: vector < std :: string > dirs@3 ; \n "
2009-07-30 19:40:41 +02:00
" 4: std :: map < int , int > coords@4 ; \n "
" 5: std :: tr1 :: unordered_map < int , int > xy@5 ; \n "
" 6: std :: list < boost :: wave :: token_id > tokens@6 ; \n "
2009-07-31 19:39:55 +02:00
" 7: static std :: vector < CvsProcess * > ex1@7 ; \n "
" 8: extern std :: vector < CvsProcess * > ex2@8 ; \n "
2009-08-01 18:17:35 +02:00
" 9: std :: map < int , 1 > m@9 ; \n "
2009-07-31 14:01:50 +02:00
) ;
2009-04-25 16:55:00 +02:00
ASSERT_EQUALS ( expected , actual ) ;
2009-03-28 21:07:33 +01:00
}
2009-04-29 21:22:56 +02:00
void varid_delete ( )
{
const std : : string code ( " void f() \n "
" { \n "
" int *a; \n "
" delete a; \n "
" } \n " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . setVarId ( ) ;
2009-03-28 21:07:33 +01:00
2009-04-29 21:22:56 +02:00
// result..
const std : : string actual ( tokenizer . tokens ( ) - > stringifyList ( true ) ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: void f ( ) \n "
" 2: { \n "
" 3: int * a@1 ; \n "
" 4: delete a@1 ; \n "
" 5: } \n " ) ;
2009-04-29 21:45:57 +02:00
ASSERT_EQUALS ( expected , actual ) ;
2009-05-02 22:57:18 +02:00
}
2009-05-27 22:34:08 +02:00
void varid_functions ( )
{
{
const std : : string code ( " void f(); \n "
" void f(){} \n " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . setVarId ( ) ;
// result..
const std : : string actual ( tokenizer . tokens ( ) - > stringifyList ( true ) ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: void f ( ) ; \n "
" 2: void f ( ) { } \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
{
const std : : string code ( " A f(3); \n "
" A f2(true); \n "
" A g(); \n "
" A e(int c); \n " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . setVarId ( ) ;
// result..
const std : : string actual ( tokenizer . tokens ( ) - > stringifyList ( true ) ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: A f@1 ( 3 ) ; \n "
" 2: A f2@2 ( true ) ; \n "
" 3: A g ( ) ; \n "
" 4: A e ( int c@3 ) ; \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
2009-06-18 20:05:42 +02:00
2009-06-18 18:30:04 +02:00
{
const std : : string code ( " void f1(int &p) \n "
" { \n "
" p = 0; \n "
" } \n "
" void f2(std::string &str) \n "
" { \n "
" str.clear(); \n "
" } \n "
" void f3(const std::string &s) \n "
" { \n "
" s.size(); \n "
" } \n " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . setVarId ( ) ;
// result..
const std : : string actual ( tokenizer . tokens ( ) - > stringifyList ( true ) ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: void f1 ( int & p@1 ) \n "
" 2: { \n "
" 3: p@1 = 0 ; \n "
" 4: } \n "
" 5: void f2 ( std :: string & str@2 ) \n "
" 6: { \n "
" 7: str@2 . clear ( ) ; \n "
" 8: } \n "
" 9: void f3 ( const std :: string & s@3 ) \n "
" 10: { \n "
" 11: s@3 . size ( ) ; \n "
" 12: } \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
2009-05-27 22:34:08 +02:00
}
2009-05-02 22:57:18 +02:00
2009-07-18 12:37:51 +02:00
void varid_reference_to_containers ( )
{
const std : : string code ( " void f() \n "
" { \n "
2009-07-18 20:35:22 +02:00
" std::vector<int> b; \n "
" std::vector<int> &a = b; \n "
" std::vector<int> *c = &b; \n "
" } \n " ) ;
2009-07-18 12:37:51 +02:00
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . setVarId ( ) ;
// result..
const std : : string actual ( tokenizer . tokens ( ) - > stringifyList ( true ) ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: void f ( ) \n "
" 2: { \n "
" 3: std :: vector < int > b@1 ; \n "
" 4: std :: vector < int > & a@2 = b@1 ; \n "
" 5: std :: vector < int > * c@3 = & b@1 ; \n "
" 6: } \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
2009-08-09 10:41:31 +02:00
void varid_in_class ( )
{
const std : : string code ( " class Foo \n "
" { \n "
" public: \n "
" std::string name1; \n "
" std::string name2; \n "
" }; \n " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . setVarId ( ) ;
// result..
const std : : string actual ( tokenizer . tokens ( ) - > stringifyList ( true ) ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: class Foo \n "
" 2: { \n "
" 3: public: \n "
" 4: std :: string name1@1 ; \n "
" 5: std :: string name2@2 ; \n "
" 6: } ; \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
2009-05-03 13:50:26 +02:00
void varidclass1 ( )
2009-05-02 22:57:18 +02:00
{
const std : : string code ( " class Fred \n "
" { \n "
" private: \n "
" int i; \n "
" \n "
" void foo1(); \n "
" void foo2() \n "
" { \n "
" ++i; \n "
" } \n "
" } \n "
" \n "
" Fred::foo1() \n "
" { \n "
" i = 0; \n "
" } \n " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . setVarId ( ) ;
// result..
const std : : string actual ( tokenizer . tokens ( ) - > stringifyList ( true ) ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: class Fred \n "
" 2: { \n "
" 3: private: \n "
" 4: int i@1 ; \n "
" 5: \n "
2009-05-27 22:34:08 +02:00
" 6: void foo1 ( ) ; \n "
2009-05-02 22:57:18 +02:00
" 7: void foo2 ( ) \n "
" 8: { \n "
" 9: ++ i@1 ; \n "
" 10: } \n "
" 11: } \n "
" 12: \n "
" 13: Fred :: foo1 ( ) \n "
" 14: { \n "
" 15: i@1 = 0 ; \n "
" 16: } \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
2009-05-03 13:50:26 +02:00
}
void varidclass2 ( )
{
const std : : string code ( " class Fred \n "
" { void f(); }; \n "
" \n "
" void A::foo1() \n "
" { \n "
" int i = 0; \n "
" } \n "
" \n "
" void Fred::f() \n "
" { \n "
" i = 0; \n "
" } \n " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . setVarId ( ) ;
// result..
const std : : string actual ( tokenizer . tokens ( ) - > stringifyList ( true ) ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: class Fred \n "
2009-05-27 22:34:08 +02:00
" 2: { void f ( ) ; } ; \n "
2009-05-03 13:50:26 +02:00
" 3: \n "
" 4: void A :: foo1 ( ) \n "
" 5: { \n "
2009-05-27 22:34:08 +02:00
" 6: int i@1 ; i@1 = 0 ; \n "
2009-05-03 13:50:26 +02:00
" 7: } \n "
" 8: \n "
" 9: void Fred :: f ( ) \n "
" 10: { \n "
" 11: i = 0 ; \n "
" 12: } \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
void varidclass3 ( )
{
const std : : string code ( " class Fred \n "
" { int i; void f(); }; \n "
" \n "
" void Fred::f() \n "
" { \n "
" i = 0; \n "
" } \n "
" \n "
" void A::f() \n "
" { \n "
" i = 0; \n "
" } \n " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . setVarId ( ) ;
// result..
const std : : string actual ( tokenizer . tokens ( ) - > stringifyList ( true ) ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: class Fred \n "
2009-05-27 22:34:08 +02:00
" 2: { int i@1 ; void f ( ) ; } ; \n "
2009-05-03 13:50:26 +02:00
" 3: \n "
" 4: void Fred :: f ( ) \n "
" 5: { \n "
" 6: i@1 = 0 ; \n "
" 7: } \n "
" 8: \n "
" 9: void A :: f ( ) \n "
" 10: { \n "
" 11: i = 0 ; \n "
" 12: } \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
void varidclass4 ( )
{
const std : : string code ( " class Fred \n "
" { int i; void f(); }; \n "
" \n "
" void Fred::f() \n "
" { \n "
" if (i) { } \n "
" i = 0; \n "
" } \n " ) ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . setVarId ( ) ;
// result..
const std : : string actual ( tokenizer . tokens ( ) - > stringifyList ( true ) ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: class Fred \n "
2009-05-27 22:34:08 +02:00
" 2: { int i@1 ; void f ( ) ; } ; \n "
2009-05-03 13:50:26 +02:00
" 3: \n "
" 4: void Fred :: f ( ) \n "
" 5: { \n "
" 6: if ( i@1 ) { } \n "
" 7: i@1 = 0 ; \n "
" 8: } \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
2009-04-29 21:22:56 +02:00
}
2009-02-16 21:46:24 +01:00
2009-01-19 19:40:24 +01:00
void file1 ( )
{
const char code [ ] = " a1 \n "
" #file \" b \" \n "
" b1 \n "
" b2 \n "
" #endfile \n "
" a3 \n " ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " a " ) ;
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) )
{
std : : ostringstream ostr ;
ostr < < char ( ' a ' + tok - > fileIndex ( ) ) < < tok - > linenr ( ) ;
ASSERT_EQUALS ( tok - > str ( ) , ostr . str ( ) ) ;
}
}
2009-01-20 18:05:42 +01:00
void file2 ( )
{
const char code [ ] = " a1 \n "
" #file \" b \" \n "
" b1 \n "
" b2 \n "
" #file \" c \" \n "
" c1 \n "
" c2 \n "
" #endfile \n "
" b4 \n "
" #endfile \n "
" a3 \n "
" #file \" d \" \n "
" d1 \n "
" #endfile \n "
" a5 \n " ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " a " ) ;
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) )
{
std : : ostringstream ostr ;
ostr < < char ( ' a ' + tok - > fileIndex ( ) ) < < tok - > linenr ( ) ;
ASSERT_EQUALS ( tok - > str ( ) , ostr . str ( ) ) ;
}
}
2009-01-20 18:26:16 +01:00
2009-02-15 12:42:04 +01:00
void file3 ( )
{
const char code [ ] = " #file \" c: \\ a.h \" \n "
" 123 \n "
" #endfile \n " ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " a.cpp " ) ;
ASSERT_EQUALS ( " [c: \\ a.h:1] " , tokenizer . fileLine ( tokenizer . tokens ( ) ) ) ;
}
2009-01-20 18:26:16 +01:00
void doublesharp ( )
{
const char code [ ] = " TEST(var,val) var##_##val = val \n " ;
// Tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " " ) ;
// Stringify the tokens..
std : : ostringstream ostr ;
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) )
ostr < < tok - > str ( ) < < " " ;
ASSERT_EQUALS ( " TEST ( var , val ) var ## _ ## val = val " , ostr . str ( ) ) ;
}
2009-01-24 08:56:47 +01:00
2009-02-13 14:33:12 +01:00
void macrodoublesharp ( )
{
const char code [ ] = " DBG(fmt,args...) printf(fmt, ## args) \n " ;
// Tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " " ) ;
// Stringify the tokens..
std : : ostringstream ostr ;
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) )
ostr < < tok - > str ( ) < < " " ;
ASSERT_EQUALS ( " DBG ( fmt , args . . . ) printf ( fmt , ## args ) " , ostr . str ( ) ) ;
}
2009-01-24 08:56:47 +01:00
void simplify_function_parameters ( )
{
2009-12-31 19:31:21 +01:00
{
const char code [ ] = " char a [ ABC ( DEF ) ] ; " ;
ASSERT_EQUALS ( code , tokenizeAndStringify ( code , true ) ) ;
}
2010-01-01 13:26:54 +01:00
{
const char code [ ] = " module ( a , a , sizeof ( a ) , 0444 ) ; " ;
ASSERT_EQUALS ( code , tokenizeAndStringify ( code , true ) ) ;
}
2009-12-30 15:12:38 +01:00
ASSERT_EQUALS ( " void f ( int x ) { } " , tokenizeAndStringify ( " void f(x) int x; { } " , true ) ) ;
ASSERT_EQUALS ( " void f ( int x , char y ) { } " , tokenizeAndStringify ( " void f(x,y) int x; char y; { } " , true ) ) ;
2009-01-24 08:56:47 +01:00
2009-12-30 15:12:38 +01:00
// #1067 - Not simplified. Feel free to fix so it is simplified correctly but this syntax is obsolete.
ASSERT_EQUALS ( " int ( * d ( a , b , c ) ) ( ) int a ; int b ; int c ; { } " , tokenizeAndStringify ( " int (*d(a,b,c))()int a,b,c; { } " , true ) ) ;
2009-01-24 19:21:16 +01:00
{
// This is not a function but the pattern is similar..
2009-12-30 15:12:38 +01:00
const char code [ ] = " void foo() "
" { "
" if (x) "
" int x; "
" { } "
" } " ;
2010-01-31 09:33:57 +01:00
ASSERT_EQUALS ( " void foo ( ) { if ( x ) { ; } { } } " , tokenizeAndStringify ( code , true ) ) ;
2009-01-24 19:21:16 +01:00
}
2009-01-24 08:56:47 +01:00
}
2009-01-25 20:39:05 +01:00
// Simplify "((..))" into "(..)"
2009-05-28 19:37:39 +02:00
void removeParantheses1 ( )
2009-01-25 20:39:05 +01:00
{
const char code [ ] = " void foo() \n "
" { \n "
" free(((void*)p)); \n "
" } " ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyTokenList ( ) ;
std : : ostringstream ostr ;
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) )
ostr < < " " < < tok - > str ( ) ;
2009-05-31 21:48:55 +02:00
ASSERT_EQUALS ( " void foo ( ) { free ( p ) ; } " , ostr . str ( ) ) ;
2009-01-25 20:39:05 +01:00
}
2009-05-28 19:37:39 +02:00
void removeParantheses2 ( )
{
const char code [ ] = " void foo() \n "
" { \n "
" if (__builtin_expect((s == NULL), 0)) \n "
" return; \n "
" } " ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyTokenList ( ) ;
std : : ostringstream ostr ;
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) )
ostr < < " " < < tok - > str ( ) ;
2009-05-31 21:48:55 +02:00
ASSERT_EQUALS ( " void foo ( ) { if ( ! s ) { return ; } } " , ostr . str ( ) ) ;
2009-05-28 19:37:39 +02:00
}
2009-05-28 22:03:36 +02:00
void removeParantheses3 ( )
{
2009-05-29 23:04:01 +02:00
{
const char code [ ] = " void foo() \n "
" { \n "
" if (( true )==(true)){} \n "
" } " ;
2009-05-28 22:03:36 +02:00
2009-05-29 23:04:01 +02:00
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
2009-05-28 19:37:39 +02:00
2009-05-29 23:04:01 +02:00
tokenizer . simplifyTokenList ( ) ;
2009-02-04 20:31:25 +01:00
2009-05-29 23:04:01 +02:00
std : : ostringstream ostr ;
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) )
ostr < < " " < < tok - > str ( ) ;
2009-05-31 21:48:55 +02:00
ASSERT_EQUALS ( " void foo ( ) { { } } " , ostr . str ( ) ) ;
2009-05-29 23:04:01 +02:00
}
{
const char code [ ] = " void foo() \n "
" { \n "
" if (( 2 )==(2)){} \n "
" } " ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyTokenList ( ) ;
std : : ostringstream ostr ;
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) )
ostr < < " " < < tok - > str ( ) ;
2009-05-31 21:48:55 +02:00
ASSERT_EQUALS ( " void foo ( ) { { } } " , ostr . str ( ) ) ;
2009-05-29 23:04:01 +02:00
}
2009-05-31 14:55:06 +02:00
{
const char code [ ] = " void foo() \n "
" { \n "
" if( g(10)){} \n "
" } " ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyTokenList ( ) ;
std : : ostringstream ostr ;
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) )
ostr < < " " < < tok - > str ( ) ;
2009-05-31 21:48:55 +02:00
ASSERT_EQUALS ( " void foo ( ) { if ( g ( 10 ) ) { } } " , ostr . str ( ) ) ;
2009-05-31 14:55:06 +02:00
}
2009-05-28 22:03:36 +02:00
}
2009-02-02 07:21:48 +01:00
2009-06-12 15:04:58 +02:00
// Simplify "( function (..))" into "function (..)"
void removeParantheses4 ( )
{
const char code [ ] = " void foo() \n "
" { \n "
" (free(p)); \n "
" } " ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyTokenList ( ) ;
std : : ostringstream ostr ;
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) )
ostr < < " " < < tok - > str ( ) ;
ASSERT_EQUALS ( " void foo ( ) { free ( p ) ; } " , ostr . str ( ) ) ;
}
2009-06-12 16:14:01 +02:00
void removeParantheses5 ( )
{
// Simplify "( delete x )" into "delete x"
{
const char code [ ] = " void foo() \n "
" { \n "
" (delete p); \n "
" } " ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyTokenList ( ) ;
std : : ostringstream ostr ;
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) )
ostr < < " " < < tok - > str ( ) ;
ASSERT_EQUALS ( " void foo ( ) { delete p ; } " , ostr . str ( ) ) ;
}
// Simplify "( delete [] x )" into "delete [] x"
{
const char code [ ] = " void foo() \n "
" { \n "
" (delete [] p); \n "
" } " ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyTokenList ( ) ;
std : : ostringstream ostr ;
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) )
ostr < < " " < < tok - > str ( ) ;
ASSERT_EQUALS ( " void foo ( ) { delete [ ] p ; } " , ostr . str ( ) ) ;
}
}
2009-11-15 09:28:08 +01:00
// "!(abc.a)" => "!abc.a"
void removeParantheses6 ( )
{
const char code [ ] = " (!(abc.a)) " ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyTokenList ( ) ;
std : : ostringstream ostr ;
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) )
ostr < < " " < < tok - > str ( ) ;
ASSERT_EQUALS ( " ( ! abc . a ) " , ostr . str ( ) ) ;
}
2009-02-08 10:51:45 +01:00
void tokenize_double ( )
{
const char code [ ] = " void f() \n "
" { \n "
" double a = 4.2; \n "
" float b = 4.2f; \n "
2009-02-08 11:39:55 +01:00
" double c = 4.2e+10; \n "
" double d = 4.2e-10; \n "
2009-02-08 11:56:20 +01:00
" int e = 4+2; \n "
2009-02-08 10:51:45 +01:00
" } \n " ;
2009-02-07 20:15:10 +01:00
2009-02-08 10:51:45 +01:00
// tokenize..
2009-08-04 21:32:14 +02:00
Tokenizer tokenizer ;
2009-02-08 10:51:45 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
std : : ostringstream ostr ;
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) )
ostr < < " " < < tok - > str ( ) ;
2009-05-31 21:48:55 +02:00
ASSERT_EQUALS ( " void f ( ) { double a ; a = 4.2 ; float b ; b = 4.2f ; double c ; c = 4.2e+10 ; double d ; d = 4.2e-10 ; int e ; e = 4 + 2 ; } " , ostr . str ( ) ) ;
2009-02-08 10:51:45 +01:00
}
2009-02-08 11:25:33 +01:00
void tokenize_strings ( )
{
const char code [ ] = " void f() \n "
" { \n "
" const char *a = \n "
" { \n "
" \" hello \" \n "
" \" more \" \n "
" \" world \" \n "
" }; \n "
" } \n " ;
// tokenize..
2009-08-04 21:32:14 +02:00
Tokenizer tokenizer ;
2009-02-08 11:25:33 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyTokenList ( ) ;
std : : ostringstream ostr ;
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) )
ostr < < " " < < tok - > str ( ) ;
2009-05-31 21:48:55 +02:00
ASSERT_EQUALS ( " void f ( ) { const char * a ; a = { \" hello more world \" } ; } " , ostr . str ( ) ) ;
2009-02-08 11:25:33 +01:00
}
2009-02-11 23:15:22 +01:00
void simplify_constants ( )
{
const char code [ ] =
" void f() \n "
" { \n "
" const int a = 45; \n "
2009-02-12 20:26:42 +01:00
" if( a ) \n "
" { int b = a; } \n "
2009-02-11 23:15:22 +01:00
" } \n "
" void g() \n "
" { \n "
" int a = 2; \n "
" } \n " ;
// 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 ( ) )
ostr < < " " < < tok - > str ( ) ;
2010-01-31 09:52:19 +01:00
ASSERT_EQUALS ( " void f ( ) { const int a = 45 ; { ; ; } } void g ( ) { ; ; } " , ostr . str ( ) ) ;
2009-02-11 23:15:22 +01:00
}
2009-02-14 22:33:28 +01:00
void simplify_constants2 ( )
{
const char code [ ] =
" void f( Foo &foo, Foo *foo2 ) \n "
" { \n "
" const int a = 45; \n "
" foo.a=a+a; \n "
" foo2->a=a; \n "
" } \n " ;
// 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 ( ) )
ostr < < " " < < tok - > str ( ) ;
std : : ostringstream oss ;
oss < < " void f ( Foo & foo , Foo * foo2 ) { const int a = 45 ; foo . a = 90 ; foo2 . a = 45 ; } " ;
ASSERT_EQUALS ( oss . str ( ) , ostr . str ( ) ) ;
}
2009-03-13 22:38:42 +01:00
void findClassFunction1 ( )
{
const char code [ ] =
" class Fred "
" { \n "
2010-01-02 17:29:55 +01:00
" public: \n "
2009-03-13 22:38:42 +01:00
" Fred() \n "
" { } \n "
" }; \n " ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
int i ;
i = 0 ;
2010-01-29 22:22:18 +01:00
const Token * tok = tokenizer . findClassFunction ( tokenizer . tokens ( ) , " Fred " , " %var% " , i ) ;
2009-03-13 22:38:42 +01:00
ASSERT_EQUALS ( true , Token : : simpleMatch ( tok , " Fred ( ) { " ) ) ;
2010-01-29 22:22:18 +01:00
tok = tokenizer . findClassFunction ( tok - > next ( ) , " Fred " , " %var% " , i ) ;
2009-03-18 22:13:27 +01:00
ASSERT_EQUALS ( 0 , tok ? 1 : 0 ) ;
2009-03-13 22:38:42 +01:00
}
2010-01-02 17:29:55 +01:00
void findClassFunction2 ( )
{
const char code [ ] =
" struct Fred "
" { \n "
" Fred() \n "
" { } \n "
" }; \n " ;
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
int i ;
i = 0 ;
2010-01-29 22:22:18 +01:00
const Token * tok = tokenizer . findClassFunction ( tokenizer . tokens ( ) , " Fred " , " %var% " , i , true ) ;
2010-01-02 17:29:55 +01:00
ASSERT_EQUALS ( true , Token : : simpleMatch ( tok , " Fred ( ) { " ) ) ;
2010-01-29 22:22:18 +01:00
tok = tokenizer . findClassFunction ( tok - > next ( ) , " Fred " , " %var% " , i , false ) ;
2010-01-02 17:29:55 +01:00
ASSERT_EQUALS ( 0 , tok ? 1 : 0 ) ;
}
2009-03-16 19:03:23 +01:00
void vardecl1 ( )
{
const char code [ ] = " unsigned int a, b; " ;
2009-03-18 20:32:05 +01:00
const std : : string actual ( tokenizeAndStringify ( code ) ) ;
2009-03-16 19:03:23 +01:00
2009-03-18 20:32:05 +01:00
ASSERT_EQUALS ( " unsigned int a ; unsigned int b ; " , actual ) ;
2009-03-16 19:03:23 +01:00
}
void vardecl2 ( )
{
const char code [ ] = " void foo(a,b) unsigned int a, b; { } " ;
2009-03-18 20:32:05 +01:00
const std : : string actual ( tokenizeAndStringify ( code ) ) ;
2009-03-16 19:03:23 +01:00
2009-03-18 20:32:05 +01:00
ASSERT_EQUALS ( " void foo ( a , b ) unsigned int a ; unsigned int b ; { } " , actual ) ;
2009-03-16 19:03:23 +01:00
}
2009-04-20 20:38:05 +02:00
2009-06-14 14:57:47 +02:00
void vardecl3 ( )
{
const char code [ ] = " void f() { char * p = foo<10,char>(); } " ;
const std : : string actual ( tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " void f ( ) { char * p ; p = foo < 10 , char > ( ) ; } " , actual ) ;
}
2009-06-14 19:32:34 +02:00
void vardecl4 ( )
{
// ticket #346
const char code1 [ ] = " void *p = NULL; " ;
const char res1 [ ] = " void * p ; p = NULL ; " ;
ASSERT_EQUALS ( res1 , tokenizeAndStringify ( code1 ) ) ;
const char code2 [ ] = " const void *p = NULL; " ;
const char res2 [ ] = " const void * p ; p = NULL ; " ;
ASSERT_EQUALS ( res2 , tokenizeAndStringify ( code2 ) ) ;
const char code3 [ ] = " void * const p = NULL; " ;
const char res3 [ ] = " void * const p ; p = NULL ; " ;
ASSERT_EQUALS ( res3 , tokenizeAndStringify ( code3 ) ) ;
const char code4 [ ] = " const void * const p = NULL; " ;
const char res4 [ ] = " const void * const p ; p = NULL ; " ;
ASSERT_EQUALS ( res4 , tokenizeAndStringify ( code4 ) ) ;
}
2009-07-28 21:46:33 +02:00
void vardecl_stl ( )
{
// ticket #520
const char code1 [ ] = " std::vector<std::string>a, b; " ;
const char res1 [ ] = " std :: vector < std :: string > a ; std :: vector < std :: string > b ; " ;
ASSERT_EQUALS ( res1 , tokenizeAndStringify ( code1 ) ) ;
const char code2 [ ] = " std::vector<std::string>::const_iterator it, cit; " ;
const char res2 [ ] = " std :: vector < std :: string > :: const_iterator it ; std :: vector < std :: string > :: const_iterator cit ; " ;
ASSERT_EQUALS ( res2 , tokenizeAndStringify ( code2 ) ) ;
const char code3 [ ] = " std::vector<std::pair<std::string, std::string > > *c, d; " ;
const char res3 [ ] = " std :: vector < std :: pair < std :: string , std :: string > > * c ; std :: vector < std :: pair < std :: string , std :: string > > d ; " ;
ASSERT_EQUALS ( res3 , tokenizeAndStringify ( code3 ) ) ;
}
2009-12-05 21:15:14 +01:00
void vardecl_template ( )
{
// ticket #1046
const char code1 [ ] = " b<(1<<24),10,24> u, v; " ;
const char res1 [ ] = " b < ( 1 << 24 ) , 10 , 24 > u ; b < ( 1 << 24 ) , 10 , 24 > v ; " ;
ASSERT_EQUALS ( res1 , tokenizeAndStringify ( code1 ) ) ;
}
2009-10-02 16:28:30 +02:00
void vardec_static ( )
2009-07-06 11:45:14 +02:00
{
2009-10-02 16:28:30 +02:00
{
// don't simplify declarations of static variables
// "static int i = 0;" is not the same as "static int i; i = 0;"
const char code [ ] = " static int i = 0 ; " ;
ASSERT_EQUALS ( code , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " static int a, b; " ;
2009-10-03 12:03:54 +02:00
ASSERT_EQUALS ( " static int a ; static int b ; " , tokenizeAndStringify ( code ) ) ;
2009-10-02 16:28:30 +02:00
}
{
const char code [ ] = " static unsigned int a, b; " ;
2009-10-03 12:03:54 +02:00
ASSERT_EQUALS ( " static unsigned int a ; static unsigned int b ; " , tokenizeAndStringify ( code ) ) ;
2009-10-02 16:28:30 +02:00
}
{
const char code [ ] = " static int a=1, b=1; " ;
2009-10-03 12:03:54 +02:00
ASSERT_EQUALS ( " static int a = 1 ; static int b = 1 ; " , tokenizeAndStringify ( code ) ) ;
2009-10-02 16:28:30 +02:00
}
{
const char code [ ] = " static int *a, *b; " ;
2009-10-03 12:03:54 +02:00
ASSERT_EQUALS ( " static int * a ; static int * b ; " , tokenizeAndStringify ( code ) ) ;
2009-10-02 16:28:30 +02:00
}
{
const char code [ ] = " static unsigned int *a=0, *b=0; " ;
2009-10-03 12:03:54 +02:00
ASSERT_EQUALS ( " static unsigned int * a = 0 ; static unsigned int * b = 0 ; " , tokenizeAndStringify ( code ) ) ;
2009-10-02 16:28:30 +02:00
}
2009-07-06 11:45:14 +02:00
}
2009-08-08 12:33:07 +02:00
void vardecl6 ( )
{
// ticket #565
const char code1 [ ] = " int z = x >> 16; " ;
const char res1 [ ] = " int z ; z = x >> 16 ; " ;
ASSERT_EQUALS ( res1 , tokenizeAndStringify ( code1 ) ) ;
}
2009-08-23 08:26:16 +02:00
void vardecl7 ( )
{
// ticket #603
const char code [ ] = " for (int c = 0; c < 0; ++c) {} \n "
" int t; \n "
" D(3 > t, \" T \" ); " ;
const char res [ ] = " for ( int c = 0 ; c < 0 ; ++ c ) { } \n "
" int t ; \n "
" D ( 3 > t , \" T \" ) ; " ;
ASSERT_EQUALS ( res , tokenizeAndStringify ( code ) ) ;
}
2009-09-20 12:28:15 +02:00
void vardecl8 ( )
{
// ticket #696
const char code [ ] = " char a[10]={' \\ 0'}, b[10]={' \\ 0'}; " ;
const char res [ ] = " char a [ 10 ] = { ' \\ 0' } ; char b [ 10 ] = { ' \\ 0' } ; " ;
ASSERT_EQUALS ( res , tokenizeAndStringify ( code ) ) ;
}
2009-08-23 08:26:16 +02:00
2009-09-20 13:28:56 +02:00
void vardecl9 ( )
{
const char code [ ] = " char a[2] = {'A', ' \\ 0'}, b[2] = {'B', ' \\ 0'}; " ;
const char res [ ] = " char a [ 2 ] = { 'A' , ' \\ 0' } ; char b [ 2 ] = { 'B' , ' \\ 0' } ; " ;
ASSERT_EQUALS ( res , tokenizeAndStringify ( code ) ) ;
}
2009-09-26 12:02:13 +02:00
void vardecl10 ( )
{
// ticket #732
const char code [ ] = " char a [ 2 ] = { '-' } ; memset ( a , '-' , sizeof ( a ) ) ; " ;
ASSERT_EQUALS ( code , tokenizeAndStringify ( code ) ) ;
}
2009-04-20 20:38:05 +02:00
void volatile_variables ( )
{
const char code [ ] = " volatile int a=0; \n "
" volatile int b=0; \n "
" volatile int c=0; \n " ;
const std : : string actual ( tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " int a ; a = 0 ; \n int b ; b = 0 ; \n int c ; c = 0 ; " , actual ) ;
}
2009-05-07 22:17:29 +02:00
void syntax_error ( )
{
{
2009-05-11 21:52:04 +02:00
errout . str ( " " ) ;
2009-05-07 22:17:29 +02:00
const char code [ ] = " void f() {} " ;
2009-07-13 19:11:31 +02:00
Tokenizer tokenizer ( 0 , this ) ;
2009-05-07 22:17:29 +02:00
std : : istringstream istr ( code ) ;
ASSERT_EQUALS ( true , tokenizer . tokenize ( istr , " test.cpp " ) ) ;
2009-06-05 02:39:36 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-05-07 22:17:29 +02:00
}
{
2009-05-11 21:52:04 +02:00
errout . str ( " " ) ;
2009-05-07 22:17:29 +02:00
const char code [ ] = " void f() {{} " ;
2009-07-13 19:11:31 +02:00
Tokenizer tokenizer ( 0 , this ) ;
2009-05-07 22:17:29 +02:00
std : : istringstream istr ( code ) ;
ASSERT_EQUALS ( false , tokenizer . tokenize ( istr , " test.cpp " ) ) ;
2010-01-23 22:18:11 +01:00
ASSERT_EQUALS ( " [test.cpp:1]: (error) Invalid number of character ({) when these macros are defined: ''. \n " , errout . str ( ) ) ;
2009-05-07 22:17:29 +02:00
}
{
2009-05-11 21:52:04 +02:00
errout . str ( " " ) ;
2009-05-07 22:17:29 +02:00
const char code [ ] = " void f()) {} " ;
2009-07-13 19:11:31 +02:00
Tokenizer tokenizer ( 0 , this ) ;
2009-05-07 22:17:29 +02:00
std : : istringstream istr ( code ) ;
ASSERT_EQUALS ( false , tokenizer . tokenize ( istr , " test.cpp " ) ) ;
2010-01-23 22:18:11 +01:00
ASSERT_EQUALS ( " [test.cpp:1]: (error) Invalid number of character (() when these macros are defined: ''. \n " , errout . str ( ) ) ;
2009-05-07 22:17:29 +02:00
}
2009-05-26 22:22:00 +02:00
{
errout . str ( " " ) ;
const char code [ ] = " namespace extract{ \n B(weighted_moment) \n } \n using extract::weighted_moment; \n " ;
2009-07-13 19:11:31 +02:00
Tokenizer tokenizer ( 0 , this ) ;
2009-05-26 22:22:00 +02:00
std : : istringstream istr ( code ) ;
ASSERT_EQUALS ( true , tokenizer . tokenize ( istr , " test.cpp " ) ) ;
tokenizer . simplifyTokenList ( ) ;
2009-06-05 02:39:36 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-05-26 22:22:00 +02:00
}
2009-11-03 21:21:28 +01:00
{
errout . str ( " " ) ;
const char code [ ] = " void f() \n "
" { \n "
" foo(; \n "
" } \n " ;
Tokenizer tokenizer ( 0 , this ) ;
std : : istringstream istr ( code ) ;
2010-01-23 22:18:11 +01:00
ASSERT_EQUALS ( false , tokenizer . tokenize ( istr , " test.cpp " , " ABC " ) ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (error) Invalid number of character (() when these macros are defined: 'ABC'. \n " , errout . str ( ) ) ;
2009-11-03 21:21:28 +01:00
}
{
errout . str ( " " ) ;
const char code [ ] = " void f() \n "
" { \n "
" for(;;){ foo(); \n "
" } \n " ;
Tokenizer tokenizer ( 0 , this ) ;
std : : istringstream istr ( code ) ;
ASSERT_EQUALS ( false , tokenizer . tokenize ( istr , " test.cpp " ) ) ;
2010-01-23 22:18:11 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (error) Invalid number of character ({) when these macros are defined: ''. \n " , errout . str ( ) ) ;
2009-11-03 21:21:28 +01:00
}
{
errout . str ( " " ) ;
const char code [ ] = " void f() \n "
" { \n "
" a[10; \n "
" } \n " ;
Tokenizer tokenizer ( 0 , this ) ;
std : : istringstream istr ( code ) ;
ASSERT_EQUALS ( false , tokenizer . tokenize ( istr , " test.cpp " ) ) ;
2010-01-23 22:18:11 +01:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Invalid number of character ([) when these macros are defined: ''. \n " , errout . str ( ) ) ;
2009-11-03 21:21:28 +01:00
}
2009-11-20 20:10:42 +01:00
{
errout . str ( " " ) ;
const char code [ ] = " { \n "
" a( \n "
" } \n "
" { \n "
" b()); \n "
" } \n " ;
Tokenizer tokenizer ( 0 , this ) ;
std : : istringstream istr ( code ) ;
ASSERT_EQUALS ( false , tokenizer . tokenize ( istr , " test.cpp " ) ) ;
2010-01-23 22:18:11 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (error) Invalid number of character (() when these macros are defined: ''. \n " , errout . str ( ) ) ;
2009-11-20 20:10:42 +01:00
}
2009-05-07 22:17:29 +02:00
}
2009-05-27 20:49:29 +02:00
void removeKeywords ( )
{
const char code [ ] = " if (__builtin_expect(!!(x), 1)); " ;
const std : : string actual ( tokenizeAndStringify ( code , true ) ) ;
ASSERT_EQUALS ( " if ( ! ! x ) { ; } " , actual ) ;
}
2009-06-06 10:40:48 +02:00
2009-09-28 20:25:05 +02:00
/**
2009-09-29 16:19:56 +02:00
* tokenize " signed i " = > " int i "
2009-09-28 20:25:05 +02:00
*/
void signed1 ( )
{
{
const char code [ ] = " void foo ( signed int , signed float ) ; " ;
const char code2 [ ] = " void foo ( int , float ) ; " ;
ASSERT_EQUALS ( code2 , tokenizeAndStringify ( code ) ) ;
}
{
const char code1 [ ] = " signed i ; " ;
const char code2 [ ] = " int i ; " ;
ASSERT_EQUALS ( code2 , tokenizeAndStringify ( code1 ) ) ;
}
{
const char code1 [ ] = " signed int i ; " ;
const char code2 [ ] = " int i ; " ;
ASSERT_EQUALS ( code2 , tokenizeAndStringify ( code1 ) ) ;
}
{
const char code1 [ ] = " int signed i ; " ;
const char code2 [ ] = " int i ; " ;
ASSERT_EQUALS ( code2 , tokenizeAndStringify ( code1 ) ) ;
}
{
const char code1 [ ] = " for (signed i=0; i<10; i++) " ;
const char code2 [ ] = " for ( int i = 0 ; i < 10 ; i ++ ) " ;
ASSERT_EQUALS ( code2 , tokenizeAndStringify ( code1 ) ) ;
}
}
2009-06-06 10:40:48 +02:00
/**
* tokenize " unsigned i " = > " unsigned int i "
* tokenize " unsigned int " = > " unsigned int "
*/
void unsigned1 ( )
{
// No changes..
{
const char code [ ] = " void foo ( unsigned int , unsigned float ) ; " ;
ASSERT_EQUALS ( code , tokenizeAndStringify ( code ) ) ;
}
// insert "int" after "unsigned"..
{
const char code1 [ ] = " unsigned i ; " ;
const char code2 [ ] = " unsigned int i ; " ;
ASSERT_EQUALS ( code2 , tokenizeAndStringify ( code1 ) ) ;
}
2009-09-28 20:25:05 +02:00
{
const char code1 [ ] = " int unsigned i ; " ;
const char code2 [ ] = " unsigned int i ; " ;
ASSERT_EQUALS ( code2 , tokenizeAndStringify ( code1 ) ) ;
}
2009-08-05 20:15:48 +02:00
// insert "int" after "unsigned"..
{
const char code1 [ ] = " for (unsigned i=0; i<10; i++) " ;
const char code2 [ ] = " for ( unsigned int i = 0 ; i < 10 ; i ++ ) " ;
ASSERT_EQUALS ( code2 , tokenizeAndStringify ( code1 ) ) ;
}
2009-06-06 10:40:48 +02:00
}
2009-11-17 19:03:23 +01:00
void unsigned2 ( )
{
const char code [ ] = " i = (unsigned)j; " ;
const char expected [ ] = " i = ( unsigned int ) j ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
2009-08-12 22:50:03 +02:00
void tokenizeAndUpdateClassList ( const char code [ ] )
{
// tokenize..
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . updateClassList ( ) ;
}
void testUpdateClassList ( )
{
const char code [ ] = " class A{ \n "
" void f() {} \n "
" public: \n "
" void g() {} \n "
" }; " ;
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . updateClassList ( ) ;
ASSERT_EQUALS ( 2 , tokenizer . _classInfoList [ " A " ] . _memberFunctions . size ( ) ) ;
if ( tokenizer . _classInfoList [ " A " ] . _memberFunctions . size ( ) > 1 )
{
ASSERT_EQUALS ( std : : string ( " f " ) , tokenizer . _classInfoList [ " A " ] . _memberFunctions [ 0 ] . _name ) ;
ASSERT_EQUALS ( std : : string ( " f " ) , tokenizer . _classInfoList [ " A " ] . _memberFunctions [ 0 ] . _declaration - > str ( ) ) ;
ASSERT_EQUALS ( ClassInfo : : PRIVATE , tokenizer . _classInfoList [ " A " ] . _memberFunctions [ 0 ] . _type ) ;
ASSERT_EQUALS ( std : : string ( " g " ) , tokenizer . _classInfoList [ " A " ] . _memberFunctions [ 1 ] . _name ) ;
ASSERT_EQUALS ( std : : string ( " g " ) , tokenizer . _classInfoList [ " A " ] . _memberFunctions [ 1 ] . _declaration - > str ( ) ) ;
ASSERT_EQUALS ( ClassInfo : : PUBLIC , tokenizer . _classInfoList [ " A " ] . _memberFunctions [ 1 ] . _type ) ;
}
}
2009-09-20 22:13:06 +02:00
void createLinks ( )
{
{
const char code [ ] = " class A{ \n "
" void f() {} \n "
" }; " ;
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
const Token * tok = tokenizer . tokens ( ) ;
// A body {}
ASSERT_EQUALS ( true , tok - > tokAt ( 2 ) - > link ( ) = = tok - > tokAt ( 9 ) ) ;
ASSERT_EQUALS ( true , tok - > tokAt ( 9 ) - > link ( ) = = tok - > tokAt ( 2 ) ) ;
// f body {}
ASSERT_EQUALS ( true , tok - > tokAt ( 7 ) - > link ( ) = = tok - > tokAt ( 8 ) ) ;
ASSERT_EQUALS ( true , tok - > tokAt ( 8 ) - > link ( ) = = tok - > tokAt ( 7 ) ) ;
// f ()
ASSERT_EQUALS ( true , tok - > tokAt ( 5 ) - > link ( ) = = tok - > tokAt ( 6 ) ) ;
ASSERT_EQUALS ( true , tok - > tokAt ( 6 ) - > link ( ) = = tok - > tokAt ( 5 ) ) ;
}
{
const char code [ ] = " void f(){ \n "
" char a[10]; \n "
" char *b ; b = new char[a[0]]; \n "
" }; " ;
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
const Token * tok = tokenizer . tokens ( ) ;
// a[10]
ASSERT_EQUALS ( true , tok - > tokAt ( 7 ) - > link ( ) = = tok - > tokAt ( 9 ) ) ;
ASSERT_EQUALS ( true , tok - > tokAt ( 9 ) - > link ( ) = = tok - > tokAt ( 7 ) ) ;
// new char[]
ASSERT_EQUALS ( true , tok - > tokAt ( 19 ) - > link ( ) = = tok - > tokAt ( 24 ) ) ;
ASSERT_EQUALS ( true , tok - > tokAt ( 24 ) - > link ( ) = = tok - > tokAt ( 19 ) ) ;
// a[0]
ASSERT_EQUALS ( true , tok - > tokAt ( 21 ) - > link ( ) = = tok - > tokAt ( 23 ) ) ;
ASSERT_EQUALS ( true , tok - > tokAt ( 23 ) - > link ( ) = = tok - > tokAt ( 21 ) ) ;
}
{
const char code [ ] = " void f(){ \n "
" foo(g()); \n "
" }; " ;
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
const Token * tok = tokenizer . tokens ( ) ;
// foo(
ASSERT_EQUALS ( true , tok - > tokAt ( 6 ) - > link ( ) = = tok - > tokAt ( 10 ) ) ;
ASSERT_EQUALS ( true , tok - > tokAt ( 10 ) - > link ( ) = = tok - > tokAt ( 6 ) ) ;
// g(
ASSERT_EQUALS ( true , tok - > tokAt ( 8 ) - > link ( ) = = tok - > tokAt ( 9 ) ) ;
ASSERT_EQUALS ( true , tok - > tokAt ( 9 ) - > link ( ) = = tok - > tokAt ( 8 ) ) ;
}
}
2009-10-01 19:45:48 +02:00
2010-02-02 19:56:41 +01:00
void removeExceptionSpecification1 ( )
2009-10-01 19:45:48 +02:00
{
const char code [ ] = " class A \n "
" { \n "
" private: \n "
" void f() throw (std::runtime_error); \n "
" }; \n "
" void A::f() throw (std::runtime_error) \n "
" { } " ;
const char expected [ ] = " class A \n "
" { \n "
" private: \n "
" void f ( ) ; \n "
" } ; \n "
" void A :: f ( ) \n "
" { } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
2010-02-02 19:56:41 +01:00
void removeExceptionSpecification2 ( )
{
const char code [ ] = " class A \n "
" { \n "
" private: \n "
" int value; \n "
" public: \n "
" A::A() throw () \n "
" : value(0) \n "
" { } \n "
" }; \n " ;
const char expected [ ] = " class A \n "
" { \n "
" private: \n "
" int value ; \n "
" public: \n "
" A :: A ( ) \n "
" : value ( 0 ) \n "
" { } \n "
" } ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
2009-10-04 07:51:12 +02:00
void gt ( )
{
ASSERT_EQUALS ( " ( i < 10 ) " , tokenizeAndStringify ( " (10>i) " ) ) ;
ASSERT_EQUALS ( " ; i < 10 ; " , tokenizeAndStringify ( " ;10>i; " ) ) ;
}
2009-10-09 21:11:29 +02:00
void simplifyString ( )
{
2009-12-23 12:17:48 +01:00
Tokenizer tokenizer ;
ASSERT_EQUALS ( " \" abc \" " , tokenizer . simplifyString ( " \" abc \" " ) ) ;
ASSERT_EQUALS ( " \" a \" " , tokenizer . simplifyString ( " \" \\ x3 \" " ) ) ;
ASSERT_EQUALS ( " \" a \" " , tokenizer . simplifyString ( " \" \\ x33 \" " ) ) ;
ASSERT_EQUALS ( " \" a3 \" " , tokenizer . simplifyString ( " \" \\ x333 \" " ) ) ;
2009-10-09 21:11:29 +02:00
}
2009-11-12 22:49:39 +01:00
void simplifyConst ( )
{
ASSERT_EQUALS ( " void foo ( ) { const int x ; } " ,
tokenizeAndStringify ( " void foo(){ int const x;} " ) ) ;
ASSERT_EQUALS ( " void foo ( ) { { } const long x ; } " ,
tokenizeAndStringify ( " void foo(){ {} long const x;} " ) ) ;
ASSERT_EQUALS ( " void foo ( ) { bar ( ) ; const char x ; } " ,
tokenizeAndStringify ( " void foo(){ bar(); char const x;} " ) ) ;
ASSERT_EQUALS ( " void foo ( const char x ) { } " ,
tokenizeAndStringify ( " void foo(char const x){} " ) ) ;
ASSERT_EQUALS ( " void foo ( int b , const char x ) { } " ,
tokenizeAndStringify ( " void foo(int b,char const x){} " ) ) ;
ASSERT_EQUALS ( " void foo ( ) { int * const x ; } " ,
tokenizeAndStringify ( " void foo(){ int * const x;} " ) ) ;
}
2009-12-06 23:09:56 +01:00
void switchCase ( )
{
ASSERT_EQUALS ( " void foo ( int i ) { switch ( i ) { case -1 : break ; } } " ,
tokenizeAndStringify ( " void foo (int i) { switch(i) { case -1: break; } } " ) ) ;
}
2010-01-20 21:19:06 +01:00
std : : string simplifyFunctionPointers ( const char code [ ] )
{
Tokenizer tokenizer ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyFunctionPointers ( ) ;
std : : ostringstream ostr ;
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) )
ostr < < ( tok - > isName ( ) ? " " : " " ) < < tok - > str ( ) ;
return ostr . str ( ) ;
}
void functionpointer ( )
{
ASSERT_EQUALS ( " void* f; " , simplifyFunctionPointers ( " void (*f)(); " ) ) ;
ASSERT_EQUALS ( " void** f; " , simplifyFunctionPointers ( " void *(*f)(); " ) ) ;
ASSERT_EQUALS ( " unsigned int* f; " , simplifyFunctionPointers ( " unsigned int (*f)(); " ) ) ;
ASSERT_EQUALS ( " unsigned int** f; " , simplifyFunctionPointers ( " unsigned int * (*f)(); " ) ) ;
}
2010-01-31 09:33:57 +01:00
void removeRedundantAssignment ( )
{
ASSERT_EQUALS ( " void f ( ) { ; int * q ; ; } " , tokenizeAndStringify ( " void f() { int *p, *q; p = q; } " , true ) ) ;
2010-01-31 09:52:19 +01:00
ASSERT_EQUALS ( " void f ( ) { ; ; int * q ; ; } " , tokenizeAndStringify ( " void f() { int *p = 0, *q; p = q; } " , true ) ) ;
2010-01-31 09:33:57 +01:00
}
2010-02-03 20:01:56 +01:00
void removedeclspec ( )
{
ASSERT_EQUALS ( " a b " , tokenizeAndStringify ( " a __declspec ( dllexport ) b " ) ) ;
}
2008-12-18 22:28:57 +01:00
} ;
2009-01-05 16:49:57 +01:00
REGISTER_TEST ( TestTokenizer )