BinaryStreamError.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //===- BinaryStreamError.cpp - Error extensions for streams -----*- C++ -*-===//
  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 "llvm/Support/BinaryStreamError.h"
  9. #include "llvm/Support/raw_ostream.h"
  10. using namespace llvm;
  11. char BinaryStreamError::ID = 0;
  12. BinaryStreamError::BinaryStreamError(stream_error_code C)
  13. : BinaryStreamError(C, "") {}
  14. BinaryStreamError::BinaryStreamError(StringRef Context)
  15. : BinaryStreamError(stream_error_code::unspecified, Context) {}
  16. BinaryStreamError::BinaryStreamError(stream_error_code C, StringRef Context)
  17. : Code(C) {
  18. ErrMsg = "Stream Error: ";
  19. switch (C) {
  20. case stream_error_code::unspecified:
  21. ErrMsg += "An unspecified error has occurred.";
  22. break;
  23. case stream_error_code::stream_too_short:
  24. ErrMsg += "The stream is too short to perform the requested operation.";
  25. break;
  26. case stream_error_code::invalid_array_size:
  27. ErrMsg += "The buffer size is not a multiple of the array element size.";
  28. break;
  29. case stream_error_code::invalid_offset:
  30. ErrMsg += "The specified offset is invalid for the current stream.";
  31. break;
  32. case stream_error_code::filesystem_error:
  33. ErrMsg += "An I/O error occurred on the file system.";
  34. break;
  35. }
  36. if (!Context.empty()) {
  37. ErrMsg += " ";
  38. ErrMsg += Context;
  39. }
  40. }
  41. void BinaryStreamError::log(raw_ostream &OS) const { OS << ErrMsg; }
  42. StringRef BinaryStreamError::getErrorMessage() const { return ErrMsg; }
  43. std::error_code BinaryStreamError::convertToErrorCode() const {
  44. return inconvertibleErrorCode();
  45. }