xref: /llvm-project/llvm/tools/llvm-reduce/TestRunner.cpp (revision 3e0d37fd79faeb60a47ed8aace1c594ff7f637f2)
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 
14 using namespace llvm;
15 
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,
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 
30 static constexpr std::array<std::optional<StringRef>, 3> DefaultRedirects = {
31     StringRef()};
32 static 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.
36 int 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>("Error running interesting-ness test: " +
49                                           ErrMsg,
50                                       inconvertibleErrorCode());
51     WithColor::error(errs(), ToolName) << toString(std::move(E)) << '\n';
52     exit(1);
53   }
54 
55   return !Result;
56 }
57 
58 void TestRunner::writeOutput(StringRef Message) {
59   std::error_code EC;
60   raw_fd_ostream Out(OutputFilename, EC,
61                      EmitBitcode && !Program->isMIR() ? sys::fs::OF_None
62                                                       : sys::fs::OF_Text);
63   if (EC) {
64     errs() << "Error opening output file: " << EC.message() << "!\n";
65     exit(1);
66   }
67 
68   Program->writeOutput(Out, EmitBitcode);
69   errs() << Message << OutputFilename << '\n';
70 }
71