Skip to content

Commit 43ed3fd

Browse files
committed
ProgramMemory: consistently added impossible parameter to lookup functions
1 parent fea194b commit 43ed3fd

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
@@ -97,9 +97,9 @@ const ValueFlow::Value* ProgramMemory::getValue(nonneg int exprid, bool impossib
9797
}
9898

9999
// cppcheck-suppress unusedFunction
100-
bool ProgramMemory::getIntValue(nonneg int exprid, MathLib::bigint& result) const
100+
bool ProgramMemory::getIntValue(nonneg int exprid, MathLib::bigint& result, bool impossible) const
101101
{
102-
const ValueFlow::Value* value = getValue(exprid);
102+
const ValueFlow::Value* value = getValue(exprid, impossible);
103103
if (value && value->isIntValue()) {
104104
result = value->intvalue;
105105
return true;
@@ -115,9 +115,9 @@ void ProgramMemory::setIntValue(const Token* expr, MathLib::bigint value, bool i
115115
setValue(expr, v);
116116
}
117117

118-
bool ProgramMemory::getTokValue(nonneg int exprid, const Token*& result) const
118+
bool ProgramMemory::getTokValue(nonneg int exprid, const Token*& result, bool impossible) const
119119
{
120-
const ValueFlow::Value* value = getValue(exprid);
120+
const ValueFlow::Value* value = getValue(exprid, impossible);
121121
if (value && value->isTokValue()) {
122122
result = value->tokvalue;
123123
return true;
@@ -126,18 +126,18 @@ bool ProgramMemory::getTokValue(nonneg int exprid, const Token*& result) const
126126
}
127127

128128
// cppcheck-suppress unusedFunction
129-
bool ProgramMemory::getContainerSizeValue(nonneg int exprid, MathLib::bigint& result) const
129+
bool ProgramMemory::getContainerSizeValue(nonneg int exprid, MathLib::bigint& result, bool impossible) const
130130
{
131-
const ValueFlow::Value* value = getValue(exprid);
131+
const ValueFlow::Value* value = getValue(exprid, impossible);
132132
if (value && value->isContainerSizeValue()) {
133133
result = value->intvalue;
134134
return true;
135135
}
136136
return false;
137137
}
138-
bool ProgramMemory::getContainerEmptyValue(nonneg int exprid, MathLib::bigint& result) const
138+
bool ProgramMemory::getContainerEmptyValue(nonneg int exprid, MathLib::bigint& result, bool impossible) const
139139
{
140-
const ValueFlow::Value* value = getValue(exprid, true);
140+
const ValueFlow::Value* value = getValue(exprid, impossible);
141141
if (value && value->isContainerSizeValue()) {
142142
if (value->isImpossible() && value->intvalue == 0) {
143143
result = false;
@@ -166,25 +166,25 @@ void ProgramMemory::setUnknown(const Token* expr) {
166166
(*mValues)[expr].valueType = ValueFlow::Value::ValueType::UNINIT;
167167
}
168168

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

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

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

186186
const auto it = find(exprid);
187-
if (it == mValues->end()) {
187+
if (it == mValues->end() && (impossible || !it->second.isImpossible())) {
188188
throw std::out_of_range("ProgramMemory::at");
189189
}
190190
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
@@ -715,7 +715,8 @@ struct ValueFlowAnalyzer : Analyzer {
715715
return {value->intvalue == 0};
716716
ProgramMemory pm = pms.get(tok, ctx, getProgramState());
717717
MathLib::bigint out = 0;
718-
if (pm.getContainerEmptyValue(tok->exprId(), out))
718+
// TODO: do we really went to return an impossible value?
719+
if (pm.getContainerEmptyValue(tok->exprId(), out, true))
719720
return {static_cast<int>(out)};
720721
return {};
721722
}

test/testprogrammemory.cpp

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

4444
ProgramMemory pm;
45-
const ValueFlow::Value* v = pm.getValue(id);
45+
const ValueFlow::Value* v = pm.getValue(id, false);
4646
ASSERT(!v);
4747
pm.setValue(tok, ValueFlow::Value{41});
4848

49-
v = pm.getValue(id);
49+
v = pm.getValue(id, false);
5050
ASSERT(v);
5151
ASSERT_EQUALS(41, v->intvalue);
5252

5353
// create a copy
5454
ProgramMemory pm2 = pm;
5555

5656
// make sure the value was copied
57-
v = pm2.getValue(id);
57+
v = pm2.getValue(id, false);
5858
ASSERT(v);
5959
ASSERT_EQUALS(41, v->intvalue);
6060

@@ -68,17 +68,17 @@ class TestProgramMemory : public TestFixture {
6868
pm3.setValue(tok, ValueFlow::Value{43});
6969

7070
// make sure the value was set
71-
v = pm2.getValue(id);
71+
v = pm2.getValue(id, false);
7272
ASSERT(v);
7373
ASSERT_EQUALS(42, v->intvalue);
7474

7575
// make sure the value was set
76-
v = pm3.getValue(id);
76+
v = pm3.getValue(id, false);
7777
ASSERT(v);
7878
ASSERT_EQUALS(43, v->intvalue);
7979

8080
// make sure the original value remains unchanged
81-
v = pm.getValue(id);
81+
v = pm.getValue(id, false);
8282
ASSERT(v);
8383
ASSERT_EQUALS(41, v->intvalue);
8484
}
@@ -90,7 +90,7 @@ class TestProgramMemory : public TestFixture {
9090

9191
void getValue() const {
9292
ProgramMemory pm;
93-
ASSERT(!pm.getValue(123));
93+
ASSERT(!pm.getValue(123, false));
9494
}
9595

9696
void at() const {

0 commit comments

Comments
 (0)