Merge pull request #17722 from pemmanuelviel:pev--replace-asserts
authorpemmanuelviel <p.emmanuel.viel@gmail.com>
Sat, 4 Jul 2020 17:15:44 +0000 (19:15 +0200)
committerGitHub <noreply@github.com>
Sat, 4 Jul 2020 17:15:44 +0000 (20:15 +0300)
* Clean: replace C style asserts by CV_Assert and CV_DbgAssert

* Try fixing warning on Windows compilation

* Another way trying to fix warnings on Win

* Fixing warnings with some compilers:
Some compilers warn on systematic exit preventing to execute the code that follows.
This is why assert(0) that exits only in debug was working, but not CV_Assert or CV_Error
that exit both in release and debug, even if with different behavior.
In addition, other compilers complain when return 0 is removed from getKey(),
even if before we have a statement leading to systematic exit.

* Disable "unreachable code" warnings for Win compilers so we can use proper CV_Error

modules/flann/include/opencv2/flann/autotuned_index.h
modules/flann/include/opencv2/flann/flann_base.hpp
modules/flann/include/opencv2/flann/hierarchical_clustering_index.h
modules/flann/include/opencv2/flann/index_testing.h
modules/flann/include/opencv2/flann/kdtree_index.h
modules/flann/include/opencv2/flann/kdtree_single_index.h
modules/flann/include/opencv2/flann/kmeans_index.h
modules/flann/include/opencv2/flann/lsh_index.h
modules/flann/include/opencv2/flann/lsh_table.h
modules/flann/include/opencv2/flann/nn_index.h
modules/flann/include/opencv2/flann/simplex_downhill.h

index eb4554f077654e1b4a02ce7bddad97ea48eb9216..54a60a73d687fb18839a6b5cec7a440384f80a91 100644 (file)
@@ -497,7 +497,7 @@ private:
         const int nn = 1;
         const size_t SAMPLE_COUNT = 1000;
 
-        assert(bestIndex_ != NULL); // must have a valid index
+        CV_Assert(bestIndex_ != NULL && "Requires a valid index"); // must have a valid index
 
         float speedup = 0;
 
index 83606d232f5071644842c81b194a7b0e0730c147..641fdb01e2cf978a2f9527bf12cfae929fdd1bb8 100644 (file)
@@ -34,7 +34,6 @@
 //! @cond IGNORED
 
 #include <vector>
-#include <cassert>
 #include <cstdio>
 
 #include "general.h"
index d830bc6a00ee76ef7e7cc0511a68bc0b858d1788..a52166d3c4cdf2c61536f169de625ba06fe4ad5c 100644 (file)
@@ -35,7 +35,6 @@
 
 #include <algorithm>
 #include <map>
-#include <cassert>
 #include <limits>
 #include <cmath>
 
@@ -153,7 +152,7 @@ private:
         int n = indices_length;
 
         int rnd = rand_int(n);
-        assert(rnd >=0 && rnd < n);
+        CV_DbgAssert(rnd >=0 && rnd < n);
 
         centers[0] = dsindices[rnd];
 
@@ -208,7 +207,7 @@ private:
 
         // Choose one random center and set the closestDistSq values
         int index = rand_int(n);
-        assert(index >=0 && index < n);
+        CV_DbgAssert(index >=0 && index < n);
         centers[0] = dsindices[index];
 
         // Computing distance^2 will have the advantage of even higher probability further to pick new centers
@@ -295,7 +294,7 @@ private:
 
         // Choose one random center and set the closestDistSq values
         int index = rand_int(n);
-        assert(index >=0 && index < n);
+        CV_DbgAssert(index >=0 && index < n);
         centers[0] = dsindices[index];
 
         for (int i = 0; i < n; i++) {
@@ -564,10 +563,10 @@ public:
             NodePtr node = branch.node;
             findNN(node, result, vec, checks, maxChecks, heap, checked);
         }
