Better handle const/noexcept methods#2211
Conversation
e16ea17 to
88d8a50
Compare
lib/tokenize.cpp
Outdated
|
|
||
| // skip over function parameters | ||
| if (tok2->strAt(1) == "(") { | ||
| if (tok2->str() == "(" || tok2->strAt(1) == "(") { |
There was a problem hiding this comment.
if tok2->str() == "(" is true.. then don't you want to jump to the ")" on the next line?
There was a problem hiding this comment.
Yes indeed. Thanks.
|
|
||
| { | ||
| const char code[] = "class c {\n" | ||
| " typedef char foo[4];\n" |
There was a problem hiding this comment.
I don't think this is C++ code. At least g++ 8.3.0 does not like it when I test. Which compiler do you use?
123.cpp:3:9: error: expected ‘;’ at end of member declaration
foo & f const;
^
There was a problem hiding this comment.
Indeed. My brain switched off when I tried to add some more test cases. /o
I'll clean this up.
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.
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 danmar#2 0x00007ffff6b1d957 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 danmar#3 0x00007ffff6b23ab6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 danmar#4 0x00007ffff6b23af1 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 danmar#5 0x00007ffff6b23d24 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 danmar#6 0x0000555556366bf8 in Tokenizer::cppcheckError (this=0x7fffffffc2d0, tok=0x607000006760) at ../lib/tokenize.cpp:8721 danmar#7 0x000055555636a4bb in Tokenizer::validate (this=0x7fffffffc2d0) at ../lib/tokenize.cpp:9154 danmar#8 0x000055555633e3aa in Tokenizer::simplifyTokenList1 (this=0x7fffffffc2d0, FileName=0x603000002d50 "test.cpp") at ../lib/tokenize.cpp:4477 danmar#9 0x00005555563223ca in Tokenizer::simplifyTokens1 (this=0x7fffffffc2d0, configuration="") at ../lib/tokenize.cpp:2286 danmar#10 0x00005555563235c8 in Tokenizer::tokenize (this=0x7fffffffc2d0, code=..., FileName=0x555556fda9a0 "test.cpp", configuration="") at ../lib/tokenize.cpp:2345 danmar#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 danmar#12 0x000055555697239e in TestSimplifyTypedef::simplifyTypedef129 (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:2599 danmar#13 0x000055555694092c in TestSimplifyTypedef::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:167 danmar#14 0x00005555569cab84 in TestFixture::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>, str="simplifyTypedef129") at ../test/testsuite.cpp:306 danmar#15 0x00005555569cb445 in TestFixture::runTests (args=...) at ../test/testsuite.cpp:329 danmar#16 0x000055555687bdfb in main (argc=2, argv=0x7fffffffd988) at ../test/testrunner.cpp:44 ```
…sult is still wrong
88d8a50 to
e7f0e9b
Compare
| " constexpr const foo &c_str() const noexcept { return _a; }\n" | ||
| "};"; | ||
|
|
||
| const char exp [] = "class c { char _a [ 4 ] ; const const char ( & c_str ( ) const noexcept ) [ 4 ] { return _a ; } } ;"; |
There was a problem hiding this comment.
I guess this is the correct expansion.. however I admit I don't understand this simplification. I'll trust you on this.
However I guess that for instance isFunctionHead won't handle this code well.. and then function handling does not work. If you run Cppcheck like cppcheck --debug -v 2211.cpp on this code then we can see that the function c_str() is not printed out. I wonder... do you want to look into this further later? I would suggest you do that in a separate PR.
|
|
||
| // handle missing variable name | ||
| if (tok2->strAt(3) == ")" || tok2->strAt(3) == ",") | ||
| if (tok2->strAt(3) == ")" || tok2->strAt(3) == "," || tok2->strAt(3) == "(") |
There was a problem hiding this comment.
I would probably have written a Token::Match here. That is shorter code and should be faster. But I don't have a strong opinion about it so will merge anyway..
|
As you said in the PR description... the parsing is far from perfect. This is just a small step. Feel free to improve the parsing later.. |
* 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 danmar#2 0x00007ffff6b1d957 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 danmar#3 0x00007ffff6b23ab6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 danmar#4 0x00007ffff6b23af1 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 danmar#5 0x00007ffff6b23d24 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 danmar#6 0x0000555556366bf8 in Tokenizer::cppcheckError (this=0x7fffffffc2d0, tok=0x607000006760) at ../lib/tokenize.cpp:8721 danmar#7 0x000055555636a4bb in Tokenizer::validate (this=0x7fffffffc2d0) at ../lib/tokenize.cpp:9154 danmar#8 0x000055555633e3aa in Tokenizer::simplifyTokenList1 (this=0x7fffffffc2d0, FileName=0x603000002d50 "test.cpp") at ../lib/tokenize.cpp:4477 danmar#9 0x00005555563223ca in Tokenizer::simplifyTokens1 (this=0x7fffffffc2d0, configuration="") at ../lib/tokenize.cpp:2286 danmar#10 0x00005555563235c8 in Tokenizer::tokenize (this=0x7fffffffc2d0, code=..., FileName=0x555556fda9a0 "test.cpp", configuration="") at ../lib/tokenize.cpp:2345 danmar#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 danmar#12 0x000055555697239e in TestSimplifyTypedef::simplifyTypedef129 (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:2599 danmar#13 0x000055555694092c in TestSimplifyTypedef::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>) at ../test/testsimplifytypedef.cpp:167 danmar#14 0x00005555569cab84 in TestFixture::run (this=0x555557728580 <(anonymous namespace)::instance_TestSimplifyTypedef>, str="simplifyTypedef129") at ../test/testsuite.cpp:306 danmar#15 0x00005555569cb445 in TestFixture::runTests (args=...) at ../test/testsuite.cpp:329 danmar#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
Better handle const/noexcept methods
Fix parsing of some operator
It is still very broken, but at least, it does not fail.
Here is the previous error: