RemarkFormat.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //===- RemarkFormat.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. // Implementation of utilities to handle the different remark formats.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Remarks/RemarkFormat.h"
  13. #include "llvm/ADT/StringSwitch.h"
  14. #include "llvm/Remarks/BitstreamRemarkContainer.h"
  15. using namespace llvm;
  16. using namespace llvm::remarks;
  17. Expected<Format> llvm::remarks::parseFormat(StringRef FormatStr) {
  18. auto Result = StringSwitch<Format>(FormatStr)
  19. .Cases("", "yaml", Format::YAML)
  20. .Case("yaml-strtab", Format::YAMLStrTab)
  21. .Case("bitstream", Format::Bitstream)
  22. .Default(Format::Unknown);
  23. if (Result == Format::Unknown)
  24. return createStringError(std::make_error_code(std::errc::invalid_argument),
  25. "Unknown remark format: '%s'",
  26. FormatStr.data());
  27. return Result;
  28. }
  29. Expected<Format> llvm::remarks::magicToFormat(StringRef MagicStr) {
  30. auto Result =
  31. StringSwitch<Format>(MagicStr)
  32. .StartsWith("--- ", Format::YAML) // This is only an assumption.
  33. .StartsWith(remarks::Magic, Format::YAMLStrTab)
  34. .StartsWith(remarks::ContainerMagic, Format::Bitstream)
  35. .Default(Format::Unknown);
  36. if (Result == Format::Unknown)
  37. return createStringError(std::make_error_code(std::errc::invalid_argument),
  38. "Unknown remark magic: '%s'", MagicStr.data());
  39. return Result;
  40. }