From e4677d2ee526e6bd785c218eb672053318053f64 Mon Sep 17 00:00:00 2001 From: Nitish Date: Sat, 7 Sep 2024 01:46:56 +0530 Subject: [PATCH] refactor `parseProgram` fn Signed-off-by: Nitish --- backends/p4fmt/p4fmt.cpp | 49 ++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/backends/p4fmt/p4fmt.cpp b/backends/p4fmt/p4fmt.cpp index fccc1667c3..faf76f83f7 100644 --- a/backends/p4fmt/p4fmt.cpp +++ b/backends/p4fmt/p4fmt.cpp @@ -13,37 +13,30 @@ namespace P4::P4Fmt { -std::pair parseProgram( +std::optional> parseProgram( const ParserOptions &options) { - BUG_CHECK(&options == &P4CContext::get().options(), - "Parsing using options that don't match the current " - "compiler context"); - - std::pair result = {nullptr, nullptr}; - - if (options.doNotPreprocess) { - auto *file = fopen(options.file.c_str(), "r"); - if (file == nullptr) { - ::P4::error(ErrorType::ERR_NOT_FOUND, "%1%: No such file or directory.", options.file); - return result; - } - result = P4ParserDriver::parseProgramSources(file, options.file.string()); + auto *file = fopen(options.file.c_str(), "r"); + if (file == nullptr) { + ::P4::error(ErrorType::ERR_NOT_FOUND, "%1%: No such file or directory.", options.file); + return std::nullopt; + } + if (options.isv1()) { + ::P4::error(ErrorType::ERR_UNKNOWN, "p4fmt cannot deal with p4-14 programs"); fclose(file); - } else { - auto preprocessorResult = options.preprocess(); - if (::P4::errorCount() > 0 || !preprocessorResult.has_value()) { - return result; - } - result = P4ParserDriver::parseProgramSources(preprocessorResult.value().get(), - options.file.string()); + return std::nullopt; } + auto preprocessorResult = options.preprocess(); + auto result = + P4ParserDriver::parseProgramSources(preprocessorResult.value().get(), options.file.c_str()); + fclose(file); if (::P4::errorCount() > 0) { ::P4::error(ErrorType::ERR_OVERLIMIT, "%1% errors encountered, aborting compilation", ::P4::errorCount()); - return {nullptr, nullptr}; + return std::nullopt; } - BUG_CHECK(result.first != nullptr, "Parsing failed, but we didn't report an error"); + + BUG_CHECK(result.first != nullptr, "Parsing failed, but no error was reported"); return result; } @@ -56,12 +49,14 @@ std::stringstream getFormattedOutput(std::filesystem::path inputFile) { std::stringstream formattedOutput; - const auto &[program, sources] = parseProgram(options); - - if (program == nullptr && ::P4::errorCount() != 0) { - ::P4::error("Failed to parse P4 file."); + auto parseResult = parseProgram(options); + if (!parseResult) { + if (::P4::errorCount() > 0) { + ::P4::error("Failed to parse P4 file."); + } return formattedOutput; } + const auto &[program, sources] = *parseResult; std::unordered_map globalCommentsMap;