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
2012-01-01 00:05:37 +01:00
* Copyright ( C ) 2007 - 2012 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-10-25 12:49:06 +01:00
# include "tokenize.h"
# include "token.h"
2010-07-26 22:05:17 +02:00
# include "settings.h"
2012-05-06 13:01:56 +02:00
# include "path.h"
2010-07-26 22:05:17 +02:00
# include <cstring>
2008-12-18 22:28:57 +01:00
extern std : : ostringstream errout ;
2011-10-13 20:53:06 +02:00
class TestTokenizer : public TestFixture {
2008-12-18 22:28:57 +01:00
public :
TestTokenizer ( ) : TestFixture ( " TestTokenizer " )
{ }
private :
2011-10-13 20:53:06 +02:00
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 ) ;
2010-04-04 08:01:05 +02:00
TEST_CASE ( tokenize5 ) ;
2010-09-05 07:53:43 +02:00
TEST_CASE ( tokenize6 ) ; // array access. replace "*(p+1)" => "p[1]"
2010-08-27 22:58:21 +02:00
TEST_CASE ( tokenize7 ) ;
2010-08-28 13:32:43 +02:00
TEST_CASE ( tokenize8 ) ;
2010-08-29 13:54:26 +02:00
TEST_CASE ( tokenize9 ) ;
2010-08-31 19:48:04 +02:00
TEST_CASE ( tokenize10 ) ;
2010-09-04 11:49:56 +02:00
TEST_CASE ( tokenize11 ) ;
2010-10-26 19:39:48 +02:00
TEST_CASE ( tokenize12 ) ;
2010-12-26 15:07:14 +01:00
TEST_CASE ( tokenize13 ) ; // bailout if the code contains "@" - that is not handled well.
2010-12-27 08:09:05 +01:00
TEST_CASE ( tokenize14 ) ; // tokenize "0X10" => 16
2011-01-09 10:09:54 +01:00
TEST_CASE ( tokenize15 ) ; // tokenize ".123"
2011-03-06 09:33:46 +01:00
TEST_CASE ( tokenize16 ) ; // #2612 - segfault for "<><<"
2011-04-29 15:19:22 +02:00
TEST_CASE ( tokenize17 ) ; // #2759
2011-07-15 19:37:39 +02:00
TEST_CASE ( tokenize18 ) ; // tokenize "(X&&Y)" into "( X && Y )" instead of "( X & & Y )"
2011-08-15 13:19:49 +02:00
TEST_CASE ( tokenize19 ) ; // #3006 (segmentation fault)
2011-09-03 23:10:16 +02:00
TEST_CASE ( tokenize20 ) ; // replace C99 _Bool => bool
2011-12-18 07:37:20 +01:00
TEST_CASE ( tokenize21 ) ; // tokenize 0x0E-7
2011-12-18 13:33:23 +01:00
TEST_CASE ( tokenize22 ) ; // special marker $ from preprocessor
2012-01-04 12:55:51 +01:00
TEST_CASE ( tokenize23 ) ; // tokenize "return - __LINE__;"
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
2012-01-13 07:57:12 +01:00
TEST_CASE ( wrong_syntax1 ) ;
TEST_CASE ( wrong_syntax2 ) ;
2012-06-02 16:15:12 +02:00
TEST_CASE ( wrong_syntax3 ) ; // #3544
2012-06-12 21:07:17 +02:00
TEST_CASE ( wrong_syntax4 ) ; // #3618
2011-01-27 21:16:25 +01:00
TEST_CASE ( wrong_syntax_if_macro ) ; // #2518 - if MACRO()
2012-09-10 17:33:52 +02:00
TEST_CASE ( garbageCode ) ;
2010-01-21 20:05:32 +01:00
2012-09-03 20:23:53 +02:00
TEST_CASE ( foreach ) ; // #3690
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 ) ;
2010-07-05 13:16:33 +02:00
TEST_CASE ( removeCast5 ) ;
2010-10-20 20:38:37 +02:00
TEST_CASE ( removeCast6 ) ;
2011-11-25 07:23:54 +01:00
TEST_CASE ( removeCast7 ) ;
TEST_CASE ( removeCast8 ) ;
2012-02-18 14:26:00 +01:00
TEST_CASE ( removeCast9 ) ;
2012-02-27 18:55:36 +01:00
TEST_CASE ( removeCast10 ) ;
2012-06-19 20:07:39 +02:00
TEST_CASE ( removeCast11 ) ;
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 ( 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 ) ;
2010-02-07 15:00:19 +01:00
TEST_CASE ( ifAddBraces10 ) ;
2010-02-20 11:43:53 +01:00
TEST_CASE ( ifAddBraces11 ) ;
2010-02-20 13:24:50 +01:00
TEST_CASE ( ifAddBraces12 ) ;
2010-06-26 17:15:44 +02:00
TEST_CASE ( ifAddBraces13 ) ;
2011-10-27 20:54:42 +02:00
TEST_CASE ( ifAddBraces14 ) ; // #2610 - segfault: if()<{}
TEST_CASE ( ifAddBraces15 ) ; // #2616 - unknown macro before if
2012-01-26 22:25:19 +01:00
TEST_CASE ( ifAddBraces16 ) ; // ticket # 2739 (segmentation fault)
TEST_CASE ( ifAddBraces17 ) ; // '} else' should be in the same line
TEST_CASE ( ifAddBraces18 ) ; // #3424 - if if { } else else
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
2010-06-13 08:00:46 +02:00
TEST_CASE ( forAddBraces ) ;
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 ) ;
2010-02-20 15:50:44 +01:00
TEST_CASE ( simplifyKnownVariables20 ) ;
2010-03-14 09:57:34 +01:00
TEST_CASE ( simplifyKnownVariables21 ) ;
2010-04-04 18:48:51 +02:00
TEST_CASE ( simplifyKnownVariables22 ) ;
2010-04-15 19:09:19 +02:00
TEST_CASE ( simplifyKnownVariables23 ) ;
2010-05-02 14:41:21 +02:00
TEST_CASE ( simplifyKnownVariables24 ) ;
2010-05-04 20:02:47 +02:00
TEST_CASE ( simplifyKnownVariables25 ) ;
2010-06-15 17:56:14 +02:00
TEST_CASE ( simplifyKnownVariables26 ) ;
2010-06-19 14:00:45 +02:00
TEST_CASE ( simplifyKnownVariables27 ) ;
2010-06-19 17:54:38 +02:00
TEST_CASE ( simplifyKnownVariables28 ) ;
2010-11-07 08:43:30 +01:00
TEST_CASE ( simplifyKnownVariables29 ) ; // ticket #1811
2010-10-10 19:27:42 +02:00
TEST_CASE ( simplifyKnownVariables30 ) ;
2010-11-06 19:42:38 +01:00
TEST_CASE ( simplifyKnownVariables31 ) ;
2010-11-07 08:43:30 +01:00
TEST_CASE ( simplifyKnownVariables32 ) ; // const
2010-11-13 18:45:35 +01:00
TEST_CASE ( simplifyKnownVariables33 ) ; // struct variable
2010-12-16 20:16:59 +01:00
TEST_CASE ( simplifyKnownVariables34 ) ;
2010-12-25 10:07:57 +01:00
TEST_CASE ( simplifyKnownVariables35 ) ; // ticket #2353 - False positive: Division by zero 'if (x == 0) return 0; return 10 / x;'
2010-12-26 20:34:07 +01:00
TEST_CASE ( simplifyKnownVariables36 ) ; // ticket #2304 - known value for strcpy parameter
2011-01-04 19:36:29 +01:00
TEST_CASE ( simplifyKnownVariables37 ) ; // ticket #2398 - false positive caused by no simplification in for loop
2011-01-04 21:35:35 +01:00
TEST_CASE ( simplifyKnownVariables38 ) ; // ticket #2399 - simplify conditions
2011-01-11 19:34:35 +01:00
TEST_CASE ( simplifyKnownVariables39 ) ;
2011-02-01 21:46:07 +01:00
TEST_CASE ( simplifyKnownVariables40 ) ;
2011-02-20 18:18:27 +01:00
TEST_CASE ( simplifyKnownVariables41 ) ; // p=&x; if (p) ..
2011-02-26 21:11:56 +01:00
TEST_CASE ( simplifyKnownVariables42 ) ; // ticket #2031 - known string value after strcpy
2011-08-30 19:13:04 +02:00
TEST_CASE ( simplifyKnownVariables43 ) ;
2011-11-05 08:30:11 +01:00
TEST_CASE ( simplifyKnownVariables44 ) ; // ticket #3117 - don't simplify static variables
2011-11-20 21:50:26 +01:00
TEST_CASE ( simplifyKnownVariables45 ) ; // ticket #3281 - static constant variable not simplified
2012-02-07 08:05:50 +01:00
TEST_CASE ( simplifyKnownVariables46 ) ; // ticket #3587 - >>
2012-03-15 19:09:36 +01:00
TEST_CASE ( simplifyKnownVariables47 ) ; // ticket #3627 - >>
2012-04-25 19:23:17 +02:00
TEST_CASE ( simplifyKnownVariables48 ) ; // ticket #3754 - wrong simplification in for loop header
2012-05-07 21:11:23 +02:00
TEST_CASE ( simplifyKnownVariables49 ) ; // #3691 - continue in switch
2012-08-22 16:51:44 +02:00
TEST_CASE ( simplifyKnownVariables50 ) ; // #4066 sprintf changes
2012-04-22 12:22:49 +02:00
TEST_CASE ( simplifyKnownVariablesIfEq1 ) ; // if (a==5) => a is 5 in the block
TEST_CASE ( simplifyKnownVariablesIfEq2 ) ; // if (a==5) { buf[a++] = 0; }
2011-08-21 17:49:00 +02:00
TEST_CASE ( simplifyKnownVariablesBailOutAssign1 ) ;
TEST_CASE ( simplifyKnownVariablesBailOutAssign2 ) ;
2010-11-07 10:42:08 +01:00
TEST_CASE ( simplifyKnownVariablesBailOutFor1 ) ;
TEST_CASE ( simplifyKnownVariablesBailOutFor2 ) ;
2010-11-13 16:40:04 +01:00
TEST_CASE ( simplifyKnownVariablesBailOutFor3 ) ;
2010-11-07 08:10:09 +01:00
TEST_CASE ( simplifyKnownVariablesBailOutMemberFunction ) ;
2010-11-21 13:40:42 +01:00
TEST_CASE ( simplifyKnownVariablesBailOutConditionalIncrement ) ;
2011-10-27 20:54:42 +02:00
TEST_CASE ( simplifyKnownVariablesBailOutSwitchBreak ) ; // ticket #2324
2011-01-15 08:48:42 +01:00
TEST_CASE ( simplifyKnownVariablesFloat ) ; // #2454 - float variable
2011-06-23 16:58:01 +02:00
TEST_CASE ( simplifyKnownVariablesClassMember ) ; // #2815 - value of class member may be changed by function call
2012-06-24 13:39:14 +02:00
TEST_CASE ( simplifyKnownVariablesFunctionCalls ) ; // Function calls (don't assume pass by reference)
2012-04-16 16:25:04 +02:00
TEST_CASE ( simplifyExternC ) ;
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 ) ;
2010-05-06 18:40:52 +02:00
TEST_CASE ( varidReturn1 ) ;
TEST_CASE ( varidReturn2 ) ;
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 ) ;
2010-03-18 18:14:52 +01:00
TEST_CASE ( varid15 ) ;
2010-05-01 10:09:53 +02:00
TEST_CASE ( varid16 ) ;
2010-12-15 18:45:53 +01:00
TEST_CASE ( varid17 ) ; // ticket #1810
2010-08-10 18:22:58 +02:00
TEST_CASE ( varid18 ) ;
2010-08-14 20:50:49 +02:00
TEST_CASE ( varid19 ) ;
2010-08-15 11:54:28 +02:00
TEST_CASE ( varid20 ) ;
2010-08-18 22:42:04 +02:00
TEST_CASE ( varid24 ) ;
TEST_CASE ( varid25 ) ;
2010-12-15 18:45:53 +01:00
TEST_CASE ( varid26 ) ; // ticket #1967 (list of function pointers)
2011-10-27 20:54:42 +02:00
TEST_CASE ( varid27 ) ; // Ticket #2280 (same name for namespace and variable)
2011-03-19 01:44:58 +01:00
TEST_CASE ( varid28 ) ; // ticket #2630
2011-03-22 01:57:17 +01:00
TEST_CASE ( varid29 ) ; // ticket #1974
2011-04-18 13:45:38 +02:00
TEST_CASE ( varid30 ) ; // ticket #2614
2011-06-12 14:38:15 +02:00
TEST_CASE ( varid31 ) ; // ticket #2831 (segmentation fault)
2011-06-12 18:09:52 +02:00
TEST_CASE ( varid32 ) ; // ticket #2835 (segmentation fault)
2011-06-29 00:47:18 +02:00
TEST_CASE ( varid33 ) ; // ticket #2875 (segmentation fault)
2011-06-30 02:04:04 +02:00
TEST_CASE ( varid34 ) ; // ticket #2825
2011-08-01 12:45:06 +02:00
TEST_CASE ( varid35 ) ; // ticket #2937
2011-08-08 12:58:55 +02:00
TEST_CASE ( varid36 ) ; // ticket #2980 (segmentation fault)
2011-10-29 21:25:58 +02:00
TEST_CASE ( varid37 ) ; // ticket #3092 (varid for 'Bar bar(*this);')
2011-11-01 18:03:32 +01:00
TEST_CASE ( varid38 ) ; // ticket #3272 (varid for 'FOO class C;')
2011-11-02 18:31:13 +01:00
TEST_CASE ( varid39 ) ; // ticket #3279 (varid for 'FOO::BAR const')
2011-11-09 18:49:17 +01:00
TEST_CASE ( varid40 ) ; // ticket #3279
2011-11-20 08:13:28 +01:00
TEST_CASE ( varid41 ) ; // ticket #3340 (varid for union type)
2011-11-20 16:22:21 +01:00
TEST_CASE ( varid42 ) ; // ticket #3316 (varid for array)
2012-03-20 19:00:16 +01:00
TEST_CASE ( varid43 ) ;
2011-11-21 21:18:56 +01:00
TEST_CASE ( varid44 ) ;
2012-04-25 21:14:25 +02:00
TEST_CASE ( varid45 ) ; // #3466
2012-05-03 19:10:51 +02:00
TEST_CASE ( varid46 ) ; // struct varname
2012-05-05 09:59:43 +02:00
TEST_CASE ( varid47 ) ; // function parameters
2012-05-08 21:04:54 +02:00
TEST_CASE ( varid48 ) ; // #3785 - return (a*b)
2012-05-13 07:55:35 +02:00
TEST_CASE ( varid49 ) ; // #3799 - void f(std::vector<int>)
2012-05-16 10:59:52 +02:00
TEST_CASE ( varid50 ) ; // #3760 - explicit
2012-05-22 18:58:13 +02:00
TEST_CASE ( varid51 ) ; // don't set varid for template function
2012-08-26 09:49:38 +02:00
TEST_CASE ( varid52 ) ; // Set varid for nested templates
2012-04-15 18:23:12 +02:00
TEST_CASE ( varid_cpp_keywords_in_c_code ) ;
2010-11-03 20:13:34 +01:00
TEST_CASE ( varidFunctionCall1 ) ;
TEST_CASE ( varidFunctionCall2 ) ;
2010-12-25 12:40:44 +01:00
TEST_CASE ( varidFunctionCall3 ) ;
2011-11-20 12:09:07 +01:00
TEST_CASE ( varidFunctionCall4 ) ; // ticket #3280
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 ) ;
2012-05-15 18:40:24 +02:00
TEST_CASE ( varid_sizeof ) ;
2009-07-18 12:37:51 +02:00
TEST_CASE ( varid_reference_to_containers ) ;
2010-08-20 19:28:10 +02:00
TEST_CASE ( varid_in_class1 ) ;
TEST_CASE ( varid_in_class2 ) ;
2011-10-29 21:25:58 +02:00
TEST_CASE ( varid_in_class3 ) ; // #3092 - shadow variable in member function
2011-10-31 18:33:15 +01:00
TEST_CASE ( varid_in_class4 ) ; // #3271 - public: class C;
2012-02-12 17:30:47 +01:00
TEST_CASE ( varid_in_class5 ) ; // #3584 - std::vector<::FOO::B> b;
2012-04-26 18:38:47 +02:00
TEST_CASE ( varid_in_class6 ) ; // #3755
TEST_CASE ( varid_in_class7 ) ; // set variable id for struct members
2012-05-15 07:17:31 +02:00
TEST_CASE ( varid_in_class8 ) ; // unknown macro in class
2012-08-28 12:44:40 +02:00
TEST_CASE ( varid_initList ) ;
2010-02-07 13:04:33 +01:00
TEST_CASE ( varid_operator ) ;
2010-06-03 07:05:57 +02:00
TEST_CASE ( varid_throw ) ;
2011-03-13 17:52:45 +01:00
TEST_CASE ( varid_unknown_macro ) ; // #2638 - unknown macro is not type
2012-03-20 19:05:24 +01:00
TEST_CASE ( varid_using ) ; // ticket #3648
2012-04-15 17:39:22 +02:00
TEST_CASE ( varid_catch ) ;
2012-08-12 14:19:56 +02:00
TEST_CASE ( varid_functionPrototypeTemplate ) ;
2009-05-03 13:50:26 +02:00
TEST_CASE ( varidclass1 ) ;
TEST_CASE ( varidclass2 ) ;
TEST_CASE ( varidclass3 ) ;
TEST_CASE ( varidclass4 ) ;
2010-02-07 13:34:39 +01:00
TEST_CASE ( varidclass5 ) ;
2010-02-25 21:02:12 +01:00
TEST_CASE ( varidclass6 ) ;
TEST_CASE ( varidclass7 ) ;
2010-07-12 20:18:03 +02:00
TEST_CASE ( varidclass8 ) ;
2011-03-08 20:41:41 +01:00
TEST_CASE ( varidclass9 ) ;
2011-03-19 09:04:03 +01:00
TEST_CASE ( varidclass10 ) ; // variable declaration below usage
2011-03-19 11:09:51 +01:00
TEST_CASE ( varidclass11 ) ; // variable declaration below usage
2011-03-26 08:56:41 +01:00
TEST_CASE ( varidclass12 ) ;
2011-03-26 10:05:07 +01:00
TEST_CASE ( varidclass13 ) ;
2011-10-29 18:22:58 +02:00
TEST_CASE ( varidclass14 ) ;
2012-04-23 18:26:27 +02:00
TEST_CASE ( varidclass15 ) ; // initializer list
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 ) ;
2012-01-23 16:10:15 +01:00
TEST_CASE ( simplifyFunctionParameters ) ;
2012-04-22 17:28:27 +02:00
TEST_CASE ( simplifyFunctionParameters1 ) ; // #3721
2012-01-23 16:10:15 +01:00
TEST_CASE ( simplifyFunctionParametersErrors ) ;
2009-01-25 20:39:05 +01:00
2011-03-30 16:45:31 +02:00
TEST_CASE ( removeParentheses1 ) ; // Ticket #61
TEST_CASE ( removeParentheses2 ) ;
TEST_CASE ( removeParentheses3 ) ;
TEST_CASE ( removeParentheses4 ) ; // Ticket #390
TEST_CASE ( removeParentheses5 ) ; // Ticket #392
TEST_CASE ( removeParentheses6 ) ;
TEST_CASE ( removeParentheses7 ) ;
TEST_CASE ( removeParentheses8 ) ; // Ticket #1865
TEST_CASE ( removeParentheses9 ) ; // Ticket #1962
TEST_CASE ( removeParentheses10 ) ; // Ticket #2320
TEST_CASE ( removeParentheses11 ) ; // Ticket #2505
2011-05-01 08:27:59 +02:00
TEST_CASE ( removeParentheses12 ) ; // Ticket #2760 ',(b)='
2011-11-07 23:40:06 +01:00
TEST_CASE ( removeParentheses13 ) ;
2011-11-08 22:48:14 +01:00
TEST_CASE ( removeParentheses14 ) ; // Ticket #3309
2012-09-06 16:16:29 +02:00
TEST_CASE ( removeParentheses15 ) ; // Ticket #4142
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 ) ;
2011-01-29 16:14:09 +01:00
TEST_CASE ( simplify_constants3 ) ;
2011-02-05 09:03:31 +01:00
TEST_CASE ( simplify_null ) ;
2011-12-09 20:47:51 +01:00
TEST_CASE ( simplifyMulAndParens ) ; // Ticket #2784 + #3184
2009-03-13 22:38:42 +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 ) ;
2010-05-16 20:21:22 +02:00
TEST_CASE ( vardecl11 ) ;
2010-08-25 20:17:31 +02:00
TEST_CASE ( vardecl12 ) ;
2010-09-15 19:53:47 +02:00
TEST_CASE ( vardecl13 ) ;
2011-03-25 04:06:20 +01:00
TEST_CASE ( vardecl14 ) ;
2012-01-30 23:41:43 +01:00
TEST_CASE ( vardecl15 ) ;
TEST_CASE ( vardecl16 ) ;
TEST_CASE ( vardecl17 ) ;
2012-02-18 15:05:29 +01:00
TEST_CASE ( vardecl18 ) ;
2012-03-31 18:45:29 +02:00
TEST_CASE ( vardecl19 ) ;
2012-04-09 12:55:26 +02:00
TEST_CASE ( vardecl20 ) ; // #3700 - register const int H = 0;
2012-09-14 16:46:45 +02:00
TEST_CASE ( vardecl21 ) ; // #4042 - a::b const *p = 0;
2012-09-17 19:45:42 +02:00
TEST_CASE ( vardecl22 ) ; // #4211 - segmentation fault
2010-11-13 15:45:33 +01:00
TEST_CASE ( vardecl_stl_1 ) ;
TEST_CASE ( vardecl_stl_2 ) ;
2012-04-05 08:53:10 +02:00
TEST_CASE ( vardecl_template_1 ) ;
TEST_CASE ( vardecl_template_2 ) ;
2010-09-03 08:10:29 +02:00
TEST_CASE ( vardecl_union ) ;
2011-05-07 14:23:14 +02:00
TEST_CASE ( vardecl_par ) ; // #2743 - set links if variable type contains parentheses
2012-07-03 06:34:14 +02:00
TEST_CASE ( vardecl_par2 ) // #3912 - set correct links
2009-04-20 20:38:05 +02:00
TEST_CASE ( volatile_variables ) ;
2009-05-07 22:17:29 +02:00
TEST_CASE ( syntax_error ) ;
2011-02-17 21:15:17 +01:00
TEST_CASE ( syntax_error_templates_1 ) ;
TEST_CASE ( syntax_error_templates_2 ) ;
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 ) ;
2010-05-07 18:37:50 +02:00
TEST_CASE ( unsigned3 ) ; // template arguments
2010-03-19 19:34:26 +01:00
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 ) ;
2010-12-17 20:56:46 +01:00
TEST_CASE ( removeExceptionSpecification3 ) ;
2011-12-06 20:36:23 +01:00
TEST_CASE ( removeExceptionSpecification4 ) ;
TEST_CASE ( removeExceptionSpecification5 ) ;
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
2011-10-16 08:09:57 +02:00
TEST_CASE ( simplifyPointerToStandardType ) ;
2011-03-19 21:21:26 +01:00
TEST_CASE ( functionpointer1 ) ;
TEST_CASE ( functionpointer2 ) ;
2011-07-22 07:58:53 +02:00
TEST_CASE ( functionpointer3 ) ;
2011-11-09 21:02:41 +01:00
TEST_CASE ( functionpointer4 ) ;
2011-12-27 11:56:40 +01:00
TEST_CASE ( functionpointer5 ) ;
2010-01-31 09:33:57 +01:00
TEST_CASE ( removeRedundantAssignment ) ;
2010-02-03 20:01:56 +01:00
TEST_CASE ( removedeclspec ) ;
2010-05-27 18:15:42 +02:00
TEST_CASE ( removeattribute ) ;
2010-09-05 08:16:19 +02:00
TEST_CASE ( cpp0xtemplate1 ) ;
TEST_CASE ( cpp0xtemplate2 ) ;
2011-02-12 16:51:59 +01:00
TEST_CASE ( cpp0xtemplate3 ) ;
2010-08-17 19:50:21 +02:00
TEST_CASE ( cpp0xdefault ) ;
2010-02-20 18:13:09 +01:00
TEST_CASE ( arraySize ) ;
2010-02-21 09:47:41 +01:00
TEST_CASE ( labels ) ;
2010-04-14 19:04:16 +02:00
TEST_CASE ( simplifyInitVar ) ;
2010-08-15 11:54:28 +02:00
2010-08-18 22:42:04 +02:00
TEST_CASE ( bitfields1 ) ;
TEST_CASE ( bitfields2 ) ;
TEST_CASE ( bitfields3 ) ;
2010-08-21 16:34:41 +02:00
TEST_CASE ( bitfields4 ) ; // ticket #1956
2010-08-22 09:41:22 +02:00
TEST_CASE ( bitfields5 ) ; // ticket #1956
2011-02-21 00:22:49 +01:00
TEST_CASE ( bitfields6 ) ; // ticket #2595
2011-03-19 21:00:43 +01:00
TEST_CASE ( bitfields7 ) ; // ticket #1987
2011-03-22 01:17:14 +01:00
TEST_CASE ( bitfields8 ) ;
2011-04-05 04:18:12 +02:00
TEST_CASE ( bitfields9 ) ; // ticket #2706
2011-05-19 17:04:36 +02:00
TEST_CASE ( bitfields10 ) ;
2011-06-21 13:31:55 +02:00
TEST_CASE ( bitfields11 ) ; // ticket #2845 (segmentation fault)
2012-01-09 16:24:11 +01:00
TEST_CASE ( bitfields12 ) ; // ticket #3485 (segmentation fault)
2012-01-23 07:39:31 +01:00
TEST_CASE ( bitfields13 ) ; // ticket #3502 (segmentation fault)
2010-08-18 22:42:04 +02:00
2012-07-15 11:05:19 +02:00
TEST_CASE ( simplifyNamespaceStd ) ;
2010-08-18 22:42:04 +02:00
TEST_CASE ( microsoftMFC ) ;
2011-09-23 01:59:56 +02:00
TEST_CASE ( microsoftMemory ) ;
2010-08-31 21:40:51 +02:00
2010-09-01 18:10:12 +02:00
TEST_CASE ( borland ) ;
2010-12-02 17:41:49 +01:00
TEST_CASE ( Qt ) ;
2010-08-31 21:40:51 +02:00
TEST_CASE ( sql ) ;
2010-09-09 17:43:09 +02:00
TEST_CASE ( simplifyLogicalOperators ) ;
2010-09-09 19:40:36 +02:00
2012-06-14 21:47:03 +02:00
TEST_CASE ( simplifyCalculations ) ;
2011-08-06 06:50:29 +02:00
2010-09-09 19:40:36 +02:00
// foo(p = new char[10]); => p = new char[10]; foo(p);
2010-10-02 12:44:38 +02:00
TEST_CASE ( simplifyAssignmentInFunctionCall ) ;
2010-10-19 21:54:15 +02:00
2010-10-31 08:47:13 +01:00
// "x += .." => "x = x + .."
TEST_CASE ( simplifyCompoundAssignment ) ;
2010-10-19 21:54:15 +02:00
// Tokenize C#
TEST_CASE ( cs ) ;
2010-10-20 22:15:35 +02:00
// Tokenize JAVA
TEST_CASE ( java ) ;
2011-01-27 18:44:20 +01:00
2011-02-28 20:29:34 +01:00
TEST_CASE ( simplifyOperatorName1 ) ;
TEST_CASE ( simplifyOperatorName2 ) ;
TEST_CASE ( simplifyOperatorName3 ) ;
TEST_CASE ( simplifyOperatorName4 ) ;
2011-03-29 02:02:06 +02:00
TEST_CASE ( simplifyOperatorName5 ) ;
2011-10-12 15:10:34 +02:00
TEST_CASE ( simplifyOperatorName6 ) ; // ticket #3194
2011-01-30 08:34:58 +01:00
2012-08-26 10:03:05 +02:00
TEST_CASE ( simplifyNullArray ) ;
2011-01-30 08:34:58 +01:00
// Some simple cleanups of unhandled macros in the global scope
TEST_CASE ( removeMacrosInGlobalScope ) ;
2011-03-26 12:20:23 +01:00
// a = b = 0;
TEST_CASE ( multipleAssignment ) ;
2011-06-11 21:51:12 +02:00
2011-09-18 01:40:52 +02:00
TEST_CASE ( platformWin32 ) ;
2011-09-24 20:51:03 +02:00
TEST_CASE ( platformWin32A ) ;
TEST_CASE ( platformWin32W ) ;
2011-09-18 01:40:52 +02:00
TEST_CASE ( platformWin64 ) ;
TEST_CASE ( platformUnix32 ) ;
TEST_CASE ( platformUnix64 ) ;
2012-09-03 18:51:15 +02:00
TEST_CASE ( simplifyMathExpressions ) ; //ticket #1620
2008-12-18 22:28:57 +01:00
}
2012-08-25 21:57:45 +02:00
std : : string tokenizeAndStringify ( const char code [ ] , bool simplify = false , bool expand = true , Settings : : PlatformType platform = Settings : : Unspecified , const char * filename = " test.cpp " , bool cpp11 = true ) {
2010-11-07 11:07:56 +01:00
errout . str ( " " ) ;
Settings settings ;
settings . debugwarnings = true ;
2011-09-18 01:40:52 +02:00
settings . platform ( platform ) ;
2012-08-25 21:57:45 +02:00
settings . standards . cpp = cpp11 ? Standards : : CPP11 : Standards : : CPP03 ;
2010-12-01 18:00:55 +01:00
// tokenize..
2010-11-07 11:07:56 +01:00
Tokenizer tokenizer ( & settings , this ) ;
2009-01-18 09:52:20 +01:00
std : : istringstream istr ( code ) ;
2012-04-16 19:51:07 +02:00
tokenizer . tokenize ( istr , filename ) ;
2010-04-02 07:30:58 +02:00
if ( simplify )
2009-05-27 20:49:29 +02:00
tokenizer . simplifyTokenList ( ) ;
2009-01-18 09:52:20 +01:00
2012-04-16 19:51:07 +02:00
return tokenizer . tokens ( ) - > stringifyList ( false , expand , false , true , false , 0 , 0 ) ;
2009-01-18 09:52:20 +01:00
}
2011-10-13 20:53:06 +02:00
void tokenize1 ( ) {
2009-11-12 18:53:26 +01:00
const std : : string code ( " void f ( ) \n "
" { if ( p . y ( ) > yof ) { } } " ) ;
ASSERT_EQUALS ( code , tokenizeAndStringify ( code . c_str ( ) ) ) ;
}
2011-10-13 20:53:06 +02:00
void tokenize2 ( ) {
2009-11-22 09:06:39 +01:00
const std : : string code ( " { sizeof a, sizeof b } " ) ;
ASSERT_EQUALS ( " { sizeof a , sizeof b } " , tokenizeAndStringify ( code . c_str ( ) ) ) ;
}
2011-10-13 20:53:06 +02:00
void tokenize3 ( ) {
2009-12-12 19:58:19 +01:00
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 ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void tokenize4 ( ) {
2010-01-29 19:34:43 +01:00
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 ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void tokenize5 ( ) {
2010-04-04 08:01:05 +02:00
// Tokenize values
ASSERT_EQUALS ( " ; + 1E3 ; " , tokenizeAndStringify ( " ; +1E3 ; " ) ) ;
ASSERT_EQUALS ( " ; 1E-2 ; " , tokenizeAndStringify ( " ; 1E-2 ; " ) ) ;
}
2011-10-13 20:53:06 +02:00
void tokenize6 ( ) {
2011-11-08 17:55:04 +01:00
// "&p[1]" => "p+1"
/*
ASSERT_EQUALS ( " ; x = p + n ; " , tokenizeAndStringify ( " ; x = & p [ n ] ; " , true ) ) ;
ASSERT_EQUALS ( " ; x = ( p + n ) [ m ] ; " , tokenizeAndStringify ( " ; x = & p [ n ] [ m ] ; " , true ) ) ;
ASSERT_EQUALS ( " ; x = y & p [ n ] ; " , tokenizeAndStringify ( " ; x = y & p [ n ] ; " , true ) ) ;
ASSERT_EQUALS ( " ; x = 10 & p [ n ] ; " , tokenizeAndStringify ( " ; x = 10 & p [ n ] ; " , true ) ) ;
ASSERT_EQUALS ( " ; x = y [ 10 ] & p [ n ] ; " , tokenizeAndStringify ( " ; x = y [ 10 ] & p [ n ] ; " , true ) ) ;
ASSERT_EQUALS ( " ; x = ( a + m ) & p [ n ] ; " , tokenizeAndStringify ( " ; x = ( a + m ) & p [ n ] ; " , true ) ) ; */
2010-06-14 08:36:34 +02:00
// "*(p+1)" => "p[1]"
ASSERT_EQUALS ( " ; x = p [ 1 ] ; " , tokenizeAndStringify ( " ; x = * ( p + 1 ) ; " , true ) ) ;
ASSERT_EQUALS ( " ; x = p [ n ] ; " , tokenizeAndStringify ( " ; x = * ( p + n ) ; " , true ) ) ;
2011-11-08 17:55:04 +01:00
ASSERT_EQUALS ( " ; x = y * ( p + n ) ; " , tokenizeAndStringify ( " ; x = y * ( p + n ) ; " , true ) ) ;
ASSERT_EQUALS ( " ; x = 10 * ( p + n ) ; " , tokenizeAndStringify ( " ; x = 10 * ( p + n ) ; " , true ) ) ;
ASSERT_EQUALS ( " ; x = y [ 10 ] * ( p + n ) ; " , tokenizeAndStringify ( " ; x = y [ 10 ] * ( p + n ) ; " , true ) ) ;
ASSERT_EQUALS ( " ; x = ( a + m ) * ( p + n ) ; " , tokenizeAndStringify ( " ; x = ( a + m ) * ( p + n ) ; " , true ) ) ;
2010-06-14 08:36:34 +02:00
}
2011-10-13 20:53:06 +02:00
void tokenize7 ( ) {
2010-08-27 22:58:21 +02:00
const std : : string code = " void f() { \n "
" int x1 = 1; \n "
" int x2(x1); \n "
" } \n " ;
ASSERT_EQUALS ( " void f ( ) { \n int x1 ; x1 = 1 ; \n int x2 ; x2 = x1 ; \n } " ,
tokenizeAndStringify ( code . c_str ( ) , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void tokenize8 ( ) {
2010-08-28 13:32:43 +02:00
const std : : string code = " void f() { \n "
" int x1(g()); \n "
" int x2(x1); \n "
" } \n " ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: void f ( ) { \n "
" 2: int x1@1 ; x1@1 = g ( ) ; \n "
" 3: int x2@2 ; x2@2 = x1@1 ; \n "
" 4: } \n " ,
2012-02-17 15:47:08 +01:00
tokenizeDebugListing ( code , false ) ) ;
2010-08-28 13:32:43 +02:00
}
2011-10-13 20:53:06 +02:00
void tokenize9 ( ) {
2010-08-29 13:54:26 +02:00
const char code [ ] = " typedef void (*fp)(); \n "
" typedef fp (*fpp)(); \n "
" void f() { \n "
" fpp x = (fpp)f(); \n "
" } " ;
tokenizeAndStringify ( code , false ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void tokenize10 ( ) {
2010-08-31 19:48:04 +02:00
ASSERT_EQUALS ( " private: " , tokenizeAndStringify ( " private: " , false ) ) ;
ASSERT_EQUALS ( " protected: " , tokenizeAndStringify ( " protected: " , false ) ) ;
ASSERT_EQUALS ( " public: " , tokenizeAndStringify ( " public: " , false ) ) ;
ASSERT_EQUALS ( " __published: " , tokenizeAndStringify ( " __published: " , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void tokenize11 ( ) {
2010-09-04 11:49:56 +02:00
ASSERT_EQUALS ( " X * sizeof ( Y ( ) ) ; " , tokenizeAndStringify ( " X * sizeof(Y()); " , false ) ) ;
}
2010-10-26 19:39:48 +02:00
// ticket #2118 - invalid syntax error
2011-10-13 20:53:06 +02:00
void tokenize12 ( ) {
2010-10-26 19:39:48 +02:00
tokenizeAndStringify ( " Q_GLOBAL_STATIC_WITH_INITIALIZER(Qt4NodeStaticData, qt4NodeStaticData, { \n "
" for (unsigned i = 0 ; i < count; i++) { \n "
" } \n "
" }); " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2010-12-26 15:07:14 +01:00
// bailout if there is "@" - it is not handled well
2011-10-13 20:53:06 +02:00
void tokenize13 ( ) {
2010-12-26 15:07:14 +01:00
const char code [ ] = " @implementation \n "
" -(Foo *)foo: (Bar *)bar \n "
" { } \n "
" @end \n " ;
ASSERT_EQUALS ( " " , tokenizeAndStringify ( code ) ) ;
}
2010-12-27 08:09:05 +01:00
// Ticket #2361: 0X10 => 16
2011-10-13 20:53:06 +02:00
void tokenize14 ( ) {
2010-12-27 08:09:05 +01:00
ASSERT_EQUALS ( " ; 16 ; " , tokenizeAndStringify ( " ;0x10; " ) ) ;
ASSERT_EQUALS ( " ; 16 ; " , tokenizeAndStringify ( " ;0X10; " ) ) ;
2012-02-17 15:47:08 +01:00
ASSERT_EQUALS ( " ; 292 ; " , tokenizeAndStringify ( " ;0444; " ) ) ;
2010-12-27 08:09:05 +01:00
}
2011-01-09 18:39:59 +01:00
2011-01-09 10:09:54 +01:00
// Ticket #2429: 0.125
2011-10-13 20:53:06 +02:00
void tokenize15 ( ) {
2011-01-09 10:09:54 +01:00
ASSERT_EQUALS ( " 0.125 " , tokenizeAndStringify ( " .125 " ) ) ;
2012-02-17 19:54:53 +01:00
ASSERT_EQUALS ( " 005.125 " , tokenizeAndStringify ( " 005.125 " ) ) ; // Don't confuse with octal values
2011-01-09 10:09:54 +01:00
}
2010-12-27 08:09:05 +01:00
2011-03-06 09:33:46 +01:00
// #2612 - segfault for "<><<"
2011-10-13 20:53:06 +02:00
void tokenize16 ( ) {
2011-03-06 09:33:46 +01:00
tokenizeAndStringify ( " <><< " ) ;
}
2011-10-13 20:53:06 +02:00
void tokenize17 ( ) { // #2759
2011-04-29 15:19:22 +02:00
ASSERT_EQUALS ( " class B : private :: A { } ; " , tokenizeAndStringify ( " class B : private ::A { }; " ) ) ;
}
2011-10-13 20:53:06 +02:00
void tokenize18 ( ) { // tokenize "(X&&Y)" into "( X && Y )" instead of "( X & & Y )"
2011-07-15 19:37:39 +02:00
ASSERT_EQUALS ( " ( X && Y ) " , tokenizeAndStringify ( " (X&&Y) " ) ) ;
}
2012-01-15 14:33:53 +01:00
void tokenize19 ( ) {
// #3006 - added hasComplicatedSyntaxErrorsInTemplates to avoid segmentation fault
2011-08-15 13:19:49 +02:00
tokenizeAndStringify ( " x < () < " ) ;
2012-01-15 19:47:51 +01:00
2012-01-15 14:33:53 +01:00
// #3496 - make sure hasComplicatedSyntaxErrorsInTemplates works
ASSERT_EQUALS ( " void a ( Fred * f ) { for ( ; n < f . x ( ) ; ) { } } " ,
tokenizeAndStringify ( " void a(Fred* f) MACRO { for (;n < f->x();) {} } " ) ) ;
2011-08-15 13:19:49 +02:00
}
2011-10-13 20:53:06 +02:00
void tokenize20 ( ) { // replace C99 _Bool => bool
2011-09-03 23:10:16 +02:00
ASSERT_EQUALS ( " bool a ; a = true ; " , tokenizeAndStringify ( " _Bool a = true; " ) ) ;
}
2011-12-18 07:37:20 +01:00
void tokenize21 ( ) { // tokenize 0x0E-7
ASSERT_EQUALS ( " 14 - 7 " , tokenizeAndStringify ( " 0x0E-7 " ) ) ;
}
2011-12-18 13:33:23 +01:00
void tokenize22 ( ) { // tokenize special marker $ from preprocessor
ASSERT_EQUALS ( " a b " , tokenizeAndStringify ( " a$b " ) ) ;
}
2012-01-04 12:55:51 +01:00
void tokenize23 ( ) { // tokenize 'return - __LINE__' correctly
ASSERT_EQUALS ( " return -1 ; " , tokenizeAndStringify ( " return - __LINE__; " ) ) ;
}
2012-01-13 07:57:12 +01:00
void wrong_syntax1 ( ) {
2010-07-22 19:57:48 +02:00
{
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 ( ) ) ;
}
{
const std : : string code ( " struct A { template<int> struct { }; }; " ) ;
ASSERT_EQUALS ( " " , tokenizeAndStringify ( code . c_str ( ) , true ) ) ;
ASSERT_EQUALS ( " [test.cpp:1]: (error) syntax error \n " , errout . str ( ) ) ;
}
2011-01-16 11:54:28 +01:00
{
const std : : string code ( " enum ABC { A,B, typedef enum { C } }; " ) ;
tokenizeAndStringify ( code . c_str ( ) , true ) ;
ASSERT_EQUALS ( " [test.cpp:1]: (error) syntax error \n " , errout . str ( ) ) ;
}
2011-11-11 10:53:49 +01:00
{
// #3314 - don't report syntax error.
const std : : string code ( " struct A { typedef B::C (A::*f)(); }; " ) ;
tokenizeAndStringify ( code . c_str ( ) , true ) ;
ASSERT_EQUALS ( " [test.cpp:1]: (debug) Failed to parse 'typedef B :: C ( A :: * f ) ( ) ;'. The checking continues anyway. \n " , errout . str ( ) ) ;
}
2010-01-21 20:05:32 +01:00
}
2012-01-13 07:57:12 +01:00
void wrong_syntax2 ( ) { // #3504
const char code [ ] = " void f() { \n "
" X<int> x; \n "
" Y<int, int, int, int, int, char> y; \n "
" } \n "
" \n "
" void G( template <typename T> class (j) ) {} " ;
// don't segfault..
tokenizeAndStringify ( code ) ;
2012-06-02 16:15:12 +02:00
}
void wrong_syntax3 ( ) { // #3544
const char code [ ] = " X #define \n "
" { \n "
" ( \n "
" for( #endif typedef typedef cb[N] ) \n "
" ca[N]; = cb[i] \n "
" ) \n "
" } " ;
tokenizeAndStringify ( code ) ;
2012-01-13 07:57:12 +01:00
}
2012-06-12 21:07:17 +02:00
void wrong_syntax4 ( ) { // #3618
const char code [ ] = " typedef void (x) (int); return x& " ;
tokenizeAndStringify ( code ) ;
}
2011-10-13 20:53:06 +02:00
void wrong_syntax_if_macro ( ) {
2012-09-09 09:48:07 +02:00
// #2518 and #4171
const char code [ ] = " void f() { if MACRO() { } } " ;
ASSERT_EQUALS ( " void f ( ) { if ( MACRO ( ) ) { } } " , tokenizeAndStringify ( code , false ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-01-27 21:16:25 +01:00
}
2012-09-10 17:33:52 +02:00
void garbageCode ( ) {
tokenizeAndStringify ( " struct x foo_t; foo_t typedef y; " ) ;
}
2012-09-03 20:23:53 +02:00
void foreach ( ) {
// #3690
const std : : string code ( " void f() { for each ( char c in MyString ) { Console::Write(c); } } " ) ;
ASSERT_EQUALS ( " void f ( ) { for ( char c in MyString ) { Console :: Write ( c ) ; } } " , tokenizeAndStringify ( code . c_str ( ) ) ) ;
}
2011-10-13 20:53:06 +02:00
void minus ( ) {
2009-03-17 20:50:06 +01:00
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
2011-10-13 20:53:06 +02:00
void longtok ( ) {
2012-02-17 19:54:53 +01:00
const std : : string filedata ( 10000 , ' a ' ) ;
2008-12-18 22:28:57 +01:00
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
Settings settings ;
2008-12-18 22:28:57 +01:00
// tokenize..
2010-12-01 18:00:55 +01:00
Tokenizer tokenizer ( & settings , this ) ;
2008-12-18 22:28:57 +01:00
std : : istringstream istr ( filedata ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
// Expected result..
2012-02-17 19:54:53 +01:00
ASSERT_EQUALS ( filedata , tokenizer . tokens ( ) - > str ( ) ) ;
2008-12-18 22:28:57 +01:00
}
2009-02-04 20:40:48 +01:00
2010-12-15 18:45:53 +01:00
// Don’ t remove "(int *)"..
2011-10-13 20:53:06 +02:00
void removeCast1 ( ) {
2009-02-04 20:40:48 +01:00
const char code [ ] = " int *f(int *); " ;
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
Settings settings ;
2009-02-04 20:40:48 +01:00
// tokenize..
2010-12-01 18:00:55 +01:00
Tokenizer tokenizer ( & settings , this ) ;
2009-02-04 20:40:48 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyCasts ( ) ;
2012-04-16 19:51:07 +02:00
ASSERT_EQUALS ( " int * f ( int * ) ; " , tokenizer . tokens ( ) - > stringifyList ( 0 , false ) ) ;
2009-02-04 20:40:48 +01:00
}
2009-05-27 20:06:19 +02:00
// remove static_cast..
2011-10-13 20:53:06 +02:00
void removeCast2 ( ) {
2009-05-27 20:06:19 +02:00
const char code [ ] = " t = (static_cast<std::vector<int> *>(&p)); \n " ;
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
Settings settings ;
2009-05-27 20:06:19 +02:00
// tokenize..
2010-12-01 18:00:55 +01:00
Tokenizer tokenizer ( & settings , this ) ;
2009-05-27 20:06:19 +02:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyCasts ( ) ;
2012-04-16 19:51:07 +02:00
ASSERT_EQUALS ( " t = & p ; " , tokenizer . tokens ( ) - > stringifyList ( 0 , false ) ) ;
2009-05-27 20:06:19 +02:00
}
2011-10-13 20:53:06 +02:00
void removeCast3 ( ) {
2009-11-14 11:59:05 +01:00
// 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 ) ) ;
}
2011-10-13 20:53:06 +02:00
void removeCast4 ( ) {
2009-11-17 19:03:23 +01:00
// ticket #970
const char code [ ] = " if (a >= (unsigned)(b)) {} " ;
2011-09-18 16:31:31 +02:00
const char expected [ ] = " if ( a >= ( unsigned int ) b ) { } " ;
2009-11-17 19:03:23 +01:00
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2009-02-04 20:40:48 +01:00
2011-10-13 20:53:06 +02:00
void removeCast5 ( ) {
2010-07-05 13:16:33 +02:00
// ticket #1817
ASSERT_EQUALS ( " a . data = f ; " , tokenizeAndStringify ( " a->data = reinterpret_cast<void*>(static_cast<intptr_t>(f)); " , true ) ) ;
}
2011-10-13 20:53:06 +02:00
void removeCast6 ( ) {
2010-10-20 20:38:37 +02:00
// ticket #2103
2012-01-15 14:50:01 +01:00
ASSERT_EQUALS ( " if ( ! x ) { ; } " , tokenizeAndStringify ( " if (x == (char *) ((void *)0)) ; " , true ) ) ;
2010-10-20 20:38:37 +02:00
}
2011-11-25 07:23:54 +01:00
void removeCast7 ( ) {
ASSERT_EQUALS ( " str = malloc ( 3 ) " , tokenizeAndStringify ( " str=(char **)malloc(3) " , true ) ) ;
}
void removeCast8 ( ) {
ASSERT_EQUALS ( " ptr1 = ptr2 " , tokenizeAndStringify ( " ptr1=(int * **)ptr2 " , true ) ) ;
}
2012-02-18 14:26:00 +01:00
void removeCast9 ( ) {
ASSERT_EQUALS ( " f ( ( double ) v1 * v2 ) " , tokenizeAndStringify ( " f((double)(v1)*v2) " , true ) ) ;
}
2012-02-27 18:55:36 +01:00
void removeCast10 ( ) {
ASSERT_EQUALS ( " ; ( * f ) ( p ) ; " , tokenizeAndStringify ( " ; (*(void (*)(char *))f)(p); " , true ) ) ;
}
2012-06-19 20:07:39 +02:00
void removeCast11 ( ) {
ASSERT_EQUALS ( " ; x = 0 ; " , tokenizeAndStringify ( " ; *(int *)&x = 0; " , true ) ) ;
}
2011-10-13 20:53:06 +02:00
void inlineasm ( ) {
2012-01-15 19:48:27 +01:00
ASSERT_EQUALS ( " asm ( \" mov ax , bx \" ) ; " , tokenizeAndStringify ( " asm { mov ax,bx }; " ) ) ;
ASSERT_EQUALS ( " asm ( \" mov ax , bx \" ) ; " , tokenizeAndStringify ( " _asm { mov ax,bx }; " ) ) ;
2012-02-19 16:04:35 +01:00
ASSERT_EQUALS ( " asm ( \" mov ax , bx \" ) ; " , tokenizeAndStringify ( " _asm mov ax,bx " ) ) ;
2012-01-15 19:48:27 +01:00
ASSERT_EQUALS ( " asm ( \" mov ax , bx \" ) ; " , tokenizeAndStringify ( " __asm { mov ax,bx }; " ) ) ;
ASSERT_EQUALS ( " asm ( \" \" mov ax,bx \" \" ) ; " , tokenizeAndStringify ( " __asm__ __volatile__ ( \" mov ax,bx \" ); " ) ) ;
ASSERT_EQUALS ( " asm ( \" _emit 12h \" ) ; " , tokenizeAndStringify ( " __asm _emit 12h ; " ) ) ;
ASSERT_EQUALS ( " asm ( \" mov a , b \" ) ; " , tokenizeAndStringify ( " __asm mov a, b ; " ) ) ;
ASSERT_EQUALS ( " asm ( \" \" fnstcw %0 \" : \" = m \" ( old_cw ) \" ) ; " , tokenizeAndStringify ( " asm volatile ( \" fnstcw %0 \" : \" = m \" (old_cw)); " ) ) ;
ASSERT_EQUALS ( " asm ( \" \" fnstcw %0 \" : \" = m \" ( old_cw ) \" ) ; " , tokenizeAndStringify ( " __asm__ ( \" fnstcw %0 \" : \" = m \" (old_cw)); " ) ) ;
ASSERT_EQUALS ( " asm ( \" \" ddd \" \" ) ; " , tokenizeAndStringify ( " __asm __volatile__ ( \" ddd \" ) ; " ) ) ;
ASSERT_EQUALS ( " asm ( \" \" mov ax,bx \" \" ) ; " , tokenizeAndStringify ( " __asm__ volatile ( \" mov ax,bx \" ); " ) ) ;
2011-12-12 20:50:49 +01:00
// 'asm ( ) ;' should be in the same line
2011-12-13 21:42:38 +01:00
ASSERT_EQUALS ( " ; \n \n asm ( \" \" mov ax,bx \" \" ) ; " , tokenizeAndStringify ( " ; \n \n __asm__ volatile ( \" mov ax,bx \" ); " , true ) ) ;
2008-12-18 22:28:57 +01:00
}
2011-10-13 20:53:06 +02:00
void pointers_condition ( ) {
2010-03-24 19:55:02 +01:00
ASSERT_EQUALS ( " ( p ) " , tokenizeAndStringify ( " ( p != NULL ) " , true ) ) ;
ASSERT_EQUALS ( " ( p ) " , tokenizeAndStringify ( " ( NULL != p ) " , true ) ) ;
ASSERT_EQUALS ( " ( this . p ) " , tokenizeAndStringify ( " ( this->p != NULL ) " , true ) ) ;
ASSERT_EQUALS ( " ( this . p ) " , tokenizeAndStringify ( " ( NULL != this->p ) " , true ) ) ;
ASSERT_EQUALS ( " ( Foo :: p ) " , tokenizeAndStringify ( " ( Foo::p != NULL ) " , true ) ) ;
ASSERT_EQUALS ( " ( Foo :: p ) " , tokenizeAndStringify ( " ( NULL != Foo::p ) " , true ) ) ;
ASSERT_EQUALS ( " ( ! p ) " , tokenizeAndStringify ( " ( p == NULL ) " , true ) ) ;
ASSERT_EQUALS ( " ( ! p ) " , tokenizeAndStringify ( " ( NULL == p ) " , true ) ) ;
ASSERT_EQUALS ( " ( ! this . p ) " , tokenizeAndStringify ( " ( this->p == NULL ) " , true ) ) ;
ASSERT_EQUALS ( " ( ! this . p ) " , tokenizeAndStringify ( " ( NULL == this->p ) " , true ) ) ;
ASSERT_EQUALS ( " ( ! Foo :: p ) " , tokenizeAndStringify ( " ( Foo::p == NULL ) " , true ) ) ;
ASSERT_EQUALS ( " ( ! Foo :: p ) " , tokenizeAndStringify ( " ( NULL == Foo::p ) " , true ) ) ;
ASSERT_EQUALS ( " ( p1 || ! p2 ) " , tokenizeAndStringify ( " ( p1 != NULL || p2 == NULL ) " , true ) ) ;
ASSERT_EQUALS ( " ( p1 && ! p2 ) " , tokenizeAndStringify ( " ( p1 != NULL && p2 == NULL ) " , true ) ) ;
2010-07-05 22:41:02 +02:00
ASSERT_EQUALS ( " a & & b " , tokenizeAndStringify ( " a & &b " , true ) ) ;
2010-03-24 19:55:02 +01:00
ASSERT_EQUALS ( " ( ! p ) " , tokenizeAndStringify ( " ( p == false ) " , true ) ) ;
ASSERT_EQUALS ( " ( ! p ) " , tokenizeAndStringify ( " ( p == 0 ) " , true ) ) ;
ASSERT_EQUALS ( " ( ! p ) " , tokenizeAndStringify ( " ( p == ' \\ 0' ) " , true ) ) ;
ASSERT_EQUALS ( " ( ! p ) " , tokenizeAndStringify ( " ( p == 0L ) " , true ) ) ;
ASSERT_EQUALS ( " ( ! p ) " , tokenizeAndStringify ( " ( p == 0UL ) " , true ) ) ;
ASSERT_EQUALS ( " ( ! p ) " , tokenizeAndStringify ( " ( p == 0ul ) " , true ) ) ;
ASSERT_EQUALS ( " ( ! p ) " , tokenizeAndStringify ( " ( p == 0l ) " , true ) ) ;
ASSERT_EQUALS ( " ( ! p ) " , tokenizeAndStringify ( " ( false == p ) " , true ) ) ;
ASSERT_EQUALS ( " ( ! p ) " , tokenizeAndStringify ( " ( 0 == p ) " , true ) ) ;
ASSERT_EQUALS ( " ( ! p ) " , tokenizeAndStringify ( " ( ' \\ 0' == p ) " , true ) ) ;
ASSERT_EQUALS ( " ( ! p ) " , tokenizeAndStringify ( " ( 0L == p ) " , true ) ) ;
ASSERT_EQUALS ( " ( ! p ) " , tokenizeAndStringify ( " ( 0UL == p ) " , true ) ) ;
ASSERT_EQUALS ( " ( ! p ) " , tokenizeAndStringify ( " ( 0ul == p ) " , true ) ) ;
ASSERT_EQUALS ( " ( ! p ) " , tokenizeAndStringify ( " ( 0l == p ) " , true ) ) ;
2009-07-25 13:11:29 +02:00
}
2010-03-24 19:55:02 +01:00
2011-10-13 20:53:06 +02:00
void ifAddBraces1 ( ) {
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
2011-10-13 20:53:06 +02:00
void ifAddBraces2 ( ) {
2008-12-22 19:05:22 +01:00
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
2011-10-13 20:53:06 +02:00
void ifAddBraces3 ( ) {
2008-12-22 19:05:22 +01:00
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
}
2011-10-13 20:53:06 +02:00
void ifAddBraces4 ( ) {
2008-12-22 19:32:04 +01:00
const char code [ ] = " char * foo () \n "
" { \n "
" char *str = malloc(10); \n "
" if (somecondition) \n "
2010-10-27 12:14:40 +02:00
" for ( ; ; ) \n "
2008-12-22 19:32:04 +01:00
" { } \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 "
2010-10-27 12:14:40 +02:00
" for ( ; ; ) \n "
2009-06-20 13:20:51 +02:00
" { } } \n "
" return str ; \n "
" } " , tokenizeAndStringify ( code , true ) ) ;
2008-12-22 19:32:04 +01:00
}
2011-10-13 20:53:06 +02:00
void ifAddBraces5 ( ) {
2009-03-15 13:44:57 +01:00
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 "
2011-11-12 12:01:31 +01:00
" \n "
" return ; } \n \n "
2009-06-20 13:20:51 +02:00
" return ; \n "
" } " , tokenizeAndStringify ( code , true ) ) ;
2009-03-15 13:44:57 +01:00
}
2011-10-13 20:53:06 +02:00
void ifAddBraces6 ( ) {
2009-05-09 22:12:14 +02:00
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
2011-10-13 20:53:06 +02:00
void ifAddBraces7 ( ) {
2009-06-22 22:54:11 +02:00
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 ) ) ;
}
2011-10-13 20:53:06 +02:00
void ifAddBraces9 ( ) {
2009-11-20 20:38:30 +01:00
// ticket #990
const char code [ ] =
2010-08-25 22:10:21 +02:00
" void f() { "
2009-11-20 20:38:30 +01:00
" for (int k=0; k<VectorSize; k++) "
" LOG_OUT(ID_Vector[k]) "
" } " ;
const char expected [ ] =
2010-08-25 22:10:21 +02:00
" void f ( ) { "
2009-11-20 20:38:30 +01:00
" for ( int k = 0 ; k < VectorSize ; k ++ ) { "
" LOG_OUT ( ID_Vector [ k ] ) "
" } "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2011-10-13 20:53:06 +02:00
void ifAddBraces10 ( ) {
2010-02-07 15:00:19 +01:00
// ticket #1361
const char code [ ] = " { DEBUG(if (x) y; else z); } " ;
const char expected [ ] = " { DEBUG ( if ( x ) y ; else z ) ; } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
2010-02-20 11:43:53 +01:00
}
2011-10-13 20:53:06 +02:00
void ifAddBraces11 ( ) {
2010-02-20 11:43:53 +01:00
const char code [ ] = " { if (x) if (y) ; else ; } " ;
const char expected [ ] = " { if ( x ) { if ( y ) { ; } else { ; } } } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
2010-02-20 13:24:50 +01:00
}
2011-10-13 20:53:06 +02:00
void ifAddBraces12 ( ) {
2010-02-20 13:24:50 +01:00
// ticket #1424
const char code [ ] = " { if (x) do { } while(x); } " ;
const char expected [ ] = " { if ( x ) { do { } while ( x ) ; } } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
2010-02-07 15:00:19 +01:00
}
2011-10-13 20:53:06 +02:00
void ifAddBraces13 ( ) {
2010-06-26 17:15:44 +02:00
// ticket #1809
const char code [ ] = " { if (x) if (y) { } else { } else { } } " ;
const char expected [ ] = " { if ( x ) { if ( y ) { } else { } } else { } } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
// ticket #1809
const char code2 [ ] = " { if (x) while (y) { } else { } } " ;
const char expected2 [ ] = " { if ( x ) { while ( y ) { } } else { } } " ;
ASSERT_EQUALS ( expected2 , tokenizeAndStringify ( code2 , true ) ) ;
}
2011-10-13 20:53:06 +02:00
void ifAddBraces14 ( ) {
2011-02-24 18:49:16 +01:00
// ticket #2610 (segfault)
tokenizeAndStringify ( " if()<{} " , false ) ;
}
2011-10-13 20:53:06 +02:00
void ifAddBraces15 ( ) {
2011-03-01 18:02:50 +01:00
// ticket #2616 - unknown macro before if
ASSERT_EQUALS ( " { A if ( x ) { y ( ) ; } } " , tokenizeAndStringify ( " {A if(x)y();} " , false ) ) ;
}
2012-01-26 22:25:19 +01:00
void ifAddBraces16 ( ) { // ticket # 2739 (segmentation fault)
tokenizeAndStringify ( " if()x " ) ;
ASSERT_EQUALS ( " [test.cpp:1]: (error) syntax error \n " , errout . str ( ) ) ;
// ticket #2873 - the fix is not needed anymore.
{
const char code [ ] = " void f() { "
" (void) ( { if(*p) (*p) = x(); } ) "
" } " ;
ASSERT_EQUALS ( " void f ( ) { ( void ) ( { if ( * p ) ( * p ) = x ( ) ; } ) } " ,
tokenizeAndStringify ( code ) ) ;
}
}
void ifAddBraces17 ( ) {
2011-12-12 20:50:49 +01:00
const char code [ ] = " void f() \n "
" { \n "
" if (a) \n "
" bar1 (); \n "
" \n "
" else \n "
" bar2 (); \n "
" } \n " ;
ASSERT_EQUALS ( " void f ( ) \n "
" { \n "
" if ( a ) { \n "
" bar1 ( ) ; \n \n "
" } else { \n "
" bar2 ( ) ; } \n "
" } " , tokenizeAndStringify ( code , true ) ) ;
}
2012-01-26 22:25:19 +01:00
void ifAddBraces18 ( ) {
2011-12-17 19:05:14 +01:00
// ticket #3424 - if if { } else else
ASSERT_EQUALS ( " { if ( x ) { if ( y ) { } else { ; } } else { ; } } " ,
tokenizeAndStringify ( " { if(x) if(y){}else;else;} " , false ) ) ;
}
2011-12-12 20:50:49 +01:00
2011-02-24 18:49:16 +01:00
2011-10-13 20:53:06 +02:00
void whileAddBraces ( ) {
2010-06-13 08:00:46 +02:00
const char code [ ] = " ;while(a); " ;
ASSERT_EQUALS ( " ; while ( a ) { ; } " , tokenizeAndStringify ( code , true ) ) ;
2009-06-20 13:20:51 +02:00
}
2009-05-09 22:12:14 +02:00
2011-10-13 20:53:06 +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
2011-10-13 20:53:06 +02:00
void forAddBraces ( ) {
2010-06-13 08:00:46 +02:00
{
const char code [ ] = " void f() { \n "
" for(;;) \n "
" if (a) { } \n "
" else { } \n "
" } " ;
const char expected [ ] = " void f ( ) { \n "
" for ( ; ; ) { \n "
" if ( a ) { } \n "
" else { } } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
{
const char code [ ] = " void f() { \n "
" for(;;) \n "
" if (a) { } \n "
" else if (b) { } \n "
" else { } \n "
" } " ;
const char expected [ ] = " void f ( ) { \n "
" for ( ; ; ) { \n "
" if ( a ) { } \n "
" else { if ( b ) { } \n "
" else { } } } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
}
2011-10-13 20:53:06 +02:00
std : : string simplifyKnownVariables ( const char code [ ] ) {
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
2009-09-19 08:55:41 +02:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyKnownVariables ( ) ;
2012-04-16 19:51:07 +02:00
return tokenizer . tokens ( ) - > stringifyList ( 0 , false ) ;
2009-09-19 08:55:41 +02:00
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables1 ( ) {
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
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables2 ( ) {
2008-12-22 19:08:32 +01:00
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
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables3 ( ) {
2008-12-22 19:08:32 +01:00
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
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables4 ( ) {
2008-12-22 19:08:32 +01:00
const char code [ ] = " void f() \n "
" { \n "
" int a = 4; \n "
" if ( g(a)); \n "
" } \n " ;
2008-12-18 22:28:57 +01:00
2010-02-08 18:25:08 +01:00
// TODO: if a is passed by value is is ok to simplify..
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2010-02-08 18:25:08 +01:00
" void f ( ) { int a ; a = 4 ; if ( g ( 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
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables5 ( ) {
2008-12-22 19:08:32 +01:00
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
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables6 ( ) {
2009-02-20 18:27:57 +01:00
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
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables7 ( ) {
2009-02-24 17:11:37 +01:00
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
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables8 ( ) {
2009-02-27 19:25:47 +01:00
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 (
2011-12-02 22:49:14 +01:00
" void foo ( ) { int i ; i = 23 ; abc [ 23 ] = 0 ; } " ,
2009-09-19 13:21:50 +02:00
simplifyKnownVariables ( code ) ) ;
2009-03-25 07:10:17 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables9 ( ) {
2009-03-25 07:10:17 +01:00
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
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables10 ( ) {
2009-05-01 20:31:07 +02:00
{
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
2010-11-06 11:08:05 +01:00
const std : : string expected1 ( " void f ( ) { "
" bool b ; b = false ; "
" { b = true ; } " ) ;
2009-09-19 08:55:41 +02:00
TODO_ASSERT_EQUALS (
2010-11-06 11:08:05 +01:00
expected1 + " if ( true ) { a ( ) ; } } " ,
expected1 + " if ( b ) { a ( ) ; } } " ,
2009-09-19 13:21:50 +02:00
simplifyKnownVariables ( code ) ) ;
2011-01-30 12:54:19 +01:00
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 ( ) ; } } " ,
2011-01-30 12:54:19 +01:00
" void f ( ) { bool b ; b = false ; { b = false ; } { b = true ; } if ( b ) { a ( ) ; } } " ,
2009-09-19 13:21:50 +02:00
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 "
2010-02-08 18:25:08 +01:00
" return b; \n "
2009-05-03 20:57:27 +02:00
" } \n " ;
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2010-02-08 18:25:08 +01:00
" void f ( ) { int b ; b = 0 ; b = 1 ; for ( int i = 0 ; i < 10 ; i ++ ) { } return 1 ; } " ,
2009-09-19 13:21:50 +02:00
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
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables11 ( ) {
2009-06-19 18:03:09 +02:00
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 (
2011-12-02 22:35:03 +01:00
" int main ( ) { int foo ; foo = 0 ; } " ,
2009-09-19 13:21:50 +02:00
simplifyKnownVariables ( code ) ) ;
2009-06-19 18:03:09 +02:00
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables12 ( ) {
2009-08-01 15:57:54 +02:00
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
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables13 ( ) {
2009-08-22 10:23:55 +02:00
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
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables14 ( ) {
2009-09-28 17:15:31 +02:00
// 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
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables15 ( ) {
2009-10-06 08:14:59 +02:00
{
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
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables16 ( ) {
2009-10-10 10:13:56 +02:00
// ticket #807 - segmentation fault when macro isn't found
const char code [ ] = " void f ( ) { int n = 1; DISPATCH(while); } " ;
simplifyKnownVariables ( code ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables17 ( ) {
2009-11-02 23:07:15 +01:00
// 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 ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables18 ( ) {
2010-01-15 22:46:47 +01:00
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 ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables19 ( ) {
2010-01-24 19:27:39 +01:00
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 ) ) ;
2010-02-20 15:50:44 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables20 ( ) {
2010-02-20 15:50:44 +01:00
const char code [ ] = " void f() \n "
" { \n "
" int i = 0; \n "
" if (x) { \n "
" if (i) i=0; \n "
" } \n "
" } \n " ;
ASSERT_EQUALS (
" void f ( ) { int i ; i = 0 ; if ( x ) { if ( 0 ) { i = 0 ; } } } " ,
simplifyKnownVariables ( code ) ) ;
2010-01-24 19:27:39 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables21 ( ) {
2010-03-14 09:57:34 +01:00
const char code [ ] = " void foo() { int n = 10; for (int i = 0; i < n; ++i) { } } " ;
ASSERT_EQUALS (
" void foo ( ) { int n ; n = 10 ; for ( int i = 0 ; i < 10 ; ++ i ) { } } " ,
simplifyKnownVariables ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables22 ( ) {
2010-04-04 18:48:51 +02:00
// This testcase is related to ticket #1169
2010-04-24 07:59:53 +02:00
{
const char code [ ] = " void foo() \n "
" { \n "
" int n = 10; \n "
" i = (n >> 1); \n "
" } \n " ;
2010-04-04 18:48:51 +02:00
2010-04-24 07:59:53 +02:00
ASSERT_EQUALS (
2010-12-18 09:44:58 +01:00
" void foo ( ) { int n ; n = 10 ; i = 10 >> 1 ; } " ,
2010-04-24 07:59:53 +02:00
simplifyKnownVariables ( code ) ) ;
}
{
const char code [ ] = " void foo() \n "
" { \n "
" int n = 10; \n "
" i = (n << 1); \n "
" } \n " ;
2010-04-04 18:48:51 +02:00
2010-04-24 07:59:53 +02:00
ASSERT_EQUALS (
2010-12-18 09:44:58 +01:00
" void foo ( ) { int n ; n = 10 ; i = 10 << 1 ; } " ,
2010-04-24 07:59:53 +02:00
simplifyKnownVariables ( code ) ) ;
}
{
const char code [ ] = " void foo() \n "
" { \n "
" int n = 10; \n "
" i = (1 << n); \n "
" } \n " ;
ASSERT_EQUALS (
2010-12-18 09:44:58 +01:00
" void foo ( ) { int n ; n = 10 ; i = 1 << 10 ; } " ,
2010-04-24 07:59:53 +02:00
simplifyKnownVariables ( code ) ) ;
}
{
const char code [ ] = " void foo() \n "
" { \n "
" int n = 10; \n "
" i = (1 >> n); \n "
" } \n " ;
ASSERT_EQUALS (
2010-12-18 09:44:58 +01:00
" void foo ( ) { int n ; n = 10 ; i = 1 >> 10 ; } " ,
2010-04-24 07:59:53 +02:00
simplifyKnownVariables ( code ) ) ;
}
2010-04-04 18:48:51 +02:00
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables23 ( ) {
2010-04-15 19:09:19 +02:00
// This testcase is related to ticket #1596
const char code [ ] = " void foo(int x) \n "
" { \n "
" int a[10], c = 0; \n "
" if (x) { \n "
" a[c] = 0; \n "
" c++; \n "
" } else { \n "
" a[c] = 0; \n "
" } \n "
" } \n " ;
TODO_ASSERT_EQUALS (
" void foo ( int x ) "
" { "
" int a [ 10 ] ; int c ; c = 0 ; "
" if ( x ) { a [ 0 ] = 0 ; c = 1 ; } "
" else { a [ 0 ] = 0 ; } "
" } " ,
" void foo ( int x ) "
" { "
" int a [ 10 ] ; int c ; c = 0 ; "
" if ( x ) { a [ 0 ] = 0 ; c ++ ; } "
" else { a [ c ] = 0 ; } "
" } " ,
2011-01-30 12:54:19 +01:00
2010-04-15 19:09:19 +02:00
simplifyKnownVariables ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables24 ( ) {
2010-05-18 19:55:23 +02:00
{
// This testcase is related to ticket #1596
const char code [ ] = " void foo() \n "
" { \n "
" int c; \n "
" for (c=0;c<10;++c) { } \n "
" a[c] = 0; \n "
" } \n " ;
2010-05-02 14:41:21 +02:00
2010-05-18 19:55:23 +02:00
ASSERT_EQUALS (
" void foo ( ) "
" { "
" int c ; "
" for ( c = 0 ; c < 10 ; ++ c ) { } "
" a [ 10 ] = 0 ; "
" } " ,
simplifyKnownVariables ( code ) ) ;
}
{
// #1692 - unknown counter value after for loop
const char code [ ] = " void foo(const char s[]) \n "
" { \n "
" int x[3]; \n "
" int i; \n "
" for (i = 0; i < 3; ++i) { \n "
" if (s[i]) break; \n "
" } "
" if (i < 3) x[i] = 0; \n "
" } \n " ;
ASSERT_EQUALS (
" void foo ( const char s [ ] ) "
" { "
" int x [ 3 ] ; "
" int i ; "
" for ( i = 0 ; i < 3 ; ++ i ) { "
" if ( s [ i ] ) { break ; } "
" } "
" if ( i < 3 ) { x [ i ] = 0 ; } "
" } " ,
simplifyKnownVariables ( code ) ) ;
}
2010-05-02 14:41:21 +02:00
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables25 ( ) {
2010-05-05 18:29:56 +02:00
{
// This testcase is related to ticket #1646
const char code [ ] = " void foo(char *str) \n "
" { \n "
" int i; \n "
" for (i=0;i<10;++i) { \n "
" if (*str == 0) goto label; \n "
" } \n "
" return; \n "
" label: \n "
" str[i] = 0; \n "
" } \n " ;
2010-05-04 20:02:47 +02:00
2010-05-05 18:29:56 +02:00
// Current result
ASSERT_EQUALS (
" void foo ( char * str ) "
" { "
" int i ; "
" for ( i = 0 ; i < 10 ; ++ i ) { "
2011-04-02 21:21:05 +02:00
" if ( ! * str ) { goto label ; } "
2010-05-05 18:29:56 +02:00
" } "
" return ; "
" label : ; "
" str [ i ] = 0 ; "
" } " ,
simplifyKnownVariables ( code ) ) ;
}
{
// This testcase is related to ticket #1646
const char code [ ] = " void foo(char *str) \n "
" { \n "
" int i; \n "
" for (i=0;i<10;++i) { } \n "
" return; \n "
" str[i] = 0; \n "
" } \n " ;
// Current result
ASSERT_EQUALS (
" void foo ( char * str ) "
" { "
" int i ; "
" for ( i = 0 ; i < 10 ; ++ i ) { } "
2011-10-13 12:26:27 +02:00
" return ; "
" str [ i ] = 0 ; "
2010-05-05 18:29:56 +02:00
" } " ,
simplifyKnownVariables ( code ) ) ;
}
2010-05-04 20:02:47 +02:00
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables26 ( ) {
2010-06-15 17:56:14 +02:00
// This testcase is related to ticket #887
const char code [ ] = " void foo() \n "
" { \n "
" int i; \n "
" for (i=0;i<10;++i) { } \n "
" int k = i++; \n "
" } \n " ;
ASSERT_EQUALS (
" void foo ( ) "
" { "
" int i ; "
" for ( i = 0 ; i < 10 ; ++ i ) { } "
" int k ; k = 10 ; "
" } " ,
simplifyKnownVariables ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables27 ( ) {
2010-06-19 14:00:45 +02:00
// This testcase is related to ticket #1633
const char code [ ] = " void foo() \n "
" { \n "
" int i1 = 1; \n "
" int i2 = 2; \n "
" int i3 = (i1 + i2) * 3; \n "
" } \n " ;
ASSERT_EQUALS (
" void foo ( ) "
" { "
" int i1 ; i1 = 1 ; "
" int i2 ; i2 = 2 ; "
" int i3 ; i3 = ( 1 + 2 ) * 3 ; "
" } " ,
simplifyKnownVariables ( code ) ) ;
}
2010-05-02 14:41:21 +02:00
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables28 ( ) {
2010-06-19 17:54:38 +02:00
const char code [ ] = " void foo(int g) \n "
" { \n "
" int i = 2; \n "
" if (g) { \n "
" } \n "
" if (i > 0) { \n "
" } \n "
" } \n " ;
ASSERT_EQUALS (
" void foo ( int g ) "
" { "
" int i ; i = 2 ; "
" if ( g ) { } "
" if ( 0 < 2 ) { } "
" } " ,
simplifyKnownVariables ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables29 ( ) { // ticket #1811
2010-06-26 07:50:53 +02:00
{
const char code [ ] = " int foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h + i; \n "
" } \n " ;
const char expected [ ] = " \n \n ##file 0 \n "
" 1: int foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2010-10-26 21:05:20 +02:00
" 4: \n "
2010-06-26 07:50:53 +02:00
" 5: return u@1 + v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
}
{
const char code [ ] = " int foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h - i; \n "
" } \n " ;
const char expected [ ] = " \n \n ##file 0 \n "
" 1: int foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2010-10-26 21:05:20 +02:00
" 4: \n "
2010-06-26 07:50:53 +02:00
" 5: return u@1 - v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
}
{
const char code [ ] = " int foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h * i; \n "
" } \n " ;
const char expected [ ] = " \n \n ##file 0 \n "
" 1: int foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2010-10-26 21:05:20 +02:00
" 4: \n "
2010-06-26 07:50:53 +02:00
" 5: return u@1 * v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
}
{
const char code [ ] = " int foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h / i; \n "
" } \n " ;
const char expected [ ] = " \n \n ##file 0 \n "
" 1: int foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2010-10-26 21:05:20 +02:00
" 4: \n "
2010-06-26 07:50:53 +02:00
" 5: return u@1 / v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
}
{
const char code [ ] = " int foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h & i; \n "
" } \n " ;
const char expected [ ] = " \n \n ##file 0 \n "
" 1: int foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2010-10-26 21:05:20 +02:00
" 4: \n "
2010-06-26 07:50:53 +02:00
" 5: return u@1 & v@2 ; \n "
" 6: } \n " ;
2010-11-06 11:38:58 +01:00
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
2010-06-26 07:50:53 +02:00
}
{
const char code [ ] = " int foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h | i; \n "
" } \n " ;
2011-02-13 22:09:04 +01:00
const char expected [ ] = " \n \n ##file 0 \n "
" 1: int foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2011-02-13 22:09:04 +01:00
" 4: \n "
" 5: return u@1 | v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
2010-06-26 07:50:53 +02:00
}
{
const char code [ ] = " int foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h ^ i; \n "
" } \n " ;
2011-02-13 22:13:19 +01:00
const char expected [ ] = " \n \n ##file 0 \n "
" 1: int foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2011-02-13 22:13:19 +01:00
" 4: \n "
" 5: return u@1 ^ v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
2010-06-26 07:50:53 +02:00
}
{
const char code [ ] = " int foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h % i; \n "
" } \n " ;
2011-02-13 22:03:46 +01:00
const char expected [ ] = " \n \n ##file 0 \n "
" 1: int foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2011-02-13 22:03:46 +01:00
" 4: \n "
" 5: return u@1 % v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
2010-06-26 07:50:53 +02:00
}
{
const char code [ ] = " int foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h >> i; \n "
" } \n " ;
const char expected [ ] = " \n \n ##file 0 \n "
" 1: int foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2010-10-26 21:05:20 +02:00
" 4: \n "
2010-06-26 07:50:53 +02:00
" 5: return u@1 >> v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
}
{
const char code [ ] = " int foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h << i; \n "
" } \n " ;
const char expected [ ] = " \n \n ##file 0 \n "
" 1: int foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2010-10-26 21:05:20 +02:00
" 4: \n "
2010-06-26 07:50:53 +02:00
" 5: return u@1 << v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
}
{
const char code [ ] = " bool foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h == i; \n "
" } \n " ;
2011-02-13 22:24:45 +01:00
const char expected [ ] = " \n \n ##file 0 \n "
" 1: bool foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2011-02-13 22:24:45 +01:00
" 4: \n "
" 5: return u@1 == v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
2010-06-26 07:50:53 +02:00
}
{
const char code [ ] = " bool foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h != i; \n "
" } \n " ;
2011-02-13 22:24:45 +01:00
const char expected [ ] = " \n \n ##file 0 \n "
2010-06-26 07:50:53 +02:00
" 1: bool foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2011-02-13 22:24:45 +01:00
" 4: \n "
" 5: return u@1 != v@2 ; \n "
2010-06-26 07:50:53 +02:00
" 6: } \n " ;
2011-02-13 22:24:45 +01:00
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
2010-06-26 07:50:53 +02:00
}
{
const char code [ ] = " bool foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h > i; \n "
" } \n " ;
2011-02-13 22:24:45 +01:00
const char expected [ ] = " \n \n ##file 0 \n "
" 1: bool foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2011-02-13 22:24:45 +01:00
" 4: \n "
" 5: return u@1 > v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
2010-06-26 07:50:53 +02:00
}
{
const char code [ ] = " bool foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h >= i; \n "
" } \n " ;
2011-02-13 22:24:45 +01:00
const char expected [ ] = " \n \n ##file 0 \n "
" 1: bool foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2011-02-13 22:24:45 +01:00
" 4: \n "
" 5: return u@1 >= v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
2010-06-26 07:50:53 +02:00
}
{
const char code [ ] = " bool foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h < i; \n "
" } \n " ;
2011-02-13 22:24:45 +01:00
const char expected [ ] = " \n \n ##file 0 \n "
" 1: bool foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2011-02-13 22:24:45 +01:00
" 4: \n "
" 5: return u@1 < v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
2010-06-26 07:50:53 +02:00
}
{
const char code [ ] = " bool foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h <= i; \n "
" } \n " ;
2011-02-13 22:24:45 +01:00
const char expected [ ] = " \n \n ##file 0 \n "
" 1: bool foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2011-02-13 22:24:45 +01:00
" 4: \n "
" 5: return u@1 <= v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
2010-06-26 07:50:53 +02:00
}
{
const char code [ ] = " bool foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h && i; \n "
" } \n " ;
2011-01-30 12:54:19 +01:00
const char wanted [ ] = " \n \n ##file 0 \n "
" 1: bool foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
" 4: \n "
2011-01-30 12:54:19 +01:00
" 5: return u@1 && v@2 ; \n "
" 6: } \n " ;
2011-12-02 23:01:55 +01:00
const char current [ ] = " \n \n ##file 0 \n 1: bool foo ( int u@1 , int v@2 ) \n 2: { \n 3: \n 4: int i@4 ; i@4 = v@2 ; \n 5: return u@1 && i@4 ; \n 6: } \n " ;
2011-01-30 12:54:19 +01:00
TODO_ASSERT_EQUALS ( wanted , current , tokenizeDebugListing ( code , true ) ) ;
2010-06-26 07:50:53 +02:00
}
{
const char code [ ] = " bool foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h || i; \n "
" } \n " ;
2011-01-30 12:54:19 +01:00
const char wanted [ ] = " \n \n ##file 0 \n "
" 1: bool foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
" 3: ; \n "
" 4: ; \n "
" 5: return u@1 || v@2 ; \n "
" 6: } \n " ;
2011-12-02 23:01:55 +01:00
const char current [ ] = " \n \n ##file 0 \n 1: bool foo ( int u@1 , int v@2 ) \n 2: { \n 3: \n 4: int i@4 ; i@4 = v@2 ; \n 5: return u@1 || i@4 ; \n 6: } \n " ;
2011-01-30 12:54:19 +01:00
TODO_ASSERT_EQUALS ( wanted , current , tokenizeDebugListing ( code , true ) ) ;
2010-06-26 07:50:53 +02:00
}
}
2010-06-19 17:54:38 +02:00
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables30 ( ) {
2010-10-10 19:27:42 +02:00
const char code [ ] = " int foo() { \n "
" iterator it1 = ints.begin(); \n "
" iterator it2 = it1; \n "
" for (++it2;it2!=ints.end();++it2); \n "
" } \n " ;
const char expected [ ] = " int foo ( ) { \n "
" iterator it1 ; it1 = ints . begin ( ) ; \n "
" iterator it2 ; it2 = it1 ; \n "
" for ( ++ it2 ; it2 != ints . end ( ) ; ++ it2 ) { ; } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables31 ( ) {
2010-11-06 19:42:38 +01:00
const char code [ ] = " void foo(const char str[]) { \n "
" const char *p = str; \n "
" if (p[0] == 0) { \n "
" } \n "
" } \n " ;
const char expected [ ] = " void foo ( const char str [ ] ) { \n "
" const char * p ; p = str ; \n "
" if ( str [ 0 ] == 0 ) { \n "
" } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables32 ( ) {
2011-07-20 09:07:47 +02:00
{
const char code [ ] = " void foo() { \n "
" const int x = 0; \n "
" bar(0,x); \n "
" } \n " ;
2011-12-02 22:35:03 +01:00
const char expected [ ] = " void foo ( ) { \n \n bar ( 0 , 0 ) ; \n } " ;
2011-07-20 09:07:47 +02:00
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
{
const char code [ ] = " static int const SZ = 22; char str[SZ]; \n " ;
2011-12-02 22:35:03 +01:00
ASSERT_EQUALS ( " char str [ 22 ] ; " , tokenizeAndStringify ( code , true ) ) ;
2011-07-20 09:07:47 +02:00
}
2010-11-13 18:45:35 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables33 ( ) {
2010-11-13 18:45:35 +01:00
const char code [ ] = " static void foo(struct Foo *foo) { \n "
" foo->a = 23; \n "
" x[foo->a] = 0; \n "
" } \n " ;
const char expected [ ] = " static void foo ( struct Foo * foo ) { \n "
" foo . a = 23 ; \n "
" x [ 23 ] = 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
2010-11-07 08:43:30 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables34 ( ) {
2010-12-16 20:16:59 +01:00
const char code [ ] = " void f() { \n "
" int x = 10; \n "
" do { cin >> x; } while (x > 5); \n "
" a[x] = 0; \n "
" } \n " ;
const char expected [ ] = " void f ( ) { \n "
" int x ; x = 10 ; \n "
" do { cin >> x ; } while ( 5 < x ) ; \n "
" a [ x ] = 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables35 ( ) {
2010-12-25 10:07:57 +01:00
// Ticket #2353
const char code [ ] = " int f() { "
" int x = 0; "
" if (x == 0) { "
" return 0; "
" } "
" return 10 / x; "
" } " ;
2012-01-30 19:04:35 +01:00
const char expected [ ] = " int f ( ) { int x ; x = 0 ; { return 0 ; } } " ;
2010-12-25 10:07:57 +01:00
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables36 ( ) {
2010-12-26 20:34:07 +01:00
// Ticket #2304
const char code [ ] = " void f() { "
" const char *q = \" hello \" ; "
" strcpy(p, q); "
" } " ;
const char expected [ ] = " void f ( ) { const char * q ; q = \" hello \" ; strcpy ( p , \" hello \" ) ; } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables37 ( ) {
2011-01-04 19:36:29 +01:00
// Ticket #2398 - no simplication in for loop
const char code [ ] = " void f() { \n "
" double x = 0; \n "
" for (int iter=0; iter<42; iter++) { \n "
" int EvaldF = 1; \n "
" if (EvaldF) \n "
" Eval (x); \n "
" } \n "
" } " ;
const char expected [ ] = " void f ( ) { \n "
" double x ; x = 0 ; \n "
" for ( int iter = 0 ; iter < 42 ; iter ++ ) { \n "
2011-12-02 23:01:55 +01:00
" \n "
" \n "
" Eval ( x ) ; \n "
2011-01-04 19:36:29 +01:00
" } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables38 ( ) {
2011-01-04 21:35:35 +01:00
// Ticket #2399 - simplify conditions
const char code [ ] = " void f() { \n "
" int x = 0; \n "
" int y = 1; \n "
" if (x || y); \n "
" } " ;
const char expected [ ] = " void f ( ) { \n "
" \n "
2011-06-30 21:04:26 +02:00
" \n "
2011-12-02 23:01:55 +01:00
" ; \n "
2011-01-04 21:35:35 +01:00
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables39 ( ) {
2011-01-11 19:34:35 +01:00
// Ticket #2296 - simplify pointer alias 'delete p;'
{
const char code [ ] = " void f() { \n "
" int *x; \n "
" int *y = x; \n "
" delete y; \n "
" } " ;
ASSERT_EQUALS ( " void f ( ) { \n int * x ; \n \n delete x ; \n } " , tokenizeAndStringify ( code , true ) ) ;
}
{
const char code [ ] = " void f() { \n "
" int *x; \n "
" int *y = x; \n "
" delete [] y; \n "
" } " ;
ASSERT_EQUALS ( " void f ( ) { \n int * x ; \n \n delete [ ] x ; \n } " , tokenizeAndStringify ( code , true ) ) ;
}
}
2011-02-01 21:46:07 +01:00
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables40 ( ) {
2011-02-01 21:46:07 +01:00
const char code [ ] = " void f() { \n "
" char c1 = 'a'; \n "
" char c2 = { c1 }; \n "
" } " ;
2011-12-02 23:01:55 +01:00
ASSERT_EQUALS ( " void f ( ) { \n \n char c2 ; c2 = { 'a' } ; \n } " , tokenizeAndStringify ( code , true ) ) ;
2011-02-01 21:46:07 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables41 ( ) {
2011-02-20 18:18:27 +01:00
const char code [ ] = " void f() { \n "
" int x = 0; \n "
" const int *p; p = &x; \n "
" if (p) { return 0; } \n "
" } " ;
ASSERT_EQUALS ( " void f ( ) { \n int x ; x = 0 ; \n const int * p ; p = & x ; \n if ( & x ) { return 0 ; } \n } " , tokenizeAndStringify ( code , true ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables42 ( ) {
2011-03-01 20:20:48 +01:00
{
const char code [ ] = " void f() { \n "
" char str1[10], str2[10]; \n "
" strcpy(str1, \" abc \" ); \n "
" strcpy(str2, str1); \n "
2011-02-26 21:11:56 +01:00
" } " ;
2011-03-01 20:20:48 +01:00
const char expected [ ] = " void f ( ) { \n "
" char str1 [ 10 ] ; char str2 [ 10 ] ; \n "
" strcpy ( str1 , \" abc \" ) ; \n "
" strcpy ( str2 , \" abc \" ) ; \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
{
const char code [ ] = " void f() { "
" char *s = malloc(10); "
" strcpy(s, \" \" ); "
" free(s); "
" } " ;
const char expected [ ] = " void f ( ) { "
" char * s ; s = malloc ( 10 ) ; "
" strcpy ( s , \" \" ) ; "
" free ( s ) ; "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2011-03-03 20:07:56 +01:00
{
const char code [ ] = " void f(char *p, char *q) { "
" strcpy(p, \" abc \" ); "
" q = p; "
" } " ;
const char expected [ ] = " void f ( char * p , char * q ) { "
" strcpy ( p , \" abc \" ) ; "
" q = p ; "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2012-02-01 21:13:26 +01:00
// 3538
{
const char code [ ] = " void f() { \n "
" char s[10]; \n "
" strcpy(s, \" 123 \" ); \n "
" if (s[6] == ' '); \n "
" } " ;
const char expected [ ] = " void f ( ) { \n "
" char s [ 10 ] ; \n "
" strcpy ( s , \" 123 \" ) ; \n "
" if ( s [ 6 ] == ' ' ) { ; } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2011-02-26 21:11:56 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariables43 ( ) {
2011-08-31 19:45:20 +02:00
{
const char code [ ] = " void f() { \n "
" int a, *p; p = &a; \n "
" { int a = *p; } \n "
2011-08-30 19:13:04 +02:00
" } " ;
2011-08-31 19:45:20 +02:00
const char expected [ ] = " void f ( ) { \n "
" int a ; int * p ; p = & a ; \n "
" { int a ; a = * p ; } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
{
const char code [ ] = " void f() { \n "
" int *a, **p; p = &a; \n "
" { int *a = *p; } \n "
" } " ;
const char expected [ ] = " void f ( ) { \n "
" int * a ; int * * p ; p = & a ; \n "
" { int * a ; a = * p ; } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2011-08-30 19:13:04 +02:00
}
2011-11-05 08:30:11 +01:00
void simplifyKnownVariables44 ( ) {
const char code [ ] = " void a() { \n "
" static int i = 10; \n "
" b(i++); \n "
" } " ;
const char expected [ ] = " void a ( ) { \n "
" static int i = 10 ; \n "
" b ( i ++ ) ; \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2011-11-20 21:50:26 +01:00
void simplifyKnownVariables45 ( ) {
const char code [ ] = " class Fred { \n "
" private: \n "
" const static int NUM = 2; \n "
" int array[NUM]; \n "
" } " ;
const char expected [ ] = " class Fred { \n "
" private: \n "
2011-12-02 22:35:03 +01:00
" \n "
2011-11-20 21:50:26 +01:00
" int array [ 2 ] ; \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2012-02-07 08:05:50 +01:00
void simplifyKnownVariables46 ( ) {
const char code [ ] = " void f() { \n "
" int x = 0; \n "
" cin >> x; \n "
" return x; \n "
" } " ;
{
const char expected [ ] = " void f ( ) { \n "
" int x ; x = 0 ; \n "
" cin >> x ; \n "
" return x ; \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true , true , Settings : : Unspecified , " test.cpp " ) ) ;
}
{
const char expected [ ] = " void f ( ) { \n "
" \n "
" cin >> 0 ; \n "
" return 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true , true , Settings : : Unspecified , " test.c " ) ) ;
}
}
2012-03-15 19:09:36 +01:00
void simplifyKnownVariables47 ( ) {
// #3621
const char code [ ] = " void f() { \n "
" int x = 0; \n "
" cin >> std::hex >> x; \n "
" } " ;
const char expected [ ] = " void f ( ) { \n "
" int x ; x = 0 ; \n "
" cin >> std :: hex >> x ; \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true , true , Settings : : Unspecified , " test.cpp " ) ) ;
}
2012-04-25 19:23:17 +02:00
void simplifyKnownVariables48 ( ) {
// #3754
const char code [ ] = " void f(int sz) { \n "
" int i; \n "
" for (i = 0; ((i<sz) && (sz>3)); ++i) { } \n "
" } " ;
const char expected [ ] = " void f ( int sz ) { \n "
" int i ; \n "
" for ( i = 0 ; ( i < sz ) && ( 3 < sz ) ; ++ i ) { } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true , true , Settings : : Unspecified , " test.c " ) ) ;
2012-05-07 21:11:23 +02:00
}
void simplifyKnownVariables49 ( ) { // #3691
const char code [ ] = " void f(int sz) { \n "
" switch (x) { \n "
" case 1: sz = 2; continue; \n "
" case 2: x = sz; break; \n "
" } \n "
" } " ;
const char expected [ ] = " void f ( int sz ) { \n "
" switch ( x ) { \n "
" case 1 : ; sz = 2 ; continue ; \n "
" case 2 : ; x = sz ; break ; \n "
" } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true , true , Settings : : Unspecified , " test.c " ) ) ;
2012-04-25 19:23:17 +02:00
}
2012-08-22 16:09:19 +02:00
void simplifyKnownVariables50 ( ) { // #4066
{
const char code [ ] = " void f() { \n "
" char str1[10], str2[10]; \n "
" sprintf(str1, \" %% \" ); \n "
" strcpy(str2, str1); \n "
" } " ;
const char expected [ ] = " void f ( ) { \n "
" char str1 [ 10 ] ; char str2 [ 10 ] ; \n "
" sprintf ( str1 , \" %% \" ) ; \n "
" strcpy ( str2 , \" % \" ) ; \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
{
const char code [ ] = " void f() { \n "
" char str1[25], str2[25]; \n "
" sprintf(str1, \" abcdef%%%% and %% and % \" ); \n "
" strcpy(str2, str1); \n "
" } " ;
const char expected [ ] = " void f ( ) { \n "
" char str1 [ 25 ] ; char str2 [ 25 ] ; \n "
" sprintf ( str1 , \" abcdef%%%% and %% and % \" ) ; \n "
" strcpy ( str2 , \" abcdef%% and % and % \" ) ; \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
{
const char code [ ] = " void f() { \n "
" char str1[10], str2[10]; \n "
" sprintf(str1, \" abc \" ); \n "
" strcpy(str2, str1); \n "
" } " ;
const char expected [ ] = " void f ( ) { \n "
" char str1 [ 10 ] ; char str2 [ 10 ] ; \n "
" sprintf ( str1 , \" abc \" ) ; \n "
" strcpy ( str2 , \" abc \" ) ; \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
}
2012-04-22 12:22:49 +02:00
void simplifyKnownVariablesIfEq1 ( ) {
2012-03-12 17:32:30 +01:00
const char code [ ] = " void f(int x) { \n "
" if (x==5) { \n "
" return x; \n "
" } \n "
" } " ;
const char expected [ ] = " void f ( int x ) { \n "
" if ( x == 5 ) { \n "
" return 5 ; \n "
" } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true , true , Settings : : Unspecified , " test.c " ) ) ;
}
2012-04-22 12:22:49 +02:00
void simplifyKnownVariablesIfEq2 ( ) {
const char code [ ] = " void f(int x) { \n "
" if (x==5) { \n "
" buf[x++] = 0; \n "
" } \n "
" } " ;
const char expected [ ] = " void f ( int x ) { \n "
" if ( x == 5 ) { \n "
" buf [ x ++ ] = 0 ; \n "
" } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true , true , Settings : : Unspecified , " test.c " ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariablesBailOutAssign1 ( ) {
2010-11-07 11:07:56 +01:00
const char code [ ] = " int foo() { \n "
" int i; i = 0; \n "
" if (x) { i = 10; } \n "
" return i; \n "
" } \n " ;
const char expected [ ] = " int foo ( ) { \n "
" int i ; i = 0 ; \n "
" if ( x ) { i = 10 ; } \n "
" return i ; \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariablesBailOutAssign2 ( ) {
2011-08-21 17:49:00 +02:00
// ticket #3032 - assignment in condition
const char code [ ] = " void f(struct ABC *list) { \n "
" struct ABC *last = NULL; \n "
" nr = (last = list->prev)->nr; \n " // <- don't replace "last" with 0
" } \n " ;
const char expected [ ] = " void f ( struct ABC * list ) { \n "
" struct ABC * last ; last = 0 ; \n "
" nr = ( last = list . prev ) . nr ; \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariablesBailOutFor1 ( ) {
2010-11-07 08:10:09 +01:00
const char code [ ] = " void foo() { \n "
" for (int i = 0; i < 10; ++i) { } \n "
" } \n " ;
const char expected [ ] = " void foo ( ) { \n "
" for ( int i = 0 ; i < 10 ; ++ i ) { } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
2012-09-17 13:18:19 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ; // debug warnings
2010-11-07 08:10:09 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariablesBailOutFor2 ( ) {
2010-11-07 10:42:08 +01:00
const char code [ ] = " void foo() { \n "
" int i = 0; \n "
" while (i < 10) { ++i; } \n "
" } \n " ;
const char expected [ ] = " void foo ( ) { \n "
" int i ; i = 0 ; \n "
" while ( i < 10 ) { ++ i ; } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
2012-09-17 13:18:19 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ; // debug warnings
2010-11-07 10:42:08 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariablesBailOutFor3 ( ) {
2010-11-13 16:40:04 +01:00
const char code [ ] = " void foo() { \n "
" for (std::string::size_type pos = 0; pos < 10; ++pos) \n "
" { } \n "
" } \n " ;
const char expected [ ] = " void foo ( ) { \n "
" for ( std :: string :: size_type pos = 0 ; pos < 10 ; ++ pos ) \n "
" { } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
2012-09-17 13:18:19 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ; // debug warnings
2010-11-13 16:40:04 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariablesBailOutMemberFunction ( ) {
2010-11-06 19:42:38 +01:00
const char code [ ] = " void foo(obj a) { \n "
" obj b = a; \n "
" b.f(); \n "
" } \n " ;
const char expected [ ] = " void foo ( obj a ) { \n "
" obj b ; b = a ; \n "
" b . f ( ) ; \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariablesBailOutConditionalIncrement ( ) {
2010-11-21 13:40:42 +01:00
const char code [ ] = " int f() { \n "
" int a = 0; \n "
" if (x) { \n "
2012-09-05 19:46:44 +02:00
" ++a; \n " // conditional increment
2010-11-21 13:40:42 +01:00
" } \n "
" return a; \n "
" } \n " ;
tokenizeAndStringify ( code , true ) ;
2012-09-17 13:18:19 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ; // no debug warnings
2010-11-21 13:40:42 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariablesBailOutSwitchBreak ( ) {
2010-12-18 11:14:31 +01:00
// Ticket #2324
const char code [ ] = " int f(char *x) { \n "
" char *p; \n "
" char *q; \n "
" \n "
" switch (x & 0x3) \n "
" { \n "
" case 1: \n "
" p = x; \n "
" x = p; \n "
" break; \n "
" case 2: \n "
2012-09-05 19:46:44 +02:00
" q = x; \n " // x is not equal with p
2010-12-18 11:14:31 +01:00
" x = q; \n "
" break; \n "
" } \n "
" } \n " ;
const char expected [ ] = " int f ( char * x ) { \n "
" char * p ; \n "
" char * q ; \n "
" \n "
" switch ( x & 3 ) \n "
" { \n "
" case 1 : ; \n "
" p = x ; \n "
" x = p ; \n "
" break ; \n "
" case 2 : ; \n "
" q = x ; \n "
" x = q ; \n "
" break ; \n "
" } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariablesFloat ( ) {
2011-01-15 08:48:42 +01:00
// Ticket #2454
const char code [ ] = " void f() { \n "
" float a = 40; \n "
" x(10 / a); \n "
" } \n " ;
2011-12-02 23:01:55 +01:00
const char expected [ ] = " void f ( ) { \n \n x ( 0.25 ) ; \n } " ;
2011-01-15 08:48:42 +01:00
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2012-06-24 13:39:14 +02:00
void simplifyKnownVariablesFunctionCalls ( ) {
{
const char code [ ] = " void a(int x); " // <- x is passed by value
" void b() { "
" int x = 123; "
2012-09-17 13:18:19 +02:00
" a(x); " // <- replace with a(123);
2012-06-24 13:39:14 +02:00
" } " ;
const char expected [ ] = " void a ( int x ) ; void b ( ) { a ( 123 ) ; } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
{
2012-09-17 13:18:19 +02:00
const char code [ ] = " void a(int &x); " // <- x is passed by reference
2012-06-24 13:39:14 +02:00
" void b() { "
" int x = 123; "
2012-09-17 13:18:19 +02:00
" a(x); " // <- don't replace with a(123);
2012-06-24 13:39:14 +02:00
" } " ;
const char expected [ ] = " void a ( int & x ) ; void b ( ) { int x ; x = 123 ; a ( x ) ; } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
}
2011-10-13 20:53:06 +02:00
void simplifyKnownVariablesClassMember ( ) {
2011-06-23 16:58:01 +02:00
// Ticket #2815
2011-06-23 17:03:14 +02:00
{
const char code [ ] = " char *a; \n "
" void f(const char *s) { \n "
" a = NULL; \n "
" x(); \n "
" memcpy(a, s, 10); \n " // <- don't simplify "a" here
" } \n " ;
const std : : string s ( tokenizeAndStringify ( code , true ) ) ;
ASSERT_EQUALS ( true , s . find ( " memcpy ( a , s , 10 ) ; " ) ! = std : : string : : npos ) ;
}
2011-06-23 16:58:01 +02:00
2011-06-23 17:03:14 +02:00
// If the variable is local then perform simplification..
{
const char code [ ] = " void f(const char *s) { \n "
" char *a = NULL; \n "
" x(); \n "
" memcpy(a, s, 10); \n " // <- simplify "a"
" } \n " ;
const std : : string s ( tokenizeAndStringify ( code , true ) ) ;
TODO_ASSERT_EQUALS ( true , false , s . find ( " memcpy ( 0 , s , 10 ) ; " ) ! = std : : string : : npos ) ;
}
2011-06-23 16:58:01 +02:00
}
2012-04-16 16:25:04 +02:00
void simplifyExternC ( ) {
ASSERT_EQUALS ( " int foo ( ) ; " , tokenizeAndStringify ( " extern \" C \" int foo(); " ) ) ;
ASSERT_EQUALS ( " int foo ( ) ; " , tokenizeAndStringify ( " extern \" C \" { int foo(); } " ) ) ;
}
2011-01-15 08:48:42 +01:00
2012-04-15 12:49:53 +02:00
std : : string tokenizeDebugListing ( const std : : string & code , bool simplify = false , const char filename [ ] = " test.cpp " ) {
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
Settings settings ;
2012-08-25 21:57:45 +02:00
settings . standards . c = Standards : : C89 ;
settings . standards . cpp = Standards : : CPP03 ;
2010-12-01 18:00:55 +01:00
Tokenizer tokenizer ( & settings , this ) ;
2010-02-25 21:00:39 +01:00
std : : istringstream istr ( code ) ;
2012-04-15 12:49:53 +02:00
tokenizer . tokenize ( istr , filename ) ;
2010-02-25 21:00:39 +01:00
2010-04-02 07:30:58 +02:00
if ( simplify )
2010-02-25 21:00:39 +01:00
tokenizer . simplifyTokenList ( ) ;
// result..
return tokenizer . tokens ( ) - > stringifyList ( true ) ;
}
2011-10-13 20:53:06 +02:00
void varid1 ( ) {
2009-09-30 13:40:10 +02:00
{
2010-02-25 21:00:39 +01:00
const std : : string actual = tokenizeDebugListing (
" 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 "
2012-04-15 12:49:53 +02:00
" } \n " , false , " test.c " ) ;
2009-09-30 13:40:10 +02:00
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
{
2010-02-25 21:00:39 +01:00
const std : : string actual = tokenizeDebugListing (
" 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 "
2012-04-15 12:49:53 +02:00
" } \n " , false , " test.c " ) ;
2009-09-30 13:40:10 +02:00
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
}
2011-10-13 20:53:06 +02:00
void varid2 ( ) {
2010-02-25 21:00:39 +01:00
const std : : string actual = tokenizeDebugListing (
" void f() \n "
" { \n "
" struct ABC abc; \n "
" abc.a = 3; \n "
" i = abc.a; \n "
2012-04-15 13:42:13 +02:00
" } \n " , false , " test.c " ) ;
2008-12-18 22:28:57 +01:00
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
2011-10-13 20:53:06 +02:00
void varid3 ( ) {
2010-02-25 21:00:39 +01:00
const std : : string actual = tokenizeDebugListing (
" static char str[4]; \n "
" void f() \n "
" { \n "
" char str[10]; \n "
" str[0] = 0; \n "
2012-04-15 12:49:53 +02:00
" } \n " , false , " test.c " ) ;
2009-02-02 18:27:34 +01:00
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 ) ;
}
2011-10-13 20:53:06 +02:00
void varid4 ( ) {
2010-02-25 21:00:39 +01:00
const std : : string actual = tokenizeDebugListing (
" void f(const unsigned int a[]) \n "
" { \n "
" int i = *(a+10); \n "
2012-04-15 12:49:53 +02:00
" } \n " , false , " test.c " ) ;
2009-02-13 17:23:02 +01:00
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 "
2012-04-15 12:49:53 +02:00
" 3: int i@2 ; i@2 = * ( a@1 + 10 ) ; \n "
2009-02-13 17:23:02 +01:00
" 4: } \n " ) ;
2009-02-13 18:25:23 +01:00
ASSERT_EQUALS ( expected , actual ) ;
}
2011-10-13 20:53:06 +02:00
void varid5 ( ) {
2010-02-25 21:00:39 +01:00
const std : : string actual = tokenizeDebugListing (
" void f() \n "
" { \n "
" int a,b; \n "
2012-04-15 12:49:53 +02:00
" } \n " , false , " test.c " ) ;
2009-02-13 18:25:23 +01:00
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 "
2012-04-15 12:49:53 +02:00
" 3: int a@1 ; int b@2 ; \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
2011-10-13 20:53:06 +02:00
void varid6 ( ) {
2010-02-25 21:00:39 +01:00
const std : : string actual = tokenizeDebugListing (
" int f(int a, int b) \n "
" { \n "
" return a+b; \n "
2012-04-15 12:49:53 +02:00
" } \n " , false , " test.c " ) ;
2009-02-16 21:46:24 +01:00
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
2011-10-13 20:53:06 +02:00
void varid7 ( ) {
2010-02-25 21:00:39 +01:00
const std : : string actual = tokenizeDebugListing (
2012-04-15 12:49:53 +02:00
" void func() { \n "
" char a[256] = \" test \" ; \n "
" { \n "
" char b[256] = \" test \" ; \n "
" } \n "
" } \n " , false , " test.c " ) ;
2009-03-01 17:37:02 +01:00
2009-03-03 21:17:23 +01:00
const std : : string expected ( " \n \n ##file 0 \n "
2012-04-15 12:49:53 +02:00
" 1: void func ( ) { \n "
" 2: char a@1 [ 256 ] = \" test \" ; \n "
" 3: { \n "
" 4: char b@2 [ 256 ] = \" test \" ; \n "
" 5: } \n "
" 6: } \n " ) ;
2009-03-01 17:37:02 +01:00
ASSERT_EQUALS ( expected , actual ) ;
}
2011-10-13 20:53:06 +02:00
void varidReturn1 ( ) {
2010-02-25 21:00:39 +01:00
const std : : string actual = tokenizeDebugListing (
" int f() \n "
" { \n "
" int a; \n "
" return a; \n "
2012-04-15 12:49:53 +02:00
" } \n " , false , " test.c " ) ;
2009-02-28 21:21:48 +01:00
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 ) ;
}
2011-10-13 20:53:06 +02:00
void varidReturn2 ( ) {
2010-05-06 18:40:52 +02:00
const std : : string actual = tokenizeDebugListing (
" void foo() \n "
" { \n "
" unsigned long mask = (1UL << size_) - 1; \n "
" return (abits_val_ & mask); \n "
2012-04-15 14:19:17 +02:00
" } \n " , false , " test.c " ) ;
2010-05-06 18:40:52 +02:00
const std : : string expected ( " \n \n ##file 0 \n "
" 1: void foo ( ) \n "
" 2: { \n "
" 3: long mask@1 ; mask@1 = ( 1UL << size_ ) - 1 ; \n "
" 4: return ( abits_val_ & mask@1 ) ; \n "
" 5: } \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
2011-10-13 20:53:06 +02:00
void varid8 ( ) {
2010-02-25 21:00:39 +01:00
const std : : string actual = tokenizeDebugListing (
" void func() \n "
" { \n "
" std::string str( \" test \" ); \n "
" str.clear(); \n "
" } \n " ) ;
2009-03-25 18:10:03 +01:00
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
2011-10-13 20:53:06 +02:00
void varid9 ( ) {
2010-02-25 21:00:39 +01:00
const std : : string actual = tokenizeDebugListing (
2012-04-15 12:49:53 +02:00
" typedef int INT32; \n " , false , " test.c " ) ;
2009-03-28 21:07:33 +01:00
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
}
2011-10-13 20:53:06 +02:00
void varid10 ( ) {
2010-02-25 21:00:39 +01:00
const std : : string actual = tokenizeDebugListing (
" void foo() \n "
" { \n "
" int abc; \n "
" struct abc abc1; \n "
2012-04-15 12:49:53 +02:00
" } " , false , " test.c " ) ;
2009-05-31 18:46:32 +02:00
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 ) ;
}
2011-10-13 20:53:06 +02:00
void varid11 ( ) {
2010-02-25 21:00:39 +01:00
const std : : string actual = tokenizeDebugListing (
" class Foo; \n " ) ;
2009-08-11 20:58:49 +02:00
const std : : string expected ( " \n \n ##file 0 \n "
" 1: class Foo ; \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
2011-10-13 20:53:06 +02:00
void varid12 ( ) {
2010-02-25 21:00:39 +01:00
const std : : string actual = tokenizeDebugListing (
" static void a() \n "
" { \n "
" class Foo *foo; \n "
" } \n " ) ;
2009-09-22 17:49:13 +02:00
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 ) ;
}
2011-10-13 20:53:06 +02:00
void varid13 ( ) {
2010-02-25 21:00:39 +01:00
const std : : string actual = tokenizeDebugListing (
" void f() \n "
" { \n "
" int a; int b; \n "
" a = a; \n "
2012-04-15 12:49:53 +02:00
" } \n " , false , " test.c " ) ;
2009-12-30 20:15:44 +01:00
const std : : string expected ( " \n \n ##file 0 \n "
" 1: void f ( ) \n "
" 2: { \n "
2012-04-15 12:49:53 +02:00
" 3: int a@1 ; int b@2 ; \n "
2009-12-30 20:15:44 +01:00
" 4: a@1 = a@1 ; \n "
" 5: } \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
2011-10-13 20:53:06 +02:00
void varid14 ( ) {
2010-02-05 23:24:57 +01:00
// Overloaded operator*
2010-02-25 21:00:39 +01:00
const std : : string actual = tokenizeDebugListing (
" void foo() \n "
" { \n "
" A a; \n "
" B b; \n "
" b * a; \n "
2012-04-15 12:49:53 +02:00
" } " , false , " test.c " ) ;
2010-02-05 23:24:57 +01:00
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
2010-02-07 13:34:39 +01:00
ASSERT_EQUALS ( expected , actual ) ;
2010-02-05 23:24:57 +01:00
}
2011-10-13 20:53:06 +02:00
void varid15 ( ) {
2010-03-19 16:17:25 +01:00
{
const std : : string actual = tokenizeDebugListing (
" struct S { \n "
" struct T { \n "
" } t; \n "
2012-04-15 12:49:53 +02:00
" } s; " , false , " test.c " ) ;
2010-03-18 18:14:52 +01:00
2010-03-19 16:17:25 +01:00
const std : : string expected ( " \n \n ##file 0 \n "
" 1: struct S { \n "
" 2: struct T { \n "
" 3: } ; T t@1 ; \n "
" 4: } ; S s@2 ; \n " ) ;
2010-03-18 18:14:52 +01:00
2010-03-19 16:17:25 +01:00
ASSERT_EQUALS ( expected , actual ) ;
}
{
const std : : string actual = tokenizeDebugListing (
" struct S { \n "
" struct T { \n "
" } t; \n "
2012-04-15 12:49:53 +02:00
" }; " , false , " test.c " ) ;
2010-03-19 16:17:25 +01:00
const std : : string expected ( " \n \n ##file 0 \n "
" 1: struct S { \n "
" 2: struct T { \n "
" 3: } ; T t@1 ; \n "
" 4: } ; \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
2010-03-18 18:14:52 +01:00
}
2011-10-13 20:53:06 +02:00
void varid16 ( ) {
2010-05-01 10:09:53 +02:00
const std : : string code ( " void foo() \n "
" { \n "
" int x = 1; \n "
" y = (z * x); \n "
" } \n " ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: void foo ( ) \n "
" 2: { \n "
" 3: int x@1 ; x@1 = 1 ; \n "
2010-12-18 09:44:58 +01:00
" 4: y = z * x@1 ; \n "
2010-05-01 10:09:53 +02:00
" 5: } \n " ) ;
2012-04-15 12:49:53 +02:00
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , false , " test.c " ) ) ;
2010-05-01 10:09:53 +02:00
}
2011-10-13 20:53:06 +02:00
void varid17 ( ) { // ticket #1810
2010-06-22 17:07:41 +02:00
const std : : string code ( " char foo() \n "
" { \n "
" char c('c'); \n "
" return c; \n "
" } \n " ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: char foo ( ) \n "
" 2: { \n "
" 3: char c@1 ( 'c' ) ; \n "
" 4: return c@1 ; \n "
" 5: } \n " ) ;
2012-04-15 12:49:53 +02:00
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , false , " test.c " ) ) ;
2010-06-22 17:07:41 +02:00
}
2011-10-13 20:53:06 +02:00
void varid18 ( ) {
2010-08-10 18:22:58 +02:00
const std : : string code ( " char foo(char c) \n "
" { \n "
" bar::c = c; \n "
" } \n " ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: char foo ( char c@1 ) \n "
" 2: { \n "
" 3: bar :: c = c@1 ; \n "
" 4: } \n " ) ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void varid19 ( ) {
2010-08-14 20:50:49 +02:00
const std : : string code ( " void foo() \n "
" { \n "
" std::pair<std::vector<double>, int> x; \n "
" } \n " ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: void foo ( ) \n "
" 2: { \n "
" 3: std :: pair < std :: vector < double > , int > x@1 ; \n "
" 4: } \n " ) ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void varid20 ( ) {
2010-08-15 11:54:28 +02:00
const std : : string code ( " void foo() \n "
" { \n "
" pair<vector<int>, vector<double> > x; \n "
" } \n " ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: void foo ( ) \n "
" 2: { \n "
" 3: pair < vector < int > , vector < double > > x@1 ; \n "
" 4: } \n " ) ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void varid24 ( ) {
2010-08-18 22:42:04 +02:00
const std : : string code ( " class foo() \n "
" { \n "
" public: \n "
" ; \n "
" private: \n "
" static int i; \n "
" }; \n " ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: class foo ( ) \n "
" 2: { \n "
" 3: public: \n "
" 4: ; \n "
" 5: private: \n "
" 6: static int i@1 ; \n "
" 7: } ; \n " ) ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void varid25 ( ) {
2010-08-18 22:42:04 +02:00
const std : : string code ( " class foo() \n "
" { \n "
" public: \n "
" ; \n "
" private: \n "
" mutable int i; \n "
" }; \n " ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: class foo ( ) \n "
" 2: { \n "
" 3: public: \n "
" 4: ; \n "
" 5: private: \n "
" 6: mutable int i@1 ; \n "
" 7: } ; \n " ) ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void varid26 ( ) {
2010-08-23 20:15:02 +02:00
const std : : string code ( " list<int (*)()> functions; \n " ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: list < int ( * ) ( ) > functions@1 ; \n " ) ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void varid27 ( ) {
2010-12-06 17:52:44 +01:00
const std : : string code ( " int fooled_ya; \n "
" fooled_ya::iterator iter; \n " ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: int fooled_ya@1 ; \n "
" 2: fooled_ya :: iterator iter@2 ; \n " ) ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void varid28 ( ) { // ticket #2630 (segmentation fault)
2011-03-19 01:44:58 +01:00
tokenizeDebugListing ( " template <typedef A> \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void varid29 ( ) {
2011-03-22 01:57:17 +01:00
const std : : string code ( " class A { \n "
" B<C<1>,1> b; \n "
" }; \n " ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: class A { \n "
" 2: B < C < 1 > , 1 > b@1 ; \n "
" 3: } ; \n " ) ;
2011-04-19 00:53:11 +02:00
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code ) ) ;
2011-04-18 13:45:38 +02:00
}
2011-10-13 20:53:06 +02:00
void varid30 ( ) { // ticket #2614
2011-04-18 13:45:38 +02:00
const std : : string code1 ( " void f(EventPtr *eventP, ActionPtr **actionsP) \n "
" { \n "
" EventPtr event = *eventP; \n "
" *actionsP = &event->actions; \n "
" } \n " ) ;
const std : : string expected1 ( " \n \n ##file 0 \n "
" 1: void f ( EventPtr * eventP@1 , ActionPtr * * actionsP@2 ) \n "
" 2: { \n "
" 3: EventPtr event@3 ; event@3 = * eventP@1 ; \n "
" 4: * actionsP@2 = & event@3 . actions@4 ; \n "
" 5: } \n " ) ;
2012-04-15 13:42:13 +02:00
ASSERT_EQUALS ( expected1 , tokenizeDebugListing ( code1 , false , " test.c " ) ) ;
2011-04-18 13:45:38 +02:00
const std : : string code2 ( " void f(int b, int c) { \n "
" x(a*b*c,10); \n "
" } \n " ) ;
const std : : string expected2 ( " \n \n ##file 0 \n "
" 1: void f ( int b@1 , int c@2 ) { \n "
2011-10-29 21:25:58 +02:00
" 2: x ( a * b@1 * c@2 , 10 ) ; \n "
2011-04-18 13:45:38 +02:00
" 3: } \n " ) ;
2012-04-15 12:49:53 +02:00
ASSERT_EQUALS ( expected2 , tokenizeDebugListing ( code2 , false , " test.c " ) ) ;
2011-04-23 03:23:40 +02:00
const std : : string code3 ( " class Nullpointer : public ExecutionPath \n "
" { \n "
" Nullpointer(Check *c, const unsigned int id, const std::string &name) \n "
" : ExecutionPath(c, id) \n "
" { \n "
" } \n "
" } \n " ) ;
const std : : string expected3 ( " \n \n ##file 0 \n "
" 1: class Nullpointer : public ExecutionPath \n "
" 2: { \n "
" 3: Nullpointer ( Check * c@1 , const int id@2 , const std :: string & name@3 ) \n "
" 4: : ExecutionPath ( c@1 , id@2 ) \n "
" 5: { \n "
" 6: } \n "
" 7: } \n " ) ;
ASSERT_EQUALS ( expected3 , tokenizeDebugListing ( code3 ) ) ;
2011-03-22 01:57:17 +01:00
}
2011-10-13 20:53:06 +02:00
void varid31 ( ) { // ticket #2831 (segmentation fault)
2011-06-12 14:38:15 +02:00
const std : : string code ( " z<y<x> " ) ;
2011-12-18 17:54:24 +01:00
tokenizeDebugListing ( code ) ;
2011-06-12 14:38:15 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void varid32 ( ) { // ticket #2835 (segmentation fault)
2011-06-12 18:09:52 +02:00
const std : : string code ( " ><,f<i, " ) ;
2011-12-18 17:54:24 +01:00
tokenizeDebugListing ( code ) ;
2011-06-12 18:09:52 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void varid33 ( ) { // ticket #2875 (segmentation fault)
2011-06-29 00:47:18 +02:00
const std : : string code ( " 0; (a) < (a) " ) ;
2012-04-26 10:35:40 +02:00
tokenizeDebugListing ( code , true ) ;
2011-06-29 00:47:18 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void varid34 ( ) { // ticket #2825
2011-06-30 02:04:04 +02:00
const std : : string code ( " class Fred : public B1, public B2 \n "
" { \n "
" public: \n "
" Fred() { a = 0; } \n "
" private: \n "
" int a; \n "
" }; \n " ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: class Fred : public B1 , public B2 \n "
" 2: { \n "
" 3: public: \n "
" 4: Fred ( ) { a@1 = 0 ; } \n "
" 5: private: \n "
" 6: int a@1 ; \n "
" 7: } ; \n " ) ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void varid35 ( ) { // ticket #2937
2011-08-01 12:45:06 +02:00
const std : : string code ( " int foo() { \n "
" int f(x); \n "
" return f; \n "
" } \n " ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: int foo ( ) { \n "
" 2: int f@1 ( x ) ; \n "
" 3: return f@1 ; \n "
" 4: } \n " ) ;
2012-04-22 11:36:31 +02:00
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code ) ) ;
2011-08-01 12:45:06 +02:00
}
2011-10-13 20:53:06 +02:00
void varid36 ( ) { // ticket #2980 (segmentation fault)
2011-08-08 12:58:55 +02:00
const std : : string code ( " #elif A \n "
2012-04-26 10:58:35 +02:00
" A,a<b<x0 \n " ) ;
2011-12-18 17:54:24 +01:00
tokenizeDebugListing ( code ) ;
2011-08-08 12:58:55 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-29 21:25:58 +02:00
void varid37 ( ) {
2011-12-31 21:43:06 +01:00
{
const std : : string code = " void blah() { "
" Bar bar(*x); "
" } " ;
ASSERT_EQUALS ( " \n \n ##file 0 \n 1: "
" void blah ( ) { Bar bar@1 ( * x ) ; } \n " ,
tokenizeDebugListing ( code ) ) ;
}
{
const std : : string code = " void blah() { "
" Bar bar(&x); "
" } " ;
ASSERT_EQUALS ( " \n \n ##file 0 \n 1: "
" void blah ( ) { Bar bar@1 ( & x ) ; } \n " ,
tokenizeDebugListing ( code ) ) ;
}
2011-10-29 21:25:58 +02:00
}
2011-11-01 18:03:32 +01:00
void varid38 ( ) {
const std : : string code = " FOO class C; \n " ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: FOO class C ; \n " ,
tokenizeDebugListing ( code ) ) ;
}
2011-10-29 21:25:58 +02:00
2011-11-02 18:31:13 +01:00
void varid39 ( ) {
2011-11-02 20:42:38 +01:00
// const..
{
const std : : string code = " void f(FOO::BAR const); \n " ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: void f ( FOO :: BAR const ) ; \n " ,
tokenizeDebugListing ( code ) ) ;
}
{
const std : : string code = " static int const SZ = 22; \n " ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
2012-09-07 11:41:41 +02:00
" 1: static const int SZ@1 = 22 ; \n " ,
2012-04-15 14:30:46 +02:00
tokenizeDebugListing ( code , false , " test.c " ) ) ;
2011-11-02 20:42:38 +01:00
}
2011-11-02 18:31:13 +01:00
}
2011-11-09 18:49:17 +01:00
void varid40 ( ) {
const std : : string code ( " extern \" C \" int (*a())(); " ) ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
2012-04-16 16:25:04 +02:00
" 1: int ( * a ( ) ) ( ) ; \n " ,
2011-11-09 18:49:17 +01:00
tokenizeDebugListing ( code ) ) ;
}
2011-11-20 08:13:28 +01:00
void varid41 ( ) {
const std : : string code1 ( " union evt; void f(const evt & event); " ) ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: union evt ; void f ( const evt & event@1 ) ; \n " ,
2012-04-15 13:42:13 +02:00
tokenizeDebugListing ( code1 , false , " test.c " ) ) ;
2011-11-20 08:13:28 +01:00
const std : : string code2 ( " struct evt; void f(const evt & event); " ) ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: struct evt ; void f ( const evt & event@1 ) ; \n " ,
2012-04-15 13:42:13 +02:00
tokenizeDebugListing ( code2 , false , " test.c " ) ) ;
2011-11-20 08:13:28 +01:00
}
2011-11-20 16:50:41 +01:00
2011-11-20 16:22:21 +01:00
void varid42 ( ) {
const std : : string code ( " namespace fruit { struct banana {}; }; \n "
" class Fred { \n "
" public: \n "
" struct fruit::banana Bananas[25]; \n "
" }; " ) ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: namespace fruit { struct banana { } ; } ; \n "
" 2: class Fred { \n "
" 3: public: \n "
" 4: struct fruit :: banana Bananas@1 [ 25 ] ; \n "
" 5: } ; \n " ,
tokenizeDebugListing ( code ) ) ;
}
2011-11-20 08:13:28 +01:00
2012-03-20 19:00:16 +01:00
void varid43 ( ) {
const std : : string code ( " int main(int flag) { if(a & flag) { return 1; } } " ) ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: int main ( int flag@1 ) { if ( a & flag@1 ) { return 1 ; } } \n " ,
2012-04-15 14:30:46 +02:00
tokenizeDebugListing ( code , false , " test.c " ) ) ;
2012-03-20 19:00:16 +01:00
}
2011-11-21 21:18:56 +01:00
void varid44 ( ) {
const std : : string code ( " class A:public B,public C,public D {}; " ) ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: class A : public B , public C , public D { } ; \n " ,
tokenizeDebugListing ( code ) ) ;
2012-04-25 21:14:25 +02:00
}
void varid45 ( ) { // #3466
const std : : string code ( " void foo() { B b(this); A a(this, b); } " ) ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: void foo ( ) { B b@1 ( this ) ; A a@2 ( this , b@1 ) ; } \n " ,
tokenizeDebugListing ( code ) ) ;
2011-11-21 21:18:56 +01:00
}
2012-05-03 19:10:51 +02:00
void varid46 ( ) { // #3756
const std : : string code ( " void foo() { int t; x = (struct t *)malloc(); f(t); } " ) ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: void foo ( ) { int t@1 ; x = ( struct t * ) malloc ( ) ; f ( t@1 ) ; } \n " ,
tokenizeDebugListing ( code , false , " test.c " ) ) ;
}
2012-05-05 09:59:43 +02:00
void varid47 ( ) { // #3768
const std : : string code ( " void f(std::string &string, std::string &len) {} " ) ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: void f ( std :: string & string@1 , std :: string & len@2 ) { } \n " ,
tokenizeDebugListing ( code , false , " test.cpp " ) ) ;
}
2012-05-08 21:04:54 +02:00
void varid48 ( ) { // #3785 - return (a*b)
const std : : string code ( " int X::f(int b) const { return(a*b); } " ) ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: int X :: f ( int b@1 ) const { return ( a * b@1 ) ; } \n " ,
tokenizeDebugListing ( code , false , " test.c " ) ) ;
}
2012-05-13 07:55:35 +02:00
void varid49 ( ) { // #3799 - void f(std::vector<int>)
const std : : string code ( " void f(std::vector<int>) " ) ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: void f ( std :: vector < int > ) \n " ,
tokenizeDebugListing ( code , false , " test.cpp " ) ) ;
}
2012-05-16 10:59:52 +02:00
void varid50 ( ) { // #3760 - explicit
const std : : string code ( " class A { explicit A(const A&); }; " ) ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: class A { explicit A ( const A & ) ; } ; \n " ,
tokenizeDebugListing ( code , false , " test.cpp " ) ) ;
}
2012-05-22 18:58:13 +02:00
void varid51 ( ) { // don't set varid on template function
const std : : string code ( " T t; t.x<0>(); " ) ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: T t@1 ; t@1 . x < 0 > ( ) ; \n " ,
tokenizeDebugListing ( code , false , " test.cpp " ) ) ;
}
2012-08-26 09:49:38 +02:00
void varid52 ( ) {
const std : : string code ( " A<B<C>::D> e; \n "
" B< C<> > b[10]; \n "
" B<C<>> c[10]; " ) ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: A < B < C > :: D > e@1 ; \n "
" 2: B < C < > > b@2 [ 10 ] ; \n "
" 3: B < C < > > c@3 [ 10 ] ; \n " ,
tokenizeDebugListing ( code , false , " test.cpp " ) ) ;
}
2012-04-15 18:23:12 +02:00
void varid_cpp_keywords_in_c_code ( ) {
const char code [ ] = " void f() { \n "
" delete d; \n "
" throw t; \n "
" } " ;
const char expected [ ] = " \n \n ##file 0 \n "
" 1: void f ( ) { \n "
" 2: delete d@1 ; \n "
" 3: throw t@2 ; \n "
" 4: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , false , " test.c " ) ) ;
}
2011-10-13 20:53:06 +02:00
void varidFunctionCall1 ( ) {
2010-11-03 19:30:00 +01:00
const std : : string code ( " void f() { \n "
" int x; \n "
" x = a(y*x,10); \n "
" } " ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: void f ( ) { \n "
" 2: int x@1 ; \n "
" 3: x@1 = a ( y * x@1 , 10 ) ; \n "
" 4: } \n " ) ;
2012-04-15 14:30:46 +02:00
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , false , " test.c " ) ) ;
2010-11-03 19:30:00 +01:00
}
2011-10-13 20:53:06 +02:00
void varidFunctionCall2 ( ) {
2011-01-22 09:27:16 +01:00
// #2491
2010-11-03 20:13:34 +01:00
const std : : string code ( " void f(int b) { \n "
" x(a*b,10); \n "
" } " ) ;
const std : : string expected1 ( " \n \n ##file 0 \n "
" 1: void f ( int b@1 ) { \n "
" 2: x ( a * b " ) ;
const std : : string expected2 ( " , 10 ) ; \n "
" 3: } \n " ) ;
2012-04-15 17:44:51 +02:00
ASSERT_EQUALS ( expected1 + " @1 " + expected2 , tokenizeDebugListing ( code , false , " test.c " ) ) ;
2010-11-03 20:13:34 +01:00
}
2011-10-13 20:53:06 +02:00
void varidFunctionCall3 ( ) {
2010-12-25 12:40:44 +01:00
// Ticket #2339
const std : : string code ( " void f() { \n "
" int a = 0; \n "
" int b = c - (foo::bar * a); \n "
" } " ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: void f ( ) { \n "
" 2: int a@1 ; a@1 = 0 ; \n "
" 3: int b@2 ; b@2 = c - ( foo :: bar * a@1 ) ; \n "
" 4: } \n " ) ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code ) ) ;
}
2011-11-20 12:09:07 +01:00
void varidFunctionCall4 ( ) {
// Ticket #3280
const std : : string code1 ( " void f() { int x; fun(a,b*x); } " ) ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: void f ( ) { int x@1 ; fun ( a , b * x@1 ) ; } \n " ,
2012-04-15 17:44:51 +02:00
tokenizeDebugListing ( code1 , false , " test.c " ) ) ;
2011-11-20 12:09:07 +01:00
const std : : string code2 ( " void f(int a) { int x; fun(a,b*x); } " ) ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: void f ( int a@1 ) { int x@2 ; fun ( a@1 , b * x@2 ) ; } \n " ,
2012-04-15 17:44:51 +02:00
tokenizeDebugListing ( code2 , false , " test.c " ) ) ;
2011-11-20 12:09:07 +01:00
}
2011-10-13 20:53:06 +02:00
void varidStl ( ) {
2010-02-25 21:00:39 +01:00
const std : : string actual = tokenizeDebugListing (
" list<int> ints; \n "
" list<int>::iterator it; \n "
" std::vector<std::string> dirs; \n "
" std::map<int, int> coords; \n "
" std::tr1::unordered_map<int, int> xy; \n "
" std::list<boost::wave::token_id> tokens; \n "
" static std::vector<CvsProcess*> ex1; \n "
" extern std::vector<CvsProcess*> ex2; \n "
" std::map<int, 1> m; \n "
) ;
2009-04-25 16:55:00 +02:00
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
}
2011-10-13 20:53:06 +02:00
void varid_delete ( ) {
2010-02-25 21:00:39 +01:00
const std : : string actual = tokenizeDebugListing (
" void f() \n "
" { \n "
" int *a; \n "
" delete a; \n "
" } \n " ) ;
2009-03-28 21:07:33 +01:00
2009-04-29 21:22:56 +02:00
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
}
2011-10-13 20:53:06 +02:00
void varid_functions ( ) {
2009-05-27 22:34:08 +02:00
{
2010-02-25 21:00:39 +01:00
const std : : string actual = tokenizeDebugListing (
" void f(); \n "
2012-04-15 12:49:53 +02:00
" void f(){} \n " , false , " test.c " ) ;
2009-05-27 22:34:08 +02:00
const std : : string expected ( " \n \n ##file 0 \n "
" 1: void f ( ) ; \n "
" 2: void f ( ) { } \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
{
2010-02-25 21:00:39 +01:00
const std : : string actual = tokenizeDebugListing (
" A f(3); \n "
" A f2(true); \n "
" A g(); \n "
2012-04-15 12:49:53 +02:00
" A e(int c); \n " , false , " test.c " ) ;
2009-05-27 22:34:08 +02:00
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
{
2010-02-25 21:00:39 +01:00
const std : : string actual = tokenizeDebugListing (
" 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 " ) ;
2009-06-18 18:30:04 +02:00
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
2012-05-15 18:40:24 +02:00
void varid_sizeof ( ) {
const char code [ ] = " x = sizeof(a*b); " ;
const char expected [ ] = " \n \n ##file 0 \n "
" 1: x = sizeof ( a * b ) ; \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , false , " test.c " ) ) ;
}
2011-10-13 20:53:06 +02:00
void varid_reference_to_containers ( ) {
2010-02-25 21:00:39 +01:00
const std : : string actual = tokenizeDebugListing (
" void f() \n "
" { \n "
" 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
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 "
2011-03-24 02:15:49 +01:00
" 5: std :: vector < int > * c@3 ; c@3 = & b@1 ; \n "
2009-07-18 12:37:51 +02:00
" 6: } \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
2011-10-13 20:53:06 +02:00
void varid_in_class1 ( ) {
2010-04-12 21:16:47 +02:00
{
const std : : string actual = tokenizeDebugListing (
" class Foo \n "
" { \n "
" public: \n "
" std::string name1; \n "
" std::string name2; \n "
" }; \n " ) ;
2009-08-09 10:41:31 +02:00
2010-04-12 21:16:47 +02:00
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 " ) ;
2009-08-09 10:41:31 +02:00
2010-04-12 21:16:47 +02:00
ASSERT_EQUALS ( expected , actual ) ;
}
{
const std : : string actual = tokenizeDebugListing (
" class foo \n "
" { \n "
" public: \n "
" void do_something(const int x, const int y); \n "
" void bar(); \n "
" }; \n "
" \n "
" void foo::bar() \n "
" { \n "
" POINT pOutput = { 0 , 0 }; \n "
" int x = pOutput.x; \n "
" int y = pOutput.y; \n "
" } \n " ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: class foo \n "
" 2: { \n "
" 3: public: \n "
" 4: void do_something ( const int x@1 , const int y@2 ) ; \n "
" 5: void bar ( ) ; \n "
" 6: } ; \n "
" 7: \n "
" 8: void foo :: bar ( ) \n "
" 9: { \n "
" 10: POINT pOutput@3 ; pOutput@3 = { 0 , 0 } ; \n "
2012-04-22 11:36:31 +02:00
" 11: int x@4 ; x@4 = pOutput@3 . x@5 ; \n "
" 12: int y@6 ; y@6 = pOutput@3 . y@7 ; \n "
2010-04-12 21:16:47 +02:00
" 13: } \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
2009-08-09 10:41:31 +02:00
}
2011-10-13 20:53:06 +02:00
void varid_in_class2 ( ) {
2010-08-20 19:28:10 +02:00
const std : : string actual = tokenizeDebugListing (
" struct Foo { \n "
" int x; \n "
" }; \n "
" \n "
" struct Bar { \n "
" Foo foo; \n "
" int x; \n "
" void f(); \n "
" }; \n "
" \n "
" void Bar::f() \n "
" { \n "
" foo.x = x; \n "
" } \n " ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: struct Foo { \n "
" 2: int x@1 ; \n "
" 3: } ; \n "
" 4: \n "
" 5: struct Bar { \n "
" 6: Foo foo@2 ; \n "
" 7: int x@3 ; \n "
" 8: void f ( ) ; \n "
" 9: } ; \n "
" 10: \n "
" 11: void Bar :: f ( ) \n "
" 12: { \n "
2011-03-19 09:04:03 +01:00
" 13: foo@2 . x@4 = x@3 ; \n "
2010-08-20 19:28:10 +02:00
" 14: } \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
2011-10-29 21:25:58 +02:00
void varid_in_class3 ( ) {
const std : : string code = " class Foo { \n "
" void blah() { \n "
" Bar x(*this); \n " // <- ..
" } \n "
" int x; \n " // <- .. don't assign same varid
" }; " ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: class Foo { \n "
" 2: void blah ( ) { \n "
" 3: Bar x@1 ( * this ) ; \n "
" 4: } \n "
" 5: int x@2 ; \n "
" 6: } ; \n " , tokenizeDebugListing ( code ) ) ;
}
2011-10-31 18:33:15 +01:00
void varid_in_class4 ( ) {
const std : : string code = " class Foo { \n "
" public: class C; \n "
" }; " ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: class Foo { \n "
" 2: public: class C ; \n "
" 3: } ; \n " ,
tokenizeDebugListing ( code ) ) ;
}
2011-10-29 21:25:58 +02:00
2012-02-12 17:30:47 +01:00
void varid_in_class5 ( ) {
const char code [ ] = " struct Foo { \n "
" std::vector<::X> v; \n "
" } " ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: struct Foo { \n "
" 2: std :: vector < :: X > v@1 ; \n "
" 3: } \n " ,
tokenizeDebugListing ( code ) ) ;
}
2012-04-26 18:38:47 +02:00
void varid_in_class6 ( ) {
const char code [ ] = " class A { \n "
" void f(const char *str) const { \n "
" std::stringstream sst; \n "
" sst.str(); \n "
" } \n "
" }; " ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: class A { \n "
" 2: void f ( const char * str@1 ) const { \n "
" 3: std :: stringstream sst@2 ; \n "
" 4: sst@2 . str ( ) ; \n "
" 5: } \n "
" 6: } ; \n " ,
tokenizeDebugListing ( code ) ) ;
}
void varid_in_class7 ( ) {
const char code [ ] = " class A { \n "
" void f() { \n "
" abc.a = 0; \n "
" } \n "
" struct ABC abc; \n "
" }; " ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: class A { \n "
" 2: void f ( ) { \n "
" 3: abc@1 . a@2 = 0 ; \n "
" 4: } \n "
" 5: struct ABC abc@1 ; \n "
" 6: } ; \n " ,
tokenizeDebugListing ( code ) ) ;
}
2012-05-15 07:17:31 +02:00
void varid_in_class8 ( ) { // #3776 - unknown macro
const char code [ ] = " class A { \n "
" UNKNOWN_MACRO(A) \n "
" private: \n "
" int x; \n "
" }; " ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: class A { \n "
" 2: UNKNOWN_MACRO ( A ) \n "
" 3: private: \n "
" 4: int x@1 ; \n "
" 5: } ; \n " ,
tokenizeDebugListing ( code ) ) ;
}
2012-08-28 12:44:40 +02:00
void varid_initList ( ) {
const char code [ ] = " class A { \n "
" A() : x(0) {} \n "
" int x; \n "
" }; " ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: class A { \n "
" 2: A ( ) : x@1 ( 0 ) { } \n "
" 3: int x@1 ; \n "
" 4: } ; \n " ,
tokenizeDebugListing ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void varid_operator ( ) {
2010-08-31 20:15:24 +02:00
{
const std : : string actual = tokenizeDebugListing (
" class Foo \n "
" { \n "
" public: \n "
" void operator=(const Foo &); \n "
" }; \n " ) ;
2010-02-07 13:04:33 +01:00
2010-08-31 20:15:24 +02:00
const std : : string expected ( " \n \n ##file 0 \n "
" 1: class Foo \n "
" 2: { \n "
" 3: public: \n "
2011-01-27 18:44:20 +01:00
" 4: void operator= ( const Foo & ) ; \n "
2010-08-31 20:15:24 +02:00
" 5: } ; \n " ) ;
2010-02-07 13:04:33 +01:00
2010-08-31 20:15:24 +02:00
ASSERT_EQUALS ( expected , actual ) ;
}
{
const std : : string actual = tokenizeDebugListing (
" struct Foo { \n "
" void * operator new [](int); \n "
" }; \n " ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: struct Foo { \n "
2011-01-27 18:44:20 +01:00
" 2: void * operatornew[] ( int ) ; \n "
2010-08-31 20:15:24 +02:00
" 3: } ; \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
2010-02-07 13:04:33 +01:00
}
2011-10-13 20:53:06 +02:00
void varid_throw ( ) { // ticket #1723
2010-06-03 07:05:57 +02:00
const std : : string actual = tokenizeDebugListing (
" UserDefinedException* pe = new UserDefinedException(); \n "
" throw pe; \n " ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: UserDefinedException * pe@1 ; pe@1 = new UserDefinedException ( ) ; \n "
" 2: throw pe@1 ; \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
2011-10-13 20:53:06 +02:00
void varid_unknown_macro ( ) {
2011-03-13 17:52:45 +01:00
// #2638 - unknown macro
const char code [ ] = " void f() { \n "
" int a[10]; \n "
" AAA \n "
" a[0] = 0; \n "
" } " ;
const char expected [ ] = " \n \n ##file 0 \n "
" 1: void f ( ) { \n "
" 2: int a@1 [ 10 ] ; \n "
" 3: AAA \n "
" 4: a@1 [ 0 ] = 0 ; \n "
" 5: } \n " ;
2012-04-21 17:57:20 +02:00
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , false , " test.c " ) ) ;
2011-03-13 17:52:45 +01:00
}
2012-03-20 19:05:24 +01:00
void varid_using ( ) {
// #3648
const char code [ ] = " using std::size_t; " ;
const char expected [ ] = " \n \n ##file 0 \n "
" 1: using long ; \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code ) ) ;
}
2012-04-15 17:39:22 +02:00
void varid_catch ( ) {
const char code [ ] = " void f() { \n "
" try { dostuff(); } \n "
" catch (exception &e) { } \n "
" } " ;
const char expected [ ] = " \n \n ##file 0 \n "
" 1: void f ( ) { \n "
" 2: try { dostuff ( ) ; } \n "
" 3: catch ( exception & e@1 ) { } \n "
" 4: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code ) ) ;
}
2012-08-12 14:19:56 +02:00
void varid_functionPrototypeTemplate ( ) {
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: function < void ( ) > fptr@1 ; \n " , tokenizeDebugListing ( " function<void(void)> fptr; " ) ) ;
}
2011-10-13 20:53:06 +02:00
void varidclass1 ( ) {
2010-02-25 21:00:39 +01:00
const std : : string actual = tokenizeDebugListing (
" 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 " ) ;
2009-05-02 22:57:18 +02:00
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
}
2011-10-13 20:53:06 +02:00
void varidclass2 ( ) {
2010-02-25 21:00:39 +01:00
const std : : string actual = tokenizeDebugListing (
" 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 " ) ;
2009-05-03 13:50:26 +02:00
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 ) ;
}
2011-10-13 20:53:06 +02:00
void varidclass3 ( ) {
2010-02-25 21:00:39 +01:00
const std : : string actual = tokenizeDebugListing (
" 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 " ) ;
2009-05-03 13:50:26 +02:00
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 "
2011-03-19 11:09:51 +01:00
" 11: i = 0 ; \n "
2009-05-03 13:50:26 +02:00
" 12: } \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
}
2011-10-13 20:53:06 +02:00
void varidclass4 ( ) {
2010-02-25 21:00:39 +01:00
const std : : string actual = tokenizeDebugListing (
" class Fred \n "
" { int i; void f(); }; \n "
" \n "
" void Fred::f() \n "
" { \n "
" if (i) { } \n "
" i = 0; \n "
" } \n " ) ;
2009-05-03 13:50:26 +02:00
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 ) ;
2010-02-07 13:34:39 +01:00
}
2011-10-13 20:53:06 +02:00
void varidclass5 ( ) {
2010-02-25 21:00:39 +01:00
const std : : string actual = tokenizeDebugListing (
" class A { }; \n "
" class B \n "
" { \n "
" A *a; \n "
" B() : a(new A) \n "
" { } \n "
" }; \n " ) ;
2010-02-07 13:34:39 +01:00
const std : : string expected ( " \n \n ##file 0 \n "
" 1: class A { } ; \n "
" 2: class B \n "
" 3: { \n "
" 4: A * a@1 ; \n "
" 5: B ( ) : a@1 ( new A ) \n "
" 6: { } \n "
" 7: } ; \n " ) ;
ASSERT_EQUALS ( expected , actual ) ;
2009-04-29 21:22:56 +02:00
}
2009-02-16 21:46:24 +01:00
2011-10-13 20:53:06 +02:00
void varidclass6 ( ) {
2010-02-25 21:02:12 +01:00
const std : : string actual = tokenizeDebugListing (
" class A \n "
" { \n "
" public: \n "
" static char buf[20]; \n "
" }; \n "
" char A::buf[20]; \n "
" int main() \n "
" { \n "
" char buf[2]; \n "
" A::buf[10] = 0; \n "
" } " ) ;
2011-01-30 12:54:19 +01:00
const std : : string wanted ( " \n \n ##file 0 \n "
" 1: class A \n "
" 2: { \n "
" 3: public: \n "
" 4: static char buf@1 [ 20 ] ; \n "
" 5: } ; \n "
" 6: char A :: buf@1 [ 20 ] ; \n "
" 7: int main ( ) \n "
" 8: { \n "
" 9: char buf@2 [ 2 ] ; \n "
" 10: A :: buf@1 [ 10 ] = 0 ; \n "
" 11: } \n " ) ;
2010-02-25 21:02:12 +01:00
2011-01-30 12:54:19 +01:00
const char current [ ] = " \n \n ##file 0 \n 1: class A \n 2: { \n 3: public: \n 4: static char buf@1 [ 20 ] ; \n 5: } ; \n 6: char A :: buf [ 20 ] ; \n 7: int main ( ) \n 8: { \n 9: char buf@2 [ 2 ] ; \n 10: A :: buf [ 10 ] = 0 ; \n 11: } \n " ;
TODO_ASSERT_EQUALS ( wanted , current , actual ) ;
2010-02-25 21:02:12 +01:00
}
2011-10-13 20:53:06 +02:00
void varidclass7 ( ) {
2010-02-25 21:02:12 +01:00
const std : : string actual = tokenizeDebugListing (
" int main() \n "
" { \n "
" char buf[2]; \n "
" A::buf[10] = 0; \n "
" } " ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: int main ( ) \n "
" 2: { \n "
" 3: char buf@1 [ 2 ] ; \n "
" 4: A :: buf [ 10 ] = 0 ; \n "
" 5: } \n " ) ;
2010-08-10 18:22:58 +02:00
ASSERT_EQUALS ( expected , actual ) ;
2010-02-25 21:02:12 +01:00
}
2011-10-13 20:53:06 +02:00
void varidclass8 ( ) {
2010-07-12 20:18:03 +02:00
const std : : string code ( " class Fred { \n "
" public: \n "
" void foo(int d) { \n "
" int i = bar(x * d); \n "
" } \n "
" int x; \n "
" } \n " ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: class Fred { \n "
" 2: public: \n "
" 3: void foo ( int d@1 ) { \n "
2011-03-19 09:04:03 +01:00
" 4: int i@2 ; i@2 = bar ( x@3 * d@1 ) ; \n "
2010-07-12 20:18:03 +02:00
" 5: } \n "
" 6: int x@3 ; \n "
" 7: } \n " ) ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void varidclass9 ( ) {
2011-03-08 20:41:41 +01:00
const std : : string code ( " typedef char Str[10]; "
" class A { \n "
" public: \n "
" void f(Str &cl); \n "
" void g(Str cl); \n "
" } \n "
" void Fred::f(Str &cl) { \n "
" sizeof(cl); \n "
" } " ) ;
const std : : string expected ( " \n \n "
" ##file 0 \n "
2011-12-04 17:15:53 +01:00
" 1: class A { \n "
2011-03-08 20:41:41 +01:00
" 2: public: \n "
" 3: void f ( char ( & cl ) [ 10 ] ) ; \n "
" 4: void g ( char cl@1 [ 10 ] ) ; \n "
" 5: } \n "
" 6: void Fred :: f ( char ( & cl ) [ 10 ] ) { \n "
" 7: sizeof ( cl ) ; \n "
" 8: } \n " ) ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code ) ) ;
2011-03-19 09:04:03 +01:00
}
2011-10-13 20:53:06 +02:00
void varidclass10 ( ) {
2011-03-19 09:04:03 +01:00
const std : : string code ( " class A { \n "
" void f() { \n "
" a = 3; \n "
" } \n "
" int a; \n "
2011-03-19 11:09:51 +01:00
" }; \n " ) ;
2011-03-19 09:04:03 +01:00
const std : : string expected ( " \n \n ##file 0 \n "
" 1: class A { \n "
" 2: void f ( ) { \n "
" 3: a@1 = 3 ; \n "
" 4: } \n "
" 5: int a@1 ; \n "
" 6: } ; \n " ) ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code ) ) ;
2011-03-08 20:41:41 +01:00
}
2011-10-13 20:53:06 +02:00
void varidclass11 ( ) {
2011-03-19 11:09:51 +01:00
const std : : string code ( " class Fred { \n "
" int a; \n "
" void f(); \n "
" }; \n "
" class Wilma { \n "
" int a; \n "
" void f(); \n "
" }; \n "
" void Fred::f() { a = 0; } \n "
" void Wilma::f() { a = 0; } \n " ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: class Fred { \n "
" 2: int a@1 ; \n "
" 3: void f ( ) ; \n "
" 4: } ; \n "
" 5: class Wilma { \n "
" 6: int a@2 ; \n "
" 7: void f ( ) ; \n "
" 8: } ; \n "
" 9: void Fred :: f ( ) { a@1 = 0 ; } \n "
" 10: void Wilma :: f ( ) { a@2 = 0 ; } \n " ) ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code ) ) ;
}
2010-07-12 20:18:03 +02:00
2011-10-13 20:53:06 +02:00
void varidclass12 ( ) {
2011-03-26 08:56:41 +01:00
const std : : string code ( " class Fred { \n "
" int a; \n "
" void f() { Fred::a = 0; } \n "
" }; \n " ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: class Fred { \n "
" 2: int a@1 ; \n "
" 3: void f ( ) { Fred :: a@1 = 0 ; } \n "
" 4: } ; \n " ) ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void varidclass13 ( ) {
2011-03-26 10:05:07 +01:00
const std : : string code ( " class Fred { \n "
" int a; \n "
" void f() { Foo::Fred::a = 0; } \n "
" }; \n " ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: class Fred { \n "
" 2: int a@1 ; \n "
" 3: void f ( ) { Foo :: Fred :: a = 0 ; } \n "
" 4: } ; \n " ) ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code ) ) ;
}
2011-10-29 18:22:58 +02:00
void varidclass14 ( ) {
// don't give friend classes varid
{
const std : : string code ( " class A { \n "
" friend class B; \n "
" } " ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: class A { \n "
" 2: friend class B ; \n "
" 3: } \n " ) ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code ) ) ;
}
{
const std : : string code ( " class A { \n "
" private: friend class B; \n "
" } " ) ;
const std : : string expected ( " \n \n ##file 0 \n "
" 1: class A { \n "
" 2: private: friend class B ; \n "
" 3: } \n " ) ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code ) ) ;
}
}
2012-04-23 18:26:27 +02:00
void varidclass15 ( ) {
const char code [ ] = " class A { \n "
" int a; \n "
" int b; \n "
" A(); \n "
" }; \n "
" A::A() : a(0) { b = 1; } " ;
const char expected [ ] = " \n \n ##file 0 \n "
" 1: class A { \n "
" 2: int a@1 ; \n "
" 3: int b@2 ; \n "
" 4: A ( ) ; \n "
" 5: } ; \n "
" 6: A :: A ( ) : a@1 ( 0 ) { b@2 = 1 ; } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void file1 ( ) {
2009-01-19 19:40:24 +01:00
const char code [ ] = " a1 \n "
" #file \" b \" \n "
" b1 \n "
" b2 \n "
" #endfile \n "
" a3 \n " ;
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
Settings settings ;
2009-01-19 19:40:24 +01:00
// tokenize..
2010-12-01 18:00:55 +01:00
Tokenizer tokenizer ( & settings , this ) ;
2009-01-19 19:40:24 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " a " ) ;
2011-10-13 20:53:06 +02:00
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) ) {
2009-01-19 19:40:24 +01:00
std : : ostringstream ostr ;
ostr < < char ( ' a ' + tok - > fileIndex ( ) ) < < tok - > linenr ( ) ;
ASSERT_EQUALS ( tok - > str ( ) , ostr . str ( ) ) ;
}
}
2009-01-20 18:05:42 +01:00
2011-10-13 20:53:06 +02:00
void file2 ( ) {
2009-01-20 18:05:42 +01:00
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 " ;
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
Settings settings ;
2009-01-20 18:05:42 +01:00
// tokenize..
2010-12-01 18:00:55 +01:00
Tokenizer tokenizer ( & settings , this ) ;
2009-01-20 18:05:42 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " a " ) ;
2011-10-13 20:53:06 +02:00
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) ) {
2009-01-20 18:05:42 +01:00
std : : ostringstream ostr ;
ostr < < char ( ' a ' + tok - > fileIndex ( ) ) < < tok - > linenr ( ) ;
ASSERT_EQUALS ( tok - > str ( ) , ostr . str ( ) ) ;
}
}
2009-01-20 18:26:16 +01:00
2011-10-13 20:53:06 +02:00
void file3 ( ) {
2009-02-15 12:42:04 +01:00
const char code [ ] = " #file \" c: \\ a.h \" \n "
" 123 \n "
" #endfile \n " ;
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
Settings settings ;
2009-02-15 12:42:04 +01:00
// tokenize..
2010-12-01 18:00:55 +01:00
Tokenizer tokenizer ( & settings , this ) ;
2009-02-15 12:42:04 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " a.cpp " ) ;
2012-05-06 13:01:56 +02:00
ASSERT_EQUALS ( Path : : toNativeSeparators ( " [c: \\ a.h:1] " ) , tokenizer . list . fileLine ( tokenizer . tokens ( ) ) ) ;
2009-02-15 12:42:04 +01:00
}
2011-10-13 20:53:06 +02:00
void doublesharp ( ) {
2011-12-10 23:49:56 +01:00
const char code [ ] = " a##_##b TEST(var,val) var##_##val = val \n " ;
2009-01-20 18:26:16 +01:00
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
Settings settings ;
2009-01-20 18:26:16 +01:00
// Tokenize..
2010-12-01 18:00:55 +01:00
Tokenizer tokenizer ( & settings , this ) ;
2009-01-20 18:26:16 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " " ) ;
2012-04-16 19:51:07 +02:00
ASSERT_EQUALS ( " a_b TEST ( var , val ) var_val = val " , tokenizer . tokens ( ) - > stringifyList ( 0 , false ) ) ;
2009-01-20 18:26:16 +01:00
}
2009-01-24 08:56:47 +01:00
2011-10-13 20:53:06 +02:00
void macrodoublesharp ( ) {
2009-02-13 14:33:12 +01:00
const char code [ ] = " DBG(fmt,args...) printf(fmt, ## args) \n " ;
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
Settings settings ;
2009-02-13 14:33:12 +01:00
// Tokenize..
2010-12-01 18:00:55 +01:00
Tokenizer tokenizer ( & settings , this ) ;
2009-02-13 14:33:12 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " " ) ;
2012-04-16 19:51:07 +02:00
ASSERT_EQUALS ( " DBG ( fmt , args . . . ) printf ( fmt , ## args ) " , tokenizer . tokens ( ) - > stringifyList ( 0 , false ) ) ;
2009-02-13 14:33:12 +01:00
}
2012-01-23 16:10:15 +01:00
void simplifyFunctionParameters ( ) {
2009-12-31 19:31:21 +01:00
{
const char code [ ] = " char a [ ABC ( DEF ) ] ; " ;
2012-07-29 15:11:48 +02:00
ASSERT_EQUALS ( code , tokenizeAndStringify ( code ) ) ;
2009-12-31 19:31:21 +01:00
}
2010-01-01 13:26:54 +01:00
{
const char code [ ] = " module ( a , a , sizeof ( a ) , 0444 ) ; " ;
2012-07-29 15:11:48 +02:00
ASSERT_EQUALS ( " module ( a , a , sizeof ( a ) , 292 ) ; " , tokenizeAndStringify ( code ) ) ;
2010-01-01 13:26:54 +01:00
}
2012-07-29 15:11:48 +02:00
ASSERT_EQUALS ( " void f ( int x ) { } " , tokenizeAndStringify ( " void f(x) int x; { } " ) ) ;
ASSERT_EQUALS ( " void f ( int x , char y ) { } " , tokenizeAndStringify ( " void f(x,y) int x; char y; { } " ) ) ;
ASSERT_EQUALS ( " int main ( int argc , char * argv [ ] ) { } " , tokenizeAndStringify ( " int main(argc,argv) int argc; char *argv[]; { } " ) ) ;
ASSERT_EQUALS ( " int f ( int p , int w , float d ) { } " , tokenizeAndStringify ( " int f(p,w,d) float d; { } " ) ) ;
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.
2012-07-29 15:11:48 +02:00
ASSERT_EQUALS ( " int ( * d ( a , b , c ) ) ( ) int a ; int b ; int c ; { } " , tokenizeAndStringify ( " int (*d(a,b,c))()int a,b,c; { } " ) ) ;
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; "
" { } "
" } " ;
2011-12-02 23:01:55 +01:00
ASSERT_EQUALS ( " void foo ( ) { if ( x ) { } { } } " , tokenizeAndStringify ( code , true ) ) ;
2009-01-24 19:21:16 +01:00
}
2012-04-29 12:58:12 +02:00
// #3770 - Don't segfault and don't change macro argument as if it's a K&R function argument
{
const char code [ ] = " MACRO(a) "
" "
" void f() "
" { "
" SetLanguage(); "
" { "
" } "
" } " ;
2012-07-29 15:11:48 +02:00
ASSERT_EQUALS ( " MACRO ( a ) void f ( ) { SetLanguage ( ) ; { } } " , tokenizeAndStringify ( code ) ) ;
2012-04-29 12:58:12 +02:00
}
2009-01-24 08:56:47 +01:00
}
2009-01-25 20:39:05 +01:00
2012-04-22 17:28:27 +02:00
void simplifyFunctionParameters1 ( ) { // ticket #3721
const char code [ ] = " typedef float ufloat; \n "
" typedef short ftnlen; \n "
" int f(p,w,d,e,len) ufloat *p; ftnlen len; \n "
" { \n "
" } \n " ;
ASSERT_EQUALS ( " int f ( float * p , int w , int d , int e , short len ) \n "
" { \n "
2012-07-29 15:11:48 +02:00
" } " , tokenizeAndStringify ( code ) ) ;
2012-04-22 17:28:27 +02:00
}
2012-01-23 16:10:15 +01:00
void simplifyFunctionParametersErrors ( ) {
//same parameters...
tokenizeAndStringify ( " void foo(x, x) \n "
" int x; \n "
" int x; \n "
" {} \n " ) ;
ASSERT_EQUALS ( " [test.cpp:1]: (error) syntax error \n " , errout . str ( ) ) ;
tokenizeAndStringify ( " void foo(x, y) \n "
" int x; \n "
" int x; \n "
" {} \n " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (error) syntax error \n " , errout . str ( ) ) ;
tokenizeAndStringify ( " void foo(int, int) \n "
" {} \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-07-29 15:11:48 +02:00
// #3848 - Don't hang
tokenizeAndStringify ( " sal_Bool ShapeHasText(sal_uLong, sal_uLong) const { \n "
" return sal_True; \n "
" } \n "
" void CreateSdrOLEFromStorage() { \n "
" comphelper::EmbeddedObjectContainer aCnt( xDestStorage ); \n "
" { } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-01-23 16:10:15 +01:00
}
2009-01-25 20:39:05 +01:00
// Simplify "((..))" into "(..)"
2011-10-13 20:53:06 +02:00
void removeParentheses1 ( ) {
2011-05-01 07:50:19 +02:00
const char code [ ] = " void foo() "
" { "
" free(((void*)p)); "
2009-01-25 20:39:05 +01:00
" } " ;
2011-05-01 07:50:19 +02:00
ASSERT_EQUALS ( " void foo ( ) { free ( p ) ; } " , tokenizeAndStringify ( code , true ) ) ;
2009-01-25 20:39:05 +01:00
}
2011-10-13 20:53:06 +02:00
void removeParentheses2 ( ) {
2011-05-01 07:50:19 +02:00
const char code [ ] = " void foo() "
" { "
" if (__builtin_expect((s == NULL), 0)) "
" return; "
2009-05-28 19:37:39 +02:00
" } " ;
2011-05-01 07:50:19 +02:00
ASSERT_EQUALS ( " void foo ( ) { if ( ! s ) { return ; } } " , tokenizeAndStringify ( code ) ) ;
2009-05-28 19:37:39 +02:00
}
2011-10-13 20:53:06 +02:00
void removeParentheses3 ( ) {
2009-05-29 23:04:01 +02:00
{
2011-05-01 07:50:19 +02:00
const char code [ ] = " void foo() "
" { "
" if (( true )==(true)){} "
2009-05-29 23:04:01 +02:00
" } " ;
2011-11-12 12:01:31 +01:00
ASSERT_EQUALS ( " void foo ( ) { } " , tokenizeAndStringify ( code , true ) ) ;
2009-05-29 23:04:01 +02:00
}
{
2011-05-01 07:50:19 +02:00
const char code [ ] = " void foo() "
" { "
" if (( 2 )==(2)){} "
2009-05-29 23:04:01 +02:00
" } " ;
2011-11-12 12:01:31 +01:00
ASSERT_EQUALS ( " void foo ( ) { } " , tokenizeAndStringify ( code , true ) ) ;
2009-05-29 23:04:01 +02:00
}
2009-05-31 14:55:06 +02:00
{
2011-05-01 07:50:19 +02:00
const char code [ ] = " void foo() "
" { "
" if( g(10)){} "
2009-05-31 14:55:06 +02:00
" } " ;
2011-05-01 07:50:19 +02:00
ASSERT_EQUALS ( " void foo ( ) { if ( g ( 10 ) ) { } } " , tokenizeAndStringify ( code ) ) ;
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 (..)"
2011-10-13 20:53:06 +02:00
void removeParentheses4 ( ) {
2011-05-01 07:50:19 +02:00
const char code [ ] = " void foo() "
" { "
" (free(p)); "
2009-06-12 15:04:58 +02:00
" } " ;
2011-05-01 07:50:19 +02:00
ASSERT_EQUALS ( " void foo ( ) { free ( p ) ; } " , tokenizeAndStringify ( code ) ) ;
2009-06-12 15:04:58 +02:00
}
2011-10-13 20:53:06 +02:00
void removeParentheses5 ( ) {
2009-06-12 16:14:01 +02:00
// Simplify "( delete x )" into "delete x"
{
2011-05-01 07:50:19 +02:00
const char code [ ] = " void foo() "
" { "
" (delete p); "
2009-06-12 16:14:01 +02:00
" } " ;
2011-05-01 07:50:19 +02:00
ASSERT_EQUALS ( " void foo ( ) { delete p ; } " , tokenizeAndStringify ( code ) ) ;
2009-06-12 16:14:01 +02:00
}
// Simplify "( delete [] x )" into "delete [] x"
{
2011-05-01 07:50:19 +02:00
const char code [ ] = " void foo() "
" { "
" (delete [] p); "
2009-06-12 16:14:01 +02:00
" } " ;
2011-05-01 07:50:19 +02:00
ASSERT_EQUALS ( " void foo ( ) { delete [ ] p ; } " , tokenizeAndStringify ( code ) ) ;
2009-06-12 16:14:01 +02:00
}
}
2009-11-15 09:28:08 +01:00
// "!(abc.a)" => "!abc.a"
2011-10-13 20:53:06 +02:00
void removeParentheses6 ( ) {
2009-11-15 09:28:08 +01:00
const char code [ ] = " (!(abc.a)) " ;
2011-05-01 07:50:19 +02:00
ASSERT_EQUALS ( " ( ! abc . a ) " , tokenizeAndStringify ( code ) ) ;
2009-11-15 09:28:08 +01:00
}
2011-10-13 20:53:06 +02:00
void removeParentheses7 ( ) {
2010-09-05 07:53:43 +02:00
const char code [ ] = " ;char *p; (delete(p), (p)=0); " ;
2011-05-01 08:27:59 +02:00
ASSERT_EQUALS ( " ; char * p ; delete p ; p = 0 ; " , tokenizeAndStringify ( code , true ) ) ;
2010-05-07 18:37:50 +02:00
}
2011-10-13 20:53:06 +02:00
void removeParentheses8 ( ) {
2010-07-19 12:06:20 +02:00
const char code [ ] = " struct foo { \n "
" void operator delete(void *obj, size_t sz); \n "
" } \n " ;
2011-09-24 20:51:03 +02:00
const std : : string actual ( tokenizeAndStringify ( code , false , true , Settings : : Win32A ) ) ;
2010-07-19 12:06:20 +02:00
const char expected [ ] = " struct foo { \n "
2011-09-18 16:31:31 +02:00
" void operatordelete ( void * obj , unsigned long sz ) ; \n "
2010-07-19 12:06:20 +02:00
" } " ;
ASSERT_EQUALS ( expected , actual ) ;
}
2011-10-13 20:53:06 +02:00
void removeParentheses9 ( ) {
2010-09-05 07:53:43 +02:00
ASSERT_EQUALS ( " void delete ( double num ) ; " , tokenizeAndStringify ( " void delete(double num); " , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void removeParentheses10 ( ) {
2010-12-18 09:44:58 +01:00
ASSERT_EQUALS ( " p = buf + 8 ; " , tokenizeAndStringify ( " p = (buf + 8); " , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void removeParentheses11 ( ) {
2011-01-28 09:19:30 +01:00
// #2502
ASSERT_EQUALS ( " { } x ( ) ; " , tokenizeAndStringify ( " {}(x()); " , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void removeParentheses12 ( ) {
2011-05-01 08:27:59 +02:00
// #2760
ASSERT_EQUALS ( " , x = 0 ; " , tokenizeAndStringify ( " ,(x)=0; " , false ) ) ;
}
2011-11-07 23:40:06 +01:00
void removeParentheses13 ( ) {
ASSERT_EQUALS ( " ; f ( a + b , c ) ; " , tokenizeAndStringify ( " ;f((a+b),c); " , false ) ) ;
ASSERT_EQUALS ( " ; x = y [ a + b ] ; " , tokenizeAndStringify ( " ;x=y[(a+b)]; " , false ) ) ;
}
2011-11-08 22:48:14 +01:00
void removeParentheses14 ( ) {
2011-11-08 23:10:53 +01:00
ASSERT_EQUALS ( " ; if ( ! ( i & 1 ) ) { ; } ; " , tokenizeAndStringify ( " ; if ( (i & 1) == 0 ); ; " , false ) ) ;
2011-11-08 22:48:14 +01:00
}
2012-09-06 16:16:29 +02:00
void removeParentheses15 ( ) {
ASSERT_EQUALS ( " a = b ? c : 123 ; " , tokenizeAndStringify ( " a = b ? c : (123); " , false ) ) ;
2012-09-08 10:15:37 +02:00
ASSERT_EQUALS ( " a = b ? c : 579 ; " , tokenizeAndStringify ( " a = b ? c : ((123)+(456)); " , false ) ) ;
2012-09-08 10:45:00 +02:00
ASSERT_EQUALS ( " a = b ? 123 : c ; " , tokenizeAndStringify ( " a = b ? (123) : c; " , false ) ) ;
2012-09-06 16:16:29 +02:00
}
2011-10-13 20:53:06 +02:00
void tokenize_double ( ) {
2009-02-08 10:51:45 +01:00
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
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
Settings settings ;
2009-02-08 10:51:45 +01:00
// tokenize..
2010-12-01 18:00:55 +01:00
Tokenizer tokenizer ( & settings , this ) ;
2009-02-08 10:51:45 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
2012-08-20 18:10:32 +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 = 6 ; } " , tokenizer . tokens ( ) - > stringifyList ( 0 , false ) ) ;
2009-02-08 10:51:45 +01:00
}
2009-02-08 11:25:33 +01:00
2011-10-13 20:53:06 +02:00
void tokenize_strings ( ) {
2009-02-08 11:25:33 +01:00
const char code [ ] = " void f() \n "
" { \n "
" const char *a = \n "
" { \n "
" \" hello \" \n "
" \" more \" \n "
" \" world \" \n "
" }; \n "
" } \n " ;
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
Settings settings ;
2009-02-08 11:25:33 +01:00
// tokenize..
2010-12-01 18:00:55 +01:00
Tokenizer tokenizer ( & settings , this ) ;
2009-02-08 11:25:33 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyTokenList ( ) ;
2012-04-16 19:51:07 +02:00
ASSERT_EQUALS ( " void f ( ) { const char * a ; a = { \" hello more world \" } ; } " , tokenizer . tokens ( ) - > stringifyList ( 0 , false ) ) ;
2009-02-08 11:25:33 +01:00
}
2009-02-11 23:15:22 +01:00
2011-10-13 20:53:06 +02:00
void simplify_constants ( ) {
2009-02-11 23:15:22 +01:00
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 " ;
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
Settings settings ;
2009-02-11 23:15:22 +01:00
// tokenize..
2010-12-01 18:00:55 +01:00
Tokenizer tokenizer ( & settings , this ) ;
2009-02-11 23:15:22 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyTokenList ( ) ;
2012-04-16 19:51:07 +02:00
ASSERT_EQUALS ( " void f ( ) { } void g ( ) { } " , tokenizer . tokens ( ) - > stringifyList ( 0 , false ) ) ;
2009-02-11 23:15:22 +01:00
}
2009-02-14 22:33:28 +01:00
2011-10-13 20:53:06 +02:00
void simplify_constants2 ( ) {
2009-02-14 22:33:28 +01:00
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 " ;
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
Settings settings ;
2009-02-14 22:33:28 +01:00
// tokenize..
2010-12-01 18:00:55 +01:00
Tokenizer tokenizer ( & settings , this ) ;
2009-02-14 22:33:28 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyTokenList ( ) ;
2012-04-16 19:51:07 +02:00
ASSERT_EQUALS ( " void f ( Foo & foo , Foo * foo2 ) { foo . a = 90 ; foo2 . a = 45 ; } " , tokenizer . tokens ( ) - > stringifyList ( 0 , false ) ) ;
2009-02-14 22:33:28 +01:00
}
2009-03-13 22:38:42 +01:00
2011-10-13 20:53:06 +02:00
void simplify_constants3 ( ) {
2011-01-29 16:14:09 +01:00
const char code [ ] =
" static const char str[] = \" abcd \" ; \n "
" static const unsigned int SZ = sizeof(str); \n "
" void f() { \n "
" a = SZ; \n "
" } \n " ;
const char expected [ ] =
2011-12-04 16:46:03 +01:00
" const static char str [ 5 ] = \" abcd \" ; \n \n void f ( ) { \n a = 5 ; \n } " ;
2011-01-29 16:14:09 +01:00
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplify_null ( ) {
2012-08-25 12:00:25 +02:00
{
const char code [ ] =
" int * p = NULL; \n "
" int * q = __null; \n " ;
const char expected [ ] =
" int * p ; p = 0 ; \n int * q ; q = 0 ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
ASSERT_EQUALS ( " ( a == nullptr ) " , tokenizeAndStringify ( " (a==nullptr) " , false , false , Settings : : Unspecified , " test.c " ) ) ;
ASSERT_EQUALS ( " ( ! a ) " , tokenizeAndStringify ( " (a==nullptr) " , false , false , Settings : : Unspecified , " test.cpp " ) ) ;
2011-02-05 09:03:31 +01:00
}
2011-12-09 20:47:51 +01:00
void simplifyMulAndParens ( ) {
2011-05-18 07:25:30 +02:00
// (error) Resource leak
2011-12-09 20:47:51 +01:00
const char code [ ] = " void f() { "
" *&n1=open(); "
" *&(n2)=open(); "
" *(&n3)=open(); "
" *&*&n4=open(); "
" *&*&*&(n5)=open(); "
" *&*&(*&n6)=open(); "
" *&*(&*&n7)=open(); "
" *(&*&n8)=open(); "
" *&(*&*&(*&n9))=open(); "
" (n10) = open(); "
" ((n11)) = open(); "
" ((*&n12))=open(); "
" *(&(*&n13))=open(); "
" ((*&(*&n14)))=open(); "
" ((*&(*&n15)))+=10; "
" } " ;
const char expected [ ] = " void f ( ) { "
" n1 = open ( ) ; "
" n2 = open ( ) ; "
" n3 = open ( ) ; "
" n4 = open ( ) ; "
" n5 = open ( ) ; "
" n6 = open ( ) ; "
" n7 = open ( ) ; "
" n8 = open ( ) ; "
" n9 = open ( ) ; "
" n10 = open ( ) ; "
" n11 = open ( ) ; "
" n12 = open ( ) ; "
" n13 = open ( ) ; "
" n14 = open ( ) ; "
" n15 = n15 + 10 ; "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
2011-05-18 07:25:30 +02:00
}
2011-10-13 20:53:06 +02:00
void vardecl1 ( ) {
2009-03-16 19:03:23 +01:00
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
}
2011-10-13 20:53:06 +02:00
void vardecl2 ( ) {
2009-03-16 19:03:23 +01:00
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
2011-11-05 12:23:05 +01:00
ASSERT_EQUALS ( " void foo ( unsigned int a , unsigned int b ) { } " , actual ) ;
2009-03-16 19:03:23 +01:00
}
2009-04-20 20:38:05 +02:00
2011-10-13 20:53:06 +02:00
void vardecl3 ( ) {
2009-06-14 14:57:47 +02:00
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 ) ;
}
2011-10-13 20:53:06 +02:00
void vardecl4 ( ) {
2009-06-14 19:32:34 +02:00
// ticket #346
const char code1 [ ] = " void *p = NULL; " ;
2011-01-06 20:16:14 +01:00
const char res1 [ ] = " void * p ; p = 0 ; " ;
2009-06-14 19:32:34 +02:00
ASSERT_EQUALS ( res1 , tokenizeAndStringify ( code1 ) ) ;
const char code2 [ ] = " const void *p = NULL; " ;
2011-01-06 20:16:14 +01:00
const char res2 [ ] = " const void * p ; p = 0 ; " ;
2009-06-14 19:32:34 +02:00
ASSERT_EQUALS ( res2 , tokenizeAndStringify ( code2 ) ) ;
const char code3 [ ] = " void * const p = NULL; " ;
2011-01-06 20:16:14 +01:00
const char res3 [ ] = " void * const p ; p = 0 ; " ;
2009-06-14 19:32:34 +02:00
ASSERT_EQUALS ( res3 , tokenizeAndStringify ( code3 ) ) ;
const char code4 [ ] = " const void * const p = NULL; " ;
2011-01-06 20:16:14 +01:00
const char res4 [ ] = " const void * const p ; p = 0 ; " ;
2009-06-14 19:32:34 +02:00
ASSERT_EQUALS ( res4 , tokenizeAndStringify ( code4 ) ) ;
}
2011-10-13 20:53:06 +02:00
void vardecl_stl_1 ( ) {
2009-07-28 21:46:33 +02:00
// 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 ) ) ;
}
2011-10-13 20:53:06 +02:00
void vardecl_stl_2 ( ) {
2010-11-13 15:45:33 +01:00
const char code1 [ ] = " { std::string x = \" abc \" ; } " ;
2011-03-23 12:48:18 +01:00
ASSERT_EQUALS ( " { std :: string x ; x = \" abc \" ; } " , tokenizeAndStringify ( code1 ) ) ;
2010-11-13 15:45:33 +01:00
const char code2 [ ] = " { std::vector<int> x = y; } " ;
2011-03-24 02:15:49 +01:00
ASSERT_EQUALS ( " { std :: vector < int > x ; x = y ; } " , tokenizeAndStringify ( code2 ) ) ;
2010-11-13 15:45:33 +01:00
}
2012-04-05 08:53:10 +02:00
void vardecl_template_1 ( ) {
2009-12-05 21:15:14 +01:00
// ticket #1046
const char code1 [ ] = " b<(1<<24),10,24> u, v; " ;
2012-09-08 07:01:35 +02:00
const char res1 [ ] = " b < 16777216 , 10 , 24 > u ; b < 16777216 , 10 , 24 > v ; " ;
2009-12-05 21:15:14 +01:00
ASSERT_EQUALS ( res1 , tokenizeAndStringify ( code1 ) ) ;
2012-01-31 18:41:57 +01:00
// ticket #3571 (segmentation fault)
tokenizeAndStringify ( " template <int i = (3>4) > class X4 {}; " ) ;
2009-12-05 21:15:14 +01:00
}
2012-04-05 08:53:10 +02:00
void vardecl_template_2 ( ) {
// ticket #3650
const char code [ ] = " const string str = x<8,int>(); " ;
const char expected [ ] = " const string str = x < 8 , int > ( ) ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void vardecl_union ( ) {
2010-09-03 08:10:29 +02:00
// ticket #1976
const char code1 [ ] = " class Fred { public: union { int a ; int b ; } ; } ; " ;
ASSERT_EQUALS ( code1 , tokenizeAndStringify ( code1 ) ) ;
2010-09-09 20:15:00 +02:00
// ticket #2039
const char code2 [ ] = " void f() { \n "
" union { \n "
" int x; \n "
" long y; \n "
" }; \n "
" } " ;
ASSERT_EQUALS ( " void f ( ) { \n \n int x ; \n long & y = x ; \n \n } " , tokenizeAndStringify ( code2 ) ) ;
2012-07-08 13:59:00 +02:00
// ticket #3927
const char code3 [ ] = " union xy *p = NULL; " ;
ASSERT_EQUALS ( " union xy * p ; p = 0 ; " , tokenizeAndStringify ( code3 ) ) ;
2010-09-03 08:10:29 +02:00
}
2011-10-13 20:53:06 +02:00
void vardecl_par ( ) {
2011-05-07 14:23:14 +02:00
// ticket #2743 - set links if variable type contains parentheses
const char code [ ] = " Fred<int(*)()> fred1=a, fred2=b; " ;
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " , " " , false ) ;
ASSERT_EQUALS ( true , tokenizer . validate ( ) ) ;
}
2012-07-03 06:34:14 +02:00
void vardecl_par2 ( ) {
// ticket #3912 - set correct links
const char code [ ] = " function<void (shared_ptr<MyClass>)> v; " ;
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " , " " , false ) ;
ASSERT_EQUALS ( true , tokenizer . validate ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void vardec_static ( ) {
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
}
2011-10-13 20:53:06 +02:00
void vardecl6 ( ) {
2009-08-08 12:33:07 +02:00
// ticket #565
const char code1 [ ] = " int z = x >> 16; " ;
const char res1 [ ] = " int z ; z = x >> 16 ; " ;
ASSERT_EQUALS ( res1 , tokenizeAndStringify ( code1 ) ) ;
}
2011-10-13 20:53:06 +02:00
void vardecl7 ( ) {
2009-08-23 08:26:16 +02:00
// ticket #603
2010-08-25 22:10:21 +02:00
const char code [ ] = " void f() { \n "
" for (int c = 0; c < 0; ++c) {} \n "
" int t; \n "
" D(3 > t, \" T \" ); \n "
" } " ;
const char res [ ] = " void f ( ) { \n "
" for ( int c = 0 ; c < 0 ; ++ c ) { } \n "
2009-08-23 08:26:16 +02:00
" int t ; \n "
2010-08-25 22:10:21 +02:00
" D ( 3 > t , \" T \" ) ; \n "
" } " ;
2009-08-23 08:26:16 +02:00
ASSERT_EQUALS ( res , tokenizeAndStringify ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void vardecl8 ( ) {
2009-09-20 12:28:15 +02:00
// ticket #696
const char code [ ] = " char a[10]={' \\ 0'}, b[10]={' \\ 0'}; " ;
2011-01-06 20:16:14 +01:00
const char res [ ] = " char a [ 10 ] = { 0 } ; char b [ 10 ] = { 0 } ; " ;
2009-09-20 12:28:15 +02:00
ASSERT_EQUALS ( res , tokenizeAndStringify ( code ) ) ;
}
2009-08-23 08:26:16 +02:00
2011-10-13 20:53:06 +02:00
void vardecl9 ( ) {
2009-09-20 13:28:56 +02:00
const char code [ ] = " char a[2] = {'A', ' \\ 0'}, b[2] = {'B', ' \\ 0'}; " ;
2011-01-06 20:16:14 +01:00
const char res [ ] = " char a [ 2 ] = { 'A' , 0 } ; char b [ 2 ] = { 'B' , 0 } ; " ;
2009-09-20 13:28:56 +02:00
ASSERT_EQUALS ( res , tokenizeAndStringify ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void vardecl10 ( ) {
2009-09-26 12:02:13 +02:00
// ticket #732
const char code [ ] = " char a [ 2 ] = { '-' } ; memset ( a , '-' , sizeof ( a ) ) ; " ;
ASSERT_EQUALS ( code , tokenizeAndStringify ( code ) ) ;
2010-05-16 20:21:22 +02:00
}
2011-10-13 20:53:06 +02:00
void vardecl11 ( ) {
2010-05-16 20:21:22 +02:00
// ticket #1684
const char code [ ] = " char a[5][8], b[5][8]; " ;
ASSERT_EQUALS ( " char a [ 5 ] [ 8 ] ; char b [ 5 ] [ 8 ] ; " , tokenizeAndStringify ( code ) ) ;
2009-09-26 12:02:13 +02:00
}
2011-10-13 20:53:06 +02:00
void vardecl12 ( ) {
2010-08-25 20:17:31 +02:00
const char code [ ] = " struct A { public: B a, b, c, d; }; " ;
ASSERT_EQUALS ( " struct A { public: B a ; B b ; B c ; B d ; } ; " , tokenizeAndStringify ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void vardecl13 ( ) {
2010-09-15 19:53:47 +02:00
const char code [ ] = " void f() { \n "
" int a = (x < y) ? 1 : 0; \n "
" } " ;
ASSERT_EQUALS ( " void f ( ) { \n int a ; a = ( x < y ) ? 1 : 0 ; \n } " , tokenizeAndStringify ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void vardecl14 ( ) {
2011-03-25 04:06:20 +01:00
const char code [ ] = " ::std::tr1::shared_ptr<int> pNum1, pNum2; \n " ;
2012-08-25 21:57:45 +02:00
ASSERT_EQUALS ( " :: std :: tr1 :: shared_ptr < int > pNum1 ; :: std :: tr1 :: shared_ptr < int > pNum2 ; " , tokenizeAndStringify ( code , false , false , Settings : : Unspecified , " test.cpp " , false ) ) ;
2011-03-25 04:06:20 +01:00
}
2012-01-30 23:41:43 +01:00
void vardecl15 ( ) {
const char code [ ] = " const char x[] = \" foo \" , y[] = \" bar \" ; \n " ;
ASSERT_EQUALS ( " const char x [ 4 ] = \" foo \" ; const char y [ 4 ] = \" bar \" ; " , tokenizeAndStringify ( code ) ) ;
}
void vardecl16 ( ) {
const char code [ ] = " const a::b<c,d(e),f>::g::h<i>::l *x [] = foo(),y [][] = bar(); \n " ;
ASSERT_EQUALS ( " const a :: b < c , d ( e ) , f > :: g :: h < i > :: l * x [ ] = foo ( ) ; "
" const a :: b < c , d ( e ) , f > :: g :: h < i > :: l y [ ] [ ] = bar ( ) ; " , tokenizeAndStringify ( code ) ) ;
}
void vardecl17 ( ) {
const char code [ ] = " a < b > :: c :: d :: e < f > x = foo(), y = bar(); \n " ;
ASSERT_EQUALS ( " a < b > :: c :: d :: e < f > x ; x = foo ( ) ; "
" a < b > :: c :: d :: e < f > y ; y = bar ( ) ; " , tokenizeAndStringify ( code ) ) ;
}
2012-02-18 15:05:29 +01:00
void vardecl18 ( ) {
const char code [ ] = " void f() { \n "
" g((double)v1*v2, v3, v4); \n "
" } \n " ;
ASSERT_EQUALS ( " void f ( ) { \n "
" g ( ( double ) v1 * v2 , v3 , v4 ) ; \n "
" } " , tokenizeAndStringify ( code ) ) ;
}
2012-03-31 18:45:29 +02:00
void vardecl19 ( ) {
2012-04-03 19:48:58 +02:00
{
const char code [ ] = " void func(in, r, m) \n "
" int in; "
" int r,m; "
" { \n "
" } \n " ;
2012-03-31 18:45:29 +02:00
2012-04-03 19:48:58 +02:00
ASSERT_EQUALS ( " void func ( \n "
2012-04-16 19:51:07 +02:00
" int in , \n "
" int r , \n "
" int m ) \n "
2012-04-03 19:48:58 +02:00
" { \n "
" } " , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " void f(r,f) \n "
" char *r; \n "
" { \n "
" } \n " ;
ASSERT_EQUALS ( " void f ( \n "
2012-04-16 19:51:07 +02:00
" char * r ) \n "
2012-04-03 19:48:58 +02:00
" \n "
" { \n "
" } " , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " void f(f) \n "
" { \n "
" } \n " ;
ASSERT_EQUALS ( " void f ( ) \n "
" { \n "
" } " , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " void f(f,r) \n "
" char *r; \n "
" { \n "
" } \n " ;
ASSERT_EQUALS ( " void f ( \n "
2012-04-16 19:51:07 +02:00
" char * r ) \n "
2012-04-03 19:48:58 +02:00
" \n "
" { \n "
" } " , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " void f(r,f,s) \n "
" char *r; \n "
" char *s; \n "
" { \n "
" } \n " ;
ASSERT_EQUALS ( " void f ( \n "
2012-04-16 19:51:07 +02:00
" char * r , \n "
2012-04-03 19:48:58 +02:00
" \n "
2012-04-16 19:51:07 +02:00
" char * s ) \n "
2012-04-03 19:48:58 +02:00
" \n "
" \n "
" { \n "
" } " , tokenizeAndStringify ( code ) ) ;
}
2012-04-03 20:12:34 +02:00
{
const char code [ ] = " void f(r,s,t) \n "
" char *r,*s,*t; \n "
" { \n "
" } \n " ;
ASSERT_EQUALS ( " void f ( \n "
2012-04-16 19:51:07 +02:00
" char * r , \n "
" char * s , \n "
" char * t ) \n "
2012-04-03 20:12:34 +02:00
" \n "
" { \n "
" } " , tokenizeAndStringify ( code ) ) ;
}
2012-04-13 00:39:40 +02:00
{
const char code [ ] = " void f(a, b) register char *a, *b; \n "
" { \n "
" } \n " ;
ASSERT_EQUALS ( " void f ( char * a , char * b ) \n "
" { \n "
" } " , tokenizeAndStringify ( code ) ) ;
}
2012-03-31 18:45:29 +02:00
}
2012-04-09 12:55:26 +02:00
void vardecl20 ( ) {
// #3700
const char code [ ] = " void a::b() const \n "
" { \n "
" register const int X = 0; \n "
" } \n " ;
ASSERT_EQUALS ( " void a :: b ( ) const \n "
" { \n "
" const int X = 0 ; \n "
" } " , tokenizeAndStringify ( code ) ) ;
}
2012-09-14 16:46:45 +02:00
void vardecl21 ( ) { // #4042
const char code [ ] = " void f() { \n "
" a::b const *p = 0; \n "
" } \n " ;
ASSERT_EQUALS ( " void f ( ) { \n "
" a :: b const * p ; p = 0 ; \n "
" } "
, tokenizeAndStringify ( code ) ) ;
}
2012-09-17 19:45:42 +02:00
void vardecl22 ( ) { // #4211 - segmentation fault
tokenizeAndStringify ( " A<B<C<int>> >* p = 0; " ) ;
}
2011-10-13 20:53:06 +02:00
void volatile_variables ( ) {
2009-04-20 20:38:05 +02:00
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
2011-10-13 20:53:06 +02:00
void syntax_error ( ) {
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() {} " ;
2010-12-01 18:00:55 +01:00
Settings settings ;
Tokenizer tokenizer ( & settings , 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() {{} " ;
2010-12-01 18:00:55 +01:00
Settings settings ;
Tokenizer tokenizer ( & settings , 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()) {} " ;
2010-12-01 18:00:55 +01:00
Settings settings ;
Tokenizer tokenizer ( & settings , 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 " ;
2010-12-01 18:00:55 +01:00
Settings settings ;
Tokenizer tokenizer ( & settings , 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 " ;
2010-12-01 18:00:55 +01:00
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
2009-11-03 21:21:28 +01:00
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 " ;
2010-12-01 18:00:55 +01:00
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
2009-11-03 21:21:28 +01: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: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 " ;
2010-12-01 18:00:55 +01:00
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
2009-11-03 21:21:28 +01: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: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 " ;
2010-12-01 18:00:55 +01:00
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
2009-11-20 20:10:42 +01: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:2]: (error) Invalid number of character (() when these macros are defined: ''. \n " , errout . str ( ) ) ;
2010-08-25 21:57:57 +02:00
}
}
2011-10-13 20:53:06 +02:00
void syntax_error_templates_1 ( ) {
2010-08-25 21:57:57 +02:00
// ok code.. using ">" for a comparison
{
errout . str ( " " ) ;
std : : istringstream istr ( " x<y>z> xyz; \n " ) ;
2010-12-01 18:00:55 +01:00
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
2010-08-25 21:57:57 +02:00
tokenizer . tokenize ( istr , " test.cpp " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2010-08-26 07:43:00 +02:00
// ok code..
{
errout . str ( " " ) ;
std : : istringstream istr ( " template<class T> operator<(T a, T b) { } \n " ) ;
2010-12-01 18:00:55 +01:00
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
2010-08-26 07:43:00 +02:00
tokenizer . tokenize ( istr , " test.cpp " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2010-08-26 23:24:01 +02:00
// ok code (ticket #1984)..
{
errout . str ( " " ) ;
std : : istringstream istr ( " void f(a) int a; \n "
" { ;x<y; } " ) ;
2010-12-01 18:00:55 +01:00
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
2010-08-26 23:24:01 +02:00
tokenizer . tokenize ( istr , " test.cpp " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2010-08-26 23:19:18 +02:00
// ok code (ticket #1985)..
{
errout . str ( " " ) ;
std : : istringstream istr ( " void f() \n "
" try { ;x<y; } " ) ;
2010-12-01 18:00:55 +01:00
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
2010-08-26 23:19:18 +02:00
tokenizer . tokenize ( istr , " test.cpp " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-09 18:42:36 +02:00
// ok code (ticket #3183)
{
errout . str ( ) ;
std : : istringstream istr ( " MACRO(({ i < x })) " ) ;
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2010-08-25 21:57:57 +02:00
// bad code.. missing ">"
{
errout . str ( " " ) ;
std : : istringstream istr ( " x<y<int> xyz; \n " ) ;
2010-12-01 18:00:55 +01:00
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
2010-08-25 21:57:57 +02:00
tokenizer . tokenize ( istr , " test.cpp " ) ;
ASSERT_EQUALS ( " [test.cpp:1]: (error) syntax error \n " , errout . str ( ) ) ;
}
// bad code
{
errout . str ( " " ) ;
std : : istringstream istr ( " typedef \n "
" typename boost::mpl::if_c< \n "
" _visitableIndex < boost::mpl::size< typename _Visitables::ConcreteVisitables >::value \n "
" , ConcreteVisitable \n "
" , Dummy< _visitableIndex > \n "
" >::type ConcreteVisitableOrDummy; \n " ) ;
2010-12-01 18:00:55 +01:00
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
2010-08-25 21:57:57 +02:00
tokenizer . tokenize ( istr , " test.cpp " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) syntax error \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
2011-10-13 20:53:06 +02:00
void syntax_error_templates_2 ( ) {
2011-02-17 21:15:17 +01:00
std : : istringstream istr ( " template<> \n " ) ;
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ; // shouldn't segfault
}
2011-10-13 20:53:06 +02:00
void removeKeywords ( ) {
2009-05-27 20:49:29 +02:00
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
/**
2010-03-31 17:14:49 +02:00
* tokenize " signed i " = > " signed int i "
2009-09-28 20:25:05 +02:00
*/
2011-10-13 20:53:06 +02:00
void signed1 ( ) {
2009-09-28 20:25:05 +02:00
{
2010-03-31 17:14:49 +02:00
const char code1 [ ] = " void foo ( signed int , float ) ; " ;
ASSERT_EQUALS ( code1 , tokenizeAndStringify ( code1 ) ) ;
2009-09-28 20:25:05 +02:00
}
{
const char code1 [ ] = " signed i ; " ;
2010-03-31 17:14:49 +02:00
const char code2 [ ] = " signed int i ; " ;
2009-09-28 20:25:05 +02:00
ASSERT_EQUALS ( code2 , tokenizeAndStringify ( code1 ) ) ;
}
{
const char code1 [ ] = " signed int i ; " ;
2010-03-31 17:14:49 +02:00
ASSERT_EQUALS ( code1 , tokenizeAndStringify ( code1 ) ) ;
2009-09-28 20:25:05 +02:00
}
{
const char code1 [ ] = " int signed i ; " ;
2010-03-31 17:14:49 +02:00
const char code2 [ ] = " signed int i ; " ;
2009-09-28 20:25:05 +02:00
ASSERT_EQUALS ( code2 , tokenizeAndStringify ( code1 ) ) ;
}
{
2010-08-25 22:10:21 +02:00
const char code1 [ ] = " void f() { for (signed i=0; i<10; i++) {} } " ;
const char code2 [ ] = " void f ( ) { for ( signed int i = 0 ; i < 10 ; i ++ ) { } } " ;
2009-09-28 20:25:05 +02:00
ASSERT_EQUALS ( code2 , tokenizeAndStringify ( code1 ) ) ;
}
}
2009-06-06 10:40:48 +02:00
/**
* tokenize " unsigned i " = > " unsigned int i "
2010-03-31 17:14:49 +02:00
* tokenize " unsigned " = > " unsigned int "
2009-06-06 10:40:48 +02:00
*/
2011-10-13 20:53:06 +02:00
void unsigned1 ( ) {
2009-06-06 10:40:48 +02:00
// No changes..
{
2010-03-31 17:14:49 +02:00
const char code [ ] = " void foo ( unsigned int , float ) ; " ;
2009-06-06 10:40:48 +02:00
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"..
{
2010-08-25 22:10:21 +02:00
const char code1 [ ] = " void f() { for (unsigned i=0; i<10; i++) {} } " ;
const char code2 [ ] = " void f ( ) { for ( unsigned int i = 0 ; i < 10 ; i ++ ) { } } " ;
2009-08-05 20:15:48 +02:00
ASSERT_EQUALS ( code2 , tokenizeAndStringify ( code1 ) ) ;
}
2010-03-20 08:03:18 +01:00
// "extern unsigned x;" => "extern int x;"
{
const char code1 [ ] = " ; extern unsigned x; " ;
2010-03-31 17:14:49 +02:00
const char code2 [ ] = " ; extern unsigned int x ; " ;
2010-03-20 08:03:18 +01:00
ASSERT_EQUALS ( code2 , tokenizeAndStringify ( code1 ) ) ;
}
2009-06-06 10:40:48 +02:00
}
2011-10-13 20:53:06 +02:00
void unsigned2 ( ) {
2009-11-17 19:03:23 +01:00
const char code [ ] = " i = (unsigned)j; " ;
const char expected [ ] = " i = ( unsigned int ) j ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
2010-03-19 19:34:26 +01:00
// simplify "unsigned" when using templates..
2011-10-13 20:53:06 +02:00
void unsigned3 ( ) {
2010-03-19 19:34:26 +01:00
{
const char code [ ] = " ; foo<unsigned>(); " ;
const char expected [ ] = " ; foo<int> ( ) ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " ; foo<unsigned int>(); " ;
const char expected [ ] = " ; foo<int> ( ) ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
}
2011-10-13 20:53:06 +02:00
void createLinks ( ) {
2009-09-20 22:13:06 +02:00
{
const char code [ ] = " class A{ \n "
" void f() {} \n "
" }; " ;
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
2009-09-20 22:13:06 +02:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
const Token * tok = tokenizer . tokens ( ) ;
// A body {}
2011-11-20 14:22:39 +01:00
ASSERT_EQUALS ( true , tok - > linkAt ( 2 ) = = tok - > tokAt ( 9 ) ) ;
ASSERT_EQUALS ( true , tok - > linkAt ( 9 ) = = tok - > tokAt ( 2 ) ) ;
2009-09-20 22:13:06 +02:00
// f body {}
2011-11-20 14:22:39 +01:00
ASSERT_EQUALS ( true , tok - > linkAt ( 7 ) = = tok - > tokAt ( 8 ) ) ;
ASSERT_EQUALS ( true , tok - > linkAt ( 8 ) = = tok - > tokAt ( 7 ) ) ;
2009-09-20 22:13:06 +02:00
// f ()
2011-11-20 14:22:39 +01:00
ASSERT_EQUALS ( true , tok - > linkAt ( 5 ) = = tok - > tokAt ( 6 ) ) ;
ASSERT_EQUALS ( true , tok - > linkAt ( 6 ) = = tok - > tokAt ( 5 ) ) ;
2012-03-21 18:40:32 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-09-20 22:13:06 +02:00
}
{
const char code [ ] = " void f(){ \n "
" char a[10]; \n "
" char *b ; b = new char[a[0]]; \n "
" }; " ;
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
2009-09-20 22:13:06 +02:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
const Token * tok = tokenizer . tokens ( ) ;
// a[10]
2011-11-20 14:22:39 +01:00
ASSERT_EQUALS ( true , tok - > linkAt ( 7 ) = = tok - > tokAt ( 9 ) ) ;
ASSERT_EQUALS ( true , tok - > linkAt ( 9 ) = = tok - > tokAt ( 7 ) ) ;
2009-09-20 22:13:06 +02:00
// new char[]
2011-11-20 14:22:39 +01:00
ASSERT_EQUALS ( true , tok - > linkAt ( 19 ) = = tok - > tokAt ( 24 ) ) ;
ASSERT_EQUALS ( true , tok - > linkAt ( 24 ) = = tok - > tokAt ( 19 ) ) ;
2009-09-20 22:13:06 +02:00
// a[0]
2011-11-20 14:22:39 +01:00
ASSERT_EQUALS ( true , tok - > linkAt ( 21 ) = = tok - > tokAt ( 23 ) ) ;
ASSERT_EQUALS ( true , tok - > linkAt ( 23 ) = = tok - > tokAt ( 21 ) ) ;
2012-03-21 18:40:32 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-09-20 22:13:06 +02:00
}
{
const char code [ ] = " void f(){ \n "
" foo(g()); \n "
" }; " ;
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
2009-09-20 22:13:06 +02:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
const Token * tok = tokenizer . tokens ( ) ;
// foo(
2011-11-20 14:22:39 +01:00
ASSERT_EQUALS ( true , tok - > linkAt ( 6 ) = = tok - > tokAt ( 10 ) ) ;
ASSERT_EQUALS ( true , tok - > linkAt ( 10 ) = = tok - > tokAt ( 6 ) ) ;
2009-09-20 22:13:06 +02:00
// g(
2011-11-20 14:22:39 +01:00
ASSERT_EQUALS ( true , tok - > linkAt ( 8 ) = = tok - > tokAt ( 9 ) ) ;
ASSERT_EQUALS ( true , tok - > linkAt ( 9 ) = = tok - > tokAt ( 8 ) ) ;
2012-03-21 18:40:32 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-09-20 22:13:06 +02:00
}
2012-03-20 19:00:16 +01:00
{
const char code [ ] = " bool foo(C<z> a, bar<int, x<float>>& f, int b) { \n "
" return(a<b && b>f); \n "
" } " ;
errout . str ( " " ) ;
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
const Token * tok = tokenizer . tokens ( ) ;
// template<
ASSERT_EQUALS ( ( long long ) tok - > tokAt ( 6 ) , ( long long ) tok - > linkAt ( 4 ) ) ;
ASSERT_EQUALS ( ( long long ) tok - > tokAt ( 4 ) , ( long long ) tok - > linkAt ( 6 ) ) ;
// bar<
ASSERT_EQUALS ( ( long long ) tok - > tokAt ( 17 ) , ( long long ) tok - > linkAt ( 10 ) ) ;
ASSERT_EQUALS ( ( long long ) tok - > tokAt ( 10 ) , ( long long ) tok - > linkAt ( 17 ) ) ;
// x<
ASSERT_EQUALS ( ( long long ) tok - > tokAt ( 16 ) , ( long long ) tok - > linkAt ( 14 ) ) ;
ASSERT_EQUALS ( ( long long ) tok - > tokAt ( 14 ) , ( long long ) tok - > linkAt ( 16 ) ) ;
// a<b && b>f
ASSERT_EQUALS ( 0 , ( long long ) tok - > linkAt ( 28 ) ) ;
ASSERT_EQUALS ( 0 , ( long long ) tok - > linkAt ( 32 ) ) ;
2012-03-21 18:40:32 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " void foo() { \n "
" return static_cast<bar>(a); \n "
" } " ;
errout . str ( " " ) ;
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
const Token * tok = tokenizer . tokens ( ) ;
// static_cast<
ASSERT_EQUALS ( ( long long ) tok - > tokAt ( 9 ) , ( long long ) tok - > linkAt ( 7 ) ) ;
ASSERT_EQUALS ( ( long long ) tok - > tokAt ( 7 ) , ( long long ) tok - > linkAt ( 9 ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-03-20 19:00:16 +01:00
}
2012-09-09 15:31:23 +02:00
{
const char code [ ] = " void foo() { \n "
" nvwa<(x > y)> ERROR_nnn; \n "
" } " ;
errout . str ( " " ) ;
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
const Token * tok = tokenizer . tokens ( ) ;
// nvwa<(x > y)>
ASSERT_EQUALS ( ( long long ) tok - > tokAt ( 12 ) , ( long long ) tok - > linkAt ( 6 ) ) ;
ASSERT_EQUALS ( ( long long ) tok - > tokAt ( 6 ) , ( long long ) tok - > linkAt ( 12 ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2009-09-20 22:13:06 +02:00
}
2009-10-01 19:45:48 +02:00
2011-10-13 20:53:06 +02: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 ) ) ;
}
2011-10-13 20:53:06 +02:00
void removeExceptionSpecification2 ( ) {
2010-02-02 19:56:41 +01:00
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 "
" } ; " ;
2010-12-17 20:56:46 +01:00
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void removeExceptionSpecification3 ( ) {
2010-12-17 20:56:46 +01:00
const char code [ ] = " namespace A { \n "
" struct B { \n "
" B() throw () \n "
" { } \n "
" }; \n "
" }; \n " ;
const char expected [ ] = " namespace A { \n "
" struct B { \n "
" B ( ) \n "
" { } \n "
" } ; \n "
" } ; " ;
2010-02-02 19:56:41 +01:00
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
2011-12-06 20:36:23 +01:00
void removeExceptionSpecification4 ( ) {
const char code [ ] = " namespace { \n "
" void B() throw (); \n "
" }; " ;
const char expected [ ] = " namespace { \n "
" void B ( ) ; \n "
" } ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
void removeExceptionSpecification5 ( ) {
ASSERT_EQUALS ( " void foo ( struct S ) ; " ,
tokenizeAndStringify ( " void foo (struct S) throw(); " ) ) ;
ASSERT_EQUALS ( " void foo ( struct S , int ) ; " ,
tokenizeAndStringify ( " void foo (struct S, int) throw(); " ) ) ;
ASSERT_EQUALS ( " void foo ( int , struct S ) ; " ,
tokenizeAndStringify ( " void foo (int, struct S) throw(); " ) ) ;
ASSERT_EQUALS ( " void foo ( struct S1 , struct S2 ) ; " ,
tokenizeAndStringify ( " void foo (struct S1, struct S2) throw(); " ) ) ;
}
2009-10-04 07:51:12 +02:00
2011-10-13 20:53:06 +02:00
void gt ( ) {
2009-10-04 07:51:12 +02:00
ASSERT_EQUALS ( " ( i < 10 ) " , tokenizeAndStringify ( " (10>i) " ) ) ;
ASSERT_EQUALS ( " ; i < 10 ; " , tokenizeAndStringify ( " ;10>i; " ) ) ;
2011-03-09 21:25:44 +01:00
ASSERT_EQUALS ( " void > ( ) ; void > ( ) " ,
tokenizeAndStringify ( " void>(); void>() " ) ) ;
2009-10-04 07:51:12 +02:00
}
2009-10-09 21:11:29 +02:00
2011-10-13 20:53:06 +02:00
void simplifyString ( ) {
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
2009-12-23 12:17:48 +01:00
ASSERT_EQUALS ( " \" abc \" " , tokenizer . simplifyString ( " \" abc \" " ) ) ;
2012-09-13 16:44:10 +02:00
ASSERT_EQUALS ( " \" \n \" " , tokenizer . simplifyString ( " \" \\ xa \" " ) ) ;
ASSERT_EQUALS ( " \" 3 \" " , tokenizer . simplifyString ( " \" \\ x33 \" " ) ) ;
ASSERT_EQUALS ( " \" 33 \" " , tokenizer . simplifyString ( " \" \\ x333 \" " ) ) ;
ASSERT_EQUALS ( " \" a \" " , tokenizer . simplifyString ( " \" \\ x61 \" " ) ) ;
ASSERT_EQUALS ( " \" \n 1 \" " , tokenizer . simplifyString ( " \" \\ 0121 \" " ) ) ;
ASSERT_EQUALS ( " \" 3 \" " , tokenizer . simplifyString ( " \" \\ x33 \" " ) ) ;
ASSERT_EQUALS ( " \" 0 \" " , tokenizer . simplifyString ( " \" \\ 0400 \" " ) ) ;
ASSERT_EQUALS ( " \" \\ nhello \" " , tokenizer . simplifyString ( " \" \\ nhello \" " ) ) ;
ASSERT_EQUALS ( " \" aaa \" " , tokenizer . simplifyString ( " \" \\ x61 \\ x61 \\ x61 \" " ) ) ;
ASSERT_EQUALS ( " \" \n 1 \n 1 \n 1 \" " , tokenizer . simplifyString ( " \" \\ 0121 \\ 0121 \\ 0121 \" " ) ) ;
ASSERT_EQUALS ( " \" \\ \\ x61 \" " , tokenizer . simplifyString ( " \" \\ \\ x61 \" " ) ) ;
ASSERT_EQUALS ( " \" b \" " , tokenizer . simplifyString ( " \" \\ x62 \" " ) ) ;
ASSERT_EQUALS ( " \" 7 \" " , tokenizer . simplifyString ( " \" \\ 0407 \" " ) ) ;
// terminate a string at null character.
ASSERT_EQUALS ( " \" a \" " , tokenizer . simplifyString ( " \" a \\ 0 \" " ) ) ;
2009-10-09 21:11:29 +02:00
}
2011-10-13 20:53:06 +02:00
void simplifyConst ( ) {
2009-11-12 22:49:39 +01:00
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;} " ) ) ;
2011-12-04 16:46:03 +01:00
ASSERT_EQUALS ( " const int foo ( ) ; " , tokenizeAndStringify ( " int const foo (); " ) ) ;
2012-09-07 11:41:41 +02:00
ASSERT_EQUALS ( " const int x ; " , tokenizeAndStringify ( " int const x; " ) ) ;
ASSERT_EQUALS ( " const struct X x ; " , tokenizeAndStringify ( " struct X const x; " ) ) ;
2009-11-12 22:49:39 +01:00
}
2009-12-06 23:09:56 +01:00
2011-10-13 20:53:06 +02:00
void switchCase ( ) {
2011-10-19 14:20:09 +02:00
ASSERT_EQUALS ( " void foo ( int i ) { switch ( i ) { case -1 : ; break ; } } " ,
2009-12-06 23:09:56 +01:00
tokenizeAndStringify ( " void foo (int i) { switch(i) { case -1: break; } } " ) ) ;
2011-10-19 14:20:09 +02:00
//ticket #3227
ASSERT_EQUALS ( " void foo ( ) { switch ( n ) { label : ; case 1 : ; label1 : ; label2 : ; break ; } } " ,
tokenizeAndStringify ( " void foo(){ switch (n){ label: case 1: label1: label2: break; }} " ) ) ;
2009-12-06 23:09:56 +01:00
}
2010-01-20 21:19:06 +01:00
2011-10-16 08:09:57 +02:00
void simplifyPointerToStandardType ( ) {
// Pointer to standard type
ASSERT_EQUALS ( " char buf [ 100 ] ; readlink ( path , buf , 99 ) ; " ,
tokenizeAndStringify ( " char buf[100] ; readlink(path, &buf[0], 99); " ,
false , true , Settings : : Unspecified , " test.c " ) ) ;
// Simplification of unknown type - C only
ASSERT_EQUALS ( " foo data [ 100 ] ; something ( foo ) ; " ,
tokenizeAndStringify ( " foo data[100]; something(&foo[0]); " , false , true , Settings : : Unspecified , " test.c " ) ) ;
// C++: No pointer simplification
ASSERT_EQUALS ( " foo data [ 100 ] ; something ( & foo [ 0 ] ) ; " ,
tokenizeAndStringify ( " foo data[100]; something(&foo[0]); " ) ) ;
}
2011-10-13 20:53:06 +02:00
std : : string simplifyFunctionPointers ( const char code [ ] ) {
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
2010-01-20 21:19:06 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyFunctionPointers ( ) ;
2012-04-16 19:51:07 +02:00
return tokenizer . tokens ( ) - > stringifyList ( 0 , true ) ;
2010-01-20 21:19:06 +01:00
}
2011-10-13 20:53:06 +02:00
void functionpointer1 ( ) {
2012-04-16 19:51:07 +02:00
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-20 21:19:06 +01:00
}
2010-01-31 09:33:57 +01:00
2011-10-13 20:53:06 +02:00
void functionpointer2 ( ) {
2011-03-19 21:21:26 +01:00
const char code [ ] = " typedef void (* PF)(); "
" void f1 ( ) { } "
" PF pf = &f1; "
" PF pfs[] = { &f1, &f1 }; " ;
2012-04-16 19:51:07 +02:00
const char expected [ ] = " void f1 ( ) { } "
" void * pf ; pf = & f1 ; "
" void * pfs [ 2 ] = { & f1 , & f1 } ; " ;
2011-03-19 21:21:26 +01:00
ASSERT_EQUALS ( expected , simplifyFunctionPointers ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void functionpointer3 ( ) {
2011-07-22 07:58:53 +02:00
// Related with ticket #2873
const char code [ ] = " void f() { \n "
" (void)(xy(*p)(0);) "
" \n } " ;
2012-04-16 19:51:07 +02:00
const char expected [ ] = " void f ( ) { "
" ( void ) ( xy ( * p ) ( 0 ) ; ) "
2011-07-22 07:58:53 +02:00
" } " ;
ASSERT_EQUALS ( expected , simplifyFunctionPointers ( code ) ) ;
}
2011-11-09 21:02:41 +01:00
void functionpointer4 ( ) {
const char code [ ] = " "
2011-11-10 21:32:37 +01:00
" struct S \n "
" { \n "
" typedef void (*FP)(); \n "
" virtual FP getFP(); \n "
" virtual void execute(); \n "
" }; \n "
" void f() { \n "
" int a[9]; \n "
" } \n " ;
2011-11-09 21:02:41 +01:00
const char expected [ ] = " \n \n ##file 0 \n "
2011-11-10 21:32:37 +01:00
" 1: struct S \n "
" 2: { \n "
2011-12-04 15:40:05 +01:00
" 3: \n "
2011-11-10 21:32:37 +01:00
" 4: virtual void ( * getFP ( ) ) ( ) ; \n "
" 5: virtual void execute ( ) ; \n "
" 6: } ; \n "
" 7: void f ( ) { \n "
" 8: int a@1 [ 9 ] ; \n "
" 9: } \n " ;
2011-11-09 21:02:41 +01:00
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , false ) ) ;
}
2011-12-27 11:56:40 +01:00
void functionpointer5 ( ) {
const char code [ ] = " ;void (*fp[])(int a) = {0,0,0}; " ;
const char expected [ ] = " \n \n ##file 0 \n "
" 1: ; void * fp@1 [ ] = { 0 , 0 , 0 } ; \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void removeRedundantAssignment ( ) {
2012-09-04 13:41:14 +02:00
ASSERT_EQUALS ( " void f ( ) { } " , tokenizeAndStringify ( " void f() { int *p, *q; p = q; } " , true ) ) ;
ASSERT_EQUALS ( " void f ( ) { } " , tokenizeAndStringify ( " void f() { int *p = 0, *q; p = q; } " , true ) ) ;
2010-12-22 08:24:50 +01:00
ASSERT_EQUALS ( " int f ( int * x ) { return * x ; } " , tokenizeAndStringify ( " int f(int *x) { return *x; } " , true ) ) ;
2010-01-31 09:33:57 +01:00
}
2010-02-03 20:01:56 +01:00
2011-10-13 20:53:06 +02:00
void removedeclspec ( ) {
2010-02-03 20:01:56 +01:00
ASSERT_EQUALS ( " a b " , tokenizeAndStringify ( " a __declspec ( dllexport ) b " ) ) ;
2010-04-24 09:40:05 +02:00
ASSERT_EQUALS ( " int a ; " , tokenizeAndStringify ( " __declspec(thread) __declspec(align(32)) int a; " ) ) ;
ASSERT_EQUALS ( " int i ; " , tokenizeAndStringify ( " __declspec(allocate( \" mycode \" )) int i; " ) ) ;
ASSERT_EQUALS ( " struct IUnknown ; " , tokenizeAndStringify ( " struct __declspec(uuid( \" 00000000-0000-0000-c000-000000000046 \" )) IUnknown; " ) ) ;
ASSERT_EQUALS ( " int x [ ] ; " , tokenizeAndStringify ( " __declspec(property(get=GetX, put=PutX)) int x[]; " ) ) ;
2010-02-03 20:01:56 +01:00
}
2010-02-08 23:16:12 +01:00
2011-10-13 20:53:06 +02:00
void removeattribute ( ) {
2010-05-27 18:15:42 +02:00
ASSERT_EQUALS ( " short array [ 3 ] ; " , tokenizeAndStringify ( " short array[3] __attribute__ ((aligned)); " ) ) ;
ASSERT_EQUALS ( " int x [ 2 ] ; " , tokenizeAndStringify ( " int x[2] __attribute__ ((packed)); " ) ) ;
2011-04-24 03:04:02 +02:00
ASSERT_EQUALS ( " int vecint ; " , tokenizeAndStringify ( " int __attribute__((mode(SI))) __attribute__((vector_size (16))) vecint; " ) ) ;
2010-05-27 18:15:42 +02:00
}
2011-10-13 20:53:06 +02:00
void cpp0xtemplate1 ( ) {
2010-02-08 23:16:12 +01:00
const char * code = " template <class T> \n "
" void fn2 (T t = []{return 1;}()) \n "
" {} \n "
" int main() \n "
" { \n "
" fn2<int>(); \n "
" } \n " ;
2012-04-16 19:51:07 +02:00
ASSERT_EQUALS ( " int main ( ) \n { \n fn2<int> ( ) ; \n } void fn2<int> ( int t = [ ] { return 1 ; } ( ) ) \n { } " , tokenizeAndStringify ( code ) ) ;
2010-02-08 23:16:12 +01:00
}
2010-08-17 19:50:21 +02:00
2011-10-13 20:53:06 +02:00
void cpp0xtemplate2 ( ) {
2010-09-05 08:16:19 +02:00
// tokenize ">>" into "> >"
const char * code = " list<list<int>> ints; \n " ;
2012-03-20 19:00:16 +01:00
ASSERT_EQUALS ( " list < list < int > > ints ; " , tokenizeAndStringify ( code ) ) ;
2010-09-05 08:16:19 +02:00
}
2011-10-13 20:53:06 +02:00
void cpp0xtemplate3 ( ) {
2011-02-12 16:51:59 +01:00
// #2549
const char * code = " template<class T, T t = (T)0> \n "
" struct S \n "
" {}; \n "
" S<int> s; \n " ;
2012-07-29 16:01:05 +02:00
TODO_ASSERT_EQUALS ( " S<int,(int)0> s ; struct S<int,(int)0> { } ; " , // wanted result
" template < class T , T t > \n "
" struct S \n "
" { } ; \n "
2011-12-06 16:38:13 +01:00
" S < int , ( T ) 0 > s ; " , // current result
2011-02-12 16:51:59 +01:00
tokenizeAndStringify ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void cpp0xdefault ( ) {
2010-08-17 19:50:21 +02:00
{
const char * code = " struct foo { "
" foo() = default; "
" } " ;
ASSERT_EQUALS ( " struct foo { } " , tokenizeAndStringify ( code ) ) ;
}
2010-08-18 22:22:14 +02:00
{
const char * code = " struct A { "
" void operator delete (void *) = delete; "
" void operator delete[] (void *) = delete; "
" } " ;
ASSERT_EQUALS ( " struct A { } " , tokenizeAndStringify ( code ) ) ;
}
2010-11-20 17:20:25 +01:00
{
const char * code = " struct A { "
" void operator = (void *) = delete; "
" } " ;
ASSERT_EQUALS ( " struct A { } " , tokenizeAndStringify ( code ) ) ;
}
2010-08-17 19:50:21 +02:00
{
const char * code = " struct foo { "
" foo(); "
" } "
" foo::foo() = delete; " ;
2011-01-30 12:54:19 +01:00
TODO_ASSERT_EQUALS ( " struct foo { } " ,
" struct foo { foo ( ) ; } foo :: foo ( ) = delete ; " , tokenizeAndStringify ( code ) ) ;
2011-12-31 21:43:06 +01:00
}
//ticket #3448 (segmentation fault)
{
const char * code = " struct A { "
" void bar () = delete; "
" }; "
" void baz () = delete; " ;
ASSERT_EQUALS ( " struct A { } ; void baz ( ) = delete ; " , tokenizeAndStringify ( code ) ) ;
2010-08-17 19:50:21 +02:00
}
}
2010-02-20 18:13:09 +01:00
2011-10-13 20:53:06 +02:00
std : : string arraySize_ ( const std : : string & code ) {
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
Settings settings ;
2010-02-20 18:13:09 +01:00
// tokenize..
2010-12-01 18:00:55 +01:00
Tokenizer tokenizer ( & settings , this ) ;
2011-12-08 21:28:34 +01:00
std : : istringstream istr ( code ) ;
2010-02-20 18:13:09 +01:00
tokenizer . tokenize ( istr , " test.cpp " ) ;
std : : ostringstream ostr ;
2011-10-13 20:53:06 +02:00
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) ) {
2012-04-16 19:51:07 +02:00
if ( tok - > isName ( ) & & tok - > previous ( ) )
2010-02-20 18:13:09 +01:00
ostr < < " " ;
ostr < < tok - > str ( ) ;
}
return ostr . str ( ) ;
}
2011-10-13 20:53:06 +02:00
void arraySize ( ) {
2010-02-20 18:13:09 +01:00
ASSERT_EQUALS ( " ; int a[3]={1,2,3}; " , arraySize_ ( " ;int a[]={1,2,3}; " ) ) ;
2011-12-07 21:15:00 +01:00
ASSERT_EQUALS ( " ; int a[3]={1,2,3}; " , arraySize_ ( " ;int a[]={1,2,3,}; " ) ) ;
ASSERT_EQUALS ( " ; foo a[3]={{1,2},{3,4},{5,6}}; " , arraySize_ ( " ;foo a[]={{1,2},{3,4},{5,6}}; " ) ) ;
TODO_ASSERT_EQUALS ( " ; int a[1]={ foo< bar1, bar2>(123,4)}; " , " ; int a[]={ foo< bar1, bar2>(123,4)}; " , arraySize_ ( " ;int a[]={foo<bar1,bar2>(123,4)}; " ) ) ;
ASSERT_EQUALS ( " ; int a[2]={ b> c?1:2,3}; " , arraySize_ ( " ;int a[]={ b>c?1:2,3}; " ) ) ;
2012-04-16 19:51:07 +02:00
TODO_ASSERT_EQUALS ( " int main(){ int a[2]={ b< c?1:2,3}} " , " int main(){ int a[]={ b< c?1:2,3}} " , arraySize_ ( " int main(){int a[]={b<c?1:2,3}} " ) ) ;
2011-12-07 21:15:00 +01:00
ASSERT_EQUALS ( " ; int a[3]={ ABC,2,3}; " , arraySize_ ( " ;int a[]={ABC,2,3}; " ) ) ;
2010-02-20 18:13:09 +01:00
}
2010-02-21 09:47:41 +01:00
2011-10-13 20:53:06 +02:00
std : : string labels_ ( const std : : string & code ) {
2010-02-21 09:47:41 +01:00
// the arraySize_ does what we want currently..
return arraySize_ ( code ) ;
}
2011-10-13 20:53:06 +02:00
void labels ( ) {
2012-04-16 19:51:07 +02:00
ASSERT_EQUALS ( " void f(){ ab:; a=0;} " , labels_ ( " void f() { ab: a=0; } " ) ) ;
2011-10-08 21:13:53 +02:00
//ticket #3176
2012-04-16 19:51:07 +02:00
ASSERT_EQUALS ( " void f(){ ab:;(* func)();} " , labels_ ( " void f() { ab: (*func)(); } " ) ) ;
2011-10-08 21:13:53 +02:00
//with '*' operator
2012-04-16 19:51:07 +02:00
ASSERT_EQUALS ( " void f(){ ab:;* b=0;} " , labels_ ( " void f() { ab: *b=0; } " ) ) ;
ASSERT_EQUALS ( " void f(){ ab:;** b=0;} " , labels_ ( " void f() { ab: **b=0; } " ) ) ;
2011-10-08 21:13:53 +02:00
//with '&' operator
2012-04-16 19:51:07 +02:00
ASSERT_EQUALS ( " void f(){ ab:;& b=0;} " , labels_ ( " void f() { ab: &b=0; } " ) ) ;
ASSERT_EQUALS ( " void f(){ ab:;&( b. x)=0;} " , labels_ ( " void f() { ab: &(b->x)=0; } " ) ) ;
2011-10-08 21:13:53 +02:00
//with '(' parenthesis
2012-04-16 19:51:07 +02:00
ASSERT_EQUALS ( " void f(){ ab:;*(* b). x=0;} " , labels_ ( " void f() { ab: *(* b)->x=0; } " ) ) ;
ASSERT_EQUALS ( " void f(){ ab:;(** b). x=0;} " , labels_ ( " void f() { ab: (** b).x=0; } " ) ) ;
ASSERT_EQUALS ( " void f(){ ab:;&(* b. x)=0;} " , labels_ ( " void f() { ab: &(*b.x)=0; } " ) ) ;
2011-10-08 21:13:53 +02:00
//with '{' parenthesis
2012-04-16 19:51:07 +02:00
ASSERT_EQUALS ( " void f(){ ab:;{ b=0;}} " , labels_ ( " void f() { ab: {b=0;} } " ) ) ;
ASSERT_EQUALS ( " void f(){ ab:;{* b=0;}} " , labels_ ( " void f() { ab: { *b=0;} } " ) ) ;
ASSERT_EQUALS ( " void f(){ ab:;{& b=0;}} " , labels_ ( " void f() { ab: { &b=0;} } " ) ) ;
ASSERT_EQUALS ( " void f(){ ab:;{&(* b. x)=0;}} " , labels_ ( " void f() { ab: {&(*b.x)=0;} } " ) ) ;
2011-10-08 21:13:53 +02:00
//with unhandled MACRO() code
2012-04-16 19:51:07 +02:00
ASSERT_EQUALS ( " void f(){ MACRO( ab: b=0;, foo)} " , labels_ ( " void f() { MACRO(ab: b=0;, foo)} " ) ) ;
ASSERT_EQUALS ( " void f(){ MACRO( bar, ab:{&(* b. x)=0;})} " , labels_ ( " void f() { MACRO(bar, ab: {&(*b.x)=0;})} " ) ) ;
2012-01-09 14:37:20 +01:00
//don't crash with garbage code
2012-04-16 19:51:07 +02:00
ASSERT_EQUALS ( " switch(){ case} " , labels_ ( " switch(){case} " ) ) ;
2010-02-21 09:47:41 +01:00
}
2010-04-14 19:04:16 +02:00
2011-10-13 20:53:06 +02:00
void simplifyInitVar ( ) {
2010-04-14 19:04:16 +02:00
{
const char code [ ] = " int i ; int p(0); " ;
ASSERT_EQUALS ( " int i ; int p ; p = 0 ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " int i; int *p(0); " ;
ASSERT_EQUALS ( " int i ; int * p ; p = 0 ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " int p(0); " ;
ASSERT_EQUALS ( " int p ; p = 0 ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " int *p(0); " ;
ASSERT_EQUALS ( " int * p ; p = 0 ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " int i ; int p(i); " ;
2010-08-27 22:58:21 +02:00
ASSERT_EQUALS ( " int i ; int p ; p = i ; " , tokenizeAndStringify ( code ) ) ;
2010-04-14 19:04:16 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " int i; int *p(&i); " ;
ASSERT_EQUALS ( " int i ; int * p ; p = & i ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " int i; void *p(&i); " ;
ASSERT_EQUALS ( " int i ; void * p ; p = & i ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " struct S { }; struct S s; struct S *p(&s); " ;
ASSERT_EQUALS ( " struct S { } ; struct S s ; struct S * p ; p = & s ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " struct S { }; S s; S *p(&s); " ;
ASSERT_EQUALS ( " struct S { } ; S s ; S * p ; p = & s ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " union S { int i; float f; }; union S s; union S *p(&s); " ;
ASSERT_EQUALS ( " union S { int i ; float f ; } ; union S s ; union S * p ; p = & s ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " union S { int i; float f; }; S s; S *p(&s); " ;
ASSERT_EQUALS ( " union S { int i ; float f ; } ; S s ; S * p ; p = & s ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " class C { }; class C c; class C *p(&c); " ;
ASSERT_EQUALS ( " class C { } ; class C c ; class C * p ; p = & c ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " class C { }; C c; C *p(&c); " ;
ASSERT_EQUALS ( " class C { } ; C c ; C * p ; p = & c ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " struct S { }; struct S s; struct S s1(s); " ;
ASSERT_EQUALS ( " struct S { } ; struct S s ; struct S s1 ( s ) ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " struct S { }; S s; S s1(s); " ;
ASSERT_EQUALS ( " struct S { } ; S s ; S s1 ( s ) ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " struct S { }; struct S s; struct S s1(&s); " ;
ASSERT_EQUALS ( " struct S { } ; struct S s ; struct S s1 ( & s ) ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " struct S { }; S s; S s1(&s); " ;
ASSERT_EQUALS ( " struct S { } ; S s ; S s1 ( & s ) ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2010-04-15 18:42:11 +02:00
{
const char code [ ] = " class S { int function(); }; " ;
ASSERT_EQUALS ( " class S { int function ( ) ; } ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2010-04-14 19:04:16 +02:00
{
const char code [ ] = " class S { int function(void); }; " ;
2011-10-27 20:54:42 +02:00
ASSERT_EQUALS ( " class S { int function ( ) ; } ; " , tokenizeAndStringify ( code ) ) ;
2010-04-14 19:04:16 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " class S { int function(int); }; " ;
ASSERT_EQUALS ( " class S { int function ( int ) ; } ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " int function(void); " ;
2011-10-27 20:54:42 +02:00
ASSERT_EQUALS ( " int function ( ) ; " , tokenizeAndStringify ( code ) ) ;
2010-04-14 19:04:16 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " int function(int); " ;
ASSERT_EQUALS ( " int function ( int ) ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " extern int function(void); " ;
2011-10-27 20:54:42 +02:00
ASSERT_EQUALS ( " extern int function ( ) ; " , tokenizeAndStringify ( code ) ) ;
2010-04-14 19:04:16 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " int function1(void); int function2(void); " ;
2011-10-27 20:54:42 +02:00
ASSERT_EQUALS ( " int function1 ( ) ; int function2 ( ) ; " , tokenizeAndStringify ( code ) ) ;
2010-04-14 19:04:16 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2010-04-15 18:42:11 +02:00
{
const char code [ ] = " int function(A); " ;
// We can't tell if this a function prototype or a variable without knowing
// what A is. Since A is undefined, just leave it alone.
ASSERT_EQUALS ( " int function ( A ) ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " int i; int function(A); " ;
ASSERT_EQUALS ( " int i ; int function ( A ) ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " class A { } ; int foo(A); " ;
2010-08-27 22:58:21 +02:00
ASSERT_EQUALS ( " class A { } ; int foo ( A ) ; " , tokenizeAndStringify ( code ) ) ;
2010-04-15 18:42:11 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " class A { } ; A a; int foo(a); " ;
2010-08-27 22:58:21 +02:00
ASSERT_EQUALS ( " class A { } ; A a ; int foo ; foo = a ; " , tokenizeAndStringify ( code , false ) ) ;
2010-04-15 18:42:11 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-08-27 22:58:21 +02:00
}
{
const char code [ ] = " int x(f()); " ;
ASSERT_EQUALS ( " int x ; x = f ( ) ; " , tokenizeAndStringify ( code , false ) ) ;
2010-04-15 18:42:11 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2012-09-15 11:55:08 +02:00
{
const char code [ ] = " return doSomething(X), 0; " ;
ASSERT_EQUALS ( " return doSomething ( X ) , 0 ; " , tokenizeAndStringify ( code , false ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2010-04-14 19:04:16 +02:00
}
2010-08-15 11:54:28 +02:00
2011-10-13 20:53:06 +02:00
void bitfields1 ( ) {
2010-08-18 22:42:04 +02:00
const char code1 [ ] = " struct A { bool x : 1; }; " ;
ASSERT_EQUALS ( " struct A { bool x ; } ; " , tokenizeAndStringify ( code1 , false ) ) ;
const char code2 [ ] = " struct A { char x : 3; }; " ;
ASSERT_EQUALS ( " struct A { char x ; } ; " , tokenizeAndStringify ( code2 , false ) ) ;
const char code3 [ ] = " struct A { short x : 3; }; " ;
ASSERT_EQUALS ( " struct A { short x ; } ; " , tokenizeAndStringify ( code3 , false ) ) ;
const char code4 [ ] = " struct A { int x : 3; }; " ;
ASSERT_EQUALS ( " struct A { int x ; } ; " , tokenizeAndStringify ( code4 , false ) ) ;
const char code5 [ ] = " struct A { long x : 3; }; " ;
ASSERT_EQUALS ( " struct A { long x ; } ; " , tokenizeAndStringify ( code5 , false ) ) ;
2010-08-22 13:25:47 +02:00
const char code6 [ ] = " struct A { __int8 x : 3; }; " ;
ASSERT_EQUALS ( " struct A { char x ; } ; " , tokenizeAndStringify ( code6 , false ) ) ;
2010-08-18 22:42:04 +02:00
2010-08-22 13:25:47 +02:00
const char code7 [ ] = " struct A { __int16 x : 3; }; " ;
ASSERT_EQUALS ( " struct A { short x ; } ; " , tokenizeAndStringify ( code7 , false ) ) ;
2010-08-18 22:42:04 +02:00
2010-08-22 13:25:47 +02:00
const char code8 [ ] = " struct A { __int32 x : 3; }; " ;
ASSERT_EQUALS ( " struct A { int x ; } ; " , tokenizeAndStringify ( code8 , false ) ) ;
2010-08-18 22:42:04 +02:00
2010-08-22 13:25:47 +02:00
const char code9 [ ] = " struct A { __int64 x : 3; }; " ;
ASSERT_EQUALS ( " struct A { long long x ; } ; " , tokenizeAndStringify ( code9 , false ) ) ;
2010-08-18 22:42:04 +02:00
2010-08-22 13:25:47 +02:00
const char code10 [ ] = " struct A { unsigned char x : 3; }; " ;
ASSERT_EQUALS ( " struct A { unsigned char x ; } ; " , tokenizeAndStringify ( code10 , false ) ) ;
2010-08-16 18:51:25 +02:00
2010-08-22 13:25:47 +02:00
const char code11 [ ] = " struct A { unsigned short x : 3; }; " ;
ASSERT_EQUALS ( " struct A { unsigned short x ; } ; " , tokenizeAndStringify ( code11 , false ) ) ;
2010-08-18 22:42:04 +02:00
2010-08-22 13:25:47 +02:00
const char code12 [ ] = " struct A { unsigned int x : 3; }; " ;
ASSERT_EQUALS ( " struct A { unsigned int x ; } ; " , tokenizeAndStringify ( code12 , false ) ) ;
2010-08-18 22:42:04 +02:00
2010-08-22 13:25:47 +02:00
const char code13 [ ] = " struct A { unsigned long x : 3; }; " ;
ASSERT_EQUALS ( " struct A { unsigned long x ; } ; " , tokenizeAndStringify ( code13 , false ) ) ;
2010-08-22 09:41:22 +02:00
2010-08-22 13:25:47 +02:00
const char code14 [ ] = " struct A { unsigned __int8 x : 3; }; " ;
ASSERT_EQUALS ( " struct A { unsigned char x ; } ; " , tokenizeAndStringify ( code14 , false ) ) ;
2010-08-22 09:41:22 +02:00
2010-08-22 13:25:47 +02:00
const char code15 [ ] = " struct A { unsigned __int16 x : 3; }; " ;
ASSERT_EQUALS ( " struct A { unsigned short x ; } ; " , tokenizeAndStringify ( code15 , false ) ) ;
2010-08-22 09:41:22 +02:00
2010-08-22 13:25:47 +02:00
const char code16 [ ] = " struct A { unsigned __int32 x : 3; }; " ;
ASSERT_EQUALS ( " struct A { unsigned int x ; } ; " , tokenizeAndStringify ( code16 , false ) ) ;
const char code17 [ ] = " struct A { unsigned __int64 x : 3; }; " ;
ASSERT_EQUALS ( " struct A { unsigned long long x ; } ; " , tokenizeAndStringify ( code17 , false ) ) ;
const char code18 [ ] = " struct A { signed char x : 3; }; " ;
ASSERT_EQUALS ( " struct A { signed char x ; } ; " , tokenizeAndStringify ( code18 , false ) ) ;
const char code19 [ ] = " struct A { signed short x : 3; }; " ;
ASSERT_EQUALS ( " struct A { signed short x ; } ; " , tokenizeAndStringify ( code19 , false ) ) ;
const char code20 [ ] = " struct A { signed int x : 3; }; " ;
ASSERT_EQUALS ( " struct A { signed int x ; } ; " , tokenizeAndStringify ( code20 , false ) ) ;
const char code21 [ ] = " struct A { signed long x : 3; }; " ;
ASSERT_EQUALS ( " struct A { signed long x ; } ; " , tokenizeAndStringify ( code21 , false ) ) ;
const char code22 [ ] = " struct A { signed __int8 x : 3; }; " ;
ASSERT_EQUALS ( " struct A { signed char x ; } ; " , tokenizeAndStringify ( code22 , false ) ) ;
const char code23 [ ] = " struct A { signed __int16 x : 3; }; " ;
ASSERT_EQUALS ( " struct A { signed short x ; } ; " , tokenizeAndStringify ( code23 , false ) ) ;
const char code24 [ ] = " struct A { signed __int32 x : 3; }; " ;
ASSERT_EQUALS ( " struct A { signed int x ; } ; " , tokenizeAndStringify ( code24 , false ) ) ;
const char code25 [ ] = " struct A { signed __int64 x : 3; }; " ;
ASSERT_EQUALS ( " struct A { signed long long x ; } ; " , tokenizeAndStringify ( code25 , false ) ) ;
2010-08-18 22:42:04 +02:00
}
2011-10-13 20:53:06 +02:00
void bitfields2 ( ) {
2010-08-18 22:42:04 +02:00
const char code1 [ ] = " struct A { public: int x : 3; }; " ;
ASSERT_EQUALS ( " struct A { public: int x ; } ; " , tokenizeAndStringify ( code1 , false ) ) ;
const char code2 [ ] = " struct A { public: unsigned long x : 3; }; " ;
ASSERT_EQUALS ( " struct A { public: unsigned long x ; } ; " , tokenizeAndStringify ( code2 , false ) ) ;
const char code3 [ ] = " struct A { protected: int x : 3; }; " ;
ASSERT_EQUALS ( " struct A { protected: int x ; } ; " , tokenizeAndStringify ( code3 , false ) ) ;
const char code4 [ ] = " struct A { protected: unsigned long x : 3; }; " ;
ASSERT_EQUALS ( " struct A { protected: unsigned long x ; } ; " , tokenizeAndStringify ( code4 , false ) ) ;
const char code5 [ ] = " struct A { private: int x : 3; }; " ;
ASSERT_EQUALS ( " struct A { private: int x ; } ; " , tokenizeAndStringify ( code5 , false ) ) ;
const char code6 [ ] = " struct A { private: unsigned long x : 3; }; " ;
ASSERT_EQUALS ( " struct A { private: unsigned long x ; } ; " , tokenizeAndStringify ( code6 , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void bitfields3 ( ) {
2010-08-18 22:42:04 +02:00
const char code1 [ ] = " struct A { const int x : 3; }; " ;
ASSERT_EQUALS ( " struct A { const int x ; } ; " , tokenizeAndStringify ( code1 , false ) ) ;
const char code2 [ ] = " struct A { const unsigned long x : 3; }; " ;
ASSERT_EQUALS ( " struct A { const unsigned long x ; } ; " , tokenizeAndStringify ( code2 , false ) ) ;
const char code3 [ ] = " struct A { public: const int x : 3; }; " ;
ASSERT_EQUALS ( " struct A { public: const int x ; } ; " , tokenizeAndStringify ( code3 , false ) ) ;
const char code4 [ ] = " struct A { public: const unsigned long x : 3; }; " ;
ASSERT_EQUALS ( " struct A { public: const unsigned long x ; } ; " , tokenizeAndStringify ( code4 , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void bitfields4 ( ) { // ticket #1956
2010-08-21 16:34:41 +02:00
const char code1 [ ] = " struct A { CHAR x : 3; }; " ;
ASSERT_EQUALS ( " struct A { CHAR x ; } ; " , tokenizeAndStringify ( code1 , false ) ) ;
const char code2 [ ] = " struct A { UCHAR x : 3; }; " ;
ASSERT_EQUALS ( " struct A { UCHAR x ; } ; " , tokenizeAndStringify ( code2 , false ) ) ;
const char code3 [ ] = " struct A { BYTE x : 3; }; " ;
ASSERT_EQUALS ( " struct A { BYTE x ; } ; " , tokenizeAndStringify ( code3 , false ) ) ;
const char code4 [ ] = " struct A { WORD x : 3; }; " ;
ASSERT_EQUALS ( " struct A { WORD x ; } ; " , tokenizeAndStringify ( code4 , false ) ) ;
const char code5 [ ] = " struct A { DWORD x : 3; }; " ;
ASSERT_EQUALS ( " struct A { DWORD x ; } ; " , tokenizeAndStringify ( code5 , false ) ) ;
const char code6 [ ] = " struct A { LONG x : 3; }; " ;
ASSERT_EQUALS ( " struct A { LONG x ; } ; " , tokenizeAndStringify ( code6 , false ) ) ;
const char code7 [ ] = " struct A { UINT8 x : 3; }; " ;
ASSERT_EQUALS ( " struct A { UINT8 x ; } ; " , tokenizeAndStringify ( code7 , false ) ) ;
const char code8 [ ] = " struct A { UINT16 x : 3; }; " ;
ASSERT_EQUALS ( " struct A { UINT16 x ; } ; " , tokenizeAndStringify ( code8 , false ) ) ;
const char code9 [ ] = " struct A { UINT32 x : 3; }; " ;
ASSERT_EQUALS ( " struct A { UINT32 x ; } ; " , tokenizeAndStringify ( code9 , false ) ) ;
const char code10 [ ] = " struct A { UINT64 x : 3; }; " ;
ASSERT_EQUALS ( " struct A { UINT64 x ; } ; " , tokenizeAndStringify ( code10 , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void bitfields5 ( ) { // ticket #1956
2010-08-22 09:41:22 +02:00
const char code1 [ ] = " struct RGB { unsigned int r : 3, g : 3, b : 2; }; " ;
ASSERT_EQUALS ( " struct RGB { unsigned int r ; unsigned int g ; unsigned int b ; } ; " , tokenizeAndStringify ( code1 , false ) ) ;
2010-08-22 13:25:47 +02:00
const char code2 [ ] = " struct A { int a : 3; int : 3; int c : 3; }; " ;
ASSERT_EQUALS ( " struct A { int a ; int c ; } ; " , tokenizeAndStringify ( code2 , false ) ) ;
2010-08-23 07:29:05 +02:00
const char code3 [ ] = " struct A { virtual void f() {} int f1 : 1; }; " ;
ASSERT_EQUALS ( " struct A { virtual void f ( ) { } int f1 ; } ; " , tokenizeAndStringify ( code3 , false ) ) ;
2011-02-21 00:22:49 +01:00
}
2011-10-13 20:53:06 +02:00
void bitfields6 ( ) { // ticket #2595
2011-02-21 00:22:49 +01:00
const char code1 [ ] = " struct A { bool b : true; }; " ;
ASSERT_EQUALS ( " struct A { bool b ; } ; " , tokenizeAndStringify ( code1 , false ) ) ;
const char code2 [ ] = " struct A { bool b : true, c : true; }; " ;
ASSERT_EQUALS ( " struct A { bool b ; bool c ; } ; " , tokenizeAndStringify ( code2 , false ) ) ;
const char code3 [ ] = " struct A { bool : true; }; " ;
ASSERT_EQUALS ( " struct A { } ; " , tokenizeAndStringify ( code3 , false ) ) ;
2011-03-01 01:35:00 +01:00
const char code4 [ ] = " void f(int a) { switch (a) { case b: break; } } " ;
ASSERT_EQUALS ( " void f ( int a ) { switch ( a ) { case b : ; break ; } } " , tokenizeAndStringify ( code4 , true ) ) ;
const char code5 [ ] = " void f(int a) { switch (a) { default: break; } } " ;
ASSERT_EQUALS ( " void f ( int a ) { switch ( a ) { default : ; break ; } } " , tokenizeAndStringify ( code5 , true ) ) ;
2010-08-22 09:41:22 +02:00
}
2011-10-13 20:53:06 +02:00
void bitfields7 ( ) { // ticket #1987
2011-03-19 21:00:43 +01:00
const char code [ ] = " typedef struct Descriptor { "
" unsigned element_size: 8* sizeof( unsigned ); "
" } Descriptor; " ;
const char expected [ ] = " struct Descriptor { "
" unsigned int element_size ; "
" } ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , false ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void bitfields8 ( ) {
2011-03-22 01:17:14 +01:00
const char code [ ] = " struct A; "
" class B : virtual public C "
" { "
" int f(); "
" }; " ;
const char expected [ ] = " struct A ; "
" class B : virtual public C "
" { "
" int f ( ) ; "
" } ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , false ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void bitfields9 ( ) { // ticket #2706
2011-04-05 04:18:12 +02:00
const char code [ ] = " void f() { \n "
" goto half; \n "
" half: \n "
" { \n "
" ; \n "
" } \n "
" }; " ;
tokenizeAndStringify ( code , false ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void bitfields10 ( ) { // ticket #2737
2011-05-19 17:04:36 +02:00
const char code [ ] = " {} "
" MACRO "
" default: { } "
" ; " ;
ASSERT_EQUALS ( " { } MACRO default : { } ; " , tokenizeAndStringify ( code , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void bitfields11 ( ) { // ticket #2845 (segmentation fault)
2011-06-21 13:31:55 +02:00
const char code [ ] = " #if b&&a \n "
" #ifdef y z: \n " ;
tokenizeAndStringify ( code , false ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2012-01-09 16:24:11 +01:00
void bitfields12 ( ) { // ticket #3485 (segmentation fault)
const char code [ ] = " {a:1;}; \n " ;
ASSERT_EQUALS ( " { } ; " , tokenizeAndStringify ( code , false ) ) ;
}
2012-01-23 07:39:31 +01:00
void bitfields13 ( ) { // ticket #3502 (segmentation fault)
ASSERT_EQUALS ( " x y ; " , tokenizeAndStringify ( " struct{x y:}; \n " , false ) ) ;
}
2012-07-15 11:05:19 +02:00
void simplifyNamespaceStd ( ) {
static const char code1 [ ] = " map<foo, bar> m; " ; // namespace std is not used
ASSERT_EQUALS ( " map < foo , bar > m ; " , tokenizeAndStringify ( code1 , false ) ) ;
static const char code2 [ ] = " using namespace std; \n "
" map<foo, bar> m; " ;
ASSERT_EQUALS ( " std :: map < foo , bar > m ; " , tokenizeAndStringify ( code2 , false ) ) ;
static const char code3 [ ] = " using namespace std; \n "
" string s; " ;
ASSERT_EQUALS ( " std :: string s ; " , tokenizeAndStringify ( code3 , false ) ) ;
static const char code4 [ ] = " using namespace std; \n "
" void foo() {swap(a, b); } " ;
ASSERT_EQUALS ( " void foo ( ) { std :: swap ( a , b ) ; } " , tokenizeAndStringify ( code4 , false ) ) ;
static const char code5 [ ] = " using namespace std; \n "
" void foo() {map(a, b); } " ; // Thats obviously not std::map<>
ASSERT_EQUALS ( " void foo ( ) { map ( a , b ) ; } " , tokenizeAndStringify ( code5 , false ) ) ;
static const char code6 [ ] = " using namespace std; \n "
" string<wchar_t> s; " ; // Thats obviously not std::string
ASSERT_EQUALS ( " string < wchar_t > s ; " , tokenizeAndStringify ( code6 , false ) ) ;
static const char code7 [ ] = " using namespace std; \n "
" swap s; " ; // Thats obviously not std::swap
ASSERT_EQUALS ( " swap s ; " , tokenizeAndStringify ( code7 , false ) ) ;
static const char code8 [ ] = " using namespace std; \n "
" std::string s; " ;
ASSERT_EQUALS ( " std :: string s ; " , tokenizeAndStringify ( code8 , false ) ) ;
static const char code9 [ ] = " using namespace std; \n "
" tr1::function <void(int)> f; " ;
ASSERT_EQUALS ( " tr1 :: function < void ( int ) > f ; " , tokenizeAndStringify ( code9 , false , true , Settings : : Unspecified , " test.cpp " , false ) ) ;
ASSERT_EQUALS ( " std :: function < void ( int ) > f ; " , tokenizeAndStringify ( code9 , false , true , Settings : : Unspecified , " test.cpp " , true ) ) ;
static const char code10 [ ] = " std::tr1::function <void(int)> f; " ;
ASSERT_EQUALS ( " std :: tr1 :: function < void ( int ) > f ; " , tokenizeAndStringify ( code10 , false , true , Settings : : Unspecified , " test.cpp " , false ) ) ;
ASSERT_EQUALS ( " std :: function < void ( int ) > f ; " , tokenizeAndStringify ( code10 , false , true , Settings : : Unspecified , " test.cpp " , true ) ) ;
}
2011-10-13 20:53:06 +02:00
void microsoftMFC ( ) {
2010-08-18 22:42:04 +02:00
const char code1 [ ] = " class MyDialog : public CDialog { DECLARE_MESSAGE_MAP() private: CString text; }; " ;
2011-09-24 20:51:03 +02:00
ASSERT_EQUALS ( " class MyDialog : public CDialog { private: CString text ; } ; " , tokenizeAndStringify ( code1 , false , true , Settings : : Win32A ) ) ;
2010-08-21 07:55:14 +02:00
const char code2 [ ] = " class MyDialog : public CDialog { DECLARE_DYNAMIC(MyDialog) private: CString text; }; " ;
2011-09-24 20:51:03 +02:00
ASSERT_EQUALS ( " class MyDialog : public CDialog { private: CString text ; } ; " , tokenizeAndStringify ( code2 , false , true , Settings : : Win32A ) ) ;
2010-08-22 09:38:05 +02:00
const char code3 [ ] = " class MyDialog : public CDialog { DECLARE_DYNCREATE(MyDialog) private: CString text; }; " ;
2011-09-24 20:51:03 +02:00
ASSERT_EQUALS ( " class MyDialog : public CDialog { private: CString text ; } ; " , tokenizeAndStringify ( code3 , false , true , Settings : : Win32A ) ) ;
2010-08-23 21:22:11 +02:00
const char code4 [ ] = " class MyDialog : public CDialog { DECLARE_DYNAMIC_CLASS(MyDialog) private: CString text; }; " ;
2011-09-24 20:51:03 +02:00
ASSERT_EQUALS ( " class MyDialog : public CDialog { private: CString text ; } ; " , tokenizeAndStringify ( code4 , false , true , Settings : : Win32A ) ) ;
2010-08-15 11:54:28 +02:00
}
2010-08-31 21:40:51 +02:00
2011-10-13 20:53:06 +02:00
void microsoftMemory ( ) {
2012-03-27 21:24:46 +02:00
const char code1a [ ] = " void foo() { int a[10], b[10]; CopyMemory(a, b, sizeof(a)); } " ;
ASSERT_EQUALS ( " void foo ( ) { int a [ 10 ] ; int b [ 10 ] ; memcpy ( a , b , sizeof ( a ) ) ; } " , tokenizeAndStringify ( code1a , false , true , Settings : : Win32A ) ) ;
const char code1b [ ] = " void foo() { int a[10], b[10]; RtlCopyMemory(a, b, sizeof(a)); } " ;
ASSERT_EQUALS ( " void foo ( ) { int a [ 10 ] ; int b [ 10 ] ; memcpy ( a , b , sizeof ( a ) ) ; } " , tokenizeAndStringify ( code1b , false , true , Settings : : Win32A ) ) ;
const char code1c [ ] = " void foo() { int a[10], b[10]; RtlCopyBytes(a, b, sizeof(a)); } " ;
ASSERT_EQUALS ( " void foo ( ) { int a [ 10 ] ; int b [ 10 ] ; memcpy ( a , b , sizeof ( a ) ) ; } " , tokenizeAndStringify ( code1c , false , true , Settings : : Win32A ) ) ;
const char code2a [ ] = " void foo() { int a[10]; FillMemory(a, sizeof(a), 255); } " ;
ASSERT_EQUALS ( " void foo ( ) { int a [ 10 ] ; memset ( a , 255 , sizeof ( a ) ) ; } " , tokenizeAndStringify ( code2a , false , true , Settings : : Win32A ) ) ;
const char code2b [ ] = " void foo() { int a[10]; RtlFillMemory(a, sizeof(a), 255); } " ;
ASSERT_EQUALS ( " void foo ( ) { int a [ 10 ] ; memset ( a , 255 , sizeof ( a ) ) ; } " , tokenizeAndStringify ( code2b , false , true , Settings : : Win32A ) ) ;
const char code2c [ ] = " void foo() { int a[10]; RtlFillBytes(a, sizeof(a), 255); } " ;
ASSERT_EQUALS ( " void foo ( ) { int a [ 10 ] ; memset ( a , 255 , sizeof ( a ) ) ; } " , tokenizeAndStringify ( code2c , false , true , Settings : : Win32A ) ) ;
const char code3a [ ] = " void foo() { int a[10], b[10]; MoveMemory(a, b, sizeof(a)); } " ;
ASSERT_EQUALS ( " void foo ( ) { int a [ 10 ] ; int b [ 10 ] ; memmove ( a , b , sizeof ( a ) ) ; } " , tokenizeAndStringify ( code3a , false , true , Settings : : Win32A ) ) ;
const char code3b [ ] = " void foo() { int a[10], b[10]; RtlMoveMemory(a, b, sizeof(a)); } " ;
ASSERT_EQUALS ( " void foo ( ) { int a [ 10 ] ; int b [ 10 ] ; memmove ( a , b , sizeof ( a ) ) ; } " , tokenizeAndStringify ( code3b , false , true , Settings : : Win32A ) ) ;
const char code4a [ ] = " void foo() { int a[10]; ZeroMemory(a, sizeof(a)); } " ;
ASSERT_EQUALS ( " void foo ( ) { int a [ 10 ] ; memset ( a , 0 , sizeof ( a ) ) ; } " , tokenizeAndStringify ( code4a , false , true , Settings : : Win32A ) ) ;
const char code4b [ ] = " void foo() { int a[10]; RtlZeroMemory(a, sizeof(a)); } " ;
ASSERT_EQUALS ( " void foo ( ) { int a [ 10 ] ; memset ( a , 0 , sizeof ( a ) ) ; } " , tokenizeAndStringify ( code4b , false , true , Settings : : Win32A ) ) ;
const char code4c [ ] = " void foo() { int a[10]; RtlZeroBytes(a, sizeof(a)); } " ;
ASSERT_EQUALS ( " void foo ( ) { int a [ 10 ] ; memset ( a , 0 , sizeof ( a ) ) ; } " , tokenizeAndStringify ( code4c , false , true , Settings : : Win32A ) ) ;
const char code4d [ ] = " void foo() { int a[10]; RtlSecureZeroMemory(a, sizeof(a)); } " ;
ASSERT_EQUALS ( " void foo ( ) { int a [ 10 ] ; memset ( a , 0 , sizeof ( a ) ) ; } " , tokenizeAndStringify ( code4d , false , true , Settings : : Win32A ) ) ;
const char code5 [ ] = " void foo() { int a[10], b[10]; RtlCompareMemory(a, b, sizeof(a)); } " ;
ASSERT_EQUALS ( " void foo ( ) { int a [ 10 ] ; int b [ 10 ] ; memcmp ( a , b , sizeof ( a ) ) ; } " , tokenizeAndStringify ( code5 , false , true , Settings : : Win32A ) ) ;
const char code6 [ ] = " void foo() { ZeroMemory(f(1, g(a, b)), h(i, j(0, 1))); } " ;
ASSERT_EQUALS ( " void foo ( ) { memset ( f ( 1 , g ( a , b ) ) , 0 , h ( i , j ( 0 , 1 ) ) ) ; } " , tokenizeAndStringify ( code6 , false , true , Settings : : Win32A ) ) ;
const char code7 [ ] = " void foo() { FillMemory(f(1, g(a, b)), h(i, j(0, 1)), 255); } " ;
ASSERT_EQUALS ( " void foo ( ) { memset ( f ( 1 , g ( a , b ) ) , 255 , h ( i , j ( 0 , 1 ) ) ) ; } " , tokenizeAndStringify ( code7 , false , true , Settings : : Win32A ) ) ;
2011-09-23 01:59:56 +02:00
}
2011-10-13 20:53:06 +02:00
void borland ( ) {
2010-09-01 18:10:12 +02:00
// __closure
ASSERT_EQUALS ( " int * a ; " ,
tokenizeAndStringify ( " int (__closure *a)(); " , false ) ) ;
// __property
2010-09-07 18:37:43 +02:00
ASSERT_EQUALS ( " class Fred { ; __property ; } ; " ,
2010-09-01 18:10:12 +02:00
tokenizeAndStringify ( " class Fred { __property int x = { } }; " , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void Qt ( ) {
2010-12-03 08:14:09 +01:00
const char code1 [ ] = " class Counter : public QObject "
" { "
" Q_OBJECT "
" public: "
" Counter() { m_value = 0; } "
" int value() const { return m_value; } "
" public slots: "
" void setValue(int value); "
" signals: "
" void valueChanged(int newValue); "
" private: "
" int m_value; "
2011-08-26 02:54:35 +02:00
" }; "
" void Counter::setValue(int value) "
" { "
" if (value != m_value) { "
" m_value = value; "
" emit valueChanged(value); "
" } "
" } " ;
2010-12-03 08:14:09 +01:00
const char result1 [ ] = " class Counter : public QObject "
" { "
" public: "
" Counter ( ) { m_value = 0 ; } "
" int value ( ) const { return m_value ; } "
" public: "
" void setValue ( int value ) ; "
" protected: "
" void valueChanged ( int newValue ) ; "
" private: "
" int m_value ; "
2011-08-26 02:54:35 +02:00
" } ; "
" void Counter :: setValue ( int value ) "
" { "
" if ( value != m_value ) { "
" m_value = value ; "
" valueChanged ( value ) ; "
" } "
" } " ;
2010-12-03 08:14:09 +01:00
ASSERT_EQUALS ( result1 , tokenizeAndStringify ( code1 , false ) ) ;
const char code2 [ ] = " class Counter : public QObject "
" { "
" Q_OBJECT "
" public: "
" Counter() { m_value = 0; } "
" int value() const { return m_value; } "
" public Q_SLOTS: "
" void setValue(int value); "
" Q_SIGNALS: "
" void valueChanged(int newValue); "
" private: "
" int m_value; "
2011-08-26 02:54:35 +02:00
" }; "
" void Counter::setValue(int value) "
" { "
" if (value != m_value) { "
" m_value = value; "
" emit valueChanged(value); "
" } "
" } " ;
2010-12-03 08:14:09 +01:00
const char result2 [ ] = " class Counter : public QObject "
" { "
" public: "
" Counter ( ) { m_value = 0 ; } "
" int value ( ) const { return m_value ; } "
" public: "
" void setValue ( int value ) ; "
" protected: "
" void valueChanged ( int newValue ) ; "
" private: "
" int m_value ; "
2011-08-26 02:54:35 +02:00
" } ; "
" void Counter :: setValue ( int value ) "
" { "
" if ( value != m_value ) { "
" m_value = value ; "
" valueChanged ( value ) ; "
" } "
" } " ;
2010-12-03 08:14:09 +01:00
ASSERT_EQUALS ( result2 , tokenizeAndStringify ( code2 , false ) ) ;
2011-09-16 22:59:43 +02:00
const char code3 [ ] = " class MyObject : public QObject { "
" MyObject() {} "
" ~MyObject() {} "
" public slots: "
" signals: "
" void test() {} "
" }; " ;
const char result3 [ ] = " class MyObject : public QObject { "
" MyObject ( ) { } "
" ~ MyObject ( ) { } "
" public: "
" protected: "
" void test ( ) { } "
" } ; " ;
ASSERT_EQUALS ( result3 , tokenizeAndStringify ( code3 , false ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-12-02 17:41:49 +01:00
}
2011-10-13 20:53:06 +02:00
void sql ( ) {
2010-08-31 21:40:51 +02:00
// Oracle PRO*C extensions for inline SQL. Just replace the SQL with "asm()" to fix wrong error messages
// ticket: #1959
2011-12-13 21:42:38 +01:00
ASSERT_EQUALS ( " asm ( \" \" EXEC SQL SELECT A FROM B \" \" ) ; " , tokenizeAndStringify ( " EXEC SQL SELECT A FROM B; " , false ) ) ;
ASSERT_EQUALS ( " asm ( \" \" EXEC SQL \" \" ) ; " , tokenizeAndStringify ( " EXEC SQL " , false ) ) ;
2010-08-31 21:40:51 +02:00
}
2010-09-09 17:43:09 +02:00
2011-10-13 20:53:06 +02:00
void simplifyLogicalOperators ( ) {
2012-01-15 14:50:01 +01:00
ASSERT_EQUALS ( " if ( a && b ) { ; } " , tokenizeAndStringify ( " if (a and b); " ) ) ;
ASSERT_EQUALS ( " if ( a || b ) { ; } " , tokenizeAndStringify ( " if (a or b); " ) ) ;
ASSERT_EQUALS ( " if ( a & b ) { ; } " , tokenizeAndStringify ( " if (a bitand b); " ) ) ;
ASSERT_EQUALS ( " if ( a | b ) { ; } " , tokenizeAndStringify ( " if (a bitor b); " ) ) ;
ASSERT_EQUALS ( " if ( a ^ b ) { ; } " , tokenizeAndStringify ( " if (a xor b); " ) ) ;
ASSERT_EQUALS ( " if ( ~ b ) { ; } " , tokenizeAndStringify ( " if (compl b); " ) ) ;
ASSERT_EQUALS ( " if ( ! b ) { ; } " , tokenizeAndStringify ( " if (not b); " ) ) ;
ASSERT_EQUALS ( " if ( a != b ) { ; } " , tokenizeAndStringify ( " if (a not_eq b); " ) ) ;
2010-09-09 17:43:09 +02:00
}
2010-09-09 19:40:36 +02:00
2011-10-13 20:53:06 +02:00
void simplifyCalculations ( ) {
2011-08-06 23:53:06 +02:00
ASSERT_EQUALS ( " void foo ( char str [ ] ) { char x ; x = * str ; } " ,
tokenizeAndStringify ( " void foo ( char str [ ] ) { char x = 0 | ( * str ) ; } " , true ) ) ;
2011-08-06 06:50:29 +02:00
ASSERT_EQUALS ( " void foo ( ) { if ( b ) { } } " ,
tokenizeAndStringify ( " void foo ( ) { if (b | 0) { } } " , true ) ) ;
ASSERT_EQUALS ( " void foo ( ) { if ( b ) { } } " ,
tokenizeAndStringify ( " void foo ( ) { if (0 | b) { } } " , true ) ) ;
ASSERT_EQUALS ( " void foo ( int b ) { int a ; a = b ; bar ( a ) ; } " ,
tokenizeAndStringify ( " void foo ( int b ) { int a = b | 0 ; bar ( a ) ; } " , true ) ) ;
ASSERT_EQUALS ( " void foo ( int b ) { int a ; a = b ; bar ( a ) ; } " ,
tokenizeAndStringify ( " void foo ( int b ) { int a = 0 | b ; bar ( a ) ; } " , true ) ) ;
2011-09-10 13:28:54 +02:00
// ticket #3093
2011-12-02 23:01:55 +01:00
ASSERT_EQUALS ( " int f ( ) { return 15 ; } " ,
2011-09-10 13:28:54 +02:00
tokenizeAndStringify ( " int f() { int a = 10; int b = 5; return a + b; } " , true ) ) ;
ASSERT_EQUALS ( " int f ( ) { return a ; } " ,
tokenizeAndStringify ( " int f() { return a * 1; } " , true ) ) ;
ASSERT_EQUALS ( " int f ( int a ) { return 0 ; } " ,
tokenizeAndStringify ( " int f(int a) { return 0 * a; } " , true ) ) ;
2011-09-11 00:44:40 +02:00
ASSERT_EQUALS ( " bool f ( int i ) { switch ( i ) { case 15 : ; return true ; } } " ,
tokenizeAndStringify ( " bool f(int i) { switch (i) { case 10 + 5: return true; } } " , true ) ) ;
2012-01-14 00:43:58 +01:00
// ticket #3512 - Don't crash on garbage code
ASSERT_EQUALS ( " p = const " , tokenizeAndStringify ( " 1 *p = const " , true ) ) ;
2012-03-07 20:31:23 +01:00
// ticket #3576 - False positives in boolean expressions
ASSERT_EQUALS ( " int foo ( ) { return 1 ; } " ,
tokenizeAndStringify ( " int foo ( ) { int i; int j; i = 1 || j; return i; } " , true ) ) ;
ASSERT_EQUALS ( " int foo ( ) { return 0 ; } " ,
2012-06-14 21:47:03 +02:00
tokenizeAndStringify ( " int foo ( ) { int i; int j; i = 0 && j; return i; } " , true ) ) ; // ticket #3576 - False positives in boolean expressions
// ticket #3723 - Simplify condition (0 && a < 123)
ASSERT_EQUALS ( " ( 0 ) " ,
tokenizeAndStringify ( " ( 0 && a < 123 ) " , true ) ) ;
2012-08-20 18:10:32 +02:00
// ticket #3964 - simplify numeric calculations in tokenization
ASSERT_EQUALS ( " char a [ 10 ] ; " , tokenizeAndStringify ( " char a[9+1]; " ) ) ;
2011-08-06 06:50:29 +02:00
}
2011-10-13 20:53:06 +02:00
void simplifyCompoundAssignment ( ) {
2010-10-31 08:47:13 +01:00
ASSERT_EQUALS ( " ; x = x + y ; " , tokenizeAndStringify ( " ; x += y; " ) ) ;
ASSERT_EQUALS ( " ; x = x - y ; " , tokenizeAndStringify ( " ; x -= y; " ) ) ;
ASSERT_EQUALS ( " ; x = x * y ; " , tokenizeAndStringify ( " ; x *= y; " ) ) ;
ASSERT_EQUALS ( " ; x = x / y ; " , tokenizeAndStringify ( " ; x /= y; " ) ) ;
ASSERT_EQUALS ( " ; x = x % y ; " , tokenizeAndStringify ( " ; x %= y; " ) ) ;
ASSERT_EQUALS ( " ; x = x & y ; " , tokenizeAndStringify ( " ; x &= y; " ) ) ;
ASSERT_EQUALS ( " ; x = x | y ; " , tokenizeAndStringify ( " ; x |= y; " ) ) ;
ASSERT_EQUALS ( " ; x = x ^ y ; " , tokenizeAndStringify ( " ; x ^= y; " ) ) ;
ASSERT_EQUALS ( " ; x = x << y ; " , tokenizeAndStringify ( " ; x <<= y; " ) ) ;
ASSERT_EQUALS ( " ; x = x >> y ; " , tokenizeAndStringify ( " ; x >>= y; " ) ) ;
2010-11-01 17:36:54 +01:00
ASSERT_EQUALS ( " { x = x + y ; } " , tokenizeAndStringify ( " { x += y;} " ) ) ;
ASSERT_EQUALS ( " { x = x - y ; } " , tokenizeAndStringify ( " { x -= y;} " ) ) ;
ASSERT_EQUALS ( " { x = x * y ; } " , tokenizeAndStringify ( " { x *= y;} " ) ) ;
ASSERT_EQUALS ( " { x = x / y ; } " , tokenizeAndStringify ( " { x /= y;} " ) ) ;
ASSERT_EQUALS ( " { x = x % y ; } " , tokenizeAndStringify ( " { x %= y;} " ) ) ;
ASSERT_EQUALS ( " { x = x & y ; } " , tokenizeAndStringify ( " { x &= y;} " ) ) ;
ASSERT_EQUALS ( " { x = x | y ; } " , tokenizeAndStringify ( " { x |= y;} " ) ) ;
ASSERT_EQUALS ( " { x = x ^ y ; } " , tokenizeAndStringify ( " { x ^= y;} " ) ) ;
ASSERT_EQUALS ( " { x = x << y ; } " , tokenizeAndStringify ( " { x <<= y;} " ) ) ;
ASSERT_EQUALS ( " { x = x >> y ; } " , tokenizeAndStringify ( " { x >>= y;} " ) ) ;
2010-11-01 19:31:30 +01:00
2010-11-04 20:16:17 +01:00
ASSERT_EQUALS ( " ; * p = * p + y ; " , tokenizeAndStringify ( " ; *p += y; " ) ) ;
2011-02-16 20:33:24 +01:00
ASSERT_EQUALS ( " ; ( * p ) = ( * p ) + y ; " , tokenizeAndStringify ( " ; (*p) += y; " ) ) ;
2010-11-23 20:35:08 +01:00
ASSERT_EQUALS ( " ; * ( p [ 0 ] ) = * ( p [ 0 ] ) + y ; " , tokenizeAndStringify ( " ; *(p[0]) += y; " ) ) ;
2010-11-04 20:16:17 +01:00
2010-11-01 19:31:30 +01:00
ASSERT_EQUALS ( " case 0 : x = x + y ; break ; " , tokenizeAndStringify ( " case 0: x += y; break; " ) ) ;
2010-11-01 20:33:55 +01:00
ASSERT_EQUALS ( " ; x . y = x . y + 1 ; " , tokenizeAndStringify ( " ; x.y += 1; " ) ) ;
2010-11-02 19:22:48 +01:00
ASSERT_EQUALS ( " ; x [ 0 ] = x [ 0 ] + 1 ; " , tokenizeAndStringify ( " ; x[0] += 1; " ) ) ;
2010-11-04 19:02:54 +01:00
ASSERT_EQUALS ( " ; x [ y - 1 ] = x [ y - 1 ] + 1 ; " , tokenizeAndStringify ( " ; x[y-1] += 1; " ) ) ;
2011-12-04 12:22:21 +01:00
ASSERT_EQUALS ( " ; x [ y ] = x [ y ++ ] + 1 ; " , tokenizeAndStringify ( " ; x[y++] += 1; " ) ) ;
ASSERT_EQUALS ( " ; x [ ++ y ] = x [ y ] + 1 ; " , tokenizeAndStringify ( " ; x[++y] += 1; " ) ) ;
2010-11-06 07:23:35 +01:00
ASSERT_EQUALS ( " ; " , tokenizeAndStringify ( " ;x += 0; " ) ) ;
2010-11-28 11:48:06 +01:00
ASSERT_EQUALS ( " ; " , tokenizeAndStringify ( " ;x += ' \\ 0'; " ) ) ;
2010-11-06 07:23:35 +01:00
ASSERT_EQUALS ( " ; " , tokenizeAndStringify ( " ;x -= 0; " ) ) ;
ASSERT_EQUALS ( " ; " , tokenizeAndStringify ( " ;x |= 0; " ) ) ;
ASSERT_EQUALS ( " ; " , tokenizeAndStringify ( " ;x *= 1; " ) ) ;
ASSERT_EQUALS ( " ; " , tokenizeAndStringify ( " ;x /= 1; " ) ) ;
2010-11-25 18:16:11 +01:00
ASSERT_EQUALS ( " ; a . x ( ) = a . x ( ) + 1 ; " , tokenizeAndStringify ( " ; a.x() += 1; " ) ) ;
2011-03-27 08:19:09 +02:00
ASSERT_EQUALS ( " ; x ( 1 ) = x ( 1 ) + 1 ; " , tokenizeAndStringify ( " ; x(1) += 1; " ) ) ;
2010-12-27 20:49:07 +01:00
// #2368
ASSERT_EQUALS ( " if ( false ) { } else { j = j - i ; } " , tokenizeAndStringify ( " if (false) {} else { j -= i; } " ) ) ;
2011-04-14 17:30:50 +02:00
// #2714 - wrong simplification of "a += b?c:d;"
ASSERT_EQUALS ( " ; a = a + ( b ? c : d ) ; " , tokenizeAndStringify ( " ; a+=b?c:d; " ) ) ;
ASSERT_EQUALS ( " ; a = a * ( b + 1 ) ; " , tokenizeAndStringify ( " ; a*=b+1; " ) ) ;
2011-11-06 14:28:19 +01:00
ASSERT_EQUALS ( " ; a = a + ( b && c ) ; " , tokenizeAndStringify ( " ; a+=b&&c; " ) ) ;
ASSERT_EQUALS ( " ; a = a * ( b || c ) ; " , tokenizeAndStringify ( " ; a*=b||c; " ) ) ;
ASSERT_EQUALS ( " ; a = a | ( b == c ) ; " , tokenizeAndStringify ( " ; a|=b==c; " ) ) ;
2012-03-06 18:51:50 +01:00
// #3469
ASSERT_EQUALS ( " ; a = a + ( b = 1 ) ; " , tokenizeAndStringify ( " ; a += b = 1; " ) ) ;
2010-10-31 08:47:13 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyAssignmentInFunctionCall ( ) {
2010-09-09 19:40:36 +02:00
ASSERT_EQUALS ( " ; x = g ( ) ; f ( x ) ; " , tokenizeAndStringify ( " ;f(x=g()); " ) ) ;
}
2010-10-19 21:54:15 +02:00
2011-10-13 20:53:06 +02:00
void cs ( ) {
2011-12-27 21:50:56 +01:00
bool simplify = false ;
bool expand = true ;
Settings : : PlatformType platform = Settings : : Unspecified ;
2012-04-16 19:51:07 +02:00
const char filename [ ] = " test.cs " ;
2011-12-30 12:12:54 +01:00
ASSERT_EQUALS ( " int * x ; " , tokenizeAndStringify ( " int [] x; " , simplify , expand , platform , filename ) ) ;
ASSERT_EQUALS ( " ; int * x , int * y ; " , tokenizeAndStringify ( " ; int [] x, int [] y; " , simplify , expand , platform , filename ) ) ;
ASSERT_EQUALS ( " ; int * * x ; " , tokenizeAndStringify ( " ; int [][] x; " , simplify , expand , platform , filename ) ) ;
ASSERT_EQUALS ( " ; int * * * x ; " , tokenizeAndStringify ( " ; int [][][] x; " , simplify , expand , platform , filename ) ) ;
2012-01-02 00:21:58 +01:00
ASSERT_EQUALS ( " ; int * * x ; " , tokenizeAndStringify ( " ; int [,] x; " , simplify , expand , platform , filename ) ) ;
ASSERT_EQUALS ( " ; int * * * * x ; " , tokenizeAndStringify ( " ; int [][,][] x; " , simplify , expand , platform , filename ) ) ;
ASSERT_EQUALS ( " ; int * * * x ; " , tokenizeAndStringify ( " ; int [,,] x; " , simplify , expand , platform , filename ) ) ;
ASSERT_EQUALS ( " ; int * * * * * * x ; " , tokenizeAndStringify ( " ; int [,][,,][] x; " , simplify , expand , platform , filename ) ) ;
ASSERT_EQUALS ( " ; int * * * * x ; " , tokenizeAndStringify ( " ; int [,,,] x; " , simplify , expand , platform , filename ) ) ;
2010-10-19 21:54:15 +02:00
}
2010-10-20 22:15:35 +02:00
2011-10-13 20:53:06 +02:00
std : : string javatest ( const char javacode [ ] ) {
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
Settings settings ;
2010-10-20 22:15:35 +02:00
// tokenize..
2010-12-01 18:00:55 +01:00
Tokenizer tokenizer ( & settings , this ) ;
2010-10-20 22:15:35 +02:00
std : : istringstream istr ( javacode ) ;
tokenizer . tokenize ( istr , " test.java " ) ;
2012-04-16 19:51:07 +02:00
return tokenizer . tokens ( ) - > stringifyList ( 0 , false ) ;
2010-10-20 22:15:35 +02:00
}
2011-10-13 20:53:06 +02:00
void java ( ) {
2010-10-20 22:15:35 +02:00
ASSERT_EQUALS ( " void f ( ) { } " , javatest ( " void f() throws Exception { } " ) ) ;
}
2011-01-27 18:44:20 +01:00
2011-10-13 20:53:06 +02:00
void simplifyOperatorName1 ( ) {
2011-01-27 18:44:20 +01:00
// make sure C code doesn't get changed
2011-02-28 20:29:34 +01:00
const char code [ ] = " void operator () {} "
" int main() "
" { "
" operator(); "
" } " ;
2011-01-27 18:44:20 +01:00
2011-02-28 20:29:34 +01:00
const char result [ ] = " void operator ( ) { } "
" int main ( ) "
" { "
" operator ( ) ; "
" } " ;
2011-01-27 18:44:20 +01:00
2011-02-28 20:29:34 +01:00
ASSERT_EQUALS ( result , tokenizeAndStringify ( code , false ) ) ;
}
2011-01-27 18:44:20 +01:00
2011-10-13 20:53:06 +02:00
void simplifyOperatorName2 ( ) {
2011-02-28 20:29:34 +01:00
const char code [ ] = " class Fred "
" { "
" Fred(const Fred & f) { operator = (f); } "
" operator = (); "
" } " ;
2011-01-27 18:44:20 +01:00
2011-02-28 20:29:34 +01:00
const char result [ ] = " class Fred "
" { "
" Fred ( const Fred & f ) { operator= ( f ) ; } "
" operator= ( ) ; "
" } " ;
2011-01-27 18:44:20 +01:00
2011-02-28 20:29:34 +01:00
ASSERT_EQUALS ( result , tokenizeAndStringify ( code , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyOperatorName3 ( ) {
2011-02-28 20:29:34 +01:00
// #2615
const char code [ ] = " void f() { "
" static_cast<ScToken*>(xResult.operator->())->GetMatrix(); "
" } " ;
const char result [ ] = " void f ( ) { static_cast < ScToken * > ( xResult . operator. ( ) ) . GetMatrix ( ) ; } " ;
ASSERT_EQUALS ( result , tokenizeAndStringify ( code , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyOperatorName4 ( ) {
2011-02-28 20:29:34 +01:00
const char code [ ] = " void operator==() { } " ;
const char result [ ] = " void operator== ( ) { } " ;
ASSERT_EQUALS ( result , tokenizeAndStringify ( code , false ) ) ;
2011-01-27 18:44:20 +01:00
}
2011-01-30 08:34:58 +01:00
2011-10-13 20:53:06 +02:00
void simplifyOperatorName5 ( ) {
2011-03-29 02:02:06 +02:00
const char code1 [ ] = " std::istream & operator >> (std::istream & s, Fred &f); " ;
const char result1 [ ] = " std :: istream & operator>> ( std :: istream & s , Fred & f ) ; " ;
ASSERT_EQUALS ( result1 , tokenizeAndStringify ( code1 , false ) ) ;
const char code2 [ ] = " std::ostream & operator << (std::ostream & s, const Fred &f); " ;
const char result2 [ ] = " std :: ostream & operator<< ( std :: ostream & s , const Fred & f ) ; " ;
ASSERT_EQUALS ( result2 , tokenizeAndStringify ( code2 , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyOperatorName6 ( ) { // ticket #3195
2011-10-12 15:10:34 +02:00
const char code1 [ ] = " value_type * operator ++ (int); " ;
const char result1 [ ] = " value_type * operator++ ( int ) ; " ;
ASSERT_EQUALS ( result1 , tokenizeAndStringify ( code1 , false ) ) ;
const char code2 [ ] = " value_type * operator -- (int); " ;
const char result2 [ ] = " value_type * operator-- ( int ) ; " ;
ASSERT_EQUALS ( result2 , tokenizeAndStringify ( code2 , false ) ) ;
}
2012-08-26 10:03:05 +02:00
void simplifyNullArray ( ) {
ASSERT_EQUALS ( " * ( foo . bar [ 5 ] ) = x ; " , tokenizeAndStringify ( " 0[foo.bar[5]] = x; " ) ) ;
}
2011-10-13 20:53:06 +02:00
void removeMacrosInGlobalScope ( ) {
2011-01-30 08:34:58 +01:00
// remove some unhandled macros in the global scope.
ASSERT_EQUALS ( " void f ( ) { } " , tokenizeAndStringify ( " void f() NOTHROW { } " ) ) ;
2011-03-30 19:49:55 +02:00
ASSERT_EQUALS ( " struct Foo { } ; " , tokenizeAndStringify ( " struct __declspec(dllexport) Foo {}; " ) ) ;
2011-10-12 19:23:39 +02:00
ASSERT_EQUALS ( " ABA ( ) namespace { } " , tokenizeAndStringify ( " ABA() namespace { } " ) ) ;
2012-06-12 18:45:31 +02:00
// #3750
ASSERT_EQUALS ( " ; foo :: foo ( ) { } " ,
tokenizeAndStringify ( " ; AB(foo*) foo::foo() { } " ) ) ;
2012-06-13 19:21:20 +02:00
// #3855
ASSERT_EQUALS ( " ; class foo { } " ,
tokenizeAndStringify ( " ; AB class foo { } " ) ) ;
ASSERT_EQUALS ( " ; CONST struct ABC abc ; " ,
tokenizeAndStringify ( " ; CONST struct ABC abc ; " ) ) ;
2011-01-30 08:34:58 +01:00
}
2011-03-26 12:20:23 +01:00
2011-10-13 20:53:06 +02:00
void multipleAssignment ( ) {
2011-03-26 12:20:23 +01:00
ASSERT_EQUALS ( " a = b = 0 ; " , tokenizeAndStringify ( " a=b=0; " ) ) ;
}
2011-06-11 21:51:12 +02:00
2011-10-13 20:53:06 +02:00
void platformWin32 ( ) {
2012-01-18 12:57:41 +01:00
// WIN32A
2011-09-18 01:40:52 +02:00
const char code [ ] = " unsigned int sizeof_short = sizeof(short); "
" unsigned int sizeof_unsigned_short = sizeof(unsigned short); "
" unsigned int sizeof_int = sizeof(int); "
" unsigned int sizeof_unsigned_int = sizeof(unsigned int); "
" unsigned int sizeof_long = sizeof(long); "
" unsigned int sizeof_unsigned_long = sizeof(unsigned long); "
" unsigned int sizeof_long_long = sizeof(long long); "
" unsigned int sizeof_unsigned_long_long = sizeof(unsigned long long); "
" unsigned int sizeof_float = sizeof(float); "
" unsigned int sizeof_double = sizeof(double); "
" unsigned int sizeof_long_double = sizeof(long double); "
" unsigned int sizeof_bool = sizeof(bool); "
" unsigned int sizeof_pointer = sizeof(void *); "
2011-09-18 16:31:31 +02:00
" unsigned int sizeof_size_t = sizeof(size_t); "
" size_t a; "
" ssize_t b; "
" ptrdiff_t c; "
" intptr_t d; "
2011-09-18 19:06:54 +02:00
" uintptr_t e; "
" BOOL f; "
" BOOLEAN g; "
" BYTE h; "
" CHAR i; "
" DWORD j; "
" FLOAT k; "
" INT l; "
" INT32 m; "
" INT64 n; "
" LONG o; "
" SHORT p; "
" UCHAR q; "
" UINT r; "
" ULONG s; "
" USHORT t; "
" WORD u; "
2011-09-20 05:14:58 +02:00
" VOID *v; "
" LPBOOL w; "
" PBOOL x; "
" LPBYTE y; "
" PBOOLEAN z; "
" PBYTE A; "
" LPCSTR B; "
" PCSTR C; "
" LPCVOID D; "
" LPDWORD E; "
" LPINT F; "
" PINT G; "
" LPLONG H; "
" PLONG I; "
" LPSTR J; "
" PSTR K; "
" PCHAR L; "
" LPVOID M; "
2011-09-21 02:17:02 +02:00
" PVOID N; "
" DWORD_PTR O; "
" ULONG_PTR P; "
" SIZE_T Q; "
" HRESULT R; "
" LONG_PTR S; "
2012-01-18 12:57:41 +01:00
" HANDLE T; "
" BOOL _bool; "
" HFILE hfile; "
" LONG32 long32; "
" LCID lcid; "
" LCTYPE lctype; "
" LGRPID lgrpid; "
" LONG64 long64; "
" SSIZE_T _ssize_t; "
" PUCHAR puchar; "
" LPCOLORREF lpcolorref; "
" PDWORD pdword; "
" PULONG pulong; "
" SERVICE_STATUS_HANDLE service_status_hanlde; "
" SC_LOCK sc_lock; "
" SC_HANDLE sc_handle; "
" HACCEL haccel; "
" HCONV hconv; "
" HCONVLIST hconvlist; "
" HDDEDATA hddedata; "
" HDESK hdesk; "
" HDROP hdrop; "
" HDWP hdwp; "
" HENHMETAFILE henhmetafile; "
" HHOOK hhook; "
" HKL hkl; "
" HMONITOR hmonitor; "
" HSZ hsz; "
" HWINSTA hwinsta; "
" PWCHAR pwchar; "
" PUSHORT pushort; "
" UINT_PTR uint_ptr; "
" WPARAM wparam; "
" LANGID langid; "
" DWORD64 dword64; "
" ULONG64 ulong64; "
" HALF_PTR half_ptr; "
" INT_PTR int_ptr; "
" LPCWSTR lpcwstr; " ;
2011-09-18 16:31:31 +02:00
const char expected [ ] = " unsigned int sizeof_short ; sizeof_short = 2 ; "
" unsigned int sizeof_unsigned_short ; sizeof_unsigned_short = 2 ; "
" unsigned int sizeof_int ; sizeof_int = 4 ; "
" unsigned int sizeof_unsigned_int ; sizeof_unsigned_int = 4 ; "
" unsigned int sizeof_long ; sizeof_long = 4 ; "
" unsigned int sizeof_unsigned_long ; sizeof_unsigned_long = 4 ; "
" unsigned int sizeof_long_long ; sizeof_long_long = 8 ; "
" unsigned int sizeof_unsigned_long_long ; sizeof_unsigned_long_long = 8 ; "
" unsigned int sizeof_float ; sizeof_float = 4 ; "
" unsigned int sizeof_double ; sizeof_double = 8 ; "
" unsigned int sizeof_long_double ; sizeof_long_double = 8 ; "
" unsigned int sizeof_bool ; sizeof_bool = 1 ; "
" unsigned int sizeof_pointer ; sizeof_pointer = 4 ; "
" unsigned int sizeof_size_t ; sizeof_size_t = 4 ; "
" unsigned long a ; "
" long b ; "
" long c ; "
" long d ; "
2011-09-18 19:06:54 +02:00
" unsigned long e ; "
" int f ; "
" unsigned char g ; "
" unsigned char h ; "
" char i ; "
" unsigned long j ; "
" float k ; "
" int l ; "
" int m ; "
" long long n ; "
" long o ; "
" short p ; "
" unsigned char q ; "
" unsigned int r ; "
" unsigned long s ; "
" unsigned short t ; "
" unsigned short u ; "
2011-09-20 05:14:58 +02:00
" void * v ; "
" int * w ; "
" int * x ; "
" unsigned char * y ; "
" unsigned char * z ; "
" unsigned char * A ; "
" const char * B ; "
" const char * C ; "
" const void * D ; "
" unsigned long * E ; "
" int * F ; "
" int * G ; "
" long * H ; "
" long * I ; "
" char * J ; "
" char * K ; "
" char * L ; "
" void * M ; "
2011-09-21 02:17:02 +02:00
" void * N ; "
" unsigned long O ; "
" unsigned long P ; "
" unsigned long Q ; "
" long R ; "
" long S ; "
2012-01-18 12:57:41 +01:00
" void * T ; "
" int _bool ; "
" int hfile ; "
" int long32 ; "
" unsigned long lcid ; "
" unsigned long lctype ; "
" unsigned long lgrpid ; "
" long long long64 ; "
" long _ssize_t ; "
" unsigned char * puchar ; "
" unsigned long * lpcolorref ; "
" unsigned long * pdword ; "
" unsigned long * pulong ; "
" void * service_status_hanlde ; "
" void * sc_lock ; "
" void * sc_handle ; "
" void * haccel ; "
" void * hconv ; "
" void * hconvlist ; "
" void * hddedata ; "
" void * hdesk ; "
" void * hdrop ; "
" void * hdwp ; "
" void * henhmetafile ; "
" void * hhook ; "
" void * hkl ; "
" void * hmonitor ; "
" void * hsz ; "
" void * hwinsta ; "
" unsigned short * pwchar ; "
" unsigned short * pushort ; "
" unsigned int uint_ptr ; "
" unsigned int wparam ; "
" unsigned short langid ; "
" unsigned long dword64 ; "
" unsigned long ulong64 ; "
" short half_ptr ; "
" int int_ptr ; "
" const unsigned short * lpcwstr ; " ;
2011-09-18 16:31:31 +02:00
2011-09-24 20:51:03 +02:00
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true , true , Settings : : Win32A ) ) ;
}
2011-10-13 20:53:06 +02:00
void platformWin32A ( ) {
2011-09-24 22:02:56 +02:00
const char code [ ] = " wchar_t wc; "
" TCHAR c; "
2011-09-24 20:51:03 +02:00
" PTSTR ptstr; "
" LPTSTR lptstr; "
" PCTSTR pctstr; "
" LPCTSTR lpctstr; "
" void foo() { "
2011-09-27 04:08:24 +02:00
" TCHAR tc = _T( \' c \' ); "
2011-09-24 20:51:03 +02:00
" TCHAR src[10] = _T( \" 123456789 \" ); "
" TCHAR dst[10]; "
" _tcscpy(dst, src); "
" dst[0] = 0; "
2011-09-24 22:02:56 +02:00
" _tcscat(dst, src); "
2011-09-27 13:29:36 +02:00
" LPTSTR d = _tcsdup(str); "
2011-09-25 16:02:27 +02:00
" _tprintf(_T( \" Hello world! \n \" )); "
" _stprintf(dst, _T( \" Hello! \n \" )); "
" _sntprintf(dst, sizeof(dst) / sizeof(TCHAR), _T( \" Hello world! \n \" )); "
" _tscanf(_T( \" %s \" ), dst); "
" _stscanf(dst, _T( \" %s \" ), dst); "
2012-01-18 12:57:41 +01:00
" } "
" TBYTE tbyte; " ;
2011-09-24 22:02:56 +02:00
const char expected [ ] = " unsigned short wc ; "
" char c ; "
2011-09-24 20:51:03 +02:00
" char * ptstr ; "
" char * lptstr ; "
" const char * pctstr ; "
" const char * lpctstr ; "
" void foo ( ) { "
2011-09-27 04:08:24 +02:00
" char tc ; tc = \' c \' ; "
2011-09-24 20:51:03 +02:00
" char src [ 10 ] = \" 123456789 \" ; "
" char dst [ 10 ] ; "
" strcpy ( dst , src ) ; "
" dst [ 0 ] = 0 ; "
2011-09-24 22:02:56 +02:00
" strcat ( dst , src ) ; "
2011-09-27 13:29:36 +02:00
" char * d ; d = strdup ( str ) ; "
2011-09-25 16:02:27 +02:00
" printf ( \" Hello world! \n \" ) ; "
" sprintf ( dst , \" Hello! \n \" ) ; "
" snprintf ( dst , sizeof ( dst ) / sizeof ( char ) , \" Hello world! \n \" ) ; "
" scanf ( \" %s \" , dst ) ; "
" sscanf ( dst , \" %s \" , dst ) ; "
2012-01-18 12:57:41 +01:00
" } "
" unsigned short tbyte ; " ;
2011-09-24 20:51:03 +02:00
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , false , true , Settings : : Win32A ) ) ;
}
2011-10-13 20:53:06 +02:00
void platformWin32W ( ) {
2011-09-24 22:02:56 +02:00
const char code [ ] = " wchar_t wc; "
" TCHAR c; "
" PTSTR ptstr; "
" LPTSTR lptstr; "
" PCTSTR pctstr; "
" LPCTSTR lpctstr; "
2012-01-18 12:57:41 +01:00
" TBYTE tbyte; "
2011-09-24 22:02:56 +02:00
" void foo() { "
2011-09-27 04:08:24 +02:00
" TCHAR tc = _T( \' c \' ); "
2011-09-24 22:02:56 +02:00
" TCHAR src[10] = _T( \" 123456789 \" ); "
" TCHAR dst[10]; "
" _tcscpy(dst, src); "
" dst[0] = 0; "
" _tcscat(dst, src); "
2011-09-27 13:29:36 +02:00
" LPTSTR d = _tcsdup(str); "
2011-09-25 16:02:27 +02:00
" _tprintf(_T( \" Hello world! \n \" )); "
" _stprintf(dst, _T( \" Hello! \n \" )); "
" _sntprintf(dst, sizeof(dst) / sizeof(TCHAR), _T( \" Hello world! \n \" )); "
" _tscanf(_T( \" %s \" ), dst); "
" _stscanf(dst, _T( \" %s \" ), dst); "
2011-09-24 22:02:56 +02:00
" } " ;
const char expected [ ] = " unsigned short wc ; "
" unsigned short c ; "
" unsigned short * ptstr ; "
" unsigned short * lptstr ; "
" const unsigned short * pctstr ; "
" const unsigned short * lpctstr ; "
2012-01-18 12:57:41 +01:00
" unsigned char tbyte ; "
2011-09-24 22:02:56 +02:00
" void foo ( ) { "
2011-09-27 04:08:24 +02:00
" unsigned short tc ; tc = \' c \' ; "
2011-09-24 22:02:56 +02:00
" unsigned short src [ 10 ] = \" 123456789 \" ; "
" unsigned short dst [ 10 ] ; "
" wcscpy ( dst , src ) ; "
" dst [ 0 ] = 0 ; "
" wcscat ( dst , src ) ; "
2011-09-27 13:29:36 +02:00
" unsigned short * d ; d = wcsdup ( str ) ; "
2011-09-25 16:02:27 +02:00
" wprintf ( \" Hello world! \n \" ) ; "
" swprintf ( dst , \" Hello! \n \" ) ; "
" snwprintf ( dst , sizeof ( dst ) / sizeof ( unsigned short ) , \" Hello world! \n \" ) ; "
" wscanf ( \" %s \" , dst ) ; "
" swscanf ( dst , \" %s \" , dst ) ; "
2011-09-24 22:02:56 +02:00
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , false , true , Settings : : Win32W ) ) ;
2011-09-18 01:40:52 +02:00
}
2011-10-13 20:53:06 +02:00
void platformWin64 ( ) {
2011-09-18 01:40:52 +02:00
const char code [ ] = " unsigned int sizeof_short = sizeof(short); "
" unsigned int sizeof_unsigned_short = sizeof(unsigned short); "
" unsigned int sizeof_int = sizeof(int); "
" unsigned int sizeof_unsigned_int = sizeof(unsigned int); "
" unsigned int sizeof_long = sizeof(long); "
" unsigned int sizeof_unsigned_long = sizeof(unsigned long); "
" unsigned int sizeof_long_long = sizeof(long long); "
" unsigned int sizeof_unsigned_long_long = sizeof(unsigned long long); "
" unsigned int sizeof_float = sizeof(float); "
" unsigned int sizeof_double = sizeof(double); "
" unsigned int sizeof_long_double = sizeof(long double); "
" unsigned int sizeof_bool = sizeof(bool); "
" unsigned int sizeof_pointer = sizeof(void *); "
2011-09-18 16:31:31 +02:00
" unsigned int sizeof_size_t = sizeof(size_t); "
" size_t a; "
" ssize_t b; "
" ptrdiff_t c; "
" intptr_t d; "
2011-09-21 02:17:02 +02:00
" uintptr_t e; "
" DWORD_PTR O; "
" ULONG_PTR P; "
" SIZE_T Q; "
" HRESULT R; "
" LONG_PTR S; "
2012-01-18 12:57:41 +01:00
" HANDLE T; "
" SSIZE_T _ssize_t; "
" UINT_PTR uint_ptr; "
" WPARAM wparam; "
" INT_PTR int_ptr; " ;
2011-09-18 16:31:31 +02:00
const char expected [ ] = " unsigned int sizeof_short ; sizeof_short = 2 ; "
" unsigned int sizeof_unsigned_short ; sizeof_unsigned_short = 2 ; "
" unsigned int sizeof_int ; sizeof_int = 4 ; "
" unsigned int sizeof_unsigned_int ; sizeof_unsigned_int = 4 ; "
" unsigned int sizeof_long ; sizeof_long = 4 ; "
" unsigned int sizeof_unsigned_long ; sizeof_unsigned_long = 4 ; "
" unsigned int sizeof_long_long ; sizeof_long_long = 8 ; "
" unsigned int sizeof_unsigned_long_long ; sizeof_unsigned_long_long = 8 ; "
" unsigned int sizeof_float ; sizeof_float = 4 ; "
" unsigned int sizeof_double ; sizeof_double = 8 ; "
" unsigned int sizeof_long_double ; sizeof_long_double = 8 ; "
" unsigned int sizeof_bool ; sizeof_bool = 1 ; "
" unsigned int sizeof_pointer ; sizeof_pointer = 8 ; "
" unsigned int sizeof_size_t ; sizeof_size_t = 8 ; "
" unsigned long long a ; "
" long long b ; "
" long long c ; "
" long long d ; "
2011-09-21 02:17:02 +02:00
" unsigned long long e ; "
" unsigned long long O ; "
" unsigned long long P ; "
" unsigned long long Q ; "
" long R ; "
" long long S ; "
2012-01-18 12:57:41 +01:00
" void * T ; "
" long long _ssize_t ; "
" unsigned long long uint_ptr ; "
" unsigned long long wparam ; "
" long long int_ptr ; "
;
2011-09-18 16:31:31 +02:00
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true , true , Settings : : Win64 ) ) ;
2011-09-18 01:40:52 +02:00
}
2011-10-13 20:53:06 +02:00
void platformUnix32 ( ) {
2011-09-18 01:40:52 +02:00
const char code [ ] = " unsigned int sizeof_short = sizeof(short); "
" unsigned int sizeof_unsigned_short = sizeof(unsigned short); "
" unsigned int sizeof_int = sizeof(int); "
" unsigned int sizeof_unsigned_int = sizeof(unsigned int); "
" unsigned int sizeof_long = sizeof(long); "
" unsigned int sizeof_unsigned_long = sizeof(unsigned long); "
" unsigned int sizeof_long_long = sizeof(long long); "
" unsigned int sizeof_unsigned_long_long = sizeof(unsigned long long); "
" unsigned int sizeof_float = sizeof(float); "
" unsigned int sizeof_double = sizeof(double); "
" unsigned int sizeof_long_double = sizeof(long double); "
" unsigned int sizeof_bool = sizeof(bool); "
" unsigned int sizeof_pointer = sizeof(void *); "
2011-09-18 16:31:31 +02:00
" unsigned int sizeof_size_t = sizeof(size_t); "
" size_t a; "
" ssize_t b; "
" ptrdiff_t c; "
" intptr_t d; "
" uintptr_t e; " ;
const char expected [ ] = " unsigned int sizeof_short ; sizeof_short = 2 ; "
" unsigned int sizeof_unsigned_short ; sizeof_unsigned_short = 2 ; "
" unsigned int sizeof_int ; sizeof_int = 4 ; "
" unsigned int sizeof_unsigned_int ; sizeof_unsigned_int = 4 ; "
" unsigned int sizeof_long ; sizeof_long = 4 ; "
" unsigned int sizeof_unsigned_long ; sizeof_unsigned_long = 4 ; "
" unsigned int sizeof_long_long ; sizeof_long_long = 8 ; "
" unsigned int sizeof_unsigned_long_long ; sizeof_unsigned_long_long = 8 ; "
" unsigned int sizeof_float ; sizeof_float = 4 ; "
" unsigned int sizeof_double ; sizeof_double = 8 ; "
" unsigned int sizeof_long_double ; sizeof_long_double = 12 ; "
" unsigned int sizeof_bool ; sizeof_bool = 1 ; "
" unsigned int sizeof_pointer ; sizeof_pointer = 4 ; "
" unsigned int sizeof_size_t ; sizeof_size_t = 4 ; "
" unsigned long a ; "
" long b ; "
" long c ; "
" long d ; "
" unsigned long e ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true , true , Settings : : Unix32 ) ) ;
2011-09-18 01:40:52 +02:00
}
2011-10-13 20:53:06 +02:00
void platformUnix64 ( ) {
2011-09-18 01:40:52 +02:00
const char code [ ] = " unsigned int sizeof_short = sizeof(short); "
" unsigned int sizeof_unsigned_short = sizeof(unsigned short); "
" unsigned int sizeof_int = sizeof(int); "
" unsigned int sizeof_unsigned_int = sizeof(unsigned int); "
" unsigned int sizeof_long = sizeof(long); "
" unsigned int sizeof_unsigned_long = sizeof(unsigned long); "
" unsigned int sizeof_long_long = sizeof(long long); "
" unsigned int sizeof_unsigned_long_long = sizeof(unsigned long long); "
" unsigned int sizeof_float = sizeof(float); "
" unsigned int sizeof_double = sizeof(double); "
" unsigned int sizeof_long_double = sizeof(long double); "
" unsigned int sizeof_bool = sizeof(bool); "
" unsigned int sizeof_pointer = sizeof(void *); "
2011-09-18 16:31:31 +02:00
" unsigned int sizeof_size_t = sizeof(size_t); "
" size_t a; "
" ssize_t b; "
" ptrdiff_t c; "
" intptr_t d; "
" uintptr_t e; " ;
const char expected [ ] = " unsigned int sizeof_short ; sizeof_short = 2 ; "
" unsigned int sizeof_unsigned_short ; sizeof_unsigned_short = 2 ; "
" unsigned int sizeof_int ; sizeof_int = 4 ; "
" unsigned int sizeof_unsigned_int ; sizeof_unsigned_int = 4 ; "
" unsigned int sizeof_long ; sizeof_long = 8 ; "
" unsigned int sizeof_unsigned_long ; sizeof_unsigned_long = 8 ; "
" unsigned int sizeof_long_long ; sizeof_long_long = 8 ; "
" unsigned int sizeof_unsigned_long_long ; sizeof_unsigned_long_long = 8 ; "
" unsigned int sizeof_float ; sizeof_float = 4 ; "
" unsigned int sizeof_double ; sizeof_double = 8 ; "
" unsigned int sizeof_long_double ; sizeof_long_double = 16 ; "
" unsigned int sizeof_bool ; sizeof_bool = 1 ; "
" unsigned int sizeof_pointer ; sizeof_pointer = 8 ; "
" unsigned int sizeof_size_t ; sizeof_size_t = 8 ; "
" unsigned long long a ; "
" long long b ; "
" long long c ; "
" long long d ; "
" unsigned long long e ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true , true , Settings : : Unix64 ) ) ;
2011-09-18 01:40:52 +02:00
}
2012-09-03 18:51:15 +02:00
void simplifyMathExpressions ( ) { //#1620
const char * code1 = " void foo() { \n "
" std::cout<<sin(0); \n "
" std::cout<<cos(0); \n "
" std::cout<<sinh(0); \n "
" std::cout<<cosh(0); \n "
" std::cout<<exp(0); \n "
" std::cout<<sqrt(0); \n "
" std::cout<<sqrt(1); \n "
" std::cout<<ln(1); \n "
" std::cout<<pow(sin(x),2)+pow(cos(x),2); \n "
" std::cout<<pow(sinh(x),2)-pow(cosh(x),2); \n "
" } " ;
const char * expected1 = " void foo ( ) { \n "
" std :: cout << 0 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << 0 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << 0 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << 0 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << -1 ; \n "
" } " ;
ASSERT_EQUALS ( expected1 , tokenizeAndStringify ( code1 ) ) ;
}
2008-12-18 22:28:57 +01:00
} ;
2009-01-05 16:49:57 +01:00
REGISTER_TEST ( TestTokenizer )