Skip to content

Commit 8f42596

Browse files
authored
Fix #13557: Tokenizer: Use same simplification for 'int x{0}' and 'int x(0)' (#7223)
1 parent 6d3b47c commit 8f42596

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

Diff for: lib/tokenize.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -7686,7 +7686,7 @@ void Tokenizer::simplifyInitVar()
76867686
if (tok->str() == "return")
76877687
continue;
76887688

7689-
if (Token::Match(tok, "class|struct|union| %type% *| %name% ( &| %any% ) ;")) {
7689+
if (Token::Match(tok, "class|struct|union| %type% *| %name% (|{ &| %any% )|} ;")) {
76907690
tok = initVar(tok);
76917691
} else if (Token::Match(tok, "%type% *| %name% ( %type% (")) {
76927692
const Token* tok2 = tok->tokAt(2);
@@ -7730,12 +7730,13 @@ Token * Tokenizer::initVar(Token * tok)
77307730
// check initializer..
77317731
if (tok->tokAt(2)->isStandardType() || tok->strAt(2) == "void")
77327732
return tok;
7733-
if (!tok->tokAt(2)->isNumber() && !Token::Match(tok->tokAt(2), "%type% (") && tok->strAt(2) != "&" && tok->tokAt(2)->varId() == 0)
7733+
if (!tok->tokAt(2)->isNumber() && !Token::Match(tok->tokAt(2), "%type% (|{") && tok->strAt(2) != "&" && tok->tokAt(2)->varId() == 0)
77347734
return tok;
77357735

77367736
// insert '; var ='
77377737
tok->insertToken(";");
77387738
tok->next()->insertToken(tok->str());
7739+
tok->next()->isSplittedVarDeclEq(true);
77397740
tok->tokAt(2)->varId(tok->varId());
77407741
tok = tok->tokAt(2);
77417742
tok->insertToken("=");

Diff for: test/testtokenize.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ class TestTokenizer : public TestFixture {
284284
TEST_CASE(simplifyInitVar);
285285
TEST_CASE(simplifyInitVar2);
286286
TEST_CASE(simplifyInitVar3);
287+
TEST_CASE(simplifyInitVar4);
287288

288289
TEST_CASE(bitfields1);
289290
TEST_CASE(bitfields2);
@@ -4490,6 +4491,15 @@ class TestTokenizer : public TestFixture {
44904491
"}", tokenizeAndStringify(code));
44914492
}
44924493

4494+
void simplifyInitVar4() {
4495+
const char code[] = "void f() {\n"
4496+
" uint32_t x{0};\n"
4497+
"}";
4498+
ASSERT_EQUALS("void f ( ) {\n"
4499+
"uint32_t x ; x = 0 ;\n"
4500+
"}", tokenizeAndStringify(code));
4501+
}
4502+
44934503
void bitfields1() {
44944504
const char code1[] = "struct A { bool x : 1; };";
44954505
ASSERT_EQUALS("struct A { bool x ; } ;", tokenizeAndStringify(code1));

Diff for: test/testunusedvar.cpp

+16-4
Original file line numberDiff line numberDiff line change
@@ -2083,7 +2083,10 @@ class TestUnusedVar : public TestFixture {
20832083
"{\n"
20842084
" int i(0);\n"
20852085
"}");
2086-
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'i' is assigned a value that is never used.\n", errout_str());
2086+
ASSERT_EQUALS(
2087+
"[test.cpp:3]: (style) Variable 'i' is assigned a value that is never used.\n"
2088+
"[test.cpp:3]: (style) Variable 'i' is assigned a value that is never used.\n", // duplicate
2089+
errout_str());
20872090

20882091
// if a is undefined then Cppcheck can't determine if "int i(a)" is a
20892092
// * variable declaration
@@ -2099,7 +2102,10 @@ class TestUnusedVar : public TestFixture {
20992102
" int j = 0;\n"
21002103
" int i(j);\n"
21012104
"}");
2102-
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used.\n", errout_str());
2105+
ASSERT_EQUALS(
2106+
"[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used.\n"
2107+
"[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used.\n", // duplicate
2108+
errout_str());
21032109

21042110
functionVariableUsage("void foo()\n"
21052111
"{\n"
@@ -2138,7 +2144,10 @@ class TestUnusedVar : public TestFixture {
21382144
" int * j = Data;\n"
21392145
" int * i(j);\n"
21402146
"}");
2141-
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used.\n", errout_str());
2147+
ASSERT_EQUALS(
2148+
"[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used.\n"
2149+
"[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used.\n", // duplicate
2150+
errout_str());
21422151

21432152
functionVariableUsage("void foo()\n"
21442153
"{\n"
@@ -6841,7 +6850,10 @@ class TestUnusedVar : public TestFixture {
68416850
functionVariableUsage("void f(int* p) {\n"
68426851
" int* q{ p };\n"
68436852
"}\n");
6844-
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'q' is assigned a value that is never used.\n", errout_str());
6853+
ASSERT_EQUALS(
6854+
"[test.cpp:2]: (style) Variable 'q' is assigned a value that is never used.\n"
6855+
"[test.cpp:2]: (style) Variable 'q' is assigned a value that is never used.\n", // duplicate
6856+
errout_str());
68456857
}
68466858

68476859
void localvarRangeBasedFor() {

Diff for: test/testvarid.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -2917,11 +2917,11 @@ class TestVarID : public TestFixture {
29172917
}
29182918

29192919
void varid_cpp11initialization() {
2920-
ASSERT_EQUALS("1: int i@1 { 1 } ;\n"
2920+
ASSERT_EQUALS("1: int i@1 ; i@1 = 1 ;\n"
29212921
"2: std :: vector < int > vec@2 { 1 , 2 , 3 } ;\n"
29222922
"3: namespace n { int z@3 ; } ;\n"
29232923
"4: int & j@4 { i@1 } ;\n"
2924-
"5: int k@5 { 1 } ; int l@6 { 2 } ;\n",
2924+
"5: int k@5 ; k@5 = 1 ; int l@6 ; l@6 = 2 ;\n",
29252925
tokenize("int i{1};\n"
29262926
"std::vector<int> vec{1, 2, 3};\n"
29272927
"namespace n { int z; };\n"
@@ -2940,7 +2940,7 @@ class TestVarID : public TestFixture {
29402940
ASSERT_EQUALS("1: class A : public B , public C :: D , public E < F > :: G < H > {\n"
29412941
"2: int i@1 ;\n"
29422942
"3: A ( int i@2 ) : B { i@2 } , C :: D { i@2 } , E < F > :: G < H > { i@2 } , i@1 { i@2 } {\n"
2943-
"4: int j@3 { i@2 } ;\n"
2943+
"4: int j@3 ; j@3 = i@2 ;\n"
29442944
"5: }\n"
29452945
"6: } ;\n",
29462946
tokenize("class A: public B, public C::D, public E<F>::G<H> {\n"

0 commit comments

Comments
 (0)