1 //===- Error.cpp - system_error extensions for lld --------------*- C++ -*-===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lld/Core/Error.h"
11 #include "llvm/ADT/Twine.h"
12 #include "llvm/Support/ErrorHandling.h"
19 class _YamlReaderErrorCategory : public std::error_category {
21 const char* name() const LLVM_NOEXCEPT override {
22 return "lld.yaml.reader";
25 std::string message(int ev) const override {
26 switch (static_cast<YamlReaderError>(ev)) {
27 case YamlReaderError::unknown_keyword:
28 return "Unknown keyword found in yaml file";
29 case YamlReaderError::illegal_value:
30 return "Bad value found in yaml file";
32 llvm_unreachable("An enumerator of YamlReaderError does not have a "
37 const std::error_category &lld::YamlReaderCategory() {
38 static _YamlReaderErrorCategory o;
44 /// Temporary class to enable make_dynamic_error_code() until
45 /// llvm::ErrorOr<> is updated to work with error encapsulations
46 /// other than error_code.
47 class dynamic_error_category : public std::error_category {
49 ~dynamic_error_category() override = default;
51 const char *name() const LLVM_NOEXCEPT override {
52 return "lld.dynamic_error";
55 std::string message(int ev) const override {
57 assert(ev < (int)_messages.size());
58 // The value is an index into the string vector.
62 int add(std::string msg) {
63 std::lock_guard<std::recursive_mutex> lock(_mutex);
64 // Value zero is always the successs value.
65 if (_messages.empty())
66 _messages.push_back("Success");
67 _messages.push_back(msg);
68 // Return the index of the string just appended.
69 return _messages.size() - 1;
73 std::vector<std::string> _messages;
74 std::recursive_mutex _mutex;
77 static dynamic_error_category categorySingleton;
79 std::error_code make_dynamic_error_code(StringRef msg) {
80 return std::error_code(categorySingleton.add(msg), categorySingleton);
83 char GenericError::ID = 0;
85 GenericError::GenericError(Twine Msg) : Msg(Msg.str()) { }
87 void GenericError::log(raw_ostream &OS) const {