Skip to content

Commit 43adf75

Browse files
authored
fix #13861: Tokenizer/ValueFlow: wrong handling of struct initialization (danmar#7604)
1 parent 5e30dfb commit 43adf75

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

lib/tokenize.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,6 +2257,9 @@ void Tokenizer::simplifyTypedefCpp()
22572257
if (!tok2->next())
22582258
syntaxError(tok2);
22592259

2260+
if (Token::Match(tok2, "] ; %name% = {") && tok2->next()->isSplittedVarDeclEq())
2261+
tok2->deleteNext(2);
2262+
22602263
if (tok2->str() == "=") {
22612264
if (tok2->strAt(1) == "{")
22622265
tok2 = tok2->linkAt(1)->next();

test/testnullpointer.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ class TestNullPointer : public TestFixture {
141141
TEST_CASE(nullpointer102);
142142
TEST_CASE(nullpointer103);
143143
TEST_CASE(nullpointer104); // #13881
144+
TEST_CASE(nullpointer105); // #13861
144145
TEST_CASE(nullpointer_addressOf); // address of
145146
TEST_CASE(nullpointerSwitch); // #2626
146147
TEST_CASE(nullpointer_cast); // #4692
@@ -2929,6 +2930,17 @@ class TestNullPointer : public TestFixture {
29292930
ASSERT_EQUALS("", errout_str());
29302931
}
29312932

2933+
void nullpointer105() // #13861
2934+
{
2935+
check("struct AB { int a; int b; };\n"
2936+
"namespace ns { typedef AB S[10]; }\n"
2937+
"void foo(void) {\n"
2938+
" ns::S x = {0};\n"
2939+
" x[1].a = 2;\n"
2940+
"}\n");
2941+
ASSERT_EQUALS("", errout_str());
2942+
}
2943+
29322944
void nullpointer_addressOf() { // address of
29332945
check("void f() {\n"
29342946
" struct X *x = 0;\n"

test/testtokenize.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class TestTokenizer : public TestFixture {
8181
TEST_CASE(tokenize39); // #9771
8282
TEST_CASE(tokenize40); // #13181
8383
TEST_CASE(tokenize41); // #13847
84+
TEST_CASE(tokenize42); // #13861
8485

8586
TEST_CASE(validate);
8687

@@ -878,6 +879,22 @@ class TestTokenizer : public TestFixture {
878879
(void)errout_str();
879880
}
880881

882+
void tokenize42() { // #13861
883+
const char code[] = "struct AB { int a; int b; };\n"
884+
"namespace ns { typedef AB S[10]; }\n"
885+
"void foo(void) {\n"
886+
" ns::S x = {0};\n"
887+
" x[1].a = 2;\n"
888+
"}\n";
889+
ASSERT_EQUALS("struct AB { int a ; int b ; } ;\n"
890+
"\n"
891+
"void foo ( ) {\n"
892+
"AB x [ 10 ] = { 0 } ;\n"
893+
"x [ 1 ] . a = 2 ;\n"
894+
"}", tokenizeAndStringify(code));
895+
(void)errout_str();
896+
}
897+
881898
void validate() {
882899
// C++ code in C file
883900
ASSERT_THROW_INTERNAL(tokenizeAndStringify(";using namespace std;",false,Platform::Type::Native,false), SYNTAX);

0 commit comments

Comments
 (0)