From cd99bb3af9c8ef6fd881fa0457121894f8edd14e Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 19 Aug 2019 18:19:10 +0200 Subject: [PATCH] macro processor: Make @#include and @#includepath immutable --- src/macro/Directives.cc | 18 ++++++++++++++++-- src/macro/Directives.hh | 6 ++---- src/macro/Driver.cc | 14 +++++++------- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/macro/Directives.cc b/src/macro/Directives.cc index 9880bece..6b44c088 100644 --- a/src/macro/Directives.cc +++ b/src/macro/Directives.cc @@ -41,13 +41,19 @@ Eval::interpret(ostream &output, bool no_line_macro) void Include::interpret(ostream &output, bool no_line_macro) +{ + error(StackTrace("@#include", "should never be interpreted", location)); +} + +string +Include::interpretAndGetName() { try { StringPtr msp = dynamic_pointer_cast(expr->eval()); if (!msp) throw StackTrace("File name does not evaluate to a string"); - name = *msp; + return msp->to_string(); } catch (StackTrace &ex) { @@ -58,17 +64,24 @@ Include::interpret(ostream &output, bool no_line_macro) { error(StackTrace("@#include", e.what(), location)); } + return ""; } void IncludePath::interpret(ostream &output, bool no_line_macro) +{ +error(StackTrace("@#includepath", "should never be interpreted", location)); +} + +string +IncludePath::interpretAndGetPath() { try { StringPtr msp = dynamic_pointer_cast(expr->eval()); if (!msp) throw StackTrace("File name does not evaluate to a string"); - path = *msp; + return *msp; } catch (StackTrace &ex) { @@ -79,6 +92,7 @@ IncludePath::interpret(ostream &output, bool no_line_macro) { error(StackTrace("@#includepath", e.what(), location)); } + return ""; } void diff --git a/src/macro/Directives.hh b/src/macro/Directives.hh index 58d4d546..4b0d530a 100644 --- a/src/macro/Directives.hh +++ b/src/macro/Directives.hh @@ -66,12 +66,11 @@ namespace macro { private: const ExpressionPtr expr; - string name; public: Include(ExpressionPtr expr_arg, Environment &env_arg, Tokenizer::location location_arg) : Directive(env_arg, move(location_arg)), expr{move(expr_arg)} { } void interpret(ostream &output, bool no_line_macro) override; - inline const string & getName() const { return name; } + string interpretAndGetName(); }; @@ -79,12 +78,11 @@ namespace macro { private: const ExpressionPtr expr; - string path; public: IncludePath(ExpressionPtr expr_arg, Environment &env_arg, Tokenizer::location location_arg) : Directive(env_arg, move(location_arg)), expr{move(expr_arg)} { } void interpret(ostream &output, bool no_line_macro) override; - inline const string & getPath() const { return path; } + string interpretAndGetPath(); }; diff --git a/src/macro/Driver.cc b/src/macro/Driver.cc index 3635aaf2..8c1fc5ff 100644 --- a/src/macro/Driver.cc +++ b/src/macro/Driver.cc @@ -80,16 +80,14 @@ Driver::parse(const string &file_arg, const string &basename_arg, istream &modfi statement->printLineInfo(output, no_line_macro); printLine = false; } - statement->interpret(output, no_line_macro); - - auto ipp = dynamic_pointer_cast(statement); - if (ipp) - paths.emplace_back(ipp->getPath()); auto ip = dynamic_pointer_cast(statement); - if (ip) + auto ipp = dynamic_pointer_cast(statement); + if (ipp) + paths.emplace_back(ipp->interpretAndGetPath()); + else if (ip) { - string filename = ip->getName(); + string filename = ip->interpretAndGetName(); ifstream incfile(filename, ios::binary); if (incfile.fail()) { @@ -116,6 +114,8 @@ Driver::parse(const string &file_arg, const string &basename_arg, istream &modfi Driver m(env, no_line_macro); m.parse(filename, basename, incfile, output, debug, vector>{}, paths); } + else + statement->interpret(output, no_line_macro); } }