Skip to content

Commit 21cb0cf

Browse files
committed
Take simplifyIfNotNull and simplifyIfNot out of simplifyTokenList1 (first step for cppcheck-opensource#6072)
1 parent 1f97e30 commit 21cb0cf

11 files changed

Lines changed: 99 additions & 109 deletions

lib/tokenize.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3528,10 +3528,6 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
35283528
// simplify bit fields..
35293529
simplifyBitfields();
35303530

3531-
// Simplify '(p == 0)' to '(!p)'
3532-
simplifyIfNot();
3533-
simplifyIfNotNull();
3534-
35353531
// The simplifyTemplates have inner loops
35363532
if (_settings->terminated())
35373533
return false;
@@ -6119,15 +6115,15 @@ void Tokenizer::simplifyIfNotNull()
61196115
for (Token *tok = list.front(); tok; tok = tok->next()) {
61206116
Token *deleteFrom = nullptr;
61216117

6122-
// Remove 'x = (x != 0)'
6123-
if (Token::simpleMatch(tok, "= (")) {
6118+
// Remove 'x = x != 0;'
6119+
if (Token::simpleMatch(tok, "=")) {
61246120
if (Token::Match(tok->tokAt(-2), "[;{}] %name%")) {
61256121
const std::string& varname(tok->previous()->str());
61266122

6127-
if (Token::simpleMatch(tok->tokAt(2), (varname + " != 0 ) ;").c_str()) ||
6128-
Token::simpleMatch(tok->tokAt(2), ("0 != " + varname + " ) ;").c_str())) {
6123+
if (Token::simpleMatch(tok->next(), (varname + " != 0 ;").c_str()) ||
6124+
Token::simpleMatch(tok->next(), ("0 != " + varname + " ;").c_str())) {
61296125
tok = tok->tokAt(-2);
6130-
tok->deleteNext(8);
6126+
tok->deleteNext(6);
61316127
}
61326128
}
61336129
continue;

test/testbufferoverrun.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1927,7 +1927,7 @@ class TestBufferOverrun : public TestFixture {
19271927
" }\n"
19281928
" a[i - 1] = 0;\n"
19291929
" }\n"
1930-
"}");
1930+
"}", true, "test.cpp", false);
19311931
ASSERT_EQUALS("", errout.str());
19321932

19331933
check("void f() {\n"

test/testclass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3889,7 +3889,7 @@ class TestClass : public TestFixture {
38893889
" if( m_d != 0 )\n"
38903890
" return m_iRealVal / m_d;\n"
38913891
" return dRet;\n"
3892-
"};\n"
3892+
"};", nullptr, true, false
38933893
);
38943894
ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:4]: (style, inconclusive) Technically the member function 'A::dGetValue' can be const.\n", errout.str());
38953895
}

test/testcondition.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ class TestCondition : public TestFixture {
793793
void incorrectLogicOperator4() {
794794
check("void f(int x) {\n"
795795
" if (x && x != $0) {}\n"
796-
"}");
796+
"}", false);
797797
ASSERT_EQUALS("", errout.str());
798798
}
799799

test/testgarbage.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class TestGarbage : public TestFixture {
6969
TEST_CASE(garbageCode26);
7070
TEST_CASE(garbageCode27);
7171
TEST_CASE(garbageCode28);
72+
TEST_CASE(garbageCode29);
7273

7374
TEST_CASE(garbageValueFlow);
7475
TEST_CASE(garbageSymbolDatabase);
@@ -379,6 +380,11 @@ class TestGarbage : public TestFixture {
379380
"};\n"), InternalError);
380381
}
381382

383+
void garbageCode29() {
384+
// ticket #2601 segmentation fault
385+
checkCode("|| #if #define <=");
386+
}
387+
382388
void garbageValueFlow() {
383389
// #6089
384390
const char* code = "{} int foo(struct, x1, struct x2, x3, int, x5, x6, x7)\n"

test/testnullpointer.cpp

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ class TestNullPointer : public TestFixture {
308308
" return;\n"
309309
" }\n"
310310
" if (!abc);\n"
311-
"}");
311+
"}", false, "test.cpp", false);
312312
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:5]: (warning) Possible null pointer dereference: abc - otherwise it is redundant to check it against null.\n", errout.str());
313313

314314
// TODO: False negative if member of member is dereferenced
@@ -323,7 +323,7 @@ class TestNullPointer : public TestFixture {
323323
" abc->a = 0;\n"
324324
" if (abc && abc->b == 0)\n"
325325
" ;\n"
326-
"}");
326+
"}", false, "test.cpp", false);
327327
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Possible null pointer dereference: abc - otherwise it is redundant to check it against null.\n", errout.str());
328328

329329
// ok dereferencing in a condition
@@ -560,7 +560,7 @@ class TestNullPointer : public TestFixture {
560560
"{\n"
561561
" if (*p == 0) { }\n"
562562
" if (!p) { }\n"
563-
"}");
563+
"}", false, "test.cpp", false);
564564
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
565565

