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

Method knownValue

test/testvalueflow.cpp:5328–5744  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5326 }
5327
5328 void knownValue() {
5329 const char *code;
5330 ValueFlow::Value value;
5331
5332 ASSERT(valueOfTok("x = 1;", "1").isKnown());
5333
5334 // after assignment
5335 code = "void f() {\n"
5336 " int x = 1;\n"
5337 " return x + 2;\n" // <- known value
5338 "}";
5339 value = valueOfTok(code, "+");
5340 ASSERT_EQUALS(3, value.intvalue);
5341 ASSERT(value.isKnown());
5342
5343 {
5344 code = "void f() {\n"
5345 " int x = 15;\n"
5346 " if (x == 15) { x += 7; }\n" // <- condition is true
5347 "}";
5348 value = valueOfTok(code, "==");
5349 ASSERT_EQUALS(1, value.intvalue);
5350 ASSERT(value.isKnown());
5351
5352 code = "int f() {\n"
5353 " int a = 0, x = 0;\n"
5354 " a = index();\n"
5355 " if (a != 0)\n"
5356 " x = next();\n"
5357 " return x + 1;\n"
5358 "}\n";
5359 value = valueOfTok(code, "+");
5360 ASSERT(value.isPossible());
5361 }
5362
5363 code = "void f() {\n"
5364 " int x;\n"
5365 " if (ab) { x = 7; }\n"
5366 " return x + 2;\n" // <- possible value
5367 "}";
5368 value = valueOfTok(code, "+");
5369 ASSERT_EQUALS(9, value.intvalue);
5370 ASSERT(value.isPossible());
5371
5372 code = "void f(int c) {\n"
5373 " int x = 0;\n"
5374 " if (c) {} else { x++; }\n"
5375 " return x + 2;\n" // <- possible value
5376 "}";
5377 ASSERT(isNotKnownValues(code, "+"));
5378
5379 code = "void f() {\n"
5380 " int x = 0;\n"
5381 " dostuff(&x);\n"
5382 " if (x < 0) {}\n"
5383 "}\n";
5384 ASSERT(isNotKnownValues(code, "<"));
5385

Callers

nothing calls this directly

Calls 3

isKnownMethod · 0.80
isPossibleMethod · 0.80
ValueClass · 0.50

Tested by

no test coverage detected