@@ -591,6 +591,75 @@ class TestCondition : public TestFixture {
591591 " }\n "
592592 " }" );
593593 ASSERT_EQUALS (" " , errout.str ());
594+
595+ // Various bitmask comparison checks
596+ // #7428 false negative: condition '(a&7)>7U' is always false
597+ // #7522 false positive: condition '(X|7)>=6' is correct
598+
599+ check (" void f() {\n "
600+ " assert( (a & 0x07) == 8U );\n " // statement always false
601+ " }" );
602+ ASSERT_EQUALS (" [test.cpp:2]: (style) Expression '(X & 0x7) == 0x8' is always false.\n " ,
603+ errout.str ());
604+
605+ check (" void f() {\n "
606+ " assert( (a & 0x07) == 7U );\n " // statement correct
607+ " assert( (a & 0x07) == 6U );\n " // statement correct
608+ " }" );
609+ ASSERT_EQUALS (" " , errout.str ());
610+
611+ check (" void f() {\n "
612+ " assert( (a | 0x07) == 8U );\n " // statement always false
613+ " }" );
614+ ASSERT_EQUALS (" [test.cpp:2]: (style) Expression '(X | 0x7) == 0x8' is always false.\n " ,
615+ errout.str ());
616+
617+ check (" void f() {\n "
618+ " assert( (a | 0x07) == 7U );\n " // statement correct
619+ " assert( (a | 0x07) == 23U );\n " // statement correct
620+ " }" );
621+ ASSERT_EQUALS (" " , errout.str ());
622+
623+ check (" void f() {\n "
624+ " assert( (a & 0x07) != 8U );\n " // statement always true
625+ " }" );
626+ ASSERT_EQUALS (" [test.cpp:2]: (style) Expression '(X & 0x7) != 0x8' is always true.\n " ,
627+ errout.str ());
628+
629+ check (" void f() {\n "
630+ " assert( (a & 0x07) != 7U );\n " // statement correct
631+ " assert( (a & 0x07) != 0U );\n " // statement correct
632+ " }" );
633+ ASSERT_EQUALS (" " , errout.str ());
634+
635+ check (" void f() {\n "
636+ " assert( (a | 0x07) != 8U );\n " // statement always true
637+ " }" );
638+ ASSERT_EQUALS (" [test.cpp:2]: (style) Expression '(X | 0x7) != 0x8' is always true.\n " ,
639+ errout.str ());
640+
641+ check (" void f() {\n "
642+ " assert( (a | 0x07) != 7U );\n " // statement correct
643+ " }" );
644+ ASSERT_EQUALS (" " , errout.str ());
645+
646+ // TRAC #7428 false negative: condition '(X&7)>7' is always false
647+ check (" void f() {\n "
648+ " assert( (a & 0x07) > 7U );\n " // statement always false
649+ " }" );
650+ ASSERT_EQUALS (" [test.cpp:2]: (style) Expression '(X & 0x7) > 0x7' is always false.\n " ,
651+ errout.str ());
652+
653+ check (" void f() {\n "
654+ " assert( (a & 0x07) > 6U );\n " // statement correct
655+ " }" );
656+ ASSERT_EQUALS (" " , errout.str ());
657+
658+ // TRAC #7522 false positive: condition '(X|7)>=6' is correct (X can be negative)
659+ check (" void f() {\n "
660+ " assert( (a | 0x07) >= 6U );\n " // statement correct (X can be negative)
661+ " }" );
662+ ASSERT_EQUALS (" " ,errout.str ());
594663 }
595664
596665
@@ -1651,29 +1720,6 @@ class TestCondition : public TestFixture {
16511720 " assert(x == 0);\n "
16521721 " }" );
16531722 ASSERT_EQUALS (" " , errout.str ());
1654-
1655- // TRAC #7428 false negative: Statement is always false
1656- check (" void f() {\n "
1657- " assert( (a & 0x07) == 8U );\n " // statement always false, because 7 == 8 is false
1658- " assert( (a & 0x07) > 7U );\n " // statement always false, because 7 > 7 is false
1659- " assert( (a | 0x07) < 7U );\n " // statement always false, because 7 < 7 is false
1660- " assert( (a & 0x07) > 8U );\n " // statement always false, because 7 > 8 is false
1661- " assert( (a | 0x07) < 6U );\n " // statement always false, because 7 < 6 is false
1662- " assert( (a & 0x07) >= 7U );\n " // statement correct
1663- " assert( (a | 0x07) <= 7U );\n " // statement correct
1664- " assert( (a & 0x07) >= 8U );\n " // statement always false, because 7 >= 8 is false
1665- " assert( (a | 0x07) <= 6U );\n " // statement always false, because 7 <= 6 is false
1666- " assert( (a & 0x07) > 3U );\n " // statement correct
1667- " assert( (a | 0x07) < 9U );\n " // statement correct
1668- " }" );
1669- ASSERT_EQUALS (" [test.cpp:2]: (style) Expression '(X & 0x7) == 0x8' is always false.\n "
1670- " [test.cpp:3]: (style) Expression '(X & 0x7) > 0x7' is always false.\n "
1671- " [test.cpp:4]: (style) Expression '(X | 0x7) < 0x7' is always false.\n "
1672- " [test.cpp:5]: (style) Expression '(X & 0x7) > 0x8' is always false.\n "
1673- " [test.cpp:6]: (style) Expression '(X | 0x7) < 0x6' is always false.\n "
1674- " [test.cpp:9]: (style) Expression '(X & 0x7) >= 0x8' is always false.\n "
1675- " [test.cpp:10]: (style) Expression '(X | 0x7) <= 0x6' is always false.\n " ,
1676- errout.str ());
16771723 }
16781724
16791725 void checkInvalidTestForOverflow () {
0 commit comments