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
2013-01-01 17:29:08 +01:00
* Copyright ( C ) 2007 - 2013 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-04-27 18:02:07 +02:00
# include "templatesimplifier.h"
2012-06-10 21:52:32 +02:00
# include "path.h"
2010-07-26 22:05:17 +02:00
2008-12-18 22:28:57 +01:00
# include <sstream>
2011-11-08 14:07:22 +01:00
# include <list>
2008-12-18 22:28:57 +01:00
extern std : : ostringstream errout ;
2009-02-17 20:18:26 +01:00
2011-10-13 20:53:06 +02:00
class TestSimplifyTokens : public TestFixture {
2008-12-18 22:28:57 +01:00
public :
TestSimplifyTokens ( ) : TestFixture ( " TestSimplifyTokens " )
{ }
private :
2011-10-13 20:53:06 +02:00
void run ( ) {
2010-05-30 13:15:15 +02:00
// Make sure the Tokenizer::simplifyTokenList works.
// The order of the simplifications is important. So this test
// case shall make sure the simplifications are done in the
// correct order
TEST_CASE ( simplifyTokenList1 ) ;
2009-08-28 12:57:29 +02:00
TEST_CASE ( cast ) ;
2009-01-05 16:49:57 +01:00
TEST_CASE ( iftruefalse ) ;
2009-02-08 09:52:03 +01:00
TEST_CASE ( combine_strings ) ;
2009-02-14 19:44:50 +01:00
TEST_CASE ( double_plus ) ;
2009-02-14 21:56:08 +01:00
TEST_CASE ( redundant_plus ) ;
2012-01-04 13:45:27 +01:00
TEST_CASE ( redundant_plus_numbers ) ;
2011-03-30 16:45:31 +02:00
TEST_CASE ( parentheses1 ) ;
TEST_CASE ( parenthesesVar ) ; // Remove redundant parentheses around variable .. "( %var% )"
2009-02-28 21:40:37 +01:00
TEST_CASE ( declareVar ) ;
2009-02-17 20:18:26 +01:00
2009-06-10 19:36:00 +02:00
TEST_CASE ( declareArray ) ;
2009-05-25 14:21:58 +02:00
TEST_CASE ( dontRemoveIncrement ) ;
2009-05-25 08:26:11 +02:00
TEST_CASE ( removePostIncrement ) ;
2009-05-25 08:31:20 +02:00
TEST_CASE ( removePreIncrement ) ;
2009-05-25 08:26:11 +02:00
2009-02-17 20:18:26 +01:00
TEST_CASE ( elseif1 ) ;
2010-01-22 20:20:43 +01:00
TEST_CASE ( ifa_ifa ) ; // "if (a) { if (a) .." => "if (a) { if (1) .."
2009-02-28 10:09:55 +01:00
TEST_CASE ( sizeof2 ) ;
TEST_CASE ( sizeof3 ) ;
TEST_CASE ( sizeof4 ) ;
TEST_CASE ( sizeof5 ) ;
TEST_CASE ( sizeof6 ) ;
2009-03-29 16:36:34 +02:00
TEST_CASE ( sizeof7 ) ;
2009-05-31 21:33:44 +02:00
TEST_CASE ( sizeof8 ) ;
2009-09-06 13:28:25 +02:00
TEST_CASE ( sizeof9 ) ;
2009-10-10 14:26:03 +02:00
TEST_CASE ( sizeof10 ) ;
2009-10-18 18:06:32 +02:00
TEST_CASE ( sizeof11 ) ;
2009-10-21 21:08:00 +02:00
TEST_CASE ( sizeof12 ) ;
2009-10-22 23:00:27 +02:00
TEST_CASE ( sizeof13 ) ;
2009-11-13 20:07:44 +01:00
TEST_CASE ( sizeof14 ) ;
2009-11-29 12:14:42 +01:00
TEST_CASE ( sizeof15 ) ;
2009-11-29 17:25:35 +01:00
TEST_CASE ( sizeof16 ) ;
2009-12-06 08:46:59 +01:00
TEST_CASE ( sizeof17 ) ;
2010-03-28 15:56:13 +02:00
TEST_CASE ( sizeof18 ) ;
2010-07-22 22:21:12 +02:00
TEST_CASE ( sizeof19 ) ; // #1891 - sizeof 'x'
2010-09-06 21:34:51 +02:00
TEST_CASE ( sizeof20 ) ; // #2024 - sizeof a)
2010-11-29 17:46:10 +01:00
TEST_CASE ( sizeof21 ) ; // #2232 - sizeof...(Args)
2011-02-21 23:49:30 +01:00
TEST_CASE ( sizeof22 ) ; // #2599
2011-02-23 20:44:14 +01:00
TEST_CASE ( sizeof23 ) ; // #2604
2010-05-14 18:40:15 +02:00
TEST_CASE ( sizeofsizeof ) ;
2009-03-05 20:32:02 +01:00
TEST_CASE ( casting ) ;
2009-03-11 19:14:45 +01:00
2009-11-07 08:25:39 +01:00
TEST_CASE ( strlen1 ) ;
2013-06-25 20:10:40 +02:00
TEST_CASE ( strlen2 ) ;
2009-11-07 08:25:39 +01:00
2009-03-11 18:50:24 +01:00
TEST_CASE ( template1 ) ;
2009-03-12 22:17:42 +01:00
TEST_CASE ( template2 ) ;
2009-03-14 21:26:32 +01:00
TEST_CASE ( template3 ) ;
2009-03-15 17:07:05 +01:00
TEST_CASE ( template4 ) ;
2009-03-15 20:03:29 +01:00
TEST_CASE ( template5 ) ;
2009-05-05 20:16:57 +02:00
TEST_CASE ( template6 ) ;
2009-05-05 22:09:15 +02:00
TEST_CASE ( template7 ) ;
2009-05-08 16:19:22 +02:00
TEST_CASE ( template8 ) ;
2009-05-09 08:02:59 +02:00
TEST_CASE ( template9 ) ;
2009-06-14 11:28:25 +02:00
TEST_CASE ( template10 ) ;
2009-06-14 14:57:47 +02:00
TEST_CASE ( template11 ) ;
2009-07-22 13:29:42 +02:00
TEST_CASE ( template12 ) ;
2009-09-14 21:49:41 +02:00
TEST_CASE ( template13 ) ;
2009-10-31 12:34:52 +01:00
TEST_CASE ( template14 ) ;
2009-11-01 15:56:00 +01:00
TEST_CASE ( template15 ) ;
2009-11-01 19:03:52 +01:00
TEST_CASE ( template16 ) ;
2009-12-22 20:10:08 +01:00
TEST_CASE ( template17 ) ;
2010-01-24 20:04:09 +01:00
TEST_CASE ( template18 ) ;
2010-03-03 22:02:32 +01:00
TEST_CASE ( template19 ) ;
2010-06-12 08:32:05 +02:00
TEST_CASE ( template20 ) ;
2010-06-16 07:13:52 +02:00
TEST_CASE ( template21 ) ;
2011-02-20 12:17:05 +01:00
TEST_CASE ( template22 ) ;
2011-03-06 21:15:42 +01:00
TEST_CASE ( template23 ) ;
2011-03-16 22:16:30 +01:00
TEST_CASE ( template24 ) ; // #2648 - using sizeof in template parameter
2011-03-17 21:45:03 +01:00
TEST_CASE ( template25 ) ; // #2648 - another test for sizeof template parameter
2011-05-01 19:05:18 +02:00
TEST_CASE ( template26 ) ; // #2721 - passing 'char[2]' as template parameter
2011-12-03 15:14:53 +01:00
TEST_CASE ( template27 ) ; // #3350 - removing unused template in macro call
2011-12-04 11:38:41 +01:00
TEST_CASE ( template28 ) ;
2012-02-18 08:51:09 +01:00
TEST_CASE ( template29 ) ; // #3449 - don't crash for garbage code
TEST_CASE ( template30 ) ; // #3529 - template < template < ..
2012-08-05 17:11:39 +02:00
TEST_CASE ( template31 ) ; // #4010 - reference type
2012-08-08 06:43:41 +02:00
TEST_CASE ( template32 ) ; // #3818 - mismatching template not handled well
2013-04-12 16:38:12 +02:00
TEST_CASE ( template33 ) ; // #3818,#4544 - inner templates in template instantiation not handled well
2012-08-13 16:17:33 +02:00
TEST_CASE ( template34 ) ; // #3706 - namespace => hang
2012-09-24 06:44:51 +02:00
TEST_CASE ( template35 ) ; // #4074 - A<'x'> a;
2013-04-07 16:48:42 +02:00
TEST_CASE ( template36 ) ; // #4310 - passing unknown template instantiation as template argument
2013-04-11 18:58:39 +02:00
TEST_CASE ( template37 ) ; // #4544 - A<class B> a;
2013-06-12 22:43:29 +02:00
TEST_CASE ( template38 ) ; // #4832 - crash on C++11 right angle brackets
2013-07-06 13:12:39 +02:00
TEST_CASE ( template39 ) ; // #4742 - freeze
2010-03-16 19:53:09 +01:00
TEST_CASE ( template_unhandled ) ;
2009-09-02 22:54:50 +02:00
TEST_CASE ( template_default_parameter ) ;
2009-11-22 13:38:45 +01:00
TEST_CASE ( template_default_type ) ;
2009-09-03 22:19:44 +02:00
TEST_CASE ( template_typename ) ;
2011-10-29 19:45:47 +02:00
TEST_CASE ( template_constructor ) ; // #3152 - template constructor is removed
2009-03-14 21:26:32 +01:00
2012-04-27 18:02:07 +02:00
// Test TemplateSimplifier::templateParameters
TEST_CASE ( templateParameters ) ;
2012-09-19 11:03:40 +02:00
TEST_CASE ( templateParameters1 ) ; // #4169 - segmentation fault
2012-04-27 18:02:07 +02:00
2009-03-13 00:07:05 +01:00
TEST_CASE ( namespaces ) ;
2009-03-23 18:20:56 +01:00
// Assignment in condition..
TEST_CASE ( ifassign1 ) ;
2010-04-13 21:57:52 +02:00
TEST_CASE ( ifAssignWithCast ) ;
2010-07-28 21:03:15 +02:00
TEST_CASE ( whileAssign1 ) ;
TEST_CASE ( whileAssign2 ) ;
2012-10-06 12:49:24 +02:00
TEST_CASE ( whileAssign3 ) ; // varid
2009-03-24 18:23:21 +01:00
// "if(0==x)" => "if(!x)"
TEST_CASE ( ifnot ) ;
2009-04-05 21:21:38 +02:00
TEST_CASE ( combine_wstrings ) ;
2009-05-31 10:42:27 +02:00
// Simplify "not" to "!" (#345)
TEST_CASE ( not1 ) ;
2009-08-29 14:26:01 +02:00
// Simplify "and" to "&&" (#620)
TEST_CASE ( and1 ) ;
2010-05-13 13:59:41 +02:00
// Simplify "or" to "||"
TEST_CASE ( or1 ) ;
2009-06-10 23:12:26 +02:00
TEST_CASE ( comma_keyword ) ;
2009-07-04 17:06:25 +02:00
TEST_CASE ( remove_comma ) ;
2009-07-14 12:06:38 +02:00
// Simplify "?:"
2012-09-30 18:49:25 +02:00
TEST_CASE ( simplifyConditionOperator ) ;
2009-08-02 10:45:33 +02:00
// Simplify calculations
TEST_CASE ( calculations ) ;
2013-06-14 18:51:52 +02:00
TEST_CASE ( comparisons ) ;
2009-08-24 23:10:12 +02:00
// Simplify goto..
TEST_CASE ( goto1 ) ;
2010-12-23 22:03:58 +01:00
TEST_CASE ( goto2 ) ;
2012-04-09 15:06:06 +02:00
TEST_CASE ( goto3 ) ; // #3138
TEST_CASE ( goto4 ) ; // #3459
TEST_CASE ( goto5 ) ; // #3705 - return ({asm("");});
2012-12-22 19:28:53 +01:00
TEST_CASE ( goto6 ) ;
2009-09-05 18:46:27 +02:00
2011-11-08 14:07:22 +01:00
//remove dead code after flow control statements
TEST_CASE ( flowControl ) ;
2011-10-15 01:34:07 +02:00
2009-09-05 18:46:27 +02:00
// Simplify nested strcat() calls
TEST_CASE ( strcat1 ) ;
2009-11-27 22:21:13 +01:00
TEST_CASE ( strcat2 ) ;
2009-09-05 21:21:25 +02:00
2009-09-12 22:54:47 +02:00
TEST_CASE ( simplifyAtol )
2012-01-23 16:10:15 +01:00
2010-01-20 21:19:06 +01:00
TEST_CASE ( simplifyTypedef1 )
2009-09-30 09:11:09 +02:00
TEST_CASE ( simplifyTypedef2 )
TEST_CASE ( simplifyTypedef3 )
2009-09-30 13:35:00 +02:00
TEST_CASE ( simplifyTypedef4 )
2009-10-03 17:02:23 +02:00
TEST_CASE ( simplifyTypedef5 )
2009-11-19 19:02:16 +01:00
TEST_CASE ( simplifyTypedef6 )
2009-11-27 17:32:53 +01:00
TEST_CASE ( simplifyTypedef7 ) ;
2009-12-22 19:10:33 +01:00
TEST_CASE ( simplifyTypedef8 ) ;
2009-12-30 20:56:16 +01:00
TEST_CASE ( simplifyTypedef9 ) ;
2010-01-07 18:09:35 +01:00
TEST_CASE ( simplifyTypedef10 ) ;
2010-01-10 08:49:02 +01:00
TEST_CASE ( simplifyTypedef11 ) ;
TEST_CASE ( simplifyTypedef12 ) ;
TEST_CASE ( simplifyTypedef13 ) ;
2010-01-11 17:09:04 +01:00
TEST_CASE ( simplifyTypedef14 ) ;
2010-01-11 22:58:57 +01:00
TEST_CASE ( simplifyTypedef15 ) ;
2010-01-12 17:38:36 +01:00
TEST_CASE ( simplifyTypedef16 ) ;
2010-01-16 09:18:21 +01:00
TEST_CASE ( simplifyTypedef17 ) ;
2010-01-16 19:42:37 +01:00
TEST_CASE ( simplifyTypedef18 ) ; // typedef vector<int[4]> a;
2010-01-17 08:16:17 +01:00
TEST_CASE ( simplifyTypedef19 ) ;
2010-01-20 21:16:40 +01:00
TEST_CASE ( simplifyTypedef20 ) ;
TEST_CASE ( simplifyTypedef21 ) ;
TEST_CASE ( simplifyTypedef22 ) ;
2010-01-21 07:51:08 +01:00
TEST_CASE ( simplifyTypedef23 ) ;
2010-01-22 17:27:40 +01:00
TEST_CASE ( simplifyTypedef24 ) ;
2010-01-22 20:26:07 +01:00
TEST_CASE ( simplifyTypedef25 ) ;
2010-01-24 22:40:35 +01:00
TEST_CASE ( simplifyTypedef26 ) ;
2010-01-27 19:03:24 +01:00
TEST_CASE ( simplifyTypedef27 ) ;
2010-01-30 08:00:11 +01:00
TEST_CASE ( simplifyTypedef28 ) ;
2010-01-30 19:41:22 +01:00
TEST_CASE ( simplifyTypedef29 ) ;
2010-01-31 21:46:18 +01:00
TEST_CASE ( simplifyTypedef30 ) ;
2010-02-01 19:59:24 +01:00
TEST_CASE ( simplifyTypedef31 ) ;
2010-02-03 07:58:36 +01:00
TEST_CASE ( simplifyTypedef32 ) ;
TEST_CASE ( simplifyTypedef33 ) ;
2010-02-18 07:24:24 +01:00
TEST_CASE ( simplifyTypedef34 ) ; // ticket #1411
2010-02-20 09:07:29 +01:00
TEST_CASE ( simplifyTypedef35 ) ;
2010-02-23 22:12:55 +01:00
TEST_CASE ( simplifyTypedef36 ) ; // ticket #1434
2010-02-24 17:50:02 +01:00
TEST_CASE ( simplifyTypedef37 ) ; // ticket #1449
2010-02-26 21:43:00 +01:00
TEST_CASE ( simplifyTypedef38 ) ;
TEST_CASE ( simplifyTypedef39 ) ;
2010-02-27 07:27:51 +01:00
TEST_CASE ( simplifyTypedef40 ) ;
2010-03-10 16:47:39 +01:00
TEST_CASE ( simplifyTypedef41 ) ; // ticket #1488
2010-03-17 17:31:08 +01:00
TEST_CASE ( simplifyTypedef42 ) ; // ticket #1506
2010-04-12 19:05:31 +02:00
TEST_CASE ( simplifyTypedef43 ) ; // ticket #1588
2010-04-14 19:06:51 +02:00
TEST_CASE ( simplifyTypedef44 ) ;
2010-04-18 09:13:55 +02:00
TEST_CASE ( simplifyTypedef45 ) ; // ticket #1613
2010-07-02 15:22:29 +02:00
TEST_CASE ( simplifyTypedef46 ) ;
2010-05-11 21:41:33 +02:00
TEST_CASE ( simplifyTypedef47 ) ;
2010-05-17 19:58:27 +02:00
TEST_CASE ( simplifyTypedef48 ) ; // ticket #1673
2010-05-18 18:20:11 +02:00
TEST_CASE ( simplifyTypedef49 ) ; // ticket #1691
2010-05-28 06:55:54 +02:00
TEST_CASE ( simplifyTypedef50 ) ;
TEST_CASE ( simplifyTypedef51 ) ;
2010-06-10 07:18:55 +02:00
TEST_CASE ( simplifyTypedef52 ) ; // ticket #1782
2010-06-17 06:59:01 +02:00
TEST_CASE ( simplifyTypedef53 ) ; // ticket #1801
2010-06-28 15:33:27 +02:00
TEST_CASE ( simplifyTypedef54 ) ; // ticket #1814
2010-07-05 23:03:54 +02:00
TEST_CASE ( simplifyTypedef55 ) ;
2010-07-07 07:40:11 +02:00
TEST_CASE ( simplifyTypedef56 ) ; // ticket #1829
2010-07-13 08:08:12 +02:00
TEST_CASE ( simplifyTypedef57 ) ; // ticket #1846
2010-08-20 07:11:02 +02:00
TEST_CASE ( simplifyTypedef58 ) ; // ticket #1963
2010-09-02 19:09:06 +02:00
TEST_CASE ( simplifyTypedef59 ) ; // ticket #2011
2010-09-08 06:45:57 +02:00
TEST_CASE ( simplifyTypedef60 ) ; // ticket #2035
2010-10-08 16:00:21 +02:00
TEST_CASE ( simplifyTypedef61 ) ; // ticket #2074 and 2075
2010-10-13 20:02:37 +02:00
TEST_CASE ( simplifyTypedef62 ) ; // ticket #2082
2010-11-06 09:31:43 +01:00
TEST_CASE ( simplifyTypedef63 ) ; // ticket #2175 'typedef float x[3];'
2010-11-11 06:40:49 +01:00
TEST_CASE ( simplifyTypedef64 ) ;
2010-12-16 07:48:46 +01:00
TEST_CASE ( simplifyTypedef65 ) ; // ticket #2314
2010-12-22 18:37:23 +01:00
TEST_CASE ( simplifyTypedef66 ) ; // ticket #2341
2010-12-28 08:01:32 +01:00
TEST_CASE ( simplifyTypedef67 ) ; // ticket #2354
2010-12-28 14:04:44 +01:00
TEST_CASE ( simplifyTypedef68 ) ; // ticket #2355
2010-12-29 09:18:41 +01:00
TEST_CASE ( simplifyTypedef69 ) ; // ticket #2348
TEST_CASE ( simplifyTypedef70 ) ; // ticket #2348
2010-12-29 20:25:47 +01:00
TEST_CASE ( simplifyTypedef71 ) ; // ticket #2348
2011-01-02 07:42:47 +01:00
TEST_CASE ( simplifyTypedef72 ) ; // ticket #2375
2011-01-05 17:42:55 +01:00
TEST_CASE ( simplifyTypedef73 ) ; // ticket #2412
2011-01-06 07:52:59 +01:00
TEST_CASE ( simplifyTypedef74 ) ; // ticket #2414
2011-01-07 08:02:47 +01:00
TEST_CASE ( simplifyTypedef75 ) ; // ticket #2426
2011-01-14 07:41:22 +01:00
TEST_CASE ( simplifyTypedef76 ) ; // ticket #2453
2011-02-09 01:47:14 +01:00
TEST_CASE ( simplifyTypedef77 ) ; // ticket #2554
2011-02-12 01:09:24 +01:00
TEST_CASE ( simplifyTypedef78 ) ; // ticket #2568
2011-02-14 01:02:57 +01:00
TEST_CASE ( simplifyTypedef79 ) ; // ticket #2348
2011-02-19 20:18:37 +01:00
TEST_CASE ( simplifyTypedef80 ) ; // ticket #2587
2011-02-22 13:48:34 +01:00
TEST_CASE ( simplifyTypedef81 ) ; // ticket #2603
2011-02-23 04:11:17 +01:00
TEST_CASE ( simplifyTypedef82 ) ; // ticket #2403
2011-03-04 02:32:10 +01:00
TEST_CASE ( simplifyTypedef83 ) ; // ticket #2620
2011-03-07 00:59:56 +01:00
TEST_CASE ( simplifyTypedef84 ) ; // ticket #2630
2011-03-16 00:24:30 +01:00
TEST_CASE ( simplifyTypedef85 ) ; // ticket #2651
2011-03-16 04:28:45 +01:00
TEST_CASE ( simplifyTypedef86 ) ; // ticket #2581
2011-03-18 01:00:49 +01:00
TEST_CASE ( simplifyTypedef87 ) ; // ticket #2651
2011-03-25 03:26:42 +01:00
TEST_CASE ( simplifyTypedef88 ) ; // ticket #2675
2011-04-14 05:24:41 +02:00
TEST_CASE ( simplifyTypedef89 ) ; // ticket #2717
2011-04-14 05:58:58 +02:00
TEST_CASE ( simplifyTypedef90 ) ; // ticket #2718
2011-04-16 16:35:23 +02:00
TEST_CASE ( simplifyTypedef91 ) ; // ticket #2716
2011-04-20 13:49:04 +02:00
TEST_CASE ( simplifyTypedef92 ) ; // ticket #2736
2011-04-21 01:09:28 +02:00
TEST_CASE ( simplifyTypedef93 ) ; // ticket #2738
2011-05-03 04:42:52 +02:00
TEST_CASE ( simplifyTypedef94 ) ; // ticket #1982
2011-06-19 23:57:01 +02:00
TEST_CASE ( simplifyTypedef95 ) ; // ticket #2844
2011-07-03 16:50:15 +02:00
TEST_CASE ( simplifyTypedef96 ) ; // ticket #2886
2011-08-10 00:31:02 +02:00
TEST_CASE ( simplifyTypedef97 ) ; // ticket #2983 (segmentation fault)
2011-08-12 00:18:47 +02:00
TEST_CASE ( simplifyTypedef98 ) ; // ticket #2963
2011-08-15 00:06:05 +02:00
TEST_CASE ( simplifyTypedef99 ) ; // ticket #2999
2011-08-15 00:50:33 +02:00
TEST_CASE ( simplifyTypedef100 ) ; // ticket #3000
2011-08-15 02:38:16 +02:00
TEST_CASE ( simplifyTypedef101 ) ; // ticket #3003 (segmentation fault)
2011-08-15 03:00:05 +02:00
TEST_CASE ( simplifyTypedef102 ) ; // ticket #3004
2011-08-15 12:56:15 +02:00
TEST_CASE ( simplifyTypedef103 ) ; // ticket #3007
2011-09-02 01:10:58 +02:00
TEST_CASE ( simplifyTypedef104 ) ; // ticket #3070
2012-02-18 17:57:50 +01:00
TEST_CASE ( simplifyTypedef105 ) ; // ticket #3616
2012-03-25 10:33:18 +02:00
TEST_CASE ( simplifyTypedef106 ) ; // ticket #3619
2012-08-07 16:39:41 +02:00
TEST_CASE ( simplifyTypedef107 ) ; // ticket #3963 - bad code => segmentation fault
2010-02-27 07:27:51 +01:00
2010-05-18 07:11:23 +02:00
TEST_CASE ( simplifyTypedefFunction1 ) ;
TEST_CASE ( simplifyTypedefFunction2 ) ; // ticket #1685
2010-05-23 10:46:39 +02:00
TEST_CASE ( simplifyTypedefFunction3 ) ;
2010-05-25 20:43:44 +02:00
TEST_CASE ( simplifyTypedefFunction4 ) ;
2010-05-27 07:00:33 +02:00
TEST_CASE ( simplifyTypedefFunction5 ) ;
2010-11-06 15:15:50 +01:00
TEST_CASE ( simplifyTypedefFunction6 ) ;
2010-11-11 17:02:04 +01:00
TEST_CASE ( simplifyTypedefFunction7 ) ;
2011-01-23 09:04:34 +01:00
TEST_CASE ( simplifyTypedefFunction8 ) ;
2010-05-10 07:12:06 +02:00
2011-10-23 10:25:14 +02:00
TEST_CASE ( simplifyOperator1 ) ;
2009-10-04 22:33:41 +02:00
TEST_CASE ( reverseArraySyntax )
2009-11-27 17:32:53 +01:00
TEST_CASE ( simplify_numeric_condition )
2011-01-04 21:35:35 +01:00
TEST_CASE ( simplify_condition ) ;
2009-10-17 18:21:13 +02:00
2010-01-31 16:29:12 +01:00
TEST_CASE ( pointeralias1 ) ;
TEST_CASE ( pointeralias2 ) ;
2010-02-06 16:51:32 +01:00
TEST_CASE ( pointeralias3 ) ;
2010-04-25 15:12:06 +02:00
TEST_CASE ( pointeralias4 ) ;
2010-05-16 19:38:44 +02:00
TEST_CASE ( pointeralias5 ) ;
2009-11-05 21:30:05 +01:00
TEST_CASE ( reduceConstness ) ;
2009-12-28 08:37:34 +01:00
// simplify "while (0)"
TEST_CASE ( while0 ) ;
2011-10-10 15:13:47 +02:00
// ticket #3140
TEST_CASE ( while0for ) ;
2010-01-01 22:25:37 +01:00
TEST_CASE ( while1 ) ;
2010-01-04 17:22:06 +01:00
TEST_CASE ( enum1 ) ;
TEST_CASE ( enum2 ) ;
2010-01-25 07:45:16 +01:00
TEST_CASE ( enum3 ) ;
2010-01-29 15:57:26 +01:00
TEST_CASE ( enum4 ) ;
2010-02-03 21:23:44 +01:00
TEST_CASE ( enum5 ) ;
2010-02-06 09:49:56 +01:00
TEST_CASE ( enum6 ) ;
2010-02-16 07:33:23 +01:00
TEST_CASE ( enum7 ) ;
TEST_CASE ( enum8 ) ;
2010-02-17 22:42:08 +01:00
TEST_CASE ( enum9 ) ; // ticket 1404
2010-02-25 07:24:32 +01:00
TEST_CASE ( enum10 ) ; // ticket 1445
2010-02-26 21:40:57 +01:00
TEST_CASE ( enum11 ) ;
2010-03-06 13:48:43 +01:00
TEST_CASE ( enum12 ) ;
2010-03-07 12:50:45 +01:00
TEST_CASE ( enum13 ) ;
2010-03-07 13:01:56 +01:00
TEST_CASE ( enum14 ) ;
2010-08-21 07:44:56 +02:00
TEST_CASE ( enum15 ) ;
2010-08-30 07:25:17 +02:00
TEST_CASE ( enum16 ) ; // ticket #1988
2010-12-30 20:55:23 +01:00
TEST_CASE ( enum17 ) ; // ticket #2381 (duplicate enums)
2011-01-15 08:21:06 +01:00
TEST_CASE ( enum18 ) ; // #2466 (array with same name as enum constant)
2011-02-03 07:58:49 +01:00
TEST_CASE ( enum19 ) ; // ticket #2536
2011-02-22 01:30:40 +01:00
TEST_CASE ( enum20 ) ; // ticket #2600
2011-04-15 01:50:17 +02:00
TEST_CASE ( enum21 ) ; // ticket #2720
2011-04-24 04:08:03 +02:00
TEST_CASE ( enum22 ) ; // ticket #2745
2011-05-27 17:17:39 +02:00
TEST_CASE ( enum23 ) ; // ticket #2804
2011-06-09 02:14:52 +02:00
TEST_CASE ( enum24 ) ; // ticket #2828
2011-08-06 00:07:18 +02:00
TEST_CASE ( enum25 ) ; // ticket #2966
2011-08-07 22:48:47 +02:00
TEST_CASE ( enum26 ) ; // ticket #2975 (segmentation fault)
2011-08-15 13:07:12 +02:00
TEST_CASE ( enum27 ) ; // ticket #3005 (segmentation fault)
2011-10-22 13:43:53 +02:00
TEST_CASE ( enum28 ) ;
2012-05-16 18:48:33 +02:00
TEST_CASE ( enum29 ) ; // ticket #3747 (bitwise or value)
2012-05-29 13:20:51 +02:00
TEST_CASE ( enum30 ) ; // ticket #3852 (false positive)
2012-09-08 12:42:24 +02:00
TEST_CASE ( enum31 ) ; // ticket #3934 (calculation in first item)
TEST_CASE ( enum32 ) ; // ticket #3998 (access violation)
TEST_CASE ( enum33 ) ; // ticket #4015 (segmentation fault)
2012-09-09 12:38:15 +02:00
TEST_CASE ( enum34 ) ; // ticket #4141 (division by zero)
2012-09-21 16:17:20 +02:00
TEST_CASE ( enum35 ) ; // ticket #3953 (avoid simplification of type)
2012-12-02 07:22:55 +01:00
TEST_CASE ( enum36 ) ; // ticket #4378
2013-06-03 19:18:51 +02:00
TEST_CASE ( enum37 ) ; // ticket #4280 (shadow variable)
2013-06-12 06:45:26 +02:00
TEST_CASE ( enum38 ) ; // ticket #4463 (when throwing enum id, don't warn about shadow variable)
2012-09-08 12:42:24 +02:00
TEST_CASE ( enumscope1 ) ; // ticket #3949
2012-09-19 19:19:13 +02:00
TEST_CASE ( duplicateDefinition ) ; // ticket #3565
2010-01-12 21:25:31 +01:00
// remove "std::" on some standard functions
TEST_CASE ( removestd ) ;
2010-01-30 09:33:16 +01:00
// Tokenizer::simplifyInitVar
TEST_CASE ( simplifyInitVar ) ;
2010-01-31 16:29:12 +01:00
// Tokenizer::simplifyReference
TEST_CASE ( simplifyReference ) ;
2010-02-27 19:41:14 +01:00
// x = realloc(y,0); => free(y);x=0;
TEST_CASE ( simplifyRealloc ) ;
2010-03-06 15:28:52 +01:00
2010-05-30 12:49:39 +02:00
// while(f() && errno==EINTR) { } => while (f()) { }
TEST_CASE ( simplifyErrNoInWhile ) ;
2010-03-06 15:28:52 +01:00
// while(fclose(f)); => r = fclose(f); while(r){r=fclose(f);}
TEST_CASE ( simplifyFuncInWhile ) ;
2010-03-13 11:52:48 +01:00
// struct ABC abc = { .a = 3 }; => struct ABC abc; abc.a = 3;
TEST_CASE ( initstruct ) ;
2010-03-18 18:14:52 +01:00
// struct ABC { } abc; => struct ABC { }; ABC abc;
2011-02-13 20:11:22 +01:00
TEST_CASE ( simplifyStructDecl1 ) ;
TEST_CASE ( simplifyStructDecl2 ) ; // ticket #2579
2011-07-02 15:21:30 +02:00
TEST_CASE ( simplifyStructDecl3 ) ;
2011-07-03 14:14:51 +02:00
TEST_CASE ( simplifyStructDecl4 ) ;
2012-01-31 18:23:02 +01:00
TEST_CASE ( simplifyStructDecl5 ) ; // ticket #3533 (segmentation fault)
2012-09-08 10:51:31 +02:00
TEST_CASE ( simplifyStructDecl6 ) ; // ticket #3732
2013-01-10 18:24:01 +01:00
TEST_CASE ( simplifyStructDecl7 ) ; // ticket #476 (static anonymous struct array)
2010-04-10 09:58:09 +02:00
// register int var; => int var;
// inline int foo() {} => int foo() {}
TEST_CASE ( removeUnwantedKeywords ) ;
2010-04-21 21:08:47 +02:00
// remove calling convention __cdecl, __stdcall, ...
TEST_CASE ( simplifyCallingConvention ) ;
2010-10-02 13:09:07 +02:00
TEST_CASE ( simplifyFunctorCall ) ;
2010-10-26 21:05:20 +02:00
TEST_CASE ( redundant_semicolon ) ;
2010-11-06 06:53:09 +01:00
TEST_CASE ( simplifyFunctionReturn ) ;
2011-02-02 07:40:08 +01:00
2011-10-27 20:54:42 +02:00
// void foo(void) -> void foo()
TEST_CASE ( removeVoidFromFunction ) ;
2011-02-24 12:39:46 +01:00
TEST_CASE ( removeUnnecessaryQualification1 ) ;
TEST_CASE ( removeUnnecessaryQualification2 ) ;
2011-06-14 21:31:52 +02:00
TEST_CASE ( removeUnnecessaryQualification3 ) ;
TEST_CASE ( removeUnnecessaryQualification4 ) ;
TEST_CASE ( removeUnnecessaryQualification5 ) ;
2011-11-08 17:37:24 +01:00
TEST_CASE ( removeUnnecessaryQualification6 ) ; // ticket #2859
TEST_CASE ( removeUnnecessaryQualification7 ) ; // ticket #2970
2011-09-24 01:45:19 +02:00
TEST_CASE ( removeUnnecessaryQualification8 ) ;
2011-11-08 17:37:24 +01:00
TEST_CASE ( removeUnnecessaryQualification9 ) ; // ticket #3151
TEST_CASE ( removeUnnecessaryQualification10 ) ; // ticket #3310 segmentation fault
2011-02-22 13:02:15 +01:00
TEST_CASE ( simplifyIfNotNull ) ;
2011-06-12 14:24:49 +02:00
TEST_CASE ( simplifyVarDecl1 ) ; // ticket # 2682 segmentation fault
TEST_CASE ( simplifyVarDecl2 ) ; // ticket # 2834 segmentation fault
2011-06-26 22:53:16 +02:00
TEST_CASE ( return_strncat ) ; // ticket # 2860 Returning value of strncat() reported as memory leak
2011-09-03 20:45:48 +02:00
// #3069 : for loop with 1 iteration
// for (x=0;x<1;x++) { .. }
// The for is redundant
TEST_CASE ( removeRedundantFor ) ;
2011-11-12 12:01:31 +01:00
TEST_CASE ( consecutiveBraces ) ;
2012-01-26 17:07:10 +01:00
TEST_CASE ( undefinedSizeArray ) ;
2012-09-08 20:59:25 +02:00
TEST_CASE ( simplifyArrayAddress ) ; // Replace "&str[num]" => "(str + num)"
2013-01-13 20:52:38 +01:00
TEST_CASE ( simplifyCharAt ) ;
2008-12-18 22:28:57 +01:00
}
2012-04-17 09:51:08 +02:00
std : : string tok ( const char code [ ] , bool simplify = true , Settings : : PlatformType type = Settings : : Unspecified ) {
2009-11-29 12:14:42 +01:00
errout . str ( " " ) ;
Settings settings ;
2011-09-03 15:30:30 +02:00
settings . addEnabled ( " portability " ) ;
2011-09-18 16:31:31 +02:00
settings . platform ( type ) ;
2009-11-29 12:14:42 +01:00
Tokenizer tokenizer ( & settings , this ) ;
2008-12-18 22:28:57 +01:00
std : : istringstream istr ( code ) ;
2009-01-05 16:49:57 +01:00
tokenizer . tokenize ( istr , " test.cpp " ) ;
2009-11-29 12:14:42 +01:00
2010-04-02 07:30:58 +02:00
if ( simplify )
2009-09-30 13:35:00 +02:00
tokenizer . simplifyTokenList ( ) ;
2012-04-16 19:51:07 +02:00
return tokenizer . tokens ( ) - > stringifyList ( 0 , ! simplify ) ;
2008-12-18 22:28:57 +01:00
}
2012-04-17 09:51:08 +02:00
std : : string tok ( const std : : string & code , bool simplify = true , Settings : : PlatformType type = Settings : : Unspecified ) {
return tok ( code . c_str ( ) , simplify , type ) ;
}
2012-06-10 21:52:32 +02:00
std : : string tok ( const char code [ ] , const char filename [ ] ) {
errout . str ( " " ) ;
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , filename ) ;
tokenizer . simplifyTokenList ( ) ;
return tokenizer . tokens ( ) - > stringifyList ( 0 , false ) ;
}
2010-05-30 13:15:15 +02:00
2012-08-17 16:37:25 +02:00
std : : string tokenizeDebugListing ( const std : : string & code , bool simplify = false , const char filename [ ] = " test.cpp " ) {
errout . str ( " " ) ;
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , filename ) ;
if ( simplify )
tokenizer . simplifyTokenList ( ) ;
// result..
return tokenizer . tokens ( ) - > stringifyList ( true ) ;
}
2010-05-30 13:15:15 +02:00
2011-10-13 20:53:06 +02:00
void simplifyTokenList1 ( ) {
2010-05-30 13:15:15 +02:00
// #1717 : The simplifyErrNoInWhile needs to be used before simplifyIfAssign..
2011-04-09 18:30:47 +02:00
ASSERT_EQUALS ( " ; x = f ( ) ; while ( x == -1 ) { x = f ( ) ; } " ,
2010-05-30 13:15:15 +02:00
tok ( " ;while((x=f())==-1 && errno==EINTR){} " , true ) ) ;
}
2008-12-18 22:28:57 +01:00
2011-10-13 20:53:06 +02:00
void cast ( ) {
2012-01-15 14:50:01 +01:00
ASSERT_EQUALS ( " if ( ! p ) { ; } " , tok ( " if (p == (char *)0); " ) ) ;
2009-08-28 12:57:29 +02:00
ASSERT_EQUALS ( " return str ; " , tok ( " return (char *)str; " ) ) ;
2008-12-18 22:28:57 +01:00
2009-08-28 12:57:29 +02:00
{
const char code [ ] = " static void crash() \n "
" { \n "
" goto err_exit; \n "
" err_exit: \n "
" (void)foo(); \n "
" } \n " ;
const char expected [ ] = " static void crash ( ) "
2011-09-13 07:55:47 +02:00
" { foo ( ) ; return ; } " ;
2009-08-28 12:57:29 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2009-10-04 23:12:12 +02:00
ASSERT_EQUALS ( " if ( * a ) " , tok ( " if ((char)*a) " ) ) ;
ASSERT_EQUALS ( " if ( & a ) " , tok ( " if ((int)&a) " ) ) ;
ASSERT_EQUALS ( " if ( * a ) " , tok ( " if ((unsigned int)(unsigned char)*a) " ) ) ;
2011-01-27 18:44:20 +01:00
ASSERT_EQUALS ( " class A { A operator* ( int ) ; } ; " , tok ( " class A { A operator *(int); }; " ) ) ;
ASSERT_EQUALS ( " class A { A operator* ( int ) const ; } ; " , tok ( " class A { A operator *(int) const; }; " ) ) ;
2012-01-15 14:50:01 +01:00
ASSERT_EQUALS ( " if ( ! p ) { ; } " , tok ( " if (p == (char *)(char *)0); " ) ) ;
2011-07-21 09:32:24 +02:00
// no simplification as the cast may be important here. see #2897 for example
ASSERT_EQUALS ( " ; * ( ( char * ) p + 1 ) = 0 ; " , tok ( " ; *((char *)p + 1) = 0; " ) ) ;
2012-09-18 20:55:37 +02:00
ASSERT_EQUALS ( " if ( true ) " , tok ( " if ((unsigned char)1) " ) ) ; // #4164
ASSERT_EQUALS ( " f ( 200 ) " , tok ( " f((unsigned char)200) " ) ) ;
ASSERT_EQUALS ( " f ( ( char ) 1234 ) " , tok ( " f((char)1234) " ) ) ; // dont simplify downcast
2009-03-09 19:47:21 +01:00
}
2009-08-28 12:57:29 +02:00
2011-10-13 20:53:06 +02:00
void iftruefalse ( ) {
2008-12-21 16:10:44 +01:00
{
2009-07-14 11:37:26 +02:00
const char code1 [ ] = " void f() { int a; bool use = false; if( use ) { a=0; } else {a=1;} } " ;
const char code2 [ ] = " void f() { int a; bool use = false; {a=1;} } " ;
2009-01-05 16:49:57 +01:00
ASSERT_EQUALS ( tok ( code2 ) , tok ( code1 ) ) ;
2008-12-21 16:10:44 +01:00
}
{
2009-07-14 11:37:26 +02:00
const char code1 [ ] = " void f() { int a; bool use = true; if( use ) { a=0; } else {a=1;} } " ;
const char code2 [ ] = " void f() { int a; bool use = true; { a=0; } } " ;
2009-01-05 16:49:57 +01:00
ASSERT_EQUALS ( tok ( code2 ) , tok ( code1 ) ) ;
2008-12-21 16:10:44 +01:00
}
{
2009-07-14 11:37:26 +02:00
const char code1 [ ] = " void f() { int a; int use = 5; if( use ) { a=0; } else {a=1;} } " ;
const char code2 [ ] = " void f() { int a; int use = 5; { a=0; } } " ;
2009-01-05 16:49:57 +01:00
ASSERT_EQUALS ( tok ( code2 ) , tok ( code1 ) ) ;
2008-12-21 16:10:44 +01:00
}
{
2009-07-14 11:37:26 +02:00
const char code1 [ ] = " void f() { int a; int use = 0; if( use ) { a=0; } else {a=1;} } " ;
const char code2 [ ] = " void f() { int a; int use = 0; {a=1;} } " ;
2009-01-05 16:49:57 +01:00
ASSERT_EQUALS ( tok ( code2 ) , tok ( code1 ) ) ;
2008-12-21 16:10:44 +01:00
}
2008-12-21 22:04:58 +01:00
{
2009-07-14 11:37:26 +02:00
const char code1 [ ] = " void f() { int a; bool use = false; if( use ) a=0; else a=1; int c=1; } " ;
const char code2 [ ] = " void f() { int a; bool use = false; { a=1; } int c=1; } " ;
2009-01-05 16:49:57 +01:00
ASSERT_EQUALS ( tok ( code2 ) , tok ( code1 ) ) ;
2008-12-21 22:04:58 +01:00
}
{
2009-07-14 11:37:26 +02:00
const char code1 [ ] = " void f() { int a; bool use = true; if( use ) a=0; else a=1; int c=1; } " ;
const char code2 [ ] = " void f() { int a; bool use = true; { a=0; } int c=1; } " ;
2009-01-05 16:49:57 +01:00
ASSERT_EQUALS ( tok ( code2 ) , tok ( code1 ) ) ;
2008-12-21 22:04:58 +01:00
}
{
2009-07-14 11:37:26 +02:00
const char code1 [ ] = " void f() { int a; bool use = false; if( use ) a=0; else if( bb ) a=1; int c=1; } " ;
const char code2 [ ] = " void f ( ) { int a ; bool use ; use = false ; { if ( bb ) { a = 1 ; } } int c ; c = 1 ; } " ;
2009-01-05 16:49:57 +01:00
ASSERT_EQUALS ( tok ( code2 ) , tok ( code1 ) ) ;
2008-12-21 22:04:58 +01:00
}
{
2009-07-14 11:37:26 +02:00
const char code1 [ ] = " void f() { int a; bool use = true; if( use ) a=0; else if( bb ) a=1; int c=1; } " ;
const char code2 [ ] = " void f() { int a; bool use = true; { a=0;} int c=1; } " ;
2009-01-05 16:49:57 +01:00
ASSERT_EQUALS ( tok ( code2 ) , tok ( code1 ) ) ;
2008-12-21 22:04:58 +01:00
}
{
2010-01-31 09:33:57 +01:00
const char code1 [ ] = " void f() { int a; bool use = true; if( use ) a=0; else if( bb ) a=1; else if( cc ) a=33; else { gg = 0; } int c=1; } " ;
2011-12-02 23:01:55 +01:00
const char code2 [ ] = " void f ( ) { } " ;
2010-01-31 09:33:57 +01:00
ASSERT_EQUALS ( code2 , tok ( code1 ) ) ;
2008-12-26 13:55:53 +01:00
}
2008-12-26 16:42:04 +01:00
2008-12-26 15:43:16 +01:00
{
2009-07-14 11:37:26 +02:00
const char code1 [ ] = " void f() { if( aa ) { a=0; } else if( true ) a=1; else { a=2; } } " ;
const char code2 [ ] = " void f ( ) { if ( aa ) { a = 0 ; } else { { a = 1 ; } } } " ;
2009-01-05 16:49:57 +01:00
ASSERT_EQUALS ( tok ( code2 ) , tok ( code1 ) ) ;
2008-12-26 15:43:16 +01:00
}
2008-12-26 16:46:59 +01:00
{
2009-07-14 11:37:26 +02:00
const char code1 [ ] = " void f() { if( aa ) { a=0; } else if( false ) a=1; else { a=2; } } " ;
const char code2 [ ] = " void f ( ) { if ( aa ) { a = 0 ; } else { { a = 2 ; } } } " ;
2009-01-05 16:49:57 +01:00
ASSERT_EQUALS ( tok ( code2 ) , tok ( code1 ) ) ;
2008-12-26 16:46:59 +01:00
}
2013-01-19 21:13:51 +01:00
{
const char code1 [ ] = " static const int x=1; void f() { if(x) { a=0; } } " ;
ASSERT_EQUALS ( " void f ( ) { a = 0 ; } " , tok ( code1 ) ) ;
}
2008-12-21 16:10:44 +01:00
}
2009-02-08 09:52:03 +01:00
2011-10-13 20:53:06 +02:00
void combine_strings ( ) {
2009-02-08 09:52:03 +01:00
const char code1 [ ] = " void foo() \n "
" { \n "
" const char *a = \n "
" { \n "
" \" hello \" \n "
" \" world \" \n "
" }; \n "
" } \n " ;
const char code2 [ ] = " void foo() \n "
" { \n "
" const char *a = \n "
" { \n "
" \" hello world \" \n "
" }; \n "
2009-04-05 21:21:38 +02:00
" } \n " ;
ASSERT_EQUALS ( tok ( code2 ) , tok ( code1 ) ) ;
}
2011-10-13 20:53:06 +02:00
void combine_wstrings ( ) {
2009-04-05 21:21:38 +02:00
const char code1 [ ] = " void foo() \n "
" { \n "
" const wchar_t *a = \n "
" { \n "
" L \" hello \" \n "
" L \" world \" \n "
" }; \n "
" } \n " ;
const char code2 [ ] = " void foo() \n "
" { \n "
" const wchar_t *a = \n "
" { \n "
" \" hello world \" \n "
" }; \n "
2009-02-08 09:52:03 +01:00
" } \n " ;
2012-09-01 13:12:47 +02:00
Settings settings ;
settings . platform ( Settings : : Unspecified ) ;
Tokenizer tokenizer ( & settings , this ) ;
std : : istringstream istr ( code1 ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
ASSERT_EQUALS ( tok ( code2 ) , tokenizer . tokens ( ) - > stringifyList ( 0 , false ) ) ;
ASSERT_EQUALS ( true , tokenizer . tokens ( ) - > tokAt ( 13 ) & & tokenizer . tokens ( ) - > tokAt ( 13 ) - > isLong ( ) ) ;
2009-02-08 09:52:03 +01:00
}
2009-02-12 21:32:59 +01:00
2011-10-13 20:53:06 +02:00
void double_plus ( ) {
2009-02-12 21:32:59 +01:00
{
const char code1 [ ] = " void foo( int a ) \n "
" { \n "
" a++; \n "
" a--; \n "
" ++a; \n "
" --a; \n "
" } \n " ;
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " void foo ( int a ) { a ++ ; a -- ; ++ a ; -- a ; } " , tok ( code1 ) ) ;
2009-02-12 21:32:59 +01:00
}
{
const char code1 [ ] = " void foo( int a ) \n "
" { \n "
" a=a+a; \n "
" } \n " ;
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " void foo ( int a ) { a = a + a ; } " , tok ( code1 ) ) ;
2009-02-12 21:32:59 +01:00
}
2009-02-12 21:49:05 +01:00
{
const char code1 [ ] = " void foo( int a, int b ) \n "
" { \n "
" a=a+++b; \n "
" } \n " ;
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " void foo ( int a , int b ) { a = a ++ + b ; } " , tok ( code1 ) ) ;
2009-02-12 21:49:05 +01:00
}
{
const char code1 [ ] = " void foo( int a, int b ) \n "
" { \n "
" a=a---b; \n "
" } \n " ;
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " void foo ( int a , int b ) { a = a -- - b ; } " , tok ( code1 ) ) ;
2009-02-12 21:49:05 +01:00
}
{
const char code1 [ ] = " void foo( int a, int b ) \n "
" { \n "
" a=a--+b; \n "
" } \n " ;
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " void foo ( int a , int b ) { a = a -- + b ; } " , tok ( code1 ) ) ;
2009-02-12 21:49:05 +01:00
}
{
const char code1 [ ] = " void foo( int a, int b ) \n "
" { \n "
" a=a++-b; \n "
" } \n " ;
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " void foo ( int a , int b ) { a = a ++ - b ; } " , tok ( code1 ) ) ;
2009-02-12 21:49:05 +01:00
}
{
const char code1 [ ] = " void foo( int a, int b ) \n "
" { \n "
" a=a+--b; \n "
" } \n " ;
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " void foo ( int a , int b ) { a = a + -- b ; } " , tok ( code1 ) ) ;
2009-02-12 21:49:05 +01:00
}
{
const char code1 [ ] = " void foo( int a, int b ) \n "
" { \n "
" a=a-++b; \n "
" } \n " ;
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " void foo ( int a , int b ) { a = a - ++ b ; } " , tok ( code1 ) ) ;
2009-02-12 21:49:05 +01:00
}
2009-02-12 21:32:59 +01:00
}
2009-02-14 21:56:08 +01:00
2011-10-13 20:53:06 +02:00
void redundant_plus ( ) {
2009-02-14 21:56:08 +01:00
{
const char code1 [ ] = " void foo( int a, int b ) \n "
" { \n "
" a=a + + b; \n "
" } \n " ;
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " void foo ( int a , int b ) { a = a + b ; } " , tok ( code1 ) ) ;
2009-02-14 21:56:08 +01:00
}
{
const char code1 [ ] = " void foo( int a, int b ) \n "
" { \n "
" a=a + + + b; \n "
" } \n " ;
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " void foo ( int a , int b ) { a = a + b ; } " , tok ( code1 ) ) ;
2009-02-14 21:56:08 +01:00
}
{
const char code1 [ ] = " void foo( int a, int b ) \n "
" { \n "
" a=a + - b; \n "
" } \n " ;
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " void foo ( int a , int b ) { a = a - b ; } " , tok ( code1 ) ) ;
2009-02-14 21:56:08 +01:00
}
{
const char code1 [ ] = " void foo( int a, int b ) \n "
" { \n "
" a=a - + b; \n "
" } \n " ;
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " void foo ( int a , int b ) { a = a - b ; } " , tok ( code1 ) ) ;
2009-02-14 21:56:08 +01:00
}
{
const char code1 [ ] = " void foo( int a, int b ) \n "
" { \n "
" a=a - - b; \n "
" } \n " ;
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " void foo ( int a , int b ) { a = a + b ; } " , tok ( code1 ) ) ;
2009-02-14 21:56:08 +01:00
}
{
const char code1 [ ] = " void foo( int a, int b ) \n "
" { \n "
" a=a - + - b; \n "
" } \n " ;
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " void foo ( int a , int b ) { a = a + b ; } " , tok ( code1 ) ) ;
2009-02-14 21:56:08 +01:00
}
{
const char code1 [ ] = " void foo( int a, int b ) \n "
" { \n "
" a=a - - - b; \n "
" } \n " ;
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " void foo ( int a , int b ) { a = a - b ; } " , tok ( code1 ) ) ;
2009-02-14 21:56:08 +01:00
}
}
2009-02-15 14:28:54 +01:00
2012-01-04 13:45:27 +01:00
void redundant_plus_numbers ( ) {
{
const char code1 [ ] = " void foo( int a ) \n "
" { \n "
" a=a + + 1; \n "
" } \n " ;
ASSERT_EQUALS ( " void foo ( int a ) { a = a + 1 ; } " , tok ( code1 ) ) ;
}
{
const char code1 [ ] = " void foo( int a ) \n "
" { \n "
" a=a + + + 1; \n "
" } \n " ;
ASSERT_EQUALS ( " void foo ( int a ) { a = a + 1 ; } " , tok ( code1 ) ) ;
}
{
const char code1 [ ] = " void foo( int a ) \n "
" { \n "
" a=a + - 1; \n "
" } \n " ;
ASSERT_EQUALS ( " void foo ( int a ) { a = a - 1 ; } " , tok ( code1 ) ) ;
}
{
const char code1 [ ] = " void foo( int a ) \n "
" { \n "
" a=a - + 1; \n "
" } \n " ;
ASSERT_EQUALS ( " void foo ( int a ) { a = a - 1 ; } " , tok ( code1 ) ) ;
}
{
const char code1 [ ] = " void foo( int a ) \n "
" { \n "
" a=a - - 1; \n "
" } \n " ;
ASSERT_EQUALS ( " void foo ( int a ) { a = a + 1 ; } " , tok ( code1 ) ) ;
}
{
const char code1 [ ] = " void foo( int a ) \n "
" { \n "
" a=a - + - 1; \n "
" } \n " ;
ASSERT_EQUALS ( " void foo ( int a ) { a = a + 1 ; } " , tok ( code1 ) ) ;
}
{
const char code1 [ ] = " void foo( int a ) \n "
" { \n "
" a=a - - - 1; \n "
" } \n " ;
ASSERT_EQUALS ( " void foo ( int a ) { a = a - 1 ; } " , tok ( code1 ) ) ;
}
}
2009-02-15 14:28:54 +01:00
2011-10-13 20:53:06 +02:00
void parentheses1 ( ) {
2009-10-05 16:34:38 +02:00
ASSERT_EQUALS ( " <= 110 ; " , tok ( " <= (10+100); " ) ) ;
2010-05-29 22:20:10 +02:00
ASSERT_EQUALS ( " while ( x ( ) == -1 ) { } " , tok ( " while((x()) == -1){ } " ) ) ;
2009-02-15 14:28:54 +01:00
}
2009-02-17 20:18:26 +01:00
2011-10-13 20:53:06 +02:00
void parenthesesVar ( ) {
2010-12-15 18:45:53 +01:00
// remove parentheses..
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " = p ; " , tok ( " = (p); " ) ) ;
ASSERT_EQUALS ( " if ( a < p ) { } " , tok ( " if(a<(p)){} " ) ) ;
ASSERT_EQUALS ( " void f ( ) { int p ; if ( p == -1 ) { } } " , tok ( " void f(){int p; if((p)==-1){}} " ) ) ;
2009-11-30 16:45:19 +01:00
ASSERT_EQUALS ( " void f ( ) { int p ; if ( p == -1 ) { } } " , tok ( " void f(){int p; if(-1==(p)){}} " ) ) ;
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " void f ( ) { int p ; if ( p ) { } } " , tok ( " void f(){int p; if((p)){}} " ) ) ;
2009-07-24 21:55:35 +02:00
ASSERT_EQUALS ( " return p ; " , tok ( " return (p); " ) ) ;
2009-10-05 13:10:09 +02:00
ASSERT_EQUALS ( " void f ( ) { int * p ; if ( ! * p ) { } } " , tok ( " void f(){int *p; if (*(p) == 0) {}} " ) ) ;
ASSERT_EQUALS ( " void f ( ) { int * p ; if ( ! * p ) { } } " , tok ( " void f(){int *p; if (*p == 0) {}} " ) ) ;
2010-01-31 09:52:19 +01:00
ASSERT_EQUALS ( " void f ( int & p ) { p = 1 ; } " , tok ( " void f(int &p) {(p) = 1;} " ) ) ;
2009-10-05 13:22:35 +02:00
ASSERT_EQUALS ( " void f ( ) { int p [ 10 ] ; p [ 0 ] = 1 ; } " , tok ( " void f(){int p[10]; (p)[0] = 1;} " ) ) ;
ASSERT_EQUALS ( " void f ( ) { int p ; if ( ! p ) { } } " , tok ( " void f(){int p; if ((p) == 0) {}} " ) ) ;
ASSERT_EQUALS ( " void f ( ) { int * p ; * p = 1 ; } " , tok ( " void f(){int *p; *(p) = 1;} " ) ) ;
2009-10-05 13:30:05 +02:00
ASSERT_EQUALS ( " void f ( ) { int p ; if ( p ) { } p = 1 ; } " , tok ( " void f(){int p; if ( p ) { } (p) = 1;} " ) ) ;
2009-02-27 07:07:38 +01:00
2010-12-15 18:45:53 +01:00
// keep parentheses..
2009-07-14 11:37:26 +02:00
ASSERT_EQUALS ( " = a ; " , tok ( " = (char)a; " ) ) ;
ASSERT_EQUALS ( " cast < char * > ( p ) " , tok ( " cast<char *>(p) " ) ) ;
2009-07-24 21:55:35 +02:00
ASSERT_EQUALS ( " return ( a + b ) * c ; " , tok ( " return (a+b)*c; " ) ) ;
2009-10-05 13:10:09 +02:00
ASSERT_EQUALS ( " void f ( ) { int p ; if ( 2 * p == 0 ) { } } " , tok ( " void f(){int p; if (2*p == 0) {}} " ) ) ;
ASSERT_EQUALS ( " void f ( ) { DIR * f ; f = opendir ( dirname ) ; if ( closedir ( f ) ) { } } " , tok ( " void f(){DIR * f = opendir(dirname);if (closedir(f)){}} " ) ) ;
2009-11-04 23:58:15 +01:00
ASSERT_EQUALS ( " void foo ( int p ) { if ( 0 <= p ) { ; } } " , tok ( " void foo(int p){if((p)>=0);} " ) ) ;
2009-02-25 20:55:24 +01:00
}
2011-10-13 20:53:06 +02:00
void declareVar ( ) {
2009-07-14 11:37:26 +02:00
const char code [ ] = " void f ( ) { char str [ 100 ] = \" 100 \" ; } " ;
2009-02-28 21:40:37 +01:00
ASSERT_EQUALS ( code , tok ( code ) ) ;
}
2009-02-17 20:18:26 +01:00
2011-10-13 20:53:06 +02:00
void declareArray ( ) {
2012-12-25 08:56:12 +01:00
const char code1 [ ] = " void f ( ) { char str [ ] = \" 100 \" ; } " ;
const char expected1 [ ] = " void f ( ) { char str [ 4 ] = \" 100 \" ; } " ;
ASSERT_EQUALS ( expected1 , tok ( code1 ) ) ;
const char code2 [ ] = " char str [ ] = \" \\ x00 \" ; " ;
const char expected2 [ ] = " char str [ 2 ] = \" \\ 0 \" ; " ;
2012-12-26 12:08:40 +01:00
ASSERT_EQUALS ( expected2 , tok ( code2 ) ) ;
const char code3 [ ] = " char str [ ] = \" \\ 0 \" ; " ;
const char expected3 [ ] = " char str [ 2 ] = \" \\ 0 \" ; " ;
ASSERT_EQUALS ( expected3 , tok ( code3 ) ) ;
2009-06-10 19:36:00 +02:00
}
2009-05-25 08:26:11 +02:00
2011-10-13 20:53:06 +02:00
void dontRemoveIncrement ( ) {
2009-05-25 14:21:58 +02:00
{
const char code [ ] = " void f(int a) \n "
" { \n "
" if (a > 10) \n "
" a = 5; \n "
" else \n "
" a = 10; \n "
" a++; \n "
" } \n " ;
2009-10-04 07:51:12 +02:00
ASSERT_EQUALS ( " void f ( int a ) { if ( 10 < a ) { a = 5 ; } else { a = 10 ; } a ++ ; } " , tok ( code ) ) ;
2009-05-25 14:21:58 +02:00
}
{
const char code [ ] = " void f(int a) \n "
" { \n "
" if (a > 10) \n "
" a = 5; \n "
" else \n "
" a = 10; \n "
" ++a; \n "
" } \n " ;
2009-10-04 07:51:12 +02:00
ASSERT_EQUALS ( " void f ( int a ) { if ( 10 < a ) { a = 5 ; } else { a = 10 ; } ++ a ; } " , tok ( code ) ) ;
2009-05-25 14:21:58 +02:00
}
}
2011-10-13 20:53:06 +02:00
void removePostIncrement ( ) {
2010-01-31 09:52:19 +01:00
const char code [ ] = " void f(int &c) \n "
2009-05-25 08:26:11 +02:00
" { \n "
2010-01-31 09:52:19 +01:00
" c = 0; \n "
2009-05-25 08:26:11 +02:00
" c++; \n "
" if (c>0) { c++; } \n "
" c++; \n "
" } \n " ;
2011-01-30 12:54:19 +01:00
TODO_ASSERT_EQUALS ( " void f ( int & c ) { c = 3 ; { ; } ; } " ,
" void f ( int & c ) { c = 1 ; { c ++ ; } c ++ ; } " , tok ( code ) ) ;
2009-05-25 08:26:11 +02:00
}
2011-10-13 20:53:06 +02:00
void removePreIncrement ( ) {
2009-12-07 22:42:30 +01:00
{
2010-01-31 09:52:19 +01:00
const char code [ ] = " void f(int &c) \n "
2009-12-07 22:42:30 +01:00
" { \n "
2010-01-31 09:52:19 +01:00
" c = 0; \n "
2009-12-07 22:42:30 +01:00
" ++c; \n "
" if (c>0) { ++c; } \n "
" ++c; \n "
" } \n " ;
2011-01-30 12:54:19 +01:00
TODO_ASSERT_EQUALS ( " void f ( int & c ) { c = 3 ; { ; } ; } " ,
" void f ( int & c ) { c = 1 ; { ++ c ; } ++ c ; } " , tok ( code ) ) ;
2009-12-07 22:42:30 +01:00
}
{
const char code [ ] = " void f() \n "
" { \n "
" char a[] = \" p \" ; \n "
" ++a[0]; \n "
" } \n " ;
2010-04-24 21:48:58 +02:00
ASSERT_EQUALS ( " void f ( ) { char a [ 2 ] = \" p \" ; ++ a [ 0 ] ; } " , tok ( code ) ) ;
2009-12-07 22:42:30 +01:00
}
2009-05-25 08:31:20 +02:00
}
2011-10-13 20:53:06 +02:00
std : : string elseif ( const char code [ ] ) {
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
2009-02-17 20:18:26 +01:00
2010-12-01 18:00:55 +01:00
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
std : : istringstream istr ( code ) ;
2012-02-26 02:44:16 +01:00
tokenizer . tokenize ( istr , " test.cpp " ) ;
2009-08-04 21:32:14 +02:00
tokenizer . elseif ( ) ;
2009-02-17 20:18:26 +01:00
return tokenizer . tokens ( ) - > stringifyList ( false ) ;
}
2011-10-13 20:53:06 +02:00
void elseif1 ( ) {
2009-02-17 20:18:26 +01:00
const char code [ ] = " else if(ab) { cd } else { ef }gh " ;
2009-03-03 21:17:23 +01:00
ASSERT_EQUALS ( " \n \n ##file 0 \n 1: else { if ( ab ) { cd } else { ef } } gh \n " , elseif ( code ) ) ;
2009-11-23 18:59:50 +01:00
// syntax error: assert there is no segmentation fault
ASSERT_EQUALS ( " \n \n ##file 0 \n 1: else if ( x ) { } \n " , elseif ( " else if (x) { } " ) ) ;
2009-12-08 00:22:24 +01:00
{
const char src [ ] = " void f(int g,int f) { \n "
" if(g==1) {poo();} \n "
" else if( g == 2 ) \n "
" { \n "
" if( f == 0 ){coo();} \n "
" else if( f==1) \n "
" goo(); \n "
" } \n "
" } " ;
const char expected [ ] = " void f ( int g , int f ) "
" { "
" if ( g == 1 ) { poo ( ) ; } "
" else { "
" if ( g == 2 ) "
" { "
" if ( ! f ) { coo ( ) ; } "
" else { "
" if ( f == 1 ) "
" { "
" goo ( ) ; "
" } "
" } "
" } "
" } "
" } " ;
ASSERT_EQUALS ( tok ( expected ) , tok ( src ) ) ;
}
2009-02-17 20:18:26 +01:00
}
2009-02-28 10:09:55 +01:00
2011-10-13 20:53:06 +02:00
void ifa_ifa ( ) {
2010-01-22 20:20:43 +01:00
ASSERT_EQUALS ( " int a ; if ( a ) { { ab } cd } " , tok ( " int a ; if (a) { if (a) { ab } cd } " , true ) ) ;
ASSERT_EQUALS ( " int a ; if ( a ) { { ab } cd } " , tok ( " int a ; if (unlikely(a)) { if (a) { ab } cd } " , true ) ) ;
}
2009-02-28 10:09:55 +01:00
2011-10-13 20:53:06 +02:00
unsigned int sizeofFromTokenizer ( const char type [ ] ) {
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
2009-10-27 19:23:45 +01:00
std : : istringstream istr ( " " ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
2010-04-09 21:40:37 +02:00
Token tok1 ( 0 ) ;
tok1 . str ( type ) ;
return tokenizer . sizeOfType ( & tok1 ) ;
2009-10-27 19:23:45 +01:00
}
2009-02-28 10:09:55 +01:00
2011-10-13 20:53:06 +02:00
void sizeof2 ( ) {
2009-02-28 10:09:55 +01:00
const char code [ ] = " void foo() \n "
" { \n "
" int i[4]; \n "
" sizeof(i); \n "
" sizeof(*i); \n "
" } \n " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " void foo ( ) { int i [ 4 ] ; 16 ; 4 ; } " , tok ( code ) ) ;
2009-02-28 10:09:55 +01:00
}
2011-10-13 20:53:06 +02:00
void sizeof3 ( ) {
2009-02-28 10:09:55 +01:00
const char code [ ] = " static int i[4]; \n "
" void f() \n "
" { \n "
" int i[10]; \n "
" sizeof(i); \n "
" } \n " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " static int i [ 4 ] ; void f ( ) { int i [ 10 ] ; 40 ; } " , tok ( code ) ) ;
2009-02-28 10:09:55 +01:00
}
2011-10-13 20:53:06 +02:00
void sizeof4 ( ) {
2009-09-20 22:38:32 +02:00
{
const char code [ ] = " int i[10]; \n "
" sizeof(i[0]); \n " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " int i [ 10 ] ; 4 ; " , tok ( code ) ) ;
2009-09-20 22:38:32 +02:00
}
{
const char code [ ] = " int i[10]; \n "
" sizeof i[0]; \n " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " int i [ 10 ] ; 4 ; " , tok ( code ) ) ;
2009-09-20 22:38:32 +02:00
}
2009-02-28 10:09:55 +01:00
}
2011-10-13 20:53:06 +02:00
void sizeof5 ( ) {
2009-02-28 10:09:55 +01:00
const char code [ ] =
2009-10-21 21:08:00 +02:00
" const char * names[2]; "
2010-08-25 22:10:21 +02:00
" for (int i = 0; i != sizeof(names[0]); i++) "
2009-02-28 10:09:55 +01:00
" {} " ;
2009-10-27 19:23:45 +01:00
std : : ostringstream expected ;
2010-08-25 22:10:21 +02:00
expected < < " const char * names [ 2 ] ; for ( int i = 0 ; i != " < < sizeofFromTokenizer ( " * " ) < < " ; i ++ ) { } " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected . str ( ) , tok ( code ) ) ;
2009-02-28 10:09:55 +01:00
}
2011-10-13 20:53:06 +02:00
void sizeof6 ( ) {
2009-02-28 10:09:55 +01:00
const char code [ ] = " ;int i; \n "
" sizeof(i); \n " ;
std : : ostringstream expected ;
2009-09-13 12:59:59 +02:00
expected < < " ; int i ; " < < sizeof ( int ) < < " ; " ;
2009-02-28 10:09:55 +01:00
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected . str ( ) , tok ( code ) ) ;
2009-02-28 10:09:55 +01:00
}
2011-10-13 20:53:06 +02:00
void sizeof7 ( ) {
2009-03-29 16:36:34 +02:00
const char code [ ] = " ;INT32 i[10]; \n "
" sizeof(i[0]); \n " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " ; INT32 i [ 10 ] ; sizeof ( i [ 0 ] ) ; " , tok ( code , true , Settings : : Unspecified ) ) ;
ASSERT_EQUALS ( " ; int i [ 10 ] ; 4 ; " , tok ( code , true , Settings : : Win32A ) ) ;
2009-03-29 16:36:34 +02:00
}
2011-10-13 20:53:06 +02:00
void sizeof8 ( ) {
2009-05-31 21:33:44 +02:00
{
const char code [ ] = " void f() \n "
" { \n "
" char* ptrs[2]; \n "
2010-01-31 09:52:19 +01:00
" a = sizeof( ptrs ); \n "
2009-05-31 21:33:44 +02:00
" } \n " ;
std : : ostringstream oss ;
2009-10-27 19:23:45 +01:00
oss < < ( sizeofFromTokenizer ( " * " ) * 2 ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " void f ( ) { char * ptrs [ 2 ] ; a = " + oss . str ( ) + " ; } " , tok ( code ) ) ;
2009-05-31 21:33:44 +02:00
}
{
const char code [ ] = " void f() \n "
" { \n "
" char* ptrs[55]; \n "
2010-01-31 09:52:19 +01:00
" a = sizeof( ptrs ); \n "
2009-05-31 21:33:44 +02:00
" } \n " ;
std : : ostringstream oss ;
2009-10-27 19:23:45 +01:00
oss < < ( sizeofFromTokenizer ( " * " ) * 55 ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " void f ( ) { char * ptrs [ 55 ] ; a = " + oss . str ( ) + " ; } " , tok ( code ) ) ;
2009-05-31 21:33:44 +02:00
}
{
const char code [ ] = " void f() \n "
" { \n "
" char* ptrs; \n "
2010-01-31 09:52:19 +01:00
" a = sizeof( ptrs ); \n "
2009-05-31 21:33:44 +02:00
" } \n " ;
std : : ostringstream oss ;
2009-10-27 19:23:45 +01:00
oss < < sizeofFromTokenizer ( " * " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " void f ( ) { a = " + oss . str ( ) + " ; } " , tok ( code ) ) ;
2009-05-31 21:33:44 +02:00
}
}
2011-10-13 20:53:06 +02:00
void sizeof9 ( ) {
2009-09-06 13:28:25 +02:00
// ticket #487
{
2009-10-07 09:54:34 +02:00
const char code [ ] = " ; const char *str = \" 1 \" ; sizeof(str); " ;
2009-09-06 13:28:25 +02:00
std : : ostringstream expected ;
2009-10-27 19:23:45 +01:00
expected < < " ; const char * str ; str = \" 1 \" ; " < < sizeofFromTokenizer ( " * " ) < < " ; " ;
2009-09-06 13:28:25 +02:00
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected . str ( ) , tok ( code ) ) ;
2009-09-06 13:28:25 +02:00
}
{
2009-10-07 09:54:34 +02:00
const char code [ ] = " ; const char str[] = \" 1 \" ; sizeof(str); " ;
2009-09-06 13:28:25 +02:00
std : : ostringstream expected ;
2010-04-24 21:48:58 +02:00
expected < < " ; const char str [ 2 ] = \" 1 \" ; " < < sizeofFromTokenizer ( " char " ) * 2 < < " ; " ;
2009-09-06 13:28:25 +02:00
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected . str ( ) , tok ( code ) ) ;
2009-09-06 13:28:25 +02:00
}
{
2010-04-04 19:28:15 +02:00
// Ticket #799
2009-10-07 09:54:34 +02:00
const char code [ ] = " ; const char str[] = {'1'}; sizeof(str); " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " ; const char str [ 1 ] = { '1' } ; 1 ; " , tok ( code ) ) ;
2009-09-06 13:28:25 +02:00
}
2009-09-22 20:50:00 +02:00
2010-10-12 21:52:02 +02:00
{
// Ticket #2087
const char code [ ] = " ; const char str[] = { \" abc \" }; sizeof(str); " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " ; const char str [ 4 ] = \" abc \" ; 4 ; " , tok ( code ) ) ;
2010-10-12 21:52:02 +02:00
}
2009-09-22 20:50:00 +02:00
// ticket #716 - sizeof string
{
std : : ostringstream expected ;
expected < < " ; " < < ( sizeof " 123 " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected . str ( ) , tok ( " ; sizeof \" 123 \" " ) ) ;
ASSERT_EQUALS ( expected . str ( ) , tok ( " ; sizeof( \" 123 \" ) " ) ) ;
2009-09-22 20:50:00 +02:00
}
2009-09-23 17:59:26 +02:00
2009-10-07 11:05:44 +02:00
{
const char code [ ] = " void f(char *a,char *b, char *c) "
" {g(sizeof(a),sizeof(b),sizeof(c));} " ;
std : : ostringstream expected ;
expected < < " void f ( char * a , char * b , char * c ) { g ( " < <
2010-04-15 20:08:51 +02:00
sizeofFromTokenizer ( " * " ) < < " , " < < sizeofFromTokenizer ( " * " ) < < " , " < < sizeofFromTokenizer ( " * " ) < < " ) ; } " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected . str ( ) , tok ( code ) ) ;
2009-10-07 11:05:44 +02:00
}
{
const char code [ ] = " void f(char a,char b, char c) "
" {g(sizeof(a),sizeof(b),sizeof(c));} " ;
std : : ostringstream expected ;
expected < < " void f ( char a , char b , char c ) { g ( " < <
2010-04-15 20:08:51 +02:00
sizeofFromTokenizer ( " char " ) < < " , " < < sizeofFromTokenizer ( " char " ) < < " , " < < sizeofFromTokenizer ( " char " ) < < " ) ; } " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected . str ( ) , tok ( code ) ) ;
2009-10-07 11:05:44 +02:00
}
{
const char code [ ] = " void f(const char *a,const char *b, const char *c) "
" {g(sizeof(a),sizeof(b),sizeof(c));} " ;
std : : ostringstream expected ;
expected < < " void f ( const char * a , const char * b , const char * c ) { g ( " < <
2010-04-15 20:08:51 +02:00
sizeofFromTokenizer ( " * " ) < < " , " < < sizeofFromTokenizer ( " * " ) < < " , " < < sizeofFromTokenizer ( " * " ) < < " ) ; } " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected . str ( ) , tok ( code ) ) ;
2009-10-07 11:05:44 +02:00
}
{
const char code [ ] = " void f(char a[10],char b[10], char c[10]) "
" {g(sizeof(a),sizeof(b),sizeof(c));} " ;
std : : ostringstream expected ;
expected < < " void f ( char a [ 10 ] , char b [ 10 ] , char c [ 10 ] ) { g ( " < <
2010-04-15 20:08:51 +02:00
sizeofFromTokenizer ( " * " ) < < " , " < < sizeofFromTokenizer ( " * " ) < < " , " < < sizeofFromTokenizer ( " * " ) < < " ) ; } " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected . str ( ) , tok ( code ) ) ;
2009-10-07 11:05:44 +02:00
}
{
const char code [ ] = " void f(const char a[10],const char b[10], const char c[10]) "
" {g(sizeof(a),sizeof(b),sizeof(c));} " ;
std : : ostringstream expected ;
expected < < " void f ( const char a [ 10 ] , "
2010-04-15 20:08:51 +02:00
" const char b [ 10 ] , "
" const char c [ 10 ] ) { g ( " < <
sizeofFromTokenizer ( " * " ) < < " , " < < sizeofFromTokenizer ( " * " ) < < " , " < < sizeofFromTokenizer ( " * " ) < < " ) ; } " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected . str ( ) , tok ( code ) ) ;
2009-10-07 11:05:44 +02:00
}
{
const char code [ ] = " void f(const char *a[10],const char *b[10], const char *c[10]) "
" {g(sizeof(a),sizeof(b),sizeof(c));} " ;
std : : ostringstream expected ;
expected < < " void f ( const char * a [ 10 ] , "
2010-04-15 20:08:51 +02:00
" const char * b [ 10 ] , "
" const char * c [ 10 ] ) { g ( " < <
sizeofFromTokenizer ( " * " ) < < " , " < < sizeofFromTokenizer ( " * " ) < < " , " < < sizeofFromTokenizer ( " * " ) < < " ) ; } " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected . str ( ) , tok ( code ) ) ;
2009-10-07 11:05:44 +02:00
}
{
const char code [ ] = " void f(char *a[10],char *b[10], char *c[10]) "
" {g(sizeof(a),sizeof(b),sizeof(c));} " ;
std : : ostringstream expected ;
expected < < " void f ( char * a [ 10 ] , char * b [ 10 ] , char * c [ 10 ] ) { g ( " < <
2010-04-15 20:08:51 +02:00
sizeofFromTokenizer ( " * " ) < < " , " < < sizeofFromTokenizer ( " * " ) < < " , " < < sizeofFromTokenizer ( " * " ) < < " ) ; } " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected . str ( ) , tok ( code ) ) ;
2009-10-07 11:05:44 +02:00
}
2009-09-23 17:59:26 +02:00
{
std : : ostringstream expected ;
expected < < " ; " < < sizeof ( " \" quote \" " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected . str ( ) , tok ( " ; sizeof( \" \\ \" quote \\ \" \" ) " ) ) ;
2009-09-23 17:59:26 +02:00
}
2009-10-07 09:54:34 +02:00
{
std : : ostringstream expected ;
2010-04-02 07:30:58 +02:00
expected < < " void f ( ) { char str [ 100 ] = \" 100 \" ; " < < sizeofFromTokenizer ( " char " ) * 100 < < " } " ;
2009-10-07 09:54:34 +02:00
ASSERT_EQUALS ( expected . str ( ) , tok ( " void f ( ) { char str [ 100 ] = \" 100 \" ; sizeof ( str ) } " ) ) ;
}
2009-09-06 13:28:25 +02:00
}
2011-10-13 20:53:06 +02:00
void sizeof10 ( ) {
2009-10-10 14:26:03 +02:00
// ticket #809
const char code [ ] = " int m ; "
" compat_ulong_t um ; "
2011-09-18 16:31:31 +02:00
" long size ; size = sizeof ( m ) / sizeof ( um ) ; " ;
2009-10-10 14:26:03 +02:00
2011-09-24 20:51:03 +02:00
ASSERT_EQUALS ( code , tok ( code , true , Settings : : Win32A ) ) ;
2009-10-10 14:26:03 +02:00
}
2011-10-13 20:53:06 +02:00
void sizeof11 ( ) {
2009-10-18 18:06:32 +02:00
// ticket #827
const char code [ ] = " void f() \n "
" { \n "
" char buf2[4]; \n "
" sizeof buf2; \n "
" } \n "
" \n "
" void g() \n "
" { \n "
" struct A a[2]; \n "
" char buf[32]; \n "
" sizeof buf; \n "
" } " ;
const char expected [ ] = " void f ( ) "
" { "
" char buf2 [ 4 ] ; "
" 4 ; "
" } "
" "
" void g ( ) "
" { "
" struct A a [ 2 ] ; "
" char buf [ 32 ] ; "
" 32 ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void sizeof12 ( ) {
2009-10-21 21:08:00 +02:00
// ticket #827
const char code [ ] = " void f() \n "
" { \n "
" int *p; \n "
" (sizeof *p); \n "
" } " ;
const char expected [ ] = " void f ( ) "
" { "
2011-12-02 23:01:55 +01:00
" "
2009-10-21 21:08:00 +02:00
" 4 ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void sizeof13 ( ) {
2009-10-22 23:00:27 +02:00
// ticket #851
const char code [ ] = " int main() \n "
" { \n "
" char *a; \n "
" a = malloc(sizeof(*a)); \n "
" } \n "
" \n "
" struct B \n "
" { \n "
" char * b[2]; \n "
" }; " ;
const char expected [ ] = " int main ( ) "
" { "
" char * a ; "
" a = malloc ( 1 ) ; "
" } "
" struct B "
" { "
" char * b [ 2 ] ; "
" } ; " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void sizeof14 ( ) {
2009-11-13 20:07:44 +01:00
// ticket #954
const char code [ ] = " void f() \n "
" { \n "
" A **a; \n "
" int aa = sizeof *(*a)->b; \n "
" } \n " ;
const char expected [ ] = " void f ( ) "
" { "
" A * * a ; "
" int aa ; aa = sizeof ( * ( * a ) . b ) ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void sizeof15 ( ) {
2009-11-29 12:14:42 +01:00
// ticket #1020
tok ( " void f() \n "
" { \n "
" int *n; \n "
" sizeof *(n); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-11-29 12:14:42 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void sizeof16 ( ) {
2009-11-29 17:25:35 +01:00
// ticket #1027
const char code [ ] = " void f() \n "
" { \n "
" int a; \n "
" printf( \" %i \" , sizeof a++); \n "
" } \n " ;
ASSERT_EQUALS ( " void f ( ) { int a ; printf ( \" %i \" , sizeof ( a ++ ) ) ; } " , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void sizeof17 ( ) {
2009-12-06 08:46:59 +01:00
// ticket #1050
const char code [ ] = " void f() \n "
" { \n "
" sizeof 1; \n "
" while (0); \n "
" } \n " ;
2009-12-28 08:37:34 +01:00
ASSERT_EQUALS ( " void f ( ) { sizeof ( 1 ) ; } " , tok ( code ) ) ;
2009-12-06 08:46:59 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void sizeof18 ( ) {
2010-03-28 15:56:13 +02:00
{
2010-04-13 22:17:27 +02:00
std : : ostringstream expected ;
expected < < sizeof ( short int ) ;
2010-03-28 15:56:13 +02:00
{
const char code [ ] = " void f() \n "
" { \n "
" sizeof(short int); \n "
" } \n " ;
2010-04-13 22:17:27 +02:00
ASSERT_EQUALS ( " void f ( ) { " + expected . str ( ) + " ; } " , tok ( code ) ) ;
2010-03-28 15:56:13 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " void f() \n "
" { \n "
" sizeof(unsigned short int); \n "
" } \n " ;
2010-04-13 22:17:27 +02:00
ASSERT_EQUALS ( " void f ( ) { " + expected . str ( ) + " ; } " , tok ( code ) ) ;
2010-03-28 15:56:13 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " void f() \n "
" { \n "
" sizeof(short unsigned int); \n "
" } \n " ;
2010-04-13 22:17:27 +02:00
ASSERT_EQUALS ( " void f ( ) { " + expected . str ( ) + " ; } " , tok ( code ) ) ;
2010-03-28 15:56:13 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " void f() \n "
" { \n "
" sizeof(signed short int); \n "
" } \n " ;
2010-04-13 22:17:27 +02:00
ASSERT_EQUALS ( " void f ( ) { " + expected . str ( ) + " ; } " , tok ( code ) ) ;
2010-03-28 15:56:13 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
}
{
2010-04-13 22:17:27 +02:00
std : : ostringstream expected ;
expected < < sizeof ( long long ) ;
2010-03-28 15:56:13 +02:00
{
const char code [ ] = " void f() \n "
" { \n "
" sizeof(long long); \n "
" } \n " ;
2010-04-13 22:17:27 +02:00
ASSERT_EQUALS ( " void f ( ) { " + expected . str ( ) + " ; } " , tok ( code ) ) ;
2010-03-28 15:56:13 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " void f() \n "
" { \n "
" sizeof(signed long long); \n "
" } \n " ;
2010-04-13 22:17:27 +02:00
ASSERT_EQUALS ( " void f ( ) { " + expected . str ( ) + " ; } " , tok ( code ) ) ;
2010-03-28 15:56:13 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " void f() \n "
" { \n "
" sizeof(unsigned long long); \n "
" } \n " ;
2010-04-13 22:17:27 +02:00
ASSERT_EQUALS ( " void f ( ) { " + expected . str ( ) + " ; } " , tok ( code ) ) ;
2010-03-28 15:56:13 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " void f() \n "
" { \n "
" sizeof(long unsigned long); \n "
" } \n " ;
2010-04-13 22:17:27 +02:00
ASSERT_EQUALS ( " void f ( ) { " + expected . str ( ) + " ; } " , tok ( code ) ) ;
2010-03-28 15:56:13 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " void f() \n "
" { \n "
" sizeof(long long int); \n "
" } \n " ;
2010-04-13 22:17:27 +02:00
ASSERT_EQUALS ( " void f ( ) { " + expected . str ( ) + " ; } " , tok ( code ) ) ;
2010-03-28 15:56:13 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " void f() \n "
" { \n "
" sizeof(signed long long int); \n "
" } \n " ;
2010-04-13 22:17:27 +02:00
ASSERT_EQUALS ( " void f ( ) { " + expected . str ( ) + " ; } " , tok ( code ) ) ;
2010-03-28 15:56:13 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " void f() \n "
" { \n "
" sizeof(unsigned long long int); \n "
" } \n " ;
2010-04-13 22:17:27 +02:00
ASSERT_EQUALS ( " void f ( ) { " + expected . str ( ) + " ; } " , tok ( code ) ) ;
2010-03-28 15:56:13 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " void f() \n "
" { \n "
" sizeof(long unsigned long int); \n "
" } \n " ;
2010-04-13 22:17:27 +02:00
ASSERT_EQUALS ( " void f ( ) { " + expected . str ( ) + " ; } " , tok ( code ) ) ;
2010-03-28 15:56:13 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
}
2010-04-13 22:17:27 +02:00
{
const char code [ ] = " void f() \n "
" { \n "
" sizeof(char*); \n "
" } \n " ;
std : : ostringstream expected ;
expected < < sizeof ( int * ) ;
ASSERT_EQUALS ( " void f ( ) { " + expected . str ( ) + " ; } " , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " void f() \n "
" { \n "
" sizeof(unsigned int*); \n "
" } \n " ;
std : : ostringstream expected ;
expected < < sizeof ( int * ) ;
ASSERT_EQUALS ( " void f ( ) { " + expected . str ( ) + " ; } " , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-07-22 22:21:12 +02:00
}
}
2011-10-13 20:53:06 +02:00
void sizeof19 ( ) {
2010-07-22 22:21:12 +02:00
// ticket #1891 - sizeof 'x'
{
const char code [ ] = " void f() \n "
" { \n "
" sizeof 'x'; \n "
" } \n " ;
std : : ostringstream sz ;
sz < < sizeof ( ' x ' ) ;
ASSERT_EQUALS ( " void f ( ) { " + sz . str ( ) + " ; } " , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " void f() \n "
" { \n "
" sizeof('x'); \n "
" } \n " ;
std : : ostringstream sz ;
sz < < sizeof ( ' x ' ) ;
ASSERT_EQUALS ( " void f ( ) { " + sz . str ( ) + " ; } " , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-04-13 22:17:27 +02:00
}
2010-03-28 15:56:13 +02:00
}
2011-10-13 20:53:06 +02:00
void sizeof20 ( ) {
2010-09-06 21:34:51 +02:00
// ticket #2024 - sizeof a)
const char code [ ] = " struct struct_a { \n "
" char a[20]; \n "
" }; \n "
" \n "
" void foo() { \n "
" struct_a a; \n "
" append(sizeof a).append(); \n "
" } \n " ;
ASSERT_EQUALS ( " struct struct_a { char a [ 20 ] ; } ; "
" void foo ( ) { "
" struct_a a ; "
2012-09-16 08:20:43 +02:00
" append ( sizeof ( a ) ) . append ( ) ; "
2010-09-06 21:34:51 +02:00
" } " , tok ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void sizeof21 ( ) {
2010-11-29 17:46:10 +01:00
// ticket #2232 - sizeof...(Args)
const char code [ ] = " struct Internal { \n "
" int operator()(const Args&... args) const { \n "
" int n = sizeof...(Args); \n "
" return n; \n "
" } \n "
" }; \n "
" \n "
" int main() { \n "
" Internal internal; \n "
" int n = 0; n = internal(1); \n "
" return 0; \n "
" } \n " ;
// don't segfault
tok ( code ) ;
}
2011-10-13 20:53:06 +02:00
void sizeof22 ( ) {
2011-02-21 23:49:30 +01:00
// ticket #2599 segmentation fault
const char code [ ] = " sizeof \n " ;
// don't segfault
tok ( code ) ;
}
2011-10-13 20:53:06 +02:00
void sizeof23 ( ) {
2011-02-23 20:44:14 +01:00
// ticket #2604 segmentation fault
const char code [ ] = " sizeof <= A \n " ;
// don't segfault
tok ( code ) ;
}
2010-09-06 21:34:51 +02:00
2011-10-13 20:53:06 +02:00
void sizeofsizeof ( ) {
2010-05-14 18:40:15 +02:00
// ticket #1682
const char code [ ] = " void f() \n "
" { \n "
" sizeof sizeof 1; \n "
" } \n " ;
ASSERT_EQUALS ( " void f ( ) { sizeof sizeof ( 1 ) ; } " , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void casting ( ) {
2009-06-19 19:25:56 +02:00
{
const char code [ ] = " void f() \n "
" { \n "
" for (int i = 0; i < static_cast<int>(3); ++i) {} \n "
" } \n " ;
2009-09-13 12:59:59 +02:00
const std : : string expected ( " void f ( ) { for ( int i = 0 ; i < 3 ; ++ i ) { } } " ) ;
2009-03-05 20:32:02 +01:00
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2009-06-19 19:25:56 +02:00
}
{
const char code [ ] = " void f() \n "
" { \n "
" p = const_cast<char *> qtu (); \n "
" } \n " ;
2009-03-05 20:32:02 +01:00
2009-09-13 12:59:59 +02:00
const std : : string expected ( " void f ( ) { p = const_cast < char * > qtu ( ) ; } " ) ;
2009-06-19 19:25:56 +02:00
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2009-06-19 19:25:56 +02:00
}
2009-03-11 18:50:24 +01:00
2009-09-13 12:31:32 +02:00
{
// ticket #645
const char code [ ] = " void f() \n "
" { \n "
" return dynamic_cast<Foo *>((bar())); \n "
" } \n " ;
2009-09-13 12:59:59 +02:00
const std : : string expected ( " void f ( ) { return bar ( ) ; } " ) ;
2009-09-13 12:31:32 +02:00
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2009-09-13 12:31:32 +02:00
}
}
2009-03-11 18:50:24 +01:00
2011-10-13 20:53:06 +02:00
void strlen1 ( ) {
2009-11-07 08:25:39 +01:00
ASSERT_EQUALS ( " 4 " , tok ( " strlen( \" abcd \" ) " ) ) ;
{
const char code [ ] = " void f() \n "
" { \n "
" const char *s = \" abcd \" ; \n "
" strlen(s); \n "
" } \n " ;
const char expected [ ] = " void f ( ) "
" { "
" const char * s ; "
" s = \" abcd \" ; "
" 4 ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
{
const char code [ ] = " void f() \n "
" { \n "
" const char s [ ] = \" abcd \" ; \n "
" strlen(s); \n "
" } \n " ;
const char expected [ ] = " void f ( ) "
" { "
2010-04-24 21:48:58 +02:00
" const char s [ 5 ] = \" abcd \" ; "
2009-11-07 08:25:39 +01:00
" 4 ; "
" } " ;
2010-11-05 20:05:57 +01:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2009-11-07 08:25:39 +01:00
}
}
2013-06-25 20:10:40 +02:00
void strlen2 ( ) {
// #4530 - make sure calculation with strlen is simplified
ASSERT_EQUALS ( " i = -4 ; " ,
tok ( " i = (strlen( \" abcd \" ) - 8); " ) ) ;
}
2009-11-07 08:25:39 +01:00
2009-03-11 18:50:24 +01:00
2011-10-13 20:53:06 +02:00
void template1 ( ) {
2009-03-11 18:50:24 +01:00
const char code [ ] = " template <classname T> void f(T val) { T a; } \n "
" f<int>(10); " ;
2011-12-06 16:38:13 +01:00
const std : : string expected ( " f<int> ( 10 ) ; "
2011-12-02 23:01:55 +01:00
" void f<int> ( int val ) { } " ) ;
2009-03-11 19:14:45 +01:00
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2009-03-05 20:32:02 +01:00
}
2009-03-12 22:17:42 +01:00
2011-10-13 20:53:06 +02:00
void template2 ( ) {
2009-03-12 22:17:42 +01:00
const char code [ ] = " template <classname T> class Fred { T a; }; \n "
" Fred<int> fred; " ;
2011-12-06 16:38:13 +01:00
const std : : string expected ( " Fred<int> fred ; "
2009-03-12 22:17:42 +01:00
" class Fred<int> { int a ; } " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2009-03-12 22:17:42 +01:00
}
2009-03-13 00:07:05 +01:00
2011-10-13 20:53:06 +02:00
void template3 ( ) {
2009-03-14 21:26:32 +01:00
const char code [ ] = " template <classname T, int sz> class Fred { T data[sz]; }; \n "
" Fred<float,4> fred; " ;
2011-12-06 16:38:13 +01:00
const std : : string expected ( " Fred<float,4> fred ; "
2009-03-14 21:26:32 +01:00
" class Fred<float,4> { float data [ 4 ] ; } " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2009-03-14 21:26:32 +01:00
}
2011-10-13 20:53:06 +02:00
void template4 ( ) {
2009-03-15 17:07:05 +01:00
const char code [ ] = " template <classname T> class Fred { Fred(); }; \n "
" Fred<float> fred; " ;
2011-12-06 16:38:13 +01:00
const std : : string expected ( " Fred<float> fred ; "
2009-03-15 17:07:05 +01:00
" class Fred<float> { Fred<float> ( ) ; } " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2009-03-15 17:07:05 +01:00
}
2011-10-13 20:53:06 +02:00
void template5 ( ) {
2009-03-15 20:03:29 +01:00
const char code [ ] = " template <classname T> class Fred { }; \n "
" template <classname T> Fred<T>::Fred() { } \n "
" Fred<float> fred; " ;
2012-07-29 16:01:05 +02:00
const std : : string expected ( " template < classname T > Fred < T > :: Fred ( ) { } " // <- TODO: this should be removed
" Fred<float> fred ; "
2009-03-15 20:03:29 +01:00
" class Fred<float> { } "
" Fred<float> :: Fred<float> ( ) { } " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2009-03-15 20:03:29 +01:00
}
2011-10-13 20:53:06 +02:00
void template6 ( ) {
2009-05-05 20:16:57 +02:00
const char code [ ] = " template <classname T> class Fred { }; \n "
" Fred<float> fred1; \n "
" Fred<float> fred2; " ;
2011-12-06 16:38:13 +01:00
const std : : string expected ( " Fred<float> fred1 ; "
" Fred<float> fred2 ; "
" class Fred<float> { } " ) ;
2009-05-05 20:16:57 +02:00
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2009-05-05 20:16:57 +02:00
}
2011-10-13 20:53:06 +02:00
void template7 ( ) {
2009-05-06 21:03:11 +02:00
// A template class that is not used => no simplification
2009-05-05 22:09:15 +02:00
{
const char code [ ] = " template <class T> \n "
" class ABC \n "
" { \n "
" public: \n "
" typedef ABC<T> m; \n "
2009-05-06 21:03:11 +02:00
" }; \n " ;
2009-05-05 22:09:15 +02:00
2012-07-29 16:01:05 +02:00
const std : : string expected ( " template < class T > class ABC { public: } ; " ) ;
2009-05-05 22:09:15 +02:00
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2009-05-05 22:09:15 +02:00
}
{
const char code [ ] = " template <typename T> class ABC { \n "
" public: \n "
" typedef std::vector<T> type; \n "
" }; \n "
" int main() { \n "
" ABC<int>::type v; \n "
" v.push_back(4); \n "
" return 0; \n "
" } \n " ;
2012-07-29 16:01:05 +02:00
const std : : string wanted ( " template < typename T > class ABC { public: } ; "
" int main ( ) { "
2011-01-30 12:54:19 +01:00
" std :: vector < int > v ; "
" v . push_back ( 4 ) ; "
" return 0 ; "
" } " ) ;
2012-07-29 16:01:05 +02:00
const std : : string current ( " template < typename T > class ABC { public: } ; "
" int main ( ) { "
2011-01-30 12:54:19 +01:00
" ABC < int > :: type v ; "
" v . push_back ( 4 ) ; "
" return 0 ; "
2012-07-29 16:01:05 +02:00
" } " ) ;
2009-05-05 22:09:15 +02:00
2012-04-17 09:51:08 +02:00
TODO_ASSERT_EQUALS ( wanted , current , tok ( code ) ) ;
2009-05-05 22:09:15 +02:00
}
{
const char code [ ] = " template <typename T> class ABC { \n "
" public: \n "
" typedef std::vector<T> type; \n "
" void f() \n "
" { \n "
" ABC<int>::type v; \n "
" v.push_back(4); \n "
" } \n "
2009-07-08 09:38:59 +02:00
" }; \n " ;
2009-05-05 22:09:15 +02:00
2012-07-29 16:01:05 +02:00
const std : : string expected ( " template < typename T > class ABC { "
" public: void f ( ) { "
" ABC < int > :: type v ; "
" v . push_back ( 4 ) ; "
" } "
" } ; " ) ;
2009-05-05 22:09:15 +02:00
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2009-05-05 22:09:15 +02:00
}
}
2009-03-14 21:26:32 +01:00
2009-05-08 16:19:22 +02:00
// Template definitions but no usage => no expansion
2011-10-13 20:53:06 +02:00
void template8 ( ) {
2009-05-08 16:19:22 +02:00
const char code [ ] = " template<typename T> class A; \n "
" template<typename T> class B; \n "
" \n "
" typedef A<int> x; \n "
" typedef B<int> y; \n "
" \n "
" template<typename T> class A { \n "
" void f() { \n "
" B<T> a = B<T>::g(); \n "
" T b = 0; \n "
" if (b) \n "
" b = 0; \n "
" } \n "
" }; \n "
" \n "
" template<typename T> inline B<T> h() { return B<T>(); } \n " ;
2012-07-29 16:01:05 +02:00
ASSERT_EQUALS ( " template < typename T > class A ; "
" template < typename T > class B ; "
" template < typename T > class A { void f ( ) { B < T > a ; a = B < T > :: g ( ) ; T b ; b = 0 ; } } ; "
" template < typename T > B < T > h ( ) { return B < T > ( ) ; } " , tok ( code ) ) ;
2009-11-01 20:49:43 +01:00
2012-07-29 16:01:05 +02:00
ASSERT_EQUALS ( " class A { template < typename T > int foo ( T d ) ; } ; " , tok ( " class A{ template<typename T> int foo(T d);}; " ) ) ;
2009-05-08 16:19:22 +02:00
}
2011-10-13 20:53:06 +02:00
void template9 ( ) {
2009-05-09 08:02:59 +02:00
const char code [ ] = " template < typename T > class A { } ; \n "
" \n "
" void f ( ) { \n "
" A<int> a ; \n "
" } \n "
" \n "
" template < typename T > \n "
" class B { \n "
" void g ( ) { \n "
" A < T > b = A < T > :: h ( ) ; \n "
" } \n "
" } ; \n " ;
// The expected result..
2012-07-29 16:01:05 +02:00
std : : string expected ( " void f ( ) { A<int> a ; } "
" template < typename T > class B { void g ( ) { A<T> b ; b = A<T> :: h ( ) ; } } ; "
" class A<int> { } class A<T> { } " ) ;
2009-05-09 08:02:59 +02:00
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2009-05-09 08:02:59 +02:00
}
2011-10-13 20:53:06 +02:00
void template10 ( ) {
2009-06-14 11:28:25 +02:00
const char code [ ] = " template <int ui, typename T> T * foo() \n "
" { return new T[ui]; } \n "
" \n "
" void f ( ) \n "
" { \n "
" foo<3,int>(); \n "
" } \n " ;
// The expected result..
2011-12-06 16:38:13 +01:00
const std : : string expected ( " void f ( ) "
2009-11-01 19:03:52 +01:00
" { "
" foo<3,int> ( ) ; "
" } "
" int * foo<3,int> ( ) { return new int [ 3 ] ; } " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2009-06-14 11:28:25 +02:00
}
2009-05-08 16:19:22 +02:00
2011-10-13 20:53:06 +02:00
void template11 ( ) {
2009-06-14 14:57:47 +02:00
const char code [ ] = " template <int ui, typename T> T * foo() \n "
" { return new T[ui]; } \n "
" \n "
" void f ( ) \n "
" { \n "
" char * p = foo<3,char>(); \n "
" } \n " ;
// The expected result..
2011-12-06 16:38:13 +01:00
const std : : string expected ( " void f ( ) "
2009-11-01 19:03:52 +01:00
" { "
" char * p ; p = foo<3,char> ( ) ; "
" } "
" char * foo<3,char> ( ) { return new char [ 3 ] ; } " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2009-06-14 14:57:47 +02:00
}
2011-10-13 20:53:06 +02:00
void template12 ( ) {
2009-07-22 13:29:42 +02:00
const char code [ ] = " template <int x, int y, int z> \n "
" class A : public B<x, y, (x - y) ? ((y < z) ? 1 : -1) : 0> \n "
" { }; \n "
" \n "
" void f() \n "
" { \n "
" A<12,12,11> a; \n "
" } \n " ;
// The expected result..
2011-12-06 16:38:13 +01:00
const std : : string expected ( " void f ( ) "
2009-11-01 19:03:52 +01:00
" { "
" A<12,12,11> a ; "
" } "
" class A<12,12,11> : public B < 12 , 12 , 0 > "
" { } " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2009-07-22 13:29:42 +02:00
}
2011-10-13 20:53:06 +02:00
void template13 ( ) {
2009-09-14 21:49:41 +02:00
const char code [ ] = " class BB {}; \n "
" \n "
" template <class T> \n "
" class AA \n "
" { \n "
" public: \n "
" static AA<T> create(T* newObject); \n "
2013-05-01 13:28:01 +02:00
" static int size(); \n "
2009-09-14 21:49:41 +02:00
" }; \n "
" \n "
" class CC { public: CC(AA<BB>, int) {} }; \n "
" \n "
" class XX { \n "
" AA<CC> y; \n "
" public: \n "
" XX(); \n "
" }; \n "
" \n "
" XX::XX(): \n "
" y(AA<CC>::create(new CC(AA<BB>(), 0))) \n "
2013-05-01 13:28:01 +02:00
" {} \n "
" \n "
" int yy[AA<CC>::size()]; " ;
2009-09-14 21:49:41 +02:00
// Just run it and check that there are not assertions.
2012-04-17 09:51:08 +02:00
tok ( code ) ;
2009-09-14 21:49:41 +02:00
}
2011-10-13 20:53:06 +02:00
void template14 ( ) {
2009-10-31 12:34:52 +01:00
const char code [ ] = " template <> void foo<int *>() \n "
" { x(); } \n "
" \n "
" int main() \n "
" { \n "
" foo<int*>(); \n "
" } \n " ;
// The expected result..
const std : : string expected ( " void foo<int*> ( ) "
" { x ( ) ; } "
" int main ( ) "
" { foo<int*> ( ) ; } " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2009-11-01 15:56:00 +01:00
}
2011-10-13 20:53:06 +02:00
void template15 ( ) {
2009-11-01 15:56:00 +01:00
const char code [ ] = " template <unsigned int i> void a() \n "
" { \n "
" a<i-1>(); \n "
" } \n "
" \n "
" template <> void a<0>() \n "
" { } \n "
" \n "
" int main() \n "
" { \n "
" a<2>(); \n "
" return 0; \n "
" } \n " ;
// The expected result..
2011-12-06 16:38:13 +01:00
const std : : string expected ( " void a<0> ( ) { } "
2009-11-01 15:56:00 +01:00
" int main ( ) "
" { a<2> ( ) ; return 0 ; } "
" void a<2> ( ) { a<1> ( ) ; } "
" void a<1> ( ) { a<0> ( ) ; } " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2009-10-31 12:34:52 +01:00
}
2011-10-13 20:53:06 +02:00
void template16 ( ) {
2009-11-01 19:03:52 +01:00
const char code [ ] = " template <unsigned int i> void a() \n "
" { } \n "
" \n "
" template <unsigned int i> void b() \n "
" { a<i>(); } \n "
" \n "
" int main() \n "
" { \n "
" b<2>(); \n "
" return 0; \n "
" } \n " ;
2011-12-06 16:38:13 +01:00
const std : : string expected ( " int main ( ) { b<2> ( ) ; return 0 ; } "
2011-03-07 21:21:06 +01:00
" void b<2> ( ) { a<2> ( ) ; } "
" void a<i> ( ) { } "
" void a<2> ( ) { } " ) ;
2009-11-01 19:03:52 +01:00
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2009-12-22 20:10:08 +01:00
}
2011-10-13 20:53:06 +02:00
void template17 ( ) {
2009-12-22 20:10:08 +01:00
const char code [ ] = " template<class T> \n "
" class Fred \n "
" { \n "
" template<class T> \n "
" static shared_ptr< Fred<T> > CreateFred() \n "
" { \n "
" } \n "
" }; \n "
" \n "
" shared_ptr<int> i; \n " ;
2011-01-30 12:54:19 +01:00
// Assert that there is no segmentation fault..
2012-04-17 09:51:08 +02:00
tok ( code ) ;
2009-11-01 19:03:52 +01:00
}
2011-10-13 20:53:06 +02:00
void template18 ( ) {
2010-01-24 20:04:09 +01:00
const char code [ ] = " template <class T> class foo { T a; }; \n "
" foo<int> *f; " ;
2011-12-06 16:38:13 +01:00
const std : : string expected ( " foo<int> * f ; "
2010-01-24 20:04:09 +01:00
" class foo<int> { int a ; } " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2010-01-24 20:04:09 +01:00
}
2011-10-13 20:53:06 +02:00
void template19 ( ) {
2010-03-03 22:02:32 +01:00
const char code [ ] = " template <typename T> T & foo() \n "
" { static T temp; return temp; } \n "
" \n "
" void f ( ) \n "
" { \n "
" char p = foo<char>(); \n "
" } \n " ;
// The expected result..
2011-12-06 16:38:13 +01:00
const std : : string expected ( " void f ( ) "
2010-03-03 22:02:32 +01:00
" { "
" char p ; p = foo<char> ( ) ; "
" } "
" char & foo<char> ( ) { static char temp ; return temp ; } " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2010-03-03 22:02:32 +01:00
}
2011-10-13 20:53:06 +02:00
void template20 ( ) {
2010-06-12 08:32:05 +02:00
// Ticket #1788 - the destructor implementation is lost
const char code [ ] = " template <class T> class A \n "
" { \n "
" public: \n "
" ~A(); \n "
" }; \n "
" \n "
" template <class T> A<T>::~A() \n "
" { \n "
" } \n "
" \n "
" A<int> a; \n " ;
// The expected result..
2012-07-29 16:01:05 +02:00
const std : : string expected ( " template < class T > A < T > :: ~ A ( ) { } " // <- TODO: this should be removed
" A<int> a ; "
2010-06-12 08:32:05 +02:00
" class A<int> { public: ~ A<int> ( ) ; } "
" A<int> :: ~ A<int> ( ) { } " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2010-06-12 08:32:05 +02:00
}
2011-10-13 20:53:06 +02:00
void template21 ( ) {
2010-06-16 07:13:52 +02:00
{
const char code [ ] = " template <classname T> struct Fred { T a; }; \n "
" Fred<int> fred; " ;
2011-12-06 16:38:13 +01:00
const std : : string expected ( " Fred<int> fred ; "
2010-06-16 07:13:52 +02:00
" struct Fred<int> { int a ; } " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2010-06-16 07:13:52 +02:00
}
{
const char code [ ] = " template <classname T, int sz> struct Fred { T data[sz]; }; \n "
" Fred<float,4> fred; " ;
2011-12-06 16:38:13 +01:00
const std : : string expected ( " Fred<float,4> fred ; "
2010-06-16 07:13:52 +02:00
" struct Fred<float,4> { float data [ 4 ] ; } " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2010-06-16 07:13:52 +02:00
}
{
const char code [ ] = " template <classname T> struct Fred { Fred(); }; \n "
" Fred<float> fred; " ;
2011-12-06 16:38:13 +01:00
const std : : string expected ( " Fred<float> fred ; "
2010-06-16 07:13:52 +02:00
" struct Fred<float> { Fred<float> ( ) ; } " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2010-06-16 07:13:52 +02:00
}
{
const char code [ ] = " template <classname T> struct Fred { }; \n "
" Fred<float> fred1; \n "
" Fred<float> fred2; " ;
2011-12-06 16:38:13 +01:00
const std : : string expected ( " Fred<float> fred1 ; "
" Fred<float> fred2 ; "
" struct Fred<float> { } " ) ;
2010-06-16 07:13:52 +02:00
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2010-06-16 07:13:52 +02:00
}
}
2011-10-13 20:53:06 +02:00
void template22 ( ) {
2011-02-20 12:22:01 +01:00
const char code [ ] = " template <classname T> struct Fred { T a; }; \n "
" Fred<std::string> fred; " ;
2011-02-20 12:17:05 +01:00
2011-12-06 16:38:13 +01:00
const std : : string expected ( " Fred<std::string> fred ; "
2011-02-20 12:22:01 +01:00
" struct Fred<std::string> { std :: string a ; } " ) ;
2011-02-20 12:17:05 +01:00
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-02-20 12:22:01 +01:00
}
2011-02-20 12:17:05 +01:00
2011-10-13 20:53:06 +02:00
void template23 ( ) {
2011-03-06 21:15:42 +01:00
const char code [ ] = " template <classname T> void foo() { } \n "
" void bar() { \n "
" std::cout << (foo<double>()); \n "
" } " ;
2011-12-06 16:38:13 +01:00
const std : : string expected ( " void bar ( ) { "
2011-03-06 21:15:42 +01:00
" std :: cout << ( foo<double> ( ) ) ; "
" } "
" void foo<double> ( ) { } " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:15:42 +01:00
}
2011-10-13 20:53:06 +02:00
void template24 ( ) {
2011-03-16 22:16:30 +01:00
// #2648
const char code [ ] = " template<int n> struct B \n "
" { \n "
" int a[n]; \n "
" }; \n "
" \n "
" template<int x> class bitset: B<sizeof(int)> \n "
" {}; \n "
" \n "
" bitset<1> z; " ;
2011-12-06 16:38:13 +01:00
const char expected [ ] = " bitset<1> z ; "
2011-03-16 22:16:30 +01:00
" class bitset<1> : B<4> { } "
" struct B<4> { int a [ 4 ] ; } " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-16 22:16:30 +01:00
}
2011-02-20 12:17:05 +01:00
2011-10-13 20:53:06 +02:00
void template25 ( ) {
2011-03-17 21:45:03 +01:00
const char code [ ] = " template<int n> struct B \n "
" { \n "
" int a[n]; \n "
" }; \n "
" \n "
" template<int x> class bitset: B<((sizeof(int)) ? : 1)> \n "
" {}; \n "
" \n "
" bitset<1> z; " ;
2012-07-29 16:01:05 +02:00
const char actual [ ] = " template < int n > struct B { int a [ n ] ; } ; "
" bitset<1> z ; "
2012-11-08 17:49:43 +01:00
" class bitset<1> : B < 4 > { } " ;
2011-03-17 21:45:03 +01:00
2011-12-06 16:38:13 +01:00
const char expected [ ] = " bitset<1> z ; "
2011-03-17 21:45:03 +01:00
" class bitset<1> : B<4> { } "
" struct B<4> { int a [ 4 ] ; } " ;
2012-04-17 09:51:08 +02:00
TODO_ASSERT_EQUALS ( expected , actual , tok ( code ) ) ;
2011-03-17 21:45:03 +01:00
}
2011-10-13 20:53:06 +02:00
void template26 ( ) {
2011-05-01 19:05:18 +02:00
// #2721
2011-05-02 15:08:16 +02:00
const char code [ ] = " template<class T> \n "
" class A { public: T x; }; \n "
" \n "
" template<class M> \n "
" class C: public A<char[M]> {}; \n "
" \n "
2011-05-01 19:05:18 +02:00
" C<2> a; \n " ;
// TODO: expand A also
2012-07-29 16:01:05 +02:00
ASSERT_EQUALS ( " template < class T > class A { public: T x ; } ; C<2> a ; class C<2> : public A < char [ 2 ] > { } " , tok ( code ) ) ;
2011-05-01 19:05:18 +02:00
}
2011-12-03 15:14:53 +01:00
void template27 ( ) {
// #3350 - template inside macro call
const char code [ ] = " X(template<class T> class Fred); " ;
2012-07-29 16:01:05 +02:00
ASSERT_EQUALS ( " X ( template < class T > class Fred ) ; " , tok ( code ) ) ;
2011-12-03 15:14:53 +01:00
}
2011-12-04 11:38:41 +01:00
void template28 ( ) {
// #3226 - inner template
const char code [ ] = " template<class A, class B> class Fred {}; \n "
" Fred<int,Fred<int,int> > x; \n " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " Fred<int,Fred<int,int>> x ; class Fred<int,int> { } class Fred<int,Fred<int,int>> { } " , tok ( code ) ) ;
2011-12-04 11:38:41 +01:00
}
2011-12-31 12:08:49 +01:00
void template29 ( ) {
// #3449 - garbage code (don't segfault)
const char code [ ] = " template<typename T> struct A; \n "
" struct B { template<typename T> struct C }; \n "
" {}; " ;
2012-07-29 16:01:05 +02:00
ASSERT_EQUALS ( " template < typename T > struct A ; struct B { template < typename T > struct C } ; { } ; " , tok ( code ) ) ;
2011-12-31 12:08:49 +01:00
}
2012-02-18 08:51:09 +01:00
void template30 ( ) {
// #3529 - template < template < ..
const char code [ ] = " template<template<class> class A, class B> void f(){} " ;
2012-07-29 16:01:05 +02:00
ASSERT_EQUALS ( " template < template < class > class A , class B > void f ( ) { } " , tok ( code ) ) ;
2012-02-18 08:51:09 +01:00
}
2012-08-05 17:11:39 +02:00
void template31 ( ) {
// #4010 - template reference type
const char code [ ] = " template<class T> struct A{}; A<int&> a; " ;
ASSERT_EQUALS ( " A<int&> a ; struct A<int&> { } " , tok ( code ) ) ;
}
2012-08-08 06:43:41 +02:00
void template32 ( ) {
// #3818 - mismatching template not handled well
const char code [ ] = " template <class T1, class T2, class T3, class T4 > struct A { }; \n "
" \n "
" template <class T> \n "
" struct B \n "
" { \n "
" public: \n "
" A < int, Pair<T, int>, int > a; \n " // mismatching parameters => don't instantiate
" }; \n "
" \n "
" B<int> b; \n " ;
ASSERT_EQUALS ( " template < class T1 , class T2 , class T3 , class T4 > struct A { } ; "
" B<int> b ; "
" struct B<int> { public: A < int , Pair < int , int > , int > a ; } " , tok ( code ) ) ;
}
2012-08-09 17:19:15 +02:00
void template33 ( ) {
2013-04-12 16:38:12 +02:00
{
// #3818 - inner templates in template instantiation not handled well
const char code [ ] = " template<class T> struct A { }; \n "
" template<class T> struct B { }; \n "
" template<class T> struct C { A<B<X<T> > > ab; }; \n "
" C<int> c; " ;
ASSERT_EQUALS ( " C<int> c ; "
" struct C<int> { A<B<X<int>>> ab ; } "
" struct B<X<int>> { } " // <- redundant.. but nevermind
" struct A<B<X<T>>> { } " // <- redundant.. but nevermind
" struct A<B<X<int>>> { } " , tok ( code ) ) ;
}
{
// #4544
const char code [ ] = " struct A { }; \n "
" template<class T> struct B { }; \n "
" template<class T> struct C { }; \n "
" C< B<A> > c; " ;
ASSERT_EQUALS ( " struct A { } ; "
" template < class T > struct B { } ; " // <- redundant.. but nevermind
" C<B<A>> c ; struct C<B<A>> { } " ,
tok ( code ) ) ;
}
2012-08-09 17:19:15 +02:00
}
2012-08-13 16:17:33 +02:00
void template34 ( ) {
// #3706 - namespace => hang
const char code [ ] = " namespace abc { \n "
" template <typename T> struct X { void f(X<T> &x) {} }; \n "
" } \n "
" template <> int X<int>::Y(0); " ;
ASSERT_EQUALS ( " namespace abc { "
" template < typename T > struct X { void f ( X < T > & x ) { } } ; "
" } "
" template < > int X < int > :: Y ( 0 ) ; " , tok ( code ) ) ;
}
2012-09-24 06:44:51 +02:00
void template35 ( ) { // #4074 - "A<'x'> a;" is not recognized as template instantiation
const char code [ ] = " template <char c> class A {}; \n "
2012-09-26 18:17:02 +02:00
" A<'x'> a; " ;
2012-09-24 06:44:51 +02:00
ASSERT_EQUALS ( " A<'x'> a ; class A<'x'> { } " , tok ( code ) ) ;
}
2013-04-07 16:48:42 +02:00
void template36 ( ) { // #4310 - Passing unknown template instantiation as template argument
const char code [ ] = " template <class T> struct X { T t; }; \n "
" template <class C> struct Y { Foo < X< Bar<C> > > _foo; }; \n " // <- Bar is unknown
" Y<int> bar; " ;
ASSERT_EQUALS ( " Y<int> bar ; "
" struct Y<int> { Foo < X<Bar<int>> > _foo ; } "
" struct X<Bar<int>> { Bar < int > t ; } " ,
tok ( code ) ) ;
}
2013-04-11 18:58:39 +02:00
void template37 ( ) { // #4544 - A<class B> a;
const char code [ ] = " class A { }; \n "
" template<class T> class B {}; \n "
" B<class A> b1; \n "
" B<A> b2; " ;
ASSERT_EQUALS ( " class A { } ; B<A> b1 ; B<A> b2 ; class B<A> { } " ,
tok ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void template_unhandled ( ) {
2010-03-16 19:53:09 +01:00
// An unhandled template usage should be simplified..
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " x<int> ( ) ; " , tok ( " x<int>(); " ) ) ;
2010-03-16 19:53:09 +01:00
}
2013-06-12 22:43:29 +02:00
void template38 ( ) { // #4832 - Crash on C++11 right angle brackets
const char code [ ] = " template <class T> class A { \n "
" T mT; \n "
" public: \n "
" void foo() {} \n "
" }; \n "
" \n "
" int main() { \n "
" A<A<BLA>> gna1; \n "
" A<BLA> gna2; \n "
" } \n " ;
tok ( code ) ; // Don't crash or freeze
}
2013-07-06 13:12:39 +02:00
void template39 ( ) { // #4742 - Used to freeze in 1.60
2013-07-07 10:26:20 +02:00
const char code [ ] = " template<typename T> struct vector { "
2013-07-06 13:12:39 +02:00
" operator T() const; "
" }; "
" void f() { "
" vector<vector<int>> v; "
" const vector<int> vi = static_cast<vector<int>>(v); "
" } " ;
tok ( code ) ;
}
2011-10-13 20:53:06 +02:00
void template_default_parameter ( ) {
2009-09-06 08:22:45 +02:00
{
const char code [ ] = " template <class T, int n=3> \n "
" class A \n "
" { T ar[n]; }; \n "
" \n "
" void f() \n "
" { \n "
" A<int,2> a1; \n "
" A<int> a2; \n "
" } \n " ;
2009-05-08 16:19:22 +02:00
2009-09-06 08:22:45 +02:00
// The expected result..
2011-12-06 16:38:13 +01:00
const std : : string expected ( " void f ( ) "
2009-11-01 19:03:52 +01:00
" { "
2009-09-06 08:22:45 +02:00
" A<int,2> a1 ; "
2009-11-01 19:03:52 +01:00
" A<int,3> a2 ; "
" } "
" class A<int,2> "
" { int ar [ 2 ] ; } "
" class A<int,3> "
" { int ar [ 3 ] ; } " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2009-09-06 08:22:45 +02:00
}
{
const char code [ ] = " template <class T, int n1=3, int n2=2> \n "
" class A \n "
" { T ar[n1+n2]; }; \n "
" \n "
" void f() \n "
" { \n "
" A<int> a1; \n "
" A<int,3> a2; \n "
" } \n " ;
// The expected result..
2011-12-06 16:38:13 +01:00
const std : : string expected ( " void f ( ) "
2009-11-01 19:03:52 +01:00
" { "
2009-09-06 08:22:45 +02:00
" A<int,3,2> a1 ; "
2009-11-01 19:03:52 +01:00
" A<int,3,2> a2 ; "
" } "
" class A<int,3,2> "
" { int ar [ 5 ] ; } " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2009-09-06 08:22:45 +02:00
}
2009-09-09 22:57:28 +02:00
{
const char code [ ] = " template <class T, int n=3> \n "
" class A \n "
" { T ar[n]; }; \n "
" \n "
" void f() \n "
" { \n "
" A<int,(int)2> a1; \n "
" A<int> a2; \n "
" } \n " ;
2011-01-30 12:54:19 +01:00
const std : : string wanted ( " template < class T , int n > "
" class A "
" { T ar [ n ] ; } ; "
" void f ( ) "
" { "
" A<int,(int)2> a1 ; "
" A<int,3> a2 ; "
" } "
" class A<int,2> "
" { int ar [ 2 ] ; } "
" class A<int,3> "
" { int ar [ 3 ] ; } " ) ;
2011-12-06 16:38:13 +01:00
const std : : string current ( " void f ( ) "
2011-01-30 12:54:19 +01:00
" { "
" A < int , ( int ) 2 > a1 ; "
" A<int,3> a2 ; "
" } "
" class A<int,3> "
" { int ar [ 3 ] ; } "
) ;
2012-04-17 09:51:08 +02:00
TODO_ASSERT_EQUALS ( wanted , current , tok ( code ) ) ;
2009-09-09 22:57:28 +02:00
}
2009-09-02 22:54:50 +02:00
}
2009-05-08 16:19:22 +02:00
2011-10-13 20:53:06 +02:00
void template_default_type ( ) {
2009-12-05 21:17:58 +01:00
const char code [ ] = " template <typename T, typename U=T> \n "
" class A \n "
" { \n "
" public: \n "
" void foo() { \n "
" int a; \n "
" a = static_cast<U>(a); \n "
" } \n "
" }; \n "
" \n "
" template <typename T> \n "
" class B \n "
" { \n "
" protected: \n "
" A<int> a; \n "
" }; \n "
" \n "
" class C \n "
" : public B<int> \n "
" { \n "
2009-11-22 13:38:45 +01:00
" }; \n " ;
2012-04-17 09:51:08 +02:00
tok ( code ) ;
2009-11-22 13:38:45 +01:00
2009-12-22 20:10:08 +01:00
//ASSERT_EQUALS("[file1.cpp:15]: (error) Internal error: failed to instantiate template. The checking continues anyway.\n", errout.str());
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-11-22 13:38:45 +01:00
}
2011-10-13 20:53:06 +02:00
void template_typename ( ) {
2010-10-02 13:28:44 +02:00
{
const char code [ ] = " template <class T> \n "
" void foo(typename T::t *) \n "
" { } " ;
2009-09-03 22:19:44 +02:00
2010-10-02 13:28:44 +02:00
// The expected result..
2012-07-29 16:01:05 +02:00
const std : : string expected ( " template < class T > void foo ( T :: t * ) { } " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2010-10-02 13:28:44 +02:00
}
{
const char code [ ] = " void f() { \n "
" x(sizeof typename); \n "
" type = 0; \n "
" } " ;
2012-04-16 19:51:07 +02:00
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " void f ( ) { x ( sizeof ( typename ) ) ; type = 0 ; } " , tok ( code ) ) ;
2010-10-02 13:28:44 +02:00
}
2009-09-03 22:19:44 +02:00
}
2009-05-08 16:19:22 +02:00
2011-10-29 19:45:47 +02:00
void template_constructor ( ) {
// #3152 - if template constructor is removed then there might be
// "no constructor" false positives
const char code [ ] = " class Fred { \n "
" template<class T> explicit Fred(T t) { } \n "
" } " ;
2012-07-29 16:01:05 +02:00
ASSERT_EQUALS ( " class Fred { template < class T > explicit Fred ( T t ) { } } " , tok ( code ) ) ;
2012-03-31 11:30:26 +02:00
// #3532
const char code2 [ ] = " class Fred { \n "
" template<class T> Fred(T t) { } \n "
" } " ;
2012-07-29 16:01:05 +02:00
ASSERT_EQUALS ( " class Fred { template < class T > Fred ( T t ) { } } " , tok ( code2 ) ) ;
2011-10-29 19:45:47 +02:00
}
2012-04-27 18:02:07 +02:00
unsigned int templateParameters ( const char code [ ] ) {
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
return TemplateSimplifier : : templateParameters ( tokenizer . tokens ( ) ) ;
}
void templateParameters ( ) {
// Test that the function TemplateSimplifier::templateParameters works
ASSERT_EQUALS ( 1U , templateParameters ( " <struct C> x; " ) ) ;
ASSERT_EQUALS ( 1U , templateParameters ( " <union C> x; " ) ) ;
ASSERT_EQUALS ( 1U , templateParameters ( " <const int> x; " ) ) ;
ASSERT_EQUALS ( 1U , templateParameters ( " <int const *> x; " ) ) ;
2012-04-27 20:51:55 +02:00
ASSERT_EQUALS ( 1U , templateParameters ( " <const struct C> x; " ) ) ;
2012-04-27 18:02:07 +02:00
}
2012-09-19 11:03:40 +02:00
void templateParameters1 ( ) {
2013-02-10 07:43:09 +01:00
// #4169 - segmentation fault (invalid code)
2012-09-19 11:03:40 +02:00
const char code [ ] = " volatile true , test < test < #ifdef __ppc__ true , " ;
// do not crash on invalid code
ASSERT_EQUALS ( 0 , templateParameters ( code ) ) ;
}
2012-04-27 18:02:07 +02:00
2011-10-13 20:53:06 +02:00
void namespaces ( ) {
2009-03-13 00:07:05 +01:00
{
2012-08-28 22:32:12 +02:00
const char code [ ] = " namespace std { } " ;
2009-03-13 00:07:05 +01:00
2012-09-03 15:20:34 +02:00
ASSERT_EQUALS ( " " , tok ( code ) ) ;
2009-03-13 00:07:05 +01:00
}
2012-11-04 23:14:41 +01:00
{
const char code [ ] = " ; namespace std { } " ;
ASSERT_EQUALS ( " ; " , tok ( code ) ) ;
}
2009-03-13 00:07:05 +01:00
{
2012-08-28 22:32:12 +02:00
const char code [ ] = " using namespace std; namespace a{ namespace b{ void f(){} } } " ;
2009-03-13 00:07:05 +01:00
2012-08-28 22:32:12 +02:00
const std : : string expected ( " namespace a { namespace b { void f ( ) { } } } " ) ;
2009-03-13 00:07:05 +01:00
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2009-03-13 00:07:05 +01:00
}
2009-03-30 23:59:33 +02:00
{
2012-08-28 22:32:12 +02:00
const char code [ ] = " namespace b{ void f(){} } " ;
2009-03-30 23:59:33 +02:00
2012-08-28 22:32:12 +02:00
const std : : string expected ( " namespace b { void f ( ) { } } " ) ;
2009-03-30 23:59:33 +02:00
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2009-03-30 23:59:33 +02:00
}
2010-01-01 21:06:30 +01:00
{
const char code [ ] = " void f(int namespace) { } " ;
const std : : string expected ( " void f ( int namespace ) { } " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2010-01-01 21:06:30 +01:00
}
2009-03-13 00:07:05 +01:00
}
2009-03-23 18:20:56 +01:00
2011-10-13 20:53:06 +02:00
std : : string simplifyIfAssign ( const char code [ ] ) {
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
Settings settings ;
2009-03-23 18:20:56 +01:00
// tokenize..
2010-12-01 18:00:55 +01:00
Tokenizer tokenizer ( & settings , this ) ;
2009-03-23 18:20:56 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyIfAssign ( ) ;
2012-04-16 19:51:07 +02:00
return tokenizer . tokens ( ) - > stringifyList ( 0 , false ) ;
2009-03-23 18:20:56 +01:00
}
2011-10-13 20:53:06 +02:00
void ifassign1 ( ) {
2009-09-30 20:42:14 +02:00
ASSERT_EQUALS ( " ; a = b ; if ( a ) { ; } " , simplifyIfAssign ( " ;if(a=b); " ) ) ;
2010-01-30 09:33:16 +01:00
ASSERT_EQUALS ( " ; a = b ( ) ; if ( a ) { ; } " , simplifyIfAssign ( " ;if((a=b())); " ) ) ;
2009-09-30 20:42:14 +02:00
ASSERT_EQUALS ( " ; a = b ( ) ; if ( ! ( a ) ) { ; } " , simplifyIfAssign ( " ;if(!(a=b())); " ) ) ;
ASSERT_EQUALS ( " ; a . x = b ( ) ; if ( ! ( a . x ) ) { ; } " , simplifyIfAssign ( " ;if(!(a->x=b())); " ) ) ;
2013-06-16 16:53:09 +02:00
ASSERT_EQUALS ( " void f ( ) { A ( ) a = b ; if ( a ) { ; } } " , simplifyIfAssign ( " void f() { A() if(a=b); } " ) ) ;
2009-11-04 23:58:15 +01:00
ASSERT_EQUALS ( " void foo ( int a ) { a = b ( ) ; if ( 0 <= a ) { ; } } " , tok ( " void foo(int a) {if((a=b())>=0);} " ) ) ;
2011-01-30 12:54:19 +01:00
TODO_ASSERT_EQUALS ( " void foo ( A a ) { a . c = b ( ) ; if ( 0 <= a . c ) { ; } } " ,
" void foo ( A a ) { a . c = b ( ) ; if ( a . c >= 0 ) { ; } } " ,
tok ( " void foo(A a) {if((a.c=b())>=0);} " ) ) ;
2009-03-24 18:23:21 +01:00
}
2011-10-13 20:53:06 +02:00
void ifAssignWithCast ( ) {
2010-04-13 21:57:52 +02:00
const char * code = " void foo() \n "
" { \n "
" FILE *f; \n "
" if( (f = fopen( \" foo \" , \" r \" )) == ((FILE*)NULL) ) \n "
" return(-1); \n "
" fclose(f); \n "
" } \n " ;
2013-01-16 15:37:07 +01:00
const char * expected = " void foo ( ) "
" { "
" FILE * f ; "
" f = fopen ( \" foo \" , \" r \" ) ; "
" if ( ! f ) "
" { "
" return -1 ; "
" } "
" fclose ( f ) ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2010-04-13 21:57:52 +02:00
}
2009-03-24 18:23:21 +01:00
2011-10-13 20:53:06 +02:00
void whileAssign1 ( ) {
2009-06-20 13:58:30 +02:00
ASSERT_EQUALS ( " ; a = b ; while ( a ) { b = 0 ; a = b ; } " , simplifyIfAssign ( " ;while(a=b) { b = 0; } " ) ) ;
2009-08-05 19:45:05 +02:00
ASSERT_EQUALS ( " ; a . b = c ; while ( a . b ) { c = 0 ; a . b = c ; } " , simplifyIfAssign ( " ;while(a.b=c) { c=0; } " ) ) ;
2009-11-12 20:04:11 +01:00
ASSERT_EQUALS ( " struct hfs_bnode * node ; "
" struct hfs_btree * tree ; "
" node = tree . node_hash [ i ++ ] ; "
" while ( node ) { node = tree . node_hash [ i ++ ] ; } " ,
tok ( " struct hfs_bnode *node; "
" struct hfs_btree *tree; "
" while ((node = tree->node_hash[i++])) { } " ) ) ;
2010-02-26 22:14:56 +01:00
ASSERT_EQUALS ( " char * s ; s = new char [ 10 ] ; while ( ! s ) { s = new char [ 10 ] ; } " ,
tok ( " char *s; while (0 == (s=new char[10])) { } " ) ) ;
2009-06-20 13:58:30 +02:00
}
2011-10-13 20:53:06 +02:00
void whileAssign2 ( ) {
2010-07-28 21:03:15 +02:00
// #1909 - Internal error
tok ( " void f() \n "
" { \n "
" int b; \n "
" while (b = sizeof (struct foo { int i0;})) \n "
" ; \n "
" if (!(0 <= b )) \n "
" ; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2009-06-20 13:58:30 +02:00
2012-10-06 12:49:24 +02:00
void whileAssign3 ( ) {
// #4254 - Variable id
const char code [ ] = " void f() { \n "
" int a; \n "
" while (a = x()); \n "
" } " ;
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: void f ( ) { \n "
" 2: int a@1 ; \n "
" 3: a@1 = x ( ) ; while ( a@1 ) { ; a@1 = x ( ) ; } \n "
" 4: } \n " , tokenizeDebugListing ( code , true , " test.c " ) ) ;
}
2009-03-24 18:23:21 +01:00
2011-10-13 20:53:06 +02:00
void ifnot ( ) {
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " if ( ! x ) { ; } " , tok ( " if(0==x); " , false ) ) ;
ASSERT_EQUALS ( " if ( ! x ) { ; } " , tok ( " if(x==0); " , false ) ) ;
ASSERT_EQUALS ( " if ( ! ( a = b ) ) { ; } " , tok ( " if(0==(a=b)); " , false ) ) ;
ASSERT_EQUALS ( " if ( ! a && b ( ) ) { ; } " , tok ( " if( 0 == a && b() ); " , false ) ) ;
ASSERT_EQUALS ( " if ( b ( ) && ! a ) { ; } " , tok ( " if( b() && 0 == a ); " , false ) ) ;
ASSERT_EQUALS ( " if ( ! ( a = b ) ) { ; } " , tok ( " if((a=b)==0); " , false ) ) ;
ASSERT_EQUALS ( " if ( ! x . y ) { ; } " , tok ( " if(x.y==0); " , false ) ) ;
ASSERT_EQUALS ( " if ( ! x ) { ; } " , tok ( " if((x==0)); " , false ) ) ;
ASSERT_EQUALS ( " if ( ( ! x ) && ! y ) { ; } " , tok ( " if((x==0) && y==0); " , false ) ) ;
ASSERT_EQUALS ( " if ( ! ( ! fclose ( fd ) ) ) { ; } " , tok ( " if(!(fclose(fd) == 0)); " , false ) ) ;
2009-03-23 18:20:56 +01:00
}
2009-05-31 10:42:27 +02:00
2011-10-13 20:53:06 +02:00
void not1 ( ) {
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " if ( ! p ) { ; } " , tok ( " if (not p); " , false ) ) ;
ASSERT_EQUALS ( " if ( p && ! q ) { ; } " , tok ( " if (p && not q); " , false ) ) ;
ASSERT_EQUALS ( " void foo ( not i ) " , tok ( " void foo ( not i ) " , false ) ) ;
2009-05-31 10:42:27 +02:00
}
2011-10-13 20:53:06 +02:00
void and1 ( ) {
2009-09-30 20:42:14 +02:00
ASSERT_EQUALS ( " if ( p && q ) { ; } " ,
2012-04-17 09:51:08 +02:00
tok ( " if (p and q) ; " , false ) ) ;
2009-08-29 14:26:01 +02:00
2009-09-30 20:42:14 +02:00
ASSERT_EQUALS ( " if ( foo ( ) && q ) { ; } " ,
2012-04-17 09:51:08 +02:00
tok ( " if (foo() and q) ; " , false ) ) ;
2009-08-29 15:29:19 +02:00
2009-09-30 20:42:14 +02:00
ASSERT_EQUALS ( " if ( foo ( ) && bar ( ) ) { ; } " ,
2012-04-17 09:51:08 +02:00
tok ( " if (foo() and bar()) ; " , false ) ) ;
2009-08-29 15:29:19 +02:00
2009-09-30 20:42:14 +02:00
ASSERT_EQUALS ( " if ( p && bar ( ) ) { ; } " ,
2012-04-17 09:51:08 +02:00
tok ( " if (p and bar()) ; " , false ) ) ;
2010-05-13 13:59:41 +02:00
2012-01-15 14:50:01 +01:00
ASSERT_EQUALS ( " if ( p && ! q ) { ; } " ,
2012-04-17 09:51:08 +02:00
tok ( " if (p and not q) ; " , false ) ) ;
2010-05-13 13:59:41 +02:00
}
2011-10-13 20:53:06 +02:00
void or1 ( ) {
2010-05-13 13:59:41 +02:00
ASSERT_EQUALS ( " if ( p || q ) { ; } " ,
2012-04-17 09:51:08 +02:00
tok ( " if (p or q) ; " , false ) ) ;
2010-05-13 13:59:41 +02:00
ASSERT_EQUALS ( " if ( foo ( ) || q ) { ; } " ,
2012-04-17 09:51:08 +02:00
tok ( " if (foo() or q) ; " , false ) ) ;
2010-05-13 13:59:41 +02:00
ASSERT_EQUALS ( " if ( foo ( ) || bar ( ) ) { ; } " ,
2012-04-17 09:51:08 +02:00
tok ( " if (foo() or bar()) ; " , false ) ) ;
2010-05-13 13:59:41 +02:00
ASSERT_EQUALS ( " if ( p || bar ( ) ) { ; } " ,
2012-04-17 09:51:08 +02:00
tok ( " if (p or bar()) ; " , false ) ) ;
2010-05-13 13:59:41 +02:00
2012-01-15 14:50:01 +01:00
ASSERT_EQUALS ( " if ( p || ! q ) { ; } " ,
2012-04-17 09:51:08 +02:00
tok ( " if (p or not q) ; " , false ) ) ;
2009-08-29 14:26:01 +02:00
}
2011-10-13 20:53:06 +02:00
void comma_keyword ( ) {
2009-06-18 23:00:16 +02:00
{
const char code [ ] = " void foo() \n "
" { \n "
" char *a, *b; \n "
" delete a, delete b; \n "
" } \n " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " void foo ( ) { char * a ; char * b ; delete a ; delete b ; } " , tok ( code ) ) ;
2009-06-18 23:00:16 +02:00
}
2009-08-06 23:31:39 +02:00
{
const char code [ ] = " void foo() \n "
" { \n "
" struct A *a, *b; \n "
" } \n " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " void foo ( ) { struct A * a ; struct A * b ; } " , tok ( code ) ) ;
2009-08-06 23:31:39 +02:00
}
{
const char code [ ] = " void foo() \n "
" { \n "
" struct A **a, **b; \n "
" } \n " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " void foo ( ) { struct A * * a ; struct A * * b ; } " , tok ( code ) ) ;
2009-08-06 23:31:39 +02:00
}
2009-06-18 23:00:16 +02:00
{
const char code [ ] = " void foo() \n "
" { \n "
" char *a, *b; \n "
" delete a, b; \n "
" } \n " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " void foo ( ) { char * a ; char * b ; delete a ; delete b ; } " , tok ( code ) ) ;
2009-06-18 23:00:16 +02:00
}
2009-07-26 13:00:11 +02:00
2009-08-06 22:11:29 +02:00
{
const char code [ ] = " void foo() \n "
" { \n "
" char **a, **b, **c; \n "
" } \n " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " void foo ( ) { char * * a ; char * * b ; char * * c ; } " , tok ( code ) ) ;
2009-08-06 22:11:29 +02:00
}
2009-07-26 13:00:11 +02:00
{
const char code [ ] = " int f() \n "
" { \n "
" if (something) \n "
" return a(2, c(3, 4)), b(3), 10; \n "
" return a(), b(0, 0, 0), 10; \n "
" } \n " ;
2009-09-13 12:59:59 +02:00
ASSERT_EQUALS ( " int f ( ) "
2009-07-26 13:00:11 +02:00
" { "
" if ( something ) "
" { "
2009-07-26 22:13:08 +02:00
" a ( 2 , c ( 3 , 4 ) ) ; "
" b ( 3 ) ; "
" return 10 ; "
2009-07-26 13:00:11 +02:00
" } "
" a ( ) ; "
" b ( 0 , 0 , 0 ) ; "
" return 10 ; "
2012-04-17 09:51:08 +02:00
" } " , tok ( code ) ) ;
2009-07-26 13:00:11 +02:00
}
2009-07-26 23:08:18 +02:00
{
const char code [ ] = " void foo() \n "
" { \n "
" delete [] a, a = 0; \n "
" } \n " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " void foo ( ) { delete [ ] a ; a = 0 ; } " , tok ( code ) ) ;
2009-07-27 21:13:08 +02:00
}
{
const char code [ ] = " void foo() \n "
" { \n "
" delete a, a = 0; \n "
" } \n " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " void foo ( ) { delete a ; a = 0 ; } " , tok ( code ) ) ;
2009-07-26 23:08:18 +02:00
}
2009-07-28 18:40:15 +02:00
{
const char code [ ] = " void f() \n "
" { \n "
" for(int a,b; a < 10; a = a + 1, b = b + 1); \n "
" } \n " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " void f ( ) { for ( int a , b ; a < 10 ; a = a + 1 , b = b + 1 ) { ; } } " , tok ( code ) ) ;
2009-07-28 18:40:15 +02:00
}
2009-08-09 10:16:37 +02:00
{
const char code [ ] = " void f() \n "
" { \n "
" char buf[BUFSIZ], **p; \n "
" char *ptrs[BUFSIZ], **pp; \n "
" } \n " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " void f ( ) { char buf [ BUFSIZ ] ; char * * p ; char * ptrs [ BUFSIZ ] ; char * * pp ; } " , tok ( code ) ) ;
2009-08-09 10:16:37 +02:00
}
2013-05-06 16:56:34 +02:00
{
// #4786 - don't replace , with ; in ".. : public B, C .." code
const char code [ ] = " template < class T = X > class A : public B , C { } ; " ;
ASSERT_EQUALS ( code , tok ( code ) ) ;
}
2009-06-10 23:12:26 +02:00
}
2009-05-31 10:42:27 +02:00
2011-10-13 20:53:06 +02:00
void remove_comma ( ) {
2009-07-04 17:06:25 +02:00
{
const char code [ ] = " void f() \n "
" { \n "
" int a,b; \n "
" if( a ) \n "
" a=0, \n "
" b=0; \n "
" } \n " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " void f ( ) { int a ; int b ; if ( a ) { a = 0 ; b = 0 ; } } " , tok ( code ) ) ;
2009-07-04 17:06:25 +02:00
}
{
const char code [ ] = " void f() \n "
" { \n "
" A a,b; \n "
" if( a.f ) \n "
" a.f=b.f, \n "
" a.g=b.g; \n "
" } \n " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " void f ( ) { A a ; A b ; if ( a . f ) { a . f = b . f ; a . g = b . g ; } } " , tok ( code ) ) ;
2009-07-04 17:06:25 +02:00
}
2009-09-12 08:12:00 +02:00
// keep the comma in template specifiers..
{
const char code [ ] = " void f() \n "
" { \n "
" int a = b<T<char,3>, int>(); \n "
" } \n " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " void f ( ) { int a ; a = b < T < char , 3 > , int > ( ) ; } " , tok ( code ) ) ;
2009-09-12 08:12:00 +02:00
}
2010-01-29 22:28:49 +01:00
{
// ticket #1327
const char code [ ] = " const C<1,2,3> foo () \n "
" { \n "
" return C<1,2,3>(x,y); \n "
" } \n " ;
const char expected [ ] = " const C < 1 , 2 , 3 > foo ( ) "
" { "
" return C < 1 , 2 , 3 > ( x , y ) ; "
" } " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2010-01-29 22:28:49 +01:00
}
2012-09-20 21:45:16 +02:00
{
const char code [ ] = " int foo () \n "
" { \n "
" return doSomething(), 0; \n "
" } \n " ;
const char expected [ ] = " int foo ( ) "
" { "
" doSomething ( ) ; return 0 ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2009-07-04 17:06:25 +02:00
}
2009-07-14 12:06:38 +02:00
2012-09-30 18:49:25 +02:00
void simplifyConditionOperator ( ) {
2009-07-22 13:29:42 +02:00
{
const char code [ ] = " ; x = a ? b : c; " ;
ASSERT_EQUALS ( " ; if ( a ) { x = b ; } else { x = c ; } " , tok ( code ) ) ;
}
{
const char code [ ] = " (0?(false?1:2):3) " ;
ASSERT_EQUALS ( " ( 3 ) " , tok ( code ) ) ;
}
{
const char code [ ] = " (1?(false?1:2):3) " ;
2009-07-23 10:12:02 +02:00
ASSERT_EQUALS ( " ( 2 ) " , tok ( code ) ) ;
}
2009-10-05 16:34:38 +02:00
{
const char code [ ] = " int a = (1?0:1 == 1?0:1); " ;
ASSERT_EQUALS ( " int a ; a = 0 ; " , tok ( code ) ) ;
}
{
const char code [ ] = " (1?0:foo()) " ;
ASSERT_EQUALS ( " ( 0 ) " , tok ( code ) ) ;
2012-10-13 02:32:43 +02:00
}
{
const char code [ ] = " void f () { switch(n) { case 1?0:foo(): break; }} " ;
ASSERT_EQUALS ( " void f ( ) { switch ( n ) { case 0 : ; break ; } } " , tok ( code ) ) ;
}
{
const char code [ ] = " void f () { switch(n) { case 1?0?1:0:foo(): break; }} " ;
ASSERT_EQUALS ( " void f ( ) { switch ( n ) { case 0 : ; break ; } } " , tok ( code ) ) ;
}
{
const char code [ ] = " void f () { switch(n) { case 0?foo():1: break; }} " ;
ASSERT_EQUALS ( " void f ( ) { switch ( n ) { case 1 : ; break ; } } " , tok ( code ) ) ;
2009-10-05 16:34:38 +02:00
}
2009-07-23 10:12:02 +02:00
{
const char code [ ] = " ( true ? a ( ) : b ( ) ) " ;
2009-10-05 16:34:38 +02:00
ASSERT_EQUALS ( " ( a ( ) ) " , tok ( code ) ) ;
2009-07-23 10:12:02 +02:00
}
{
const char code [ ] = " ( true ? abc . a : abc . b ) " ;
2009-10-05 16:34:38 +02:00
ASSERT_EQUALS ( " ( abc . a ) " , tok ( code ) ) ;
2009-07-22 13:29:42 +02:00
}
2009-10-20 22:58:46 +02:00
{
const char code [ ] = " void f() \n "
" { \n "
" bool x = false; \n "
" int b = x ? 44 : 3; \n "
" } \n " ;
2011-12-02 23:01:55 +01:00
ASSERT_EQUALS ( " void f ( ) { } " , tok ( code ) ) ;
2009-10-20 22:58:46 +02:00
}
2009-12-06 10:58:27 +01:00
{
const char code [ ] = " int vals[] = { 0x13, 1?0x01:0x00 }; " ;
2011-12-07 21:15:00 +01:00
ASSERT_EQUALS ( " int vals [ 2 ] = { 19 , 1 } ; " , tok ( code ) ) ;
2009-12-06 10:58:27 +01:00
}
{
const char code [ ] = " int vals[] = { 0x13, 0?0x01:0x00 }; " ;
2011-12-07 21:15:00 +01:00
ASSERT_EQUALS ( " int vals [ 2 ] = { 19 , 0 } ; " , tok ( code ) ) ;
2009-12-06 10:58:27 +01:00
}
2010-02-07 09:14:59 +01:00
{
const char code [ ] = " = 1 ? 0 : ({ 0; }); " ;
ASSERT_EQUALS ( " = 0 ; " , tok ( code ) ) ;
2012-11-08 17:49:43 +01:00
}
//GNU extension: "x ?: y" <-> "x ? x : y"
{
const char code [ ] = " ; a = 1 ? : x; b = 0 ? : 2; " ;
ASSERT_EQUALS ( " ; a = 1 ; b = 2 ; " , tok ( code ) ) ;
2010-02-07 09:14:59 +01:00
}
2011-06-30 21:04:26 +02:00
{
const char code [ ] = " int f(int b, int d) \n "
" { \n "
" d = b ? b : 10; \n "
" return d; \n "
" } \n " ;
ASSERT_EQUALS ( " int f ( int b , int d ) { if ( b ) { d = b ; } else { d = 10 ; } return d ; } " , tok ( code ) ) ;
}
{
const char code [ ] = " int f(int b, int *d) \n "
" { \n "
" *d = b ? b : 10; \n "
" return *d; \n "
" } \n " ;
ASSERT_EQUALS ( " int f ( int b , int * d ) { if ( b ) { * d = b ; } else { * d = 10 ; } return * d ; } " , tok ( code ) ) ;
}
{
const char code [ ] = " int f(int b, int *d) \n "
" { \n "
" if(b) {b++;} "
" *d = b ? b : 10; \n "
" return *d; \n "
" } \n " ;
ASSERT_EQUALS ( " int f ( int b , int * d ) { if ( b ) { b ++ ; } if ( b ) { * d = b ; } else { * d = 10 ; } return * d ; } " , tok ( code ) ) ;
}
2011-07-21 16:13:35 +02:00
{
// Ticket #2885
2012-01-31 22:32:28 +01:00
const char code [ ] = " ; const char *cx16 = has_cmpxchg16b ? \" -mcx16 \" : \" -mno-cx16 \" ; " ;
const char expected [ ] = " ; const char * cx16 ; if ( has_cmpxchg16b ) { cx16 = \" -mcx16 \" ; } else { cx16 = \" -mno-cx16 \" ; } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-07-21 16:13:35 +02:00
}
2012-01-31 22:32:28 +01:00
// Ticket #3572 (segmentation fault)
ASSERT_EQUALS ( " 0 ; x = { ? y : z ; } " , tok ( " 0; x = { ? y : z; } " ) ) ;
2012-08-17 16:37:25 +02:00
{
// #4019 - varid
const char code [ ] = " ; char *p; *p = a ? 1 : 0; " ;
const char expected [ ] = " \n \n ##file 0 \n "
" 1: ; char * p@1 ; if ( a ) { * p@1 = 1 ; } else { * p@1 = 0 ; } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
}
2012-08-18 22:11:48 +02:00
{
// #3922 - (true)
ASSERT_EQUALS ( " ; x = 2 ; " , tok ( " ; x = (true)?2:4; " ) ) ;
ASSERT_EQUALS ( " ; x = 4 ; " , tok ( " ; x = (false)?2:4; " ) ) ;
ASSERT_EQUALS ( " ; x = * a ; " , tok ( " ; x = (true)?*a:*b; " ) ) ;
ASSERT_EQUALS ( " ; x = * b ; " , tok ( " ; x = (false)?*a:*b; " ) ) ;
ASSERT_EQUALS ( " void f ( ) { return 1 ; } " , tok ( " void f() { char *p=0; return (p==0)?1:2; } " ) ) ;
}
2012-09-30 18:49:25 +02:00
// 4225 - varid gets lost
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: int a@1 ; int b@2 ; int c@3 ; int d@4 ; if ( b@2 ) { a@1 = c@3 ; } else { a@1 = d@4 ; } \n " ,
tokenizeDebugListing ( " int a, b, c, d; a = b ? (int *)c : d; " , true ) ) ;
2009-07-14 12:06:38 +02:00
}
2009-08-02 10:45:33 +02:00
2011-10-13 20:53:06 +02:00
void calculations ( ) {
2009-08-02 10:45:33 +02:00
{
const char code [ ] = " a[i+8+2] " ;
ASSERT_EQUALS ( " a [ i + 10 ] " , tok ( code ) ) ;
}
{
const char code [ ] = " a[8+2+i] " ;
ASSERT_EQUALS ( " a [ 10 + i ] " , tok ( code ) ) ;
}
{
const char code [ ] = " a[i + 2 * (2 * 4)] " ;
ASSERT_EQUALS ( " a [ i + 16 ] " , tok ( code ) ) ;
}
{
const char code [ ] = " a[i + 100 - 90] " ;
ASSERT_EQUALS ( " a [ i + 10 ] " , tok ( code ) ) ;
}
{
const char code [ ] = " a[1+1+1+1+1+1+1+1+1+1-2+5-3] " ;
ASSERT_EQUALS ( " a [ 10 ] " , tok ( code ) ) ;
}
{
const char code [ ] = " a[10+10-10-10] " ;
ASSERT_EQUALS ( " a [ 0 ] " , tok ( code ) ) ;
}
2009-08-28 12:13:46 +02:00
2011-02-14 20:43:26 +01:00
ASSERT_EQUALS ( " a [ 4 ] ; " , tok ( " a[1+3|4]; " ) ) ;
2009-08-28 12:13:46 +02:00
ASSERT_EQUALS ( " x = 1 + 2 * y ; " , tok ( " x=1+2*y; " ) ) ;
ASSERT_EQUALS ( " x = 7 ; " , tok ( " x=1+2*3; " ) ) ;
2009-09-15 22:38:54 +02:00
ASSERT_EQUALS ( " x = 47185 ; " , tok ( " x=(65536*72/100); " ) ) ;
2009-11-20 20:42:28 +01:00
ASSERT_EQUALS ( " x = 900 ; " , tok ( " x = 1500000 / ((145000 - 55000) * 1000 / 54000); " ) ) ;
2009-12-25 14:47:15 +01:00
ASSERT_EQUALS ( " int a [ 8 ] ; " , tok ( " int a[5+6/2]; " ) ) ;
ASSERT_EQUALS ( " int a [ 4 ] ; " , tok ( " int a[(10)-1-5]; " ) ) ;
2010-01-20 20:26:54 +01:00
ASSERT_EQUALS ( " int a [ i - 9 ] ; " , tok ( " int a[i - 10 + 1]; " ) ) ;
2010-05-29 20:27:08 +02:00
2010-11-05 20:35:31 +01:00
ASSERT_EQUALS ( " x = y ; " , tok ( " x=0+y+0-0; " ) ) ;
ASSERT_EQUALS ( " x = 0 ; " , tok ( " x=0*y; " ) ) ;
2010-05-29 20:27:08 +02:00
ASSERT_EQUALS ( " x = 501 ; " , tok ( " x = 1000 + 2 >> 1; " ) ) ;
ASSERT_EQUALS ( " x = 125 ; " , tok ( " x = 1000 / 2 >> 2; " ) ) ;
2010-08-30 21:06:22 +02:00
{
// Ticket #1997
const char code [ ] = " void * operator new[](size_t); " ;
2011-09-24 20:51:03 +02:00
ASSERT_EQUALS ( " void * operatornew[] ( long ) ; " , tok ( code , true , Settings : : Win32A ) ) ;
2010-08-30 21:06:22 +02:00
}
2010-12-09 19:35:52 +01:00
2010-12-23 18:59:18 +01:00
ASSERT_EQUALS ( " ; a [ 0 ] ; " , tok ( " ;a[0*(*p)]; " ) ) ;
2010-12-09 19:35:52 +01:00
ASSERT_EQUALS ( " ; " , tok ( " ; x = x + 0; " ) ) ;
2011-04-02 11:08:58 +02:00
2012-01-15 14:50:01 +01:00
ASSERT_EQUALS ( " if ( a == 2 ) { ; } " , tok ( " if (a==1+1); " ) ) ;
ASSERT_EQUALS ( " if ( a + 2 != 6 ) { ; } " , tok ( " if (a+1+1!=1+2+3); " ) ) ;
ASSERT_EQUALS ( " if ( 4 < a ) { ; } " , tok ( " if (14-2*5<a*4/(2*2)); " ) ) ;
2011-04-10 21:27:09 +02:00
ASSERT_EQUALS ( " ( y / 2 - 2 ) " , tok ( " (y / 2 - 2) " ) ) ;
2011-04-14 03:12:35 +02:00
ASSERT_EQUALS ( " ( y % 2 - 2 ) " , tok ( " (y % 2 - 2) " ) ) ;
2012-04-13 11:55:09 +02:00
ASSERT_EQUALS ( " ( 4 ) " , tok ( " (1 * 2 / 1 * 2) " ) ) ; // #3722
2009-08-02 10:45:33 +02:00
}
2013-06-14 18:51:52 +02:00
void comparisons ( ) {
ASSERT_EQUALS ( " ( 1 ) " , tok ( " ( 1 < 2 ) " ) ) ;
ASSERT_EQUALS ( " ( x ) " , tok ( " ( x && 1 < 2 ) " ) ) ;
ASSERT_EQUALS ( " ( 5 ) " , tok ( " ( 1 < 2 && 3 < 4 ? 5 : 6 ) " ) ) ;
ASSERT_EQUALS ( " ( 6 ) " , tok ( " ( 1 > 2 && 3 > 4 ? 5 : 6 ) " ) ) ;
}
2009-08-24 23:10:12 +02:00
2011-10-13 20:53:06 +02:00
void goto1 ( ) {
2009-08-24 23:10:12 +02:00
{
const char code [ ] = " void foo() \n "
" { \n "
" if (a()) \n "
" { \n "
" goto out; \n "
" } \n "
" b(); \n "
" out: \n "
" c(); \n "
" } " ;
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
2009-12-02 19:47:35 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyTokenList ( ) ;
const char expect [ ] = " \n \n ##file 0 \n "
" 1: void foo ( ) \n "
" 2: { \n "
" 3: if ( a ( ) ) \n "
" 4: { \n "
2012-08-07 10:22:11 +02:00
" 5: \n | \n 8: \n "
2009-12-02 19:47:35 +01:00
" 9: c ( ) ; return ; } \n "
" 7: b ( ) ; \n "
" 8: \n "
" 9: c ( ) ; \n "
" 10: } \n " ;
2009-08-24 23:10:12 +02:00
2012-03-01 18:38:20 +01:00
ASSERT_EQUALS ( expect , tokenizer . tokens ( ) - > stringifyList ( ) ) ;
2009-08-24 23:10:12 +02:00
}
{
const char code [ ] = " void foo() \n "
" { \n "
" if (a()) \n "
" goto out; \n "
" b(); \n "
" out: \n "
" if (c()) \n "
" d(); \n "
" } " ;
2009-12-02 19:47:35 +01:00
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
2009-12-02 19:47:35 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyTokenList ( ) ;
const char expect [ ] = " \n \n ##file 0 \n "
" 1: void foo ( ) \n "
" 2: { \n "
" 3: if ( a ( ) ) { \n "
" 4: \n 5: \n 6: \n "
" 7: if ( c ( ) ) { \n "
" 8: d ( ) ; } return ; } \n "
" 5: b ( ) ; \n "
" 6: \n "
" 7: if ( c ( ) ) { \n "
" 8: d ( ) ; } \n "
" 9: } \n " ;
2012-03-01 18:38:20 +01:00
ASSERT_EQUALS ( expect , tokenizer . tokens ( ) - > stringifyList ( ) ) ;
2009-08-24 23:10:12 +02:00
}
2009-08-26 18:46:56 +02:00
2009-09-30 13:30:53 +02:00
{
const char code [ ] = " void foo() \n "
" { \n "
" if (a()) \n "
" goto out; \n "
" b(); \n "
" out: \n "
" if (c()) \n "
" { \n "
" d(); \n "
" } \n "
" } " ;
const char expect [ ] = " void foo ( ) "
" { "
" if ( a ( ) ) "
" { "
" if ( c ( ) ) "
" { d ( ) ; } "
" return ; "
" } "
" b ( ) ; "
" if ( c ( ) ) "
" { d ( ) ; } "
" } " ;
ASSERT_EQUALS ( expect , tok ( code ) ) ;
}
2009-10-01 09:25:32 +02:00
{
const char code [ ] = " void foo(int x) \n "
" { \n "
" if (a()) \n "
" goto out; \n "
" b(); \n "
" out: \n "
" if (x) \n "
" { \n "
" x++; b[0]=x; \n "
" } \n "
" } " ;
const char expect [ ] = " void foo ( int x ) "
" { "
" if ( a ( ) ) "
" { "
" if ( x ) "
" { x ++ ; b [ 0 ] = x ; } "
" return ; "
" } "
" b ( ) ; "
" if ( x ) "
" { x ++ ; b [ 0 ] = x ; } "
" } " ;
ASSERT_EQUALS ( expect , tok ( code ) ) ;
}
2009-08-26 18:46:56 +02:00
{
const char code [ ] = " class NoLabels { bool varOne : 1 ; bool varTwo : 1 ; } ; " ;
2010-08-18 22:42:04 +02:00
const char expect [ ] = " class NoLabels { bool varOne ; bool varTwo ; } ; " ;
ASSERT_EQUALS ( expect , tok ( code ) ) ;
2009-08-26 18:46:56 +02:00
}
2009-08-26 18:54:28 +02:00
{
const char code [ ] = " void foo ( ) { int var ; var = x < y ? y : z ; } ; " ;
ASSERT_EQUALS ( code , tok ( code ) ) ;
}
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void goto2 ( ) {
2011-03-06 21:07:06 +01:00
// Don't simplify goto inside function call (macro)
const char code [ ] = " void f ( ) { slist_iter ( if ( a ) { goto dont_write ; } dont_write : ; x ( ) ; ) ; } " ;
ASSERT_EQUALS ( code , tok ( code ) ) ;
2011-10-18 23:56:35 +02:00
//ticket #3229 (segmentation fault)
ASSERT_EQUALS ( " void f ( ) { MACRO ( return ; ) return ; } " , tok ( " void f ( ) {goto label; label: MACRO(return;)} " ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-15 11:35:45 +02:00
void goto3 ( ) {
// Simplify goto inside the namespace|struct|class|union block
{
const char code [ ] = " namespace A1 "
" { "
" void foo() "
" { "
" goto source ; "
" bleeh; "
" source: "
" boo(); "
" } "
" } " ;
const char expected [ ] = " namespace A1 "
" { "
" void foo ( ) "
" { "
" boo ( ) ; return ; "
" } "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
{
const char code [ ] = " class A "
" { "
" int n,m; "
" A() "
" { "
" goto source ; "
" bleeh; "
" source: "
" boo(); "
" } "
" void boo(); "
" } " ;
const char expected [ ] = " class A "
" { "
" int n ; int m ; "
" A ( ) "
" { "
" boo ( ) ; return ; "
" } "
" void boo ( ) ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
{
const char code [ ] = " struct A "
" { "
" int n,m; "
" A() : m(0) "
" { "
" goto source; "
" bleeh; "
" source: "
" n=10; "
" } "
" } " ;
const char expected [ ] = " struct A "
" { "
" int n ; int m ; "
" A ( ) : m ( 0 ) "
" { "
" n = 10 ; return ; "
" } "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
{
const char code [ ] = " namespace A1 "
" { "
" class A "
" { "
" int n,m; "
" A() "
" { "
" goto source ; "
" bleeh; "
" source: "
" boo(); "
" } "
" void boo(); "
" } "
" } " ;
const char expected [ ] = " namespace A1 "
" { "
" class A "
" { "
" int n ; int m ; "
" A ( ) "
" { "
" boo ( ) ; return ; "
" } "
" void boo ( ) ; "
" } "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
{
const char code [ ] = " namespace A1 "
" { "
" namespace AA1 "
" { "
" void foo1() "
" { "
" goto source1 ; "
" bleeh; "
" source1: "
" boo1(); "
" } "
" } "
" namespace AA2 "
" { "
" void foo2() "
" { "
" goto source2 ; "
" bleeh; "
" source2: "
" boo2(); "
" } "
" } "
" } " ;
const char expected [ ] = " namespace A1 "
" { "
" namespace AA1 "
" { "
" void foo1 ( ) "
" { "
" boo1 ( ) ; return ; "
" } "
" } "
" namespace AA2 "
" { "
" void foo2 ( ) "
" { "
" boo2 ( ) ; return ; "
" } "
" } "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
{
const char code [ ] = " union A1 "
" { "
" int a; "
" double b; "
" A1() : b(3.22) "
" { "
" goto source ; "
" bleeh; "
" source: "
" a = 322; "
" } "
" } " ;
const char expected [ ] = " union A1 "
" { "
" int a ; "
" double b ; "
" A1 ( ) : b ( 3.22 ) "
" { "
" a = 322 ; return ; "
" } "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2012-01-04 16:50:38 +01:00
{
const char code [ ] = " class A "
" { "
" int a; "
" double b; "
" A() : b(3.22) "
" { "
" goto source ; "
" bleeh; "
" source: "
" a = 322; "
" } "
" } "
" class A1 : public A "
" { "
" int a1; "
" double b1; "
" A1() : b1(3.22) "
" { "
" goto source1 ; "
" bleeh1; "
" source1: "
" a = 322; "
" } "
" } " ;
const char expected [ ] = " class A "
" { "
" int a ; "
" double b ; "
" A ( ) : b ( 3.22 ) "
" { "
" a = 322 ; "
" return ; "
" } "
" } "
" class A1 : public A "
" { "
" int a1 ; "
" double b1 ; "
" A1 ( ) : b1 ( 3.22 ) "
" { "
" a = 322 ; "
" return ; "
" } "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2011-10-15 11:35:45 +02:00
}
2012-01-27 16:56:12 +01:00
void goto4 ( ) {
const char code [ ] = " int main() \n "
" { \n "
" goto SkipIncr; \n "
" do { \n "
" f(); \n "
" SkipIncr: \n "
" printf( \" . \" ); \n "
" } while (bar()); \n "
" } \n " ;
const char expected [ ] = " int main ( ) "
" { "
" goto SkipIncr ; "
" do { "
" f ( ) ; "
" SkipIncr : ; "
" printf ( \" . \" ) ; "
" } while ( bar ( ) ) ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2012-04-09 15:06:06 +02:00
void goto5 ( ) {
const char code [ ] = " int foo() { \n "
" goto err; \n "
" err: \n "
" return ( { __asm__(X); } ); "
" } \n " ;
ASSERT_EQUALS ( " int foo ( ) { "
" return { asm ( \" X \" ) ; } ; "
" return { asm ( \" X \" ) ; } ; "
" } " , tok ( code ) ) ;
}
2012-12-22 19:28:53 +01:00
void goto6 ( ) { // from code in linux that wasn't handled well
const char code1 [ ] = " static void a() { \n "
" unlock: \n "
" } \n "
" \n "
" static void b() { \n "
" if (c) \n "
" goto defer; \n "
" defer: \n "
" } \n " ;
ASSERT_EQUALS ( " static void a ( ) { } "
" static void b ( ) { if ( c ) { return ; } } " , tok ( code1 ) ) ;
const char code2 [ ] = " void a() \n "
" { \n "
" if (x) {} \n "
" unlock: \n "
" } \n "
" \n "
" void b() \n "
" { \n "
" { goto defer; } \n "
" defer: \n "
" } " ;
ASSERT_EQUALS ( " void a ( ) { if ( x ) { } } void b ( ) { return ; } " , tok ( code2 ) ) ;
}
2011-11-08 14:07:22 +01:00
void flowControl ( ) {
std : : list < std : : string > beforedead ;
2012-01-30 19:04:35 +01:00
//beforedead.push_back("return");
//beforedead.push_back("throw ( 10 )");
2011-12-06 22:56:12 +01:00
beforedead . push_back ( " exit ( 0 ) " ) ;
2012-01-30 19:04:35 +01:00
//beforedead.push_back("abort ( )");
//beforedead.push_back("goto labels");
//beforedead.push_back("break");
//beforedead.push_back("continue");
2011-10-13 12:26:27 +02:00
2011-11-08 14:07:22 +01:00
for ( std : : list < std : : string > : : iterator it = beforedead . begin ( ) ; it ! = beforedead . end ( ) ; + + it ) {
{
2011-11-12 12:01:31 +01:00
ASSERT_EQUALS ( " void f ( ) { " + * it + " ; } " , tok ( " void f() { " + * it + " ; foo(); } " ) ) ;
ASSERT_EQUALS ( " void f ( ) { " + * it + " ; } " , tok ( " void f() { " + * it + " ; if (m) foo(); } " ) ) ;
ASSERT_EQUALS ( " void f ( int n ) { if ( n ) { " + * it + " ; } foo ( ) ; } " , tok ( " void f(int n) { if (n) { " + * it + " ; } foo(); } " ) ) ;
ASSERT_EQUALS ( " void f ( ) { " + * it + " ; } " , tok ( " void f() { " + * it + " ; dead(); switch (n) { case 1: deadcode () ; default: deadcode (); } } " ) ) ;
2011-11-08 14:07:22 +01:00
ASSERT_EQUALS ( " int f ( int n ) { switch ( n ) { case 0 : ; " + * it + " ; default : ; " + * it + " ; } " + * it + " ; } " ,
tok ( " int f(int n) { switch (n) {case 0: " + * it + " ; n*=2; default: " + * it + " ; n*=6;} " + * it + " ; foo();} " ) ) ;
//ticket #3132
ASSERT_EQUALS ( " void f ( int i ) { goto label ; { label : ; " + * it + " ; } } " , tok ( " void f (int i) { goto label; switch(i) { label: " + * it + " ; } } " ) ) ;
//ticket #3148
ASSERT_EQUALS ( " void f ( ) { MACRO ( " + * it + " ) } " , tok ( " void f() { MACRO( " + * it + " ) } " ) ) ;
2013-01-13 12:02:10 +01:00
ASSERT_EQUALS ( " void f ( ) { MACRO ( " + * it + " ; , NULL ) } " , tok ( " void f() { MACRO( " + * it + " ;, NULL) } " ) ) ;
2011-11-08 14:07:22 +01:00
ASSERT_EQUALS ( " void f ( ) { MACRO ( bar1 , " + * it + " ) } " , tok ( " void f() { MACRO(bar1, " + * it + " ) } " ) ) ;
ASSERT_EQUALS ( " void f ( ) { MACRO ( " + * it + " ; bar2 , foo ) } " , tok ( " void f() { MACRO( " + * it + " ; bar2, foo) } " ) ) ;
}
{
std : : string code = " void f(){ "
" if (k>0) goto label; "
" " + * it + " ; "
" if (tnt) "
" { "
" { "
" check(); "
" k=0; "
" } "
" label: "
" bar(); "
" } "
" } " ;
ASSERT_EQUALS ( " void f ( ) { if ( 0 < k ) { goto label ; } " + * it + " ; { label : ; bar ( ) ; } } " , tok ( code ) ) ;
}
{
std : : string code = " void foo () { "
" " + * it + " ; "
" { "
" boo(); "
" while (n) { --n; } "
" { "
" label: "
" ok(); "
" } "
" } "
" } " ;
2011-11-12 12:01:31 +01:00
ASSERT_EQUALS ( " void foo ( ) { " + * it + " ; { label : ; ok ( ) ; } } " , tok ( code ) ) ;
2011-11-08 14:07:22 +01:00
}
2011-11-18 21:05:35 +01:00
{
std : : string code = " void foo () { "
" " + * it + " ; "
" switch (n) { "
" case 1: "
" label: "
" foo(); break; "
" default: "
" break; "
" } "
" } " ;
std : : string expected = " void foo ( ) { " + * it + " ; { label : ; foo ( ) ; break ; } } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2012-02-17 21:14:38 +01:00
{
std : : string code = " void foo () { "
" " + * it + " ; "
" switch (n) { "
" case 1: "
" { "
" foo(); "
" } "
" label: "
" bar(); "
" } "
" } " ;
std : : string expected = " void foo ( ) { " + * it + " ; { label : ; bar ( ) ; } } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
{
std : : string code = " void foo () { "
" " + * it + " ; "
" switch (n) { "
" case a: "
" { "
" foo(); "
" } "
" case b|c: "
" bar(); "
" } "
" } " ;
std : : string expected = " void foo ( ) { " + * it + " ; } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2011-11-18 21:05:35 +01:00
{
std : : string code = " void foo () { "
" " + * it + " ; "
" switch (n) { "
" case 1: "
" label: "
" foo(); break; "
" default: "
" break; break; "
" } "
" } " ;
2011-12-03 16:34:50 +01:00
std : : string expected = " void foo ( ) { " + * it + " ; { label : ; foo ( ) ; break ; } } " ;
2011-11-20 18:08:07 +01:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-11-18 21:05:35 +01:00
}
{
std : : string code = " void foo () { "
" " + * it + " ; "
" switch (n) { "
" case 1: "
" label: "
" foo(); break; break; "
" default: "
" break; "
" } "
" } " ;
2011-12-03 16:34:50 +01:00
std : : string expected = " void foo ( ) { " + * it + " ; { label : ; foo ( ) ; break ; } } " ;
2011-11-18 21:05:35 +01:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
{
std : : string code = " void foo () { "
" " + * it + " ; "
" switch (n) { "
" case 1: "
" label: "
" foo(); break; break; "
" default: "
" break; break; "
" } "
" } " ;
2011-12-03 16:34:50 +01:00
std : : string expected = " void foo ( ) { " + * it + " ; { label : ; foo ( ) ; break ; } } " ;
2011-11-18 21:05:35 +01:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2011-11-08 14:07:22 +01:00
{
std : : string code = " int f() { "
2011-12-09 20:47:51 +01:00
" switch (x) { case 1: " + * it + " ; bar(); tack; { ticak(); " + * it + " } " + * it + " ; "
2011-11-08 14:07:22 +01:00
" case 2: " + * it + " ; { random(); } tack(); "
" switch(y) { case 1: " + * it + " ; case 2: " + * it + " ; } "
" " + * it + " ; } " + * it + " ; } " ;
ASSERT_EQUALS ( " int f ( ) { switch ( x ) { case 1 : ; " + * it + " ; case 2 : ; " + * it + " ; } " + * it + " ; } " , tok ( code ) ) ;
}
{
std : : string code = " int f() { "
" switch (x) { case 1: " + * it + " ; bar(); tack; { ticak(); " + * it + " ; } " + * it + " ; "
" case 2: switch(y) { case 1: " + * it + " ; bar2(); foo(); case 2: " + * it + " ; } "
" " + * it + " ; } " + * it + " ; } " ;
std : : string expected = " int f ( ) { "
" switch ( x ) { case 1 : ; " + * it + " ; "
" case 2 : ; switch ( y ) { case 1 : ; " + * it + " ; case 2 : ; " + * it + " ; } "
" " + * it + " ; } " + * it + " ; } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
{
std : : string code = " void foo () { "
" switch (i) { case 0: switch (j) { case 0: " + * it + " ; } "
" case 1: switch (j) { case -1: " + * it + " ; } "
" case 2: switch (j) { case -2: " + * it + " ; } "
" case 3: if (blah6) { " + * it + " ;} break; } } " ;
std : : string expected = " void foo ( ) { "
" switch ( i ) { case 0 : ; switch ( j ) { case 0 : ; " + * it + " ; } "
" case 1 : ; switch ( j ) { case -1 : ; " + * it + " ; } "
" case 2 : ; switch ( j ) { case -2 : ; " + * it + " ; } "
" case 3 : ; if ( blah6 ) { " + * it + " ; } break ; } } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2011-11-18 21:05:35 +01:00
{
std : : string code = " void foo () { "
" " + * it + " ; "
" switch (i) { case 0: switch (j) { case 0: foo(); } "
" case 1: switch (j) { case -1: bar(); label:; ok(); } "
" case 3: if (blah6) { boo(); break; } } } " ;
std : : string expected = " void foo ( ) { " + * it + " ; { { label : ; ok ( ) ; } case 3 : ; if ( blah6 ) { boo ( ) ; break ; } } } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2011-11-20 18:08:07 +01:00
{
std : : string code = " void foo() { "
" switch ( t ) { "
" case 0: "
" if ( t ) switch ( b ) {} "
" break; "
" case 1: "
" " + * it + " ; "
" return 0; "
" } "
" return 0; "
" } " ;
std : : string expected = " void foo ( ) { "
" switch ( t ) { "
" case 0 : ; "
" if ( t ) { switch ( b ) { } } "
" break ; "
" case 1 : ; "
" " + * it + " ; "
" } "
" return 0 ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2011-11-08 14:07:22 +01:00
}
2012-01-30 19:04:35 +01:00
{
const char code [ ] = " void foo() \n "
" { \n "
" A *a = 0; \n "
" if (!a) { \n "
" nondeadcode; \n "
" return; \n "
" dead; \n "
" } \n "
" stilldead; \n "
" a->_a; \n "
" } \n " ;
const char expected [ ] = " void foo ( ) "
" { "
" A * a ; a = 0 ; { "
" nondeadcode ; "
" return ; "
" } "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2012-08-07 10:59:27 +02:00
{
const char code [ ] = " class Fred \n "
" { \n "
" public: \n "
" bool foo() const { return f; } \n "
" bool exit(); \n "
" \n "
" private: \n "
" bool f; \n "
" }; \n " ;
const char expected [ ] = " class Fred "
" { "
" public: "
" bool foo ( ) const { return f ; } "
" bool exit ( ) ; "
" "
" private: "
" bool f ; "
" } ; " ;
2012-08-10 17:43:09 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
{
const char code [ ] = " class abort { }; \n "
" \n "
" class Fred \n "
" { \n "
" public: \n "
" bool foo() const { return f; } \n "
" abort exit(); \n "
" \n "
" private: \n "
" bool f; \n "
" }; \n " ;
const char expected [ ] = " class abort { } ; "
" class Fred "
" { "
" public: "
" bool foo ( ) const { return f ; } "
" abort exit ( ) ; "
" "
" private: "
" bool f ; "
" } ; " ;
2012-08-07 10:59:27 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2011-10-15 01:34:07 +02:00
}
2011-10-13 20:53:06 +02:00
void strcat1 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " ; strcat(strcat(strcat(strcat(strcat(strcat(dst, \" this \" ), \" \" ), \" is \" ), \" a \" ), \" test \" ), \" . \" ); " ;
const char expect [ ] = " ; "
" strcat ( dst , \" this \" ) ; "
" strcat ( dst , \" \" ) ; "
" strcat ( dst , \" is \" ) ; "
" strcat ( dst , \" a \" ) ; "
" strcat ( dst , \" test \" ) ; "
" strcat ( dst , \" . \" ) ; " ;
ASSERT_EQUALS ( expect , tok ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void strcat2 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " ; strcat(strcat(dst, foo[0]), \" \" ); " ;
const char expect [ ] = " ; "
" strcat ( dst , foo [ 0 ] ) ; "
" strcat ( dst , \" \" ) ; " ;
ASSERT_EQUALS ( expect , tok ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyAtol ( ) {
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " a = std :: atol ( x ) ; " , tok ( " a = std::atol(x); " ) ) ;
ASSERT_EQUALS ( " a = atol ( \" text \" ) ; " , tok ( " a = atol( \" text \" ); " ) ) ;
ASSERT_EQUALS ( " a = 0 ; " , tok ( " a = std::atol( \" 0 \" ); " ) ) ;
ASSERT_EQUALS ( " a = 10 ; " , tok ( " a = atol( \" 0xa \" ); " ) ) ;
}
2011-10-13 20:53:06 +02:00
std : : string simplifyTypedef ( const char code [ ] ) {
2011-03-06 21:07:06 +01:00
errout . str ( " " ) ;
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
std : : istringstream istr ( code ) ;
2012-05-05 18:33:26 +02:00
tokenizer . list . createTokens ( istr ) ;
2011-03-06 21:07:06 +01:00
tokenizer . createLinks ( ) ;
tokenizer . simplifyTypedef ( ) ;
2012-04-16 19:51:07 +02:00
return tokenizer . tokens ( ) - > stringifyList ( 0 , false ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef1 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " class A \n "
" { \n "
" public: \n "
" typedef wchar_t duplicate; \n "
" void foo() {} \n "
" }; \n "
" typedef A duplicate; \n "
" int main() \n "
" { \n "
" duplicate a; \n "
" a.foo(); \n "
" A::duplicate c = 0; \n "
" } \n " ;
const std : : string expected =
" class A "
" { "
" public: "
2011-12-04 15:40:05 +01:00
" "
2011-03-06 21:07:06 +01:00
" void foo ( ) { } "
" } ; "
" int main ( ) "
" { "
" A a ; "
" a . foo ( ) ; "
" wchar_t c ; c = 0 ; "
" } " ;
2012-11-11 15:16:08 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef2 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " class A; \n "
" typedef A duplicate; \n "
" class A \n "
" { \n "
" public: \n "
" typedef wchar_t duplicate; \n "
" duplicate foo() { wchar_t b; return b; } \n "
" }; \n " ;
const std : : string expected =
" class A ; "
" class A "
" { "
" public: "
2011-12-04 15:40:05 +01:00
" "
2011-03-06 21:07:06 +01:00
" wchar_t foo ( ) { wchar_t b ; return b ; } "
" } ; " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef3 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " class A {}; \n "
" typedef A duplicate; \n "
" wchar_t foo() \n "
" { \n "
" typedef wchar_t duplicate; \n "
" duplicate b; \n "
" return b; \n "
" } \n "
" int main() \n "
" { \n "
" duplicate b; \n "
" } \n " ;
const std : : string expected =
" class A { } ; "
" wchar_t foo ( ) "
" { "
2011-12-04 15:40:05 +01:00
" "
2011-03-06 21:07:06 +01:00
" wchar_t b ; "
" return b ; "
" } "
" int main ( ) "
" { "
" A b ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef4 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef int s32; \n "
" typedef unsigned int u32; \n "
" void f() \n "
" { \n "
" s32 ivar = -2; \n "
" u32 uvar = 2; \n "
" return uvar / ivar; \n "
" } \n " ;
const std : : string expected =
" void f ( ) "
" { "
" int ivar ; ivar = -2 ; "
" unsigned int uvar ; uvar = 2 ; "
" return uvar / ivar ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef5 ( ) {
2011-03-06 21:07:06 +01:00
// ticket #780
const char code [ ] =
" typedef struct yy_buffer_state *YY_BUFFER_STATE; \n "
" void f() \n "
" { \n "
" YY_BUFFER_STATE state; \n "
" } \n " ;
const char expected [ ] =
" void f ( ) "
" { "
" struct yy_buffer_state * state ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef6 ( ) {
2011-03-06 21:07:06 +01:00
// ticket #983
const char code [ ] =
" namespace VL { \n "
" typedef float float_t ; \n "
" inline VL::float_t fast_atan2(VL::float_t y, VL::float_t x){} \n "
" } \n " ;
const char expected [ ] =
" namespace VL { "
2011-12-04 15:40:05 +01:00
" "
2011-03-06 21:07:06 +01:00
" float fast_atan2 ( float y , float x ) { } "
" } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef7 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef int abc ; "
" Fred :: abc f ; " ;
2011-12-04 17:15:53 +01:00
const char expected [ ] = " Fred :: abc f ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef8 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef int INT; \n "
" typedef unsigned int UINT; \n "
" typedef int * PINT; \n "
" typedef unsigned int * PUINT; \n "
" typedef int & RINT; \n "
" typedef unsigned int & RUINT; \n "
" typedef const int & RCINT; \n "
" typedef const unsigned int & RCUINT; \n "
" INT ti; \n "
" UINT tui; \n "
" PINT tpi; \n "
" PUINT tpui; \n "
" RINT tri; \n "
" RUINT trui; \n "
" RCINT trci; \n "
" RCUINT trcui; " ;
const char expected [ ] =
" int ti ; "
" unsigned int tui ; "
" int * tpi ; "
" unsigned int * tpui ; "
" int & tri ; "
" unsigned int & trui ; "
" const int & trci ; "
" const unsigned int & trcui ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef9 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef struct s S, * PS; \n "
" typedef struct t { int a; } T, *TP; \n "
" typedef struct { int a; } U; \n "
" typedef struct { int a; } * V; \n "
" S s; \n "
" PS ps; \n "
" T t; \n "
" TP tp; \n "
" U u; \n "
" V v; " ;
const char expected [ ] =
" struct t { int a ; } ; "
" struct U { int a ; } ; "
" struct Unnamed0 { int a ; } ; "
" struct s s ; "
" struct s * ps ; "
" struct t t ; "
" struct t * tp ; "
" struct U u ; "
" struct Unnamed0 * v ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef10 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef union s S, * PS; \n "
" typedef union t { int a; float b ; } T, *TP; \n "
" typedef union { int a; float b; } U; \n "
" typedef union { int a; float b; } * V; \n "
" S s; \n "
" PS ps; \n "
" T t; \n "
" TP tp; \n "
" U u; \n "
" V v; " ;
const char expected [ ] =
" union t { int a ; float b ; } ; "
" union U { int a ; float b ; } ; "
" union Unnamed1 { int a ; float b ; } ; "
" union s s ; "
" union s * ps ; "
" union t t ; "
" union t * tp ; "
" union U u ; "
" union Unnamed1 * v ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef11 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef enum { a = 0 , b = 1 , c = 2 } abc; \n "
" typedef enum xyz { x = 0 , y = 1 , z = 2 } XYZ; \n "
" abc e1; \n "
" XYZ e2; " ;
2011-12-04 18:56:12 +01:00
const char expected [ ] = " int e1 ; "
" int e2 ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef12 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef vector<int> V1; \n "
" typedef std::vector<int> V2; \n "
" typedef std::vector<std::vector<int> > V3; \n "
" typedef std::list<int>::iterator IntListIterator; \n "
" V1 v1; \n "
" V2 v2; \n "
" V3 v3; \n "
" IntListIterator iter; " ;
const char expected [ ] =
" vector < int > v1 ; "
" std :: vector < int > v2 ; "
" std :: vector < std :: vector < int > > v3 ; "
" std :: list < int > :: iterator iter ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef13 ( ) {
2011-03-06 21:07:06 +01:00
// ticket # 1167
const char code [ ] = " typedef std::pair<int(*)(void*), void*> Func; "
" typedef std::vector<Func> CallQueue; "
" int main() {} " ;
2012-04-17 09:51:08 +02:00
// Tokenize and check output..
tok ( code ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef14 ( ) {
2011-03-06 21:07:06 +01:00
// ticket # 1232
const char code [ ] = " template <typename F, unsigned int N> struct E "
" { "
" typedef E<F,(N>0)?(N-1):0> v; "
" typedef typename add<v,v>::val val; "
" FP_M(val); "
" }; "
" template <typename F> struct E <F,0> "
" { "
" typedef typename D<1>::val val; "
" FP_M(val); "
" }; " ;
2012-04-17 09:51:08 +02:00
// Tokenize and check output..
tok ( code ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef15 ( ) {
2011-03-06 21:07:06 +01:00
{
const char code [ ] = " typedef char frame[10]; \n "
" frame f; " ;
2011-12-04 17:15:53 +01:00
const char expected [ ] = " char f [ 10 ] ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " typedef unsigned char frame[10]; \n "
" frame f; " ;
2011-12-04 17:15:53 +01:00
const char expected [ ] = " unsigned char f [ 10 ] ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef16 ( ) {
2011-03-06 21:07:06 +01:00
// ticket # 1252
const char code [ ] = " typedef char MOT8; \n "
" typedef MOT8 CHFOO[4096]; \n "
" typedef struct { \n "
" CHFOO freem; \n "
" } STRFOO; " ;
2012-04-17 09:51:08 +02:00
// Tokenize and check output..
tok ( code ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef17 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef char * PCHAR, CHAR; \n "
" PCHAR pc; \n "
" CHAR c; " ;
const char expected [ ] =
" char * pc ; "
" char c ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef18 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef vector<int[4]> a; \n "
" a b; \n " ;
// Clear the error buffer..
errout . str ( " " ) ;
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyTokenList ( ) ;
ASSERT_EQUALS ( true , tokenizer . validate ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef19 ( ) {
2011-03-06 21:07:06 +01:00
{
// ticket #1275
const char code [ ] = " typedef struct {} A, *B, **C; \n "
" A a; \n "
" B b; \n "
" C c; " ;
const char expected [ ] =
" struct A { } ; "
" struct A a ; "
" struct A * b ; "
" struct A * * c ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " typedef struct {} A, *********B; \n "
" A a; \n "
" B b; " ;
const char expected [ ] =
" struct A { } ; "
" struct A a ; "
" struct A * * * * * * * * * b ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " typedef struct {} **********A, *B, C; \n "
" A a; \n "
" B b; \n "
" C c; " ;
const char expected [ ] =
" struct Unnamed2 { } ; "
" struct Unnamed2 * * * * * * * * * * a ; "
" struct Unnamed2 * b ; "
" struct Unnamed2 c ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef20 ( ) {
2011-03-06 21:07:06 +01:00
// ticket #1284
const char code [ ] = " typedef jobject invoke_t (jobject, Proxy *, Method *, JArray< jobject > *); " ;
// Clear the error buffer..
errout . str ( " " ) ;
Settings settings ;
Tokenizer tokenizer ( & settings , this ) ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyTokenList ( ) ;
ASSERT_EQUALS ( true , tokenizer . validate ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef21 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef void (* PF)(); \n "
" typedef void * (* PFV)(void *); \n "
" PF pf; \n "
" PFV pfv; " ;
const char expected [ ] =
2011-12-04 17:15:53 +01:00
" "
2011-12-04 15:40:05 +01:00
" "
2011-03-06 21:07:06 +01:00
" void ( * pf ) ( ) ; "
" void * ( * pfv ) ( void * ) ; " ;
ASSERT_EQUALS ( expected , simplifyTypedef ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef22 ( ) {
2011-03-06 21:07:06 +01:00
{
const char code [ ] = " class Fred { \n "
" typedef void (*testfp)(); \n "
" testfp get() { return test; } \n "
" static void test() { } \n "
" }; " ;
const char expected [ ] =
" class Fred { "
2011-12-04 15:40:05 +01:00
" "
2011-03-06 21:07:06 +01:00
" void ( * get ( ) ) ( ) { return test ; } "
" static void test ( ) { } "
" } ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " class Fred { \n "
" typedef void * (*testfp)(void *); \n "
" testfp get() { return test; } \n "
" static void * test(void * p) { return p; } \n "
" }; \n " ;
const char expected [ ] =
" class Fred { "
2011-12-04 15:40:05 +01:00
" "
2011-03-06 21:07:06 +01:00
" void * ( * get ( ) ) ( void * ) { return test ; } "
" static void * test ( void * p ) { return p ; } "
" } ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " class Fred { \n "
" typedef unsigned int * (*testfp)(unsigned int *); \n "
" testfp get() { return test; } \n "
" static unsigned int * test(unsigned int * p) { return p; } \n "
" }; \n " ;
const char expected [ ] =
" class Fred { "
2011-12-04 15:40:05 +01:00
" "
2011-03-06 21:07:06 +01:00
" unsigned int * ( * get ( ) ) ( unsigned int * ) { return test ; } "
" static unsigned int * test ( unsigned int * p ) { return p ; } "
" } ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " class Fred { \n "
" typedef const unsigned int * (*testfp)(const unsigned int *); \n "
" testfp get() { return test; } \n "
" static const unsigned int * test(const unsigned int * p) { return p; } \n "
" }; \n " ;
// static const gets changed to const static
const char expected [ ] =
" class Fred { "
2011-12-04 15:40:05 +01:00
" "
2011-03-06 21:07:06 +01:00
" const unsigned int * ( * get ( ) ) ( const unsigned int * ) { return test ; } "
" const static unsigned int * test ( const unsigned int * p ) { return p ; } "
" } ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " class Fred { \n "
" typedef void * (*testfp)(void *); \n "
" testfp get(int i) { return test; } \n "
" static void * test(void * p) { return p; } \n "
" }; \n " ;
const char expected [ ] =
" class Fred { "
2011-12-04 15:40:05 +01:00
" "
2011-03-06 21:07:06 +01:00
" void * ( * get ( int i ) ) ( void * ) { return test ; } "
" static void * test ( void * p ) { return p ; } "
" } ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef23 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef bool (*Callback) (int i); \n "
" void addCallback(Callback callback) { } \n "
" void addCallback1(Callback callback, int j) { } " ;
const char expected [ ] =
" void addCallback ( bool * callback ) { } "
" void addCallback1 ( bool * callback , int j ) { } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef24 ( ) {
2011-03-06 21:07:06 +01:00
{
const char code [ ] = " typedef int (*fp)(); \n "
" void g( fp f ) \n "
" { \n "
" fp f2 = (fp)f; \n "
" } " ;
const char expected [ ] =
" void g ( int * f ) "
" { "
" int * f2 ; f2 = ( int * ) f ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " typedef int (*fp)(); \n "
" void g( fp f ) \n "
" { \n "
" fp f2 = static_cast<fp>(f); \n "
" } " ;
const char expected [ ] =
" void g ( int * f ) "
" { "
" int * f2 ; f2 = static_cast < int * > ( f ) ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef25 ( ) {
2011-03-06 21:07:06 +01:00
{
// ticket #1298
const char code [ ] = " typedef void (*fill_names_f) (const char *); \n "
" struct vfs_class { \n "
" void (*fill_names) (struct vfs_class *me, fill_names_f); \n "
" } " ;
const char expected [ ] =
" struct vfs_class { "
" void ( * fill_names ) ( struct vfs_class * me , void ( * ) ( const char * ) ) ; "
" } " ;
ASSERT_EQUALS ( expected , simplifyTypedef ( code ) ) ;
}
{
const char code [ ] = " typedef void (*fill_names_f) (const char *); \n "
" struct vfs_class { \n "
" void (*fill_names) (fill_names_f, struct vfs_class *me); \n "
" } " ;
const char expected [ ] =
" struct vfs_class { "
" void ( * fill_names ) ( void ( * ) ( const char * ) , struct vfs_class * me ) ; "
" } " ;
ASSERT_EQUALS ( expected , simplifyTypedef ( code ) ) ;
}
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef26 ( ) {
2011-03-06 21:07:06 +01:00
{
const char code [ ] = " typedef void (*Callback) (); \n "
" void addCallback(Callback (*callback)()); " ;
2011-12-04 17:15:53 +01:00
const char expected [ ] = " void addCallback ( void ( * ( * callback ) ( ) ) ( ) ) ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
// ticket # 1307
const char code [ ] = " typedef void (*pc_video_update_proc)(bitmap_t *bitmap, \n "
" struct mscrtc6845 *crtc); \n "
" \n "
" struct mscrtc6845 *pc_video_start(pc_video_update_proc (*choosevideomode)(running_machine *machine, int *width, int *height, struct mscrtc6845 *crtc)); " ;
2011-12-04 17:15:53 +01:00
const char expected [ ] = " struct mscrtc6845 * pc_video_start ( void ( * ( * choosevideomode ) ( running_machine * machine , int * width , int * height , struct mscrtc6845 * crtc ) ) ( bitmap_t * bitmap , struct mscrtc6845 * crtc ) ) ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef27 ( ) {
2011-03-06 21:07:06 +01:00
// ticket #1316
const char code [ ] = " int main() \n "
" { \n "
" typedef int (*func_ptr)(float, double); \n "
" VERIFY((is_same<result_of<func_ptr(char, float)>::type, int>::value)); \n "
" } " ;
const char expected [ ] =
" int main ( ) "
" { "
2011-12-04 15:40:05 +01:00
" "
2011-03-06 21:07:06 +01:00
" VERIFY ( is_same < result_of < int ( * ( char , float ) ) ( float , double ) > :: type , int > :: value ) ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef28 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef std::pair<double, double> (*F)(double); \n "
" F f; " ;
2011-12-04 17:15:53 +01:00
const char expected [ ] = " std :: pair < double , double > ( * f ) ( double ) ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef29 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef int array [ice_or<is_int<int>::value, is_int<UDT>::value>::value ? 1 : -1]; \n "
" typedef int array1 [N]; \n "
" typedef int array2 [N][M]; \n "
" typedef int int_t, int_array[N]; \n "
" array a; \n "
" array1 a1; \n "
" array2 a2; \n "
" int_t t; \n "
" int_array ia; " ;
const char expected [ ] =
2011-04-09 15:54:36 +02:00
" int a [ ice_or < is_int < int > :: value , is_int < UDT > :: value > :: value ? 1 : -1 ] ; "
2011-03-06 21:07:06 +01:00
" int a1 [ N ] ; "
" int a2 [ N ] [ M ] ; "
" int t ; "
" int ia [ N ] ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef30 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef ::std::list<int> int_list; \n "
" typedef ::std::list<int>::iterator int_list_iterator; \n "
" typedef ::std::list<int> int_list_array[10]; \n "
" int_list il; \n "
" int_list_iterator ili; \n "
" int_list_array ila; " ;
const char expected [ ] =
" :: std :: list < int > il ; "
" :: std :: list < int > :: iterator ili ; "
" :: std :: list < int > ila [ 10 ] ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef31 ( ) {
2011-03-06 21:07:06 +01:00
{
const char code [ ] = " class A { \n "
" public: \n "
" typedef int INT; \n "
" INT get() const; \n "
" void put(INT x) { a = x; } \n "
" INT a; \n "
" }; \n "
" A::INT A::get() const { return a; } \n "
" A::INT i = A::a; " ;
const char expected [ ] = " class A { "
" public: "
2011-12-04 15:40:05 +01:00
" "
2011-03-06 21:07:06 +01:00
" int get ( ) const ; "
" void put ( int x ) { a = x ; } "
" int a ; "
" } ; "
" int A :: get ( ) const { return a ; } "
" int i ; i = A :: a ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " struct A { \n "
" typedef int INT; \n "
" INT get() const; \n "
" void put(INT x) { a = x; } \n "
" INT a; \n "
" }; \n "
" A::INT A::get() const { return a; } \n "
" A::INT i = A::a; " ;
const char expected [ ] = " struct A { "
2011-12-04 15:40:05 +01:00
" "
2011-03-06 21:07:06 +01:00
" int get ( ) const ; "
" void put ( int x ) { a = x ; } "
" int a ; "
" } ; "
" int A :: get ( ) const { return a ; } "
" int i ; i = A :: a ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef32 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef char CHAR; \n "
" typedef CHAR * LPSTR; \n "
" typedef const CHAR * LPCSTR; \n "
" CHAR c; \n "
" LPSTR cp; \n "
" LPCSTR ccp; " ;
const char expected [ ] =
" char c ; "
" char * cp ; "
" const char * ccp ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef33 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " class A { \n "
" public: \n "
" typedef char CHAR_A; \n "
" CHAR_A funA(); \n "
" class B { \n "
" public: \n "
" typedef short SHRT_B; \n "
" SHRT_B funB(); \n "
" class C { \n "
" public: \n "
" typedef int INT_C; \n "
" INT_C funC(); \n "
" struct D { \n "
" typedef long LONG_D; \n "
" LONG_D funD(); \n "
" LONG_D d; \n "
" }; \n "
" INT_C c; \n "
" }; \n "
" SHRT_B b; \n "
" }; \n "
" CHAR_A a; \n "
" }; \n "
" A::CHAR_A A::funA() { return a; } \n "
" A::B::SHRT_B A::B::funB() { return b; } \n "
" A::B::C::INT_C A::B::C::funC() { return c; } "
" A::B::C::D::LONG_D A::B::C::D::funD() { return d; } " ;
const char expected [ ] =
" class A { "
" public: "
2011-12-04 15:40:05 +01:00
" "
2011-03-06 21:07:06 +01:00
" char funA ( ) ; "
" class B { "
" public: "
2011-12-04 15:40:05 +01:00
" "
2011-03-06 21:07:06 +01:00
" short funB ( ) ; "
" class C { "
" public: "
2011-12-04 15:40:05 +01:00
" "
2011-03-06 21:07:06 +01:00
" int funC ( ) ; "
" struct D { "
2011-12-04 15:40:05 +01:00
" "
2011-03-06 21:07:06 +01:00
" long funD ( ) ; "
" long d ; "
" } ; "
" int c ; "
" } ; "
" short b ; "
" } ; "
" char a ; "
" } ; "
" char A :: funA ( ) { return a ; } "
" short A :: B :: funB ( ) { return b ; } "
" int A :: B :: C :: funC ( ) { return c ; } "
" long A :: B :: C :: D :: funD ( ) { return d ; } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef34 ( ) {
2011-03-06 21:07:06 +01:00
// ticket #1411
const char code [ ] = " class X { }; \n "
" typedef X (*foofunc)(const X&); \n "
" int main() \n "
" { \n "
" foofunc *Foo = new foofunc[2]; \n "
" } " ;
const char expected [ ] =
" class X { } ; "
" int main ( ) "
" { "
" X ( * * Foo ) ( const X & ) = new X ( * ) ( const X & ) [ 2 ] ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
// Check simplifyTypedef
2011-10-13 20:53:06 +02:00
void checkSimplifyTypedef ( const char code [ ] ) {
2011-03-06 21:07:06 +01:00
errout . str ( " " ) ;
// Tokenize..
Settings settings ;
settings . inconclusive = true ;
2011-08-07 09:28:08 +02:00
settings . addEnabled ( " style " ) ;
2011-03-06 21:07:06 +01:00
settings . debugwarnings = true ; // show warnings about unhandled typedef
Tokenizer tokenizer ( & settings , this ) ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef35 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef int A; \n "
" class S \n "
" { \n "
" public: \n "
" typedef float A; \n "
" A a; \n "
" virtual void fun(A x); \n "
" }; \n "
" void S::fun(S::A) { }; \n "
" class S1 : public S \n "
" { \n "
" public: \n "
" void fun(S::A) { } \n "
" }; \n "
" struct T \n "
" { \n "
" typedef A B; \n "
" B b; \n "
" }; \n "
" float fun1(float A) { return A; } \n "
" float fun2(float a) { float A = a++; return A; } \n "
" float fun3(int a) \n "
" { \n "
" typedef struct { int a; } A; \n "
" A s; s.a = a; \n "
" return s.a; \n "
" } \n "
" int main() \n "
" { \n "
" A a = 0; \n "
" S::A s = fun1(a) + fun2(a) - fun3(a); \n "
" return a + s; \n "
" } " ;
2011-12-04 17:15:53 +01:00
const char expected [ ] = " class S "
2011-03-06 21:07:06 +01:00
" { "
" public: "
2011-12-04 15:40:05 +01:00
" "
2011-03-06 21:07:06 +01:00
" float a ; "
" virtual void fun ( float x ) ; "
" } ; "
" void S :: fun ( float ) { } ; "
" class S1 : public S "
" { "
" public: "
" void fun ( float ) { } "
" } ; "
" struct T "
" { "
2011-12-04 15:40:05 +01:00
" "
2011-03-06 21:07:06 +01:00
" int b ; "
" } ; "
" float fun1 ( float A ) { return A ; } "
" float fun2 ( float a ) { float A ; A = a ++ ; return A ; } "
" float fun3 ( int a ) "
" { "
" struct A { int a ; } ; "
" struct A s ; s . a = a ; "
" return s . a ; "
" } "
" int main ( ) "
" { "
" int a ; a = 0 ; "
" float s ; s = fun1 ( a ) + fun2 ( a ) - fun3 ( a ) ; "
" return a + s ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
checkSimplifyTypedef ( code ) ;
2012-05-06 19:37:41 +02:00
ASSERT_EQUALS ( " [test.cpp:5] -> [test.cpp:1]: (style, inconclusive) The typedef 'A' hides a typedef with the same name. \n "
" [test.cpp:20] -> [test.cpp:1]: (style, inconclusive) The function parameter 'A' hides a typedef with the same name. \n "
" [test.cpp:21] -> [test.cpp:1]: (style, inconclusive) The variable 'A' hides a typedef with the same name. \n "
" [test.cpp:24] -> [test.cpp:1]: (style, inconclusive) The typedef 'A' hides a typedef with the same name. \n " , errout . str ( ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef36 ( ) {
2011-03-06 21:07:06 +01:00
// ticket #1434
const char code [ ] = " typedef void (*TIFFFaxFillFunc)(); \n "
" void f(va_list ap) \n "
" { \n "
" *va_arg(ap, TIFFFaxFillFunc*) = 0; \n "
" } " ;
2011-12-04 17:15:53 +01:00
const char expected [ ] = " void f ( va_list ap ) "
2011-03-06 21:07:06 +01:00
" { "
" * va_arg ( ap , void ( * * ) ( ) ) = 0 ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef37 ( ) {
2011-03-06 21:07:06 +01:00
{
// ticket #1449
const char code [ ] = " template <class T> class V {}; \n "
" typedef V<int> A; \n "
" typedef int B; \n "
" typedef V<int> A; \n "
" typedef int B; " ;
checkSimplifyTypedef ( code ) ;
2012-05-06 19:37:41 +02:00
ASSERT_EQUALS ( " [test.cpp:4] -> [test.cpp:2]: (style, inconclusive) The typedef 'A' hides a typedef with the same name. \n "
" [test.cpp:5] -> [test.cpp:3]: (style, inconclusive) The typedef 'B' hides a typedef with the same name. \n " , errout . str ( ) ) ;
2011-03-06 21:07:06 +01:00
}
{
const char code [ ] = " typedef int INT; \n "
" void f() \n "
" { \n "
" INT i; { } \n "
" } " ;
2011-12-04 17:15:53 +01:00
const char expected [ ] = " void f ( ) "
2011-03-06 21:07:06 +01:00
" { "
" int i ; { } "
" } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef38 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef C A; \n "
" struct AB : public A, public B { }; " ;
2011-12-04 17:15:53 +01:00
const char expected [ ] = " struct AB : public C , public B { } ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef39 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef int A; \n "
" template <const A, volatile A>::value; " ;
2012-07-29 16:01:05 +02:00
const char expected [ ] = " template < const int , int > :: value ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef40 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef int A; \n "
" typedef int B; \n "
" template <class A, class B> class C { }; " ;
2012-07-29 16:01:05 +02:00
const char expected [ ] = " template < class A , class B > class C { } ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
checkSimplifyTypedef ( code ) ;
2012-05-06 19:37:41 +02:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:1]: (style, inconclusive) The template parameter 'A' hides a typedef with the same name. \n "
" [test.cpp:3] -> [test.cpp:2]: (style, inconclusive) The template parameter 'B' hides a typedef with the same name. \n " , errout . str ( ) ) ;
2011-03-06 21:07:06 +01:00
checkSimplifyTypedef ( " typedef tuple<double&, const double&, const double, double*, const double*> t2; \n "
" void ordering_test() \n "
" { \n "
" tuple<short, float> t2(5, 3.3f); \n "
" BOOST_CHECK(t3 > t2); \n "
" } " ) ;
2012-05-06 19:37:41 +02:00
ASSERT_EQUALS ( " [test.cpp:4] -> [test.cpp:1]: (style, inconclusive) The template instantiation 't2' hides a typedef with the same name. \n " , errout . str ( ) ) ;
2011-03-06 21:07:06 +01:00
checkSimplifyTypedef ( " class MyOverflowingUnsigned \n "
" { \n "
" public: \n "
" typedef unsigned self_type::* bool_type; \n "
" operator bool_type() const { return this->v_ ? &self_type::v_ : 0; } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
checkSimplifyTypedef ( " typedef int (*fptr_type)(int, int); \n "
" struct which_one { \n "
" typedef fptr_type (*result_type)(bool x); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
checkSimplifyTypedef ( " class my_configuration \n "
" { \n "
" public: \n "
" template < typename T > \n "
" class hook \n "
" { \n "
" public: \n "
" typedef ::boost::rational<T> rational_type; \n "
" public: \n "
" rational_type ( &r_ )[ 9 ]; \n "
" }; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
checkSimplifyTypedef ( " class A \n "
" { \n "
" typedef B b; \n "
" friend b; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef41 ( ) {
2011-03-06 21:07:06 +01:00
// ticket #1488
checkSimplifyTypedef ( " class Y; \n "
" class X \n "
" { \n "
" typedef Y type; \n "
" friend class type; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef42 ( ) {
2011-03-06 21:07:06 +01:00
// ticket #1506
checkSimplifyTypedef ( " typedef struct A { } A; \n "
" struct A; " ) ;
2012-04-25 15:09:46 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:1]: (style) The struct 'A' forward declaration is unnecessary. Type struct is already declared earlier. \n " , errout . str ( ) ) ;
2011-03-06 21:07:06 +01:00
checkSimplifyTypedef ( " typedef union A { int i; float f; } A; \n "
" union A; " ) ;
2012-04-25 15:09:46 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:1]: (style) The union 'A' forward declaration is unnecessary. Type union is already declared earlier. \n " , errout . str ( ) ) ;
2011-03-06 21:07:06 +01:00
checkSimplifyTypedef ( " typedef std::map<std::string, int> A; \n "
" class A; " ) ;
2012-04-25 15:09:46 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:1]: (style) The class 'A' forward declaration is unnecessary. Type class is already declared earlier. \n " , errout . str ( ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef43 ( ) {
2011-03-06 21:07:06 +01:00
// ticket #1588
{
const char code [ ] = " typedef struct foo A; \n "
" struct A \n "
" { \n "
" int alloclen; \n "
" }; \n " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " struct A "
2011-03-06 21:07:06 +01:00
" { "
" int alloclen ; "
" } ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
checkSimplifyTypedef ( code ) ;
2012-05-06 19:37:41 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:1]: (style, inconclusive) The struct 'A' hides a typedef with the same name. \n " , errout . str ( ) ) ;
2011-03-06 21:07:06 +01:00
}
{
const char code [ ] = " typedef union foo A; \n "
" union A \n "
" { \n "
" int alloclen; \n "
" }; \n " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " union A "
2011-03-06 21:07:06 +01:00
" { "
" int alloclen ; "
" } ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
checkSimplifyTypedef ( code ) ;
2012-05-06 19:37:41 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:1]: (style, inconclusive) The union 'A' hides a typedef with the same name. \n " , errout . str ( ) ) ;
2011-03-06 21:07:06 +01:00
}
{
const char code [ ] = " typedef class foo A; \n "
" class A \n "
" { \n "
" int alloclen; \n "
" }; \n " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " class A "
2011-03-06 21:07:06 +01:00
" { "
" int alloclen ; "
" } ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
checkSimplifyTypedef ( code ) ;
2012-05-06 19:37:41 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:1]: (style, inconclusive) The class 'A' hides a typedef with the same name. \n " , errout . str ( ) ) ;
2011-03-06 21:07:06 +01:00
}
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef44 ( ) {
2011-03-06 21:07:06 +01:00
{
const char code [ ] = " typedef std::map<std::string, int> Map; \n "
" class MyMap : public Map \n "
" { \n "
" }; \n " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " class MyMap : public std :: map < std :: string , int > "
2011-03-06 21:07:06 +01:00
" { "
" } ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef std::map<std::string, int> Map; \n "
" class MyMap : protected Map \n "
" { \n "
" }; \n " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " class MyMap : protected std :: map < std :: string , int > "
2011-03-06 21:07:06 +01:00
" { "
" } ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef std::map<std::string, int> Map; \n "
" class MyMap : private Map \n "
" { \n "
" }; \n " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " class MyMap : private std :: map < std :: string , int > "
2011-03-06 21:07:06 +01:00
" { "
" } ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef struct foo { } A; \n "
" struct MyA : public A \n "
" { \n "
" }; \n " ;
// The expected result..
const std : : string expected ( " struct foo { } ; "
" struct MyA : public foo "
" { "
" } ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef class foo { } A; \n "
" class MyA : public A \n "
" { \n "
" }; \n " ;
// The expected result..
const std : : string expected ( " class foo { } ; "
" class MyA : public foo "
" { "
" } ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef45 ( ) {
2011-03-06 21:07:06 +01:00
// ticket # 1613
const char code [ ] = " void fn() { \n "
" typedef foo<> bar; \n "
" while (0 > bar(1)) {} \n "
" } " ;
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef46 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef const struct A { int a; } * AP; \n "
" AP ap; \n " ;
// The expected result..
const std : : string expected ( " struct A { int a ; } ; "
" const struct A * ap ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef47 ( ) {
2011-03-06 21:07:06 +01:00
{
const char code [ ] = " typedef std::pair<int, int> const I; \n "
" I i; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " std :: pair < int , int > const i ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
{
const char code [ ] = " typedef void (X:: *F)(); \n "
" F f; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " void ( X :: * f ) ( ) ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef48 ( ) { // ticket #1673
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef struct string { } string; \n "
" void foo (LIST *module_name) \n "
" { \n "
" bar(module_name ? module_name->string : 0); \n "
" } \n " ;
// The expected result..
const std : : string expected ( " struct string { } ; "
" void foo ( LIST * module_name ) "
" { "
" bar ( module_name ? module_name . string : 0 ) ; "
" } " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef49 ( ) { // ticket #1691
2011-03-06 21:07:06 +01:00
const char code [ ] = " class Class2 { \n "
" typedef const Class & Const_Reference; \n "
" void some_method (Const_Reference x) const {} \n "
" void another_method (Const_Reference x) const {} \n "
" } " ;
// The expected result..
const std : : string expected ( " class Class2 { "
2011-12-04 15:40:05 +01:00
" "
2011-03-06 21:07:06 +01:00
" void some_method ( const Class & x ) const { } "
" void another_method ( const Class & x ) const { } "
" } " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef50 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef char (* type1)[10]; \n "
" typedef char (& type2)[10]; \n "
" typedef char (& type3)[x]; \n "
" typedef char (& type4)[x + 2]; \n "
" type1 t1; \n "
" type1 (*tp1)[2]; \n "
" type2 t2; \n "
" type3 t3; \n "
" type4 t4; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " char ( * t1 ) [ 10 ] ; "
2011-03-06 21:07:06 +01:00
" char ( * ( * tp1 ) [ 2 ] ) [ 10 ] ; "
" char ( & t2 ) [ 10 ] ; "
" char ( & t3 ) [ x ] ; "
" char ( & t4 ) [ x + 2 ] ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef51 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " class A { public: int i; }; \n "
" typedef const char (A :: * type1); \n "
" type1 t1 = &A::i; " ;
// The expected result..
const std : : string expected ( " class A { public: int i ; } ; "
" const char ( A :: * t1 ) = & A :: i ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef52 ( ) { // ticket #1782
2011-03-06 21:07:06 +01:00
{
const char code [ ] = " typedef char (* type1)[10]; \n "
" type1 foo() { } " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " char ( * foo ( ) ) [ 10 ] { } " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef char (* type1)[10]; \n "
" LOCAL(type1) foo() { } " ;
// this is invalid C so just make sure it doesn't generate an internal error
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef53 ( ) { // ticket #1801
2011-03-06 21:07:06 +01:00
{
const char code [ ] = " typedef int ( * int ( * ) ( ) ) ( ) ; " ;
// this is invalid C so just make sure it doesn't crash
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " [test.cpp:1]: (debug) Failed to parse 'typedef int ( * int ( * ) ( ) ) ( ) ;'. The checking continues anyway. \n " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef int (*PPDMarkOption)(ppd_file_t *ppd, const char *keyword, const char *option); \n "
" typedef int (*PPDMarkOption)(ppd_file_t *ppd, const char *keyword, const char *option); \n " ;
checkSimplifyTypedef ( code ) ;
2012-05-06 19:37:41 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:1]: (style, inconclusive) The typedef 'PPDMarkOption' hides a typedef with the same name. \n " , errout . str ( ) ) ;
2011-03-06 21:07:06 +01:00
}
{
const char code [ ] = " typedef int * A; \n "
" typedef int * A; \n " ;
checkSimplifyTypedef ( code ) ;
2012-05-06 19:37:41 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:1]: (style, inconclusive) The typedef 'A' hides a typedef with the same name. \n " , errout . str ( ) ) ;
2011-03-06 21:07:06 +01:00
}
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef54 ( ) { // ticket #1814
2011-03-06 21:07:06 +01:00
const char code [ ] = " void foo() \n "
" { \n "
" typedef std::basic_string<char, traits_type, allocator_type> string_type; \n "
" try \n "
" { \n "
" throw string_type( \" leak \" ); \n "
" } \n "
" catch (const string_type&) \n "
" { \n "
" pthread_exit (0); \n "
" } \n "
" } " ;
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef55 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef volatile unsigned long * const hwreg_t ; \n "
" typedef void *const t1[2]; \n "
" typedef int*const *_Iterator; \n "
" hwreg_t v1; \n "
" t1 v2; \n "
" _Iterator v3; \n " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " long * v1 ; "
2011-03-06 21:07:06 +01:00
" void * v2 [ 2 ] ; "
" int * * v3 ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef56 ( ) { // ticket #1829
2011-03-06 21:07:06 +01:00
const char code [ ] = " struct C { \n "
" typedef void (*fptr)(); \n "
" const fptr pr; \n "
" operator const fptr& () { return pr; } \n "
" }; \n " ;
// The expected result..
const std : : string expected ( " struct C { "
2011-12-04 15:40:05 +01:00
" "
2011-03-06 21:07:06 +01:00
" const void * pr ; " // this gets simplified to a regular pointer
" operatorconstvoid(*)()& ( ) { return pr ; } "
" } ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef57 ( ) { // ticket #1846
2011-03-06 21:07:06 +01:00
const char code [ ] = " void foo { \n "
" typedef int A; \n "
" A a = A(1) * A(2); \n "
" }; \n " ;
// The expected result..
const std : : string expected ( " void foo { "
2011-12-04 15:40:05 +01:00
" "
2011-03-06 21:07:06 +01:00
" int a ; a = int ( 1 ) * int ( 2 ) ; "
" } ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef58 ( ) { // ticket #1963
2011-03-06 21:07:06 +01:00
{
const char code [ ] = " typedef int vec2_t[2]; \n "
" vec2_t coords[4] = {1,2,3,4,5,6,7,8}; \n " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " int coords [ 4 ] [ 2 ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef int vec2_t[2]; \n "
" vec2_t coords[4][5][6+1] = {1,2,3,4,5,6,7,8}; \n " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " int coords [ 4 ] [ 5 ] [ 7 ] [ 2 ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef59 ( ) { // ticket #2011
2011-03-06 21:07:06 +01:00
const char code [ ] = " template<typename DISPATCHER> class SomeTemplateClass { \n "
" typedef void (SomeTemplateClass<DISPATCHER>::*MessageDispatcherFunc)(SerialInputMessage&); \n "
" }; \n " ;
// The expected result..
2012-07-29 16:01:05 +02:00
const std : : string expected ( " template < typename DISPATCHER > class SomeTemplateClass { } ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef60 ( ) { // ticket #2035
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef enum {qfalse, qtrue} qboolean; \n "
" typedef qboolean (*localEntitiyAddFunc_t) (struct le_s * le, entity_t * ent); \n "
" void f() \n "
" { \n "
" qboolean b; \n "
" localEntitiyAddFunc_t f; \n "
" } \n " ;
// The expected result..
2011-12-04 18:56:12 +01:00
const std : : string expected ( " void f ( ) { int b ; int * f ; } " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef61 ( ) { // ticket #2074 and 2075
2011-03-06 21:07:06 +01:00
const char code1 [ ] = " typedef unsigned char (*Mf_GetIndexByte_Func) (void); \n "
" typedef const unsigned char * (*Mf_GetPointerToCurrentPos_Func)(void); \n " ;
// Check for output..
checkSimplifyTypedef ( code1 ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
const char code2 [ ] = " typedef unsigned long uint32_t; \n "
" typedef uint32_t (*write_type_t) (uint32_t); \n " ;
// Check for output..
checkSimplifyTypedef ( code2 ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef62 ( ) { // ticket #2082
2011-03-06 21:07:06 +01:00
const char code1 [ ] = " typedef char TString[256]; \n "
" void f() \n "
" { \n "
" TString a, b; \n "
" } " ;
// The expected tokens..
2011-12-04 17:15:53 +01:00
const std : : string expected1 ( " void f ( ) { char a [ 256 ] ; char b [ 256 ] ; } " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected1 , tok ( code1 , false ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code1 ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
const char code2 [ ] = " typedef char TString[256]; \n "
" void f() \n "
" { \n "
" TString a = { 0 }, b = { 0 }; \n "
" } " ;
// The expected tokens..
2011-12-04 17:15:53 +01:00
const std : : string expected2 ( " void f ( ) { char a [ 256 ] = { 0 } ; char b [ 256 ] = { 0 } ; } " ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected2 , tok ( code2 , false ) ) ;
// Check for output..
checkSimplifyTypedef ( code2 ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
const char code3 [ ] = " typedef char TString[256]; \n "
" void f() \n "
" { \n "
" TString a = \" \" , b = \" \" ; \n "
" } " ;
// The expected tokens..
2011-12-04 17:15:53 +01:00
const std : : string expected3 ( " void f ( ) { char a [ 256 ] = \" \" ; char b [ 256 ] = \" \" ; } " ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected3 , tok ( code3 , false ) ) ;
// Check for output..
checkSimplifyTypedef ( code3 ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
const char code4 [ ] = " typedef char TString[256]; \n "
" void f() \n "
" { \n "
" TString a = \" 1234 \" , b = \" 5678 \" ; \n "
" } " ;
// The expected tokens..
2011-12-04 17:15:53 +01:00
const std : : string expected4 ( " void f ( ) { char a [ 256 ] = \" 1234 \" ; char b [ 256 ] = \" 5678 \" ; } " ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected4 , tok ( code4 , false ) ) ;
// Check for output..
checkSimplifyTypedef ( code4 ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef63 ( ) { // ticket #2175 'typedef float x[3];'
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef float x[3]; \n "
" x a,b,c; \n " ;
2012-04-17 09:51:08 +02:00
const std : : string actual ( tok ( code ) ) ;
2011-12-04 17:15:53 +01:00
ASSERT_EQUALS ( " float a [ 3 ] ; float b [ 3 ] ; float c [ 3 ] ; " , actual ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef64 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef __typeof__(__type1() + __type2()) __type; "
" __type t; \n " ;
2012-04-17 09:51:08 +02:00
const std : : string actual ( tok ( code ) ) ;
2011-12-04 17:15:53 +01:00
ASSERT_EQUALS ( " __typeof__ ( __type1 ( ) + __type2 ( ) ) t ; " , actual ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef65 ( ) { // ticket #2314
2012-12-06 19:19:22 +01:00
const char code [ ] = " typedef BAR<int> Foo; \n "
" int main() { \n "
" Foo b(0); \n "
" return b > Foo(10); \n "
2011-03-06 21:07:06 +01:00
" } " ;
2012-04-17 09:51:08 +02:00
const std : : string actual ( tok ( code ) ) ;
2011-12-04 17:15:53 +01:00
ASSERT_EQUALS ( " int main ( ) { BAR < int > b ( 0 ) ; return b > BAR < int > ( 10 ) ; } " , actual ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef66 ( ) { // ticket #2341
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef long* GEN; \n "
" extern GEN (*foo)(long); " ;
2012-07-25 10:34:54 +02:00
tok ( code ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef67 ( ) { // ticket #2354
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef int ( * Function ) ( ) ; \n "
" void f ( ) { \n "
" ((Function * (*) (char *, char *, int, int)) global[6]) ( \" assoc \" , \" eggdrop \" , 106, 0); \n "
" } \n " ;
2011-12-04 17:15:53 +01:00
const std : : string expected = " void f ( ) { "
2011-03-06 21:07:06 +01:00
" ( ( int ( * * ( * ) ( char * , char * , int , int ) ) ( ) ) global [ 6 ] ) ( \" assoc \" , \" eggdrop \" , 106 , 0 ) ; "
" } " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef68 ( ) { // ticket #2355
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef FMAC1 void (* a) (); \n "
" void *(*b) (); \n " ;
2012-04-17 09:51:08 +02:00
const std : : string actual ( tok ( code ) ) ;
2011-12-04 17:30:25 +01:00
ASSERT_EQUALS ( " void * * b ; " , actual ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef69 ( ) { // ticket #2348
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef int (*CompilerHook)(); \n "
2012-12-06 19:19:22 +01:00
" typedef struct VirtualMachine \n "
2011-03-06 21:07:06 +01:00
" { \n "
" CompilerHook *(*compilerHookVector)(void); \n "
" }VirtualMachine; \n " ;
2011-12-04 17:15:53 +01:00
const std : : string expected = " struct VirtualMachine "
2011-03-06 21:07:06 +01:00
" { "
" int ( * * ( * compilerHookVector ) ( void ) ) ( ) ; "
" } ; " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef70 ( ) { // ticket #2348
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef int pread_f ( int ) ; \n "
" pread_f *(*test_func)(char *filename); \n " ;
2011-12-04 17:15:53 +01:00
const std : : string expected = " int ( * ( * test_func ) ( char * filename ) ) ( int ) ; " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef71 ( ) { // ticket #2348
2011-03-06 21:07:06 +01:00
{
const char code [ ] = " typedef int RexxFunctionHandler(); \n "
" RexxFunctionHandler *(efuncs[1]); \n " ;
2011-12-04 17:15:53 +01:00
const std : : string expected = " int ( * ( efuncs [ 1 ] ) ) ( ) ; " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef int RexxFunctionHandler(); \n "
" RexxFunctionHandler *(efuncs[]) = { NULL, NULL }; \n " ;
2011-12-04 17:15:53 +01:00
const std : : string expected = " int ( * ( efuncs [ ] ) ) ( ) = { 0 , 0 } ; " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef72 ( ) { // ticket #2374
2011-03-06 21:07:06 +01:00
// inline operator
{
const char code [ ] = " class Fred { \n "
" typedef int* (Fred::*F); \n "
" operator F() const { } \n "
" }; \n " ;
const std : : string expected = " class Fred { "
2011-12-04 15:40:05 +01:00
" "
2011-03-06 21:07:06 +01:00
" operatorint** ( ) const { } "
" } ; " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
// inline local variable
{
const char code [ ] = " class Fred { \n "
" typedef int INT; \n "
" void f1() const { INT i; } \n "
" }; \n " ;
const std : : string expected = " class Fred { "
2011-12-04 15:40:05 +01:00
" "
2011-03-06 21:07:06 +01:00
" void f1 ( ) const { int i ; } "
" } ; " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
// out of line member variable
{
const char code [ ] = " class Fred { \n "
" typedef int INT; \n "
" void f1() const; \n "
" }; \n "
" void Fred::f1() const { INT i; f(i); } \n " ;
const std : : string expected = " class Fred { "
2011-12-04 15:40:05 +01:00
" "
2011-03-06 21:07:06 +01:00
" void f1 ( ) const ; "
" } ; "
" void Fred :: f1 ( ) const { int i ; f ( i ) ; } " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
// out of line operator
{
const char code [ ] = " class Fred { \n "
" typedef int* (Fred::*F); \n "
" operator F() const; \n "
" }; \n "
" Fred::operator F() const { } \n " ;
const std : : string expected = " class Fred { "
2011-12-04 15:40:05 +01:00
" "
2011-03-06 21:07:06 +01:00
" operatorint** ( ) const ; "
" } ; "
" Fred :: operatorint** ( ) const { } " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef73 ( ) { // ticket #2412
2011-03-06 21:07:06 +01:00
const char code [ ] = " struct B {}; \n "
" typedef struct A : public B { \n "
" void f(); \n "
" } a, *aPtr; \n " ;
const std : : string expected = " struct B { } ; "
" struct A : public B { "
" void f ( ) ; "
" } ; " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef74 ( ) { // ticket #2414
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef long (*state_func_t)(void); \n "
" typedef state_func_t (*state_t)(void); \n "
" state_t current_state = death; \n "
" static char get_runlevel(const state_t); \n " ;
2011-12-04 17:15:53 +01:00
const std : : string expected = " long ( * ( * current_state ) ( void ) ) ( void ) = death ; "
2011-03-06 21:07:06 +01:00
" static char get_runlevel ( const long ( * ( * ) ( void ) ) ( void ) ) ; " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef75 ( ) { // ticket #2426
2012-12-06 19:19:22 +01:00
const char code [ ] = " typedef _Packed struct S { long l; }; \n " ;
2012-02-13 17:44:08 +01:00
const std : : string expected = " " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef76 ( ) { // ticket #2453 segmentation fault
2011-03-06 21:07:06 +01:00
const char code [ ] = " void f1(typedef int x) {} \n " ;
const std : : string expected = " void f1 ( typedef int x ) { } " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2012-09-09 09:14:46 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef77 ( ) { // ticket #2554
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef char Str[10]; int x = sizeof(Str); \n " ;
2011-12-04 17:15:53 +01:00
const std : : string expected = " int x ; x = 10 ; " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef78 ( ) { // ticket #2568
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef struct A A_t; \n "
" A_t a; \n "
" typedef struct A { } A_t; \n "
" A_t a1; \n " ;
2011-12-04 17:15:53 +01:00
const std : : string expected = " struct A a ; struct A { } ; struct A a1 ; " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef79 ( ) { // ticket #2348
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef int (Tcl_ObjCmdProc) (int x); \n "
" typedef struct LangVtab \n "
" { \n "
" Tcl_ObjCmdProc * (*V_LangOptionCommand); \n "
" } LangVtab; \n " ;
2011-12-04 17:15:53 +01:00
const std : : string expected = " struct LangVtab "
2011-03-06 21:07:06 +01:00
" { "
" int ( * ( * V_LangOptionCommand ) ) ( int x ) ; "
" } ; " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef80 ( ) { // ticket #2587
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef struct s { }; \n "
" void f() { \n "
" sizeof(struct s); \n "
" }; \n " ;
const std : : string expected = " struct s { } ; "
" void f ( ) { "
" sizeof ( struct s ) ; "
" } ; " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef81 ( ) { // ticket #2603 segmentation fault
2011-03-06 21:07:06 +01:00
checkSimplifyTypedef ( " typedef \n " ) ;
ASSERT_EQUALS ( " [test.cpp:1]: (error) syntax error \n " , errout . str ( ) ) ;
checkSimplifyTypedef ( " typedef constexpr \n " ) ;
2012-11-09 19:53:10 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef82 ( ) { // ticket #2403
2011-03-06 21:07:06 +01:00
checkSimplifyTypedef ( " class A { \n "
" public: \n "
" typedef int F(int idx); \n "
" }; \n "
" class B { \n "
" public: \n "
" A::F ** f; \n "
" }; \n "
" int main() \n "
" { \n "
" B * b = new B; \n "
" b->f = new A::F * [ 10 ]; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef83 ( ) { // ticket #2620
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef char Str[10]; \n "
" void f(Str &cl) { } \n " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " void f ( char ( & cl ) [ 10 ] ) { } " ) ;
2011-03-06 21:07:06 +01:00
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef84 ( ) { // ticket #2630 (segmentation fault)
2011-03-09 02:24:57 +01:00
const char code1 [ ] = " typedef y x () x \n " ;
checkSimplifyTypedef ( code1 ) ;
ASSERT_EQUALS ( " [test.cpp:1]: (error) syntax error \n " , errout . str ( ) ) ;
const char code2 [ ] = " typedef struct template <> \n " ;
checkSimplifyTypedef ( code2 ) ;
ASSERT_EQUALS ( " [test.cpp:1]: (error) syntax error \n " , errout . str ( ) ) ;
const char code3 [ ] = " typedef ::<> \n " ;
checkSimplifyTypedef ( code3 ) ;
2011-03-07 00:59:56 +01:00
ASSERT_EQUALS ( " [test.cpp:1]: (error) syntax error \n " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef85 ( ) { // ticket #2651
2011-03-16 00:24:30 +01:00
const char code [ ] = " typedef FOO ((BAR)(void, int, const int, int*)); \n " ;
const char expected [ ] = " ; " ;
checkSimplifyTypedef ( code ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-16 00:24:30 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef86 ( ) { // ticket #2581
2011-03-16 04:28:45 +01:00
const char code [ ] = " class relational { \n "
" typedef void (safe_bool_helper::*safe_bool)(); \n "
" public: \n "
" operator safe_bool() const; \n "
" safe_bool operator!() const; \n "
" }; \n " ;
const char expected [ ] = " class relational { "
2011-12-04 15:40:05 +01:00
" "
2011-03-16 04:28:45 +01:00
" public: "
" operatorsafe_bool ( ) const ; "
" safe_bool operator! ( ) const ; "
" } ; " ;
checkSimplifyTypedef ( code ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-16 04:28:45 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef87 ( ) { // ticket #2651
2011-03-18 01:00:49 +01:00
const char code [ ] = " typedef FOO (*(*BAR)(void, int, const int, int*)); \n " ;
const char expected [ ] = " ; " ;
checkSimplifyTypedef ( code ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-18 01:00:49 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef88 ( ) { // ticket #2675
2011-03-25 03:26:42 +01:00
const char code [ ] = " typedef short int (*x)(...); \n " ;
const char expected [ ] = " ; " ;
checkSimplifyTypedef ( code ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-25 03:26:42 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef89 ( ) { // ticket #2717
2011-04-14 05:24:41 +02:00
const char code [ ] = " class Fred { \n "
" typedef void f(int) const; \n "
" f func; \n "
" }; \n " ;
2011-12-04 15:40:05 +01:00
const char expected [ ] = " class Fred { void func ( int ) const ; } ; " ;
2011-04-14 05:24:41 +02:00
checkSimplifyTypedef ( code ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-04-14 05:24:41 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef90 ( ) { // ticket #2718
2011-04-14 05:58:58 +02:00
const char code [ ] = " typedef int IA[2]; \n "
" void f(const IA&) {}; \n " ;
2011-12-04 17:15:53 +01:00
const char expected [ ] = " void f ( const int ( & ) [ 2 ] ) { } ; " ;
2011-04-14 05:58:58 +02:00
checkSimplifyTypedef ( code ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-04-14 05:58:58 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef91 ( ) { // ticket #2716
2011-04-20 13:49:04 +02:00
const char code1 [ ] = " namespace NS { \n "
" typedef int (*T)(); \n "
" class A { \n "
" T f(); \n "
" }; \n "
" } \n "
" namespace NS { \n "
" T A::f() {} \n "
" } \n " ;
const char expected1 [ ] = " namespace NS { "
2011-12-04 15:40:05 +01:00
" "
2011-04-20 13:49:04 +02:00
" class A { "
" int ( * f ( ) ) ( ) ; "
" } ; "
" } "
" namespace NS { "
" int ( * A :: f ( ) ) ( ) { } "
" } " ;
checkSimplifyTypedef ( code1 ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected1 , tok ( code1 ) ) ;
2011-04-20 13:49:04 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
const char code2 [ ] = " namespace NS { \n "
" typedef int (*T)(); \n "
" class A { \n "
" T f(); \n "
" }; \n "
" } \n "
" NS::T NS::A::f() {} \n " ;
const char expected2 [ ] = " namespace NS { "
2011-12-04 15:40:05 +01:00
" "
2011-04-20 13:49:04 +02:00
" class A { "
" int ( * f ( ) ) ( ) ; "
" } ; "
" } "
" int ( * NS :: A :: f ( ) ) ( ) { } " ;
checkSimplifyTypedef ( code2 ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected2 , tok ( code2 ) ) ;
2011-04-20 13:49:04 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
const char code3 [ ] = " namespace NS1 { \n "
" namespace NS2 { \n "
" typedef int (*T)(); \n "
" class A { \n "
" T f(); \n "
" }; \n "
" } \n "
" } \n "
" namespace NS1 { \n "
" namespace NS2 { \n "
" T A::f() {} \n "
" } \n "
" } \n " ;
const char expected3 [ ] = " namespace NS1 { "
" namespace NS2 { "
2011-12-04 15:40:05 +01:00
" "
2011-04-20 13:49:04 +02:00
" class A { "
" int ( * f ( ) ) ( ) ; "
" } ; "
" } "
" } "
" namespace NS1 { "
" namespace NS2 { "
" int ( * A :: f ( ) ) ( ) { } "
" } "
" } " ;
checkSimplifyTypedef ( code3 ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected3 , tok ( code3 ) ) ;
2011-04-20 13:49:04 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
const char code4 [ ] = " namespace NS1 { \n "
" namespace NS2 { \n "
" typedef int (*T)(); \n "
" class A { \n "
" T f(); \n "
" }; \n "
" } \n "
" } \n "
" namespace NS1 { \n "
" NS2::T NS2::A::f() {} \n "
" } \n " ;
const char expected4 [ ] = " namespace NS1 { "
" namespace NS2 { "
2011-12-04 15:40:05 +01:00
" "
2011-04-20 13:49:04 +02:00
" class A { "
" int ( * f ( ) ) ( ) ; "
" } ; "
" } "
" } "
" namespace NS1 { "
" int ( * NS2 :: A :: f ( ) ) ( ) { } "
" } " ;
checkSimplifyTypedef ( code4 ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected4 , tok ( code4 ) ) ;
2011-04-20 13:49:04 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef92 ( ) { // ticket #2736 (segmentation fault)
2011-04-20 13:49:04 +02:00
const char code [ ] = " typedef long Long; \n "
2011-04-16 16:35:23 +02:00
" namespace NS { \n "
" } \n " ;
checkSimplifyTypedef ( code ) ;
2012-09-03 15:20:34 +02:00
ASSERT_EQUALS ( " " , tok ( code ) ) ;
2011-04-16 16:35:23 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef93 ( ) { // ticket #2738 (syntax error)
2011-04-21 01:09:28 +02:00
const char code [ ] = " struct s { double x; }; \n "
" typedef struct s (*binop) (struct s, struct s); \n " ;
const char expected [ ] = " struct s { double x ; } ; " ;
checkSimplifyTypedef ( code ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-04-21 01:09:28 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef94 ( ) { // ticket #1982
2011-05-03 04:42:52 +02:00
const char code1 [ ] = " class A { \n "
" public: \n "
" typedef struct { \n "
" int a[4]; \n "
" } data; \n "
" }; \n "
" A::data d; \n " ;
const char expected1 [ ] = " class A { "
" public: "
" struct data { "
" int a [ 4 ] ; "
" } ; "
" } ; "
" struct A :: data d ; " ;
checkSimplifyTypedef ( code1 ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected1 , tok ( code1 ) ) ;
2011-05-03 04:42:52 +02:00
TODO_ASSERT_EQUALS ( " [test.cpp:7]: (debug) Scope::checkVariable found variable 'd' with varid 0. \n " , " " , errout . str ( ) ) ;
const char code2 [ ] = " class A { \n "
" public: \n "
" typedef struct { \n "
" int a[4]; \n "
" } data; \n "
" }; \n "
" ::A::data d; \n " ;
const char expected2 [ ] = " class A { "
" public: "
" struct data { "
" int a [ 4 ] ; "
" } ; "
" } ; "
" struct :: A :: data d ; " ;
checkSimplifyTypedef ( code2 ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected2 , tok ( code2 ) ) ;
2011-05-03 04:42:52 +02:00
TODO_ASSERT_EQUALS ( " [test.cpp:7]: (debug) Scope::checkVariable found variable 'd' with varid 0. \n " , " " , errout . str ( ) ) ;
const char code3 [ ] = " class A { \n "
" public: \n "
" typedef struct { \n "
" int a[4]; \n "
" } data; \n "
" }; \n "
" class B : public ::A::data { }; \n " ;
const char expected3 [ ] = " class A { "
" public: "
" struct data { "
" int a [ 4 ] ; "
" } ; "
" } ; "
" class B : public :: A :: data { } ; " ;
checkSimplifyTypedef ( code3 ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected3 , tok ( code3 ) ) ;
2011-05-03 04:42:52 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef95 ( ) { // ticket #2844
2011-06-20 02:19:16 +02:00
const char code [ ] = " class symbol_table { \n "
" public: \n "
" typedef expression_error::error_code (*valid_func)(void *cbparam, const char *name, expression_space space); \n "
" valid_func f; \n "
" }; \n " ;
const char expected [ ] = " class symbol_table { "
" public: "
2011-12-04 15:40:05 +01:00
" "
2011-06-20 02:19:16 +02:00
" expression_error :: error_code ( * f ) ( void * cbparam , const char * name , expression_space space ) ; "
" } ; " ;
checkSimplifyTypedef ( code ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-06-19 23:57:01 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef96 ( ) { // ticket #2886 (segmentation fault)
2011-07-03 16:50:15 +02:00
const char code [ ] = " typedef struct x { } \n " ;
2012-04-17 09:51:08 +02:00
tok ( code ) ;
2011-07-03 16:50:15 +02:00
ASSERT_EQUALS ( " [test.cpp:1]: (error) syntax error \n " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef97 ( ) { // ticket #2983 (segmentation fault)
2011-08-10 00:31:02 +02:00
const char code [ ] = " typedef x y \n "
" (A); y \n " ;
2012-04-17 09:51:08 +02:00
tok ( code ) ;
2011-08-10 00:31:02 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef98 ( ) { // ticket #2963
2012-11-30 12:40:22 +01:00
const char code [ ] = " typedef int type ## __LINE__; \n "
" typedef int type ## __LINE__; \n "
" type1 x; \n "
" type2 y; " ;
ASSERT_EQUALS ( " int x ; int y ; " , tok ( code ) ) ;
2011-08-12 00:18:47 +02:00
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef99 ( ) { // ticket #2999
2011-08-15 00:06:05 +02:00
const char code [ ] = " typedef struct Fred Fred; \n "
" struct Fred { }; \n " ;
2012-04-17 09:51:08 +02:00
tok ( code ) ;
2011-08-15 00:06:05 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
const char code1 [ ] = " struct Fred { }; \n "
" typedef struct Fred Fred; \n " ;
2012-04-17 09:51:08 +02:00
tok ( code1 ) ;
2011-08-15 00:06:05 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef100 ( ) { // ticket #3000
2011-08-15 00:50:33 +02:00
const char code [ ] = " typedef struct Fred { } Fred; \n "
" Fred * foo() { \n "
" Fred *fred; \n "
" fred = se_alloc(sizeof(struct Fred)); \n "
" return fred; \n "
" } \n " ;
2012-04-17 09:51:08 +02:00
tok ( code ) ;
2011-08-15 00:50:33 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef101 ( ) { // ticket #3003 (segmentation fault)
2011-08-15 02:38:16 +02:00
const char code [ ] = " typedef a x[]; \n "
" y = x \n " ;
2012-04-17 09:51:08 +02:00
tok ( code ) ;
2011-08-15 02:38:16 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (error) syntax error \n " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef102 ( ) { // ticket #3004
2011-08-15 03:00:05 +02:00
const char code [ ] = " typedef struct { } Fred; \n "
" void foo() \n "
" { \n "
" Fred * Fred; \n "
" } \n " ;
2012-04-17 09:51:08 +02:00
tok ( code ) ;
2011-08-15 03:00:05 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef103 ( ) { // ticket #3007
2011-08-15 12:56:15 +02:00
const char code [ ] = " typedef struct { } Fred; \n "
" void foo() \n "
" { \n "
" Fred Fred; \n "
" } \n " ;
2012-04-17 09:51:08 +02:00
tok ( code ) ;
2011-08-15 12:56:15 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedef104 ( ) { // ticket #3070
2011-09-02 01:10:58 +02:00
const char code [ ] = " typedef int (*in_func) (void FAR *, unsigned char FAR * FAR *); \n " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " ; " , tok ( code ) ) ;
2011-09-02 01:10:58 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2012-02-18 17:57:50 +01:00
void simplifyTypedef105 ( ) { // ticket #3616 (segmentation fault)
const char code [ ] = " ( int typedef char x; ){} \n " ;
2012-04-17 09:51:08 +02:00
tok ( code ) ;
2012-02-18 17:57:50 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2012-03-25 10:33:18 +02:00
void simplifyTypedef106 ( ) { // ticket #3619 (segmentation fault)
const char code [ ] = " typedef void f (); \n typedef { f } " ;
2012-04-17 09:51:08 +02:00
tok ( code ) ;
2012-03-25 10:33:18 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2012-08-07 16:39:41 +02:00
void simplifyTypedef107 ( ) { // ticket #3963 (bad code => segmentation fault)
const char code [ ] = " typedef int x[]; int main() { return x } " ;
tok ( code ) ;
2012-08-07 16:42:11 +02:00
ASSERT_EQUALS ( " [test.cpp:1]: (error) syntax error \n " , errout . str ( ) ) ;
2012-08-07 16:39:41 +02:00
}
2011-10-13 20:53:06 +02:00
void simplifyTypedefFunction1 ( ) {
2011-03-06 21:07:06 +01:00
{
const char code [ ] = " typedef void (*my_func)(); \n "
" std::queue<my_func> func_queue; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " std :: queue < void ( * ) ( ) > func_queue ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void (*my_func)(void); \n "
" std::queue<my_func> func_queue; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " std :: queue < void ( * ) ( void ) > func_queue ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void (*my_func)(int); \n "
" std::queue<my_func> func_queue; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " std :: queue < void ( * ) ( int ) > func_queue ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void (*my_func)(int*); \n "
" std::queue<my_func> func_queue; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " std :: queue < void ( * ) ( int * ) > func_queue ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
// ticket # 1615
const char code [ ] = " typedef void (*my_func)(arg_class*); \n "
" std::queue<my_func> func_queue; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " std :: queue < void ( * ) ( arg_class * ) > func_queue ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void (my_func)(); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " std :: queue < void ( * ) ( ) > func_queue ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void (my_func)(void); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " std :: queue < void ( * ) ( void ) > func_queue ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void (my_func)(int); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " std :: queue < void ( * ) ( int ) > func_queue ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void (my_func)(int*); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " std :: queue < void ( * ) ( int * ) > func_queue ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void (my_func)(arg_class*); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " std :: queue < void ( * ) ( arg_class * ) > func_queue ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void my_func(); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " std :: queue < void ( * ) ( ) > func_queue ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void my_func(void); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " std :: queue < void ( * ) ( void ) > func_queue ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void my_func(int); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " std :: queue < void ( * ) ( int ) > func_queue ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void my_func(int*); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " std :: queue < void ( * ) ( int * ) > func_queue ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void my_func(arg_class*); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " std :: queue < void ( * ) ( arg_class * ) > func_queue ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void (my_func()); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " std :: queue < void ( * ) ( ) > func_queue ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void (my_func(void)); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " std :: queue < void ( * ) ( void ) > func_queue ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void (my_func(int)); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " std :: queue < void ( * ) ( int ) > func_queue ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void (my_func(int*)); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " std :: queue < void ( * ) ( int * ) > func_queue ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void (my_func(arg_class*)); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " std :: queue < void ( * ) ( arg_class * ) > func_queue ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
}
2011-10-13 20:53:06 +02:00
void simplifyTypedefFunction2 ( ) { // ticket #1685
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef void voidfn (int); \n "
" voidfn xxx; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " void xxx ( int ) ; " ) ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyTypedefFunction3 ( ) {
2011-03-06 21:07:06 +01:00
{
const char code [ ] = " typedef C func1(); \n "
" typedef C (* func2)(); \n "
" typedef C (& func3)(); \n "
" typedef C (C::* func4)(); \n "
" typedef C (C::* func5)() const; \n "
" typedef C (C::* func6)() volatile; \n "
" typedef C (C::* func7)() const volatile; \n "
" func1 f1; \n "
" func2 f2; \n "
" func3 f3; \n "
" func4 f4; \n "
" func5 f5; \n "
" func6 f6; \n "
" func7 f7; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " C f1 ( ) ; "
2011-03-06 21:07:06 +01:00
" C * f2 ; " // this gets simplified to a regular pointer
" C ( & f3 ) ( ) ; "
" C ( C :: * f4 ) ( ) ; "
" C ( C :: * f5 ) ( ) const ; "
" C ( C :: * f6 ) ( ) ; " // volatile is removed
" C ( C :: * f7 ) ( ) const ; " ) ; // volatile is removed
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef C const func1(); \n "
" typedef C const (* func2)(); \n "
" typedef C const (& func3)(); \n "
" typedef C const (C::* func4)(); \n "
" typedef C const (C::* func5)() const; \n "
" typedef C const (C::* func6)() volatile; \n "
" typedef C const (C::* func7)() const volatile; \n "
" func1 f1; \n "
" func2 f2; \n "
" func3 f3; \n "
" func4 f4; \n "
" func5 f5; \n "
" func6 f6; \n "
" func7 f7; " ;
// The expected result..
// C const -> const C
2011-12-04 17:15:53 +01:00
const std : : string expected ( " const C f1 ( ) ; "
2011-03-06 21:07:06 +01:00
" const C * f2 ; " // this gets simplified to a regular pointer
" const C ( & f3 ) ( ) ; "
" const C ( C :: * f4 ) ( ) ; "
" const C ( C :: * f5 ) ( ) const ; "
" const C ( C :: * f6 ) ( ) ; " // volatile is removed
" const C ( C :: * f7 ) ( ) const ; " ) ; // volatile is removed
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef const C func1(); \n "
" typedef const C (* func2)(); \n "
" typedef const C (& func3)(); \n "
" typedef const C (C::* func4)(); \n "
" typedef const C (C::* func5)() const; \n "
" typedef const C (C::* func6)() volatile; \n "
" typedef const C (C::* func7)() const volatile; \n "
" func1 f1; \n "
" func2 f2; \n "
" func3 f3; \n "
" func4 f4; \n "
" func5 f5; \n "
" func6 f6; \n "
" func7 f7; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " const C f1 ( ) ; "
2011-03-06 21:07:06 +01:00
" const C * f2 ; " // this gets simplified to a regular pointer
" const C ( & f3 ) ( ) ; "
" const C ( C :: * f4 ) ( ) ; "
" const C ( C :: * f5 ) ( ) const ; "
" const C ( C :: * f6 ) ( ) ; " // volatile is removed
" const C ( C :: * f7 ) ( ) const ; " ) ; // volatile is removed
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef C * func1(); \n "
" typedef C * (* func2)(); \n "
" typedef C * (& func3)(); \n "
" typedef C * (C::* func4)(); \n "
" typedef C * (C::* func5)() const; \n "
" typedef C * (C::* func6)() volatile; \n "
" typedef C * (C::* func7)() const volatile; \n "
" func1 f1; \n "
" func2 f2; \n "
" func3 f3; \n "
" func4 f4; \n "
" func5 f5; \n "
" func6 f6; \n "
" func7 f7; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " C * f1 ( ) ; "
2011-03-06 21:07:06 +01:00
" C * * f2 ; " // this gets simplified to a regular pointer
" C * ( & f3 ) ( ) ; "
" C * ( C :: * f4 ) ( ) ; "
" C * ( C :: * f5 ) ( ) const ; "
" C * ( C :: * f6 ) ( ) ; " // volatile is removed
" C * ( C :: * f7 ) ( ) const ; " ) ; // volatile is removed
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef const C * func1(); \n "
" typedef const C * (* func2)(); \n "
" typedef const C * (& func3)(); \n "
" typedef const C * (C::* func4)(); \n "
" typedef const C * (C::* func5)() const; \n "
" typedef const C * (C::* func6)() volatile; \n "
" typedef const C * (C::* func7)() const volatile; \n "
" func1 f1; \n "
" func2 f2; \n "
" func3 f3; \n "
" func4 f4; \n "
" func5 f5; \n "
" func6 f6; \n "
" func7 f7; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " const C * f1 ( ) ; "
2011-03-06 21:07:06 +01:00
" const C * * f2 ; " // this gets simplified to a regular pointer
" const C * ( & f3 ) ( ) ; "
" const C * ( C :: * f4 ) ( ) ; "
" const C * ( C :: * f5 ) ( ) const ; "
" const C * ( C :: * f6 ) ( ) ; " // volatile is removed
" const C * ( C :: * f7 ) ( ) const ; " ) ; // volatile is removed
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef C const * func1(); \n "
" typedef C const * (* func2)(); \n "
" typedef C const * (& func3)(); \n "
" typedef C const * (C::* func4)(); \n "
" typedef C const * (C::* func5)() const; \n "
" typedef C const * (C::* func6)() volatile; \n "
" typedef C const * (C::* func7)() const volatile; \n "
" func1 f1; \n "
" func2 f2; \n "
" func3 f3; \n "
" func4 f4; \n "
" func5 f5; \n "
" func6 f6; \n "
" func7 f7; " ;
// The expected result..
// C const -> const C
2011-12-04 17:15:53 +01:00
const std : : string expected ( " const C * f1 ( ) ; "
2011-03-06 21:07:06 +01:00
" const C * * f2 ; " // this gets simplified to a regular pointer
" const C * ( & f3 ) ( ) ; "
" const C * ( C :: * f4 ) ( ) ; "
" const C * ( C :: * f5 ) ( ) const ; "
" const C * ( C :: * f6 ) ( ) ; " // volatile is removed
" const C * ( C :: * f7 ) ( ) const ; " ) ; // volatile is removed
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
}
2011-10-13 20:53:06 +02:00
void simplifyTypedefFunction4 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef int ( * ( * type1 ) ( bool ) ) ( int , int ) ; \n "
" typedef int ( * ( type2 ) ( bool ) ) ( int , int ) ; \n "
" typedef int ( * type3 ( bool ) ) ( int , int ) ; \n "
" type1 t1; \n "
" type2 t2; \n "
" type3 t3; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " int ( * ( * t1 ) ( bool ) ) ( int , int ) ; "
2011-03-06 21:07:06 +01:00
" int ( * t2 ( bool ) ) ( int , int ) ; "
" int ( * t3 ( bool ) ) ( int , int ) ; " ) ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedefFunction5 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef int ( * type1 ) ( float ) ; \n "
" typedef int ( * const type2 ) ( float ) ; \n "
" typedef int ( * volatile type3 ) ( float ) ; \n "
" typedef int ( * const volatile type4 ) ( float ) ; \n "
" typedef int ( C :: * type5 ) ( float ) ; \n "
" typedef int ( C :: * const type6 ) ( float ) ; \n "
" typedef int ( C :: * volatile type7 ) ( float ) ; \n "
" typedef int ( C :: * const volatile type8 ) ( float ) ; \n "
" typedef int ( :: C :: * type9 ) ( float ) ; \n "
" typedef int ( :: C :: * const type10 ) ( float ) ; \n "
" typedef int ( :: C :: * volatile type11 ) ( float ) ; \n "
" typedef int ( :: C :: * const volatile type12 ) ( float ) ; \n "
" type1 t1; \n "
" type2 t2; \n "
" type3 t3; \n "
" type4 t4; \n "
" type5 t5; \n "
" type6 t6; \n "
" type7 t7; \n "
" type8 t8; \n "
" type9 t9; \n "
" type10 t10; \n "
" type11 t11; \n "
" type12 t12; " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " int * t1 ; " // simplified to regular pointer
2011-03-06 21:07:06 +01:00
" int ( * const t2 ) ( float ) ; "
" int * t3 ; " // volatile removed, gets simplified to regular pointer
" int ( * const t4 ) ( float ) ; " // volatile removed
" int ( C :: * t5 ) ( float ) ; "
" int ( C :: * const t6 ) ( float ) ; "
" int ( C :: * t7 ) ( float ) ; " // volatile removed
" int ( C :: * const t8 ) ( float ) ; " // volatile removed
" int ( :: C :: * t9 ) ( float ) ; "
" int ( :: C :: * const t10 ) ( float ) ; "
" int ( :: C :: * t11 ) ( float ) ; " // volatile removed
" int ( :: C :: * const t12 ) ( float ) ; " ) ; // volatile removed
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedefFunction6 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef void (*testfp)(); \n "
" struct Fred \n "
" { \n "
" testfp get1() { return 0; } \n "
" void ( * get2 ( ) ) ( ) { return 0 ; } \n "
" testfp get3(); \n "
" void ( * get4 ( ) ) ( ); \n "
" }; \n "
" testfp Fred::get3() { return 0; } \n "
" void ( * Fred::get4 ( ) ) ( ) { return 0 ; } \n " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " struct Fred "
2011-03-06 21:07:06 +01:00
" { "
" void ( * get1 ( ) ) ( ) { return 0 ; } "
" void ( * get2 ( ) ) ( ) { return 0 ; } "
" void ( * get3 ( ) ) ( ) ; "
" void ( * get4 ( ) ) ( ) ; "
" } ; "
" void ( * Fred :: get3 ( ) ) ( ) { return 0 ; } "
" void ( * Fred :: get4 ( ) ) ( ) { return 0 ; } " ) ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedefFunction7 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef void ( __gnu_cxx :: _SGIAssignableConcept < _Tp > :: * _func_Tp_SGIAssignableConcept ) () ; "
" _func_Tp_SGIAssignableConcept X; \n " ;
// The expected result..
2011-12-04 17:15:53 +01:00
const std : : string expected ( " void ( __gnu_cxx :: _SGIAssignableConcept < _Tp > :: * X ) ( ) ; " ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyTypedefFunction8 ( ) {
2011-03-06 21:07:06 +01:00
// #2376 - internal error
const char code [ ] = " typedef int f_expand(const nrv_byte *); \n "
" void f(f_expand *(*get_fexp(int))){} \n " ;
checkSimplifyTypedef ( code ) ;
2012-08-12 12:01:24 +02:00
TODO_ASSERT_EQUALS ( " " , " [test.cpp:2]: (debug) Function::addArguments found argument 'int' with varid 0. \n " , errout . str ( ) ) ; // make sure that there is no internal error
2011-03-06 21:07:06 +01:00
}
2011-10-23 10:25:14 +02:00
void simplifyOperator1 ( ) {
// #3237 - error merging namespaces with operators
const char code [ ] = " class c { \n "
" public: \n "
" operator std::string() const; \n "
" operator string() const; \n "
" }; \n " ;
const char expected [ ] = " class c { "
" public: "
" operatorstd::string ( ) const ; "
" operatorstring ( ) const ; "
" } ; " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void reverseArraySyntax ( ) {
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " a [ 13 ] " , tok ( " 13[a] " ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplify_numeric_condition ( ) {
2011-03-06 21:07:06 +01:00
{
const char code [ ] =
" void f() \n "
" { \n "
" int x = 0; \n "
" if( !x || 0 ) \n "
" { g(); \n "
" } \n "
" } " ;
2011-12-02 23:01:55 +01:00
ASSERT_EQUALS ( " void f ( ) { g ( ) ; } " , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
{
const char code [ ] =
" void f() \n "
" { \n "
" int x = 1; \n "
" if( !x ) \n "
" { g(); \n "
" } \n "
" } " ;
2011-12-02 23:01:55 +01:00
ASSERT_EQUALS ( " void f ( ) { } " , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
{
const char code [ ] =
" void f() \n "
" { \n "
" bool x = true; \n "
" if( !x ) \n "
" { g(); \n "
" } \n "
" } " ;
2011-12-02 23:01:55 +01:00
ASSERT_EQUALS ( " void f ( ) { } " , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
{
const char code [ ] =
" void f() \n "
" { \n "
" bool x = false; \n "
" if( !x ) \n "
" { g(); \n "
" } \n "
" } " ;
2011-12-02 23:01:55 +01:00
ASSERT_EQUALS ( " void f ( ) { g ( ) ; } " , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
{
const char code [ ] = " void f() \n "
" { \n "
" if (5==5); \n "
" } \n " ;
2011-11-12 12:01:31 +01:00
ASSERT_EQUALS ( " void f ( ) { ; } " , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
{
const char code [ ] = " void f() \n "
" { \n "
" if (4<5); \n "
" } \n " ;
2011-11-12 12:01:31 +01:00
ASSERT_EQUALS ( " void f ( ) { ; } " , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
{
const char code [ ] = " void f() \n "
" { \n "
" if (5<5); \n "
" } \n " ;
ASSERT_EQUALS ( " void f ( ) { } " , tok ( code ) ) ;
}
{
const char code [ ] = " void f() \n "
" { \n "
" if (13>12?true:false); \n "
" } \n " ;
2011-11-12 12:01:31 +01:00
ASSERT_EQUALS ( " void f ( ) { ; } " , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
}
2011-10-13 20:53:06 +02:00
void simplify_condition ( ) {
2011-03-06 21:07:06 +01:00
{
const char code [ ] =
" void f(int a) \n "
" { \n "
" if (a && false) g(); \n "
" } " ;
ASSERT_EQUALS ( " void f ( int a ) { } " , tok ( code ) ) ;
}
{
const char code [ ] =
" void f(int a) \n "
" { \n "
" if (false && a) g(); \n "
" } " ;
ASSERT_EQUALS ( " void f ( int a ) { } " , tok ( code ) ) ;
}
{
const char code [ ] =
" void f(int a) \n "
" { \n "
" if (true || a) g(); \n "
" } " ;
2011-11-12 12:01:31 +01:00
ASSERT_EQUALS ( " void f ( int a ) { g ( ) ; } " , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
{
const char code [ ] =
" void f(int a) \n "
" { \n "
" if (a || true) g(); \n "
" } " ;
2011-11-12 12:01:31 +01:00
ASSERT_EQUALS ( " void f ( int a ) { g ( ) ; } " , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
2012-01-26 23:12:01 +01:00
{
const char code [ ] =
" void f(int a) \n "
" { \n "
" if (a || true || b) g(); \n "
" } " ;
ASSERT_EQUALS ( " void f ( int a ) { g ( ) ; } " , tok ( code ) ) ;
}
{
const char code [ ] =
" void f(int a) \n "
" { \n "
" if (a && false && b) g(); \n "
" } " ;
ASSERT_EQUALS ( " void f ( int a ) { } " , tok ( code ) ) ;
}
{
const char code [ ] =
" void f(int a) \n "
" { \n "
" if (a || (b && false && c) || d) g(); \n "
" } " ;
ASSERT_EQUALS ( " void f ( int a ) { if ( a || d ) { g ( ) ; } } " , tok ( code ) ) ;
}
{
const char code [ ] =
" void f(int a) \n "
" { \n "
" if ((a && b) || true || (c && d)) g(); \n "
" } " ;
ASSERT_EQUALS ( " void f ( int a ) { g ( ) ; } " , tok ( code ) ) ;
}
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void pointeralias1 ( ) {
2011-03-06 21:07:06 +01:00
{
const char code [ ] = " void f() \n "
" { \n "
" char buf[100]; \n "
" char *p = buf; \n "
" free(p); \n "
" } \n " ;
const char expected [ ] = " void f ( ) "
" { "
" char buf [ 100 ] ; "
" free ( buf ) ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
{
const char code [ ] = " void f(char *p1) \n "
" { \n "
" char *p = p1; \n "
" p1 = 0; "
" x(p); \n "
" } \n " ;
const char expected [ ] = " void f ( char * p1 ) "
" { "
" char * p ; p = p1 ; "
" p1 = 0 ; "
" x ( p ) ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
{
const char code [ ] = " void foo(Result* ptr) \n "
" { \n "
" Result* obj = ptr; \n "
" ++obj->total; \n "
" } \n " ;
const char expected [ ] = " void foo ( Result * ptr ) "
" { "
" Result * obj ; obj = ptr ; "
" ++ obj . total ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
{
const char code [ ] = " int *foo() \n "
" { \n "
" int a[10]; \n "
" int *b = a; \n "
" return b; \n "
" } \n " ;
const char expected [ ] = " int * foo ( ) "
" { "
" int a [ 10 ] ; "
" return a ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
{
const char code [ ] = " void f() { \n "
" int a[10]; \n "
" int *b = a; \n "
" memset(b,0,sizeof(a)); \n "
" } " ;
const char expected [ ] = " void f ( ) { "
" int a [ 10 ] ; "
" memset ( a , 0 , 40 ) ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
}
2011-10-13 20:53:06 +02:00
void pointeralias2 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " void f() \n "
" { \n "
" int i; \n "
" int *p = &i; \n "
" return *p; \n "
" } \n " ;
ASSERT_EQUALS ( " void f ( ) { int i ; return i ; } " , tok ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void pointeralias3 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " void f() \n "
" { \n "
" int i, j, *p; \n "
" if (ab) p = &i; \n "
" else p = &j; \n "
" *p = 0; \n "
" } \n " ;
const char expected [ ] = " void f ( ) "
" { "
" int i ; int j ; int * p ; "
" if ( ab ) { p = & i ; } "
" else { p = & j ; } "
" * p = 0 ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void pointeralias4 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " void f() \n "
" { \n "
" int a[10]; \n "
" int *p = &a[0]; \n "
" *p = 0; \n "
" } \n " ;
const char expected [ ] = " void f ( ) "
" { "
" int a [ 10 ] ; "
" * a = 0 ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void pointeralias5 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " int f() \n "
" { \n "
" int i; \n "
" int *p = &i; \n "
" *p = 5; \n "
" return i; \n "
" } \n " ;
const char expected [ ] = " int f ( ) "
" { "
2011-12-02 23:01:55 +01:00
" return 5 ; "
2011-03-06 21:07:06 +01:00
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void reduceConstness ( ) {
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " char * p ; " , tok ( " char * const p; " ) ) ;
}
2011-10-13 20:53:06 +02:00
void while0 ( ) {
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " ; x = 1 ; " , tok ( " ; do { x = 1 ; } while (0); " ) ) ;
2011-10-19 18:20:29 +02:00
ASSERT_EQUALS ( " ; return 0 ; " , tok ( " ; do { return 0; } while (0); " ) ) ;
ASSERT_EQUALS ( " void foo ( ) { goto label ; } " , tok ( " void foo() { do { goto label; } while (0); } " ) ) ;
ASSERT_EQUALS ( " ; { continue ; } " , tok ( " ; do { continue ; } while (0); " ) ) ;
ASSERT_EQUALS ( " ; { break ; } " , tok ( " ; do { break; } while (0); " ) ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " ; " , tok ( " ; while (false) { a; } " ) ) ;
2011-10-19 18:49:02 +02:00
ASSERT_EQUALS ( " ; " , tok ( " ; while (false) { switch (n) { case 0: return; default: break; } n*=1; } " ) ) ;
2011-10-10 15:13:47 +02:00
}
2011-03-06 21:07:06 +01:00
2011-10-13 20:53:06 +02:00
void while0for ( ) {
2011-03-06 21:07:06 +01:00
// for (condition is always false)
2011-12-02 23:01:55 +01:00
ASSERT_EQUALS ( " void f ( ) { } " , tok ( " void f() { int i; for (i = 0; i < 0; i++) { a; } } " ) ) ;
2011-10-10 15:13:47 +02:00
//ticket #3140
2011-12-02 23:01:55 +01:00
ASSERT_EQUALS ( " void f ( ) { } " , tok ( " void f() { int i; for (i = 0; i < 0; i++) { foo(); break; } } " ) ) ;
ASSERT_EQUALS ( " void f ( ) { } " , tok ( " void f() { int i; for (i = 0; i < 0; i++) { foo(); continue; } } " ) ) ;
2011-10-10 15:13:47 +02:00
ASSERT_EQUALS ( " void f ( ) { } " , tok ( " void f() { for (int i = 0; i < 0; i++) { a; } } " ) ) ;
ASSERT_EQUALS ( " void f ( ) { } " , tok ( " void f() { for (unsigned int i = 0; i < 0; i++) { a; } } " ) ) ;
ASSERT_EQUALS ( " void f ( ) { } " , tok ( " void f() { for (long long i = 0; i < 0; i++) { a; } } " ) ) ;
ASSERT_EQUALS ( " void f ( ) { } " , tok ( " void f() { for (signed long long i = 0; i < 0; i++) { a; } } " ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void while1 ( ) {
2011-03-06 21:07:06 +01:00
// ticket #1197
const char code [ ] = " void do {} while (0) { } " ;
const char expected [ ] = " void { } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2011-10-13 20:53:06 +02:00
void enum1 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " enum A { a, b, c }; A c1 = c; " ;
2011-12-04 18:08:35 +01:00
const char expected [ ] = " int c1 ; c1 = 2 ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void enum2 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " enum A { a, }; int array[a]; " ;
2011-12-04 18:08:35 +01:00
const char expected [ ] = " int array [ 0 ] ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void enum3 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " enum { a, }; int array[a]; " ;
2011-12-04 18:08:35 +01:00
const char expected [ ] = " int array [ 0 ] ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void enum4 ( ) {
2011-03-06 21:07:06 +01:00
{
const char code [ ] = " class A { \n "
" public: \n "
" enum EA { a1, a2, a3 }; \n "
" EA get() const; \n "
" void put(EA a) { ea = a; ea = a1; } \n "
" private: \n "
" EA ea; \n "
" }; \n "
" A::EA A::get() const { return ea; } \n "
" A::EA e = A::a1; " ;
const char expected [ ] = " class A { "
" public: "
2011-12-04 18:08:35 +01:00
" "
2011-03-06 21:07:06 +01:00
" int get ( ) const ; "
" void put ( int a ) { ea = a ; ea = 0 ; } "
" private: "
" int ea ; "
" } ; "
" int A :: get ( ) const { return ea ; } "
" int e ; e = 0 ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " struct A { \n "
" enum EA { a1, a2, a3 }; \n "
" EA get() const; \n "
" void put(EA a) { ea = a; ea = a1; } \n "
" EA ea; \n "
" }; \n "
" A::EA A::get() const { return ea; } \n "
" A::EA e = A::a1; " ;
const char expected [ ] = " struct A { "
2011-12-04 18:08:35 +01:00
" "
2011-03-06 21:07:06 +01:00
" int get ( ) const ; "
" void put ( int a ) { ea = a ; ea = 0 ; } "
" int ea ; "
" } ; "
" int A :: get ( ) const { return ea ; } "
" int e ; e = 0 ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
}
2011-10-13 20:53:06 +02:00
void enum5 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " enum ABC { \n "
" a = sizeof(int), \n "
" b = 1 + a, \n "
" c = b + 100, \n "
" d, \n "
" e, \n "
" f = 90, \n "
" g \n "
" }; \n "
" int sum = a + b + c + d + e + f + g; " ;
2011-12-04 18:08:35 +01:00
const char expected [ ] = " int sum ; sum = "
2011-03-06 21:07:06 +01:00
" sizeof ( int ) + "
" 1 + sizeof ( int ) + "
2012-09-08 10:15:37 +02:00
" 1 + sizeof ( int ) + 101 + " // 101 = 100 + 1
" sizeof ( int ) + 102 + " // 102 = 100 + 1 + 1
" sizeof ( int ) + 283 " // 283 = 100+2+90+91
" ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
2011-12-04 18:08:35 +01:00
ASSERT_EQUALS ( " int sum ; sum = 508 ; " , tok ( code , true ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void enum6 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " enum { a = MAC(A, B, C) }; void f(a) { } " ;
2012-09-21 16:17:20 +02:00
const char expected [ ] = " void f ( a ) { } " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void enum7 ( ) {
2011-03-06 21:07:06 +01:00
{
// ticket 1388
const char code [ ] = " enum FOO {A,B,C}; \n "
" int main() \n "
" { \n "
" int A = B; \n "
" { float A = C; } \n "
" } " ;
2011-12-04 18:08:35 +01:00
const char expected [ ] = " int main ( ) "
2011-03-06 21:07:06 +01:00
" { "
" int A ; A = 1 ; "
" { float A ; A = 2 ; } "
" } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " enum FOO {A,B,C}; \n "
" void f(int A, float B, char C) { } " ;
2011-12-04 18:08:35 +01:00
const char expected [ ] = " void f ( int A , float B , char C ) { } " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
}
// Check simplifyEnum
2012-04-17 09:51:08 +02:00
std : : string checkSimplifyEnum ( const char code [ ] ) {
2011-03-06 21:07:06 +01:00
errout . str ( " " ) ;
// Tokenize..
Settings settings ;
2011-08-07 09:28:08 +02:00
settings . addEnabled ( " style " ) ;
2011-03-06 21:07:06 +01:00
Tokenizer tokenizer ( & settings , this ) ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
2012-04-17 09:51:08 +02:00
return tokenizer . tokens ( ) - > stringifyList ( 0 , true ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void enum8 ( ) {
2011-03-06 21:07:06 +01:00
// ticket 1388
checkSimplifyEnum ( " enum Direction {N=100,E,S,W,ALL}; \n "
" template<class T,int S> class EF_Vector{ \n "
" T v_v[S]; \n "
" \n "
" public: \n "
" EF_Vector(); \n "
" explicit EF_Vector(const T &); \n "
" explicit EF_Vector(const T arr[S]); \n "
" }; \n "
" \n "
" template<class T,int S> \n "
" EF_Vector<T,S>::EF_Vector() \n "
" { \n "
" } \n "
" \n "
" template<class T,int S> \n "
" EF_Vector<T,S>::EF_Vector(const T &t) \n "
" { \n "
" for(int i=0;i<S;i++) \n "
" v_v[i]=t; \n "
" } \n "
" \n "
" template<class T,int S> \n "
" EF_Vector<T,S>::EF_Vector(const T arr[S]) \n "
" { \n "
" for(int i=0;i<S;i++) \n "
" v_v[i]=arr[i]; \n "
" } \n "
" \n "
" void initialize() \n "
" { \n "
" EF_Vector<float,6> d; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:1]: (style) Template parameter 'S' hides enumerator with same name \n "
" [test.cpp:11] -> [test.cpp:1]: (style) Template parameter 'S' hides enumerator with same name \n "
" [test.cpp:16] -> [test.cpp:1]: (style) Template parameter 'S' hides enumerator with same name \n "
" [test.cpp:23] -> [test.cpp:1]: (style) Template parameter 'S' hides enumerator with same name \n " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void enum9 ( ) {
2011-03-06 21:07:06 +01:00
// ticket 1404
checkSimplifyEnum ( " class XX { \n "
" public: \n "
" static void Set(const int &p){m_p=p;} \n "
" static int m_p; \n "
" }; \n "
" int XX::m_p=0; \n "
" int main() { \n "
" enum { XX }; \n "
" XX::Set(std::numeric_limits<X>::digits()); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void enum10 ( ) {
2011-03-06 21:07:06 +01:00
// ticket 1445
const char code [ ] = " enum { \n "
2012-12-06 19:19:22 +01:00
" SHELL_SIZE = sizeof(union { int i; char *cp; double d; }) - 1, \n "
2011-03-06 21:07:06 +01:00
" } e = SHELL_SIZE; " ;
2011-12-04 18:08:35 +01:00
const char expected [ ] = " int e ; e = sizeof ( union { int i ; char * cp ; double d ; } ) - 1 ; " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , checkSimplifyEnum ( code ) ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void enum11 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " int main() \n "
" { \n "
" enum { u, v }; \n "
" A u = 1, v = 2; \n "
" } " ;
const char expected [ ] = " int main ( ) "
" { "
2011-12-04 18:08:35 +01:00
" "
2011-03-06 21:07:06 +01:00
" A u ; u = 1 ; A v ; v = 2 ; "
" } " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , checkSimplifyEnum ( code ) ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " [test.cpp:4] -> [test.cpp:3]: (style) Variable 'u' hides enumerator with same name \n "
" [test.cpp:4] -> [test.cpp:3]: (style) Variable 'v' hides enumerator with same name \n " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void enum12 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " enum fred { a, b }; \n "
" void foo() \n "
" { \n "
" unsigned int fred = 0; \n "
" } " ;
2011-12-04 18:08:35 +01:00
const char expected [ ] = " void foo ( ) { unsigned int fred ; fred = 0 ; } " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , checkSimplifyEnum ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void enum13 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " enum ab { ENTRY(1, a = 0), ENTRY(2, b) }; \n "
" void foo() \n "
" { \n "
" unsigned int fred = a; \n "
" } " ;
2011-12-04 18:08:35 +01:00
const char expected [ ] = " void foo ( ) { unsigned int fred ; fred = a ; } " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , checkSimplifyEnum ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void enum14 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " enum ab { a }; \n "
" ab " ;
2011-12-04 18:08:35 +01:00
const char expected [ ] = " ab " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , checkSimplifyEnum ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void enum15 ( ) { // C++0x features
2011-03-06 21:07:06 +01:00
{
const char code [ ] = " enum : char { a = 99 }; \n "
" char c1 = a; " ;
2011-12-04 18:08:35 +01:00
const char expected [ ] = " char c1 ; c1 = 99 ; " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , checkSimplifyEnum ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
{
const char code [ ] = " enum class Enum1 { a }; \n "
" Enum1 e1 = a; " ;
2011-12-04 18:08:35 +01:00
const char expected [ ] = " int e1 ; e1 = 0 ; " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , checkSimplifyEnum ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
{
const char code [ ] = " enum Enum1 : char { a }; \n "
" Enum1 e1 = a; " ;
2011-12-04 18:08:35 +01:00
const char expected [ ] = " char e1 ; e1 = 0 ; " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , checkSimplifyEnum ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
{
const char code [ ] = " enum class Enum1 : unsigned char { a }; \n "
" Enum1 e1 = a; " ;
2011-12-04 18:08:35 +01:00
const char expected [ ] = " unsigned char e1 ; e1 = 0 ; " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , checkSimplifyEnum ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
{
const char code [ ] = " enum class Enum1 : unsigned int { a }; \n "
" Enum1 e1 = a; " ;
2011-12-04 18:08:35 +01:00
const char expected [ ] = " unsigned int e1 ; e1 = 0 ; " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , checkSimplifyEnum ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
{
const char code [ ] = " enum class Enum1 : unsigned long long int { a }; \n "
" Enum1 e1 = a; " ;
2011-12-04 18:08:35 +01:00
const char expected [ ] = " unsigned long long e1 ; e1 = 0 ; " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , checkSimplifyEnum ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
}
2011-10-13 20:53:06 +02:00
void enum16 ( ) { // ticket #1988
2011-03-06 21:07:06 +01:00
const char code [ ] = " enum D : auto * { FF = 0 }; " ;
checkSimplifyEnum ( code ) ;
ASSERT_EQUALS ( " [test.cpp:1]: (error) syntax error \n " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void enum17 ( ) { // ticket #2381
2011-03-06 21:07:06 +01:00
// if header is included twice its enums will be duplicated
const char code [ ] = " enum ab { a=0, b }; "
" enum ab { a=0, b }; \n " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " ; " , checkSimplifyEnum ( code ) ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void enum18 ( ) { // ticket #2466 - array with same name as enum constant
2011-03-06 21:07:06 +01:00
const char code [ ] = " enum ab { a=0, b }; \n "
" void f() { a[0]; } \n " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " void f ( ) { a [ 0 ] ; } " , checkSimplifyEnum ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void enum19 ( ) { // ticket #2536
2011-03-06 21:07:06 +01:00
const char code [ ] = " enum class E1; \n "
" enum class E2 : int; \n " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " ; " , checkSimplifyEnum ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void enum20 ( ) { // ticket #2600 segmentation fault
2011-03-06 21:07:06 +01:00
const char code [ ] = " enum { const } \n " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " " , checkSimplifyEnum ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void enum21 ( ) { // ticket #2720 syntax error
2011-04-15 01:50:17 +02:00
const char code [ ] = " enum E2 : signed const short { }; \n " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " ; " , checkSimplifyEnum ( code ) ) ;
2011-04-15 01:50:17 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void enum22 ( ) { // ticket #2745
2011-04-24 04:08:03 +02:00
const char code [ ] = " enum en { x = 0 }; \n "
" void f() { \n "
" int x = 0; \n "
" g(x); \n "
" } \n "
" void f2(int &x) { \n "
" x+=1; \n "
" } \n " ;
checkSimplifyEnum ( code ) ;
2013-06-04 18:57:46 +02:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:1]: (style) Variable 'x' hides enumerator with same name \n "
" [test.cpp:6] -> [test.cpp:1]: (style) Function argument 'x' hides enumerator with same name \n " ,
errout . str ( ) ) ;
2013-06-04 21:18:20 +02:00
// avoid false positive: in other scope
const char code2 [ ] = " class C1 { enum en { x = 0 }; }; \n "
2013-06-05 22:19:37 +02:00
" class C2 { bool x; }; \n " ;
2013-06-04 21:18:20 +02:00
checkSimplifyEnum ( code2 ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// avoid false positive: inner if-scope
const char code3 [ ] = " enum en { x = 0 }; \n "
2013-06-05 22:19:37 +02:00
" void f() { if (aa) ; else if (bb==x) df; } \n " ;
2013-06-04 21:18:20 +02:00
checkSimplifyEnum ( code3 ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-04-24 04:08:03 +02:00
}
2011-10-13 20:53:06 +02:00
void enum23 ( ) { // ticket #2804
2011-05-27 17:17:39 +02:00
const char code [ ] = " enum Enumerator : std::uint8_t { ITEM1, ITEM2, ITEM3 }; \n "
" Enumerator e = ITEM3; \n " ;
2011-12-04 18:08:35 +01:00
const char expected [ ] = " std :: uint8_t e ; e = 2 ; " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , checkSimplifyEnum ( code ) ) ;
2011-05-27 17:17:39 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void enum24 ( ) { // ticket #2828
2011-06-09 02:14:52 +02:00
const char code [ ] = " enum EnumName { STYLE = 0x0001 }; \n "
" void f(long style) { \n "
" if (style & STYLE) { } \n "
" } \n " ;
2011-12-04 18:08:35 +01:00
const char expected [ ] = " void f ( long style ) { if ( style & 1 ) { } } " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( expected , checkSimplifyEnum ( code ) ) ;
2011-06-09 02:14:52 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void enum25 ( ) { // ticket #2966 (segmentation fault)
2011-08-06 00:07:18 +02:00
const char code [ ] = " enum x : \n " ;
2012-04-17 09:51:08 +02:00
checkSimplifyEnum ( code ) ;
2011-08-06 00:07:18 +02:00
ASSERT_EQUALS ( " [test.cpp:1]: (error) syntax error \n " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void enum26 ( ) { // ticket #2975 (segmentation fault)
2011-08-07 22:48:47 +02:00
const char code [ ] = " enum E {} e enum \n " ;
2012-04-17 09:51:08 +02:00
checkSimplifyEnum ( code ) ;
2011-08-07 22:48:47 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void enum27 ( ) { // ticket #3005 (segmentation fault)
2011-08-15 13:07:12 +02:00
const char code [ ] = " enum : x \n " ;
2012-04-17 09:51:08 +02:00
checkSimplifyEnum ( code ) ;
2011-08-15 13:07:12 +02:00
ASSERT_EQUALS ( " [test.cpp:1]: (error) syntax error \n " , errout . str ( ) ) ;
}
2011-10-22 13:43:53 +02:00
void enum28 ( ) {
const char code [ ] = " enum { x=0 }; \n "
2012-12-06 19:19:22 +01:00
" void f() { char x[4]; memset(x, 0, 4); \n "
2011-10-22 13:43:53 +02:00
" { x } }; \n "
" void g() { x; } " ;
2012-04-17 09:51:08 +02:00
ASSERT_EQUALS ( " void f ( ) { char x [ 4 ] ; memset ( x , 0 , 4 ) ; { x } } ; void g ( ) { 0 ; } " , checkSimplifyEnum ( code ) ) ;
2011-10-22 13:43:53 +02:00
}
2012-05-16 18:48:33 +02:00
void enum29 ( ) { // #3747 - bitwise or value
const char code [ ] = " enum { x=1, y=x|2 }; i = (3==y); " ;
2012-09-09 16:22:39 +02:00
ASSERT_EQUALS ( " i = 3 == 3 ; " , checkSimplifyEnum ( code ) ) ;
2012-05-16 18:48:33 +02:00
}
2012-05-29 13:20:51 +02:00
void enum30 ( ) { // #3852 - false positive
const char code [ ] = " class TestIf \n "
" { \n "
" public: \n "
" enum class Foo \n "
" { \n "
" one = 0, \n "
" two = 1 \n "
" }; \n "
" enum class Bar \n "
" { \n "
" one = 0, \n "
" two = 1 \n "
" }; \n "
" }; \n " ;
checkSimplifyEnum ( code ) ;
TODO_ASSERT_EQUALS ( " " , " [test.cpp:12] -> [test.cpp:7]: (style) Variable 'two' hides enumerator with same name \n " , errout . str ( ) ) ;
}
2012-09-08 12:42:24 +02:00
void enum31 ( ) { // #3934 - calculation in first item
const char code [ ] = " enum { x=2*32, y }; i = y; " ;
ASSERT_EQUALS ( " i = 65 ; " , checkSimplifyEnum ( code ) ) ;
}
void enum32 ( ) { // #3998 - wrong enum simplification => access violation
const char code [ ] = " enum { x=(32), y=x, z }; { a, z } " ;
2012-09-09 16:22:39 +02:00
ASSERT_EQUALS ( " { a , 33 } " , checkSimplifyEnum ( code ) ) ;
2012-09-08 12:42:24 +02:00
}
void enum33 ( ) { // #4015 - segmentation fault
const char code [ ] = " enum { A=SOME_VALUE, B=A }; " ;
ASSERT_EQUALS ( " ; " , checkSimplifyEnum ( code ) ) ;
}
2012-09-09 12:38:15 +02:00
void enum34 ( ) { // #4141 - division by zero
const char code [ ] = " enum { A=1/0 }; " ;
ASSERT_EQUALS ( " ; " , checkSimplifyEnum ( code ) ) ;
}
2012-09-21 16:17:20 +02:00
void enum35 ( ) { // #3953 - avoid simplification of type
ASSERT_EQUALS ( " void f ( A * a ) ; " , checkSimplifyEnum ( " enum { A }; void f(A * a) ; " ) ) ;
ASSERT_EQUALS ( " void f ( A * a ) { } " , checkSimplifyEnum ( " enum { A }; void f(A * a) { } " ) ) ;
}
2012-12-02 07:22:55 +01:00
void enum36 ( ) { // #4378
const char code [ ] = " struct X { enum Y { a, b }; X(Y) { Y y = (Y)1; } }; " ;
ASSERT_EQUALS ( " struct X { X ( int ) { int y ; y = ( int ) 1 ; } } ; " , checkSimplifyEnum ( code ) ) ;
}
2013-06-04 06:51:01 +02:00
void enum37 ( ) { // #4280 - shadow variables
const char code1 [ ] = " enum { a, b }; void f(int a) { return a + 1; } " ;
ASSERT_EQUALS ( " void f ( int a ) { return a + 1 ; } " , checkSimplifyEnum ( code1 ) ) ;
const char code2 [ ] = " enum { a, b }; void f() { int a; } " ;
ASSERT_EQUALS ( " void f ( ) { int a ; } " , checkSimplifyEnum ( code2 ) ) ;
const char code3 [ ] = " enum { a, b }; void f() { int *a=do_something(); } " ;
ASSERT_EQUALS ( " void f ( ) { int * a ; a = do_something ( ) ; } " , checkSimplifyEnum ( code3 ) ) ;
const char code4 [ ] = " enum { a, b }; void f() { int &a=x; } " ;
ASSERT_EQUALS ( " void f ( ) { int & a = x ; } " , checkSimplifyEnum ( code4 ) ) ;
2013-06-16 08:13:41 +02:00
// #4857 - not shadow variable
checkSimplifyEnum ( " enum { a,b }; void f() { if (x) { } else if ( x & a ) {} } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-06-03 19:18:51 +02:00
}
2013-06-12 06:45:26 +02:00
void enum38 ( ) { // #4463
const char code [ ] = " enum { a,b }; void f() { throw a; } " ;
checkSimplifyEnum ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2012-09-08 12:42:24 +02:00
void enumscope1 ( ) { // #3949 - don't simplify enum from one function in another function
const char code [ ] = " void foo() { enum { A = 0, B = 1 }; } \n "
" void bar() { int a = A; } " ;
ASSERT_EQUALS ( " void foo ( ) { } void bar ( ) { int a ; a = A ; } " , checkSimplifyEnum ( code ) ) ;
}
2012-08-06 16:21:21 +02:00
2012-09-19 19:19:13 +02:00
void duplicateDefinition ( ) { // #3565 - wrongly detects duplicate definition
const Settings settings ;
2013-03-12 15:42:00 +01:00
Tokenizer tokenizer ( & settings , this ) ;
2012-09-19 19:19:13 +02:00
std : : istringstream istr ( " x ; return a not_eq x; " ) ;
tokenizer . tokenize ( istr , " test.c " ) ;
2012-09-29 14:10:41 +02:00
Token * x_token = tokenizer . list . front ( ) - > tokAt ( 5 ) ;
2012-09-21 16:17:20 +02:00
ASSERT_EQUALS ( false , tokenizer . duplicateDefinition ( & x_token , tokenizer . tokens ( ) ) ) ;
2012-09-19 19:19:13 +02:00
}
2011-10-13 20:53:06 +02:00
void removestd ( ) {
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " ; strcpy ( a , b ) ; " , tok ( " ; std::strcpy(a,b); " ) ) ;
ASSERT_EQUALS ( " ; strcat ( a , b ) ; " , tok ( " ; std::strcat(a,b); " ) ) ;
ASSERT_EQUALS ( " ; strncpy ( a , b , 10 ) ; " , tok ( " ; std::strncpy(a,b,10); " ) ) ;
ASSERT_EQUALS ( " ; strncat ( a , b , 10 ) ; " , tok ( " ; std::strncat(a,b,10); " ) ) ;
ASSERT_EQUALS ( " ; free ( p ) ; " , tok ( " ; std::free(p); " ) ) ;
ASSERT_EQUALS ( " ; malloc ( 10 ) ; " , tok ( " ; std::malloc(10); " ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyInitVar ( ) {
2011-03-06 21:07:06 +01:00
// ticket #1005 - int *p(0); => int *p = 0;
{
const char code [ ] = " void foo() { int *p(0); } " ;
2011-12-02 23:01:55 +01:00
ASSERT_EQUALS ( " void foo ( ) { } " , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
{
const char code [ ] = " void foo() { int p(0); } " ;
2011-12-02 23:01:55 +01:00
ASSERT_EQUALS ( " void foo ( ) { } " , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
{
const char code [ ] = " void a() { foo *p(0); } " ;
2011-12-02 23:01:55 +01:00
ASSERT_EQUALS ( " void a ( ) { } " , tok ( code ) ) ;
2011-03-06 21:07:06 +01:00
}
}
2011-10-13 20:53:06 +02:00
void simplifyReference ( ) {
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " void f ( ) { int a ; a ++ ; } " ,
tok ( " void f() { int a; int &b(a); b++; } " ) ) ;
ASSERT_EQUALS ( " void f ( ) { int a ; a ++ ; } " ,
tok ( " void f() { int a; int &b = a; b++; } " ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyRealloc ( ) {
2012-01-27 13:56:06 +01:00
ASSERT_EQUALS ( " ; free ( p ) ; p = 0 ; " , tok ( " ; p = realloc(p, 0); " ) ) ;
ASSERT_EQUALS ( " ; p = malloc ( 100 ) ; " , tok ( " ; p = realloc(0, 100); " ) ) ;
ASSERT_EQUALS ( " ; p = malloc ( 0 ) ; " , tok ( " ; p = realloc(0, 0); " ) ) ;
ASSERT_EQUALS ( " ; free ( q ) ; p = 0 ; " , tok ( " ; p = realloc(q, 0); " ) ) ;
ASSERT_EQUALS ( " ; free ( * q ) ; p = 0 ; " , tok ( " ; p = realloc(*q, 0); " ) ) ;
ASSERT_EQUALS ( " ; free ( f ( z ) ) ; p = 0 ; " , tok ( " ; p = realloc(f(z), 0); " ) ) ;
ASSERT_EQUALS ( " ; p = malloc ( n * m ) ; " , tok ( " ; p = realloc(0, n*m); " ) ) ;
ASSERT_EQUALS ( " ; p = malloc ( f ( 1 ) ) ; " , tok ( " ; p = realloc(0, f(1)); " ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyErrNoInWhile ( ) {
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " ; while ( f ( ) ) { } " ,
tok ( " ; while (f() && errno == EINTR) { } " ) ) ;
ASSERT_EQUALS ( " ; while ( f ( ) ) { } " ,
tok ( " ; while (f() && (errno == EINTR)) { } " ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyFuncInWhile ( ) {
2012-11-26 02:19:35 +01:00
ASSERT_EQUALS ( " int cppcheck:r1 = fclose ( f ) ; "
" while ( cppcheck:r1 ) "
2011-03-06 21:07:06 +01:00
" { "
" foo ( ) ; "
2012-11-26 02:19:35 +01:00
" cppcheck:r1 = fclose ( f ) ; "
2011-03-06 21:07:06 +01:00
" } " ,
tok ( " while(fclose(f))foo(); " ) ) ;
2012-11-26 02:19:35 +01:00
ASSERT_EQUALS ( " int cppcheck:r1 = fclose ( f ) ; "
" while ( cppcheck:r1 ) "
2011-03-06 21:07:06 +01:00
" { "
2012-11-26 02:19:35 +01:00
" ; cppcheck:r1 = fclose ( f ) ; "
2011-03-06 21:07:06 +01:00
" } " ,
tok ( " while(fclose(f)); " ) ) ;
2012-11-26 02:19:35 +01:00
ASSERT_EQUALS ( " int cppcheck:r1 = fclose ( f ) ; "
" while ( cppcheck:r1 ) "
2011-03-06 21:07:06 +01:00
" { "
2012-11-26 02:19:35 +01:00
" ; cppcheck:r1 = fclose ( f ) ; "
2011-03-06 21:07:06 +01:00
" } "
2012-11-26 02:19:35 +01:00
" int cppcheck:r2 = fclose ( g ) ; "
" while ( cppcheck:r2 ) "
2011-03-06 21:07:06 +01:00
" { "
2012-11-26 02:19:35 +01:00
" ; cppcheck:r2 = fclose ( g ) ; "
2011-03-06 21:07:06 +01:00
" } " ,
tok ( " while(fclose(f)); while(fclose(g)); " ) ) ;
}
2011-10-13 20:53:06 +02:00
void initstruct ( ) {
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " ; struct A a ; a . buf = 3 ; " , tok ( " ; struct A a = { .buf = 3 }; " ) ) ;
ASSERT_EQUALS ( " ; struct A a ; a . buf = x ; " , tok ( " ; struct A a = { .buf = x }; " ) ) ;
ASSERT_EQUALS ( " ; struct A a ; a . buf = & key ; " , tok ( " ; struct A a = { .buf = &key }; " ) ) ;
ASSERT_EQUALS ( " ; struct ABC abc ; abc . a = 3 ; abc . b = x ; abc . c = & key ; " , tok ( " ; struct ABC abc = { .a = 3, .b = x, .c = &key }; " ) ) ;
TODO_ASSERT_EQUALS ( " ; struct A a ; a . buf = { 0 } ; " ,
" ; struct A a ; a = { . buf = { 0 } } ; " ,
tok ( " ; struct A a = { .buf = {0} }; " ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyStructDecl1 ( ) {
2011-03-06 21:07:06 +01:00
{
const char code [ ] = " struct ABC { } abc; " ;
2013-01-07 19:20:15 +01:00
const char expected [ ] = " struct ABC { } ; struct ABC abc ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " struct ABC { } * pabc; " ;
2013-01-07 19:20:15 +01:00
const char expected [ ] = " struct ABC { } ; struct ABC * pabc ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " struct ABC { } abc[4]; " ;
2013-01-07 19:20:15 +01:00
const char expected [ ] = " struct ABC { } ; struct ABC abc [ 4 ] ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " struct ABC { } abc, def; " ;
2013-01-07 19:20:15 +01:00
const char expected [ ] = " struct ABC { } ; struct ABC abc ; struct ABC def ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " struct ABC { } abc, * pabc; " ;
2013-01-07 19:20:15 +01:00
const char expected [ ] = " struct ABC { } ; struct ABC abc ; struct ABC * pabc ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " struct ABC { struct DEF {} def; } abc; " ;
2013-01-07 19:20:15 +01:00
const char expected [ ] = " struct ABC { struct DEF { } ; struct DEF def ; } ; struct ABC abc ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " struct { } abc; " ;
2013-01-10 18:24:01 +01:00
const char expected [ ] = " struct Anonymous0 { } ; struct Anonymous0 abc ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " struct { } * pabc; " ;
2013-01-10 18:24:01 +01:00
const char expected [ ] = " struct Anonymous0 { } ; struct Anonymous0 * pabc ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " struct { } abc[4]; " ;
2013-01-10 18:24:01 +01:00
const char expected [ ] = " struct Anonymous0 { } ; struct Anonymous0 abc [ 4 ] ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " struct { } abc, def; " ;
2013-01-10 18:24:01 +01:00
const char expected [ ] = " struct Anonymous0 { } ; struct Anonymous0 abc ; struct Anonymous0 def ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " struct { } abc, * pabc; " ;
2013-01-10 18:24:01 +01:00
const char expected [ ] = " struct Anonymous0 { } ; struct Anonymous0 abc ; struct Anonymous0 * pabc ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " struct { struct DEF {} def; } abc; " ;
2013-01-10 18:24:01 +01:00
const char expected [ ] = " struct Anonymous0 { struct DEF { } ; struct DEF def ; } ; struct Anonymous0 abc ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " struct ABC { struct {} def; } abc; " ;
2013-01-10 18:24:01 +01:00
const char expected [ ] = " struct ABC { struct Anonymous0 { } ; struct Anonymous0 def ; } ; struct ABC abc ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " struct { struct {} def; } abc; " ;
2013-01-10 18:24:01 +01:00
const char expected [ ] = " struct Anonymous0 { struct Anonymous1 { } ; struct Anonymous1 def ; } ; struct Anonymous0 abc ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " union ABC { int i; float f; } abc; " ;
2013-01-07 19:20:15 +01:00
const char expected [ ] = " union ABC { int i ; float f ; } ; union ABC abc ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " struct ABC { struct {} def; }; " ;
2013-01-10 18:24:01 +01:00
const char expected [ ] = " struct ABC { struct Anonymous0 { } ; struct Anonymous0 def ; } ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " struct ABC : public XYZ { struct {} def; }; " ;
2013-01-10 18:24:01 +01:00
const char expected [ ] = " struct ABC : public XYZ { struct Anonymous0 { } ; struct Anonymous0 def ; } ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " struct { int x; }; int y; " ;
const char expected [ ] = " int x ; int y ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " struct { int x; }; " ;
const char expected [ ] = " int x ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " struct { }; " ;
const char expected [ ] = " ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " struct { struct { struct { } ; } ; }; " ;
const char expected [ ] = " ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
// ticket 2464
{
const char code [ ] = " static struct ABC { } abc ; " ;
2013-01-07 19:20:15 +01:00
const char expected [ ] = " struct ABC { } ; static struct ABC abc ; " ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
// ticket #980
{
const char code [ ] = " void f() { int A(1),B(2),C=3,D,E(5),F=6; } " ;
const char expected [ ] = " void f ( ) { int A ; A = 1 ; int B ; B = 2 ; int C ; C = 3 ; int D ; int E ; E = 5 ; int F ; F = 6 ; } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
}
2011-10-13 20:53:06 +02:00
void simplifyStructDecl2 ( ) { // ticket #2479 (segmentation fault)
2011-03-06 21:07:06 +01:00
const char code [ ] = " struct { char c; } " ;
const char expected [ ] = " struct { char c ; } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyStructDecl3 ( ) {
2011-07-02 15:21:30 +02:00
{
const char code [ ] = " class ABC { } abc; " ;
const char expected [ ] = " class ABC { } ; ABC abc ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " class ABC { } * pabc; " ;
const char expected [ ] = " class ABC { } ; ABC * pabc ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " class ABC { } abc[4]; " ;
const char expected [ ] = " class ABC { } ; ABC abc [ 4 ] ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " class ABC { } abc, def; " ;
const char expected [ ] = " class ABC { } ; ABC abc ; ABC def ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " class ABC { } abc, * pabc; " ;
const char expected [ ] = " class ABC { } ; ABC abc ; ABC * pabc ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " class ABC { class DEF {} def; } abc; " ;
const char expected [ ] = " class ABC { class DEF { } ; DEF def ; } ; ABC abc ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " class { } abc; " ;
const char expected [ ] = " class { } abc ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " class { } * pabc; " ;
const char expected [ ] = " class { } * pabc ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " class { } abc[4]; " ;
const char expected [ ] = " class { } abc [ 4 ] ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " class { } abc, def; " ;
const char expected [ ] = " class { } abc , def ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " class { } abc, * pabc; " ;
const char expected [ ] = " class { } abc , * pabc ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " struct { class DEF {} def; } abc; " ;
2013-01-10 18:24:01 +01:00
const char expected [ ] = " struct Anonymous0 { class DEF { } ; DEF def ; } ; struct Anonymous0 abc ; " ;
2011-07-02 15:21:30 +02:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " class ABC { struct {} def; } abc; " ;
2013-01-10 18:24:01 +01:00
const char expected [ ] = " class ABC { struct Anonymous0 { } ; struct Anonymous0 def ; } ; ABC abc ; " ;
2011-07-02 15:21:30 +02:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " class { class {} def; } abc; " ;
const char expected [ ] = " class { class { } def ; } abc ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " class ABC { struct {} def; }; " ;
2013-01-10 18:24:01 +01:00
const char expected [ ] = " class ABC { struct Anonymous0 { } ; struct Anonymous0 def ; } ; " ;
2011-07-02 15:21:30 +02:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " class ABC : public XYZ { struct {} def; }; " ;
2013-01-10 18:24:01 +01:00
const char expected [ ] = " class ABC : public XYZ { struct Anonymous0 { } ; struct Anonymous0 def ; } ; " ;
2011-07-02 15:21:30 +02:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " class { int x; }; int y; " ;
const char expected [ ] = " class { int x ; } ; int y ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " class { int x; }; " ;
const char expected [ ] = " class { int x ; } ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " class { }; " ;
const char expected [ ] = " class { } ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " class { struct { struct { } ; } ; }; " ;
const char expected [ ] = " class { } ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
}
2011-10-13 20:53:06 +02:00
void simplifyStructDecl4 ( ) {
2012-01-31 18:23:02 +01:00
const char code [ ] = " class ABC { \n "
" void foo() { \n "
" union { \n "
" int i; \n "
" float f; \n "
" }; \n "
" struct Fee { } fee; \n "
" } \n "
" union { \n "
" long long ll; \n "
" double d; \n "
" }; \n "
" } abc; \n " ;
const char expected [ ] = " class ABC { "
" void foo ( ) { "
" int i ; "
" float & f = i ; "
2013-01-07 19:20:15 +01:00
" struct Fee { } ; struct Fee fee ; "
2012-01-31 18:23:02 +01:00
" } "
" union { "
" long long ll ; "
" double d ; "
" } ; "
" } ; ABC abc ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
void simplifyStructDecl5 ( ) {
const char code [ ] = " <class T> \n "
" { \n "
" struct { \n "
" typename D4:typename Base<T*> \n "
" }; \n "
" }; \n " ;
//don't crash
tok ( code , false ) ;
2011-07-03 14:14:51 +02:00
}
2012-09-08 10:51:31 +02:00
void simplifyStructDecl6 ( ) {
ASSERT_EQUALS ( " struct A { "
" char integers [ X ] ; "
2013-01-07 19:20:15 +01:00
" } ; struct A arrays ; arrays = { { 0 } } ; " ,
2012-09-08 10:51:31 +02:00
tok ( " struct A { \n "
" char integers[X]; \n "
" } arrays = {{0}}; " , false ) ) ;
}
2013-01-10 18:24:01 +01:00
void simplifyStructDecl7 ( ) {
ASSERT_EQUALS ( " struct Anonymous0 { char x ; } ; struct Anonymous0 a [ 2 ] ; " ,
tok ( " struct { char x; } a[2]; " , false ) ) ;
ASSERT_EQUALS ( " struct Anonymous0 { char x ; } ; static struct Anonymous0 a [ 2 ] ; " ,
tok ( " static struct { char x; } a[2]; " , false ) ) ;
}
2011-10-13 20:53:06 +02:00
void removeUnwantedKeywords ( ) {
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " int var ; " , tok ( " register int var ; " , true ) ) ;
ASSERT_EQUALS ( " short var ; " , tok ( " register short int var ; " , true ) ) ;
ASSERT_EQUALS ( " int foo ( ) { } " , tok ( " inline int foo ( ) { } " , true ) ) ;
ASSERT_EQUALS ( " int foo ( ) { } " , tok ( " __inline int foo ( ) { } " , true ) ) ;
ASSERT_EQUALS ( " int foo ( ) { } " , tok ( " __forceinline int foo ( ) { } " , true ) ) ;
2012-11-09 19:53:10 +01:00
ASSERT_EQUALS ( " int foo ( ) { } " , tok ( " constexpr int foo() { } " , true ) ) ;
2012-11-11 13:32:19 +01:00
ASSERT_EQUALS ( " class C { int f ( ) ; } ; " , tok ( " class C { int f() override ; }; " , true ) ) ;
ASSERT_EQUALS ( " class C { int f ( ) ; } ; " , tok ( " class C { int f() final ; }; " , true ) ) ;
2013-02-10 23:54:15 +01:00
ASSERT_EQUALS ( " void f ( ) { int final [ 10 ] ; } " , tok ( " void f() { int final[10]; } " , true ) ) ;
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " if ( a ) { } " , tok ( " if ( likely ( a ) ) { } " , true ) ) ;
ASSERT_EQUALS ( " if ( a ) { } " , tok ( " if ( unlikely ( a ) ) { } " , true ) ) ;
2012-06-10 21:52:32 +02:00
ASSERT_EQUALS ( " int * p ; " , tok ( " int * __restrict p; " , " test.c " ) ) ;
ASSERT_EQUALS ( " int * * p ; " , tok ( " int * __restrict__ * p; " , " test.c " ) ) ;
ASSERT_EQUALS ( " void foo ( float * a , float * b ) ; " , tok ( " void foo(float * __restrict__ a, float * __restrict__ b); " , " test.c " ) ) ;
ASSERT_EQUALS ( " int * p ; " , tok ( " int * restrict p; " , " test.c " ) ) ;
ASSERT_EQUALS ( " int * * p ; " , tok ( " int * restrict * p; " , " test.c " ) ) ;
ASSERT_EQUALS ( " void foo ( float * a , float * b ) ; " , tok ( " void foo(float * restrict a, float * restrict b); " , " test.c " ) ) ;
ASSERT_EQUALS ( " int * p ; " , tok ( " typedef int * __restrict__ rint; rint p; " , " test.c " ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void simplifyCallingConvention ( ) {
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " int f ( ) ; " , tok ( " int __cdecl f(); " , true ) ) ;
ASSERT_EQUALS ( " int f ( ) ; " , tok ( " int __stdcall f(); " , true ) ) ;
ASSERT_EQUALS ( " int f ( ) ; " , tok ( " int __fastcall f(); " , true ) ) ;
ASSERT_EQUALS ( " int f ( ) ; " , tok ( " int __clrcall f(); " , true ) ) ;
ASSERT_EQUALS ( " int f ( ) ; " , tok ( " int __thiscall f(); " , true ) ) ;
ASSERT_EQUALS ( " int f ( ) ; " , tok ( " int __syscall f(); " , true ) ) ;
ASSERT_EQUALS ( " int f ( ) ; " , tok ( " int __pascal f(); " , true ) ) ;
ASSERT_EQUALS ( " int f ( ) ; " , tok ( " int __fortran f(); " , true ) ) ;
ASSERT_EQUALS ( " int f ( ) ; " , tok ( " int __far __cdecl f(); " , true ) ) ;
ASSERT_EQUALS ( " int f ( ) ; " , tok ( " int __far __stdcall f(); " , true ) ) ;
ASSERT_EQUALS ( " int f ( ) ; " , tok ( " int __far __fastcall f(); " , true ) ) ;
ASSERT_EQUALS ( " int f ( ) ; " , tok ( " int __far __clrcall f(); " , true ) ) ;
ASSERT_EQUALS ( " int f ( ) ; " , tok ( " int __far __thiscall f(); " , true ) ) ;
ASSERT_EQUALS ( " int f ( ) ; " , tok ( " int __far __syscall f(); " , true ) ) ;
ASSERT_EQUALS ( " int f ( ) ; " , tok ( " int __far __pascal f(); " , true ) ) ;
ASSERT_EQUALS ( " int f ( ) ; " , tok ( " int __far __fortran f(); " , true ) ) ;
ASSERT_EQUALS ( " int f ( ) ; " , tok ( " int WINAPI f(); " , true ) ) ;
ASSERT_EQUALS ( " int f ( ) ; " , tok ( " int APIENTRY f(); " , true ) ) ;
ASSERT_EQUALS ( " int f ( ) ; " , tok ( " int CALLBACK f(); " , true ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyFunctorCall ( ) {
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " IncrementFunctor ( ) ( a ) ; " , tok ( " IncrementFunctor()(a); " , true ) ) ;
}
2011-10-13 20:53:06 +02:00
void redundant_semicolon ( ) {
2011-03-06 21:07:06 +01:00
ASSERT_EQUALS ( " void f ( ) { ; } " , tok ( " void f() { ; } " , false ) ) ;
ASSERT_EQUALS ( " void f ( ) { ; } " , tok ( " void f() { do { ; } while (0); } " , true ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyFunctionReturn ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " typedef void (*testfp)(); \n "
" struct Fred \n "
" { \n "
" testfp get1() { return 0; } \n "
" void ( * get2 ( ) ) ( ) { return 0 ; } \n "
" testfp get3(); \n "
" void ( * get4 ( ) ) ( ); \n "
" }; " ;
2011-12-04 17:15:53 +01:00
const char expected [ ] = " struct Fred "
2011-03-06 21:07:06 +01:00
" { "
" void ( * get1 ( ) ) ( ) { return 0 ; } "
" void ( * get2 ( ) ) ( ) { return 0 ; } "
" void ( * get3 ( ) ) ( ) ; "
" void ( * get4 ( ) ) ( ) ; "
" } ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2011-10-27 20:54:42 +02:00
void removeVoidFromFunction ( ) {
ASSERT_EQUALS ( " void foo ( ) ; " , tok ( " void foo(void); " ) ) ;
}
2011-10-13 20:53:06 +02:00
void removeUnnecessaryQualification1 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " class Fred { Fred::Fred() {} }; " ;
const char expected [ ] = " class Fred { Fred ( ) { } } ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
2012-04-25 15:09:46 +02:00
ASSERT_EQUALS ( " [test.cpp:1]: (portability) The extra qualification 'Fred::' is unnecessary and is considered an error by many compilers. \n " , errout . str ( ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-10-13 20:53:06 +02:00
void removeUnnecessaryQualification2 ( ) {
2011-03-06 21:07:06 +01:00
const char code [ ] = " template<typename Iter, typename Skip> \n "
" struct grammar : qi::grammar<Iter, int(), Skip> { \n "
" grammar() : grammar::base_type(start) { } \n "
" }; \n " ;
tok ( code , false ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void removeUnnecessaryQualification3 ( ) {
2011-06-14 21:31:52 +02:00
const char code [ ] = " namespace one { \n "
" class c { \n "
" public: \n "
" void function() {} \n "
" }; \n "
" } \n "
" namespace two { \n "
" class c : public one::c { \n "
" public: \n "
" void function() { \n "
" one::c::function(); \n "
" } \n "
" }; \n "
" } \n " ;
tok ( code , false ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void removeUnnecessaryQualification4 ( ) {
2011-06-14 21:31:52 +02:00
const char code [ ] = " namespace one { \n "
" class c { \n "
" public: \n "
" void function() {} \n "
" }; \n "
" } \n "
" class c : public one::c { \n "
" public: \n "
" void function() { \n "
" one::c::function(); \n "
" } \n "
" }; \n " ;
tok ( code , false ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void removeUnnecessaryQualification5 ( ) {
2011-06-14 21:31:52 +02:00
const char code [ ] = " namespace one { \n "
" class c { \n "
" public: \n "
" void function() {} \n "
" }; \n "
" } \n "
" namespace two { \n "
" class c : public one::c { \n "
" public: \n "
" void function() { \n "
" two::c::function(); \n "
" } \n "
" }; \n "
" } \n " ;
tok ( code , false ) ;
2012-04-25 15:09:46 +02:00
ASSERT_EQUALS ( " [test.cpp:11]: (portability) The extra qualification 'two::c::' is unnecessary and is considered an error by many compilers. \n " , errout . str ( ) ) ;
2011-06-14 21:31:52 +02:00
}
2011-10-13 20:53:06 +02:00
void removeUnnecessaryQualification6 ( ) {
2011-06-27 03:13:29 +02:00
const char code [ ] = " namespace NS { \n "
" int HRDF_bit() { return 1; } \n "
" void HRDF_bit_set() { } \n "
" void func(int var) { \n "
" if (!NS::HRDF_bit()) \n "
" return; \n "
" else \n "
" NS::HRDF_bit_set(); \n "
" } \n "
" } \n " ;
tok ( code , false ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void removeUnnecessaryQualification7 ( ) { // ticket #2970
2011-08-06 23:54:02 +02:00
const char code [ ] = " class TProcedure { \n "
" public: \n "
" TProcedure::TProcedure(long endAddress) : m_lEndAddr(endAddress){} \n "
" private: \n "
" long m_lEndAddr; \n "
2011-09-24 01:45:19 +02:00
" }; \n " ;
2011-08-06 23:54:02 +02:00
tok ( code , false ) ;
2012-04-25 15:09:46 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (portability) The extra qualification 'TProcedure::' is unnecessary and is considered an error by many compilers. \n " , errout . str ( ) ) ;
2011-08-06 23:54:02 +02:00
}
2011-10-13 20:53:06 +02:00
void removeUnnecessaryQualification8 ( ) {
2011-09-24 01:45:19 +02:00
const char code [ ] = " class Fred { \n "
" public: \n "
" Fred & Fred::operator = (const Fred &); \n "
" void Fred::operator () (void); \n "
" void Fred::operator delete[](void* x); \n "
" }; \n " ;
tok ( code , false ) ;
2012-04-25 15:09:46 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (portability) The extra qualification 'Fred::' is unnecessary and is considered an error by many compilers. \n "
" [test.cpp:4]: (portability) The extra qualification 'Fred::' is unnecessary and is considered an error by many compilers. \n "
" [test.cpp:5]: (portability) The extra qualification 'Fred::' is unnecessary and is considered an error by many compilers. \n " , errout . str ( ) ) ;
2011-09-24 01:45:19 +02:00
}
2011-10-13 20:53:06 +02:00
void removeUnnecessaryQualification9 ( ) {
2011-09-30 01:25:14 +02:00
const char code [ ] = " class Fred { \n "
" public: \n "
" Fred::~Fred(); \n "
" }; \n " ;
tok ( code , false ) ;
2012-04-25 15:09:46 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (portability) The extra qualification 'Fred::' is unnecessary and is considered an error by many compilers. \n " , errout . str ( ) ) ;
2011-09-30 01:25:14 +02:00
}
2011-11-08 17:37:24 +01:00
void removeUnnecessaryQualification10 ( ) {
const char code [ ] = " template<typename T> class A \n "
" { \n "
" operator T(); \n "
" A() { T (A::*f)() = &A::operator T; } \n "
" }; \n " ;
tok ( code , false ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void simplifyIfNotNull ( ) {
2011-04-16 12:07:56 +02:00
{
// ticket # 2601 segmentation fault
const char code [ ] = " || #if #define <= " ;
tok ( code , false ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " void f(int x) { \n "
" x = (x != 0); \n "
" } " ;
ASSERT_EQUALS ( " void f ( int x ) { } " , tok ( code , false ) ) ;
}
2011-06-10 21:11:45 +02:00
}
2011-04-16 12:07:56 +02:00
2011-10-13 20:53:06 +02:00
void simplifyVarDecl1 ( ) { // ticket # 2682 segmentation fault
2011-06-10 21:11:45 +02:00
const char code [ ] = " x a[0] = " ;
tok ( code , false ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-03-06 21:07:06 +01:00
}
2011-06-12 14:24:49 +02:00
2011-10-13 20:53:06 +02:00
void simplifyVarDecl2 ( ) { // ticket # 2834 segmentation fault
2011-06-12 14:24:49 +02:00
const char code [ ] = " std::vector<int>::iterator " ;
tok ( code , false ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-06-26 22:53:16 +02:00
2011-10-13 20:53:06 +02:00
void return_strncat ( ) {
2012-01-26 16:09:32 +01:00
{
const char code [ ] = " char *f() \n "
" { \n "
" char *temp=malloc(2); \n "
" strcpy(temp, \" \" ); \n "
" return (strncat(temp, \" a \" ,1)); \n "
" } " ;
ASSERT_EQUALS ( " char * f ( ) { "
" char * temp ; "
" temp = malloc ( 2 ) ; "
" strcpy ( temp , \" \" ) ; "
" strncat ( temp , \" a \" , 1 ) ; "
" return temp ; "
" } " , tok ( code , true ) ) ;
}
{
const char code [ ] = " char *f() \n "
" { \n "
" char **temp=malloc(8); \n "
" *temp = malloc(2); \n "
" strcpy(*temp, \" \" ); \n "
" return (strncat(*temp, \" a \" ,1)); \n "
" } " ;
ASSERT_EQUALS ( " char * f ( ) { "
" char * * temp ; "
" temp = malloc ( 8 ) ; "
" * temp = malloc ( 2 ) ; "
" strcpy ( * temp , \" \" ) ; "
" strncat ( * temp , \" a \" , 1 ) ; "
" return * temp ; "
" } " , tok ( code , true ) ) ;
}
{
const char code [ ] = " char *f() \n "
" { \n "
" char **temp=malloc(8); \n "
" *temp = malloc(2); \n "
" strcpy(*temp, \" \" ); \n "
" return (strncat(temp[0],foo(b),calc(c-d))); \n "
" } " ;
ASSERT_EQUALS ( " char * f ( ) { "
" char * * temp ; "
" temp = malloc ( 8 ) ; "
" * temp = malloc ( 2 ) ; "
" strcpy ( * temp , \" \" ) ; "
" strncat ( temp [ 0 ] , foo ( b ) , calc ( c - d ) ) ; "
" return temp [ 0 ] ; "
" } " , tok ( code , true ) ) ;
}
2011-06-26 22:53:16 +02:00
}
2011-09-03 20:45:48 +02:00
2011-10-13 20:53:06 +02:00
void removeRedundantFor ( ) { // ticket #3069
2011-09-03 20:45:48 +02:00
{
const char code [ ] = " void f() { "
" for(x=0;x<1;x++) { "
" y = 1; "
" } "
" } " ;
ASSERT_EQUALS ( " void f ( ) { { y = 1 ; } x = 1 ; } " , tok ( code , true ) ) ;
}
{
const char code [ ] = " void f() { "
" for(x=0;x<1;x++) { "
" y = 1 + x; "
" } "
" } " ;
ASSERT_EQUALS ( " void f ( ) { x = 0 ; { y = 1 + x ; } x = 1 ; } " , tok ( code , true ) ) ;
}
}
2011-11-12 12:01:31 +01:00
void consecutiveBraces ( ) {
2011-11-14 09:21:42 +01:00
ASSERT_EQUALS ( " void f ( ) { } " , tok ( " void f(){{}} " , true ) ) ;
ASSERT_EQUALS ( " void f ( ) { } " , tok ( " void f(){{{}}} " , true ) ) ;
2011-11-18 20:57:29 +01:00
ASSERT_EQUALS ( " void f ( ) { for ( ; ; ) { } } " , tok ( " void f () { for(;;){} } " , true ) ) ;
ASSERT_EQUALS ( " void f ( ) { { scope_lock lock ; foo ( ) ; } { scope_lock lock ; bar ( ) ; } } " , tok ( " void f () { {scope_lock lock; foo();} {scope_lock lock; bar();} } " , true ) ) ;
2011-11-12 12:01:31 +01:00
}
2012-01-26 17:07:10 +01:00
void undefinedSizeArray ( ) {
ASSERT_EQUALS ( " int * x ; " , tok ( " int x []; " ) ) ;
ASSERT_EQUALS ( " int * * x ; " , tok ( " int x [][]; " ) ) ;
ASSERT_EQUALS ( " int * * x ; " , tok ( " int * x []; " ) ) ;
ASSERT_EQUALS ( " int * * * x ; " , tok ( " int * x [][]; " ) ) ;
ASSERT_EQUALS ( " int * * * * x ; " , tok ( " int * * x [][]; " ) ) ;
ASSERT_EQUALS ( " void f ( int x [ ] , double y [ ] ) { } " , tok ( " void f(int x[], double y[]) { } " ) ) ;
}
2012-09-04 13:41:14 +02:00
2012-09-08 20:59:25 +02:00
void simplifyArrayAddress ( ) { // ticket #3304
2012-09-04 13:41:14 +02:00
const char code [ ] = " void foo() { \n "
" int a[10]; \n "
" memset(&a[4], 0, 20*sizeof(int)); \n "
" } " ;
ASSERT_EQUALS ( " void foo ( ) { "
" int a [ 10 ] ; "
" memset ( a + 4 , 0 , 80 ) ; "
" } " , tok ( code , true ) ) ;
2012-09-08 20:59:25 +02:00
// Don't crash
tok ( " int " , true ) ;
2012-09-04 13:41:14 +02:00
}
2013-01-13 20:52:38 +01:00
void simplifyCharAt ( ) { // ticket #4481
ASSERT_EQUALS ( " 'h' ; " , tok ( " \" hello \" [0] ; " ) ) ;
ASSERT_EQUALS ( " ' \n ' ; " , tok ( " \" \n \" [0] ; " ) ) ;
ASSERT_EQUALS ( " ' \\ 0' ; " , tok ( " \" hello \" [5] ; " ) ) ;
ASSERT_EQUALS ( " ' \\ 0' ; " , tok ( " \" \" [0] ; " ) ) ;
ASSERT_EQUALS ( " ' \\ 0' ; " , tok ( " \" \\ 0 \" [0] ; " ) ) ;
ASSERT_EQUALS ( " ' \\ n' ; " , tok ( " \" hello \\ nworld \" [5] ; " ) ) ;
ASSERT_EQUALS ( " 'w' ; " , tok ( " \" hello \n world \" [6] ; " ) ) ;
ASSERT_EQUALS ( " \" hello \" [ 7 ] ; " , tok ( " \" hello \" [7] ; " ) ) ;
ASSERT_EQUALS ( " \" hello \" [ -1 ] ; " , tok ( " \" hello \" [-1] ; " ) ) ;
}
2011-03-06 21:07:06 +01:00
} ;
REGISTER_TEST ( TestSimplifyTokens )