|
| 1 | +/** |
| 2 | + * @file VixcFrontend.cpp |
| 3 | + * @brief Experimental VixC source preparation adapter. |
| 4 | + */ |
| 5 | + |
| 6 | +#include <vix/cli/app/VixcFrontend.hpp> |
| 7 | + |
| 8 | +#include <fstream> |
| 9 | +#include <sstream> |
| 10 | + |
| 11 | +#if defined(VIX_CLI_ENABLE_VIXC_FRONTEND) |
| 12 | +#include <vixc/vixc.hpp> |
| 13 | +#endif |
| 14 | + |
| 15 | +namespace vix::cli::app |
| 16 | +{ |
| 17 | + VixcFrontendResult process_with_vixc(const std::filesystem::path &source) |
| 18 | + { |
| 19 | + VixcFrontendResult output; |
| 20 | + |
| 21 | +#if !defined(VIX_CLI_ENABLE_VIXC_FRONTEND) |
| 22 | + output.diagnostics = "Vix was built without experimental VixC frontend support."; |
| 23 | + return output; |
| 24 | +#else |
| 25 | + std::ifstream input(source, std::ios::binary); |
| 26 | + if (!input) |
| 27 | + { |
| 28 | + output.diagnostics = "Unable to read VixC source: " + source.string(); |
| 29 | + return output; |
| 30 | + } |
| 31 | + |
| 32 | + const std::string text{ |
| 33 | + std::istreambuf_iterator<char>{input}, |
| 34 | + std::istreambuf_iterator<char>{}}; |
| 35 | + |
| 36 | + vixc::FrontendOptions options; |
| 37 | + options.action = vixc::FrontendAction::Emit; |
| 38 | + options.retain_source_map = true; |
| 39 | + |
| 40 | + const vixc::FrontendResult result = |
| 41 | + vixc::Frontend{}.process(source.string(), text, options); |
| 42 | + |
| 43 | + std::ostringstream diagnostics; |
| 44 | + for (const vixc::Diagnostic &diagnostic : result.diagnostics()) |
| 45 | + { |
| 46 | + diagnostics << "vixc "; |
| 47 | + if (diagnostic.has_code()) |
| 48 | + diagnostics << '[' << diagnostic.code() << "] "; |
| 49 | + diagnostics << diagnostic.message(); |
| 50 | + if (diagnostic.has_range()) |
| 51 | + { |
| 52 | + diagnostics << " (offsets " |
| 53 | + << diagnostic.range().begin_offset() << '-' |
| 54 | + << diagnostic.range().end_offset() << ')'; |
| 55 | + } |
| 56 | + diagnostics << '\n'; |
| 57 | + } |
| 58 | + |
| 59 | + if (!result.success() || !result.has_generated_output()) |
| 60 | + { |
| 61 | + output.diagnostics = diagnostics.str(); |
| 62 | + if (output.diagnostics.empty()) |
| 63 | + output.diagnostics = "VixC did not produce generated C++ output."; |
| 64 | + return output; |
| 65 | + } |
| 66 | + |
| 67 | + const std::filesystem::path directory = |
| 68 | + source.parent_path() / ".vix" / "vixc"; |
| 69 | + const std::filesystem::path generated = |
| 70 | + directory / (source.filename().string() + ".vixc.cpp"); |
| 71 | + std::error_code error; |
| 72 | + std::filesystem::create_directories(directory, error); |
| 73 | + if (error) |
| 74 | + { |
| 75 | + output.diagnostics = "Unable to create VixC generated-source directory: " + error.message(); |
| 76 | + return output; |
| 77 | + } |
| 78 | + |
| 79 | + bool unchanged = false; |
| 80 | + { |
| 81 | + std::ifstream existing(generated, std::ios::binary); |
| 82 | + if (existing) |
| 83 | + { |
| 84 | + const std::string existing_text{ |
| 85 | + std::istreambuf_iterator<char>{existing}, |
| 86 | + std::istreambuf_iterator<char>{}}; |
| 87 | + unchanged = existing_text == result.generated_output(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + if (!unchanged) |
| 92 | + { |
| 93 | + std::ofstream generated_output(generated, std::ios::binary | std::ios::trunc); |
| 94 | + if (!generated_output) |
| 95 | + { |
| 96 | + output.diagnostics = "Unable to write VixC generated source: " + generated.string(); |
| 97 | + return output; |
| 98 | + } |
| 99 | + generated_output << result.generated_output(); |
| 100 | + } |
| 101 | + |
| 102 | + output.success = true; |
| 103 | + output.generated_source = generated; |
| 104 | + output.diagnostics = diagnostics.str(); |
| 105 | + return output; |
| 106 | +#endif |
| 107 | + } |
| 108 | +} |
0 commit comments