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

Method valueFlowFwdAnalysis

test/testvalueflow.cpp:4445–4535  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4443 }
4444
4445 void valueFlowFwdAnalysis() {
4446 const char *code;
4447 std::list<ValueFlow::Value> values;
4448
4449 code = "void f() {\n"
4450 " struct Foo foo;\n"
4451 " foo.x = 1;\n"
4452 " x = 0 + foo.x;\n" // <- foo.x is 1
4453 "}";
4454 values = removeSymbolicTok(tokenValues(code, "+"));
4455 ASSERT_EQUALS(1U, values.size());
4456 ASSERT_EQUALS(true, values.front().isKnown());
4457 ASSERT_EQUALS(true, values.front().isIntValue());
4458 ASSERT_EQUALS(1, values.front().intvalue);
4459
4460 code = "void f() {\n"
4461 " S s;\n"
4462 " s.x = 1;\n"
4463 " int y = 10;\n"
4464 " while (s.x < y)\n" // s.x does not have known value
4465 " s.x++;\n"
4466 "}";
4467 values = removeImpossible(tokenValues(code, "<"));
4468 ASSERT_EQUALS(1, values.size());
4469 ASSERT(values.front().isPossible());
4470 ASSERT_EQUALS(1, values.front().intvalue);
4471
4472 code = "void f() {\n"
4473 " S s;\n"
4474 " s.x = 37;\n"
4475 " int y = 10;\n"
4476 " while (s.x < y)\n" // s.x has a known value
4477 " y--;\n"
4478 "}";
4479 values = tokenValues(code, ". x <");
4480 ASSERT(values.size() == 1 &&
4481 values.front().isKnown() &&
4482 values.front().isIntValue() &&
4483 values.front().intvalue == 37);
4484
4485 code = "void f() {\n"
4486 " Hints hints;\n"
4487 " hints.x = 1;\n"
4488 " if (foo)\n"
4489 " hints.x = 2;\n"
4490 " x = 0 + foo.x;\n" // <- foo.x is possible 1, possible 2
4491 "}";
4492 values = removeSymbolicTok(tokenValues(code, "+"));
4493 TODO_ASSERT_EQUALS(2U, 0U, values.size()); // should be 2
4494
4495 // FP: Condition '*b>0' is always true
4496 code = "bool dostuff(const char *x, const char *y);\n"
4497 "void fun(char *s, int *b) {\n"
4498 " for (int i = 0; i < 42; ++i) {\n"
4499 " if (dostuff(s, \"1\")) {\n"
4500 " *b = 1;\n"
4501 " break;\n"
4502 " }\n"

Callers

nothing calls this directly

Calls 6

removeImpossibleFunction · 0.85
isKnownMethod · 0.80
frontMethod · 0.80
isPossibleMethod · 0.80
sizeMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected