Path: blob/main/contrib/llvm-project/llvm/lib/Object/Error.cpp
35232 views
//===- Error.cpp - system_error extensions for Object -----------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This defines a new error_category for the Object library.9//10//===----------------------------------------------------------------------===//1112#include "llvm/Object/Error.h"13#include "llvm/ADT/Twine.h"14#include "llvm/Support/ErrorHandling.h"1516using namespace llvm;17using namespace object;1819namespace {20// FIXME: This class is only here to support the transition to llvm::Error. It21// will be removed once this transition is complete. Clients should prefer to22// deal with the Error value directly, rather than converting to error_code.23class _object_error_category : public std::error_category {24public:25const char* name() const noexcept override;26std::string message(int ev) const override;27};28}2930const char *_object_error_category::name() const noexcept {31return "llvm.object";32}3334std::string _object_error_category::message(int EV) const {35object_error E = static_cast<object_error>(EV);36switch (E) {37case object_error::arch_not_found:38return "No object file for requested architecture";39case object_error::invalid_file_type:40return "The file was not recognized as a valid object file";41case object_error::parse_failed:42return "Invalid data was encountered while parsing the file";43case object_error::unexpected_eof:44return "The end of the file was unexpectedly encountered";45case object_error::string_table_non_null_end:46return "String table must end with a null terminator";47case object_error::invalid_section_index:48return "Invalid section index";49case object_error::bitcode_section_not_found:50return "Bitcode section not found in object file";51case object_error::invalid_symbol_index:52return "Invalid symbol index";53case object_error::section_stripped:54return "Section has been stripped from the object file";55}56llvm_unreachable("An enumerator of object_error does not have a message "57"defined.");58}5960void BinaryError::anchor() {}61char BinaryError::ID = 0;62char GenericBinaryError::ID = 0;6364GenericBinaryError::GenericBinaryError(const Twine &Msg) : Msg(Msg.str()) {}6566GenericBinaryError::GenericBinaryError(const Twine &Msg,67object_error ECOverride)68: Msg(Msg.str()) {69setErrorCode(make_error_code(ECOverride));70}7172void GenericBinaryError::log(raw_ostream &OS) const {73OS << Msg;74}7576const std::error_category &object::object_category() {77static _object_error_category error_category;78return error_category;79}8081llvm::Error llvm::object::isNotObjectErrorInvalidFileType(llvm::Error Err) {82return handleErrors(std::move(Err), [](std::unique_ptr<ECError> M) -> Error {83// Try to handle 'M'. If successful, return a success value from84// the handler.85if (M->convertToErrorCode() == object_error::invalid_file_type)86return Error::success();8788// We failed to handle 'M' - return it from the handler.89// This value will be passed back from catchErrors and90// wind up in Err2, where it will be returned from this function.91return Error(std::move(M));92});93}949596