Skip to content

Commit fec2914

Browse files
committed
Add tests for container changes
1 parent 26693df commit fec2914

4 files changed

Lines changed: 62 additions & 4 deletions

File tree

lib/valueflow.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5640,6 +5640,11 @@ struct ContainerVariableForwardAnalyzer : VariableForwardAnalyzer {
56405640
if (std::any_of(rhs->values().begin(), rhs->values().end(), [&](const ValueFlow::Value &rhsval) { return rhsval.isKnown() && rhsval.isContainerSizeValue(); }))
56415641
return Action::Read | Action::Write;
56425642
}
5643+
} else if (Token::Match(tok, "%name% . %name% (")) {
5644+
Library::Container::Action action = tok->valueType()->container->getAction(tok->strAt(2));
5645+
if (action == Library::Container::Action::PUSH || action == Library::Container::Action::POP)
5646+
return Action::Read | Action::Write;
5647+
const Token* arg = tok->tokAt(4);
56435648
}
56445649
return Action::None;
56455650
}
@@ -5664,6 +5669,12 @@ struct ContainerVariableForwardAnalyzer : VariableForwardAnalyzer {
56645669
}
56655670
}
56665671
}
5672+
} else if (Token::Match(tok, "%name% . %name% (")) {
5673+
Library::Container::Action action = tok->valueType()->container->getAction(tok->strAt(2));
5674+
if (action == Library::Container::Action::PUSH)
5675+
value->intvalue++;
5676+
if (action == Library::Container::Action::POP)
5677+
value->intvalue--;
56675678
}
56685679
}
56695680

@@ -5885,6 +5896,19 @@ static void valueFlowContainerSize(TokenList *tokenlist, SymbolDatabase* symbold
58855896
value.setKnown();
58865897
valueFlowContainerForward(containerTok->next(), containerTok->variable(), value, tokenlist);
58875898
}
5899+
} else if (Token::Match(tok, "%var% . %name% (") && tok->valueType() && tok->valueType()->container) {
5900+
Library::Container::Action action = tok->valueType()->container->getAction(tok->strAt(2));
5901+
if (action == Library::Container::Action::CLEAR) {
5902+
ValueFlow::Value value(0);
5903+
value.valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE;
5904+
value.setKnown();
5905+
valueFlowContainerForward(tok->next(), tok->variable(), value, tokenlist);
5906+
} else if (action == Library::Container::Action::RESIZE && tok->tokAt(4)->hasKnownIntValue()) {
5907+
ValueFlow::Value value(tok->tokAt(4)->values().front());
5908+
value.valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE;
5909+
value.setKnown();
5910+
valueFlowContainerForward(tok->next(), tok->variable(), value, tokenlist);
5911+
}
58885912
}
58895913
}
58905914
}

test/cfg/qt.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void QList1(QList<int> intListArg)
8888
QList<QString> qstringList2 = {"one", "two"};
8989
(void)qstringList2[1];
9090
qstringList2.clear();
91-
// TODO: cppcheck-suppress containerOutOfBounds #9243
91+
// cppcheck-suppress containerOutOfBounds
9292
(void)qstringList2[1];
9393

9494
QList<QString> qstringList3;
@@ -117,7 +117,7 @@ void QList1(QList<int> intListArg)
117117
qstringList4.append("a");
118118
(void)qstringList4[0];
119119
qstringList4.clear();
120-
// TODO: cppcheck-suppress containerOutOfBounds #9243
120+
// cppcheck-suppress containerOutOfBounds
121121
(void)qstringList4[0];
122122
}
123123

test/cfg/wxwidgets.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ wxString containerOutOfBounds_wxArrayString(void)
3333
wxArrayString a;
3434
a.Add("42");
3535
a.Clear();
36-
// cppcheck-suppress containerOutOfBounds
36+
// TODO: wxArrayString is defined to be a vector
37+
// TODO: cppcheck-suppress containerOutOfBounds
3738
return a[0];
3839
}
3940

@@ -42,7 +43,8 @@ int containerOutOfBounds_wxArrayInt(void)
4243
wxArrayInt a;
4344
a.Add(42);
4445
a.Clear();
45-
// cppcheck-suppress containerOutOfBounds
46+
// TODO: wxArrayString is defined to be a vector
47+
// TODO: cppcheck-suppress containerOutOfBounds
4648
return a[0];
4749
}
4850

test/testvalueflow.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,12 @@ class TestValueFlow : public TestFixture {
324324
return tok ? tok->values() : std::list<ValueFlow::Value>();
325325
}
326326

327+
std::list<ValueFlow::Value> tokenValues(const char code[], const char tokstr[], ValueFlow::Value::ValueType vt, const Settings *s = nullptr) {
328+
std::list<ValueFlow::Value> values = tokenValues(code, tokstr, s);
329+
values.remove_if([&](const ValueFlow::Value& v) { return v.valueType != vt; });
330+
return values;
331+
}
332+
327333
ValueFlow::Value valueOfTok(const char code[], const char tokstr[]) {
328334
std::list<ValueFlow::Value> values = tokenValues(code, tokstr);
329335
return values.size() == 1U && !values.front().isTokValue() ? values.front() : ValueFlow::Value();
@@ -4439,6 +4445,32 @@ class TestValueFlow : public TestFixture {
44394445
" x = s + s;\n"
44404446
"}";
44414447
ASSERT_EQUALS("", isKnownContainerSizeValue(tokenValues(code, "+"), 8));
4448+
4449+
code = "void f(const std::vector<int> &ints) {\n"
4450+
" ints.clear();\n"
4451+
" ints.front();\n"
4452+
"}";
4453+
ASSERT_EQUALS("", isKnownContainerSizeValue(tokenValues(code, "ints . front", ValueFlow::Value::CONTAINER_SIZE), 0));
4454+
4455+
code = "void f(const std::vector<int> &ints) {\n"
4456+
" ints.resize(3);\n"
4457+
" ints.front();\n"
4458+
"}";
4459+
ASSERT_EQUALS("", isKnownContainerSizeValue(tokenValues(code, "ints . front", ValueFlow::Value::CONTAINER_SIZE), 3));
4460+
4461+
code = "void f(const std::vector<int> &ints) {\n"
4462+
" ints.resize(3);\n"
4463+
" ints.push_back(3);\n"
4464+
" ints.front();\n"
4465+
"}";
4466+
ASSERT_EQUALS("", isKnownContainerSizeValue(tokenValues(code, "ints . front", ValueFlow::Value::CONTAINER_SIZE), 4));
4467+
4468+
code = "void f(const std::vector<int> &ints) {\n"
4469+
" ints.resize(3);\n"
4470+
" ints.pop_back();\n"
4471+
" ints.front();\n"
4472+
"}";
4473+
ASSERT_EQUALS("", isKnownContainerSizeValue(tokenValues(code, "ints . front", ValueFlow::Value::CONTAINER_SIZE), 2));
44424474
}
44434475

44444476
void valueFlowDynamicBufferSize() {

0 commit comments

Comments
 (0)