Skip to content

Commit

Permalink
Fix #13557: Tokenizer: Use same simplification for 'int x{0}' and 'in…
Browse files Browse the repository at this point in the history
…t x(0)' (#7223)
  • Loading branch information
olabetskyi authored Jan 18, 2025
1 parent 6d3b47c commit 8f42596
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
5 changes: 3 additions & 2 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7686,7 +7686,7 @@ void Tokenizer::simplifyInitVar()
if (tok->str() == "return")
continue;

if (Token::Match(tok, "class|struct|union| %type% *| %name% ( &| %any% ) ;")) {
if (Token::Match(tok, "class|struct|union| %type% *| %name% (|{ &| %any% )|} ;")) {
tok = initVar(tok);
} else if (Token::Match(tok, "%type% *| %name% ( %type% (")) {
const Token* tok2 = tok->tokAt(2);
Expand Down Expand Up @@ -7730,12 +7730,13 @@ Token * Tokenizer::initVar(Token * tok)
// check initializer..
if (tok->tokAt(2)->isStandardType() || tok->strAt(2) == "void")
return tok;
if (!tok->tokAt(2)->isNumber() && !Token::Match(tok->tokAt(2), "%type% (") && tok->strAt(2) != "&" && tok->tokAt(2)->varId() == 0)
if (!tok->tokAt(2)->isNumber() && !Token::Match(tok->tokAt(2), "%type% (|{") && tok->strAt(2) != "&" && tok->tokAt(2)->varId() == 0)
return tok;

// insert '; var ='
tok->insertToken(";");
tok->next()->insertToken(tok->str());
tok->next()->isSplittedVarDeclEq(true);
tok->tokAt(2)->varId(tok->varId());
tok = tok->tokAt(2);
tok->insertToken("=");
Expand Down
10 changes: 10 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ class TestTokenizer : public TestFixture {
TEST_CASE(simplifyInitVar);
TEST_CASE(simplifyInitVar2);
TEST_CASE(simplifyInitVar3);
TEST_CASE(simplifyInitVar4);

TEST_CASE(bitfields1);
TEST_CASE(bitfields2);
Expand Down Expand Up @@ -4490,6 +4491,15 @@ class TestTokenizer : public TestFixture {
"}", tokenizeAndStringify(code));
}

void simplifyInitVar4() {
const char code[] = "void f() {\n"
" uint32_t x{0};\n"
"}";
ASSERT_EQUALS("void f ( ) {\n"
"uint32_t x ; x = 0 ;\n"
"}", tokenizeAndStringify(code));
}

void bitfields1() {
const char code1[] = "struct A { bool x : 1; };";
ASSERT_EQUALS("struct A { bool x ; } ;", tokenizeAndStringify(code1));
Expand Down
20 changes: 16 additions & 4 deletions test/testunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2083,7 +2083,10 @@ class TestUnusedVar : public TestFixture {
"{\n"
" int i(0);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'i' is assigned a value that is never used.\n", errout_str());
ASSERT_EQUALS(
"[test.cpp:3]: (style) Variable 'i' is assigned a value that is never used.\n"
"[test.cpp:3]: (style) Variable 'i' is assigned a value that is never used.\n", // duplicate
errout_str());

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

functionVariableUsage("void foo()\n"
"{\n"
Expand Down Expand Up @@ -2138,7 +2144,10 @@ class TestUnusedVar : public TestFixture {
" int * j = Data;\n"
" int * i(j);\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used.\n", errout_str());
ASSERT_EQUALS(
"[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used.\n"
"[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used.\n", // duplicate
errout_str());

functionVariableUsage("void foo()\n"
"{\n"
Expand Down Expand Up @@ -6841,7 +6850,10 @@ class TestUnusedVar : public TestFixture {
functionVariableUsage("void f(int* p) {\n"
" int* q{ p };\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'q' is assigned a value that is never used.\n", errout_str());
ASSERT_EQUALS(
"[test.cpp:2]: (style) Variable 'q' is assigned a value that is never used.\n"
"[test.cpp:2]: (style) Variable 'q' is assigned a value that is never used.\n", // duplicate
errout_str());
}

void localvarRangeBasedFor() {
Expand Down
6 changes: 3 additions & 3 deletions test/testvarid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2917,11 +2917,11 @@ class TestVarID : public TestFixture {
}

void varid_cpp11initialization() {
ASSERT_EQUALS("1: int i@1 { 1 } ;\n"
ASSERT_EQUALS("1: int i@1 ; i@1 = 1 ;\n"
"2: std :: vector < int > vec@2 { 1 , 2 , 3 } ;\n"
"3: namespace n { int z@3 ; } ;\n"
"4: int & j@4 { i@1 } ;\n"
"5: int k@5 { 1 } ; int l@6 { 2 } ;\n",
"5: int k@5 ; k@5 = 1 ; int l@6 ; l@6 = 2 ;\n",
tokenize("int i{1};\n"
"std::vector<int> vec{1, 2, 3};\n"
"namespace n { int z; };\n"
Expand All @@ -2940,7 +2940,7 @@ class TestVarID : public TestFixture {
ASSERT_EQUALS("1: class A : public B , public C :: D , public E < F > :: G < H > {\n"
"2: int i@1 ;\n"
"3: A ( int i@2 ) : B { i@2 } , C :: D { i@2 } , E < F > :: G < H > { i@2 } , i@1 { i@2 } {\n"
"4: int j@3 { i@2 } ;\n"
"4: int j@3 ; j@3 = i@2 ;\n"
"5: }\n"
"6: } ;\n",
tokenize("class A: public B, public C::D, public E<F>::G<H> {\n"
Expand Down

0 comments on commit 8f42596

Please sign in to comment.