LLVM  9.0.0svn
Error.cpp
Go to the documentation of this file.
1 //===- Error.cpp - system_error extensions for Object -----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This defines a new error_category for the Object library.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Object/Error.h"
16 
17 using namespace llvm;
18 using namespace object;
19 
20 namespace {
21 // FIXME: This class is only here to support the transition to llvm::Error. It
22 // will be removed once this transition is complete. Clients should prefer to
23 // deal with the Error value directly, rather than converting to error_code.
24 class _object_error_category : public std::error_category {
25 public:
26  const char* name() const noexcept override;
27  std::string message(int ev) const override;
28 };
29 }
30 
31 const char *_object_error_category::name() const noexcept {
32  return "llvm.object";
33 }
34 
35 std::string _object_error_category::message(int EV) const {
36  object_error E = static_cast<object_error>(EV);
37  switch (E) {
39  return "No object file for requested architecture";
41  return "The file was not recognized as a valid object file";
43  return "Invalid data was encountered while parsing the file";
45  return "The end of the file was unexpectedly encountered";
47  return "String table must end with a null terminator";
49  return "Invalid section index";
51  return "Bitcode section not found in object file";
53  return "Invalid symbol index";
54  }
55  llvm_unreachable("An enumerator of object_error does not have a message "
56  "defined.");
57 }
58 
59 void BinaryError::anchor() {}
60 char BinaryError::ID = 0;
61 char GenericBinaryError::ID = 0;
62 
64 
66  : Msg(Msg.str()) {
67  setErrorCode(make_error_code(ECOverride));
68 }
69 
71  OS << Msg;
72 }
73 
75 
77  return *error_category;
78 }
79 
81  if (auto Err2 =
82  handleErrors(std::move(Err), [](std::unique_ptr<ECError> M) -> Error {
83  // Try to handle 'M'. If successful, return a success value from
84  // the handler.
85  if (M->convertToErrorCode() == object_error::invalid_file_type)
86  return Error::success();
87 
88  // We failed to handle 'M' - return it from the handler.
89  // This value will be passed back from catchErrors and
90  // wind up in Err2, where it will be returned from this function.
91  return Error(std::move(M));
92  }))
93  return Err2;
94  return Err;
95 }
This class represents lattice values for constants.
Definition: AllocatorList.h:23
std::error_code make_error_code(object_error e)
Definition: Error.h:39
void log(raw_ostream &OS) const override
Definition: Error.cpp:70
const std::error_category & object_category()
Definition: Error.cpp:76
Definition: BitVector.h:937
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
Error isNotObjectErrorInvalidFileType(llvm::Error Err)
isNotObjectErrorInvalidFileType() is used when looping through the children of an archive after calli...
Definition: Error.cpp:80
static char ID
Definition: Error.h:54
object_error
Definition: Error.h:27
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static ManagedStatic< _object_error_category > error_category
Definition: Error.cpp:74
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
aarch64 promote const
static const char * name
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
Error handleErrors(Error E, HandlerTs &&... Hs)
Pass the ErrorInfo(s) contained in E to their respective handlers.
Definition: Error.h:881
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:60