From 790c56612c0e58e6bc51bad5bfd259e13b7cfa34 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Wed, 20 Feb 2019 18:07:07 +0100
Subject: [PATCH] Dynare++ tensor library: changes to exception handling

- TL_RAISE now unconditionally raises an exception
- rathe use TL_RAISE_IF at some places, to save a test in non-debug mode
---
 dynare++/tl/cc/equivalence.cc     | 10 +++-------
 dynare++/tl/cc/ps_tensor.cc       |  2 --
 dynare++/tl/cc/pyramid_prod2.hh   |  2 +-
 dynare++/tl/cc/sparse_tensor.cc   |  6 +-----
 dynare++/tl/cc/stack_container.hh |  2 --
 dynare++/tl/cc/tl_exception.hh    | 16 ++++++++--------
 6 files changed, 13 insertions(+), 25 deletions(-)

diff --git a/dynare++/tl/cc/equivalence.cc b/dynare++/tl/cc/equivalence.cc
index ca0a525abf..3c1a392e21 100644
--- a/dynare++/tl/cc/equivalence.cc
+++ b/dynare++/tl/cc/equivalence.cc
@@ -384,13 +384,9 @@ EquivalenceBundle::EquivalenceBundle(int nmax)
 const EquivalenceSet &
 EquivalenceBundle::get(int n) const
 {
-  if (n > static_cast<int>(bundle.size()) || n < 1)
-    {
-      TL_RAISE("Equivalence set not found in EquivalenceBundle::get");
-      return bundle[0];
-    }
-  else
-    return bundle[n-1];
+  TL_RAISE_IF(n > static_cast<int>(bundle.size()) || n < 1,
+              "Equivalence set not found in EquivalenceBundle::get");
+  return bundle[n-1];
 }
 
 /* Get |curmax| which is a maximum size in the bundle, and generate for
diff --git a/dynare++/tl/cc/ps_tensor.cc b/dynare++/tl/cc/ps_tensor.cc
index f9880df02d..48acd7cb0a 100644
--- a/dynare++/tl/cc/ps_tensor.cc
+++ b/dynare++/tl/cc/ps_tensor.cc
@@ -62,7 +62,6 @@ std::unique_ptr<FTensor>
 UPSTensor::fold() const
 {
   TL_RAISE("Never should come to this place in UPSTensor::fold");
-  return std::make_unique<FFSTensor>(0, 0, 0);
 }
 
 int
@@ -319,7 +318,6 @@ std::unique_ptr<UTensor>
 FPSTensor::unfold() const
 {
   TL_RAISE("Unfolding of FPSTensor not implemented");
-  return std::make_unique<UFSTensor>(0, 0, 0);
 }
 
 /* We only call |calcOffset| of the |PerTensorDimens2|. */
diff --git a/dynare++/tl/cc/pyramid_prod2.hh b/dynare++/tl/cc/pyramid_prod2.hh
index d8b9e33a04..5e10c3c5b6 100644
--- a/dynare++/tl/cc/pyramid_prod2.hh
+++ b/dynare++/tl/cc/pyramid_prod2.hh
@@ -147,7 +147,7 @@ public:
   int
   getOffset(const IntSequence &v) const override
   {
-    TL_RAISE("Not implemented error in IrregTensor::getOffset"); return 0;
+    TL_RAISE("Not implemented error in IrregTensor::getOffset");
   }
 };
 
diff --git a/dynare++/tl/cc/sparse_tensor.cc b/dynare++/tl/cc/sparse_tensor.cc
index 308c88c593..b8e50ddf5a 100644
--- a/dynare++/tl/cc/sparse_tensor.cc
+++ b/dynare++/tl/cc/sparse_tensor.cc
@@ -24,11 +24,7 @@ SparseTensor::insert(const IntSequence &key, int r, double c)
   // check that pair |key| and |r| is unique
   auto last_pos = m.upper_bound(key);
   for (auto it = first_pos; it != last_pos; ++it)
-    if ((*it).second.first == r)
-      {
-        TL_RAISE("Duplicate <key, r> insertion in SparseTensor::insert");
-        return;
-      }
+    TL_RAISE_IF(it->second.first == r, "Duplicate <key, r> insertion in SparseTensor::insert");
 
   m.insert(first_pos, Map::value_type(key, Item(r, c)));
   if (first_nz_row > r)
diff --git a/dynare++/tl/cc/stack_container.hh b/dynare++/tl/cc/stack_container.hh
index 670ac29d5c..6e256defd4 100644
--- a/dynare++/tl/cc/stack_container.hh
+++ b/dynare++/tl/cc/stack_container.hh
@@ -381,7 +381,6 @@ public:
         return _Stype::zero;
 
     TL_RAISE("Wrong stack index in ZContainer::getType");
-    return _Stype::zero;
   }
 
 };
@@ -464,7 +463,6 @@ public:
         return _Stype::zero;
 
     TL_RAISE("Wrong stack index in GContainer::getType");
-    return _Stype::zero;
   }
 
 };
diff --git a/dynare++/tl/cc/tl_exception.hh b/dynare++/tl/cc/tl_exception.hh
index eb14fecb6b..300289244e 100644
--- a/dynare++/tl/cc/tl_exception.hh
+++ b/dynare++/tl/cc/tl_exception.hh
@@ -24,13 +24,13 @@
    the exceptions. If the |TL_DEBUG| is equal or higher than
    |TL_DEBUG_EXCEPTION|, the exception conditions are checked.
 
-   We define |TL_RAISE|, and |TL_RAISE_IF| macros which throw an instance
-   of |TLException| if |TL_DEBUG >= TL_DEBUG_EXCEPTION|. The first is
-   unconditional throw, the second is conditioned by a given
-   expression. Note that if |TL_DEBUG < TL_DEBUG_EXCEPTION| then the code
-   is compiled but evaluation of the condition is passed. If code is
-   optimized, the optimizer also passes evaluation of |TL_DEBUG| and
-   |TL_DEBUG_EXCEPTION| comparison (I hope).
+   We define |TL_RAISE|, and |TL_RAISE_IF| macros which throw an instance of
+   |TLException| (only if |TL_DEBUG >= TL_DEBUG_EXCEPTION| for the latter). The
+   first is unconditional throw, the second is conditioned by a given
+   expression. Note that if |TL_DEBUG < TL_DEBUG_EXCEPTION| then the code is
+   compiled but evaluation of the condition is passed. If code is optimized,
+   the optimizer also passes evaluation of |TL_DEBUG| and |TL_DEBUG_EXCEPTION|
+   comparison (I hope).
 
    We provide default values for |TL_DEBUG| and |TL_DEBUG_EXCEPTION|. */
 
@@ -43,7 +43,7 @@
 #endif
 
 #define TL_RAISE(mes)                                                   \
-  if (TL_DEBUG >= TL_DEBUG_EXCEPTION) throw TLException(__FILE__, __LINE__, mes);
+  throw TLException(__FILE__, __LINE__, mes)
 
 #define TL_RAISE_IF(expr, mes)                                          \
   if (TL_DEBUG >= TL_DEBUG_EXCEPTION && (expr)) throw TLException(__FILE__, __LINE__, mes);
-- 
GitLab