Skip to content

Commit 3923618

Browse files
zingsheimPKEuS
authored andcommitted
Fixed cppcheck-opensource#6222 (Missing varid for multiple braced initialized variables)
-> Fixed broken code in unit tests
1 parent 079f495 commit 3923618

5 files changed

Lines changed: 26 additions & 24 deletions

File tree

lib/tokenize.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5527,6 +5527,16 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, Token * tokEnd, bool only_k_r_
55275527
if (tok2 && tok2->str() == ";")
55285528
tok2 = nullptr;
55295529
}
5530+
}
5531+
5532+
// brace initialization
5533+
else if (Token::Match(varName, "%var% {")) {
5534+
tok2 = varName->next();
5535+
tok2 = tok2->link();
5536+
if (tok2)
5537+
tok2 = tok2->next();
5538+
if (tok2 && tok2->str() != ",")
5539+
tok2 = nullptr;
55305540
} else
55315541
tok2 = nullptr;
55325542
} else {

test/testconstructors.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -387,22 +387,12 @@ class TestConstructors : public TestFixture {
387387
" int z{0};\n"
388388
" int (*pf[2])(){nullptr, nullptr};\n"
389389
" int a[2][3] = {{1,2,3},{4,5,6}};\n"
390-
" int d, e{3};\n"
391-
"};");
392-
ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable 'Fred::d' is not initialized in the constructor.\n", errout.str());
393-
394-
check("class Fred {\n"
395-
"public:\n"
396-
" Fred() {}\n"
397-
"private:\n"
398390
" int b{1}, c{2};\n"
399391
" int d, e{3};\n"
400392
" int f{4}, g;\n"
401393
"};");
402-
TODO_ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable 'Fred::d' is not initialized in the constructor.\n"
403-
"[test.cpp:3]: (warning) Member variable 'Fred::g' is not initialized in the constructor.\n",
404-
"[test.cpp:3]: (warning) Member variable 'Fred::d' is not initialized in the constructor.\n",
405-
errout.str()); // fails due to missing varid
394+
ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable 'Fred::d' is not initialized in the constructor.\n"
395+
"[test.cpp:3]: (warning) Member variable 'Fred::g' is not initialized in the constructor.\n", errout.str());
406396
}
407397

408398
void simple12() { // ticket #4620

test/testother.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5942,37 +5942,37 @@ class TestOther : public TestFixture {
59425942
ASSERT_EQUALS("", errout.str());
59435943

59445944
// avoid crash with pointer variable - for local variable on stack as well - see #4801
5945-
check("void foo {\n"
5945+
check("void foo() {\n"
59465946
" int *cp;\n"
59475947
" if ( pipe (cp) == -1 ) {\n"
59485948
" return;\n"
59495949
" }\n"
5950-
"}\n");
5950+
"}");
59515951
ASSERT_EQUALS("", errout.str());
59525952

59535953
// test with unknown variable
5954-
check("void foo {\n"
5954+
check("void foo() {\n"
59555955
" if ( pipe (cp) == -1 ) {\n"
59565956
" return;\n"
59575957
" }\n"
5958-
"}\n");
5958+
"}");
59595959
ASSERT_EQUALS("", errout.str());
59605960

59615961
// avoid crash with pointer variable - for local variable on stack as well - see #4801
5962-
check("void foo {\n"
5962+
check("void foo() {\n"
59635963
" int *cp;\n"
59645964
" if ( pipe (cp) == -1 ) {\n"
59655965
" return;\n"
59665966
" }\n"
5967-
"}\n");
5967+
"}");
59685968
ASSERT_EQUALS("", errout.str());
59695969

59705970
// test with unknown variable
5971-
check("void foo {\n"
5971+
check("void foo() {\n"
59725972
" if ( pipe (cp) == -1 ) {\n"
59735973
" return;\n"
59745974
" }\n"
5975-
"}\n");
5975+
"}");
59765976
ASSERT_EQUALS("", errout.str());
59775977

59785978
}

test/testsimplifytypedef.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,13 +1599,13 @@ class TestSimplifyTypedef : public TestFixture {
15991599
}
16001600

16011601
void simplifyTypedef57() { // ticket #1846
1602-
const char code[] = "void foo {\n"
1602+
const char code[] = "void foo() {\n"
16031603
" typedef int A;\n"
16041604
" A a = A(1) * A(2);\n"
16051605
"};\n";
16061606

16071607
// The expected result..
1608-
const std::string expected("void foo { "
1608+
const std::string expected("void foo ( ) { "
16091609
""
16101610
"int a ; a = int ( 1 ) * int ( 2 ) ; "
16111611
"} ;");

test/testvarid.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,11 +1851,13 @@ class TestVarID : public TestFixture {
18511851
"1: int i@1 { 1 } ;\n"
18521852
"2: std :: vector < int > vec@2 { 1 , 2 , 3 } ;\n"
18531853
"3: namespace n { int z@3 ; } ;\n"
1854-
"4: int & j@4 { i@1 } ;\n",
1854+
"4: int & j@4 { i@1 } ;\n"
1855+
"5: int k@5 { 1 } ; int l@6 { 2 } ;\n",
18551856
tokenize("int i{1};\n"
18561857
"std::vector<int> vec{1, 2, 3};\n"
18571858
"namespace n { int z; };\n"
1858-
"int& j{i};\n"));
1859+
"int& j{i};\n"
1860+
"int k{1}, l{2};"));
18591861

18601862
// #6030
18611863
ASSERT_EQUALS("\n\n##file 0\n"

0 commit comments

Comments
 (0)