From 8d0b89f34bfb59c1caaec21d3b22d60a44ee796d Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Mon, 24 Aug 2015 12:54:05 +0200
Subject: [PATCH] preprocessor: remove extra exogenous variables. closes #841

---
 doc/dynare.texi                    |  3 +-
 preprocessor/DynareMain2.cc        |  4 +--
 preprocessor/ModFile.cc            | 23 +++++++------
 preprocessor/ModFile.hh            |  4 +--
 tests/Makefile.am                  |  3 +-
 tests/example1_extra_exo_xfail.mod | 52 ++++++++++++++++++++++++++++++
 6 files changed, 73 insertions(+), 16 deletions(-)
 create mode 100644 tests/example1_extra_exo_xfail.mod

diff --git a/doc/dynare.texi b/doc/dynare.texi
index 8d327be7d1..1ca3bc42e4 100644
--- a/doc/dynare.texi
+++ b/doc/dynare.texi
@@ -95,7 +95,7 @@
 @c %**end of header
 
 @copying
-Copyright @copyright{} 1996-2016, Dynare Team.
+Copyright @copyright{} 1996-2017, Dynare Team.
 
 @quotation
 Permission is granted to copy, distribute and/or modify this document
@@ -928,6 +928,7 @@ Allows Dynare to issue a warning and continue processing when
 @enumerate
 @item there are more endogenous variables than equations
 @item an undeclared symbol is assigned in @code{initval} or @code{endval}
+@item exogenous variables were declared but not used in the @code{model} block
 @end enumerate
 
 @item fast
diff --git a/preprocessor/DynareMain2.cc b/preprocessor/DynareMain2.cc
index 7d867831b6..bc5065b2d2 100644
--- a/preprocessor/DynareMain2.cc
+++ b/preprocessor/DynareMain2.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008-2016 Dynare Team
+ * Copyright (C) 2008-2017 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -42,7 +42,7 @@ main2(stringstream &in, string &basename, bool debug, bool clear_all, bool clear
   ModFile *mod_file = p.parse(in, debug);
 
   // Run checking pass
-  mod_file->checkPass();
+  mod_file->checkPass(nostrict);
 
   // Perform transformations on the model (creation of auxiliary vars and equations)
   mod_file->transformPass(nostrict);
diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc
index 862b686640..0d51bd0be2 100644
--- a/preprocessor/ModFile.cc
+++ b/preprocessor/ModFile.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2006-2016 Dynare Team
+ * Copyright (C) 2006-2017 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -107,7 +107,7 @@ ModFile::addStatementAtFront(Statement *st)
 }
 
 void
-ModFile::checkPass()
+ModFile::checkPass(bool nostrict)
 {
   for (vector<Statement *>::iterator it = statements.begin();
        it != statements.end(); it++)
@@ -299,19 +299,22 @@ ModFile::checkPass()
       exit(EXIT_FAILURE);
     }
 
-  // Check if some exogenous is not used in the model block
+  // Check if some exogenous is not used in the model block, Issue #841
   set<int> unusedExo = dynamic_model.findUnusedExogenous();
   if (unusedExo.size() > 0)
     {
-      warnings << "WARNING: some exogenous (";
-      for (set<int>::const_iterator it = unusedExo.begin();
-           it != unusedExo.end(); )
+      ostringstream unused_exos;
+      for (set<int>::iterator it = unusedExo.begin(); it != unusedExo.end(); it++)
+        unused_exos << symbol_table.getName(*it) << " ";
+
+      if (nostrict)
+        warnings << "WARNING: " << unused_exos.str()
+                 << "not used in model block, removed by nostrict command-line option" << endl;
+      else
         {
-          warnings << symbol_table.getName(*it);
-          if (++it != unusedExo.end())
-            warnings << ", ";
+          cerr << "ERROR: " << unused_exos.str() << "not used in model block. To bypass this error, use the `nostrict` option. This may lead to crashes or unexpected behavior." << endl;
+          exit(EXIT_FAILURE);
         }
-      warnings << ") are declared but not used in the model. This may lead to crashes or unexpected behaviour." << endl;
     }
 }
 
diff --git a/preprocessor/ModFile.hh b/preprocessor/ModFile.hh
index 506ddacc8d..6ed24d33f7 100644
--- a/preprocessor/ModFile.hh
+++ b/preprocessor/ModFile.hh
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2006-2016 Dynare Team
+ * Copyright (C) 2006-2017 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -128,7 +128,7 @@ public:
   void evalAllExpressions(bool warn_uninit);
   //! Do some checking and fills mod_file_struct
   /*! \todo add check for number of equations and endogenous if ramsey_policy is present */
-  void checkPass();
+  void checkPass(bool nostrict);
   //! Perform some transformations on the model (creation of auxiliary vars and equations)
   void transformPass(bool nostrict);
   //! Execute computations
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 374e99e10c..8a9941cbc1 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -341,7 +341,8 @@ XFAIL_MODFILES = ramst_xfail.mod \
 	estimation/fs2000_mixed_ML_xfail.mod \
 	estimation/fs2000_stochastic_singularity_xfail.mod \
 	identification/ident_unit_root/ident_unit_root_xfail.mod \
-	steady_state/Linear_steady_state_xfail.mod
+	steady_state/Linear_steady_state_xfail.mod \
+	example1_extra_exo_xfail.mod
 
 MFILES = initval_file/ramst_initval_file_data.m
 
diff --git a/tests/example1_extra_exo_xfail.mod b/tests/example1_extra_exo_xfail.mod
new file mode 100644
index 0000000000..caec3da8df
--- /dev/null
+++ b/tests/example1_extra_exo_xfail.mod
@@ -0,0 +1,52 @@
+// Example 1 from Collard's guide to Dynare
+var y, c, k, a, h, b;
+varexo e, u, extra;
+
+verbatim;
+% I want these comments included in
+% example1.m 1999q1 1999y
+%
+var = 1;
+end;
+
+parameters beta, rho, alpha, delta, theta, psi, tau;
+
+alpha = 0.36;
+rho   = 0.95;
+tau   = 0.025;
+beta  = 0.99;
+delta = 0.025;
+psi   = 0;
+theta = 2.95;
+
+phi   = 0.1;
+
+model;
+c*theta*h^(1+psi)=(1-alpha)*y;
+k = beta*(((exp(b)*c)/(exp(b(+1))*c(+1)))
+    *(exp(b(+1))*alpha*y(+1)+(1-delta)*k));
+y = exp(a)*(k(-1)^alpha)*(h^(1-alpha));
+k = exp(b)*(y-c)+(1-delta)*k(-1);
+a = rho*a(-1)+tau*b(-1) + e;
+b = tau*a(-1)+rho*b(-1) + u;
+end;
+
+initval;
+y = 1.08068253095672;
+c = 0.80359242014163;
+h = 0.29175631001732;
+k = 11.08360443260358;
+a = 0;
+b = 0;
+e = 0;
+u = 0;
+extra = 0;
+end;
+
+shocks;
+var e; stderr 0.009;
+var u; stderr 0.009;
+var e, u = phi*0.009*0.009;
+end;
+
+stoch_simul;
-- 
GitLab