Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:18

0001 //===- Error.h - system_error extensions for Object -------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This declares a new error_category for the Object library.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_OBJECT_ERROR_H
0014 #define LLVM_OBJECT_ERROR_H
0015 
0016 #include "llvm/Support/Error.h"
0017 #include <system_error>
0018 
0019 namespace llvm {
0020 
0021 class Twine;
0022 
0023 namespace object {
0024 
0025 const std::error_category &object_category();
0026 
0027 enum class object_error {
0028   // Error code 0 is absent. Use std::error_code() instead.
0029   arch_not_found = 1,
0030   invalid_file_type,
0031   parse_failed,
0032   unexpected_eof,
0033   string_table_non_null_end,
0034   invalid_section_index,
0035   bitcode_section_not_found,
0036   invalid_symbol_index,
0037   section_stripped,
0038 };
0039 
0040 inline std::error_code make_error_code(object_error e) {
0041   return std::error_code(static_cast<int>(e), object_category());
0042 }
0043 
0044 /// Base class for all errors indicating malformed binary files.
0045 ///
0046 /// Having a subclass for all malformed binary files allows archive-walking
0047 /// code to skip malformed files without having to understand every possible
0048 /// way that a binary file might be malformed.
0049 ///
0050 /// Currently inherits from ECError for easy interoperability with
0051 /// std::error_code, but this will be removed in the future.
0052 class BinaryError : public ErrorInfo<BinaryError, ECError> {
0053   void anchor() override;
0054 public:
0055   static char ID;
0056   BinaryError() {
0057     // Default to parse_failed, can be overridden with setErrorCode.
0058     setErrorCode(make_error_code(object_error::parse_failed));
0059   }
0060 };
0061 
0062 /// Generic binary error.
0063 ///
0064 /// For errors that don't require their own specific sub-error (most errors)
0065 /// this class can be used to describe the error via a string message.
0066 class GenericBinaryError : public ErrorInfo<GenericBinaryError, BinaryError> {
0067 public:
0068   static char ID;
0069   GenericBinaryError(const Twine &Msg);
0070   GenericBinaryError(const Twine &Msg, object_error ECOverride);
0071   const std::string &getMessage() const { return Msg; }
0072   void log(raw_ostream &OS) const override;
0073 private:
0074   std::string Msg;
0075 };
0076 
0077 /// isNotObjectErrorInvalidFileType() is used when looping through the children
0078 /// of an archive after calling getAsBinary() on the child and it returns an
0079 /// llvm::Error.  In the cases we want to loop through the children and ignore the
0080 /// non-objects in the archive this is used to test the error to see if an
0081 /// error() function needs to called on the llvm::Error.
0082 Error isNotObjectErrorInvalidFileType(llvm::Error Err);
0083 
0084 inline Error createError(const Twine &Err) {
0085   return make_error<StringError>(Err, object_error::parse_failed);
0086 }
0087 
0088 } // end namespace object.
0089 
0090 } // end namespace llvm.
0091 
0092 namespace std {
0093 template <>
0094 struct is_error_code_enum<llvm::object::object_error> : std::true_type {};
0095 }
0096 
0097 #endif