Skip to content

Commit 3693a29

Browse files
author
Your Name
committed
Format
1 parent fd54aac commit 3693a29

8 files changed

Lines changed: 47 additions & 18 deletions

File tree

lib/forwardanalyzer.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -777,8 +777,8 @@ namespace {
777777
if (thenBranch.check) {
778778
// The condition is only "known" because of an earlier assumption, so the
779779
// skipped else block could still modify the value -> lower to possible
780-
if (!condTok->hasKnownIntValue() && hasElse && analyzeScope(elseBranch.endBlock).isModified() &&
781-
!analyzer->lowerToPossible())
780+
if (!condTok->hasKnownIntValue() && hasElse &&
781+
analyzeScope(elseBranch.endBlock).isModified() && !analyzer->lowerToPossible())
782782
return Break(Analyzer::Terminate::Bail);
783783
if (updateScope(thenBranch.endBlock, depth - 1) == Progress::Break)
784784
return Break();
@@ -808,7 +808,9 @@ namespace {
808808
// no else the false path continues past the closing brace, so record the
809809
// assumed state there (None).
810810
if (thenBranch.isDead())
811-
analyzer->assume(condTok, false, hasElse ? Analyzer::Assume::Pending : Analyzer::Assume::None);
811+
analyzer->assume(condTok,
812+
false,
813+
hasElse ? Analyzer::Assume::Pending : Analyzer::Assume::None);
812814
// The else block is traversed on the main path. If it kills the value
813815
// (modified) the main path stops, but the then-fork may still carry the
814816
// value forward, so defer the break until after the fork continues.

lib/programmemory.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,16 @@ ProgramMemory::Map::iterator ProgramMemory::find(nonneg int exprid)
262262
return mValues->find(ExprIdToken::create(exprid));
263263
}
264264

265-
static ValueFlow::Value execute(const Token* expr, ProgramMemory& pm, const Settings& settings, const ProgramMemory::Map& vars = {});
266-
267-
static bool evaluateCondition(MathLib::bigint r, const Token* condition, ProgramMemory& pm, const Settings& settings, const ProgramMemory::Map& vars = {})
265+
static ValueFlow::Value execute(const Token* expr,
266+
ProgramMemory& pm,
267+
const Settings& settings,
268+
const ProgramMemory::Map& vars = {});
269+
270+
static bool evaluateCondition(MathLib::bigint r,
271+
const Token* condition,
272+
ProgramMemory& pm,
273+
const Settings& settings,
274+
const ProgramMemory::Map& vars = {})
268275
{
269276
if (!condition)
270277
return false;
@@ -1320,7 +1327,8 @@ namespace {
13201327
}
13211328

13221329
// Is the tracked value for this expression available?
1323-
const ValueFlow::Value* getTrackedValue(const Token* expr) const {
1330+
const ValueFlow::Value* getTrackedValue(const Token* expr) const
1331+
{
13241332
if (!vars || expr->exprId() == 0)
13251333
return nullptr;
13261334
const auto it = vars->find(ExprIdToken::create(expr->exprId()));
@@ -1329,7 +1337,8 @@ namespace {
13291337

13301338
// Does the expression read a tracked value? If so, any value cached for it may be stale
13311339
// (the tracked value may have changed since), so it must be re-evaluated, not served cached.
1332-
bool dependsOnTrackedValue(const Token* expr) const {
1340+
bool dependsOnTrackedValue(const Token* expr) const
1341+
{
13331342
if (!vars || vars->empty())
13341343
return false;
13351344
return findAstNode(expr, [&](const Token* tok) {
@@ -1836,7 +1845,10 @@ namespace {
18361845
};
18371846
} // namespace
18381847

1839-
static ValueFlow::Value execute(const Token* expr, ProgramMemory& pm, const Settings& settings, const ProgramMemory::Map& vars)
1848+
static ValueFlow::Value execute(const Token* expr,
1849+
ProgramMemory& pm,
1850+
const Settings& settings,
1851+
const ProgramMemory::Map& vars)
18401852
{
18411853
Executor ex{&pm, settings};
18421854
ex.vars = &vars;

lib/programmemory.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,21 @@ void execute(const Token* expr,
193193
* \param pm program memory
194194
* \param vars optional tracked values that take precedence over the program memory
195195
*/
196-
bool conditionIsFalse(const Token* condition, ProgramMemory pm, const Settings& settings, const ProgramMemory::Map& vars = {});
196+
bool conditionIsFalse(const Token* condition,
197+
ProgramMemory pm,
198+
const Settings& settings,
199+
const ProgramMemory::Map& vars = {});
197200

198201
/**
199202
* Is condition always true when variable has given value?
200203
* \param condition top ast token in condition
201204
* \param pm program memory
202205
* \param vars optional tracked values that take precedence over the program memory
203206
*/
204-
bool conditionIsTrue(const Token* condition, ProgramMemory pm, const Settings& settings, const ProgramMemory::Map& vars = {});
207+
bool conditionIsTrue(const Token* condition,
208+
ProgramMemory pm,
209+
const Settings& settings,
210+
const ProgramMemory::Map& vars = {});
205211

206212
/**
207213
* Get program memory by looking backwards from given token.

lib/vf_analyzers.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,8 @@ struct ValueFlowAnalyzer : Analyzer {
727727
return {};
728728
}
729729

730-
void assume(const Token* tok, bool state, unsigned int flags) override {
730+
void assume(const Token* tok, bool state, unsigned int flags) override
731+
{
731732
bool isCondBlock = false;
732733
const Token* parent = tok->astParent();
733734
if (parent) {

test/testautovariables.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4848,7 +4848,9 @@ class TestAutoVariables : public TestFixture {
48484848
" return *iPtr;\n"
48494849
" return 0;\n"
48504850
"}");
4851-
ASSERT_EQUALS("[test.cpp:5:16] -> [test.cpp:7:10] -> [test.cpp:4:13] -> [test.cpp:8:17]: (error) Using pointer to local variable 'x' that is out of scope. [invalidLifetime]\n", errout_str());
4851+
ASSERT_EQUALS(
4852+
"[test.cpp:5:16] -> [test.cpp:7:10] -> [test.cpp:4:13] -> [test.cpp:8:17]: (error) Using pointer to local variable 'x' that is out of scope. [invalidLifetime]\n",
4853+
errout_str());
48524854

48534855
// #11753
48544856
check("int main(int argc, const char *argv[]) {\n"

test/testother.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11491,8 +11491,9 @@ class TestOther : public TestFixture {
1149111491
" x = a + b;\n"
1149211492
" return x;\n"
1149311493
"}\n");
11494-
ASSERT_EQUALS("[test.cpp:2:11] -> [test.cpp:3:9] -> [test.cpp:4:9]: (style) Variable 'x' is assigned an expression that holds the same value. [redundantAssignment]\n",
11495-
errout_str());
11494+
ASSERT_EQUALS(
11495+
"[test.cpp:2:11] -> [test.cpp:3:9] -> [test.cpp:4:9]: (style) Variable 'x' is assigned an expression that holds the same value. [redundantAssignment]\n",
11496+
errout_str());
1149611497
}
1149711498

1149811499
void varFuncNullUB() { // #4482

test/teststl.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,9 @@ class TestStl : public TestFixture {
368368
" if(b) ++x;\n"
369369
" return s[x];\n"
370370
"}");
371-
ASSERT_EQUALS("[test.cpp:5:13]: error: Out of bounds access in 's[x]', if 's' size is 6 and 'x' is 7 [containerOutOfBounds]\n", errout_str());
371+
ASSERT_EQUALS(
372+
"[test.cpp:5:13]: error: Out of bounds access in 's[x]', if 's' size is 6 and 'x' is 7 [containerOutOfBounds]\n",
373+
errout_str());
372374

373375
checkNormal("void f() {\n"
374376
" static const int N = 4;\n"

test/testuninitvar.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4284,7 +4284,8 @@ class TestUninitVar : public TestFixture {
42844284
" else y = 123;\n" // <- y is always initialized
42854285
" return y;\n"
42864286
"}");
4287-
ASSERT_EQUALS("", errout_str()); // #4560: fork-based condition analysis tracks x==0 -> else branch -> y initialized
4287+
ASSERT_EQUALS("",
4288+
errout_str()); // #4560: fork-based condition analysis tracks x==0 -> else branch -> y initialized
42884289

42894290
valueFlowUninit("void f(int x) {\n" // #3948
42904291
" int value;\n"
@@ -5632,7 +5633,9 @@ class TestUninitVar : public TestFixture {
56325633
" return;\n"
56335634
" if (!mightBeLarger) {}\n"
56345635
"}");
5635-
ASSERT_EQUALS("[test.cpp:5:9] -> [test.cpp:7:24] -> [test.cpp:14:10]: (warning) Uninitialized variable: mightBeLarger [uninitvar]\n", errout_str());
5636+
ASSERT_EQUALS(
5637+
"[test.cpp:5:9] -> [test.cpp:7:24] -> [test.cpp:14:10]: (warning) Uninitialized variable: mightBeLarger [uninitvar]\n",
5638+
errout_str());
56365639

56375640
// Same shape, but the later condition (dimensions == 0) is implied by the narrowing, so the
56385641
// early return fires on the uninitialized path and there must be no warning.

0 commit comments

Comments
 (0)