Skip to content

Commit 077e90a

Browse files
committed
ProgramMemory: consistently added impossible parameter to lookup functions
1 parent 9ab1585 commit 077e90a

File tree

4 files changed

+31
-30
lines changed

4 files changed

+31
-30
lines changed

lib/programmemory.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ const ValueFlow::Value* ProgramMemory::getValue(nonneg int exprid, bool impossib
9696
return nullptr;
9797
}
9898

99-
bool ProgramMemory::getIntValue(nonneg int exprid, MathLib::bigint& result) const
99+
bool ProgramMemory::getIntValue(nonneg int exprid, MathLib::bigint& result, bool impossible) const
100100
{
101-
const ValueFlow::Value* value = getValue(exprid);
101+
const ValueFlow::Value* value = getValue(exprid, impossible);
102102
if (value && value->isIntValue()) {
103103
result = value->intvalue;
104104
return true;
@@ -114,9 +114,9 @@ void ProgramMemory::setIntValue(const Token* expr, MathLib::bigint value, bool i
114114
setValue(expr, v);
115115
}
116116

117-
bool ProgramMemory::getTokValue(nonneg int exprid, const Token*& result) const
117+
bool ProgramMemory::getTokValue(nonneg int exprid, const Token*& result, bool impossible) const
118118
{
119-
const ValueFlow::Value* value = getValue(exprid);
119+
const ValueFlow::Value* value = getValue(exprid, impossible);
120120
if (value && value->isTokValue()) {
121121
result = value->tokvalue;
122122
return true;
@@ -125,18 +125,18 @@ bool ProgramMemory::getTokValue(nonneg int exprid, const Token*& result) const
125125
}
126126

127127
// cppcheck-suppress unusedFunction
128-
bool ProgramMemory::getContainerSizeValue(nonneg int exprid, MathLib::bigint& result) const
128+
bool ProgramMemory::getContainerSizeValue(nonneg int exprid, MathLib::bigint& result, bool impossible) const
129129
{
130-
const ValueFlow::Value* value = getValue(exprid);
130+
const ValueFlow::Value* value = getValue(exprid, impossible);
131131
if (value && value->isContainerSizeValue()) {
132132
result = value->intvalue;
133133
return true;
134134
}
135135
return false;
136136
}
137-
bool ProgramMemory::getContainerEmptyValue(nonneg int exprid, MathLib::bigint& result) const
137+
bool ProgramMemory::getContainerEmptyValue(nonneg int exprid, MathLib::bigint& result, bool impossible) const
138138
{
139-
const ValueFlow::Value* value = getValue(exprid, true);
139+
const ValueFlow::Value* value = getValue(exprid, impossible);
140140
if (value && value->isContainerSizeValue()) {
141141
if (value->isImpossible() && value->intvalue == 0) {
142142
result = false;
@@ -165,25 +165,25 @@ void ProgramMemory::setUnknown(const Token* expr) {
165165
(*mValues)[expr].valueType = ValueFlow::Value::ValueType::UNINIT;
166166
}
167167

168-
bool ProgramMemory::hasValue(nonneg int exprid) const
168+
bool ProgramMemory::hasValue(nonneg int exprid, bool impossible) const
169169
{
170170
const auto it = find(exprid);
171-
return it != mValues->cend();
171+
return it != mValues->cend() && (impossible || !it->second.isImpossible());
172172
}
173173

174-
const ValueFlow::Value& ProgramMemory::at(nonneg int exprid) const {
174+
const ValueFlow::Value& ProgramMemory::at(nonneg int exprid, bool impossible) const {
175175
const auto it = find(exprid);
176-
if (it == mValues->cend()) {
176+
if (it == mValues->cend() && (impossible || !it->second.isImpossible())) {
177177
throw std::out_of_range("ProgramMemory::at");
178178
}
179179
return it->second;
180180
}
181181

182-
ValueFlow::Value& ProgramMemory::at(nonneg int exprid) {
182+
ValueFlow::Value& ProgramMemory::at(nonneg int exprid, bool impossible) {
183183
copyOnWrite();
184184

185185
const auto it = find(exprid);
186-
if (it == mValues->end()) {
186+
if (it == mValues->end() && (impossible || !it->second.isImpossible())) {
187187
throw std::out_of_range("ProgramMemory::at");
188188
}
189189
return it->second;

lib/programmemory.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,22 +109,22 @@ struct CPPCHECKLIB ProgramMemory {
109109
explicit ProgramMemory(Map values) : mValues(new Map(std::move(values))) {}
110110

111111
void setValue(const Token* expr, const ValueFlow::Value& value);
112-
const ValueFlow::Value* getValue(nonneg int exprid, bool impossible = false) const;
112+
const ValueFlow::Value* getValue(nonneg int exprid, bool impossible) const;
113113

114-
bool getIntValue(nonneg int exprid, MathLib::bigint& result) const;
114+
bool getIntValue(nonneg int exprid, MathLib::bigint& result, bool impossible = false) const;
115115
void setIntValue(const Token* expr, MathLib::bigint value, bool impossible = false);
116116

117-
bool getContainerSizeValue(nonneg int exprid, MathLib::bigint& result) const;
118-
bool getContainerEmptyValue(nonneg int exprid, MathLib::bigint& result) const;
117+
bool getContainerSizeValue(nonneg int exprid, MathLib::bigint& result, bool impossible = false) const;
118+
bool getContainerEmptyValue(nonneg int exprid, MathLib::bigint& result, bool impossible = false) const;
119119
void setContainerSizeValue(const Token* expr, MathLib::bigint value, bool isEqual = true);
120120

121121
void setUnknown(const Token* expr);
122122

123-
bool getTokValue(nonneg int exprid, const Token*& result) const;
124-
bool hasValue(nonneg int exprid) const;
123+
bool getTokValue(nonneg int exprid, const Token*& result, bool impossible = false) const;
124+
bool hasValue(nonneg int exprid, bool impossible = true) const;
125125

126-
const ValueFlow::Value& at(nonneg int exprid) const;
127-
ValueFlow::Value& at(nonneg int exprid);
126+
const ValueFlow::Value& at(nonneg int exprid, bool impossible = true) const;
127+
ValueFlow::Value& at(nonneg int exprid, bool impossible = true);
128128

129129
void erase_if(const std::function<bool(const ExprIdToken&)>& pred);
130130

lib/vf_analyzers.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,8 @@ struct ValueFlowAnalyzer : Analyzer {
718718
return {value->intvalue == 0};
719719
ProgramMemory pm = pms.get(tok, ctx, getProgramState());
720720
MathLib::bigint out = 0;
721-
if (pm.getContainerEmptyValue(tok->exprId(), out))
721+
// TODO: do we really went to return an impossible value?
722+
if (pm.getContainerEmptyValue(tok->exprId(), out, true))
722723
return {static_cast<int>(out)};
723724
return {};
724725
}

test/testprogrammemory.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,19 @@ class TestProgramMemory : public TestFixture {
4545
tok->exprId(id);
4646

4747
ProgramMemory pm;
48-
const ValueFlow::Value* v = pm.getValue(id);
48+
const ValueFlow::Value* v = pm.getValue(id, false);
4949
ASSERT(!v);
5050
pm.setValue(tok, ValueFlow::Value{41});
5151

52-
v = pm.getValue(id);
52+
v = pm.getValue(id, false);
5353
ASSERT(v);
5454
ASSERT_EQUALS(41, v->intvalue);
5555

5656
// create a copy
5757
ProgramMemory pm2 = pm;
5858

5959
// make sure the value was copied
60-
v = pm2.getValue(id);
60+
v = pm2.getValue(id, false);
6161
ASSERT(v);
6262
ASSERT_EQUALS(41, v->intvalue);
6363

@@ -71,17 +71,17 @@ class TestProgramMemory : public TestFixture {
7171
pm3.setValue(tok, ValueFlow::Value{43});
7272

7373
// make sure the value was set
74-
v = pm2.getValue(id);
74+
v = pm2.getValue(id, false);
7575
ASSERT(v);
7676
ASSERT_EQUALS(42, v->intvalue);
7777

7878
// make sure the value was set
79-
v = pm3.getValue(id);
79+
v = pm3.getValue(id, false);
8080
ASSERT(v);
8181
ASSERT_EQUALS(43, v->intvalue);
8282

8383
// make sure the original value remains unchanged
84-
v = pm.getValue(id);
84+
v = pm.getValue(id, false);
8585
ASSERT(v);
8686
ASSERT_EQUALS(41, v->intvalue);
8787
}
@@ -93,7 +93,7 @@ class TestProgramMemory : public TestFixture {
9393

9494
void getValue() const {
9595
ProgramMemory pm;
96-
ASSERT(!pm.getValue(123));
96+
ASSERT(!pm.getValue(123, false));
9797
}
9898

9999
void at() const {

0 commit comments

Comments
 (0)