Skip to content

Commit 21951e6

Browse files
committed
test negative shift folding via template simplifier
Move the regression test to testsimplifytemplate.cpp so it exercises the reachable path through simplifyNumericCalculations instead of calling the MathLib::value operators directly, which are not exported and broke the Windows/test link.
1 parent 5eadcbb commit 21951e6

2 files changed

Lines changed: 17 additions & 15 deletions

File tree

test/testmathlib.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ class TestMathLib : public TestFixture {
6464
TEST_CASE(tan);
6565
TEST_CASE(abs);
6666
TEST_CASE(toString);
67-
TEST_CASE(valueShift);
6867
}
6968

7069
void isGreater() const {
@@ -1512,20 +1511,6 @@ class TestMathLib : public TestFixture {
15121511
ASSERT_EQUALS("2.22507385851e-308", MathLib::toString(std::numeric_limits<double>::min()));
15131512
ASSERT_EQUALS("1.79769313486e+308", MathLib::toString(std::numeric_limits<double>::max()));
15141513
}
1515-
1516-
void valueShift() const {
1517-
// ordinary shifts
1518-
ASSERT_EQUALS("16", (MathLib::value("1") << MathLib::value("4")).str());
1519-
ASSERT_EQUALS("64", (MathLib::value("256") >> MathLib::value("2")).str());
1520-
1521-
// a large hex literal is not negative as a string but parses to a negative
1522-
// bigint; shifting it must not be performed (left shift of a negative value
1523-
// and shifting by a negative count are both undefined). the operand is
1524-
// returned unchanged, as already done for counts >= bigint_bits.
1525-
ASSERT_EQUALS("9223372036854775808U", (MathLib::value("0x8000000000000000") << MathLib::value("1")).str());
1526-
ASSERT_EQUALS("1", (MathLib::value("1") << MathLib::value("0x8000000000000000")).str());
1527-
ASSERT_EQUALS("1", (MathLib::value("1") >> MathLib::value("0x8000000000000000")).str());
1528-
}
15291514
};
15301515

15311516
REGISTER_TEST(TestMathLib)

test/testsimplifytemplate.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,8 @@ class TestSimplifyTemplate : public TestFixture {
320320

321321
TEST_CASE(templateArgPreserveType); // #13882 - type of template argument
322322

323+
TEST_CASE(template_shift_negative); // shift folding with a negative operand
324+
323325
TEST_CASE(dumpTemplateArgFrom);
324326
}
325327

@@ -6715,6 +6717,21 @@ class TestSimplifyTemplate : public TestFixture {
67156717
tok(code));
67166718
}
67176719

6720+
void template_shift_negative() {
6721+
// a large hex literal is not negative as a string but parses to a negative
6722+
// bigint, so folding the shift in simplifyNumericCalculations would left-shift
6723+
// a negative value / shift by a negative count, both UB. the operand must be
6724+
// returned unchanged. parentheses are needed so the numeric folding is reached.
6725+
const char code[] = "template <long long> struct S { };\n"
6726+
"S<(0x8000000000000000 << 1)> s1;\n"
6727+
"S<(1 << 0x8000000000000000)> s2;\n"
6728+
"S<(1 >> 0x8000000000000000)> s3;";
6729+
const char expected[] = "struct S<9223372036854775808U> ; struct S<1> ; "
6730+
"S<9223372036854775808U> s1 ; S<1> s2 ; S<1> s3 ; "
6731+
"struct S<9223372036854775808U> { } ; struct S<1> { } ;";
6732+
ASSERT_EQUALS(expected, tok(code));
6733+
}
6734+
67186735
void dumpTemplateArgFrom() {
67196736
const char code[] = "template<class T> void foo(T t) {}\n"
67206737
"foo<int>(23);";

0 commit comments

Comments
 (0)