1 //===- Error.cpp - system_error extensions for lld --------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lld/Core/Error.h"
10 #include "llvm/ADT/Twine.h"
11 #include "llvm/Support/ErrorHandling.h"
19 class _YamlReaderErrorCategory : public std::error_category {
21 const char* name() const 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 "
36 } // end anonymous namespace
38 const std::error_category &lld::YamlReaderCategory() {
39 static _YamlReaderErrorCategory o;
45 /// Temporary class to enable make_dynamic_error_code() until
46 /// llvm::ErrorOr<> is updated to work with error encapsulations
47 /// other than error_code.
48 class dynamic_error_category : public std::error_category {
50 ~dynamic_error_category() override = default;
52 const char *name() const noexcept override {
53 return "lld.dynamic_error";
56 std::string message(int ev) const override {
58 assert(ev < (int)_messages.size());
59 // The value is an index into the string vector.
63 int add(std::string msg) {
64 std::lock_guard<std::recursive_mutex> lock(_mutex);
65 // Value zero is always the successs value.
66 if (_messages.empty())
67 _messages.push_back("Success");
68 _messages.push_back(msg);
69 // Return the index of the string just appended.
70 return _messages.size() - 1;
74 std::vector<std::string> _messages;
75 std::recursive_mutex _mutex;
78 static dynamic_error_category categorySingleton;
80 std::error_code make_dynamic_error_code(StringRef msg) {
81 return std::error_code(categorySingleton.add(msg), categorySingleton);
84 char GenericError::ID = 0;
86 GenericError::GenericError(Twine Msg) : Msg(Msg.str()) { }
88 void GenericError::log(raw_ostream &OS) const {