Skip to content

Commit bce5666

Browse files
authored
pass Settings by reference into ProgramMemoryState (danmar#6977)
1 parent 648d1c8 commit bce5666

File tree

3 files changed

+23
-26
lines changed

3 files changed

+23
-26
lines changed

lib/programmemory.cpp

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -468,10 +468,8 @@ static ProgramMemory getInitialProgramState(const Token* tok,
468468
return pm;
469469
}
470470

471-
ProgramMemoryState::ProgramMemoryState(const Settings* s) : settings(s)
472-
{
473-
assert(settings != nullptr);
474-
}
471+
ProgramMemoryState::ProgramMemoryState(const Settings& s) : settings(s)
472+
{}
475473

476474
void ProgramMemoryState::replace(ProgramMemory pm, const Token* origin)
477475
{
@@ -493,9 +491,9 @@ void ProgramMemoryState::addState(const Token* tok, const ProgramMemory::Map& va
493491
{
494492
ProgramMemory pm = state;
495493
addVars(pm, vars);
496-
fillProgramMemoryFromConditions(pm, tok, *settings);
494+
fillProgramMemoryFromConditions(pm, tok, settings);
497495
ProgramMemory local = pm;
498-
fillProgramMemoryFromAssignments(pm, tok, *settings, local, vars);
496+
fillProgramMemoryFromAssignments(pm, tok, settings, local, vars);
499497
addVars(pm, vars);
500498
replace(std::move(pm), tok);
501499
}
@@ -506,7 +504,7 @@ void ProgramMemoryState::assume(const Token* tok, bool b, bool isEmpty)
506504
if (isEmpty)
507505
pm.setContainerSizeValue(tok, 0, b);
508506
else
509-
programMemoryParseCondition(pm, tok, nullptr, *settings, b);
507+
programMemoryParseCondition(pm, tok, nullptr, settings, b);
510508
const Token* origin = tok;
511509
const Token* top = tok->astTop();
512510
if (Token::Match(top->previous(), "for|while|if (") && !Token::simpleMatch(tok->astParent(), "?")) {
@@ -523,7 +521,7 @@ void ProgramMemoryState::removeModifiedVars(const Token* tok)
523521
const ProgramMemory& pm = state;
524522
auto eval = [&](const Token* cond) -> std::vector<MathLib::bigint> {
525523
ProgramMemory pm2 = pm;
526-
auto result = execute(cond, pm2, *settings);
524+
auto result = execute(cond, pm2, settings);
527525
if (isTrue(result))
528526
return {1};
529527
if (isFalse(result))
@@ -533,7 +531,7 @@ void ProgramMemoryState::removeModifiedVars(const Token* tok)
533531
state.erase_if([&](const ExprIdToken& e) {
534532
const Token* start = origins[e.getExpressionId()];
535533
const Token* expr = e.tok;
536-
if (!expr || findExpressionChangedSkipDeadCode(expr, start, tok, *settings, eval)) {
534+
if (!expr || findExpressionChangedSkipDeadCode(expr, start, tok, settings, eval)) {
537535
origins.erase(e.getExpressionId());
538536
return true;
539537
}
@@ -1276,14 +1274,14 @@ static void pruneConditions(std::vector<const Token*>& conds,
12761274

12771275
namespace {
12781276
struct Executor {
1279-
ProgramMemory* pm = nullptr;
1280-
const Settings* settings = nullptr;
1277+
ProgramMemory* pm;
1278+
const Settings& settings;
12811279
int fdepth = 4;
12821280
int depth = 10;
12831281

1284-
Executor(ProgramMemory* pm, const Settings* settings) : pm(pm), settings(settings)
1282+
Executor(ProgramMemory* pm, const Settings& settings) : pm(pm), settings(settings)
12851283
{
1286-
assert(settings != nullptr);
1284+
assert(pm != nullptr);
12871285
}
12881286

12891287
static ValueFlow::Value unknown() {
@@ -1395,7 +1393,7 @@ namespace {
13951393
continue;
13961394
for (const Token* cond1 : diffConditions1) {
13971395
auto it = std::find_if(diffConditions2.begin(), diffConditions2.end(), [&](const Token* cond2) {
1398-
return evalSameCondition(*pm, cond2, cond1, *settings);
1396+
return evalSameCondition(*pm, cond2, cond1, settings);
13991397
});
14001398
if (it == diffConditions2.end())
14011399
break;
@@ -1580,7 +1578,7 @@ namespace {
15801578
}
15811579
if (expr->exprId() > 0 && pm->hasValue(expr->exprId())) {
15821580
ValueFlow::Value result = utils::as_const(*pm).at(expr->exprId());
1583-
if (result.isImpossible() && result.isIntValue() && result.intvalue == 0 && isUsedAsBool(expr, *settings)) {
1581+
if (result.isImpossible() && result.isIntValue() && result.intvalue == 0 && isUsedAsBool(expr, settings)) {
15841582
result.intvalue = !result.intvalue;
15851583
result.setKnown();
15861584
}
@@ -1591,7 +1589,7 @@ namespace {
15911589
const Token* ftok = expr->previous();
15921590
const Function* f = ftok->function();
15931591
ValueFlow::Value result = unknown();
1594-
if (settings && expr->str() == "(") {
1592+
if (expr->str() == "(") {
15951593
std::vector<const Token*> tokArgs = getArguments(expr);
15961594
std::vector<ValueFlow::Value> args(tokArgs.size());
15971595
std::transform(
@@ -1619,7 +1617,7 @@ namespace {
16191617
BuiltinLibraryFunction lf = getBuiltinLibraryFunction(ftok->str());
16201618
if (lf)
16211619
return lf(args);
1622-
const std::string& returnValue = settings->library.returnValue(ftok);
1620+
const std::string& returnValue = settings.library.returnValue(ftok);
16231621
if (!returnValue.empty()) {
16241622
std::unordered_map<nonneg int, ValueFlow::Value> arg_map;
16251623
int argn = 0;
@@ -1628,20 +1626,19 @@ namespace {
16281626
arg_map[argn] = v;
16291627
argn++;
16301628
}
1631-
return evaluateLibraryFunction(arg_map, returnValue, *settings, ftok->isCpp());
1629+
return evaluateLibraryFunction(arg_map, returnValue, settings, ftok->isCpp());
16321630
}
16331631
}
16341632
}
16351633
// Check if function modifies argument
16361634
visitAstNodes(expr->astOperand2(), [&](const Token* child) {
16371635
if (child->exprId() > 0 && pm->hasValue(child->exprId())) {
16381636
ValueFlow::Value& v = pm->at(child->exprId());
1639-
assert(settings != nullptr);
16401637
if (v.valueType == ValueFlow::Value::ValueType::CONTAINER_SIZE) {
1641-
if (ValueFlow::isContainerSizeChanged(child, v.indirect, *settings))
1638+
if (ValueFlow::isContainerSizeChanged(child, v.indirect, settings))
16421639
v = unknown();
16431640
} else if (v.valueType != ValueFlow::Value::ValueType::UNINIT) {
1644-
if (isVariableChanged(child, v.indirect, *settings))
1641+
if (isVariableChanged(child, v.indirect, settings))
16451642
v = unknown();
16461643
}
16471644
}
@@ -1773,13 +1770,13 @@ namespace {
17731770

17741771
static ValueFlow::Value execute(const Token* expr, ProgramMemory& pm, const Settings& settings)
17751772
{
1776-
Executor ex{&pm, &settings};
1773+
Executor ex{&pm, settings};
17771774
return ex.execute(expr);
17781775
}
17791776

17801777
std::vector<ValueFlow::Value> execute(const Scope* scope, ProgramMemory& pm, const Settings& settings)
17811778
{
1782-
Executor ex{&pm, &settings};
1779+
Executor ex{&pm, settings};
17831780
return ex.execute(scope);
17841781
}
17851782

lib/programmemory.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ struct CPPCHECKLIB ProgramMemory {
157157
struct ProgramMemoryState {
158158
ProgramMemory state;
159159
std::map<nonneg int, const Token*> origins;
160-
const Settings* settings;
160+
const Settings& settings;
161161

162-
explicit ProgramMemoryState(const Settings* s);
162+
explicit ProgramMemoryState(const Settings& s);
163163

164164
void replace(ProgramMemory pm, const Token* origin = nullptr);
165165

lib/vf_analyzers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct ValueFlowAnalyzer : Analyzer {
5151
const Settings& settings;
5252
ProgramMemoryState pms;
5353

54-
explicit ValueFlowAnalyzer(const Settings& s) : settings(s), pms(&settings) {}
54+
explicit ValueFlowAnalyzer(const Settings& s) : settings(s), pms(settings) {}
5555

5656
virtual const ValueFlow::Value* getValue(const Token* tok) const = 0;
5757
virtual ValueFlow::Value* getValue(const Token* tok) = 0;

0 commit comments

Comments
 (0)