From c88c17728e362341573f563145dcb72f743ba8d4 Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Thu, 30 Jul 2015 14:40:03 +0200
Subject: [PATCH] preprocessor: issue warning when param used with lead/lag.
 closes #976

---
 matlab/global_initialization.m |  1 +
 preprocessor/DataTree.cc       | 12 +++++++++++-
 preprocessor/DataTree.hh       |  4 +++-
 preprocessor/DynamicModel.cc   |  6 ++++++
 preprocessor/DynamicModel.hh   |  3 +++
 preprocessor/ModFile.cc        | 11 +++++++++--
 preprocessor/ModFile.hh        |  3 +++
 7 files changed, 36 insertions(+), 4 deletions(-)

diff --git a/matlab/global_initialization.m b/matlab/global_initialization.m
index 65acab6edc..9fe808c7e8 100644
--- a/matlab/global_initialization.m
+++ b/matlab/global_initialization.m
@@ -495,6 +495,7 @@ M_.exo_histval = [];
 M_.exo_det_histval = [];
 M_.Correlation_matrix = [];
 M_.Correlation_matrix_ME = [];
+M_.parameter_used_with_lead_lag = false;
 
 % homotopy for steady state
 options_.homotopy_mode = 0;
diff --git a/preprocessor/DataTree.cc b/preprocessor/DataTree.cc
index 3f90a44f87..7c718c5ec6 100644
--- a/preprocessor/DataTree.cc
+++ b/preprocessor/DataTree.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2003-2014 Dynare Team
+ * Copyright (C) 2003-2015 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -72,6 +72,16 @@ DataTree::AddVariableInternal(int symb_id, int lag)
     return new VariableNode(*this, symb_id, lag);
 }
 
+bool
+DataTree::ParamUsedWithLeadLagInternal() const
+{
+  for (variable_node_map_t::const_iterator it = variable_node_map.begin();
+       it != variable_node_map.end(); it++)
+    if (symbol_table.getType(it->first.first) == eParameter && it->first.second != 0)
+      return true;
+  return false;
+}
+
 VariableNode *
 DataTree::AddVariable(int symb_id, int lag)
 {
diff --git a/preprocessor/DataTree.hh b/preprocessor/DataTree.hh
index d7c53645d5..4c740113f0 100644
--- a/preprocessor/DataTree.hh
+++ b/preprocessor/DataTree.hh
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2003-2014 Dynare Team
+ * Copyright (C) 2003-2015 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -88,6 +88,8 @@ protected:
   //! Internal implementation of AddVariable(), without the check on the lag
   VariableNode *AddVariableInternal(int symb_id, int lag);
 
+  //! Internal implementation of ParamUsedWithLeadLag()
+  bool ParamUsedWithLeadLagInternal() const;
 private:
   typedef list<expr_t> node_list_t;
   //! The list of nodes
diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc
index 70b1617659..6c506c400a 100644
--- a/preprocessor/DynamicModel.cc
+++ b/preprocessor/DynamicModel.cc
@@ -3499,6 +3499,12 @@ DynamicModel::toStatic(StaticModel &static_model) const
     static_model.addAuxEquation((*it)->toStatic(static_model));
 }
 
+bool
+DynamicModel::ParamUsedWithLeadLag() const
+{
+  return ParamUsedWithLeadLagInternal();
+}
+
 set<int>
 DynamicModel::findUnusedEndogenous()
 {
diff --git a/preprocessor/DynamicModel.hh b/preprocessor/DynamicModel.hh
index 0bf34f4793..6f1eefd7e9 100644
--- a/preprocessor/DynamicModel.hh
+++ b/preprocessor/DynamicModel.hh
@@ -469,6 +469,9 @@ public:
   };
   bool isModelLocalVariableUsed() const;
 
+  //! Returns true if a parameter was used in the model block with a lead or lag
+  bool ParamUsedWithLeadLag() const;
+
   //! Writes model initialization and lead/lag incidence matrix to C output
   void writeCOutput(ostream &output, const string &basename, bool block, bool byte_code, bool use_dll, int order, bool estimation_present) const;
   //! Writes model initialization and lead/lag incidence matrix to Cpp output
diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc
index a4693cfc71..1b7f286b02 100644
--- a/preprocessor/ModFile.cc
+++ b/preprocessor/ModFile.cc
@@ -41,7 +41,7 @@ ModFile::ModFile(WarningConsolidation &warnings_arg)
     linear(false), block(false), byte_code(false), use_dll(false), no_static(false), 
     differentiate_forward_vars(false),
     nonstationary_variables(false), orig_eqn_nbr(0), ramsey_eqn_nbr(0),
-    warnings(warnings_arg)
+    param_used_with_lead_lag(false), warnings(warnings_arg)
 {
 }
 
@@ -120,6 +120,10 @@ ModFile::checkPass()
   if (!mod_file_struct.order_option)
     mod_file_struct.order_option = 2;
 
+  param_used_with_lead_lag = dynamic_model.ParamUsedWithLeadLag();
+  if (param_used_with_lead_lag)
+    warnings << "WARNING: A parameter was used with a lead or a lag in the model block" << endl;
+
   bool stochastic_statement_present = mod_file_struct.stoch_simul_present
     || mod_file_struct.estimation_present
     || mod_file_struct.osr_present
@@ -599,7 +603,10 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool clear_glo
 
   if (nointeractive)
     mOutputFile << "options_.nointeractive = 1;" << endl;
-    
+
+  if (param_used_with_lead_lag)
+    mOutputFile << "M_.parameter_used_with_lead_lag = true;" << endl;
+
   cout << "Processing outputs ..." << endl;
 
   symbol_table.writeOutput(mOutputFile);
diff --git a/preprocessor/ModFile.hh b/preprocessor/ModFile.hh
index 11e3e42461..23ea3371e5 100644
--- a/preprocessor/ModFile.hh
+++ b/preprocessor/ModFile.hh
@@ -107,6 +107,9 @@ public:
   //! Stores the number of equations added to the Ramsey model
   int ramsey_eqn_nbr;
 
+  //! Parameter used with lead/lag
+  bool param_used_with_lead_lag;
+
   //! Stores the list of extra files to be transefered during a parallel run
   /*! (i.e. option parallel_local_files of model block) */
   vector<string> parallel_local_files;
-- 
GitLab