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
, ArrayRef
<std::string
> RawTestArgs
,
17 std::unique_ptr
<ReducerWorkItem
> Program
,
18 std::unique_ptr
<TargetMachine
> TM
, StringRef ToolName
,
19 StringRef OutputName
, bool InputIsBitcode
,
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?");
26 TestArgs
.push_back(TestName
); // argv[0]
27 TestArgs
.append(RawTestArgs
.begin(), RawTestArgs
.end());
30 static constexpr std::array
<std::optional
<StringRef
>, 3> DefaultRedirects
= {
32 static constexpr std::array
<std::optional
<StringRef
>, 3> NullRedirects
;
34 /// Runs the interestingness test, passes file to be tested as first argument
35 /// and other specified test arguments after that.
36 int TestRunner::run(StringRef Filename
) const {
37 SmallVector
<StringRef
> ExecArgs(TestArgs
);
38 ExecArgs
.push_back(Filename
);
43 sys::ExecuteAndWait(TestName
, ExecArgs
, /*Env=*/std::nullopt
,
44 Verbose
? DefaultRedirects
: NullRedirects
,
45 /*SecondsToWait=*/0, /*MemoryLimit=*/0, &ErrMsg
);
48 Error E
= make_error
<StringError
>("Error running interesting-ness test: " +
50 inconvertibleErrorCode());
51 WithColor::error(errs(), ToolName
) << toString(std::move(E
)) << '\n';
58 void TestRunner::writeOutput(StringRef Message
) {
60 raw_fd_ostream
Out(OutputFilename
, EC
,
61 EmitBitcode
&& !Program
->isMIR() ? sys::fs::OF_None
64 errs() << "Error opening output file: " << EC
.message() << "!\n";
68 Program
->writeOutput(Out
, EmitBitcode
);
69 errs() << Message
<< OutputFilename
<< '\n';