566566
// no error
@@ -597,7 +597,7 @@ class TestNullPointer : public TestFixture {
597597
" int a = 2 * x;"
598598
" if (x == 0)\n"
599599
" ;\n"
600-
"}", true);
600+
"}", true, "test.cpp", false);
601601
ASSERT_EQUALS("", errout.str());
602602

603603
check("void foo(int *p)\n"
@@ -823,12 +823,12 @@ class TestNullPointer : public TestFixture {
823823
check("void f(struct ABC *abc) {\n"
824824
" WARN_ON(!abc || abc->x == 0);\n"
825825
" if (!abc) { }\n"
826-
"}");
826+
"}", false, "test.cpp", false);
827827
ASSERT_EQUALS("", errout.str());
828828
check("void f(struct ABC *abc) {\n"
829829
" WARN_ON(!abc || abc->x == 7);\n"
830830
" if (!abc) { }\n"
831-
"}");
831+
"}", false, "test.cpp", false);
832832
ASSERT_EQUALS("", errout.str());
833833

834834
// #3425 - false positives when there are macros
@@ -1343,34 +1343,34 @@ class TestNullPointer : public TestFixture {
13431343
" if (NULL == p) {\n"
13441344
" }\n"
13451345
" *p = 0;\n"
1346-
"}");
1346+
"}", false, "test.cpp", false);
13471347
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:2]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
13481348

13491349
check("void foo(char *p) {\n"
13501350
" if (p == NULL) {\n"
13511351
" }\n"
13521352
" *p = 0;\n"
1353-
"}");
1353+
"}", false, "test.cpp", false);
13541354
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:2]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
13551355

13561356
check("void foo(char *p) {\n"
13571357
" if (p == NULL) {\n"
13581358
" }\n"
13591359
" printf(\"%c\", *p);\n"
1360-
"}");
1360+
"}", false, "test.cpp", false);
13611361
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:2]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
13621362

13631363
check("void foo(char *p) {\n"
13641364
" if (p && *p == 0) {\n"
13651365
" }\n"
13661366
" printf(\"%c\", *p);\n"
1367-
"}");
1367+
"}", false, "test.cpp", false);
13681368
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:2]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
13691369

13701370
check("void foo(char *p) {\n"
13711371
" if (p && *p == 0) {\n"
13721372
" } else { *p = 0; }\n"
1373-
"}");
1373+
"}", false, "test.cpp", false);
13741374
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
13751375

13761376
check("void foo(char *p) {\n"
@@ -1500,7 +1500,7 @@ class TestNullPointer : public TestFixture {
15001500
" MACRO;\n"
15011501
" }\n"
15021502
" fred->a();\n"
1503-
"}");
1503+
"}", false, "test.cpp", false);
15041504
ASSERT_EQUALS("", errout.str());
15051505

15061506
// #2493 - switch
@@ -1513,7 +1513,7 @@ class TestNullPointer : public TestFixture {
15131513
" fred->a();\n"
15141514
" break;\n"
15151515
" };\n"
1516-
"}");
1516+
"}", false, "test.cpp", false);
15171517
ASSERT_EQUALS("", errout.str());
15181518

15191519
// #4118 - second if
@@ -1549,7 +1549,7 @@ class TestNullPointer : public TestFixture {
15491549
" else {\n"
15501550
" int b = *i;\n"
15511551
" }\n"
1552-
"}", true);
1552+
"}", true, "test.cpp", false);
15531553
ASSERT_EQUALS("", errout.str());
15541554

15551555
// #2696 - false positives nr 1
@@ -1611,7 +1611,7 @@ class TestNullPointer : public TestFixture {
16111611
" if (p == 0 && (p = malloc(10)) != 0) {\n"
16121612
" *p = 0;\n"
16131613
" }\n"
1614-
"}");
1614+
"}", false, "test.cpp", false);
16151615
ASSERT_EQUALS("", errout.str());
16161616

16171617
// check, assign and use
@@ -1620,7 +1620,7 @@ class TestNullPointer : public TestFixture {
16201620
" if (p == 0 && (p = malloc(10)) != a && (*p = a)) {\n"
16211621
" *p = 0;\n"
16221622
" }\n"
1623-
"}");
1623+
"}", false, "test.cpp", false);
16241624
ASSERT_EQUALS("", errout.str());
16251625

16261626
// check, and use
@@ -1629,7 +1629,7 @@ class TestNullPointer : public TestFixture {
16291629
" if (p == 0 && (*p = 0)) {\n"
16301630
" return;\n"
16311631
" }\n"
1632-
"}");
1632+
"}", false, "test.cpp", false);
16331633
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:3]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
16341634

16351635
// check, and use
@@ -1638,7 +1638,7 @@ class TestNullPointer : public TestFixture {
16381638
" if (p == 0 && p->x == 10) {\n"
16391639
" return;\n"
16401640
" }\n"
1641-
"}");
1641+
"}", false, "test.cpp", false);
16421642
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:3]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
16431643

