From 25706a611d0bf76781c162652bcc259b2c46a595 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 5 Jul 2020 17:09:35 +0100 Subject: Add support for a slicer post-processor Defaults to clang-format -i if clang-format in path --- slicer/tool/slicer.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/slicer/tool/slicer.cpp b/slicer/tool/slicer.cpp index 0145d15..a256bda 100644 --- a/slicer/tool/slicer.cpp +++ b/slicer/tool/slicer.cpp @@ -1,12 +1,35 @@ +#include #include +#include #include +#include namespace po = boost::program_options; +using namespace AdHoc::literals; + +static std::string +defaultPostProcessor() +{ + constexpr std::array, 1> pps {{ + {"clang-format", "-i"}, + }}; + const std::string_view path {getenv("PATH")}; + const auto pathBegin = make_split_iterator(path, first_finder(":", boost::is_equal())); + for (const auto & [cmd, opts] : pps) { + for (auto p = pathBegin; p != decltype(pathBegin) {}; ++p) { + if (std::filesystem::exists(std::filesystem::path(p->begin(), p->end()) / cmd)) { + return "%? %?"_fmt(cmd, opts); + } + } + } + return ""; +} int main(int argc, char ** argv) { Slicer::Slicer slicer; + std::string post; po::options_description opts("Slicer options"); // clang-format off @@ -15,6 +38,7 @@ main(int argc, char ** argv) ("include,I", po::value(&slicer.includes), "Add include directory to search path") // NOLINTNEXTLINE(clang-analyzer-optin.cplusplus.VirtualCall) ("headerPrefix", po::value(&slicer.headerPrefix)->default_value(slicer.headerPrefix), "Prefix path for Slicer C++ #includes") + ("post,p", po::value(&post)->default_value(defaultPostProcessor()), "Post-process command") ("slice,i", po::value(&slicer.slicePath), "Input ICE Slice file") ("cpp,o", po::value(&slicer.cppPath), "Output C++ file"); // clang-format on @@ -35,5 +59,9 @@ main(int argc, char ** argv) } slicer.Execute(); + if (!post.empty()) { + return system("%? %?"_fmt(post, slicer.cppPath).c_str()); + } + return 0; } -- cgit v1.2.3