Skip to content

Commit 26e5459

Browse files
committed
Token: avoid unnecessary direct access of TokenImpl::mValues
1 parent b8b6b41 commit 26e5459

2 files changed

Lines changed: 42 additions & 45 deletions

File tree

lib/token.cpp

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,19 +1689,17 @@ void Token::printValueFlow(bool xml, std::ostream &out) const
16891689
else
16901690
out << "\n\n##Value flow" << std::endl;
16911691
for (const Token *tok = this; tok; tok = tok->next()) {
1692-
if (!tok->mImpl->mValues)
1693-
continue;
1694-
if (tok->mImpl->mValues->empty()) // Values might be removed by removeContradictions
1692+
if (tok->values().empty()) // Values might be removed by removeContradictions
16951693
continue;
16961694
if (xml)
1697-
out << " <values id=\"" << tok->mImpl->mValues << "\">" << std::endl;
1695+
out << " <values id=\"" << &tok->values() << "\">" << std::endl;
16981696
else if (line != tok->linenr())
16991697
out << "Line " << tok->linenr() << std::endl;
17001698
line = tok->linenr();
17011699
if (!xml) {
1702-
ValueFlow::Value::ValueKind valueKind = tok->mImpl->mValues->front().valueKind;
1700+
ValueFlow::Value::ValueKind valueKind = tok->values().front().valueKind;
17031701
bool same = true;
1704-
for (const ValueFlow::Value &value : *tok->mImpl->mValues) {
1702+
for (const ValueFlow::Value &value : tok->values()) {
17051703
if (value.valueKind != valueKind) {
17061704
same = false;
17071705
break;
@@ -1722,10 +1720,10 @@ void Token::printValueFlow(bool xml, std::ostream &out) const
17221720
break;
17231721
}
17241722
}
1725-
if (tok->mImpl->mValues->size() > 1U)
1723+
if (tok->values().size() > 1U)
17261724
out << '{';
17271725
}
1728-
for (const ValueFlow::Value &value : *tok->mImpl->mValues) {
1726+
for (const ValueFlow::Value &value : tok->values()) {
17291727
if (xml) {
17301728
out << " <value ";
17311729
switch (value.valueType) {
@@ -1785,14 +1783,14 @@ void Token::printValueFlow(bool xml, std::ostream &out) const
17851783
}
17861784

17871785
else {
1788-
if (&value != &tok->mImpl->mValues->front())
1786+
if (&value != &tok->values().front())
17891787
out << ",";
17901788
out << value.toString();
17911789
}
17921790
}
17931791
if (xml)
17941792
out << " </values>" << std::endl;
1795-
else if (tok->mImpl->mValues->size() > 1U)
1793+
else if (tok->values().size() > 1U)
17961794
out << '}' << std::endl;
17971795
else
17981796
out << std::endl;
@@ -1803,29 +1801,29 @@ void Token::printValueFlow(bool xml, std::ostream &out) const
18031801

18041802
const ValueFlow::Value * Token::getValueLE(const MathLib::bigint val, const Settings *settings) const
18051803
{
1806-
if (!mImpl->mValues)
1804+
if (values().empty())
18071805
return nullptr;
1808-
return ValueFlow::findValue(*mImpl->mValues, settings, [&](const ValueFlow::Value& v) {
1806+
return ValueFlow::findValue(values(), settings, [&](const ValueFlow::Value& v) {
18091807
return !v.isImpossible() && v.isIntValue() && v.intvalue <= val;
18101808
});
18111809
}
18121810

18131811
const ValueFlow::Value * Token::getValueGE(const MathLib::bigint val, const Settings *settings) const
18141812
{
1815-
if (!mImpl->mValues)
1813+
if (values().empty())
18161814
return nullptr;
1817-
return ValueFlow::findValue(*mImpl->mValues, settings, [&](const ValueFlow::Value& v) {
1815+
return ValueFlow::findValue(values(), settings, [&](const ValueFlow::Value& v) {
18181816
return !v.isImpossible() && v.isIntValue() && v.intvalue >= val;
18191817
});
18201818
}
18211819

18221820
const ValueFlow::Value * Token::getInvalidValue(const Token *ftok, nonneg int argnr, const Settings *settings) const
18231821
{
1824-
if (!mImpl->mValues || !settings)
1822+
if (values().empty() || !settings)
18251823
return nullptr;
18261824
const ValueFlow::Value *ret = nullptr;
18271825
std::list<ValueFlow::Value>::const_iterator it;
1828-
for (it = mImpl->mValues->begin(); it != mImpl->mValues->end(); ++it) {
1826+
for (it = values().begin(); it != values().end(); ++it) {
18291827
if (it->isImpossible())
18301828
continue;
18311829
if ((it->isIntValue() && !settings->library.isIntArgValid(ftok, argnr, it->intvalue)) ||
@@ -1847,12 +1845,12 @@ const ValueFlow::Value * Token::getInvalidValue(const Token *ftok, nonneg int ar
18471845

18481846
const Token *Token::getValueTokenMinStrSize(const Settings *settings, MathLib::bigint* path) const
18491847
{
1850-
if (!mImpl->mValues)
1848+
if (values().empty())
18511849
return nullptr;
18521850
const Token *ret = nullptr;
18531851
int minsize = INT_MAX;
18541852
std::list<ValueFlow::Value>::const_iterator it;
1855-
for (it = mImpl->mValues->begin(); it != mImpl->mValues->end(); ++it) {
1853+
for (it = values().begin(); it != values().end(); ++it) {
18561854
if (it->isTokValue() && it->tokvalue && it->tokvalue->tokType() == Token::eString) {
18571855
const int size = getStrSize(it->tokvalue, settings);
18581856
if (!ret || size < minsize) {
@@ -1868,12 +1866,12 @@ const Token *Token::getValueTokenMinStrSize(const Settings *settings, MathLib::b
18681866

18691867
const Token *Token::getValueTokenMaxStrLength() const
18701868
{
1871-
if (!mImpl->mValues)
1869+
if (values().empty())
18721870
return nullptr;
18731871
const Token *ret = nullptr;
18741872
int maxlength = 0;
18751873
std::list<ValueFlow::Value>::const_iterator it;
1876-
for (it = mImpl->mValues->begin(); it != mImpl->mValues->end(); ++it) {
1874+
for (it = values().begin(); it != values().end(); ++it) {
18771875
if (it->isTokValue() && it->tokvalue && it->tokvalue->tokType() == Token::eString) {
18781876
const int length = getStrLength(it->tokvalue);
18791877
if (!ret || length > maxlength) {
@@ -2088,7 +2086,7 @@ bool Token::addValue(const ValueFlow::Value &value)
20882086
{
20892087
if (value.isKnown() && mImpl->mValues) {
20902088
// Clear all other values of the same type since value is known
2091-
mImpl->mValues->remove_if([&](const ValueFlow::Value& x) {
2089+
removeValues([&](const ValueFlow::Value& x) {
20922090
return sameValueType(x, value);
20932091
});
20942092
}
@@ -2316,22 +2314,21 @@ std::shared_ptr<ScopeInfo2> Token::scopeInfo() const
23162314

23172315
bool Token::hasKnownIntValue() const
23182316
{
2319-
if (!mImpl->mValues)
2317+
if (values().empty())
23202318
return false;
2321-
return std::any_of(mImpl->mValues->begin(), mImpl->mValues->end(), [](const ValueFlow::Value& value) {
2319+
return std::any_of(values().begin(), values().end(), [](const ValueFlow::Value& value) {
23222320
return value.isKnown() && value.isIntValue();
23232321
});
23242322
}
23252323

23262324
bool Token::hasKnownValue() const
23272325
{
2328-
return mImpl->mValues && std::any_of(mImpl->mValues->begin(), mImpl->mValues->end(), std::mem_fn(&ValueFlow::Value::isKnown));
2326+
return !values().empty() && std::any_of(values().begin(), values().end(), std::mem_fn(&ValueFlow::Value::isKnown));
23292327
}
23302328

23312329
bool Token::hasKnownValue(ValueFlow::Value::ValueType t) const
23322330
{
2333-
return mImpl->mValues &&
2334-
std::any_of(mImpl->mValues->begin(), mImpl->mValues->end(), [&](const ValueFlow::Value& value) {
2331+
return !values().empty() && std::any_of(values().begin(), values().end(), [&](const ValueFlow::Value& value) {
23352332
return value.isKnown() && value.valueType == t;
23362333
});
23372334
}
@@ -2340,39 +2337,39 @@ bool Token::hasKnownSymbolicValue(const Token* tok) const
23402337
{
23412338
if (tok->exprId() == 0)
23422339
return false;
2343-
return mImpl->mValues &&
2344-
std::any_of(mImpl->mValues->begin(), mImpl->mValues->end(), [&](const ValueFlow::Value& value) {
2340+
return !values().empty() &&
2341+
std::any_of(values().begin(), values().end(), [&](const ValueFlow::Value& value) {
23452342
return value.isKnown() && value.isSymbolicValue() && value.tokvalue &&
2346-
value.tokvalue->exprId() == tok->exprId();
2343+
value.tokvalue->exprId() == tok->exprId();
23472344
});
23482345
}
23492346

23502347
const ValueFlow::Value* Token::getKnownValue(ValueFlow::Value::ValueType t) const
23512348
{
2352-
if (!mImpl->mValues)
2349+
if (values().empty())
23532350
return nullptr;
2354-
auto it = std::find_if(mImpl->mValues->begin(), mImpl->mValues->end(), [&](const ValueFlow::Value& value) {
2351+
auto it = std::find_if(values().begin(), values().end(), [&](const ValueFlow::Value& value) {
23552352
return value.isKnown() && value.valueType == t;
23562353
});
2357-
return it == mImpl->mValues->end() ? nullptr : &*it;
2354+
return it == values().end() ? nullptr : &*it;
23582355
}
23592356

23602357
const ValueFlow::Value* Token::getValue(const MathLib::bigint val) const
23612358
{
2362-
if (!mImpl->mValues)
2359+
if (values().empty())
23632360
return nullptr;
2364-
const auto it = std::find_if(mImpl->mValues->begin(), mImpl->mValues->end(), [=](const ValueFlow::Value& value) {
2361+
const auto it = std::find_if(values().begin(), values().end(), [=](const ValueFlow::Value& value) {
23652362
return value.isIntValue() && !value.isImpossible() && value.intvalue == val;
23662363
});
2367-
return it == mImpl->mValues->end() ? nullptr : &*it;
2364+
return it == values().end() ? nullptr : &*it;
23682365
}
23692366

23702367
const ValueFlow::Value* Token::getMaxValue(bool condition, MathLib::bigint path) const
23712368
{
2372-
if (!mImpl->mValues)
2369+
if (values().empty())
23732370
return nullptr;
23742371
const ValueFlow::Value* ret = nullptr;
2375-
for (const ValueFlow::Value& value : *mImpl->mValues) {
2372+
for (const ValueFlow::Value& value : values()) {
23762373
if (!value.isIntValue())
23772374
continue;
23782375
if (value.isImpossible())
@@ -2388,24 +2385,24 @@ const ValueFlow::Value* Token::getMaxValue(bool condition, MathLib::bigint path)
23882385

23892386
const ValueFlow::Value* Token::getMovedValue() const
23902387
{
2391-
if (!mImpl->mValues)
2388+
if (values().empty())
23922389
return nullptr;
2393-
const auto it = std::find_if(mImpl->mValues->begin(), mImpl->mValues->end(), [](const ValueFlow::Value& value) {
2390+
const auto it = std::find_if(values().begin(), values().end(), [](const ValueFlow::Value& value) {
23942391
return value.isMovedValue() && !value.isImpossible() &&
2395-
value.moveKind != ValueFlow::Value::MoveKind::NonMovedVariable;
2392+
value.moveKind != ValueFlow::Value::MoveKind::NonMovedVariable;
23962393
});
2397-
return it == mImpl->mValues->end() ? nullptr : &*it;
2394+
return it == values().end() ? nullptr : &*it;
23982395
}
23992396

24002397
// cppcheck-suppress unusedFunction
24012398
const ValueFlow::Value* Token::getContainerSizeValue(const MathLib::bigint val) const
24022399
{
2403-
if (!mImpl->mValues)
2400+
if (values().empty())
24042401
return nullptr;
2405-
const auto it = std::find_if(mImpl->mValues->begin(), mImpl->mValues->end(), [=](const ValueFlow::Value& value) {
2402+
const auto it = std::find_if(values().begin(), values().end(), [=](const ValueFlow::Value& value) {
24062403
return value.isContainerSizeValue() && !value.isImpossible() && value.intvalue == val;
24072404
});
2408-
return it == mImpl->mValues->end() ? nullptr : &*it;
2405+
return it == values().end() ? nullptr : &*it;
24092406
}
24102407

24112408
TokenImpl::~TokenImpl()

lib/token.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,7 @@ class CPPCHECKLIB Token {
11921192

11931193
const ValueFlow::Value* getKnownValue(ValueFlow::Value::ValueType t) const;
11941194
MathLib::bigint getKnownIntValue() const {
1195-
return mImpl->mValues->front().intvalue;
1195+
return values().front().intvalue;
11961196
}
11971197

11981198
const ValueFlow::Value* getValue(const MathLib::bigint val) const;

0 commit comments

Comments
 (0)