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 
11 using namespace llvm;
12 
13 TestRunner::TestRunner(StringRef TestName,
14                        const std::vector<std::string> &TestArgs,
15                        std::unique_ptr<ReducerWorkItem> Program,
16                        std::unique_ptr<TargetMachine> TM)
17     : TestName(TestName), TestArgs(TestArgs), Program(std::move(Program)),
18       TM(std::move(TM)) {
19   assert(this->Program && "Initialized with null program?");
20 }
21 
22 /// Runs the interestingness test, passes file to be tested as first argument
23 /// and other specified test arguments after that.
24 int TestRunner::run(StringRef Filename) {
25   std::vector<StringRef> ProgramArgs;
26   ProgramArgs.push_back(TestName);
27 
28   for (const auto &Arg : TestArgs)
29     ProgramArgs.push_back(Arg);
30 
31   ProgramArgs.push_back(Filename);
32 
33   std::string ErrMsg;
34   int Result = sys::ExecuteAndWait(
35       TestName, ProgramArgs, /*Env=*/None, /*Redirects=*/None,
36       /*SecondsToWait=*/0, /*MemoryLimit=*/0, &ErrMsg);
37 
38   if (Result < 0) {
39     Error E = make_error<StringError>("Error running interesting-ness test: " +
40                                           ErrMsg,
41                                       inconvertibleErrorCode());
42     errs() << toString(std::move(E));
43     exit(1);
44   }
45 
46   return !Result;
47 }
48