From 079cd53a6efea31672ee9a8ff3886d3088d4f1da Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 21 Sep 2016 22:07:47 +0100 Subject: Allow default constructor of parser, set options and execute instead of multiple ever growing wrappers --- slicer/test/preprocessor.cpp | 25 ++++++++++++++++---- slicer/tool/parser.cpp | 55 ++++++++++++++++++-------------------------- slicer/tool/parser.h | 13 ++++++----- slicer/tool/slicer.cpp | 21 ++++++----------- 4 files changed, 57 insertions(+), 57 deletions(-) diff --git a/slicer/test/preprocessor.cpp b/slicer/test/preprocessor.cpp index ae4f1b7..3eb50f6 100644 --- a/slicer/test/preprocessor.cpp +++ b/slicer/test/preprocessor.cpp @@ -19,16 +19,26 @@ BOOST_FIXTURE_TEST_SUITE ( preprocessor, FileStructure ); BOOST_AUTO_TEST_CASE( slicer_test_counts_path ) { - auto count = Slicer::Slicer::Apply(slice, boost::filesystem::path("/dev/null"), {"-I" + included.string()}, false); + Slicer::Slicer s; + s.slicePath = slice; + s.cppPath = "/dev/null"; + s.includes.push_back(included); + + auto count = s.Execute(); BOOST_REQUIRE_EQUAL(COMPONENTS_IN_TEST_ICE, count); + BOOST_REQUIRE_EQUAL(COMPONENTS_IN_TEST_ICE, s.Components()); } BOOST_AUTO_TEST_CASE( slicer_test_counts_filestar ) { FILE * file = fopen("/dev/null", "a"); BOOST_REQUIRE(file); + Slicer::Slicer s; + s.slicePath = slice; + s.cpp = file; + s.includes.push_back(included); - auto count = Slicer::Slicer::Apply(slice, file, {"-I" + included.string()}, false); + auto count = s.Execute(); BOOST_REQUIRE_EQUAL(COMPONENTS_IN_TEST_ICE, count); fclose(file); @@ -36,13 +46,20 @@ BOOST_AUTO_TEST_CASE( slicer_test_counts_filestar ) BOOST_AUTO_TEST_CASE( slicer_test_counts_nullfilestar ) { - auto count = Slicer::Slicer::Apply(slice, NULL, {"-I" + included.string()}, false); + Slicer::Slicer s; + s.slicePath = slice; + s.includes.push_back(included); + + auto count = s.Execute(); BOOST_REQUIRE_EQUAL(COMPONENTS_IN_TEST_ICE, count); } BOOST_AUTO_TEST_CASE( slicer_test_counts_interfacesOnly ) { - auto count = Slicer::Slicer::Apply(root / "interfaces.ice", NULL); + Slicer::Slicer s; + s.slicePath = root / "interfaces.ice"; + + auto count = s.Execute(); BOOST_REQUIRE_EQUAL(0, count); } diff --git a/slicer/tool/parser.cpp b/slicer/tool/parser.cpp index 6d89719..d2332b1 100644 --- a/slicer/tool/parser.cpp +++ b/slicer/tool/parser.cpp @@ -9,18 +9,16 @@ #include #include #include -#include #include #include namespace fs = boost::filesystem; -std::mutex slicePreprocessor; - namespace Slicer { - Slicer::Slicer(FILE * c) : + Slicer::Slicer() : + cpp(NULL), + allowIcePrefix(false), components(0), - cpp(c), classNo(0) { } @@ -580,33 +578,25 @@ namespace Slicer { } unsigned int - Slicer::Apply(const boost::filesystem::path & ice, const boost::filesystem::path & cpp) - { - return Apply(ice, cpp, {}, false); - } - - unsigned int - Slicer::Apply(const boost::filesystem::path & ice, const boost::filesystem::path & cpp, const Args & args, bool allowIcePrefix) + Slicer::Execute() { - FilePtr cppfile(fopen(cpp.string(), "a"), fclose); - if (!cppfile) { - throw std::runtime_error("failed to open code file"); + if (cpp != NULL && !cppPath.empty()) { + throw std::runtime_error("Both file handle and path provided."); + } + FilePtr cppfile( + cpp || cppPath.empty() ? cpp : fopen(cppPath.string(), "a"), + cppPath.empty() ? fflush : fclose); + if (!cppfile && !cppPath.empty()) { + throw std::runtime_error("Failed to open output file"); + } + cpp = cppfile.get(); + Slicer::Slicer::Args args; + // Copy includes to args + for(const auto & include : includes) { + args.push_back("-I" + include.string()); } - return Apply(ice, cppfile.get(), args, allowIcePrefix); - } - - unsigned int - Slicer::Apply(const boost::filesystem::path & ice, FILE * cpp) - { - return Apply(ice, cpp, {}, false); - } - - unsigned int - Slicer::Apply(const boost::filesystem::path & ice, FILE * cpp, const Args & args, bool allowIcePrefix) - { - std::lock_guard lock(slicePreprocessor); - Slice::PreprocessorPtr icecpp = Slice::Preprocessor::create("slicer", ice.string(), args); + Slice::PreprocessorPtr icecpp = Slice::Preprocessor::create("slicer", slicePath.string(), args); FILE * cppHandle = icecpp->preprocess(false); if (cppHandle == NULL) { @@ -615,7 +605,7 @@ namespace Slicer { Slice::UnitPtr u = Slice::Unit::createUnit(false, false, allowIcePrefix, false); - int parseStatus = u->parse(ice.string(), cppHandle, false); + int parseStatus = u->parse(slicePath.string(), cppHandle, false); if (!icecpp->close()) { throw std::runtime_error("preprocess close failed"); @@ -625,12 +615,11 @@ namespace Slicer { throw std::runtime_error("unit parse failed"); } - Slicer s(cpp); - u->visit(&s, false); + u->visit(this, false); u->destroy(); - return s.Components(); + return Components(); } Slicer::ConversionSpec::ConversionSpec(const Slicer::Args & s) : diff --git a/slicer/tool/parser.h b/slicer/tool/parser.h index 1193664..4ae7752 100644 --- a/slicer/tool/parser.h +++ b/slicer/tool/parser.h @@ -23,13 +23,15 @@ namespace Slicer { }; typedef std::vector Conversions; - Slicer(FILE * c); + Slicer(); - static unsigned int Apply(const boost::filesystem::path & ice, const boost::filesystem::path & cpp); - static unsigned int Apply(const boost::filesystem::path & ice, FILE *); - static unsigned int Apply(const boost::filesystem::path & ice, const boost::filesystem::path & cpp, const Args &, bool); - static unsigned int Apply(const boost::filesystem::path & ice, FILE *, const Args &, bool); + FILE * cpp; + boost::filesystem::path slicePath; + boost::filesystem::path cppPath; + std::vector includes; + bool allowIcePrefix; + unsigned int Execute(); unsigned int Components() const; #pragma GCC visibility push(hidden) @@ -66,7 +68,6 @@ namespace Slicer { #pragma GCC visibility pop unsigned int components; - FILE * cpp; unsigned int classNo; }; } diff --git a/slicer/tool/slicer.cpp b/slicer/tool/slicer.cpp index 8a4e764..c150dcf 100644 --- a/slicer/tool/slicer.cpp +++ b/slicer/tool/slicer.cpp @@ -6,18 +6,15 @@ namespace po = boost::program_options; int main(int argc, char ** argv) { - boost::filesystem::path slice; - boost::filesystem::path cpp; - std::vector includes; - bool allowIcePrefix; + Slicer::Slicer slicer; po::options_description opts("Slicer options"); opts.add_options() ("help,h", "Show this help message") - ("include,I", po::value(&includes), "Add include directory to search path") - ("ice", po::value(&allowIcePrefix)->default_value(false)->zero_tokens(), "Allow reserved Ice prefix in Slice identifiers") - ("slice,i", po::value(&slice), "Input ICE Slice file") - ("cpp,o", po::value(&cpp), "Output C++ file"); + ("include,I", po::value(&slicer.includes), "Add include directory to search path") + ("ice", po::value(&slicer.allowIcePrefix)->default_value(false)->zero_tokens(), "Allow reserved Ice prefix in Slice identifiers") + ("slice,i", po::value(&slicer.slicePath), "Input ICE Slice file") + ("cpp,o", po::value(&slicer.cppPath), "Output C++ file"); po::positional_options_description p; p.add("slice", 1); @@ -27,17 +24,13 @@ main(int argc, char ** argv) po::store(po::command_line_parser(argc, argv).options(opts).positional(p).run(), vm); po::notify(vm); - if (vm.count("help") || slice.empty() || cpp.empty()) { + if (vm.count("help") || slicer.slicePath.empty() || slicer.cppPath.empty()) { // LCOV_EXCL_START std::cout << opts << std::endl; return 1; // LCOV_EXCL_STOP } - Slicer::Slicer::Args args; - for(const auto & include : includes) { - args.push_back("-I" + include.string()); - } - Slicer::Slicer::Apply(slice, cpp, args, allowIcePrefix); + slicer.Execute(); return 0; } -- cgit v1.2.3