@@ -137,6 +137,7 @@ class TestValueFlow : public TestFixture {
137137 TEST_CASE (valueFlowConditionExpressions);
138138
139139 TEST_CASE (valueFlowContainerSize);
140+ TEST_CASE (valueFlowContainerSizeIterator);
140141 TEST_CASE (valueFlowContainerElement);
141142
142143 TEST_CASE (valueFlowDynamicBufferSize);
@@ -7592,6 +7593,47 @@ class TestValueFlow : public TestFixture {
75927593 ASSERT (!isKnownContainerSizeValue (tokenValues (code, " m ." ), 0 ).empty ());
75937594 }
75947595
7596+ void valueFlowContainerSizeIterator () {
7597+ const char * code;
7598+
7599+ // valueFlowForwardConst: the container size is added to iterators of a const container
7600+ code = " void f() {\n "
7601+ " const std::vector<int> v{1, 2, 3};\n "
7602+ " auto it = v.begin();\n "
7603+ " if (it != v.end()) {}\n "
7604+ " }" ;
7605+ ASSERT_EQUALS (
7606+ " " ,
7607+ isKnownContainerSizeValue (tokenValues (code, " it !=" , ValueFlow::Value::ValueType::CONTAINER_SIZE ), 3 ));
7608+
7609+ // ..also to iterators created in place
7610+ code = " void f() {\n "
7611+ " const std::deque<int> d{1, 2, 3, 4, 5, 6};\n "
7612+ " if (std::equal(d.cbegin(), d.cend(), d.cbegin())) {}\n "
7613+ " }" ;
7614+ ASSERT_EQUALS (
7615+ " " ,
7616+ isKnownContainerSizeValue (tokenValues (code, " ( ) ," , ValueFlow::Value::ValueType::CONTAINER_SIZE ), 6 ));
7617+
7618+ // ..and to iterators of containers with a static size
7619+ code = " void f() {\n "
7620+ " std::array<int, 5> a;\n "
7621+ " auto it = a.begin();\n "
7622+ " if (it != a.end()) {}\n "
7623+ " }" ;
7624+ ASSERT_EQUALS (
7625+ " " ,
7626+ isKnownContainerSizeValue (tokenValues (code, " it !=" , ValueFlow::Value::ValueType::CONTAINER_SIZE ), 5 ));
7627+
7628+ // the size of another container is not added to the iterator
7629+ code = " void f(std::vector<int>& w) {\n "
7630+ " const std::vector<int> v{1, 2, 3};\n "
7631+ " auto it = w.begin();\n "
7632+ " if (it != w.end()) {}\n "
7633+ " }" ;
7634+ ASSERT (tokenValues (code, " it !=" , ValueFlow::Value::ValueType::CONTAINER_SIZE ).empty ());
7635+ }
7636+
75957637 void valueFlowContainerElement ()
75967638 {
75977639 const char * code;
0 commit comments