MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / valueFlowForLoop

Method valueFlowForLoop

test/testvalueflow.cpp:4558–4991  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4556 }
4557
4558 void valueFlowForLoop() {
4559 const char *code;
4560 ValueFlow::Value value;
4561
4562 code = "void f() {\n"
4563 " for (int x = 0; x < 10; x++)\n"
4564 " a[x] = 0;\n"
4565 "}";
4566 ASSERT_EQUALS(true, testValueOfX(code, 3U, 0));
4567 ASSERT_EQUALS(true, testValueOfX(code, 3U, 9));
4568 ASSERT_EQUALS(false, testValueOfX(code, 3U, 10));
4569
4570 code = "void f() {\n"
4571 " int x;\n"
4572 " for (x = 2; x < 1; x++)\n"
4573 " a[x] = 0;\n" // <- not 2
4574 " b = x;\n" // 2
4575 "}";
4576 ASSERT_EQUALS(false, testValueOfX(code, 4U, 2));
4577 ASSERT_EQUALS(true, testValueOfX(code, 5U, 2));
4578
4579 code = "void f() {\n"
4580 " int x;\n"
4581 " for (x = 2; x < 1; ++x)\n"
4582 " a[x] = 0;\n" // <- not 2
4583 " b = x;\n" // 2
4584 "}";
4585 ASSERT_EQUALS(false, testValueOfX(code, 4U, 2));
4586 ASSERT_EQUALS(true, testValueOfX(code, 5U, 2));
4587
4588 code = "enum AB {A,B};\n" // enum => handled by valueForLoop2
4589 "void f() {\n"
4590 " int x;\n"
4591 " for (x = 1; x < B; ++x)\n"
4592 " a[x] = 0;\n" // <- not 1
4593 "}";
4594 ASSERT_EQUALS(false, testValueOfX(code, 5U, 1));
4595
4596 code = "void f(int a) {\n"
4597 " for (int x = a; x < 10; x++)\n"
4598 " a[x] = 0;\n"
4599 "}";
4600 ASSERT_EQUALS(true, testValueOfX(code, 3U, 9));
4601
4602 code = "void f() {\n"
4603 " for (int x = 0; x < 5; x += 2)\n"
4604 " a[x] = 0;\n"
4605 "}";
4606 ASSERT_EQUALS(true, testValueOfX(code, 3U, 0));
4607 ASSERT_EQUALS(true, testValueOfX(code, 3U, 4));
4608
4609 code = "void f() {\n"
4610 " for (int x = 0; x < 10; x = x + 2)\n"
4611 " a[x] = 0;\n"
4612 "}";
4613 ASSERT_EQUALS(true, testValueOfX(code, 3U, 0));
4614 ASSERT_EQUALS(true, testValueOfX(code, 3U, 8));
4615 ASSERT_EQUALS(false, testValueOfX(code, 3U, 10));

Callers

nothing calls this directly

Calls 6

isPossibleMethod · 0.80
isKnownMethod · 0.80
isImpossibleMethod · 0.80
emptyMethod · 0.45
sizeMethod · 0.45
beginMethod · 0.45

Tested by

no test coverage detected