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
2015-11-18 20:04:50 +01:00
* Copyright ( C ) 2007 - 2015 Cppcheck team .
2008-12-18 22:28:57 +01:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2009-09-27 17:08:31 +02:00
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
2008-12-18 22:28:57 +01:00
*/
# include "testsuite.h"
2009-10-25 12:49:06 +01:00
# include "tokenize.h"
# include "token.h"
2010-07-26 22:05:17 +02:00
# include "settings.h"
2012-05-06 13:01:56 +02:00
# include "path.h"
2014-04-13 12:47:54 +02:00
# include "preprocessor.h" // usually tests here should not use preprocessor...
2010-07-26 22:05:17 +02:00
# include <cstring>
2008-12-18 22:28:57 +01:00
2014-09-24 13:23:44 +02:00
2011-10-13 20:53:06 +02:00
class TestTokenizer : public TestFixture {
2008-12-18 22:28:57 +01:00
public :
2014-11-20 14:20:09 +01:00
TestTokenizer ( ) : TestFixture ( " TestTokenizer " ) {
2013-08-07 16:27:37 +02:00
}
2008-12-18 22:28:57 +01:00
private :
2015-10-07 18:33:57 +02:00
Settings settings0 ;
Settings settings1 ;
Settings settings2 ;
2014-10-19 07:34:40 +02:00
Settings settings_windows ;
2008-12-18 22:28:57 +01:00
2014-11-20 14:20:09 +01:00
void run ( ) {
2014-10-19 07:34:40 +02:00
LOAD_LIB_2 ( settings_windows . library , " windows.cfg " ) ;
2009-11-12 18:53:26 +01:00
TEST_CASE ( tokenize1 ) ;
2009-11-22 09:06:39 +01:00
TEST_CASE ( tokenize2 ) ;
2009-12-12 19:58:19 +01:00
TEST_CASE ( tokenize3 ) ;
2010-01-29 19:34:43 +01:00
TEST_CASE ( tokenize4 ) ;
2010-04-04 08:01:05 +02:00
TEST_CASE ( tokenize5 ) ;
2010-09-05 07:53:43 +02:00
TEST_CASE ( tokenize6 ) ; // array access. replace "*(p+1)" => "p[1]"
2010-08-27 22:58:21 +02:00
TEST_CASE ( tokenize7 ) ;
2010-08-28 13:32:43 +02:00
TEST_CASE ( tokenize8 ) ;
2010-08-29 13:54:26 +02:00
TEST_CASE ( tokenize9 ) ;
2010-09-04 11:49:56 +02:00
TEST_CASE ( tokenize11 ) ;
2010-12-26 15:07:14 +01:00
TEST_CASE ( tokenize13 ) ; // bailout if the code contains "@" - that is not handled well.
2010-12-27 08:09:05 +01:00
TEST_CASE ( tokenize14 ) ; // tokenize "0X10" => 16
2011-01-09 10:09:54 +01:00
TEST_CASE ( tokenize15 ) ; // tokenize ".123"
2011-03-06 09:33:46 +01:00
TEST_CASE ( tokenize16 ) ; // #2612 - segfault for "<><<"
2011-04-29 15:19:22 +02:00
TEST_CASE ( tokenize17 ) ; // #2759
2011-07-15 19:37:39 +02:00
TEST_CASE ( tokenize18 ) ; // tokenize "(X&&Y)" into "( X && Y )" instead of "( X & & Y )"
2011-08-15 13:19:49 +02:00
TEST_CASE ( tokenize19 ) ; // #3006 (segmentation fault)
2011-09-03 23:10:16 +02:00
TEST_CASE ( tokenize20 ) ; // replace C99 _Bool => bool
2011-12-18 07:37:20 +01:00
TEST_CASE ( tokenize21 ) ; // tokenize 0x0E-7
2011-12-18 13:33:23 +01:00
TEST_CASE ( tokenize22 ) ; // special marker $ from preprocessor
2012-09-19 10:37:30 +02:00
TEST_CASE ( tokenize24 ) ; // #4195 (segmentation fault)
2012-09-27 06:35:36 +02:00
TEST_CASE ( tokenize25 ) ; // #4239 (segmentation fault)
2012-10-08 21:49:25 +02:00
TEST_CASE ( tokenize26 ) ; // #4245 (segmentation fault)
2013-02-19 21:56:13 +01:00
TEST_CASE ( tokenize27 ) ; // #4525 (segmentation fault)
2013-05-09 14:47:18 +02:00
TEST_CASE ( tokenize28 ) ; // #4725 (writing asm() around "^{}")
2014-04-12 10:36:01 +02:00
TEST_CASE ( tokenize31 ) ; // #3503 (Wrong handling of member function taking function pointer as argument)
2014-06-16 10:58:41 +02:00
TEST_CASE ( tokenize32 ) ; // #5884 (fsanitize=undefined: left shift of negative value -10000 in lib/templatesimplifier.cpp:852:46)
2014-08-26 09:12:10 +02:00
TEST_CASE ( tokenize33 ) ; // #5780 Various crashes on valid template code
2009-11-12 20:04:11 +01:00
2012-10-12 17:08:21 +02:00
TEST_CASE ( syntax_case_default ) ;
2013-04-18 16:13:58 +02:00
TEST_CASE ( simplifyFileAndLineMacro ) ; // tokenize "return - __LINE__;"
2012-09-03 20:23:53 +02:00
TEST_CASE ( foreach ) ; // #3690
2015-10-19 10:01:57 +02:00
TEST_CASE ( combineOperators ) ;
2012-11-28 07:09:56 +01:00
TEST_CASE ( concatenateNegativeNumber ) ;
2009-03-17 20:50:06 +01:00
2009-01-05 16:49:57 +01:00
TEST_CASE ( longtok ) ;
2008-12-18 22:28:57 +01:00
2009-02-04 20:40:48 +01:00
TEST_CASE ( removeCast1 ) ;
2009-05-27 20:06:19 +02:00
TEST_CASE ( removeCast2 ) ;
2009-11-14 11:59:05 +01:00
TEST_CASE ( removeCast3 ) ;
2009-11-17 19:03:23 +01:00
TEST_CASE ( removeCast4 ) ;
2010-07-05 13:16:33 +02:00
TEST_CASE ( removeCast5 ) ;
2011-11-25 07:23:54 +01:00
TEST_CASE ( removeCast7 ) ;
TEST_CASE ( removeCast8 ) ;
2012-02-18 14:26:00 +01:00
TEST_CASE ( removeCast9 ) ;
2012-02-27 18:55:36 +01:00
TEST_CASE ( removeCast10 ) ;
2012-06-19 20:07:39 +02:00
TEST_CASE ( removeCast11 ) ;
2012-10-07 18:38:05 +02:00
TEST_CASE ( removeCast12 ) ;
2013-09-03 06:27:11 +02:00
TEST_CASE ( removeCast13 ) ;
2013-11-28 16:04:47 +01:00
TEST_CASE ( removeCast14 ) ;
2014-07-19 18:29:53 +02:00
TEST_CASE ( removeCast15 ) ; // #5996 - don't remove cast in 'a+static_cast<int>(b?60:0)'
2014-11-18 19:23:03 +01:00
TEST_CASE ( removeCast16 ) ; // #6278
2015-03-02 16:27:00 +01:00
TEST_CASE ( removeCast17 ) ; // #6110 - don't remove any parentheses in 'a(b)(c)'
2009-02-04 20:40:48 +01:00
2009-01-05 16:49:57 +01:00
TEST_CASE ( inlineasm ) ;
2008-12-18 22:28:57 +01:00
2009-01-05 16:49:57 +01:00
TEST_CASE ( ifAddBraces1 ) ;
TEST_CASE ( ifAddBraces2 ) ;
TEST_CASE ( ifAddBraces3 ) ;
TEST_CASE ( ifAddBraces4 ) ;
2009-03-15 13:44:57 +01:00
TEST_CASE ( ifAddBraces5 ) ;
2009-05-09 22:12:14 +02:00
TEST_CASE ( ifAddBraces6 ) ;
2009-06-22 22:54:11 +02:00
TEST_CASE ( ifAddBraces7 ) ;
2009-11-20 20:38:30 +01:00
TEST_CASE ( ifAddBraces9 ) ;
2010-02-07 15:00:19 +01:00
TEST_CASE ( ifAddBraces10 ) ;
2010-02-20 11:43:53 +01:00
TEST_CASE ( ifAddBraces11 ) ;
2010-02-20 13:24:50 +01:00
TEST_CASE ( ifAddBraces12 ) ;
2010-06-26 17:15:44 +02:00
TEST_CASE ( ifAddBraces13 ) ;
2011-10-27 20:54:42 +02:00
TEST_CASE ( ifAddBraces14 ) ; // #2610 - segfault: if()<{}
TEST_CASE ( ifAddBraces15 ) ; // #2616 - unknown macro before if
2012-01-26 22:25:19 +01:00
TEST_CASE ( ifAddBraces16 ) ; // ticket # 2739 (segmentation fault)
TEST_CASE ( ifAddBraces17 ) ; // '} else' should be in the same line
TEST_CASE ( ifAddBraces18 ) ; // #3424 - if if { } else else
2013-01-24 16:53:20 +01:00
TEST_CASE ( ifAddBraces19 ) ; // #3928 - if for if else
2013-09-05 06:38:07 +02:00
TEST_CASE ( ifAddBraces20 ) ; // #5012 - syntax error 'else }'
2008-12-22 10:20:46 +01:00
2009-06-20 13:20:51 +02:00
TEST_CASE ( whileAddBraces ) ;
2009-08-22 12:42:19 +02:00
TEST_CASE ( doWhileAddBraces ) ;
2009-06-20 13:20:51 +02:00
2013-10-14 21:05:54 +02:00
TEST_CASE ( forAddBraces1 ) ;
TEST_CASE ( forAddBraces2 ) ; // #5088
2010-06-13 08:00:46 +02:00
2009-01-05 16:49:57 +01:00
TEST_CASE ( simplifyKnownVariables1 ) ;
TEST_CASE ( simplifyKnownVariables2 ) ;
TEST_CASE ( simplifyKnownVariables3 ) ;
TEST_CASE ( simplifyKnownVariables4 ) ;
TEST_CASE ( simplifyKnownVariables5 ) ;
2009-02-20 18:27:57 +01:00
TEST_CASE ( simplifyKnownVariables6 ) ;
2009-02-24 17:11:37 +01:00
TEST_CASE ( simplifyKnownVariables7 ) ;
2009-02-27 19:25:47 +01:00
TEST_CASE ( simplifyKnownVariables8 ) ;
2009-03-25 07:10:17 +01:00
TEST_CASE ( simplifyKnownVariables9 ) ;
2009-05-01 20:31:07 +02:00
TEST_CASE ( simplifyKnownVariables10 ) ;
2009-06-19 18:03:09 +02:00
TEST_CASE ( simplifyKnownVariables11 ) ;
2009-08-01 15:57:54 +02:00
TEST_CASE ( simplifyKnownVariables12 ) ;
2009-08-22 10:23:55 +02:00
TEST_CASE ( simplifyKnownVariables13 ) ;
2009-09-28 17:15:31 +02:00
TEST_CASE ( simplifyKnownVariables14 ) ;
2009-10-06 08:14:59 +02:00
TEST_CASE ( simplifyKnownVariables15 ) ;
2009-10-10 10:13:56 +02:00
TEST_CASE ( simplifyKnownVariables16 ) ;
2009-11-02 23:07:15 +01:00
TEST_CASE ( simplifyKnownVariables17 ) ;
2010-01-15 22:46:47 +01:00
TEST_CASE ( simplifyKnownVariables18 ) ;
2010-01-24 19:27:39 +01:00
TEST_CASE ( simplifyKnownVariables19 ) ;
2010-02-20 15:50:44 +01:00
TEST_CASE ( simplifyKnownVariables20 ) ;
2010-03-14 09:57:34 +01:00
TEST_CASE ( simplifyKnownVariables21 ) ;
2010-04-04 18:48:51 +02:00
TEST_CASE ( simplifyKnownVariables22 ) ;
2010-04-15 19:09:19 +02:00
TEST_CASE ( simplifyKnownVariables23 ) ;
2010-05-04 20:02:47 +02:00
TEST_CASE ( simplifyKnownVariables25 ) ;
2010-06-19 14:00:45 +02:00
TEST_CASE ( simplifyKnownVariables27 ) ;
2010-06-19 17:54:38 +02:00
TEST_CASE ( simplifyKnownVariables28 ) ;
2010-11-07 08:43:30 +01:00
TEST_CASE ( simplifyKnownVariables29 ) ; // ticket #1811
2010-10-10 19:27:42 +02:00
TEST_CASE ( simplifyKnownVariables30 ) ;
2010-11-06 19:42:38 +01:00
TEST_CASE ( simplifyKnownVariables31 ) ;
2010-11-07 08:43:30 +01:00
TEST_CASE ( simplifyKnownVariables32 ) ; // const
2010-11-13 18:45:35 +01:00
TEST_CASE ( simplifyKnownVariables33 ) ; // struct variable
2010-12-16 20:16:59 +01:00
TEST_CASE ( simplifyKnownVariables34 ) ;
2010-12-25 10:07:57 +01:00
TEST_CASE ( simplifyKnownVariables35 ) ; // ticket #2353 - False positive: Division by zero 'if (x == 0) return 0; return 10 / x;'
2010-12-26 20:34:07 +01:00
TEST_CASE ( simplifyKnownVariables36 ) ; // ticket #2304 - known value for strcpy parameter
2011-01-04 19:36:29 +01:00
TEST_CASE ( simplifyKnownVariables37 ) ; // ticket #2398 - false positive caused by no simplification in for loop
2011-01-04 21:35:35 +01:00
TEST_CASE ( simplifyKnownVariables38 ) ; // ticket #2399 - simplify conditions
2011-01-11 19:34:35 +01:00
TEST_CASE ( simplifyKnownVariables39 ) ;
2011-02-01 21:46:07 +01:00
TEST_CASE ( simplifyKnownVariables40 ) ;
2011-02-20 18:18:27 +01:00
TEST_CASE ( simplifyKnownVariables41 ) ; // p=&x; if (p) ..
2011-02-26 21:11:56 +01:00
TEST_CASE ( simplifyKnownVariables42 ) ; // ticket #2031 - known string value after strcpy
2011-08-30 19:13:04 +02:00
TEST_CASE ( simplifyKnownVariables43 ) ;
2011-11-05 08:30:11 +01:00
TEST_CASE ( simplifyKnownVariables44 ) ; // ticket #3117 - don't simplify static variables
2011-11-20 21:50:26 +01:00
TEST_CASE ( simplifyKnownVariables45 ) ; // ticket #3281 - static constant variable not simplified
2012-02-07 08:05:50 +01:00
TEST_CASE ( simplifyKnownVariables46 ) ; // ticket #3587 - >>
2012-03-15 19:09:36 +01:00
TEST_CASE ( simplifyKnownVariables47 ) ; // ticket #3627 - >>
2012-04-25 19:23:17 +02:00
TEST_CASE ( simplifyKnownVariables48 ) ; // ticket #3754 - wrong simplification in for loop header
2012-05-07 21:11:23 +02:00
TEST_CASE ( simplifyKnownVariables49 ) ; // #3691 - continue in switch
2012-08-22 16:51:44 +02:00
TEST_CASE ( simplifyKnownVariables50 ) ; // #4066 sprintf changes
2013-01-03 19:14:44 +01:00
TEST_CASE ( simplifyKnownVariables51 ) ; // #4409 hang
2013-06-26 21:47:03 +02:00
TEST_CASE ( simplifyKnownVariables52 ) ; // #4728 "= x %cop%"
2013-07-30 11:30:14 +02:00
TEST_CASE ( simplifyKnownVariables53 ) ; // references
2013-08-25 08:54:33 +02:00
TEST_CASE ( simplifyKnownVariables54 ) ; // #4913 'x' is not 0 after *--x=0;
2013-11-16 17:30:04 +01:00
TEST_CASE ( simplifyKnownVariables55 ) ; // pointer alias
2014-02-22 12:09:54 +01:00
TEST_CASE ( simplifyKnownVariables56 ) ; // ticket #5301 - >>
2014-04-10 19:22:14 +02:00
TEST_CASE ( simplifyKnownVariables57 ) ; // ticket #4724
2014-06-15 15:52:58 +02:00
TEST_CASE ( simplifyKnownVariables58 ) ; // ticket #5268
2014-08-17 07:39:42 +02:00
TEST_CASE ( simplifyKnownVariables59 ) ; // skip for header
2015-11-10 14:45:24 +01:00
TEST_CASE ( simplifyKnownVariables60 ) ; // #6829
2011-08-21 17:49:00 +02:00
TEST_CASE ( simplifyKnownVariablesBailOutAssign1 ) ;
TEST_CASE ( simplifyKnownVariablesBailOutAssign2 ) ;
2012-12-10 06:08:33 +01:00
TEST_CASE ( simplifyKnownVariablesBailOutAssign3 ) ; // #4395 - nested assignments
2010-11-07 10:42:08 +01:00
TEST_CASE ( simplifyKnownVariablesBailOutFor1 ) ;
TEST_CASE ( simplifyKnownVariablesBailOutFor2 ) ;
2010-11-13 16:40:04 +01:00
TEST_CASE ( simplifyKnownVariablesBailOutFor3 ) ;
2010-11-07 08:10:09 +01:00
TEST_CASE ( simplifyKnownVariablesBailOutMemberFunction ) ;
2010-11-21 13:40:42 +01:00
TEST_CASE ( simplifyKnownVariablesBailOutConditionalIncrement ) ;
2011-10-27 20:54:42 +02:00
TEST_CASE ( simplifyKnownVariablesBailOutSwitchBreak ) ; // ticket #2324
2011-01-15 08:48:42 +01:00
TEST_CASE ( simplifyKnownVariablesFloat ) ; // #2454 - float variable
2011-06-23 16:58:01 +02:00
TEST_CASE ( simplifyKnownVariablesClassMember ) ; // #2815 - value of class member may be changed by function call
2012-06-24 13:39:14 +02:00
TEST_CASE ( simplifyKnownVariablesFunctionCalls ) ; // Function calls (don't assume pass by reference)
2013-01-19 13:39:23 +01:00
TEST_CASE ( simplifyKnownVariablesReturn ) ; // 3500 - return
2012-04-16 16:25:04 +02:00
TEST_CASE ( simplifyExternC ) ;
2014-05-13 16:28:28 +02:00
TEST_CASE ( simplifyKeyword ) ; // #5842 - remove C99 static keyword between []
2008-12-18 22:28:57 +01:00
2009-01-19 19:51:27 +01:00
TEST_CASE ( file1 ) ;
2009-01-20 23:25:49 +01:00
TEST_CASE ( file2 ) ;
2009-02-15 12:42:04 +01:00
TEST_CASE ( file3 ) ;
2009-01-20 18:26:16 +01:00
TEST_CASE ( doublesharp ) ;
2009-01-24 08:56:47 +01:00
2013-10-01 20:30:59 +02:00
TEST_CASE ( isZeroNumber ) ;
TEST_CASE ( isOneNumber ) ;
2013-10-03 15:41:12 +02:00
TEST_CASE ( isTwoNumber ) ;
2013-10-01 20:30:59 +02:00
2009-02-13 14:33:12 +01:00
TEST_CASE ( macrodoublesharp ) ;
2012-01-23 16:10:15 +01:00
TEST_CASE ( simplifyFunctionParameters ) ;
2012-04-22 17:28:27 +02:00
TEST_CASE ( simplifyFunctionParameters1 ) ; // #3721
2012-12-23 08:04:44 +01:00
TEST_CASE ( simplifyFunctionParameters2 ) ; // #4430
2012-12-27 17:15:38 +01:00
TEST_CASE ( simplifyFunctionParameters3 ) ; // #4436
2012-01-23 16:10:15 +01:00
TEST_CASE ( simplifyFunctionParametersErrors ) ;
2009-01-25 20:39:05 +01:00
2011-03-30 16:45:31 +02:00
TEST_CASE ( removeParentheses1 ) ; // Ticket #61
TEST_CASE ( removeParentheses3 ) ;
TEST_CASE ( removeParentheses4 ) ; // Ticket #390
TEST_CASE ( removeParentheses5 ) ; // Ticket #392
TEST_CASE ( removeParentheses6 ) ;
TEST_CASE ( removeParentheses7 ) ;
TEST_CASE ( removeParentheses8 ) ; // Ticket #1865
TEST_CASE ( removeParentheses9 ) ; // Ticket #1962
TEST_CASE ( removeParentheses10 ) ; // Ticket #2320
TEST_CASE ( removeParentheses11 ) ; // Ticket #2505
2011-05-01 08:27:59 +02:00
TEST_CASE ( removeParentheses12 ) ; // Ticket #2760 ',(b)='
2011-11-07 23:40:06 +01:00
TEST_CASE ( removeParentheses13 ) ;
2011-11-08 22:48:14 +01:00
TEST_CASE ( removeParentheses14 ) ; // Ticket #3309
2012-09-06 16:16:29 +02:00
TEST_CASE ( removeParentheses15 ) ; // Ticket #4142
2013-02-16 16:07:05 +01:00
TEST_CASE ( removeParentheses16 ) ; // Ticket #4423 '*(x.y)='
2014-01-08 20:53:33 +01:00
TEST_CASE ( removeParentheses17 ) ; // Don't remove parentheses in 'a ? b : (c>0 ? d : e);'
2014-04-21 16:14:49 +02:00
TEST_CASE ( removeParentheses18 ) ; // 'float(*a)[2]' => 'float *a[2]'
2014-05-04 18:36:04 +02:00
TEST_CASE ( removeParentheses19 ) ; // ((typeof(x) *)0)
2014-05-11 08:22:28 +02:00
TEST_CASE ( removeParentheses20 ) ; // Ticket #5479: a<b<int>>(2);
2014-05-24 19:04:47 +02:00
TEST_CASE ( removeParentheses21 ) ; // Don't "simplify" casts
2014-06-28 09:36:51 +02:00
TEST_CASE ( removeParentheses22 ) ;
2014-09-05 00:31:58 +02:00
TEST_CASE ( removeParentheses23 ) ; // Ticket #6103 - Infinite loop upon valid input
2015-10-13 20:31:17 +02:00
TEST_CASE ( removeParentheses24 ) ; // Ticket #7040
2009-02-02 07:21:48 +01:00
2009-02-08 10:51:45 +01:00
TEST_CASE ( tokenize_double ) ;
2009-02-08 11:25:33 +01:00
TEST_CASE ( tokenize_strings ) ;
2009-02-12 20:26:42 +01:00
TEST_CASE ( simplify_constants ) ;
2009-02-14 22:33:28 +01:00
TEST_CASE ( simplify_constants2 ) ;
2011-01-29 16:14:09 +01:00
TEST_CASE ( simplify_constants3 ) ;
2012-10-17 00:25:20 +02:00
TEST_CASE ( simplify_constants4 ) ;
2013-06-25 06:37:51 +02:00
TEST_CASE ( simplify_constants5 ) ;
2014-10-04 20:49:57 +02:00
TEST_CASE ( simplify_constants6 ) ; // Ticket #5625: Ternary operator as template parameter
2015-01-31 20:12:02 +01:00
TEST_CASE ( simplifyNull ) ;
2011-12-09 20:47:51 +01:00
TEST_CASE ( simplifyMulAndParens ) ; // Ticket #2784 + #3184
2009-03-13 22:38:42 +01:00
2013-01-07 19:20:15 +01:00
TEST_CASE ( simplifyStructDecl ) ;
2009-03-18 20:32:05 +01:00
TEST_CASE ( vardecl1 ) ;
TEST_CASE ( vardecl2 ) ;
2009-06-14 14:57:47 +02:00
TEST_CASE ( vardecl3 ) ;
2009-06-14 19:32:34 +02:00
TEST_CASE ( vardecl4 ) ;
2015-10-15 10:31:08 +02:00
TEST_CASE ( vardecl5 ) ; // #7048
2009-10-02 16:28:30 +02:00
TEST_CASE ( vardec_static ) ;
2009-08-08 12:33:07 +02:00
TEST_CASE ( vardecl6 ) ;
2009-08-23 08:26:16 +02:00
TEST_CASE ( vardecl7 ) ;
2009-09-20 12:28:15 +02:00
TEST_CASE ( vardecl8 ) ;
2009-09-20 13:28:56 +02:00
TEST_CASE ( vardecl9 ) ;
2009-09-26 12:02:13 +02:00
TEST_CASE ( vardecl10 ) ;
2010-05-16 20:21:22 +02:00
TEST_CASE ( vardecl11 ) ;
2010-08-25 20:17:31 +02:00
TEST_CASE ( vardecl12 ) ;
2010-09-15 19:53:47 +02:00
TEST_CASE ( vardecl13 ) ;
2011-03-25 04:06:20 +01:00
TEST_CASE ( vardecl14 ) ;
2012-01-30 23:41:43 +01:00
TEST_CASE ( vardecl15 ) ;
TEST_CASE ( vardecl16 ) ;
TEST_CASE ( vardecl17 ) ;
2012-02-18 15:05:29 +01:00
TEST_CASE ( vardecl18 ) ;
2012-03-31 18:45:29 +02:00
TEST_CASE ( vardecl19 ) ;
2012-04-09 12:55:26 +02:00
TEST_CASE ( vardecl20 ) ; // #3700 - register const int H = 0;
2012-09-14 16:46:45 +02:00
TEST_CASE ( vardecl21 ) ; // #4042 - a::b const *p = 0;
2012-09-17 19:45:42 +02:00
TEST_CASE ( vardecl22 ) ; // #4211 - segmentation fault
2012-10-20 21:39:29 +02:00
TEST_CASE ( vardecl23 ) ; // #4276 - segmentation fault
2012-11-30 10:30:26 +01:00
TEST_CASE ( vardecl24 ) ; // #4187 - variable declaration within lambda function
2013-05-14 20:56:31 +02:00
TEST_CASE ( vardecl25 ) ; // #4799 - segmentation fault
2014-06-08 14:59:58 +02:00
TEST_CASE ( vardecl26 ) ; // #5907 - incorrect handling of extern declarations
2010-11-13 15:45:33 +01:00
TEST_CASE ( vardecl_stl_1 ) ;
TEST_CASE ( vardecl_stl_2 ) ;
2012-04-05 08:53:10 +02:00
TEST_CASE ( vardecl_template_1 ) ;
TEST_CASE ( vardecl_template_2 ) ;
2010-09-03 08:10:29 +02:00
TEST_CASE ( vardecl_union ) ;
2011-05-07 14:23:14 +02:00
TEST_CASE ( vardecl_par ) ; // #2743 - set links if variable type contains parentheses
2015-03-06 17:30:20 +01:00
TEST_CASE ( vardecl_par2 ) ; // #3912 - set correct links
TEST_CASE ( vardecl_par3 ) ; // #6556 - Fred x1(a), x2(b);
2009-04-20 20:38:05 +02:00
TEST_CASE ( volatile_variables ) ;
2009-05-27 20:49:29 +02:00
2009-06-06 10:40:48 +02:00
// unsigned i; => unsigned int i;
TEST_CASE ( unsigned1 ) ;
2009-11-17 19:03:23 +01:00
TEST_CASE ( unsigned2 ) ;
2010-05-07 18:37:50 +02:00
TEST_CASE ( unsigned3 ) ; // template arguments
2010-03-19 19:34:26 +01:00
2013-08-12 06:23:01 +02:00
TEST_CASE ( simplifyStdType ) ; // #4947, #4950, #4951
2013-08-09 23:13:04 +02:00
2009-09-20 22:13:06 +02:00
TEST_CASE ( createLinks ) ;
2009-09-28 20:25:05 +02:00
TEST_CASE ( signed1 ) ;
2009-10-01 19:45:48 +02:00
2009-10-09 21:11:29 +02:00
TEST_CASE ( simplifyString ) ;
2009-11-12 22:49:39 +01:00
TEST_CASE ( simplifyConst ) ;
2009-12-06 23:09:56 +01:00
TEST_CASE ( switchCase ) ;
2010-01-20 21:19:06 +01:00
2011-10-16 08:09:57 +02:00
TEST_CASE ( simplifyPointerToStandardType ) ;
2011-03-19 21:21:26 +01:00
TEST_CASE ( functionpointer1 ) ;
TEST_CASE ( functionpointer2 ) ;
2011-07-22 07:58:53 +02:00
TEST_CASE ( functionpointer3 ) ;
2011-11-09 21:02:41 +01:00
TEST_CASE ( functionpointer4 ) ;
2011-12-27 11:56:40 +01:00
TEST_CASE ( functionpointer5 ) ;
2014-04-12 16:06:31 +02:00
TEST_CASE ( functionpointer6 ) ;
2014-04-27 19:49:21 +02:00
TEST_CASE ( functionpointer7 ) ;
2010-01-31 09:33:57 +01:00
TEST_CASE ( removeRedundantAssignment ) ;
2010-02-03 20:01:56 +01:00
TEST_CASE ( removedeclspec ) ;
2010-05-27 18:15:42 +02:00
TEST_CASE ( removeattribute ) ;
2014-04-20 20:40:55 +02:00
TEST_CASE ( functionAttributeBefore ) ;
TEST_CASE ( functionAttributeAfter ) ;
2015-10-19 20:03:33 +02:00
TEST_CASE ( cpp03template1 ) ;
2010-09-05 08:16:19 +02:00
TEST_CASE ( cpp0xtemplate1 ) ;
TEST_CASE ( cpp0xtemplate2 ) ;
2011-02-12 16:51:59 +01:00
TEST_CASE ( cpp0xtemplate3 ) ;
2014-12-14 14:58:54 +01:00
TEST_CASE ( cpp0xtemplate4 ) ; // Ticket #6181: Mishandled C++11 syntax
2015-05-26 00:28:08 +02:00
TEST_CASE ( cpp14template ) ; // Ticket #6708
2010-02-20 18:13:09 +01:00
TEST_CASE ( arraySize ) ;
2010-02-21 09:47:41 +01:00
TEST_CASE ( labels ) ;
2010-04-14 19:04:16 +02:00
TEST_CASE ( simplifyInitVar ) ;
2013-10-31 17:20:00 +01:00
TEST_CASE ( simplifyInitVar2 ) ;
TEST_CASE ( simplifyInitVar3 ) ;
2010-08-15 11:54:28 +02:00
2010-08-18 22:42:04 +02:00
TEST_CASE ( bitfields1 ) ;
TEST_CASE ( bitfields2 ) ;
TEST_CASE ( bitfields3 ) ;
2010-08-21 16:34:41 +02:00
TEST_CASE ( bitfields4 ) ; // ticket #1956
2010-08-22 09:41:22 +02:00
TEST_CASE ( bitfields5 ) ; // ticket #1956
2011-02-21 00:22:49 +01:00
TEST_CASE ( bitfields6 ) ; // ticket #2595
2011-03-19 21:00:43 +01:00
TEST_CASE ( bitfields7 ) ; // ticket #1987
2011-03-22 01:17:14 +01:00
TEST_CASE ( bitfields8 ) ;
2011-04-05 04:18:12 +02:00
TEST_CASE ( bitfields9 ) ; // ticket #2706
2011-05-19 17:04:36 +02:00
TEST_CASE ( bitfields10 ) ;
2011-06-21 13:31:55 +02:00
TEST_CASE ( bitfields11 ) ; // ticket #2845 (segmentation fault)
2012-01-09 16:24:11 +01:00
TEST_CASE ( bitfields12 ) ; // ticket #3485 (segmentation fault)
2012-01-23 07:39:31 +01:00
TEST_CASE ( bitfields13 ) ; // ticket #3502 (segmentation fault)
2013-02-07 17:03:08 +01:00
TEST_CASE ( bitfields14 ) ; // ticket #4561 (segfault for 'class a { signals: };')
2010-08-18 22:42:04 +02:00
2012-07-15 11:05:19 +02:00
TEST_CASE ( simplifyNamespaceStd ) ;
2011-09-23 01:59:56 +02:00
TEST_CASE ( microsoftMemory ) ;
2010-08-31 21:40:51 +02:00
2010-09-01 18:10:12 +02:00
TEST_CASE ( borland ) ;
2010-12-02 17:41:49 +01:00
TEST_CASE ( Qt ) ;
2013-04-18 16:13:58 +02:00
TEST_CASE ( simplifySQL ) ;
2010-09-09 17:43:09 +02:00
2014-03-16 18:51:05 +01:00
TEST_CASE ( simplifyCAlternativeTokens ) ;
2010-09-09 19:40:36 +02:00
2012-06-14 21:47:03 +02:00
TEST_CASE ( simplifyCalculations ) ;
2011-08-06 06:50:29 +02:00
2010-09-09 19:40:36 +02:00
// foo(p = new char[10]); => p = new char[10]; foo(p);
2010-10-02 12:44:38 +02:00
TEST_CASE ( simplifyAssignmentInFunctionCall ) ;
2010-10-19 21:54:15 +02:00
2010-10-31 08:47:13 +01:00
// "x += .." => "x = x + .."
TEST_CASE ( simplifyCompoundAssignment ) ;
2012-09-22 18:41:33 +02:00
// x = ({ 123; }); => { x = 123; }
2013-04-19 06:42:09 +02:00
TEST_CASE ( simplifyRoundCurlyParentheses ) ;
2012-09-22 18:41:33 +02:00
2011-02-28 20:29:34 +01:00
TEST_CASE ( simplifyOperatorName1 ) ;
TEST_CASE ( simplifyOperatorName2 ) ;
TEST_CASE ( simplifyOperatorName3 ) ;
TEST_CASE ( simplifyOperatorName4 ) ;
2011-03-29 02:02:06 +02:00
TEST_CASE ( simplifyOperatorName5 ) ;
2011-10-12 15:10:34 +02:00
TEST_CASE ( simplifyOperatorName6 ) ; // ticket #3194
2013-03-01 13:06:51 +01:00
TEST_CASE ( simplifyOperatorName7 ) ; // ticket #4619
2014-04-26 18:30:09 +02:00
TEST_CASE ( simplifyOperatorName8 ) ; // ticket #5706
2014-05-03 21:35:04 +02:00
TEST_CASE ( simplifyOperatorName9 ) ; // ticket #5709 - comma operator not properly tokenized
2011-01-30 08:34:58 +01:00
2012-08-26 10:03:05 +02:00
TEST_CASE ( simplifyNullArray ) ;
2011-01-30 08:34:58 +01:00
// Some simple cleanups of unhandled macros in the global scope
TEST_CASE ( removeMacrosInGlobalScope ) ;
2012-11-29 08:44:12 +01:00
TEST_CASE ( removeMacroInVarDecl ) ;
2011-03-26 12:20:23 +01:00
// a = b = 0;
TEST_CASE ( multipleAssignment ) ;
2011-06-11 21:51:12 +02:00
2012-11-11 16:16:17 +01:00
TEST_CASE ( platformWin ) ;
2011-09-18 01:40:52 +02:00
TEST_CASE ( platformWin32 ) ;
2011-09-24 20:51:03 +02:00
TEST_CASE ( platformWin32A ) ;
TEST_CASE ( platformWin32W ) ;
2011-09-18 01:40:52 +02:00
TEST_CASE ( platformWin64 ) ;
TEST_CASE ( platformUnix32 ) ;
TEST_CASE ( platformUnix64 ) ;
2013-09-06 05:36:33 +02:00
TEST_CASE ( platformWin32AStringCat ) ; // ticket #5015
TEST_CASE ( platformWin32WStringCat ) ; // ticket #5015
2014-10-19 07:34:40 +02:00
TEST_CASE ( platformWinWithNamespace ) ;
2012-09-03 18:51:15 +02:00
2013-09-21 16:59:06 +02:00
TEST_CASE ( simplifyMathFunctions ) ; // ticket #5031
2013-10-08 16:05:05 +02:00
TEST_CASE ( simplifyMathFunctions_sqrt ) ;
TEST_CASE ( simplifyMathFunctions_cbrt ) ;
TEST_CASE ( simplifyMathFunctions_exp ) ;
2013-10-13 15:41:47 +02:00
TEST_CASE ( simplifyMathFunctions_exp2 ) ;
TEST_CASE ( simplifyMathFunctions_logb ) ;
TEST_CASE ( simplifyMathFunctions_log1p ) ;
TEST_CASE ( simplifyMathFunctions_ilogb ) ;
TEST_CASE ( simplifyMathFunctions_log10 ) ;
2013-10-08 16:05:05 +02:00
TEST_CASE ( simplifyMathFunctions_log ) ;
2013-10-13 15:41:47 +02:00
TEST_CASE ( simplifyMathFunctions_log2 ) ;
2013-10-08 16:05:05 +02:00
TEST_CASE ( simplifyMathFunctions_div ) ;
TEST_CASE ( simplifyMathFunctions_pow ) ;
TEST_CASE ( simplifyMathFunctions_islessgreater ) ;
TEST_CASE ( simplifyMathFunctions_islessequal ) ;
TEST_CASE ( simplifyMathFunctions_isless ) ;
TEST_CASE ( simplifyMathFunctions_isgreaterequal ) ;
TEST_CASE ( simplifyMathFunctions_isgreater ) ;
TEST_CASE ( simplifyMathFunctions_fmin ) ;
TEST_CASE ( simplifyMathFunctions_fmax ) ;
2013-10-07 22:10:58 +02:00
TEST_CASE ( simplifyMathFunctions_acosh ) ;
2013-10-09 16:59:32 +02:00
TEST_CASE ( simplifyMathFunctions_acos ) ;
2013-10-13 14:23:10 +02:00
TEST_CASE ( simplifyMathFunctions_cosh ) ;
TEST_CASE ( simplifyMathFunctions_cos ) ;
TEST_CASE ( simplifyMathFunctions_erfc ) ;
2013-10-13 15:41:47 +02:00
TEST_CASE ( simplifyMathFunctions_erf ) ;
TEST_CASE ( simplifyMathFunctions_sin ) ;
TEST_CASE ( simplifyMathFunctions_sinh ) ;
TEST_CASE ( simplifyMathFunctions_asin ) ;
TEST_CASE ( simplifyMathFunctions_asinh ) ;
TEST_CASE ( simplifyMathFunctions_tan ) ;
TEST_CASE ( simplifyMathFunctions_tanh ) ;
TEST_CASE ( simplifyMathFunctions_atan ) ;
TEST_CASE ( simplifyMathFunctions_atanh ) ;
TEST_CASE ( simplifyMathFunctions_expm1 ) ;
2013-10-13 16:28:02 +02:00
TEST_CASE ( simplifyMathFunctions_fma ) ;
2013-09-21 16:59:06 +02:00
2012-09-03 18:51:15 +02:00
TEST_CASE ( simplifyMathExpressions ) ; //ticket #1620
2014-07-02 08:59:04 +02:00
TEST_CASE ( simplifyStaticConst ) ;
2012-12-15 20:21:09 +01:00
2015-05-10 12:35:47 +02:00
TEST_CASE ( simplifyDeprecated ) ;
2015-11-06 18:39:03 +01:00
TEST_CASE ( simplifyCaseRange ) ;
2014-04-13 12:47:54 +02:00
TEST_CASE ( compileLimits ) ; // #5592 crash: gcc: testsuit: gcc.c-torture/compile/limits-declparen.c
2015-10-12 18:14:56 +02:00
TEST_CASE ( prepareTernaryOpForAST ) ;
2012-12-15 20:21:09 +01:00
// AST data
TEST_CASE ( astexpr ) ;
TEST_CASE ( astpar ) ;
2014-06-04 18:08:51 +02:00
TEST_CASE ( astnewdelete ) ;
2012-12-15 20:21:09 +01:00
TEST_CASE ( astbrackets ) ;
TEST_CASE ( astunaryop ) ;
2012-12-16 08:41:04 +01:00
TEST_CASE ( astfunction ) ;
TEST_CASE ( asttemplate ) ;
2014-05-24 11:28:43 +02:00
TEST_CASE ( astcast ) ;
2014-05-25 19:48:31 +02:00
TEST_CASE ( astlambda ) ;
2014-04-26 18:31:56 +02:00
TEST_CASE ( startOfExecutableScope ) ;
2015-01-17 07:42:49 +01:00
TEST_CASE ( removeMacroInClassDef ) ; // #6058
2015-02-22 13:38:06 +01:00
TEST_CASE ( sizeofAddParentheses ) ;
2015-07-19 15:03:02 +02:00
TEST_CASE ( noreturn ) ; // #5783
2008-12-18 22:28:57 +01:00
}
2014-11-20 14:20:09 +01:00
std : : string tokenizeAndStringify ( const char code [ ] , bool simplify = false , bool expand = true , Settings : : PlatformType platform = Settings : : Unspecified , const char * filename = " test.cpp " , bool cpp11 = true ) {
2010-11-07 11:07:56 +01:00
errout . str ( " " ) ;
2015-10-07 18:33:57 +02:00
settings1 . debugwarnings = true ;
settings1 . platform ( platform ) ;
settings1 . standards . cpp = cpp11 ? Standards : : CPP11 : Standards : : CPP03 ;
2010-12-01 18:00:55 +01:00
// tokenize..
2015-10-07 18:33:57 +02:00
Tokenizer tokenizer ( & settings1 , this ) ;
2009-01-18 09:52:20 +01:00
std : : istringstream istr ( code ) ;
2012-04-16 19:51:07 +02:00
tokenizer . tokenize ( istr , filename ) ;
2010-04-02 07:30:58 +02:00
if ( simplify )
2013-12-30 17:45:28 +01:00
tokenizer . simplifyTokenList2 ( ) ;
2009-01-18 09:52:20 +01:00
2014-01-17 18:07:05 +01:00
// filter out ValueFlow messages..
const std : : string debugwarnings = errout . str ( ) ;
errout . str ( " " ) ;
std : : istringstream istr2 ( debugwarnings . c_str ( ) ) ;
std : : string line ;
while ( std : : getline ( istr2 , line ) ) {
if ( line . find ( " ValueFlow " ) = = std : : string : : npos )
errout < < line < < " \n " ;
}
2014-05-20 21:55:08 +02:00
if ( tokenizer . tokens ( ) )
return tokenizer . tokens ( ) - > stringifyList ( false , expand , false , true , false , 0 , 0 ) ;
else
return " " ;
2009-01-18 09:52:20 +01:00
}
2014-11-20 14:20:09 +01:00
std : : string tokenizeAndStringifyWindows ( const char code [ ] , bool simplify = false , bool expand = true , Settings : : PlatformType platform = Settings : : Unspecified , const char * filename = " test.cpp " , bool cpp11 = true ) {
2014-10-19 07:34:40 +02:00
errout . str ( " " ) ;
settings_windows . debugwarnings = true ;
settings_windows . platform ( platform ) ;
settings_windows . standards . cpp = cpp11 ? Standards : : CPP11 : Standards : : CPP03 ;
// tokenize..
Tokenizer tokenizer ( & settings_windows , this ) ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , filename ) ;
if ( simplify )
tokenizer . simplifyTokenList2 ( ) ;
// filter out ValueFlow messages..
const std : : string debugwarnings = errout . str ( ) ;
errout . str ( " " ) ;
std : : istringstream istr2 ( debugwarnings . c_str ( ) ) ;
std : : string line ;
while ( std : : getline ( istr2 , line ) ) {
if ( line . find ( " ValueFlow " ) = = std : : string : : npos )
errout < < line < < " \n " ;
}
if ( tokenizer . tokens ( ) )
return tokenizer . tokens ( ) - > stringifyList ( false , expand , false , true , false , 0 , 0 ) ;
else
return " " ;
}
2015-10-07 18:33:57 +02:00
std : : string tokenizeDebugListing ( const char code [ ] , bool simplify = false , const char filename [ ] = " test.cpp " ) {
errout . str ( " " ) ;
settings2 . standards . c = Standards : : C89 ;
settings2 . standards . cpp = Standards : : CPP03 ;
Tokenizer tokenizer ( & settings2 , this ) ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , filename ) ;
if ( simplify )
tokenizer . simplifyTokenList2 ( ) ;
// result..
return tokenizer . tokens ( ) - > stringifyList ( true ) ;
}
2009-01-18 09:52:20 +01:00
2014-11-20 14:20:09 +01:00
void tokenize1 ( ) {
2014-04-02 13:56:34 +02:00
const char code [ ] = " void f ( ) \n "
" { if ( p . y ( ) > yof ) { } } " ;
ASSERT_EQUALS ( code , tokenizeAndStringify ( code ) ) ;
2009-11-12 18:53:26 +01:00
}
2014-11-20 14:20:09 +01:00
void tokenize2 ( ) {
2014-04-02 13:56:34 +02:00
const char code [ ] = " { sizeof a, sizeof b } " ;
ASSERT_EQUALS ( " { sizeof ( a ) , sizeof ( b ) } " , tokenizeAndStringify ( code ) ) ;
2009-11-22 09:06:39 +01:00
}
2014-11-20 14:20:09 +01:00
void tokenize3 ( ) {
2014-04-02 13:56:34 +02:00
const char code [ ] = " void foo() \n "
" { \n "
" int i; \n "
" ABC(for(i=0;i<10;i++) x()); \n "
" } " ;
2009-12-12 19:58:19 +01:00
ASSERT_EQUALS ( " void foo ( ) \n "
" { \n "
" int i ; \n "
" ABC ( for ( i = 0 ; i < 10 ; i ++ ) x ( ) ) ; \n "
2014-04-02 13:56:34 +02:00
" } " , tokenizeAndStringify ( code ) ) ;
2009-12-12 19:58:19 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void tokenize4 ( ) {
2014-04-02 13:56:34 +02:00
const char code [ ] = " class foo \n "
" { \n "
" public: \n "
" const int i; \n "
" } " ;
2010-01-29 19:34:43 +01:00
ASSERT_EQUALS ( " class foo \n "
" { \n "
" public: \n "
" const int i ; \n "
2014-04-02 13:56:34 +02:00
" } " , tokenizeAndStringify ( code ) ) ;
2010-01-29 19:34:43 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void tokenize5 ( ) {
2010-04-04 08:01:05 +02:00
// Tokenize values
ASSERT_EQUALS ( " ; + 1E3 ; " , tokenizeAndStringify ( " ; +1E3 ; " ) ) ;
ASSERT_EQUALS ( " ; 1E-2 ; " , tokenizeAndStringify ( " ; 1E-2 ; " ) ) ;
}
2014-11-20 14:20:09 +01:00
void tokenize6 ( ) {
2011-11-08 17:55:04 +01:00
// "&p[1]" => "p+1"
/*
ASSERT_EQUALS ( " ; x = p + n ; " , tokenizeAndStringify ( " ; x = & p [ n ] ; " , true ) ) ;
ASSERT_EQUALS ( " ; x = ( p + n ) [ m ] ; " , tokenizeAndStringify ( " ; x = & p [ n ] [ m ] ; " , true ) ) ;
ASSERT_EQUALS ( " ; x = y & p [ n ] ; " , tokenizeAndStringify ( " ; x = y & p [ n ] ; " , true ) ) ;
ASSERT_EQUALS ( " ; x = 10 & p [ n ] ; " , tokenizeAndStringify ( " ; x = 10 & p [ n ] ; " , true ) ) ;
ASSERT_EQUALS ( " ; x = y [ 10 ] & p [ n ] ; " , tokenizeAndStringify ( " ; x = y [ 10 ] & p [ n ] ; " , true ) ) ;
ASSERT_EQUALS ( " ; x = ( a + m ) & p [ n ] ; " , tokenizeAndStringify ( " ; x = ( a + m ) & p [ n ] ; " , true ) ) ; */
2010-06-14 08:36:34 +02:00
// "*(p+1)" => "p[1]"
ASSERT_EQUALS ( " ; x = p [ 1 ] ; " , tokenizeAndStringify ( " ; x = * ( p + 1 ) ; " , true ) ) ;
2013-05-03 06:50:48 +02:00
ASSERT_EQUALS ( " ; x = p [ 10 ] ; " , tokenizeAndStringify ( " ; x = * ( p + 0xA ) ; " , true ) ) ;
2010-06-14 08:36:34 +02:00
ASSERT_EQUALS ( " ; x = p [ n ] ; " , tokenizeAndStringify ( " ; x = * ( p + n ) ; " , true ) ) ;
2011-11-08 17:55:04 +01:00
ASSERT_EQUALS ( " ; x = y * ( p + n ) ; " , tokenizeAndStringify ( " ; x = y * ( p + n ) ; " , true ) ) ;
ASSERT_EQUALS ( " ; x = 10 * ( p + n ) ; " , tokenizeAndStringify ( " ; x = 10 * ( p + n ) ; " , true ) ) ;
ASSERT_EQUALS ( " ; x = y [ 10 ] * ( p + n ) ; " , tokenizeAndStringify ( " ; x = y [ 10 ] * ( p + n ) ; " , true ) ) ;
ASSERT_EQUALS ( " ; x = ( a + m ) * ( p + n ) ; " , tokenizeAndStringify ( " ; x = ( a + m ) * ( p + n ) ; " , true ) ) ;
2013-05-03 06:50:48 +02:00
// "*(p-1)" => "p[-1]" and "*(p-n)" => "p[-n]"
ASSERT_EQUALS ( " ; x = p [ -1 ] ; " , tokenizeAndStringify ( " ; x = *(p - 1); " , true ) ) ;
ASSERT_EQUALS ( " ; x = p [ -10 ] ; " , tokenizeAndStringify ( " ; x = *(p - 0xA); " , true ) ) ;
ASSERT_EQUALS ( " ; x = p [ - n ] ; " , tokenizeAndStringify ( " ; x = *(p - n); " , true ) ) ;
ASSERT_EQUALS ( " ; x = y * ( p - 1 ) ; " , tokenizeAndStringify ( " ; x = y * (p - 1); " , true ) ) ;
ASSERT_EQUALS ( " ; x = 10 * ( p - 1 ) ; " , tokenizeAndStringify ( " ; x = 10 * (p - 1); " , true ) ) ;
ASSERT_EQUALS ( " ; x = y [ 10 ] * ( p - 1 ) ; " , tokenizeAndStringify ( " ; x = y[10] * (p - 1); " , true ) ) ;
ASSERT_EQUALS ( " ; x = ( a - m ) * ( p - n ) ; " , tokenizeAndStringify ( " ; x = (a - m) * (p - n); " , true ) ) ;
// Test that the array-index simplification is not applied when there's no dereference:
// "(x-y)" => "(x-y)" and "(x+y)" => "(x+y)"
ASSERT_EQUALS ( " ; a = b * ( x - y ) ; " , tokenizeAndStringify ( " ; a = b * (x - y); " , true ) ) ;
ASSERT_EQUALS ( " ; a = b * x [ - y ] ; " , tokenizeAndStringify ( " ; a = b * *(x - y); " , true ) ) ;
ASSERT_EQUALS ( " ; a = a * ( x - y ) ; " , tokenizeAndStringify ( " ; a *= (x - y); " , true ) ) ;
ASSERT_EQUALS ( " ; z = a ++ * ( x - y ) ; " , tokenizeAndStringify ( " ; z = a++ * (x - y); " , true ) ) ;
ASSERT_EQUALS ( " ; z = a ++ * ( x + y ) ; " , tokenizeAndStringify ( " ; z = a++ * (x + y); " , true ) ) ;
ASSERT_EQUALS ( " ; z = a -- * ( x - y ) ; " , tokenizeAndStringify ( " ; z = a-- * (x - y); " , true ) ) ;
ASSERT_EQUALS ( " ; z = a -- * ( x + y ) ; " , tokenizeAndStringify ( " ; z = a-- * (x + y); " , true ) ) ;
ASSERT_EQUALS ( " ; z = 'a' * ( x - y ) ; " , tokenizeAndStringify ( " ; z = 'a' * (x - y); " , true ) ) ;
ASSERT_EQUALS ( " ; z = \" a \" * ( x - y ) ; " , tokenizeAndStringify ( " ; z = \" a \" * (x - y); " , true ) ) ;
ASSERT_EQUALS ( " ; z = 'a' * ( x + y ) ; " , tokenizeAndStringify ( " ; z = 'a' * (x + y); " , true ) ) ;
ASSERT_EQUALS ( " ; z = \" a \" * ( x + y ) ; " , tokenizeAndStringify ( " ; z = \" a \" * (x + y); " , true ) ) ;
ASSERT_EQUALS ( " ; z = foo ( ) * ( x + y ) ; " , tokenizeAndStringify ( " ; z = foo() * (x + y); " , true ) ) ;
2010-06-14 08:36:34 +02:00
}
2014-11-20 14:20:09 +01:00
void tokenize7 ( ) {
2014-04-02 13:56:34 +02:00
const char code [ ] = " void f() { \n "
" int x1 = 1; \n "
" int x2(x1); \n "
" } \n " ;
2010-08-27 22:58:21 +02:00
ASSERT_EQUALS ( " void f ( ) { \n int x1 ; x1 = 1 ; \n int x2 ; x2 = x1 ; \n } " ,
2014-04-02 13:56:34 +02:00
tokenizeAndStringify ( code , false ) ) ;
2010-08-27 22:58:21 +02:00
}
2014-11-20 14:20:09 +01:00
void tokenize8 ( ) {
2014-04-02 13:56:34 +02:00
const char code [ ] = " void f() { \n "
" int x1(g()); \n "
" int x2(x1); \n "
" } \n " ;
2010-08-28 13:32:43 +02:00
ASSERT_EQUALS ( " \n \n ##file 0 \n "
" 1: void f ( ) { \n "
" 2: int x1@1 ; x1@1 = g ( ) ; \n "
" 3: int x2@2 ; x2@2 = x1@1 ; \n "
" 4: } \n " ,
2012-02-17 15:47:08 +01:00
tokenizeDebugListing ( code , false ) ) ;
2010-08-28 13:32:43 +02:00
}
2014-11-20 14:20:09 +01:00
void tokenize9 ( ) {
2010-08-29 13:54:26 +02:00
const char code [ ] = " typedef void (*fp)(); \n "
" typedef fp (*fpp)(); \n "
" void f() { \n "
" fpp x = (fpp)f(); \n "
" } " ;
tokenizeAndStringify ( code , false ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void tokenize11 ( ) {
2010-09-04 11:49:56 +02:00
ASSERT_EQUALS ( " X * sizeof ( Y ( ) ) ; " , tokenizeAndStringify ( " X * sizeof(Y()); " , false ) ) ;
}
2010-12-26 15:07:14 +01:00
// bailout if there is "@" - it is not handled well
2014-11-20 14:20:09 +01:00
void tokenize13 ( ) {
2010-12-26 15:07:14 +01:00
const char code [ ] = " @implementation \n "
" -(Foo *)foo: (Bar *)bar \n "
" { } \n "
" @end \n " ;
2015-08-01 07:39:56 +02:00
ASSERT_THROW ( tokenizeAndStringify ( code ) , InternalError ) ;
2010-12-26 15:07:14 +01:00
}
2010-12-27 08:09:05 +01:00
// Ticket #2361: 0X10 => 16
2014-11-20 14:20:09 +01:00
void tokenize14 ( ) {
2010-12-27 08:09:05 +01:00
ASSERT_EQUALS ( " ; 16 ; " , tokenizeAndStringify ( " ;0x10; " ) ) ;
ASSERT_EQUALS ( " ; 16 ; " , tokenizeAndStringify ( " ;0X10; " ) ) ;
2012-02-17 15:47:08 +01:00
ASSERT_EQUALS ( " ; 292 ; " , tokenizeAndStringify ( " ;0444; " ) ) ;
2010-12-27 08:09:05 +01:00
}
2011-01-09 18:39:59 +01:00
2011-01-09 10:09:54 +01:00
// Ticket #2429: 0.125
2014-11-20 14:20:09 +01:00
void tokenize15 ( ) {
2011-01-09 10:09:54 +01:00
ASSERT_EQUALS ( " 0.125 " , tokenizeAndStringify ( " .125 " ) ) ;
2012-02-17 19:54:53 +01:00
ASSERT_EQUALS ( " 005.125 " , tokenizeAndStringify ( " 005.125 " ) ) ; // Don't confuse with octal values
2011-01-09 10:09:54 +01:00
}
2010-12-27 08:09:05 +01:00
2011-03-06 09:33:46 +01:00
// #2612 - segfault for "<><<"
2014-11-20 14:20:09 +01:00
void tokenize16 ( ) {
2011-03-06 09:33:46 +01:00
tokenizeAndStringify ( " <><< " ) ;
}
2014-11-20 14:20:09 +01:00
void tokenize17 ( ) { // #2759
2011-04-29 15:19:22 +02:00
ASSERT_EQUALS ( " class B : private :: A { } ; " , tokenizeAndStringify ( " class B : private ::A { }; " ) ) ;
}
2014-11-20 14:20:09 +01:00
void tokenize18 ( ) { // tokenize "(X&&Y)" into "( X && Y )" instead of "( X & & Y )"
2011-07-15 19:37:39 +02:00
ASSERT_EQUALS ( " ( X && Y ) " , tokenizeAndStringify ( " (X&&Y) " ) ) ;
}
2014-11-20 14:20:09 +01:00
void tokenize19 ( ) {
2012-01-15 14:33:53 +01:00
// #3006 - added hasComplicatedSyntaxErrorsInTemplates to avoid segmentation fault
2014-03-27 13:15:21 +01:00
ASSERT_THROW ( tokenizeAndStringify ( " x < () < " ) , InternalError ) ;
2012-01-15 19:47:51 +01:00
2012-01-15 14:33:53 +01:00
// #3496 - make sure hasComplicatedSyntaxErrorsInTemplates works
ASSERT_EQUALS ( " void a ( Fred * f ) { for ( ; n < f . x ( ) ; ) { } } " ,
tokenizeAndStringify ( " void a(Fred* f) MACRO { for (;n < f->x();) {} } " ) ) ;
2014-10-15 21:33:07 +02:00
// #6216 - make sure hasComplicatedSyntaxErrorsInTemplates works
ASSERT_EQUALS ( " C :: C ( ) \n "
" : v { } \n "
" { \n "
" for ( int dim = 0 ; dim < v . size ( ) ; ++ dim ) { \n "
" v [ dim ] . f ( ) ; \n "
" } \n "
" } ; " ,
tokenizeAndStringify ( " C::C() \n "
" :v{} \n "
" { \n "
" for (int dim = 0; dim < v.size(); ++dim) { \n "
" v[dim]->f(); \n "
" } \n "
" }; " ) ) ;
2011-08-15 13:19:49 +02:00
}
2014-11-20 14:20:09 +01:00
void tokenize20 ( ) { // replace C99 _Bool => bool
2011-09-03 23:10:16 +02:00
ASSERT_EQUALS ( " bool a ; a = true ; " , tokenizeAndStringify ( " _Bool a = true; " ) ) ;
}
2014-11-20 14:20:09 +01:00
void tokenize21 ( ) { // tokenize 0x0E-7
2011-12-18 07:37:20 +01:00
ASSERT_EQUALS ( " 14 - 7 " , tokenizeAndStringify ( " 0x0E-7 " ) ) ;
}
2014-11-20 14:20:09 +01:00
void tokenize22 ( ) { // tokenize special marker $ from preprocessor
2014-01-03 18:53:37 +01:00
ASSERT_EQUALS ( " a $b " , tokenizeAndStringify ( " a$b " ) ) ;
ASSERT_EQUALS ( " a $b \n c " , tokenizeAndStringify ( " a $b \n c " ) ) ;
ASSERT_EQUALS ( " a = $0 ; " , tokenizeAndStringify ( " a = $0; " ) ) ;
ASSERT_EQUALS ( " a $++ ; " , tokenizeAndStringify ( " a$++; " ) ) ;
ASSERT_EQUALS ( " $if ( ! p ) " , tokenizeAndStringify ( " $if(!p) " ) ) ;
2011-12-18 13:33:23 +01:00
}
2012-09-19 10:37:30 +02:00
// #4195 - segfault for "enum { int f ( ) { return = } r = f ( ) ; }"
2014-11-20 14:20:09 +01:00
void tokenize24 ( ) {
2014-03-27 13:15:21 +01:00
ASSERT_THROW ( tokenizeAndStringify ( " enum { int f ( ) { return = } r = f ( ) ; } " ) , InternalError ) ;
2012-09-19 10:37:30 +02:00
}
2012-09-27 06:35:36 +02:00
// #4239 - segfault for "f ( struct { int typedef T x ; } ) { }"
2014-11-20 14:20:09 +01:00
void tokenize25 ( ) {
2012-09-27 06:35:36 +02:00
tokenizeAndStringify ( " f ( struct { int typedef T x ; } ) { } " ) ;
}
2012-10-08 21:50:21 +02:00
2012-10-08 21:49:25 +02:00
// #4245 - segfault
2014-11-20 14:20:09 +01:00
void tokenize26 ( ) {
2012-10-08 21:49:25 +02:00
tokenizeAndStringify ( " class x { protected : template < int y = } ; " ) ;
}
2012-09-27 06:35:36 +02:00
2014-11-20 14:20:09 +01:00
void tokenize27 ( ) {
2014-03-19 16:47:11 +01:00
// #4525 - segfault
2013-02-19 21:56:13 +01:00
tokenizeAndStringify ( " struct except_spec_d_good : except_spec_a, except_spec_b { \n "
" ~except_spec_d_good(); \n "
" }; \n "
" struct S { S(); }; \n "
" S::S() __attribute((pure)) = default; "
) ;
2014-03-19 16:47:11 +01:00
// original code: glibc-2.18/posix/bug-regex20.c
tokenizeAndStringify ( " static unsigned int re_string_context_at (const re_string_t *input, int idx, int eflags) internal_function __attribute__ ((pure)); " ) ;
2013-02-19 21:56:13 +01:00
}
2013-05-09 14:47:18 +02:00
// #4725 - ^{}
2014-11-20 14:20:09 +01:00
void tokenize28 ( ) {
2013-05-09 14:47:18 +02:00
ASSERT_EQUALS ( " void f ( ) { asm ( \" ^{} \" ) ; } " , tokenizeAndStringify ( " void f() { ^{} } " ) ) ;
2015-11-02 22:14:41 +01:00
ASSERT_EQUALS ( " void f ( ) { asm ( \" x(^{}); \" ) ; } " , tokenizeAndStringify ( " void f() { x(^{}); } " ) ) ;
2015-11-03 19:23:45 +01:00
ASSERT_EQUALS ( " void f ( ) { asm ( \" foo(A(),^{bar();}); \" ) ; } " , tokenizeAndStringify ( " void f() { foo(A(), ^{ bar(); }); } " ) ) ;
2015-11-02 22:14:41 +01:00
ASSERT_EQUALS ( " int f0 ( Args args ) { \n "
2015-11-03 19:23:45 +01:00
" asm ( \" return^{returnsizeof...(Args);}()+ \" ) ; \n "
2015-11-02 22:14:41 +01:00
" \n "
2015-11-03 19:23:45 +01:00
" asm ( \" ^{returnsizeof...(args);} \" ) ; \n "
2015-11-02 22:14:41 +01:00
" \n "
" \n "
" } ; " , tokenizeAndStringify ( " int f0(Args args) { \n "
" return ^{ \n "
" return sizeof...(Args); \n "
" }() + ^ { \n "
" return sizeof...(args); \n "
" }(); \n "
" }; " ) ) ;
2015-11-02 22:52:27 +01:00
ASSERT_EQUALS ( " int ( ^ block ) ( void ) = asm ( \" ^{staticinttest=0;returntest;} \" ) ; " ,
tokenizeAndStringify ( " int(^block)(void) = ^{ \n "
" static int test = 0; \n "
" return test; \n "
" }; " ) ) ;
2013-05-09 14:47:18 +02:00
}
2014-04-12 10:36:01 +02:00
// #3503 - don't "simplify" SetFunction member function to a variable
2014-11-20 14:20:09 +01:00
void tokenize31 ( ) {
2014-04-12 10:36:01 +02:00
ASSERT_EQUALS ( " struct TTestClass { TTestClass ( ) { } \n "
" void SetFunction ( Other * m_f ) { } \n "
" } ; " ,
tokenizeAndStringify ( " struct TTestClass { TTestClass() { } \n "
" void SetFunction(Other(*m_f)()) { } \n "
" }; " ) ) ;
ASSERT_EQUALS ( " struct TTestClass { TTestClass ( ) { } \n "
" void SetFunction ( Other * m_f ) ; \n "
" } ; " ,
tokenizeAndStringify ( " struct TTestClass { TTestClass() { } \n "
" void SetFunction(Other(*m_f)()); \n "
" }; " ) ) ;
}
2014-06-16 10:58:41 +02:00
// #5884 - Avoid left shift of negative integer value.
2014-11-20 14:20:09 +01:00
void tokenize32 ( ) {
2014-06-16 10:58:41 +02:00
// Do not simplify negative integer left shifts.
const char * code = " void f ( ) { int max_x ; max_x = -10000 << 16 ; } " ;
ASSERT_EQUALS ( code , tokenizeAndStringify ( code ) ) ;
}
2014-08-26 15:21:19 +02:00
// #5780 Various crashes on valid template code in Tokenizer::setVarId()
2014-11-20 14:20:09 +01:00
void tokenize33 ( ) {
2014-08-26 09:12:10 +02:00
const char * code = " template<typename T, typename A = Alloc<T>> struct vector {}; \n "
2014-08-26 10:40:00 +02:00
" void z() { \n "
" vector<int> VI; \n "
" } \n " ;
2015-03-07 11:06:04 +01:00
tokenizeAndStringify ( code , true ) ;
2014-08-26 09:12:10 +02:00
}
2014-11-20 14:20:09 +01:00
void syntax_case_default ( ) { // correct syntax
2014-09-24 13:45:56 +02:00
tokenizeAndStringify ( " void f() {switch (n) { case 0: z(); break;}} " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-09-10 17:33:52 +02:00
2014-09-24 13:45:56 +02:00
tokenizeAndStringify ( " void f() {switch (n) { case 0:; break;}} " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-02-20 06:58:27 +01:00
2014-09-24 13:45:56 +02:00
tokenizeAndStringify ( " void f() {switch (n) { case 0?1:2 : z(); break;}} " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-06-22 14:05:49 +02:00
2014-09-24 13:45:56 +02:00
tokenizeAndStringify ( " void f() {switch (n) { case 0?(1?3:4):2 : z(); break;}} " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-06-29 14:05:49 +02:00
2015-01-31 10:50:39 +01:00
//allow GCC '({ %name%|%num%|%bool% ; })' statement expression extension
2014-09-24 13:45:56 +02:00
tokenizeAndStringify ( " void f() {switch (n) { case 0?({0;}):1: z(); break;}} " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-12-23 12:26:12 +01:00
2014-09-24 13:45:56 +02:00
//'b' can be or a macro or an undefined enum
tokenizeAndStringify ( " void f() {switch (n) { case b: z(); break;}} " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-11-23 20:33:03 +01:00
2014-09-24 13:45:56 +02:00
//valid, when there's this declaration: 'constexpr int g() { return 2; }'
tokenizeAndStringify ( " void f() {switch (n) { case g(): z(); break;}} " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-01-01 19:04:02 +01:00
2014-09-24 13:45:56 +02:00
//valid, when there's also this declaration: 'constexpr int g[1] = {0};'
tokenizeAndStringify ( " void f() {switch (n) { case g[0]: z(); break;}} " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-03-30 11:38:01 +02:00
2014-09-24 13:45:56 +02:00
//valid, similar to above case
tokenizeAndStringify ( " void f() {switch (n) { case *g: z(); break;}} " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-03-17 11:50:45 +01:00
2014-09-24 13:45:56 +02:00
//valid, when 'x' and 'y' are constexpr.
tokenizeAndStringify ( " void f() {switch (n) { case sqrt(x+y): z(); break;}} " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-09-04 21:27:33 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyFileAndLineMacro ( ) { // tokenize 'return - __LINE__' correctly
2013-08-14 06:36:03 +02:00
ASSERT_EQUALS ( " \" test.cpp \" " , tokenizeAndStringify ( " __FILE__ " ) ) ;
2013-04-18 16:13:58 +02:00
ASSERT_EQUALS ( " return -1 ; " , tokenizeAndStringify ( " return - __LINE__; " ) ) ;
}
2015-12-17 15:40:54 +01:00
void foreach ( ) {
2013-12-17 06:34:27 +01:00
// #3690,#5154
2014-04-02 13:56:34 +02:00
const char code [ ] = " void f() { for each ( char c in MyString ) { Console::Write(c); } } " ;
ASSERT_EQUALS ( " void f ( ) { asm ( \" char c in MyString \" ) { Console :: Write ( c ) ; } } " , tokenizeAndStringify ( code ) ) ;
2012-09-03 20:23:53 +02:00
}
2015-10-19 10:01:57 +02:00
void combineOperators ( ) {
ASSERT_EQUALS ( " ; private: " , tokenizeAndStringify ( " ;private: " , false ) ) ;
ASSERT_EQUALS ( " ; protected: " , tokenizeAndStringify ( " ;protected: " , false ) ) ;
ASSERT_EQUALS ( " ; public: " , tokenizeAndStringify ( " ;public: " , false ) ) ;
ASSERT_EQUALS ( " ; __published: " , tokenizeAndStringify ( " ;__published: " , false ) ) ;
ASSERT_EQUALS ( " a . public : " , tokenizeAndStringify ( " a.public: " , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void concatenateNegativeNumber ( ) {
2009-03-17 20:50:06 +01:00
ASSERT_EQUALS ( " i = -12 " , tokenizeAndStringify ( " i = -12 " ) ) ;
ASSERT_EQUALS ( " 1 - 2 " , tokenizeAndStringify ( " 1-2 " ) ) ;
ASSERT_EQUALS ( " foo ( -1 ) - 2 " , tokenizeAndStringify ( " foo(-1)-2 " ) ) ;
2009-03-18 19:48:06 +01:00
ASSERT_EQUALS ( " int f ( ) { return -2 ; } " , tokenizeAndStringify ( " int f(){return -2;} " ) ) ;
2012-11-20 19:12:22 +01:00
ASSERT_EQUALS ( " int x [ 2 ] = { -2 , 1 } " , tokenizeAndStringify ( " int x[2] = {-2,1} " ) ) ;
2012-11-28 07:09:56 +01:00
ASSERT_EQUALS ( " f ( 123 ) " , tokenizeAndStringify ( " f(+123) " ) ) ;
2009-03-17 20:50:06 +01:00
}
2009-01-18 09:52:20 +01:00
2014-11-20 14:20:09 +01:00
void longtok ( ) {
2012-02-17 19:54:53 +01:00
const std : : string filedata ( 10000 , ' a ' ) ;
2015-10-08 11:35:51 +02:00
ASSERT_EQUALS ( filedata , tokenizeAndStringify ( filedata . c_str ( ) , true ) ) ;
2008-12-18 22:28:57 +01:00
}
2009-02-04 20:40:48 +01:00
2010-12-15 18:45:53 +01:00
// Don’ t remove "(int *)"..
2014-11-20 14:20:09 +01:00
void removeCast1 ( ) {
2009-02-04 20:40:48 +01:00
const char code [ ] = " int *f(int *); " ;
2015-10-08 11:35:51 +02:00
ASSERT_EQUALS ( " int * f ( int * ) ; " , tokenizeAndStringify ( code , true ) ) ;
2009-02-04 20:40:48 +01:00
}
2009-05-27 20:06:19 +02:00
// remove static_cast..
2014-11-20 14:20:09 +01:00
void removeCast2 ( ) {
2009-05-27 20:06:19 +02:00
const char code [ ] = " t = (static_cast<std::vector<int> *>(&p)); \n " ;
2015-10-08 11:35:51 +02:00
ASSERT_EQUALS ( " t = & p ; " , tokenizeAndStringify ( code , true ) ) ;
2009-05-27 20:06:19 +02:00
}
2014-11-20 14:20:09 +01:00
void removeCast3 ( ) {
2009-11-14 11:59:05 +01:00
// ticket #961
const char code [ ] = " assert (iplen >= (unsigned) ipv4->ip_hl * 4 + 20); " ;
const char expected [ ] = " assert ( iplen >= ipv4 . ip_hl * 4 + 20 ) ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void removeCast4 ( ) {
2009-11-17 19:03:23 +01:00
// ticket #970
const char code [ ] = " if (a >= (unsigned)(b)) {} " ;
2015-03-02 16:27:00 +01:00
const char expected [ ] = " if ( a >= ( unsigned int ) ( b ) ) { } " ;
2009-11-17 19:03:23 +01:00
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2009-02-04 20:40:48 +01:00
2014-11-20 14:20:09 +01:00
void removeCast5 ( ) {
2010-07-05 13:16:33 +02:00
// ticket #1817
ASSERT_EQUALS ( " a . data = f ; " , tokenizeAndStringify ( " a->data = reinterpret_cast<void*>(static_cast<intptr_t>(f)); " , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void removeCast7 ( ) {
2011-11-25 07:23:54 +01:00
ASSERT_EQUALS ( " str = malloc ( 3 ) " , tokenizeAndStringify ( " str=(char **)malloc(3) " , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void removeCast8 ( ) {
2011-11-25 07:23:54 +01:00
ASSERT_EQUALS ( " ptr1 = ptr2 " , tokenizeAndStringify ( " ptr1=(int * **)ptr2 " , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void removeCast9 ( ) {
2013-02-16 16:58:36 +01:00
ASSERT_EQUALS ( " f ( ( double ) ( v1 ) * v2 ) " , tokenizeAndStringify ( " f((double)(v1)*v2) " , true ) ) ;
2015-03-02 16:27:00 +01:00
ASSERT_EQUALS ( " int v1 ; f ( ( double ) ( v1 ) * v2 ) " , tokenizeAndStringify ( " int v1; f((double)(v1)*v2) " , true ) ) ;
2013-02-16 16:58:36 +01:00
ASSERT_EQUALS ( " f ( ( A ) ( B ) & x ) " , tokenizeAndStringify ( " f((A)(B)&x) " , true ) ) ; // #4439
2012-02-18 14:26:00 +01:00
}
2014-11-20 14:20:09 +01:00
void removeCast10 ( ) {
2012-02-27 18:55:36 +01:00
ASSERT_EQUALS ( " ; ( * f ) ( p ) ; " , tokenizeAndStringify ( " ; (*(void (*)(char *))f)(p); " , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void removeCast11 ( ) {
2012-06-19 20:07:39 +02:00
ASSERT_EQUALS ( " ; x = 0 ; " , tokenizeAndStringify ( " ; *(int *)&x = 0; " , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void removeCast12 ( ) {
2012-10-07 18:38:05 +02:00
// #3935 - don't remove this cast
ASSERT_EQUALS ( " ; ( ( short * ) data ) [ 5 ] = 0 ; " , tokenizeAndStringify ( " ; ((short*)data)[5] = 0; " , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void removeCast13 ( ) {
2013-09-03 06:27:11 +02:00
// casting deref / address of
ASSERT_EQUALS ( " ; int x ; x = * y ; " , tokenizeAndStringify ( " ;int x=(int)*y; " , true ) ) ;
ASSERT_EQUALS ( " ; int x ; x = & y ; " , tokenizeAndStringify ( " ;int x=(int)&y; " , true ) ) ;
TODO_ASSERT_EQUALS ( " ; int x ; x = ( INT ) * y ; " ,
" ; int x ; x = * y ; " ,
tokenizeAndStringify ( " ;int x=(INT)*y; " , true ) ) ; // INT might be a variable
TODO_ASSERT_EQUALS ( " ; int x ; x = ( INT ) & y ; " ,
" ; int x ; x = & y ; " ,
tokenizeAndStringify ( " ;int x=(INT)&y; " , true ) ) ; // INT might be a variable
// #4899 - False positive on unused variable
ASSERT_EQUALS ( " ; float angle ; angle = tilt ; " , tokenizeAndStringify ( " ; float angle = (float) tilt; " , true ) ) ; // status quo
2015-10-17 18:58:13 +02:00
ASSERT_EQUALS ( " ; float angle ; angle = ( float ) - tilt ; " , tokenizeAndStringify ( " ; float angle = (float) -tilt; " , true ) ) ;
ASSERT_EQUALS ( " ; float angle ; angle = ( float ) + tilt ; " , tokenizeAndStringify ( " ; float angle = (float) +tilt; " , true ) ) ;
ASSERT_EQUALS ( " ; int a ; a = ( int ) ~ c ; " , tokenizeAndStringify ( " ; int a = (int)~c; " , true ) ) ;
2013-09-03 06:27:11 +02:00
}
2014-11-20 14:20:09 +01:00
void removeCast14 ( ) { // const
2013-12-04 20:54:37 +01:00
// #5081
2013-11-28 16:04:47 +01:00
ASSERT_EQUALS ( " ( ! ( & s ) . a ) " , tokenizeAndStringify ( " (! ( (struct S const *) &s)->a) " , true ) ) ;
2014-03-18 06:37:19 +01:00
// #5244
ASSERT_EQUALS ( " bar ( & ptr ) ; " , tokenizeAndStringify ( " bar((const X**)&ptr); " , true ) ) ;
2013-11-28 16:04:47 +01:00
}
2014-11-20 14:20:09 +01:00
void removeCast15 ( ) { // #5996 - don't remove cast in 'a+static_cast<int>(b?60:0)'
2014-07-19 18:29:53 +02:00
ASSERT_EQUALS ( " a + ( b ? 60 : 0 ) ; " ,
2014-11-18 19:23:03 +01:00
tokenizeAndStringify ( " a + static_cast<int>(b ? 60 : 0); " , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void removeCast16 ( ) { // #6278
2014-11-18 19:23:03 +01:00
ASSERT_EQUALS ( " Get ( pArray ) ; " ,
tokenizeAndStringify ( " Get((CObject*&)pArray); " , true ) ) ;
2014-07-19 18:29:53 +02:00
}
2015-03-02 16:27:00 +01:00
void removeCast17 ( ) { // #6110 - don't remove any parentheses in 'a(b)(c)'
ASSERT_EQUALS ( " if ( a ( b ) ( c ) >= 3 ) " ,
tokenizeAndStringify ( " if (a(b)(c) >= 3) " , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void inlineasm ( ) {
2012-01-15 19:48:27 +01:00
ASSERT_EQUALS ( " asm ( \" mov ax , bx \" ) ; " , tokenizeAndStringify ( " asm { mov ax,bx }; " ) ) ;
ASSERT_EQUALS ( " asm ( \" mov ax , bx \" ) ; " , tokenizeAndStringify ( " _asm { mov ax,bx }; " ) ) ;
2012-02-19 16:04:35 +01:00
ASSERT_EQUALS ( " asm ( \" mov ax , bx \" ) ; " , tokenizeAndStringify ( " _asm mov ax,bx " ) ) ;
2012-01-15 19:48:27 +01:00
ASSERT_EQUALS ( " asm ( \" mov ax , bx \" ) ; " , tokenizeAndStringify ( " __asm { mov ax,bx }; " ) ) ;
ASSERT_EQUALS ( " asm ( \" \" mov ax,bx \" \" ) ; " , tokenizeAndStringify ( " __asm__ __volatile__ ( \" mov ax,bx \" ); " ) ) ;
ASSERT_EQUALS ( " asm ( \" _emit 12h \" ) ; " , tokenizeAndStringify ( " __asm _emit 12h ; " ) ) ;
ASSERT_EQUALS ( " asm ( \" mov a , b \" ) ; " , tokenizeAndStringify ( " __asm mov a, b ; " ) ) ;
ASSERT_EQUALS ( " asm ( \" \" fnstcw %0 \" : \" = m \" ( old_cw ) \" ) ; " , tokenizeAndStringify ( " asm volatile ( \" fnstcw %0 \" : \" = m \" (old_cw)); " ) ) ;
ASSERT_EQUALS ( " asm ( \" \" fnstcw %0 \" : \" = m \" ( old_cw ) \" ) ; " , tokenizeAndStringify ( " __asm__ ( \" fnstcw %0 \" : \" = m \" (old_cw)); " ) ) ;
ASSERT_EQUALS ( " asm ( \" \" ddd \" \" ) ; " , tokenizeAndStringify ( " __asm __volatile__ ( \" ddd \" ) ; " ) ) ;
2013-11-13 06:04:00 +01:00
ASSERT_EQUALS ( " asm ( \" \" ddd \" \" ) ; " , tokenizeAndStringify ( " __asm __volatile ( \" ddd \" ) ; " ) ) ;
2012-01-15 19:48:27 +01:00
ASSERT_EQUALS ( " asm ( \" \" mov ax,bx \" \" ) ; " , tokenizeAndStringify ( " __asm__ volatile ( \" mov ax,bx \" ); " ) ) ;
2015-07-20 22:51:06 +02:00
ASSERT_EQUALS ( " asm ( \" mov ax , bx \" ) ; int a ; " , tokenizeAndStringify ( " asm { mov ax,bx } int a; " ) ) ;
2015-09-28 15:42:00 +02:00
ASSERT_EQUALS ( " asm \n \n ( \" mov ax , bx __endasm \" ) ; " , tokenizeAndStringify ( " __asm \n mov ax,bx \n __endasm; " ) ) ;
2011-12-12 20:50:49 +01:00
// 'asm ( ) ;' should be in the same line
2011-12-13 21:42:38 +01:00
ASSERT_EQUALS ( " ; \n \n asm ( \" \" mov ax,bx \" \" ) ; " , tokenizeAndStringify ( " ; \n \n __asm__ volatile ( \" mov ax,bx \" ); " , true ) ) ;
2008-12-18 22:28:57 +01:00
}
2014-11-20 14:20:09 +01:00
void ifAddBraces1 ( ) {
2008-12-22 19:05:22 +01:00
const char code [ ] = " void f() \n "
" { \n "
" if (a); \n "
2008-12-23 09:11:33 +01:00
" else ; \n "
2008-12-22 19:05:22 +01:00
" } \n " ;
2009-06-20 13:20:51 +02:00
ASSERT_EQUALS ( " void f ( ) \n "
" { \n "
" if ( a ) { ; } \n "
" else { ; } \n "
" } " , tokenizeAndStringify ( code , true ) ) ;
2008-12-22 19:05:22 +01:00
}
2008-12-22 10:20:46 +01:00
2014-11-20 14:20:09 +01:00
void ifAddBraces2 ( ) {
2008-12-22 19:05:22 +01:00
const char code [ ] = " void f() \n "
" { \n "
" if (a) if (b) { } \n "
" } \n " ;
2009-06-20 13:20:51 +02:00
ASSERT_EQUALS ( " void f ( ) \n "
" { \n "
" if ( a ) { if ( b ) { } } \n "
" } " , tokenizeAndStringify ( code , true ) ) ;
2008-12-22 19:05:22 +01:00
}
2008-12-22 10:20:46 +01:00
2014-11-20 14:20:09 +01:00
void ifAddBraces3 ( ) {
2008-12-22 19:05:22 +01:00
const char code [ ] = " void f() \n "
" { \n "
" if (a) for (;;) { } \n "
" } \n " ;
2009-06-20 13:20:51 +02:00
ASSERT_EQUALS ( " void f ( ) \n "
" { \n "
" if ( a ) { for ( ; ; ) { } } \n "
" } " , tokenizeAndStringify ( code , true ) ) ;
2008-12-22 10:20:46 +01:00
}
2014-11-20 14:20:09 +01:00
void ifAddBraces4 ( ) {
2008-12-22 19:32:04 +01:00
const char code [ ] = " char * foo () \n "
" { \n "
" char *str = malloc(10); \n "
" if (somecondition) \n "
2010-10-27 12:14:40 +02:00
" for ( ; ; ) \n "
2008-12-22 19:32:04 +01:00
" { } \n "
" return str; \n "
" } \n " ;
2009-06-20 13:20:51 +02:00
ASSERT_EQUALS ( " char * foo ( ) \n "
" { \n "
" char * str ; str = malloc ( 10 ) ; \n "
" if ( somecondition ) { \n "
2010-10-27 12:14:40 +02:00
" for ( ; ; ) \n "
2009-06-20 13:20:51 +02:00
" { } } \n "
" return str ; \n "
" } " , tokenizeAndStringify ( code , true ) ) ;
2008-12-22 19:32:04 +01:00
}
2014-11-20 14:20:09 +01:00
void ifAddBraces5 ( ) {
2009-03-15 13:44:57 +01:00
const char code [ ] = " void f() \n "
" { \n "
" for(int i = 0; i < 2; i++) \n "
" if(true) \n "
" return; \n "
" \n "
" return; \n "
" } \n " ;
2009-06-20 13:20:51 +02:00
ASSERT_EQUALS ( " void f ( ) \n "
" { \n "
" for ( int i = 0 ; i < 2 ; i ++ ) { \n "
2011-11-12 12:01:31 +01:00
" \n "
" return ; } \n \n "
2009-06-20 13:20:51 +02:00
" return ; \n "
" } " , tokenizeAndStringify ( code , true ) ) ;
2009-03-15 13:44:57 +01:00
}
2014-11-20 14:20:09 +01:00
void ifAddBraces6 ( ) {
2009-05-09 22:12:14 +02:00
const char code [ ] = " if() " ;
2009-06-20 13:20:51 +02:00
ASSERT_EQUALS ( " if ( ) " , tokenizeAndStringify ( code , true ) ) ;
}
2009-05-09 22:12:14 +02:00
2014-11-20 14:20:09 +01:00
void ifAddBraces7 ( ) {
2009-06-22 22:54:11 +02:00
const char code [ ] = " void f() \n "
" { \n "
" int a; \n "
" if( a ) \n "
" ({a=4;}),({a=5;}); \n "
" } \n " ;
ASSERT_EQUALS ( " void f ( ) \n "
" { \n "
" int a ; \n "
" if ( a ) { \n "
" ( { a = 4 ; } ) , ( { a = 5 ; } ) ; } \n "
" } " , tokenizeAndStringify ( code , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void ifAddBraces9 ( ) {
2009-11-20 20:38:30 +01:00
// ticket #990
const char code [ ] =
2010-08-25 22:10:21 +02:00
" void f() { "
2009-11-20 20:38:30 +01:00
" for (int k=0; k<VectorSize; k++) "
" LOG_OUT(ID_Vector[k]) "
" } " ;
const char expected [ ] =
2010-08-25 22:10:21 +02:00
" void f ( ) { "
2013-02-02 16:01:34 +01:00
" for ( int k = 0 ; k < VectorSize ; k ++ ) "
2009-11-20 20:38:30 +01:00
" LOG_OUT ( ID_Vector [ k ] ) "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void ifAddBraces10 ( ) {
2010-02-07 15:00:19 +01:00
// ticket #1361
const char code [ ] = " { DEBUG(if (x) y; else z); } " ;
2013-02-02 16:01:34 +01:00
const char expected [ ] = " { DEBUG ( if ( x ) { y ; } else z ) ; } " ;
2010-02-07 15:00:19 +01:00
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
2010-02-20 11:43:53 +01:00
}
2014-11-20 14:20:09 +01:00
void ifAddBraces11 ( ) {
2010-02-20 11:43:53 +01:00
const char code [ ] = " { if (x) if (y) ; else ; } " ;
const char expected [ ] = " { if ( x ) { if ( y ) { ; } else { ; } } } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
2010-02-20 13:24:50 +01:00
}
2014-11-20 14:20:09 +01:00
void ifAddBraces12 ( ) {
2010-02-20 13:24:50 +01:00
// ticket #1424
const char code [ ] = " { if (x) do { } while(x); } " ;
const char expected [ ] = " { if ( x ) { do { } while ( x ) ; } } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
2010-02-07 15:00:19 +01:00
}
2014-11-20 14:20:09 +01:00
void ifAddBraces13 ( ) {
2010-06-26 17:15:44 +02:00
// ticket #1809
const char code [ ] = " { if (x) if (y) { } else { } else { } } " ;
const char expected [ ] = " { if ( x ) { if ( y ) { } else { } } else { } } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
// ticket #1809
const char code2 [ ] = " { if (x) while (y) { } else { } } " ;
const char expected2 [ ] = " { if ( x ) { while ( y ) { } } else { } } " ;
ASSERT_EQUALS ( expected2 , tokenizeAndStringify ( code2 , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void ifAddBraces14 ( ) {
2011-02-24 18:49:16 +01:00
// ticket #2610 (segfault)
tokenizeAndStringify ( " if()<{} " , false ) ;
}
2014-11-20 14:20:09 +01:00
void ifAddBraces15 ( ) {
2011-03-01 18:02:50 +01:00
// ticket #2616 - unknown macro before if
2015-05-26 00:28:08 +02:00
ASSERT_EQUALS ( " { A if ( x ) { y ( ) ; } } " , tokenizeAndStringify ( " {A if(x)y();} " , false ) ) ;
2011-03-01 18:02:50 +01:00
}
2014-11-20 14:20:09 +01:00
void ifAddBraces16 ( ) { // ticket # 2739 (segmentation fault)
2012-01-26 22:25:19 +01:00
tokenizeAndStringify ( " if()x " ) ;
2013-02-02 16:01:34 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-01-26 22:25:19 +01:00
// ticket #2873 - the fix is not needed anymore.
{
const char code [ ] = " void f() { "
" (void) ( { if(*p) (*p) = x(); } ) "
" } " ;
2013-02-02 16:01:34 +01:00
ASSERT_EQUALS ( " void f ( ) { ( void ) ( { if ( * p ) { ( * p ) = x ( ) ; } } ) } " ,
2012-01-26 22:25:19 +01:00
tokenizeAndStringify ( code ) ) ;
}
}
2014-11-20 14:20:09 +01:00
void ifAddBraces17 ( ) {
2011-12-12 20:50:49 +01:00
const char code [ ] = " void f() \n "
" { \n "
" if (a) \n "
" bar1 (); \n "
2012-12-06 19:19:22 +01:00
" \n "
2011-12-12 20:50:49 +01:00
" else \n "
" bar2 (); \n "
" } \n " ;
ASSERT_EQUALS ( " void f ( ) \n "
" { \n "
" if ( a ) { \n "
2013-02-02 16:01:34 +01:00
" bar1 ( ) ; } \n "
" \n "
" else { \n "
2011-12-12 20:50:49 +01:00
" bar2 ( ) ; } \n "
" } " , tokenizeAndStringify ( code , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void ifAddBraces18 ( ) {
2011-12-17 19:05:14 +01:00
// ticket #3424 - if if { } else else
ASSERT_EQUALS ( " { if ( x ) { if ( y ) { } else { ; } } else { ; } } " ,
tokenizeAndStringify ( " { if(x) if(y){}else;else;} " , false ) ) ;
2013-02-02 16:01:34 +01:00
ASSERT_EQUALS ( " { if ( x ) { if ( y ) { if ( z ) { } else { ; } } else { ; } } else { ; } } " ,
tokenizeAndStringify ( " { if(x) if(y) if(z){}else;else;else;} " , false ) ) ;
2011-12-17 19:05:14 +01:00
}
2014-11-20 14:20:09 +01:00
void ifAddBraces19 ( ) {
2013-01-24 16:53:20 +01:00
// #3928 - if for if else
const char code [ ] = " void f() \n "
" { \n "
" if (a) \n "
" for (;;) \n "
" if (b) \n "
" bar1(); \n "
" else \n "
" bar2(); \n "
" } \n " ;
ASSERT_EQUALS ( " void f ( ) \n "
" { \n "
" if ( a ) { \n "
" for ( ; ; ) { \n "
" if ( b ) { \n "
2013-02-02 16:01:34 +01:00
" bar1 ( ) ; } \n "
" else { \n "
2013-01-24 16:53:20 +01:00
" bar2 ( ) ; } } } \n "
" } " , tokenizeAndStringify ( code , true ) ) ;
}
2011-12-12 20:50:49 +01:00
2014-11-20 14:20:09 +01:00
void ifAddBraces20 ( ) { // #5012 - syntax error 'else }'
2013-09-05 06:38:07 +02:00
const char code [ ] = " void f() { if(x) {} else } " ;
2014-03-27 13:15:21 +01:00
ASSERT_THROW ( tokenizeAndStringify ( code , true ) , InternalError ) ;
2013-09-05 06:38:07 +02:00
}
2011-02-24 18:49:16 +01:00
2014-11-20 14:20:09 +01:00
void whileAddBraces ( ) {
2010-06-13 08:00:46 +02:00
const char code [ ] = " ;while(a); " ;
ASSERT_EQUALS ( " ; while ( a ) { ; } " , tokenizeAndStringify ( code , true ) ) ;
2009-06-20 13:20:51 +02:00
}
2009-05-09 22:12:14 +02:00
2014-11-20 14:20:09 +01:00
void doWhileAddBraces ( ) {
2009-12-03 23:06:03 +01:00
{
const char code [ ] = " do ; while (0); " ;
2009-12-28 08:37:34 +01:00
const char result [ ] = " do { ; } while ( 0 ) ; " ;
2009-12-03 23:06:03 +01:00
2009-12-28 08:37:34 +01:00
ASSERT_EQUALS ( result , tokenizeAndStringify ( code , false ) ) ;
2009-12-03 23:06:03 +01:00
}
{
const char code [ ] = " UNKNOWN_MACRO ( do ) ; while ( a -- ) ; " ;
const char result [ ] = " UNKNOWN_MACRO ( do ) ; while ( a -- ) { ; } " ;
ASSERT_EQUALS ( result , tokenizeAndStringify ( code , true ) ) ;
}
{
const char code [ ] = " UNKNOWN_MACRO ( do , foo ) ; while ( a -- ) ; " ;
const char result [ ] = " UNKNOWN_MACRO ( do , foo ) ; while ( a -- ) { ; } " ;
ASSERT_EQUALS ( result , tokenizeAndStringify ( code , true ) ) ;
}
{
const char code [ ] = " void foo ( int c , int d ) { \n "
" do \n "
" if ( c ) { \n "
" while ( c ) { c -- ; } \n "
" } \n "
" while ( -- d > 0 ) ; \n "
" return 0 ; \n "
" } \n " ;
const char result [ ] = " void foo ( int c , int d ) { \n "
" do { \n "
" if ( c ) { \n "
" while ( c ) { c -- ; } \n "
" } } \n "
" while ( -- d > 0 ) ; \n "
" return 0 ; \n "
" } " ;
ASSERT_EQUALS ( result , tokenizeAndStringify ( code , true ) ) ;
}
2009-05-09 22:12:14 +02:00
2009-12-03 23:06:03 +01:00
{
const char code [ ] = " void foo ( int c , int d ) { \n "
" do \n "
" do c -- ; while ( c ) ; \n "
" while ( -- d > 0 ) ; \n "
" return 0 ; \n "
" } \n " ;
const char result [ ] = " void foo ( int c , int d ) { \n "
" do { \n "
" do { c -- ; } while ( c ) ; } \n "
" while ( -- d > 0 ) ; \n "
" return 0 ; \n "
" } " ;
ASSERT_EQUALS ( result , tokenizeAndStringify ( code , true ) ) ;
}
2009-08-22 12:42:19 +02:00
}
2009-03-15 13:44:57 +01:00
2014-11-20 14:20:09 +01:00
void forAddBraces1 ( ) {
2010-06-13 08:00:46 +02:00
{
const char code [ ] = " void f() { \n "
" for(;;) \n "
" if (a) { } \n "
" else { } \n "
" } " ;
const char expected [ ] = " void f ( ) { \n "
" for ( ; ; ) { \n "
" if ( a ) { } \n "
" else { } } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
{
const char code [ ] = " void f() { \n "
" for(;;) \n "
" if (a) { } \n "
" else if (b) { } \n "
" else { } \n "
" } " ;
const char expected [ ] = " void f ( ) { \n "
" for ( ; ; ) { \n "
" if ( a ) { } \n "
" else { if ( b ) { } \n "
" else { } } } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
}
2014-11-20 14:20:09 +01:00
void forAddBraces2 ( ) { // #5088
2013-10-14 21:05:54 +02:00
const char code [ ] = " void f() { \n "
" for(;;) try { } catch (...) { } \n "
" } " ;
const char expected [ ] = " void f ( ) { \n "
" for ( ; ; ) { try { } catch ( . . . ) { } } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2010-06-13 08:00:46 +02:00
2014-11-20 14:20:09 +01:00
std : : string simplifyKnownVariables ( const char code [ ] ) {
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
2015-10-07 18:33:57 +02:00
Tokenizer tokenizer ( & settings0 , this ) ;
2009-09-19 08:55:41 +02:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyKnownVariables ( ) ;
2012-04-16 19:51:07 +02:00
return tokenizer . tokens ( ) - > stringifyList ( 0 , false ) ;
2009-09-19 08:55:41 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables1 ( ) {
2009-06-03 21:02:16 +02:00
{
const char code [ ] = " void f() \n "
" { \n "
" int a = 10; \n "
" if (a); \n "
" } \n " ;
2008-12-18 22:28:57 +01:00
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2009-09-30 20:42:14 +02:00
" void f ( ) { int a ; a = 10 ; if ( 10 ) { ; } } " ,
2009-09-19 13:21:50 +02:00
simplifyKnownVariables ( code ) ) ;
2009-06-03 21:02:16 +02:00
}
{
const char code [ ] = " void f() \n "
" { \n "
" int a = 10; \n "
" if (!a); \n "
" } \n " ;
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2009-09-30 20:42:14 +02:00
" void f ( ) { int a ; a = 10 ; if ( ! 10 ) { ; } } " ,
2009-09-19 13:21:50 +02:00
simplifyKnownVariables ( code ) ) ;
2009-06-03 21:02:16 +02:00
}
2008-12-22 19:08:32 +01:00
}
2008-12-18 22:28:57 +01:00
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables2 ( ) {
2008-12-22 19:08:32 +01:00
const char code [ ] = " void f() \n "
" { \n "
" int a = 10; \n "
" a = g(); \n "
" if (a); \n "
" } \n " ;
2008-12-18 22:28:57 +01:00
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2009-09-30 20:42:14 +02:00
" void f ( ) { int a ; a = 10 ; a = g ( ) ; if ( a ) { ; } } " ,
2009-09-19 13:21:50 +02:00
simplifyKnownVariables ( code ) ) ;
2008-12-22 19:08:32 +01:00
}
2008-12-18 22:28:57 +01:00
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables3 ( ) {
2008-12-22 19:08:32 +01:00
const char code [ ] = " void f() \n "
" { \n "
" int a = 4; \n "
" while(true){ \n "
" break; \n "
" a = 10; \n "
" } \n "
" if (a); \n "
" } \n " ;
2008-12-18 22:28:57 +01:00
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2009-09-30 20:42:14 +02:00
" void f ( ) { int a ; a = 4 ; while ( true ) { break ; a = 10 ; } if ( a ) { ; } } " ,
2009-09-19 13:21:50 +02:00
simplifyKnownVariables ( code ) ) ;
2008-12-22 19:08:32 +01:00
}
2008-12-18 22:28:57 +01:00
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables4 ( ) {
2008-12-22 19:08:32 +01:00
const char code [ ] = " void f() \n "
" { \n "
" int a = 4; \n "
" if ( g(a)); \n "
" } \n " ;
2008-12-18 22:28:57 +01:00
2010-02-08 18:25:08 +01:00
// TODO: if a is passed by value is is ok to simplify..
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2010-02-08 18:25:08 +01:00
" void f ( ) { int a ; a = 4 ; if ( g ( a ) ) { ; } } " ,
2009-09-19 13:21:50 +02:00
simplifyKnownVariables ( code ) ) ;
2008-12-22 19:08:32 +01:00
}
2008-12-18 22:28:57 +01:00
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables5 ( ) {
2008-12-22 19:08:32 +01:00
const char code [ ] = " void f() \n "
" { \n "
" int a = 4; \n "
" if ( a = 5 ); \n "
" } \n " ;
2008-12-18 22:28:57 +01:00
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2009-09-30 20:42:14 +02:00
" void f ( ) { int a ; a = 4 ; if ( a = 5 ) { ; } } " ,
2009-09-19 13:21:50 +02:00
simplifyKnownVariables ( code ) ) ;
2008-12-18 22:28:57 +01:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables6 ( ) {
2009-02-20 18:27:57 +01:00
const char code [ ] = " void f() \n "
" { \n "
" char str[2]; "
" int a = 4; \n "
" str[a] = 0; \n "
" } \n " ;
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2009-09-19 13:21:50 +02:00
" void f ( ) { char str [ 2 ] ; int a ; a = 4 ; str [ 4 ] = 0 ; } " ,
simplifyKnownVariables ( code ) ) ;
2009-02-20 18:27:57 +01:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables7 ( ) {
2009-02-24 17:11:37 +01:00
const char code [ ] = " void foo() \n "
" { \n "
" int i = 22; \n "
" abc[i++] = 1; \n "
" abc[++i] = 2; \n "
" } \n " ;
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2009-09-19 13:21:50 +02:00
" void foo ( ) { int i ; i = 24 ; abc [ 22 ] = 1 ; abc [ 24 ] = 2 ; } " ,
simplifyKnownVariables ( code ) ) ;
2009-02-24 17:11:37 +01:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables8 ( ) {
2009-02-27 19:25:47 +01:00
const char code [ ] = " void foo() \n "
" { \n "
" int i = 22; \n "
" i++; \n "
" abc[i] = 0; \n "
" } \n " ;
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2011-12-02 22:49:14 +01:00
" void foo ( ) { int i ; i = 23 ; abc [ 23 ] = 0 ; } " ,
2009-09-19 13:21:50 +02:00
simplifyKnownVariables ( code ) ) ;
2009-03-25 07:10:17 +01:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables9 ( ) {
2009-03-25 07:10:17 +01:00
const char code [ ] = " void foo() \n "
" { \n "
" int a = 1, b = 2; \n "
" if (a < b) \n "
" ; \n "
" } \n " ;
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2009-09-30 20:42:14 +02:00
" void foo ( ) { int a ; a = 1 ; int b ; b = 2 ; if ( 1 < 2 ) { ; } } " ,
2009-09-19 13:21:50 +02:00
simplifyKnownVariables ( code ) ) ;
2009-02-27 19:25:47 +01:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables10 ( ) {
2009-05-01 20:31:07 +02:00
{
const char code [ ] = " void f() \n "
" { \n "
" bool b=false; \n "
" \n "
" { \n "
" b = true; \n "
" } \n "
" \n "
" if( b ) \n "
" { \n "
" a(); \n "
" } \n "
" } \n " ;
2009-09-19 08:55:41 +02:00
2010-11-06 11:08:05 +01:00
const std : : string expected1 ( " void f ( ) { "
" bool b ; b = false ; "
" { b = true ; } " ) ;
2009-09-19 08:55:41 +02:00
TODO_ASSERT_EQUALS (
2010-11-06 11:08:05 +01:00
expected1 + " if ( true ) { a ( ) ; } } " ,
expected1 + " if ( b ) { a ( ) ; } } " ,
2009-09-19 13:21:50 +02:00
simplifyKnownVariables ( code ) ) ;
2011-01-30 12:54:19 +01:00
2009-05-01 20:31:07 +02:00
}
2009-02-24 17:11:37 +01:00
2009-05-01 20:31:07 +02:00
{
const char code [ ] = " void f() \n "
" { \n "
" bool b=false; \n "
" { b = false; } \n "
" { \n "
" b = true; \n "
" } \n "
" \n "
" if( b ) \n "
" { \n "
" a(); \n "
" } \n "
" } \n " ;
2009-09-19 08:55:41 +02:00
TODO_ASSERT_EQUALS (
2009-09-19 13:21:50 +02:00
" void f ( ) { bool b ; b = false ; { b = false ; } { b = true ; } if ( true ) { a ( ) ; } } " ,
2011-01-30 12:54:19 +01:00
" void f ( ) { bool b ; b = false ; { b = false ; } { b = true ; } if ( b ) { a ( ) ; } } " ,
2009-09-19 13:21:50 +02:00
simplifyKnownVariables ( code ) ) ;
2009-05-01 20:31:07 +02:00
}
2009-05-03 20:57:27 +02:00
{
const char code [ ] = " void f() \n "
" { \n "
" int b=0; \n "
" b = 1; \n "
" for( int i = 0; i < 10; i++ ) "
" { \n "
" } \n "
" \n "
2010-02-08 18:25:08 +01:00
" return b; \n "
2009-05-03 20:57:27 +02:00
" } \n " ;
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2010-02-08 18:25:08 +01:00
" void f ( ) { int b ; b = 0 ; b = 1 ; for ( int i = 0 ; i < 10 ; i ++ ) { } return 1 ; } " ,
2009-09-19 13:21:50 +02:00
simplifyKnownVariables ( code ) ) ;
2009-05-03 20:57:27 +02:00
}
2009-05-01 20:31:07 +02:00
}
2009-02-24 17:11:37 +01:00
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables11 ( ) {
2009-06-19 18:03:09 +02:00
const char code [ ] = " const int foo = 0; \n "
" int main() \n "
" { \n "
" int foo=0; \n "
" } \n " ;
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2011-12-02 22:35:03 +01:00
" int main ( ) { int foo ; foo = 0 ; } " ,
2009-09-19 13:21:50 +02:00
simplifyKnownVariables ( code ) ) ;
2009-06-19 18:03:09 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables12 ( ) {
2009-08-01 15:57:54 +02:00
const char code [ ] = " ENTER_NAMESPACE(project_namespace) \n "
" const double pi = 3.14; \n "
" int main(){} \n " ;
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2009-09-19 13:21:50 +02:00
" ENTER_NAMESPACE ( project_namespace ) const double pi = 3.14 ; int main ( ) { } " ,
simplifyKnownVariables ( code ) ) ;
2009-08-01 15:57:54 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables13 ( ) {
2009-08-22 10:23:55 +02:00
const char code [ ] = " void f() \n "
" { \n "
" int i = 10; \n "
" while(--i) {} \n "
" } \n " ;
2009-09-19 08:55:41 +02:00
ASSERT_EQUALS (
2009-09-19 13:21:50 +02:00
" void f ( ) { int i ; i = 10 ; while ( -- i ) { } } " ,
simplifyKnownVariables ( code ) ) ;
2009-09-28 17:15:31 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables14 ( ) {
2009-09-28 17:15:31 +02:00
// ticket #753
const char code [ ] = " void f ( ) { int n ; n = 1 ; do { ++ n ; } while ( n < 10 ) ; } " ;
ASSERT_EQUALS ( code , simplifyKnownVariables ( code ) ) ;
2009-08-22 10:23:55 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables15 ( ) {
2009-10-06 08:14:59 +02:00
{
const char code [ ] = " int main() \n "
" { \n "
" int x=5; \n "
" std::cout << 10 / x << std::endl; \n "
" } \n " ;
ASSERT_EQUALS (
" int main ( ) { int x ; x = 5 ; std :: cout << 10 / 5 << std :: endl ; } " ,
simplifyKnownVariables ( code ) ) ;
}
{
const char code [ ] = " int main() \n "
" { \n "
" int x=5; \n "
" std::cout << x / ( x == 1 ) << std::endl; \n "
" } \n " ;
ASSERT_EQUALS (
" int main ( ) { int x ; x = 5 ; std :: cout << 5 / ( 5 == 1 ) << std :: endl ; } " ,
simplifyKnownVariables ( code ) ) ;
}
}
2009-08-22 10:23:55 +02:00
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables16 ( ) {
2009-10-10 10:13:56 +02:00
// ticket #807 - segmentation fault when macro isn't found
const char code [ ] = " void f ( ) { int n = 1; DISPATCH(while); } " ;
2014-03-27 13:15:21 +01:00
ASSERT_THROW ( simplifyKnownVariables ( code ) , InternalError ) ;
2009-10-10 10:13:56 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables17 ( ) {
2009-11-02 23:07:15 +01:00
// ticket #807 - segmentation fault when macro isn't found
const char code [ ] = " void f ( ) { char *s = malloc(100);mp_ptr p = s; p++; } " ;
ASSERT_EQUALS (
" void f ( ) { char * s ; s = malloc ( 100 ) ; mp_ptr p ; p = s ; p ++ ; } " ,
simplifyKnownVariables ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables18 ( ) {
2010-01-15 22:46:47 +01:00
const char code [ ] = " void f ( ) { char *s = malloc(100);mp_ptr p = s; ++p; } " ;
ASSERT_EQUALS (
" void f ( ) { char * s ; s = malloc ( 100 ) ; mp_ptr p ; p = s ; ++ p ; } " ,
simplifyKnownVariables ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables19 ( ) {
2010-01-24 19:27:39 +01:00
const char code [ ] = " void f ( ) { int i=0; do { if (i>0) { a(); } i=b(); } while (i != 12); } " ;
ASSERT_EQUALS (
2014-01-17 17:25:56 +01:00
" void f ( ) { int i ; i = 0 ; do { if ( i > 0 ) { a ( ) ; } i = b ( ) ; } while ( i != 12 ) ; } " ,
2010-01-24 19:27:39 +01:00
simplifyKnownVariables ( code ) ) ;
2010-02-20 15:50:44 +01:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables20 ( ) {
2010-02-20 15:50:44 +01:00
const char code [ ] = " void f() \n "
" { \n "
" int i = 0; \n "
" if (x) { \n "
" if (i) i=0; \n "
" } \n "
" } \n " ;
ASSERT_EQUALS (
" void f ( ) { int i ; i = 0 ; if ( x ) { if ( 0 ) { i = 0 ; } } } " ,
simplifyKnownVariables ( code ) ) ;
2010-01-24 19:27:39 +01:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables21 ( ) {
2010-03-14 09:57:34 +01:00
const char code [ ] = " void foo() { int n = 10; for (int i = 0; i < n; ++i) { } } " ;
ASSERT_EQUALS (
" void foo ( ) { int n ; n = 10 ; for ( int i = 0 ; i < 10 ; ++ i ) { } } " ,
simplifyKnownVariables ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables22 ( ) {
2010-04-04 18:48:51 +02:00
// This testcase is related to ticket #1169
2010-04-24 07:59:53 +02:00
{
const char code [ ] = " void foo() \n "
" { \n "
" int n = 10; \n "
" i = (n >> 1); \n "
" } \n " ;
2010-04-04 18:48:51 +02:00
2010-04-24 07:59:53 +02:00
ASSERT_EQUALS (
2010-12-18 09:44:58 +01:00
" void foo ( ) { int n ; n = 10 ; i = 10 >> 1 ; } " ,
2010-04-24 07:59:53 +02:00
simplifyKnownVariables ( code ) ) ;
}
{
const char code [ ] = " void foo() \n "
" { \n "
" int n = 10; \n "
" i = (n << 1); \n "
" } \n " ;
2010-04-04 18:48:51 +02:00
2010-04-24 07:59:53 +02:00
ASSERT_EQUALS (
2010-12-18 09:44:58 +01:00
" void foo ( ) { int n ; n = 10 ; i = 10 << 1 ; } " ,
2010-04-24 07:59:53 +02:00
simplifyKnownVariables ( code ) ) ;
}
{
const char code [ ] = " void foo() \n "
" { \n "
" int n = 10; \n "
" i = (1 << n); \n "
" } \n " ;
ASSERT_EQUALS (
2010-12-18 09:44:58 +01:00
" void foo ( ) { int n ; n = 10 ; i = 1 << 10 ; } " ,
2010-04-24 07:59:53 +02:00
simplifyKnownVariables ( code ) ) ;
}
{
const char code [ ] = " void foo() \n "
" { \n "
" int n = 10; \n "
" i = (1 >> n); \n "
" } \n " ;
ASSERT_EQUALS (
2010-12-18 09:44:58 +01:00
" void foo ( ) { int n ; n = 10 ; i = 1 >> 10 ; } " ,
2010-04-24 07:59:53 +02:00
simplifyKnownVariables ( code ) ) ;
}
2010-04-04 18:48:51 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables23 ( ) {
2010-04-15 19:09:19 +02:00
// This testcase is related to ticket #1596
const char code [ ] = " void foo(int x) \n "
" { \n "
" int a[10], c = 0; \n "
" if (x) { \n "
" a[c] = 0; \n "
" c++; \n "
" } else { \n "
" a[c] = 0; \n "
" } \n "
" } \n " ;
TODO_ASSERT_EQUALS (
" void foo ( int x ) "
" { "
" int a [ 10 ] ; int c ; c = 0 ; "
" if ( x ) { a [ 0 ] = 0 ; c = 1 ; } "
" else { a [ 0 ] = 0 ; } "
" } " ,
" void foo ( int x ) "
" { "
" int a [ 10 ] ; int c ; c = 0 ; "
" if ( x ) { a [ 0 ] = 0 ; c ++ ; } "
" else { a [ c ] = 0 ; } "
" } " ,
2011-01-30 12:54:19 +01:00
2010-04-15 19:09:19 +02:00
simplifyKnownVariables ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables25 ( ) {
2010-05-05 18:29:56 +02:00
{
// This testcase is related to ticket #1646
const char code [ ] = " void foo(char *str) \n "
" { \n "
" int i; \n "
" for (i=0;i<10;++i) { \n "
" if (*str == 0) goto label; \n "
" } \n "
" return; \n "
" label: \n "
" str[i] = 0; \n "
" } \n " ;
2010-05-04 20:02:47 +02:00
2010-05-05 18:29:56 +02:00
// Current result
ASSERT_EQUALS (
" void foo ( char * str ) "
" { "
" int i ; "
" for ( i = 0 ; i < 10 ; ++ i ) { "
2015-01-31 20:12:02 +01:00
" if ( * str == 0 ) { goto label ; } "
2010-05-05 18:29:56 +02:00
" } "
" return ; "
" label : ; "
" str [ i ] = 0 ; "
" } " ,
simplifyKnownVariables ( code ) ) ;
}
{
// This testcase is related to ticket #1646
const char code [ ] = " void foo(char *str) \n "
" { \n "
" int i; \n "
" for (i=0;i<10;++i) { } \n "
" return; \n "
" str[i] = 0; \n "
" } \n " ;
// Current result
ASSERT_EQUALS (
" void foo ( char * str ) "
" { "
" int i ; "
" for ( i = 0 ; i < 10 ; ++ i ) { } "
2011-10-13 12:26:27 +02:00
" return ; "
" str [ i ] = 0 ; "
2010-05-05 18:29:56 +02:00
" } " ,
simplifyKnownVariables ( code ) ) ;
}
2010-05-04 20:02:47 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables27 ( ) {
2010-06-19 14:00:45 +02:00
// This testcase is related to ticket #1633
const char code [ ] = " void foo() \n "
" { \n "
" int i1 = 1; \n "
" int i2 = 2; \n "
" int i3 = (i1 + i2) * 3; \n "
" } \n " ;
ASSERT_EQUALS (
" void foo ( ) "
" { "
" int i1 ; i1 = 1 ; "
" int i2 ; i2 = 2 ; "
" int i3 ; i3 = ( 1 + 2 ) * 3 ; "
" } " ,
simplifyKnownVariables ( code ) ) ;
}
2010-05-02 14:41:21 +02:00
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables28 ( ) {
2010-06-19 17:54:38 +02:00
const char code [ ] = " void foo(int g) \n "
" { \n "
" int i = 2; \n "
" if (g) { \n "
" } \n "
" if (i > 0) { \n "
" } \n "
" } \n " ;
ASSERT_EQUALS (
" void foo ( int g ) "
" { "
" int i ; i = 2 ; "
" if ( g ) { } "
2014-01-17 17:25:56 +01:00
" if ( 2 > 0 ) { } "
2010-06-19 17:54:38 +02:00
" } " ,
simplifyKnownVariables ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables29 ( ) { // ticket #1811
2010-06-26 07:50:53 +02:00
{
const char code [ ] = " int foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h + i; \n "
" } \n " ;
const char expected [ ] = " \n \n ##file 0 \n "
" 1: int foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2010-10-26 21:05:20 +02:00
" 4: \n "
2010-06-26 07:50:53 +02:00
" 5: return u@1 + v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
}
{
const char code [ ] = " int foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h - i; \n "
" } \n " ;
const char expected [ ] = " \n \n ##file 0 \n "
" 1: int foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2010-10-26 21:05:20 +02:00
" 4: \n "
2010-06-26 07:50:53 +02:00
" 5: return u@1 - v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
}
{
const char code [ ] = " int foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h * i; \n "
" } \n " ;
const char expected [ ] = " \n \n ##file 0 \n "
" 1: int foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2010-10-26 21:05:20 +02:00
" 4: \n "
2010-06-26 07:50:53 +02:00
" 5: return u@1 * v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
}
{
const char code [ ] = " int foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h / i; \n "
" } \n " ;
const char expected [ ] = " \n \n ##file 0 \n "
" 1: int foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2010-10-26 21:05:20 +02:00
" 4: \n "
2010-06-26 07:50:53 +02:00
" 5: return u@1 / v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
}
{
const char code [ ] = " int foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h & i; \n "
" } \n " ;
const char expected [ ] = " \n \n ##file 0 \n "
" 1: int foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2010-10-26 21:05:20 +02:00
" 4: \n "
2010-06-26 07:50:53 +02:00
" 5: return u@1 & v@2 ; \n "
" 6: } \n " ;
2010-11-06 11:38:58 +01:00
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
2010-06-26 07:50:53 +02:00
}
{
const char code [ ] = " int foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h | i; \n "
" } \n " ;
2011-02-13 22:09:04 +01:00
const char expected [ ] = " \n \n ##file 0 \n "
" 1: int foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2011-02-13 22:09:04 +01:00
" 4: \n "
" 5: return u@1 | v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
2010-06-26 07:50:53 +02:00
}
{
const char code [ ] = " int foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h ^ i; \n "
" } \n " ;
2011-02-13 22:13:19 +01:00
const char expected [ ] = " \n \n ##file 0 \n "
" 1: int foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2011-02-13 22:13:19 +01:00
" 4: \n "
" 5: return u@1 ^ v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
2010-06-26 07:50:53 +02:00
}
{
const char code [ ] = " int foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h % i; \n "
" } \n " ;
2011-02-13 22:03:46 +01:00
const char expected [ ] = " \n \n ##file 0 \n "
" 1: int foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2011-02-13 22:03:46 +01:00
" 4: \n "
" 5: return u@1 % v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
2010-06-26 07:50:53 +02:00
}
{
const char code [ ] = " int foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h >> i; \n "
" } \n " ;
const char expected [ ] = " \n \n ##file 0 \n "
" 1: int foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2010-10-26 21:05:20 +02:00
" 4: \n "
2010-06-26 07:50:53 +02:00
" 5: return u@1 >> v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
}
{
const char code [ ] = " int foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h << i; \n "
" } \n " ;
const char expected [ ] = " \n \n ##file 0 \n "
" 1: int foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2010-10-26 21:05:20 +02:00
" 4: \n "
2010-06-26 07:50:53 +02:00
" 5: return u@1 << v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
}
{
const char code [ ] = " bool foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h == i; \n "
" } \n " ;
2011-02-13 22:24:45 +01:00
const char expected [ ] = " \n \n ##file 0 \n "
" 1: bool foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2011-02-13 22:24:45 +01:00
" 4: \n "
" 5: return u@1 == v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
2010-06-26 07:50:53 +02:00
}
{
const char code [ ] = " bool foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h != i; \n "
" } \n " ;
2011-02-13 22:24:45 +01:00
const char expected [ ] = " \n \n ##file 0 \n "
2010-06-26 07:50:53 +02:00
" 1: bool foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2011-02-13 22:24:45 +01:00
" 4: \n "
" 5: return u@1 != v@2 ; \n "
2010-06-26 07:50:53 +02:00
" 6: } \n " ;
2011-02-13 22:24:45 +01:00
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
2010-06-26 07:50:53 +02:00
}
{
const char code [ ] = " bool foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h > i; \n "
" } \n " ;
2011-02-13 22:24:45 +01:00
const char expected [ ] = " \n \n ##file 0 \n "
" 1: bool foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2011-02-13 22:24:45 +01:00
" 4: \n "
" 5: return u@1 > v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
2010-06-26 07:50:53 +02:00
}
{
const char code [ ] = " bool foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h >= i; \n "
" } \n " ;
2011-02-13 22:24:45 +01:00
const char expected [ ] = " \n \n ##file 0 \n "
" 1: bool foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2011-02-13 22:24:45 +01:00
" 4: \n "
" 5: return u@1 >= v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
2010-06-26 07:50:53 +02:00
}
{
const char code [ ] = " bool foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h < i; \n "
" } \n " ;
2011-02-13 22:24:45 +01:00
const char expected [ ] = " \n \n ##file 0 \n "
" 1: bool foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2011-02-13 22:24:45 +01:00
" 4: \n "
" 5: return u@1 < v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
2010-06-26 07:50:53 +02:00
}
{
const char code [ ] = " bool foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h <= i; \n "
" } \n " ;
2011-02-13 22:24:45 +01:00
const char expected [ ] = " \n \n ##file 0 \n "
" 1: bool foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
2011-02-13 22:24:45 +01:00
" 4: \n "
" 5: return u@1 <= v@2 ; \n "
" 6: } \n " ;
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , true ) ) ;
2010-06-26 07:50:53 +02:00
}
{
const char code [ ] = " bool foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h && i; \n "
" } \n " ;
2011-01-30 12:54:19 +01:00
const char wanted [ ] = " \n \n ##file 0 \n "
" 1: bool foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2011-12-02 23:01:55 +01:00
" 3: \n "
" 4: \n "
2011-01-30 12:54:19 +01:00
" 5: return u@1 && v@2 ; \n "
" 6: } \n " ;
2015-05-30 23:30:36 +02:00
ASSERT_EQUALS ( wanted , tokenizeDebugListing ( code , true ) ) ;
2010-06-26 07:50:53 +02:00
}
{
const char code [ ] = " bool foo(int u, int v) \n "
" { \n "
" int h = u; \n "
" int i = v; \n "
" return h || i; \n "
" } \n " ;
2011-01-30 12:54:19 +01:00
const char wanted [ ] = " \n \n ##file 0 \n "
" 1: bool foo ( int u@1 , int v@2 ) \n "
" 2: { \n "
2015-05-30 23:30:36 +02:00
" 3: \n "
" 4: \n "
2011-01-30 12:54:19 +01:00
" 5: return u@1 || v@2 ; \n "
" 6: } \n " ;
2015-05-30 23:30:36 +02:00
ASSERT_EQUALS ( wanted , tokenizeDebugListing ( code , true ) ) ;
2010-06-26 07:50:53 +02:00
}
}
2010-06-19 17:54:38 +02:00
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables30 ( ) {
2010-10-10 19:27:42 +02:00
const char code [ ] = " int foo() { \n "
" iterator it1 = ints.begin(); \n "
" iterator it2 = it1; \n "
" for (++it2;it2!=ints.end();++it2); \n "
" } \n " ;
const char expected [ ] = " int foo ( ) { \n "
" iterator it1 ; it1 = ints . begin ( ) ; \n "
" iterator it2 ; it2 = it1 ; \n "
" for ( ++ it2 ; it2 != ints . end ( ) ; ++ it2 ) { ; } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables31 ( ) {
2010-11-06 19:42:38 +01:00
const char code [ ] = " void foo(const char str[]) { \n "
" const char *p = str; \n "
" if (p[0] == 0) { \n "
" } \n "
" } \n " ;
const char expected [ ] = " void foo ( const char str [ ] ) { \n "
" const char * p ; p = str ; \n "
" if ( str [ 0 ] == 0 ) { \n "
" } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables32 ( ) {
2011-07-20 09:07:47 +02:00
{
const char code [ ] = " void foo() { \n "
" const int x = 0; \n "
" bar(0,x); \n "
" } \n " ;
2011-12-02 22:35:03 +01:00
const char expected [ ] = " void foo ( ) { \n \n bar ( 0 , 0 ) ; \n } " ;
2011-07-20 09:07:47 +02:00
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
{
const char code [ ] = " static int const SZ = 22; char str[SZ]; \n " ;
2011-12-02 22:35:03 +01:00
ASSERT_EQUALS ( " char str [ 22 ] ; " , tokenizeAndStringify ( code , true ) ) ;
2011-07-20 09:07:47 +02:00
}
2010-11-13 18:45:35 +01:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables33 ( ) {
2010-11-13 18:45:35 +01:00
const char code [ ] = " static void foo(struct Foo *foo) { \n "
" foo->a = 23; \n "
" x[foo->a] = 0; \n "
" } \n " ;
const char expected [ ] = " static void foo ( struct Foo * foo ) { \n "
" foo . a = 23 ; \n "
" x [ 23 ] = 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
2010-11-07 08:43:30 +01:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables34 ( ) {
2010-12-16 20:16:59 +01:00
const char code [ ] = " void f() { \n "
" int x = 10; \n "
" do { cin >> x; } while (x > 5); \n "
" a[x] = 0; \n "
" } \n " ;
const char expected [ ] = " void f ( ) { \n "
" int x ; x = 10 ; \n "
2014-01-17 17:25:56 +01:00
" do { cin >> x ; } while ( x > 5 ) ; \n "
2010-12-16 20:16:59 +01:00
" a [ x ] = 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables35 ( ) {
2010-12-25 10:07:57 +01:00
// Ticket #2353
const char code [ ] = " int f() { "
" int x = 0; "
" if (x == 0) { "
" return 0; "
" } "
" return 10 / x; "
" } " ;
2012-01-30 19:04:35 +01:00
const char expected [ ] = " int f ( ) { int x ; x = 0 ; { return 0 ; } } " ;
2010-12-25 10:07:57 +01:00
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables36 ( ) {
2010-12-26 20:34:07 +01:00
// Ticket #2304
const char code [ ] = " void f() { "
" const char *q = \" hello \" ; "
" strcpy(p, q); "
" } " ;
const char expected [ ] = " void f ( ) { const char * q ; q = \" hello \" ; strcpy ( p , \" hello \" ) ; } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
2014-07-08 16:31:08 +02:00
// Ticket #5972
const char code2 [ ] = " void f() { "
" char buf[10] = \" ab \" ; "
" memset(buf, 0, 10); "
" } " ;
const char expected2 [ ] = " void f ( ) { char buf [ 10 ] = \" ab \" ; memset ( buf , 0 , 10 ) ; } " ;
ASSERT_EQUALS ( expected2 , tokenizeAndStringify ( code2 , true ) ) ;
2010-12-26 20:34:07 +01:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables37 ( ) {
2013-01-16 15:37:07 +01:00
// Ticket #2398 - no simplification in for loop
2011-01-04 19:36:29 +01:00
const char code [ ] = " void f() { \n "
" double x = 0; \n "
" for (int iter=0; iter<42; iter++) { \n "
" int EvaldF = 1; \n "
" if (EvaldF) \n "
" Eval (x); \n "
" } \n "
" } " ;
const char expected [ ] = " void f ( ) { \n "
" double x ; x = 0 ; \n "
" for ( int iter = 0 ; iter < 42 ; iter ++ ) { \n "
2011-12-02 23:01:55 +01:00
" \n "
" \n "
" Eval ( x ) ; \n "
2011-01-04 19:36:29 +01:00
" } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables38 ( ) {
2011-01-04 21:35:35 +01:00
// Ticket #2399 - simplify conditions
const char code [ ] = " void f() { \n "
" int x = 0; \n "
" int y = 1; \n "
" if (x || y); \n "
" } " ;
const char expected [ ] = " void f ( ) { \n "
" \n "
2011-06-30 21:04:26 +02:00
" \n "
2011-12-02 23:01:55 +01:00
" ; \n "
2011-01-04 21:35:35 +01:00
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables39 ( ) {
2011-01-11 19:34:35 +01:00
// Ticket #2296 - simplify pointer alias 'delete p;'
{
const char code [ ] = " void f() { \n "
" int *x; \n "
" int *y = x; \n "
" delete y; \n "
" } " ;
ASSERT_EQUALS ( " void f ( ) { \n int * x ; \n \n delete x ; \n } " , tokenizeAndStringify ( code , true ) ) ;
}
{
const char code [ ] = " void f() { \n "
" int *x; \n "
" int *y = x; \n "
" delete [] y; \n "
" } " ;
ASSERT_EQUALS ( " void f ( ) { \n int * x ; \n \n delete [ ] x ; \n } " , tokenizeAndStringify ( code , true ) ) ;
}
}
2011-02-01 21:46:07 +01:00
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables40 ( ) {
2011-02-01 21:46:07 +01:00
const char code [ ] = " void f() { \n "
" char c1 = 'a'; \n "
" char c2 = { c1 }; \n "
" } " ;
2011-12-02 23:01:55 +01:00
ASSERT_EQUALS ( " void f ( ) { \n \n char c2 ; c2 = { 'a' } ; \n } " , tokenizeAndStringify ( code , true ) ) ;
2011-02-01 21:46:07 +01:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables41 ( ) {
2011-02-20 18:18:27 +01:00
const char code [ ] = " void f() { \n "
" int x = 0; \n "
" const int *p; p = &x; \n "
" if (p) { return 0; } \n "
" } " ;
ASSERT_EQUALS ( " void f ( ) { \n int x ; x = 0 ; \n const int * p ; p = & x ; \n if ( & x ) { return 0 ; } \n } " , tokenizeAndStringify ( code , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables42 ( ) {
2011-03-01 20:20:48 +01:00
{
const char code [ ] = " void f() { \n "
" char str1[10], str2[10]; \n "
" strcpy(str1, \" abc \" ); \n "
" strcpy(str2, str1); \n "
2011-02-26 21:11:56 +01:00
" } " ;
2011-03-01 20:20:48 +01:00
const char expected [ ] = " void f ( ) { \n "
" char str1 [ 10 ] ; char str2 [ 10 ] ; \n "
" strcpy ( str1 , \" abc \" ) ; \n "
" strcpy ( str2 , \" abc \" ) ; \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
{
const char code [ ] = " void f() { "
" char *s = malloc(10); "
" strcpy(s, \" \" ); "
" free(s); "
" } " ;
const char expected [ ] = " void f ( ) { "
" char * s ; s = malloc ( 10 ) ; "
" strcpy ( s , \" \" ) ; "
" free ( s ) ; "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2011-03-03 20:07:56 +01:00
{
const char code [ ] = " void f(char *p, char *q) { "
" strcpy(p, \" abc \" ); "
" q = p; "
" } " ;
const char expected [ ] = " void f ( char * p , char * q ) { "
" strcpy ( p , \" abc \" ) ; "
" q = p ; "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2012-02-01 21:13:26 +01:00
// 3538
{
const char code [ ] = " void f() { \n "
" char s[10]; \n "
" strcpy(s, \" 123 \" ); \n "
" if (s[6] == ' '); \n "
" } " ;
const char expected [ ] = " void f ( ) { \n "
" char s [ 10 ] ; \n "
" strcpy ( s , \" 123 \" ) ; \n "
" if ( s [ 6 ] == ' ' ) { ; } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2011-02-26 21:11:56 +01:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables43 ( ) {
2011-08-31 19:45:20 +02:00
{
const char code [ ] = " void f() { \n "
" int a, *p; p = &a; \n "
" { int a = *p; } \n "
2011-08-30 19:13:04 +02:00
" } " ;
2011-08-31 19:45:20 +02:00
const char expected [ ] = " void f ( ) { \n "
" int a ; int * p ; p = & a ; \n "
" { int a ; a = * p ; } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
{
const char code [ ] = " void f() { \n "
" int *a, **p; p = &a; \n "
" { int *a = *p; } \n "
" } " ;
const char expected [ ] = " void f ( ) { \n "
" int * a ; int * * p ; p = & a ; \n "
" { int * a ; a = * p ; } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2011-08-30 19:13:04 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables44 ( ) {
2011-11-05 08:30:11 +01:00
const char code [ ] = " void a() { \n "
" static int i = 10; \n "
" b(i++); \n "
" } " ;
const char expected [ ] = " void a ( ) { \n "
" static int i = 10 ; \n "
" b ( i ++ ) ; \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables45 ( ) {
2011-11-20 21:50:26 +01:00
const char code [ ] = " class Fred { \n "
" private: \n "
" const static int NUM = 2; \n "
" int array[NUM]; \n "
" } " ;
const char expected [ ] = " class Fred { \n "
" private: \n "
2011-12-02 22:35:03 +01:00
" \n "
2011-11-20 21:50:26 +01:00
" int array [ 2 ] ; \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables46 ( ) {
2012-02-07 08:05:50 +01:00
const char code [ ] = " void f() { \n "
" int x = 0; \n "
" cin >> x; \n "
" return x; \n "
" } " ;
{
const char expected [ ] = " void f ( ) { \n "
" int x ; x = 0 ; \n "
" cin >> x ; \n "
" return x ; \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true , true , Settings : : Unspecified , " test.cpp " ) ) ;
}
{
const char expected [ ] = " void f ( ) { \n "
" \n "
" cin >> 0 ; \n "
" return 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true , true , Settings : : Unspecified , " test.c " ) ) ;
}
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables47 ( ) {
2012-03-15 19:09:36 +01:00
// #3621
const char code [ ] = " void f() { \n "
" int x = 0; \n "
" cin >> std::hex >> x; \n "
" } " ;
const char expected [ ] = " void f ( ) { \n "
" int x ; x = 0 ; \n "
" cin >> std :: hex >> x ; \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true , true , Settings : : Unspecified , " test.cpp " ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables48 ( ) {
2012-04-25 19:23:17 +02:00
// #3754
const char code [ ] = " void f(int sz) { \n "
" int i; \n "
" for (i = 0; ((i<sz) && (sz>3)); ++i) { } \n "
" } " ;
const char expected [ ] = " void f ( int sz ) { \n "
" int i ; \n "
2014-01-17 17:25:56 +01:00
" for ( i = 0 ; ( i < sz ) && ( sz > 3 ) ; ++ i ) { } \n "
2012-04-25 19:23:17 +02:00
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true , true , Settings : : Unspecified , " test.c " ) ) ;
2012-05-07 21:11:23 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables49 ( ) { // #3691
2012-05-07 21:11:23 +02:00
const char code [ ] = " void f(int sz) { \n "
" switch (x) { \n "
" case 1: sz = 2; continue; \n "
" case 2: x = sz; break; \n "
" } \n "
" } " ;
const char expected [ ] = " void f ( int sz ) { \n "
" switch ( x ) { \n "
" case 1 : ; sz = 2 ; continue ; \n "
" case 2 : ; x = sz ; break ; \n "
" } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true , true , Settings : : Unspecified , " test.c " ) ) ;
2012-04-25 19:23:17 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables50 ( ) { // #4066
2012-08-22 16:09:19 +02:00
{
const char code [ ] = " void f() { \n "
" char str1[10], str2[10]; \n "
" sprintf(str1, \" %% \" ); \n "
" strcpy(str2, str1); \n "
" } " ;
const char expected [ ] = " void f ( ) { \n "
" char str1 [ 10 ] ; char str2 [ 10 ] ; \n "
" sprintf ( str1 , \" %% \" ) ; \n "
" strcpy ( str2 , \" % \" ) ; \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
{
const char code [ ] = " void f() { \n "
" char str1[25], str2[25]; \n "
" sprintf(str1, \" abcdef%%%% and %% and % \" ); \n "
" strcpy(str2, str1); \n "
" } " ;
const char expected [ ] = " void f ( ) { \n "
" char str1 [ 25 ] ; char str2 [ 25 ] ; \n "
" sprintf ( str1 , \" abcdef%%%% and %% and % \" ) ; \n "
" strcpy ( str2 , \" abcdef%% and % and % \" ) ; \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
{
const char code [ ] = " void f() { \n "
" char str1[10], str2[10]; \n "
" sprintf(str1, \" abc \" ); \n "
" strcpy(str2, str1); \n "
" } " ;
const char expected [ ] = " void f ( ) { \n "
" char str1 [ 10 ] ; char str2 [ 10 ] ; \n "
" sprintf ( str1 , \" abc \" ) ; \n "
" strcpy ( str2 , \" abc \" ) ; \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2012-11-26 16:54:13 +01:00
{
//don't simplify '&x'!
const char code [ ] = " const char * foo ( ) { \n "
" const char x1 = 'b' ; \n "
" f ( & x1 ) ; \n "
" const char x2 = 'b' ; \n "
" f ( y , & x2 ) ; \n "
" const char x3 = 'b' ; \n "
" t = & x3 ; \n "
" const char x4 = 'b' ; \n "
" t = y + & x4 ; \n "
" const char x5 = 'b' ; \n "
" z [ & x5 ] = y ; \n "
" const char x6 = 'b' ; \n "
" v = { & x6 } ; \n "
" const char x7 = 'b' ; \n "
" return & x7 ; \n "
" } " ;
ASSERT_EQUALS ( code , tokenizeAndStringify ( code , true ) ) ;
}
2015-06-01 10:00:03 +02:00
{
//don't simplify '&x'!
const char code [ ] = " const int * foo ( ) { \n "
" const int x1 = 1 ; \n "
" f ( & x1 ) ; \n "
" const int x2 = 1 ; \n "
" f ( y , & x2 ) ; \n "
" const int x3 = 1 ; \n "
" t = & x3 ; \n "
" const int x4 = 1 ; \n "
" t = y + & x4 ; \n "
" const int x5 = 1 ; \n "
" z [ & x5 ] = y ; \n "
" const int x6 = 1 ; \n "
" v = { & x6 } ; \n "
" const int x7 = 1 ; \n "
" return & x7 ; \n "
" } " ;
ASSERT_EQUALS ( code , tokenizeAndStringify ( code , true ) ) ;
}
2012-08-22 16:09:19 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables51 ( ) { // #4409 hang
2013-01-03 19:14:44 +01:00
const char code [ ] = " void mhz_M(int enough) { \n "
" TYPE *x=&x, **p=x, **q = NULL; \n "
" BENCH1(q = _mhz_M(n); n = 1;) \n "
" use_pointer(q); \n "
" } " ;
tokenizeAndStringify ( code , true ) ; // don't hang
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables52 ( ) { // #4728 "= x %op%"
2013-06-26 21:47:03 +02:00
ASSERT_EQUALS ( " void f ( ) { int y ; y = 34 + z ; } " , tokenizeAndStringify ( " void f() { int x=34; int y=x+z; } " , true ) ) ;
ASSERT_EQUALS ( " void f ( ) { int y ; y = 34 - z ; } " , tokenizeAndStringify ( " void f() { int x=34; int y=x-z; } " , true ) ) ;
ASSERT_EQUALS ( " void f ( ) { int y ; y = 34 * z ; } " , tokenizeAndStringify ( " void f() { int x=34; int y=x*z; } " , true ) ) ;
ASSERT_EQUALS ( " void f ( ) { int y ; y = 34 / z ; } " , tokenizeAndStringify ( " void f() { int x=34; int y=x/z; } " , true ) ) ;
ASSERT_EQUALS ( " void f ( ) { int y ; y = 34 % z ; } " , tokenizeAndStringify ( " void f() { int x=34; int y=x%z; } " , true ) ) ;
ASSERT_EQUALS ( " void f ( ) { int y ; y = 34 & z ; } " , tokenizeAndStringify ( " void f() { int x=34; int y=x&z; } " , true ) ) ;
ASSERT_EQUALS ( " void f ( ) { int y ; y = 34 | z ; } " , tokenizeAndStringify ( " void f() { int x=34; int y=x|z; } " , true ) ) ;
ASSERT_EQUALS ( " void f ( ) { int y ; y = 34 ^ z ; } " , tokenizeAndStringify ( " void f() { int x=34; int y=x^z; } " , true ) ) ;
ASSERT_EQUALS ( " void f ( ) { int y ; y = 34 << z ; } " , tokenizeAndStringify ( " void f() { int x=34; int y=x<<z; } " , true ) ) ;
ASSERT_EQUALS ( " void f ( ) { int y ; y = 34 >> z ; } " , tokenizeAndStringify ( " void f() { int x=34; int y=x>>z; } " , true ) ) ;
ASSERT_EQUALS ( " void f ( ) { int y ; y = 34 && z ; } " , tokenizeAndStringify ( " void f() { int x=34; int y=x&&z; } " , true ) ) ;
ASSERT_EQUALS ( " void f ( ) { int y ; y = 34 || z ; } " , tokenizeAndStringify ( " void f() { int x=34; int y=x||z; } " , true ) ) ;
ASSERT_EQUALS ( " void f ( ) { int y ; y = 34 > z ; } " , tokenizeAndStringify ( " void f() { int x=34; int y=x>z; } " , true ) ) ;
ASSERT_EQUALS ( " void f ( ) { int y ; y = 34 >= z ; } " , tokenizeAndStringify ( " void f() { int x=34; int y=x>=z; } " , true ) ) ;
ASSERT_EQUALS ( " void f ( ) { int y ; y = 34 < z ; } " , tokenizeAndStringify ( " void f() { int x=34; int y=x<z; } " , true ) ) ;
ASSERT_EQUALS ( " void f ( ) { int y ; y = 34 <= z ; } " , tokenizeAndStringify ( " void f() { int x=34; int y=x<=z; } " , true ) ) ;
ASSERT_EQUALS ( " void f ( ) { int y ; y = 34 == z ; } " , tokenizeAndStringify ( " void f() { int x=34; int y=x==z; } " , true ) ) ;
ASSERT_EQUALS ( " void f ( ) { int y ; y = 34 != z ; } " , tokenizeAndStringify ( " void f() { int x=34; int y=x!=z; } " , true ) ) ;
// #4007
ASSERT_EQUALS ( " void f ( ) { } " , tokenizeAndStringify ( " void f() { char *p = 0; int result = p && (!*p); } " , true ) ) ;
ASSERT_EQUALS ( " void f ( ) { } " , tokenizeAndStringify ( " void f() { Foo *p = 0; bool b = (p && (p->type() == 1)); } " , true ) ) ;
2013-06-26 18:13:28 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables53 ( ) { // references
2013-07-30 11:30:14 +02:00
ASSERT_EQUALS ( " void f ( ) { int x ; x = abc ( ) ; } " , tokenizeAndStringify ( " void f() { int x; int &ref=x; ref=abc(); } " , true ) ) ;
ASSERT_EQUALS ( " void f ( ) { int * p ; p = abc ( ) ; } " , tokenizeAndStringify ( " void f() { int *p; int *&ref=p; ref=abc(); } " , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables54 ( ) { // #4913
2013-08-25 08:54:33 +02:00
ASSERT_EQUALS ( " void f ( int * p ) { * -- p = 0 ; * p = 0 ; } " , tokenizeAndStringify ( " void f(int*p) { *--p=0; *p=0; } " , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables55 ( ) { // pointer alias
2013-11-16 17:30:04 +01:00
ASSERT_EQUALS ( " void f ( ) { int a ; if ( a > 0 ) { } } " , tokenizeAndStringify ( " void f() { int a; int *p=&a; if (*p>0) {} } " , true ) ) ;
ASSERT_EQUALS ( " void f ( ) { int a ; struct AB ab ; ab . a = & a ; if ( a > 0 ) { } } " , tokenizeAndStringify ( " void f() { int a; struct AB ab; ab.a = &a; if (*ab.a>0) {} } " , true ) ) ;
ASSERT_EQUALS ( " void f ( ) { int a ; if ( x > a ) { } } " , tokenizeAndStringify ( " void f() { int a; int *p=&a; if (x>*p) {} } " , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables56 ( ) { // ticket #5301 - >>
2014-02-22 12:09:54 +01:00
ASSERT_EQUALS ( " void f ( ) { int a ; a = 0 ; int b ; b = 0 ; * p >> a >> b ; return a / b ; } " ,
tokenizeAndStringify ( " void f() { int a=0,b=0; *p>>a>>b; return a/b; } " , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables57 ( ) { // #4724
2014-04-10 19:22:14 +02:00
ASSERT_EQUALS ( " unsigned long long x ; x = 9223372036854775808 ; " , tokenizeAndStringify ( " unsigned long long x = 1UL << 63 ; " , true ) ) ;
ASSERT_EQUALS ( " long long x ; x = -9223372036854775808 ; " , tokenizeAndStringify ( " long long x = 1L << 63 ; " , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables58 ( ) { // #5268
2014-06-15 15:52:58 +02:00
const char code [ ] = " enum e { VAL1 = 1, VAL2 }; "
" typedef char arr_t[VAL2]; "
" int foo(int) ; "
" void bar () { "
" throw foo (VAL1); "
" } "
" int baz() { "
" return sizeof(arr_t); "
" } " ;
2015-09-06 18:37:22 +02:00
ASSERT_EQUALS ( " int foo ( int ) ; "
2014-06-15 15:52:58 +02:00
" void bar ( ) { throw foo ( 1 ) ; } "
" int baz ( ) { return 2 ; } " , tokenizeAndStringify ( code , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariables59 ( ) { // #5062 - for head
2014-08-17 07:39:42 +02:00
const char code [ ] = " void f() { \n "
" int a[3], i, j; \n "
" for(i = 0, j = 1; i < 3, j < 12; i++,j++) { \n "
" a[i] = 0; \n "
" } \n "
" } " ;
ASSERT_EQUALS ( " void f ( ) { \n "
" int a [ 3 ] ; int i ; int j ; \n "
" for ( i = 0 , j = 1 ; i < 3 , j < 12 ; i ++ , j ++ ) { \n "
" a [ i ] = 0 ; \n "
" } \n "
" } " , tokenizeAndStringify ( code , true ) ) ;
}
2015-11-10 14:45:24 +01:00
void simplifyKnownVariables60 ( ) { // #6829
const char code [ ] = " void f() { \n "
" int i = 1; \n "
" const int * const constPtrToConst = &i; \n "
" std::cout << *constPtrToConst << std::endl; \n "
" std::cout << constPtrToConst << std::endl; \n "
" } " ;
ASSERT_EQUALS ( " void f ( ) { \n "
" int i ; i = 1 ; \n "
" const int * constPtrToConst ; constPtrToConst = & i ; \n "
" std :: cout << i << std :: endl ; \n "
" std :: cout << & i << std :: endl ; \n "
" } " , tokenizeAndStringify ( code , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariablesBailOutAssign1 ( ) {
2010-11-07 11:07:56 +01:00
const char code [ ] = " int foo() { \n "
" int i; i = 0; \n "
" if (x) { i = 10; } \n "
" return i; \n "
" } \n " ;
const char expected [ ] = " int foo ( ) { \n "
" int i ; i = 0 ; \n "
" if ( x ) { i = 10 ; } \n "
" return i ; \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariablesBailOutAssign2 ( ) {
2011-08-21 17:49:00 +02:00
// ticket #3032 - assignment in condition
const char code [ ] = " void f(struct ABC *list) { \n "
" struct ABC *last = NULL; \n "
" nr = (last = list->prev)->nr; \n " // <- don't replace "last" with 0
" } \n " ;
const char expected [ ] = " void f ( struct ABC * list ) { \n "
" struct ABC * last ; last = 0 ; \n "
" nr = ( last = list . prev ) . nr ; \n "
" } " ;
2012-12-10 06:08:33 +01:00
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariablesBailOutAssign3 ( ) { // #4395 - nested assignments
2012-12-10 06:08:33 +01:00
const char code [ ] = " void f() { \n "
" int *p = 0; \n "
" a = p = (VdbeCursor*)pMem->z; \n "
" return p ; \n "
" } \n " ;
const char expected [ ] = " void f ( ) { \n "
" int * p ; p = 0 ; \n "
" a = p = pMem . z ; \n "
" return p ; \n "
" } " ;
2011-08-21 17:49:00 +02:00
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariablesBailOutFor1 ( ) {
2010-11-07 08:10:09 +01:00
const char code [ ] = " void foo() { \n "
" for (int i = 0; i < 10; ++i) { } \n "
" } \n " ;
const char expected [ ] = " void foo ( ) { \n "
" for ( int i = 0 ; i < 10 ; ++ i ) { } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
2012-09-17 13:18:19 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ; // debug warnings
2010-11-07 08:10:09 +01:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariablesBailOutFor2 ( ) {
2010-11-07 10:42:08 +01:00
const char code [ ] = " void foo() { \n "
" int i = 0; \n "
" while (i < 10) { ++i; } \n "
" } \n " ;
const char expected [ ] = " void foo ( ) { \n "
" int i ; i = 0 ; \n "
" while ( i < 10 ) { ++ i ; } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
2012-09-17 13:18:19 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ; // debug warnings
2010-11-07 10:42:08 +01:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariablesBailOutFor3 ( ) {
2010-11-13 16:40:04 +01:00
const char code [ ] = " void foo() { \n "
" for (std::string::size_type pos = 0; pos < 10; ++pos) \n "
" { } \n "
" } \n " ;
const char expected [ ] = " void foo ( ) { \n "
" for ( std :: string :: size_type pos = 0 ; pos < 10 ; ++ pos ) \n "
" { } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
2012-09-17 13:18:19 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ; // debug warnings
2010-11-13 16:40:04 +01:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariablesBailOutMemberFunction ( ) {
2010-11-06 19:42:38 +01:00
const char code [ ] = " void foo(obj a) { \n "
" obj b = a; \n "
" b.f(); \n "
" } \n " ;
const char expected [ ] = " void foo ( obj a ) { \n "
" obj b ; b = a ; \n "
" b . f ( ) ; \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariablesBailOutConditionalIncrement ( ) {
2010-11-21 13:40:42 +01:00
const char code [ ] = " int f() { \n "
" int a = 0; \n "
" if (x) { \n "
2012-09-05 19:46:44 +02:00
" ++a; \n " // conditional increment
2010-11-21 13:40:42 +01:00
" } \n "
" return a; \n "
" } \n " ;
tokenizeAndStringify ( code , true ) ;
2012-09-17 13:18:19 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ; // no debug warnings
2010-11-21 13:40:42 +01:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariablesBailOutSwitchBreak ( ) {
2010-12-18 11:14:31 +01:00
// Ticket #2324
const char code [ ] = " int f(char *x) { \n "
" char *p; \n "
" char *q; \n "
" \n "
" switch (x & 0x3) \n "
" { \n "
" case 1: \n "
" p = x; \n "
" x = p; \n "
" break; \n "
" case 2: \n "
2012-09-05 19:46:44 +02:00
" q = x; \n " // x is not equal with p
2010-12-18 11:14:31 +01:00
" x = q; \n "
" break; \n "
" } \n "
" } \n " ;
const char expected [ ] = " int f ( char * x ) { \n "
" char * p ; \n "
" char * q ; \n "
" \n "
" switch ( x & 3 ) \n "
" { \n "
" case 1 : ; \n "
" p = x ; \n "
" x = p ; \n "
" break ; \n "
" case 2 : ; \n "
" q = x ; \n "
" x = q ; \n "
" break ; \n "
" } \n "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariablesFloat ( ) {
2011-01-15 08:48:42 +01:00
// Ticket #2454
const char code [ ] = " void f() { \n "
" float a = 40; \n "
" x(10 / a); \n "
" } \n " ;
2011-12-02 23:01:55 +01:00
const char expected [ ] = " void f ( ) { \n \n x ( 0.25 ) ; \n } " ;
2011-01-15 08:48:42 +01:00
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
2012-11-29 09:29:25 +01:00
// Ticket #4227
const char code2 [ ] = " double f() { "
" double a = false; "
" return a; "
" } " ;
ASSERT_EQUALS ( " double f ( ) { return 0.0 ; } " , tokenizeAndStringify ( code2 , true ) ) ;
2014-05-22 17:40:15 +02:00
// Ticket #5485
const char code3 [ ] = " void f() { "
" double a = 1e+007; \n "
" std::cout << a; \n "
" } " ;
ASSERT_EQUALS ( " void f ( ) { \n std :: cout << 1e+007 ; \n } " , tokenizeAndStringify ( code3 , true ) ) ;
const char code4 [ ] = " void f() { "
" double a = 1; \n "
" std::cout << a; \n "
" } " ;
ASSERT_EQUALS ( " void f ( ) { \n std :: cout << 1.0 ; \n } " , tokenizeAndStringify ( code4 , true ) ) ;
2011-01-15 08:48:42 +01:00
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariablesFunctionCalls ( ) {
2012-06-24 13:39:14 +02:00
{
const char code [ ] = " void a(int x); " // <- x is passed by value
" void b() { "
" int x = 123; "
2012-09-17 13:18:19 +02:00
" a(x); " // <- replace with a(123);
2012-06-24 13:39:14 +02:00
" } " ;
const char expected [ ] = " void a ( int x ) ; void b ( ) { a ( 123 ) ; } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
{
2012-09-17 13:18:19 +02:00
const char code [ ] = " void a(int &x); " // <- x is passed by reference
2012-06-24 13:39:14 +02:00
" void b() { "
" int x = 123; "
2012-09-17 13:18:19 +02:00
" a(x); " // <- don't replace with a(123);
2012-06-24 13:39:14 +02:00
" } " ;
const char expected [ ] = " void a ( int & x ) ; void b ( ) { int x ; x = 123 ; a ( x ) ; } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariablesReturn ( ) {
2013-01-19 13:39:23 +01:00
const char code [ ] = " int a() { "
" int x = 123; "
" return (x); "
" } " ;
ASSERT_EQUALS ( " int a ( ) { return 123 ; } " , tokenizeAndStringify ( code , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyKnownVariablesClassMember ( ) {
2011-06-23 16:58:01 +02:00
// Ticket #2815
2011-06-23 17:03:14 +02:00
{
const char code [ ] = " char *a; \n "
" void f(const char *s) { \n "
" a = NULL; \n "
" x(); \n "
" memcpy(a, s, 10); \n " // <- don't simplify "a" here
" } \n " ;
const std : : string s ( tokenizeAndStringify ( code , true ) ) ;
ASSERT_EQUALS ( true , s . find ( " memcpy ( a , s , 10 ) ; " ) ! = std : : string : : npos ) ;
}
2011-06-23 16:58:01 +02:00
2011-06-23 17:03:14 +02:00
// If the variable is local then perform simplification..
{
const char code [ ] = " void f(const char *s) { \n "
" char *a = NULL; \n "
" x(); \n "
" memcpy(a, s, 10); \n " // <- simplify "a"
" } \n " ;
const std : : string s ( tokenizeAndStringify ( code , true ) ) ;
TODO_ASSERT_EQUALS ( true , false , s . find ( " memcpy ( 0 , s , 10 ) ; " ) ! = std : : string : : npos ) ;
}
2011-06-23 16:58:01 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyExternC ( ) {
2012-04-16 16:25:04 +02:00
ASSERT_EQUALS ( " int foo ( ) ; " , tokenizeAndStringify ( " extern \" C \" int foo(); " ) ) ;
ASSERT_EQUALS ( " int foo ( ) ; " , tokenizeAndStringify ( " extern \" C \" { int foo(); } " ) ) ;
}
2011-01-15 08:48:42 +01:00
2014-11-20 14:20:09 +01:00
void file1 ( ) {
2009-01-19 19:40:24 +01:00
const char code [ ] = " a1 \n "
" #file \" b \" \n "
" b1 \n "
" b2 \n "
" #endfile \n "
" a3 \n " ;
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
2009-01-19 19:40:24 +01:00
// tokenize..
2015-10-07 18:33:57 +02:00
Tokenizer tokenizer ( & settings0 , this ) ;
2009-01-19 19:40:24 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " a " ) ;
2011-10-13 20:53:06 +02:00
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) ) {
2009-01-19 19:40:24 +01:00
std : : ostringstream ostr ;
ostr < < char ( ' a ' + tok - > fileIndex ( ) ) < < tok - > linenr ( ) ;
ASSERT_EQUALS ( tok - > str ( ) , ostr . str ( ) ) ;
}
}
2009-01-20 18:05:42 +01:00
2014-11-20 14:20:09 +01:00
void file2 ( ) {
2009-01-20 18:05:42 +01:00
const char code [ ] = " a1 \n "
" #file \" b \" \n "
" b1 \n "
" b2 \n "
" #file \" c \" \n "
" c1 \n "
" c2 \n "
" #endfile \n "
" b4 \n "
" #endfile \n "
" a3 \n "
" #file \" d \" \n "
" d1 \n "
" #endfile \n "
" a5 \n " ;
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
2009-01-20 18:05:42 +01:00
// tokenize..
2015-10-07 18:33:57 +02:00
Tokenizer tokenizer ( & settings0 , this ) ;
2009-01-20 18:05:42 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " a " ) ;
2011-10-13 20:53:06 +02:00
for ( const Token * tok = tokenizer . tokens ( ) ; tok ; tok = tok - > next ( ) ) {
2009-01-20 18:05:42 +01:00
std : : ostringstream ostr ;
ostr < < char ( ' a ' + tok - > fileIndex ( ) ) < < tok - > linenr ( ) ;
ASSERT_EQUALS ( tok - > str ( ) , ostr . str ( ) ) ;
}
}
2009-01-20 18:26:16 +01:00
2014-11-20 14:20:09 +01:00
void file3 ( ) {
2009-02-15 12:42:04 +01:00
const char code [ ] = " #file \" c: \\ a.h \" \n "
" 123 \n "
" #endfile \n " ;
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
2009-02-15 12:42:04 +01:00
// tokenize..
2015-10-07 18:33:57 +02:00
Tokenizer tokenizer ( & settings0 , this ) ;
2009-02-15 12:42:04 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " a.cpp " ) ;
2012-05-06 13:01:56 +02:00
ASSERT_EQUALS ( Path : : toNativeSeparators ( " [c: \\ a.h:1] " ) , tokenizer . list . fileLine ( tokenizer . tokens ( ) ) ) ;
2009-02-15 12:42:04 +01:00
}
2014-11-20 14:20:09 +01:00
void doublesharp ( ) {
2011-12-10 23:49:56 +01:00
const char code [ ] = " a##_##b TEST(var,val) var##_##val = val \n " ;
2015-10-07 18:33:57 +02:00
ASSERT_EQUALS ( " a_b TEST ( var , val ) var_val = val " , tokenizeAndStringify ( code ) ) ;
2009-01-20 18:26:16 +01:00
}
2009-01-24 08:56:47 +01:00
2014-11-20 14:20:09 +01:00
void macrodoublesharp ( ) {
2009-02-13 14:33:12 +01:00
const char code [ ] = " DBG(fmt,args...) printf(fmt, ## args) \n " ;
2015-10-07 18:33:57 +02:00
ASSERT_EQUALS ( " DBG ( fmt , args . . . ) printf ( fmt , ## args ) " , tokenizeAndStringify ( code ) ) ;
2009-02-13 14:33:12 +01:00
}
2014-11-20 14:20:09 +01:00
void simplifyFunctionParameters ( ) {
2009-12-31 19:31:21 +01:00
{
const char code [ ] = " char a [ ABC ( DEF ) ] ; " ;
2012-07-29 15:11:48 +02:00
ASSERT_EQUALS ( code , tokenizeAndStringify ( code ) ) ;
2009-12-31 19:31:21 +01:00
}
2010-01-01 13:26:54 +01:00
{
const char code [ ] = " module ( a , a , sizeof ( a ) , 0444 ) ; " ;
2012-07-29 15:11:48 +02:00
ASSERT_EQUALS ( " module ( a , a , sizeof ( a ) , 292 ) ; " , tokenizeAndStringify ( code ) ) ;
2010-01-01 13:26:54 +01:00
}
2012-07-29 15:11:48 +02:00
ASSERT_EQUALS ( " void f ( int x ) { } " , tokenizeAndStringify ( " void f(x) int x; { } " ) ) ;
ASSERT_EQUALS ( " void f ( int x , char y ) { } " , tokenizeAndStringify ( " void f(x,y) int x; char y; { } " ) ) ;
ASSERT_EQUALS ( " int main ( int argc , char * argv [ ] ) { } " , tokenizeAndStringify ( " int main(argc,argv) int argc; char *argv[]; { } " ) ) ;
ASSERT_EQUALS ( " int f ( int p , int w , float d ) { } " , tokenizeAndStringify ( " int f(p,w,d) float d; { } " ) ) ;
2009-01-24 08:56:47 +01:00
2014-09-23 16:18:04 +02:00
// #1067 - Not simplified. Feel free to fix so it is simplified correctly but this syntax is obsolescent.
2012-07-29 15:11:48 +02:00
ASSERT_EQUALS ( " int ( * d ( a , b , c ) ) ( ) int a ; int b ; int c ; { } " , tokenizeAndStringify ( " int (*d(a,b,c))()int a,b,c; { } " ) ) ;
2009-01-24 19:21:16 +01:00
{
// This is not a function but the pattern is similar..
2009-12-30 15:12:38 +01:00
const char code [ ] = " void foo() "
" { "
" if (x) "
" int x; "
" { } "
" } " ;
2011-12-02 23:01:55 +01:00
ASSERT_EQUALS ( " void foo ( ) { if ( x ) { } { } } " , tokenizeAndStringify ( code , true ) ) ;
2009-01-24 19:21:16 +01:00
}
2012-04-29 12:58:12 +02:00
// #3770 - Don't segfault and don't change macro argument as if it's a K&R function argument
{
const char code [ ] = " MACRO(a) "
" "
" void f() "
" { "
" SetLanguage(); "
" { "
" } "
" } " ;
2012-07-29 15:11:48 +02:00
ASSERT_EQUALS ( " MACRO ( a ) void f ( ) { SetLanguage ( ) ; { } } " , tokenizeAndStringify ( code ) ) ;
2012-04-29 12:58:12 +02:00
}
2009-01-24 08:56:47 +01:00
}
2009-01-25 20:39:05 +01:00
2014-11-20 14:20:09 +01:00
void simplifyFunctionParameters1 ( ) { // ticket #3721
2012-04-22 17:28:27 +02:00
const char code [ ] = " typedef float ufloat; \n "
" typedef short ftnlen; \n "
" int f(p,w,d,e,len) ufloat *p; ftnlen len; \n "
" { \n "
" } \n " ;
ASSERT_EQUALS ( " int f ( float * p , int w , int d , int e , short len ) \n "
" { \n "
2012-07-29 15:11:48 +02:00
" } " , tokenizeAndStringify ( code ) ) ;
2012-04-22 17:28:27 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyFunctionParameters2 ( ) { // #4430
2012-12-23 08:04:44 +01:00
const char code [ ] = " class Item { "
" int i ; "
" public: "
" Item ( int i ) ; "
" } ; "
" Item :: Item ( int i ) : i ( i ) { } " ;
ASSERT_EQUALS ( code , tokenizeAndStringify ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyFunctionParameters3 ( ) { // #4436
2012-12-27 17:15:38 +01:00
const char code [ ] = " class Item { "
" int i ; "
" int j ; "
" public: "
" Item ( int i , int j ) ; "
" } ; "
" Item :: Item ( int i , int j ) : i ( i ) , j ( j ) { } " ;
ASSERT_EQUALS ( code , tokenizeAndStringify ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyFunctionParametersErrors ( ) {
2012-01-23 16:10:15 +01:00
//same parameters...
2014-03-27 13:15:21 +01:00
ASSERT_THROW ( tokenizeAndStringify ( " void foo(x, x) \n "
" int x; \n "
" int x; \n "
" {} \n " ) , InternalError ) ;
2012-01-23 16:10:15 +01:00
2014-03-27 13:15:21 +01:00
ASSERT_THROW ( tokenizeAndStringify ( " void foo(x, y) \n "
" int x; \n "
" int x; \n "
" {} \n " ) , InternalError ) ;
2012-01-23 16:10:15 +01:00
tokenizeAndStringify ( " void foo(int, int) \n "
" {} \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-07-29 15:11:48 +02:00
// #3848 - Don't hang
tokenizeAndStringify ( " sal_Bool ShapeHasText(sal_uLong, sal_uLong) const { \n "
" return sal_True; \n "
" } \n "
" void CreateSdrOLEFromStorage() { \n "
" comphelper::EmbeddedObjectContainer aCnt( xDestStorage ); \n "
" { } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-01-23 16:10:15 +01:00
}
2009-01-25 20:39:05 +01:00
// Simplify "((..))" into "(..)"
2014-11-20 14:20:09 +01:00
void removeParentheses1 ( ) {
2011-05-01 07:50:19 +02:00
const char code [ ] = " void foo() "
" { "
" free(((void*)p)); "
2009-01-25 20:39:05 +01:00
" } " ;
2011-05-01 07:50:19 +02:00
ASSERT_EQUALS ( " void foo ( ) { free ( p ) ; } " , tokenizeAndStringify ( code , true ) ) ;
2009-01-25 20:39:05 +01:00
}
2014-11-20 14:20:09 +01:00
void removeParentheses3 ( ) {
2009-05-29 23:04:01 +02:00
{
2011-05-01 07:50:19 +02:00
const char code [ ] = " void foo() "
" { "
" if (( true )==(true)){} "
2009-05-29 23:04:01 +02:00
" } " ;
2011-11-12 12:01:31 +01:00
ASSERT_EQUALS ( " void foo ( ) { } " , tokenizeAndStringify ( code , true ) ) ;
2009-05-29 23:04:01 +02:00
}
{
2011-05-01 07:50:19 +02:00
const char code [ ] = " void foo() "
" { "
" if (( 2 )==(2)){} "
2009-05-29 23:04:01 +02:00
" } " ;
2011-11-12 12:01:31 +01:00
ASSERT_EQUALS ( " void foo ( ) { } " , tokenizeAndStringify ( code , true ) ) ;
2009-05-29 23:04:01 +02:00
}
2009-05-31 14:55:06 +02:00
{
2011-05-01 07:50:19 +02:00
const char code [ ] = " void foo() "
" { "
" if( g(10)){} "
2009-05-31 14:55:06 +02:00
" } " ;
2011-05-01 07:50:19 +02:00
ASSERT_EQUALS ( " void foo ( ) { if ( g ( 10 ) ) { } } " , tokenizeAndStringify ( code ) ) ;
2009-05-31 14:55:06 +02:00
}
2009-05-28 22:03:36 +02:00
}
2009-02-02 07:21:48 +01:00
2009-06-12 15:04:58 +02:00
// Simplify "( function (..))" into "function (..)"
2014-11-20 14:20:09 +01:00
void removeParentheses4 ( ) {
2011-05-01 07:50:19 +02:00
const char code [ ] = " void foo() "
" { "
" (free(p)); "
2009-06-12 15:04:58 +02:00
" } " ;
2011-05-01 07:50:19 +02:00
ASSERT_EQUALS ( " void foo ( ) { free ( p ) ; } " , tokenizeAndStringify ( code ) ) ;
2009-06-12 15:04:58 +02:00
}
2014-11-20 14:20:09 +01:00
void removeParentheses5 ( ) {
2009-06-12 16:14:01 +02:00
// Simplify "( delete x )" into "delete x"
{
2011-05-01 07:50:19 +02:00
const char code [ ] = " void foo() "
" { "
" (delete p); "
2009-06-12 16:14:01 +02:00
" } " ;
2011-05-01 07:50:19 +02:00
ASSERT_EQUALS ( " void foo ( ) { delete p ; } " , tokenizeAndStringify ( code ) ) ;
2009-06-12 16:14:01 +02:00
}
// Simplify "( delete [] x )" into "delete [] x"
{
2011-05-01 07:50:19 +02:00
const char code [ ] = " void foo() "
" { "
" (delete [] p); "
2009-06-12 16:14:01 +02:00
" } " ;
2011-05-01 07:50:19 +02:00
ASSERT_EQUALS ( " void foo ( ) { delete [ ] p ; } " , tokenizeAndStringify ( code ) ) ;
2009-06-12 16:14:01 +02:00
}
}
2009-11-15 09:28:08 +01:00
// "!(abc.a)" => "!abc.a"
2014-11-20 14:20:09 +01:00
void removeParentheses6 ( ) {
2012-11-04 22:38:18 +01:00
{
const char code [ ] = " (!(abc.a)) " ;
ASSERT_EQUALS ( " ( ! abc . a ) " , tokenizeAndStringify ( code ) ) ;
}
//handle more complex member selections
{
const char code [ ] = " (!(a.b.c.d)); " ;
ASSERT_EQUALS ( " ( ! a . b . c . d ) ; " , tokenizeAndStringify ( code ) ) ;
}
2009-11-15 09:28:08 +01:00
}
2014-11-20 14:20:09 +01:00
void removeParentheses7 ( ) {
2010-09-05 07:53:43 +02:00
const char code [ ] = " ;char *p; (delete(p), (p)=0); " ;
2011-05-01 08:27:59 +02:00
ASSERT_EQUALS ( " ; char * p ; delete p ; p = 0 ; " , tokenizeAndStringify ( code , true ) ) ;
2010-05-07 18:37:50 +02:00
}
2014-11-20 14:20:09 +01:00
void removeParentheses8 ( ) {
2010-07-19 12:06:20 +02:00
const char code [ ] = " struct foo { \n "
" void operator delete(void *obj, size_t sz); \n "
" } \n " ;
2011-09-24 20:51:03 +02:00
const std : : string actual ( tokenizeAndStringify ( code , false , true , Settings : : Win32A ) ) ;
2010-07-19 12:06:20 +02:00
const char expected [ ] = " struct foo { \n "
2011-09-18 16:31:31 +02:00
" void operatordelete ( void * obj , unsigned long sz ) ; \n "
2010-07-19 12:06:20 +02:00
" } " ;
ASSERT_EQUALS ( expected , actual ) ;
}
2014-11-20 14:20:09 +01:00
void removeParentheses9 ( ) {
2010-09-05 07:53:43 +02:00
ASSERT_EQUALS ( " void delete ( double num ) ; " , tokenizeAndStringify ( " void delete(double num); " , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void removeParentheses10 ( ) {
2010-12-18 09:44:58 +01:00
ASSERT_EQUALS ( " p = buf + 8 ; " , tokenizeAndStringify ( " p = (buf + 8); " , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void removeParentheses11 ( ) {
2011-01-28 09:19:30 +01:00
// #2502
ASSERT_EQUALS ( " { } x ( ) ; " , tokenizeAndStringify ( " {}(x()); " , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void removeParentheses12 ( ) {
2011-05-01 08:27:59 +02:00
// #2760
ASSERT_EQUALS ( " , x = 0 ; " , tokenizeAndStringify ( " ,(x)=0; " , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void removeParentheses13 ( ) {
2011-11-07 23:40:06 +01:00
ASSERT_EQUALS ( " ; f ( a + b , c ) ; " , tokenizeAndStringify ( " ;f((a+b),c); " , false ) ) ;
ASSERT_EQUALS ( " ; x = y [ a + b ] ; " , tokenizeAndStringify ( " ;x=y[(a+b)]; " , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void removeParentheses14 ( ) {
2015-01-31 20:12:02 +01:00
ASSERT_EQUALS ( " ; if ( ( i & 1 ) == 0 ) { ; } ; " , tokenizeAndStringify ( " ; if ( (i & 1) == 0 ); ; " , false ) ) ;
2011-11-08 22:48:14 +01:00
}
2014-11-20 14:20:09 +01:00
void removeParentheses15 ( ) {
2012-09-06 16:16:29 +02:00
ASSERT_EQUALS ( " a = b ? c : 123 ; " , tokenizeAndStringify ( " a = b ? c : (123); " , false ) ) ;
2014-01-08 20:53:33 +01:00
ASSERT_EQUALS ( " a = b ? c : ( 579 ) ; " , tokenizeAndStringify ( " a = b ? c : ((123)+(456)); " , false ) ) ;
2012-09-08 10:45:00 +02:00
ASSERT_EQUALS ( " a = b ? 123 : c ; " , tokenizeAndStringify ( " a = b ? (123) : c; " , false ) ) ;
2012-12-29 17:13:06 +01:00
// #4316
ASSERT_EQUALS ( " a = b ? c : ( d = 1 , 0 ) ; " , tokenizeAndStringify ( " a = b ? c : (d=1,0); " , false ) ) ;
2012-09-06 16:16:29 +02:00
}
2014-11-20 14:20:09 +01:00
void removeParentheses16 ( ) { // *(x.y)=
2013-02-16 16:07:05 +01:00
// #4423
ASSERT_EQUALS ( " * x = 0 ; " , tokenizeAndStringify ( " *(x)=0; " , false ) ) ;
ASSERT_EQUALS ( " * x . y = 0 ; " , tokenizeAndStringify ( " *(x.y)=0; " , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void removeParentheses17 ( ) { // a ? b : (c > 0 ? d : e)
2014-01-08 20:53:33 +01:00
ASSERT_EQUALS ( " a ? b : ( c > 0 ? d : e ) ; " , tokenizeAndStringify ( " a?b:(c>0?d:e); " , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void removeParentheses18 ( ) {
2015-08-25 21:19:19 +02:00
ASSERT_EQUALS ( " float ( * a ) [ 2 ] ; " , tokenizeAndStringify ( " float(*a)[2]; " , false ) ) ;
2014-04-21 16:14:49 +02:00
}
2014-11-20 14:20:09 +01:00
void removeParentheses19 ( ) {
2014-05-04 18:36:04 +02:00
ASSERT_EQUALS ( " ( ( ( typeof ( X ) ) * ) 0 ) " , tokenizeAndStringify ( " (((typeof(X))*)0) " , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void removeParentheses20 ( ) {
2014-05-11 08:22:28 +02:00
ASSERT_EQUALS ( " a < b < int > > ( 2 ) ; " , tokenizeAndStringify ( " a<b<int>>(2); " , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void removeParentheses21 ( ) {
2014-05-24 19:04:47 +02:00
ASSERT_EQUALS ( " a = ( int ) - b ; " , tokenizeAndStringify ( " a = ((int)-b); " , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void removeParentheses22 ( ) {
2014-06-28 09:36:51 +02:00
static char code [ ] = " struct S { "
" char *(a); "
" char &(b); "
" const static char *(c); "
" } ; " ;
static char exp [ ] = " struct S { "
" char * a ; "
" char & b ; "
2015-08-27 14:34:00 +02:00
" static const char * c ; "
2014-06-28 09:36:51 +02:00
" } ; " ;
ASSERT_EQUALS ( exp , tokenizeAndStringify ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void removeParentheses23 ( ) { // Ticket #6103
2014-09-05 00:31:58 +02:00
// Reported case
{
static char code [ ] = " * * p f ( ) int = { new int ( * [ 2 ] ) ; void } " ;
static char exp [ ] = " * * p f ( ) int = { new int ( * [ 2 ] ) ; void } " ;
ASSERT_EQUALS ( exp , tokenizeAndStringify ( code ) ) ;
}
// Various valid cases
{
static char code [ ] = " int * f [ 1 ] = { new ( int ) } ; " ;
static char exp [ ] = " int * f [ 1 ] = { new int } ; " ;
ASSERT_EQUALS ( exp , tokenizeAndStringify ( code ) ) ;
}
{
static char code [ ] = " int * * f [ 1 ] = { new ( int ) [ 1 ] } ; " ;
static char exp [ ] = " int * * f [ 1 ] = { new int [ 1 ] } ; " ;
ASSERT_EQUALS ( exp , tokenizeAndStringify ( code ) ) ;
}
{
static char code [ ] = " list < int > * f [ 1 ] = { new ( list < int > ) } ; " ;
static char exp [ ] = " list < int > * f [ 1 ] = { new list < int > } ; " ;
ASSERT_EQUALS ( exp , tokenizeAndStringify ( code ) ) ;
}
2014-10-11 09:18:43 +02:00
// don't remove parentheses in operator new overload
{
static char code [ ] = " void *operator new(__SIZE_TYPE__, int); " ;
static char exp [ ] = " void * operatornew ( __SIZE_TYPE__ , int ) ; " ;
ASSERT_EQUALS ( exp , tokenizeAndStringify ( code ) ) ;
}
2014-09-05 00:31:58 +02:00
}
2015-10-13 20:31:17 +02:00
void removeParentheses24 ( ) { // Ticket #7040
static char code [ ] = " std::hash<decltype(t._data)>()(t._data); " ;
static char exp [ ] = " std :: hash < decltype ( t . _data ) > ( ) ( t . _data ) ; " ;
ASSERT_EQUALS ( exp , tokenizeAndStringify ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void tokenize_double ( ) {
2015-10-07 18:33:57 +02:00
const char code [ ] = " void f() { \n "
2009-02-08 10:51:45 +01:00
" double a = 4.2; \n "
" float b = 4.2f; \n "
2009-02-08 11:39:55 +01:00
" double c = 4.2e+10; \n "
" double d = 4.2e-10; \n "
2009-02-08 11:56:20 +01:00
" int e = 4+2; \n "
2015-10-07 18:33:57 +02:00
" } " ;
ASSERT_EQUALS ( " void f ( ) { \n "
" double a ; a = 4.2 ; \n "
" float b ; b = 4.2f ; \n "
" double c ; c = 4.2e+10 ; \n "
" double d ; d = 4.2e-10 ; \n "
" int e ; e = 6 ; \n "
" } " , tokenizeAndStringify ( code ) ) ;
2009-02-08 10:51:45 +01:00
}
2009-02-08 11:25:33 +01:00
2014-11-20 14:20:09 +01:00
void tokenize_strings ( ) {
2015-10-07 18:33:57 +02:00
const char code [ ] = " void f() { \n "
2009-02-08 11:25:33 +01:00
" const char *a = \n "
" { \n "
" \" hello \" \n "
" \" more \" \n "
" \" world \" \n "
" }; \n "
2015-10-07 18:33:57 +02:00
" } " ;
ASSERT_EQUALS ( " void f ( ) { \n "
" const char * a ; a = \n "
" { \n "
" \" hello more world \" \n "
" \n "
" \n "
" } ; \n "
" } " , tokenizeAndStringify ( code ) ) ;
2009-02-08 11:25:33 +01:00
}
2009-02-11 23:15:22 +01:00
2014-11-20 14:20:09 +01:00
void simplify_constants ( ) {
2009-02-11 23:15:22 +01:00
const char code [ ] =
2015-10-07 18:33:57 +02:00
" void f() { \n "
2009-02-11 23:15:22 +01:00
" const int a = 45; \n "
2009-02-12 20:26:42 +01:00
" if( a ) \n "
" { int b = a; } \n "
2009-02-11 23:15:22 +01:00
" } \n "
2015-10-07 18:33:57 +02:00
" void g() { \n "
2009-02-11 23:15:22 +01:00
" int a = 2; \n "
2015-10-07 18:33:57 +02:00
" } " ;
ASSERT_EQUALS ( " void f ( ) { \n "
" \n "
" \n "
" \n "
" } \n "
" void g ( ) { \n "
" \n "
" } " , tokenizeAndStringify ( code , true ) ) ;
2009-02-11 23:15:22 +01:00
}
2009-02-14 22:33:28 +01:00
2014-11-20 14:20:09 +01:00
void simplify_constants2 ( ) {
2009-02-14 22:33:28 +01:00
const char code [ ] =
2015-10-07 18:33:57 +02:00
" void f( Foo &foo, Foo *foo2 ) { \n "
2009-02-14 22:33:28 +01:00
" const int a = 45; \n "
" foo.a=a+a; \n "
" foo2->a=a; \n "
2015-10-07 18:33:57 +02:00
" } " ;
ASSERT_EQUALS ( " void f ( Foo & foo , Foo * foo2 ) { \n "
" \n "
" foo . a = 90 ; \n "
" foo2 . a = 45 ; \n "
" } " , tokenizeAndStringify ( code , true ) ) ;
2009-02-14 22:33:28 +01:00
}
2009-03-13 22:38:42 +01:00
2014-11-20 14:20:09 +01:00
void simplify_constants3 ( ) {
2011-01-29 16:14:09 +01:00
const char code [ ] =
" static const char str[] = \" abcd \" ; \n "
" static const unsigned int SZ = sizeof(str); \n "
" void f() { \n "
" a = SZ; \n "
" } \n " ;
const char expected [ ] =
2015-08-27 14:34:00 +02:00
" static const char str [ 5 ] = \" abcd \" ; \n \n void f ( ) { \n a = 5 ; \n } " ;
2011-01-29 16:14:09 +01:00
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplify_constants4 ( ) {
2012-10-17 00:25:20 +02:00
const char code [ ] = " static const int bSize = 4; \n "
" static const int aSize = 50; \n "
2013-01-19 21:13:51 +01:00
" x = bSize; \n "
" y = aSize; \n " ;
ASSERT_EQUALS ( " x = 4 ; \n y = 50 ; " , tokenizeAndStringify ( code , true ) ) ;
2012-10-17 00:25:20 +02:00
}
2014-11-20 14:20:09 +01:00
void simplify_constants5 ( ) {
2013-06-25 06:37:51 +02:00
const char code [ ] = " int buffer[10]; \n "
" static const int NELEMS = sizeof(buffer)/sizeof(int); \n "
" static const int NELEMS2(sizeof(buffer)/sizeof(int)); \n "
" x = NELEMS; \n "
" y = NELEMS2; \n " ;
ASSERT_EQUALS ( " int buffer [ 10 ] ; \n \n \n x = 10 ; \n y = 10 ; " , tokenizeAndStringify ( code , true ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplify_constants6 ( ) { // Ticket #5625
2014-10-11 17:01:08 +02:00
{
const char code [ ] = " template < class T > struct foo ; \n "
" void bar ( ) { \n "
" foo < 1 ? 0 ? 1 : 6 : 2 > x ; \n "
" foo < 1 ? 0 : 2 > y ; \n "
" } " ;
const char exp [ ] = " template < class T > struct foo ; \n "
" void bar ( ) { \n "
" foo < 6 > x ; \n "
" foo < 0 > y ; \n "
" } " ;
ASSERT_EQUALS ( exp , tokenizeAndStringify ( code , true ) ) ;
}
{
const char code [ ] = " bool b = true ? false : 1 > 2 ; " ;
const char exp [ ] = " bool b ; b = false ; " ;
ASSERT_EQUALS ( exp , tokenizeAndStringify ( code , true ) ) ;
}
2014-10-04 20:49:57 +02:00
}
2015-01-31 20:12:02 +01:00
void simplifyNull ( ) {
2012-08-25 12:00:25 +02:00
{
const char code [ ] =
" int * p = NULL; \n "
" int * q = __null; \n " ;
const char expected [ ] =
" int * p ; p = 0 ; \n int * q ; q = 0 ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true ) ) ;
}
ASSERT_EQUALS ( " ( a == nullptr ) " , tokenizeAndStringify ( " (a==nullptr) " , false , false , Settings : : Unspecified , " test.c " ) ) ;
2015-01-31 20:12:02 +01:00
ASSERT_EQUALS ( " ( a == 0 ) " , tokenizeAndStringify ( " (a==nullptr) " , false , false , Settings : : Unspecified , " test.cpp " ) ) ;
ASSERT_EQUALS ( " if ( p == 0 ) " , tokenizeAndStringify ( " if (p==NULL) " ) ) ;
ASSERT_EQUALS ( " f ( NULL ) ; " , tokenizeAndStringify ( " f(NULL); " ) ) ;
ASSERT_EQUALS ( " char * i ; i = 0 ; " , tokenizeAndStringify ( " char* i = (NULL); " ) ) ;
2011-02-05 09:03:31 +01:00
}
2014-11-20 14:20:09 +01:00
void simplifyMulAndParens ( ) {
2011-05-18 07:25:30 +02:00
// (error) Resource leak
2011-12-09 20:47:51 +01:00
const char code [ ] = " void f() { "
" *&n1=open(); "
" *&(n2)=open(); "
" *(&n3)=open(); "
" *&*&n4=open(); "
" *&*&*&(n5)=open(); "
" *&*&(*&n6)=open(); "
" *&*(&*&n7)=open(); "
" *(&*&n8)=open(); "
" *&(*&*&(*&n9))=open(); "
" (n10) = open(); "
" ((n11)) = open(); "
" ((*&n12))=open(); "
" *(&(*&n13))=open(); "
" ((*&(*&n14)))=open(); "
" ((*&(*&n15)))+=10; "
" } " ;
const char expected [ ] = " void f ( ) { "
" n1 = open ( ) ; "
" n2 = open ( ) ; "
" n3 = open ( ) ; "
" n4 = open ( ) ; "
" n5 = open ( ) ; "
" n6 = open ( ) ; "
" n7 = open ( ) ; "
" n8 = open ( ) ; "
" n9 = open ( ) ; "
" n10 = open ( ) ; "
" n11 = open ( ) ; "
" n12 = open ( ) ; "
" n13 = open ( ) ; "
" n14 = open ( ) ; "
" n15 = n15 + 10 ; "
" } " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
2011-05-18 07:25:30 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyStructDecl ( ) {
2013-01-07 19:20:15 +01:00
const char code [ ] = " const struct A { int a; int b; } a; " ;
ASSERT_EQUALS ( " struct A { int a ; int b ; } ; const struct A a ; " , tokenizeAndStringify ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void vardecl1 ( ) {
2009-03-16 19:03:23 +01:00
const char code [ ] = " unsigned int a, b; " ;
2009-03-18 20:32:05 +01:00
const std : : string actual ( tokenizeAndStringify ( code ) ) ;
2009-03-16 19:03:23 +01:00
2009-03-18 20:32:05 +01:00
ASSERT_EQUALS ( " unsigned int a ; unsigned int b ; " , actual ) ;
2009-03-16 19:03:23 +01:00
}
2014-11-20 14:20:09 +01:00
void vardecl2 ( ) {
2009-03-16 19:03:23 +01:00
const char code [ ] = " void foo(a,b) unsigned int a, b; { } " ;
2009-03-18 20:32:05 +01:00
const std : : string actual ( tokenizeAndStringify ( code ) ) ;
2009-03-16 19:03:23 +01:00
2011-11-05 12:23:05 +01:00
ASSERT_EQUALS ( " void foo ( unsigned int a , unsigned int b ) { } " , actual ) ;
2009-03-16 19:03:23 +01:00
}
2009-04-20 20:38:05 +02:00
2014-11-20 14:20:09 +01:00
void vardecl3 ( ) {
2009-06-14 14:57:47 +02:00
const char code [ ] = " void f() { char * p = foo<10,char>(); } " ;
const std : : string actual ( tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " void f ( ) { char * p ; p = foo < 10 , char > ( ) ; } " , actual ) ;
}
2014-11-20 14:20:09 +01:00
void vardecl4 ( ) {
2009-06-14 19:32:34 +02:00
// ticket #346
const char code1 [ ] = " void *p = NULL; " ;
2011-01-06 20:16:14 +01:00
const char res1 [ ] = " void * p ; p = 0 ; " ;
2009-06-14 19:32:34 +02:00
ASSERT_EQUALS ( res1 , tokenizeAndStringify ( code1 ) ) ;
const char code2 [ ] = " const void *p = NULL; " ;
2011-01-06 20:16:14 +01:00
const char res2 [ ] = " const void * p ; p = 0 ; " ;
2009-06-14 19:32:34 +02:00
ASSERT_EQUALS ( res2 , tokenizeAndStringify ( code2 ) ) ;
const char code3 [ ] = " void * const p = NULL; " ;
2011-01-06 20:16:14 +01:00
const char res3 [ ] = " void * const p ; p = 0 ; " ;
2009-06-14 19:32:34 +02:00
ASSERT_EQUALS ( res3 , tokenizeAndStringify ( code3 ) ) ;
const char code4 [ ] = " const void * const p = NULL; " ;
2011-01-06 20:16:14 +01:00
const char res4 [ ] = " const void * const p ; p = 0 ; " ;
2009-06-14 19:32:34 +02:00
ASSERT_EQUALS ( res4 , tokenizeAndStringify ( code4 ) ) ;
}
2015-10-15 10:31:08 +02:00
void vardecl5 ( ) {
ASSERT_EQUALS ( " void foo ( int nX ) { \n "
" int addI ; addI = frontPoint == 2 || frontPoint == 1 ? ( i = 0 , 1 ) : ( i = nX - 2 , -1 ) ; \n "
" } " , tokenizeAndStringify ( " void foo(int nX) { \n "
" int addI = frontPoint == 2 || frontPoint == 1 ? i = 0, 1 : (i = nX - 2, -1); \n "
" } " ) ) ;
}
2014-11-20 14:20:09 +01:00
void vardecl_stl_1 ( ) {
2009-07-28 21:46:33 +02:00
// ticket #520
const char code1 [ ] = " std::vector<std::string>a, b; " ;
const char res1 [ ] = " std :: vector < std :: string > a ; std :: vector < std :: string > b ; " ;
ASSERT_EQUALS ( res1 , tokenizeAndStringify ( code1 ) ) ;
const char code2 [ ] = " std::vector<std::string>::const_iterator it, cit; " ;
const char res2 [ ] = " std :: vector < std :: string > :: const_iterator it ; std :: vector < std :: string > :: const_iterator cit ; " ;
ASSERT_EQUALS ( res2 , tokenizeAndStringify ( code2 ) ) ;
const char code3 [ ] = " std::vector<std::pair<std::string, std::string > > *c, d; " ;
const char res3 [ ] = " std :: vector < std :: pair < std :: string , std :: string > > * c ; std :: vector < std :: pair < std :: string , std :: string > > d ; " ;
ASSERT_EQUALS ( res3 , tokenizeAndStringify ( code3 ) ) ;
}
2014-11-20 14:20:09 +01:00
void vardecl_stl_2 ( ) {
2010-11-13 15:45:33 +01:00
const char code1 [ ] = " { std::string x = \" abc \" ; } " ;
2011-03-23 12:48:18 +01:00
ASSERT_EQUALS ( " { std :: string x ; x = \" abc \" ; } " , tokenizeAndStringify ( code1 ) ) ;
2010-11-13 15:45:33 +01:00
const char code2 [ ] = " { std::vector<int> x = y; } " ;
2011-03-24 02:15:49 +01:00
ASSERT_EQUALS ( " { std :: vector < int > x ; x = y ; } " , tokenizeAndStringify ( code2 ) ) ;
2010-11-13 15:45:33 +01:00
}
2014-11-20 14:20:09 +01:00
void vardecl_template_1 ( ) {
2009-12-05 21:15:14 +01:00
// ticket #1046
const char code1 [ ] = " b<(1<<24),10,24> u, v; " ;
2012-09-08 07:01:35 +02:00
const char res1 [ ] = " b < 16777216 , 10 , 24 > u ; b < 16777216 , 10 , 24 > v ; " ;
2009-12-05 21:15:14 +01:00
ASSERT_EQUALS ( res1 , tokenizeAndStringify ( code1 ) ) ;
2012-01-31 18:41:57 +01:00
// ticket #3571 (segmentation fault)
tokenizeAndStringify ( " template <int i = (3>4) > class X4 {}; " ) ;
2009-12-05 21:15:14 +01:00
}
2014-11-20 14:20:09 +01:00
void vardecl_template_2 ( ) {
2012-04-05 08:53:10 +02:00
// ticket #3650
const char code [ ] = " const string str = x<8,int>(); " ;
const char expected [ ] = " const string str = x < 8 , int > ( ) ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void vardecl_union ( ) {
2010-09-03 08:10:29 +02:00
// ticket #1976
const char code1 [ ] = " class Fred { public: union { int a ; int b ; } ; } ; " ;
ASSERT_EQUALS ( code1 , tokenizeAndStringify ( code1 ) ) ;
2010-09-09 20:15:00 +02:00
// ticket #2039
const char code2 [ ] = " void f() { \n "
" union { \n "
" int x; \n "
" long y; \n "
" }; \n "
" } " ;
ASSERT_EQUALS ( " void f ( ) { \n \n int x ; \n long & y = x ; \n \n } " , tokenizeAndStringify ( code2 ) ) ;
2012-07-08 13:59:00 +02:00
// ticket #3927
const char code3 [ ] = " union xy *p = NULL; " ;
ASSERT_EQUALS ( " union xy * p ; p = 0 ; " , tokenizeAndStringify ( code3 ) ) ;
2010-09-03 08:10:29 +02:00
}
2014-11-20 14:20:09 +01:00
void vardecl_par ( ) {
2011-05-07 14:23:14 +02:00
// ticket #2743 - set links if variable type contains parentheses
const char code [ ] = " Fred<int(*)()> fred1=a, fred2=b; " ;
2015-10-08 11:35:51 +02:00
ASSERT_EQUALS ( " Fred < int ( * ) ( ) > fred1 ; fred1 = a ; Fred < int ( * ) ( ) > fred2 ; fred2 = b ; " , tokenizeAndStringify ( code ) ) ;
2011-05-07 14:23:14 +02:00
}
2014-11-20 14:20:09 +01:00
void vardecl_par2 ( ) {
2012-07-03 06:34:14 +02:00
// ticket #3912 - set correct links
const char code [ ] = " function<void (shared_ptr<MyClass>)> v; " ;
2015-10-08 11:35:51 +02:00
ASSERT_EQUALS ( " function < void ( shared_ptr < MyClass > ) > v ; " , tokenizeAndStringify ( code ) ) ;
2012-07-03 06:34:14 +02:00
}
2015-03-06 17:30:20 +01:00
void vardecl_par3 ( ) {
// ticket #6556- Fred x1(a), x2(b);
const char code [ ] = " Fred x1(a), x2(b); " ;
ASSERT_EQUALS ( " Fred x1 ( a ) ; Fred x2 ( b ) ; " , tokenizeAndStringify ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void vardec_static ( ) {
2009-10-02 16:28:30 +02:00
{
// don't simplify declarations of static variables
// "static int i = 0;" is not the same as "static int i; i = 0;"
const char code [ ] = " static int i = 0 ; " ;
ASSERT_EQUALS ( code , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " static int a, b; " ;
2009-10-03 12:03:54 +02:00
ASSERT_EQUALS ( " static int a ; static int b ; " , tokenizeAndStringify ( code ) ) ;
2009-10-02 16:28:30 +02:00
}
{
const char code [ ] = " static unsigned int a, b; " ;
2009-10-03 12:03:54 +02:00
ASSERT_EQUALS ( " static unsigned int a ; static unsigned int b ; " , tokenizeAndStringify ( code ) ) ;
2009-10-02 16:28:30 +02:00
}
{
const char code [ ] = " static int a=1, b=1; " ;
2009-10-03 12:03:54 +02:00
ASSERT_EQUALS ( " static int a = 1 ; static int b = 1 ; " , tokenizeAndStringify ( code ) ) ;
2009-10-02 16:28:30 +02:00
}
{
const char code [ ] = " static int *a, *b; " ;
2009-10-03 12:03:54 +02:00
ASSERT_EQUALS ( " static int * a ; static int * b ; " , tokenizeAndStringify ( code ) ) ;
2009-10-02 16:28:30 +02:00
}
{
const char code [ ] = " static unsigned int *a=0, *b=0; " ;
2009-10-03 12:03:54 +02:00
ASSERT_EQUALS ( " static unsigned int * a = 0 ; static unsigned int * b = 0 ; " , tokenizeAndStringify ( code ) ) ;
2009-10-02 16:28:30 +02:00
}
2013-08-24 22:27:54 +02:00
{
2013-10-30 15:48:00 +01:00
// Ticket #4450
2013-08-24 22:27:54 +02:00
const char code [ ] = " static int large_eeprom_type = (13 | (5)), "
" default_flash_type = 42; " ;
ASSERT_EQUALS ( " static int large_eeprom_type = 13 ; static int default_flash_type = 42 ; " ,
tokenizeAndStringify ( code ) ) ;
}
2013-10-30 15:48:00 +01:00
{
// Ticket #5121
const char code [ ] = " unsigned int x; "
" static const unsigned int A = 1, B = A, C = 0, D = (A), E = 0; "
" void f() { "
" unsigned int *foo = &x; "
" } " ;
ASSERT_EQUALS ( " unsigned int x ; "
2015-08-27 14:34:00 +02:00
" static const unsigned int A = 1 ; "
" static const unsigned int B = A ; "
" static const unsigned int C = 0 ; "
" static const unsigned int D = A ; "
" static const unsigned int E = 0 ; "
2013-10-30 15:48:00 +01:00
" void f ( ) { "
" unsigned int * foo ; "
" foo = & x ; "
" } " ,
tokenizeAndStringify ( code ) ) ;
}
2014-01-04 10:49:27 +01:00
{
// Ticket #5266
const char code [ ] = " class Machine { \n "
" static int const STACK_ORDER = 10, STACK_MAX = 1 << STACK_ORDER, "
" STACK_GUARD = 2; \n "
" }; " ;
ASSERT_EQUALS ( " class Machine { \n "
" static const int STACK_ORDER = 10 ; static const int STACK_MAX = 1 << STACK_ORDER ; "
" static const int STACK_GUARD = 2 ; \n "
" } ; " ,
tokenizeAndStringify ( code ) ) ;
}
2009-07-06 11:45:14 +02:00
}
2014-11-20 14:20:09 +01:00
void vardecl6 ( ) {
2009-08-08 12:33:07 +02:00
// ticket #565
const char code1 [ ] = " int z = x >> 16; " ;
const char res1 [ ] = " int z ; z = x >> 16 ; " ;
ASSERT_EQUALS ( res1 , tokenizeAndStringify ( code1 ) ) ;
}
2014-11-20 14:20:09 +01:00
void vardecl7 ( ) {
2009-08-23 08:26:16 +02:00
// ticket #603
2010-08-25 22:10:21 +02:00
const char code [ ] = " void f() { \n "
" for (int c = 0; c < 0; ++c) {} \n "
" int t; \n "
" D(3 > t, \" T \" ); \n "
" } " ;
const char res [ ] = " void f ( ) { \n "
" for ( int c = 0 ; c < 0 ; ++ c ) { } \n "
2009-08-23 08:26:16 +02:00
" int t ; \n "
2010-08-25 22:10:21 +02:00
" D ( 3 > t , \" T \" ) ; \n "
" } " ;
2009-08-23 08:26:16 +02:00
ASSERT_EQUALS ( res , tokenizeAndStringify ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void vardecl8 ( ) {
2009-09-20 12:28:15 +02:00
// ticket #696
const char code [ ] = " char a[10]={' \\ 0'}, b[10]={' \\ 0'}; " ;
2011-01-06 20:16:14 +01:00
const char res [ ] = " char a [ 10 ] = { 0 } ; char b [ 10 ] = { 0 } ; " ;
2009-09-20 12:28:15 +02:00
ASSERT_EQUALS ( res , tokenizeAndStringify ( code ) ) ;
}
2009-08-23 08:26:16 +02:00
2014-11-20 14:20:09 +01:00
void vardecl9 ( ) {
2009-09-20 13:28:56 +02:00
const char code [ ] = " char a[2] = {'A', ' \\ 0'}, b[2] = {'B', ' \\ 0'}; " ;
2011-01-06 20:16:14 +01:00
const char res [ ] = " char a [ 2 ] = { 'A' , 0 } ; char b [ 2 ] = { 'B' , 0 } ; " ;
2009-09-20 13:28:56 +02:00
ASSERT_EQUALS ( res , tokenizeAndStringify ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void vardecl10 ( ) {
2009-09-26 12:02:13 +02:00
// ticket #732
const char code [ ] = " char a [ 2 ] = { '-' } ; memset ( a , '-' , sizeof ( a ) ) ; " ;
ASSERT_EQUALS ( code , tokenizeAndStringify ( code ) ) ;
2010-05-16 20:21:22 +02:00
}
2014-11-20 14:20:09 +01:00
void vardecl11 ( ) {
2010-05-16 20:21:22 +02:00
// ticket #1684
const char code [ ] = " char a[5][8], b[5][8]; " ;
ASSERT_EQUALS ( " char a [ 5 ] [ 8 ] ; char b [ 5 ] [ 8 ] ; " , tokenizeAndStringify ( code ) ) ;
2009-09-26 12:02:13 +02:00
}
2014-11-20 14:20:09 +01:00
void vardecl12 ( ) {
2010-08-25 20:17:31 +02:00
const char code [ ] = " struct A { public: B a, b, c, d; }; " ;
ASSERT_EQUALS ( " struct A { public: B a ; B b ; B c ; B d ; } ; " , tokenizeAndStringify ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void vardecl13 ( ) {
2010-09-15 19:53:47 +02:00
const char code [ ] = " void f() { \n "
" int a = (x < y) ? 1 : 0; \n "
" } " ;
ASSERT_EQUALS ( " void f ( ) { \n int a ; a = ( x < y ) ? 1 : 0 ; \n } " , tokenizeAndStringify ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void vardecl14 ( ) {
2011-03-25 04:06:20 +01:00
const char code [ ] = " ::std::tr1::shared_ptr<int> pNum1, pNum2; \n " ;
2012-08-25 21:57:45 +02:00
ASSERT_EQUALS ( " :: std :: tr1 :: shared_ptr < int > pNum1 ; :: std :: tr1 :: shared_ptr < int > pNum2 ; " , tokenizeAndStringify ( code , false , false , Settings : : Unspecified , " test.cpp " , false ) ) ;
2011-03-25 04:06:20 +01:00
}
2014-11-20 14:20:09 +01:00
void vardecl15 ( ) {
2012-01-30 23:41:43 +01:00
const char code [ ] = " const char x[] = \" foo \" , y[] = \" bar \" ; \n " ;
ASSERT_EQUALS ( " const char x [ 4 ] = \" foo \" ; const char y [ 4 ] = \" bar \" ; " , tokenizeAndStringify ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void vardecl16 ( ) {
2012-11-05 00:33:02 +01:00
{
const char code [ ] = " const a::b<c,d(e),f>::g::h<i>::l *x [] = foo(),y [][] = bar(); \n " ;
ASSERT_EQUALS ( " const a :: b < c , d ( e ) , f > :: g :: h < i > :: l * x [ ] = foo ( ) ; "
" const a :: b < c , d ( e ) , f > :: g :: h < i > :: l y [ ] [ ] = bar ( ) ; " , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " const ::b<c,d(e),f>::g::h<i>::l *x [] = foo(),y [][] = bar(); \n " ;
ASSERT_EQUALS ( " const :: b < c , d ( e ) , f > :: g :: h < i > :: l * x [ ] = foo ( ) ; "
" const :: b < c , d ( e ) , f > :: g :: h < i > :: l y [ ] [ ] = bar ( ) ; " , tokenizeAndStringify ( code ) ) ;
}
2012-01-30 23:41:43 +01:00
}
2014-11-20 14:20:09 +01:00
void vardecl17 ( ) {
2012-01-30 23:41:43 +01:00
const char code [ ] = " a < b > :: c :: d :: e < f > x = foo(), y = bar(); \n " ;
ASSERT_EQUALS ( " a < b > :: c :: d :: e < f > x ; x = foo ( ) ; "
" a < b > :: c :: d :: e < f > y ; y = bar ( ) ; " , tokenizeAndStringify ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void vardecl18 ( ) {
2012-02-18 15:05:29 +01:00
const char code [ ] = " void f() { \n "
" g((double)v1*v2, v3, v4); \n "
" } \n " ;
ASSERT_EQUALS ( " void f ( ) { \n "
" g ( ( double ) v1 * v2 , v3 , v4 ) ; \n "
" } " , tokenizeAndStringify ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void vardecl19 ( ) {
2012-04-03 19:48:58 +02:00
{
const char code [ ] = " void func(in, r, m) \n "
" int in; "
" int r,m; "
" { \n "
" } \n " ;
2012-03-31 18:45:29 +02:00
2012-04-03 19:48:58 +02:00
ASSERT_EQUALS ( " void func ( \n "
2012-04-16 19:51:07 +02:00
" int in , \n "
" int r , \n "
" int m ) \n "
2012-04-03 19:48:58 +02:00
" { \n "
" } " , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " void f(r,f) \n "
" char *r; \n "
" { \n "
" } \n " ;
ASSERT_EQUALS ( " void f ( \n "
2012-04-16 19:51:07 +02:00
" char * r ) \n "
2012-04-03 19:48:58 +02:00
" \n "
" { \n "
" } " , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " void f(f) \n "
" { \n "
" } \n " ;
ASSERT_EQUALS ( " void f ( ) \n "
" { \n "
" } " , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " void f(f,r) \n "
" char *r; \n "
" { \n "
" } \n " ;
ASSERT_EQUALS ( " void f ( \n "
2012-04-16 19:51:07 +02:00
" char * r ) \n "
2012-04-03 19:48:58 +02:00
" \n "
" { \n "
" } " , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " void f(r,f,s) \n "
" char *r; \n "
" char *s; \n "
" { \n "
" } \n " ;
ASSERT_EQUALS ( " void f ( \n "
2012-04-16 19:51:07 +02:00
" char * r , \n "
2012-04-03 19:48:58 +02:00
" \n "
2012-04-16 19:51:07 +02:00
" char * s ) \n "
2012-04-03 19:48:58 +02:00
" \n "
" \n "
" { \n "
" } " , tokenizeAndStringify ( code ) ) ;
}
2012-04-03 20:12:34 +02:00
{
const char code [ ] = " void f(r,s,t) \n "
" char *r,*s,*t; \n "
" { \n "
" } \n " ;
ASSERT_EQUALS ( " void f ( \n "
2012-04-16 19:51:07 +02:00
" char * r , \n "
" char * s , \n "
" char * t ) \n "
2012-04-03 20:12:34 +02:00
" \n "
" { \n "
" } " , tokenizeAndStringify ( code ) ) ;
}
2012-04-13 00:39:40 +02:00
{
const char code [ ] = " void f(a, b) register char *a, *b; \n "
" { \n "
" } \n " ;
ASSERT_EQUALS ( " void f ( char * a , char * b ) \n "
" { \n "
" } " , tokenizeAndStringify ( code ) ) ;
}
2012-03-31 18:45:29 +02:00
}
2014-11-20 14:20:09 +01:00
void vardecl20 ( ) {
2012-04-09 12:55:26 +02:00
// #3700
const char code [ ] = " void a::b() const \n "
" { \n "
" register const int X = 0; \n "
" } \n " ;
ASSERT_EQUALS ( " void a :: b ( ) const \n "
" { \n "
" const int X = 0 ; \n "
" } " , tokenizeAndStringify ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void vardecl21 ( ) { // type in namespace
2012-10-15 06:53:38 +02:00
// #4042 - a::b const *p = 0;
const char code1 [ ] = " void f() { \n "
" a::b const *p = 0; \n "
" } \n " ;
2012-09-14 16:46:45 +02:00
ASSERT_EQUALS ( " void f ( ) { \n "
" a :: b const * p ; p = 0 ; \n "
" } "
2012-10-15 06:53:38 +02:00
, tokenizeAndStringify ( code1 ) ) ;
// #4226 - ::a::b const *p = 0;
const char code2 [ ] = " void f() { \n "
" ::a::b const *p = 0; \n "
" } \n " ;
ASSERT_EQUALS ( " void f ( ) { \n "
" :: a :: b const * p ; p = 0 ; \n "
" } "
, tokenizeAndStringify ( code2 ) ) ;
2012-09-14 16:46:45 +02:00
}
2014-11-20 14:20:09 +01:00
void vardecl22 ( ) { // #4211 - segmentation fault
2012-09-17 19:45:42 +02:00
tokenizeAndStringify ( " A<B<C<int>> >* p = 0; " ) ;
}
2014-11-20 14:20:09 +01:00
void vardecl23 ( ) { // #4276 - segmentation fault
2012-10-20 21:39:29 +02:00
tokenizeAndStringify ( " class a { protected : template < class int x = 1 ; public : int f ( ) ; } " ) ;
}
2014-11-20 14:20:09 +01:00
void vardecl24 ( ) { // #4187 - variable declaration within lambda function
2013-05-12 10:19:43 +02:00
const char code1 [ ] = " void f() { \n "
" std::for_each(ints.begin(), ints.end(), [](int val) \n "
" { \n "
" int temp = 0; \n "
" }); \n "
" } " ;
2012-11-30 10:30:26 +01:00
2013-05-12 10:19:43 +02:00
const char expected1 [ ] = " void f ( ) { \n "
" std :: for_each ( ints . begin ( ) , ints . end ( ) , [ ] ( int val ) \n "
" { \n "
" int temp ; temp = 0 ; \n "
" } ) ; \n "
" } " ;
2012-11-30 10:30:26 +01:00
2013-05-12 10:19:43 +02:00
ASSERT_EQUALS ( expected1 , tokenizeAndStringify ( code1 ) ) ;
const char code2 [ ] = " void f(int j) { \n "
" g( [](){int temp = 0;} , j ); \n "
" } " ;
const char expected2 [ ] = " void f ( int j ) { \n "
" g ( [ ] ( ) { int temp ; temp = 0 ; } , j ) ; \n "
" } " ;
ASSERT_EQUALS ( expected2 , tokenizeAndStringify ( code2 ) ) ;
2012-11-30 10:30:26 +01:00
}
2014-11-20 14:20:09 +01:00
void vardecl25 ( ) { // #4799 - segmentation fault
2013-05-14 20:56:31 +02:00
tokenizeAndStringify ( " void A::func(P g) const {} \n "
" void A::a() { \n "
" b = new d( [this]( const P & p) -> double { return this->func(p);} ); \n "
" } " ) ;
}
2014-11-20 14:20:09 +01:00
void vardecl26 ( ) { // #5907
2014-06-08 14:59:58 +02:00
const char code [ ] = " extern int *new, obj, player; " ;
const char expected [ ] = " extern int * new ; extern int obj ; extern int player ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , false , true , Settings : : Unspecified , " test.c " ) ) ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void volatile_variables ( ) {
2009-04-20 20:38:05 +02:00
const char code [ ] = " volatile int a=0; \n "
" volatile int b=0; \n "
" volatile int c=0; \n " ;
const std : : string actual ( tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " int a ; a = 0 ; \n int b ; b = 0 ; \n int c ; c = 0 ; " , actual ) ;
}
2009-05-07 22:17:29 +02:00
2014-05-14 22:27:31 +02:00
2014-11-20 14:20:09 +01:00
void simplifyKeyword ( ) {
2015-05-17 20:02:41 +02:00
{
const char code [ ] = " void f (int a [ static 5] ); " ;
ASSERT_EQUALS ( " void f ( int a [ 5 ] ) ; " , tokenizeAndStringify ( code ) ) ;
}
2015-05-17 13:02:13 +02:00
{
const char in1 [ ] = " class Base { \n "
" virtual int test() = 0; \n "
" }; \n "
" class Derived : public Base { \n "
" virtual int test() override final { \n "
" for( int Index ( 0 ); Index < 16; ++ Index) { int stub = 0; } \n "
" } \n "
" }; " ;
const char out1 [ ] = " class Base { \n "
" virtual int test ( ) = 0 ; \n "
" } ; \n "
" class Derived : public Base { \n "
" virtual int test ( ) { \n "
" for ( int Index ( 0 ) ; Index < 16 ; ++ Index ) { int stub ; stub = 0 ; } \n "
" } \n "
" } ; " ;
ASSERT_EQUALS ( out1 , tokenizeAndStringify ( in1 ) ) ;
const char in2 [ ] = " class Derived{ \n "
" virtual int test() final override; "
" }; " ;
const char out2 [ ] = " class Derived { \n "
" virtual int test ( ) ; } ; " ;
ASSERT_EQUALS ( out2 , tokenizeAndStringify ( in2 ) ) ;
2015-05-17 20:02:41 +02:00
const char in3 [ ] = " class Derived{ \n "
2015-05-17 13:02:13 +02:00
" virtual int test() final override const; "
" }; " ;
const char out3 [ ] = " class Derived { \n "
" virtual int test ( ) const ; } ; " ;
ASSERT_EQUALS ( out3 , tokenizeAndStringify ( in3 ) ) ;
2015-05-17 20:02:41 +02:00
const char in4 [ ] = " struct B final : A { void foo(); }; " ;
const char out4 [ ] = " struct B : A { void foo ( ) ; } ; " ;
ASSERT_EQUALS ( out4 , tokenizeAndStringify ( in4 ) ) ;
2015-05-23 20:51:15 +02:00
const char in5 [ ] = " struct ArrayItemsValidator final { \n "
" SchemaError validate() const override { \n "
" for (; pos < value.size(); ++pos) { \n "
" } \n "
" return none; \n "
" } \n "
" }; \n " ;
const char out5 [ ] =
" struct ArrayItemsValidator { \n "
" SchemaError validate ( ) const { \n "
" for ( ; pos < value . size ( ) ; ++ pos ) { \n "
" } \n "
" return none ; \n "
" } \n "
" } ; " ;
ASSERT_EQUALS ( out5 , tokenizeAndStringify ( in5 ) ) ;
2015-05-17 20:02:41 +02:00
}
2014-05-13 16:28:28 +02:00
}
2009-06-06 10:40:48 +02:00
2009-09-28 20:25:05 +02:00
/**
2010-03-31 17:14:49 +02:00
* tokenize " signed i " = > " signed int i "
2009-09-28 20:25:05 +02:00
*/
2014-11-20 14:20:09 +01:00
void signed1 ( ) {
2009-09-28 20:25:05 +02:00
{
2010-03-31 17:14:49 +02:00
const char code1 [ ] = " void foo ( signed int , float ) ; " ;
ASSERT_EQUALS ( code1 , tokenizeAndStringify ( code1 ) ) ;
2009-09-28 20:25:05 +02:00
}
{
const char code1 [ ] = " signed i ; " ;
2010-03-31 17:14:49 +02:00
const char code2 [ ] = " signed int i ; " ;
2009-09-28 20:25:05 +02:00
ASSERT_EQUALS ( code2 , tokenizeAndStringify ( code1 ) ) ;
}
{
const char code1 [ ] = " signed int i ; " ;
2010-03-31 17:14:49 +02:00
ASSERT_EQUALS ( code1 , tokenizeAndStringify ( code1 ) ) ;
2009-09-28 20:25:05 +02:00
}
{
const char code1 [ ] = " int signed i ; " ;
2010-03-31 17:14:49 +02:00
const char code2 [ ] = " signed int i ; " ;
2009-09-28 20:25:05 +02:00
ASSERT_EQUALS ( code2 , tokenizeAndStringify ( code1 ) ) ;
}
{
2010-08-25 22:10:21 +02:00
const char code1 [ ] = " void f() { for (signed i=0; i<10; i++) {} } " ;
const char code2 [ ] = " void f ( ) { for ( signed int i = 0 ; i < 10 ; i ++ ) { } } " ;
2009-09-28 20:25:05 +02:00
ASSERT_EQUALS ( code2 , tokenizeAndStringify ( code1 ) ) ;
}
}
2009-06-06 10:40:48 +02:00
/**
* tokenize " unsigned i " = > " unsigned int i "
2010-03-31 17:14:49 +02:00
* tokenize " unsigned " = > " unsigned int "
2009-06-06 10:40:48 +02:00
*/
2014-11-20 14:20:09 +01:00
void unsigned1 ( ) {
2009-06-06 10:40:48 +02:00
// No changes..
{
2010-03-31 17:14:49 +02:00
const char code [ ] = " void foo ( unsigned int , float ) ; " ;
2009-06-06 10:40:48 +02:00
ASSERT_EQUALS ( code , tokenizeAndStringify ( code ) ) ;
}
// insert "int" after "unsigned"..
{
const char code1 [ ] = " unsigned i ; " ;
const char code2 [ ] = " unsigned int i ; " ;
ASSERT_EQUALS ( code2 , tokenizeAndStringify ( code1 ) ) ;
}
2009-09-28 20:25:05 +02:00
{
const char code1 [ ] = " int unsigned i ; " ;
const char code2 [ ] = " unsigned int i ; " ;
ASSERT_EQUALS ( code2 , tokenizeAndStringify ( code1 ) ) ;
}
2009-08-05 20:15:48 +02:00
// insert "int" after "unsigned"..
{
2010-08-25 22:10:21 +02:00
const char code1 [ ] = " void f() { for (unsigned i=0; i<10; i++) {} } " ;
const char code2 [ ] = " void f ( ) { for ( unsigned int i = 0 ; i < 10 ; i ++ ) { } } " ;
2009-08-05 20:15:48 +02:00
ASSERT_EQUALS ( code2 , tokenizeAndStringify ( code1 ) ) ;
}
2010-03-20 08:03:18 +01:00
// "extern unsigned x;" => "extern int x;"
{
const char code1 [ ] = " ; extern unsigned x; " ;
2010-03-31 17:14:49 +02:00
const char code2 [ ] = " ; extern unsigned int x ; " ;
2010-03-20 08:03:18 +01:00
ASSERT_EQUALS ( code2 , tokenizeAndStringify ( code1 ) ) ;
}
2009-06-06 10:40:48 +02:00
}
2014-11-20 14:20:09 +01:00
void unsigned2 ( ) {
2009-11-17 19:03:23 +01:00
const char code [ ] = " i = (unsigned)j; " ;
const char expected [ ] = " i = ( unsigned int ) j ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
2010-03-19 19:34:26 +01:00
// simplify "unsigned" when using templates..
2014-11-20 14:20:09 +01:00
void unsigned3 ( ) {
2010-03-19 19:34:26 +01:00
{
const char code [ ] = " ; foo<unsigned>(); " ;
const char expected [ ] = " ; foo<int> ( ) ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " ; foo<unsigned int>(); " ;
const char expected [ ] = " ; foo<int> ( ) ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
}
2014-11-20 14:20:09 +01:00
void simplifyStdType ( ) { // #4947, #4950, #4951
2013-08-12 06:23:01 +02:00
// usigned long long
2013-08-09 23:13:04 +02:00
{
const char code [ ] = " long long unsigned int x; " ;
const char expected [ ] = " unsigned long long x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " long long int unsigned x; " ;
const char expected [ ] = " unsigned long long x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " unsigned long long int x; " ;
const char expected [ ] = " unsigned long long x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " unsigned int long long x; " ;
const char expected [ ] = " unsigned long long x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " int unsigned long long x; " ;
const char expected [ ] = " unsigned long long x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " int long long unsigned x; " ;
const char expected [ ] = " unsigned long long x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
2013-08-12 06:23:01 +02:00
// signed long long
{
const char code [ ] = " long long signed int x; " ;
const char expected [ ] = " signed long long x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " long long int signed x; " ;
const char expected [ ] = " signed long long x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " signed long long int x; " ;
const char expected [ ] = " signed long long x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " signed int long long x; " ;
const char expected [ ] = " signed long long x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " int signed long long x; " ;
const char expected [ ] = " signed long long x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " int long long signed x; " ;
const char expected [ ] = " signed long long x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
// usigned short
{
const char code [ ] = " short unsigned int x; " ;
const char expected [ ] = " unsigned short x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " short int unsigned x; " ;
const char expected [ ] = " unsigned short x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " unsigned short int x; " ;
const char expected [ ] = " unsigned short x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " unsigned int short x; " ;
const char expected [ ] = " unsigned short x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " int unsigned short x; " ;
const char expected [ ] = " unsigned short x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " int short unsigned x; " ;
const char expected [ ] = " unsigned short x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
// signed short
{
const char code [ ] = " short signed int x; " ;
const char expected [ ] = " signed short x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " short int signed x; " ;
const char expected [ ] = " signed short x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " signed short int x; " ;
const char expected [ ] = " signed short x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " signed int short x; " ;
const char expected [ ] = " signed short x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " int signed short x; " ;
const char expected [ ] = " signed short x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " int short signed x; " ;
const char expected [ ] = " signed short x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
2015-08-27 14:34:00 +02:00
{
const char code [ ] = " unsigned static short const int i; " ;
const char expected [ ] = " static const unsigned short i ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
2015-08-26 13:13:45 +02:00
{
const char code [ ] = " float complex x; " ;
2015-08-27 14:34:00 +02:00
const char expected [ ] = " _Complex float x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " complex float x; " ;
const char expected [ ] = " _Complex float x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " complex long double x; " ;
const char expected [ ] = " _Complex long double x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " long double complex x; " ;
const char expected [ ] = " _Complex long double x ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
{
const char code [ ] = " double complex; " ;
const char expected [ ] = " double complex ; " ;
2015-08-26 13:13:45 +02:00
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
}
2013-08-09 23:13:04 +02:00
}
2014-11-20 14:20:09 +01:00
void createLinks ( ) {
2009-09-20 22:13:06 +02:00
{
const char code [ ] = " class A{ \n "
" void f() {} \n "
" }; " ;
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
2015-10-07 18:33:57 +02:00
Tokenizer tokenizer ( & settings0 , this ) ;
2009-09-20 22:13:06 +02:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
const Token * tok = tokenizer . tokens ( ) ;
// A body {}
2011-11-20 14:22:39 +01:00
ASSERT_EQUALS ( true , tok - > linkAt ( 2 ) = = tok - > tokAt ( 9 ) ) ;
ASSERT_EQUALS ( true , tok - > linkAt ( 9 ) = = tok - > tokAt ( 2 ) ) ;
2009-09-20 22:13:06 +02:00
// f body {}
2011-11-20 14:22:39 +01:00
ASSERT_EQUALS ( true , tok - > linkAt ( 7 ) = = tok - > tokAt ( 8 ) ) ;
ASSERT_EQUALS ( true , tok - > linkAt ( 8 ) = = tok - > tokAt ( 7 ) ) ;
2009-09-20 22:13:06 +02:00
// f ()
2011-11-20 14:22:39 +01:00
ASSERT_EQUALS ( true , tok - > linkAt ( 5 ) = = tok - > tokAt ( 6 ) ) ;
ASSERT_EQUALS ( true , tok - > linkAt ( 6 ) = = tok - > tokAt ( 5 ) ) ;
2012-03-21 18:40:32 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-09-20 22:13:06 +02:00
}
{
const char code [ ] = " void f(){ \n "
" char a[10]; \n "
" char *b ; b = new char[a[0]]; \n "
" }; " ;
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
2015-10-07 18:33:57 +02:00
Tokenizer tokenizer ( & settings0 , this ) ;
2009-09-20 22:13:06 +02:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
const Token * tok = tokenizer . tokens ( ) ;
// a[10]
2011-11-20 14:22:39 +01:00
ASSERT_EQUALS ( true , tok - > linkAt ( 7 ) = = tok - > tokAt ( 9 ) ) ;
ASSERT_EQUALS ( true , tok - > linkAt ( 9 ) = = tok - > tokAt ( 7 ) ) ;
2009-09-20 22:13:06 +02:00
// new char[]
2011-11-20 14:22:39 +01:00
ASSERT_EQUALS ( true , tok - > linkAt ( 19 ) = = tok - > tokAt ( 24 ) ) ;
ASSERT_EQUALS ( true , tok - > linkAt ( 24 ) = = tok - > tokAt ( 19 ) ) ;
2009-09-20 22:13:06 +02:00
// a[0]
2011-11-20 14:22:39 +01:00
ASSERT_EQUALS ( true , tok - > linkAt ( 21 ) = = tok - > tokAt ( 23 ) ) ;
ASSERT_EQUALS ( true , tok - > linkAt ( 23 ) = = tok - > tokAt ( 21 ) ) ;
2012-03-21 18:40:32 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-09-20 22:13:06 +02:00
}
{
const char code [ ] = " void f(){ \n "
" foo(g()); \n "
" }; " ;
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
2015-10-07 18:33:57 +02:00
Tokenizer tokenizer ( & settings0 , this ) ;
2009-09-20 22:13:06 +02:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
const Token * tok = tokenizer . tokens ( ) ;
// foo(
2011-11-20 14:22:39 +01:00
ASSERT_EQUALS ( true , tok - > linkAt ( 6 ) = = tok - > tokAt ( 10 ) ) ;
ASSERT_EQUALS ( true , tok - > linkAt ( 10 ) = = tok - > tokAt ( 6 ) ) ;
2009-09-20 22:13:06 +02:00
// g(
2011-11-20 14:22:39 +01:00
ASSERT_EQUALS ( true , tok - > linkAt ( 8 ) = = tok - > tokAt ( 9 ) ) ;
ASSERT_EQUALS ( true , tok - > linkAt ( 9 ) = = tok - > tokAt ( 8 ) ) ;
2012-03-21 18:40:32 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-09-20 22:13:06 +02:00
}
2012-03-20 19:00:16 +01:00
{
const char code [ ] = " bool foo(C<z> a, bar<int, x<float>>& f, int b) { \n "
" return(a<b && b>f); \n "
" } " ;
errout . str ( " " ) ;
2015-10-07 18:33:57 +02:00
Tokenizer tokenizer ( & settings0 , this ) ;
2012-03-20 19:00:16 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
const Token * tok = tokenizer . tokens ( ) ;
// template<
2013-02-10 07:43:09 +01:00
ASSERT_EQUALS ( true , tok - > tokAt ( 6 ) = = tok - > linkAt ( 4 ) ) ;
ASSERT_EQUALS ( true , tok - > tokAt ( 4 ) = = tok - > linkAt ( 6 ) ) ;
2012-03-20 19:00:16 +01:00
// bar<
2013-02-10 07:43:09 +01:00
ASSERT_EQUALS ( true , tok - > tokAt ( 17 ) = = tok - > linkAt ( 10 ) ) ;
ASSERT_EQUALS ( true , tok - > tokAt ( 10 ) = = tok - > linkAt ( 17 ) ) ;
2012-03-20 19:00:16 +01:00
// x<
2013-02-10 07:43:09 +01:00
ASSERT_EQUALS ( true , tok - > tokAt ( 16 ) = = tok - > linkAt ( 14 ) ) ;
ASSERT_EQUALS ( true , tok - > tokAt ( 14 ) = = tok - > linkAt ( 16 ) ) ;
2012-03-20 19:00:16 +01:00
// a<b && b>f
2013-02-10 07:43:09 +01:00
ASSERT_EQUALS ( true , 0 = = tok - > linkAt ( 28 ) ) ;
ASSERT_EQUALS ( true , 0 = = tok - > linkAt ( 32 ) ) ;
2012-03-21 18:40:32 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " void foo() { \n "
" return static_cast<bar>(a); \n "
" } " ;
errout . str ( " " ) ;
2015-10-07 18:33:57 +02:00
Tokenizer tokenizer ( & settings0 , this ) ;
2012-03-21 18:40:32 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
const Token * tok = tokenizer . tokens ( ) ;
// static_cast<
2013-02-10 07:43:09 +01:00
ASSERT_EQUALS ( true , tok - > tokAt ( 9 ) = = tok - > linkAt ( 7 ) ) ;
ASSERT_EQUALS ( true , tok - > tokAt ( 7 ) = = tok - > linkAt ( 9 ) ) ;
2012-03-21 18:40:32 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-03-20 19:00:16 +01:00
}
2012-09-09 15:31:23 +02:00
{
const char code [ ] = " void foo() { \n "
" nvwa<(x > y)> ERROR_nnn; \n "
" } " ;
errout . str ( " " ) ;
2015-10-07 18:33:57 +02:00
Tokenizer tokenizer ( & settings0 , this ) ;
2012-09-09 15:31:23 +02:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
const Token * tok = tokenizer . tokens ( ) ;
// nvwa<(x > y)>
2013-02-10 07:43:09 +01:00
ASSERT_EQUALS ( true , tok - > tokAt ( 12 ) = = tok - > linkAt ( 6 ) ) ;
ASSERT_EQUALS ( true , tok - > tokAt ( 6 ) = = tok - > linkAt ( 12 ) ) ;
2012-09-09 15:31:23 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2013-09-02 05:58:55 +02:00
{
// #4860
const char code [ ] = " class A : public B<int> {}; " ;
errout . str ( " " ) ;
2015-10-07 18:33:57 +02:00
Tokenizer tokenizer ( & settings0 , this ) ;
2013-09-02 05:58:55 +02:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
const Token * tok = tokenizer . tokens ( ) ;
// B<..>
ASSERT_EQUALS ( true , tok - > tokAt ( 5 ) = = tok - > linkAt ( 7 ) ) ;
ASSERT_EQUALS ( true , tok - > linkAt ( 5 ) = = tok - > tokAt ( 7 ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-03-22 13:33:34 +01:00
{
// #4860
const char code [ ] = " Bar<Typelist< int, Typelist< int, Typelist< int, FooNullType>>>>::set(1, 2, 3); " ;
errout . str ( " " ) ;
2015-10-07 18:33:57 +02:00
Tokenizer tokenizer ( & settings0 , this ) ;
2014-03-22 13:33:34 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
const Token * tok = tokenizer . tokens ( ) ;
ASSERT_EQUALS ( true , tok - > tokAt ( 1 ) = = tok - > linkAt ( 18 ) ) ;
ASSERT_EQUALS ( true , tok - > tokAt ( 3 ) = = tok - > linkAt ( 17 ) ) ;
ASSERT_EQUALS ( true , tok - > tokAt ( 7 ) = = tok - > linkAt ( 16 ) ) ;
ASSERT_EQUALS ( true , tok - > tokAt ( 11 ) = = tok - > linkAt ( 15 ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-03-31 15:55:54 +02:00
{
// #5627
const char code [ ] = " new Foo<Bar>[10]; " ;
errout . str ( " " ) ;
2015-10-07 18:33:57 +02:00
Tokenizer tokenizer ( & settings0 , this ) ;
2014-03-31 15:55:54 +02:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
const Token * tok = tokenizer . tokens ( ) ;
ASSERT_EQUALS ( true , tok - > tokAt ( 2 ) = = tok - > linkAt ( 4 ) ) ;
ASSERT_EQUALS ( true , tok - > tokAt ( 4 ) = = tok - > linkAt ( 2 ) ) ;
ASSERT_EQUALS ( true , tok - > tokAt ( 5 ) = = tok - > linkAt ( 7 ) ) ;
ASSERT_EQUALS ( true , tok - > tokAt ( 7 ) = = tok - > linkAt ( 5 ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-10-30 11:15:24 +01:00
{
// #6242
const char code [ ] = " func = integral_<uchar, int, double>; " ;
errout . str ( " " ) ;
2015-10-07 18:33:57 +02:00
Tokenizer tokenizer ( & settings0 , this ) ;
2014-10-30 11:15:24 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
const Token * tok = tokenizer . tokens ( ) ;
ASSERT_EQUALS ( true , tok - > tokAt ( 3 ) = = tok - > linkAt ( 9 ) ) ;
ASSERT_EQUALS ( true , tok - > linkAt ( 3 ) = = tok - > tokAt ( 9 ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-08-17 19:03:06 +02:00
{
// if (a < b || c > d) { }
const char code [ ] = " if (a < b || c > d); " ;
errout . str ( " " ) ;
2015-10-07 18:33:57 +02:00
Tokenizer tokenizer ( & settings0 , this ) ;
2014-08-17 19:03:06 +02:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
const Token * tok = tokenizer . tokens ( ) ;
ASSERT_EQUALS ( true , tok - > linkAt ( 3 ) = = nullptr ) ;
}
2015-11-06 10:44:37 +01:00
{
// #6601
const char code [ ] = " template<class R> struct FuncType<R(&)()> : FuncType<R()> { }; " ;
errout . str ( " " ) ;
Tokenizer tokenizer ( & settings0 , this ) ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
const Token * tok = tokenizer . tokens ( ) ;
ASSERT_EQUALS ( true , tok - > linkAt ( 1 ) = = tok - > tokAt ( 4 ) ) ; // <class R>
ASSERT_EQUALS ( true , tok - > linkAt ( 7 ) = = tok - > tokAt ( 14 ) ) ; // <R(&)()>
ASSERT_EQUALS ( true , tok - > linkAt ( 9 ) = = tok - > tokAt ( 11 ) ) ; // (&)
ASSERT_EQUALS ( true , tok - > linkAt ( 12 ) = = tok - > tokAt ( 13 ) ) ; // ()
ASSERT_EQUALS ( true , tok - > linkAt ( 17 ) = = tok - > tokAt ( 21 ) ) ; // <R()>
ASSERT_EQUALS ( true , tok - > linkAt ( 19 ) = = tok - > tokAt ( 20 ) ) ; // ()
ASSERT_EQUALS ( true , tok - > linkAt ( 22 ) = = tok - > tokAt ( 23 ) ) ; // {}
}
2009-09-20 22:13:06 +02:00
}
2009-10-01 19:45:48 +02:00
2014-11-20 14:20:09 +01:00
void simplifyString ( ) {
2010-12-01 18:00:55 +01:00
errout . str ( " " ) ;
2015-10-07 18:33:57 +02:00
Tokenizer tokenizer ( & settings0 , this ) ;
2009-12-23 12:17:48 +01:00
ASSERT_EQUALS ( " \" abc \" " , tokenizer . simplifyString ( " \" abc \" " ) ) ;
2012-09-13 16:44:10 +02:00
ASSERT_EQUALS ( " \" \n \" " , tokenizer . simplifyString ( " \" \\ xa \" " ) ) ;
ASSERT_EQUALS ( " \" 3 \" " , tokenizer . simplifyString ( " \" \\ x33 \" " ) ) ;
ASSERT_EQUALS ( " \" 33 \" " , tokenizer . simplifyString ( " \" \\ x333 \" " ) ) ;
ASSERT_EQUALS ( " \" a \" " , tokenizer . simplifyString ( " \" \\ x61 \" " ) ) ;
ASSERT_EQUALS ( " \" \n 1 \" " , tokenizer . simplifyString ( " \" \\ 0121 \" " ) ) ;
ASSERT_EQUALS ( " \" 3 \" " , tokenizer . simplifyString ( " \" \\ x33 \" " ) ) ;
ASSERT_EQUALS ( " \" 0 \" " , tokenizer . simplifyString ( " \" \\ 0400 \" " ) ) ;
ASSERT_EQUALS ( " \" \\ nhello \" " , tokenizer . simplifyString ( " \" \\ nhello \" " ) ) ;
ASSERT_EQUALS ( " \" aaa \" " , tokenizer . simplifyString ( " \" \\ x61 \\ x61 \\ x61 \" " ) ) ;
ASSERT_EQUALS ( " \" \n 1 \n 1 \n 1 \" " , tokenizer . simplifyString ( " \" \\ 0121 \\ 0121 \\ 0121 \" " ) ) ;
ASSERT_EQUALS ( " \" \\ \\ x61 \" " , tokenizer . simplifyString ( " \" \\ \\ x61 \" " ) ) ;
ASSERT_EQUALS ( " \" b \" " , tokenizer . simplifyString ( " \" \\ x62 \" " ) ) ;
ASSERT_EQUALS ( " \" 7 \" " , tokenizer . simplifyString ( " \" \\ 0407 \" " ) ) ;
// terminate a string at null character.
2012-12-26 12:08:40 +01:00
ASSERT_EQUALS ( std : : string ( " \" a " ) + ' \0 ' + " \" " , tokenizer . simplifyString ( " \" a \\ 0 \" " ) ) ;
2009-10-09 21:11:29 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyConst ( ) {
2009-11-12 22:49:39 +01:00
ASSERT_EQUALS ( " void foo ( ) { const int x ; } " ,
tokenizeAndStringify ( " void foo(){ int const x;} " ) ) ;
ASSERT_EQUALS ( " void foo ( ) { { } const long x ; } " ,
tokenizeAndStringify ( " void foo(){ {} long const x;} " ) ) ;
2014-12-27 10:53:26 +01:00
ASSERT_EQUALS ( " void foo ( int b , const unsigned int x ) { } " ,
tokenizeAndStringify ( " void foo(int b,unsigned const x){} " ) ) ;
2009-11-12 22:49:39 +01:00
ASSERT_EQUALS ( " void foo ( ) { bar ( ) ; const char x ; } " ,
tokenizeAndStringify ( " void foo(){ bar(); char const x;} " ) ) ;
ASSERT_EQUALS ( " void foo ( const char x ) { } " ,
tokenizeAndStringify ( " void foo(char const x){} " ) ) ;
ASSERT_EQUALS ( " void foo ( int b , const char x ) { } " ,
tokenizeAndStringify ( " void foo(int b,char const x){} " ) ) ;
ASSERT_EQUALS ( " void foo ( ) { int * const x ; } " ,
tokenizeAndStringify ( " void foo(){ int * const x;} " ) ) ;
2011-12-04 16:46:03 +01:00
ASSERT_EQUALS ( " const int foo ( ) ; " , tokenizeAndStringify ( " int const foo (); " ) ) ;
2012-09-07 11:41:41 +02:00
ASSERT_EQUALS ( " const int x ; " , tokenizeAndStringify ( " int const x; " ) ) ;
2014-12-27 10:53:26 +01:00
ASSERT_EQUALS ( " const unsigned int x ; " , tokenizeAndStringify ( " unsigned const x; " ) ) ;
2012-09-07 11:41:41 +02:00
ASSERT_EQUALS ( " const struct X x ; " , tokenizeAndStringify ( " struct X const x; " ) ) ;
2009-11-12 22:49:39 +01:00
}
2009-12-06 23:09:56 +01:00
2014-11-20 14:20:09 +01:00
void switchCase ( ) {
2011-10-19 14:20:09 +02:00
ASSERT_EQUALS ( " void foo ( int i ) { switch ( i ) { case -1 : ; break ; } } " ,
2009-12-06 23:09:56 +01:00
tokenizeAndStringify ( " void foo (int i) { switch(i) { case -1: break; } } " ) ) ;
2011-10-19 14:20:09 +02:00
//ticket #3227
ASSERT_EQUALS ( " void foo ( ) { switch ( n ) { label : ; case 1 : ; label1 : ; label2 : ; break ; } } " ,
tokenizeAndStringify ( " void foo(){ switch (n){ label: case 1: label1: label2: break; }} " ) ) ;
2009-12-06 23:09:56 +01:00
}
2010-01-20 21:19:06 +01:00
2014-11-20 14:20:09 +01:00
void simplifyPointerToStandardType ( ) {
2011-10-16 08:09:57 +02:00
// Pointer to standard type
ASSERT_EQUALS ( " char buf [ 100 ] ; readlink ( path , buf , 99 ) ; " ,
tokenizeAndStringify ( " char buf[100] ; readlink(path, &buf[0], 99); " ,
false , true , Settings : : Unspecified , " test.c " ) ) ;
// Simplification of unknown type - C only
ASSERT_EQUALS ( " foo data [ 100 ] ; something ( foo ) ; " ,
tokenizeAndStringify ( " foo data[100]; something(&foo[0]); " , false , true , Settings : : Unspecified , " test.c " ) ) ;
// C++: No pointer simplification
ASSERT_EQUALS ( " foo data [ 100 ] ; something ( & foo [ 0 ] ) ; " ,
tokenizeAndStringify ( " foo data[100]; something(&foo[0]); " ) ) ;
}
2014-11-20 14:20:09 +01:00
void functionpointer1 ( ) {
2015-10-07 18:33:57 +02:00
ASSERT_EQUALS ( " void * f ; " , tokenizeAndStringify ( " void (*f)(); " ) ) ;
ASSERT_EQUALS ( " void * * f ; " , tokenizeAndStringify ( " void *(*f)(); " ) ) ;
ASSERT_EQUALS ( " unsigned int * f ; " , tokenizeAndStringify ( " unsigned int (*f)(); " ) ) ;
ASSERT_EQUALS ( " unsigned int * * f ; " , tokenizeAndStringify ( " unsigned int * (*f)(); " ) ) ;
2010-01-20 21:19:06 +01:00
}
2010-01-31 09:33:57 +01:00
2014-11-20 14:20:09 +01:00
void functionpointer2 ( ) {
2011-03-19 21:21:26 +01:00
const char code [ ] = " typedef void (* PF)(); "
" void f1 ( ) { } "
" PF pf = &f1; "
" PF pfs[] = { &f1, &f1 }; " ;
2012-04-16 19:51:07 +02:00
const char expected [ ] = " void f1 ( ) { } "
" void * pf ; pf = & f1 ; "
" void * pfs [ 2 ] = { & f1 , & f1 } ; " ;
2015-10-07 18:33:57 +02:00
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
2011-03-19 21:21:26 +01:00
}
2014-11-20 14:20:09 +01:00
void functionpointer3 ( ) {
2011-07-22 07:58:53 +02:00
// Related with ticket #2873
const char code [ ] = " void f() { \n "
" (void)(xy(*p)(0);) "
" \n } " ;
2015-10-07 18:33:57 +02:00
const char expected [ ] = " void f ( ) { \n "
" ( void ) ( xy ( * p ) ( 0 ) ; ) \n "
2011-07-22 07:58:53 +02:00
" } " ;
2015-10-07 18:33:57 +02:00
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
2011-07-22 07:58:53 +02:00
}
2014-11-20 14:20:09 +01:00
void functionpointer4 ( ) {
2014-04-02 17:33:04 +02:00
const char code [ ] = " struct S \n "
2011-11-10 21:32:37 +01:00
" { \n "
" typedef void (*FP)(); \n "
" virtual FP getFP(); \n "
" virtual void execute(); \n "
" }; \n "
" void f() { \n "
" int a[9]; \n "
" } \n " ;
2011-11-09 21:02:41 +01:00
const char expected [ ] = " \n \n ##file 0 \n "
2011-11-10 21:32:37 +01:00
" 1: struct S \n "
" 2: { \n "
2011-12-04 15:40:05 +01:00
" 3: \n "
2014-04-12 16:06:31 +02:00
" 4: virtual void * getFP ( ) ; \n "
2011-11-10 21:32:37 +01:00
" 5: virtual void execute ( ) ; \n "
" 6: } ; \n "
" 7: void f ( ) { \n "
" 8: int a@1 [ 9 ] ; \n "
" 9: } \n " ;
2011-11-09 21:02:41 +01:00
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void functionpointer5 ( ) {
2011-12-27 11:56:40 +01:00
const char code [ ] = " ;void (*fp[])(int a) = {0,0,0}; " ;
const char expected [ ] = " \n \n ##file 0 \n "
2015-10-08 12:52:28 +02:00
" 1: ; void * fp@1 [ 3 ] = { 0 , 0 , 0 } ; \n " ;
2011-12-27 11:56:40 +01:00
ASSERT_EQUALS ( expected , tokenizeDebugListing ( code , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void functionpointer6 ( ) {
2014-04-12 16:06:31 +02:00
const char code1 [ ] = " ;void (*fp(f))(int); " ;
const char expected1 [ ] = " \n \n ##file 0 \n "
2014-08-19 11:06:52 +02:00
" 1: ; void * fp ( f ) ; \n " ; // No varId - it could be a function
2014-04-12 16:06:31 +02:00
ASSERT_EQUALS ( expected1 , tokenizeDebugListing ( code1 , false ) ) ;
const char code2 [ ] = " ;std::string (*fp(f))(int); " ;
const char expected2 [ ] = " \n \n ##file 0 \n "
2014-08-19 11:06:52 +02:00
" 1: ; std :: string * fp ( f ) ; \n " ;
2014-04-12 16:06:31 +02:00
ASSERT_EQUALS ( expected2 , tokenizeDebugListing ( code2 , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void functionpointer7 ( ) {
2014-04-27 19:49:21 +02:00
const char code1 [ ] = " void (X::*y)(); " ;
const char expected1 [ ] = " \n \n ##file 0 \n "
" 1: void * y@1 ; \n " ;
ASSERT_EQUALS ( expected1 , tokenizeDebugListing ( code1 , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void removeRedundantAssignment ( ) {
2012-09-04 13:41:14 +02:00
ASSERT_EQUALS ( " void f ( ) { } " , tokenizeAndStringify ( " void f() { int *p, *q; p = q; } " , true ) ) ;
ASSERT_EQUALS ( " void f ( ) { } " , tokenizeAndStringify ( " void f() { int *p = 0, *q; p = q; } " , true ) ) ;
2010-12-22 08:24:50 +01:00
ASSERT_EQUALS ( " int f ( int * x ) { return * x ; } " , tokenizeAndStringify ( " int f(int *x) { return *x; } " , true ) ) ;
2010-01-31 09:33:57 +01:00
}
2010-02-03 20:01:56 +01:00
2014-11-20 14:20:09 +01:00
void removedeclspec ( ) {
2010-02-03 20:01:56 +01:00
ASSERT_EQUALS ( " a b " , tokenizeAndStringify ( " a __declspec ( dllexport ) b " ) ) ;
2015-04-11 11:02:30 +02:00
ASSERT_EQUALS ( " a b " , tokenizeAndStringify ( " a _declspec ( dllexport ) b " ) ) ;
2010-04-24 09:40:05 +02:00
ASSERT_EQUALS ( " int a ; " , tokenizeAndStringify ( " __declspec(thread) __declspec(align(32)) int a; " ) ) ;
ASSERT_EQUALS ( " int i ; " , tokenizeAndStringify ( " __declspec(allocate( \" mycode \" )) int i; " ) ) ;
ASSERT_EQUALS ( " struct IUnknown ; " , tokenizeAndStringify ( " struct __declspec(uuid( \" 00000000-0000-0000-c000-000000000046 \" )) IUnknown; " ) ) ;
2014-08-31 20:17:18 +02:00
ASSERT_EQUALS ( " __property int x [ ] ; " , tokenizeAndStringify ( " __declspec(property(get=GetX, put=PutX)) int x[]; " ) ) ;
2010-02-03 20:01:56 +01:00
}
2010-02-08 23:16:12 +01:00
2014-11-20 14:20:09 +01:00
void removeattribute ( ) {
2010-05-27 18:15:42 +02:00
ASSERT_EQUALS ( " short array [ 3 ] ; " , tokenizeAndStringify ( " short array[3] __attribute__ ((aligned)); " ) ) ;
ASSERT_EQUALS ( " int x [ 2 ] ; " , tokenizeAndStringify ( " int x[2] __attribute__ ((packed)); " ) ) ;
2011-04-24 03:04:02 +02:00
ASSERT_EQUALS ( " int vecint ; " , tokenizeAndStringify ( " int __attribute__((mode(SI))) __attribute__((vector_size (16))) vecint; " ) ) ;
2014-03-19 05:38:23 +01:00
// alternate spelling #5328
ASSERT_EQUALS ( " short array [ 3 ] ; " , tokenizeAndStringify ( " short array[3] __attribute ((aligned)); " ) ) ;
ASSERT_EQUALS ( " int x [ 2 ] ; " , tokenizeAndStringify ( " int x[2] __attribute ((packed)); " ) ) ;
ASSERT_EQUALS ( " int vecint ; " , tokenizeAndStringify ( " int __attribute((mode(SI))) __attribute((vector_size (16))) vecint; " ) ) ;
2010-05-27 18:15:42 +02:00
}
2014-11-20 14:20:09 +01:00
void functionAttributeBefore ( ) {
2014-04-20 20:40:55 +02:00
const char code [ ] = " void __attribute__((pure)) __attribute__((nothrow)) __attribute__((const)) func1(); \n "
" void __attribute__((__pure__)) __attribute__((__nothrow__)) __attribute__((__const__)) func2(); \n "
" void __attribute__((nothrow)) __attribute__((pure)) __attribute__((const)) func3(); \n "
2014-12-24 12:50:51 +01:00
" void __attribute__((__nothrow__)) __attribute__((__pure__)) __attribute__((__const__)) func4(); \n "
" void __attribute__((noreturn)) func5(); " ;
const char expected [ ] = " void func1 ( ) ; void func2 ( ) ; void func3 ( ) ; void func4 ( ) ; void func5 ( ) ; " ;
2014-04-20 20:40:55 +02:00
errout . str ( " " ) ;
// tokenize..
2015-10-07 18:33:57 +02:00
Tokenizer tokenizer ( & settings0 , this ) ;
2014-04-20 20:40:55 +02:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
// Expected result..
ASSERT_EQUALS ( expected , tokenizer . tokens ( ) - > stringifyList ( 0 , false ) ) ;
const Token * func1 = Token : : findsimplematch ( tokenizer . tokens ( ) , " func1 " ) ;
const Token * func2 = Token : : findsimplematch ( tokenizer . tokens ( ) , " func2 " ) ;
const Token * func3 = Token : : findsimplematch ( tokenizer . tokens ( ) , " func3 " ) ;
const Token * func4 = Token : : findsimplematch ( tokenizer . tokens ( ) , " func4 " ) ;
2014-12-24 12:50:51 +01:00
const Token * func5 = Token : : findsimplematch ( tokenizer . tokens ( ) , " func5 " ) ;
2014-04-20 20:40:55 +02:00
ASSERT ( func1 & & func1 - > isAttributePure ( ) & & func1 - > isAttributeNothrow ( ) & & func1 - > isAttributeConst ( ) ) ;
ASSERT ( func2 & & func2 - > isAttributePure ( ) & & func2 - > isAttributeNothrow ( ) & & func2 - > isAttributeConst ( ) ) ;
ASSERT ( func3 & & func3 - > isAttributePure ( ) & & func3 - > isAttributeNothrow ( ) & & func3 - > isAttributeConst ( ) ) ;
ASSERT ( func4 & & func4 - > isAttributePure ( ) & & func4 - > isAttributeNothrow ( ) & & func4 - > isAttributeConst ( ) ) ;
2014-12-24 12:50:51 +01:00
ASSERT ( func5 & & func5 - > isAttributeNoreturn ( ) ) ;
2014-04-20 20:40:55 +02:00
}
2014-11-20 14:20:09 +01:00
void functionAttributeAfter ( ) {
2014-04-20 20:40:55 +02:00
const char code [ ] = " void func1() __attribute__((pure)) __attribute__((nothrow)) __attribute__((const)); \n "
" void func2() __attribute__((__pure__)) __attribute__((__nothrow__)) __attribute__((__const__)); \n "
" void func3() __attribute__((nothrow)) __attribute__((pure)) __attribute__((const)); \n "
2014-12-24 12:50:51 +01:00
" void func4() __attribute__((__nothrow__)) __attribute__((__pure__)) __attribute__((__const__)); "
" void func5() __attribute__((noreturn)); " ;
const char expected [ ] = " void func1 ( ) ; void func2 ( ) ; void func3 ( ) ; void func4 ( ) ; void func5 ( ) ; " ;
2014-04-20 20:40:55 +02:00
errout . str ( " " ) ;
// tokenize..
2015-10-07 18:33:57 +02:00
Tokenizer tokenizer ( & settings0 , this ) ;
2014-04-20 20:40:55 +02:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
// Expected result..
ASSERT_EQUALS ( expected , tokenizer . tokens ( ) - > stringifyList ( 0 , false ) ) ;
const Token * func1 = Token : : findsimplematch ( tokenizer . tokens ( ) , " func1 " ) ;
const Token * func2 = Token : : findsimplematch ( tokenizer . tokens ( ) , " func2 " ) ;
const Token * func3 = Token : : findsimplematch ( tokenizer . tokens ( ) , " func3 " ) ;
const Token * func4 = Token : : findsimplematch ( tokenizer . tokens ( ) , " func4 " ) ;
2014-12-24 12:50:51 +01:00
const Token * func5 = Token : : findsimplematch ( tokenizer . tokens ( ) , " func5 " ) ;
2014-04-20 20:40:55 +02:00
ASSERT ( func1 & & func1 - > isAttributePure ( ) & & func1 - > isAttributeNothrow ( ) & & func1 - > isAttributeConst ( ) ) ;
ASSERT ( func2 & & func2 - > isAttributePure ( ) & & func2 - > isAttributeNothrow ( ) & & func2 - > isAttributeConst ( ) ) ;
ASSERT ( func3 & & func3 - > isAttributePure ( ) & & func3 - > isAttributeNothrow ( ) & & func3 - > isAttributeConst ( ) ) ;
ASSERT ( func4 & & func4 - > isAttributePure ( ) & & func4 - > isAttributeNothrow ( ) & & func4 - > isAttributeConst ( ) ) ;
2014-12-24 12:50:51 +01:00
ASSERT ( func5 & & func5 - > isAttributeNoreturn ( ) ) ;
2014-04-20 20:40:55 +02:00
}
2015-10-19 20:03:33 +02:00
void cpp03template1 ( ) {
{
const char * code = " template<typename> struct extent {}; " ;
ASSERT_EQUALS ( " template < typename > struct extent { } ; " , tokenizeAndStringify ( code ) ) ;
}
{
const char * code = " template<typename> struct extent; " ;
ASSERT_EQUALS ( " template < typename > struct extent ; " , tokenizeAndStringify ( code ) ) ;
}
{
const char * code = " template<typename, unsigned = 0> struct extent; " ;
ASSERT_EQUALS ( " template < typename , unsigned int = 0 > struct extent ; " , tokenizeAndStringify ( code ) ) ;
}
}
2014-11-20 14:20:09 +01:00
void cpp0xtemplate1 ( ) {
2010-02-08 23:16:12 +01:00
const char * code = " template <class T> \n "
" void fn2 (T t = []{return 1;}()) \n "
" {} \n "
" int main() \n "
" { \n "
" fn2<int>(); \n "
" } \n " ;
2012-04-16 19:51:07 +02:00
ASSERT_EQUALS ( " int main ( ) \n { \n fn2<int> ( ) ; \n } void fn2<int> ( int t = [ ] { return 1 ; } ( ) ) \n { } " , tokenizeAndStringify ( code ) ) ;
2010-02-08 23:16:12 +01:00
}
2010-08-17 19:50:21 +02:00
2014-11-20 14:20:09 +01:00
void cpp0xtemplate2 ( ) {
2010-09-05 08:16:19 +02:00
// tokenize ">>" into "> >"
const char * code = " list<list<int>> ints; \n " ;
2012-03-20 19:00:16 +01:00
ASSERT_EQUALS ( " list < list < int > > ints ; " , tokenizeAndStringify ( code ) ) ;
2010-09-05 08:16:19 +02:00
}
2014-11-20 14:20:09 +01:00
void cpp0xtemplate3 ( ) {
2011-02-12 16:51:59 +01:00
// #2549
const char * code = " template<class T, T t = (T)0> \n "
" struct S \n "
" {}; \n "
" S<int> s; \n " ;
2012-07-29 16:01:05 +02:00
TODO_ASSERT_EQUALS ( " S<int,(int)0> s ; struct S<int,(int)0> { } ; " , // wanted result
" template < class T , T t > \n "
" struct S \n "
" { } ; \n "
2011-12-06 16:38:13 +01:00
" S < int , ( T ) 0 > s ; " , // current result
2011-02-12 16:51:59 +01:00
tokenizeAndStringify ( code ) ) ;
}
2015-02-14 12:29:05 +01:00
void cpp0xtemplate4 ( ) { // #6181, #6354, #6414
2015-03-07 11:06:04 +01:00
tokenizeAndStringify ( " class A; "
" template <class T> class Disposer; "
" template <typename T, class D = Disposer<T>> class Shim {}; "
" class B : public Shim<A> {}; " ) ;
2015-02-14 12:29:05 +01:00
tokenizeAndStringify ( " template <class ELFT> class ELFObjectImage {}; "
" ObjectImage *createObjectImage() { "
" return new ELFObjectImage<ELFType<little>>(Obj); "
" } "
" void resolveX86_64Relocation() { "
" reinterpret_cast<int>(0); "
" } " ) ;
tokenizeAndStringify ( " template<typename value_type, typename function_type> "
" value_type Base(const value_type x, const value_type dx, function_type func, int type_deriv) { "
" return 0.0; "
" }; "
" namespace { "
" template<class DC> class C { "
" void Fun(int G, const double x); "
" }; "
" template<class DC> void C<DC>::Fun(int G, const double x) { "
" Base<double, CDFFunctor<DC>>(2, 2, f, 0); "
" }; "
" template<class DC> class C2 {}; "
" } " ) ;
2014-12-14 14:58:54 +01:00
}
2015-05-26 00:28:08 +02:00
void cpp14template ( ) { // Ticket #6708
tokenizeAndStringify ( " template <typename T> "
" decltype(auto) forward(T& t) { return 0; } " ) ;
}
2014-11-20 14:20:09 +01:00
void arraySize ( ) {
2015-10-08 11:35:51 +02:00
ASSERT_EQUALS ( " ; int a [ 3 ] = { 1 , 2 , 3 } ; " , tokenizeAndStringify ( " ;int a[]={1,2,3}; " ) ) ;
ASSERT_EQUALS ( " ; int a [ 3 ] = { 1 , 2 , 3 } ; " , tokenizeAndStringify ( " ;int a[]={1,2,3,}; " ) ) ;
ASSERT_EQUALS ( " ; foo a [ 3 ] = { { 1 , 2 } , { 3 , 4 } , { 5 , 6 } } ; " , tokenizeAndStringify ( " ;foo a[]={{1,2},{3,4},{5,6}}; " ) ) ;
2015-10-08 12:52:28 +02:00
ASSERT_EQUALS ( " ; int a [ 1 ] = { foo < bar1 , bar2 > ( 123 , 4 ) } ; " , tokenizeAndStringify ( " ;int a[]={foo<bar1,bar2>(123,4)}; " ) ) ;
2015-10-08 11:35:51 +02:00
ASSERT_EQUALS ( " ; int a [ 2 ] = { b > c ? 1 : 2 , 3 } ; " , tokenizeAndStringify ( " ;int a[]={ b>c?1:2,3}; " ) ) ;
2015-10-08 12:52:28 +02:00
ASSERT_EQUALS ( " int main ( ) { int a [ 2 ] = { b < c ? 1 : 2 , 3 } } " , tokenizeAndStringify ( " int main(){int a[]={b<c?1:2,3}} " ) ) ;
2015-10-08 11:35:51 +02:00
ASSERT_EQUALS ( " ; int a [ 3 ] = { ABC , 2 , 3 } ; " , tokenizeAndStringify ( " ;int a[]={ABC,2,3}; " ) ) ;
ASSERT_EQUALS ( " ; int a [ 3 ] = { [ 2 ] = 5 } ; " , tokenizeAndStringify ( " ;int a[]={ [2] = 5 }; " ) ) ;
ASSERT_EQUALS ( " ; int a [ 5 ] = { 1 , 2 , [ 2 ] = 5 , 3 , 4 } ; " , tokenizeAndStringify ( " ;int a[]={ 1, 2, [2] = 5, 3, 4 }; " ) ) ;
ASSERT_EQUALS ( " ; int a [ ] = { 1 , 2 , [ x ] = 5 , 3 , 4 } ; " , tokenizeAndStringify ( " ;int a[]={ 1, 2, [x] = 5, 3, 4 }; " ) ) ;
2010-02-21 09:47:41 +01:00
}
2014-11-20 14:20:09 +01:00
void labels ( ) {
2015-10-08 11:35:51 +02:00
ASSERT_EQUALS ( " void f ( ) { ab : ; a = 0 ; } " , tokenizeAndStringify ( " void f() { ab: a=0; } " ) ) ;
2011-10-08 21:13:53 +02:00
//ticket #3176
2015-10-08 11:35:51 +02:00
ASSERT_EQUALS ( " void f ( ) { ab : ; ( * func ) ( ) ; } " , tokenizeAndStringify ( " void f() { ab: (*func)(); } " ) ) ;
2011-10-08 21:13:53 +02:00
//with '*' operator
2015-10-08 11:35:51 +02:00
ASSERT_EQUALS ( " void f ( ) { ab : ; * b = 0 ; } " , tokenizeAndStringify ( " void f() { ab: *b=0; } " ) ) ;
ASSERT_EQUALS ( " void f ( ) { ab : ; * * b = 0 ; } " , tokenizeAndStringify ( " void f() { ab: **b=0; } " ) ) ;
2011-10-08 21:13:53 +02:00
//with '&' operator
2015-10-08 11:35:51 +02:00
ASSERT_EQUALS ( " void f ( ) { ab : ; & b = 0 ; } " , tokenizeAndStringify ( " void f() { ab: &b=0; } " ) ) ;
ASSERT_EQUALS ( " void f ( ) { ab : ; & ( b . x ) = 0 ; } " , tokenizeAndStringify ( " void f() { ab: &(b->x)=0; } " ) ) ;
2013-01-16 15:37:07 +01:00
//with '(' parentheses
2015-10-08 11:35:51 +02:00
ASSERT_EQUALS ( " void f ( ) { ab : ; * ( * b ) . x = 0 ; } " , tokenizeAndStringify ( " void f() { ab: *(* b)->x=0; } " ) ) ;
ASSERT_EQUALS ( " void f ( ) { ab : ; ( * * b ) . x = 0 ; } " , tokenizeAndStringify ( " void f() { ab: (** b).x=0; } " ) ) ;
ASSERT_EQUALS ( " void f ( ) { ab : ; & ( * b . x ) = 0 ; } " , tokenizeAndStringify ( " void f() { ab: &(*b.x)=0; } " ) ) ;
2013-01-16 15:37:07 +01:00
//with '{' parentheses
2015-10-08 11:35:51 +02:00
ASSERT_EQUALS ( " void f ( ) { ab : ; { b = 0 ; } } " , tokenizeAndStringify ( " void f() { ab: {b=0;} } " ) ) ;
ASSERT_EQUALS ( " void f ( ) { ab : ; { * b = 0 ; } } " , tokenizeAndStringify ( " void f() { ab: { *b=0;} } " ) ) ;
ASSERT_EQUALS ( " void f ( ) { ab : ; { & b = 0 ; } } " , tokenizeAndStringify ( " void f() { ab: { &b=0;} } " ) ) ;
ASSERT_EQUALS ( " void f ( ) { ab : ; { & ( * b . x ) = 0 ; } } " , tokenizeAndStringify ( " void f() { ab: {&(*b.x)=0;} } " ) ) ;
2011-10-08 21:13:53 +02:00
//with unhandled MACRO() code
2015-10-08 11:35:51 +02:00
ASSERT_EQUALS ( " void f ( ) { MACRO ( ab : b = 0 ; , foo ) } " , tokenizeAndStringify ( " void f() { MACRO(ab: b=0;, foo)} " ) ) ;
ASSERT_EQUALS ( " void f ( ) { MACRO ( bar , ab : { & ( * b . x ) = 0 ; } ) } " , tokenizeAndStringify ( " void f() { MACRO(bar, ab: {&(*b.x)=0;})} " ) ) ;
2010-02-21 09:47:41 +01:00
}
2010-04-14 19:04:16 +02:00
2014-11-20 14:20:09 +01:00
void simplifyInitVar ( ) {
2010-04-14 19:04:16 +02:00
{
const char code [ ] = " int i ; int p(0); " ;
ASSERT_EQUALS ( " int i ; int p ; p = 0 ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " int i; int *p(0); " ;
ASSERT_EQUALS ( " int i ; int * p ; p = 0 ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " int p(0); " ;
ASSERT_EQUALS ( " int p ; p = 0 ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " int *p(0); " ;
ASSERT_EQUALS ( " int * p ; p = 0 ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " int i ; int p(i); " ;
2010-08-27 22:58:21 +02:00
ASSERT_EQUALS ( " int i ; int p ; p = i ; " , tokenizeAndStringify ( code ) ) ;
2010-04-14 19:04:16 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " int i; int *p(&i); " ;
ASSERT_EQUALS ( " int i ; int * p ; p = & i ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " int i; void *p(&i); " ;
ASSERT_EQUALS ( " int i ; void * p ; p = & i ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " struct S { }; struct S s; struct S *p(&s); " ;
ASSERT_EQUALS ( " struct S { } ; struct S s ; struct S * p ; p = & s ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " struct S { }; S s; S *p(&s); " ;
ASSERT_EQUALS ( " struct S { } ; S s ; S * p ; p = & s ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " union S { int i; float f; }; union S s; union S *p(&s); " ;
ASSERT_EQUALS ( " union S { int i ; float f ; } ; union S s ; union S * p ; p = & s ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " union S { int i; float f; }; S s; S *p(&s); " ;
ASSERT_EQUALS ( " union S { int i ; float f ; } ; S s ; S * p ; p = & s ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " class C { }; class C c; class C *p(&c); " ;
ASSERT_EQUALS ( " class C { } ; class C c ; class C * p ; p = & c ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " class C { }; C c; C *p(&c); " ;
ASSERT_EQUALS ( " class C { } ; C c ; C * p ; p = & c ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " struct S { }; struct S s; struct S s1(s); " ;
ASSERT_EQUALS ( " struct S { } ; struct S s ; struct S s1 ( s ) ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " struct S { }; S s; S s1(s); " ;
ASSERT_EQUALS ( " struct S { } ; S s ; S s1 ( s ) ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " struct S { }; struct S s; struct S s1(&s); " ;
ASSERT_EQUALS ( " struct S { } ; struct S s ; struct S s1 ( & s ) ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " struct S { }; S s; S s1(&s); " ;
ASSERT_EQUALS ( " struct S { } ; S s ; S s1 ( & s ) ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2010-04-15 18:42:11 +02:00
{
const char code [ ] = " class S { int function(); }; " ;
ASSERT_EQUALS ( " class S { int function ( ) ; } ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2010-04-14 19:04:16 +02:00
{
const char code [ ] = " class S { int function(void); }; " ;
2011-10-27 20:54:42 +02:00
ASSERT_EQUALS ( " class S { int function ( ) ; } ; " , tokenizeAndStringify ( code ) ) ;
2010-04-14 19:04:16 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " class S { int function(int); }; " ;
ASSERT_EQUALS ( " class S { int function ( int ) ; } ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " int function(void); " ;
2011-10-27 20:54:42 +02:00
ASSERT_EQUALS ( " int function ( ) ; " , tokenizeAndStringify ( code ) ) ;
2010-04-14 19:04:16 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " int function(int); " ;
ASSERT_EQUALS ( " int function ( int ) ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " extern int function(void); " ;
2011-10-27 20:54:42 +02:00
ASSERT_EQUALS ( " extern int function ( ) ; " , tokenizeAndStringify ( code ) ) ;
2010-04-14 19:04:16 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " int function1(void); int function2(void); " ;
2011-10-27 20:54:42 +02:00
ASSERT_EQUALS ( " int function1 ( ) ; int function2 ( ) ; " , tokenizeAndStringify ( code ) ) ;
2010-04-14 19:04:16 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2010-04-15 18:42:11 +02:00
{
const char code [ ] = " int function(A); " ;
// We can't tell if this a function prototype or a variable without knowing
// what A is. Since A is undefined, just leave it alone.
ASSERT_EQUALS ( " int function ( A ) ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " int i; int function(A); " ;
ASSERT_EQUALS ( " int i ; int function ( A ) ; " , tokenizeAndStringify ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " class A { } ; int foo(A); " ;
2010-08-27 22:58:21 +02:00
ASSERT_EQUALS ( " class A { } ; int foo ( A ) ; " , tokenizeAndStringify ( code ) ) ;
2010-04-15 18:42:11 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " class A { } ; A a; int foo(a); " ;
2010-08-27 22:58:21 +02:00
ASSERT_EQUALS ( " class A { } ; A a ; int foo ; foo = a ; " , tokenizeAndStringify ( code , false ) ) ;
2010-04-15 18:42:11 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-08-27 22:58:21 +02:00
}
{
const char code [ ] = " int x(f()); " ;
ASSERT_EQUALS ( " int x ; x = f ( ) ; " , tokenizeAndStringify ( code , false ) ) ;
2010-04-15 18:42:11 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2012-09-15 11:55:08 +02:00
{
const char code [ ] = " return doSomething(X), 0; " ;
ASSERT_EQUALS ( " return doSomething ( X ) , 0 ; " , tokenizeAndStringify ( code , false ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2015-08-02 09:11:51 +02:00
{
const char code [ ] = " const int x(1); "
" const int y(2); "
" const int z((x+1)*y); "
" f(z); " ;
ASSERT_EQUALS ( " f ( 4 ) ; " , tokenizeAndStringify ( code , true ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " const int x(1); "
" const int y(2); "
" const int z((x+1)*y); "
" f(&z); " ;
ASSERT_EQUALS ( " const int z ( 4 ) ; f ( & z ) ; " , tokenizeAndStringify ( code , true ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " const bool x(true); "
" const bool y(!x); "
" f(y); " ;
ASSERT_EQUALS ( " f ( false ) ; " , tokenizeAndStringify ( code , true ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " const bool x(true); "
" const bool y(!x); "
" f(&y); " ;
ASSERT_EQUALS ( " const bool y ( false ) ; f ( & y ) ; " , tokenizeAndStringify ( code , true ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2010-04-14 19:04:16 +02:00
}
2010-08-15 11:54:28 +02:00
2014-11-20 14:20:09 +01:00
void simplifyInitVar2 ( ) {
2013-10-31 17:20:00 +01:00
// ticket #5131 - unsigned
const char code [ ] = " void f() { \n "
" unsigned int a(0),b(0); \n "
" } " ;
ASSERT_EQUALS ( " void f ( ) { \n "
" unsigned int a ; a = 0 ; unsigned int b ; b = 0 ; \n "
" } " , tokenizeAndStringify ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyInitVar3 ( ) {
2013-10-31 17:20:00 +01:00
const char code [ ] = " void f() { \n "
" int *a(0),b(0); \n "
" } " ;
ASSERT_EQUALS ( " void f ( ) { \n "
" int * a ; a = 0 ; int b ; b = 0 ; \n "
" } " , tokenizeAndStringify ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void bitfields1 ( ) {
2010-08-18 22:42:04 +02:00
const char code1 [ ] = " struct A { bool x : 1; }; " ;
ASSERT_EQUALS ( " struct A { bool x ; } ; " , tokenizeAndStringify ( code1 , false ) ) ;
const char code2 [ ] = " struct A { char x : 3; }; " ;
ASSERT_EQUALS ( " struct A { char x ; } ; " , tokenizeAndStringify ( code2 , false ) ) ;
const char code3 [ ] = " struct A { short x : 3; }; " ;
ASSERT_EQUALS ( " struct A { short x ; } ; " , tokenizeAndStringify ( code3 , false ) ) ;
const char code4 [ ] = " struct A { int x : 3; }; " ;
ASSERT_EQUALS ( " struct A { int x ; } ; " , tokenizeAndStringify ( code4 , false ) ) ;
const char code5 [ ] = " struct A { long x : 3; }; " ;
ASSERT_EQUALS ( " struct A { long x ; } ; " , tokenizeAndStringify ( code5 , false ) ) ;
2010-08-22 13:25:47 +02:00
const char code6 [ ] = " struct A { __int8 x : 3; }; " ;
2014-10-21 06:11:19 +02:00
ASSERT_EQUALS ( " struct A { char x ; } ; " , tokenizeAndStringifyWindows ( code6 , false , true , Settings : : Win32A ) ) ;
2010-08-18 22:42:04 +02:00
2010-08-22 13:25:47 +02:00
const char code7 [ ] = " struct A { __int16 x : 3; }; " ;
2014-10-21 06:11:19 +02:00
ASSERT_EQUALS ( " struct A { short x ; } ; " , tokenizeAndStringifyWindows ( code7 , false , true , Settings : : Win32A ) ) ;
2010-08-18 22:42:04 +02:00
2010-08-22 13:25:47 +02:00
const char code8 [ ] = " struct A { __int32 x : 3; }; " ;
2014-10-21 06:11:19 +02:00
ASSERT_EQUALS ( " struct A { int x ; } ; " , tokenizeAndStringifyWindows ( code8 , false , true , Settings : : Win32A ) ) ;
2010-08-18 22:42:04 +02:00
2010-08-22 13:25:47 +02:00
const char code9 [ ] = " struct A { __int64 x : 3; }; " ;
2014-10-21 06:11:19 +02:00
ASSERT_EQUALS ( " struct A { long long x ; } ; " , tokenizeAndStringifyWindows ( code9 , false , true , Settings : : Win32A ) ) ;
2010-08-18 22:42:04 +02:00
2010-08-22 13:25:47 +02:00
const char code10 [ ] = " struct A { unsigned char x : 3; }; " ;
ASSERT_EQUALS ( " struct A { unsigned char x ; } ; " , tokenizeAndStringify ( code10 , false ) ) ;
2010-08-16 18:51:25 +02:00
2010-08-22 13:25:47 +02:00
const char code11 [ ] = " struct A { unsigned short x : 3; }; " ;
ASSERT_EQUALS ( " struct A { unsigned short x ; } ; " , tokenizeAndStringify ( code11 , false ) ) ;
2010-08-18 22:42:04 +02:00
2010-08-22 13:25:47 +02:00
const char code12 [ ] = " struct A { unsigned int x : 3; }; " ;
ASSERT_EQUALS ( " struct A { unsigned int x ; } ; " , tokenizeAndStringify ( code12 , false ) ) ;
2010-08-18 22:42:04 +02:00
2010-08-22 13:25:47 +02:00
const char code13 [ ] = " struct A { unsigned long x : 3; }; " ;
ASSERT_EQUALS ( " struct A { unsigned long x ; } ; " , tokenizeAndStringify ( code13 , false ) ) ;
2010-08-22 09:41:22 +02:00
2010-08-22 13:25:47 +02:00
const char code14 [ ] = " struct A { unsigned __int8 x : 3; }; " ;
2014-10-21 06:11:19 +02:00
ASSERT_EQUALS ( " struct A { unsigned char x ; } ; " , tokenizeAndStringifyWindows ( code14 , false , true , Settings : : Win32A ) ) ;
2010-08-22 09:41:22 +02:00
2010-08-22 13:25:47 +02:00
const char code15 [ ] = " struct A { unsigned __int16 x : 3; }; " ;
2014-10-21 06:11:19 +02:00
ASSERT_EQUALS ( " struct A { unsigned short x ; } ; " , tokenizeAndStringifyWindows ( code15 , false , true , Settings : : Win32A ) ) ;
2010-08-22 09:41:22 +02:00
2010-08-22 13:25:47 +02:00
const char code16 [ ] = " struct A { unsigned __int32 x : 3; }; " ;
2014-10-21 06:11:19 +02:00
ASSERT_EQUALS ( " struct A { unsigned int x ; } ; " , tokenizeAndStringifyWindows ( code16 , false , true , Settings : : Win32A ) ) ;
2010-08-22 13:25:47 +02:00
const char code17 [ ] = " struct A { unsigned __int64 x : 3; }; " ;
2014-10-21 06:11:19 +02:00
ASSERT_EQUALS ( " struct A { unsigned long long x ; } ; " , tokenizeAndStringifyWindows ( code17 , false , true , Settings : : Win32A ) ) ;
2010-08-22 13:25:47 +02:00
const char code18 [ ] = " struct A { signed char x : 3; }; " ;
ASSERT_EQUALS ( " struct A { signed char x ; } ; " , tokenizeAndStringify ( code18 , false ) ) ;
const char code19 [ ] = " struct A { signed short x : 3; }; " ;
ASSERT_EQUALS ( " struct A { signed short x ; } ; " , tokenizeAndStringify ( code19 , false ) ) ;
const char code20 [ ] = " struct A { signed int x : 3; }; " ;
ASSERT_EQUALS ( " struct A { signed int x ; } ; " , tokenizeAndStringify ( code20 , false ) ) ;
const char code21 [ ] = " struct A { signed long x : 3; }; " ;
2014-10-21 06:11:19 +02:00
ASSERT_EQUALS ( " struct A { signed long x ; } ; " , tokenizeAndStringifyWindows ( code21 , false ) ) ;
2010-08-22 13:25:47 +02:00
const char code22 [ ] = " struct A { signed __int8 x : 3; }; " ;
2014-10-21 06:11:19 +02:00
ASSERT_EQUALS ( " struct A { signed char x ; } ; " , tokenizeAndStringifyWindows ( code22 , false , true , Settings : : Win32A ) ) ;
2010-08-22 13:25:47 +02:00
const char code23 [ ] = " struct A { signed __int16 x : 3; }; " ;
2014-10-21 06:11:19 +02:00
ASSERT_EQUALS ( " struct A { signed short x ; } ; " , tokenizeAndStringifyWindows ( code23 , false , true , Settings : : Win32A ) ) ;
2010-08-22 13:25:47 +02:00
const char code24 [ ] = " struct A { signed __int32 x : 3; }; " ;
2014-10-21 06:11:19 +02:00
ASSERT_EQUALS ( " struct A { signed int x ; } ; " , tokenizeAndStringifyWindows ( code24 , false , true , Settings : : Win32A ) ) ;
2010-08-22 13:25:47 +02:00
const char code25 [ ] = " struct A { signed __int64 x : 3; }; " ;
2014-10-21 06:11:19 +02:00
ASSERT_EQUALS ( " struct A { signed long long x ; } ; " , tokenizeAndStringifyWindows ( code25 , false , true , Settings : : Win32A ) ) ;
2010-08-18 22:42:04 +02:00
}
2014-11-20 14:20:09 +01:00
void bitfields2 ( ) {
2010-08-18 22:42:04 +02:00
const char code1 [ ] = " struct A { public: int x : 3; }; " ;
ASSERT_EQUALS ( " struct A { public: int x ; } ; " , tokenizeAndStringify ( code1 , false ) ) ;
const char code2 [ ] = " struct A { public: unsigned long x : 3; }; " ;
ASSERT_EQUALS ( " struct A { public: unsigned long x ; } ; " , tokenizeAndStringify ( code2 , false ) ) ;
const char code3 [ ] = " struct A { protected: int x : 3; }; " ;
ASSERT_EQUALS ( " struct A { protected: int x ; } ; " , tokenizeAndStringify ( code3 , false ) ) ;
const char code4 [ ] = " struct A { protected: unsigned long x : 3; }; " ;
ASSERT_EQUALS ( " struct A { protected: unsigned long x ; } ; " , tokenizeAndStringify ( code4 , false ) ) ;
const char code5 [ ] = " struct A { private: int x : 3; }; " ;
ASSERT_EQUALS ( " struct A { private: int x ; } ; " , tokenizeAndStringify ( code5 , false ) ) ;
const char code6 [ ] = " struct A { private: unsigned long x : 3; }; " ;
ASSERT_EQUALS ( " struct A { private: unsigned long x ; } ; " , tokenizeAndStringify ( code6 , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void bitfields3 ( ) {
2010-08-18 22:42:04 +02:00
const char code1 [ ] = " struct A { const int x : 3; }; " ;
ASSERT_EQUALS ( " struct A { const int x ; } ; " , tokenizeAndStringify ( code1 , false ) ) ;
const char code2 [ ] = " struct A { const unsigned long x : 3; }; " ;
ASSERT_EQUALS ( " struct A { const unsigned long x ; } ; " , tokenizeAndStringify ( code2 , false ) ) ;
const char code3 [ ] = " struct A { public: const int x : 3; }; " ;
ASSERT_EQUALS ( " struct A { public: const int x ; } ; " , tokenizeAndStringify ( code3 , false ) ) ;
const char code4 [ ] = " struct A { public: const unsigned long x : 3; }; " ;
ASSERT_EQUALS ( " struct A { public: const unsigned long x ; } ; " , tokenizeAndStringify ( code4 , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void bitfields4 ( ) { // ticket #1956
2010-08-21 16:34:41 +02:00
const char code1 [ ] = " struct A { CHAR x : 3; }; " ;
ASSERT_EQUALS ( " struct A { CHAR x ; } ; " , tokenizeAndStringify ( code1 , false ) ) ;
const char code2 [ ] = " struct A { UCHAR x : 3; }; " ;
ASSERT_EQUALS ( " struct A { UCHAR x ; } ; " , tokenizeAndStringify ( code2 , false ) ) ;
const char code3 [ ] = " struct A { BYTE x : 3; }; " ;
ASSERT_EQUALS ( " struct A { BYTE x ; } ; " , tokenizeAndStringify ( code3 , false ) ) ;
const char code4 [ ] = " struct A { WORD x : 3; }; " ;
ASSERT_EQUALS ( " struct A { WORD x ; } ; " , tokenizeAndStringify ( code4 , false ) ) ;
const char code5 [ ] = " struct A { DWORD x : 3; }; " ;
ASSERT_EQUALS ( " struct A { DWORD x ; } ; " , tokenizeAndStringify ( code5 , false ) ) ;
const char code6 [ ] = " struct A { LONG x : 3; }; " ;
ASSERT_EQUALS ( " struct A { LONG x ; } ; " , tokenizeAndStringify ( code6 , false ) ) ;
const char code7 [ ] = " struct A { UINT8 x : 3; }; " ;
ASSERT_EQUALS ( " struct A { UINT8 x ; } ; " , tokenizeAndStringify ( code7 , false ) ) ;
const char code8 [ ] = " struct A { UINT16 x : 3; }; " ;
ASSERT_EQUALS ( " struct A { UINT16 x ; } ; " , tokenizeAndStringify ( code8 , false ) ) ;
const char code9 [ ] = " struct A { UINT32 x : 3; }; " ;
ASSERT_EQUALS ( " struct A { UINT32 x ; } ; " , tokenizeAndStringify ( code9 , false ) ) ;
const char code10 [ ] = " struct A { UINT64 x : 3; }; " ;
ASSERT_EQUALS ( " struct A { UINT64 x ; } ; " , tokenizeAndStringify ( code10 , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void bitfields5 ( ) { // ticket #1956
2010-08-22 09:41:22 +02:00
const char code1 [ ] = " struct RGB { unsigned int r : 3, g : 3, b : 2; }; " ;
ASSERT_EQUALS ( " struct RGB { unsigned int r ; unsigned int g ; unsigned int b ; } ; " , tokenizeAndStringify ( code1 , false ) ) ;
2010-08-22 13:25:47 +02:00
const char code2 [ ] = " struct A { int a : 3; int : 3; int c : 3; }; " ;
ASSERT_EQUALS ( " struct A { int a ; int c ; } ; " , tokenizeAndStringify ( code2 , false ) ) ;
2010-08-23 07:29:05 +02:00
const char code3 [ ] = " struct A { virtual void f() {} int f1 : 1; }; " ;
ASSERT_EQUALS ( " struct A { virtual void f ( ) { } int f1 ; } ; " , tokenizeAndStringify ( code3 , false ) ) ;
2011-02-21 00:22:49 +01:00
}
2014-11-20 14:20:09 +01:00
void bitfields6 ( ) { // ticket #2595
2011-02-21 00:22:49 +01:00
const char code1 [ ] = " struct A { bool b : true; }; " ;
ASSERT_EQUALS ( " struct A { bool b ; } ; " , tokenizeAndStringify ( code1 , false ) ) ;
const char code2 [ ] = " struct A { bool b : true, c : true; }; " ;
ASSERT_EQUALS ( " struct A { bool b ; bool c ; } ; " , tokenizeAndStringify ( code2 , false ) ) ;
const char code3 [ ] = " struct A { bool : true; }; " ;
ASSERT_EQUALS ( " struct A { } ; " , tokenizeAndStringify ( code3 , false ) ) ;
2011-03-01 01:35:00 +01:00
const char code4 [ ] = " void f(int a) { switch (a) { case b: break; } } " ;
ASSERT_EQUALS ( " void f ( int a ) { switch ( a ) { case b : ; break ; } } " , tokenizeAndStringify ( code4 , true ) ) ;
const char code5 [ ] = " void f(int a) { switch (a) { default: break; } } " ;
ASSERT_EQUALS ( " void f ( int a ) { switch ( a ) { default : ; break ; } } " , tokenizeAndStringify ( code5 , true ) ) ;
2010-08-22 09:41:22 +02:00
}
2014-11-20 14:20:09 +01:00
void bitfields7 ( ) { // ticket #1987
2011-03-19 21:00:43 +01:00
const char code [ ] = " typedef struct Descriptor { "
" unsigned element_size: 8* sizeof( unsigned ); "
" } Descriptor; " ;
const char expected [ ] = " struct Descriptor { "
" unsigned int element_size ; "
" } ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , false ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void bitfields8 ( ) {
2011-03-22 01:17:14 +01:00
const char code [ ] = " struct A; "
" class B : virtual public C "
" { "
" int f(); "
" }; " ;
const char expected [ ] = " struct A ; "
" class B : virtual public C "
" { "
" int f ( ) ; "
" } ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , false ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void bitfields9 ( ) { // ticket #2706
2011-04-05 04:18:12 +02:00
const char code [ ] = " void f() { \n "
" goto half; \n "
" half: \n "
" { \n "
" ; \n "
" } \n "
" }; " ;
tokenizeAndStringify ( code , false ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void bitfields10 ( ) { // ticket #2737
2011-05-19 17:04:36 +02:00
const char code [ ] = " {} "
" MACRO "
" default: { } "
" ; " ;
ASSERT_EQUALS ( " { } MACRO default : { } ; " , tokenizeAndStringify ( code , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void bitfields11 ( ) { // ticket #2845 (segmentation fault)
2011-06-21 13:31:55 +02:00
const char code [ ] = " #if b&&a \n "
" #ifdef y z: \n " ;
tokenizeAndStringify ( code , false ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void bitfields12 ( ) { // ticket #3485 (segmentation fault)
2012-01-09 16:24:11 +01:00
const char code [ ] = " {a:1;}; \n " ;
ASSERT_EQUALS ( " { } ; " , tokenizeAndStringify ( code , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void bitfields13 ( ) { // ticket #3502 (segmentation fault)
2012-01-23 07:39:31 +01:00
ASSERT_EQUALS ( " x y ; " , tokenizeAndStringify ( " struct{x y:}; \n " , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void bitfields14 ( ) { // #4561 - crash for 'signals:'
2013-02-07 17:03:08 +01:00
ASSERT_EQUALS ( " class x { signals : } ; " , tokenizeAndStringify ( " class x { signals: }; \n " , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyNamespaceStd ( ) {
2012-07-15 11:05:19 +02:00
static const char code1 [ ] = " map<foo, bar> m; " ; // namespace std is not used
ASSERT_EQUALS ( " map < foo , bar > m ; " , tokenizeAndStringify ( code1 , false ) ) ;
static const char code2 [ ] = " using namespace std; \n "
" map<foo, bar> m; " ;
ASSERT_EQUALS ( " std :: map < foo , bar > m ; " , tokenizeAndStringify ( code2 , false ) ) ;
static const char code3 [ ] = " using namespace std; \n "
" string s; " ;
ASSERT_EQUALS ( " std :: string s ; " , tokenizeAndStringify ( code3 , false ) ) ;
static const char code4 [ ] = " using namespace std; \n "
" void foo() {swap(a, b); } " ;
ASSERT_EQUALS ( " void foo ( ) { std :: swap ( a , b ) ; } " , tokenizeAndStringify ( code4 , false ) ) ;
static const char code5 [ ] = " using namespace std; \n "
" void foo() {map(a, b); } " ; // Thats obviously not std::map<>
ASSERT_EQUALS ( " void foo ( ) { map ( a , b ) ; } " , tokenizeAndStringify ( code5 , false ) ) ;
static const char code6 [ ] = " using namespace std; \n "
" string<wchar_t> s; " ; // Thats obviously not std::string
ASSERT_EQUALS ( " string < wchar_t > s ; " , tokenizeAndStringify ( code6 , false ) ) ;
static const char code7 [ ] = " using namespace std; \n "
" swap s; " ; // Thats obviously not std::swap
ASSERT_EQUALS ( " swap s ; " , tokenizeAndStringify ( code7 , false ) ) ;
static const char code8 [ ] = " using namespace std; \n "
" std::string s; " ;
ASSERT_EQUALS ( " std :: string s ; " , tokenizeAndStringify ( code8 , false ) ) ;
static const char code9 [ ] = " using namespace std; \n "
" tr1::function <void(int)> f; " ;
ASSERT_EQUALS ( " tr1 :: function < void ( int ) > f ; " , tokenizeAndStringify ( code9 , false , true , Settings : : Unspecified , " test.cpp " , false ) ) ;
ASSERT_EQUALS ( " std :: function < void ( int ) > f ; " , tokenizeAndStringify ( code9 , false , true , Settings : : Unspecified , " test.cpp " , true ) ) ;
static const char code10 [ ] = " std::tr1::function <void(int)> f; " ;
ASSERT_EQUALS ( " std :: tr1 :: function < void ( int ) > f ; " , tokenizeAndStringify ( code10 , false , true , Settings : : Unspecified , " test.cpp " , false ) ) ;
ASSERT_EQUALS ( " std :: function < void ( int ) > f ; " , tokenizeAndStringify ( code10 , false , true , Settings : : Unspecified , " test.cpp " , true ) ) ;
2013-07-11 07:13:47 +02:00
// #4042 (Do not add 'std ::' to variables)
static const char code11 [ ] = " using namespace std; \n "
" const char * string = \" Hi \" ; " ;
ASSERT_EQUALS ( " const char * string ; string = \" Hi \" ; " , tokenizeAndStringify ( code11 , false ) ) ;
static const char code12 [ ] = " using namespace std; \n "
" string f(const char * string) { \n "
" cout << string << endl; \n "
" return string; \n "
" } " ;
static const char expected12 [ ] = " std :: string f ( const char * string ) { \n "
" std :: cout << string << std :: endl ; \n "
" return string ; \n "
" } " ;
ASSERT_EQUALS ( expected12 , tokenizeAndStringify ( code12 , false ) ) ;
static const char code13 [ ] = " using namespace std; \n "
" try { } \n "
" catch(std::exception &exception) { } " ;
static const char expected13 [ ] = " try { } \n "
" catch ( std :: exception & exception ) { } " ;
ASSERT_EQUALS ( expected13 , tokenizeAndStringify ( code13 , false ) ) ;
2014-05-09 21:57:34 +02:00
// #5773 (Don't prepend 'std ::' to function definitions)
static const char code14 [ ] = " using namespace std; \n "
" class C { \n "
" void search() {} \n "
" void search() const {} \n "
" void search() THROW_MACRO {} \n "
" }; " ;
static const char expected14 [ ] = " class C { \n "
" void search ( ) { } \n "
" void search ( ) const { } \n "
2015-02-28 11:09:39 +01:00
" void search ( ) { } \n "
2014-05-09 21:57:34 +02:00
" } ; " ;
ASSERT_EQUALS ( expected14 , tokenizeAndStringify ( code14 , false ) ) ;
2012-07-15 11:05:19 +02:00
}
2014-11-20 14:20:09 +01:00
void microsoftMemory ( ) {
2012-03-27 21:24:46 +02:00
const char code1a [ ] = " void foo() { int a[10], b[10]; CopyMemory(a, b, sizeof(a)); } " ;
ASSERT_EQUALS ( " void foo ( ) { int a [ 10 ] ; int b [ 10 ] ; memcpy ( a , b , sizeof ( a ) ) ; } " , tokenizeAndStringify ( code1a , false , true , Settings : : Win32A ) ) ;
const char code1b [ ] = " void foo() { int a[10], b[10]; RtlCopyMemory(a, b, sizeof(a)); } " ;
ASSERT_EQUALS ( " void foo ( ) { int a [ 10 ] ; int b [ 10 ] ; memcpy ( a , b , sizeof ( a ) ) ; } " , tokenizeAndStringify ( code1b , false , true , Settings : : Win32A ) ) ;
const char code1c [ ] = " void foo() { int a[10], b[10]; RtlCopyBytes(a, b, sizeof(a)); } " ;
ASSERT_EQUALS ( " void foo ( ) { int a [ 10 ] ; int b [ 10 ] ; memcpy ( a , b , sizeof ( a ) ) ; } " , tokenizeAndStringify ( code1c , false , true , Settings : : Win32A ) ) ;
const char code2a [ ] = " void foo() { int a[10]; FillMemory(a, sizeof(a), 255); } " ;
ASSERT_EQUALS ( " void foo ( ) { int a [ 10 ] ; memset ( a , 255 , sizeof ( a ) ) ; } " , tokenizeAndStringify ( code2a , false , true , Settings : : Win32A ) ) ;
const char code2b [ ] = " void foo() { int a[10]; RtlFillMemory(a, sizeof(a), 255); } " ;
ASSERT_EQUALS ( " void foo ( ) { int a [ 10 ] ; memset ( a , 255 , sizeof ( a ) ) ; } " , tokenizeAndStringify ( code2b , false , true , Settings : : Win32A ) ) ;
const char code2c [ ] = " void foo() { int a[10]; RtlFillBytes(a, sizeof(a), 255); } " ;
ASSERT_EQUALS ( " void foo ( ) { int a [ 10 ] ; memset ( a , 255 , sizeof ( a ) ) ; } " , tokenizeAndStringify ( code2c , false , true , Settings : : Win32A ) ) ;
const char code3a [ ] = " void foo() { int a[10], b[10]; MoveMemory(a, b, sizeof(a)); } " ;
ASSERT_EQUALS ( " void foo ( ) { int a [ 10 ] ; int b [ 10 ] ; memmove ( a , b , sizeof ( a ) ) ; } " , tokenizeAndStringify ( code3a , false , true , Settings : : Win32A ) ) ;
const char code3b [ ] = " void foo() { int a[10], b[10]; RtlMoveMemory(a, b, sizeof(a)); } " ;
ASSERT_EQUALS ( " void foo ( ) { int a [ 10 ] ; int b [ 10 ] ; memmove ( a , b , sizeof ( a ) ) ; } " , tokenizeAndStringify ( code3b , false , true , Settings : : Win32A ) ) ;
const char code4a [ ] = " void foo() { int a[10]; ZeroMemory(a, sizeof(a)); } " ;
ASSERT_EQUALS ( " void foo ( ) { int a [ 10 ] ; memset ( a , 0 , sizeof ( a ) ) ; } " , tokenizeAndStringify ( code4a , false , true , Settings : : Win32A ) ) ;
const char code4b [ ] = " void foo() { int a[10]; RtlZeroMemory(a, sizeof(a)); } " ;
ASSERT_EQUALS ( " void foo ( ) { int a [ 10 ] ; memset ( a , 0 , sizeof ( a ) ) ; } " , tokenizeAndStringify ( code4b , false , true , Settings : : Win32A ) ) ;
const char code4c [ ] = " void foo() { int a[10]; RtlZeroBytes(a, sizeof(a)); } " ;
ASSERT_EQUALS ( " void foo ( ) { int a [ 10 ] ; memset ( a , 0 , sizeof ( a ) ) ; } " , tokenizeAndStringify ( code4c , false , true , Settings : : Win32A ) ) ;
const char code4d [ ] = " void foo() { int a[10]; RtlSecureZeroMemory(a, sizeof(a)); } " ;
ASSERT_EQUALS ( " void foo ( ) { int a [ 10 ] ; memset ( a , 0 , sizeof ( a ) ) ; } " , tokenizeAndStringify ( code4d , false , true , Settings : : Win32A ) ) ;
const char code5 [ ] = " void foo() { int a[10], b[10]; RtlCompareMemory(a, b, sizeof(a)); } " ;
ASSERT_EQUALS ( " void foo ( ) { int a [ 10 ] ; int b [ 10 ] ; memcmp ( a , b , sizeof ( a ) ) ; } " , tokenizeAndStringify ( code5 , false , true , Settings : : Win32A ) ) ;
const char code6 [ ] = " void foo() { ZeroMemory(f(1, g(a, b)), h(i, j(0, 1))); } " ;
ASSERT_EQUALS ( " void foo ( ) { memset ( f ( 1 , g ( a , b ) ) , 0 , h ( i , j ( 0 , 1 ) ) ) ; } " , tokenizeAndStringify ( code6 , false , true , Settings : : Win32A ) ) ;
const char code7 [ ] = " void foo() { FillMemory(f(1, g(a, b)), h(i, j(0, 1)), 255); } " ;
ASSERT_EQUALS ( " void foo ( ) { memset ( f ( 1 , g ( a , b ) ) , 255 , h ( i , j ( 0 , 1 ) ) ) ; } " , tokenizeAndStringify ( code7 , false , true , Settings : : Win32A ) ) ;
2011-09-23 01:59:56 +02:00
}
2014-11-20 14:20:09 +01:00
void borland ( ) {
2010-09-01 18:10:12 +02:00
// __closure
ASSERT_EQUALS ( " int * a ; " ,
2015-06-17 09:09:23 +02:00
tokenizeAndStringify ( " int (__closure *a)(); " , false , true , Settings : : Win32A ) ) ;
2010-09-01 18:10:12 +02:00
// __property
2010-09-07 18:37:43 +02:00
ASSERT_EQUALS ( " class Fred { ; __property ; } ; " ,
2015-06-16 22:45:33 +02:00
tokenizeAndStringify ( " class Fred { __property int x = { } }; " , false , true , Settings : : Win32A ) ) ;
2010-09-01 18:10:12 +02:00
}
2014-11-20 14:20:09 +01:00
void Qt ( ) {
2010-12-03 08:14:09 +01:00
const char code1 [ ] = " class Counter : public QObject "
" { "
" Q_OBJECT "
" public: "
" Counter() { m_value = 0; } "
" int value() const { return m_value; } "
" public slots: "
" void setValue(int value); "
" signals: "
" void valueChanged(int newValue); "
" private: "
" int m_value; "
2011-08-26 02:54:35 +02:00
" }; "
" void Counter::setValue(int value) "
" { "
" if (value != m_value) { "
" m_value = value; "
" emit valueChanged(value); "
" } "
" } " ;
2010-12-03 08:14:09 +01:00
const char result1 [ ] = " class Counter : public QObject "
" { "
" public: "
" Counter ( ) { m_value = 0 ; } "
" int value ( ) const { return m_value ; } "
" public: "
" void setValue ( int value ) ; "
" protected: "
" void valueChanged ( int newValue ) ; "
" private: "
" int m_value ; "
2011-08-26 02:54:35 +02:00
" } ; "
" void Counter :: setValue ( int value ) "
" { "
" if ( value != m_value ) { "
" m_value = value ; "
" valueChanged ( value ) ; "
" } "
" } " ;
2010-12-03 08:14:09 +01:00
ASSERT_EQUALS ( result1 , tokenizeAndStringify ( code1 , false ) ) ;
const char code2 [ ] = " class Counter : public QObject "
" { "
" Q_OBJECT "
" public: "
" Counter() { m_value = 0; } "
" int value() const { return m_value; } "
" public Q_SLOTS: "
" void setValue(int value); "
" Q_SIGNALS: "
" void valueChanged(int newValue); "
" private: "
" int m_value; "
2011-08-26 02:54:35 +02:00
" }; "
" void Counter::setValue(int value) "
" { "
" if (value != m_value) { "
" m_value = value; "
" emit valueChanged(value); "
" } "
" } " ;
2010-12-03 08:14:09 +01:00
const char result2 [ ] = " class Counter : public QObject "
" { "
" public: "
" Counter ( ) { m_value = 0 ; } "
" int value ( ) const { return m_value ; } "
" public: "
" void setValue ( int value ) ; "
" protected: "
" void valueChanged ( int newValue ) ; "
" private: "
" int m_value ; "
2011-08-26 02:54:35 +02:00
" } ; "
" void Counter :: setValue ( int value ) "
" { "
" if ( value != m_value ) { "
" m_value = value ; "
" valueChanged ( value ) ; "
" } "
" } " ;
2010-12-03 08:14:09 +01:00
ASSERT_EQUALS ( result2 , tokenizeAndStringify ( code2 , false ) ) ;
2011-09-16 22:59:43 +02:00
const char code3 [ ] = " class MyObject : public QObject { "
" MyObject() {} "
" ~MyObject() {} "
" public slots: "
" signals: "
" void test() {} "
" }; " ;
const char result3 [ ] = " class MyObject : public QObject { "
" MyObject ( ) { } "
" ~ MyObject ( ) { } "
" public: "
" protected: "
" void test ( ) { } "
" } ; " ;
ASSERT_EQUALS ( result3 , tokenizeAndStringify ( code3 , false ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-12-02 17:41:49 +01:00
}
2014-11-20 14:20:09 +01:00
void simplifySQL ( ) {
2010-08-31 21:40:51 +02:00
// Oracle PRO*C extensions for inline SQL. Just replace the SQL with "asm()" to fix wrong error messages
// ticket: #1959
2011-12-13 21:42:38 +01:00
ASSERT_EQUALS ( " asm ( \" \" EXEC SQL SELECT A FROM B \" \" ) ; " , tokenizeAndStringify ( " EXEC SQL SELECT A FROM B; " , false ) ) ;
ASSERT_EQUALS ( " asm ( \" \" EXEC SQL \" \" ) ; " , tokenizeAndStringify ( " EXEC SQL " , false ) ) ;
2010-08-31 21:40:51 +02:00
}
2010-09-09 17:43:09 +02:00
2014-11-20 14:20:09 +01:00
void simplifyCAlternativeTokens ( ) {
2014-03-16 18:51:05 +01:00
ASSERT_EQUALS ( " void f ( ) { if ( a && b ) { ; } } " , tokenizeAndStringify ( " void f() { if (a and b); } " ) ) ;
ASSERT_EQUALS ( " void f ( ) { if ( a || b ) { ; } } " , tokenizeAndStringify ( " void f() { if (a or b); } " ) ) ;
ASSERT_EQUALS ( " void f ( ) { if ( a & b ) { ; } } " , tokenizeAndStringify ( " void f() { if (a bitand b); } " ) ) ;
ASSERT_EQUALS ( " void f ( ) { if ( a | b ) { ; } } " , tokenizeAndStringify ( " void f() { if (a bitor b); } " ) ) ;
ASSERT_EQUALS ( " void f ( ) { if ( a ^ b ) { ; } } " , tokenizeAndStringify ( " void f() { if (a xor b); } " ) ) ;
ASSERT_EQUALS ( " void f ( ) { if ( ~ b ) { ; } } " , tokenizeAndStringify ( " void f() { if (compl b); } " ) ) ;
ASSERT_EQUALS ( " void f ( ) { if ( ! b ) { ; } } " , tokenizeAndStringify ( " void f() { if (not b); } " ) ) ;
ASSERT_EQUALS ( " void f ( ) { if ( a != b ) { ; } } " , tokenizeAndStringify ( " void f() { if (a not_eq b); } " ) ) ;
2014-10-01 08:41:28 +02:00
// #6201
ASSERT_EQUALS ( " void f ( ) { if ( ! c || ! memcmp ( a , b , s ) ) { ; } } " , tokenizeAndStringify ( " void f() { if (!c or !memcmp(a, b, s)); } " ) ) ;
2014-08-06 08:38:30 +02:00
ASSERT_EQUALS ( " \n " // #6029
" \n "
" ##file 0 \n "
" 1: void f ( bool b@1 ) { if ( ! b@1 ) { ; } } \n " ,
tokenizeDebugListing ( " void f(bool b) { if (not b); } " ) ) ;
2010-09-09 17:43:09 +02:00
}
2010-09-09 19:40:36 +02:00
2014-11-20 14:20:09 +01:00
void simplifyCalculations ( ) {
2011-08-06 23:53:06 +02:00
ASSERT_EQUALS ( " void foo ( char str [ ] ) { char x ; x = * str ; } " ,
tokenizeAndStringify ( " void foo ( char str [ ] ) { char x = 0 | ( * str ) ; } " , true ) ) ;
2011-08-06 06:50:29 +02:00
ASSERT_EQUALS ( " void foo ( ) { if ( b ) { } } " ,
tokenizeAndStringify ( " void foo ( ) { if (b | 0) { } } " , true ) ) ;
ASSERT_EQUALS ( " void foo ( ) { if ( b ) { } } " ,
tokenizeAndStringify ( " void foo ( ) { if (0 | b) { } } " , true ) ) ;
ASSERT_EQUALS ( " void foo ( int b ) { int a ; a = b ; bar ( a ) ; } " ,
tokenizeAndStringify ( " void foo ( int b ) { int a = b | 0 ; bar ( a ) ; } " , true ) ) ;
ASSERT_EQUALS ( " void foo ( int b ) { int a ; a = b ; bar ( a ) ; } " ,
tokenizeAndStringify ( " void foo ( int b ) { int a = 0 | b ; bar ( a ) ; } " , true ) ) ;
2011-09-10 13:28:54 +02:00
// ticket #3093
2011-12-02 23:01:55 +01:00
ASSERT_EQUALS ( " int f ( ) { return 15 ; } " ,
2011-09-10 13:28:54 +02:00
tokenizeAndStringify ( " int f() { int a = 10; int b = 5; return a + b; } " , true ) ) ;
ASSERT_EQUALS ( " int f ( ) { return a ; } " ,
tokenizeAndStringify ( " int f() { return a * 1; } " , true ) ) ;
ASSERT_EQUALS ( " int f ( int a ) { return 0 ; } " ,
tokenizeAndStringify ( " int f(int a) { return 0 * a; } " , true ) ) ;
2011-09-11 00:44:40 +02:00
ASSERT_EQUALS ( " bool f ( int i ) { switch ( i ) { case 15 : ; return true ; } } " ,
tokenizeAndStringify ( " bool f(int i) { switch (i) { case 10 + 5: return true; } } " , true ) ) ;
2012-03-07 20:31:23 +01:00
// ticket #3576 - False positives in boolean expressions
ASSERT_EQUALS ( " int foo ( ) { return 1 ; } " ,
tokenizeAndStringify ( " int foo ( ) { int i; int j; i = 1 || j; return i; } " , true ) ) ;
ASSERT_EQUALS ( " int foo ( ) { return 0 ; } " ,
2012-06-14 21:47:03 +02:00
tokenizeAndStringify ( " int foo ( ) { int i; int j; i = 0 && j; return i; } " , true ) ) ; // ticket #3576 - False positives in boolean expressions
// ticket #3723 - Simplify condition (0 && a < 123)
ASSERT_EQUALS ( " ( 0 ) " ,
tokenizeAndStringify ( " ( 0 && a < 123 ) " , true ) ) ;
2012-08-20 18:10:32 +02:00
// ticket #3964 - simplify numeric calculations in tokenization
ASSERT_EQUALS ( " char a [ 10 ] ; " , tokenizeAndStringify ( " char a[9+1]; " ) ) ;
2015-11-20 19:43:11 +01:00
// ticket #4931
ASSERT_EQUALS ( " dostuff ( 1 ) ; " , tokenizeAndStringify ( " dostuff(9&&8); " , true ) ) ;
2011-08-06 06:50:29 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyCompoundAssignment ( ) {
2010-10-31 08:47:13 +01:00
ASSERT_EQUALS ( " ; x = x + y ; " , tokenizeAndStringify ( " ; x += y; " ) ) ;
ASSERT_EQUALS ( " ; x = x - y ; " , tokenizeAndStringify ( " ; x -= y; " ) ) ;
ASSERT_EQUALS ( " ; x = x * y ; " , tokenizeAndStringify ( " ; x *= y; " ) ) ;
ASSERT_EQUALS ( " ; x = x / y ; " , tokenizeAndStringify ( " ; x /= y; " ) ) ;
ASSERT_EQUALS ( " ; x = x % y ; " , tokenizeAndStringify ( " ; x %= y; " ) ) ;
ASSERT_EQUALS ( " ; x = x & y ; " , tokenizeAndStringify ( " ; x &= y; " ) ) ;
ASSERT_EQUALS ( " ; x = x | y ; " , tokenizeAndStringify ( " ; x |= y; " ) ) ;
ASSERT_EQUALS ( " ; x = x ^ y ; " , tokenizeAndStringify ( " ; x ^= y; " ) ) ;
ASSERT_EQUALS ( " ; x = x << y ; " , tokenizeAndStringify ( " ; x <<= y; " ) ) ;
ASSERT_EQUALS ( " ; x = x >> y ; " , tokenizeAndStringify ( " ; x >>= y; " ) ) ;
2010-11-01 17:36:54 +01:00
ASSERT_EQUALS ( " { x = x + y ; } " , tokenizeAndStringify ( " { x += y;} " ) ) ;
ASSERT_EQUALS ( " { x = x - y ; } " , tokenizeAndStringify ( " { x -= y;} " ) ) ;
ASSERT_EQUALS ( " { x = x * y ; } " , tokenizeAndStringify ( " { x *= y;} " ) ) ;
ASSERT_EQUALS ( " { x = x / y ; } " , tokenizeAndStringify ( " { x /= y;} " ) ) ;
ASSERT_EQUALS ( " { x = x % y ; } " , tokenizeAndStringify ( " { x %= y;} " ) ) ;
ASSERT_EQUALS ( " { x = x & y ; } " , tokenizeAndStringify ( " { x &= y;} " ) ) ;
ASSERT_EQUALS ( " { x = x | y ; } " , tokenizeAndStringify ( " { x |= y;} " ) ) ;
ASSERT_EQUALS ( " { x = x ^ y ; } " , tokenizeAndStringify ( " { x ^= y;} " ) ) ;
ASSERT_EQUALS ( " { x = x << y ; } " , tokenizeAndStringify ( " { x <<= y;} " ) ) ;
ASSERT_EQUALS ( " { x = x >> y ; } " , tokenizeAndStringify ( " { x >>= y;} " ) ) ;
2010-11-01 19:31:30 +01:00
2010-11-04 20:16:17 +01:00
ASSERT_EQUALS ( " ; * p = * p + y ; " , tokenizeAndStringify ( " ; *p += y; " ) ) ;
2011-02-16 20:33:24 +01:00
ASSERT_EQUALS ( " ; ( * p ) = ( * p ) + y ; " , tokenizeAndStringify ( " ; (*p) += y; " ) ) ;
2010-11-23 20:35:08 +01:00
ASSERT_EQUALS ( " ; * ( p [ 0 ] ) = * ( p [ 0 ] ) + y ; " , tokenizeAndStringify ( " ; *(p[0]) += y; " ) ) ;
2015-07-27 13:44:35 +02:00
ASSERT_EQUALS ( " ; p [ { 1 , 2 } ] = p [ { 1 , 2 } ] + y ; " , tokenizeAndStringify ( " ; p[{1,2}] += y; " ) ) ;
2010-11-04 20:16:17 +01:00
2012-10-19 13:34:44 +02:00
ASSERT_EQUALS ( " void foo ( ) { switch ( n ) { case 0 : ; x = x + y ; break ; } } " , tokenizeAndStringify ( " void foo() { switch (n) { case 0: x += y; break; } } " ) ) ;
2010-11-01 20:33:55 +01:00
ASSERT_EQUALS ( " ; x . y = x . y + 1 ; " , tokenizeAndStringify ( " ; x.y += 1; " ) ) ;
2010-11-02 19:22:48 +01:00
ASSERT_EQUALS ( " ; x [ 0 ] = x [ 0 ] + 1 ; " , tokenizeAndStringify ( " ; x[0] += 1; " ) ) ;
2010-11-04 19:02:54 +01:00
ASSERT_EQUALS ( " ; x [ y - 1 ] = x [ y - 1 ] + 1 ; " , tokenizeAndStringify ( " ; x[y-1] += 1; " ) ) ;
2011-12-04 12:22:21 +01:00
ASSERT_EQUALS ( " ; x [ y ] = x [ y ++ ] + 1 ; " , tokenizeAndStringify ( " ; x[y++] += 1; " ) ) ;
ASSERT_EQUALS ( " ; x [ ++ y ] = x [ y ] + 1 ; " , tokenizeAndStringify ( " ; x[++y] += 1; " ) ) ;
2010-11-06 07:23:35 +01:00
ASSERT_EQUALS ( " ; " , tokenizeAndStringify ( " ;x += 0; " ) ) ;
2010-11-28 11:48:06 +01:00
ASSERT_EQUALS ( " ; " , tokenizeAndStringify ( " ;x += ' \\ 0'; " ) ) ;
2010-11-06 07:23:35 +01:00
ASSERT_EQUALS ( " ; " , tokenizeAndStringify ( " ;x -= 0; " ) ) ;
ASSERT_EQUALS ( " ; " , tokenizeAndStringify ( " ;x |= 0; " ) ) ;
ASSERT_EQUALS ( " ; " , tokenizeAndStringify ( " ;x *= 1; " ) ) ;
ASSERT_EQUALS ( " ; " , tokenizeAndStringify ( " ;x /= 1; " ) ) ;
2010-11-25 18:16:11 +01:00
ASSERT_EQUALS ( " ; a . x ( ) = a . x ( ) + 1 ; " , tokenizeAndStringify ( " ; a.x() += 1; " ) ) ;
2011-03-27 08:19:09 +02:00
ASSERT_EQUALS ( " ; x ( 1 ) = x ( 1 ) + 1 ; " , tokenizeAndStringify ( " ; x(1) += 1; " ) ) ;
2010-12-27 20:49:07 +01:00
// #2368
ASSERT_EQUALS ( " if ( false ) { } else { j = j - i ; } " , tokenizeAndStringify ( " if (false) {} else { j -= i; } " ) ) ;
2011-04-14 17:30:50 +02:00
// #2714 - wrong simplification of "a += b?c:d;"
ASSERT_EQUALS ( " ; a = a + ( b ? c : d ) ; " , tokenizeAndStringify ( " ; a+=b?c:d; " ) ) ;
ASSERT_EQUALS ( " ; a = a * ( b + 1 ) ; " , tokenizeAndStringify ( " ; a*=b+1; " ) ) ;
2011-11-06 14:28:19 +01:00
ASSERT_EQUALS ( " ; a = a + ( b && c ) ; " , tokenizeAndStringify ( " ; a+=b&&c; " ) ) ;
ASSERT_EQUALS ( " ; a = a * ( b || c ) ; " , tokenizeAndStringify ( " ; a*=b||c; " ) ) ;
ASSERT_EQUALS ( " ; a = a | ( b == c ) ; " , tokenizeAndStringify ( " ; a|=b==c; " ) ) ;
2012-03-06 18:51:50 +01:00
// #3469
ASSERT_EQUALS ( " ; a = a + ( b = 1 ) ; " , tokenizeAndStringify ( " ; a += b = 1; " ) ) ;
2010-10-31 08:47:13 +01:00
}
2014-11-20 14:20:09 +01:00
void simplifyAssignmentInFunctionCall ( ) {
2010-09-09 19:40:36 +02:00
ASSERT_EQUALS ( " ; x = g ( ) ; f ( x ) ; " , tokenizeAndStringify ( " ;f(x=g()); " ) ) ;
2015-10-27 20:01:40 +01:00
ASSERT_EQUALS ( " ; hs = ( xyz_t ) { h . centerX , h . centerY , 1 + index } ; putInput ( hs , 1 ) ; " , tokenizeAndStringify ( " ;putInput(hs = (xyz_t) { h->centerX, h->centerY, 1 + index }, 1); " ) ) ;
2010-09-09 19:40:36 +02:00
}
2010-10-19 21:54:15 +02:00
2014-11-20 14:20:09 +01:00
void simplifyRoundCurlyParentheses ( ) {
2012-10-13 13:24:41 +02:00
ASSERT_EQUALS ( " ; x = 123 ; " , tokenizeAndStringify ( " ;x=({123;}); " ) ) ;
ASSERT_EQUALS ( " ; x = y ; " , tokenizeAndStringify ( " ;x=({y;}); " ) ) ;
2012-09-22 18:41:33 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyOperatorName1 ( ) {
2011-01-27 18:44:20 +01:00
// make sure C code doesn't get changed
2011-02-28 20:29:34 +01:00
const char code [ ] = " void operator () {} "
" int main() "
" { "
" operator(); "
" } " ;
2011-01-27 18:44:20 +01:00
2011-02-28 20:29:34 +01:00
const char result [ ] = " void operator ( ) { } "
" int main ( ) "
" { "
" operator ( ) ; "
" } " ;
2011-01-27 18:44:20 +01:00
2015-05-22 22:18:42 +02:00
ASSERT_EQUALS ( result , tokenizeAndStringify ( code , /*simplify=*/ false , /*expand=*/ true , /*platform=*/ Settings : : Unspecified , " test.c " ) ) ;
2011-02-28 20:29:34 +01:00
}
2011-01-27 18:44:20 +01:00
2014-11-20 14:20:09 +01:00
void simplifyOperatorName2 ( ) {
2011-02-28 20:29:34 +01:00
const char code [ ] = " class Fred "
" { "
" Fred(const Fred & f) { operator = (f); } "
" operator = (); "
" } " ;
2011-01-27 18:44:20 +01:00
2011-02-28 20:29:34 +01:00
const char result [ ] = " class Fred "
" { "
" Fred ( const Fred & f ) { operator= ( f ) ; } "
" operator= ( ) ; "
" } " ;
2011-01-27 18:44:20 +01:00
2011-02-28 20:29:34 +01:00
ASSERT_EQUALS ( result , tokenizeAndStringify ( code , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyOperatorName3 ( ) {
2011-02-28 20:29:34 +01:00
// #2615
const char code [ ] = " void f() { "
" static_cast<ScToken*>(xResult.operator->())->GetMatrix(); "
" } " ;
const char result [ ] = " void f ( ) { static_cast < ScToken * > ( xResult . operator. ( ) ) . GetMatrix ( ) ; } " ;
ASSERT_EQUALS ( result , tokenizeAndStringify ( code , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyOperatorName4 ( ) {
2011-02-28 20:29:34 +01:00
const char code [ ] = " void operator==() { } " ;
const char result [ ] = " void operator== ( ) { } " ;
ASSERT_EQUALS ( result , tokenizeAndStringify ( code , false ) ) ;
2011-01-27 18:44:20 +01:00
}
2011-01-30 08:34:58 +01:00
2014-11-20 14:20:09 +01:00
void simplifyOperatorName5 ( ) {
2011-03-29 02:02:06 +02:00
const char code1 [ ] = " std::istream & operator >> (std::istream & s, Fred &f); " ;
const char result1 [ ] = " std :: istream & operator>> ( std :: istream & s , Fred & f ) ; " ;
ASSERT_EQUALS ( result1 , tokenizeAndStringify ( code1 , false ) ) ;
const char code2 [ ] = " std::ostream & operator << (std::ostream & s, const Fred &f); " ;
const char result2 [ ] = " std :: ostream & operator<< ( std :: ostream & s , const Fred & f ) ; " ;
ASSERT_EQUALS ( result2 , tokenizeAndStringify ( code2 , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyOperatorName6 ( ) { // ticket #3195
2011-10-12 15:10:34 +02:00
const char code1 [ ] = " value_type * operator ++ (int); " ;
const char result1 [ ] = " value_type * operator++ ( int ) ; " ;
ASSERT_EQUALS ( result1 , tokenizeAndStringify ( code1 , false ) ) ;
const char code2 [ ] = " value_type * operator -- (int); " ;
const char result2 [ ] = " value_type * operator-- ( int ) ; " ;
ASSERT_EQUALS ( result2 , tokenizeAndStringify ( code2 , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyOperatorName7 ( ) { // ticket #4619
2013-03-01 13:06:51 +01:00
const char code1 [ ] = " value_type * operator += (int); " ;
const char result1 [ ] = " value_type * operator+= ( int ) ; " ;
ASSERT_EQUALS ( result1 , tokenizeAndStringify ( code1 , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyOperatorName8 ( ) { // ticket #5706
2014-04-26 18:30:09 +02:00
const char code1 [ ] = " value_type * operator += (int) noexcept ; " ;
const char result1 [ ] = " value_type * operator+= ( int ) noexcept ; " ;
ASSERT_EQUALS ( result1 , tokenizeAndStringify ( code1 , false ) ) ;
const char code2 [ ] = " value_type * operator += (int) noexcept ( true ) ; " ;
const char result2 [ ] = " value_type * operator+= ( int ) noexcept ( true ) ; " ;
ASSERT_EQUALS ( result2 , tokenizeAndStringify ( code2 , false ) ) ;
const char code3 [ ] = " value_type * operator += (int) throw ( ) ; " ;
const char result3 [ ] = " value_type * operator+= ( int ) throw ( ) ; " ;
ASSERT_EQUALS ( result3 , tokenizeAndStringify ( code3 , false ) ) ;
const char code4 [ ] = " value_type * operator += (int) const noexcept ; " ;
const char result4 [ ] = " value_type * operator+= ( int ) const noexcept ; " ;
ASSERT_EQUALS ( result4 , tokenizeAndStringify ( code4 , false ) ) ;
const char code5 [ ] = " value_type * operator += (int) const noexcept ( true ) ; " ;
const char result5 [ ] = " value_type * operator+= ( int ) const noexcept ( true ) ; " ;
ASSERT_EQUALS ( result5 , tokenizeAndStringify ( code5 , false ) ) ;
const char code6 [ ] = " value_type * operator += (int) const throw ( ) ; " ;
const char result6 [ ] = " value_type * operator+= ( int ) const throw ( ) ; " ;
ASSERT_EQUALS ( result6 , tokenizeAndStringify ( code6 , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyOperatorName9 ( ) { // Ticket #5709
2014-05-03 21:35:04 +02:00
const char code [ ] = " struct R { R operator, ( R b ) ; } ; " ;
ASSERT_EQUALS ( code , tokenizeAndStringify ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyNullArray ( ) {
2012-08-26 10:03:05 +02:00
ASSERT_EQUALS ( " * ( foo . bar [ 5 ] ) = x ; " , tokenizeAndStringify ( " 0[foo.bar[5]] = x; " ) ) ;
}
2014-11-20 14:20:09 +01:00
void removeMacrosInGlobalScope ( ) {
2011-01-30 08:34:58 +01:00
// remove some unhandled macros in the global scope.
ASSERT_EQUALS ( " void f ( ) { } " , tokenizeAndStringify ( " void f() NOTHROW { } " ) ) ;
2011-03-30 19:49:55 +02:00
ASSERT_EQUALS ( " struct Foo { } ; " , tokenizeAndStringify ( " struct __declspec(dllexport) Foo {}; " ) ) ;
2013-06-16 15:41:13 +02:00
ASSERT_EQUALS ( " namespace { } " , tokenizeAndStringify ( " ABA() namespace { } " ) ) ;
2012-06-12 18:45:31 +02:00
// #3750
ASSERT_EQUALS ( " ; foo :: foo ( ) { } " ,
tokenizeAndStringify ( " ; AB(foo*) foo::foo() { } " ) ) ;
2012-06-13 19:21:20 +02:00
2013-06-16 15:41:13 +02:00
// #4834
ASSERT_EQUALS ( " A(B) foo ( ) { } " , tokenizeAndStringify ( " A(B) foo() {} " ) ) ;
2012-06-13 19:21:20 +02:00
// #3855
ASSERT_EQUALS ( " ; class foo { } " ,
tokenizeAndStringify ( " ; AB class foo { } " ) ) ;
ASSERT_EQUALS ( " ; CONST struct ABC abc ; " ,
tokenizeAndStringify ( " ; CONST struct ABC abc ; " ) ) ;
2011-01-30 08:34:58 +01:00
}
2011-03-26 12:20:23 +01:00
2014-11-20 14:20:09 +01:00
void removeMacroInVarDecl ( ) { // #4304
2012-11-29 08:44:12 +01:00
// only remove macros with parentheses (those hurt most)
ASSERT_EQUALS ( " void f ( ) { PROGMEM int x ; } " , tokenizeAndStringify ( " void f() { PROGMEM int x ; } " ) ) ;
ASSERT_EQUALS ( " void f ( ) { int x ; } " , tokenizeAndStringify ( " void f() { SECTION( \" .data.ro \" ) int x ; } " ) ) ;
// various variable declarations
ASSERT_EQUALS ( " void f ( ) { CONST int x ; } " , tokenizeAndStringify ( " void f() { SECTION( \" .data.ro \" ) CONST int x ; } " ) ) ;
ASSERT_EQUALS ( " void f ( ) { char a [ 4 ] ; } " , tokenizeAndStringify ( " void f() { SECTION( \" .data.ro \" ) char a[4]; } " ) ) ;
ASSERT_EQUALS ( " void f ( ) { const char a [ 4 ] ; } " , tokenizeAndStringify ( " void f() { SECTION( \" .data.ro \" ) const char a[4]; } " ) ) ;
ASSERT_EQUALS ( " void f ( ) { struct ABC abc ; } " , tokenizeAndStringify ( " void f() { SECTION( \" .data.ro \" ) struct ABC abc; } " ) ) ;
ASSERT_EQUALS ( " void f ( ) { CONST struct ABC abc ; } " , tokenizeAndStringify ( " void f() { SECTION( \" .data.ro \" ) CONST struct ABC abc; } " ) ) ;
}
2014-11-20 14:20:09 +01:00
void multipleAssignment ( ) {
2011-03-26 12:20:23 +01:00
ASSERT_EQUALS ( " a = b = 0 ; " , tokenizeAndStringify ( " a=b=0; " ) ) ;
}
2011-06-11 21:51:12 +02:00
2014-11-20 14:20:09 +01:00
void platformWin ( ) {
2012-11-11 16:16:17 +01:00
const char code [ ] = " BOOL f; "
2011-09-18 19:06:54 +02:00
" BOOLEAN g; "
" BYTE h; "
" CHAR i; "
" DWORD j; "
" FLOAT k; "
" INT l; "
" INT32 m; "
" INT64 n; "
" LONG o; "
" SHORT p; "
" UCHAR q; "
" UINT r; "
" ULONG s; "
" USHORT t; "
" WORD u; "
2011-09-20 05:14:58 +02:00
" VOID *v; "
" LPBOOL w; "
" PBOOL x; "
" LPBYTE y; "
" PBOOLEAN z; "
" PBYTE A; "
" LPCSTR B; "
" PCSTR C; "
" LPCVOID D; "
" LPDWORD E; "
" LPINT F; "
" PINT G; "
" LPLONG H; "
" PLONG I; "
" LPSTR J; "
" PSTR K; "
" PCHAR L; "
" LPVOID M; "
2011-09-21 02:17:02 +02:00
" PVOID N; "
2012-01-18 12:57:41 +01:00
" BOOL _bool; "
" HFILE hfile; "
" LONG32 long32; "
" LCID lcid; "
" LCTYPE lctype; "
" LGRPID lgrpid; "
" LONG64 long64; "
" PUCHAR puchar; "
" LPCOLORREF lpcolorref; "
" PDWORD pdword; "
" PULONG pulong; "
" SERVICE_STATUS_HANDLE service_status_hanlde; "
" SC_LOCK sc_lock; "
" SC_HANDLE sc_handle; "
" HACCEL haccel; "
" HCONV hconv; "
" HCONVLIST hconvlist; "
" HDDEDATA hddedata; "
" HDESK hdesk; "
" HDROP hdrop; "
" HDWP hdwp; "
" HENHMETAFILE henhmetafile; "
" HHOOK hhook; "
" HKL hkl; "
" HMONITOR hmonitor; "
" HSZ hsz; "
" HWINSTA hwinsta; "
" PWCHAR pwchar; "
" PUSHORT pushort; "
" LANGID langid; "
" DWORD64 dword64; "
" ULONG64 ulong64; "
2012-11-11 15:40:06 +01:00
" LPWSTR lpcwstr; "
2012-01-18 12:57:41 +01:00
" LPCWSTR lpcwstr; " ;
2011-09-18 16:31:31 +02:00
2012-11-11 16:16:17 +01:00
const char expected [ ] = " int f ; "
2011-09-18 19:06:54 +02:00
" unsigned char g ; "
" unsigned char h ; "
" char i ; "
" unsigned long j ; "
" float k ; "
" int l ; "
" int m ; "
" long long n ; "
" long o ; "
" short p ; "
" unsigned char q ; "
" unsigned int r ; "
" unsigned long s ; "
" unsigned short t ; "
" unsigned short u ; "
2011-09-20 05:14:58 +02:00
" void * v ; "
" int * w ; "
" int * x ; "
" unsigned char * y ; "
" unsigned char * z ; "
" unsigned char * A ; "
" const char * B ; "
" const char * C ; "
" const void * D ; "
" unsigned long * E ; "
" int * F ; "
" int * G ; "
" long * H ; "
" long * I ; "
" char * J ; "
" char * K ; "
" char * L ; "
" void * M ; "
2011-09-21 02:17:02 +02:00
" void * N ; "
2012-01-18 12:57:41 +01:00
" int _bool ; "
" int hfile ; "
" int long32 ; "
" unsigned long lcid ; "
" unsigned long lctype ; "
" unsigned long lgrpid ; "
" long long long64 ; "
" unsigned char * puchar ; "
" unsigned long * lpcolorref ; "
" unsigned long * pdword ; "
" unsigned long * pulong ; "
" void * service_status_hanlde ; "
" void * sc_lock ; "
" void * sc_handle ; "
" void * haccel ; "
" void * hconv ; "
" void * hconvlist ; "
" void * hddedata ; "
" void * hdesk ; "
" void * hdrop ; "
" void * hdwp ; "
" void * henhmetafile ; "
" void * hhook ; "
" void * hkl ; "
" void * hmonitor ; "
" void * hsz ; "
" void * hwinsta ; "
2012-11-11 15:40:06 +01:00
" wchar_t * pwchar ; "
2012-01-18 12:57:41 +01:00
" unsigned short * pushort ; "
" unsigned short langid ; "
2013-08-25 08:29:24 +02:00
" unsigned long long dword64 ; "
" unsigned long long ulong64 ; "
2012-11-11 15:40:06 +01:00
" wchar_t * lpcwstr ; "
" const wchar_t * lpcwstr ; " ;
2011-09-18 16:31:31 +02:00
2012-11-11 16:16:17 +01:00
// These types should be defined the same on all Windows platforms
2014-10-19 07:34:40 +02:00
std : : string win32A = tokenizeAndStringifyWindows ( code , true , true , Settings : : Win32A ) ;
2012-11-11 16:16:17 +01:00
ASSERT_EQUALS ( expected , win32A ) ;
2014-10-19 07:34:40 +02:00
ASSERT_EQUALS ( win32A , tokenizeAndStringifyWindows ( code , true , true , Settings : : Win32W ) ) ;
ASSERT_EQUALS ( win32A , tokenizeAndStringifyWindows ( code , true , true , Settings : : Win64 ) ) ;
2012-11-11 16:16:17 +01:00
}
2014-11-20 14:20:09 +01:00
void platformWin32 ( ) {
2012-11-11 16:16:17 +01:00
const char code [ ] = " unsigned int sizeof_short = sizeof(short); "
" unsigned int sizeof_unsigned_short = sizeof(unsigned short); "
" unsigned int sizeof_int = sizeof(int); "
" unsigned int sizeof_unsigned_int = sizeof(unsigned int); "
" unsigned int sizeof_long = sizeof(long); "
" unsigned int sizeof_unsigned_long = sizeof(unsigned long); "
" unsigned int sizeof_long_long = sizeof(long long); "
" unsigned int sizeof_unsigned_long_long = sizeof(unsigned long long); "
" unsigned int sizeof_float = sizeof(float); "
" unsigned int sizeof_double = sizeof(double); "
" unsigned int sizeof_long_double = sizeof(long double); "
" unsigned int sizeof_bool = sizeof(bool); "
" unsigned int sizeof_wchar_t = sizeof(wchar_t); "
" unsigned int sizeof_pointer = sizeof(void *); "
" unsigned int sizeof_size_t = sizeof(size_t); "
" size_t a; "
" ssize_t b; "
" ptrdiff_t c; "
" intptr_t d; "
" uintptr_t e; "
" DWORD_PTR O; "
" ULONG_PTR P; "
" SIZE_T Q; "
" HRESULT R; "
" LONG_PTR S; "
" HANDLE T; "
" PHANDLE U; "
" SSIZE_T _ssize_t; "
" UINT_PTR uint_ptr; "
" WPARAM wparam; "
" HALF_PTR half_ptr; "
2013-04-09 13:57:42 +02:00
" INT_PTR int_ptr; " ;
2012-11-11 16:16:17 +01:00
const char expected [ ] = " unsigned int sizeof_short ; sizeof_short = 2 ; "
" unsigned int sizeof_unsigned_short ; sizeof_unsigned_short = 2 ; "
" unsigned int sizeof_int ; sizeof_int = 4 ; "
" unsigned int sizeof_unsigned_int ; sizeof_unsigned_int = 4 ; "
" unsigned int sizeof_long ; sizeof_long = 4 ; "
" unsigned int sizeof_unsigned_long ; sizeof_unsigned_long = 4 ; "
" unsigned int sizeof_long_long ; sizeof_long_long = 8 ; "
" unsigned int sizeof_unsigned_long_long ; sizeof_unsigned_long_long = 8 ; "
" unsigned int sizeof_float ; sizeof_float = 4 ; "
" unsigned int sizeof_double ; sizeof_double = 8 ; "
" unsigned int sizeof_long_double ; sizeof_long_double = 8 ; "
" unsigned int sizeof_bool ; sizeof_bool = 1 ; "
" unsigned int sizeof_wchar_t ; sizeof_wchar_t = 2 ; "
" unsigned int sizeof_pointer ; sizeof_pointer = 4 ; "
" unsigned int sizeof_size_t ; sizeof_size_t = 4 ; "
" unsigned long a ; "
" long b ; "
" long c ; "
" long d ; "
" unsigned long e ; "
" unsigned long O ; "
" unsigned long P ; "
" unsigned long Q ; "
" long R ; "
" long S ; "
" void * T ; "
" void * * U ; "
" long _ssize_t ; "
" unsigned int uint_ptr ; "
" unsigned int wparam ; "
" short half_ptr ; "
" int int_ptr ; " ;
2012-11-11 15:40:06 +01:00
// These types should be defined the same on all Win32 platforms
2014-10-19 07:34:40 +02:00
std : : string win32A = tokenizeAndStringifyWindows ( code , true , true , Settings : : Win32A ) ;
2012-11-11 15:40:06 +01:00
ASSERT_EQUALS ( expected , win32A ) ;
2014-10-19 07:34:40 +02:00
ASSERT_EQUALS ( win32A , tokenizeAndStringifyWindows ( code , true , true , Settings : : Win32W ) ) ;
2011-09-24 20:51:03 +02:00
}
2014-11-20 14:20:09 +01:00
void platformWin32A ( ) {
2011-09-24 22:02:56 +02:00
const char code [ ] = " wchar_t wc; "
" TCHAR c; "
2011-09-24 20:51:03 +02:00
" PTSTR ptstr; "
" LPTSTR lptstr; "
" PCTSTR pctstr; "
" LPCTSTR lpctstr; "
" void foo() { "
2011-09-27 04:08:24 +02:00
" TCHAR tc = _T( \' c \' ); "
2011-09-24 20:51:03 +02:00
" TCHAR src[10] = _T( \" 123456789 \" ); "
" TCHAR dst[10]; "
" _tcscpy(dst, src); "
" dst[0] = 0; "
2011-09-24 22:02:56 +02:00
" _tcscat(dst, src); "
2011-09-27 13:29:36 +02:00
" LPTSTR d = _tcsdup(str); "
2011-09-25 16:02:27 +02:00
" _tprintf(_T( \" Hello world! \n \" )); "
" _stprintf(dst, _T( \" Hello! \n \" )); "
" _sntprintf(dst, sizeof(dst) / sizeof(TCHAR), _T( \" Hello world! \n \" )); "
" _tscanf(_T( \" %s \" ), dst); "
" _stscanf(dst, _T( \" %s \" ), dst); "
2012-01-18 12:57:41 +01:00
" } "
" TBYTE tbyte; " ;
2012-11-11 15:16:08 +01:00
const char expected [ ] = " wchar_t wc ; "
2011-09-24 22:02:56 +02:00
" char c ; "
2011-09-24 20:51:03 +02:00
" char * ptstr ; "
" char * lptstr ; "
" const char * pctstr ; "
" const char * lpctstr ; "
" void foo ( ) { "
2011-09-27 04:08:24 +02:00
" char tc ; tc = \' c \' ; "
2011-09-24 20:51:03 +02:00
" char src [ 10 ] = \" 123456789 \" ; "
" char dst [ 10 ] ; "
" strcpy ( dst , src ) ; "
" dst [ 0 ] = 0 ; "
2011-09-24 22:02:56 +02:00
" strcat ( dst , src ) ; "
2011-09-27 13:29:36 +02:00
" char * d ; d = strdup ( str ) ; "
2011-09-25 16:02:27 +02:00
" printf ( \" Hello world! \n \" ) ; "
" sprintf ( dst , \" Hello! \n \" ) ; "
2013-10-04 17:30:55 +02:00
" _snprintf ( dst , sizeof ( dst ) / sizeof ( char ) , \" Hello world! \n \" ) ; "
2011-09-25 16:02:27 +02:00
" scanf ( \" %s \" , dst ) ; "
" sscanf ( dst , \" %s \" , dst ) ; "
2012-01-18 12:57:41 +01:00
" } "
2014-10-19 07:34:40 +02:00
" unsigned char tbyte ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringifyWindows ( code , false , true , Settings : : Win32A ) ) ;
2011-09-24 20:51:03 +02:00
}
2014-11-20 14:20:09 +01:00
void platformWin32W ( ) {
2011-09-24 22:02:56 +02:00
const char code [ ] = " wchar_t wc; "
" TCHAR c; "
" PTSTR ptstr; "
" LPTSTR lptstr; "
" PCTSTR pctstr; "
" LPCTSTR lpctstr; "
2012-01-18 12:57:41 +01:00
" TBYTE tbyte; "
2011-09-24 22:02:56 +02:00
" void foo() { "
2011-09-27 04:08:24 +02:00
" TCHAR tc = _T( \' c \' ); "
2011-09-24 22:02:56 +02:00
" TCHAR src[10] = _T( \" 123456789 \" ); "
" TCHAR dst[10]; "
" _tcscpy(dst, src); "
" dst[0] = 0; "
" _tcscat(dst, src); "
2011-09-27 13:29:36 +02:00
" LPTSTR d = _tcsdup(str); "
2011-09-25 16:02:27 +02:00
" _tprintf(_T( \" Hello world! \n \" )); "
" _stprintf(dst, _T( \" Hello! \n \" )); "
" _sntprintf(dst, sizeof(dst) / sizeof(TCHAR), _T( \" Hello world! \n \" )); "
" _tscanf(_T( \" %s \" ), dst); "
" _stscanf(dst, _T( \" %s \" ), dst); "
2011-09-24 22:02:56 +02:00
" } " ;
2012-11-11 15:16:08 +01:00
const char expected [ ] = " wchar_t wc ; "
" wchar_t c ; "
2012-11-11 15:40:06 +01:00
" wchar_t * ptstr ; "
" wchar_t * lptstr ; "
" const wchar_t * pctstr ; "
" const wchar_t * lpctstr ; "
2014-10-19 07:34:40 +02:00
" unsigned wchar_t tbyte ; "
2011-09-24 22:02:56 +02:00
" void foo ( ) { "
2013-09-06 05:36:33 +02:00
" wchar_t tc ; tc = L \' c \' ; "
" wchar_t src [ 10 ] = L \" 123456789 \" ; "
2012-11-11 15:16:08 +01:00
" wchar_t dst [ 10 ] ; "
2011-09-24 22:02:56 +02:00
" wcscpy ( dst , src ) ; "
" dst [ 0 ] = 0 ; "
" wcscat ( dst , src ) ; "
2012-11-11 15:40:06 +01:00
" wchar_t * d ; d = wcsdup ( str ) ; "
2013-09-06 05:36:33 +02:00
" wprintf ( L \" Hello world! \n \" ) ; "
" swprintf ( dst , L \" Hello! \n \" ) ; "
2013-10-04 17:30:55 +02:00
" _snwprintf ( dst , sizeof ( dst ) / sizeof ( wchar_t ) , L \" Hello world! \n \" ) ; "
2013-09-06 05:36:33 +02:00
" wscanf ( L \" %s \" , dst ) ; "
" swscanf ( dst , L \" %s \" , dst ) ; "
2011-09-24 22:02:56 +02:00
" } " ;
2014-10-19 07:34:40 +02:00
ASSERT_EQUALS ( expected , tokenizeAndStringifyWindows ( code , false , true , Settings : : Win32W ) ) ;
2011-09-18 01:40:52 +02:00
}
2014-11-20 14:20:09 +01:00
void platformWin64 ( ) {
2011-09-18 01:40:52 +02:00
const char code [ ] = " unsigned int sizeof_short = sizeof(short); "
" unsigned int sizeof_unsigned_short = sizeof(unsigned short); "
" unsigned int sizeof_int = sizeof(int); "
" unsigned int sizeof_unsigned_int = sizeof(unsigned int); "
" unsigned int sizeof_long = sizeof(long); "
" unsigned int sizeof_unsigned_long = sizeof(unsigned long); "
" unsigned int sizeof_long_long = sizeof(long long); "
" unsigned int sizeof_unsigned_long_long = sizeof(unsigned long long); "
" unsigned int sizeof_float = sizeof(float); "
" unsigned int sizeof_double = sizeof(double); "
" unsigned int sizeof_long_double = sizeof(long double); "
" unsigned int sizeof_bool = sizeof(bool); "
2012-11-11 16:16:17 +01:00
" unsigned int sizeof_wchar_t = sizeof(wchar_t); "
2011-09-18 01:40:52 +02:00
" unsigned int sizeof_pointer = sizeof(void *); "
2011-09-18 16:31:31 +02:00
" unsigned int sizeof_size_t = sizeof(size_t); "
" size_t a; "
" ssize_t b; "
" ptrdiff_t c; "
" intptr_t d; "
2011-09-21 02:17:02 +02:00
" uintptr_t e; "
" DWORD_PTR O; "
" ULONG_PTR P; "
" SIZE_T Q; "
" HRESULT R; "
" LONG_PTR S; "
2012-01-18 12:57:41 +01:00
" HANDLE T; "
2012-11-05 00:35:34 +01:00
" PHANDLE U; "
2012-01-18 12:57:41 +01:00
" SSIZE_T _ssize_t; "
" UINT_PTR uint_ptr; "
" WPARAM wparam; "
2012-11-05 00:35:34 +01:00
" HALF_PTR half_ptr; "
2012-01-18 12:57:41 +01:00
" INT_PTR int_ptr; " ;
2011-09-18 16:31:31 +02:00
const char expected [ ] = " unsigned int sizeof_short ; sizeof_short = 2 ; "
" unsigned int sizeof_unsigned_short ; sizeof_unsigned_short = 2 ; "
" unsigned int sizeof_int ; sizeof_int = 4 ; "
" unsigned int sizeof_unsigned_int ; sizeof_unsigned_int = 4 ; "
" unsigned int sizeof_long ; sizeof_long = 4 ; "
" unsigned int sizeof_unsigned_long ; sizeof_unsigned_long = 4 ; "
" unsigned int sizeof_long_long ; sizeof_long_long = 8 ; "
" unsigned int sizeof_unsigned_long_long ; sizeof_unsigned_long_long = 8 ; "
" unsigned int sizeof_float ; sizeof_float = 4 ; "
" unsigned int sizeof_double ; sizeof_double = 8 ; "
" unsigned int sizeof_long_double ; sizeof_long_double = 8 ; "
" unsigned int sizeof_bool ; sizeof_bool = 1 ; "
2012-11-11 16:16:17 +01:00
" unsigned int sizeof_wchar_t ; sizeof_wchar_t = 2 ; "
2011-09-18 16:31:31 +02:00
" unsigned int sizeof_pointer ; sizeof_pointer = 8 ; "
" unsigned int sizeof_size_t ; sizeof_size_t = 8 ; "
" unsigned long long a ; "
" long long b ; "
" long long c ; "
" long long d ; "
2011-09-21 02:17:02 +02:00
" unsigned long long e ; "
" unsigned long long O ; "
" unsigned long long P ; "
" unsigned long long Q ; "
" long R ; "
" long long S ; "
2012-01-18 12:57:41 +01:00
" void * T ; "
2012-11-05 00:35:34 +01:00
" void * * U ; "
2012-01-18 12:57:41 +01:00
" long long _ssize_t ; "
" unsigned long long uint_ptr ; "
" unsigned long long wparam ; "
2012-11-05 00:35:34 +01:00
" int half_ptr ; "
" long long int_ptr ; " ;
2011-09-18 16:31:31 +02:00
2014-10-19 07:34:40 +02:00
ASSERT_EQUALS ( expected , tokenizeAndStringifyWindows ( code , true , true , Settings : : Win64 ) ) ;
2011-09-18 01:40:52 +02:00
}
2014-11-20 14:20:09 +01:00
void platformUnix32 ( ) {
2011-09-18 01:40:52 +02:00
const char code [ ] = " unsigned int sizeof_short = sizeof(short); "
" unsigned int sizeof_unsigned_short = sizeof(unsigned short); "
" unsigned int sizeof_int = sizeof(int); "
" unsigned int sizeof_unsigned_int = sizeof(unsigned int); "
" unsigned int sizeof_long = sizeof(long); "
" unsigned int sizeof_unsigned_long = sizeof(unsigned long); "
" unsigned int sizeof_long_long = sizeof(long long); "
" unsigned int sizeof_unsigned_long_long = sizeof(unsigned long long); "
" unsigned int sizeof_float = sizeof(float); "
" unsigned int sizeof_double = sizeof(double); "
" unsigned int sizeof_long_double = sizeof(long double); "
" unsigned int sizeof_bool = sizeof(bool); "
2012-11-11 16:16:17 +01:00
" unsigned int sizeof_wchar_t = sizeof(wchar_t); "
2011-09-18 01:40:52 +02:00
" unsigned int sizeof_pointer = sizeof(void *); "
2011-09-18 16:31:31 +02:00
" unsigned int sizeof_size_t = sizeof(size_t); "
" size_t a; "
" ssize_t b; "
" ptrdiff_t c; "
" intptr_t d; "
" uintptr_t e; " ;
const char expected [ ] = " unsigned int sizeof_short ; sizeof_short = 2 ; "
" unsigned int sizeof_unsigned_short ; sizeof_unsigned_short = 2 ; "
" unsigned int sizeof_int ; sizeof_int = 4 ; "
" unsigned int sizeof_unsigned_int ; sizeof_unsigned_int = 4 ; "
" unsigned int sizeof_long ; sizeof_long = 4 ; "
" unsigned int sizeof_unsigned_long ; sizeof_unsigned_long = 4 ; "
" unsigned int sizeof_long_long ; sizeof_long_long = 8 ; "
" unsigned int sizeof_unsigned_long_long ; sizeof_unsigned_long_long = 8 ; "
" unsigned int sizeof_float ; sizeof_float = 4 ; "
" unsigned int sizeof_double ; sizeof_double = 8 ; "
" unsigned int sizeof_long_double ; sizeof_long_double = 12 ; "
" unsigned int sizeof_bool ; sizeof_bool = 1 ; "
2012-11-11 16:16:17 +01:00
" unsigned int sizeof_wchar_t ; sizeof_wchar_t = 4 ; "
2011-09-18 16:31:31 +02:00
" unsigned int sizeof_pointer ; sizeof_pointer = 4 ; "
" unsigned int sizeof_size_t ; sizeof_size_t = 4 ; "
" unsigned long a ; "
" long b ; "
" long c ; "
" long d ; "
" unsigned long e ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true , true , Settings : : Unix32 ) ) ;
2011-09-18 01:40:52 +02:00
}
2014-11-20 14:20:09 +01:00
void platformUnix64 ( ) {
2011-09-18 01:40:52 +02:00
const char code [ ] = " unsigned int sizeof_short = sizeof(short); "
" unsigned int sizeof_unsigned_short = sizeof(unsigned short); "
" unsigned int sizeof_int = sizeof(int); "
" unsigned int sizeof_unsigned_int = sizeof(unsigned int); "
" unsigned int sizeof_long = sizeof(long); "
" unsigned int sizeof_unsigned_long = sizeof(unsigned long); "
" unsigned int sizeof_long_long = sizeof(long long); "
" unsigned int sizeof_unsigned_long_long = sizeof(unsigned long long); "
" unsigned int sizeof_float = sizeof(float); "
" unsigned int sizeof_double = sizeof(double); "
" unsigned int sizeof_long_double = sizeof(long double); "
" unsigned int sizeof_bool = sizeof(bool); "
2012-11-11 16:16:17 +01:00
" unsigned int sizeof_wchar_t = sizeof(wchar_t); "
2011-09-18 01:40:52 +02:00
" unsigned int sizeof_pointer = sizeof(void *); "
2011-09-18 16:31:31 +02:00
" unsigned int sizeof_size_t = sizeof(size_t); "
" size_t a; "
" ssize_t b; "
" ptrdiff_t c; "
" intptr_t d; "
" uintptr_t e; " ;
const char expected [ ] = " unsigned int sizeof_short ; sizeof_short = 2 ; "
" unsigned int sizeof_unsigned_short ; sizeof_unsigned_short = 2 ; "
" unsigned int sizeof_int ; sizeof_int = 4 ; "
" unsigned int sizeof_unsigned_int ; sizeof_unsigned_int = 4 ; "
" unsigned int sizeof_long ; sizeof_long = 8 ; "
" unsigned int sizeof_unsigned_long ; sizeof_unsigned_long = 8 ; "
" unsigned int sizeof_long_long ; sizeof_long_long = 8 ; "
" unsigned int sizeof_unsigned_long_long ; sizeof_unsigned_long_long = 8 ; "
" unsigned int sizeof_float ; sizeof_float = 4 ; "
" unsigned int sizeof_double ; sizeof_double = 8 ; "
" unsigned int sizeof_long_double ; sizeof_long_double = 16 ; "
" unsigned int sizeof_bool ; sizeof_bool = 1 ; "
2012-11-11 16:16:17 +01:00
" unsigned int sizeof_wchar_t ; sizeof_wchar_t = 4 ; "
2011-09-18 16:31:31 +02:00
" unsigned int sizeof_pointer ; sizeof_pointer = 8 ; "
" unsigned int sizeof_size_t ; sizeof_size_t = 8 ; "
2013-08-31 06:26:39 +02:00
" unsigned long a ; "
" long b ; "
" long c ; "
" long d ; "
" unsigned long e ; " ;
2011-09-18 16:31:31 +02:00
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code , true , true , Settings : : Unix64 ) ) ;
2011-09-18 01:40:52 +02:00
}
2012-09-03 18:51:15 +02:00
2014-11-20 14:20:09 +01:00
void platformWin32AStringCat ( ) { //#5150
2013-09-06 05:36:33 +02:00
const char code [ ] = " TCHAR text[] = _T( \" 123 \" ) _T( \" 456 \" ) _T( \" 789 \" ); " ;
const char expected [ ] = " char text [ 10 ] = \" 123456789 \" ; " ;
2014-10-19 07:34:40 +02:00
ASSERT_EQUALS ( expected , tokenizeAndStringifyWindows ( code , true , true , Settings : : Win32A ) ) ;
2013-09-06 05:36:33 +02:00
}
2014-11-20 14:20:09 +01:00
void platformWin32WStringCat ( ) { //#5150
2013-09-06 05:36:33 +02:00
const char code [ ] = " TCHAR text[] = _T( \" 123 \" ) _T( \" 456 \" ) _T( \" 789 \" ); " ;
const char expected [ ] = " wchar_t text [ 10 ] = L \" 123456789 \" ; " ;
2014-10-19 07:34:40 +02:00
ASSERT_EQUALS ( expected , tokenizeAndStringifyWindows ( code , true , true , Settings : : Win32W ) ) ;
}
2014-11-20 14:20:09 +01:00
void platformWinWithNamespace ( ) {
2014-10-19 07:34:40 +02:00
const char code1 [ ] = " UINT32 a; ::UINT32 b; foo::UINT32 c; " ;
const char expected1 [ ] = " unsigned int a ; unsigned int b ; foo :: UINT32 c ; " ;
ASSERT_EQUALS ( expected1 , tokenizeAndStringifyWindows ( code1 , true , true , Settings : : Win32A ) ) ;
const char code2 [ ] = " LPCVOID a; ::LPCVOID b; foo::LPCVOID c; " ;
const char expected2 [ ] = " const void * a ; const void * b ; foo :: LPCVOID c ; " ;
ASSERT_EQUALS ( expected2 , tokenizeAndStringifyWindows ( code2 , true , true , Settings : : Win32A ) ) ;
2013-09-06 05:36:33 +02:00
}
2014-11-20 14:20:09 +01:00
void isZeroNumber ( ) const {
2013-10-01 20:30:59 +02:00
ASSERT_EQUALS ( true , Tokenizer : : isZeroNumber ( " 0.0 " ) ) ;
ASSERT_EQUALS ( true , Tokenizer : : isZeroNumber ( " +0.0 " ) ) ;
ASSERT_EQUALS ( true , Tokenizer : : isZeroNumber ( " -0.0 " ) ) ;
ASSERT_EQUALS ( true , Tokenizer : : isZeroNumber ( " +0L " ) ) ;
ASSERT_EQUALS ( true , Tokenizer : : isZeroNumber ( " +0 " ) ) ;
ASSERT_EQUALS ( true , Tokenizer : : isZeroNumber ( " -0 " ) ) ;
ASSERT_EQUALS ( true , Tokenizer : : isZeroNumber ( " -0E+0 " ) ) ;
ASSERT_EQUALS ( false , Tokenizer : : isZeroNumber ( " 1.0 " ) ) ;
ASSERT_EQUALS ( false , Tokenizer : : isZeroNumber ( " +1.0 " ) ) ;
ASSERT_EQUALS ( false , Tokenizer : : isZeroNumber ( " -1 " ) ) ;
2013-10-03 20:52:07 +02:00
ASSERT_EQUALS ( false , Tokenizer : : isZeroNumber ( " " ) ) ;
ASSERT_EQUALS ( false , Tokenizer : : isZeroNumber ( " garbage " ) ) ;
2014-02-20 16:56:49 +01:00
ASSERT_EQUALS ( false , Tokenizer : : isZeroNumber ( " E2 " ) ) ;
ASSERT_EQUALS ( false , Tokenizer : : isZeroNumber ( " 2e " ) ) ;
2013-10-01 20:30:59 +02:00
}
2014-11-20 14:20:09 +01:00
void isOneNumber ( ) const {
2013-10-01 20:30:59 +02:00
ASSERT_EQUALS ( true , Tokenizer : : isOneNumber ( " 1.0 " ) ) ;
ASSERT_EQUALS ( true , Tokenizer : : isOneNumber ( " +1.0 " ) ) ;
ASSERT_EQUALS ( true , Tokenizer : : isOneNumber ( " 1.0e+0 " ) ) ;
ASSERT_EQUALS ( true , Tokenizer : : isOneNumber ( " +1L " ) ) ;
ASSERT_EQUALS ( true , Tokenizer : : isOneNumber ( " +1 " ) ) ;
ASSERT_EQUALS ( true , Tokenizer : : isOneNumber ( " 1 " ) ) ;
ASSERT_EQUALS ( true , Tokenizer : : isOneNumber ( " +1E+0 " ) ) ;
ASSERT_EQUALS ( false , Tokenizer : : isOneNumber ( " 0.0 " ) ) ;
ASSERT_EQUALS ( false , Tokenizer : : isOneNumber ( " +0.0 " ) ) ;
ASSERT_EQUALS ( false , Tokenizer : : isOneNumber ( " -0 " ) ) ;
2013-10-03 20:52:07 +02:00
ASSERT_EQUALS ( false , Tokenizer : : isOneNumber ( " " ) ) ;
ASSERT_EQUALS ( false , Tokenizer : : isOneNumber ( " garbage " ) ) ;
2013-10-01 20:30:59 +02:00
}
2014-11-20 14:20:09 +01:00
void isTwoNumber ( ) const {
2013-10-03 15:41:12 +02:00
ASSERT_EQUALS ( true , Tokenizer : : isTwoNumber ( " 2.0 " ) ) ;
ASSERT_EQUALS ( true , Tokenizer : : isTwoNumber ( " +2.0 " ) ) ;
ASSERT_EQUALS ( true , Tokenizer : : isTwoNumber ( " 2.0e+0 " ) ) ;
ASSERT_EQUALS ( true , Tokenizer : : isTwoNumber ( " +2L " ) ) ;
ASSERT_EQUALS ( true , Tokenizer : : isTwoNumber ( " +2 " ) ) ;
ASSERT_EQUALS ( true , Tokenizer : : isTwoNumber ( " 2 " ) ) ;
ASSERT_EQUALS ( true , Tokenizer : : isTwoNumber ( " +2E+0 " ) ) ;
ASSERT_EQUALS ( false , Tokenizer : : isTwoNumber ( " 0.0 " ) ) ;
ASSERT_EQUALS ( false , Tokenizer : : isTwoNumber ( " +0.0 " ) ) ;
ASSERT_EQUALS ( false , Tokenizer : : isTwoNumber ( " -0 " ) ) ;
2013-10-03 20:52:07 +02:00
ASSERT_EQUALS ( false , Tokenizer : : isTwoNumber ( " " ) ) ;
ASSERT_EQUALS ( false , Tokenizer : : isTwoNumber ( " garbage " ) ) ;
2013-10-03 15:41:12 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_erfc ( ) {
2013-10-13 14:23:10 +02:00
// verify erfc(), erfcf(), erfcl() - simplifcation
const char code_erfc [ ] = " void f(int x) { \n "
" std::cout << erfc(x); \n " // do not simplify
" std::cout << erfc(0L); \n " // simplify to 1
" } " ;
const char expected_erfc [ ] = " void f ( int x ) { \n "
" std :: cout << erfc ( x ) ; \n "
" std :: cout << 1 ; \n "
" } " ;
ASSERT_EQUALS ( expected_erfc , tokenizeAndStringify ( code_erfc ) ) ;
const char code_erfcf [ ] = " void f(float x) { \n "
" std::cout << erfcf(x); \n " // do not simplify
" std::cout << erfcf(0.0f); \n " // simplify to 1
" } " ;
const char expected_erfcf [ ] = " void f ( float x ) { \n "
" std :: cout << erfcf ( x ) ; \n "
" std :: cout << 1 ; \n "
" } " ;
ASSERT_EQUALS ( expected_erfcf , tokenizeAndStringify ( code_erfcf ) ) ;
const char code_erfcl [ ] = " void f(long double x) { \n "
" std::cout << erfcl(x); \n " // do not simplify
2014-02-24 18:52:31 +01:00
" std::cout << erfcl(0.0f); \n " // simplify to 1
2013-10-13 14:23:10 +02:00
" } " ;
const char expected_erfcl [ ] = " void f ( long double x ) { \n "
" std :: cout << erfcl ( x ) ; \n "
" std :: cout << 1 ; \n "
" } " ;
ASSERT_EQUALS ( expected_erfcl , tokenizeAndStringify ( code_erfcl ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_cos ( ) {
2013-10-13 14:23:10 +02:00
// verify cos(), cosf(), cosl() - simplifcation
const char code_cos [ ] = " void f(int x) { \n "
" std::cout << cos(x); \n " // do not simplify
" std::cout << cos(0L); \n " // simplify to 1
" } " ;
const char expected_cos [ ] = " void f ( int x ) { \n "
" std :: cout << cos ( x ) ; \n "
" std :: cout << 1 ; \n "
" } " ;
ASSERT_EQUALS ( expected_cos , tokenizeAndStringify ( code_cos ) ) ;
const char code_cosf [ ] = " void f(float x) { \n "
" std::cout << cosf(x); \n " // do not simplify
" std::cout << cosf(0.0f); \n " // simplify to 1
" } " ;
const char expected_cosf [ ] = " void f ( float x ) { \n "
" std :: cout << cosf ( x ) ; \n "
" std :: cout << 1 ; \n "
" } " ;
ASSERT_EQUALS ( expected_cosf , tokenizeAndStringify ( code_cosf ) ) ;
const char code_cosl [ ] = " void f(long double x) { \n "
" std::cout << cosl(x); \n " // do not simplify
2014-02-24 18:52:31 +01:00
" std::cout << cosl(0.0f); \n " // simplify to 1
2013-10-13 14:23:10 +02:00
" } " ;
const char expected_cosl [ ] = " void f ( long double x ) { \n "
" std :: cout << cosl ( x ) ; \n "
" std :: cout << 1 ; \n "
" } " ;
ASSERT_EQUALS ( expected_cosl , tokenizeAndStringify ( code_cosl ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_cosh ( ) {
2013-10-13 14:23:10 +02:00
// verify cosh(), coshf(), coshl() - simplifcation
const char code_cosh [ ] = " void f(int x) { \n "
" std::cout << cosh(x); \n " // do not simplify
" std::cout << cosh(0L); \n " // simplify to 1
" } " ;
const char expected_cosh [ ] = " void f ( int x ) { \n "
" std :: cout << cosh ( x ) ; \n "
" std :: cout << 1 ; \n "
" } " ;
ASSERT_EQUALS ( expected_cosh , tokenizeAndStringify ( code_cosh ) ) ;
const char code_coshf [ ] = " void f(float x) { \n "
" std::cout << coshf(x); \n " // do not simplify
" std::cout << coshf(0.0f); \n " // simplify to 1
" } " ;
const char expected_coshf [ ] = " void f ( float x ) { \n "
" std :: cout << coshf ( x ) ; \n "
" std :: cout << 1 ; \n "
" } " ;
ASSERT_EQUALS ( expected_coshf , tokenizeAndStringify ( code_coshf ) ) ;
const char code_coshl [ ] = " void f(long double x) { \n "
" std::cout << coshl(x); \n " // do not simplify
2014-02-24 18:52:31 +01:00
" std::cout << coshl(0.0f); \n " // simplify to 1
2013-10-13 14:23:10 +02:00
" } " ;
const char expected_coshl [ ] = " void f ( long double x ) { \n "
" std :: cout << coshl ( x ) ; \n "
" std :: cout << 1 ; \n "
" } " ;
ASSERT_EQUALS ( expected_coshl , tokenizeAndStringify ( code_coshl ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_acos ( ) {
2013-10-09 16:59:32 +02:00
// verify acos(), acosf(), acosl() - simplifcation
const char code_acos [ ] = " void f(int x) { \n "
" std::cout << acos(x); \n " // do not simplify
" std::cout << acos(1L); \n " // simplify to 0
" } " ;
const char expected_acos [ ] = " void f ( int x ) { \n "
" std :: cout << acos ( x ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_acos , tokenizeAndStringify ( code_acos ) ) ;
const char code_acosf [ ] = " void f(float x) { \n "
" std::cout << acosf(x); \n " // do not simplify
" std::cout << acosf(1.0f); \n " // simplify to 0
" } " ;
const char expected_acosf [ ] = " void f ( float x ) { \n "
" std :: cout << acosf ( x ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_acosf , tokenizeAndStringify ( code_acosf ) ) ;
const char code_acosl [ ] = " void f(long double x) { \n "
" std::cout << acosl(x); \n " // do not simplify
2014-02-24 18:52:31 +01:00
" std::cout << acosl(1.0f); \n " // simplify to 0
2013-10-09 16:59:32 +02:00
" } " ;
const char expected_acosl [ ] = " void f ( long double x ) { \n "
" std :: cout << acosl ( x ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_acosl , tokenizeAndStringify ( code_acosl ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_acosh ( ) {
2013-10-07 22:10:58 +02:00
// verify acosh(), acoshf(), acoshl() - simplifcation
const char code_acosh [ ] = " void f(int x) { \n "
" std::cout << acosh(x); \n " // do not simplify
" std::cout << acosh(1L); \n " // simplify to 0
" } " ;
const char expected_acosh [ ] = " void f ( int x ) { \n "
" std :: cout << acosh ( x ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_acosh , tokenizeAndStringify ( code_acosh ) ) ;
const char code_acoshf [ ] = " void f(float x) { \n "
" std::cout << acoshf(x); \n " // do not simplify
" std::cout << acoshf(1.0f); \n " // simplify to 0
" } " ;
const char expected_acoshf [ ] = " void f ( float x ) { \n "
" std :: cout << acoshf ( x ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_acoshf , tokenizeAndStringify ( code_acoshf ) ) ;
const char code_acoshl [ ] = " void f(long double x) { \n "
" std::cout << acoshl(x); \n " // do not simplify
2014-02-24 18:52:31 +01:00
" std::cout << acoshl(1.0f); \n " // simplify to 0
2013-10-07 22:10:58 +02:00
" } " ;
const char expected_acoshl [ ] = " void f ( long double x ) { \n "
" std :: cout << acoshl ( x ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_acoshl , tokenizeAndStringify ( code_acoshl ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_sqrt ( ) {
2013-10-08 16:05:05 +02:00
// verify sqrt(), sqrtf(), sqrtl() - simplifcation
const char code_sqrt [ ] = " void f(int x) { \n "
" std::cout << sqrt(x); \n " // do not simplify
" std::cout << sqrt(-1); \n " // do not simplify
" std::cout << sqrt(0L); \n " // simplify to 0
" std::cout << sqrt(1L); \n " // simplify to 1
" } " ;
const char expected_sqrt [ ] = " void f ( int x ) { \n "
" std :: cout << sqrt ( x ) ; \n "
" std :: cout << sqrt ( -1 ) ; \n "
" std :: cout << 0 ; \n "
" std :: cout << 1 ; \n "
" } " ;
ASSERT_EQUALS ( expected_sqrt , tokenizeAndStringify ( code_sqrt ) ) ;
const char code_sqrtf [ ] = " void f(float x) { \n "
" std::cout << sqrtf(x); \n " // do not simplify
" std::cout << sqrtf(-1.0f); \n " // do not simplify
" std::cout << sqrtf(0.0f); \n " // simplify to 0
" std::cout << sqrtf(1.0); \n " // simplify to 1
" } " ;
const char expected_sqrtf [ ] = " void f ( float x ) { \n "
" std :: cout << sqrtf ( x ) ; \n "
" std :: cout << sqrtf ( -1.0f ) ; \n "
" std :: cout << 0 ; \n "
" std :: cout << 1 ; \n "
" } " ;
ASSERT_EQUALS ( expected_sqrtf , tokenizeAndStringify ( code_sqrtf ) ) ;
const char code_sqrtl [ ] = " void f(long double x) { \n "
" std::cout << sqrtf(x); \n " // do not simplify
" std::cout << sqrtf(-1.0); \n " // do not simplify
" std::cout << sqrtf(0.0); \n " // simplify to 0
" std::cout << sqrtf(1.0); \n " // simplify to 1
" } " ;
const char expected_sqrtl [ ] = " void f ( long double x ) { \n "
" std :: cout << sqrtf ( x ) ; \n "
" std :: cout << sqrtf ( -1.0 ) ; \n "
" std :: cout << 0 ; \n "
" std :: cout << 1 ; \n "
" } " ;
ASSERT_EQUALS ( expected_sqrtl , tokenizeAndStringify ( code_sqrtl ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_cbrt ( ) {
2013-10-08 16:05:05 +02:00
// verify cbrt(), cbrtf(), cbrtl() - simplifcation
const char code_cbrt [ ] = " void f(int x) { \n "
" std::cout << cbrt(x); \n " // do not simplify
" std::cout << cbrt(-1); \n " // do not simplify
" std::cout << cbrt(0L); \n " // simplify to 0
" std::cout << cbrt(1L); \n " // simplify to 1
" } " ;
const char expected_cbrt [ ] = " void f ( int x ) { \n "
" std :: cout << cbrt ( x ) ; \n "
" std :: cout << cbrt ( -1 ) ; \n "
" std :: cout << 0 ; \n "
" std :: cout << 1 ; \n "
" } " ;
ASSERT_EQUALS ( expected_cbrt , tokenizeAndStringify ( code_cbrt ) ) ;
const char code_cbrtf [ ] = " void f(float x) { \n "
" std::cout << cbrtf(x); \n " // do not simplify
" std::cout << cbrtf(-1.0f); \n " // do not simplify
" std::cout << cbrtf(0.0f); \n " // simplify to 0
" std::cout << cbrtf(1.0); \n " // simplify to 1
" } " ;
const char expected_cbrtf [ ] = " void f ( float x ) { \n "
" std :: cout << cbrtf ( x ) ; \n "
" std :: cout << cbrtf ( -1.0f ) ; \n "
" std :: cout << 0 ; \n "
" std :: cout << 1 ; \n "
" } " ;
ASSERT_EQUALS ( expected_cbrtf , tokenizeAndStringify ( code_cbrtf ) ) ;
const char code_cbrtl [ ] = " void f(long double x) { \n "
" std::cout << cbrtl(x); \n " // do not simplify
" std::cout << cbrtl(-1.0); \n " // do not simplify
" std::cout << cbrtl(0.0); \n " // simplify to 0
" std::cout << cbrtl(1.0); \n " // simplify to 1
" } " ;
const char expected_cbrtl [ ] = " void f ( long double x ) { \n "
" std :: cout << cbrtl ( x ) ; \n "
" std :: cout << cbrtl ( -1.0 ) ; \n "
" std :: cout << 0 ; \n "
" std :: cout << 1 ; \n "
" } " ;
ASSERT_EQUALS ( expected_cbrtl , tokenizeAndStringify ( code_cbrtl ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_exp2 ( ) {
2013-10-08 16:05:05 +02:00
// verify exp2(), exp2f(), exp2l() - simplifcation
const char code_exp2 [ ] = " void f(int x) { \n "
" std::cout << exp2(x); \n " // do not simplify
" std::cout << exp2(-1); \n " // do not simplify
" std::cout << exp2(0L); \n " // simplify to 0
" std::cout << exp2(1L); \n " // do not simplify
" } " ;
const char expected_exp2 [ ] = " void f ( int x ) { \n "
" std :: cout << exp2 ( x ) ; \n "
" std :: cout << exp2 ( -1 ) ; \n "
" std :: cout << 1 ; \n "
" std :: cout << exp2 ( 1L ) ; \n "
" } " ;
ASSERT_EQUALS ( expected_exp2 , tokenizeAndStringify ( code_exp2 ) ) ;
const char code_exp2f [ ] = " void f(float x) { \n "
" std::cout << exp2f(x); \n " // do not simplify
" std::cout << exp2f(-1.0); \n " // do not simplify
" std::cout << exp2f(0.0); \n " // simplify to 1
" std::cout << exp2f(1.0); \n " // do not simplify
" } " ;
const char expected_exp2f [ ] = " void f ( float x ) { \n "
" std :: cout << exp2f ( x ) ; \n "
" std :: cout << exp2f ( -1.0 ) ; \n "
" std :: cout << 1 ; \n "
" std :: cout << exp2f ( 1.0 ) ; \n "
" } " ;
ASSERT_EQUALS ( expected_exp2f , tokenizeAndStringify ( code_exp2f ) ) ;
const char code_exp2l [ ] = " void f(long double x) { \n "
" std::cout << exp2l(x); \n " // do not simplify
" std::cout << exp2l(-1.0); \n " // do not simplify
" std::cout << exp2l(0.0); \n " // simplify to 1
" std::cout << exp2l(1.0); \n " // do not simplify
" } " ;
const char expected_exp2l [ ] = " void f ( long double x ) { \n "
" std :: cout << exp2l ( x ) ; \n "
" std :: cout << exp2l ( -1.0 ) ; \n "
" std :: cout << 1 ; \n "
" std :: cout << exp2l ( 1.0 ) ; \n "
" } " ;
ASSERT_EQUALS ( expected_exp2l , tokenizeAndStringify ( code_exp2l ) ) ;
2013-10-13 15:41:47 +02:00
}
2013-10-08 16:05:05 +02:00
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_exp ( ) {
2013-10-08 16:05:05 +02:00
// verify exp(), expf(), expl() - simplifcation
const char code_exp [ ] = " void f(int x) { \n "
" std::cout << exp(x); \n " // do not simplify
" std::cout << exp(-1); \n " // do not simplify
" std::cout << exp(0L); \n " // simplify to 1
" std::cout << exp(1L); \n " // do not simplify
" } " ;
const char expected_exp [ ] = " void f ( int x ) { \n "
" std :: cout << exp ( x ) ; \n "
" std :: cout << exp ( -1 ) ; \n "
" std :: cout << 1 ; \n "
" std :: cout << exp ( 1L ) ; \n "
" } " ;
ASSERT_EQUALS ( expected_exp , tokenizeAndStringify ( code_exp ) ) ;
const char code_expf [ ] = " void f(float x) { \n "
" std::cout << expf(x); \n " // do not simplify
" std::cout << expf(-1.0); \n " // do not simplify
" std::cout << expf(0.0); \n " // simplify to 1
" std::cout << expf(1.0); \n " // do not simplify
" } " ;
const char expected_expf [ ] = " void f ( float x ) { \n "
" std :: cout << expf ( x ) ; \n "
" std :: cout << expf ( -1.0 ) ; \n "
" std :: cout << 1 ; \n "
" std :: cout << expf ( 1.0 ) ; \n "
" } " ;
ASSERT_EQUALS ( expected_expf , tokenizeAndStringify ( code_expf ) ) ;
const char code_expl [ ] = " void f(long double x) { \n "
" std::cout << expl(x); \n " // do not simplify
" std::cout << expl(-1.0); \n " // do not simplify
" std::cout << expl(0.0); \n " // simplify to 1
" std::cout << expl(1.0); \n " // do not simplify
" } " ;
const char expected_expl [ ] = " void f ( long double x ) { \n "
" std :: cout << expl ( x ) ; \n "
" std :: cout << expl ( -1.0 ) ; \n "
" std :: cout << 1 ; \n "
" std :: cout << expl ( 1.0 ) ; \n "
" } " ;
ASSERT_EQUALS ( expected_expl , tokenizeAndStringify ( code_expl ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_erf ( ) {
2013-10-13 15:41:47 +02:00
// verify erf(), erff(), erfl() - simplifcation
const char code_erf [ ] = " void f(int x) { \n "
" std::cout << erf(x); \n " // do not simplify
" std::cout << erf(10); \n " // do not simplify
" std::cout << erf(0L); \n " // simplify to 0
" } " ;
const char expected_erf [ ] = " void f ( int x ) { \n "
" std :: cout << erf ( x ) ; \n "
" std :: cout << erf ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_erf , tokenizeAndStringify ( code_erf ) ) ;
const char code_erff [ ] = " void f(float x) { \n "
" std::cout << erff(x); \n " // do not simplify
" std::cout << erff(10); \n " // do not simplify
" std::cout << erff(0.0f); \n " // simplify to 0
" } " ;
const char expected_erff [ ] = " void f ( float x ) { \n "
" std :: cout << erff ( x ) ; \n "
" std :: cout << erff ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_erff , tokenizeAndStringify ( code_erff ) ) ;
const char code_erfl [ ] = " void f(long double x) { \n "
" std::cout << erfl(x); \n " // do not simplify
2014-02-24 18:52:31 +01:00
" std::cout << erfl(10.0f); \n " // do not simplify
" std::cout << erfl(0.0f); \n " // simplify to 0
2013-10-13 15:41:47 +02:00
" } " ;
const char expected_erfl [ ] = " void f ( long double x ) { \n "
" std :: cout << erfl ( x ) ; \n "
2014-02-24 18:52:31 +01:00
" std :: cout << erfl ( 10.0f ) ; \n "
2013-10-13 15:41:47 +02:00
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_erfl , tokenizeAndStringify ( code_erfl ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_atanh ( ) {
2013-10-13 15:41:47 +02:00
// verify atanh(), atanhf(), atanhl() - simplifcation
const char code_atanh [ ] = " void f(int x) { \n "
" std::cout << atanh(x); \n " // do not simplify
" std::cout << atanh(10); \n " // do not simplify
" std::cout << atanh(0L); \n " // simplify to 0
" } " ;
const char expected_atanh [ ] = " void f ( int x ) { \n "
" std :: cout << atanh ( x ) ; \n "
" std :: cout << atanh ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_atanh , tokenizeAndStringify ( code_atanh ) ) ;
2013-09-26 00:44:16 +02:00
2013-10-13 15:41:47 +02:00
const char code_atanhf [ ] = " void f(float x) { \n "
" std::cout << atanhf(x); \n " // do not simplify
" std::cout << atanhf(10); \n " // do not simplify
" std::cout << atanhf(0.0f); \n " // simplify to 0
" } " ;
const char expected_atanhf [ ] = " void f ( float x ) { \n "
" std :: cout << atanhf ( x ) ; \n "
" std :: cout << atanhf ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_atanhf , tokenizeAndStringify ( code_atanhf ) ) ;
const char code_atanhl [ ] = " void f(long double x) { \n "
" std::cout << atanhl(x); \n " // do not simplify
2014-02-24 18:52:31 +01:00
" std::cout << atanhl(10.0f); \n " // do not simplify
" std::cout << atanhl(0.0d); \n " // do not simplify - invalid number!
" std::cout << atanhl(0.0f); \n " // simplify to 0
2013-10-13 15:41:47 +02:00
" } " ;
const char expected_atanhl [ ] = " void f ( long double x ) { \n "
" std :: cout << atanhl ( x ) ; \n "
2014-02-24 18:52:31 +01:00
" std :: cout << atanhl ( 10.0f ) ; \n "
" std :: cout << atanhl ( 0.0d ) ; \n "
2013-10-13 15:41:47 +02:00
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_atanhl , tokenizeAndStringify ( code_atanhl ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_atan ( ) {
2013-10-13 15:41:47 +02:00
// verify atan(), atanf(), atanl() - simplifcation
const char code_atan [ ] = " void f(int x) { \n "
" std::cout << atan(x); \n " // do not simplify
" std::cout << atan(10); \n " // do not simplify
" std::cout << atan(0L); \n " // simplify to 0
" } " ;
const char expected_atan [ ] = " void f ( int x ) { \n "
" std :: cout << atan ( x ) ; \n "
" std :: cout << atan ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_atan , tokenizeAndStringify ( code_atan ) ) ;
const char code_atanf [ ] = " void f(float x) { \n "
" std::cout << atanf(x); \n " // do not simplify
" std::cout << atanf(10); \n " // do not simplify
" std::cout << atanf(0.0f); \n " // simplify to 0
" } " ;
const char expected_atanf [ ] = " void f ( float x ) { \n "
" std :: cout << atanf ( x ) ; \n "
" std :: cout << atanf ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_atanf , tokenizeAndStringify ( code_atanf ) ) ;
const char code_atanl [ ] = " void f(long double x) { \n "
" std::cout << atanl(x); \n " // do not simplify
2014-02-24 18:52:31 +01:00
" std::cout << atanl(10.0f); \n " // do not simplify
" std::cout << atanl(0.0f); \n " // simplify to 0
2013-10-13 15:41:47 +02:00
" } " ;
const char expected_atanl [ ] = " void f ( long double x ) { \n "
" std :: cout << atanl ( x ) ; \n "
2014-02-24 18:52:31 +01:00
" std :: cout << atanl ( 10.0f ) ; \n "
2013-10-13 15:41:47 +02:00
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_atanl , tokenizeAndStringify ( code_atanl ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_fma ( ) {
2013-10-13 16:28:02 +02:00
// verify fma(), fmal(), fmaf() - simplifcation
const char code_fma [ ] = " int f(int a, int b, int c) { return fma(a,b,c); } " ;
2015-08-29 20:51:02 +02:00
const char expected_fma [ ] = " int f ( int a , int b , int c ) { return ( a * b + c ) ; } " ;
2013-10-13 16:28:02 +02:00
ASSERT_EQUALS ( expected_fma , tokenizeAndStringify ( code_fma ) ) ;
const char code_fmaf [ ] = " float f ( float a , float b , float c ) { return fmaf(a,b,c); } " ;
2015-08-29 20:51:02 +02:00
const char expected_fmaf [ ] = " float f ( float a , float b , float c ) { return ( a * b + c ) ; } " ;
2013-10-13 16:28:02 +02:00
ASSERT_EQUALS ( expected_fmaf , tokenizeAndStringify ( code_fmaf ) ) ;
const char code_fmal [ ] = " long double f ( long double a , long double b , long double c ) { return fmal(a,b,c); } " ;
2015-08-29 20:51:02 +02:00
const char expected_fmal [ ] = " long double f ( long double a , long double b , long double c ) { return ( a * b + c ) ; } " ;
2013-10-13 16:28:02 +02:00
ASSERT_EQUALS ( expected_fmal , tokenizeAndStringify ( code_fmal ) ) ;
2013-10-29 17:26:26 +01:00
const char code_fma1 [ ] = " void f() { \n "
" std::cout << \" fma(1,2,3): \" << fma(1,2,3) << std::endl; \n "
" std::cout << \" fmaf(1,2,3): \" << fmaf(1,2,3) << std::endl; \n "
" std::cout << \" fmal(1,2,3): \" << fmal(1,2,3) << std::endl; \n "
" }; " ;
const char expected_fma1 [ ] = " void f() { \n "
" std :: cout << \" fma(1,2,3): \" << 5 << std :: endl ; \n "
" std :: cout << \" fmaf(1,2,3): \" << 5 << std :: endl ; \n "
" std :: cout << \" fmal(1,2,3): \" << 5 << std :: endl ; \n "
" } ; " ;
const char current_fma1 [ ] = " void f ( ) { \n "
2015-08-29 20:51:02 +02:00
" std :: cout << \" fma(1,2,3): \" << ( 1 * 2 + 3 ) << std :: endl ; \n "
" std :: cout << \" fmaf(1,2,3): \" << ( 1 * 2 + 3 ) << std :: endl ; \n "
" std :: cout << \" fmal(1,2,3): \" << ( 1 * 2 + 3 ) << std :: endl ; \n "
2013-10-29 17:26:26 +01:00
" } ; " ;
TODO_ASSERT_EQUALS ( expected_fma1 , current_fma1 , tokenizeAndStringify ( code_fma1 ) ) ;
2013-10-13 16:28:02 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_tanh ( ) {
2013-10-13 15:41:47 +02:00
// verify tanh(), tanhf(), tanhl() - simplifcation
const char code_tanh [ ] = " void f(int x) { \n "
" std::cout << tanh(x); \n " // do not simplify
" std::cout << tanh(10); \n " // do not simplify
" std::cout << tanh(0L); \n " // simplify to 0
" } " ;
const char expected_tanh [ ] = " void f ( int x ) { \n "
" std :: cout << tanh ( x ) ; \n "
" std :: cout << tanh ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_tanh , tokenizeAndStringify ( code_tanh ) ) ;
const char code_tanhf [ ] = " void f(float x) { \n "
" std::cout << tanhf(x); \n " // do not simplify
" std::cout << tanhf(10); \n " // do not simplify
" std::cout << tanhf(0.0f); \n " // simplify to 0
" } " ;
const char expected_tanhf [ ] = " void f ( float x ) { \n "
" std :: cout << tanhf ( x ) ; \n "
" std :: cout << tanhf ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_tanhf , tokenizeAndStringify ( code_tanhf ) ) ;
const char code_tanhl [ ] = " void f(long double x) { \n "
" std::cout << tanhl(x); \n " // do not simplify
2014-02-24 18:52:31 +01:00
" std::cout << tanhl(10.0f); \n " // do not simplify
" std::cout << tanhl(0.0f); \n " // simplify to 0
2013-10-13 15:41:47 +02:00
" } " ;
const char expected_tanhl [ ] = " void f ( long double x ) { \n "
" std :: cout << tanhl ( x ) ; \n "
2014-02-24 18:52:31 +01:00
" std :: cout << tanhl ( 10.0f ) ; \n "
2013-10-13 15:41:47 +02:00
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_tanhl , tokenizeAndStringify ( code_tanhl ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_tan ( ) {
2013-10-13 15:41:47 +02:00
// verify tan(), tanf(), tanl() - simplifcation
const char code_tan [ ] = " void f(int x) { \n "
" std::cout << tan(x); \n " // do not simplify
" std::cout << tan(10); \n " // do not simplify
" std::cout << tan(0L); \n " // simplify to 0
" } " ;
const char expected_tan [ ] = " void f ( int x ) { \n "
" std :: cout << tan ( x ) ; \n "
" std :: cout << tan ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_tan , tokenizeAndStringify ( code_tan ) ) ;
const char code_tanf [ ] = " void f(float x) { \n "
" std::cout << tanf(x); \n " // do not simplify
" std::cout << tanf(10); \n " // do not simplify
" std::cout << tanf(0.0f); \n " // simplify to 0
" } " ;
const char expected_tanf [ ] = " void f ( float x ) { \n "
" std :: cout << tanf ( x ) ; \n "
" std :: cout << tanf ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_tanf , tokenizeAndStringify ( code_tanf ) ) ;
const char code_tanl [ ] = " void f(long double x) { \n "
" std::cout << tanl(x); \n " // do not simplify
2014-02-24 18:52:31 +01:00
" std::cout << tanl(10.0f); \n " // do not simplify
" std::cout << tanl(0.0f); \n " // simplify to 0
2013-10-13 15:41:47 +02:00
" } " ;
const char expected_tanl [ ] = " void f ( long double x ) { \n "
" std :: cout << tanl ( x ) ; \n "
2014-02-24 18:52:31 +01:00
" std :: cout << tanl ( 10.0f ) ; \n "
2013-10-13 15:41:47 +02:00
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_tanl , tokenizeAndStringify ( code_tanl ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_expm1 ( ) {
2013-10-13 15:41:47 +02:00
// verify expm1(), expm1f(), expm1l() - simplifcation
const char code_expm1 [ ] = " void f(int x) { \n "
" std::cout << expm1(x); \n " // do not simplify
" std::cout << expm1(10); \n " // do not simplify
" std::cout << expm1(0L); \n " // simplify to 0
" } " ;
const char expected_expm1 [ ] = " void f ( int x ) { \n "
" std :: cout << expm1 ( x ) ; \n "
" std :: cout << expm1 ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_expm1 , tokenizeAndStringify ( code_expm1 ) ) ;
const char code_expm1f [ ] = " void f(float x) { \n "
" std::cout << expm1f(x); \n " // do not simplify
" std::cout << expm1f(10); \n " // do not simplify
" std::cout << expm1f(0.0f); \n " // simplify to 0
" } " ;
const char expected_expm1f [ ] = " void f ( float x ) { \n "
" std :: cout << expm1f ( x ) ; \n "
" std :: cout << expm1f ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_expm1f , tokenizeAndStringify ( code_expm1f ) ) ;
const char code_expm1l [ ] = " void f(long double x) { \n "
" std::cout << expm1l(x); \n " // do not simplify
2014-02-24 18:52:31 +01:00
" std::cout << expm1l(10.0f); \n " // do not simplify
" std::cout << expm1l(0.0f); \n " // simplify to 0
2013-10-13 15:41:47 +02:00
" } " ;
const char expected_expm1l [ ] = " void f ( long double x ) { \n "
" std :: cout << expm1l ( x ) ; \n "
2014-02-24 18:52:31 +01:00
" std :: cout << expm1l ( 10.0f ) ; \n "
2013-10-13 15:41:47 +02:00
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_expm1l , tokenizeAndStringify ( code_expm1l ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_asinh ( ) {
2013-10-13 15:41:47 +02:00
// verify asinh(), asinhf(), asinhl() - simplifcation
const char code_asinh [ ] = " void f(int x) { \n "
" std::cout << asinh(x); \n " // do not simplify
" std::cout << asinh(10); \n " // do not simplify
" std::cout << asinh(0L); \n " // simplify to 0
" } " ;
const char expected_asinh [ ] = " void f ( int x ) { \n "
" std :: cout << asinh ( x ) ; \n "
" std :: cout << asinh ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_asinh , tokenizeAndStringify ( code_asinh ) ) ;
const char code_asinhf [ ] = " void f(float x) { \n "
" std::cout << asinhf(x); \n " // do not simplify
" std::cout << asinhf(10); \n " // do not simplify
" std::cout << asinhf(0.0f); \n " // simplify to 0
" } " ;
const char expected_asinhf [ ] = " void f ( float x ) { \n "
" std :: cout << asinhf ( x ) ; \n "
" std :: cout << asinhf ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_asinhf , tokenizeAndStringify ( code_asinhf ) ) ;
const char code_asinhl [ ] = " void f(long double x) { \n "
" std::cout << asinhl(x); \n " // do not simplify
2014-02-24 18:52:31 +01:00
" std::cout << asinhl(10.0f); \n " // do not simplify
" std::cout << asinhl(0.0f); \n " // simplify to 0
2013-10-13 15:41:47 +02:00
" } " ;
const char expected_asinhl [ ] = " void f ( long double x ) { \n "
" std :: cout << asinhl ( x ) ; \n "
2014-02-24 18:52:31 +01:00
" std :: cout << asinhl ( 10.0f ) ; \n "
2013-10-13 15:41:47 +02:00
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_asinhl , tokenizeAndStringify ( code_asinhl ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_asin ( ) {
2013-10-13 15:41:47 +02:00
// verify asin(), asinf(), asinl() - simplifcation
const char code_asin [ ] = " void f(int x) { \n "
" std::cout << asin(x); \n " // do not simplify
" std::cout << asin(10); \n " // do not simplify
" std::cout << asin(0L); \n " // simplify to 0
" } " ;
const char expected_asin [ ] = " void f ( int x ) { \n "
" std :: cout << asin ( x ) ; \n "
" std :: cout << asin ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_asin , tokenizeAndStringify ( code_asin ) ) ;
const char code_asinf [ ] = " void f(float x) { \n "
" std::cout << asinf(x); \n " // do not simplify
" std::cout << asinf(10); \n " // do not simplify
" std::cout << asinf(0.0f); \n " // simplify to 0
" } " ;
const char expected_asinf [ ] = " void f ( float x ) { \n "
" std :: cout << asinf ( x ) ; \n "
" std :: cout << asinf ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_asinf , tokenizeAndStringify ( code_asinf ) ) ;
const char code_asinl [ ] = " void f(long double x) { \n "
" std::cout << asinl(x); \n " // do not simplify
2014-02-24 18:52:31 +01:00
" std::cout << asinl(10.0f); \n " // do not simplify
" std::cout << asinl(0.0f); \n " // simplify to 0
2013-10-13 15:41:47 +02:00
" } " ;
const char expected_asinl [ ] = " void f ( long double x ) { \n "
" std :: cout << asinl ( x ) ; \n "
2014-02-24 18:52:31 +01:00
" std :: cout << asinl ( 10.0f ) ; \n "
2013-10-13 15:41:47 +02:00
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_asinl , tokenizeAndStringify ( code_asinl ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_sinh ( ) {
2013-10-13 15:41:47 +02:00
// verify sinh(), sinhf(), sinhl() - simplifcation
const char code_sinh [ ] = " void f(int x) { \n "
" std::cout << sinh(x); \n " // do not simplify
" std::cout << sinh(10); \n " // do not simplify
" std::cout << sinh(0L); \n " // simplify to 0
" } " ;
const char expected_sinh [ ] = " void f ( int x ) { \n "
" std :: cout << sinh ( x ) ; \n "
" std :: cout << sinh ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_sinh , tokenizeAndStringify ( code_sinh ) ) ;
const char code_sinhf [ ] = " void f(float x) { \n "
" std::cout << sinhf(x); \n " // do not simplify
" std::cout << sinhf(10); \n " // do not simplify
" std::cout << sinhf(0.0f); \n " // simplify to 0
" } " ;
const char expected_sinhf [ ] = " void f ( float x ) { \n "
" std :: cout << sinhf ( x ) ; \n "
" std :: cout << sinhf ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_sinhf , tokenizeAndStringify ( code_sinhf ) ) ;
const char code_sinhl [ ] = " void f(long double x) { \n "
" std::cout << sinhl(x); \n " // do not simplify
2014-02-24 18:52:31 +01:00
" std::cout << sinhl(10.0f); \n " // do not simplify
" std::cout << sinhl(0.0f); \n " // simplify to 0
2013-10-13 15:41:47 +02:00
" } " ;
const char expected_sinhl [ ] = " void f ( long double x ) { \n "
" std :: cout << sinhl ( x ) ; \n "
2014-02-24 18:52:31 +01:00
" std :: cout << sinhl ( 10.0f ) ; \n "
2013-10-13 15:41:47 +02:00
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_sinhl , tokenizeAndStringify ( code_sinhl ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_sin ( ) {
2013-10-13 15:41:47 +02:00
// verify sin(), sinf(), sinl() - simplifcation
const char code_sin [ ] = " void f(int x) { \n "
" std::cout << sin(x); \n " // do not simplify
" std::cout << sin(10); \n " // do not simplify
" std::cout << sin(0L); \n " // simplify to 0
" } " ;
const char expected_sin [ ] = " void f ( int x ) { \n "
" std :: cout << sin ( x ) ; \n "
" std :: cout << sin ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_sin , tokenizeAndStringify ( code_sin ) ) ;
const char code_sinf [ ] = " void f(float x) { \n "
" std::cout << sinf(x); \n " // do not simplify
" std::cout << sinf(10); \n " // do not simplify
" std::cout << sinf(0.0f); \n " // simplify to 0
" } " ;
const char expected_sinf [ ] = " void f ( float x ) { \n "
" std :: cout << sinf ( x ) ; \n "
" std :: cout << sinf ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_sinf , tokenizeAndStringify ( code_sinf ) ) ;
const char code_sinl [ ] = " void f(long double x) { \n "
" std::cout << sinl(x); \n " // do not simplify
2014-02-24 18:52:31 +01:00
" std::cout << sinl(10.0f); \n " // do not simplify
" std::cout << sinl(0.0f); \n " // simplify to 0
2013-10-13 15:41:47 +02:00
" } " ;
const char expected_sinl [ ] = " void f ( long double x ) { \n "
" std :: cout << sinl ( x ) ; \n "
2014-02-24 18:52:31 +01:00
" std :: cout << sinl ( 10.0f ) ; \n "
2013-10-13 15:41:47 +02:00
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_sinl , tokenizeAndStringify ( code_sinl ) ) ;
2015-04-13 20:37:07 +02:00
// #6629
const char code [ ] = " class Foo { int sinf; Foo() : sinf(0) {} }; " ;
const char expected [ ] = " class Foo { int sinf ; Foo ( ) : sinf ( 0 ) { } } ; " ;
ASSERT_EQUALS ( expected , tokenizeAndStringify ( code ) ) ;
2013-10-13 15:41:47 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_ilogb ( ) {
2013-10-13 15:41:47 +02:00
// verify ilogb(), ilogbf(), ilogbl() - simplifcation
const char code_ilogb [ ] = " void f(int x) { \n "
" std::cout << ilogb(x); \n " // do not simplify
" std::cout << ilogb(10); \n " // do not simplify
" std::cout << ilogb(1L); \n " // simplify to 0
" } " ;
const char expected_ilogb [ ] = " void f ( int x ) { \n "
" std :: cout << ilogb ( x ) ; \n "
" std :: cout << ilogb ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_ilogb , tokenizeAndStringify ( code_ilogb ) ) ;
const char code_ilogbf [ ] = " void f(float x) { \n "
" std::cout << ilogbf(x); \n " // do not simplify
" std::cout << ilogbf(10); \n " // do not simplify
" std::cout << ilogbf(1.0f); \n " // simplify to 0
" } " ;
const char expected_ilogbf [ ] = " void f ( float x ) { \n "
" std :: cout << ilogbf ( x ) ; \n "
" std :: cout << ilogbf ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_ilogbf , tokenizeAndStringify ( code_ilogbf ) ) ;
const char code_ilogbl [ ] = " void f(long double x) { \n "
" std::cout << ilogbl(x); \n " // do not simplify
2014-02-24 18:52:31 +01:00
" std::cout << ilogbl(10.0f); \n " // do not simplify
" std::cout << ilogbl(1.0f); \n " // simplify to 0
2013-10-13 15:41:47 +02:00
" } " ;
const char expected_ilogbl [ ] = " void f ( long double x ) { \n "
" std :: cout << ilogbl ( x ) ; \n "
2014-02-24 18:52:31 +01:00
" std :: cout << ilogbl ( 10.0f ) ; \n "
2013-10-13 15:41:47 +02:00
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_ilogbl , tokenizeAndStringify ( code_ilogbl ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_logb ( ) {
2013-10-03 20:52:07 +02:00
// verify logb(), logbf(), logbl() - simplifcation
const char code_logb [ ] = " void f(int x) { \n "
" std::cout << logb(x); \n " // do not simplify
" std::cout << logb(10); \n " // do not simplify
" std::cout << logb(1L); \n " // simplify to 0
" } " ;
const char expected_logb [ ] = " void f ( int x ) { \n "
" std :: cout << logb ( x ) ; \n "
" std :: cout << logb ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_logb , tokenizeAndStringify ( code_logb ) ) ;
const char code_logbf [ ] = " void f(float x) { \n "
" std::cout << logbf(x); \n " // do not simplify
" std::cout << logbf(10); \n " // do not simplify
" std::cout << logbf(1.0f); \n " // simplify to 0
" } " ;
const char expected_logbf [ ] = " void f ( float x ) { \n "
" std :: cout << logbf ( x ) ; \n "
" std :: cout << logbf ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_logbf , tokenizeAndStringify ( code_logbf ) ) ;
const char code_logbl [ ] = " void f(long double x) { \n "
" std::cout << logbl(x); \n " // do not simplify
2014-02-24 18:52:31 +01:00
" std::cout << logbl(10.0f); \n " // do not simplify
" std::cout << logbl(1.0f); \n " // simplify to 0
2013-10-03 20:52:07 +02:00
" } " ;
const char expected_logbl [ ] = " void f ( long double x ) { \n "
" std :: cout << logbl ( x ) ; \n "
2014-02-24 18:52:31 +01:00
" std :: cout << logbl ( 10.0f ) ; \n "
2013-10-03 20:52:07 +02:00
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_logbl , tokenizeAndStringify ( code_logbl ) ) ;
2013-10-13 15:41:47 +02:00
}
2013-10-03 20:52:07 +02:00
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_log1p ( ) {
2013-10-03 20:52:07 +02:00
// verify log1p(), log1pf(), log1pl() - simplifcation
const char code_log1p [ ] = " void f(int x) { \n "
" std::cout << log1p(x); \n " // do not simplify
" std::cout << log1p(10); \n " // do not simplify
" std::cout << log1p(0L); \n " // simplify to 0
" } " ;
const char expected_log1p [ ] = " void f ( int x ) { \n "
" std :: cout << log1p ( x ) ; \n "
" std :: cout << log1p ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_log1p , tokenizeAndStringify ( code_log1p ) ) ;
const char code_log1pf [ ] = " void f(float x) { \n "
" std::cout << log1pf(x); \n " // do not simplify
" std::cout << log1pf(10); \n " // do not simplify
" std::cout << log1pf(0.0f); \n " // simplify to 0
" } " ;
const char expected_log1pf [ ] = " void f ( float x ) { \n "
" std :: cout << log1pf ( x ) ; \n "
" std :: cout << log1pf ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_log1pf , tokenizeAndStringify ( code_log1pf ) ) ;
const char code_log1pl [ ] = " void f(long double x) { \n "
" std::cout << log1pl(x); \n " // do not simplify
2014-02-24 18:52:31 +01:00
" std::cout << log1pl(10.0f); \n " // do not simplify
" std::cout << log1pl(0.0f); \n " // simplify to 0
2013-10-03 20:52:07 +02:00
" } " ;
const char expected_log1pl [ ] = " void f ( long double x ) { \n "
" std :: cout << log1pl ( x ) ; \n "
2014-02-24 18:52:31 +01:00
" std :: cout << log1pl ( 10.0f ) ; \n "
2013-10-03 20:52:07 +02:00
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_log1pl , tokenizeAndStringify ( code_log1pl ) ) ;
2013-10-13 15:41:47 +02:00
}
2013-10-03 20:52:07 +02:00
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_log10 ( ) {
2013-10-03 20:52:07 +02:00
// verify log10(), log10f(), log10l() - simplifcation
const char code_log10 [ ] = " void f(int x) { \n "
" std::cout << log10(x); \n " // do not simplify
" std::cout << log10(10); \n " // do not simplify
" std::cout << log10(1L); \n " // simplify to 0
" } " ;
const char expected_log10 [ ] = " void f ( int x ) { \n "
" std :: cout << log10 ( x ) ; \n "
" std :: cout << log10 ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_log10 , tokenizeAndStringify ( code_log10 ) ) ;
const char code_log10f [ ] = " void f(float x) { \n "
" std::cout << log10f(x); \n " // do not simplify
" std::cout << log10f(10); \n " // do not simplify
" std::cout << log10f(1.0f); \n " // simplify to 0
" } " ;
const char expected_log10f [ ] = " void f ( float x ) { \n "
" std :: cout << log10f ( x ) ; \n "
" std :: cout << log10f ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_log10f , tokenizeAndStringify ( code_log10f ) ) ;
const char code_log10l [ ] = " void f(long double x) { \n "
" std::cout << log10l(x); \n " // do not simplify
2014-02-24 18:52:31 +01:00
" std::cout << log10l(10.0f); \n " // do not simplify
" std::cout << log10l(1.0f); \n " // simplify to 0
2013-10-03 20:52:07 +02:00
" } " ;
const char expected_log10l [ ] = " void f ( long double x ) { \n "
" std :: cout << log10l ( x ) ; \n "
2014-02-24 18:52:31 +01:00
" std :: cout << log10l ( 10.0f ) ; \n "
2013-10-03 20:52:07 +02:00
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_log10l , tokenizeAndStringify ( code_log10l ) ) ;
2013-10-13 15:41:47 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_log ( ) {
2013-10-03 20:52:07 +02:00
// verify log(), logf(), logl() - simplifcation
const char code_log [ ] = " void f(int x) { \n "
" std::cout << log(x); \n " // do not simplify
" std::cout << log(10); \n " // do not simplify
" std::cout << log(1L); \n " // simplify to 0
" } " ;
const char expected_log [ ] = " void f ( int x ) { \n "
" std :: cout << log ( x ) ; \n "
" std :: cout << log ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_log , tokenizeAndStringify ( code_log ) ) ;
const char code_logf [ ] = " void f(float x) { \n "
" std::cout << logf(x); \n " // do not simplify
" std::cout << logf(10); \n " // do not simplify
" std::cout << logf(1.0f); \n " // simplify to 0
" } " ;
const char expected_logf [ ] = " void f ( float x ) { \n "
" std :: cout << logf ( x ) ; \n "
" std :: cout << logf ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_logf , tokenizeAndStringify ( code_logf ) ) ;
const char code_logl [ ] = " void f(long double x) { \n "
" std::cout << logl(x); \n " // do not simplify
2014-02-24 18:52:31 +01:00
" std::cout << logl(10.0f); \n " // do not simplify
" std::cout << logl(1.0f); \n " // simplify to 0
2013-10-03 20:52:07 +02:00
" } " ;
const char expected_logl [ ] = " void f ( long double x ) { \n "
" std :: cout << logl ( x ) ; \n "
2014-02-24 18:52:31 +01:00
" std :: cout << logl ( 10.0f ) ; \n "
2013-10-03 20:52:07 +02:00
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_logl , tokenizeAndStringify ( code_logl ) ) ;
2013-10-13 15:41:47 +02:00
}
2013-10-03 20:52:07 +02:00
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_log2 ( ) {
2013-10-01 20:30:59 +02:00
// verify log2(), log2f(), log2l() - simplifcation
const char code_log2 [ ] = " void f(int x) { \n "
" std::cout << log2(x); \n " // do not simplify
" std::cout << log2(10); \n " // do not simplify
" std::cout << log2(1L); \n " // simplify to 0
" } " ;
const char expected_log2 [ ] = " void f ( int x ) { \n "
" std :: cout << log2 ( x ) ; \n "
" std :: cout << log2 ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_log2 , tokenizeAndStringify ( code_log2 ) ) ;
const char code_log2f [ ] = " void f(float x) { \n "
" std::cout << log2f(x); \n " // do not simplify
" std::cout << log2f(10); \n " // do not simplify
" std::cout << log2f(1.0f); \n " // simplify to 0
" } " ;
const char expected_log2f [ ] = " void f ( float x ) { \n "
" std :: cout << log2f ( x ) ; \n "
" std :: cout << log2f ( 10 ) ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_log2f , tokenizeAndStringify ( code_log2f ) ) ;
const char code_log2l [ ] = " void f(long double x) { \n "
" std::cout << log2l(x); \n " // do not simplify
2014-02-24 18:52:31 +01:00
" std::cout << log2l(10.0f); \n " // do not simplify
" std::cout << log2l(1.0f); \n " // simplify to 0
2013-10-01 20:30:59 +02:00
" } " ;
const char expected_log2l [ ] = " void f ( long double x ) { \n "
" std :: cout << log2l ( x ) ; \n "
2014-02-24 18:52:31 +01:00
" std :: cout << log2l ( 10.0f ) ; \n "
2013-10-01 20:30:59 +02:00
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_log2l , tokenizeAndStringify ( code_log2l ) ) ;
2013-10-08 16:05:05 +02:00
}
2013-10-01 20:30:59 +02:00
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_div ( ) {
2013-09-26 07:01:08 +02:00
// verify div(), ldiv(), lldiv() - simplifcation
const char code_div [ ] = " void f(int x) { \n "
" std::cout << div(x,1); \n " //simplify
" std::cout << div(x,-1); \n " // do not simplify
" std::cout << ldiv(10L,1L); \n " // simplify
" std::cout << ldiv(10L,132L); \n " // do not simplify
" std::cout << lldiv(10LL,1LL); \n " // simplify
" std::cout << lldiv(10LL,132LL); \n " // do not simplify
" } " ;
const char expected_div [ ] = " void f ( int x ) { \n "
" std :: cout << x ; \n "
" std :: cout << div ( x , -1 ) ; \n "
" std :: cout << 10L ; \n "
" std :: cout << ldiv ( 10L , 132L ) ; \n "
" std :: cout << 10LL ; \n "
" std :: cout << lldiv ( 10LL , 132LL ) ; \n "
" } " ;
ASSERT_EQUALS ( expected_div , tokenizeAndStringify ( code_div ) ) ;
// Do not simplify class members.
// case: div
const char code_div1 [ ] = " int f(const Fred &fred) {return fred.div(12,3);} " ;
const char expected_div1 [ ] = " int f ( const Fred & fred ) { return fred . div ( 12 , 3 ) ; } " ;
ASSERT_EQUALS ( expected_div1 , tokenizeAndStringify ( code_div1 ) ) ;
// case: ldiv
const char code_div2 [ ] = " int f(const Fred &fred) {return fred.ldiv(12,3);} " ;
const char expected_div2 [ ] = " int f ( const Fred & fred ) { return fred . ldiv ( 12 , 3 ) ; } " ;
ASSERT_EQUALS ( expected_div2 , tokenizeAndStringify ( code_div2 ) ) ;
// case: lldiv
const char code_div3 [ ] = " int f(const Fred &fred) {return fred.lldiv(12,3);} " ;
const char expected_div3 [ ] = " int f ( const Fred & fred ) { return fred . lldiv ( 12 , 3 ) ; } " ;
ASSERT_EQUALS ( expected_div3 , tokenizeAndStringify ( code_div3 ) ) ;
2013-10-08 16:05:05 +02:00
}
2013-09-26 07:01:08 +02:00
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_pow ( ) {
2013-09-26 07:01:08 +02:00
// verify pow(),pow(),powl() - simplifcation
2013-09-26 00:44:16 +02:00
const char code_pow [ ] = " void f() { \n "
" std::cout << pow(-1.0,1); \n "
" std::cout << pow(1.0,1); \n "
" std::cout << pow(0,1); \n "
2013-10-03 22:54:45 +02:00
" std::cout << pow(1,-6); \n "
2013-09-26 00:44:16 +02:00
" std::cout << powf(-1.0,1.0f); \n "
" std::cout << powf(1.0,1.0f); \n "
" std::cout << powf(0,1.0f); \n "
2013-10-03 22:54:45 +02:00
" std::cout << powf(1.0,-6.0f); \n "
2013-09-26 00:44:16 +02:00
" std::cout << powl(-1.0,1.0); \n "
" std::cout << powl(1.0,1.0); \n "
" std::cout << powl(0,1.0); \n "
2013-10-03 22:54:45 +02:00
" std::cout << powl(1.0,-6.0d); \n "
2013-09-26 00:44:16 +02:00
" } " ;
const char expected_pow [ ] = " void f ( ) { \n "
" std :: cout << -1.0 ; \n "
2013-10-03 22:54:45 +02:00
" std :: cout << 1 ; \n "
2013-09-26 00:44:16 +02:00
" std :: cout << 0 ; \n "
2013-10-03 22:54:45 +02:00
" std :: cout << 1 ; \n "
2013-09-26 00:44:16 +02:00
" std :: cout << -1.0 ; \n "
2013-10-03 22:54:45 +02:00
" std :: cout << 1 ; \n "
2013-09-26 00:44:16 +02:00
" std :: cout << 0 ; \n "
2013-10-03 22:54:45 +02:00
" std :: cout << 1 ; \n "
2013-09-26 00:44:16 +02:00
" std :: cout << -1.0 ; \n "
2013-10-03 22:54:45 +02:00
" std :: cout << 1 ; \n "
2013-09-26 00:44:16 +02:00
" std :: cout << 0 ; \n "
2013-10-03 22:54:45 +02:00
" std :: cout << 1 ; \n "
2013-09-26 00:44:16 +02:00
" } " ;
ASSERT_EQUALS ( expected_pow , tokenizeAndStringify ( code_pow ) ) ;
// verify if code is simplified correctly.
// Do not simplify class members.
const char code_pow1 [ ] = " int f(const Fred &fred) {return fred.pow(12,3);} " ;
const char expected_pow1 [ ] = " int f ( const Fred & fred ) { return fred . pow ( 12 , 3 ) ; } " ;
ASSERT_EQUALS ( expected_pow1 , tokenizeAndStringify ( code_pow1 ) ) ;
2013-09-29 15:11:09 +02:00
const char code_pow2 [ ] = " int f() {return pow(0,0);} " ;
const char expected_pow2 [ ] = " int f ( ) { return 1 ; } " ;
ASSERT_EQUALS ( expected_pow2 , tokenizeAndStringify ( code_pow2 ) ) ;
const char code_pow3 [ ] = " int f() {return pow(0,1);} " ;
const char expected_pow3 [ ] = " int f ( ) { return 0 ; } " ;
ASSERT_EQUALS ( expected_pow3 , tokenizeAndStringify ( code_pow3 ) ) ;
const char code_pow4 [ ] = " int f() {return pow(1,0);} " ;
const char expected_pow4 [ ] = " int f ( ) { return 1 ; } " ;
ASSERT_EQUALS ( expected_pow4 , tokenizeAndStringify ( code_pow4 ) ) ;
2013-10-08 16:05:05 +02:00
}
2013-09-29 15:11:09 +02:00
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_islessgreater ( ) {
2013-09-26 00:44:16 +02:00
// verify islessgreater() simplification
const char code_islessgreater [ ] = " bool f(){ \n "
" return islessgreater(1,0); \n " // (1 < 0) or (1 > 0) --> true
" } " ;
const char expected_islessgreater [ ] = " bool f ( ) { \n return true ; \n } " ;
ASSERT_EQUALS ( expected_islessgreater , tokenizeAndStringify ( code_islessgreater ) ) ;
const char code_islessgreater1 [ ] = " bool f(){ \n "
" return islessgreater(0,1); \n " // (0 < 1) or (0 > 1) --> true
" } " ;
const char expected_islessgreater1 [ ] = " bool f ( ) { \n return true ; \n } " ;
ASSERT_EQUALS ( expected_islessgreater1 , tokenizeAndStringify ( code_islessgreater1 ) ) ;
const char code_islessgreater2 [ ] = " bool f(){ \n "
" return islessgreater(0,0); \n " // (0 < 0) or (0 > 0) --> false
" } " ;
const char expected_islessgreater2 [ ] = " bool f ( ) { \n return false ; \n } " ;
ASSERT_EQUALS ( expected_islessgreater2 , tokenizeAndStringify ( code_islessgreater2 ) ) ;
const char code_islessgreater3 [ ] = " bool f(int i){ \n "
" return islessgreater(i,0); \n " // <-- Do not simplify this
" } " ;
const char expected_islessgreater3 [ ] = " bool f ( int i ) { \n return islessgreater ( i , 0 ) ; \n } " ;
ASSERT_EQUALS ( expected_islessgreater3 , tokenizeAndStringify ( code_islessgreater3 ) ) ;
2013-10-08 16:05:05 +02:00
}
2013-09-26 00:44:16 +02:00
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_islessequal ( ) {
2013-09-26 00:44:16 +02:00
// verify islessequal() simplification
const char code_islessequal [ ] = " bool f(){ \n "
" return islessequal(1,0); \n " // (1 <= 0) --> false
" } " ;
const char expected_islessequal [ ] = " bool f ( ) { \n return false ; \n } " ;
ASSERT_EQUALS ( expected_islessequal , tokenizeAndStringify ( code_islessequal ) ) ;
const char code_islessequal1 [ ] = " bool f(){ \n "
" return islessequal(0,1); \n " // (0 <= 1) --> true
" } " ;
const char expected_islessequal1 [ ] = " bool f ( ) { \n return true ; \n } " ;
ASSERT_EQUALS ( expected_islessequal1 , tokenizeAndStringify ( code_islessequal1 ) ) ;
const char code_islessequal2 [ ] = " bool f(){ \n "
" return islessequal(0,0); \n " // (0 <= 0) --> true
" } " ;
const char expected_islessequal2 [ ] = " bool f ( ) { \n return true ; \n } " ;
ASSERT_EQUALS ( expected_islessequal2 , tokenizeAndStringify ( code_islessequal2 ) ) ;
const char code_islessequal3 [ ] = " bool f(int i){ \n "
" return islessequal(i,0); \n " // <-- Do not simplify this
" } " ;
const char expected_islessequal3 [ ] = " bool f ( int i ) { \n return islessequal ( i , 0 ) ; \n } " ;
ASSERT_EQUALS ( expected_islessequal3 , tokenizeAndStringify ( code_islessequal3 ) ) ;
2013-10-08 16:05:05 +02:00
}
2013-09-26 00:44:16 +02:00
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_isless ( ) {
2013-09-26 00:44:16 +02:00
// verify isless() simplification
const char code_isless [ ] = " bool f(){ \n "
" return isless(1,0); \n " // (1 < 0) --> false
" } " ;
const char expected_isless [ ] = " bool f ( ) { \n return false ; \n } " ;
ASSERT_EQUALS ( expected_isless , tokenizeAndStringify ( code_isless ) ) ;
const char code_isless1 [ ] = " bool f(){ \n "
" return isless(0,1); \n " // (0 < 1) --> true
" } " ;
const char expected_isless1 [ ] = " bool f ( ) { \n return true ; \n } " ;
ASSERT_EQUALS ( expected_isless1 , tokenizeAndStringify ( code_isless1 ) ) ;
const char code_isless2 [ ] = " bool f(){ \n "
" return isless(0,0); \n " // (0 < 0) --> false
" } " ;
const char expected_isless2 [ ] = " bool f ( ) { \n return false ; \n } " ;
ASSERT_EQUALS ( expected_isless2 , tokenizeAndStringify ( code_isless2 ) ) ;
const char code_isless3 [ ] = " bool f(int i){ \n "
" return isless(i,0); \n " // <-- Do not simplify this
" } " ;
const char expected_isless3 [ ] = " bool f ( int i ) { \n return isless ( i , 0 ) ; \n } " ;
ASSERT_EQUALS ( expected_isless3 , tokenizeAndStringify ( code_isless3 ) ) ;
2013-10-08 16:05:05 +02:00
}
2013-09-26 00:44:16 +02:00
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_isgreaterequal ( ) {
2013-09-26 00:44:16 +02:00
// verify isgreaterequal() simplification
const char code_isgreaterequal [ ] = " bool f(){ \n "
" return isgreaterequal(1,0); \n " // (1 >= 0) --> true
" } " ;
const char expected_isgreaterequal [ ] = " bool f ( ) { \n return true ; \n } " ;
ASSERT_EQUALS ( expected_isgreaterequal , tokenizeAndStringify ( code_isgreaterequal ) ) ;
const char code_isgreaterequal1 [ ] = " bool f(){ \n "
" return isgreaterequal(0,1); \n " // (0 >= 1) --> false
" } " ;
const char expected_isgreaterequal1 [ ] = " bool f ( ) { \n return false ; \n } " ;
ASSERT_EQUALS ( expected_isgreaterequal1 , tokenizeAndStringify ( code_isgreaterequal1 ) ) ;
const char code_isgreaterequal2 [ ] = " bool f(){ \n "
" return isgreaterequal(0,0); \n " // (0 >= 0) --> true
" } " ;
const char expected_isgreaterequal2 [ ] = " bool f ( ) { \n return true ; \n } " ;
ASSERT_EQUALS ( expected_isgreaterequal2 , tokenizeAndStringify ( code_isgreaterequal2 ) ) ;
const char code_isgreaterequal3 [ ] = " bool f(int i){ \n "
" return isgreaterequal(i,0); \n " // <-- Do not simplify this
" } " ;
const char expected_isgreaterequal3 [ ] = " bool f ( int i ) { \n return isgreaterequal ( i , 0 ) ; \n } " ;
ASSERT_EQUALS ( expected_isgreaterequal3 , tokenizeAndStringify ( code_isgreaterequal3 ) ) ;
2013-10-08 16:05:05 +02:00
}
2013-09-26 00:44:16 +02:00
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_isgreater ( ) {
2013-09-26 00:44:16 +02:00
// verify isgreater() simplification
const char code_isgreater [ ] = " bool f(){ \n "
" return isgreater(1,0); \n " // (1 > 0) --> true
" } " ;
const char expected_isgreater [ ] = " bool f ( ) { \n return true ; \n } " ;
ASSERT_EQUALS ( expected_isgreater , tokenizeAndStringify ( code_isgreater ) ) ;
const char code_isgreater1 [ ] = " bool f(){ \n "
" return isgreater(0,1); \n " // (0 > 1) --> false
" } " ;
const char expected_isgreater1 [ ] = " bool f ( ) { \n return false ; \n } " ;
ASSERT_EQUALS ( expected_isgreater1 , tokenizeAndStringify ( code_isgreater1 ) ) ;
const char code_isgreater2 [ ] = " bool f(){ \n "
" return isgreater(0,0); \n " // (0 > 0) --> false
" } " ;
const char expected_isgreater2 [ ] = " bool f ( ) { \n return false ; \n } " ;
ASSERT_EQUALS ( expected_isgreater2 , tokenizeAndStringify ( code_isgreater2 ) ) ;
const char code_isgreater3 [ ] = " bool f(int i){ \n "
" return isgreater(i,0); \n " // <-- Do not simplify this
" } " ;
const char expected_isgreater3 [ ] = " bool f ( int i ) { \n return isgreater ( i , 0 ) ; \n } " ;
ASSERT_EQUALS ( expected_isgreater3 , tokenizeAndStringify ( code_isgreater3 ) ) ;
2013-10-08 16:05:05 +02:00
}
2013-09-26 00:44:16 +02:00
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_fmin ( ) {
2013-09-26 00:44:16 +02:00
// verify fmin,fminl,fminl simplifcation
const char code_fmin [ ] = " void f() { \n "
" std::cout << fmin(-1.0,0); \n "
" std::cout << fmin(1.0,0); \n "
" std::cout << fmin(0,0); \n "
" std::cout << fminf(-1.0,0); \n "
" std::cout << fminf(1.0,0); \n "
" std::cout << fminf(0,0); \n "
" std::cout << fminl(-1.0,0); \n "
" std::cout << fminl(1.0,0); \n "
" std::cout << fminl(0,0); \n "
" } " ;
const char expected_fmin [ ] = " void f ( ) { \n "
" std :: cout << -1.0 ; \n "
" std :: cout << 0 ; \n "
" std :: cout << 0 ; \n "
" std :: cout << -1.0 ; \n "
" std :: cout << 0 ; \n "
" std :: cout << 0 ; \n "
" std :: cout << -1.0 ; \n "
" std :: cout << 0 ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_fmin , tokenizeAndStringify ( code_fmin ) ) ;
// do not simplify this case
const char code_fmin1 [ ] = " float f(float f) { return fmin(f,0);} " ;
const char expected_fmin1 [ ] = " float f ( float f ) { return fmin ( f , 0 ) ; } " ;
ASSERT_EQUALS ( expected_fmin1 , tokenizeAndStringify ( code_fmin1 ) ) ;
2013-10-08 16:05:05 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions_fmax ( ) {
2013-10-08 16:05:05 +02:00
// verify fmax(),fmax(),fmaxl() simplifcation
const char code_fmax [ ] = " void f() { \n "
" std::cout << fmax(-1.0,0); \n "
" std::cout << fmax(1.0,0); \n "
" std::cout << fmax(0,0); \n "
" std::cout << fmaxf(-1.0,0); \n "
" std::cout << fmaxf(1.0,0); \n "
" std::cout << fmaxf(0,0); \n "
" std::cout << fmaxl(-1.0,0); \n "
" std::cout << fmaxl(1.0,0); \n "
" std::cout << fmaxl(0,0); \n "
" } " ;
const char expected_fmax [ ] = " void f ( ) { \n "
" std :: cout << 0 ; \n "
" std :: cout << 1.0 ; \n "
" std :: cout << 0 ; \n "
" std :: cout << 0 ; \n "
" std :: cout << 1.0 ; \n "
" std :: cout << 0 ; \n "
" std :: cout << 0 ; \n "
" std :: cout << 1.0 ; \n "
" std :: cout << 0 ; \n "
" } " ;
ASSERT_EQUALS ( expected_fmax , tokenizeAndStringify ( code_fmax ) ) ;
2013-09-26 00:44:16 +02:00
2013-10-08 16:05:05 +02:00
// do not simplify this case
const char code_fmax1 [ ] = " float f(float f) { return fmax(f,0);} " ;
const char expected_fmax1 [ ] = " float f ( float f ) { return fmax ( f , 0 ) ; } " ;
ASSERT_EQUALS ( expected_fmax1 , tokenizeAndStringify ( code_fmax1 ) ) ;
}
2013-09-26 00:44:16 +02:00
2014-11-20 14:20:09 +01:00
void simplifyMathFunctions ( ) { //#5031
2013-09-26 00:44:16 +02:00
// verify abs,fabs,labs,llabs,atol simplifcation
2013-09-21 16:59:06 +02:00
const char code1 [ ] = " void foo() { \n "
" std::cout<<std::abs(0); \n " // in std:: namespeace
" std::cout<<std::fabs(0.0); \n " // in std:: namespeace
" std::cout<<abs(0); \n "
" std::cout<<fabs(0.0); \n "
" std::cout<<labs(0); \n "
" std::cout<<llabs(0); \n "
" std::cout<<std::abs(-1); \n " // in std:: namespeace
" std::cout<<std::fabs(-1.0); \n " // in std:: namespeace
" std::cout<<abs(-1); \n "
" std::cout<<fabs(-1.0); \n "
" std::cout<<labs(-1); \n "
" std::cout<<llabs(-1); \n "
" std::cout<<atol( \" 1 \" ); \n "
2013-09-26 00:44:16 +02:00
" std::cout<<atol( \" x \" ); \n "
2014-10-31 17:52:29 +01:00
" std::cout<<abs(atol( \" 1 \" )); \n " // nested calls
2013-09-21 16:59:06 +02:00
" } " ;
const char expected1 [ ] = " void foo ( ) { \n "
" std :: cout << 0 ; \n "
" std :: cout << 0.0 ; \n "
" std :: cout << 0 ; \n "
" std :: cout << 0.0 ; \n "
" std :: cout << 0 ; \n "
" std :: cout << 0 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << 1.0 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << 1.0 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << 1 ; \n "
2013-09-26 00:44:16 +02:00
" std :: cout << atol ( \" x \" ) ; \n "
2014-10-31 17:52:29 +01:00
" std :: cout << 1 ; \n "
2013-09-21 16:59:06 +02:00
" } " ;
ASSERT_EQUALS ( expected1 , tokenizeAndStringify ( code1 ) ) ;
// testcase from ticket #5031
const char code2 [ ] = " extern int a; void f(){printf( \" %i \" , abs(--a));} \n " ;
const char expected2 [ ] = " extern int a ; void f ( ) { printf ( \" %i \" , abs ( -- a ) ) ; } " ;
ASSERT_EQUALS ( expected2 , tokenizeAndStringify ( code2 ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyMathExpressions ( ) { //#1620
2013-01-04 11:21:53 +01:00
const char code1 [ ] = " void foo() { \n "
" std::cout<<pow(sin(x),2)+pow(cos(x),2); \n "
2013-10-03 20:52:07 +02:00
" std::cout<<pow(sin(pow(sin(y),2)+pow(cos(y),2)),2)+pow(cos(pow(sin(y),2)+pow(cos(y),2)),2); \n "
2013-10-03 15:41:12 +02:00
" std::cout<<pow(sin(x),2.0)+pow(cos(x),2.0); \n "
" std::cout<<pow(sin(x*y+z),2.0)+pow(cos(x*y+z),2.0); \n "
2013-01-04 13:06:09 +01:00
" std::cout<<pow(sin(x*y+z),2)+pow(cos(x*y+z),2); \n "
2013-10-03 15:41:12 +02:00
" std::cout<<pow(cos(x),2)+pow(sin(x),2); \n "
" std::cout<<pow(cos(x),2.0)+pow(sin(x),2.0); \n "
" std::cout<<pow(cos(x*y+z),2.0)+pow(sin(x*y+z),2.0); \n "
" std::cout<<pow(cos(x*y+z),2)+pow(sin(x*y+z),2); \n "
2013-01-04 13:06:09 +01:00
" std::cout<<pow(sinh(x*y+z),2)-pow(cosh(x*y+z),2); \n "
2013-10-03 15:41:12 +02:00
" std::cout<<pow(sinh(x),2)-pow(cosh(x),2); \n "
" std::cout<<pow(sinh(x*y+z),2.0)-pow(cosh(x*y+z),2.0); \n "
" std::cout<<pow(sinh(x),2.0)-pow(cosh(x),2.0); \n "
" std::cout<<pow(cosh(x*y+z),2)-pow(sinh(x*y+z),2); \n "
" std::cout<<pow(cosh(x),2)-pow(sinh(x),2); \n "
" std::cout<<pow(cosh(x*y+z),2.0)-pow(sinh(x*y+z),2.0); \n "
" std::cout<<pow(cosh(x),2.0)-pow(sinh(x),2.0); \n "
2013-10-03 20:52:07 +02:00
" std::cout<<pow(cosh(pow(x,1)),2.0)-pow(sinh(pow(x,1)),2.0); \n "
2013-01-04 11:21:53 +01:00
" } " ;
2012-09-03 18:51:15 +02:00
2013-01-04 11:21:53 +01:00
const char expected1 [ ] = " void foo ( ) { \n "
" std :: cout << 1 ; \n "
" std :: cout << 1 ; \n "
2013-01-04 13:06:09 +01:00
" std :: cout << 1 ; \n "
2013-10-03 15:41:12 +02:00
" std :: cout << 1 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << 1 ; \n "
2013-10-03 20:52:07 +02:00
" std :: cout << -1 ; \n "
2013-10-03 15:41:12 +02:00
" std :: cout << -1 ; \n "
" std :: cout << -1 ; \n "
" std :: cout << -1 ; \n "
" std :: cout << -1 ; \n "
" std :: cout << -1 ; \n "
" std :: cout << -1 ; \n "
" std :: cout << -1 ; \n "
2013-01-04 13:06:09 +01:00
" std :: cout << -1 ; \n "
2013-01-04 11:21:53 +01:00
" } " ;
2012-09-03 18:51:15 +02:00
ASSERT_EQUALS ( expected1 , tokenizeAndStringify ( code1 ) ) ;
2013-01-04 11:21:53 +01:00
const char code2 [ ] = " void f ( ) { \n "
2013-10-03 15:41:12 +02:00
" a = pow ( sin ( x ) , 2 ) + pow ( cos ( y ) , 2 ) ; \n "
" b = pow ( sinh ( x ) , 2 ) - pow ( cosh ( y ) , 2 ) ; \n "
" c = pow ( sin ( x ) , 2.0 ) + pow ( cos ( y ) , 2.0 ) ; \n "
" d = pow ( sinh ( x ) , 2.0 ) - pow ( cosh ( y ) , 2.0 ) ; \n "
" e = pow ( cos ( x ) , 2 ) + pow ( sin ( y ) , 2 ) ; \n "
" f = pow ( cosh ( x ) , 2 ) - pow ( sinh ( y ) , 2 ) ; \n "
" g = pow ( cos ( x ) , 2.0 ) + pow ( sin ( y ) , 2.0 ) ; \n "
" h = pow ( cosh ( x ) , 2.0 ) - pow ( sinh ( y ) , 2.0 ) ; \n "
2013-01-04 11:21:53 +01:00
" } " ;
ASSERT_EQUALS ( code2 , tokenizeAndStringify ( code2 ) ) ;
2013-10-11 20:58:37 +02:00
const char code3 [ ] = " void foo() { \n "
" std::cout<<powf(sinf(x),2)+powf(cosf(x),2); \n "
" std::cout<<powf(sinf(powf(sinf(y),2)+powf(cosf(y),2)),2)+powf(cosf(powf(sinf(y),2)+powf(cosf(y),2)),2); \n "
" std::cout<<powf(sinf(x),2.0)+powf(cosf(x),2.0); \n "
" std::cout<<powf(sinf(x*y+z),2.0)+powf(cosf(x*y+z),2.0); \n "
" std::cout<<powf(sinf(x*y+z),2)+powf(cosf(x*y+z),2); \n "
" std::cout<<powf(cosf(x),2)+powf(sinf(x),2); \n "
" std::cout<<powf(cosf(x),2.0)+powf(sinf(x),2.0); \n "
" std::cout<<powf(cosf(x*y+z),2.0)+powf(sinf(x*y+z),2.0); \n "
" std::cout<<powf(cosf(x*y+z),2)+powf(sinf(x*y+z),2); \n "
" std::cout<<powf(sinhf(x*y+z),2)-powf(coshf(x*y+z),2); \n "
" std::cout<<powf(sinhf(x),2)-powf(coshf(x),2); \n "
" std::cout<<powf(sinhf(x*y+z),2.0)-powf(coshf(x*y+z),2.0); \n "
" std::cout<<powf(sinhf(x),2.0)-powf(coshf(x),2.0); \n "
" std::cout<<powf(coshf(x*y+z),2)-powf(sinhf(x*y+z),2); \n "
" std::cout<<powf(coshf(x),2)-powf(sinhf(x),2); \n "
" std::cout<<powf(coshf(x*y+z),2.0)-powf(sinhf(x*y+z),2.0); \n "
" std::cout<<powf(coshf(x),2.0)-powf(sinhf(x),2.0); \n "
" std::cout<<powf(coshf(powf(x,1)),2.0)-powf(sinhf(powf(x,1)),2.0); \n "
" } " ;
const char expected3 [ ] = " void foo ( ) { \n "
" std :: cout << 1 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << -1 ; \n "
" std :: cout << -1 ; \n "
" std :: cout << -1 ; \n "
" std :: cout << -1 ; \n "
" std :: cout << -1 ; \n "
" std :: cout << -1 ; \n "
" std :: cout << -1 ; \n "
" std :: cout << -1 ; \n "
" std :: cout << -1 ; \n "
" } " ;
ASSERT_EQUALS ( expected3 , tokenizeAndStringify ( code3 ) ) ;
const char code4 [ ] = " void f ( ) { \n "
" a = powf ( sinf ( x ) , 2 ) + powf ( cosf ( y ) , 2 ) ; \n "
" b = powf ( sinhf ( x ) , 2 ) - powf ( coshf ( y ) , 2 ) ; \n "
" c = powf ( sinf ( x ) , 2.0 ) + powf ( cosf ( y ) , 2.0 ) ; \n "
" d = powf ( sinhf ( x ) , 2.0 ) - powf ( coshf ( y ) , 2.0 ) ; \n "
" e = powf ( cosf ( x ) , 2 ) + powf ( sinf ( y ) , 2 ) ; \n "
" f = powf ( coshf ( x ) , 2 ) - powf ( sinhf ( y ) , 2 ) ; \n "
" g = powf ( cosf ( x ) , 2.0 ) + powf ( sinf ( y ) , 2.0 ) ; \n "
" h = powf ( coshf ( x ) , 2.0 ) - powf ( sinhf ( y ) , 2.0 ) ; \n "
" } " ;
ASSERT_EQUALS ( code4 , tokenizeAndStringify ( code4 ) ) ;
2013-10-25 06:33:00 +02:00
const char code5 [ ] = " void foo() { \n "
" std::cout<<powf(sinl(x),2)+powl(cosl(x),2); \n "
" std::cout<<pow(sinl(powl(sinl(y),2)+powl(cosl(y),2)),2)+powl(cosl(powl(sinl(y),2)+powl(cosl(y),2)),2); \n "
" std::cout<<powl(sinl(x),2.0)+powl(cosl(x),2.0); \n "
" std::cout<<powl(sinl(x*y+z),2.0)+powl(cosl(x*y+z),2.0); \n "
" std::cout<<powl(sinl(x*y+z),2)+powl(cosl(x*y+z),2); \n "
" std::cout<<powl(cosl(x),2)+powl(sinl(x),2); \n "
" std::cout<<powl(cosl(x),2.0)+powl(sinl(x),2.0); \n "
" std::cout<<powl(cosl(x*y+z),2.0)+powl(sinl(x*y+z),2.0); \n "
" std::cout<<powl(cosl(x*y+z),2)+powl(sinl(x*y+z),2); \n "
" std::cout<<powl(sinhl(x*y+z),2)-powl(coshl(x*y+z),2); \n "
" std::cout<<powl(sinhl(x),2)-powl(coshl(x),2); \n "
" std::cout<<powl(sinhl(x*y+z),2.0)-powl(coshl(x*y+z),2.0); \n "
" std::cout<<powl(sinhl(x),2.0)-powl(coshl(x),2.0); \n "
" std::cout<<powl(coshl(x*y+z),2)-powl(sinhl(x*y+z),2); \n "
" std::cout<<powl(coshl(x),2)-powl(sinhl(x),2); \n "
" std::cout<<powl(coshl(x*y+z),2.0)-powl(sinhl(x*y+z),2.0); \n "
" std::cout<<powl(coshl(x),2.0)-powl(sinhl(x),2.0); \n "
" std::cout<<powl(coshl(powl(x,1)),2.0)-powl(sinhl(powl(x,1)),2.0); \n "
" } " ;
const char expected5 [ ] = " void foo ( ) { \n "
" std :: cout << 1 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << 1 ; \n "
" std :: cout << -1 ; \n "
" std :: cout << -1 ; \n "
" std :: cout << -1 ; \n "
" std :: cout << -1 ; \n "
" std :: cout << -1 ; \n "
" std :: cout << -1 ; \n "
" std :: cout << -1 ; \n "
" std :: cout << -1 ; \n "
" std :: cout << -1 ; \n "
" } " ;
ASSERT_EQUALS ( expected5 , tokenizeAndStringify ( code5 ) ) ;
const char code6 [ ] = " void f ( ) { \n "
" a = powl ( sinl ( x ) , 2 ) + powl ( cosl ( y ) , 2 ) ; \n "
" b = powl ( sinhl ( x ) , 2 ) - powl ( coshl ( y ) , 2 ) ; \n "
" c = powl ( sinl ( x ) , 2.0 ) + powl ( cosl ( y ) , 2.0 ) ; \n "
" d = powl ( sinhl ( x ) , 2.0 ) - powl ( coshl ( y ) , 2.0 ) ; \n "
" e = powl ( cosl ( x ) , 2 ) + powl ( sinl ( y ) , 2 ) ; \n "
" f = powl ( coshl ( x ) , 2 ) - powl ( sinhl ( y ) , 2 ) ; \n "
" g = powl ( cosl ( x ) , 2.0 ) + powl ( sinl ( y ) , 2.0 ) ; \n "
" h = powl ( coshl ( x ) , 2.0 ) - powl ( sinhl ( y ) , 2.0 ) ; \n "
" } " ;
ASSERT_EQUALS ( code6 , tokenizeAndStringify ( code6 ) ) ;
2012-09-03 18:51:15 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyStaticConst ( ) {
2014-07-02 08:59:04 +02:00
const char code1 [ ] = " class foo { public: bool const static c ; } " ;
const char expected1 [ ] = " class foo { public: static const bool c ; } " ;
ASSERT_EQUALS ( expected1 , tokenizeAndStringify ( code1 , true ) ) ;
const char code2 [ ] =
" int long long f() \n "
" { \n "
" static const long long signed int i1; \n "
" static const long long int signed i2; \n "
" static const signed long long int i3; \n "
" static const signed int long long i4; \n "
" static const int signed long long i5; \n "
" static const int long long signed i6; \n "
" long long static const signed int i7; \n "
" long long static const int signed i8; \n "
" signed static const long long int i9; \n "
" signed static const int long long i10; \n "
" int static const signed long long i11; \n "
" int static const long long signed i12; \n "
" long long signed int static const i13; \n "
" long long int signed static const i14; \n "
" signed long long int static const i15; \n "
" signed int long long static const i16; \n "
" int signed long long static const i17; \n "
" int long long signed static const i18; \n "
" return i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8 + i9 + i10 + i11 + i12 \n "
" + i13 + i14 + i15 + i16 + i17 + i18; \n "
" } " ;
const char expected2 [ ] =
" long long f ( ) \n "
" { \n "
" static const signed long long i1 ; \n "
" static const signed long long i2 ; \n "
" static const signed long long i3 ; \n "
" static const signed long long i4 ; \n "
" static const signed long long i5 ; \n "
" static const signed long long i6 ; \n "
2015-08-27 14:34:00 +02:00
" static const signed long long i7 ; \n "
" static const signed long long i8 ; \n "
" static const signed long long i9 ; \n "
" static const signed long long i10 ; \n "
" static const signed long long i11 ; \n "
" static const signed long long i12 ; \n "
2014-07-02 08:59:04 +02:00
" static const signed long long i13 ; \n "
" static const signed long long i14 ; \n "
" static const signed long long i15 ; \n "
" static const signed long long i16 ; \n "
" static const signed long long i17 ; \n "
" static const signed long long i18 ; \n "
" return i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8 + i9 + i10 + i11 + i12 \n "
" + i13 + i14 + i15 + i16 + i17 + i18 ; \n "
" } " ;
ASSERT_EQUALS ( expected2 , tokenizeAndStringify ( code2 , true ) ) ;
2015-08-29 10:57:52 +02:00
const char code3 [ ] = " const unsigned long extern int i; " ;
const char expected3 [ ] = " extern const unsigned long i ; " ;
ASSERT_EQUALS ( expected3 , tokenizeAndStringify ( code3 , true ) ) ;
2014-07-02 08:59:04 +02:00
}
2014-04-13 12:47:54 +02:00
2015-05-10 12:35:47 +02:00
void simplifyDeprecated ( ) {
ASSERT_EQUALS ( " int f ( ) ; " ,
tokenizeAndStringify ( " [[deprecated]] int f(); " , false , true , Settings : : Unspecified , " test.cpp " , true ) ) ;
ASSERT_EQUALS ( " [ [ deprecated ] ] int f ( ) ; " ,
tokenizeAndStringify ( " [[deprecated]] int f(); " , false , true , Settings : : Unspecified , " test.cpp " , false ) ) ;
ASSERT_EQUALS ( " [ [ deprecated ] ] int f ( ) ; " ,
tokenizeAndStringify ( " [[deprecated]] int f(); " , false , true , Settings : : Unspecified , " test.c " , true ) ) ;
}
2015-11-06 18:39:03 +01:00
void simplifyCaseRange ( ) {
2015-11-06 19:59:23 +01:00
ASSERT_EQUALS ( " void f ( ) { case 1 : case 2 : case 3 : case 4 : ; } " , tokenizeAndStringify ( " void f() { case 1 ... 4: } " ) ) ;
2015-11-06 18:39:03 +01:00
ASSERT_EQUALS ( " void f ( ) { case 4 . . . 1 : ; } " , tokenizeAndStringify ( " void f() { case 4 ... 1: } " ) ) ;
2015-11-06 19:21:56 +01:00
tokenizeAndStringify ( " void f() { case 1 ... 1000000: } " ) ; // Do not run out of memory
2015-11-06 18:46:43 +01:00
2015-11-06 19:59:23 +01:00
ASSERT_EQUALS ( " void f ( ) { case 'a' : case 'b' : case 'c' : ; } " , tokenizeAndStringify ( " void f() { case 'a' ... 'c': } " ) ) ;
2015-11-06 18:46:43 +01:00
ASSERT_EQUALS ( " void f ( ) { case 'c' . . . 'a' : ; } " , tokenizeAndStringify ( " void f() { case 'c' ... 'a': } " ) ) ;
2015-11-06 18:39:03 +01:00
}
2015-10-12 18:14:56 +02:00
void prepareTernaryOpForAST ( ) {
ASSERT_EQUALS ( " a ? b : c ; " , tokenizeAndStringify ( " a ? b : c; " ) ) ;
ASSERT_EQUALS ( " a ? ( b , c ) : d ; " , tokenizeAndStringify ( " a ? b , c : d; " ) ) ;
ASSERT_EQUALS ( " a ? ( b , c ) : d ; " , tokenizeAndStringify ( " a ? (b , c) : d; " ) ) ;
ASSERT_EQUALS ( " a ? ( 1 ? ( a , b ) : 3 ) : d ; " , tokenizeAndStringify ( " a ? 1 ? a, b : 3 : d; " ) ) ;
}
2015-10-07 18:33:57 +02:00
std : : string testAst ( const char code [ ] , bool verbose = false ) {
2012-12-15 20:21:09 +01:00
// tokenize given code..
2015-10-07 18:33:57 +02:00
Tokenizer tokenList ( & settings0 , nullptr ) ;
2012-12-15 20:21:09 +01:00
std : : istringstream istr ( code ) ;
2014-05-24 11:28:43 +02:00
if ( ! tokenList . list . createTokens ( istr , " test.cpp " ) )
2012-12-15 20:21:09 +01:00
return " ERROR " ;
2014-05-24 11:28:43 +02:00
tokenList . combineOperators ( ) ;
tokenList . createLinks ( ) ;
tokenList . createLinks2 ( ) ;
2013-11-02 19:18:58 +01:00
2012-12-15 20:21:09 +01:00
// Create AST..
2015-10-12 18:14:56 +02:00
tokenList . prepareTernaryOpForAST ( ) ;
2014-05-24 11:28:43 +02:00
tokenList . list . createAst ( ) ;
2012-12-15 20:21:09 +01:00
2014-05-19 10:38:54 +02:00
// Basic AST validation
2014-05-24 11:28:43 +02:00
for ( const Token * tok = tokenList . list . front ( ) ; tok ; tok = tok - > next ( ) ) {
2014-05-19 21:54:59 +02:00
if ( tok - > astOperand2 ( ) & & ! tok - > astOperand1 ( ) & & tok - > str ( ) ! = " ; " & & tok - > str ( ) ! = " : " )
2014-05-19 10:38:54 +02:00
return " Op2 but no Op1 for token: " + tok - > str ( ) ;
}
2014-01-27 06:18:42 +01:00
// Return stringified AST
2014-04-25 06:06:54 +02:00
if ( verbose )
2014-05-24 11:28:43 +02:00
return tokenList . list . front ( ) - > astTop ( ) - > astStringVerbose ( 0 , 0 ) ;
2014-04-25 06:06:54 +02:00
2014-01-27 06:18:42 +01:00
std : : string ret ;
std : : set < const Token * > astTop ;
2014-05-24 11:28:43 +02:00
for ( const Token * tok = tokenList . list . front ( ) ; tok ; tok = tok - > next ( ) ) {
2014-01-27 06:18:42 +01:00
if ( tok - > astOperand1 ( ) & & astTop . find ( tok - > astTop ( ) ) = = astTop . end ( ) ) {
astTop . insert ( tok - > astTop ( ) ) ;
if ( ! ret . empty ( ) )
ret = ret + " " ;
ret + = tok - > astTop ( ) - > astString ( ) ;
}
2013-11-02 18:37:35 +01:00
}
2014-01-27 06:18:42 +01:00
return ret ;
2012-12-15 20:21:09 +01:00
}
2015-10-07 18:33:57 +02:00
void astexpr ( ) { // simple expressions with arithmetical ops
2012-12-16 11:48:19 +01:00
ASSERT_EQUALS ( " 12+3+ " , testAst ( " 1+2+3 " ) ) ;
2012-12-15 20:21:09 +01:00
ASSERT_EQUALS ( " 12*3+ " , testAst ( " 1*2+3 " ) ) ;
ASSERT_EQUALS ( " 123*+ " , testAst ( " 1+2*3 " ) ) ;
ASSERT_EQUALS ( " 12*34*+ " , testAst ( " 1*2+3*4 " ) ) ;
2012-12-16 11:48:19 +01:00
ASSERT_EQUALS ( " 12*34*5*+ " , testAst ( " 1*2+3*4*5 " ) ) ;
2014-05-04 18:36:04 +02:00
ASSERT_EQUALS ( " 0(r.& " , testAst ( " (&((typeof(x))0).r); " ) ) ;
ASSERT_EQUALS ( " 0(r.& " , testAst ( " &((typeof(x))0).r; " ) ) ;
2012-12-16 11:56:23 +01:00
// Various tests of precedence
ASSERT_EQUALS ( " ab::c+ " , testAst ( " a::b+c " ) ) ;
ASSERT_EQUALS ( " abc+= " , testAst ( " a=b+c " ) ) ;
ASSERT_EQUALS ( " abc=, " , testAst ( " a,b=c " ) ) ;
2013-11-11 16:39:34 +01:00
ASSERT_EQUALS ( " a-1+ " , testAst ( " -a+1 " ) ) ;
ASSERT_EQUALS ( " ab++-c- " , testAst ( " a-b++-c " ) ) ;
2012-12-16 11:56:23 +01:00
2015-02-18 19:56:13 +01:00
// sizeof
ASSERT_EQUALS ( " ab.sizeof " , testAst ( " sizeof a.b " ) ) ;
2014-04-21 13:05:34 +02:00
// assignment operators
ASSERT_EQUALS ( " ab>>= " , testAst ( " a>>=b; " ) ) ;
ASSERT_EQUALS ( " ab<<= " , testAst ( " a<<=b; " ) ) ;
ASSERT_EQUALS ( " ab+= " , testAst ( " a+=b; " ) ) ;
ASSERT_EQUALS ( " ab-= " , testAst ( " a-=b; " ) ) ;
ASSERT_EQUALS ( " ab*= " , testAst ( " a*=b; " ) ) ;
ASSERT_EQUALS ( " ab/= " , testAst ( " a/=b; " ) ) ;
ASSERT_EQUALS ( " ab%= " , testAst ( " a%=b; " ) ) ;
ASSERT_EQUALS ( " ab&= " , testAst ( " a&=b; " ) ) ;
ASSERT_EQUALS ( " ab|= " , testAst ( " a|=b; " ) ) ;
ASSERT_EQUALS ( " ab^= " , testAst ( " a^=b; " ) ) ;
2014-05-19 21:54:59 +02:00
ASSERT_EQUALS ( " ab*c*.(+return " , testAst ( " return a + ((*b).*c)(); " ) ) ;
2014-05-03 12:08:42 +02:00
// assignments are executed from right to left
ASSERT_EQUALS ( " abc== " , testAst ( " a=b=c; " ) ) ;
2015-10-12 18:14:56 +02:00
// ternary operator
2015-07-24 13:02:00 +02:00
ASSERT_EQUALS ( " ab0=c1=:? " , testAst ( " a?b=0:c=1; " ) ) ;
2015-10-12 18:14:56 +02:00
ASSERT_EQUALS ( " fabc,d:?=e, " , testAst ( " f = a ? b, c : d, e; " ) ) ;
ASSERT_EQUALS ( " fabc,de,:?= " , testAst ( " f = (a ? (b, c) : (d, e)); " ) ) ;
ASSERT_EQUALS ( " fabc,de,:?= " , testAst ( " f = (a ? b, c : (d, e)); " ) ) ;
ASSERT_EQUALS ( " ab35,4:?foo(:?return " , testAst ( " return (a ? b ? (3,5) : 4 : foo()); " ) ) ;
2015-10-17 17:03:24 +02:00
ASSERT_EQUALS ( " check(result_type00,{invalid:?return " , testAst ( " return check() ? result_type {0, 0} : invalid; " ) ) ;
2015-07-24 13:02:00 +02:00
2013-11-04 11:26:16 +01:00
ASSERT_EQUALS ( " a \" \" = " , testAst ( " a= \" \" " ) ) ;
ASSERT_EQUALS ( " a \' \' = " , testAst ( " a= \' \' " ) ) ;
2014-06-05 17:36:29 +02:00
ASSERT_EQUALS ( " a1[ \" \" = " , testAst ( " char a[1]= \" \" ; " ) ) ;
2013-11-20 05:57:56 +01:00
ASSERT_EQUALS ( " 'X''a'> " , testAst ( " ('X' > 'a') " ) ) ;
ASSERT_EQUALS ( " 'X''a'> " , testAst ( " (L'X' > L'a') " ) ) ;
2014-01-08 21:49:42 +01:00
2014-05-11 13:39:28 +02:00
ASSERT_EQUALS ( " a0>bc/d:? " , testAst ( " (a>0) ? (b/(c)) : d; " ) ) ;
2014-01-08 21:49:42 +01:00
ASSERT_EQUALS ( " abc/+d+ " , testAst ( " a + (b/(c)) + d; " ) ) ;
2015-02-01 14:50:51 +01:00
ASSERT_EQUALS ( " f( x1024x/0:? " , testAst ( " void f() { x ? 1024 / x : 0; } " ) ) ;
2014-01-15 17:32:14 +01:00
2014-01-18 13:18:11 +01:00
ASSERT_EQUALS ( " absizeofd(ef.+(= " , testAst ( " a = b(sizeof(c d) + e.f) " ) ) ;
2014-05-17 12:09:32 +02:00
ASSERT_EQUALS ( " a*b*** " , testAst ( " *a * **b; " ) ) ; // Correctly distinguish between unary and binary operator*
2015-07-27 13:13:30 +02:00
// strings
ASSERT_EQUALS ( " f \" A \" 1,( " , testAst ( " f( \" A \" B, 1); " ) ) ;
ASSERT_EQUALS ( " fA1,( " , testAst ( " f(A \" B \" , 1); " ) ) ;
2014-01-15 17:32:14 +01:00
// for
ASSERT_EQUALS ( " for;;( " , testAst ( " for(;;) " ) ) ;
ASSERT_EQUALS ( " fora0=a8<a++;;( " , testAst ( " for(a=0;a<8;a++) " ) ) ;
2014-05-19 10:38:54 +02:00
ASSERT_EQUALS ( " fori1=current0=,iNUM<=i++;;( " , testAst ( " for(i = (1), current = 0; i <= (NUM); ++i) " ) ) ;
2014-01-23 06:13:24 +01:00
ASSERT_EQUALS ( " foreachxy,(( " , testAst ( " for(each(x,y)){} " ) ) ; // it's not well-defined what this ast should be
ASSERT_EQUALS ( " forab:( " , testAst ( " for (int a : b); " ) ) ;
2014-03-31 17:16:13 +02:00
ASSERT_EQUALS ( " forx*0=yz;;( " , testAst ( " for(*x=0;y;z) " ) ) ;
2014-01-27 06:18:42 +01:00
// problems with multiple expressions
ASSERT_EQUALS ( " ax( whilex( " , testAst ( " a(x) while (x) " ) ) ;
ASSERT_EQUALS ( " ifx( i0= whilei( " , testAst ( " if (x) { ({ int i = 0; while(i); }) }; " ) ) ;
ASSERT_EQUALS ( " ifx( BUG_ON{!( i0= whilei( " , testAst ( " if (x) { BUG_ON(!({int i=0; while(i);})); } " ) ) ;
2014-05-24 11:28:43 +02:00
ASSERT_EQUALS ( " v0= while{0!=( v0= while{0!=( v0= " , testAst ( " ({ v = 0; }); while (({ v = 0; }) != 0); while (({ v = 0; }) != 0); " ) ) ;
2014-05-19 21:54:59 +02:00
ASSERT_EQUALS ( " abc.1:?1+bd.1:?+= " , testAst ( " a =(b.c ? : 1) + 1 + (b.d ? : 1); " ) ) ;
2014-06-04 18:08:51 +02:00
2015-10-17 17:03:24 +02:00
ASSERT_EQUALS ( " try{ catch.( " , testAst ( " try {} catch (...) {} " ) ) ;
2014-06-04 18:08:51 +02:00
ASSERT_EQUALS ( " FooBar( " , testAst ( " void Foo(Bar&); " ) ) ;
ASSERT_EQUALS ( " FooBar( " , testAst ( " void Foo(Bar& &); " ) ) ; // Rvalue reference - simplified from && to & & by real tokenizer
ASSERT_EQUALS ( " DerivedDerived::( " , testAst ( " Derived::~Derived() {} " ) ) ;
2014-06-26 09:03:02 +02:00
ASSERT_EQUALS ( " ifCA_FarReadfilenew(,sizeofobjtype(,(!( " , testAst ( " if (!CA_FarRead(file, (void far *)new, sizeof(objtype))) " ) ) ; // #5910 - don't hang if C code is parsed as C++
2014-06-04 18:08:51 +02:00
}
2015-10-07 18:33:57 +02:00
void astnewdelete ( ) {
2014-06-04 18:08:51 +02:00
ASSERT_EQUALS ( " aintnew= " , testAst ( " a = new int; " ) ) ;
2014-09-29 10:26:15 +02:00
ASSERT_EQUALS ( " aint4[new= " , testAst ( " a = new int[4]; " ) ) ;
2014-10-10 10:37:54 +02:00
ASSERT_EQUALS ( " aFoobar(new= " , testAst ( " a = new Foo(bar); " ) ) ;
ASSERT_EQUALS ( " aFoobar(new= " , testAst ( " a = new Foo(bar); " ) ) ;
ASSERT_EQUALS ( " aFoo(new= " , testAst ( " a = new Foo<bar>(); " ) ) ;
ASSERT_EQUALS ( " X12,3,(new " , testAst ( " new (a,b,c) X(1,2,3); " ) ) ;
2014-10-12 09:24:43 +02:00
ASSERT_EQUALS ( " aXnew( " , testAst ( " a (new (X)); " ) ) ;
2014-10-12 10:20:03 +02:00
ASSERT_EQUALS ( " aXnew5,( " , testAst ( " a (new (X), 5); " ) ) ;
2014-06-04 18:08:51 +02:00
ASSERT_EQUALS ( " adelete " , testAst ( " delete a; " ) ) ;
ASSERT_EQUALS ( " adelete " , testAst ( " delete (a); " ) ) ;
ASSERT_EQUALS ( " adelete " , testAst ( " delete[] a; " ) ) ;
ASSERT_EQUALS ( " ab.3c-(delete " , testAst ( " delete[] a.b(3 - c); " ) ) ;
2014-10-12 11:12:25 +02:00
ASSERT_EQUALS ( " a::new= " , testAst ( " a = new (b) ::X; " ) ) ;
2014-10-12 12:13:01 +02:00
ASSERT_EQUALS ( " aA1(new(bB2(new(, " , testAst ( " a(new A(1)), b(new B(2)) " ) ) ;
2015-10-20 23:55:29 +02:00
ASSERT_EQUALS ( " Fred10[new " , testAst ( " ;new Fred[10]; " ) ) ;
2015-11-18 21:37:37 +01:00
ASSERT_EQUALS ( " f( adelete " , testAst ( " void f() { delete a; } " ) ) ;
2014-10-10 12:47:01 +02:00
2014-10-13 19:12:20 +02:00
// invalid code (libreoffice), don't hang
// #define SlideSorterViewShell
// SfxViewFrame* pFrame;
// new SlideSorterViewShell(pFrame,rViewShellBase,pParentWindow,pFrameViewArgument);
ASSERT_EQUALS ( " fxnewy,z,( " , testAst ( " f(new (x,y,z)); " ) ) ;
2014-10-10 12:47:01 +02:00
// clang testsuite..
ASSERT_EQUALS ( " const0(new " , testAst ( " new const auto (0); " ) ) ;
2014-10-11 11:27:13 +02:00
ASSERT_EQUALS ( " autonew " , testAst ( " new (auto) (0.0); " ) ) ;
2014-10-12 11:12:25 +02:00
ASSERT_EQUALS ( " int3[4[5[new " , testAst ( " new (int S::*[3][4][5]) (); " ) ) ;
2014-10-11 11:27:13 +02:00
ASSERT_EQUALS ( " pSnew= " , testAst ( " p=new (x)(S)(1,2); " ) ) ;
ASSERT_EQUALS ( " inti[new( " , testAst ( " (void)new (int[i]); " ) ) ;
2014-10-11 13:20:48 +02:00
ASSERT_EQUALS ( " intp* pnew malloc4( " , testAst ( " int*p; new (p) (malloc(4)); " ) ) ;
ASSERT_EQUALS ( " intnew " , testAst ( " new (&w.x)(int*)(0); " ) ) ;
ASSERT_EQUALS ( " &new " , testAst ( " new (&w.x)(0); " ) ) ; // <- the "(int*)" has been simplified
2014-10-12 12:57:01 +02:00
// gcc testsuite..
ASSERT_EQUALS ( " char10[new( " , testAst ( " (void)new(char*)[10]; " ) ) ;
2012-12-15 20:21:09 +01:00
}
2015-10-07 18:33:57 +02:00
void astpar ( ) { // parentheses
2012-12-15 20:21:09 +01:00
ASSERT_EQUALS ( " 12+3* " , testAst ( " (1+2)*3 " ) ) ;
ASSERT_EQUALS ( " 123+* " , testAst ( " 1*(2+3) " ) ) ;
ASSERT_EQUALS ( " 123+*4* " , testAst ( " 1*(2+3)*4 " ) ) ;
2013-11-04 11:26:16 +01:00
ASSERT_EQUALS ( " ifab.c&d==( " , testAst ( " if((a.b&c)==d){} " ) ) ;
2013-11-11 16:39:34 +01:00
2013-11-27 06:17:00 +01:00
ASSERT_EQUALS ( " pf.pf.12,(&& " , testAst ( " ((p.f) && (p.f)(1,2)) " ) ) ;
2014-04-15 15:46:26 +02:00
// problems with: if (x[y]==z)
ASSERT_EQUALS ( " ifa(0[1==( " , testAst ( " if(a()[0]==1){} " ) ) ;
ASSERT_EQUALS ( " ifbuff0[&(*1==( " , testAst ( " if (*((DWORD*)&buff[0])==1){} " ) ) ;
2014-04-26 13:16:34 +02:00
ASSERT_EQUALS ( " ifp*0[1==( " , testAst ( " if((*p)[0]==1) " ) ) ;
2014-05-08 06:48:53 +02:00
ASSERT_EQUALS ( " ifab.cd.[e==( " , testAst ( " if(a.b[c.d]==e){} " ) ) ;
2014-04-26 13:16:34 +02:00
2014-05-19 10:38:54 +02:00
ASSERT_EQUALS ( " iftpnote.i1-[note.0==tpnote.i1-[type.4>||( " , testAst ( " if ((tp.note[i - 1].note == 0) || (tp.note[i - 1].type > 4)) {} " ) ) ;
ASSERT_EQUALS ( " ab.i[j1+[ " , testAst ( " a.b[i][j+1] " ) ) ;
2014-05-17 12:09:32 +02:00
2014-04-26 13:16:34 +02:00
// problems with: x=expr
2014-04-25 06:06:54 +02:00
ASSERT_EQUALS ( " = \n "
" |-x \n "
" `-( \n "
" `-. \n "
" |-[ \n "
" | |-a \n "
" | `-i \n "
" `-f \n " ,
testAst ( " x = ((a[i]).f)(); " , true ) ) ;
2014-04-29 06:09:26 +02:00
ASSERT_EQUALS ( " abc.de.++[= " , testAst ( " a = b.c[++(d.e)]; " ) ) ;
2014-04-26 13:32:08 +02:00
ASSERT_EQUALS ( " abc(1+= " , testAst ( " a = b(c**)+1; " ) ) ;
2014-04-27 18:03:50 +02:00
ASSERT_EQUALS ( " abc.= " , testAst ( " a = (b).c; " ) ) ;
2014-04-15 15:46:26 +02:00
2013-11-11 16:39:34 +01:00
// casts
ASSERT_EQUALS ( " a1(2(+= " , testAst ( " a=(t)1+(t)2; " ) ) ;
ASSERT_EQUALS ( " a1(2+= " , testAst ( " a=(t)1+2; " ) ) ;
2013-12-25 21:19:28 +01:00
ASSERT_EQUALS ( " a1(2+= " , testAst ( " a=(t*)1+2; " ) ) ;
ASSERT_EQUALS ( " a1(2+= " , testAst ( " a=(t&)1+2; " ) ) ;
ASSERT_EQUALS ( " ab::r&c(= " , testAst ( " a::b& r = (a::b&)c; " ) ) ; // #5261
2014-05-11 13:39:28 +02:00
ASSERT_EQUALS ( " ab10:?= " , testAst ( " a=(b)?1:0; " ) ) ;
2013-11-25 20:58:40 +01:00
// ({..})
2014-01-27 06:18:42 +01:00
ASSERT_EQUALS ( " a{+d+ bc+ " , testAst ( " a+({b+c;})+d " ) ) ;
ASSERT_EQUALS ( " a{d*+ bc+ " , testAst ( " a+({b+c;})*d " ) ) ;
2014-02-05 06:05:48 +01:00
ASSERT_EQUALS ( " xa{((= bc( yd{((= ef( " ,
testAst ( " x=(int)(a({b(c);})); " // don't hang
" y=(int)(d({e(f);})); " ) ) ;
ASSERT_EQUALS ( " QT_WA{{,( QT_WA{{,( x1= " ,
testAst ( " QT_WA({},{x=0;}); " // don't hang
" QT_WA({x=1;},{x=2;}); " ) ) ;
2014-08-25 19:10:16 +02:00
ASSERT_EQUALS ( " xMACROtypeT=value1=,{({= " ,
testAst ( " x = { MACRO( { .type=T, .value=1 } ) } " ) ) ; // dont hang: MACRO({..})
2014-06-04 18:08:51 +02:00
// function pointer
TODO_ASSERT_EQUALS ( " todo " , " va_argapvoid((,(*0= " , testAst ( " *va_arg(ap, void(**) ()) = 0; " ) ) ;
2014-07-31 23:14:44 +02:00
// struct initialization
ASSERT_EQUALS ( " name_bytes[bits~unusedBits>>unusedBits<<{= " , testAst ( " const uint8_t name_bytes[] = { (~bits >> unusedBits) << unusedBits }; " ) ) ;
ASSERT_EQUALS ( " abuf0{={= " , testAst ( " a = { .buf = { 0 } }; " ) ) ;
ASSERT_EQUALS ( " tset{= " , testAst ( " struct cgroup_taskset tset = {}; " ) ) ;
2015-07-21 11:40:42 +02:00
ASSERT_EQUALS ( " s1a&,{2b&,{,{= " , testAst ( " s = { {1, &a}, {2, &b} }; " ) ) ;
2014-09-29 10:59:58 +02:00
2015-10-17 17:03:24 +02:00
// template parentheses: <>
2014-09-29 10:59:58 +02:00
ASSERT_EQUALS ( " stdfabs::m_similarity(numeric_limitsepsilon::(<=return " , testAst ( " return std::fabs(m_similarity) <= numeric_limits<double>::epsilon(); " ) ) ; // #6195
2015-10-17 17:03:24 +02:00
// C++ initializer
ASSERT_EQUALS ( " Class{ " , testAst ( " Class{}; " ) ) ;
ASSERT_EQUALS ( " Class12,{ " , testAst ( " Class{1,2}; " ) ) ;
2015-10-18 13:43:39 +02:00
ASSERT_EQUALS ( " Class12,{ " , testAst ( " Class<X>{1,2}; " ) ) ;
2015-10-17 17:03:24 +02:00
ASSERT_EQUALS ( " abc{d:?= " , testAst ( " a=b?c{}:d; " ) ) ;
ASSERT_EQUALS ( " abc12,{d:?= " , testAst ( " a=b?c{1,2}:d; " ) ) ;
2015-10-18 13:43:39 +02:00
ASSERT_EQUALS ( " abc{d:?= " , testAst ( " a=b?c<X>{}:d; " ) ) ;
ASSERT_EQUALS ( " abc12,{d:?= " , testAst ( " a=b?c<X>{1,2}:d; " ) ) ;
2015-10-18 16:58:15 +02:00
ASSERT_EQUALS ( " a::12,{ " , testAst ( " ::a{1,2}; " ) ) ; // operator precedence
2015-11-06 17:34:26 +01:00
ASSERT_EQUALS ( " Abc({newreturn " , testAst ( " return new A {b(c)}; " ) ) ;
2012-12-15 20:21:09 +01:00
}
2015-10-07 18:33:57 +02:00
void astbrackets ( ) { // []
2013-11-04 11:26:16 +01:00
ASSERT_EQUALS ( " a23+[4+ " , testAst ( " a[2+3]+4 " ) ) ;
2013-11-13 17:52:56 +01:00
ASSERT_EQUALS ( " a1[0[ " , testAst ( " a[1][0] " ) ) ;
2013-11-25 20:58:40 +01:00
ASSERT_EQUALS ( " ab0[= " , testAst ( " a=(b)[0]; " ) ) ;
2014-04-29 06:09:26 +02:00
ASSERT_EQUALS ( " abc.0[= " , testAst ( " a=b.c[0]; " ) ) ;
2013-11-25 20:58:40 +01:00
ASSERT_EQUALS ( " ab0[1[= " , testAst ( " a=b[0][1]; " ) ) ;
2012-12-15 20:21:09 +01:00
}
2015-10-07 18:33:57 +02:00
void astunaryop ( ) { // unary operators
2012-12-16 08:41:04 +01:00
ASSERT_EQUALS ( " 1a--+ " , testAst ( " 1 + --a " ) ) ;
ASSERT_EQUALS ( " 1a--+ " , testAst ( " 1 + a-- " ) ) ;
ASSERT_EQUALS ( " ab+! " , testAst ( " !(a+b) " ) ) ;
2014-04-27 16:02:24 +02:00
ASSERT_EQUALS ( " ab.++ " , testAst ( " ++a.b; " ) ) ;
ASSERT_EQUALS ( " ab.++ " , testAst ( " a.b++; " ) ) ;
ASSERT_EQUALS ( " ab::++ " , testAst ( " a::b++; " ) ) ;
ASSERT_EQUALS ( " c5[--* " , testAst ( " *c[5]--; " ) ) ;
2014-05-19 10:38:54 +02:00
ASSERT_EQUALS ( " a*bc:?return " , testAst ( " return *a ? b : c; " ) ) ;
2013-12-27 14:40:59 +01:00
2014-02-25 06:36:10 +01:00
// Unary :: operator
2014-05-17 12:09:32 +02:00
ASSERT_EQUALS ( " abcd::12,(e/:?= " , testAst ( " a = b ? c : ::d(1,2) / e; " ) ) ;
2014-02-25 06:36:10 +01:00
2013-12-27 14:40:59 +01:00
// how is "--" handled here:
2014-05-11 13:39:28 +02:00
ASSERT_EQUALS ( " ab4<<c--+1:? " , testAst ( " a ? (b << 4) + --c : 1 " ) ) ;
ASSERT_EQUALS ( " ab4<<c--+1:? " , testAst ( " a ? (b << 4) + c-- : 1 " ) ) ;
2014-06-26 17:31:57 +02:00
ASSERT_EQUALS ( " ai[i= i-- " , testAst ( " a[i]=i; --i; " ) ) ;
2012-12-16 08:41:04 +01:00
}
2015-10-07 18:33:57 +02:00
void astfunction ( ) { // function calls
2013-11-04 11:26:16 +01:00
ASSERT_EQUALS ( " 1f(+2+ " , testAst ( " 1+f()+2 " ) ) ;
ASSERT_EQUALS ( " 1f2(+3+ " , testAst ( " 1+f(2)+3 " ) ) ;
ASSERT_EQUALS ( " 1f23,(+4+ " , testAst ( " 1+f(2,3)+4 " ) ) ;
ASSERT_EQUALS ( " 1f2a&,(+ " , testAst ( " 1+f(2,&a) " ) ) ;
2014-06-05 17:36:29 +02:00
ASSERT_EQUALS ( " fargv[( " , testAst ( " int f(char argv[]); " ) ) ;
ASSERT_EQUALS ( " fchar( " , testAst ( " extern unsigned f(const char *); " ) ) ;
ASSERT_EQUALS ( " fcharformat*.,( " , testAst ( " extern void f(const char *format, ...); " ) ) ;
ASSERT_EQUALS ( " for_each_commit_graftint((void,( " , testAst ( " extern int for_each_commit_graft(int (*)(int*), void *); " ) ) ;
ASSERT_EQUALS ( " for;;( " , testAst ( " for (;;) {} " ) ) ;
2013-12-25 22:08:53 +01:00
ASSERT_EQUALS ( " xsizeofvoid(= " , testAst ( " x=sizeof(void*) " ) ) ;
2012-12-16 08:41:04 +01:00
}
2015-10-07 18:33:57 +02:00
void asttemplate ( ) { // uninstantiated templates will have <,>,etc..
2014-01-09 17:14:16 +01:00
ASSERT_EQUALS ( " a(3== " , testAst ( " a<int>()==3 " ) ) ;
2014-01-27 06:18:42 +01:00
ASSERT_EQUALS ( " ab(== f( " , testAst ( " a == b<c>(); f(); " ) ) ;
2014-10-10 08:18:24 +02:00
ASSERT_EQUALS ( " static_casta(i[ " , testAst ( " ; static_cast<char*>(a)[i]; " ) ) ; // #6203
2014-02-17 17:37:39 +01:00
2014-05-17 12:09:32 +02:00
// This two unit tests were added to avoid a crash. The actual correct AST result for non-executable code has not been determined so far.
ASSERT_EQUALS ( " Cpublica::b::: " , testAst ( " class C : public ::a::b<bool> { }; " ) ) ;
ASSERT_EQUALS ( " AB: f( abc+= " , testAst ( " struct A : public B<C*> { void f() { a=b+c; } }; " ) ) ;
2012-12-15 20:21:09 +01:00
}
2014-04-13 12:47:54 +02:00
2015-10-07 18:33:57 +02:00
void astcast ( ) {
2014-05-24 11:28:43 +02:00
ASSERT_EQUALS ( " ac&(= " , testAst ( " a = (long)&c; " ) ) ;
ASSERT_EQUALS ( " ac*(= " , testAst ( " a = (Foo*)*c; " ) ) ;
ASSERT_EQUALS ( " ac-(= " , testAst ( " a = (long)-c; " ) ) ;
2015-10-17 18:25:27 +02:00
ASSERT_EQUALS ( " ac~(= " , testAst ( " a = (b)~c; " ) ) ;
2014-05-24 11:28:43 +02:00
ASSERT_EQUALS ( " ac(= " , testAst ( " a = (some<strange, type>)c; " ) ) ;
2014-05-24 20:21:08 +02:00
ASSERT_EQUALS ( " afoveon_avgimage((foveon_avgimage((+= " , testAst ( " a = foveon_avg(((short(*)[4]) image)) + foveon_avg(((short(*)[4]) image)); " ) ) ;
2014-09-12 06:43:52 +02:00
ASSERT_EQUALS ( " c(40<<return " , testAst ( " return (long long)c << 40; " ) ) ;
2014-05-24 19:04:47 +02:00
ASSERT_EQUALS ( " ab-(= " , testAst ( " a = ((int)-b) " ) ) ; // Multiple subsequent unary operators (cast and -)
2014-10-16 15:57:05 +02:00
ASSERT_EQUALS ( " xdouble123(i*(= " , testAst ( " x = (int)(double(123)*i); " ) ) ;
2015-10-17 18:25:27 +02:00
// not cast
ASSERT_EQUALS ( " AB|| " , testAst ( " (A)||(B) " ) ) ;
2014-05-24 11:28:43 +02:00
}
2015-10-07 18:33:57 +02:00
void astlambda ( ) {
2015-06-17 19:32:44 +02:00
// a lambda expression '[x](y){}' is compiled as:
// [
// `-(
// `-{
ASSERT_EQUALS ( " x{([( ai= " , testAst ( " x([&a](int i){a=i;}); " ) ) ;
ASSERT_EQUALS ( " {([(return 0return " , testAst ( " return [](){ return 0; }(); " ) ) ;
ASSERT_EQUALS ( " {([(return 0return " , testAst ( " return []() -> int { return 0; }(); " ) ) ;
ASSERT_EQUALS ( " {([(return 0return " , testAst ( " return [something]() -> int { return 0; }(); " ) ) ;
ASSERT_EQUALS ( " {([cd,(return 0return " , testAst ( " return [](int a, int b) -> int { return 0; }(c, d); " ) ) ;
ASSERT_EQUALS ( " x{([= 0return " , testAst ( " x = [](){return 0; }; " ) ) ;
2014-05-25 19:48:31 +02:00
}
2014-11-20 14:20:09 +01:00
void compileLimits ( ) {
2014-04-13 12:47:54 +02:00
const char raw_code [ ] = " #define PTR1 (* (* (* (* (* (* (* (* (* (* \n "
" #define PTR2 PTR1 PTR1 PTR1 PTR1 PTR1 PTR1 PTR1 PTR1 PTR1 PTR1 \n "
" #define PTR3 PTR2 PTR2 PTR2 PTR2 PTR2 PTR2 PTR2 PTR2 PTR2 PTR2 \n "
" #define PTR4 PTR3 PTR3 PTR3 PTR3 PTR3 PTR3 PTR3 PTR3 PTR3 PTR3 \n "
" #define PTR5 PTR4 PTR4 PTR4 PTR4 PTR4 PTR4 PTR4 PTR4 PTR4 PTR4 \n "
" #define PTR6 PTR5 PTR5 PTR5 PTR5 PTR5 PTR5 PTR5 PTR5 PTR5 PTR5 \n "
" \n "
" #define RBR1 ) ) ) ) ) ) ) ) ) ) \n "
" #define RBR2 RBR1 RBR1 RBR1 RBR1 RBR1 RBR1 RBR1 RBR1 RBR1 RBR1 \n "
" #define RBR3 RBR2 RBR2 RBR2 RBR2 RBR2 RBR2 RBR2 RBR2 RBR2 RBR2 \n "
" #define RBR4 RBR3 RBR3 RBR3 RBR3 RBR3 RBR3 RBR3 RBR3 RBR3 RBR3 \n "
" #define RBR5 RBR4 RBR4 RBR4 RBR4 RBR4 RBR4 RBR4 RBR4 RBR4 RBR4 \n "
" #define RBR6 RBR5 RBR5 RBR5 RBR5 RBR5 RBR5 RBR5 RBR5 RBR5 RBR5 \n "
" \n "
" int PTR4 q4_var RBR4 = 0; \n " ;
// Preprocess file..
2015-10-07 18:33:57 +02:00
Preprocessor preprocessor ( settings0 ) ;
2014-04-13 12:47:54 +02:00
std : : list < std : : string > configurations ;
std : : string filedata = " " ;
std : : istringstream fin ( raw_code ) ;
2015-10-07 18:33:57 +02:00
preprocessor . preprocess ( fin , filedata , configurations , emptyString , settings0 . _includePaths ) ;
2015-03-15 12:36:40 +01:00
const std : : string code = preprocessor . getcode ( filedata , emptyString , emptyString ) ;
2014-04-13 12:47:54 +02:00
tokenizeAndStringify ( code . c_str ( ) ) ; // just survive...
}
2014-11-20 14:20:09 +01:00
bool isStartOfExecutableScope ( int offset , const char code [ ] ) {
2015-10-07 18:33:57 +02:00
Tokenizer tokenizer ( & settings0 , this ) ;
2014-04-26 18:31:56 +02:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
return Tokenizer : : startOfExecutableScope ( tokenizer . tokens ( ) - > tokAt ( offset ) ) ! = nullptr ;
}
2014-11-20 14:20:09 +01:00
void startOfExecutableScope ( ) {
2014-04-26 18:31:56 +02:00
ASSERT ( isStartOfExecutableScope ( 3 , " void foo() { } " ) ) ;
ASSERT ( isStartOfExecutableScope ( 3 , " void foo() const { } " ) ) ;
ASSERT ( isStartOfExecutableScope ( 3 , " void foo() volatile { } " ) ) ;
ASSERT ( isStartOfExecutableScope ( 3 , " void foo() noexcept { } " ) ) ;
ASSERT ( isStartOfExecutableScope ( 3 , " void foo() NOEXCEPT { } " ) ) ;
ASSERT ( isStartOfExecutableScope ( 3 , " void foo() CONST NOEXCEPT { } " ) ) ;
ASSERT ( isStartOfExecutableScope ( 3 , " void foo() const noexcept { } " ) ) ;
ASSERT ( isStartOfExecutableScope ( 3 , " void foo() noexcept(true) { } " ) ) ;
ASSERT ( isStartOfExecutableScope ( 3 , " void foo() const noexcept(true) { } " ) ) ;
ASSERT ( isStartOfExecutableScope ( 3 , " void foo() throw() { } " ) ) ;
ASSERT ( isStartOfExecutableScope ( 3 , " void foo() THROW() { } " ) ) ;
ASSERT ( isStartOfExecutableScope ( 3 , " void foo() CONST THROW() { } " ) ) ;
ASSERT ( isStartOfExecutableScope ( 3 , " void foo() const throw() { } " ) ) ;
ASSERT ( isStartOfExecutableScope ( 3 , " void foo() throw(int) { } " ) ) ;
ASSERT ( isStartOfExecutableScope ( 3 , " void foo() const throw(int) { } " ) ) ;
ASSERT ( isStartOfExecutableScope ( 2 , " foo() : a(1) { } " ) ) ;
ASSERT ( isStartOfExecutableScope ( 2 , " foo() : a(1), b(2) { } " ) ) ;
2014-10-15 21:33:07 +02:00
ASSERT ( isStartOfExecutableScope ( 2 , " foo() : a{1} { } " ) ) ;
ASSERT ( isStartOfExecutableScope ( 2 , " foo() : a{1}, b{2} { } " ) ) ;
2014-04-26 18:31:56 +02:00
}
2015-01-17 07:42:49 +01:00
void removeMacroInClassDef ( ) { // #6058
ASSERT_EQUALS ( " class Fred { } ; " , tokenizeAndStringify ( " class DLLEXPORT Fred { } ; " ) ) ;
ASSERT_EQUALS ( " class Fred : Base { } ; " , tokenizeAndStringify ( " class Fred FINAL : Base { } ; " ) ) ;
2015-09-01 07:10:12 +02:00
// Regression for C code:
ASSERT_EQUALS ( " struct Fred { } ; " , tokenizeAndStringify ( " struct DLLEXPORT Fred { } ; " , false , true , Settings : : Unspecified , " test.c " ) ) ;
2015-01-17 07:42:49 +01:00
}
2015-02-22 13:38:06 +01:00
void sizeofAddParentheses ( ) {
ASSERT_EQUALS ( " sizeof ( sizeof ( 1 ) ) ; " , tokenizeAndStringify ( " sizeof sizeof 1; " ) ) ;
ASSERT_EQUALS ( " sizeof ( a . b ) + 3 ; " , tokenizeAndStringify ( " sizeof a.b+3; " ) ) ;
ASSERT_EQUALS ( " sizeof ( a [ 2 ] . b ) + 3 ; " , tokenizeAndStringify ( " sizeof a[2].b+3; " ) ) ;
ASSERT_EQUALS ( " f ( 0 , sizeof ( ptr . bar ) ) ; " , tokenizeAndStringify ( " f(0, sizeof ptr->bar ); " ) ) ;
}
2015-04-29 09:18:54 +02:00
2015-07-19 15:03:02 +02:00
// see #5783
void noreturn ( ) {
const char code [ ] = " void myassert() { \n "
" exit(1); \n "
" } \n "
" void f(char *buf) { \n "
" if(i==0) { \n "
" free(buf); \n "
" myassert(); \n "
" } \n "
" free(buf); \n "
" } \n " ;
// tokenize..
2015-10-07 18:33:57 +02:00
Tokenizer tokenizer ( & settings0 , this ) ;
2015-07-19 15:03:02 +02:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
const Token * func = Token : : findsimplematch ( tokenizer . tokens ( ) , " myassert " ) ;
TODO_ASSERT ( func & & func - > isAttributeNoreturn ( ) ) ;
}
2008-12-18 22:28:57 +01:00
} ;
2009-01-05 16:49:57 +01:00
REGISTER_TEST ( TestTokenizer )