|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include <algorithm> |
| 10 | +#include <fstream> |
| 11 | +#include <functional> |
| 12 | +#include <iostream> |
| 13 | +#include <iterator> |
| 14 | +#include <ranges> |
| 15 | +#include <string> |
| 16 | + |
| 17 | +using namespace std::string_view_literals; |
| 18 | + |
| 19 | +enum class Result { |
| 20 | + success, |
| 21 | + mismatch, |
| 22 | + no_match_found, |
| 23 | + unknown_matcher, |
| 24 | + invalid_use, |
| 25 | +}; |
| 26 | + |
| 27 | +namespace co { |
| 28 | +namespace { |
| 29 | +[[noreturn]] void exit(Result result) { std::exit(static_cast<int>(result)); } |
| 30 | + |
| 31 | +[[noreturn]] void print_failure(int line, std::string_view stdin_content, std::string_view matcher) { |
| 32 | + std::cout << "Failed to match: `" << matcher << "`\nRemaining data:\n" << stdin_content << '\n'; |
| 33 | + co::exit(Result::mismatch); |
| 34 | +} |
| 35 | + |
| 36 | +bool is_newline(char c) { return c == '\n'; } |
| 37 | + |
| 38 | +bool isblank(char c) { return std::isblank(c); } |
| 39 | + |
| 40 | +bool consume_front(std::string_view& sv, std::string_view start) { |
| 41 | + if (!sv.starts_with(start)) |
| 42 | + return false; |
| 43 | + sv.remove_prefix(start.size()); |
| 44 | + return true; |
| 45 | +} |
| 46 | +} // namespace |
| 47 | +} // namespace co |
| 48 | + |
| 49 | +int main(int argc, char** argv) { |
| 50 | + if (argc != 2) { |
| 51 | + std::cerr << "check_output has to be used as `<command> | ./check_output %s`\n"; |
| 52 | + co::exit(Result::invalid_use); |
| 53 | + } |
| 54 | + |
| 55 | + std::string file_content_data = [&] { |
| 56 | + std::ifstream file(argv[1]); |
| 57 | + if (!file) { |
| 58 | + std::cerr << "Failed to open file: " << argv[1] << '\n'; |
| 59 | + co::exit(Result::invalid_use); |
| 60 | + } |
| 61 | + return std::string{std::istreambuf_iterator<char>{file}, {}}; |
| 62 | + }(); |
| 63 | + std::string_view file_content = file_content_data; // Don't copy the data around all the time |
| 64 | + |
| 65 | + std::string stdin_content_data = [&] { |
| 66 | + std::cin >> std::noskipws; |
| 67 | + return std::string{std::istream_iterator<char>{std::cin}, {}}; |
| 68 | + }(); |
| 69 | + std::string_view stdin_content = stdin_content_data; // Don't copy the data around all the time |
| 70 | + |
| 71 | + size_t match_count = 0; |
| 72 | + auto drop_blanks = std::views::drop_while(co::isblank); |
| 73 | + |
| 74 | + while (!file_content.empty()) { |
| 75 | + auto marker = std::ranges::search(file_content, "// CHECK"sv); |
| 76 | + if (marker.empty()) { |
| 77 | + if (match_count == 0) { |
| 78 | + std::cerr << "No matcher found!\n"; |
| 79 | + co::exit(Result::no_match_found); |
| 80 | + } |
| 81 | + co::exit(Result::success); |
| 82 | + } |
| 83 | + file_content.remove_prefix(marker.end() - file_content.begin()); |
| 84 | + |
| 85 | + const auto get_match = [&]() { |
| 86 | + return std::string_view(file_content.begin(), std::ranges::find(file_content, '\n')); |
| 87 | + }; |
| 88 | + |
| 89 | + if (co::consume_front(file_content, ":")) { |
| 90 | + auto match = get_match(); |
| 91 | + auto found = std::ranges::search( |
| 92 | + stdin_content | std::views::drop_while(std::not_fn(co::is_newline)) | std::views::drop(1), |
| 93 | + match | drop_blanks); |
| 94 | + if (found.empty()) { |
| 95 | + co::print_failure(1, stdin_content, match); |
| 96 | + } |
| 97 | + ++match_count; |
| 98 | + stdin_content.remove_prefix(found.end() - stdin_content.begin()); |
| 99 | + } else if (co::consume_front(file_content, "-SAME:")) { |
| 100 | + auto match = get_match(); |
| 101 | + auto haystack = std::string_view(stdin_content.begin(), std::ranges::find(stdin_content, '\n')); |
| 102 | + auto found = std::ranges::search(haystack, match | drop_blanks); |
| 103 | + if (found.empty()) { |
| 104 | + co::print_failure(1, stdin_content, match); |
| 105 | + } |
| 106 | + stdin_content.remove_prefix(found.end() - stdin_content.begin()); |
| 107 | + } else if (co::consume_front(file_content, "-NEXT:")) { |
| 108 | + auto match = get_match(); |
| 109 | + auto haystack = [&] { |
| 110 | + auto begin = std::ranges::find(stdin_content, '\n'); |
| 111 | + if (begin == stdin_content.end()) { |
| 112 | + co::print_failure(1, stdin_content, match); |
| 113 | + } |
| 114 | + ++begin; |
| 115 | + return std::string_view(begin, std::ranges::find(begin, stdin_content.end(), '\n')); |
| 116 | + }(); |
| 117 | + auto found = std::ranges::search(haystack, match | drop_blanks); |
| 118 | + if (found.empty()) |
| 119 | + co::print_failure(1, stdin_content, match); |
| 120 | + stdin_content.remove_prefix(found.end() - stdin_content.begin()); |
| 121 | + } else { |
| 122 | + std::cerr << "Unkown matcher type " |
| 123 | + << std::string_view(file_content.begin(), std::ranges::find(file_content, ':')) << " found\n"; |
| 124 | + co::exit(Result::unknown_matcher); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + co::exit(Result::success); |
| 129 | +} |
0 commit comments