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, 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(Elt: TestName); // argv[0]
27 TestArgs.append(in_start: RawTestArgs.begin(), in_end: 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 argument
35/// and other specified test arguments after that.
36int TestRunner::run(StringRef Filename) const {
37 SmallVector<StringRef> ExecArgs(TestArgs);
38 ExecArgs.push_back(Elt: Filename);
39
40 std::string ErrMsg;
41
42 int Result =
43 sys::ExecuteAndWait(Program: TestName, Args: ExecArgs, /*Env=*/std::nullopt,
44 Redirects: Verbose ? DefaultRedirects : NullRedirects,
45 /*SecondsToWait=*/0, /*MemoryLimit=*/0, ErrMsg: &ErrMsg);
46
47 if (Result < 0) {
48 Error E = make_error<StringError>(
49 Args: "running interesting-ness test: " + ErrMsg, Args: inconvertibleErrorCode());
50 WithColor::error(OS&: errs(), Prefix: ToolName) << toString(E: std::move(E)) << '\n';
51 exit(status: 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_None
61 : sys::fs::OF_Text);
62 if (EC) {
63 WithColor::error(OS&: errs(), Prefix: ToolName)
64 << "opening output file: " << EC.message() << '\n';
65 exit(status: 1);
66 }
67
68 Program->writeOutput(OS&: Out, EmitBitcode);
69 errs() << Message << OutputFilename << '\n';
70}
71