Reproducer.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //===- Reproducer.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 "Reproducer.h"
  9. #include "llvm/Support/Path.h"
  10. using namespace llvm;
  11. using namespace llvm::dsymutil;
  12. static std::string createReproducerDir(std::error_code &EC) {
  13. SmallString<128> Root;
  14. if (const char *Path = getenv("DSYMUTIL_REPRODUCER_PATH")) {
  15. Root.assign(Path);
  16. EC = sys::fs::create_directory(Root);
  17. } else {
  18. EC = sys::fs::createUniqueDirectory("dsymutil", Root);
  19. }
  20. return EC ? "" : std::string(Root);
  21. }
  22. Reproducer::Reproducer() : VFS(vfs::getRealFileSystem()) {}
  23. Reproducer::~Reproducer() = default;
  24. ReproducerGenerate::ReproducerGenerate(std::error_code &EC)
  25. : Root(createReproducerDir(EC)) {
  26. if (!Root.empty())
  27. FC = std::make_shared<FileCollector>(Root, Root);
  28. VFS = FileCollector::createCollectorVFS(vfs::getRealFileSystem(), FC);
  29. }
  30. ReproducerGenerate::~ReproducerGenerate() {
  31. if (!FC)
  32. return;
  33. FC->copyFiles(false);
  34. SmallString<128> Mapping(Root);
  35. sys::path::append(Mapping, "mapping.yaml");
  36. FC->writeMapping(Mapping.str());
  37. outs() << "reproducer written to " << Root << '\n';
  38. }
  39. ReproducerUse::~ReproducerUse() = default;
  40. ReproducerUse::ReproducerUse(StringRef Root, std::error_code &EC) {
  41. SmallString<128> Mapping(Root);
  42. sys::path::append(Mapping, "mapping.yaml");
  43. ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
  44. vfs::getRealFileSystem()->getBufferForFile(Mapping.str());
  45. if (!Buffer) {
  46. EC = Buffer.getError();
  47. return;
  48. }
  49. VFS = llvm::vfs::getVFSFromYAML(std::move(Buffer.get()), nullptr, Mapping);
  50. }
  51. llvm::Expected<std::unique_ptr<Reproducer>>
  52. Reproducer::createReproducer(ReproducerMode Mode, StringRef Root) {
  53. switch (Mode) {
  54. case ReproducerMode::Generate: {
  55. std::error_code EC;
  56. std::unique_ptr<Reproducer> Repro =
  57. std::make_unique<ReproducerGenerate>(EC);
  58. if (EC)
  59. return errorCodeToError(EC);
  60. return std::move(Repro);
  61. }
  62. case ReproducerMode::Use: {
  63. std::error_code EC;
  64. std::unique_ptr<Reproducer> Repro =
  65. std::make_unique<ReproducerUse>(Root, EC);
  66. if (EC)
  67. return errorCodeToError(EC);
  68. return std::move(Repro);
  69. }
  70. case ReproducerMode::Off:
  71. return std::make_unique<Reproducer>();
  72. }
  73. llvm_unreachable("All cases handled above.");
  74. }