16441644
// check, and use
@@ -1647,7 +1647,7 @@ class TestNullPointer : public TestFixture {
16471647
" if (p == 0 || p->x == 10) {\n"
16481648
" return;\n"
16491649
" }\n"
1650-
"}");
1650+
"}", false, "test.cpp", false);
16511651
ASSERT_EQUALS("", errout.str());
16521652

16531653
// check, and use
@@ -1656,15 +1656,15 @@ class TestNullPointer : public TestFixture {
16561656
" if (p == NULL && (*p = a)) {\n"
16571657
" return;\n"
16581658
" }\n"
1659-
"}");
1659+
"}", false, "test.cpp", false);
16601660
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:3]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
16611661

16621662
// check, and use
16631663
check("void f(struct X *p, int x) {\n"
16641664
" if (!p && x==1 || p && p->x==0) {\n"
16651665
" return;\n"
16661666
" }\n"
1667-
"}");
1667+
"}", false, "test.cpp", false);
16681668
ASSERT_EQUALS("", errout.str());
16691669

16701670
{
@@ -1673,17 +1673,17 @@ class TestNullPointer : public TestFixture {
16731673
" fred->x();\n"
16741674
"}";
16751675

1676-
check(code); // non-inconclusive
1676+
check(code, false, "test.cpp", false); // non-inconclusive
16771677
ASSERT_EQUALS("", errout.str());
16781678

1679-
check(code, true); // inconclusive
1679+
check(code, true, "test.cpp", false); // inconclusive
16801680
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (warning, inconclusive) Possible null pointer dereference: fred - otherwise it is redundant to check it against null.\n", errout.str());
16811681
}
16821682

16831683
check("void f(char *s) {\n" // #3358
16841684
" if (s==0);\n"
16851685
" strcpy(a, s?b:c);\n"
1686-
"}");
1686+
"}", false, "test.cpp", false);
16871687
ASSERT_EQUALS("", errout.str());
16881688

16891689
// sizeof
@@ -2054,7 +2054,7 @@ class TestNullPointer : public TestFixture {
20542054
" std::cin >> p;\n"
20552055
" std::cout << abc << p;\n"
20562056
" }\n"
2057-
"}");
2057+
"}", false, "test.cpp", false);
20582058
TODO_ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n"
20592059
"[test.cpp:4] -> [test.cpp:2]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n"
20602060
"[test.cpp:5] -> [test.cpp:2]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n"
@@ -2281,7 +2281,7 @@ class TestNullPointer : public TestFixture {
22812281
check("void f(int *p = 0) {\n"
22822282
" if (p != 0 && bar())\n"
22832283
" *p = 0;\n"
2284-
"}");
2284+
"}", false, "test.cpp", false);
22852285
ASSERT_EQUALS("", errout.str());
22862286

22872287
check("void f(int *p) {\n"
@@ -2292,21 +2292,21 @@ class TestNullPointer : public TestFixture {
22922292
check("void f(int *p = 0) {\n"
22932293
" if (p != 0)\n"
22942294
" *p = 0;\n"
2295-
"}");
2295+
"}", false, "test.cpp", false);
22962296
ASSERT_EQUALS("", errout.str());
22972297

22982298
check("void f(int *p = 0) {\n"
22992299
" int y;\n"
23002300
" if (p == 0)\n"
23012301
" p = &y;\n"
23022302
" *p = 0;\n"
2303-
"}");
2303+
"}", false, "test.cpp", false);
23042304
ASSERT_EQUALS("", errout.str());
23052305

23062306
check("void f(int *p = 0) {\n"
23072307
" if (a != 0)\n"
23082308
" *p = 0;\n"
2309-
"}");
2309+
"}", false, "test.cpp", false);
23102310
ASSERT_EQUALS("[test.cpp:3]: (warning) Possible null pointer dereference if the default parameter value is used: p\n", errout.str());
23112311

23122312
check("void f(int *p = 0) {\n"
@@ -2326,7 +2326,7 @@ class TestNullPointer : public TestFixture {
23262326
" return 0;\n"
23272327
" }\n"
23282328
" return *p;\n"
2329-
"}");
2329+
"}", false, "test.cpp", false);
23302330
ASSERT_EQUALS("", errout.str());
23312331

23322332
check("void f(int *p = 0) {\n"
@@ -2400,15 +2400,15 @@ class TestNullPointer : public TestFixture {
24002400
" init(&p);\n"
24012401
" }\n"
24022402
" *p = 0;\n"
2403-
"}");
2403+
"}", false, "test.cpp", false);
24042404
ASSERT_EQUALS("", errout.str());
24052405

24062406
check("void f(int *p = 0) {\n"
24072407
" if (p == 0) {\n"
24082408
" throw SomeException;\n"
24092409
" }\n"
24102410
" *p = 0;\n"
2411-
"}");
2411+
"}", false, "test.cpp", false);
24122412
ASSERT_EQUALS("", errout.str());
24132413

24142414
check("void foo(int *p = 0) {\n"

0 commit comments

Comments
 (0)