Error.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Error.h - system_error extensions for Object -------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This declares a new error_category for the Object library.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_OBJECT_ERROR_H
  18. #define LLVM_OBJECT_ERROR_H
  19. #include "llvm/Support/Error.h"
  20. #include <system_error>
  21. namespace llvm {
  22. class Twine;
  23. namespace object {
  24. class Binary;
  25. const std::error_category &object_category();
  26. enum class object_error {
  27. // Error code 0 is absent. Use std::error_code() instead.
  28. arch_not_found = 1,
  29. invalid_file_type,
  30. parse_failed,
  31. unexpected_eof,
  32. string_table_non_null_end,
  33. invalid_section_index,
  34. bitcode_section_not_found,
  35. invalid_symbol_index,
  36. };
  37. inline std::error_code make_error_code(object_error e) {
  38. return std::error_code(static_cast<int>(e), object_category());
  39. }
  40. /// Base class for all errors indicating malformed binary files.
  41. ///
  42. /// Having a subclass for all malformed binary files allows archive-walking
  43. /// code to skip malformed files without having to understand every possible
  44. /// way that a binary file might be malformed.
  45. ///
  46. /// Currently inherits from ECError for easy interoperability with
  47. /// std::error_code, but this will be removed in the future.
  48. class BinaryError : public ErrorInfo<BinaryError, ECError> {
  49. void anchor() override;
  50. public:
  51. static char ID;
  52. BinaryError() {
  53. // Default to parse_failed, can be overridden with setErrorCode.
  54. setErrorCode(make_error_code(object_error::parse_failed));
  55. }
  56. };
  57. /// Generic binary error.
  58. ///
  59. /// For errors that don't require their own specific sub-error (most errors)
  60. /// this class can be used to describe the error via a string message.
  61. class GenericBinaryError : public ErrorInfo<GenericBinaryError, BinaryError> {
  62. public:
  63. static char ID;
  64. GenericBinaryError(const Twine &Msg);
  65. GenericBinaryError(const Twine &Msg, object_error ECOverride);
  66. const std::string &getMessage() const { return Msg; }
  67. void log(raw_ostream &OS) const override;
  68. private:
  69. std::string Msg;
  70. };
  71. /// isNotObjectErrorInvalidFileType() is used when looping through the children
  72. /// of an archive after calling getAsBinary() on the child and it returns an
  73. /// llvm::Error. In the cases we want to loop through the children and ignore the
  74. /// non-objects in the archive this is used to test the error to see if an
  75. /// error() function needs to called on the llvm::Error.
  76. Error isNotObjectErrorInvalidFileType(llvm::Error Err);
  77. } // end namespace object.
  78. } // end namespace llvm.
  79. namespace std {
  80. template <>
  81. struct is_error_code_enum<llvm::object::object_error> : std::true_type {};
  82. }
  83. #endif
  84. #ifdef __GNUC__
  85. #pragma GCC diagnostic pop
  86. #endif