1 //===-- TestRunner.cpp ----------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "TestRunner.h"
10 #include "ReducerWorkItem.h"
11 #include "deltas/Utils.h"
12 #include "llvm/Support/WithColor.h"
16 TestRunner::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
,
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?");
29 static constexpr std::array
<std::optional
<StringRef
>, 3> DefaultRedirects
= {
31 static constexpr std::array
<std::optional
<StringRef
>, 3> NullRedirects
;
33 /// Runs the interestingness test, passes file to be tested as first argument
34 /// and other specified test arguments after that.
35 int TestRunner::run(StringRef Filename
) const {
36 std::vector
<StringRef
> ProgramArgs
;
37 ProgramArgs
.push_back(TestName
);
39 for (const auto &Arg
: TestArgs
)
40 ProgramArgs
.push_back(Arg
);
42 ProgramArgs
.push_back(Filename
);
47 sys::ExecuteAndWait(TestName
, ProgramArgs
, /*Env=*/std::nullopt
,
48 Verbose
? DefaultRedirects
: NullRedirects
,
49 /*SecondsToWait=*/0, /*MemoryLimit=*/0, &ErrMsg
);
52 Error E
= make_error
<StringError
>("Error running interesting-ness test: " +
54 inconvertibleErrorCode());
55 WithColor::error(errs(), ToolName
) << toString(std::move(E
)) << '\n';
62 void TestRunner::writeOutput(StringRef Message
) {
64 raw_fd_ostream
Out(OutputFilename
, EC
,
65 EmitBitcode
&& !Program
->isMIR() ? sys::fs::OF_None
68 errs() << "Error opening output file: " << EC
.message() << "!\n";
72 Program
->writeOutput(Out
, EmitBitcode
);
73 errs() << Message
<< OutputFilename
<< '\n';