Skip to content

Commit 5b89c84

Browse files
authored
got rid of unnecessary ExprIdToken constructors (#7728)
1 parent 107e226 commit 5b89c84

File tree

3 files changed

+54
-12
lines changed

3 files changed

+54
-12
lines changed

lib/programmemory.cpp

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ void ProgramMemory::setValue(const Token* expr, const ValueFlow::Value& value) {
7575
if (subexpr)
7676
(*mValues)[subexpr] = std::move(subvalue);
7777
}
78+
7879
const ValueFlow::Value* ProgramMemory::getValue(nonneg int exprid, bool impossible) const
7980
{
80-
const auto it = utils::as_const(*mValues).find(exprid);
81+
const auto it = find(exprid);
8182
const bool found = it != mValues->cend() && (impossible || !it->second.isImpossible());
8283
if (found)
8384
return &it->second;
@@ -154,18 +155,28 @@ void ProgramMemory::setUnknown(const Token* expr) {
154155
(*mValues)[expr].valueType = ValueFlow::Value::ValueType::UNINIT;
155156
}
156157

157-
bool ProgramMemory::hasValue(nonneg int exprid)
158+
bool ProgramMemory::hasValue(nonneg int exprid) const
158159
{
159-
return mValues->find(exprid) != mValues->end();
160+
const auto it = find(exprid);
161+
return it != mValues->cend();
160162
}
161163

162164
const ValueFlow::Value& ProgramMemory::at(nonneg int exprid) const {
163-
return mValues->at(exprid);
165+
const auto it = find(exprid);
166+
if (it == mValues->cend()) {
167+
throw std::out_of_range("ProgramMemory::at");
168+
}
169+
return it->second;
164170
}
171+
165172
ValueFlow::Value& ProgramMemory::at(nonneg int exprid) {
166173
copyOnWrite();
167174

168-
return mValues->at(exprid);
175+
const auto it = find(exprid);
176+
if (it == mValues->end()) {
177+
throw std::out_of_range("ProgramMemory::at");
178+
}
179+
return it->second;
169180
}
170181

171182
void ProgramMemory::erase_if(const std::function<bool(const ExprIdToken&)>& pred)
@@ -225,6 +236,21 @@ void ProgramMemory::copyOnWrite()
225236
mValues = std::make_shared<Map>(*mValues);
226237
}
227238

239+
ProgramMemory::Map::const_iterator ProgramMemory::find(nonneg int exprid) const
240+
{
241+
const auto& cvalues = utils::as_const(*mValues);
242+
return std::find_if(cvalues.cbegin(), cvalues.cend(), [&exprid](const Map::value_type& entry) {
243+
return entry.first.getExpressionId() == exprid;
244+
});
245+
}
246+
247+
ProgramMemory::Map::iterator ProgramMemory::find(nonneg int exprid)
248+
{
249+
return std::find_if(mValues->begin(), mValues->end(), [&exprid](const Map::value_type& entry) {
250+
return entry.first.getExpressionId() == exprid;
251+
});
252+
}
253+
228254
static ValueFlow::Value execute(const Token* expr, ProgramMemory& pm, const Settings& settings);
229255

230256
static bool evaluateCondition(MathLib::bigint r, const Token* condition, ProgramMemory& pm, const Settings& settings)
@@ -395,7 +421,7 @@ static void fillProgramMemoryFromAssignments(ProgramMemory& pm, const Token* tok
395421
bool setvar = false;
396422
const Token* vartok = tok2->astOperand1();
397423
for (const auto& p:vars) {
398-
if (p.first != vartok->exprId())
424+
if (p.first.getExpressionId() != vartok->exprId())
399425
continue;
400426
if (vartok == tok)
401427
continue;

lib/programmemory.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,9 @@ struct ExprIdToken {
4141
const Token* tok = nullptr;
4242
nonneg int exprid = 0;
4343

44-
ExprIdToken() = default;
4544
// cppcheck-suppress noExplicitConstructor
4645
// NOLINTNEXTLINE(google-explicit-constructor)
4746
ExprIdToken(const Token* tok);
48-
// TODO: Make this constructor only available from ProgramMemory
49-
// cppcheck-suppress noExplicitConstructor
50-
// NOLINTNEXTLINE(google-explicit-constructor)
51-
ExprIdToken(nonneg int exprid) : exprid(exprid) {}
5247

5348
nonneg int getExpressionId() const;
5449

@@ -117,7 +112,7 @@ struct CPPCHECKLIB ProgramMemory {
117112
void setUnknown(const Token* expr);
118113

119114
bool getTokValue(nonneg int exprid, const Token*& result) const;
120-
bool hasValue(nonneg int exprid);
115+
bool hasValue(nonneg int exprid) const;
121116

122117
const ValueFlow::Value& at(nonneg int exprid) const;
123118
ValueFlow::Value& at(nonneg int exprid);
@@ -150,6 +145,8 @@ struct CPPCHECKLIB ProgramMemory {
150145

151146
private:
152147
void copyOnWrite();
148+
Map::const_iterator find(nonneg int exprid) const;
149+
Map::iterator find(nonneg int exprid);
153150

154151
std::shared_ptr<Map> mValues;
155152
};

test/testprogrammemory.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class TestProgramMemory : public TestFixture {
3030
private:
3131
void run() override {
3232
TEST_CASE(copyOnWrite);
33+
TEST_CASE(hasValue);
34+
TEST_CASE(getValue);
35+
TEST_CASE(at);
3336
}
3437

3538
void copyOnWrite() const {
@@ -79,6 +82,22 @@ class TestProgramMemory : public TestFixture {
7982
ASSERT(v);
8083
ASSERT_EQUALS(41, v->intvalue);
8184
}
85+
86+
void hasValue() const {
87+
ProgramMemory pm;
88+
ASSERT(!pm.hasValue(123));
89+
}
90+
91+
void getValue() const {
92+
ProgramMemory pm;
93+
ASSERT(!pm.getValue(123));
94+
}
95+
96+
void at() const {
97+
ProgramMemory pm;
98+
ASSERT_THROW_EQUALS_2(pm.at(123), std::out_of_range, "ProgramMemory::at");
99+
ASSERT_THROW_EQUALS_2(utils::as_const(pm).at(123), std::out_of_range, "ProgramMemory::at");
100+
}
82101
};
83102

84103
REGISTER_TEST(TestProgramMemory)

0 commit comments

Comments
 (0)