-        assert(result.full());
 
         delete heap;
 
+        CV_Assert(result.full());
     }
 
     IndexParams getParameters() const CV_OVERRIDE
index 47b6f0b86f36d91c11d39a2dcc17e007c1d77e4f..f3d147588d3e43ae79b536cf94ec50d317eba358 100644 (file)
@@ -34,7 +34,6 @@
 //! @cond IGNORED
 
 #include <cstring>
-#include <cassert>
 #include <cmath>
 
 #include "matrix.h"
index 472350516f0c95d66236769c92e17210792f565c..acc87a3198a8d687a82fb61662b04f2021d7810e 100644 (file)
@@ -35,7 +35,6 @@
 
 #include <algorithm>
 #include <map>
-#include <cassert>
 #include <cstring>
 
 #include "general.h"
@@ -433,7 +432,7 @@ private:
         if (trees_>0) {
             searchLevelExact(result, vec, tree_roots_[0], 0.0, epsError);
         }
-        assert(result.full());
+        CV_Assert(result.full());
     }
 
     /**
@@ -462,7 +461,7 @@ private:
 
         delete heap;
 
-        assert(result.full());
+        CV_Assert(result.full());
     }
 
 
index ce4c1c5b7e00458090e380322b2ac14237ee81a8..e571403b105903d89819fb656ba4b8179d134f7b 100644 (file)
@@ -35,7 +35,6 @@
 
 #include <algorithm>
 #include <map>
-#include <cassert>
 #include <cstring>
 
 #include "general.h"
@@ -214,11 +213,11 @@ public:
      */
     void knnSearch(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, int knn, const SearchParams& params) CV_OVERRIDE
     {
-        assert(queries.cols == veclen());
-        assert(indices.rows >= queries.rows);
-        assert(dists.rows >= queries.rows);
-        assert(int(indices.cols) >= knn);
-        assert(int(dists.cols) >= knn);
+        CV_Assert(queries.cols == veclen());
+        CV_Assert(indices.rows >= queries.rows);
+        CV_Assert(dists.rows >= queries.rows);
+        CV_Assert(int(indices.cols) >= knn);
+        CV_Assert(int(dists.cols) >= knn);
 
         KNNSimpleResultSet<DistanceType> resultSet(knn);
         for (size_t i = 0; i < queries.rows; i++) {
index 695ef6e8b14d48527a98686feab7d1405e125b5d..475f79b66aaf7dc51035f1e1fde4f9ca38c294e5 100644 (file)
@@ -35,7 +35,6 @@
 
 #include <algorithm>
 #include <map>
-#include <cassert>
 #include <limits>
 #include <cmath>
 
@@ -152,7 +151,7 @@ public:
         int n = indices_length;
 
         int rnd = rand_int(n);
-        assert(rnd >=0 && rnd < n);
+        CV_DbgAssert(rnd >=0 && rnd < n);
 
         centers[0] = indices[rnd];
 
@@ -207,7 +206,7 @@ public:
 
         // Choose one random center and set the closestDistSq values
         int index = rand_int(n);
-        assert(index >=0 && index < n);
+        CV_DbgAssert(index >=0 && index < n);
         centers[0] = indices[index];
 
         for (int i = 0; i < n; i++) {
@@ -502,9 +501,9 @@ public:
                 KMeansNodePtr node = branch.node;
                 findNN(node, result, vec, checks, maxChecks, heap);
             }
-            assert(result.full());
-
             delete heap;
+
+            CV_Assert(result.full());
         }
 
     }
index 86c0654051523c9d3e49c135e26f1d915e5deecb..9ac30136ff849f67e4932a0037dcef3f66eb5835 100644 (file)
@@ -38,7 +38,6 @@
 //! @cond IGNORED
 
 #include <algorithm>
-#include <cassert>
 #include <cstring>
 #include <map>
 #include <vector>
 #include "random.h"
 #include "saving.h"
 
