2014-09-24 13:23:44 +02:00
/*
* Cppcheck - A tool for static C / C + + code analysis
2023-01-28 10:16:34 +01:00
* Copyright ( C ) 2007 - 2023 Cppcheck team .
2014-09-24 13:23:44 +02: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
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
2017-05-27 04:33:47 +02:00
2022-01-27 19:03:20 +01:00
# include "errortypes.h"
2017-05-27 04:33:47 +02:00
# include "platform.h"
# include "settings.h"
2023-01-27 08:18:32 +01:00
# include "fixture.h"
2014-09-24 13:23:44 +02:00
# include "token.h"
2017-05-27 04:33:47 +02:00
# include "tokenize.h"
# include "tokenlist.h"
2022-01-27 19:03:20 +01:00
# include <map>
2021-08-29 19:14:53 +02:00
# include <sstream>
2022-01-27 19:03:20 +01:00
# include <string>
# include <utility>
# include <vector>
2021-08-29 19:14:53 +02:00
# include <simplecpp.h>
2014-09-24 13:23:44 +02:00
class TestSimplifyTypedef : public TestFixture {
public :
2021-08-07 20:51:18 +02:00
TestSimplifyTypedef ( ) : TestFixture ( " TestSimplifyTypedef " ) { }
2014-09-24 13:23:44 +02:00
private :
2023-08-09 12:43:55 +02:00
const Settings settings0 = settingsBuilder ( ) . severity ( Severity : : style ) . build ( ) ;
const Settings settings1 = settingsBuilder ( ) . severity ( Severity : : portability ) . build ( ) ;
2014-09-24 13:23:44 +02:00
2022-02-10 23:02:24 +01:00
void run ( ) override {
2023-03-26 17:16:33 +02:00
TEST_CASE ( c1 ) ;
TEST_CASE ( c2 ) ;
TEST_CASE ( canreplace1 ) ;
TEST_CASE ( canreplace2 ) ;
2023-04-18 18:38:16 +02:00
TEST_CASE ( canreplace3 ) ;
2023-03-26 17:16:33 +02:00
TEST_CASE ( cconst ) ;
TEST_CASE ( cstruct1 ) ;
TEST_CASE ( cstruct2 ) ;
TEST_CASE ( cstruct3 ) ;
TEST_CASE ( cstruct3 ) ;
TEST_CASE ( cstruct4 ) ;
2023-05-12 20:39:08 +02:00
TEST_CASE ( cfunction1 ) ;
TEST_CASE ( cfunction2 ) ;
TEST_CASE ( cfunction3 ) ;
2023-03-26 17:16:33 +02:00
TEST_CASE ( cfp1 ) ;
TEST_CASE ( cfp2 ) ;
TEST_CASE ( cfp4 ) ;
TEST_CASE ( cfp5 ) ;
TEST_CASE ( cfp6 ) ;
2023-05-23 06:26:22 +02:00
TEST_CASE ( cfp7 ) ;
2023-03-26 17:16:33 +02:00
TEST_CASE ( carray1 ) ;
TEST_CASE ( carray2 ) ;
2023-05-26 06:12:40 +02:00
TEST_CASE ( carray3 ) ;
2023-03-26 17:16:33 +02:00
TEST_CASE ( cdonotreplace1 ) ;
TEST_CASE ( cppfp1 ) ;
2023-05-14 15:14:52 +02:00
TEST_CASE ( Generic1 ) ;
2023-03-26 17:16:33 +02:00
2020-04-21 17:27:51 +02:00
TEST_CASE ( simplifyTypedef1 ) ;
TEST_CASE ( simplifyTypedef2 ) ;
TEST_CASE ( simplifyTypedef3 ) ;
TEST_CASE ( simplifyTypedef4 ) ;
TEST_CASE ( simplifyTypedef5 ) ;
TEST_CASE ( simplifyTypedef6 ) ;
2014-09-24 13:23:44 +02:00
TEST_CASE ( simplifyTypedef7 ) ;
TEST_CASE ( simplifyTypedef8 ) ;
TEST_CASE ( simplifyTypedef9 ) ;
TEST_CASE ( simplifyTypedef10 ) ;
TEST_CASE ( simplifyTypedef11 ) ;
TEST_CASE ( simplifyTypedef12 ) ;
TEST_CASE ( simplifyTypedef13 ) ;
TEST_CASE ( simplifyTypedef14 ) ;
TEST_CASE ( simplifyTypedef15 ) ;
TEST_CASE ( simplifyTypedef16 ) ;
TEST_CASE ( simplifyTypedef17 ) ;
TEST_CASE ( simplifyTypedef18 ) ; // typedef vector<int[4]> a;
TEST_CASE ( simplifyTypedef19 ) ;
TEST_CASE ( simplifyTypedef20 ) ;
TEST_CASE ( simplifyTypedef21 ) ;
TEST_CASE ( simplifyTypedef22 ) ;
TEST_CASE ( simplifyTypedef23 ) ;
TEST_CASE ( simplifyTypedef24 ) ;
TEST_CASE ( simplifyTypedef25 ) ;
TEST_CASE ( simplifyTypedef26 ) ;
TEST_CASE ( simplifyTypedef27 ) ;
TEST_CASE ( simplifyTypedef28 ) ;
TEST_CASE ( simplifyTypedef29 ) ;
TEST_CASE ( simplifyTypedef30 ) ;
TEST_CASE ( simplifyTypedef31 ) ;
TEST_CASE ( simplifyTypedef32 ) ;
TEST_CASE ( simplifyTypedef33 ) ;
TEST_CASE ( simplifyTypedef34 ) ; // ticket #1411
TEST_CASE ( simplifyTypedef35 ) ;
TEST_CASE ( simplifyTypedef36 ) ; // ticket #1434
TEST_CASE ( simplifyTypedef37 ) ; // ticket #1449
TEST_CASE ( simplifyTypedef38 ) ;
TEST_CASE ( simplifyTypedef43 ) ; // ticket #1588
TEST_CASE ( simplifyTypedef44 ) ;
TEST_CASE ( simplifyTypedef45 ) ; // ticket #1613
TEST_CASE ( simplifyTypedef46 ) ;
TEST_CASE ( simplifyTypedef47 ) ;
TEST_CASE ( simplifyTypedef48 ) ; // ticket #1673
TEST_CASE ( simplifyTypedef49 ) ; // ticket #1691
TEST_CASE ( simplifyTypedef50 ) ;
TEST_CASE ( simplifyTypedef51 ) ;
TEST_CASE ( simplifyTypedef52 ) ; // ticket #1782
TEST_CASE ( simplifyTypedef54 ) ; // ticket #1814
TEST_CASE ( simplifyTypedef55 ) ;
TEST_CASE ( simplifyTypedef56 ) ; // ticket #1829
TEST_CASE ( simplifyTypedef57 ) ; // ticket #1846
TEST_CASE ( simplifyTypedef58 ) ; // ticket #1963
TEST_CASE ( simplifyTypedef59 ) ; // ticket #2011
TEST_CASE ( simplifyTypedef60 ) ; // ticket #2035
TEST_CASE ( simplifyTypedef61 ) ; // ticket #2074 and 2075
TEST_CASE ( simplifyTypedef62 ) ; // ticket #2082
TEST_CASE ( simplifyTypedef63 ) ; // ticket #2175 'typedef float x[3];'
TEST_CASE ( simplifyTypedef64 ) ;
TEST_CASE ( simplifyTypedef65 ) ; // ticket #2314
TEST_CASE ( simplifyTypedef66 ) ; // ticket #2341
TEST_CASE ( simplifyTypedef67 ) ; // ticket #2354
TEST_CASE ( simplifyTypedef68 ) ; // ticket #2355
TEST_CASE ( simplifyTypedef69 ) ; // ticket #2348
TEST_CASE ( simplifyTypedef70 ) ; // ticket #2348
TEST_CASE ( simplifyTypedef71 ) ; // ticket #2348
TEST_CASE ( simplifyTypedef72 ) ; // ticket #2375
TEST_CASE ( simplifyTypedef73 ) ; // ticket #2412
TEST_CASE ( simplifyTypedef74 ) ; // ticket #2414
TEST_CASE ( simplifyTypedef75 ) ; // ticket #2426
TEST_CASE ( simplifyTypedef76 ) ; // ticket #2453
TEST_CASE ( simplifyTypedef77 ) ; // ticket #2554
TEST_CASE ( simplifyTypedef78 ) ; // ticket #2568
TEST_CASE ( simplifyTypedef79 ) ; // ticket #2348
TEST_CASE ( simplifyTypedef80 ) ; // ticket #2587
TEST_CASE ( simplifyTypedef81 ) ; // ticket #2603
TEST_CASE ( simplifyTypedef82 ) ; // ticket #2403
TEST_CASE ( simplifyTypedef83 ) ; // ticket #2620
TEST_CASE ( simplifyTypedef84 ) ; // ticket #2630
TEST_CASE ( simplifyTypedef85 ) ; // ticket #2651
TEST_CASE ( simplifyTypedef86 ) ; // ticket #2581
TEST_CASE ( simplifyTypedef87 ) ; // ticket #2651
TEST_CASE ( simplifyTypedef88 ) ; // ticket #2675
TEST_CASE ( simplifyTypedef89 ) ; // ticket #2717
TEST_CASE ( simplifyTypedef90 ) ; // ticket #2718
TEST_CASE ( simplifyTypedef91 ) ; // ticket #2716
TEST_CASE ( simplifyTypedef92 ) ; // ticket #2736
TEST_CASE ( simplifyTypedef93 ) ; // ticket #2738
TEST_CASE ( simplifyTypedef94 ) ; // ticket #1982
TEST_CASE ( simplifyTypedef95 ) ; // ticket #2844
TEST_CASE ( simplifyTypedef96 ) ; // ticket #2886
TEST_CASE ( simplifyTypedef97 ) ; // ticket #2983 (segmentation fault)
TEST_CASE ( simplifyTypedef99 ) ; // ticket #2999
TEST_CASE ( simplifyTypedef100 ) ; // ticket #3000
TEST_CASE ( simplifyTypedef101 ) ; // ticket #3003 (segmentation fault)
TEST_CASE ( simplifyTypedef102 ) ; // ticket #3004
TEST_CASE ( simplifyTypedef103 ) ; // ticket #3007
TEST_CASE ( simplifyTypedef104 ) ; // ticket #3070
TEST_CASE ( simplifyTypedef105 ) ; // ticket #3616
TEST_CASE ( simplifyTypedef106 ) ; // ticket #3619
TEST_CASE ( simplifyTypedef107 ) ; // ticket #3963 - bad code => segmentation fault
TEST_CASE ( simplifyTypedef108 ) ; // ticket #4777
TEST_CASE ( simplifyTypedef109 ) ; // ticket #1823 - rvalue reference
2014-11-27 06:29:33 +01:00
TEST_CASE ( simplifyTypedef110 ) ; // ticket #6268
2015-07-20 07:55:22 +02:00
TEST_CASE ( simplifyTypedef111 ) ; // ticket #6345
2015-07-26 15:42:40 +02:00
TEST_CASE ( simplifyTypedef112 ) ; // ticket #6048
2015-10-15 16:56:22 +02:00
TEST_CASE ( simplifyTypedef113 ) ; // ticket #7030
2015-10-18 04:55:04 +02:00
TEST_CASE ( simplifyTypedef114 ) ; // ticket #7058 - skip "struct", AB::..
2015-10-24 10:08:19 +02:00
TEST_CASE ( simplifyTypedef115 ) ; // ticket #6998
2015-10-26 10:06:52 +01:00
TEST_CASE ( simplifyTypedef116 ) ; // ticket #5624
TEST_CASE ( simplifyTypedef117 ) ; // ticket #6507
2016-04-22 15:13:05 +02:00
TEST_CASE ( simplifyTypedef118 ) ; // ticket #5749
2016-10-01 20:46:33 +02:00
TEST_CASE ( simplifyTypedef119 ) ; // ticket #7541
2018-01-22 08:06:56 +01:00
TEST_CASE ( simplifyTypedef120 ) ; // ticket #8357
2018-01-24 09:51:22 +01:00
TEST_CASE ( simplifyTypedef121 ) ; // ticket #5766
2018-01-24 15:04:33 +01:00
TEST_CASE ( simplifyTypedef122 ) ; // segmentation fault
2018-05-22 17:31:58 +02:00
TEST_CASE ( simplifyTypedef123 ) ; // ticket #7406
2018-08-16 14:30:28 +02:00
TEST_CASE ( simplifyTypedef124 ) ; // ticket #7792
2018-10-21 21:15:34 +02:00
TEST_CASE ( simplifyTypedef125 ) ; // #8749 - typedef char A[10]; p = new A[1];
2019-01-05 19:42:04 +01:00
TEST_CASE ( simplifyTypedef126 ) ; // ticket #5953
2019-02-10 09:45:33 +01:00
TEST_CASE ( simplifyTypedef127 ) ; // ticket #8878
2019-03-23 10:45:38 +01:00
TEST_CASE ( simplifyTypedef128 ) ; // ticket #9053
Better handle const/noexcept methods (#2211)
* Better handle const/noexcept methods/conversion operator
const or noexcept in a method / (conversion) operator definition were
badly parsed, ending in a bad ast.
This patch tries to make it better, at least making the ast less bad,
so as to avoid errors in later checks.
* Fix parsing of some operator
It is still very broken, but at least, it does not fail.
Here is the previous error:
```
TestSimplifyTypedef::simplifyTypedef129
terminate called after throwing an instance of 'InternalError'
Program received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
51 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1 0x00007ffff612a801 in __GI_abort () at abort.c:79
#2 0x00007ffff6b1d957 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00007ffff6b23ab6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007ffff6b23af1 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007ffff6b23d24 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x0000555556366bf8 in Tokenizer::cppcheckError (this=0x7fffffffc2d0, tok=0x607000006760) at ../lib/tokenize.cpp:8721
#7 0x000055555636a4bb in Tokenizer::validate (this=0x7fffffffc2d0) at ../lib/tokenize.cpp:9154
#8 0x000055555633e3aa in Tokenizer::simplifyTokenList1 (this=0x7fffffffc2d0, FileName=0x603000002d50 "test.cpp") at ../lib/tokenize.cpp:4477
#9 0x00005555563223ca in Tokenizer::simplifyTokens1 (this=0x7fffffffc2d0, configuration="") at ../lib/tokenize.cpp:2286
#10 0x00005555563235c8 in Tokenizer::tokenize (this=0x7fffffffc2d0, code=..., FileName=0x555556fda9a0 "test.cpp", configuration="") at ../lib/tokenize.cpp:2345
#11 0x00005555569410ea in TestSimplifyTypedef::tok[abi:cxx11](char const*, bool, cppcheck::Platform::PlatformType, bool) (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>,
code=0x7fffffffcb70 "class c {\n typedef char foo[4];\n foo _a;\n constexpr operator foo &() const noexcept { return _a; }\n};", simplify=false, type=cppcheck::Platform::Native, debugwarnings=true) at ../test/testsimplifytypedef.cpp:192
#12 0x000055555697239e in TestSimplifyTypedef::simplifyTypedef129 (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:2599
#13 0x000055555694092c in TestSimplifyTypedef::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:167
#14 0x00005555569cab84 in TestFixture::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>, str="simplifyTypedef129") at ../test/testsuite.cpp:306
#15 0x00005555569cb445 in TestFixture::runTests (args=...) at ../test/testsuite.cpp:329
#16 0x000055555687bdfb in main (argc=2, argv=0x7fffffffd988) at ../test/testrunner.cpp:44
```
* Replace some ASSERT_EQUALS with TODO_ASSERT_EQUALS when the actual result is still wrong
* Remove invalid code from simplifyTypedef129
* Properly skip parentheses
2019-10-12 11:46:00 +02:00
TEST_CASE ( simplifyTypedef129 ) ;
2019-11-01 09:11:29 +01:00
TEST_CASE ( simplifyTypedef130 ) ; // ticket #9446
2020-04-08 22:40:45 +02:00
TEST_CASE ( simplifyTypedef131 ) ; // ticket #9446
2020-05-29 11:16:49 +02:00
TEST_CASE ( simplifyTypedef132 ) ; // ticket #9739 - using
2020-09-14 21:59:41 +02:00
TEST_CASE ( simplifyTypedef133 ) ; // ticket #9812 - using
2020-11-04 13:32:31 +01:00
TEST_CASE ( simplifyTypedef134 ) ;
2021-01-05 16:53:56 +01:00
TEST_CASE ( simplifyTypedef135 ) ; // ticket #10068
2021-01-17 16:10:53 +01:00
TEST_CASE ( simplifyTypedef136 ) ;
2021-01-18 10:08:55 +01:00
TEST_CASE ( simplifyTypedef137 ) ;
2021-11-06 19:07:45 +01:00
TEST_CASE ( simplifyTypedef138 ) ;
2022-01-16 12:46:20 +01:00
TEST_CASE ( simplifyTypedef139 ) ;
2022-03-02 07:46:15 +01:00
TEST_CASE ( simplifyTypedef140 ) ; // #10798
2022-03-15 12:54:19 +01:00
TEST_CASE ( simplifyTypedef141 ) ; // #10144
2022-08-27 18:02:19 +02:00
TEST_CASE ( simplifyTypedef142 ) ; // T() when T is a pointer type
2023-01-27 08:19:32 +01:00
TEST_CASE ( simplifyTypedef143 ) ; // #11506
2023-01-26 22:03:12 +01:00
TEST_CASE ( simplifyTypedef144 ) ; // #9353
2023-04-07 11:09:13 +02:00
TEST_CASE ( simplifyTypedef145 ) ; // #9353
2014-09-24 13:23:44 +02:00
TEST_CASE ( simplifyTypedefFunction1 ) ;
TEST_CASE ( simplifyTypedefFunction2 ) ; // ticket #1685
TEST_CASE ( simplifyTypedefFunction3 ) ;
TEST_CASE ( simplifyTypedefFunction4 ) ;
TEST_CASE ( simplifyTypedefFunction5 ) ;
TEST_CASE ( simplifyTypedefFunction6 ) ;
TEST_CASE ( simplifyTypedefFunction7 ) ;
TEST_CASE ( simplifyTypedefFunction8 ) ;
2014-11-28 17:44:36 +01:00
TEST_CASE ( simplifyTypedefFunction9 ) ;
2014-12-04 05:43:58 +01:00
TEST_CASE ( simplifyTypedefFunction10 ) ; // #5191
2014-09-24 13:23:44 +02:00
TEST_CASE ( simplifyTypedefShadow ) ; // #4445 - shadow variable
2021-08-29 19:14:53 +02:00
TEST_CASE ( simplifyTypedefMacro ) ;
2014-09-24 13:23:44 +02:00
}
2021-11-29 07:34:39 +01:00
# define tok(...) tok_(__FILE__, __LINE__, __VA_ARGS__)
2023-03-03 18:36:27 +01:00
std : : string tok_ ( const char * file , int line , const char code [ ] , bool simplify = true , cppcheck : : Platform : : Type type = cppcheck : : Platform : : Type : : Native , bool debugwarnings = true ) {
2014-09-24 13:23:44 +02:00
errout . str ( " " ) ;
2023-05-02 11:48:24 +02:00
// show warnings about unhandled typedef
2023-05-02 15:54:19 +02:00
const Settings settings = settingsBuilder ( settings0 ) . certainty ( Certainty : : inconclusive ) . debugwarnings ( debugwarnings ) . platform ( type ) . build ( ) ;
2023-05-02 11:48:24 +02:00
Tokenizer tokenizer ( & settings , this ) ;
2014-09-24 13:23:44 +02:00
std : : istringstream istr ( code ) ;
2021-11-29 07:34:39 +01:00
ASSERT_LOC ( tokenizer . tokenize ( istr , " test.cpp " ) , file , line ) ;
2014-09-24 13:23:44 +02:00
2019-06-30 21:39:22 +02:00
return tokenizer . tokens ( ) - > stringifyList ( nullptr , ! simplify ) ;
2014-09-24 13:23:44 +02:00
}
2014-11-20 14:20:09 +01:00
std : : string simplifyTypedef ( const char code [ ] ) {
2014-09-24 13:23:44 +02:00
errout . str ( " " ) ;
2015-10-07 18:33:57 +02:00
Tokenizer tokenizer ( & settings1 , this ) ;
2014-09-24 13:23:44 +02:00
std : : istringstream istr ( code ) ;
tokenizer . list . createTokens ( istr ) ;
tokenizer . createLinks ( ) ;
tokenizer . simplifyTypedef ( ) ;
2019-06-30 21:39:22 +02:00
return tokenizer . tokens ( ) - > stringifyList ( nullptr , false ) ;
2014-09-24 13:23:44 +02:00
}
2021-08-29 19:14:53 +02:00
std : : string simplifyTypedefP ( const char code [ ] ) {
// Clear the error buffer..
errout . str ( " " ) ;
// Raw tokens..
std : : vector < std : : string > files ( 1 , " test.cpp " ) ;
std : : istringstream istr ( code ) ;
const simplecpp : : TokenList tokens1 ( istr , files , files [ 0 ] ) ;
// Preprocess..
simplecpp : : TokenList tokens2 ( files ) ;
std : : map < std : : string , simplecpp : : TokenList * > filedata ;
simplecpp : : preprocess ( tokens2 , tokens1 , files , filedata , simplecpp : : DUI ( ) ) ;
// Tokenize..
Tokenizer tokenizer ( & settings0 , this ) ;
tokenizer . createTokens ( std : : move ( tokens2 ) ) ;
tokenizer . createLinks ( ) ;
tokenizer . simplifyTypedef ( ) ;
return tokenizer . tokens ( ) - > stringifyList ( nullptr , false ) ;
}
2021-11-29 07:34:39 +01:00
# define checkSimplifyTypedef(code) checkSimplifyTypedef_(code, __FILE__, __LINE__)
void checkSimplifyTypedef_ ( const char code [ ] , const char * file , int line ) {
2015-10-07 18:33:57 +02:00
errout . str ( " " ) ;
// Tokenize..
2023-05-02 11:48:24 +02:00
// show warnings about unhandled typedef
2023-08-09 12:43:55 +02:00
const Settings settings = settingsBuilder ( settings0 ) . certainty ( Certainty : : inconclusive ) . debugwarnings ( ) . build ( ) ;
2023-05-02 11:48:24 +02:00
Tokenizer tokenizer ( & settings , this ) ;
2015-10-07 18:33:57 +02:00
std : : istringstream istr ( code ) ;
2021-11-29 07:34:39 +01:00
ASSERT_LOC ( tokenizer . tokenize ( istr , " test.cpp " ) , file , line ) ;
2015-10-07 18:33:57 +02:00
}
2014-09-24 13:23:44 +02:00
2023-03-26 17:16:33 +02:00
std : : string simplifyTypedefC ( const char code [ ] ) {
errout . str ( " " ) ;
Tokenizer tokenizer ( & settings1 , this ) ;
std : : istringstream istr ( code ) ;
tokenizer . list . createTokens ( istr , " file.c " ) ;
tokenizer . createLinks ( ) ;
tokenizer . simplifyTypedef ( ) ;
try {
tokenizer . validate ( ) ;
} catch ( const InternalError & ) {
return " " ;
}
return tokenizer . tokens ( ) - > stringifyList ( nullptr , false ) ;
}
void c1 ( ) {
const char code [ ] = " typedef int t; \n "
" t x; " ;
ASSERT_EQUALS ( " int x ; " , simplifyTypedefC ( code ) ) ;
}
void c2 ( ) {
const char code [ ] = " void f1() { typedef int t; t x; } \n "
" void f2() { typedef float t; t x; } \n " ;
ASSERT_EQUALS ( " void f1 ( ) { int x ; } void f2 ( ) { float x ; } " , simplifyTypedefC ( code ) ) ;
}
void canreplace1 ( ) {
const char code [ ] = " typedef unsigned char u8; \n "
" void f(uint8_t u8) { x = u8 & y; } \n " ;
ASSERT_EQUALS ( " void f ( uint8_t u8 ) { x = u8 & y ; } " , simplifyTypedefC ( code ) ) ;
}
void canreplace2 ( ) {
const char code1 [ ] = " typedef char* entry; \n "
" void f(Y* entry) { for (entry=x->y; entry; entry = entry->next) {} } \n " ;
ASSERT_EQUALS ( " void f ( Y * entry ) { for ( entry = x -> y ; entry ; entry = entry -> next ) { } } " , simplifyTypedefC ( code1 ) ) ;
const char code2 [ ] = " typedef char* entry; \n "
" void f() { dostuff(entry * 2); } \n " ;
ASSERT_EQUALS ( " void f ( ) { dostuff ( entry * 2 ) ; } " , simplifyTypedefC ( code2 ) ) ;
const char code3 [ ] = " typedef char* entry; \n "
" void f() { dostuff(entry * y < z); } \n " ;
ASSERT_EQUALS ( " void f ( ) { dostuff ( entry * y < z ) ; } " , simplifyTypedefC ( code3 ) ) ;
}
2023-04-18 18:38:16 +02:00
void canreplace3 ( ) {
const char code1 [ ] = " typedef char* c_str; \n " // #11640
" struct S { \n "
" const char* g() const { \n "
" return s.c_str(); \n "
" } \n "
" std::string s; \n "
" }; \n " ;
ASSERT_EQUALS ( " struct S { const char * g ( ) const { return s . c_str ( ) ; } std :: string s ; } ; " , simplifyTypedefC ( code1 ) ) ;
}
2023-03-26 17:16:33 +02:00
void cconst ( ) {
const char code1 [ ] = " typedef void* HWND; \n "
" const HWND x; " ;
ASSERT_EQUALS ( " void * const x ; " , simplifyTypedef ( code1 ) ) ;
const char code2 [ ] = " typedef void (*fp)(); \n "
" const fp x; " ;
ASSERT_EQUALS ( " void ( * const x ) ( ) ; " , simplifyTypedef ( code2 ) ) ;
}
void cstruct1 ( ) {
const char code [ ] = " typedef struct { int a; int b; } t; \n "
" t x; " ;
ASSERT_EQUALS ( " struct t { int a ; int b ; } ; struct t x ; " , simplifyTypedef ( code ) ) ;
ASSERT_EQUALS ( " struct t { int a ; int b ; } ; struct t x ; " , simplifyTypedefC ( code ) ) ;
}
void cstruct2 ( ) {
const char code [ ] = " typedef enum { A, B } t; \n "
" t x; " ;
ASSERT_EQUALS ( " enum t { A , B } ; t x ; " , simplifyTypedef ( code ) ) ;
ASSERT_EQUALS ( " enum t { A , B } ; t x ; " , simplifyTypedefC ( code ) ) ;
}
void cstruct3 ( ) {
const char code [ ] = " typedef struct s { int a; int b; } t; \n "
" t x; " ;
ASSERT_EQUALS ( " struct s { int a ; int b ; } ; struct s x ; " , simplifyTypedefC ( code ) ) ;
}
void cstruct4 ( ) {
const char code [ ] = " typedef struct s { int a; int b; } t; \n "
" struct t x{}; " ;
ASSERT_EQUALS ( " struct s { int a ; int b ; } ; struct s x { } ; " , simplifyTypedefC ( code ) ) ;
}
2023-05-12 20:39:08 +02:00
void cfunction1 ( ) {
const char code [ ] = " typedef int callback(int); \n "
" callback* cb; " ;
ASSERT_EQUALS ( " int ( * cb ) ( int ) ; " , simplifyTypedefC ( code ) ) ;
}
void cfunction2 ( ) {
const char code [ ] = " typedef int callback(int); \n "
" typedef callback* callbackPtr; \n "
" callbackPtr cb; " ;
ASSERT_EQUALS ( " int ( * cb ) ( int ) ; " , simplifyTypedefC ( code ) ) ;
}
void cfunction3 ( ) {
const char code [ ] = " typedef int f(int); \n "
" typedef const f cf; \n " ;
simplifyTypedefC ( code ) ;
ASSERT_EQUALS ( " [file.c:2]: (portability) It is unspecified behavior to const qualify a function type. \n " , errout . str ( ) ) ;
}
2023-03-26 17:16:33 +02:00
void cfp1 ( ) {
const char code [ ] = " typedef void (*fp)(void * p); \n "
" fp x; " ;
ASSERT_EQUALS ( " void ( * x ) ( void * p ) ; " , simplifyTypedefC ( code ) ) ;
}
void cfp2 ( ) {
const char code [ ] = " typedef void (*const fp)(void * p); \n "
" fp x; " ;
ASSERT_EQUALS ( " void ( * const x ) ( void * p ) ; " , simplifyTypedefC ( code ) ) ;
}
void cfp4 ( ) {
const char code [ ] = " typedef struct S Stype ; \n "
" typedef void ( * F ) ( Stype * ) ; \n "
" F func; " ;
ASSERT_EQUALS ( " void ( * func ) ( struct S * ) ; " , simplifyTypedefC ( code ) ) ;
}
void cfp5 ( ) {
const char code [ ] = " typedef void (*fp)(void); \n "
" typedef fp t; \n "
" void foo(t p); " ;
ASSERT_EQUALS ( " void foo ( void ( * p ) ( void ) ) ; " , simplifyTypedef ( code ) ) ;
}
void cfp6 ( ) {
const char code [ ] = " typedef void (*fp)(void); \n "
" fp a[10]; " ;
ASSERT_EQUALS ( " void ( * a [ 10 ] ) ( void ) ; " , simplifyTypedef ( code ) ) ;
}
2023-05-23 06:26:22 +02:00
void cfp7 ( ) {
const char code [ ] = " typedef uint32_t ((*fp)(uint32_t n)); \n " // #11725
" uint32_t g(); \n "
" fp f; \n " ;
ASSERT_EQUALS ( " uint32_t g ( ) ; uint32_t ( * f ) ( uint32_t n ) ; " , simplifyTypedef ( code ) ) ;
}
2023-03-26 17:16:33 +02:00
void carray1 ( ) {
const char code [ ] = " typedef int t[20]; \n "
" t x; " ;
ASSERT_EQUALS ( " int x [ 20 ] ; " , simplifyTypedefC ( code ) ) ;
}
void carray2 ( ) {
const char code [ ] = " typedef double t[4]; \n "
" t x[10]; " ;
ASSERT_EQUALS ( " double x [ 10 ] [ 4 ] ; " , simplifyTypedef ( code ) ) ;
}
2023-05-26 06:12:40 +02:00
void carray3 ( ) {
const char * code { } ;
code = " typedef int a[256]; \n " // #11689
" typedef a b[256]; \n "
" b* p; \n " ;
ASSERT_EQUALS ( " int ( * p ) [ 256 ] [ 256 ] ; " , simplifyTypedef ( code ) ) ;
code = " typedef int a[1]; \n "
" typedef a b[2]; \n "
" typedef b c[3]; \n "
" c* p; \n " ;
ASSERT_EQUALS ( " int ( * p ) [ 3 ] [ 2 ] [ 1 ] ; " , simplifyTypedef ( code ) ) ;
}
2023-03-26 17:16:33 +02:00
void cdonotreplace1 ( ) {
const char code [ ] = " typedef int t; \n "
" int* t; " ;
ASSERT_EQUALS ( " int * t ; " , simplifyTypedefC ( code ) ) ;
}
void cppfp1 ( ) {
const char code [ ] = " typedef void (*fp)(void); \n "
" typedef fp t; \n "
" void foo(t p); " ;
ASSERT_EQUALS ( " void foo ( void ( * p ) ( void ) ) ; " , tok ( code ) ) ;
}
2014-09-24 13:23:44 +02:00
2023-05-14 15:14:52 +02:00
void Generic1 ( ) {
const char code [ ] = " typedef void func(void); \n "
" _Generic((x), func: 1, default: 2); " ;
ASSERT_EQUALS ( " _Generic ( x , void ( ) : 1 , default : 2 ) ; " , tok ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef1 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " class A \n "
" { \n "
" public: \n "
" typedef wchar_t duplicate; \n "
" void foo() {} \n "
" }; \n "
" typedef A duplicate; \n "
" int main() \n "
" { \n "
" duplicate a; \n "
" a.foo(); \n "
" A::duplicate c = 0; \n "
2015-03-14 20:42:44 +01:00
" } " ;
2014-09-24 13:23:44 +02:00
2015-03-14 20:42:44 +01:00
const char expected [ ] =
2014-09-24 13:23:44 +02:00
" class A "
" { "
" public: "
" "
" void foo ( ) { } "
" } ; "
" int main ( ) "
" { "
" A a ; "
" a . foo ( ) ; "
" wchar_t c ; c = 0 ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef2 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " class A; \n "
" typedef A duplicate; \n "
" class A \n "
" { \n "
" public: \n "
" typedef wchar_t duplicate; \n "
" duplicate foo() { wchar_t b; return b; } \n "
2015-03-14 20:42:44 +01:00
" }; " ;
2014-09-24 13:23:44 +02:00
2015-03-14 20:42:44 +01:00
const char expected [ ] =
2014-09-24 13:23:44 +02:00
" class A ; "
" class A "
" { "
" public: "
" "
" wchar_t foo ( ) { wchar_t b ; return b ; } "
" } ; " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef3 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " class A {}; \n "
" typedef A duplicate; \n "
" wchar_t foo() \n "
" { \n "
" typedef wchar_t duplicate; \n "
" duplicate b; \n "
" return b; \n "
" } \n "
" int main() \n "
" { \n "
" duplicate b; \n "
2015-03-14 20:42:44 +01:00
" } " ;
2014-09-24 13:23:44 +02:00
2015-03-14 20:42:44 +01:00
const char expected [ ] =
2014-09-24 13:23:44 +02:00
" class A { } ; "
" wchar_t foo ( ) "
" { "
" "
" wchar_t b ; "
" return b ; "
" } "
" int main ( ) "
" { "
" A b ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef4 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef int s32; \n "
" typedef unsigned int u32; \n "
" void f() \n "
" { \n "
" s32 ivar = -2; \n "
" u32 uvar = 2; \n "
" return uvar / ivar; \n "
2015-03-14 20:42:44 +01:00
" } " ;
2014-09-24 13:23:44 +02:00
2015-03-14 20:42:44 +01:00
const char expected [ ] =
2014-09-24 13:23:44 +02:00
" void f ( ) "
" { "
" int ivar ; ivar = -2 ; "
" unsigned int uvar ; uvar = 2 ; "
" return uvar / ivar ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef5 ( ) {
2014-09-24 13:23:44 +02:00
// ticket #780
const char code [ ] =
" typedef struct yy_buffer_state *YY_BUFFER_STATE; \n "
" void f() \n "
" { \n "
" YY_BUFFER_STATE state; \n "
2015-03-14 20:42:44 +01:00
" } " ;
2014-09-24 13:23:44 +02:00
const char expected [ ] =
" void f ( ) "
" { "
" struct yy_buffer_state * state ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef6 ( ) {
2014-09-24 13:23:44 +02:00
// ticket #983
const char code [ ] =
" namespace VL { \n "
" typedef float float_t ; \n "
" inline VL::float_t fast_atan2(VL::float_t y, VL::float_t x){} \n "
2015-03-14 20:42:44 +01:00
" } " ;
2014-09-24 13:23:44 +02:00
const char expected [ ] =
" namespace VL { "
" "
" float fast_atan2 ( float y , float x ) { } "
" } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
2022-11-02 15:15:33 +01:00
// ticket #11373
const char code1 [ ] =
" typedef int foo; \n "
" inline foo f(); \n " ;
const char expected1 [ ] = " int f ( ) ; " ;
ASSERT_EQUALS ( expected1 , tok ( code1 , false ) ) ;
2014-09-24 13:23:44 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef7 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef int abc ; "
" Fred :: abc f ; " ;
const char expected [ ] = " Fred :: abc f ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef8 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef int INT; \n "
" typedef unsigned int UINT; \n "
" typedef int * PINT; \n "
" typedef unsigned int * PUINT; \n "
" typedef int & RINT; \n "
" typedef unsigned int & RUINT; \n "
" typedef const int & RCINT; \n "
" typedef const unsigned int & RCUINT; \n "
" INT ti; \n "
" UINT tui; \n "
" PINT tpi; \n "
" PUINT tpui; \n "
" RINT tri; \n "
" RUINT trui; \n "
" RCINT trci; \n "
" RCUINT trcui; " ;
const char expected [ ] =
" int ti ; "
" unsigned int tui ; "
" int * tpi ; "
" unsigned int * tpui ; "
" int & tri ; "
" unsigned int & trui ; "
" const int & trci ; "
" const unsigned int & trcui ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef9 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef struct s S, * PS; \n "
" typedef struct t { int a; } T, *TP; \n "
" typedef struct { int a; } U; \n "
" typedef struct { int a; } * V; \n "
" S s; \n "
" PS ps; \n "
" T t; \n "
" TP tp; \n "
" U u; \n "
" V v; " ;
const char expected [ ] =
" struct t { int a ; } ; "
" struct U { int a ; } ; "
" struct Unnamed0 { int a ; } ; "
" struct s s ; "
" struct s * ps ; "
" struct t t ; "
" struct t * tp ; "
" struct U u ; "
" struct Unnamed0 * v ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef10 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef union s S, * PS; \n "
" typedef union t { int a; float b ; } T, *TP; \n "
" typedef union { int a; float b; } U; \n "
" typedef union { int a; float b; } * V; \n "
" S s; \n "
" PS ps; \n "
" T t; \n "
" TP tp; \n "
" U u; \n "
" V v; " ;
const char expected [ ] =
" union t { int a ; float b ; } ; "
" union U { int a ; float b ; } ; "
2018-03-14 11:11:35 +01:00
" union Unnamed0 { int a ; float b ; } ; "
2014-09-24 13:23:44 +02:00
" union s s ; "
" union s * ps ; "
" union t t ; "
" union t * tp ; "
" union U u ; "
2018-03-14 11:11:35 +01:00
" union Unnamed0 * v ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef11 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef enum { a = 0 , b = 1 , c = 2 } abc; \n "
" typedef enum xyz { x = 0 , y = 1 , z = 2 } XYZ; \n "
" abc e1; \n "
" XYZ e2; " ;
2016-04-22 06:02:54 +02:00
const char expected [ ] = " enum abc { a = 0 , b = 1 , c = 2 } ; "
" enum xyz { x = 0 , y = 1 , z = 2 } ; "
" abc e1 ; "
" enum xyz e2 ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef12 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef vector<int> V1; \n "
" typedef std::vector<int> V2; \n "
" typedef std::vector<std::vector<int> > V3; \n "
" typedef std::list<int>::iterator IntListIterator; \n "
" V1 v1; \n "
" V2 v2; \n "
" V3 v3; \n "
" IntListIterator iter; " ;
const char expected [ ] =
" vector < int > v1 ; "
" std :: vector < int > v2 ; "
" std :: vector < std :: vector < int > > v3 ; "
" std :: list < int > :: iterator iter ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef13 ( ) {
2014-09-24 13:23:44 +02:00
// ticket # 1167
const char code [ ] = " typedef std::pair<int(*)(void*), void*> Func; "
" typedef std::vector<Func> CallQueue; "
" int main() {} " ;
// Tokenize and check output..
tok ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef14 ( ) {
2014-09-24 13:23:44 +02:00
// ticket # 1232
const char code [ ] = " template <typename F, unsigned int N> struct E "
" { "
" typedef E<F,(N>0)?(N-1):0> v; "
" typedef typename add<v,v>::val val; "
" FP_M(val); "
" }; "
" template <typename F> struct E <F,0> "
" { "
" typedef typename D<1>::val val; "
" FP_M(val); "
" }; " ;
// Tokenize and check output..
2023-03-03 18:36:27 +01:00
TODO_ASSERT_THROW ( tok ( code , true , cppcheck : : Platform : : Type : : Native , false ) , InternalError ) ; // TODO: Do not throw exception
2020-03-07 21:46:38 +01:00
//ASSERT_EQUALS("", errout.str());
2014-09-24 13:23:44 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef15 ( ) {
2014-09-24 13:23:44 +02:00
{
const char code [ ] = " typedef char frame[10]; \n "
" frame f; " ;
const char expected [ ] = " char f [ 10 ] ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " typedef unsigned char frame[10]; \n "
" frame f; " ;
const char expected [ ] = " unsigned char f [ 10 ] ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef16 ( ) {
2014-09-24 13:23:44 +02:00
// ticket # 1252
const char code [ ] = " typedef char MOT8; \n "
" typedef MOT8 CHFOO[4096]; \n "
" typedef struct { \n "
" CHFOO freem; \n "
" } STRFOO; " ;
// Tokenize and check output..
tok ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef17 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef char * PCHAR, CHAR; \n "
" PCHAR pc; \n "
" CHAR c; " ;
const char expected [ ] =
" char * pc ; "
" char c ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef18 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef vector<int[4]> a; \n "
2015-03-14 20:42:44 +01:00
" a b; " ;
2015-10-08 11:35:51 +02:00
ASSERT_EQUALS ( " vector < int [ 4 ] > b ; " , tok ( code ) ) ;
2014-09-24 13:23:44 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef19 ( ) {
2014-09-24 13:23:44 +02:00
{
// ticket #1275
const char code [ ] = " typedef struct {} A, *B, **C; \n "
" A a; \n "
" B b; \n "
" C c; " ;
const char expected [ ] =
" struct A { } ; "
" struct A a ; "
" struct A * b ; "
" struct A * * c ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " typedef struct {} A, *********B; \n "
" A a; \n "
" B b; " ;
const char expected [ ] =
" struct A { } ; "
" struct A a ; "
" struct A * * * * * * * * * b ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " typedef struct {} **********A, *B, C; \n "
" A a; \n "
" B b; \n "
" C c; " ;
const char expected [ ] =
2018-03-14 11:11:35 +01:00
" struct Unnamed0 { } ; "
" struct Unnamed0 * * * * * * * * * * a ; "
" struct Unnamed0 * b ; "
" struct Unnamed0 c ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef20 ( ) {
2014-09-24 13:23:44 +02:00
// ticket #1284
const char code [ ] = " typedef jobject invoke_t (jobject, Proxy *, Method *, JArray< jobject > *); " ;
2015-10-08 11:35:51 +02:00
ASSERT_EQUALS ( " ; " , tok ( code ) ) ;
2014-09-24 13:23:44 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef21 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef void (* PF)(); \n "
" typedef void * (* PFV)(void *); \n "
" PF pf; \n "
" PFV pfv; " ;
const char expected [ ] =
" "
" "
" void ( * pf ) ( ) ; "
" void * ( * pfv ) ( void * ) ; " ;
ASSERT_EQUALS ( expected , simplifyTypedef ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef22 ( ) {
2014-09-24 13:23:44 +02:00
{
const char code [ ] = " class Fred { \n "
" typedef void (*testfp)(); \n "
" testfp get() { return test; } \n "
" static void test() { } \n "
" }; " ;
const char expected [ ] =
" class Fred { "
" "
" void * get ( ) { return test ; } "
" static void test ( ) { } "
" } ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " class Fred { \n "
" typedef void * (*testfp)(void *); \n "
" testfp get() { return test; } \n "
" static void * test(void * p) { return p; } \n "
2015-03-14 20:42:44 +01:00
" }; " ;
2014-09-24 13:23:44 +02:00
const char expected [ ] =
" class Fred { "
" "
" void * * get ( ) { return test ; } "
" static void * test ( void * p ) { return p ; } "
" } ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " class Fred { \n "
" typedef unsigned int * (*testfp)(unsigned int *); \n "
" testfp get() { return test; } \n "
" static unsigned int * test(unsigned int * p) { return p; } \n "
2015-03-14 20:42:44 +01:00
" }; " ;
2014-09-24 13:23:44 +02:00
const char expected [ ] =
" class Fred { "
" "
" unsigned int * * get ( ) { return test ; } "
" static unsigned int * test ( unsigned int * p ) { return p ; } "
" } ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " class Fred { \n "
" typedef const unsigned int * (*testfp)(const unsigned int *); \n "
" testfp get() { return test; } \n "
" static const unsigned int * test(const unsigned int * p) { return p; } \n "
2015-03-14 20:42:44 +01:00
" }; " ;
2014-09-24 13:23:44 +02:00
// static const gets changed to const static
const char expected [ ] =
" class Fred { "
" "
" const unsigned int * * get ( ) { return test ; } "
2015-08-27 14:34:00 +02:00
" static const unsigned int * test ( const unsigned int * p ) { return p ; } "
2014-09-24 13:23:44 +02:00
" } ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " class Fred { \n "
" typedef void * (*testfp)(void *); \n "
" testfp get(int i) { return test; } \n "
" static void * test(void * p) { return p; } \n "
2015-03-14 20:42:44 +01:00
" }; " ;
2014-09-24 13:23:44 +02:00
const char expected [ ] =
" class Fred { "
" "
" void * * get ( int i ) { return test ; } "
" static void * test ( void * p ) { return p ; } "
" } ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef23 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef bool (*Callback) (int i); \n "
" void addCallback(Callback callback) { } \n "
" void addCallback1(Callback callback, int j) { } " ;
const char expected [ ] =
2023-02-26 18:03:24 +01:00
" void addCallback ( bool ( * callback ) ( int ) ) { } "
" void addCallback1 ( bool ( * callback ) ( int ) , int j ) { } " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef24 ( ) {
2014-09-24 13:23:44 +02:00
{
const char code [ ] = " typedef int (*fp)(); \n "
" void g( fp f ) \n "
" { \n "
" fp f2 = (fp)f; \n "
" } " ;
const char expected [ ] =
2020-04-10 11:53:32 +02:00
" void g ( int ( * f ) ( ) ) "
2014-09-24 13:23:44 +02:00
" { "
2020-04-10 11:53:32 +02:00
" int ( * f2 ) ( ) ; f2 = ( int * ) f ; "
2014-09-24 13:23:44 +02:00
" } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " typedef int (*fp)(); \n "
" void g( fp f ) \n "
" { \n "
" fp f2 = static_cast<fp>(f); \n "
" } " ;
const char expected [ ] =
2020-04-10 11:53:32 +02:00
" void g ( int ( * f ) ( ) ) "
2014-09-24 13:23:44 +02:00
" { "
2020-04-10 11:53:32 +02:00
" int ( * f2 ) ( ) ; f2 = static_cast < int * > ( f ) ; "
2014-09-24 13:23:44 +02:00
" } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef25 ( ) {
2014-09-24 13:23:44 +02:00
{
// ticket #1298
const char code [ ] = " typedef void (*fill_names_f) (const char *); \n "
" struct vfs_class { \n "
" void (*fill_names) (struct vfs_class *me, fill_names_f); \n "
" } " ;
const char expected [ ] =
" struct vfs_class { "
" void ( * fill_names ) ( struct vfs_class * me , void ( * ) ( const char * ) ) ; "
" } " ;
ASSERT_EQUALS ( expected , simplifyTypedef ( code ) ) ;
}
{
const char code [ ] = " typedef void (*fill_names_f) (const char *); \n "
" struct vfs_class { \n "
" void (*fill_names) (fill_names_f, struct vfs_class *me); \n "
" } " ;
const char expected [ ] =
" struct vfs_class { "
" void ( * fill_names ) ( void ( * ) ( const char * ) , struct vfs_class * me ) ; "
" } " ;
ASSERT_EQUALS ( expected , simplifyTypedef ( code ) ) ;
}
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef26 ( ) {
2014-09-24 13:23:44 +02:00
{
const char code [ ] = " typedef void (*Callback) (); \n "
" void addCallback(Callback (*callback)()); " ;
const char expected [ ] = " void addCallback ( void ( * ( * callback ) ( ) ) ( ) ) ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
// ticket # 1307
const char code [ ] = " typedef void (*pc_video_update_proc)(bitmap_t *bitmap, \n "
" struct mscrtc6845 *crtc); \n "
" \n "
" struct mscrtc6845 *pc_video_start(pc_video_update_proc (*choosevideomode)(running_machine *machine, int *width, int *height, struct mscrtc6845 *crtc)); " ;
const char expected [ ] = " struct mscrtc6845 * pc_video_start ( void ( * ( * choosevideomode ) ( running_machine * machine , int * width , int * height , struct mscrtc6845 * crtc ) ) ( bitmap_t * bitmap , struct mscrtc6845 * crtc ) ) ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef27 ( ) {
2014-09-24 13:23:44 +02:00
// ticket #1316
const char code [ ] = " int main() \n "
" { \n "
" typedef int (*func_ptr)(float, double); \n "
" VERIFY((is_same<result_of<func_ptr(char, float)>::type, int>::value)); \n "
" } " ;
const char expected [ ] =
" int main ( ) "
" { "
" "
" VERIFY ( is_same < result_of < int ( * ( char , float ) ) ( float , double ) > :: type , int > :: value ) ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef28 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef std::pair<double, double> (*F)(double); \n "
" F f; " ;
const char expected [ ] = " std :: pair < double , double > ( * f ) ( double ) ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef29 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef int array [ice_or<is_int<int>::value, is_int<UDT>::value>::value ? 1 : -1]; \n "
" typedef int array1 [N]; \n "
" typedef int array2 [N][M]; \n "
" typedef int int_t, int_array[N]; \n "
" array a; \n "
" array1 a1; \n "
" array2 a2; \n "
" int_t t; \n "
" int_array ia; " ;
const char expected [ ] =
" int a [ ice_or < is_int < int > :: value , is_int < UDT > :: value > :: value ? 1 : -1 ] ; "
" int a1 [ N ] ; "
" int a2 [ N ] [ M ] ; "
" int t ; "
" int ia [ N ] ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef30 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef ::std::list<int> int_list; \n "
" typedef ::std::list<int>::iterator int_list_iterator; \n "
" typedef ::std::list<int> int_list_array[10]; \n "
" int_list il; \n "
" int_list_iterator ili; \n "
" int_list_array ila; " ;
const char expected [ ] =
" :: std :: list < int > il ; "
" :: std :: list < int > :: iterator ili ; "
" :: std :: list < int > ila [ 10 ] ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef31 ( ) {
2014-09-24 13:23:44 +02:00
{
const char code [ ] = " class A { \n "
" public: \n "
" typedef int INT; \n "
" INT get() const; \n "
" void put(INT x) { a = x; } \n "
" INT a; \n "
" }; \n "
" A::INT A::get() const { return a; } \n "
" A::INT i = A::a; " ;
const char expected [ ] = " class A { "
" public: "
" "
" int get ( ) const ; "
" void put ( int x ) { a = x ; } "
" int a ; "
" } ; "
" int A :: get ( ) const { return a ; } "
" int i ; i = A :: a ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
{
const char code [ ] = " struct A { \n "
" typedef int INT; \n "
" INT get() const; \n "
" void put(INT x) { a = x; } \n "
" INT a; \n "
" }; \n "
" A::INT A::get() const { return a; } \n "
" A::INT i = A::a; " ;
const char expected [ ] = " struct A { "
" "
" int get ( ) const ; "
" void put ( int x ) { a = x ; } "
" int a ; "
" } ; "
" int A :: get ( ) const { return a ; } "
" int i ; i = A :: a ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef32 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef char CHAR; \n "
" typedef CHAR * LPSTR; \n "
" typedef const CHAR * LPCSTR; \n "
" CHAR c; \n "
" LPSTR cp; \n "
" LPCSTR ccp; " ;
const char expected [ ] =
" char c ; "
" char * cp ; "
" const char * ccp ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef33 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " class A { \n "
" public: \n "
" typedef char CHAR_A; \n "
" CHAR_A funA(); \n "
" class B { \n "
" public: \n "
" typedef short SHRT_B; \n "
" SHRT_B funB(); \n "
" class C { \n "
" public: \n "
" typedef int INT_C; \n "
" INT_C funC(); \n "
" struct D { \n "
" typedef long LONG_D; \n "
" LONG_D funD(); \n "
" LONG_D d; \n "
" }; \n "
" INT_C c; \n "
" }; \n "
" SHRT_B b; \n "
" }; \n "
" CHAR_A a; \n "
" }; \n "
" A::CHAR_A A::funA() { return a; } \n "
" A::B::SHRT_B A::B::funB() { return b; } \n "
" A::B::C::INT_C A::B::C::funC() { return c; } "
" A::B::C::D::LONG_D A::B::C::D::funD() { return d; } " ;
2016-08-21 16:07:05 +02:00
const char codeFullSpecified [ ] = " class A { \n "
" public: \n "
" typedef char CHAR_A; \n "
" A::CHAR_A funA(); \n "
" class B { \n "
" public: \n "
" typedef short SHRT_B; \n "
" A::B::SHRT_B funB(); \n "
" class C { \n "
" public: \n "
" typedef int INT_C; \n "
" A::B::C::INT_C funC(); \n "
" struct D { \n "
" typedef long LONG_D; \n "
" A::B::C::D::LONG_D funD(); \n "
" A::B::C::D::LONG_D d; \n "
" }; \n "
" A::B::C::INT_C c; \n "
" }; \n "
" A::B::SHRT_B b; \n "
" }; \n "
" A::CHAR_A a; \n "
" }; \n "
" A::CHAR_A A::funA() { return a; } \n "
" A::B::SHRT_B A::B::funB() { return b; } \n "
" A::B::C::INT_C A::B::C::funC() { return c; } "
" A::B::C::D::LONG_D A::B::C::D::funD() { return d; } " ;
const char codePartialSpecified [ ] = " class A { \n "
" public: \n "
" typedef char CHAR_A; \n "
" CHAR_A funA(); \n "
" class B { \n "
" public: \n "
" typedef short SHRT_B; \n "
" B::SHRT_B funB(); \n "
" class C { \n "
" public: \n "
" typedef int INT_C; \n "
" C::INT_C funC(); \n "
" struct D { \n "
" typedef long LONG_D; \n "
" D::LONG_D funD(); \n "
" C::D::LONG_D d; \n "
" }; \n "
" B::C::INT_C c; \n "
" }; \n "
" B::SHRT_B b; \n "
" }; \n "
" CHAR_A a; \n "
" }; \n "
" A::CHAR_A A::funA() { return a; } \n "
" A::B::SHRT_B A::B::funB() { return b; } \n "
" A::B::C::INT_C A::B::C::funC() { return c; } "
" A::B::C::D::LONG_D A::B::C::D::funD() { return d; } " ;
2014-09-24 13:23:44 +02:00
const char expected [ ] =
" class A { "
" public: "
" "
" char funA ( ) ; "
" class B { "
" public: "
" "
" short funB ( ) ; "
" class C { "
" public: "
" "
" int funC ( ) ; "
" struct D { "
" "
" long funD ( ) ; "
" long d ; "
" } ; "
" int c ; "
" } ; "
" short b ; "
" } ; "
" char a ; "
" } ; "
" char A :: funA ( ) { return a ; } "
" short A :: B :: funB ( ) { return b ; } "
" int A :: B :: C :: funC ( ) { return c ; } "
" long A :: B :: C :: D :: funD ( ) { return d ; } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
2016-08-21 16:07:05 +02:00
ASSERT_EQUALS ( expected , tok ( codePartialSpecified , false ) ) ;
ASSERT_EQUALS ( expected , tok ( codeFullSpecified , false ) ) ;
2014-09-24 13:23:44 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef34 ( ) {
2014-09-24 13:23:44 +02:00
// ticket #1411
const char code [ ] = " class X { }; \n "
" typedef X (*foofunc)(const X&); \n "
" int main() \n "
" { \n "
" foofunc *Foo = new foofunc[2]; \n "
" } " ;
const char expected [ ] =
" class X { } ; "
" int main ( ) "
" { "
2023-03-26 17:16:33 +02:00
" X ( * * Foo ) ( const X & ) ; Foo = new X ( * [ 2 ] ) ( const X & ) ; "
2014-09-24 13:23:44 +02:00
" } " ;
2023-03-26 17:16:33 +02:00
// TODO: Ideally some parentheses should be added. This code is compilable:
// int (**Foo)(int);
// Foo = new (int(*[2])(int)) ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef35 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef int A; \n "
" class S \n "
" { \n "
" public: \n "
" typedef float A; \n "
" A a; \n "
" virtual void fun(A x); \n "
" }; \n "
" void S::fun(S::A) { }; \n "
" class S1 : public S \n "
" { \n "
" public: \n "
" void fun(S::A) { } \n "
" }; \n "
" struct T \n "
" { \n "
" typedef A B; \n "
" B b; \n "
" }; \n "
" float fun1(float A) { return A; } \n "
" float fun2(float a) { float A = a++; return A; } \n "
" float fun3(int a) \n "
" { \n "
" typedef struct { int a; } A; \n "
" A s; s.a = a; \n "
" return s.a; \n "
" } \n "
" int main() \n "
" { \n "
" A a = 0; \n "
" S::A s = fun1(a) + fun2(a) - fun3(a); \n "
" return a + s; \n "
" } " ;
const char expected [ ] = " class S "
" { "
" public: "
" "
" float a ; "
" virtual void fun ( float x ) ; "
" } ; "
" void S :: fun ( float ) { } ; "
" class S1 : public S "
" { "
" public: "
" void fun ( float ) { } "
" } ; "
" struct T "
" { "
" "
" int b ; "
" } ; "
" float fun1 ( float A ) { return A ; } "
" float fun2 ( float a ) { float A ; A = a ++ ; return A ; } "
" float fun3 ( int a ) "
" { "
" struct A { int a ; } ; "
" struct A s ; s . a = a ; "
" return s . a ; "
" } "
" int main ( ) "
" { "
" int a ; a = 0 ; "
" float s ; s = fun1 ( a ) + fun2 ( a ) - fun3 ( a ) ; "
" return a + s ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
2023-03-24 13:31:26 +01:00
ASSERT_EQUALS_WITHOUT_LINENUMBERS ( " " , errout . str ( ) ) ;
2014-09-24 13:23:44 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef36 ( ) {
2014-09-24 13:23:44 +02:00
// ticket #1434
const char code [ ] = " typedef void (*TIFFFaxFillFunc)(); \n "
" void f(va_list ap) \n "
" { \n "
" *va_arg(ap, TIFFFaxFillFunc*) = 0; \n "
" } " ;
const char expected [ ] = " void f ( va_list ap ) "
" { "
" * va_arg ( ap , void ( * * ) ( ) ) = 0 ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef37 ( ) {
2016-02-06 20:50:44 +01:00
const char code [ ] = " typedef int INT; \n "
" void f() \n "
" { \n "
" INT i; { } \n "
" } " ;
const char expected [ ] = " void f ( ) "
" { "
" int i ; { } "
2014-09-24 13:23:44 +02:00
" } " ;
2016-02-06 20:50:44 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
2014-09-24 13:23:44 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef38 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef C A; \n "
" struct AB : public A, public B { }; " ;
const char expected [ ] = " struct AB : public C , public B { } ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef43 ( ) {
2014-09-24 13:23:44 +02:00
// ticket #1588
{
const char code [ ] = " typedef struct foo A; \n "
" struct A \n "
" { \n "
" int alloclen; \n "
2015-03-14 20:42:44 +01:00
" }; " ;
2014-09-24 13:23:44 +02:00
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " struct A "
" { "
" int alloclen ; "
" } ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2016-02-06 15:37:58 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-09-24 13:23:44 +02:00
}
{
const char code [ ] = " typedef union foo A; \n "
" union A \n "
" { \n "
" int alloclen; \n "
2015-03-14 20:42:44 +01:00
" }; " ;
2014-09-24 13:23:44 +02:00
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " union A "
" { "
" int alloclen ; "
" } ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2016-02-06 15:37:58 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-09-24 13:23:44 +02:00
}
{
const char code [ ] = " typedef class foo A; \n "
" class A \n "
" { \n "
" int alloclen; \n "
2015-03-14 20:42:44 +01:00
" }; " ;
2014-09-24 13:23:44 +02:00
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " class A "
" { "
" int alloclen ; "
" } ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2016-02-06 15:37:58 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-09-24 13:23:44 +02:00
}
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef44 ( ) {
2014-09-24 13:23:44 +02:00
{
const char code [ ] = " typedef std::map<std::string, int> Map; \n "
" class MyMap : public Map \n "
" { \n "
2015-03-14 20:42:44 +01:00
" }; " ;
2014-09-24 13:23:44 +02:00
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " class MyMap : public std :: map < std :: string , int > "
" { "
" } ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef std::map<std::string, int> Map; \n "
" class MyMap : protected Map \n "
" { \n "
2015-03-14 20:42:44 +01:00
" }; " ;
2014-09-24 13:23:44 +02:00
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " class MyMap : protected std :: map < std :: string , int > "
" { "
" } ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef std::map<std::string, int> Map; \n "
" class MyMap : private Map \n "
" { \n "
2015-03-14 20:42:44 +01:00
" }; " ;
2014-09-24 13:23:44 +02:00
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " class MyMap : private std :: map < std :: string , int > "
" { "
" } ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef struct foo { } A; \n "
" struct MyA : public A \n "
" { \n "
2015-03-14 20:42:44 +01:00
" }; " ;
2014-09-24 13:23:44 +02:00
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " struct foo { } ; "
" struct MyA : public foo "
" { "
" } ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef class foo { } A; \n "
" class MyA : public A \n "
" { \n "
2015-03-14 20:42:44 +01:00
" }; " ;
2014-09-24 13:23:44 +02:00
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " class foo { } ; "
" class MyA : public foo "
" { "
" } ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef45 ( ) {
2014-09-24 13:23:44 +02:00
// ticket # 1613
const char code [ ] = " void fn() { \n "
" typedef foo<> bar; \n "
" while (0 > bar(1)) {} \n "
" } " ;
checkSimplifyTypedef ( code ) ;
2023-06-07 11:11:48 +02:00
ASSERT_EQUALS_WITHOUT_LINENUMBERS (
" [test.cpp:3]: (debug) valueflow.cpp:6541:(valueFlow) bailout: valueFlowAfterCondition: bailing in conditional block \n " ,
errout . str ( ) ) ;
2014-09-24 13:23:44 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef46 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef const struct A { int a; } * AP; \n "
2015-03-14 20:42:44 +01:00
" AP ap; " ;
2014-09-24 13:23:44 +02:00
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " struct A { int a ; } ; "
" const struct A * ap ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef47 ( ) {
2014-09-24 13:23:44 +02:00
{
const char code [ ] = " typedef std::pair<int, int> const I; \n "
" I i; " ;
// The expected result..
2022-04-08 08:22:39 +02:00
const char expected [ ] = " const std :: pair < int , int > i ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
{
const char code [ ] = " typedef void (X:: *F)(); \n "
" F f; " ;
// The expected result..
2020-04-10 11:53:32 +02:00
const char expected [ ] = " void ( * f ) ( ) ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef48 ( ) { // ticket #1673
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef struct string { } string; \n "
" void foo (LIST *module_name) \n "
" { \n "
" bar(module_name ? module_name->string : 0); \n "
2015-03-14 20:42:44 +01:00
" } " ;
2014-09-24 13:23:44 +02:00
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " struct string { } ; "
" void foo ( LIST * module_name ) "
" { "
" bar ( module_name ? module_name . string : 0 ) ; "
" } " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef49 ( ) { // ticket #1691
2014-09-24 13:23:44 +02:00
const char code [ ] = " class Class2 { \n "
" typedef const Class & Const_Reference; \n "
" void some_method (Const_Reference x) const {} \n "
" void another_method (Const_Reference x) const {} \n "
#11134 Fix broken AST with (designated) initializers (#4550)
* Make control flow a bit easier, and more similar to previous code
Made similar to around line 790
* In a cpp11init, always parse only the corresponding } (#11134)
- _always_, because in some cases this was omitted (around line 790) or too strict (around line 860)
- _only_, and not following tokens which happen to be } as well (around line 1030)
* Fix unit tests: AST was incorrect, now is fixed
auto var{ {{},{}}, {} };
Old AST:
```
{
|-var
`-{
`-,
|-,
| |-{
| `-{
`-{
```
New AST:
```
{
|-var
`-,
|-{
| `-,
| | |-{
| | `-{
`-{
```
Compare the same example, but with `X{}` instead of just `{}`:
`auto var{ a{b{},c{}}, d{} };`
```
{
|-var
`-,
|-{
| |-a
| `-,
| | |-{
| | | `-b
| | `-{
| | | `-c
`-{
`-d
```
This structure is similar to that of the new AST, not the old AST
* Fix unit tests: another AST was incorrect, now is fixed
Code: `auto var{{1,a::b{2,3}}, {4,a::b{5,6}}};`
Old AST:
```
{
|-var
`-{
`-,
|-,
| |-1 'signed int'
| `-{
| | |-::
| | | |-a
| | | `-b
| | `-,
| | | |-2 'signed int'
| | | `-3 'signed int'
`-{
`-,
|-4 'signed int'
`-{
|-::
| |-a
| `-b
`-,
|-5 'signed int'
`-6 'signed int'
```
New AST:
```
{
|-var
`-,
|-{
| `-,
| | |-1 'signed int'
| | `-{
| | | |-::
| | | | |-a
| | | | `-b
| | | `-,
| | | | |-2 'signed int'
| | | | `-3 'signed int'
`-{
`-,
|-4 'signed int'
`-{
|-::
| |-a
| `-b
`-,
|-5 'signed int'
`-6 'signed int'
```
* Fix unit tests: missing ; after class, resulting in incorrectly being marked as cpp11init
Because of the missing `;` after the class declaration, it was marked as a cpp11init block.
Which it isn't, and which now throws an exception
* Fix cpp11init to let unit tests pass again
The following unit tests failed on the newly introduced throws, because the code for these tests incorrectly marked some tokens as cpp11init:
TestVarID::varid_cpp11initialization
TestTokenizer::checkRefQualifiers
* Fix typo
* Improve check for void trailing return type
Observation: the only function body _not_ containing a semicolon, is a void function: something like
auto make_zero(ini& i) -> void {
while(--i > 0) {}
}
Non-void function? Then it must return a value, and thus contain a semicolon, which is checked for a few lines later.
* Fix cpp11init with templated trailing return type
In the following example, vector was marked as cpp11init due to the mismatch of `%any% {`
auto f() -> std::vector<int> { return {}; }
I made the assumption that whenever "%any% {" matches, endtok must be set too.
If this assumtion doesn't hold (so "%any% {" matches, but endtok == nullptr), then the for-loop would search all the way to the end of stream. Which I guess was not the intention.
* Remove comments
Co-authored-by: Gerbo Engels <gerbo.engels@ortec-finance.com>
2022-10-19 07:25:15 +02:00
" }; " ;
2014-09-24 13:23:44 +02:00
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " class Class2 { "
" "
" void some_method ( const Class & x ) const { } "
" void another_method ( const Class & x ) const { } "
#11134 Fix broken AST with (designated) initializers (#4550)
* Make control flow a bit easier, and more similar to previous code
Made similar to around line 790
* In a cpp11init, always parse only the corresponding } (#11134)
- _always_, because in some cases this was omitted (around line 790) or too strict (around line 860)
- _only_, and not following tokens which happen to be } as well (around line 1030)
* Fix unit tests: AST was incorrect, now is fixed
auto var{ {{},{}}, {} };
Old AST:
```
{
|-var
`-{
`-,
|-,
| |-{
| `-{
`-{
```
New AST:
```
{
|-var
`-,
|-{
| `-,
| | |-{
| | `-{
`-{
```
Compare the same example, but with `X{}` instead of just `{}`:
`auto var{ a{b{},c{}}, d{} };`
```
{
|-var
`-,
|-{
| |-a
| `-,
| | |-{
| | | `-b
| | `-{
| | | `-c
`-{
`-d
```
This structure is similar to that of the new AST, not the old AST
* Fix unit tests: another AST was incorrect, now is fixed
Code: `auto var{{1,a::b{2,3}}, {4,a::b{5,6}}};`
Old AST:
```
{
|-var
`-{
`-,
|-,
| |-1 'signed int'
| `-{
| | |-::
| | | |-a
| | | `-b
| | `-,
| | | |-2 'signed int'
| | | `-3 'signed int'
`-{
`-,
|-4 'signed int'
`-{
|-::
| |-a
| `-b
`-,
|-5 'signed int'
`-6 'signed int'
```
New AST:
```
{
|-var
`-,
|-{
| `-,
| | |-1 'signed int'
| | `-{
| | | |-::
| | | | |-a
| | | | `-b
| | | `-,
| | | | |-2 'signed int'
| | | | `-3 'signed int'
`-{
`-,
|-4 'signed int'
`-{
|-::
| |-a
| `-b
`-,
|-5 'signed int'
`-6 'signed int'
```
* Fix unit tests: missing ; after class, resulting in incorrectly being marked as cpp11init
Because of the missing `;` after the class declaration, it was marked as a cpp11init block.
Which it isn't, and which now throws an exception
* Fix cpp11init to let unit tests pass again
The following unit tests failed on the newly introduced throws, because the code for these tests incorrectly marked some tokens as cpp11init:
TestVarID::varid_cpp11initialization
TestTokenizer::checkRefQualifiers
* Fix typo
* Improve check for void trailing return type
Observation: the only function body _not_ containing a semicolon, is a void function: something like
auto make_zero(ini& i) -> void {
while(--i > 0) {}
}
Non-void function? Then it must return a value, and thus contain a semicolon, which is checked for a few lines later.
* Fix cpp11init with templated trailing return type
In the following example, vector was marked as cpp11init due to the mismatch of `%any% {`
auto f() -> std::vector<int> { return {}; }
I made the assumption that whenever "%any% {" matches, endtok must be set too.
If this assumtion doesn't hold (so "%any% {" matches, but endtok == nullptr), then the for-loop would search all the way to the end of stream. Which I guess was not the intention.
* Remove comments
Co-authored-by: Gerbo Engels <gerbo.engels@ortec-finance.com>
2022-10-19 07:25:15 +02:00
" } ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef50 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef char (* type1)[10]; \n "
" typedef char (& type2)[10]; \n "
" typedef char (& type3)[x]; \n "
" typedef char (& type4)[x + 2]; \n "
" type1 t1; \n "
" type1 (*tp1)[2]; \n "
" type2 t2; \n "
" type3 t3; \n "
" type4 t4; " ;
// The expected result..
2015-08-25 21:19:19 +02:00
const char expected [ ] = " char ( * t1 ) [ 10 ] ; "
2015-03-14 20:42:44 +01:00
" char ( * ( * tp1 ) [ 2 ] ) [ 10 ] ; "
" char ( & t2 ) [ 10 ] ; "
" char ( & t3 ) [ x ] ; "
" char ( & t4 ) [ x + 2 ] ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef51 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " class A { public: int i; }; \n "
" typedef const char (A :: * type1); \n "
" type1 t1 = &A::i; " ;
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " class A { public: int i ; } ; "
" const char ( A :: * t1 ) ; t1 = & A :: i ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef52 ( ) { // ticket #1782
2014-09-24 13:23:44 +02:00
{
const char code [ ] = " typedef char (* type1)[10]; \n "
" type1 foo() { } " ;
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " char ( * foo ( ) ) [ 10 ] { } " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef char (* type1)[10]; \n "
" LOCAL(type1) foo() { } " ;
2018-11-13 16:49:02 +01:00
// this is invalid C, assert that an "unknown macro" warning is written
ASSERT_THROW ( checkSimplifyTypedef ( code ) , InternalError ) ;
2014-09-24 13:23:44 +02:00
}
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef54 ( ) { // ticket #1814
2014-09-24 13:23:44 +02:00
const char code [ ] = " void foo() \n "
" { \n "
" typedef std::basic_string<char, traits_type, allocator_type> string_type; \n "
" try \n "
" { \n "
" throw string_type( \" leak \" ); \n "
" } \n "
" catch (const string_type&) \n "
" { \n "
" pthread_exit (0); \n "
" } \n "
" } " ;
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef55 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef volatile unsigned long * const hwreg_t ; \n "
" typedef void *const t1[2]; \n "
" typedef int*const *_Iterator; \n "
" hwreg_t v1; \n "
" t1 v2; \n "
2015-03-14 20:42:44 +01:00
" _Iterator v3; " ;
2014-09-24 13:23:44 +02:00
// The expected result..
2018-05-10 07:40:01 +02:00
const char expected [ ] = " volatile long * const v1 ; "
2016-05-08 21:32:34 +02:00
" void * const v2 [ 2 ] ; "
" int * const * v3 ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
// Check for output..
checkSimplifyTypedef ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef56 ( ) { // ticket #1829
2014-09-24 13:23:44 +02:00
const char code [ ] = " struct C { \n "
" typedef void (*fptr)(); \n "
" const fptr pr; \n "
" operator const fptr& () { return pr; } \n "
2015-03-14 20:42:44 +01:00
" }; " ;
2014-09-24 13:23:44 +02:00
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " struct C { "
" "
2020-04-10 11:53:32 +02:00
" const void ( * pr ) ( ) ; "
2015-03-14 20:42:44 +01:00
" operatorconstvoid(*)()& ( ) { return pr ; } "
" } ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef57 ( ) { // ticket #1846
2014-10-16 20:46:44 +02:00
const char code [ ] = " void foo() { \n "
2014-09-24 13:23:44 +02:00
" typedef int A; \n "
" A a = A(1) * A(2); \n "
2015-03-14 20:42:44 +01:00
" }; " ;
2014-09-24 13:23:44 +02:00
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " void foo ( ) { "
" "
" int a ; a = int ( 1 ) * int ( 2 ) ; "
" } ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef58 ( ) { // ticket #1963
2014-09-24 13:23:44 +02:00
{
const char code [ ] = " typedef int vec2_t[2]; \n "
2015-03-14 20:42:44 +01:00
" vec2_t coords[4] = {1,2,3,4,5,6,7,8}; " ;
2014-09-24 13:23:44 +02:00
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " int coords [ 4 ] [ 2 ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef int vec2_t[2]; \n "
2015-03-14 20:42:44 +01:00
" vec2_t coords[4][5][6+1] = {1,2,3,4,5,6,7,8}; " ;
2014-09-24 13:23:44 +02:00
// The expected result..
2020-05-20 18:52:46 +02:00
const char expected [ ] = " int coords [ 4 ] [ 5 ] [ 6 + 1 ] [ 2 ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef59 ( ) { // ticket #2011
2014-09-24 13:23:44 +02:00
const char code [ ] = " template<typename DISPATCHER> class SomeTemplateClass { \n "
" typedef void (SomeTemplateClass<DISPATCHER>::*MessageDispatcherFunc)(SerialInputMessage&); \n "
2015-03-14 20:42:44 +01:00
" }; " ;
2014-09-24 13:23:44 +02:00
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " template < typename DISPATCHER > class SomeTemplateClass { } ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef60 ( ) { // ticket #2035
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef enum {qfalse, qtrue} qboolean; \n "
" typedef qboolean (*localEntitiyAddFunc_t) (struct le_s * le, entity_t * ent); \n "
2015-09-06 18:37:22 +02:00
" void f() \n "
" { \n "
2014-09-24 13:23:44 +02:00
" qboolean b; \n "
" localEntitiyAddFunc_t f; \n "
2015-03-14 20:42:44 +01:00
" } " ;
2014-09-24 13:23:44 +02:00
// The expected result..
2023-02-26 18:03:24 +01:00
const char expected [ ] = " enum qboolean { qfalse , qtrue } ; void f ( ) { qboolean b ; qboolean ( * f ) ( struct le_s * , entity_t * ) ; } " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef61 ( ) { // ticket #2074 and 2075
2014-09-24 13:23:44 +02:00
const char code1 [ ] = " typedef unsigned char (*Mf_GetIndexByte_Func) (void); \n "
2015-03-14 20:42:44 +01:00
" typedef const unsigned char * (*Mf_GetPointerToCurrentPos_Func)(void); " ;
2014-09-24 13:23:44 +02:00
// Check for output..
checkSimplifyTypedef ( code1 ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
const char code2 [ ] = " typedef unsigned long uint32_t; \n "
2015-03-14 20:42:44 +01:00
" typedef uint32_t (*write_type_t) (uint32_t); " ;
2014-09-24 13:23:44 +02:00
// Check for output..
checkSimplifyTypedef ( code2 ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef62 ( ) { // ticket #2082
2014-09-24 13:23:44 +02:00
const char code1 [ ] = " typedef char TString[256]; \n "
" void f() \n "
" { \n "
" TString a, b; \n "
" } " ;
// The expected tokens..
2015-03-15 12:36:40 +01:00
const char expected1 [ ] = " void f ( ) { char a [ 256 ] ; char b [ 256 ] ; } " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected1 , tok ( code1 , false ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
const char code2 [ ] = " typedef char TString[256]; \n "
" void f() \n "
" { \n "
" TString a = { 0 }, b = { 0 }; \n "
" } " ;
// The expected tokens..
2015-03-15 12:36:40 +01:00
const char expected2 [ ] = " void f ( ) { char a [ 256 ] ; a = { 0 } ; char b [ 256 ] ; b = { 0 } ; } " ;
2023-03-03 18:36:27 +01:00
ASSERT_EQUALS ( expected2 , tok ( code2 , false , cppcheck : : Platform : : Type : : Native , false ) ) ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
const char code3 [ ] = " typedef char TString[256]; \n "
" void f() \n "
" { \n "
" TString a = \" \" , b = \" \" ; \n "
" } " ;
// The expected tokens..
2015-03-15 12:36:40 +01:00
const char expected3 [ ] = " void f ( ) { char a [ 256 ] ; a = \" \" ; char b [ 256 ] ; b = \" \" ; } " ;
2023-03-03 18:36:27 +01:00
ASSERT_EQUALS ( expected3 , tok ( code3 , false , cppcheck : : Platform : : Type : : Native , false ) ) ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
const char code4 [ ] = " typedef char TString[256]; \n "
" void f() \n "
" { \n "
" TString a = \" 1234 \" , b = \" 5678 \" ; \n "
" } " ;
// The expected tokens..
2015-03-15 12:36:40 +01:00
const char expected4 [ ] = " void f ( ) { char a [ 256 ] ; a = \" 1234 \" ; char b [ 256 ] ; b = \" 5678 \" ; } " ;
2023-03-03 18:36:27 +01:00
ASSERT_EQUALS ( expected4 , tok ( code4 , false , cppcheck : : Platform : : Type : : Native , false ) ) ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef63 ( ) { // ticket #2175 'typedef float x[3];'
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef float x[3]; \n "
2015-03-14 20:42:44 +01:00
" x a,b,c; " ;
2014-09-24 13:23:44 +02:00
const std : : string actual ( tok ( code ) ) ;
ASSERT_EQUALS ( " float a [ 3 ] ; float b [ 3 ] ; float c [ 3 ] ; " , actual ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef64 ( ) {
2019-10-14 08:20:22 +02:00
const char code [ ] = " typedef typeof(__type1() + __type2()) __type; "
2015-03-14 20:42:44 +01:00
" __type t; " ;
2014-09-24 13:23:44 +02:00
const std : : string actual ( tok ( code ) ) ;
2019-10-14 08:20:22 +02:00
ASSERT_EQUALS ( " typeof ( __type1 ( ) + __type2 ( ) ) t ; " , actual ) ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef65 ( ) { // ticket #2314
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef BAR<int> Foo; \n "
" int main() { \n "
" Foo b(0); \n "
" return b > Foo(10); \n "
" } " ;
2023-03-03 18:36:27 +01:00
const std : : string actual ( tok ( code , true , cppcheck : : Platform : : Type : : Native , false ) ) ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( " int main ( ) { BAR < int > b ( 0 ) ; return b > BAR < int > ( 10 ) ; } " , actual ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef66 ( ) { // ticket #2341
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef long* GEN; \n "
" extern GEN (*foo)(long); " ;
tok ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef67 ( ) { // ticket #2354
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef int ( * Function ) ( ) ; \n "
" void f ( ) { \n "
" ((Function * (*) (char *, char *, int, int)) global[6]) ( \" assoc \" , \" eggdrop \" , 106, 0); \n "
2015-03-14 20:42:44 +01:00
" } " ;
2023-03-26 17:16:33 +02:00
// TODO should it be simplified as below instead?
// "( ( int ( * * ( * ) ( char * , char * , int , int ) ) ( ) ) global [ 6 ] ) ( \"assoc\" , \"eggdrop\" , 106 , 0 ) ; "
2015-03-14 20:42:44 +01:00
const char expected [ ] = " void f ( ) { "
2023-03-26 17:16:33 +02:00
" ( ( int * * * ) global [ 6 ] ) ( \" assoc \" , \" eggdrop \" , 106 , 0 ) ; "
2015-03-14 20:42:44 +01:00
" } " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
2021-01-23 08:47:39 +01:00
ASSERT_EQUALS_WITHOUT_LINENUMBERS ( " [test.cpp:3]: (debug) valueflow.cpp:1319:valueFlowConditionExpressions bailout: Skipping function due to incomplete variable global \n " , errout . str ( ) ) ;
2014-09-24 13:23:44 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef68 ( ) { // ticket #2355
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef FMAC1 void (* a) (); \n "
2015-03-14 20:42:44 +01:00
" void *(*b) (); " ;
2014-09-24 13:23:44 +02:00
const std : : string actual ( tok ( code ) ) ;
2020-04-10 11:53:32 +02:00
ASSERT_EQUALS ( " void * ( * b ) ( ) ; " , actual ) ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef69 ( ) { // ticket #2348
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef int (*CompilerHook)(); \n "
" typedef struct VirtualMachine \n "
" { \n "
" CompilerHook *(*compilerHookVector)(void); \n "
2015-03-14 20:42:44 +01:00
" }VirtualMachine; " ;
const char expected [ ] = " struct VirtualMachine "
" { "
" int ( * * ( * compilerHookVector ) ( void ) ) ( ) ; "
" } ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef70 ( ) { // ticket #2348
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef int pread_f ( int ) ; \n "
2015-03-14 20:42:44 +01:00
" pread_f *(*test_func)(char *filename); " ;
const char expected [ ] = " int ( * ( * test_func ) ( char * filename ) ) ( int ) ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef71 ( ) { // ticket #2348
2014-09-24 13:23:44 +02:00
{
const char code [ ] = " typedef int RexxFunctionHandler(); \n "
2015-03-14 20:42:44 +01:00
" RexxFunctionHandler *(efuncs[1]); " ;
const char expected [ ] = " int ( * ( efuncs [ 1 ] ) ) ( ) ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef int RexxFunctionHandler(); \n "
2015-03-14 20:42:44 +01:00
" RexxFunctionHandler *(efuncs[]) = { NULL, NULL }; " ;
2016-10-18 21:44:02 +02:00
const char expected [ ] = " int ( * ( efuncs [ ] ) ) ( ) = { NULL , NULL } ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef72 ( ) { // ticket #2374
2014-09-24 13:23:44 +02:00
// inline operator
{
const char code [ ] = " class Fred { \n "
" typedef int* (Fred::*F); \n "
" operator F() const { } \n "
2015-03-14 20:42:44 +01:00
" }; " ;
const char expected [ ] = " class Fred { "
" "
" operatorint** ( ) const { } "
" } ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
// inline local variable
{
const char code [ ] = " class Fred { \n "
" typedef int INT; \n "
" void f1() const { INT i; } \n "
2015-03-14 20:42:44 +01:00
" }; " ;
const char expected [ ] = " class Fred { "
" "
" void f1 ( ) const { int i ; } "
" } ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
// out of line member variable
{
const char code [ ] = " class Fred { \n "
" typedef int INT; \n "
" void f1() const; \n "
" }; \n "
2015-03-14 20:42:44 +01:00
" void Fred::f1() const { INT i; f(i); } " ;
const char expected [ ] = " class Fred { "
" "
" void f1 ( ) const ; "
" } ; "
" void Fred :: f1 ( ) const { int i ; f ( i ) ; } " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
// out of line operator
{
const char code [ ] = " class Fred { \n "
" typedef int* (Fred::*F); \n "
" operator F() const; \n "
" }; \n "
2015-03-14 20:42:44 +01:00
" Fred::operator F() const { } " ;
const char expected [ ] = " class Fred { "
" "
" operatorint** ( ) const ; "
" } ; "
" Fred :: operatorint** ( ) const { } " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef73 ( ) { // ticket #2412
2014-09-24 13:23:44 +02:00
const char code [ ] = " struct B {}; \n "
" typedef struct A : public B { \n "
" void f(); \n "
2015-03-14 20:42:44 +01:00
" } a, *aPtr; " ;
const char expected [ ] = " struct B { } ; "
" struct A : public B { "
" void f ( ) ; "
" } ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef74 ( ) { // ticket #2414
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef long (*state_func_t)(void); \n "
" typedef state_func_t (*state_t)(void); \n "
" state_t current_state = death; \n "
2015-03-14 20:42:44 +01:00
" static char get_runlevel(const state_t); " ;
const char expected [ ] = " long ( * ( * current_state ) ( void ) ) ( void ) ; current_state = death ; "
2023-03-26 17:16:33 +02:00
" static char get_runlevel ( long ( * ( * const ) ( void ) ) ( void ) ) ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef75 ( ) { // ticket #2426
2015-03-14 20:42:44 +01:00
const char code [ ] = " typedef _Packed struct S { long l; }; " ;
2023-03-03 18:36:27 +01:00
ASSERT_EQUALS ( " ; " , tok ( code , true , cppcheck : : Platform : : Type : : Native , false ) ) ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef76 ( ) { // ticket #2453 segmentation fault
2023-04-07 11:09:13 +02:00
ASSERT_THROW ( checkSimplifyTypedef ( " void f1(typedef int x) {} " ) , InternalError ) ;
2014-09-24 13:23:44 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef77 ( ) { // ticket #2554
2015-03-14 20:42:44 +01:00
const char code [ ] = " typedef char Str[10]; int x = sizeof(Str); " ;
2020-05-20 18:52:46 +02:00
const char expected [ ] = " int x ; x = sizeof ( char [ 10 ] ) ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef78 ( ) { // ticket #2568
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef struct A A_t; \n "
" A_t a; \n "
" typedef struct A { } A_t; \n "
2015-03-14 20:42:44 +01:00
" A_t a1; " ;
const char expected [ ] = " struct A a ; struct A { } ; struct A a1 ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef79 ( ) { // ticket #2348
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef int (Tcl_ObjCmdProc) (int x); \n "
" typedef struct LangVtab \n "
" { \n "
" Tcl_ObjCmdProc * (*V_LangOptionCommand); \n "
2015-03-14 20:42:44 +01:00
" } LangVtab; " ;
const char expected [ ] = " struct LangVtab "
" { "
" int ( * ( * V_LangOptionCommand ) ) ( int x ) ; "
" } ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef80 ( ) { // ticket #2587
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef struct s { }; \n "
" void f() { \n "
" sizeof(struct s); \n "
2015-03-14 20:42:44 +01:00
" }; " ;
const char expected [ ] = " struct s { } ; "
" void f ( ) { "
" sizeof ( struct s ) ; "
" } ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef81 ( ) { // ticket #2603 segmentation fault
2014-09-24 13:23:44 +02:00
ASSERT_THROW ( checkSimplifyTypedef ( " typedef \n " ) , InternalError ) ;
2018-11-03 19:22:51 +01:00
ASSERT_THROW ( checkSimplifyTypedef ( " typedef constexpr \n " ) , InternalError ) ;
2014-09-24 13:23:44 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef82 ( ) { // ticket #2403
2014-09-24 13:23:44 +02:00
checkSimplifyTypedef ( " class A { \n "
" public: \n "
" typedef int F(int idx); \n "
" }; \n "
" class B { \n "
" public: \n "
" A::F ** f; \n "
" }; \n "
" int main() \n "
" { \n "
" B * b = new B; \n "
" b->f = new A::F * [ 10 ]; \n "
" } " ) ;
2023-01-26 22:11:37 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-09-24 13:23:44 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef83 ( ) { // ticket #2620
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef char Str[10]; \n "
2015-03-14 20:42:44 +01:00
" void f(Str &cl) { } " ;
2014-09-24 13:23:44 +02:00
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " void f ( char ( & cl ) [ 10 ] ) { } " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef84 ( ) { // ticket #2630 (segmentation fault)
2015-03-14 20:42:44 +01:00
const char code1 [ ] = " typedef y x () x " ;
2014-09-24 13:23:44 +02:00
ASSERT_THROW ( checkSimplifyTypedef ( code1 ) , InternalError ) ;
2015-03-14 20:42:44 +01:00
const char code2 [ ] = " typedef struct template <> " ;
2014-09-24 13:23:44 +02:00
ASSERT_THROW ( checkSimplifyTypedef ( code2 ) , InternalError ) ;
2015-03-14 20:42:44 +01:00
const char code3 [ ] = " typedef ::<> " ;
2014-09-24 13:23:44 +02:00
ASSERT_THROW ( checkSimplifyTypedef ( code3 ) , InternalError ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef85 ( ) { // ticket #2651
2015-03-14 20:42:44 +01:00
const char code [ ] = " typedef FOO ((BAR)(void, int, const int, int*)); " ;
2014-09-24 13:23:44 +02:00
const char expected [ ] = " ; " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef86 ( ) { // ticket #2581
2014-09-24 13:23:44 +02:00
const char code [ ] = " class relational { \n "
" typedef void (safe_bool_helper::*safe_bool)(); \n "
" public: \n "
" operator safe_bool() const; \n "
" safe_bool operator!() const; \n "
2015-03-14 20:42:44 +01:00
" }; " ;
2014-09-24 13:23:44 +02:00
const char expected [ ] = " class relational { "
" "
" public: "
" operatorsafe_bool ( ) const ; "
" safe_bool operator! ( ) const ; "
" } ; " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef87 ( ) { // ticket #2651
2015-03-14 20:42:44 +01:00
const char code [ ] = " typedef FOO (*(*BAR)(void, int, const int, int*)); " ;
2014-09-24 13:23:44 +02:00
const char expected [ ] = " ; " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef88 ( ) { // ticket #2675
2015-03-14 20:42:44 +01:00
const char code [ ] = " typedef short int (*x)(...); " ;
2014-09-24 13:23:44 +02:00
const char expected [ ] = " ; " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef89 ( ) { // ticket #2717
2014-09-24 13:23:44 +02:00
const char code [ ] = " class Fred { \n "
" typedef void f(int) const; \n "
" f func; \n "
2015-03-14 20:42:44 +01:00
" }; " ;
2014-09-24 13:23:44 +02:00
const char expected [ ] = " class Fred { void func ( int ) const ; } ; " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef90 ( ) { // ticket #2718
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef int IA[2]; \n "
2015-03-14 20:42:44 +01:00
" void f(const IA&) {}; " ;
2014-09-24 13:23:44 +02:00
const char expected [ ] = " void f ( const int ( & ) [ 2 ] ) { } ; " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef91 ( ) { // ticket #2716
2014-09-24 13:23:44 +02:00
const char code1 [ ] = " namespace NS { \n "
" typedef int (*T)(); \n "
" class A { \n "
" T f(); \n "
" }; \n "
" } \n "
" namespace NS { \n "
" T A::f() {} \n "
2015-03-14 20:42:44 +01:00
" } " ;
2014-09-24 13:23:44 +02:00
const char expected1 [ ] = " namespace NS { "
" "
" class A { "
" int * f ( ) ; "
" } ; "
" } "
" namespace NS { "
" int * A :: f ( ) { } "
" } " ;
ASSERT_EQUALS ( expected1 , tok ( code1 ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
const char code2 [ ] = " namespace NS { \n "
" typedef int (*T)(); \n "
" class A { \n "
" T f(); \n "
" }; \n "
" } \n "
2015-03-14 20:42:44 +01:00
" NS::T NS::A::f() {} " ;
2014-09-24 13:23:44 +02:00
const char expected2 [ ] = " namespace NS { "
" "
" class A { "
" int * f ( ) ; "
" } ; "
" } "
" int * NS :: A :: f ( ) { } " ;
ASSERT_EQUALS ( expected2 , tok ( code2 ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
const char code3 [ ] = " namespace NS1 { \n "
" namespace NS2 { \n "
" typedef int (*T)(); \n "
" class A { \n "
" T f(); \n "
" }; \n "
" } \n "
" } \n "
" namespace NS1 { \n "
" namespace NS2 { \n "
" T A::f() {} \n "
" } \n "
2015-03-14 20:42:44 +01:00
" } " ;
2014-09-24 13:23:44 +02:00
const char expected3 [ ] = " namespace NS1 { "
" namespace NS2 { "
" "
" class A { "
" int * f ( ) ; "
" } ; "
" } "
" } "
" namespace NS1 { "
" namespace NS2 { "
" int * A :: f ( ) { } "
" } "
" } " ;
ASSERT_EQUALS ( expected3 , tok ( code3 ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
const char code4 [ ] = " namespace NS1 { \n "
" namespace NS2 { \n "
" typedef int (*T)(); \n "
" class A { \n "
" T f(); \n "
" }; \n "
" } \n "
" } \n "
" namespace NS1 { \n "
" NS2::T NS2::A::f() {} \n "
2015-03-14 20:42:44 +01:00
" } " ;
2014-09-24 13:23:44 +02:00
const char expected4 [ ] = " namespace NS1 { "
" namespace NS2 { "
" "
" class A { "
" int * f ( ) ; "
" } ; "
" } "
" } "
" namespace NS1 { "
" int * NS2 :: A :: f ( ) { } "
" } " ;
ASSERT_EQUALS ( expected4 , tok ( code4 ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef92 ( ) { // ticket #2736 (segmentation fault)
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef long Long; \n "
" namespace NS { \n "
2015-03-14 20:42:44 +01:00
" } " ;
2020-11-22 16:43:36 +01:00
ASSERT_EQUALS ( " ; " , tok ( code ) ) ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef93 ( ) { // ticket #2738 (syntax error)
2014-09-24 13:23:44 +02:00
const char code [ ] = " struct s { double x; }; \n "
2015-03-14 20:42:44 +01:00
" typedef struct s (*binop) (struct s, struct s); " ;
2014-09-24 13:23:44 +02:00
const char expected [ ] = " struct s { double x ; } ; " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef94 ( ) { // ticket #1982
2014-09-24 13:23:44 +02:00
const char code1 [ ] = " class A { \n "
" public: \n "
" typedef struct { \n "
" int a[4]; \n "
" } data; \n "
" }; \n "
2015-03-14 20:42:44 +01:00
" A::data d; " ;
2014-09-24 13:23:44 +02:00
const char expected1 [ ] = " class A { "
" public: "
" struct data { "
" int a [ 4 ] ; "
" } ; "
" } ; "
" struct A :: data d ; " ;
ASSERT_EQUALS ( expected1 , tok ( code1 ) ) ;
2014-10-02 14:55:03 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-09-24 13:23:44 +02:00
const char code2 [ ] = " class A { \n "
" public: \n "
" typedef struct { \n "
" int a[4]; \n "
" } data; \n "
" }; \n "
2015-03-14 20:42:44 +01:00
" ::A::data d; " ;
2014-09-24 13:23:44 +02:00
const char expected2 [ ] = " class A { "
" public: "
" struct data { "
" int a [ 4 ] ; "
" } ; "
" } ; "
" struct :: A :: data d ; " ;
ASSERT_EQUALS ( expected2 , tok ( code2 ) ) ;
2014-10-02 14:55:03 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-09-24 13:23:44 +02:00
const char code3 [ ] = " class A { \n "
" public: \n "
" typedef struct { \n "
" int a[4]; \n "
" } data; \n "
" }; \n "
2015-03-14 20:42:44 +01:00
" class B : public ::A::data { }; " ;
2014-09-24 13:23:44 +02:00
const char expected3 [ ] = " class A { "
" public: "
" struct data { "
" int a [ 4 ] ; "
" } ; "
" } ; "
" class B : public :: A :: data { } ; " ;
ASSERT_EQUALS ( expected3 , tok ( code3 ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef95 ( ) { // ticket #2844
2014-09-24 13:23:44 +02:00
const char code [ ] = " class symbol_table { \n "
" public: \n "
" typedef expression_error::error_code (*valid_func)(void *cbparam, const char *name, expression_space space); \n "
" valid_func f; \n "
2015-03-14 20:42:44 +01:00
" }; " ;
2014-09-24 13:23:44 +02:00
const char expected [ ] = " class symbol_table { "
" public: "
2023-02-26 18:03:24 +01:00
" expression_error :: error_code ( * f ) ( void * , const char * , expression_space ) ; "
2014-09-24 13:23:44 +02:00
" } ; " ;
2023-03-03 18:36:27 +01:00
ASSERT_EQUALS ( expected , tok ( code , true , cppcheck : : Platform : : Type : : Native , false ) ) ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef96 ( ) { // ticket #2886 (segmentation fault)
2015-03-14 20:42:44 +01:00
const char code [ ] = " typedef struct x { } " ;
2014-09-24 13:23:44 +02:00
ASSERT_THROW ( tok ( code ) , InternalError ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef97 ( ) { // ticket #2983 (segmentation fault)
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef x y \n "
2015-03-14 20:42:44 +01:00
" (A); y " ;
2014-09-24 13:23:44 +02:00
tok ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef99 ( ) { // ticket #2999
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef struct Fred Fred; \n "
2015-03-14 20:42:44 +01:00
" struct Fred { }; " ;
2014-09-24 13:23:44 +02:00
tok ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
const char code1 [ ] = " struct Fred { }; \n "
2015-03-14 20:42:44 +01:00
" typedef struct Fred Fred; " ;
2014-09-24 13:23:44 +02:00
tok ( code1 ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef100 ( ) { // ticket #3000
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef struct Fred { } Fred; \n "
" Fred * foo() { \n "
" Fred *fred; \n "
" fred = se_alloc(sizeof(struct Fred)); \n "
" return fred; \n "
2015-03-14 20:42:44 +01:00
" } " ;
2014-09-24 13:23:44 +02:00
tok ( code ) ;
2020-10-04 11:27:31 +02:00
ASSERT_EQUALS_WITHOUT_LINENUMBERS ( " " , errout . str ( ) ) ;
2014-09-24 13:23:44 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef101 ( ) { // ticket #3003 (segmentation fault)
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef a x[]; \n "
2015-03-14 20:42:44 +01:00
" y = x " ;
2023-03-26 17:16:33 +02:00
ASSERT_EQUALS ( " y = x " , tok ( code ) ) ;
2014-09-24 13:23:44 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef102 ( ) { // ticket #3004
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef struct { } Fred; \n "
" void foo() \n "
" { \n "
" Fred * Fred; \n "
2015-03-14 20:42:44 +01:00
" } " ;
2014-09-24 13:23:44 +02:00
tok ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef103 ( ) { // ticket #3007
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef struct { } Fred; \n "
" void foo() \n "
" { \n "
" Fred Fred; \n "
2015-03-14 20:42:44 +01:00
" } " ;
2014-09-24 13:23:44 +02:00
tok ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef104 ( ) { // ticket #3070
2015-03-14 20:42:44 +01:00
const char code [ ] = " typedef int (*in_func) (void FAR *, unsigned char FAR * FAR *); " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( " ; " , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef105 ( ) { // ticket #3616 (segmentation fault)
2015-03-14 20:42:44 +01:00
const char code [ ] = " ( int typedef char x; ){} " ;
2014-09-24 13:23:44 +02:00
tok ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef106 ( ) { // ticket #3619 (segmentation fault)
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef void f (); \n typedef { f } " ;
2019-04-15 06:37:27 +02:00
ASSERT_THROW ( tok ( code ) , InternalError ) ;
2014-09-24 13:23:44 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef107 ( ) { // ticket #3963 (bad code => segmentation fault)
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef int x[]; int main() { return x } " ;
2023-03-26 17:16:33 +02:00
ASSERT_EQUALS ( " int main ( ) { return x } " , tok ( code ) ) ;
2014-09-24 13:23:44 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef108 ( ) { // ticket #4777
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef long* GEN; \n "
" void sort_factor(GEN *y, long n) { \n "
" GEN a, b; \n "
" foo(a, b); \n "
2015-03-14 20:42:44 +01:00
" } " ;
2014-09-24 13:23:44 +02:00
const char expected [ ] = " void sort_factor ( long * * y , long n ) { "
" long * a ; long * b ; "
" foo ( a , b ) ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedef109 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef int&& rref; \n "
2019-10-04 12:30:11 +02:00
" rref var = 0; " ;
const char expected [ ] = " int && var ; var = 0 ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-27 06:29:33 +01:00
void simplifyTypedef110 ( ) {
const char code [ ] = " namespace A { \n "
" namespace B { \n "
" namespace D { \n "
" typedef int DKIPtr; \n "
" } \n "
" struct ZClass { \n "
" void set1(const A::B::D::DKIPtr& p) { \n "
" membervariable1 = p; \n "
" } \n "
" void set2(const ::A::B::D::DKIPtr& p) { \n "
" membervariable2 = p; \n "
" } \n "
" void set3(const B::D::DKIPtr& p) { \n "
" membervariable3 = p; \n "
" } \n "
" void set4(const ::B::D::DKIPtr& p) { \n "
" membervariable4 = p; \n "
" } \n "
" void set5(const C::D::DKIPtr& p) { \n "
" membervariable5 = p; \n "
" } \n "
" A::B::D::DKIPtr membervariable1; \n "
" ::A::B::D::DKIPtr membervariable2; \n "
" B::D::DKIPtr membervariable3; \n "
" ::B::D::DKIPtr membervariable4; \n "
" C::D::DKIPtr membervariable5; \n "
" }; \n "
" } \n "
" } " ;
const char expected [ ] = " namespace A { "
" namespace B { "
" struct ZClass { "
" void set1 ( const int & p ) { "
" membervariable1 = p ; "
" } "
" void set2 ( const int & p ) { "
" membervariable2 = p ; "
" } "
" void set3 ( const int & p ) { "
" membervariable3 = p ; "
" } "
" void set4 ( const :: B :: D :: DKIPtr & p ) { "
" membervariable4 = p ; "
" } "
" void set5 ( const C :: D :: DKIPtr & p ) { "
" membervariable5 = p ; "
" } "
" int membervariable1 ; "
" int membervariable2 ; "
" int membervariable3 ; "
" :: B :: D :: DKIPtr membervariable4 ; "
" C :: D :: DKIPtr membervariable5 ; "
" } ; "
" } "
" } " ;
2023-03-03 18:36:27 +01:00
ASSERT_EQUALS ( expected , tok ( code , true , cppcheck : : Platform : : Type : : Native , false ) ) ;
2014-11-27 06:29:33 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2015-07-20 07:55:22 +02:00
void simplifyTypedef111 ( ) { // ticket #6345
const char code1 [ ] = " typedef typename A B; \n "
" typedef typename B C; \n "
" typename C c; \n " ;
2019-02-09 08:34:59 +01:00
const char expected1 [ ] = " A c ; " ;
2015-07-20 07:55:22 +02:00
ASSERT_EQUALS ( expected1 , tok ( code1 ) ) ;
const char code2 [ ] = " typedef typename A B; \n "
" typedef typename B C; \n "
" C c; \n " ;
2019-02-09 08:34:59 +01:00
const char expected2 [ ] = " A c ; " ;
2015-07-20 07:55:22 +02:00
ASSERT_EQUALS ( expected2 , tok ( code2 ) ) ;
const char code3 [ ] = " typedef typename A B; \n "
" typedef B C; \n "
" C c; \n " ;
2019-02-09 08:34:59 +01:00
const char expected3 [ ] = " A c ; " ;
2015-07-20 07:55:22 +02:00
ASSERT_EQUALS ( expected3 , tok ( code3 ) ) ;
const char code4 [ ] = " typedef A B; \n "
" typedef typename B C; \n "
" C c; \n " ;
2019-02-09 08:34:59 +01:00
const char expected4 [ ] = " A c ; " ;
2015-07-20 07:55:22 +02:00
ASSERT_EQUALS ( expected4 , tok ( code4 ) ) ;
const char code5 [ ] = " typedef A B; \n "
" typedef B C; \n "
" C c; \n " ;
const char expected5 [ ] = " A c ; " ;
ASSERT_EQUALS ( expected5 , tok ( code5 ) ) ;
2017-08-30 19:18:05 +02:00
// #5614
const char code5614 [ ] = " typedef typename T::U V; \n "
" typedef typename T::W (V::*Fn)(); \n " ;
const char expected5614 [ ] = " ; " ;
ASSERT_EQUALS ( expected5614 , tok ( code5614 ) ) ;
2015-07-20 07:55:22 +02:00
}
2015-07-26 15:42:40 +02:00
void simplifyTypedef112 ( ) { // ticket #6048
const char code [ ] = " template< \n "
" typename DataType, \n "
" typename SpaceType, \n "
" typename TrafoConfig> \n "
" class AsmTraits1 { \n "
" typedef typename SpaceType::TrafoType TrafoType; \n "
" typedef typename TrafoType::ShapeType ShapeType; \n "
" typedef typename TrafoType::template Evaluator<ShapeType, DataType>::Type TrafoEvaluator; \n "
" enum { \n "
" domain_dim = TrafoEvaluator::domain_dim, \n "
" }; \n "
" }; " ;
2015-09-06 18:37:22 +02:00
2016-04-22 06:02:54 +02:00
const char expected [ ] = " template < "
" typename DataType , "
" typename SpaceType , "
" typename TrafoConfig > "
" class AsmTraits1 { "
" enum Anonymous0 { "
2020-11-29 08:46:42 +01:00
" domain_dim = SpaceType :: TrafoType :: Evaluator < SpaceType :: TrafoType :: ShapeType , DataType > :: Type :: domain_dim , "
2016-04-22 06:02:54 +02:00
" } ; } ; " ;
2015-09-06 18:37:22 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-07-26 15:42:40 +02:00
}
2015-10-15 16:56:22 +02:00
void simplifyTypedef113 ( ) { // ticket #7030
const char code [ ] = " typedef int T; \n "
" void f() { T:; } " ;
const char expected [ ] = " void f ( ) { T : ; } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2015-10-18 04:55:04 +02:00
void simplifyTypedef114 ( ) { // ticket #7058
const char code [ ] = " typedef struct { enum {A,B}; } AB; \n "
" x=AB::B; " ;
2016-04-22 06:02:54 +02:00
const char expected [ ] = " struct AB { enum Anonymous0 { A , B } ; } ; x = AB :: B ; " ;
2015-10-18 04:55:04 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2015-10-24 10:08:19 +02:00
void simplifyTypedef115 ( ) { // ticket #6998
const char code [ ] = " typedef unsigned unsignedTypedef; \n "
" unsignedTypedef t1 ; \n "
" unsigned t2 ; " ;
const char expected [ ] = " unsigned int t1 ; "
" unsigned int t2 ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2015-10-26 10:06:52 +01:00
void simplifyTypedef116 ( ) { // #5624
const char code [ ] = " void fn() { \n "
" typedef std::vector<CharacterConversion> CharacterToConversion; \n "
" CharacterToConversion c2c; \n "
" for (CharacterToConversion::iterator it = c2c.begin(); it != c2c.end(); ++it) {} \n "
" CharacterToConversion().swap(c2c); \n "
" } " ;
const char expected [ ] = " void fn ( ) { "
" std :: vector < CharacterConversion > c2c ; "
" for ( std :: vector < CharacterConversion > :: iterator it = c2c . begin ( ) ; it != c2c . end ( ) ; ++ it ) { } "
" std :: vector < CharacterConversion > ( ) . swap ( c2c ) ; "
" } " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
2022-04-28 10:48:37 +02:00
ASSERT_EQUALS_WITHOUT_LINENUMBERS ( " " , errout . str ( ) ) ;
2015-10-26 10:06:52 +01:00
}
void simplifyTypedef117 ( ) { // #6507
const char code [ ] = " typedef struct bstr {} bstr; \n "
" struct bstr bstr0(const char *s) { \n "
" return (struct bstr) { (unsigned char *)s, s ? strlen(s) : 0 }; \n "
" } " ;
const char expected [ ] = " struct bstr { } ; "
" struct bstr bstr0 ( const char * s ) { "
" return ( struct bstr ) { ( unsigned char * ) s , s ? strlen ( s ) : 0 } ; "
" } " ;
2020-03-08 16:45:51 +01:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-26 10:06:52 +01:00
}
2016-04-22 15:13:05 +02:00
void simplifyTypedef118 ( ) { // #5749
const char code [ ] = " struct ClassyClass { \n "
2016-04-25 11:12:35 +02:00
" int id; \n "
" typedef int (ClassyClass::*funky_type); \n "
" operator funky_type() { \n "
" return &ClassyClass::id; \n "
" }} " ;
2016-04-22 15:13:05 +02:00
const char expected [ ] = " struct ClassyClass { "
2016-04-25 11:12:35 +02:00
" int id ; "
" operatorintClassyClass::* ( ) { "
" return & ClassyClass :: id ; "
" } } " ;
2016-04-22 15:13:05 +02:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2016-10-01 20:46:33 +02:00
void simplifyTypedef119 ( ) { // #7541
const char code [ ] = " namespace Baz { \n "
" typedef char* T1; \n "
" typedef T1 XX; \n "
" } \n "
" namespace Baz { } \n "
" enum Bar { XX = 1 }; " ;
2021-08-07 20:51:18 +02:00
const char exp [ ] = " enum Bar { XX = 1 } ; " ;
2016-10-01 20:46:33 +02:00
ASSERT_EQUALS ( exp , tok ( code , false ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2018-01-22 08:06:56 +01:00
void simplifyTypedef120 ( ) { // #8357
const char code [ ] = " typedef char test_utf8_char[5]; \n "
" static test_utf8_char const bad_chars[] = { }; \n "
" static void report_good(bool passed, test_utf8_char const c) { }; " ;
2021-08-07 20:51:18 +02:00
const char exp [ ] = " static const char bad_chars [ ] [ 5 ] = { } ; "
" static void report_good ( bool passed , const char c [ 5 ] ) { } ; " ;
2018-01-22 08:06:56 +01:00
ASSERT_EQUALS ( exp , tok ( code , false ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2018-01-24 09:51:22 +01:00
void simplifyTypedef121 ( ) { // #5766
const char code [ ] = " typedef float vec3[3]; \n "
" typedef float mat3x3[3][3]; \n "
" vec3 v3; \n "
" mat3x3 m3x3; \n "
" const vec3 &gv() { return v3; } \n "
" const mat3x3 &gm() { return m3x3; } \n "
" class Fred { \n "
" public: \n "
" vec3 &v(); \n "
" mat3x3 &m(); \n "
" const vec3 &vc() const; \n "
" const mat3x3 &mc() const; \n "
" }; \n "
" vec3 & Fred::v() { return v3; } \n "
" mat3x3 & Fred::m() { return m3x3; } \n "
" const vec3 & Fred::vc() const { return v3; } \n "
" const mat3x3 & Fred::mc() const { return m3x3; } " ;
2021-08-07 20:51:18 +02:00
const char exp [ ] = " float v3 [ 3 ] ; "
" float m3x3 [ 3 ] [ 3 ] ; "
2023-03-26 17:16:33 +02:00
" const float * & gv ( ) { return v3 ; } "
" const float * * & gm ( ) { return m3x3 ; } "
2021-08-07 20:51:18 +02:00
" class Fred { "
" public: "
2023-03-26 17:16:33 +02:00
" float * & v ( ) ; "
" float * * & m ( ) ; "
" const float * & vc ( ) const ; "
" const float * * & mc ( ) const ; "
2021-08-07 20:51:18 +02:00
" } ; "
2023-03-26 17:16:33 +02:00
" float * & Fred :: v ( ) { return v3 ; } "
" float * * & Fred :: m ( ) { return m3x3 ; } "
" const float * & Fred :: vc ( ) const { return v3 ; } "
" const float * * & Fred :: mc ( ) const { return m3x3 ; } " ;
2018-01-24 09:51:22 +01:00
ASSERT_EQUALS ( exp , tok ( code , false ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2018-01-24 15:04:33 +01:00
void simplifyTypedef122 ( ) { // segmentation fault
const char code [ ] = " int result = [] { return git_run_cmd( \" update-index \" , \" update-index -q --refresh \" ); }(); " ;
tok ( code ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2018-05-22 17:31:58 +02:00
void simplifyTypedef123 ( ) { // ticket #7406
const char code [ ] = " typedef int intvec[1]; \n "
" Dummy<intvec> y; " ;
2021-08-07 20:51:18 +02:00
const char exp [ ] = " Dummy < int [ 1 ] > y ; " ;
2018-05-22 17:31:58 +02:00
ASSERT_EQUALS ( exp , tok ( code , false ) ) ;
2020-05-30 11:23:22 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-05-22 17:31:58 +02:00
}
2018-08-16 14:30:28 +02:00
void simplifyTypedef124 ( ) { // ticket #7792
const char code [ ] = " typedef long unsigned int size_t; \n "
2022-10-22 00:28:05 +02:00
" typedef size_t (my_func)(char *, size_t, size_t, void *); "
" size_t f(size_t s); " ;
2018-08-16 14:30:28 +02:00
2022-10-22 00:28:05 +02:00
const char exp [ ] = " long f ( long s ) ; " ;
ASSERT_EQUALS ( exp , tok ( code , /*simplify*/ true ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-08-16 14:30:28 +02:00
const char code1 [ ] = " typedef long unsigned int uint32_t; \n "
" typedef uint32_t (my_func)(char *, uint32_t, uint32_t, void *); " ;
// Check for output..
checkSimplifyTypedef ( code1 ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2018-05-22 17:31:58 +02:00
2018-10-21 21:15:34 +02:00
void simplifyTypedef125 ( ) { // #8749
const char code [ ] = " typedef char A[3]; \n "
" char (*p)[3] = new A[4]; " ;
2021-08-07 20:51:18 +02:00
const char exp [ ] = " char ( * p ) [ 3 ] = new char [ 4 ] [ 3 ] ; " ;
2018-10-21 21:15:34 +02:00
ASSERT_EQUALS ( exp , tok ( code , false ) ) ;
}
2019-01-05 19:42:04 +01:00
void simplifyTypedef126 ( ) { // #5953
const char code [ ] = " typedef char automap_data_t[100]; \n "
" void write_array(automap_data_t *data) {} " ;
2021-08-07 20:51:18 +02:00
const char exp [ ] = " void write_array ( char ( * data ) [ 100 ] ) { } " ;
2019-01-05 19:42:04 +01:00
ASSERT_EQUALS ( exp , tok ( code , false ) ) ;
}
2019-02-10 09:45:33 +01:00
void simplifyTypedef127 ( ) { // #8878
const char code [ ] = " class a; typedef int (a::*b); "
" template <long, class> struct c; "
" template <int g> struct d { enum { e = c<g, b>::f }; }; " ;
2021-08-07 20:51:18 +02:00
const char exp [ ] = " class a ; "
" template < long , class > struct c ; "
" template < int g > struct d { enum Anonymous0 { e = c < g , int ( a :: * ) > :: f } ; } ; " ;
2019-02-10 09:45:33 +01:00
ASSERT_EQUALS ( exp , tok ( code , false ) ) ;
}
2019-03-23 10:45:38 +01:00
void simplifyTypedef128 ( ) { // #9053
const char code [ ] = " typedef int d[4]; \n "
" void f() { \n "
" dostuff((const d){1,2,3,4}); \n "
" } " ;
2021-08-07 20:51:18 +02:00
const char exp [ ] = " void f ( ) { "
" dostuff ( ( const int [ 4 ] ) { 1 , 2 , 3 , 4 } ) ; "
" } " ;
2019-03-23 10:45:38 +01:00
ASSERT_EQUALS ( exp , tok ( code , false ) ) ;
}
2019-10-14 19:41:51 +02:00
void simplifyTypedef129 ( ) {
Better handle const/noexcept methods (#2211)
* Better handle const/noexcept methods/conversion operator
const or noexcept in a method / (conversion) operator definition were
badly parsed, ending in a bad ast.
This patch tries to make it better, at least making the ast less bad,
so as to avoid errors in later checks.
* Fix parsing of some operator
It is still very broken, but at least, it does not fail.
Here is the previous error:
```
TestSimplifyTypedef::simplifyTypedef129
terminate called after throwing an instance of 'InternalError'
Program received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
51 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1 0x00007ffff612a801 in __GI_abort () at abort.c:79
#2 0x00007ffff6b1d957 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00007ffff6b23ab6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007ffff6b23af1 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007ffff6b23d24 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x0000555556366bf8 in Tokenizer::cppcheckError (this=0x7fffffffc2d0, tok=0x607000006760) at ../lib/tokenize.cpp:8721
#7 0x000055555636a4bb in Tokenizer::validate (this=0x7fffffffc2d0) at ../lib/tokenize.cpp:9154
#8 0x000055555633e3aa in Tokenizer::simplifyTokenList1 (this=0x7fffffffc2d0, FileName=0x603000002d50 "test.cpp") at ../lib/tokenize.cpp:4477
#9 0x00005555563223ca in Tokenizer::simplifyTokens1 (this=0x7fffffffc2d0, configuration="") at ../lib/tokenize.cpp:2286
#10 0x00005555563235c8 in Tokenizer::tokenize (this=0x7fffffffc2d0, code=..., FileName=0x555556fda9a0 "test.cpp", configuration="") at ../lib/tokenize.cpp:2345
#11 0x00005555569410ea in TestSimplifyTypedef::tok[abi:cxx11](char const*, bool, cppcheck::Platform::PlatformType, bool) (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>,
code=0x7fffffffcb70 "class c {\n typedef char foo[4];\n foo _a;\n constexpr operator foo &() const noexcept { return _a; }\n};", simplify=false, type=cppcheck::Platform::Native, debugwarnings=true) at ../test/testsimplifytypedef.cpp:192
#12 0x000055555697239e in TestSimplifyTypedef::simplifyTypedef129 (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:2599
#13 0x000055555694092c in TestSimplifyTypedef::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:167
#14 0x00005555569cab84 in TestFixture::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>, str="simplifyTypedef129") at ../test/testsuite.cpp:306
#15 0x00005555569cb445 in TestFixture::runTests (args=...) at ../test/testsuite.cpp:329
#16 0x000055555687bdfb in main (argc=2, argv=0x7fffffffd988) at ../test/testrunner.cpp:44
```
* Replace some ASSERT_EQUALS with TODO_ASSERT_EQUALS when the actual result is still wrong
* Remove invalid code from simplifyTypedef129
* Properly skip parentheses
2019-10-12 11:46:00 +02:00
{
2019-10-14 19:41:51 +02:00
const char code [ ] = " class c { \n "
" typedef char foo[4]; \n "
" foo &f ; \n "
" }; " ;
Better handle const/noexcept methods (#2211)
* Better handle const/noexcept methods/conversion operator
const or noexcept in a method / (conversion) operator definition were
badly parsed, ending in a bad ast.
This patch tries to make it better, at least making the ast less bad,
so as to avoid errors in later checks.
* Fix parsing of some operator
It is still very broken, but at least, it does not fail.
Here is the previous error:
```
TestSimplifyTypedef::simplifyTypedef129
terminate called after throwing an instance of 'InternalError'
Program received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
51 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1 0x00007ffff612a801 in __GI_abort () at abort.c:79
#2 0x00007ffff6b1d957 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00007ffff6b23ab6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007ffff6b23af1 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007ffff6b23d24 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x0000555556366bf8 in Tokenizer::cppcheckError (this=0x7fffffffc2d0, tok=0x607000006760) at ../lib/tokenize.cpp:8721
#7 0x000055555636a4bb in Tokenizer::validate (this=0x7fffffffc2d0) at ../lib/tokenize.cpp:9154
#8 0x000055555633e3aa in Tokenizer::simplifyTokenList1 (this=0x7fffffffc2d0, FileName=0x603000002d50 "test.cpp") at ../lib/tokenize.cpp:4477
#9 0x00005555563223ca in Tokenizer::simplifyTokens1 (this=0x7fffffffc2d0, configuration="") at ../lib/tokenize.cpp:2286
#10 0x00005555563235c8 in Tokenizer::tokenize (this=0x7fffffffc2d0, code=..., FileName=0x555556fda9a0 "test.cpp", configuration="") at ../lib/tokenize.cpp:2345
#11 0x00005555569410ea in TestSimplifyTypedef::tok[abi:cxx11](char const*, bool, cppcheck::Platform::PlatformType, bool) (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>,
code=0x7fffffffcb70 "class c {\n typedef char foo[4];\n foo _a;\n constexpr operator foo &() const noexcept { return _a; }\n};", simplify=false, type=cppcheck::Platform::Native, debugwarnings=true) at ../test/testsimplifytypedef.cpp:192
#12 0x000055555697239e in TestSimplifyTypedef::simplifyTypedef129 (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:2599
#13 0x000055555694092c in TestSimplifyTypedef::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:167
#14 0x00005555569cab84 in TestFixture::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>, str="simplifyTypedef129") at ../test/testsuite.cpp:306
#15 0x00005555569cb445 in TestFixture::runTests (args=...) at ../test/testsuite.cpp:329
#16 0x000055555687bdfb in main (argc=2, argv=0x7fffffffd988) at ../test/testrunner.cpp:44
```
* Replace some ASSERT_EQUALS with TODO_ASSERT_EQUALS when the actual result is still wrong
* Remove invalid code from simplifyTypedef129
* Properly skip parentheses
2019-10-12 11:46:00 +02:00
2021-08-07 20:51:18 +02:00
const char exp [ ] = " class c { char ( & f ) [ 4 ] ; } ; " ;
Better handle const/noexcept methods (#2211)
* Better handle const/noexcept methods/conversion operator
const or noexcept in a method / (conversion) operator definition were
badly parsed, ending in a bad ast.
This patch tries to make it better, at least making the ast less bad,
so as to avoid errors in later checks.
* Fix parsing of some operator
It is still very broken, but at least, it does not fail.
Here is the previous error:
```
TestSimplifyTypedef::simplifyTypedef129
terminate called after throwing an instance of 'InternalError'
Program received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
51 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1 0x00007ffff612a801 in __GI_abort () at abort.c:79
#2 0x00007ffff6b1d957 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00007ffff6b23ab6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007ffff6b23af1 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007ffff6b23d24 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x0000555556366bf8 in Tokenizer::cppcheckError (this=0x7fffffffc2d0, tok=0x607000006760) at ../lib/tokenize.cpp:8721
#7 0x000055555636a4bb in Tokenizer::validate (this=0x7fffffffc2d0) at ../lib/tokenize.cpp:9154
#8 0x000055555633e3aa in Tokenizer::simplifyTokenList1 (this=0x7fffffffc2d0, FileName=0x603000002d50 "test.cpp") at ../lib/tokenize.cpp:4477
#9 0x00005555563223ca in Tokenizer::simplifyTokens1 (this=0x7fffffffc2d0, configuration="") at ../lib/tokenize.cpp:2286
#10 0x00005555563235c8 in Tokenizer::tokenize (this=0x7fffffffc2d0, code=..., FileName=0x555556fda9a0 "test.cpp", configuration="") at ../lib/tokenize.cpp:2345
#11 0x00005555569410ea in TestSimplifyTypedef::tok[abi:cxx11](char const*, bool, cppcheck::Platform::PlatformType, bool) (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>,
code=0x7fffffffcb70 "class c {\n typedef char foo[4];\n foo _a;\n constexpr operator foo &() const noexcept { return _a; }\n};", simplify=false, type=cppcheck::Platform::Native, debugwarnings=true) at ../test/testsimplifytypedef.cpp:192
#12 0x000055555697239e in TestSimplifyTypedef::simplifyTypedef129 (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:2599
#13 0x000055555694092c in TestSimplifyTypedef::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:167
#14 0x00005555569cab84 in TestFixture::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>, str="simplifyTypedef129") at ../test/testsuite.cpp:306
#15 0x00005555569cb445 in TestFixture::runTests (args=...) at ../test/testsuite.cpp:329
#16 0x000055555687bdfb in main (argc=2, argv=0x7fffffffd988) at ../test/testrunner.cpp:44
```
* Replace some ASSERT_EQUALS with TODO_ASSERT_EQUALS when the actual result is still wrong
* Remove invalid code from simplifyTypedef129
* Properly skip parentheses
2019-10-12 11:46:00 +02:00
ASSERT_EQUALS ( exp , tok ( code , false ) ) ;
}
{
2019-10-14 19:41:51 +02:00
const char code [ ] = " class c { \n "
" typedef char foo[4]; \n "
" const foo &f; \n "
" }; " ;
Better handle const/noexcept methods (#2211)
* Better handle const/noexcept methods/conversion operator
const or noexcept in a method / (conversion) operator definition were
badly parsed, ending in a bad ast.
This patch tries to make it better, at least making the ast less bad,
so as to avoid errors in later checks.
* Fix parsing of some operator
It is still very broken, but at least, it does not fail.
Here is the previous error:
```
TestSimplifyTypedef::simplifyTypedef129
terminate called after throwing an instance of 'InternalError'
Program received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
51 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1 0x00007ffff612a801 in __GI_abort () at abort.c:79
#2 0x00007ffff6b1d957 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00007ffff6b23ab6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007ffff6b23af1 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007ffff6b23d24 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x0000555556366bf8 in Tokenizer::cppcheckError (this=0x7fffffffc2d0, tok=0x607000006760) at ../lib/tokenize.cpp:8721
#7 0x000055555636a4bb in Tokenizer::validate (this=0x7fffffffc2d0) at ../lib/tokenize.cpp:9154
#8 0x000055555633e3aa in Tokenizer::simplifyTokenList1 (this=0x7fffffffc2d0, FileName=0x603000002d50 "test.cpp") at ../lib/tokenize.cpp:4477
#9 0x00005555563223ca in Tokenizer::simplifyTokens1 (this=0x7fffffffc2d0, configuration="") at ../lib/tokenize.cpp:2286
#10 0x00005555563235c8 in Tokenizer::tokenize (this=0x7fffffffc2d0, code=..., FileName=0x555556fda9a0 "test.cpp", configuration="") at ../lib/tokenize.cpp:2345
#11 0x00005555569410ea in TestSimplifyTypedef::tok[abi:cxx11](char const*, bool, cppcheck::Platform::PlatformType, bool) (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>,
code=0x7fffffffcb70 "class c {\n typedef char foo[4];\n foo _a;\n constexpr operator foo &() const noexcept { return _a; }\n};", simplify=false, type=cppcheck::Platform::Native, debugwarnings=true) at ../test/testsimplifytypedef.cpp:192
#12 0x000055555697239e in TestSimplifyTypedef::simplifyTypedef129 (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:2599
#13 0x000055555694092c in TestSimplifyTypedef::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:167
#14 0x00005555569cab84 in TestFixture::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>, str="simplifyTypedef129") at ../test/testsuite.cpp:306
#15 0x00005555569cb445 in TestFixture::runTests (args=...) at ../test/testsuite.cpp:329
#16 0x000055555687bdfb in main (argc=2, argv=0x7fffffffd988) at ../test/testrunner.cpp:44
```
* Replace some ASSERT_EQUALS with TODO_ASSERT_EQUALS when the actual result is still wrong
* Remove invalid code from simplifyTypedef129
* Properly skip parentheses
2019-10-12 11:46:00 +02:00
2021-08-07 20:51:18 +02:00
const char exp [ ] = " class c { const char ( & f ) [ 4 ] ; } ; " ;
Better handle const/noexcept methods (#2211)
* Better handle const/noexcept methods/conversion operator
const or noexcept in a method / (conversion) operator definition were
badly parsed, ending in a bad ast.
This patch tries to make it better, at least making the ast less bad,
so as to avoid errors in later checks.
* Fix parsing of some operator
It is still very broken, but at least, it does not fail.
Here is the previous error:
```
TestSimplifyTypedef::simplifyTypedef129
terminate called after throwing an instance of 'InternalError'
Program received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
51 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1 0x00007ffff612a801 in __GI_abort () at abort.c:79
#2 0x00007ffff6b1d957 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00007ffff6b23ab6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007ffff6b23af1 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007ffff6b23d24 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x0000555556366bf8 in Tokenizer::cppcheckError (this=0x7fffffffc2d0, tok=0x607000006760) at ../lib/tokenize.cpp:8721
#7 0x000055555636a4bb in Tokenizer::validate (this=0x7fffffffc2d0) at ../lib/tokenize.cpp:9154
#8 0x000055555633e3aa in Tokenizer::simplifyTokenList1 (this=0x7fffffffc2d0, FileName=0x603000002d50 "test.cpp") at ../lib/tokenize.cpp:4477
#9 0x00005555563223ca in Tokenizer::simplifyTokens1 (this=0x7fffffffc2d0, configuration="") at ../lib/tokenize.cpp:2286
#10 0x00005555563235c8 in Tokenizer::tokenize (this=0x7fffffffc2d0, code=..., FileName=0x555556fda9a0 "test.cpp", configuration="") at ../lib/tokenize.cpp:2345
#11 0x00005555569410ea in TestSimplifyTypedef::tok[abi:cxx11](char const*, bool, cppcheck::Platform::PlatformType, bool) (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>,
code=0x7fffffffcb70 "class c {\n typedef char foo[4];\n foo _a;\n constexpr operator foo &() const noexcept { return _a; }\n};", simplify=false, type=cppcheck::Platform::Native, debugwarnings=true) at ../test/testsimplifytypedef.cpp:192
#12 0x000055555697239e in TestSimplifyTypedef::simplifyTypedef129 (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:2599
#13 0x000055555694092c in TestSimplifyTypedef::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:167
#14 0x00005555569cab84 in TestFixture::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>, str="simplifyTypedef129") at ../test/testsuite.cpp:306
#15 0x00005555569cb445 in TestFixture::runTests (args=...) at ../test/testsuite.cpp:329
#16 0x000055555687bdfb in main (argc=2, argv=0x7fffffffd988) at ../test/testrunner.cpp:44
```
* Replace some ASSERT_EQUALS with TODO_ASSERT_EQUALS when the actual result is still wrong
* Remove invalid code from simplifyTypedef129
* Properly skip parentheses
2019-10-12 11:46:00 +02:00
ASSERT_EQUALS ( exp , tok ( code , false ) ) ;
}
{
2019-10-14 19:41:51 +02:00
const char code [ ] = " class c { \n "
" typedef char foo[4]; \n "
" foo _a; \n "
" constexpr const foo &c_str() const noexcept { return _a; } \n "
" }; " ;
Better handle const/noexcept methods (#2211)
* Better handle const/noexcept methods/conversion operator
const or noexcept in a method / (conversion) operator definition were
badly parsed, ending in a bad ast.
This patch tries to make it better, at least making the ast less bad,
so as to avoid errors in later checks.
* Fix parsing of some operator
It is still very broken, but at least, it does not fail.
Here is the previous error:
```
TestSimplifyTypedef::simplifyTypedef129
terminate called after throwing an instance of 'InternalError'
Program received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
51 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1 0x00007ffff612a801 in __GI_abort () at abort.c:79
#2 0x00007ffff6b1d957 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00007ffff6b23ab6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007ffff6b23af1 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007ffff6b23d24 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x0000555556366bf8 in Tokenizer::cppcheckError (this=0x7fffffffc2d0, tok=0x607000006760) at ../lib/tokenize.cpp:8721
#7 0x000055555636a4bb in Tokenizer::validate (this=0x7fffffffc2d0) at ../lib/tokenize.cpp:9154
#8 0x000055555633e3aa in Tokenizer::simplifyTokenList1 (this=0x7fffffffc2d0, FileName=0x603000002d50 "test.cpp") at ../lib/tokenize.cpp:4477
#9 0x00005555563223ca in Tokenizer::simplifyTokens1 (this=0x7fffffffc2d0, configuration="") at ../lib/tokenize.cpp:2286
#10 0x00005555563235c8 in Tokenizer::tokenize (this=0x7fffffffc2d0, code=..., FileName=0x555556fda9a0 "test.cpp", configuration="") at ../lib/tokenize.cpp:2345
#11 0x00005555569410ea in TestSimplifyTypedef::tok[abi:cxx11](char const*, bool, cppcheck::Platform::PlatformType, bool) (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>,
code=0x7fffffffcb70 "class c {\n typedef char foo[4];\n foo _a;\n constexpr operator foo &() const noexcept { return _a; }\n};", simplify=false, type=cppcheck::Platform::Native, debugwarnings=true) at ../test/testsimplifytypedef.cpp:192
#12 0x000055555697239e in TestSimplifyTypedef::simplifyTypedef129 (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:2599
#13 0x000055555694092c in TestSimplifyTypedef::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:167
#14 0x00005555569cab84 in TestFixture::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>, str="simplifyTypedef129") at ../test/testsuite.cpp:306
#15 0x00005555569cb445 in TestFixture::runTests (args=...) at ../test/testsuite.cpp:329
#16 0x000055555687bdfb in main (argc=2, argv=0x7fffffffd988) at ../test/testrunner.cpp:44
```
* Replace some ASSERT_EQUALS with TODO_ASSERT_EQUALS when the actual result is still wrong
* Remove invalid code from simplifyTypedef129
* Properly skip parentheses
2019-10-12 11:46:00 +02:00
2022-08-07 20:06:32 +02:00
const char exp [ ] = " class c { char _a [ 4 ] ; const constexpr char ( & c_str ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ; " ;
Better handle const/noexcept methods (#2211)
* Better handle const/noexcept methods/conversion operator
const or noexcept in a method / (conversion) operator definition were
badly parsed, ending in a bad ast.
This patch tries to make it better, at least making the ast less bad,
so as to avoid errors in later checks.
* Fix parsing of some operator
It is still very broken, but at least, it does not fail.
Here is the previous error:
```
TestSimplifyTypedef::simplifyTypedef129
terminate called after throwing an instance of 'InternalError'
Program received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
51 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1 0x00007ffff612a801 in __GI_abort () at abort.c:79
#2 0x00007ffff6b1d957 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00007ffff6b23ab6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007ffff6b23af1 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007ffff6b23d24 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x0000555556366bf8 in Tokenizer::cppcheckError (this=0x7fffffffc2d0, tok=0x607000006760) at ../lib/tokenize.cpp:8721
#7 0x000055555636a4bb in Tokenizer::validate (this=0x7fffffffc2d0) at ../lib/tokenize.cpp:9154
#8 0x000055555633e3aa in Tokenizer::simplifyTokenList1 (this=0x7fffffffc2d0, FileName=0x603000002d50 "test.cpp") at ../lib/tokenize.cpp:4477
#9 0x00005555563223ca in Tokenizer::simplifyTokens1 (this=0x7fffffffc2d0, configuration="") at ../lib/tokenize.cpp:2286
#10 0x00005555563235c8 in Tokenizer::tokenize (this=0x7fffffffc2d0, code=..., FileName=0x555556fda9a0 "test.cpp", configuration="") at ../lib/tokenize.cpp:2345
#11 0x00005555569410ea in TestSimplifyTypedef::tok[abi:cxx11](char const*, bool, cppcheck::Platform::PlatformType, bool) (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>,
code=0x7fffffffcb70 "class c {\n typedef char foo[4];\n foo _a;\n constexpr operator foo &() const noexcept { return _a; }\n};", simplify=false, type=cppcheck::Platform::Native, debugwarnings=true) at ../test/testsimplifytypedef.cpp:192
#12 0x000055555697239e in TestSimplifyTypedef::simplifyTypedef129 (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:2599
#13 0x000055555694092c in TestSimplifyTypedef::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:167
#14 0x00005555569cab84 in TestFixture::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>, str="simplifyTypedef129") at ../test/testsuite.cpp:306
#15 0x00005555569cb445 in TestFixture::runTests (args=...) at ../test/testsuite.cpp:329
#16 0x000055555687bdfb in main (argc=2, argv=0x7fffffffd988) at ../test/testrunner.cpp:44
```
* Replace some ASSERT_EQUALS with TODO_ASSERT_EQUALS when the actual result is still wrong
* Remove invalid code from simplifyTypedef129
* Properly skip parentheses
2019-10-12 11:46:00 +02:00
ASSERT_EQUALS ( exp , tok ( code , false ) ) ;
}
{
2019-10-14 19:41:51 +02:00
const char code [ ] = " class c { \n "
" typedef char foo[4]; \n "
" foo _a; \n "
" constexpr operator foo &() const noexcept { return _a; } \n "
" }; " ;
Better handle const/noexcept methods (#2211)
* Better handle const/noexcept methods/conversion operator
const or noexcept in a method / (conversion) operator definition were
badly parsed, ending in a bad ast.
This patch tries to make it better, at least making the ast less bad,
so as to avoid errors in later checks.
* Fix parsing of some operator
It is still very broken, but at least, it does not fail.
Here is the previous error:
```
TestSimplifyTypedef::simplifyTypedef129
terminate called after throwing an instance of 'InternalError'
Program received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
51 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1 0x00007ffff612a801 in __GI_abort () at abort.c:79
#2 0x00007ffff6b1d957 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00007ffff6b23ab6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007ffff6b23af1 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007ffff6b23d24 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x0000555556366bf8 in Tokenizer::cppcheckError (this=0x7fffffffc2d0, tok=0x607000006760) at ../lib/tokenize.cpp:8721
#7 0x000055555636a4bb in Tokenizer::validate (this=0x7fffffffc2d0) at ../lib/tokenize.cpp:9154
#8 0x000055555633e3aa in Tokenizer::simplifyTokenList1 (this=0x7fffffffc2d0, FileName=0x603000002d50 "test.cpp") at ../lib/tokenize.cpp:4477
#9 0x00005555563223ca in Tokenizer::simplifyTokens1 (this=0x7fffffffc2d0, configuration="") at ../lib/tokenize.cpp:2286
#10 0x00005555563235c8 in Tokenizer::tokenize (this=0x7fffffffc2d0, code=..., FileName=0x555556fda9a0 "test.cpp", configuration="") at ../lib/tokenize.cpp:2345
#11 0x00005555569410ea in TestSimplifyTypedef::tok[abi:cxx11](char const*, bool, cppcheck::Platform::PlatformType, bool) (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>,
code=0x7fffffffcb70 "class c {\n typedef char foo[4];\n foo _a;\n constexpr operator foo &() const noexcept { return _a; }\n};", simplify=false, type=cppcheck::Platform::Native, debugwarnings=true) at ../test/testsimplifytypedef.cpp:192
#12 0x000055555697239e in TestSimplifyTypedef::simplifyTypedef129 (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:2599
#13 0x000055555694092c in TestSimplifyTypedef::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:167
#14 0x00005555569cab84 in TestFixture::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>, str="simplifyTypedef129") at ../test/testsuite.cpp:306
#15 0x00005555569cb445 in TestFixture::runTests (args=...) at ../test/testsuite.cpp:329
#16 0x000055555687bdfb in main (argc=2, argv=0x7fffffffd988) at ../test/testrunner.cpp:44
```
* Replace some ASSERT_EQUALS with TODO_ASSERT_EQUALS when the actual result is still wrong
* Remove invalid code from simplifyTypedef129
* Properly skip parentheses
2019-10-12 11:46:00 +02:00
2022-08-07 20:06:32 +02:00
const char actual [ ] = " class c { char _a [ 4 ] ; constexpr operatorchar ( & ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ; " ;
const char exp [ ] = " class c { char _a [ 4 ] ; const operator char ( & ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ; " ;
Better handle const/noexcept methods (#2211)
* Better handle const/noexcept methods/conversion operator
const or noexcept in a method / (conversion) operator definition were
badly parsed, ending in a bad ast.
This patch tries to make it better, at least making the ast less bad,
so as to avoid errors in later checks.
* Fix parsing of some operator
It is still very broken, but at least, it does not fail.
Here is the previous error:
```
TestSimplifyTypedef::simplifyTypedef129
terminate called after throwing an instance of 'InternalError'
Program received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
51 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1 0x00007ffff612a801 in __GI_abort () at abort.c:79
#2 0x00007ffff6b1d957 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00007ffff6b23ab6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007ffff6b23af1 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007ffff6b23d24 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x0000555556366bf8 in Tokenizer::cppcheckError (this=0x7fffffffc2d0, tok=0x607000006760) at ../lib/tokenize.cpp:8721
#7 0x000055555636a4bb in Tokenizer::validate (this=0x7fffffffc2d0) at ../lib/tokenize.cpp:9154
#8 0x000055555633e3aa in Tokenizer::simplifyTokenList1 (this=0x7fffffffc2d0, FileName=0x603000002d50 "test.cpp") at ../lib/tokenize.cpp:4477
#9 0x00005555563223ca in Tokenizer::simplifyTokens1 (this=0x7fffffffc2d0, configuration="") at ../lib/tokenize.cpp:2286
#10 0x00005555563235c8 in Tokenizer::tokenize (this=0x7fffffffc2d0, code=..., FileName=0x555556fda9a0 "test.cpp", configuration="") at ../lib/tokenize.cpp:2345
#11 0x00005555569410ea in TestSimplifyTypedef::tok[abi:cxx11](char const*, bool, cppcheck::Platform::PlatformType, bool) (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>,
code=0x7fffffffcb70 "class c {\n typedef char foo[4];\n foo _a;\n constexpr operator foo &() const noexcept { return _a; }\n};", simplify=false, type=cppcheck::Platform::Native, debugwarnings=true) at ../test/testsimplifytypedef.cpp:192
#12 0x000055555697239e in TestSimplifyTypedef::simplifyTypedef129 (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:2599
#13 0x000055555694092c in TestSimplifyTypedef::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:167
#14 0x00005555569cab84 in TestFixture::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>, str="simplifyTypedef129") at ../test/testsuite.cpp:306
#15 0x00005555569cb445 in TestFixture::runTests (args=...) at ../test/testsuite.cpp:329
#16 0x000055555687bdfb in main (argc=2, argv=0x7fffffffd988) at ../test/testrunner.cpp:44
```
* Replace some ASSERT_EQUALS with TODO_ASSERT_EQUALS when the actual result is still wrong
* Remove invalid code from simplifyTypedef129
* Properly skip parentheses
2019-10-12 11:46:00 +02:00
TODO_ASSERT_EQUALS ( exp , actual , tok ( code , false ) ) ;
}
{
2019-10-14 19:41:51 +02:00
const char code [ ] = " class c { \n "
" typedef char foo[4]; \n "
" foo _a; \n "
" constexpr operator const foo &() const noexcept { return _a; } \n "
" }; " ;
Better handle const/noexcept methods (#2211)
* Better handle const/noexcept methods/conversion operator
const or noexcept in a method / (conversion) operator definition were
badly parsed, ending in a bad ast.
This patch tries to make it better, at least making the ast less bad,
so as to avoid errors in later checks.
* Fix parsing of some operator
It is still very broken, but at least, it does not fail.
Here is the previous error:
```
TestSimplifyTypedef::simplifyTypedef129
terminate called after throwing an instance of 'InternalError'
Program received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
51 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1 0x00007ffff612a801 in __GI_abort () at abort.c:79
#2 0x00007ffff6b1d957 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00007ffff6b23ab6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007ffff6b23af1 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007ffff6b23d24 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x0000555556366bf8 in Tokenizer::cppcheckError (this=0x7fffffffc2d0, tok=0x607000006760) at ../lib/tokenize.cpp:8721
#7 0x000055555636a4bb in Tokenizer::validate (this=0x7fffffffc2d0) at ../lib/tokenize.cpp:9154
#8 0x000055555633e3aa in Tokenizer::simplifyTokenList1 (this=0x7fffffffc2d0, FileName=0x603000002d50 "test.cpp") at ../lib/tokenize.cpp:4477
#9 0x00005555563223ca in Tokenizer::simplifyTokens1 (this=0x7fffffffc2d0, configuration="") at ../lib/tokenize.cpp:2286
#10 0x00005555563235c8 in Tokenizer::tokenize (this=0x7fffffffc2d0, code=..., FileName=0x555556fda9a0 "test.cpp", configuration="") at ../lib/tokenize.cpp:2345
#11 0x00005555569410ea in TestSimplifyTypedef::tok[abi:cxx11](char const*, bool, cppcheck::Platform::PlatformType, bool) (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>,
code=0x7fffffffcb70 "class c {\n typedef char foo[4];\n foo _a;\n constexpr operator foo &() const noexcept { return _a; }\n};", simplify=false, type=cppcheck::Platform::Native, debugwarnings=true) at ../test/testsimplifytypedef.cpp:192
#12 0x000055555697239e in TestSimplifyTypedef::simplifyTypedef129 (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:2599
#13 0x000055555694092c in TestSimplifyTypedef::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:167
#14 0x00005555569cab84 in TestFixture::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>, str="simplifyTypedef129") at ../test/testsuite.cpp:306
#15 0x00005555569cb445 in TestFixture::runTests (args=...) at ../test/testsuite.cpp:329
#16 0x000055555687bdfb in main (argc=2, argv=0x7fffffffd988) at ../test/testrunner.cpp:44
```
* Replace some ASSERT_EQUALS with TODO_ASSERT_EQUALS when the actual result is still wrong
* Remove invalid code from simplifyTypedef129
* Properly skip parentheses
2019-10-12 11:46:00 +02:00
2022-08-07 20:06:32 +02:00
const char actual [ ] = " class c { char _a [ 4 ] ; constexpr operatorconstchar ( & ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ; " ;
const char exp [ ] = " class c { char _a [ 4 ] ; const operator const char ( & ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ; " ;
Better handle const/noexcept methods (#2211)
* Better handle const/noexcept methods/conversion operator
const or noexcept in a method / (conversion) operator definition were
badly parsed, ending in a bad ast.
This patch tries to make it better, at least making the ast less bad,
so as to avoid errors in later checks.
* Fix parsing of some operator
It is still very broken, but at least, it does not fail.
Here is the previous error:
```
TestSimplifyTypedef::simplifyTypedef129
terminate called after throwing an instance of 'InternalError'
Program received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
51 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1 0x00007ffff612a801 in __GI_abort () at abort.c:79
#2 0x00007ffff6b1d957 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00007ffff6b23ab6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007ffff6b23af1 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007ffff6b23d24 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x0000555556366bf8 in Tokenizer::cppcheckError (this=0x7fffffffc2d0, tok=0x607000006760) at ../lib/tokenize.cpp:8721
#7 0x000055555636a4bb in Tokenizer::validate (this=0x7fffffffc2d0) at ../lib/tokenize.cpp:9154
#8 0x000055555633e3aa in Tokenizer::simplifyTokenList1 (this=0x7fffffffc2d0, FileName=0x603000002d50 "test.cpp") at ../lib/tokenize.cpp:4477
#9 0x00005555563223ca in Tokenizer::simplifyTokens1 (this=0x7fffffffc2d0, configuration="") at ../lib/tokenize.cpp:2286
#10 0x00005555563235c8 in Tokenizer::tokenize (this=0x7fffffffc2d0, code=..., FileName=0x555556fda9a0 "test.cpp", configuration="") at ../lib/tokenize.cpp:2345
#11 0x00005555569410ea in TestSimplifyTypedef::tok[abi:cxx11](char const*, bool, cppcheck::Platform::PlatformType, bool) (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>,
code=0x7fffffffcb70 "class c {\n typedef char foo[4];\n foo _a;\n constexpr operator foo &() const noexcept { return _a; }\n};", simplify=false, type=cppcheck::Platform::Native, debugwarnings=true) at ../test/testsimplifytypedef.cpp:192
#12 0x000055555697239e in TestSimplifyTypedef::simplifyTypedef129 (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:2599
#13 0x000055555694092c in TestSimplifyTypedef::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:167
#14 0x00005555569cab84 in TestFixture::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>, str="simplifyTypedef129") at ../test/testsuite.cpp:306
#15 0x00005555569cb445 in TestFixture::runTests (args=...) at ../test/testsuite.cpp:329
#16 0x000055555687bdfb in main (argc=2, argv=0x7fffffffd988) at ../test/testrunner.cpp:44
```
* Replace some ASSERT_EQUALS with TODO_ASSERT_EQUALS when the actual result is still wrong
* Remove invalid code from simplifyTypedef129
* Properly skip parentheses
2019-10-12 11:46:00 +02:00
TODO_ASSERT_EQUALS ( exp , actual , tok ( code , false ) ) ;
}
}
2019-11-01 09:11:29 +01:00
void simplifyTypedef130 ( ) { // #9446
const char code [ ] = " template <class, class> void a() { \n "
" typedef int(*b)[10]; \n "
" a<b, b>(); \n "
" } " ;
2021-08-07 20:51:18 +02:00
const char exp [ ] = " template < class , class > void a ( ) { "
" a < int ( * ) [ 10 ] , int ( * ) [ 10 ] > ( ) ; "
" } " ;
2019-11-01 09:11:29 +01:00
ASSERT_EQUALS ( exp , tok ( code , false ) ) ;
}
2020-04-08 22:40:45 +02:00
void simplifyTypedef131 ( ) {
const char code [ ] = " typedef unsigned char a4[4]; \n "
" a4 a4obj; \n "
" a4 && a4_rref = std::move(a4obj); \n "
" a4* a4p = &(a4obj); \n "
" a4*&& a4p_rref = std::move(a4p); " ;
2021-08-07 20:51:18 +02:00
const char exp [ ] = " unsigned char a4obj [ 4 ] ; "
" unsigned char ( && a4_rref ) [ 4 ] = std :: move ( a4obj ) ; "
" unsigned char ( * a4p ) [ 4 ] ; "
" a4p = & ( a4obj ) ; "
" unsigned char ( * && a4p_rref ) [ 4 ] = std :: move ( a4p ) ; " ;
2020-04-08 22:40:45 +02:00
ASSERT_EQUALS ( exp , tok ( code , false ) ) ;
}
2020-05-29 11:16:49 +02:00
void simplifyTypedef132 ( ) {
const char code [ ] = " namespace NamespaceA { \n "
" typedef int MySpecialType; \n "
" } \n "
" \n "
" class A { \n "
" void DoSomething( NamespaceA::MySpecialType special ); \n "
" }; \n "
" \n "
" using NamespaceA::MySpecialType; \n "
" \n "
" void A::DoSomething( MySpecialType wrongName ) {} " ;
2021-08-07 20:51:18 +02:00
const char exp [ ] = " class A { "
" void DoSomething ( int special ) ; "
" } ; "
" void A :: DoSomething ( int wrongName ) { } " ;
2020-05-29 11:16:49 +02:00
ASSERT_EQUALS ( exp , tok ( code , false ) ) ;
}
2020-09-14 21:59:41 +02:00
void simplifyTypedef133 ( ) { // #9812
const char code [ ] = " typedef unsigned char array_t[16]; \n "
" using array_p = const array_t *; \n "
" array_p x; \n " ;
ASSERT_EQUALS ( " using array_p = const unsigned char ( * ) [ 16 ] ; array_p x ; " , tok ( code , false ) ) ;
}
2020-11-04 13:32:31 +01:00
void simplifyTypedef134 ( ) {
const char code [ ] = " namespace foo { typedef long long int64; } \n "
" typedef int int32; \n "
" namespace foo { int64 i; } \n "
" int32 j; " ;
2023-03-26 17:16:33 +02:00
ASSERT_EQUALS ( " ; namespace foo { long long i ; } int j ; " , tok ( code , false ) ) ;
2020-11-04 13:32:31 +01:00
}
2021-01-05 16:53:56 +01:00
void simplifyTypedef135 ( ) {
const char code [ ] = " namespace clangimport { \n "
" class AstNode; \n "
" typedef std::shared_ptr<AstNode> AstNodePtr; \n "
" class AstNode { \n "
" public: \n "
" AstNode() {} \n "
" private: \n "
" void createTokens(); \n "
" void createScope(const std::vector<AstNodePtr> &children); \n "
" }; \n "
" } \n "
" void clangimport::AstNode::createTokens() { \n "
" AstNodePtr range; \n "
" range->createTokens(); \n "
" } \n "
" void clangimport::AstNode::createScope(const std::vector<AstNodePtr> & children2) { } " ;
const char expected [ ] = " namespace clangimport { "
" class AstNode ; "
" class AstNode { "
" public: "
" AstNode ( ) "
" { } "
" private: "
" void createTokens ( ) ; "
" void createScope ( const std :: vector < std :: shared_ptr < AstNode > > & children ) ; "
" } ; "
" } "
" void clangimport :: AstNode :: createTokens ( ) { "
" std :: shared_ptr < AstNode > range ; "
" range . createTokens ( ) ; "
" } "
" void clangimport :: AstNode :: createScope ( const std :: vector < std :: shared_ptr < AstNode > > & children2 ) { } " ;
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2021-01-17 16:10:53 +01:00
void simplifyTypedef136 ( ) {
const char code [ ] = " class C1 {}; \n "
" typedef class S1 {} S1; \n "
" typedef class S2 : public C1 {} S2; \n "
" typedef class {} S3; \n "
" typedef class : public C1 {} S4; \n "
" S1 s1; \n "
" S2 s2; \n "
" S3 s3; \n "
" S4 s4; " ;
const char expected [ ] = " class C1 { } ; "
" class S1 { } ; "
" class S2 : public C1 { } ; "
" class S3 { } ; "
" class S4 : public C1 { } ; "
" class S1 s1 ; "
" class S2 s2 ; "
" class S3 s3 ; "
" class S4 s4 ; " ;
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
}
2021-01-18 10:08:55 +01:00
void simplifyTypedef137 ( ) { // #10054 debug: Executable scope 'x' with unknown function.
{
// original example: using "namespace external::ns1;" but redundant qualification
const char code [ ] = " namespace external { \n "
" namespace ns1 { \n "
" template <int size> struct B { }; \n "
" typedef B<sizeof(bool)> V; \n "
" } \n "
" } \n "
" namespace ns { \n "
" struct A { \n "
" void f(external::ns1::V); \n "
" }; \n "
" } \n "
" using namespace external::ns1; \n "
" namespace ns { \n "
" void A::f(external::ns1::V) {} \n "
" } " ;
const char exp [ ] = " namespace external { "
" namespace ns1 { "
" struct B<1> ; "
" } "
" } "
" namespace ns { "
" struct A { "
" void f ( external :: ns1 :: B<1> ) ; "
" } ; "
" } "
" using namespace external :: ns1 ; "
" namespace ns { "
" void A :: f ( external :: ns1 :: B<1> ) { } "
" } "
" struct external :: ns1 :: B<1> { } ; " ;
2023-03-03 18:36:27 +01:00
ASSERT_EQUALS ( exp , tok ( code , true , cppcheck : : Platform : : Type : : Native , true ) ) ;
2021-01-18 10:08:55 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
// no using "namespace external::ns1;"
const char code [ ] = " namespace external { \n "
" namespace ns1 { \n "
" template <int size> struct B { }; \n "
" typedef B<sizeof(bool)> V; \n "
" } \n "
" } \n "
" namespace ns { \n "
" struct A { \n "
" void f(external::ns1::V); \n "
" }; \n "
" } \n "
" namespace ns { \n "
" void A::f(external::ns1::V) {} \n "
" } " ;
const char exp [ ] = " namespace external { "
" namespace ns1 { "
" struct B<1> ; "
" } "
" } "
" namespace ns { "
" struct A { "
" void f ( external :: ns1 :: B<1> ) ; "
" } ; "
" } "
" namespace ns { "
" void A :: f ( external :: ns1 :: B<1> ) { } "
" } "
" struct external :: ns1 :: B<1> { } ; " ;
2023-03-03 18:36:27 +01:00
ASSERT_EQUALS ( exp , tok ( code , true , cppcheck : : Platform : : Type : : Native , true ) ) ;
2021-01-18 10:08:55 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
// using "namespace external::ns1;" without redundant qualification
const char code [ ] = " namespace external { \n "
" namespace ns1 { \n "
" template <int size> struct B { }; \n "
" typedef B<sizeof(bool)> V; \n "
" } \n "
" } \n "
" namespace ns { \n "
" struct A { \n "
" void f(external::ns1::V); \n "
" }; \n "
" } \n "
" using namespace external::ns1; \n "
" namespace ns { \n "
" void A::f(V) {} \n "
" } " ;
const char exp [ ] = " namespace external { "
" namespace ns1 { "
" struct B<1> ; "
" } "
" } "
" namespace ns { "
" struct A { "
" void f ( external :: ns1 :: B<1> ) ; "
" } ; "
" } "
" using namespace external :: ns1 ; "
" namespace ns { "
" void A :: f ( external :: ns1 :: B<1> ) { } "
" } "
" struct external :: ns1 :: B<1> { } ; " ;
const char act [ ] = " namespace external { "
" namespace ns1 { "
" struct B<1> ; "
" } "
" } "
" namespace ns { "
" struct A { "
" void f ( external :: ns1 :: B<1> ) ; "
" } ; "
" } "
" using namespace external :: ns1 ; "
" namespace ns { "
" void A :: f ( V ) { } "
" } "
" struct external :: ns1 :: B<1> { } ; " ;
2023-03-03 18:36:27 +01:00
TODO_ASSERT_EQUALS ( exp , act , tok ( code , true , cppcheck : : Platform : : Type : : Native , true ) ) ;
2021-01-18 10:08:55 +01:00
TODO_ASSERT_EQUALS ( " " , " [test.cpp:14]: (debug) Executable scope 'f' with unknown function. \n " , errout . str ( ) ) ;
}
{
// using "namespace external::ns1;" without redundant qualification on declaration and definition
const char code [ ] = " namespace external { \n "
" namespace ns1 { \n "
" template <int size> struct B { }; \n "
" typedef B<sizeof(bool)> V; \n "
" } \n "
" } \n "
" using namespace external::ns1; \n "
" namespace ns { \n "
" struct A { \n "
" void f(V); \n "
" }; \n "
" } \n "
" namespace ns { \n "
" void A::f(V) {} \n "
" } " ;
const char exp [ ] = " namespace external { "
" namespace ns1 { "
" struct B<1> ; "
" } "
" } "
" using namespace external :: ns1 ; "
" namespace ns { "
" struct A { "
" void f ( external :: ns1 :: B<1> ) ; "
" } ; "
" } "
" namespace ns { "
" void A :: f ( external :: ns1 :: B<1> ) { } "
" } "
" struct external :: ns1 :: B<1> { } ; " ;
const char act [ ] = " namespace external { "
" namespace ns1 { "
" template < int size > struct B { } ; "
" } "
" } "
" using namespace external :: ns1 ; "
" namespace ns { "
" struct A { "
" void f ( V ) ; "
" } ; "
" } "
" namespace ns { "
" void A :: f ( V ) { } "
" } " ;
2023-03-03 18:36:27 +01:00
TODO_ASSERT_EQUALS ( exp , act , tok ( code , true , cppcheck : : Platform : : Type : : Native , true ) ) ;
2021-01-18 10:08:55 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " namespace external { \n "
" template <int size> struct B { }; \n "
" namespace ns1 { \n "
" typedef B<sizeof(bool)> V; \n "
" } \n "
" } \n "
" namespace ns { \n "
" struct A { \n "
" void f(external::ns1::V); \n "
" }; \n "
" } \n "
" namespace ns { \n "
" void A::f(external::ns1::V) {} \n "
" } " ;
const char exp [ ] = " namespace external { "
" struct B<1> ; "
" } "
" namespace ns { "
" struct A { "
" void f ( external :: B<1> ) ; "
" } ; "
" } "
" namespace ns { "
" void A :: f ( external :: B<1> ) { } "
" } "
" struct external :: B<1> { } ; " ;
2023-03-03 18:36:27 +01:00
ASSERT_EQUALS ( exp , tok ( code , true , cppcheck : : Platform : : Type : : Native , true ) ) ;
2021-01-18 10:08:55 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " template <int size> struct B { }; \n "
" namespace external { \n "
" namespace ns1 { \n "
" typedef B<sizeof(bool)> V; \n "
" } \n "
" } \n "
" namespace ns { \n "
" struct A { \n "
" void f(external::ns1::V); \n "
" }; \n "
" } \n "
" namespace ns { \n "
" void A::f(external::ns1::V) {} \n "
" } " ;
const char exp [ ] = " struct B<1> ; "
" namespace ns { "
" struct A { "
" void f ( B<1> ) ; "
" } ; "
" } "
" namespace ns { "
" void A :: f ( B<1> ) { } "
" } "
" struct B<1> { } ; " ;
2023-03-03 18:36:27 +01:00
ASSERT_EQUALS ( exp , tok ( code , true , cppcheck : : Platform : : Type : : Native , true ) ) ;
2021-01-18 10:08:55 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
}
2021-11-06 19:07:45 +01:00
void simplifyTypedef138 ( ) {
const char code [ ] = " namespace foo { class Bar; } \n "
" class Baz; \n "
" typedef foo::Bar C; \n "
" namespace bar { \n "
" class C : Baz {}; \n "
" } \n " ;
ASSERT_EQUALS ( " namespace foo { class Bar ; } class Baz ; namespace bar { class C : Baz { } ; } " , tok ( code ) ) ;
}
2022-01-16 12:46:20 +01:00
void simplifyTypedef139 ( )
{
const char code [ ] = " typedef struct c a; \n "
" struct { \n "
" a *b; \n "
" } * d; \n "
" void e(a *a) { \n "
" if (a < d[0].b) {} \n "
" } \n " ;
ASSERT_EQUALS (
" struct Anonymous0 { struct c * b ; } ; struct Anonymous0 * d ; void e ( struct c * a ) { if ( a < d [ 0 ] . b ) { } } " ,
tok ( code ) ) ;
}
2023-04-19 21:19:45 +02:00
void simplifyTypedef140 ( ) {
{ // #10798
2022-03-02 07:46:15 +01:00
const char code [ ] = " typedef void (*b)(); \n "
" enum class E { a, b, c }; \n " ;
ASSERT_EQUALS ( " enum class E { a , b , c } ; " , tok ( code ) ) ;
}
{
const char code [ ] = " typedef int A; \n "
" enum class E { A }; \n " ;
ASSERT_EQUALS ( " enum class E { A } ; " , tok ( code ) ) ;
}
2023-04-21 18:58:44 +02:00
{
const char code [ ] = " namespace N { \n "
" struct S { enum E { E0 }; }; \n "
" } \n "
" typedef N::S T; \n "
" enum class E { a = T::E0; }; \n " ;
ASSERT_EQUALS ( " namespace N { struct S { enum E { E0 } ; } ; } enum class E { a = N :: S :: E0 ; } ; " , tok ( code ) ) ;
}
2023-04-19 21:19:45 +02:00
{ // #11494
const char code [ ] = " typedef struct S {} KEY; \n "
" class C { enum E { KEY }; }; \n " ;
ASSERT_EQUALS ( " struct S { } ; class C { enum E { KEY } ; } ; " , tok ( code ) ) ;
}
2022-03-15 12:54:19 +01:00
}
2022-03-02 07:46:15 +01:00
2022-03-15 12:54:19 +01:00
void simplifyTypedef141 ( ) { // #10144
const char code [ ] = " class C { \n "
" struct I { \n "
" using vt = const std::string; \n "
" using ptr = vt*; \n "
" }; \n "
" }; \n " ;
ASSERT_EQUALS ( " class C { struct I { } ; } ; " , tok ( code ) ) ;
2022-03-02 07:46:15 +01:00
}
2022-08-27 18:02:19 +02:00
void simplifyTypedef142 ( ) { // T() when T is a pointer type
// #11232
const char code1 [ ] = " typedef int* T; \n "
" a = T(); \n " ;
ASSERT_EQUALS ( " a = ( int * ) 0 ; " , tok ( code1 ) ) ;
// #9479
const char code2 [ ] = " typedef int* T; \n "
" void f(T = T()){} \n " ;
ASSERT_EQUALS ( " void f ( int * = ( int * ) 0 ) { } " , tok ( code2 ) ) ;
}
2023-01-27 08:19:32 +01:00
void simplifyTypedef143 ( ) { // #11506
const char code [ ] = " typedef struct { int i; } B; \n "
" void f() { \n "
" struct D : B { \n "
" char c; \n "
" }; \n "
" } \n " ;
ASSERT_EQUALS ( " struct B { int i ; } ; void f ( ) { struct D : B { char c ; } ; } " , tok ( code ) ) ;
}
2023-01-26 22:03:12 +01:00
void simplifyTypedef144 ( ) { // #9353
const char code [ ] = " typedef struct {} X; \n "
" std::vector<X> v; \n " ;
ASSERT_EQUALS ( " struct X { } ; std :: vector < X > v ; " , tok ( code ) ) ;
}
2023-04-29 15:17:55 +02:00
void simplifyTypedef145 ( ) {
// #11634
2023-04-07 11:09:13 +02:00
const char * code { } ;
code = " int typedef i; \n "
" i main() {} \n " ;
ASSERT_EQUALS ( " int main ( ) { } " , tok ( code ) ) ;
code = " struct {} typedef S; \n "
" void f() { \n "
" S(); \n "
" } \n " ;
ASSERT_EQUALS ( " struct S { } ; void f ( ) { struct S ( ) ; } " , tok ( code ) ) ;
code = " struct {} typedef S; \n " // don't crash
" S(); \n " ;
ASSERT_EQUALS ( " struct S { } ; struct S ( ) ; " , tok ( code ) ) ;
2023-04-29 15:17:55 +02:00
// #11693
code = " typedef unsigned char unsigned char; \n " // don't hang
" void f(char); \n " ;
ASSERT_EQUALS ( " void f ( char ) ; " , tok ( code ) ) ;
2023-05-04 22:23:36 +02:00
// #10796
code = " typedef unsigned a[7]; \n "
" void f(unsigned N) { \n "
" a** p = new a * [N]; \n "
" } \n " ;
TODO_ASSERT_EQUALS ( " does not compile " , " void f ( int N ) { int ( * * p ) [ 7 ] ; p = new int ( * ) [ N ] [ 7 ] ; } " , tok ( code ) ) ;
2023-06-16 19:35:01 +02:00
// #11772
code = " typedef t; \n " // don't crash on implicit int
" void g() { \n "
" sizeof(t); \n "
" } \n " ;
ASSERT_EQUALS ( " void g ( ) { sizeof ( t ) ; } " , tok ( code ) ) ; // TODO: handle implicit int
code = " typedef t[3]; \n "
" void g() { \n "
" sizeof(t); \n "
" } \n " ;
ASSERT_EQUALS ( " void g ( ) { sizeof ( t ) ; } " , tok ( code ) ) ; // TODO: handle implicit int
2023-04-07 11:09:13 +02:00
}
2014-11-20 14:20:09 +01:00
void simplifyTypedefFunction1 ( ) {
2014-09-24 13:23:44 +02:00
{
const char code [ ] = " typedef void (*my_func)(); \n "
" std::queue<my_func> func_queue; " ;
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " std :: queue < void ( * ) ( ) > func_queue ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void (*my_func)(void); \n "
" std::queue<my_func> func_queue; " ;
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " std :: queue < void ( * ) ( void ) > func_queue ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void (*my_func)(int); \n "
" std::queue<my_func> func_queue; " ;
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " std :: queue < void ( * ) ( int ) > func_queue ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void (*my_func)(int*); \n "
" std::queue<my_func> func_queue; " ;
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " std :: queue < void ( * ) ( int * ) > func_queue ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
// ticket # 1615
const char code [ ] = " typedef void (*my_func)(arg_class*); \n "
" std::queue<my_func> func_queue; " ;
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " std :: queue < void ( * ) ( arg_class * ) > func_queue ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void (my_func)(); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " std :: queue < void ( * ) ( ) > func_queue ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void (my_func)(void); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " std :: queue < void ( * ) ( void ) > func_queue ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void (my_func)(int); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " std :: queue < void ( * ) ( int ) > func_queue ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void (my_func)(int*); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " std :: queue < void ( * ) ( int * ) > func_queue ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void (my_func)(arg_class*); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " std :: queue < void ( * ) ( arg_class * ) > func_queue ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void my_func(); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " std :: queue < void ( * ) ( ) > func_queue ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void my_func(void); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " std :: queue < void ( * ) ( void ) > func_queue ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void my_func(int); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " std :: queue < void ( * ) ( int ) > func_queue ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void my_func(int*); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " std :: queue < void ( * ) ( int * ) > func_queue ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void my_func(arg_class*); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " std :: queue < void ( * ) ( arg_class * ) > func_queue ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void (my_func()); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " std :: queue < void ( * ) ( ) > func_queue ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void (my_func(void)); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " std :: queue < void ( * ) ( void ) > func_queue ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void (my_func(int)); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " std :: queue < void ( * ) ( int ) > func_queue ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void (my_func(int*)); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " std :: queue < void ( * ) ( int * ) > func_queue ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef void (my_func(arg_class*)); \n "
" std::queue<my_func *> func_queue; " ;
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " std :: queue < void ( * ) ( arg_class * ) > func_queue ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
}
2014-11-20 14:20:09 +01:00
void simplifyTypedefFunction2 ( ) { // ticket #1685
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef void voidfn (int); \n "
" voidfn xxx; " ;
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " void xxx ( int ) ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedefFunction3 ( ) {
2014-09-24 13:23:44 +02:00
{
const char code [ ] = " typedef C func1(); \n "
" typedef C (* func2)(); \n "
" typedef C (& func3)(); \n "
" typedef C (C::* func4)(); \n "
" typedef C (C::* func5)() const; \n "
" typedef C (C::* func6)() volatile; \n "
" typedef C (C::* func7)() const volatile; \n "
" func1 f1; \n "
" func2 f2; \n "
" func3 f3; \n "
" func4 f4; \n "
" func5 f5; \n "
" func6 f6; \n "
" func7 f7; " ;
// The expected result..
2023-05-12 20:39:08 +02:00
const char expected [ ] = " C f1 ( ) ; "
2020-04-10 11:53:32 +02:00
" C ( * f2 ) ( ) ; "
2015-03-14 20:42:44 +01:00
" C ( & f3 ) ( ) ; "
2020-04-10 11:53:32 +02:00
" C ( * f4 ) ( ) ; "
" C ( * f5 ) ( ) ; "
" C ( * f6 ) ( ) ; "
" C ( * f7 ) ( ) ; " ;
2023-03-03 18:36:27 +01:00
ASSERT_EQUALS ( expected , tok ( code , true , cppcheck : : Platform : : Type : : Native , false ) ) ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef C const func1(); \n "
" typedef C const (* func2)(); \n "
" typedef C const (& func3)(); \n "
" typedef C const (C::* func4)(); \n "
" typedef C const (C::* func5)() const; \n "
" typedef C const (C::* func6)() volatile; \n "
" typedef C const (C::* func7)() const volatile; \n "
" func1 f1; \n "
" func2 f2; \n "
" func3 f3; \n "
" func4 f4; \n "
" func5 f5; \n "
" func6 f6; \n "
" func7 f7; " ;
// The expected result..
// C const -> const C
2023-05-12 20:39:08 +02:00
const char expected [ ] = " const C f1 ( ) ; "
2020-04-10 11:53:32 +02:00
" const C ( * f2 ) ( ) ; "
2015-03-14 20:42:44 +01:00
" const C ( & f3 ) ( ) ; "
2020-04-10 11:53:32 +02:00
" const C ( * f4 ) ( ) ; "
" const C ( * f5 ) ( ) ; "
" const C ( * f6 ) ( ) ; "
" const C ( * f7 ) ( ) ; " ;
2023-03-03 18:36:27 +01:00
ASSERT_EQUALS ( expected , tok ( code , true , cppcheck : : Platform : : Type : : Native , false ) ) ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef const C func1(); \n "
" typedef const C (* func2)(); \n "
" typedef const C (& func3)(); \n "
" typedef const C (C::* func4)(); \n "
" typedef const C (C::* func5)() const; \n "
" typedef const C (C::* func6)() volatile; \n "
" typedef const C (C::* func7)() const volatile; \n "
" func1 f1; \n "
" func2 f2; \n "
" func3 f3; \n "
" func4 f4; \n "
" func5 f5; \n "
" func6 f6; \n "
" func7 f7; " ;
// The expected result..
2023-05-12 20:39:08 +02:00
const char expected [ ] = " const C f1 ( ) ; "
2020-04-10 11:53:32 +02:00
" const C ( * f2 ) ( ) ; "
2015-03-14 20:42:44 +01:00
" const C ( & f3 ) ( ) ; "
2020-04-10 11:53:32 +02:00
" const C ( * f4 ) ( ) ; "
" const C ( * f5 ) ( ) ; "
" const C ( * f6 ) ( ) ; "
" const C ( * f7 ) ( ) ; " ;
2023-03-03 18:36:27 +01:00
ASSERT_EQUALS ( expected , tok ( code , true , cppcheck : : Platform : : Type : : Native , false ) ) ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef C * func1(); \n "
" typedef C * (* func2)(); \n "
" typedef C * (& func3)(); \n "
" typedef C * (C::* func4)(); \n "
" typedef C * (C::* func5)() const; \n "
" typedef C * (C::* func6)() volatile; \n "
" typedef C * (C::* func7)() const volatile; \n "
" func1 f1; \n "
" func2 f2; \n "
" func3 f3; \n "
" func4 f4; \n "
" func5 f5; \n "
" func6 f6; \n "
" func7 f7; " ;
// The expected result..
2023-05-12 20:39:08 +02:00
const char expected [ ] = " C * f1 ( ) ; "
2020-04-10 11:53:32 +02:00
" C * ( * f2 ) ( ) ; "
2015-03-14 20:42:44 +01:00
" C * ( & f3 ) ( ) ; "
2020-04-10 11:53:32 +02:00
" C * ( * f4 ) ( ) ; "
" C * ( * f5 ) ( ) ; "
" C * ( * f6 ) ( ) ; "
" C * ( * f7 ) ( ) ; " ;
2023-03-03 18:36:27 +01:00
ASSERT_EQUALS ( expected , tok ( code , true , cppcheck : : Platform : : Type : : Native , false ) ) ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef const C * func1(); \n "
" typedef const C * (* func2)(); \n "
" typedef const C * (& func3)(); \n "
" typedef const C * (C::* func4)(); \n "
" typedef const C * (C::* func5)() const; \n "
" typedef const C * (C::* func6)() volatile; \n "
" typedef const C * (C::* func7)() const volatile; \n "
" func1 f1; \n "
" func2 f2; \n "
" func3 f3; \n "
" func4 f4; \n "
" func5 f5; \n "
" func6 f6; \n "
" func7 f7; " ;
// The expected result..
2023-05-12 20:39:08 +02:00
const char expected [ ] = " const C * f1 ( ) ; "
2020-04-10 11:53:32 +02:00
" const C * ( * f2 ) ( ) ; "
2015-03-14 20:42:44 +01:00
" const C * ( & f3 ) ( ) ; "
2020-04-10 11:53:32 +02:00
" const C * ( * f4 ) ( ) ; "
" const C * ( * f5 ) ( ) ; "
" const C * ( * f6 ) ( ) ; "
" const C * ( * f7 ) ( ) ; " ;
2023-03-03 18:36:27 +01:00
ASSERT_EQUALS ( expected , tok ( code , true , cppcheck : : Platform : : Type : : Native , false ) ) ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef C const * func1(); \n "
" typedef C const * (* func2)(); \n "
" typedef C const * (& func3)(); \n "
" typedef C const * (C::* func4)(); \n "
" typedef C const * (C::* func5)() const; \n "
" typedef C const * (C::* func6)() volatile; \n "
" typedef C const * (C::* func7)() const volatile; \n "
" func1 f1; \n "
" func2 f2; \n "
" func3 f3; \n "
" func4 f4; \n "
" func5 f5; \n "
" func6 f6; \n "
" func7 f7; " ;
// The expected result..
// C const -> const C
2023-05-12 20:39:08 +02:00
const char expected [ ] = " const C * f1 ( ) ; "
2020-04-10 11:53:32 +02:00
" const C * ( * f2 ) ( ) ; "
2015-03-14 20:42:44 +01:00
" const C * ( & f3 ) ( ) ; "
2020-04-10 11:53:32 +02:00
" const C * ( * f4 ) ( ) ; "
" const C * ( * f5 ) ( ) ; "
" const C * ( * f6 ) ( ) ; "
" const C * ( * f7 ) ( ) ; " ;
2023-03-03 18:36:27 +01:00
ASSERT_EQUALS ( expected , tok ( code , true , cppcheck : : Platform : : Type : : Native , false ) ) ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
}
2014-11-20 14:20:09 +01:00
void simplifyTypedefFunction4 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef int ( * ( * type1 ) ( bool ) ) ( int , int ) ; \n "
" typedef int ( * ( type2 ) ( bool ) ) ( int , int ) ; \n "
" typedef int ( * type3 ( bool ) ) ( int , int ) ; \n "
" type1 t1; \n "
" type2 t2; \n "
" type3 t3; " ;
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " int ( * ( * t1 ) ( bool ) ) ( int , int ) ; "
" int * t2 ( bool ) ; "
" int * t3 ( bool ) ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedefFunction5 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef int ( * type1 ) ( float ) ; \n "
" typedef int ( * const type2 ) ( float ) ; \n "
" typedef int ( * volatile type3 ) ( float ) ; \n "
" typedef int ( * const volatile type4 ) ( float ) ; \n "
" typedef int ( C :: * type5 ) ( float ) ; \n "
" typedef int ( C :: * const type6 ) ( float ) ; \n "
" typedef int ( C :: * volatile type7 ) ( float ) ; \n "
" typedef int ( C :: * const volatile type8 ) ( float ) ; \n "
" typedef int ( :: C :: * type9 ) ( float ) ; \n "
" typedef int ( :: C :: * const type10 ) ( float ) ; \n "
" typedef int ( :: C :: * volatile type11 ) ( float ) ; \n "
" typedef int ( :: C :: * const volatile type12 ) ( float ) ; \n "
" type1 t1; \n "
" type2 t2; \n "
" type3 t3; \n "
" type4 t4; \n "
" type5 t5; \n "
" type6 t6; \n "
" type7 t7; \n "
" type8 t8; \n "
" type9 t9; \n "
" type10 t10; \n "
" type11 t11; \n "
" type12 t12; " ;
// The expected result..
2023-02-26 18:03:24 +01:00
const char expected [ ] = " int ( * t1 ) ( float ) ; "
" int ( * const t2 ) ( float ) ; "
" int ( * volatile t3 ) ( float ) ; "
" int ( * const volatile t4 ) ( float ) ; "
" int ( * t5 ) ( float ) ; "
" int ( * const t6 ) ( float ) ; "
" int ( * volatile t7 ) ( float ) ; "
" int ( * const volatile t8 ) ( float ) ; "
2015-03-14 20:42:44 +01:00
" int ( :: C :: * t9 ) ( float ) ; "
" int ( :: C :: * const t10 ) ( float ) ; "
2018-05-10 07:40:01 +02:00
" int ( :: C :: * volatile t11 ) ( float ) ; "
" int ( :: C :: * const volatile t12 ) ( float ) ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedefFunction6 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef void (*testfp)(); \n "
" struct Fred \n "
" { \n "
" testfp get1() { return 0; } \n "
" void ( * get2 ( ) ) ( ) { return 0 ; } \n "
" testfp get3(); \n "
" void ( * get4 ( ) ) ( ); \n "
" }; \n "
" testfp Fred::get3() { return 0; } \n "
2015-03-14 20:42:44 +01:00
" void ( * Fred::get4 ( ) ) ( ) { return 0 ; } " ;
2014-09-24 13:23:44 +02:00
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " struct Fred "
" { "
" void * get1 ( ) { return 0 ; } "
" void * get2 ( ) { return 0 ; } "
" void * get3 ( ) ; "
" void * get4 ( ) ; "
" } ; "
" void * Fred :: get3 ( ) { return 0 ; } "
" void * Fred :: get4 ( ) { return 0 ; } " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedefFunction7 ( ) {
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef void ( __gnu_cxx :: _SGIAssignableConcept < _Tp > :: * _func_Tp_SGIAssignableConcept ) () ; "
2015-03-14 20:42:44 +01:00
" _func_Tp_SGIAssignableConcept X; " ;
2014-09-24 13:23:44 +02:00
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " void ( __gnu_cxx :: _SGIAssignableConcept < _Tp > :: * X ) ( ) ; " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( expected , tok ( code , false ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedefFunction8 ( ) {
2014-09-24 13:23:44 +02:00
// #2376 - internal error
const char code [ ] = " typedef int f_expand(const nrv_byte *); \n "
2016-02-06 20:50:44 +01:00
" void f(f_expand *(*get_fexp(int))){} " ;
2014-09-24 13:23:44 +02:00
checkSimplifyTypedef ( code ) ;
TODO_ASSERT_EQUALS ( " " , " [test.cpp:2]: (debug) Function::addArguments found argument 'int' with varid 0. \n " , errout . str ( ) ) ; // make sure that there is no internal error
}
2014-11-28 17:44:36 +01:00
void simplifyTypedefFunction9 ( ) {
{
const char code [ ] = " typedef ::C (::C::* func1)(); \n "
" typedef ::C (::C::* func2)() const; \n "
" typedef ::C (::C::* func3)() volatile; \n "
" typedef ::C (::C::* func4)() const volatile; \n "
" func1 f1; \n "
" func2 f2; \n "
" func3 f3; \n "
" func4 f4; " ;
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " :: C ( :: C :: * f1 ) ( ) ; "
" :: C ( :: C :: * f2 ) ( ) const ; "
2018-05-10 07:40:01 +02:00
" :: C ( :: C :: * f3 ) ( ) volatile ; "
" :: C ( :: C :: * f4 ) ( ) const volatile ; " ;
2014-11-28 17:44:36 +01:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef B::C (B::C::* func1)(); \n "
" typedef B::C (B::C::* func2)() const; \n "
" typedef B::C (B::C::* func3)() volatile; \n "
" typedef B::C (B::C::* func4)() const volatile; \n "
" func1 f1; \n "
" func2 f2; \n "
" func3 f3; \n "
" func4 f4; " ;
// The expected result..
2020-04-10 11:53:32 +02:00
const char expected [ ] = " B :: C ( * f1 ) ( ) ; "
" B :: C ( * f2 ) ( ) ; "
" B :: C ( * f3 ) ( ) ; "
" B :: C ( * f4 ) ( ) ; " ;
2023-03-03 18:36:27 +01:00
ASSERT_EQUALS ( expected , tok ( code , true , cppcheck : : Platform : : Type : : Native , false ) ) ;
2014-11-28 17:44:36 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef ::B::C (::B::C::* func1)(); \n "
" typedef ::B::C (::B::C::* func2)() const; \n "
" typedef ::B::C (::B::C::* func3)() volatile; \n "
" typedef ::B::C (::B::C::* func4)() const volatile; \n "
" func1 f1; \n "
" func2 f2; \n "
" func3 f3; \n "
" func4 f4; " ;
// The expected result..
2015-03-14 20:42:44 +01:00
const char expected [ ] = " :: B :: C ( :: B :: C :: * f1 ) ( ) ; "
" :: B :: C ( :: B :: C :: * f2 ) ( ) const ; "
2018-05-10 07:40:01 +02:00
" :: B :: C ( :: B :: C :: * f3 ) ( ) volatile ; "
" :: B :: C ( :: B :: C :: * f4 ) ( ) const volatile ; " ;
2014-11-28 17:44:36 +01:00
ASSERT_EQUALS ( expected , tok ( code ) ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
{
const char code [ ] = " typedef A::B::C (A::B::C::* func1)(); \n "
" typedef A::B::C (A::B::C::* func2)() const; \n "
" typedef A::B::C (A::B::C::* func3)() volatile; \n "
" typedef A::B::C (A::B::C::* func4)() const volatile; \n "
" func1 f1; \n "
" func2 f2; \n "
" func3 f3; \n "
" func4 f4; " ;
// The expected result..
2020-04-10 11:53:32 +02:00
const char expected [ ] = " A :: B :: C ( * f1 ) ( ) ; "
" A :: B :: C ( * f2 ) ( ) ; "
" A :: B :: C ( * f3 ) ( ) ; "
" A :: B :: C ( * f4 ) ( ) ; " ;
2023-03-03 18:36:27 +01:00
ASSERT_EQUALS ( expected , tok ( code , true , cppcheck : : Platform : : Type : : Native , false ) ) ;
2014-11-28 17:44:36 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
}
2014-12-04 05:43:58 +01:00
void simplifyTypedefFunction10 ( ) {
2016-04-22 06:02:54 +02:00
const char code [ ] = " enum Format_E1 { FORMAT11, FORMAT12 } Format_T1; \n "
2014-12-04 05:43:58 +01:00
" namespace MySpace { \n "
2016-04-22 06:02:54 +02:00
" enum Format_E2 { FORMAT21, FORMAT22 } Format_T2; \n "
2014-12-04 05:43:58 +01:00
" } \n "
" typedef Format_E1 (**PtrToFunPtr_Type1)(); \n "
" typedef MySpace::Format_E2 (**PtrToFunPtr_Type2)(); \n "
" PtrToFunPtr_Type1 t1; \n "
2015-03-14 20:42:44 +01:00
" PtrToFunPtr_Type2 t2; " ;
2016-04-22 06:02:54 +02:00
ASSERT_EQUALS ( " enum Format_E1 { FORMAT11 , FORMAT12 } ; enum Format_E1 Format_T1 ; "
" namespace MySpace { "
" enum Format_E2 { FORMAT21 , FORMAT22 } ; enum Format_E2 Format_T2 ; "
2014-12-04 05:43:58 +01:00
" } "
2020-04-10 11:53:32 +02:00
" Format_E1 ( * * t1 ) ( ) ; "
" MySpace :: Format_E2 ( * * t2 ) ( ) ; " ,
2014-12-04 05:43:58 +01:00
tok ( code , false ) ) ;
}
2014-11-20 14:20:09 +01:00
void simplifyTypedefShadow ( ) { // shadow variable (#4445)
2014-09-24 13:23:44 +02:00
const char code [ ] = " typedef struct { int x; } xyz;; \n "
" void f(){ \n "
" int abc, xyz; \n " // <- shadow variable
2015-03-14 20:42:44 +01:00
" } " ;
2014-09-24 13:23:44 +02:00
ASSERT_EQUALS ( " struct xyz { int x ; } ; void f ( ) { int abc ; int xyz ; } " ,
tok ( code , false ) ) ;
}
2021-08-29 19:14:53 +02:00
void simplifyTypedefMacro ( ) {
const char code [ ] = " typedef uint32_t index_t; \n "
" \n "
" #define NO_SEGMENT ((index_t)12) \n "
" \n "
" void foo(index_t prev_segment) { \n "
" if(prev_segment==NO_SEGMENT) {} \n " // <- test that index_t is replaced with uint32_t in the expanded tokens
" } " ;
ASSERT_EQUALS ( " void foo ( uint32_t prev_segment ) { if ( prev_segment == ( ( uint32_t ) 12 ) ) { } } " ,
simplifyTypedefP ( code ) ) ;
}
2014-09-24 13:23:44 +02:00
} ;
REGISTER_TEST ( TestSimplifyTypedef )