71 lines · cpp
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-exception6//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, ArrayRef<std::string> RawTestArgs,17 std::unique_ptr<ReducerWorkItem> Program,18 std::unique_ptr<TargetMachine> TM, StringRef ToolName,19 StringRef OutputName, bool InputIsBitcode,20 bool OutputBitcode)21 : TestName(TestName), ToolName(ToolName), Program(std::move(Program)),22 TM(std::move(TM)), OutputFilename(OutputName),23 InputIsBitcode(InputIsBitcode), EmitBitcode(OutputBitcode) {24 assert(this->Program && "Initialized with null program?");25 26 TestArgs.push_back(TestName); // argv[0]27 TestArgs.append(RawTestArgs.begin(), RawTestArgs.end());28}29 30static constexpr std::array<std::optional<StringRef>, 3> DefaultRedirects = {31 StringRef()};32static constexpr std::array<std::optional<StringRef>, 3> NullRedirects;33 34/// Runs the interestingness test, passes file to be tested as first argument35/// and other specified test arguments after that.36int TestRunner::run(StringRef Filename) const {37 SmallVector<StringRef> ExecArgs(TestArgs);38 ExecArgs.push_back(Filename);39 40 std::string ErrMsg;41 42 int Result =43 sys::ExecuteAndWait(TestName, ExecArgs, /*Env=*/std::nullopt,44 Verbose ? DefaultRedirects : NullRedirects,45 /*SecondsToWait=*/0, /*MemoryLimit=*/0, &ErrMsg);46 47 if (Result < 0) {48 Error E = make_error<StringError>(49 "running interesting-ness test: " + ErrMsg, inconvertibleErrorCode());50 WithColor::error(errs(), ToolName) << toString(std::move(E)) << '\n';51 exit(1);52 }53 54 return !Result;55}56 57void TestRunner::writeOutput(StringRef Message) {58 std::error_code EC;59 raw_fd_ostream Out(OutputFilename, EC,60 EmitBitcode && !Program->isMIR() ? sys::fs::OF_None61 : sys::fs::OF_Text);62 if (EC) {63 WithColor::error(errs(), ToolName)64 << "opening output file: " << EC.message() << '\n';65 exit(1);66 }67 68 Program->writeOutput(Out, EmitBitcode);69 errs() << Message << OutputFilename << '\n';70}71