From 4213c42b96120fccc340e012fbe83250d08b7c81 Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Tue, 12 Sep 2017 14:16:29 +0200
Subject: [PATCH] preprocessor: factorize code that checks for valid symb_id

---
 SymbolTable.cc | 15 +++++----------
 SymbolTable.hh | 36 ++++++++++++++++++------------------
 2 files changed, 23 insertions(+), 28 deletions(-)

diff --git a/SymbolTable.cc b/SymbolTable.cc
index dca0ea8f..cf986b41 100644
--- a/SymbolTable.cc
+++ b/SymbolTable.cc
@@ -156,8 +156,7 @@ SymbolTable::changeType(int id, SymbolType newtype) throw (UnknownSymbolIDExcept
   if (frozen)
     throw FrozenException();
 
-  if (id < 0 || id > symbol_table.size())
-    throw UnknownSymbolIDException(id);
+  validateSymbID(id);
 
   type_table[id] = newtype;
 }
@@ -732,8 +731,8 @@ SymbolTable::getAuxiliaryVarsExprNode(int symb_id) const throw (SearchFailedExce
 void
 SymbolTable::markPredetermined(int symb_id) throw (UnknownSymbolIDException, FrozenException)
 {
-  if (symb_id < 0 || symb_id > symbol_table.size())
-    throw UnknownSymbolIDException(symb_id);
+  validateSymbID(symb_id);
+
   if (frozen)
     throw FrozenException();
 
@@ -745,9 +744,7 @@ SymbolTable::markPredetermined(int symb_id) throw (UnknownSymbolIDException, Fro
 bool
 SymbolTable::isPredetermined(int symb_id) const throw (UnknownSymbolIDException)
 {
-  if (symb_id < 0 || symb_id > symbol_table.size())
-    throw UnknownSymbolIDException(symb_id);
-
+  validateSymbID(symb_id);
   return (predetermined_variables.find(symb_id) != predetermined_variables.end());
 }
 
@@ -760,9 +757,7 @@ SymbolTable::predeterminedNbr() const
 void
 SymbolTable::addObservedVariable(int symb_id) throw (UnknownSymbolIDException)
 {
-  if (symb_id < 0 || symb_id > symbol_table.size())
-    throw UnknownSymbolIDException(symb_id);
-
+  validateSymbID(symb_id);
   assert(getType(symb_id) == eEndogenous);
   varobs.push_back(symb_id);
 }
diff --git a/SymbolTable.hh b/SymbolTable.hh
index 20a7d573..b97ad162 100644
--- a/SymbolTable.hh
+++ b/SymbolTable.hh
@@ -219,6 +219,8 @@ private:
   int addLeadAuxiliaryVarInternal(bool endo, int index, expr_t arg) throw (FrozenException);
   //! Factorized code for Json writing
   void writeJsonVarVector(ostream &output, const vector<int> &varvec) const;
+  //! Factorized code for asserting that 0 <= symb_id <= symbol_table.size()
+  inline void validateSymbID(int symb_id) const throw (UnknownSymbolIDException);
 public:
   //! Add a symbol
   /*! Returns the symbol ID */
@@ -362,6 +364,13 @@ public:
   set <int> getOrigEndogenous() const;
 };
 
+inline void
+SymbolTable::validateSymbID(int symb_id) const throw (UnknownSymbolIDException)
+{
+  if (symb_id < 0 || symb_id > symbol_table.size())
+    throw UnknownSymbolIDException(symb_id);
+}
+
 inline bool
 SymbolTable::exists(const string &name) const
 {
@@ -372,37 +381,29 @@ SymbolTable::exists(const string &name) const
 inline string
 SymbolTable::getName(int id) const throw (UnknownSymbolIDException)
 {
-  if (id < 0 || id > symbol_table.size())
-    throw UnknownSymbolIDException(id);
-  else
-    return name_table[id];
+  validateSymbID(id);
+  return name_table[id];
 }
 
 inline string
 SymbolTable::getTeXName(int id) const throw (UnknownSymbolIDException)
 {
-  if (id < 0 || id > symbol_table.size())
-    throw UnknownSymbolIDException(id);
-  else
-    return tex_name_table[id];
+  validateSymbID(id);
+  return tex_name_table[id];
 }
 
 inline string
 SymbolTable::getLongName(int id) const throw (UnknownSymbolIDException)
 {
-  if (id < 0 || id > symbol_table.size())
-    throw UnknownSymbolIDException(id);
-  else
-    return long_name_table[id];
+  validateSymbID(id);
+  return long_name_table[id];
 }
 
 inline SymbolType
 SymbolTable::getType(int id) const throw (UnknownSymbolIDException)
 {
-  if (id < 0 || id > symbol_table.size())
-    throw UnknownSymbolIDException(id);
-  else
-    return type_table[id];
+  validateSymbID(id);
+  return type_table[id];
 }
 
 inline SymbolType
@@ -427,8 +428,7 @@ SymbolTable::getTypeSpecificID(int id) const throw (UnknownSymbolIDException, No
   if (!frozen)
     throw NotYetFrozenException();
 
-  if (id < 0 || id > symbol_table.size())
-    throw UnknownSymbolIDException(id);
+  validateSymbID(id);
 
   return type_specific_ids[id];
 }
-- 
GitLab