r344443 - [Analyzer] Iterator Checker - Part 10: Tests for iterators passed as parameter

Adam Balogh via cfe-commits cfe-commits at lists.llvm.org
Sat Oct 13 03:24:49 PDT 2018


Author: baloghadamsoftware
Date: Sat Oct 13 03:24:48 2018
New Revision: 344443

URL: http://llvm.org/viewvc/llvm-project?rev=344443&view=rev
Log:
[Analyzer] Iterator Checker - Part 10: Tests for iterators passed as parameter

In earlier Clang Static Analyzer versions `check::Bind() was not invoked for
parameter passing, so we needed a trick which is not needed anymore. However
add the tests to ensure its working.

Differential Revision: https::/reviews.llvm.org/D32906


Modified:
    cfe/trunk/test/Analysis/iterator-range.cpp
    cfe/trunk/test/Analysis/mismatched-iterator.cpp

Modified: cfe/trunk/test/Analysis/iterator-range.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/iterator-range.cpp?rev=344443&r1=344442&r2=344443&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/iterator-range.cpp (original)
+++ cfe/trunk/test/Analysis/iterator-range.cpp Sat Oct 13 03:24:48 2018
@@ -97,6 +97,28 @@ void copy_and_increase3(const std::vecto
     *i2; // expected-warning{{Iterator accessed outside of its range}}
 }
 
+template <class InputIterator, class T>
+InputIterator nonStdFind(InputIterator first, InputIterator last,
+                         const T &val) {
+  for (auto i = first; i != last; ++i) {
+    if (*i == val) {
+      return i;
+    }
+  }
+  return last;
+}
+
+void good_non_std_find(std::vector<int> &V, int e) {
+  auto first = nonStdFind(V.begin(), V.end(), e);
+  if (V.end() != first)
+    *first; // no-warning
+}
+
+void bad_non_std_find(std::vector<int> &V, int e) {
+  auto first = nonStdFind(V.begin(), V.end(), e);
+  *first; // expected-warning{{Iterator accessed outside of its range}}
+}
+
 void tricky(std::vector<int> &V, int e) {
   const auto first = V.begin();
   const auto comp1 = (first != V.end()), comp2 = (first == V.end());

Modified: cfe/trunk/test/Analysis/mismatched-iterator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/mismatched-iterator.cpp?rev=344443&r1=344442&r2=344443&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/mismatched-iterator.cpp (original)
+++ cfe/trunk/test/Analysis/mismatched-iterator.cpp Sat Oct 13 03:24:48 2018
@@ -144,6 +144,19 @@ void bad_overwrite(std::vector<int> &v1,
   v1.insert(i, n); // expected-warning{{Container accessed using foreign iterator argument}}
 }
 
+template<typename Container, typename Iterator>
+bool is_cend(Container cont, Iterator it) {
+  return it == cont.cend();
+}
+
+void good_empty(std::vector<int> &v) {
+  is_cend(v, v.cbegin()); // no-warning
+}
+
+void bad_empty(std::vector<int> &v1, std::vector<int> &v2) {
+  is_cend(v1, v2.cbegin()); // expected-warning at -8{{Iterators of different containers used where the same container is expected}}
+}
+
 void good_move(std::vector<int> &v1, std::vector<int> &v2) {
   const auto i0 = ++v2.cbegin();
   v1 = std::move(v2);




More information about the cfe-commits mailing list