+#ifdef _MSC_VER
+#pragma warning(push)
+#pragma warning(disable: 4702) //disable unreachable code
+#endif
+
 namespace cvflann
 {
 
@@ -191,11 +195,11 @@ public:
      */
     virtual void knnSearch(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, int knn, const SearchParams& params) CV_OVERRIDE
     {
-        assert(queries.cols == veclen());
-        assert(indices.rows >= queries.rows);
-        assert(dists.rows >= queries.rows);
-        assert(int(indices.cols) >= knn);
-        assert(int(dists.cols) >= knn);
+        CV_Assert(queries.cols == veclen());
+        CV_Assert(indices.rows >= queries.rows);
+        CV_Assert(dists.rows >= queries.rows);
+        CV_Assert(int(indices.cols) >= knn);
+        CV_Assert(int(dists.cols) >= knn);
 
 
         KNNUniqueResultSet<DistanceType> resultSet(knn);
@@ -391,6 +395,10 @@ private:
 };
 }
 
+#ifdef _MSC_VER
+#pragma warning(pop)
+#endif
+
 //! @endcond
 
 #endif //OPENCV_FLANN_LSH_INDEX_H_
index 8f5250171b776e880fb88f355d3bcb550fbf53b4..a189562d3af46496fa5dd87960979575e722529b 100644 (file)
 #include "dynamic_bitset.h"
 #include "matrix.h"
 
+#ifdef _MSC_VER
+#pragma warning(push)
+#pragma warning(disable: 4702) //disable unreachable code
+#endif
+
+
 namespace cvflann
 {
 
@@ -162,8 +168,7 @@ public:
     {
         feature_size_ = feature_size;
         CV_UNUSED(key_size);
-        std::cerr << "LSH is not implemented for that type" << std::endl;
-        assert(0);
+        CV_Error(cv::Error::StsUnsupportedFormat, "LSH is not implemented for that type" );
     }
 
     /** Add a feature to the table
@@ -243,8 +248,7 @@ public:
      */
     size_t getKey(const ElementType* /*feature*/) const
     {
-        std::cerr << "LSH is not implemented for that type" << std::endl;
-        assert(0);
+        CV_Error(cv::Error::StsUnsupportedFormat, "LSH is not implemented for that type" );
         return 0;
     }
 
@@ -510,6 +514,10 @@ inline LshStats LshTable<unsigned char>::getStats() const
 }
 }
 
+#ifdef _MSC_VER
+#pragma warning(pop)
+#endif
+
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
 //! @endcond
index 00fe6ec5aebeb4698d7687947495460d16ff54a1..fbb4c7924c565558af65b2a16ec6959d780dc21b 100644 (file)
@@ -69,11 +69,11 @@ public:
      */
     virtual void knnSearch(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, int knn, const SearchParams& params)
     {
-        assert(queries.cols == veclen());
-        assert(indices.rows >= queries.rows);
-        assert(dists.rows >= queries.rows);
-        assert(int(indices.cols) >= knn);
-        assert(int(dists.cols) >= knn);
+        CV_Assert(queries.cols == veclen());
+        CV_Assert(indices.rows >= queries.rows);
+        CV_Assert(dists.rows >= queries.rows);
+        CV_Assert(int(indices.cols) >= knn);
+        CV_Assert(int(dists.cols) >= knn);
 
 #if 0
         KNNResultSet<DistanceType> resultSet(knn);
index 20b7e03c92027323044ed0438adf35f4e243292d..02970148b2057aa08509153b94043e9c5ce734ef 100644 (file)
@@ -72,7 +72,7 @@ float optimizeSimplexDownhill(T* points, int n, F func, float* vals = NULL )
 {
     const int MAX_ITERATIONS = 10;
 
-    assert(n>0);
+    CV_DbgAssert(n>0);
 
     T* p_o = new T[n];
     T* p_r = new T[n];