TestRunner.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #include "TestRunner.h"
  9. #include "ReducerWorkItem.h"
  10. #include "deltas/Utils.h"
  11. #include "llvm/Support/WithColor.h"
  12. using namespace llvm;
  13. TestRunner::TestRunner(StringRef TestName,
  14. const std::vector<std::string> &TestArgs,
  15. std::unique_ptr<ReducerWorkItem> Program,
  16. std::unique_ptr<TargetMachine> TM, StringRef ToolName,
  17. StringRef OutputName, bool InputIsBitcode,
  18. bool OutputBitcode)
  19. : TestName(TestName), ToolName(ToolName), TestArgs(TestArgs),
  20. Program(std::move(Program)), TM(std::move(TM)),
  21. OutputFilename(OutputName), InputIsBitcode(InputIsBitcode),
  22. EmitBitcode(OutputBitcode) {
  23. assert(this->Program && "Initialized with null program?");
  24. }
  25. static constexpr std::array<std::optional<StringRef>, 3> DefaultRedirects = {
  26. StringRef()};
  27. static constexpr std::array<std::optional<StringRef>, 3> NullRedirects;
  28. /// Runs the interestingness test, passes file to be tested as first argument
  29. /// and other specified test arguments after that.
  30. int TestRunner::run(StringRef Filename) const {
  31. std::vector<StringRef> ProgramArgs;
  32. ProgramArgs.push_back(TestName);
  33. for (const auto &Arg : TestArgs)
  34. ProgramArgs.push_back(Arg);
  35. ProgramArgs.push_back(Filename);
  36. std::string ErrMsg;
  37. int Result =
  38. sys::ExecuteAndWait(TestName, ProgramArgs, /*Env=*/std::nullopt,
  39. Verbose ? DefaultRedirects : NullRedirects,
  40. /*SecondsToWait=*/0, /*MemoryLimit=*/0, &ErrMsg);
  41. if (Result < 0) {
  42. Error E = make_error<StringError>("Error running interesting-ness test: " +
  43. ErrMsg,
  44. inconvertibleErrorCode());
  45. WithColor::error(errs(), ToolName) << toString(std::move(E)) << '\n';
  46. exit(1);
  47. }
  48. return !Result;
  49. }
  50. void TestRunner::writeOutput(StringRef Message) {
  51. std::error_code EC;
  52. raw_fd_ostream Out(OutputFilename, EC,
  53. EmitBitcode && !Program->isMIR() ? sys::fs::OF_None
  54. : sys::fs::OF_Text);
  55. if (EC) {
  56. errs() << "Error opening output file: " << EC.message() << "!\n";
  57. exit(1);
  58. }
  59. Program->writeOutput(Out, EmitBitcode);
  60. errs() << Message << OutputFilename << '\n';
  61. }