Error.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. const std::error_category &object_category();
  25. enum class object_error {
  26. // Error code 0 is absent. Use std::error_code() instead.
  27. arch_not_found = 1,
  28. invalid_file_type,
  29. parse_failed,
  30. unexpected_eof,
  31. string_table_non_null_end,
  32. invalid_section_index,
  33. bitcode_section_not_found,
  34. invalid_symbol_index,
  35. };
  36. inline std::error_code make_error_code(object_error e) {
  37. return std::error_code(static_cast<int>(e), object_category());
  38. }
  39. /// Base class for all errors indicating malformed binary files.
  40. ///
  41. /// Having a subclass for all malformed binary files allows archive-walking
  42. /// code to skip malformed files without having to understand every possible
  43. /// way that a binary file might be malformed.
  44. ///
  45. /// Currently inherits from ECError for easy interoperability with
  46. /// std::error_code, but this will be removed in the future.
  47. class BinaryError : public ErrorInfo<BinaryError, ECError> {
  48. void anchor() override;
  49. public:
  50. static char ID;
  51. BinaryError() {
  52. // Default to parse_failed, can be overridden with setErrorCode.
  53. setErrorCode(make_error_code(object_error::parse_failed));
  54. }
  55. };
  56. /// Generic binary error.
  57. ///
  58. /// For errors that don't require their own specific sub-error (most errors)
  59. /// this class can be used to describe the error via a string message.
  60. class GenericBinaryError : public ErrorInfo<GenericBinaryError, BinaryError> {
  61. public:
  62. static char ID;
  63. GenericBinaryError(const Twine &Msg);
  64. GenericBinaryError(const Twine &Msg, object_error ECOverride);
  65. const std::string &getMessage() const { return Msg; }
  66. void log(raw_ostream &OS) const override;
  67. private:
  68. std::string Msg;
  69. };
  70. /// isNotObjectErrorInvalidFileType() is used when looping through the children
  71. /// of an archive after calling getAsBinary() on the child and it returns an
  72. /// llvm::Error. In the cases we want to loop through the children and ignore the
  73. /// non-objects in the archive this is used to test the error to see if an
  74. /// error() function needs to called on the llvm::Error.
  75. Error isNotObjectErrorInvalidFileType(llvm::Error Err);
  76. inline Error createError(const Twine &Err) {
  77. return make_error<StringError>(Err, object_error::parse_failed);
  78. }
  79. } // end namespace object.
  80. } // end namespace llvm.
  81. namespace std {
  82. template <>
  83. struct is_error_code_enum<llvm::object::object_error> : std::true_type {};
  84. }
  85. #endif
  86. #ifdef __GNUC__
  87. #pragma GCC diagnostic pop
  88. #endif