From 6fb459467ab9fbb55911752a15dcf066c0f00b87 Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 10 Oct 2019 22:00:22 +0200 Subject: [PATCH 1/3] Make NOLINT more precise. --- src/main.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 3881e42..625ed7c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -212,7 +212,7 @@ string Compilescript::compile(const string &filename) const string command = _compiler + " " + source.string() + " " + compiler_arguments + " -o " + binary.string(); - int ret = std::system(command.c_str()); // NOLINT Doesn't apply here. + int ret = std::system(command.c_str()); // NOLINT(cert-env33-c) if (ret != 0) { throw std::runtime_error("Compilation failed."); @@ -224,7 +224,9 @@ string Compilescript::compile(const string &filename) void Compilescript::run(const string &filename, char *argv[]) { - execvp(filename.c_str(), &argv[1]); // NOLINT We know that argv[1] exists. + // We know that argv[1] exists. + // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) + execvp(filename.c_str(), &argv[1]); } void Compilescript::print_version() From 5430c2de39c7f84b58501760749b2022800594ec Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 10 Oct 2019 22:01:36 +0200 Subject: [PATCH 2/3] Use regex to find compiler arguments. --- src/main.cpp | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 625ed7c..b2a4f35 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -171,18 +172,11 @@ string Compilescript::compile(const string &filename) } std::getline(in, buf); - if (buf.substr(0, 17) == "// compilescript:") + const std::regex re("^(//|#|;) ?compilescript:"); + std::smatch match; + if (std::regex_search(buf, match, re)) { - compiler_arguments = buf.substr(17); - } - else if (buf.substr(0, 16) == "//compilescript:") - { - compiler_arguments = buf.substr(16); - } - else if ((buf.substr(0, 15) == "#compilescript:") || - (buf.substr(0, 15) == ";compilescript:")) - { - compiler_arguments = buf.substr(15); + compiler_arguments = match.suffix(); } else { From c9e38fa63618d00a24ed7b5d44378223264f54bd Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 10 Oct 2019 22:52:51 +0200 Subject: [PATCH 3/3] Add --compiler, to override compiler command. --- man/compilescript.1.adoc | 5 ++++- src/main.cpp | 32 +++++++++++++++++++++++++++----- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/man/compilescript.1.adoc b/man/compilescript.1.adoc index 59e4bcd..930b170 100644 --- a/man/compilescript.1.adoc +++ b/man/compilescript.1.adoc @@ -2,7 +2,7 @@ :doctype: manpage :Author: tastytea :Email: tastytea@tastytea.de -:Date: 2019-10-01 +:Date: 2019-10-10 :Revision: 0.0.0 :man source: compilescript :man version: {revision} @@ -40,6 +40,9 @@ The compiler invocation looks like this: Delete old cache files. You can configure the timespan after which a file is considered old with the config option _clean_after_hours_. +*--compiler* _command_:: + Override compiler command. Note that *--compiler*=_command_ does NOT work. + *--version*:: Print version, copyright and license. diff --git a/src/main.cpp b/src/main.cpp index b2a4f35..4fc1703 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -60,6 +60,8 @@ public: void run(const string &filename, char *argv[]); //! Print version, copyright and license. void print_version(); + //! Set compiler command. + void set_compiler(const string &command); }; Compilescript::Compilescript() @@ -218,9 +220,7 @@ string Compilescript::compile(const string &filename) void Compilescript::run(const string &filename, char *argv[]) { - // We know that argv[1] exists. - // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) - execvp(filename.c_str(), &argv[1]); + execvp(filename.c_str(), argv); } void Compilescript::print_version() @@ -233,6 +233,11 @@ void Compilescript::print_version() "\nand you are welcome to redistribute it under certain conditions.\n"; } +void Compilescript::set_compiler(const string &command) +{ + _compiler = command; +} + int main(int argc, char *argv[]) { const vector args(argv, argv + argc); @@ -245,7 +250,8 @@ int main(int argc, char *argv[]) if (args.size() <= 1) { cerr << "usage: " << args[0] - << " [file|--cleanup|--version] [arguments]\n"; + << " [file|--cleanup|--version|--compiler command] " + << "[arguments]\n"; return 1; } if (args[1] == "--cleanup") @@ -258,9 +264,25 @@ int main(int argc, char *argv[]) App.print_version(); return 0; } + if (args[1] == "--compiler") + { + if (args.size() <= 3) + { + cerr << "Error: You need to specify a command.\n"; + return 1; + } + App.set_compiler(args[2]); + const string binary = App.compile(args[3]); + // We know that argv[3] exists. + // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) + App.run(binary, &argv[3]); + return 0; + } const string binary = App.compile(args[1]); - App.run(binary, argv); + // We know that argv[1] exists. + // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) + App.run(binary, &argv[1]); } catch (const std::exception &e) {