blob: c1dfe673b61ed7f074bd8bb7a63434b6aaa18c8e [file] [log] [blame]
Michael J. Spencere4446922011-06-25 17:42:56 +00001//===- Error.cpp - system_error extensions for Object -----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This defines a new error_category for the Object library.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Object/Error.h"
15#include "llvm/Support/ErrorHandling.h"
Chris Bieneman684981e2014-09-19 22:09:18 +000016#include "llvm/Support/ManagedStatic.h"
Michael J. Spencere4446922011-06-25 17:42:56 +000017
18using namespace llvm;
19using namespace object;
20
21namespace {
Peter Collingbourne4718f8b2016-05-24 20:13:46 +000022// FIXME: This class is only here to support the transition to llvm::Error. It
23// will be removed once this transition is complete. Clients should prefer to
24// deal with the Error value directly, rather than converting to error_code.
Rafael Espindola25188c92014-06-12 01:45:43 +000025class _object_error_category : public std::error_category {
Michael J. Spencere4446922011-06-25 17:42:56 +000026public:
Reid Kleckner990504e2016-10-19 23:52:38 +000027 const char* name() const noexcept override;
Justin Bognere3bfdc42014-03-15 04:05:59 +000028 std::string message(int ev) const override;
Michael J. Spencere4446922011-06-25 17:42:56 +000029};
30}
31
Reid Kleckner990504e2016-10-19 23:52:38 +000032const char *_object_error_category::name() const noexcept {
Michael J. Spencere4446922011-06-25 17:42:56 +000033 return "llvm.object";
34}
35
Rafael Espindolae00fec82014-06-03 05:26:12 +000036std::string _object_error_category::message(int EV) const {
37 object_error E = static_cast<object_error>(EV);
Rafael Espindolae107ade2013-06-18 13:30:31 +000038 switch (E) {
Alexey Samsonove6388e62013-06-18 15:03:28 +000039 case object_error::arch_not_found:
40 return "No object file for requested architecture";
Michael J. Spencere4446922011-06-25 17:42:56 +000041 case object_error::invalid_file_type:
42 return "The file was not recognized as a valid object file";
43 case object_error::parse_failed:
44 return "Invalid data was encountered while parsing the file";
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000045 case object_error::unexpected_eof:
46 return "The end of the file was unexpectedly encountered";
Rafael Espindola6a1bfb22015-06-29 14:39:25 +000047 case object_error::string_table_non_null_end:
48 return "String table must end with a null terminator";
Rafael Espindola6def3042015-07-01 12:56:27 +000049 case object_error::invalid_section_index:
50 return "Invalid section index";
Peter Collingbourne10039c02014-09-18 21:28:49 +000051 case object_error::bitcode_section_not_found:
52 return "Bitcode section not found in object file";
Michael J. Spencere4446922011-06-25 17:42:56 +000053 }
Rafael Espindolae107ade2013-06-18 13:30:31 +000054 llvm_unreachable("An enumerator of object_error does not have a message "
55 "defined.");
Michael J. Spencere4446922011-06-25 17:42:56 +000056}
57
Lang Hames9e964f32016-03-25 17:25:34 +000058char BinaryError::ID = 0;
59char GenericBinaryError::ID = 0;
60
Kevin Enderbyd4e075b2016-05-06 20:16:28 +000061GenericBinaryError::GenericBinaryError(Twine Msg) : Msg(Msg.str()) {}
Lang Hames9e964f32016-03-25 17:25:34 +000062
Kevin Enderbyd4e075b2016-05-06 20:16:28 +000063GenericBinaryError::GenericBinaryError(Twine Msg, object_error ECOverride)
64 : Msg(Msg.str()) {
Lang Hames9e964f32016-03-25 17:25:34 +000065 setErrorCode(make_error_code(ECOverride));
66}
67
68void GenericBinaryError::log(raw_ostream &OS) const {
Kevin Enderbyd4e075b2016-05-06 20:16:28 +000069 OS << Msg;
Lang Hames9e964f32016-03-25 17:25:34 +000070}
71
Chris Bieneman684981e2014-09-19 22:09:18 +000072static ManagedStatic<_object_error_category> error_category;
73
Rafael Espindola25188c92014-06-12 01:45:43 +000074const std::error_category &object::object_category() {
Chris Bieneman684981e2014-09-19 22:09:18 +000075 return *error_category;
Michael J. Spencere4446922011-06-25 17:42:56 +000076}
Lang Hames8a63b2a2016-05-17 21:38:53 +000077
78llvm::Error llvm::object::isNotObjectErrorInvalidFileType(llvm::Error Err) {
79 if (auto Err2 =
80 handleErrors(std::move(Err),
81 [](std::unique_ptr<ECError> M) {
82 // Try to handle 'M'. If successful, return a success value from
83 // the handler.
84 if (M->convertToErrorCode() == object_error::invalid_file_type)
85 return Error::success();
86
87 // We failed to handle 'M' - return it from the handler.
88 // This value will be passed back from catchErrors and
89 // wind up in Err2, where it will be returned from this function.
90 return Error(std::move(M));
91 }))
92 return Err2;
93 return Err;
94}