1//===-- TestRunner.cpp ----------------------------------------------------===// 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 "TestRunner.h" 10#include "ReducerWorkItem.h" 11#include "deltas/Utils.h" 12#include "llvm/Support/WithColor.h" 13 14using namespace llvm; 15 16TestRunner::TestRunner(StringRef TestName, 17 const std::vector<std::string> &TestArgs, 18 std::unique_ptr<ReducerWorkItem> Program, 19 std::unique_ptr<TargetMachine> TM, StringRef ToolName, 20 StringRef OutputName, bool InputIsBitcode, 21 bool OutputBitcode) 22 : TestName(TestName), ToolName(ToolName), TestArgs(TestArgs), 23 Program(std::move(Program)), TM(std::move(TM)), 24 OutputFilename(OutputName), InputIsBitcode(InputIsBitcode), 25 EmitBitcode(OutputBitcode) { 26 assert(this->Program && "Initialized with null program?"); 27} 28 29static constexpr std::array<std::optional<StringRef>, 3> DefaultRedirects = { 30 StringRef()}; 31static constexpr std::array<std::optional<StringRef>, 3> NullRedirects; 32 33/// Runs the interestingness test, passes file to be tested as first argument 34/// and other specified test arguments after that. 35int TestRunner::run(StringRef Filename) const { 36 std::vector<StringRef> ProgramArgs; 37 ProgramArgs.push_back(TestName); 38 39 for (const auto &Arg : TestArgs) 40 ProgramArgs.push_back(Arg); 41 42 ProgramArgs.push_back(Filename); 43 44 std::string ErrMsg; 45 46 int Result = 47 sys::ExecuteAndWait(TestName, ProgramArgs, /*Env=*/std::nullopt, 48 Verbose ? DefaultRedirects : NullRedirects, 49 /*SecondsToWait=*/0, /*MemoryLimit=*/0, &ErrMsg); 50 51 if (Result < 0) { 52 Error E = make_error<StringError>("Error running interesting-ness test: " + 53 ErrMsg, 54 inconvertibleErrorCode()); 55 WithColor::error(errs(), ToolName) << toString(std::move(E)) << '\n'; 56 exit(1); 57 } 58 59 return !Result; 60} 61 62void TestRunner::writeOutput(StringRef Message) { 63 std::error_code EC; 64 raw_fd_ostream Out(OutputFilename, EC, 65 EmitBitcode && !Program->isMIR() ? sys::fs::OF_None 66 : sys::fs::OF_Text); 67 if (EC) { 68 errs() << "Error opening output file: " << EC.message() << "!\n"; 69 exit(1); 70 } 71 72 Program->writeOutput(Out, EmitBitcode); 73 errs() << Message << OutputFilename << '\n'; 74} 75