From 4866bb5902ed547682798fd5ead4a765e2abd6ad Mon Sep 17 00:00:00 2001 From: Houtan Bastani <houtan@dynare.org> Date: Tue, 29 Oct 2019 10:48:33 +0100 Subject: [PATCH] macro processor: use C++17 structured bindings in for loops --- src/macro/Directives.cc | 22 +++++++++++----------- src/macro/Driver.cc | 12 ++++++------ 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/macro/Directives.cc b/src/macro/Directives.cc index f2601029..98c6fe98 100644 --- a/src/macro/Directives.cc +++ b/src/macro/Directives.cc @@ -244,10 +244,10 @@ For::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &pa void If::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths) { - for (auto & it : expr_and_body) + for (const auto & [expr, body] : expr_and_body) try { - auto tmp = it.first->eval(); + auto tmp = expr->eval(); RealPtr dp = dynamic_pointer_cast<Real>(tmp); BoolPtr bp = dynamic_pointer_cast<Bool>(tmp); if (!bp && !dp) @@ -255,7 +255,7 @@ If::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &pat "The condition must evaluate to a boolean or a double", location)); if ((bp && *bp) || (dp && *dp)) { - interpretBody(it.second, output, no_line_macro, paths); + interpretBody(body, output, no_line_macro, paths); break; } } @@ -289,12 +289,12 @@ If::interpretBody(const vector<DirectivePtr> &body, ostream &output, bool no_lin void Ifdef::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths) { - for (auto & it : expr_and_body) - if (VariablePtr vp = dynamic_pointer_cast<Variable>(it.first); - dynamic_pointer_cast<BaseType>(it.first) + for (const auto & [expr, body] : expr_and_body) + if (VariablePtr vp = dynamic_pointer_cast<Variable>(expr); + dynamic_pointer_cast<BaseType>(expr) || (vp && env.isVariableDefined(vp->getName()))) { - interpretBody(it.second, output, no_line_macro, paths); + interpretBody(body, output, no_line_macro, paths); break; } printEndLineInfo(output, no_line_macro); @@ -303,12 +303,12 @@ Ifdef::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> & void Ifndef::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths) { - for (auto & it : expr_and_body) - if (VariablePtr vp = dynamic_pointer_cast<Variable>(it.first); - !(dynamic_pointer_cast<BaseType>(it.first) + for (const auto & [expr, body] : expr_and_body) + if (VariablePtr vp = dynamic_pointer_cast<Variable>(expr); + !(dynamic_pointer_cast<BaseType>(expr) || (vp && env.isVariableDefined(vp->getName())))) { - interpretBody(it.second, output, no_line_macro, paths); + interpretBody(body, output, no_line_macro, paths); break; } printEndLineInfo(output, no_line_macro); diff --git a/src/macro/Driver.cc b/src/macro/Driver.cc index 689d128a..5ad37aa0 100644 --- a/src/macro/Driver.cc +++ b/src/macro/Driver.cc @@ -32,19 +32,19 @@ Driver::parse(const string &file_arg, const string &basename_arg, istream &modfi if (!defines.empty()) { stringstream command_line_defines_with_endl; - for (auto & define : defines) + for (const auto & [var, val] : defines) try { - stoi(define.second); - command_line_defines_with_endl << "@#define " << define.first << " = " << define.second << endl; + stoi(val); + command_line_defines_with_endl << "@#define " << var << " = " << val << endl; } catch (const invalid_argument &) { - if (!define.second.empty() && define.second.at(0) == '[' && define.second.at(define.second.length()-1) == ']') + if (!val.empty() && val.at(0) == '[' && val.at(val.length()-1) == ']') // If the input is an array. Issue #1578 - command_line_defines_with_endl << "@#define " << define.first << " = " << define.second << endl; + command_line_defines_with_endl << "@#define " << var << " = " << val << endl; else - command_line_defines_with_endl << "@#define " << define.first << " = \"" << define.second << "\"" << endl; + command_line_defines_with_endl << "@#define " << var << " = \"" << val << "\"" << endl; } Driver m(env, true); istream is(command_line_defines_with_endl.rdbuf()); -- GitLab