Skip to content

Commit 5782bb9

Browse files
committed
Fix #13559: Tokenizer: remove simplification that converts \'int x(0)\' to \'int x; x = 0;\'
1 parent a1a415d commit 5782bb9

File tree

6 files changed

+48
-47
lines changed

6 files changed

+48
-47
lines changed

Diff for: lib/token.h

+8
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,13 @@ class CPPCHECKLIB Token {
725725
setFlag(fIsInitComma, b);
726726
}
727727

728+
bool isInitBracket() const {
729+
return getFlag(fIsInitBracket);
730+
}
731+
void isInitBracket(bool b) {
732+
setFlag(fIsInitBracket, b);
733+
}
734+
728735
// cppcheck-suppress unusedFunction
729736
bool isBitfield() const {
730737
return mImpl->mBits > 0;
@@ -1401,6 +1408,7 @@ class CPPCHECKLIB Token {
14011408
fIsSimplifiedTypedef = (1ULL << 40),
14021409
fIsFinalType = (1ULL << 41), // Is this a type with final specifier
14031410
fIsInitComma = (1ULL << 42), // Is this comma located inside some {..}. i.e: {1,2,3,4}
1411+
fIsInitBracket = (1ULL << 43), // Is this bracket used as a part of variable initialization i.e: int a{5}, b(2);
14041412
};
14051413

14061414
enum : std::uint8_t {

Diff for: lib/tokenize.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -6072,6 +6072,8 @@ void Tokenizer::dump(std::ostream &out) const
60726072
outs += " isAttributeMaybeUnused=\"true\"";
60736073
if (tok->isAttributeUnused())
60746074
outs += " isAttributeUnused=\"true\"";
6075+
if (tok->isInitBracket())
6076+
outs += " isInitBracket=\"true\"";
60756077
if (tok->hasAttributeAlignas()) {
60766078
const std::vector<std::string>& a = tok->getAttributeAlignas();
60776079
outs += " alignas=\"" + ErrorLogger::toxml(a[0]) + "\"";
@@ -7687,8 +7689,11 @@ void Tokenizer::simplifyInitVar()
76877689
if (tok->str() == "return")
76887690
continue;
76897691

7690-
if (Token::Match(tok, "class|struct|union| %type% *| %name% (|{ &| %any% )|} ;")) {
7691-
tok = initVar(tok);
7692+
if (Token::Match(tok, "%type% *|&| %name% (|{")) {
7693+
while(tok && !Token::Match(tok, "(|{"))
7694+
tok = tok->next();
7695+
tok->isInitBracket(true);
7696+
/* tok = initVar(tok);
76927697
} else if (Token::Match(tok, "%type% *| %name% ( %type% (")) {
76937698
const Token* tok2 = tok->tokAt(2);
76947699
if (!tok2->link())
@@ -7703,7 +7708,7 @@ void Tokenizer::simplifyInitVar()
77037708

77047709
const int numTokens = (Token::Match(tok, "class|struct|union")) ? 2 : 1;
77057710
TokenList::insertTokens(tok1, tok, numTokens);
7706-
tok = initVar(tok);
7711+
tok = initVar(tok); */
77077712
}
77087713
}
77097714
}

Diff for: test/testsimplifytokens.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,7 @@ class TestSimplifyTokens : public TestFixture {
10411041
// ticket #980
10421042
{
10431043
const char code[] = "void f() { int A(1),B(2),C=3,D,E(5),F=6; }";
1044-
const char expected[] = "void f ( ) { int A ; A = 1 ; int B ; B = 2 ; int C ; C = 3 ; int D ; int E ; E = 5 ; int F ; F = 6 ; }";
1044+
const char expected[] = "void f ( ) { int A ( 1 ) ; int B ( 2 ) ; int C ; C = 3 ; int D ; int E ( 5 ) ; int F ; F = 6 ; }";
10451045
ASSERT_EQUALS(expected, tok(code));
10461046
}
10471047

@@ -2540,10 +2540,10 @@ class TestSimplifyTokens : public TestFixture {
25402540
"3: namespace M { const int m@2 = 0 ; }\n"
25412541
"4: }\n"
25422542
"5: using namespace N ;\n"
2543-
"6: int i ; i = n@1 ;\n"
2543+
"6: int i ( n@1 ) ;\n"
25442544
"7: int j ( M :: m@2 ) ;\n"
25452545
"8: using namespace N :: M ;\n"
2546-
"9: int k ; k = m@2 ;\n"
2546+
"9: int k ( m@2 ) ;\n"
25472547
"10: int l ( N :: M :: m@2 ) ;\n";
25482548
ASSERT_EQUALS(exp, tokenizeDebugListing(code));
25492549
}

Diff for: test/testtokenize.cpp

+21-21
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ class TestTokenizer : public TestFixture {
585585
" int x1 = 1;\n"
586586
" int x2(x1);\n"
587587
"}\n";
588-
ASSERT_EQUALS("void f ( ) {\nint x1 ; x1 = 1 ;\nint x2 ; x2 = x1 ;\n}",
588+
ASSERT_EQUALS("void f ( ) {\nint x1 ; x1 = 1 ;\nint x2 ( x1 ) ;\n}",
589589
tokenizeAndStringify(code));
590590
}
591591

@@ -595,8 +595,8 @@ class TestTokenizer : public TestFixture {
595595
" int x2(x1);\n"
596596
"}\n";
597597
ASSERT_EQUALS("1: void f ( ) {\n"
598-
"2: int x1@1 ; x1@1 = g ( ) ;\n"
599-
"3: int x2@2 ; x2@2 = x1@1 ;\n"
598+
"2: int x1@1 ( g ( ) ) ;\n"
599+
"3: int x2@2 ( x1@1 ) ;\n"
600600
"4: }\n",
601601
tokenizeDebugListing(code));
602602
}
@@ -4292,79 +4292,79 @@ class TestTokenizer : public TestFixture {
42924292
void simplifyInitVar() {
42934293
{
42944294
const char code[] = "int i ; int p(0);";
4295-
ASSERT_EQUALS("int i ; int p ; p = 0 ;", tokenizeAndStringify(code));
4295+
ASSERT_EQUALS("int i ; int p ( 0 ) ;", tokenizeAndStringify(code));
42964296
ASSERT_EQUALS("", errout_str());
42974297
}
42984298

42994299
{
43004300
const char code[] = "int i; int *p(0);";
4301-
ASSERT_EQUALS("int i ; int * p ; p = 0 ;", tokenizeAndStringify(code));
4301+
ASSERT_EQUALS("int i ; int * p ( 0 ) ;", tokenizeAndStringify(code));
43024302
ASSERT_EQUALS("", errout_str());
43034303
}
43044304

43054305
{
43064306
const char code[] = "int p(0);";
4307-
ASSERT_EQUALS("int p ; p = 0 ;", tokenizeAndStringify(code));
4307+
ASSERT_EQUALS("int p ( 0 ) ;", tokenizeAndStringify(code));
43084308
ASSERT_EQUALS("", errout_str());
43094309
}
43104310

43114311
{
43124312
const char code[] = "int *p(0);";
4313-
ASSERT_EQUALS("int * p ; p = 0 ;", tokenizeAndStringify(code));
4313+
ASSERT_EQUALS("int * p ( 0 ) ;", tokenizeAndStringify(code));
43144314
ASSERT_EQUALS("", errout_str());
43154315
}
43164316

43174317
{
43184318
const char code[] = "int i ; int p(i);";
4319-
ASSERT_EQUALS("int i ; int p ; p = i ;", tokenizeAndStringify(code));
4319+
ASSERT_EQUALS("int i ; int p ( i ) ;", tokenizeAndStringify(code));
43204320
ASSERT_EQUALS("", errout_str());
43214321
}
43224322

43234323
{
43244324
const char code[] = "int i; int *p(&i);";
4325-
ASSERT_EQUALS("int i ; int * p ; p = & i ;", tokenizeAndStringify(code));
4325+
ASSERT_EQUALS("int i ; int * p ( & i ) ;", tokenizeAndStringify(code));
43264326
ASSERT_EQUALS("", errout_str());
43274327
}
43284328

43294329
{
43304330
const char code[] = "int i; void *p(&i);";
4331-
ASSERT_EQUALS("int i ; void * p ; p = & i ;", tokenizeAndStringify(code));
4331+
ASSERT_EQUALS("int i ; void * p ( & i ) ;", tokenizeAndStringify(code));
43324332
ASSERT_EQUALS("", errout_str());
43334333
}
43344334

43354335
{
43364336
const char code[] = "struct S { }; struct S s; struct S *p(&s);";
4337-
ASSERT_EQUALS("struct S { } ; struct S s ; struct S * p ; p = & s ;", tokenizeAndStringify(code));
4337+
ASSERT_EQUALS("struct S { } ; struct S s ; struct S * p ( & s ) ;", tokenizeAndStringify(code));
43384338
ASSERT_EQUALS("", errout_str());
43394339
}
43404340

43414341
{
43424342
const char code[] = "struct S { }; S s; S *p(&s);";
4343-
ASSERT_EQUALS("struct S { } ; S s ; S * p ; p = & s ;", tokenizeAndStringify(code));
4343+
ASSERT_EQUALS("struct S { } ; S s ; S * p ( & s ) ;", tokenizeAndStringify(code));
43444344
ASSERT_EQUALS("", errout_str());
43454345
}
43464346

43474347
{
43484348
const char code[] = "union S { int i; float f; }; union S s; union S *p(&s);";
4349-
ASSERT_EQUALS("union S { int i ; float f ; } ; union S s ; union S * p ; p = & s ;", tokenizeAndStringify(code));
4349+
ASSERT_EQUALS("union S { int i ; float f ; } ; union S s ; union S * p ( & s ) ;", tokenizeAndStringify(code));
43504350
ASSERT_EQUALS("", errout_str());
43514351
}
43524352

43534353
{
43544354
const char code[] = "union S { int i; float f; }; S s; S *p(&s);";
4355-
ASSERT_EQUALS("union S { int i ; float f ; } ; S s ; S * p ; p = & s ;", tokenizeAndStringify(code));
4355+
ASSERT_EQUALS("union S { int i ; float f ; } ; S s ; S * p ( & s ) ;", tokenizeAndStringify(code));
43564356
ASSERT_EQUALS("", errout_str());
43574357
}
43584358

43594359
{
43604360
const char code[] = "class C { }; class C c; class C *p(&c);";
4361-
ASSERT_EQUALS("class C { } ; class C c ; class C * p ; p = & c ;", tokenizeAndStringify(code));
4361+
ASSERT_EQUALS("class C { } ; class C c ; class C * p ( & c ) ;", tokenizeAndStringify(code));
43624362
ASSERT_EQUALS("", errout_str());
43634363
}
43644364

43654365
{
43664366
const char code[] = "class C { }; C c; C *p(&c);";
4367-
ASSERT_EQUALS("class C { } ; C c ; C * p ; p = & c ;", tokenizeAndStringify(code));
4367+
ASSERT_EQUALS("class C { } ; C c ; C * p ( & c ) ;", tokenizeAndStringify(code));
43684368
ASSERT_EQUALS("", errout_str());
43694369
}
43704370

@@ -4456,13 +4456,13 @@ class TestTokenizer : public TestFixture {
44564456

44574457
{
44584458
const char code[] = "class A { } ; A a; int foo(a);";
4459-
ASSERT_EQUALS("class A { } ; A a ; int foo ; foo = a ;", tokenizeAndStringify(code));
4459+
ASSERT_EQUALS("class A { } ; A a ; int foo ( a ) ;", tokenizeAndStringify(code));
44604460
ASSERT_EQUALS("", errout_str());
44614461
}
44624462

44634463
{
44644464
const char code[] = "int x(f());";
4465-
ASSERT_EQUALS("int x ; x = f ( ) ;", tokenizeAndStringify(code));
4465+
ASSERT_EQUALS("int x ( f ( ) ) ;", tokenizeAndStringify(code));
44664466
ASSERT_EQUALS("", errout_str());
44674467
}
44684468

@@ -4479,7 +4479,7 @@ class TestTokenizer : public TestFixture {
44794479
" unsigned int a(0),b(0);\n"
44804480
"}";
44814481
ASSERT_EQUALS("void f ( ) {\n"
4482-
"unsigned int a ; a = 0 ; unsigned int b ; b = 0 ;\n"
4482+
"unsigned int a ( 0 ) ; unsigned int b ( 0 ) ;\n"
44834483
"}", tokenizeAndStringify(code));
44844484
}
44854485

@@ -4488,7 +4488,7 @@ class TestTokenizer : public TestFixture {
44884488
" int *a(0),b(0);\n"
44894489
"}";
44904490
ASSERT_EQUALS("void f ( ) {\n"
4491-
"int * a ; a = 0 ; int b ; b = 0 ;\n"
4491+
"int * a ( 0 ) ; int b ( 0 ) ;\n"
44924492
"}", tokenizeAndStringify(code));
44934493
}
44944494

@@ -4497,7 +4497,7 @@ class TestTokenizer : public TestFixture {
44974497
" uint32_t x{0};\n"
44984498
"}";
44994499
ASSERT_EQUALS("void f ( ) {\n"
4500-
"uint32_t x ; x = 0 ;\n"
4500+
"uint32_t x { 0 } ;\n"
45014501
"}", tokenizeAndStringify(code));
45024502
}
45034503

Diff for: test/testunusedvar.cpp

+4-16
Original file line numberDiff line numberDiff line change
@@ -2096,10 +2096,7 @@ class TestUnusedVar : public TestFixture {
20962096
"{\n"
20972097
" int i(0);\n"
20982098
"}");
2099-
ASSERT_EQUALS(
2100-
"[test.cpp:3]: (style) Variable 'i' is assigned a value that is never used.\n"
2101-
"[test.cpp:3]: (style) Variable 'i' is assigned a value that is never used.\n", // duplicate
2102-
errout_str());
2099+
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'i' is assigned a value that is never used.\n", errout_str());
21032100

21042101
// if a is undefined then Cppcheck can't determine if "int i(a)" is a
21052102
// * variable declaration
@@ -2115,10 +2112,7 @@ class TestUnusedVar : public TestFixture {
21152112
" int j = 0;\n"
21162113
" int i(j);\n"
21172114
"}");
2118-
ASSERT_EQUALS(
2119-
"[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used.\n"
2120-
"[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used.\n", // duplicate
2121-
errout_str());
2115+
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used.\n", errout_str());
21222116

21232117
functionVariableUsage("void foo()\n"
21242118
"{\n"
@@ -2157,10 +2151,7 @@ class TestUnusedVar : public TestFixture {
21572151
" int * j = Data;\n"
21582152
" int * i(j);\n"
21592153
"}");
2160-
ASSERT_EQUALS(
2161-
"[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used.\n"
2162-
"[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used.\n", // duplicate
2163-
errout_str());
2154+
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used.\n", errout_str());
21642155

21652156
functionVariableUsage("void foo()\n"
21662157
"{\n"
@@ -6863,10 +6854,7 @@ class TestUnusedVar : public TestFixture {
68636854
functionVariableUsage("void f(int* p) {\n"
68646855
" int* q{ p };\n"
68656856
"}\n");
6866-
ASSERT_EQUALS(
6867-
"[test.cpp:2]: (style) Variable 'q' is assigned a value that is never used.\n"
6868-
"[test.cpp:2]: (style) Variable 'q' is assigned a value that is never used.\n", // duplicate
6869-
errout_str());
6857+
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'q' is assigned a value that is never used.\n", errout_str());
68706858
}
68716859

68726860
void localvarRangeBasedFor() {

Diff for: test/testvarid.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -2924,11 +2924,11 @@ class TestVarID : public TestFixture {
29242924
}
29252925

29262926
void varid_cpp11initialization() {
2927-
ASSERT_EQUALS("1: int i@1 ; i@1 = 1 ;\n"
2927+
ASSERT_EQUALS("1: int i@1 { 1 } ;\n"
29282928
"2: std :: vector < int > vec@2 { 1 , 2 , 3 } ;\n"
29292929
"3: namespace n { int z@3 ; } ;\n"
29302930
"4: int & j@4 { i@1 } ;\n"
2931-
"5: int k@5 ; k@5 = 1 ; int l@6 ; l@6 = 2 ;\n",
2931+
"5: int k@5 { 1 } ; int l@6 { 2 } ;\n",
29322932
tokenize("int i{1};\n"
29332933
"std::vector<int> vec{1, 2, 3};\n"
29342934
"namespace n { int z; };\n"
@@ -2947,7 +2947,7 @@ class TestVarID : public TestFixture {
29472947
ASSERT_EQUALS("1: class A : public B , public C :: D , public E < F > :: G < H > {\n"
29482948
"2: int i@1 ;\n"
29492949
"3: A ( int i@2 ) : B { i@2 } , C :: D { i@2 } , E < F > :: G < H > { i@2 } , i@1 { i@2 } {\n"
2950-
"4: int j@3 ; j@3 = i@2 ;\n"
2950+
"4: int j@3 { i@2 } ;\n"
29512951
"5: }\n"
29522952
"6: } ;\n",
29532953
tokenize("class A: public B, public C::D, public E<F>::G<H> {\n"
@@ -3818,7 +3818,7 @@ class TestVarID : public TestFixture {
38183818
const char expected[] = "1: class A : public B , public C :: D {\n"
38193819
"2: int i@1 ;\n"
38203820
"3: A ( int i@2 ) : B ( i@2 ) , C :: D ( i@2 ) , i@1 ( i@2 ) {\n"
3821-
"4: int j@3 ; j@3 = i@2 ;\n"
3821+
"4: int j@3 ( i@2 ) ;\n"
38223822
"5: }\n"
38233823
"6: } ;\n";
38243824
ASSERT_EQUALS(expected, tokenize(code));

0 commit comments

Comments
 (0)