From a431682697504bdc79ec8d574fda615917f3591c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Mon, 11 Apr 2022 12:26:14 +0200
Subject: [PATCH] Fix check for the presence of ramsey_{model,policy} when
 ramsey_constraints is used

The existing check would incorrectly fail if ramsey_constraints appeared before
ramsey_{model,policy}.

Also, the check would not terminate the processing in case of error.

Closes: #91
---
 src/ComputingTasks.cc | 3 +--
 src/ModFile.cc        | 6 ++++++
 src/Statement.hh      | 4 +++-
 3 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/ComputingTasks.cc b/src/ComputingTasks.cc
index a6d2c561..22b33b21 100644
--- a/src/ComputingTasks.cc
+++ b/src/ComputingTasks.cc
@@ -545,8 +545,7 @@ RamseyConstraintsStatement::RamseyConstraintsStatement(const SymbolTable &symbol
 void
 RamseyConstraintsStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings)
 {
-  if (!mod_file_struct.ramsey_model_present && !mod_file_struct.ramsey_policy_present)
-    cerr << "ramsey_constraints: can only be used with ramsey_model or ramsey_policy" << endl;
+  mod_file_struct.ramsey_constraints_present = true;
 }
 
 void
diff --git a/src/ModFile.cc b/src/ModFile.cc
index ceac9f32..c16a5ab2 100644
--- a/src/ModFile.cc
+++ b/src/ModFile.cc
@@ -170,6 +170,12 @@ ModFile::checkPass(bool nostrict, bool stochastic)
       exit(EXIT_FAILURE);
     }
 
+  if (mod_file_struct.ramsey_constraints_present && !mod_file_struct.ramsey_model_present && !mod_file_struct.ramsey_policy_present)
+    {
+      cerr << "ERROR: A ramsey_constraints block requires the presence of a ramsey_model or ramsey_policy statement" << endl;
+      exit(EXIT_FAILURE);
+    }
+
   if ((mod_file_struct.osr_present && (!mod_file_struct.osr_params_present || !mod_file_struct.optim_weights_present))
       || ((!mod_file_struct.osr_present || !mod_file_struct.osr_params_present) && mod_file_struct.optim_weights_present)
       || ((!mod_file_struct.osr_present || !mod_file_struct.optim_weights_present) && mod_file_struct.osr_params_present))
diff --git a/src/Statement.hh b/src/Statement.hh
index 24d0d463..57d36061 100644
--- a/src/Statement.hh
+++ b/src/Statement.hh
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2006-2021 Dynare Team
+ * Copyright © 2006-2022 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -149,6 +149,8 @@ public:
   bool shocks_surprise_present{false};
   // Whether an occbin_constraints block appears
   bool occbin_constraints_present{false};
+  // Whether a ramsey_constraints block appears
+  bool ramsey_constraints_present{false};
 };
 
 class Statement
-- 
GitLab