cppcheck reports a false positive syntaxError on c++20 bitfields that contain both a size and a brace initializer, which were allowed in c++20. A test case below includes sizes from more complex integral constant expressions, but handling the simple case "int m5:5{55};" would allow the most common usage to proceed in cppcheck.
I reproduced this with 2.19.0 on ubuntu 26.04, but it also fails with main@eb4b6526b3502ee779a74019f814d45d890427ba
cppcheck --enable=all --std=c++20 test.cc
Checking test.cc ...
test.cc:10:12: error: syntax error [syntaxError]
int m5:5{55};
^
nofile:0:0: information: Active checkers: There was critical errors (use --checkers-report=<filename> to see details) [checkersReport]
cat test.cc
int main(int argc, char* argv[])
{
constexpr int a = 1;
constexpr int b = 2;
struct s {
int m1;
int m2:2;
int m3:3+3;
int m4:4=44;
int m5:5{55};
int maba:a+b{99};
int m6:6+6{66};
};
}
</filename>
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The syntaxError comes from Tokenizer::findGarbageCode. (line numbers from eb4b6526b3502ee779a74019f814d45d890427ba)
Breakpoint1.1,Tokenizer::syntaxError(this=0x7fffffffc550,tok=0x555556240920,code="")at/home/tsteven4/work/cppcheck/lib/tokenize.cpp:84418441{(gdb)bt#0 Tokenizer::syntaxError (this=0x7fffffffc550, tok=0x555556240920, code="") at /home/tsteven4/work/cppcheck/lib/tokenize.cpp:8441#1 0x00005555556f183f in Tokenizer::findGarbageCode (this=0x7fffffffc550) at /home/tsteven4/work/cppcheck/lib/tokenize.cpp:9113#2 0x00005555556dada3 in operator() (__closure=0x7fffffffb860) at /home/tsteven4/work/cppcheck/lib/tokenize.cpp:5846#3 0x0000555555709866 in Timer::run<Tokenizer::simplifyTokenList1(char const*)::<lambda()> >(std::string, TimerResultsIntf *, const struct {...} &) (str="", timerResults=0x0, f=...)at/home/tsteven4/work/cppcheck/lib/timer.h:77#4 0x00005555556db45b in Tokenizer::simplifyTokenList1 (this=0x7fffffffc550, FileName=0x555556250d60 "/home/tsteven4/work/cppchk/test.cc") at /home/tsteven4/work/cppcheck/lib/tokenize.cpp:5845#5 0x00005555556cabec in Tokenizer::simplifyTokens1 (this=0x7fffffffc550, configuration="", fileIndex=0) at /home/tsteven4/work/cppcheck/lib/tokenize.cpp:3569#6 0x0000555555a74ae4 in CppCheck::checkInternal (this=0x7fffffffcea0, file=..., cfgname="", createTokenList=...) at /home/tsteven4/work/cppcheck/lib/cppcheck.cpp:1252#7 0x0000555555a718bd in CppCheck::checkFile (this=0x7fffffffcea0, file=..., cfgname="") at /home/tsteven4/work/cppcheck/lib/cppcheck.cpp:914#8 0x0000555555a70266 in CppCheck::check (this=0x7fffffffcea0, file=...) at /home/tsteven4/work/cppcheck/lib/cppcheck.cpp:814#9 0x0000555555d048ba in SingleExecutor::check (this=0x7fffffffcf30) at /home/tsteven4/work/cppcheck/cli/singleexecutor.cpp:52#10 0x0000555555ceedfd in CppCheckExecutor::check_internal (this=0x7fffffffdb70, settings=..., supprs=...) at /home/tsteven4/work/cppcheck/cli/cppcheckexecutor.cpp:453#11 0x0000555555ced2d9 in CppCheckExecutor::check_wrapper (this=0x7fffffffdb70, settings=..., supprs=...) at /home/tsteven4/work/cppcheck/cli/cppcheckexecutor.cpp:295#12 0x0000555555ced13f in CppCheckExecutor::check (this=0x7fffffffdb70, argc=6, argv=0x7fffffffdd28) at /home/tsteven4/work/cppcheck/cli/cppcheckexecutor.cpp:280#13 0x0000555555cebc92 in main (argc=6, argv=0x7fffffffdd28) at /home/tsteven4/work/cppcheck/cli/main.cpp:71(gdb)quit()❌️Asyntaxerrorinexpression,near`)'.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
cppcheck reports a false positive syntaxError on c++20 bitfields that contain both a size and a brace initializer, which were allowed in c++20. A test case below includes sizes from more complex integral constant expressions, but handling the simple case "int m5:5{55};" would allow the most common usage to proceed in cppcheck.
I reproduced this with 2.19.0 on ubuntu 26.04, but it also fails with main@eb4b6526b3502ee779a74019f814d45d890427ba
Checking test.cc ...
test.cc:10:12: error: syntax error [syntaxError]
int m5:5{55};
^
nofile:0:0: information: Active checkers: There was critical errors (use --checkers-report=<filename> to see details) [checkersReport] </filename>
The syntaxError comes from Tokenizer::findGarbageCode. (line numbers from eb4b6526b3502ee779a74019f814d45d890427ba)
Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/15070